eventfd.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * kvm eventfd support - use eventfd objects to signal various KVM events
  4. *
  5. * Copyright 2009 Novell. All Rights Reserved.
  6. * Copyright 2010 Red Hat, Inc. and/or its affiliates.
  7. *
  8. * Author:
  9. * Gregory Haskins <ghaskins@novell.com>
  10. */
  11. #include <linux/kvm_host.h>
  12. #include <linux/kvm.h>
  13. #include <linux/kvm_irqfd.h>
  14. #include <linux/workqueue.h>
  15. #include <linux/syscalls.h>
  16. #include <linux/wait.h>
  17. #include <linux/poll.h>
  18. #include <linux/file.h>
  19. #include <linux/list.h>
  20. #include <linux/eventfd.h>
  21. #include <linux/kernel.h>
  22. #include <linux/srcu.h>
  23. #include <linux/slab.h>
  24. #include <linux/seqlock.h>
  25. #include <linux/irqbypass.h>
  26. #include <trace/events/kvm.h>
  27. #include <kvm/iodev.h>
  28. #ifdef CONFIG_HAVE_KVM_IRQCHIP
  29. static struct workqueue_struct *irqfd_cleanup_wq;
  30. bool __attribute__((weak))
  31. kvm_arch_irqfd_allowed(struct kvm *kvm, struct kvm_irqfd *args)
  32. {
  33. return true;
  34. }
  35. static void
  36. irqfd_inject(struct work_struct *work)
  37. {
  38. struct kvm_kernel_irqfd *irqfd =
  39. container_of(work, struct kvm_kernel_irqfd, inject);
  40. struct kvm *kvm = irqfd->kvm;
  41. if (!irqfd->resampler) {
  42. kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, irqfd->gsi, 1,
  43. false);
  44. kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, irqfd->gsi, 0,
  45. false);
  46. } else
  47. kvm_set_irq(kvm, KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID,
  48. irqfd->gsi, 1, false);
  49. }
  50. static void irqfd_resampler_notify(struct kvm_kernel_irqfd_resampler *resampler)
  51. {
  52. struct kvm_kernel_irqfd *irqfd;
  53. list_for_each_entry_srcu(irqfd, &resampler->list, resampler_link,
  54. srcu_read_lock_held(&resampler->kvm->irq_srcu))
  55. eventfd_signal(irqfd->resamplefd);
  56. }
  57. /*
  58. * Since resampler irqfds share an IRQ source ID, we de-assert once
  59. * then notify all of the resampler irqfds using this GSI. We can't
  60. * do multiple de-asserts or we risk racing with incoming re-asserts.
  61. */
  62. static void
  63. irqfd_resampler_ack(struct kvm_irq_ack_notifier *kian)
  64. {
  65. struct kvm_kernel_irqfd_resampler *resampler;
  66. struct kvm *kvm;
  67. int idx;
  68. resampler = container_of(kian,
  69. struct kvm_kernel_irqfd_resampler, notifier);
  70. kvm = resampler->kvm;
  71. kvm_set_irq(kvm, KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID,
  72. resampler->notifier.gsi, 0, false);
  73. idx = srcu_read_lock(&kvm->irq_srcu);
  74. irqfd_resampler_notify(resampler);
  75. srcu_read_unlock(&kvm->irq_srcu, idx);
  76. }
  77. static void
  78. irqfd_resampler_shutdown(struct kvm_kernel_irqfd *irqfd)
  79. {
  80. struct kvm_kernel_irqfd_resampler *resampler = irqfd->resampler;
  81. struct kvm *kvm = resampler->kvm;
  82. mutex_lock(&kvm->irqfds.resampler_lock);
  83. list_del_rcu(&irqfd->resampler_link);
  84. if (list_empty(&resampler->list)) {
  85. list_del_rcu(&resampler->link);
  86. kvm_unregister_irq_ack_notifier(kvm, &resampler->notifier);
  87. /*
  88. * synchronize_srcu_expedited(&kvm->irq_srcu) already called
  89. * in kvm_unregister_irq_ack_notifier().
  90. */
  91. kvm_set_irq(kvm, KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID,
  92. resampler->notifier.gsi, 0, false);
  93. kfree(resampler);
  94. } else {
  95. synchronize_srcu_expedited(&kvm->irq_srcu);
  96. }
  97. mutex_unlock(&kvm->irqfds.resampler_lock);
  98. }
  99. /*
  100. * Race-free decouple logic (ordering is critical)
  101. */
  102. static void
  103. irqfd_shutdown(struct work_struct *work)
  104. {
  105. struct kvm_kernel_irqfd *irqfd =
  106. container_of(work, struct kvm_kernel_irqfd, shutdown);
  107. struct kvm *kvm = irqfd->kvm;
  108. u64 cnt;
  109. /* Make sure irqfd has been initialized in assign path. */
  110. synchronize_srcu_expedited(&kvm->irq_srcu);
  111. /*
  112. * Synchronize with the wait-queue and unhook ourselves to prevent
  113. * further events.
  114. */
  115. eventfd_ctx_remove_wait_queue(irqfd->eventfd, &irqfd->wait, &cnt);
  116. /*
  117. * We know no new events will be scheduled at this point, so block
  118. * until all previously outstanding events have completed
  119. */
  120. flush_work(&irqfd->inject);
  121. if (irqfd->resampler) {
  122. irqfd_resampler_shutdown(irqfd);
  123. eventfd_ctx_put(irqfd->resamplefd);
  124. }
  125. /*
  126. * It is now safe to release the object's resources
  127. */
  128. #if IS_ENABLED(CONFIG_HAVE_KVM_IRQ_BYPASS)
  129. irq_bypass_unregister_consumer(&irqfd->consumer);
  130. #endif
  131. eventfd_ctx_put(irqfd->eventfd);
  132. kfree(irqfd);
  133. }
  134. static bool irqfd_is_active(struct kvm_kernel_irqfd *irqfd)
  135. {
  136. /*
  137. * Assert that either irqfds.lock or SRCU is held, as irqfds.lock must
  138. * be held to prevent false positives (on the irqfd being active), and
  139. * while false negatives are impossible as irqfds are never added back
  140. * to the list once they're deactivated, the caller must at least hold
  141. * SRCU to guard against routing changes if the irqfd is deactivated.
  142. */
  143. lockdep_assert_once(lockdep_is_held(&irqfd->kvm->irqfds.lock) ||
  144. srcu_read_lock_held(&irqfd->kvm->irq_srcu));
  145. return list_empty(&irqfd->list) ? false : true;
  146. }
  147. /*
  148. * Mark the irqfd as inactive and schedule it for removal
  149. */
  150. static void irqfd_deactivate(struct kvm_kernel_irqfd *irqfd)
  151. {
  152. lockdep_assert_held(&irqfd->kvm->irqfds.lock);
  153. BUG_ON(!irqfd_is_active(irqfd));
  154. list_del_init(&irqfd->list);
  155. queue_work(irqfd_cleanup_wq, &irqfd->shutdown);
  156. }
  157. int __attribute__((weak)) kvm_arch_set_irq_inatomic(
  158. struct kvm_kernel_irq_routing_entry *irq,
  159. struct kvm *kvm, int irq_source_id,
  160. int level,
  161. bool line_status)
  162. {
  163. return -EWOULDBLOCK;
  164. }
  165. /*
  166. * Called with wqh->lock held and interrupts disabled
  167. */
  168. static int
  169. irqfd_wakeup(wait_queue_entry_t *wait, unsigned mode, int sync, void *key)
  170. {
  171. struct kvm_kernel_irqfd *irqfd =
  172. container_of(wait, struct kvm_kernel_irqfd, wait);
  173. __poll_t flags = key_to_poll(key);
  174. struct kvm_kernel_irq_routing_entry irq;
  175. struct kvm *kvm = irqfd->kvm;
  176. unsigned seq;
  177. int idx;
  178. int ret = 0;
  179. if (flags & EPOLLIN) {
  180. /*
  181. * WARNING: Do NOT take irqfds.lock in any path except EPOLLHUP,
  182. * as KVM holds irqfds.lock when registering the irqfd with the
  183. * eventfd.
  184. */
  185. u64 cnt;
  186. eventfd_ctx_do_read(irqfd->eventfd, &cnt);
  187. idx = srcu_read_lock(&kvm->irq_srcu);
  188. do {
  189. seq = read_seqcount_begin(&irqfd->irq_entry_sc);
  190. irq = irqfd->irq_entry;
  191. } while (read_seqcount_retry(&irqfd->irq_entry_sc, seq));
  192. /*
  193. * An event has been signaled, inject an interrupt unless the
  194. * irqfd is being deassigned (isn't active), in which case the
  195. * routing information may be stale (once the irqfd is removed
  196. * from the list, it will stop receiving routing updates).
  197. */
  198. if (unlikely(!irqfd_is_active(irqfd)) ||
  199. kvm_arch_set_irq_inatomic(&irq, kvm,
  200. KVM_USERSPACE_IRQ_SOURCE_ID, 1,
  201. false) == -EWOULDBLOCK)
  202. schedule_work(&irqfd->inject);
  203. srcu_read_unlock(&kvm->irq_srcu, idx);
  204. ret = 1;
  205. }
  206. if (flags & EPOLLHUP) {
  207. /* The eventfd is closing, detach from KVM */
  208. unsigned long iflags;
  209. /*
  210. * Taking irqfds.lock is safe here, as KVM holds a reference to
  211. * the eventfd when registering the irqfd, i.e. this path can't
  212. * be reached while kvm_irqfd_add() is running.
  213. */
  214. spin_lock_irqsave(&kvm->irqfds.lock, iflags);
  215. /*
  216. * We must check if someone deactivated the irqfd before
  217. * we could acquire the irqfds.lock since the item is
  218. * deactivated from the KVM side before it is unhooked from
  219. * the wait-queue. If it is already deactivated, we can
  220. * simply return knowing the other side will cleanup for us.
  221. * We cannot race against the irqfd going away since the
  222. * other side is required to acquire wqh->lock, which we hold
  223. */
  224. if (irqfd_is_active(irqfd))
  225. irqfd_deactivate(irqfd);
  226. spin_unlock_irqrestore(&kvm->irqfds.lock, iflags);
  227. }
  228. return ret;
  229. }
  230. static void irqfd_update(struct kvm *kvm, struct kvm_kernel_irqfd *irqfd)
  231. {
  232. struct kvm_kernel_irq_routing_entry *e;
  233. struct kvm_kernel_irq_routing_entry entries[KVM_NR_IRQCHIPS];
  234. int n_entries;
  235. lockdep_assert_held(&kvm->irqfds.lock);
  236. n_entries = kvm_irq_map_gsi(kvm, entries, irqfd->gsi);
  237. write_seqcount_begin(&irqfd->irq_entry_sc);
  238. e = entries;
  239. if (n_entries == 1)
  240. irqfd->irq_entry = *e;
  241. else
  242. irqfd->irq_entry.type = 0;
  243. write_seqcount_end(&irqfd->irq_entry_sc);
  244. }
  245. struct kvm_irqfd_pt {
  246. struct kvm_kernel_irqfd *irqfd;
  247. struct kvm *kvm;
  248. poll_table pt;
  249. int ret;
  250. };
  251. static void kvm_irqfd_register(struct file *file, wait_queue_head_t *wqh,
  252. poll_table *pt)
  253. {
  254. struct kvm_irqfd_pt *p = container_of(pt, struct kvm_irqfd_pt, pt);
  255. struct kvm_kernel_irqfd *irqfd = p->irqfd;
  256. struct kvm *kvm = p->kvm;
  257. /*
  258. * Note, irqfds.lock protects the irqfd's irq_entry, i.e. its routing,
  259. * and irqfds.items. It does NOT protect registering with the eventfd.
  260. */
  261. spin_lock_irq(&kvm->irqfds.lock);
  262. /*
  263. * Initialize the routing information prior to adding the irqfd to the
  264. * eventfd's waitqueue, as irqfd_wakeup() can be invoked as soon as the
  265. * irqfd is registered.
  266. */
  267. irqfd_update(kvm, irqfd);
  268. /*
  269. * Add the irqfd as a priority waiter on the eventfd, with a custom
  270. * wake-up handler, so that KVM *and only KVM* is notified whenever the
  271. * underlying eventfd is signaled.
  272. */
  273. init_waitqueue_func_entry(&irqfd->wait, irqfd_wakeup);
  274. /*
  275. * Temporarily lie to lockdep about holding irqfds.lock to avoid a
  276. * false positive regarding potential deadlock with irqfd_wakeup()
  277. * (see irqfd_wakeup() for details).
  278. *
  279. * Adding to the wait queue will fail if there is already a priority
  280. * waiter, i.e. if the eventfd is associated with another irqfd (in any
  281. * VM). Note, kvm_irqfd_deassign() waits for all in-flight shutdown
  282. * jobs to complete, i.e. ensures the irqfd has been removed from the
  283. * eventfd's waitqueue before returning to userspace.
  284. */
  285. spin_release(&kvm->irqfds.lock.dep_map, _RET_IP_);
  286. p->ret = add_wait_queue_priority_exclusive(wqh, &irqfd->wait);
  287. spin_acquire(&kvm->irqfds.lock.dep_map, 0, 0, _RET_IP_);
  288. if (p->ret)
  289. goto out;
  290. list_add_tail(&irqfd->list, &kvm->irqfds.items);
  291. out:
  292. spin_unlock_irq(&kvm->irqfds.lock);
  293. }
  294. #if IS_ENABLED(CONFIG_HAVE_KVM_IRQ_BYPASS)
  295. void __attribute__((weak)) kvm_arch_irq_bypass_stop(
  296. struct irq_bypass_consumer *cons)
  297. {
  298. }
  299. void __attribute__((weak)) kvm_arch_irq_bypass_start(
  300. struct irq_bypass_consumer *cons)
  301. {
  302. }
  303. void __weak kvm_arch_update_irqfd_routing(struct kvm_kernel_irqfd *irqfd,
  304. struct kvm_kernel_irq_routing_entry *old,
  305. struct kvm_kernel_irq_routing_entry *new)
  306. {
  307. }
  308. #endif
  309. static int
  310. kvm_irqfd_assign(struct kvm *kvm, struct kvm_irqfd *args)
  311. {
  312. struct kvm_kernel_irqfd *irqfd;
  313. struct eventfd_ctx *eventfd = NULL, *resamplefd = NULL;
  314. struct kvm_irqfd_pt irqfd_pt;
  315. int ret;
  316. __poll_t events;
  317. int idx;
  318. if (!kvm_arch_intc_initialized(kvm))
  319. return -EAGAIN;
  320. if (!kvm_arch_irqfd_allowed(kvm, args))
  321. return -EINVAL;
  322. irqfd = kzalloc_obj(*irqfd, GFP_KERNEL_ACCOUNT);
  323. if (!irqfd)
  324. return -ENOMEM;
  325. irqfd->kvm = kvm;
  326. irqfd->gsi = args->gsi;
  327. INIT_LIST_HEAD(&irqfd->list);
  328. INIT_WORK(&irqfd->inject, irqfd_inject);
  329. INIT_WORK(&irqfd->shutdown, irqfd_shutdown);
  330. seqcount_spinlock_init(&irqfd->irq_entry_sc, &kvm->irqfds.lock);
  331. CLASS(fd, f)(args->fd);
  332. if (fd_empty(f)) {
  333. ret = -EBADF;
  334. goto out;
  335. }
  336. eventfd = eventfd_ctx_fileget(fd_file(f));
  337. if (IS_ERR(eventfd)) {
  338. ret = PTR_ERR(eventfd);
  339. goto out;
  340. }
  341. irqfd->eventfd = eventfd;
  342. if (args->flags & KVM_IRQFD_FLAG_RESAMPLE) {
  343. struct kvm_kernel_irqfd_resampler *resampler;
  344. resamplefd = eventfd_ctx_fdget(args->resamplefd);
  345. if (IS_ERR(resamplefd)) {
  346. ret = PTR_ERR(resamplefd);
  347. goto fail;
  348. }
  349. irqfd->resamplefd = resamplefd;
  350. INIT_LIST_HEAD(&irqfd->resampler_link);
  351. mutex_lock(&kvm->irqfds.resampler_lock);
  352. list_for_each_entry(resampler,
  353. &kvm->irqfds.resampler_list, link) {
  354. if (resampler->notifier.gsi == irqfd->gsi) {
  355. irqfd->resampler = resampler;
  356. break;
  357. }
  358. }
  359. if (!irqfd->resampler) {
  360. resampler = kzalloc_obj(*resampler, GFP_KERNEL_ACCOUNT);
  361. if (!resampler) {
  362. ret = -ENOMEM;
  363. mutex_unlock(&kvm->irqfds.resampler_lock);
  364. goto fail;
  365. }
  366. resampler->kvm = kvm;
  367. INIT_LIST_HEAD(&resampler->list);
  368. resampler->notifier.gsi = irqfd->gsi;
  369. resampler->notifier.irq_acked = irqfd_resampler_ack;
  370. INIT_LIST_HEAD(&resampler->link);
  371. list_add_rcu(&resampler->link, &kvm->irqfds.resampler_list);
  372. kvm_register_irq_ack_notifier(kvm,
  373. &resampler->notifier);
  374. irqfd->resampler = resampler;
  375. }
  376. list_add_rcu(&irqfd->resampler_link, &irqfd->resampler->list);
  377. synchronize_srcu_expedited(&kvm->irq_srcu);
  378. mutex_unlock(&kvm->irqfds.resampler_lock);
  379. }
  380. /*
  381. * Set the irqfd routing and add it to KVM's list before registering
  382. * the irqfd with the eventfd, so that the routing information is valid
  383. * and stays valid, e.g. if there are GSI routing changes, prior to
  384. * making the irqfd visible, i.e. before it might be signaled.
  385. *
  386. * Note, holding SRCU ensures a stable read of routing information, and
  387. * also prevents irqfd_shutdown() from freeing the irqfd before it's
  388. * fully initialized.
  389. */
  390. idx = srcu_read_lock(&kvm->irq_srcu);
  391. /*
  392. * Register the irqfd with the eventfd by polling on the eventfd, and
  393. * simultaneously and the irqfd to KVM's list. If there was en event
  394. * pending on the eventfd prior to registering, manually trigger IRQ
  395. * injection.
  396. */
  397. irqfd_pt.irqfd = irqfd;
  398. irqfd_pt.kvm = kvm;
  399. init_poll_funcptr(&irqfd_pt.pt, kvm_irqfd_register);
  400. events = vfs_poll(fd_file(f), &irqfd_pt.pt);
  401. ret = irqfd_pt.ret;
  402. if (ret)
  403. goto fail_poll;
  404. if (events & EPOLLIN)
  405. schedule_work(&irqfd->inject);
  406. #if IS_ENABLED(CONFIG_HAVE_KVM_IRQ_BYPASS)
  407. if (kvm_arch_has_irq_bypass()) {
  408. irqfd->consumer.add_producer = kvm_arch_irq_bypass_add_producer;
  409. irqfd->consumer.del_producer = kvm_arch_irq_bypass_del_producer;
  410. irqfd->consumer.stop = kvm_arch_irq_bypass_stop;
  411. irqfd->consumer.start = kvm_arch_irq_bypass_start;
  412. ret = irq_bypass_register_consumer(&irqfd->consumer, irqfd->eventfd);
  413. if (ret)
  414. pr_info("irq bypass consumer (eventfd %p) registration fails: %d\n",
  415. irqfd->eventfd, ret);
  416. }
  417. #endif
  418. srcu_read_unlock(&kvm->irq_srcu, idx);
  419. return 0;
  420. fail_poll:
  421. srcu_read_unlock(&kvm->irq_srcu, idx);
  422. fail:
  423. if (irqfd->resampler)
  424. irqfd_resampler_shutdown(irqfd);
  425. if (resamplefd && !IS_ERR(resamplefd))
  426. eventfd_ctx_put(resamplefd);
  427. if (eventfd && !IS_ERR(eventfd))
  428. eventfd_ctx_put(eventfd);
  429. out:
  430. kfree(irqfd);
  431. return ret;
  432. }
  433. bool kvm_irq_has_notifier(struct kvm *kvm, unsigned irqchip, unsigned pin)
  434. {
  435. struct kvm_irq_ack_notifier *kian;
  436. int gsi, idx;
  437. idx = srcu_read_lock(&kvm->irq_srcu);
  438. gsi = kvm_irq_map_chip_pin(kvm, irqchip, pin);
  439. if (gsi != -1)
  440. hlist_for_each_entry_srcu(kian, &kvm->irq_ack_notifier_list,
  441. link, srcu_read_lock_held(&kvm->irq_srcu))
  442. if (kian->gsi == gsi) {
  443. srcu_read_unlock(&kvm->irq_srcu, idx);
  444. return true;
  445. }
  446. srcu_read_unlock(&kvm->irq_srcu, idx);
  447. return false;
  448. }
  449. EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_irq_has_notifier);
  450. void kvm_notify_acked_gsi(struct kvm *kvm, int gsi)
  451. {
  452. struct kvm_irq_ack_notifier *kian;
  453. hlist_for_each_entry_srcu(kian, &kvm->irq_ack_notifier_list,
  454. link, srcu_read_lock_held(&kvm->irq_srcu))
  455. if (kian->gsi == gsi)
  456. kian->irq_acked(kian);
  457. }
  458. void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin)
  459. {
  460. int gsi, idx;
  461. trace_kvm_ack_irq(irqchip, pin);
  462. idx = srcu_read_lock(&kvm->irq_srcu);
  463. gsi = kvm_irq_map_chip_pin(kvm, irqchip, pin);
  464. if (gsi != -1)
  465. kvm_notify_acked_gsi(kvm, gsi);
  466. srcu_read_unlock(&kvm->irq_srcu, idx);
  467. }
  468. void kvm_register_irq_ack_notifier(struct kvm *kvm,
  469. struct kvm_irq_ack_notifier *kian)
  470. {
  471. mutex_lock(&kvm->irq_lock);
  472. hlist_add_head_rcu(&kian->link, &kvm->irq_ack_notifier_list);
  473. mutex_unlock(&kvm->irq_lock);
  474. kvm_arch_post_irq_ack_notifier_list_update(kvm);
  475. }
  476. void kvm_unregister_irq_ack_notifier(struct kvm *kvm,
  477. struct kvm_irq_ack_notifier *kian)
  478. {
  479. mutex_lock(&kvm->irq_lock);
  480. hlist_del_init_rcu(&kian->link);
  481. mutex_unlock(&kvm->irq_lock);
  482. synchronize_srcu_expedited(&kvm->irq_srcu);
  483. kvm_arch_post_irq_ack_notifier_list_update(kvm);
  484. }
  485. /*
  486. * shutdown any irqfd's that match fd+gsi
  487. */
  488. static int
  489. kvm_irqfd_deassign(struct kvm *kvm, struct kvm_irqfd *args)
  490. {
  491. struct kvm_kernel_irqfd *irqfd, *tmp;
  492. struct eventfd_ctx *eventfd;
  493. eventfd = eventfd_ctx_fdget(args->fd);
  494. if (IS_ERR(eventfd))
  495. return PTR_ERR(eventfd);
  496. spin_lock_irq(&kvm->irqfds.lock);
  497. list_for_each_entry_safe(irqfd, tmp, &kvm->irqfds.items, list) {
  498. if (irqfd->eventfd == eventfd && irqfd->gsi == args->gsi)
  499. irqfd_deactivate(irqfd);
  500. }
  501. spin_unlock_irq(&kvm->irqfds.lock);
  502. eventfd_ctx_put(eventfd);
  503. /*
  504. * Block until we know all outstanding shutdown jobs have completed
  505. * so that we guarantee there will not be any more interrupts on this
  506. * gsi once this deassign function returns.
  507. */
  508. flush_workqueue(irqfd_cleanup_wq);
  509. return 0;
  510. }
  511. int
  512. kvm_irqfd(struct kvm *kvm, struct kvm_irqfd *args)
  513. {
  514. if (args->flags & ~(KVM_IRQFD_FLAG_DEASSIGN | KVM_IRQFD_FLAG_RESAMPLE))
  515. return -EINVAL;
  516. if (args->flags & KVM_IRQFD_FLAG_DEASSIGN)
  517. return kvm_irqfd_deassign(kvm, args);
  518. return kvm_irqfd_assign(kvm, args);
  519. }
  520. /*
  521. * This function is called as the kvm VM fd is being released. Shutdown all
  522. * irqfds that still remain open
  523. */
  524. void
  525. kvm_irqfd_release(struct kvm *kvm)
  526. {
  527. struct kvm_kernel_irqfd *irqfd, *tmp;
  528. spin_lock_irq(&kvm->irqfds.lock);
  529. list_for_each_entry_safe(irqfd, tmp, &kvm->irqfds.items, list)
  530. irqfd_deactivate(irqfd);
  531. spin_unlock_irq(&kvm->irqfds.lock);
  532. /*
  533. * Block until we know all outstanding shutdown jobs have completed
  534. * since we do not take a kvm* reference.
  535. */
  536. flush_workqueue(irqfd_cleanup_wq);
  537. }
  538. /*
  539. * Take note of a change in irq routing.
  540. * Caller must invoke synchronize_srcu_expedited(&kvm->irq_srcu) afterwards.
  541. */
  542. void kvm_irq_routing_update(struct kvm *kvm)
  543. {
  544. struct kvm_kernel_irqfd *irqfd;
  545. spin_lock_irq(&kvm->irqfds.lock);
  546. list_for_each_entry(irqfd, &kvm->irqfds.items, list) {
  547. #if IS_ENABLED(CONFIG_HAVE_KVM_IRQ_BYPASS)
  548. /* Under irqfds.lock, so can read irq_entry safely */
  549. struct kvm_kernel_irq_routing_entry old = irqfd->irq_entry;
  550. #endif
  551. irqfd_update(kvm, irqfd);
  552. #if IS_ENABLED(CONFIG_HAVE_KVM_IRQ_BYPASS)
  553. if (irqfd->producer)
  554. kvm_arch_update_irqfd_routing(irqfd, &old, &irqfd->irq_entry);
  555. #endif
  556. }
  557. spin_unlock_irq(&kvm->irqfds.lock);
  558. }
  559. bool kvm_notify_irqfd_resampler(struct kvm *kvm,
  560. unsigned int irqchip,
  561. unsigned int pin)
  562. {
  563. struct kvm_kernel_irqfd_resampler *resampler;
  564. int gsi, idx;
  565. idx = srcu_read_lock(&kvm->irq_srcu);
  566. gsi = kvm_irq_map_chip_pin(kvm, irqchip, pin);
  567. if (gsi != -1) {
  568. list_for_each_entry_srcu(resampler,
  569. &kvm->irqfds.resampler_list, link,
  570. srcu_read_lock_held(&kvm->irq_srcu)) {
  571. if (resampler->notifier.gsi == gsi) {
  572. irqfd_resampler_notify(resampler);
  573. srcu_read_unlock(&kvm->irq_srcu, idx);
  574. return true;
  575. }
  576. }
  577. }
  578. srcu_read_unlock(&kvm->irq_srcu, idx);
  579. return false;
  580. }
  581. /*
  582. * create a host-wide workqueue for issuing deferred shutdown requests
  583. * aggregated from all vm* instances. We need our own isolated
  584. * queue to ease flushing work items when a VM exits.
  585. */
  586. int kvm_irqfd_init(void)
  587. {
  588. irqfd_cleanup_wq = alloc_workqueue("kvm-irqfd-cleanup", WQ_PERCPU, 0);
  589. if (!irqfd_cleanup_wq)
  590. return -ENOMEM;
  591. return 0;
  592. }
  593. void kvm_irqfd_exit(void)
  594. {
  595. destroy_workqueue(irqfd_cleanup_wq);
  596. }
  597. #endif
  598. /*
  599. * --------------------------------------------------------------------
  600. * ioeventfd: translate a PIO/MMIO memory write to an eventfd signal.
  601. *
  602. * userspace can register a PIO/MMIO address with an eventfd for receiving
  603. * notification when the memory has been touched.
  604. * --------------------------------------------------------------------
  605. */
  606. struct _ioeventfd {
  607. struct list_head list;
  608. u64 addr;
  609. int length;
  610. struct eventfd_ctx *eventfd;
  611. u64 datamatch;
  612. struct kvm_io_device dev;
  613. u8 bus_idx;
  614. bool wildcard;
  615. };
  616. static inline struct _ioeventfd *
  617. to_ioeventfd(struct kvm_io_device *dev)
  618. {
  619. return container_of(dev, struct _ioeventfd, dev);
  620. }
  621. static void
  622. ioeventfd_release(struct _ioeventfd *p)
  623. {
  624. eventfd_ctx_put(p->eventfd);
  625. list_del(&p->list);
  626. kfree(p);
  627. }
  628. static bool
  629. ioeventfd_in_range(struct _ioeventfd *p, gpa_t addr, int len, const void *val)
  630. {
  631. u64 _val;
  632. if (addr != p->addr)
  633. /* address must be precise for a hit */
  634. return false;
  635. if (!p->length)
  636. /* length = 0 means only look at the address, so always a hit */
  637. return true;
  638. if (len != p->length)
  639. /* address-range must be precise for a hit */
  640. return false;
  641. if (p->wildcard)
  642. /* all else equal, wildcard is always a hit */
  643. return true;
  644. /* otherwise, we have to actually compare the data */
  645. BUG_ON(!IS_ALIGNED((unsigned long)val, len));
  646. switch (len) {
  647. case 1:
  648. _val = *(u8 *)val;
  649. break;
  650. case 2:
  651. _val = *(u16 *)val;
  652. break;
  653. case 4:
  654. _val = *(u32 *)val;
  655. break;
  656. case 8:
  657. _val = *(u64 *)val;
  658. break;
  659. default:
  660. return false;
  661. }
  662. return _val == p->datamatch;
  663. }
  664. /* MMIO/PIO writes trigger an event if the addr/val match */
  665. static int
  666. ioeventfd_write(struct kvm_vcpu *vcpu, struct kvm_io_device *this, gpa_t addr,
  667. int len, const void *val)
  668. {
  669. struct _ioeventfd *p = to_ioeventfd(this);
  670. if (!ioeventfd_in_range(p, addr, len, val))
  671. return -EOPNOTSUPP;
  672. eventfd_signal(p->eventfd);
  673. return 0;
  674. }
  675. /*
  676. * This function is called as KVM is completely shutting down. We do not
  677. * need to worry about locking just nuke anything we have as quickly as possible
  678. */
  679. static void
  680. ioeventfd_destructor(struct kvm_io_device *this)
  681. {
  682. struct _ioeventfd *p = to_ioeventfd(this);
  683. ioeventfd_release(p);
  684. }
  685. static const struct kvm_io_device_ops ioeventfd_ops = {
  686. .write = ioeventfd_write,
  687. .destructor = ioeventfd_destructor,
  688. };
  689. /* assumes kvm->slots_lock held */
  690. static bool
  691. ioeventfd_check_collision(struct kvm *kvm, struct _ioeventfd *p)
  692. {
  693. struct _ioeventfd *_p;
  694. list_for_each_entry(_p, &kvm->ioeventfds, list)
  695. if (_p->bus_idx == p->bus_idx &&
  696. _p->addr == p->addr &&
  697. (!_p->length || !p->length ||
  698. (_p->length == p->length &&
  699. (_p->wildcard || p->wildcard ||
  700. _p->datamatch == p->datamatch))))
  701. return true;
  702. return false;
  703. }
  704. static enum kvm_bus ioeventfd_bus_from_flags(__u32 flags)
  705. {
  706. if (flags & KVM_IOEVENTFD_FLAG_PIO)
  707. return KVM_PIO_BUS;
  708. if (flags & KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY)
  709. return KVM_VIRTIO_CCW_NOTIFY_BUS;
  710. return KVM_MMIO_BUS;
  711. }
  712. static int kvm_assign_ioeventfd_idx(struct kvm *kvm,
  713. enum kvm_bus bus_idx,
  714. struct kvm_ioeventfd *args)
  715. {
  716. struct eventfd_ctx *eventfd;
  717. struct _ioeventfd *p;
  718. int ret;
  719. eventfd = eventfd_ctx_fdget(args->fd);
  720. if (IS_ERR(eventfd))
  721. return PTR_ERR(eventfd);
  722. p = kzalloc_obj(*p, GFP_KERNEL_ACCOUNT);
  723. if (!p) {
  724. ret = -ENOMEM;
  725. goto fail;
  726. }
  727. INIT_LIST_HEAD(&p->list);
  728. p->addr = args->addr;
  729. p->bus_idx = bus_idx;
  730. p->length = args->len;
  731. p->eventfd = eventfd;
  732. /* The datamatch feature is optional, otherwise this is a wildcard */
  733. if (args->flags & KVM_IOEVENTFD_FLAG_DATAMATCH)
  734. p->datamatch = args->datamatch;
  735. else
  736. p->wildcard = true;
  737. mutex_lock(&kvm->slots_lock);
  738. /* Verify that there isn't a match already */
  739. if (ioeventfd_check_collision(kvm, p)) {
  740. ret = -EEXIST;
  741. goto unlock_fail;
  742. }
  743. kvm_iodevice_init(&p->dev, &ioeventfd_ops);
  744. ret = kvm_io_bus_register_dev(kvm, bus_idx, p->addr, p->length,
  745. &p->dev);
  746. if (ret < 0)
  747. goto unlock_fail;
  748. kvm_get_bus(kvm, bus_idx)->ioeventfd_count++;
  749. list_add_tail(&p->list, &kvm->ioeventfds);
  750. mutex_unlock(&kvm->slots_lock);
  751. return 0;
  752. unlock_fail:
  753. mutex_unlock(&kvm->slots_lock);
  754. kfree(p);
  755. fail:
  756. eventfd_ctx_put(eventfd);
  757. return ret;
  758. }
  759. static int
  760. kvm_deassign_ioeventfd_idx(struct kvm *kvm, enum kvm_bus bus_idx,
  761. struct kvm_ioeventfd *args)
  762. {
  763. struct _ioeventfd *p;
  764. struct eventfd_ctx *eventfd;
  765. struct kvm_io_bus *bus;
  766. int ret = -ENOENT;
  767. bool wildcard;
  768. eventfd = eventfd_ctx_fdget(args->fd);
  769. if (IS_ERR(eventfd))
  770. return PTR_ERR(eventfd);
  771. wildcard = !(args->flags & KVM_IOEVENTFD_FLAG_DATAMATCH);
  772. mutex_lock(&kvm->slots_lock);
  773. list_for_each_entry(p, &kvm->ioeventfds, list) {
  774. if (p->bus_idx != bus_idx ||
  775. p->eventfd != eventfd ||
  776. p->addr != args->addr ||
  777. p->length != args->len ||
  778. p->wildcard != wildcard)
  779. continue;
  780. if (!p->wildcard && p->datamatch != args->datamatch)
  781. continue;
  782. kvm_io_bus_unregister_dev(kvm, bus_idx, &p->dev);
  783. bus = kvm_get_bus(kvm, bus_idx);
  784. if (bus)
  785. bus->ioeventfd_count--;
  786. ret = 0;
  787. break;
  788. }
  789. mutex_unlock(&kvm->slots_lock);
  790. eventfd_ctx_put(eventfd);
  791. return ret;
  792. }
  793. static int kvm_deassign_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
  794. {
  795. enum kvm_bus bus_idx = ioeventfd_bus_from_flags(args->flags);
  796. int ret = kvm_deassign_ioeventfd_idx(kvm, bus_idx, args);
  797. if (!args->len && bus_idx == KVM_MMIO_BUS)
  798. kvm_deassign_ioeventfd_idx(kvm, KVM_FAST_MMIO_BUS, args);
  799. return ret;
  800. }
  801. static int
  802. kvm_assign_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
  803. {
  804. enum kvm_bus bus_idx;
  805. int ret;
  806. bus_idx = ioeventfd_bus_from_flags(args->flags);
  807. /* must be natural-word sized, or 0 to ignore length */
  808. switch (args->len) {
  809. case 0:
  810. case 1:
  811. case 2:
  812. case 4:
  813. case 8:
  814. break;
  815. default:
  816. return -EINVAL;
  817. }
  818. /* check for range overflow */
  819. if (args->addr + args->len < args->addr)
  820. return -EINVAL;
  821. /* check for extra flags that we don't understand */
  822. if (args->flags & ~KVM_IOEVENTFD_VALID_FLAG_MASK)
  823. return -EINVAL;
  824. /* ioeventfd with no length can't be combined with DATAMATCH */
  825. if (!args->len && (args->flags & KVM_IOEVENTFD_FLAG_DATAMATCH))
  826. return -EINVAL;
  827. ret = kvm_assign_ioeventfd_idx(kvm, bus_idx, args);
  828. if (ret)
  829. goto fail;
  830. /* When length is ignored, MMIO is also put on a separate bus, for
  831. * faster lookups.
  832. */
  833. if (!args->len && bus_idx == KVM_MMIO_BUS) {
  834. ret = kvm_assign_ioeventfd_idx(kvm, KVM_FAST_MMIO_BUS, args);
  835. if (ret < 0)
  836. goto fast_fail;
  837. }
  838. return 0;
  839. fast_fail:
  840. kvm_deassign_ioeventfd_idx(kvm, bus_idx, args);
  841. fail:
  842. return ret;
  843. }
  844. int
  845. kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
  846. {
  847. if (args->flags & KVM_IOEVENTFD_FLAG_DEASSIGN)
  848. return kvm_deassign_ioeventfd(kvm, args);
  849. return kvm_assign_ioeventfd(kvm, args);
  850. }
  851. void
  852. kvm_eventfd_init(struct kvm *kvm)
  853. {
  854. #ifdef CONFIG_HAVE_KVM_IRQCHIP
  855. spin_lock_init(&kvm->irqfds.lock);
  856. INIT_LIST_HEAD(&kvm->irqfds.items);
  857. INIT_LIST_HEAD(&kvm->irqfds.resampler_list);
  858. mutex_init(&kvm->irqfds.resampler_lock);
  859. #endif
  860. INIT_LIST_HEAD(&kvm->ioeventfds);
  861. }