eventfd.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  1. /*
  2. * kvm eventfd support - use eventfd objects to signal various KVM events
  3. *
  4. * Copyright 2009 Novell. All Rights Reserved.
  5. * Copyright 2010 Red Hat, Inc. and/or its affiliates.
  6. *
  7. * Author:
  8. * Gregory Haskins <ghaskins@novell.com>
  9. *
  10. * This file is free software; you can redistribute it and/or modify
  11. * it under the terms of version 2 of the GNU General Public License
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software Foundation,
  21. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
  22. */
  23. #include <linux/kvm_host.h>
  24. #include <linux/kvm.h>
  25. #include <linux/workqueue.h>
  26. #include <linux/syscalls.h>
  27. #include <linux/wait.h>
  28. #include <linux/poll.h>
  29. #include <linux/file.h>
  30. #include <linux/list.h>
  31. #include <linux/eventfd.h>
  32. #include <linux/kernel.h>
  33. #include <linux/srcu.h>
  34. #include <linux/slab.h>
  35. #include <linux/seqlock.h>
  36. #include <trace/events/kvm.h>
  37. #include "iodev.h"
  38. #ifdef CONFIG_HAVE_KVM_IRQFD
  39. /*
  40. * --------------------------------------------------------------------
  41. * irqfd: Allows an fd to be used to inject an interrupt to the guest
  42. *
  43. * Credit goes to Avi Kivity for the original idea.
  44. * --------------------------------------------------------------------
  45. */
  46. /*
  47. * Resampling irqfds are a special variety of irqfds used to emulate
  48. * level triggered interrupts. The interrupt is asserted on eventfd
  49. * trigger. On acknowledgement through the irq ack notifier, the
  50. * interrupt is de-asserted and userspace is notified through the
  51. * resamplefd. All resamplers on the same gsi are de-asserted
  52. * together, so we don't need to track the state of each individual
  53. * user. We can also therefore share the same irq source ID.
  54. */
  55. struct _irqfd_resampler {
  56. struct kvm *kvm;
  57. /*
  58. * List of resampling struct _irqfd objects sharing this gsi.
  59. * RCU list modified under kvm->irqfds.resampler_lock
  60. */
  61. struct list_head list;
  62. struct kvm_irq_ack_notifier notifier;
  63. /*
  64. * Entry in list of kvm->irqfd.resampler_list. Use for sharing
  65. * resamplers among irqfds on the same gsi.
  66. * Accessed and modified under kvm->irqfds.resampler_lock
  67. */
  68. struct list_head link;
  69. };
  70. struct _irqfd {
  71. /* Used for MSI fast-path */
  72. struct kvm *kvm;
  73. wait_queue_t wait;
  74. /* Update side is protected by irqfds.lock */
  75. struct kvm_kernel_irq_routing_entry irq_entry;
  76. seqcount_t irq_entry_sc;
  77. /* Used for level IRQ fast-path */
  78. int gsi;
  79. struct work_struct inject;
  80. /* The resampler used by this irqfd (resampler-only) */
  81. struct _irqfd_resampler *resampler;
  82. /* Eventfd notified on resample (resampler-only) */
  83. struct eventfd_ctx *resamplefd;
  84. /* Entry in list of irqfds for a resampler (resampler-only) */
  85. struct list_head resampler_link;
  86. /* Used for setup/shutdown */
  87. struct eventfd_ctx *eventfd;
  88. struct list_head list;
  89. poll_table pt;
  90. struct work_struct shutdown;
  91. };
  92. static struct workqueue_struct *irqfd_cleanup_wq;
  93. static void
  94. irqfd_inject(struct work_struct *work)
  95. {
  96. struct _irqfd *irqfd = container_of(work, struct _irqfd, inject);
  97. struct kvm *kvm = irqfd->kvm;
  98. if (!irqfd->resampler) {
  99. kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, irqfd->gsi, 1,
  100. false);
  101. kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, irqfd->gsi, 0,
  102. false);
  103. } else
  104. kvm_set_irq(kvm, KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID,
  105. irqfd->gsi, 1, false);
  106. }
  107. /*
  108. * Since resampler irqfds share an IRQ source ID, we de-assert once
  109. * then notify all of the resampler irqfds using this GSI. We can't
  110. * do multiple de-asserts or we risk racing with incoming re-asserts.
  111. */
  112. static void
  113. irqfd_resampler_ack(struct kvm_irq_ack_notifier *kian)
  114. {
  115. struct _irqfd_resampler *resampler;
  116. struct kvm *kvm;
  117. struct _irqfd *irqfd;
  118. int idx;
  119. resampler = container_of(kian, struct _irqfd_resampler, notifier);
  120. kvm = resampler->kvm;
  121. kvm_set_irq(kvm, KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID,
  122. resampler->notifier.gsi, 0, false);
  123. idx = srcu_read_lock(&kvm->irq_srcu);
  124. list_for_each_entry_rcu(irqfd, &resampler->list, resampler_link)
  125. eventfd_signal(irqfd->resamplefd, 1);
  126. srcu_read_unlock(&kvm->irq_srcu, idx);
  127. }
  128. static void
  129. irqfd_resampler_shutdown(struct _irqfd *irqfd)
  130. {
  131. struct _irqfd_resampler *resampler = irqfd->resampler;
  132. struct kvm *kvm = resampler->kvm;
  133. mutex_lock(&kvm->irqfds.resampler_lock);
  134. list_del_rcu(&irqfd->resampler_link);
  135. synchronize_srcu(&kvm->irq_srcu);
  136. if (list_empty(&resampler->list)) {
  137. list_del(&resampler->link);
  138. kvm_unregister_irq_ack_notifier(kvm, &resampler->notifier);
  139. kvm_set_irq(kvm, KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID,
  140. resampler->notifier.gsi, 0, false);
  141. kfree(resampler);
  142. }
  143. mutex_unlock(&kvm->irqfds.resampler_lock);
  144. }
  145. /*
  146. * Race-free decouple logic (ordering is critical)
  147. */
  148. static void
  149. irqfd_shutdown(struct work_struct *work)
  150. {
  151. struct _irqfd *irqfd = container_of(work, struct _irqfd, shutdown);
  152. u64 cnt;
  153. /*
  154. * Synchronize with the wait-queue and unhook ourselves to prevent
  155. * further events.
  156. */
  157. eventfd_ctx_remove_wait_queue(irqfd->eventfd, &irqfd->wait, &cnt);
  158. /*
  159. * We know no new events will be scheduled at this point, so block
  160. * until all previously outstanding events have completed
  161. */
  162. flush_work(&irqfd->inject);
  163. if (irqfd->resampler) {
  164. irqfd_resampler_shutdown(irqfd);
  165. eventfd_ctx_put(irqfd->resamplefd);
  166. }
  167. /*
  168. * It is now safe to release the object's resources
  169. */
  170. eventfd_ctx_put(irqfd->eventfd);
  171. kfree(irqfd);
  172. }
  173. /* assumes kvm->irqfds.lock is held */
  174. static bool
  175. irqfd_is_active(struct _irqfd *irqfd)
  176. {
  177. return list_empty(&irqfd->list) ? false : true;
  178. }
  179. /*
  180. * Mark the irqfd as inactive and schedule it for removal
  181. *
  182. * assumes kvm->irqfds.lock is held
  183. */
  184. static void
  185. irqfd_deactivate(struct _irqfd *irqfd)
  186. {
  187. BUG_ON(!irqfd_is_active(irqfd));
  188. list_del_init(&irqfd->list);
  189. queue_work(irqfd_cleanup_wq, &irqfd->shutdown);
  190. }
  191. /*
  192. * Called with wqh->lock held and interrupts disabled
  193. */
  194. static int
  195. irqfd_wakeup(wait_queue_t *wait, unsigned mode, int sync, void *key)
  196. {
  197. struct _irqfd *irqfd = container_of(wait, struct _irqfd, wait);
  198. unsigned long flags = (unsigned long)key;
  199. struct kvm_kernel_irq_routing_entry irq;
  200. struct kvm *kvm = irqfd->kvm;
  201. unsigned seq;
  202. int idx;
  203. if (flags & POLLIN) {
  204. idx = srcu_read_lock(&kvm->irq_srcu);
  205. do {
  206. seq = read_seqcount_begin(&irqfd->irq_entry_sc);
  207. irq = irqfd->irq_entry;
  208. } while (read_seqcount_retry(&irqfd->irq_entry_sc, seq));
  209. /* An event has been signaled, inject an interrupt */
  210. if (irq.type == KVM_IRQ_ROUTING_MSI)
  211. kvm_set_msi(&irq, kvm, KVM_USERSPACE_IRQ_SOURCE_ID, 1,
  212. false);
  213. else
  214. schedule_work(&irqfd->inject);
  215. srcu_read_unlock(&kvm->irq_srcu, idx);
  216. }
  217. if (flags & POLLHUP) {
  218. /* The eventfd is closing, detach from KVM */
  219. unsigned long flags;
  220. spin_lock_irqsave(&kvm->irqfds.lock, flags);
  221. /*
  222. * We must check if someone deactivated the irqfd before
  223. * we could acquire the irqfds.lock since the item is
  224. * deactivated from the KVM side before it is unhooked from
  225. * the wait-queue. If it is already deactivated, we can
  226. * simply return knowing the other side will cleanup for us.
  227. * We cannot race against the irqfd going away since the
  228. * other side is required to acquire wqh->lock, which we hold
  229. */
  230. if (irqfd_is_active(irqfd))
  231. irqfd_deactivate(irqfd);
  232. spin_unlock_irqrestore(&kvm->irqfds.lock, flags);
  233. }
  234. return 0;
  235. }
  236. static void
  237. irqfd_ptable_queue_proc(struct file *file, wait_queue_head_t *wqh,
  238. poll_table *pt)
  239. {
  240. struct _irqfd *irqfd = container_of(pt, struct _irqfd, pt);
  241. add_wait_queue(wqh, &irqfd->wait);
  242. }
  243. /* Must be called under irqfds.lock */
  244. static void irqfd_update(struct kvm *kvm, struct _irqfd *irqfd)
  245. {
  246. struct kvm_kernel_irq_routing_entry *e;
  247. struct kvm_kernel_irq_routing_entry entries[KVM_NR_IRQCHIPS];
  248. int i, n_entries;
  249. n_entries = kvm_irq_map_gsi(kvm, entries, irqfd->gsi);
  250. write_seqcount_begin(&irqfd->irq_entry_sc);
  251. irqfd->irq_entry.type = 0;
  252. e = entries;
  253. for (i = 0; i < n_entries; ++i, ++e) {
  254. /* Only fast-path MSI. */
  255. if (e->type == KVM_IRQ_ROUTING_MSI)
  256. irqfd->irq_entry = *e;
  257. }
  258. write_seqcount_end(&irqfd->irq_entry_sc);
  259. }
  260. static int
  261. kvm_irqfd_assign(struct kvm *kvm, struct kvm_irqfd *args)
  262. {
  263. struct _irqfd *irqfd, *tmp;
  264. struct fd f;
  265. struct eventfd_ctx *eventfd = NULL, *resamplefd = NULL;
  266. int ret;
  267. unsigned int events;
  268. int idx;
  269. irqfd = kzalloc(sizeof(*irqfd), GFP_KERNEL);
  270. if (!irqfd)
  271. return -ENOMEM;
  272. irqfd->kvm = kvm;
  273. irqfd->gsi = args->gsi;
  274. INIT_LIST_HEAD(&irqfd->list);
  275. INIT_WORK(&irqfd->inject, irqfd_inject);
  276. INIT_WORK(&irqfd->shutdown, irqfd_shutdown);
  277. seqcount_init(&irqfd->irq_entry_sc);
  278. f = fdget(args->fd);
  279. if (!f.file) {
  280. ret = -EBADF;
  281. goto out;
  282. }
  283. eventfd = eventfd_ctx_fileget(f.file);
  284. if (IS_ERR(eventfd)) {
  285. ret = PTR_ERR(eventfd);
  286. goto fail;
  287. }
  288. irqfd->eventfd = eventfd;
  289. if (args->flags & KVM_IRQFD_FLAG_RESAMPLE) {
  290. struct _irqfd_resampler *resampler;
  291. resamplefd = eventfd_ctx_fdget(args->resamplefd);
  292. if (IS_ERR(resamplefd)) {
  293. ret = PTR_ERR(resamplefd);
  294. goto fail;
  295. }
  296. irqfd->resamplefd = resamplefd;
  297. INIT_LIST_HEAD(&irqfd->resampler_link);
  298. mutex_lock(&kvm->irqfds.resampler_lock);
  299. list_for_each_entry(resampler,
  300. &kvm->irqfds.resampler_list, link) {
  301. if (resampler->notifier.gsi == irqfd->gsi) {
  302. irqfd->resampler = resampler;
  303. break;
  304. }
  305. }
  306. if (!irqfd->resampler) {
  307. resampler = kzalloc(sizeof(*resampler), GFP_KERNEL);
  308. if (!resampler) {
  309. ret = -ENOMEM;
  310. mutex_unlock(&kvm->irqfds.resampler_lock);
  311. goto fail;
  312. }
  313. resampler->kvm = kvm;
  314. INIT_LIST_HEAD(&resampler->list);
  315. resampler->notifier.gsi = irqfd->gsi;
  316. resampler->notifier.irq_acked = irqfd_resampler_ack;
  317. INIT_LIST_HEAD(&resampler->link);
  318. list_add(&resampler->link, &kvm->irqfds.resampler_list);
  319. kvm_register_irq_ack_notifier(kvm,
  320. &resampler->notifier);
  321. irqfd->resampler = resampler;
  322. }
  323. list_add_rcu(&irqfd->resampler_link, &irqfd->resampler->list);
  324. synchronize_srcu(&kvm->irq_srcu);
  325. mutex_unlock(&kvm->irqfds.resampler_lock);
  326. }
  327. /*
  328. * Install our own custom wake-up handling so we are notified via
  329. * a callback whenever someone signals the underlying eventfd
  330. */
  331. init_waitqueue_func_entry(&irqfd->wait, irqfd_wakeup);
  332. init_poll_funcptr(&irqfd->pt, irqfd_ptable_queue_proc);
  333. spin_lock_irq(&kvm->irqfds.lock);
  334. ret = 0;
  335. list_for_each_entry(tmp, &kvm->irqfds.items, list) {
  336. if (irqfd->eventfd != tmp->eventfd)
  337. continue;
  338. /* This fd is used for another irq already. */
  339. ret = -EBUSY;
  340. spin_unlock_irq(&kvm->irqfds.lock);
  341. goto fail;
  342. }
  343. idx = srcu_read_lock(&kvm->irq_srcu);
  344. irqfd_update(kvm, irqfd);
  345. srcu_read_unlock(&kvm->irq_srcu, idx);
  346. list_add_tail(&irqfd->list, &kvm->irqfds.items);
  347. spin_unlock_irq(&kvm->irqfds.lock);
  348. /*
  349. * Check if there was an event already pending on the eventfd
  350. * before we registered, and trigger it as if we didn't miss it.
  351. */
  352. events = f.file->f_op->poll(f.file, &irqfd->pt);
  353. if (events & POLLIN)
  354. schedule_work(&irqfd->inject);
  355. /*
  356. * do not drop the file until the irqfd is fully initialized, otherwise
  357. * we might race against the POLLHUP
  358. */
  359. fdput(f);
  360. return 0;
  361. fail:
  362. if (irqfd->resampler)
  363. irqfd_resampler_shutdown(irqfd);
  364. if (resamplefd && !IS_ERR(resamplefd))
  365. eventfd_ctx_put(resamplefd);
  366. if (eventfd && !IS_ERR(eventfd))
  367. eventfd_ctx_put(eventfd);
  368. fdput(f);
  369. out:
  370. kfree(irqfd);
  371. return ret;
  372. }
  373. bool kvm_irq_has_notifier(struct kvm *kvm, unsigned irqchip, unsigned pin)
  374. {
  375. struct kvm_irq_ack_notifier *kian;
  376. int gsi, idx;
  377. idx = srcu_read_lock(&kvm->irq_srcu);
  378. gsi = kvm_irq_map_chip_pin(kvm, irqchip, pin);
  379. if (gsi != -1)
  380. hlist_for_each_entry_rcu(kian, &kvm->irq_ack_notifier_list,
  381. link)
  382. if (kian->gsi == gsi) {
  383. srcu_read_unlock(&kvm->irq_srcu, idx);
  384. return true;
  385. }
  386. srcu_read_unlock(&kvm->irq_srcu, idx);
  387. return false;
  388. }
  389. EXPORT_SYMBOL_GPL(kvm_irq_has_notifier);
  390. void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin)
  391. {
  392. struct kvm_irq_ack_notifier *kian;
  393. int gsi, idx;
  394. trace_kvm_ack_irq(irqchip, pin);
  395. idx = srcu_read_lock(&kvm->irq_srcu);
  396. gsi = kvm_irq_map_chip_pin(kvm, irqchip, pin);
  397. if (gsi != -1)
  398. hlist_for_each_entry_rcu(kian, &kvm->irq_ack_notifier_list,
  399. link)
  400. if (kian->gsi == gsi)
  401. kian->irq_acked(kian);
  402. srcu_read_unlock(&kvm->irq_srcu, idx);
  403. }
  404. void kvm_register_irq_ack_notifier(struct kvm *kvm,
  405. struct kvm_irq_ack_notifier *kian)
  406. {
  407. mutex_lock(&kvm->irq_lock);
  408. hlist_add_head_rcu(&kian->link, &kvm->irq_ack_notifier_list);
  409. mutex_unlock(&kvm->irq_lock);
  410. kvm_vcpu_request_scan_ioapic(kvm);
  411. }
  412. void kvm_unregister_irq_ack_notifier(struct kvm *kvm,
  413. struct kvm_irq_ack_notifier *kian)
  414. {
  415. mutex_lock(&kvm->irq_lock);
  416. hlist_del_init_rcu(&kian->link);
  417. mutex_unlock(&kvm->irq_lock);
  418. synchronize_srcu(&kvm->irq_srcu);
  419. kvm_vcpu_request_scan_ioapic(kvm);
  420. }
  421. #endif
  422. void
  423. kvm_eventfd_init(struct kvm *kvm)
  424. {
  425. #ifdef CONFIG_HAVE_KVM_IRQFD
  426. spin_lock_init(&kvm->irqfds.lock);
  427. INIT_LIST_HEAD(&kvm->irqfds.items);
  428. INIT_LIST_HEAD(&kvm->irqfds.resampler_list);
  429. mutex_init(&kvm->irqfds.resampler_lock);
  430. #endif
  431. INIT_LIST_HEAD(&kvm->ioeventfds);
  432. }
  433. #ifdef CONFIG_HAVE_KVM_IRQFD
  434. /*
  435. * shutdown any irqfd's that match fd+gsi
  436. */
  437. static int
  438. kvm_irqfd_deassign(struct kvm *kvm, struct kvm_irqfd *args)
  439. {
  440. struct _irqfd *irqfd, *tmp;
  441. struct eventfd_ctx *eventfd;
  442. eventfd = eventfd_ctx_fdget(args->fd);
  443. if (IS_ERR(eventfd))
  444. return PTR_ERR(eventfd);
  445. spin_lock_irq(&kvm->irqfds.lock);
  446. list_for_each_entry_safe(irqfd, tmp, &kvm->irqfds.items, list) {
  447. if (irqfd->eventfd == eventfd && irqfd->gsi == args->gsi) {
  448. /*
  449. * This clearing of irq_entry.type is needed for when
  450. * another thread calls kvm_irq_routing_update before
  451. * we flush workqueue below (we synchronize with
  452. * kvm_irq_routing_update using irqfds.lock).
  453. */
  454. write_seqcount_begin(&irqfd->irq_entry_sc);
  455. irqfd->irq_entry.type = 0;
  456. write_seqcount_end(&irqfd->irq_entry_sc);
  457. irqfd_deactivate(irqfd);
  458. }
  459. }
  460. spin_unlock_irq(&kvm->irqfds.lock);
  461. eventfd_ctx_put(eventfd);
  462. /*
  463. * Block until we know all outstanding shutdown jobs have completed
  464. * so that we guarantee there will not be any more interrupts on this
  465. * gsi once this deassign function returns.
  466. */
  467. flush_workqueue(irqfd_cleanup_wq);
  468. return 0;
  469. }
  470. int
  471. kvm_irqfd(struct kvm *kvm, struct kvm_irqfd *args)
  472. {
  473. if (args->flags & ~(KVM_IRQFD_FLAG_DEASSIGN | KVM_IRQFD_FLAG_RESAMPLE))
  474. return -EINVAL;
  475. if (args->flags & KVM_IRQFD_FLAG_DEASSIGN)
  476. return kvm_irqfd_deassign(kvm, args);
  477. return kvm_irqfd_assign(kvm, args);
  478. }
  479. /*
  480. * This function is called as the kvm VM fd is being released. Shutdown all
  481. * irqfds that still remain open
  482. */
  483. void
  484. kvm_irqfd_release(struct kvm *kvm)
  485. {
  486. struct _irqfd *irqfd, *tmp;
  487. spin_lock_irq(&kvm->irqfds.lock);
  488. list_for_each_entry_safe(irqfd, tmp, &kvm->irqfds.items, list)
  489. irqfd_deactivate(irqfd);
  490. spin_unlock_irq(&kvm->irqfds.lock);
  491. /*
  492. * Block until we know all outstanding shutdown jobs have completed
  493. * since we do not take a kvm* reference.
  494. */
  495. flush_workqueue(irqfd_cleanup_wq);
  496. }
  497. /*
  498. * Take note of a change in irq routing.
  499. * Caller must invoke synchronize_srcu(&kvm->irq_srcu) afterwards.
  500. */
  501. void kvm_irq_routing_update(struct kvm *kvm)
  502. {
  503. struct _irqfd *irqfd;
  504. spin_lock_irq(&kvm->irqfds.lock);
  505. list_for_each_entry(irqfd, &kvm->irqfds.items, list)
  506. irqfd_update(kvm, irqfd);
  507. spin_unlock_irq(&kvm->irqfds.lock);
  508. }
  509. /*
  510. * create a host-wide workqueue for issuing deferred shutdown requests
  511. * aggregated from all vm* instances. We need our own isolated single-thread
  512. * queue to prevent deadlock against flushing the normal work-queue.
  513. */
  514. int kvm_irqfd_init(void)
  515. {
  516. irqfd_cleanup_wq = create_singlethread_workqueue("kvm-irqfd-cleanup");
  517. if (!irqfd_cleanup_wq)
  518. return -ENOMEM;
  519. return 0;
  520. }
  521. void kvm_irqfd_exit(void)
  522. {
  523. destroy_workqueue(irqfd_cleanup_wq);
  524. }
  525. #endif
  526. /*
  527. * --------------------------------------------------------------------
  528. * ioeventfd: translate a PIO/MMIO memory write to an eventfd signal.
  529. *
  530. * userspace can register a PIO/MMIO address with an eventfd for receiving
  531. * notification when the memory has been touched.
  532. * --------------------------------------------------------------------
  533. */
  534. struct _ioeventfd {
  535. struct list_head list;
  536. u64 addr;
  537. int length;
  538. struct eventfd_ctx *eventfd;
  539. u64 datamatch;
  540. struct kvm_io_device dev;
  541. u8 bus_idx;
  542. bool wildcard;
  543. };
  544. static inline struct _ioeventfd *
  545. to_ioeventfd(struct kvm_io_device *dev)
  546. {
  547. return container_of(dev, struct _ioeventfd, dev);
  548. }
  549. static void
  550. ioeventfd_release(struct _ioeventfd *p)
  551. {
  552. eventfd_ctx_put(p->eventfd);
  553. list_del(&p->list);
  554. kfree(p);
  555. }
  556. static bool
  557. ioeventfd_in_range(struct _ioeventfd *p, gpa_t addr, int len, const void *val)
  558. {
  559. u64 _val;
  560. if (addr != p->addr)
  561. /* address must be precise for a hit */
  562. return false;
  563. if (!p->length)
  564. /* length = 0 means only look at the address, so always a hit */
  565. return true;
  566. if (len != p->length)
  567. /* address-range must be precise for a hit */
  568. return false;
  569. if (p->wildcard)
  570. /* all else equal, wildcard is always a hit */
  571. return true;
  572. /* otherwise, we have to actually compare the data */
  573. BUG_ON(!IS_ALIGNED((unsigned long)val, len));
  574. switch (len) {
  575. case 1:
  576. _val = *(u8 *)val;
  577. break;
  578. case 2:
  579. _val = *(u16 *)val;
  580. break;
  581. case 4:
  582. _val = *(u32 *)val;
  583. break;
  584. case 8:
  585. _val = *(u64 *)val;
  586. break;
  587. default:
  588. return false;
  589. }
  590. return _val == p->datamatch ? true : false;
  591. }
  592. /* MMIO/PIO writes trigger an event if the addr/val match */
  593. static int
  594. ioeventfd_write(struct kvm_io_device *this, gpa_t addr, int len,
  595. const void *val)
  596. {
  597. struct _ioeventfd *p = to_ioeventfd(this);
  598. if (!ioeventfd_in_range(p, addr, len, val))
  599. return -EOPNOTSUPP;
  600. eventfd_signal(p->eventfd, 1);
  601. return 0;
  602. }
  603. /*
  604. * This function is called as KVM is completely shutting down. We do not
  605. * need to worry about locking just nuke anything we have as quickly as possible
  606. */
  607. static void
  608. ioeventfd_destructor(struct kvm_io_device *this)
  609. {
  610. struct _ioeventfd *p = to_ioeventfd(this);
  611. ioeventfd_release(p);
  612. }
  613. static const struct kvm_io_device_ops ioeventfd_ops = {
  614. .write = ioeventfd_write,
  615. .destructor = ioeventfd_destructor,
  616. };
  617. /* assumes kvm->slots_lock held */
  618. static bool
  619. ioeventfd_check_collision(struct kvm *kvm, struct _ioeventfd *p)
  620. {
  621. struct _ioeventfd *_p;
  622. list_for_each_entry(_p, &kvm->ioeventfds, list)
  623. if (_p->bus_idx == p->bus_idx &&
  624. _p->addr == p->addr &&
  625. (!_p->length || !p->length ||
  626. (_p->length == p->length &&
  627. (_p->wildcard || p->wildcard ||
  628. _p->datamatch == p->datamatch))))
  629. return true;
  630. return false;
  631. }
  632. static enum kvm_bus ioeventfd_bus_from_flags(__u32 flags)
  633. {
  634. if (flags & KVM_IOEVENTFD_FLAG_PIO)
  635. return KVM_PIO_BUS;
  636. if (flags & KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY)
  637. return KVM_VIRTIO_CCW_NOTIFY_BUS;
  638. return KVM_MMIO_BUS;
  639. }
  640. static int
  641. kvm_assign_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
  642. {
  643. enum kvm_bus bus_idx;
  644. struct _ioeventfd *p;
  645. struct eventfd_ctx *eventfd;
  646. int ret;
  647. bus_idx = ioeventfd_bus_from_flags(args->flags);
  648. /* must be natural-word sized, or 0 to ignore length */
  649. switch (args->len) {
  650. case 0:
  651. case 1:
  652. case 2:
  653. case 4:
  654. case 8:
  655. break;
  656. default:
  657. return -EINVAL;
  658. }
  659. /* check for range overflow */
  660. if (args->addr + args->len < args->addr)
  661. return -EINVAL;
  662. /* check for extra flags that we don't understand */
  663. if (args->flags & ~KVM_IOEVENTFD_VALID_FLAG_MASK)
  664. return -EINVAL;
  665. /* ioeventfd with no length can't be combined with DATAMATCH */
  666. if (!args->len &&
  667. args->flags & (KVM_IOEVENTFD_FLAG_PIO |
  668. KVM_IOEVENTFD_FLAG_DATAMATCH))
  669. return -EINVAL;
  670. eventfd = eventfd_ctx_fdget(args->fd);
  671. if (IS_ERR(eventfd))
  672. return PTR_ERR(eventfd);
  673. p = kzalloc(sizeof(*p), GFP_KERNEL);
  674. if (!p) {
  675. ret = -ENOMEM;
  676. goto fail;
  677. }
  678. INIT_LIST_HEAD(&p->list);
  679. p->addr = args->addr;
  680. p->bus_idx = bus_idx;
  681. p->length = args->len;
  682. p->eventfd = eventfd;
  683. /* The datamatch feature is optional, otherwise this is a wildcard */
  684. if (args->flags & KVM_IOEVENTFD_FLAG_DATAMATCH)
  685. p->datamatch = args->datamatch;
  686. else
  687. p->wildcard = true;
  688. mutex_lock(&kvm->slots_lock);
  689. /* Verify that there isn't a match already */
  690. if (ioeventfd_check_collision(kvm, p)) {
  691. ret = -EEXIST;
  692. goto unlock_fail;
  693. }
  694. kvm_iodevice_init(&p->dev, &ioeventfd_ops);
  695. ret = kvm_io_bus_register_dev(kvm, bus_idx, p->addr, p->length,
  696. &p->dev);
  697. if (ret < 0)
  698. goto unlock_fail;
  699. /* When length is ignored, MMIO is also put on a separate bus, for
  700. * faster lookups.
  701. */
  702. if (!args->len && !(args->flags & KVM_IOEVENTFD_FLAG_PIO)) {
  703. ret = kvm_io_bus_register_dev(kvm, KVM_FAST_MMIO_BUS,
  704. p->addr, 0, &p->dev);
  705. if (ret < 0)
  706. goto register_fail;
  707. }
  708. kvm->buses[bus_idx]->ioeventfd_count++;
  709. list_add_tail(&p->list, &kvm->ioeventfds);
  710. mutex_unlock(&kvm->slots_lock);
  711. return 0;
  712. register_fail:
  713. kvm_io_bus_unregister_dev(kvm, bus_idx, &p->dev);
  714. unlock_fail:
  715. mutex_unlock(&kvm->slots_lock);
  716. fail:
  717. kfree(p);
  718. eventfd_ctx_put(eventfd);
  719. return ret;
  720. }
  721. static int
  722. kvm_deassign_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
  723. {
  724. enum kvm_bus bus_idx;
  725. struct _ioeventfd *p, *tmp;
  726. struct eventfd_ctx *eventfd;
  727. int ret = -ENOENT;
  728. bus_idx = ioeventfd_bus_from_flags(args->flags);
  729. eventfd = eventfd_ctx_fdget(args->fd);
  730. if (IS_ERR(eventfd))
  731. return PTR_ERR(eventfd);
  732. mutex_lock(&kvm->slots_lock);
  733. list_for_each_entry_safe(p, tmp, &kvm->ioeventfds, list) {
  734. bool wildcard = !(args->flags & KVM_IOEVENTFD_FLAG_DATAMATCH);
  735. if (p->bus_idx != bus_idx ||
  736. p->eventfd != eventfd ||
  737. p->addr != args->addr ||
  738. p->length != args->len ||
  739. p->wildcard != wildcard)
  740. continue;
  741. if (!p->wildcard && p->datamatch != args->datamatch)
  742. continue;
  743. kvm_io_bus_unregister_dev(kvm, bus_idx, &p->dev);
  744. if (!p->length) {
  745. kvm_io_bus_unregister_dev(kvm, KVM_FAST_MMIO_BUS,
  746. &p->dev);
  747. }
  748. kvm->buses[bus_idx]->ioeventfd_count--;
  749. ioeventfd_release(p);
  750. ret = 0;
  751. break;
  752. }
  753. mutex_unlock(&kvm->slots_lock);
  754. eventfd_ctx_put(eventfd);
  755. return ret;
  756. }
  757. int
  758. kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
  759. {
  760. if (args->flags & KVM_IOEVENTFD_FLAG_DEASSIGN)
  761. return kvm_deassign_ioeventfd(kvm, args);
  762. return kvm_assign_ioeventfd(kvm, args);
  763. }