eventfd.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  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/slab.h>
  34. #include "iodev.h"
  35. #ifdef __KVM_HAVE_IOAPIC
  36. /*
  37. * --------------------------------------------------------------------
  38. * irqfd: Allows an fd to be used to inject an interrupt to the guest
  39. *
  40. * Credit goes to Avi Kivity for the original idea.
  41. * --------------------------------------------------------------------
  42. */
  43. /*
  44. * Resampling irqfds are a special variety of irqfds used to emulate
  45. * level triggered interrupts. The interrupt is asserted on eventfd
  46. * trigger. On acknowledgement through the irq ack notifier, the
  47. * interrupt is de-asserted and userspace is notified through the
  48. * resamplefd. All resamplers on the same gsi are de-asserted
  49. * together, so we don't need to track the state of each individual
  50. * user. We can also therefore share the same irq source ID.
  51. */
  52. struct _irqfd_resampler {
  53. struct kvm *kvm;
  54. /*
  55. * List of resampling struct _irqfd objects sharing this gsi.
  56. * RCU list modified under kvm->irqfds.resampler_lock
  57. */
  58. struct list_head list;
  59. struct kvm_irq_ack_notifier notifier;
  60. /*
  61. * Entry in list of kvm->irqfd.resampler_list. Use for sharing
  62. * resamplers among irqfds on the same gsi.
  63. * Accessed and modified under kvm->irqfds.resampler_lock
  64. */
  65. struct list_head link;
  66. };
  67. struct _irqfd {
  68. /* Used for MSI fast-path */
  69. struct kvm *kvm;
  70. wait_queue_t wait;
  71. /* Update side is protected by irqfds.lock */
  72. struct kvm_kernel_irq_routing_entry __rcu *irq_entry;
  73. /* Used for level IRQ fast-path */
  74. int gsi;
  75. struct work_struct inject;
  76. /* The resampler used by this irqfd (resampler-only) */
  77. struct _irqfd_resampler *resampler;
  78. /* Eventfd notified on resample (resampler-only) */
  79. struct eventfd_ctx *resamplefd;
  80. /* Entry in list of irqfds for a resampler (resampler-only) */
  81. struct list_head resampler_link;
  82. /* Used for setup/shutdown */
  83. struct eventfd_ctx *eventfd;
  84. struct list_head list;
  85. poll_table pt;
  86. struct work_struct shutdown;
  87. };
  88. static struct workqueue_struct *irqfd_cleanup_wq;
  89. static void
  90. irqfd_inject(struct work_struct *work)
  91. {
  92. struct _irqfd *irqfd = container_of(work, struct _irqfd, inject);
  93. struct kvm *kvm = irqfd->kvm;
  94. if (!irqfd->resampler) {
  95. kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, irqfd->gsi, 1);
  96. kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, irqfd->gsi, 0);
  97. } else
  98. kvm_set_irq(kvm, KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID,
  99. irqfd->gsi, 1);
  100. }
  101. /*
  102. * Since resampler irqfds share an IRQ source ID, we de-assert once
  103. * then notify all of the resampler irqfds using this GSI. We can't
  104. * do multiple de-asserts or we risk racing with incoming re-asserts.
  105. */
  106. static void
  107. irqfd_resampler_ack(struct kvm_irq_ack_notifier *kian)
  108. {
  109. struct _irqfd_resampler *resampler;
  110. struct _irqfd *irqfd;
  111. resampler = container_of(kian, struct _irqfd_resampler, notifier);
  112. kvm_set_irq(resampler->kvm, KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID,
  113. resampler->notifier.gsi, 0);
  114. rcu_read_lock();
  115. list_for_each_entry_rcu(irqfd, &resampler->list, resampler_link)
  116. eventfd_signal(irqfd->resamplefd, 1);
  117. rcu_read_unlock();
  118. }
  119. static void
  120. irqfd_resampler_shutdown(struct _irqfd *irqfd)
  121. {
  122. struct _irqfd_resampler *resampler = irqfd->resampler;
  123. struct kvm *kvm = resampler->kvm;
  124. mutex_lock(&kvm->irqfds.resampler_lock);
  125. list_del_rcu(&irqfd->resampler_link);
  126. synchronize_rcu();
  127. if (list_empty(&resampler->list)) {
  128. list_del(&resampler->link);
  129. kvm_unregister_irq_ack_notifier(kvm, &resampler->notifier);
  130. kvm_set_irq(kvm, KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID,
  131. resampler->notifier.gsi, 0);
  132. kfree(resampler);
  133. }
  134. mutex_unlock(&kvm->irqfds.resampler_lock);
  135. }
  136. /*
  137. * Race-free decouple logic (ordering is critical)
  138. */
  139. static void
  140. irqfd_shutdown(struct work_struct *work)
  141. {
  142. struct _irqfd *irqfd = container_of(work, struct _irqfd, shutdown);
  143. u64 cnt;
  144. /*
  145. * Synchronize with the wait-queue and unhook ourselves to prevent
  146. * further events.
  147. */
  148. eventfd_ctx_remove_wait_queue(irqfd->eventfd, &irqfd->wait, &cnt);
  149. /*
  150. * We know no new events will be scheduled at this point, so block
  151. * until all previously outstanding events have completed
  152. */
  153. flush_work(&irqfd->inject);
  154. if (irqfd->resampler) {
  155. irqfd_resampler_shutdown(irqfd);
  156. eventfd_ctx_put(irqfd->resamplefd);
  157. }
  158. /*
  159. * It is now safe to release the object's resources
  160. */
  161. eventfd_ctx_put(irqfd->eventfd);
  162. kfree(irqfd);
  163. }
  164. /* assumes kvm->irqfds.lock is held */
  165. static bool
  166. irqfd_is_active(struct _irqfd *irqfd)
  167. {
  168. return list_empty(&irqfd->list) ? false : true;
  169. }
  170. /*
  171. * Mark the irqfd as inactive and schedule it for removal
  172. *
  173. * assumes kvm->irqfds.lock is held
  174. */
  175. static void
  176. irqfd_deactivate(struct _irqfd *irqfd)
  177. {
  178. BUG_ON(!irqfd_is_active(irqfd));
  179. list_del_init(&irqfd->list);
  180. queue_work(irqfd_cleanup_wq, &irqfd->shutdown);
  181. }
  182. /*
  183. * Called with wqh->lock held and interrupts disabled
  184. */
  185. static int
  186. irqfd_wakeup(wait_queue_t *wait, unsigned mode, int sync, void *key)
  187. {
  188. struct _irqfd *irqfd = container_of(wait, struct _irqfd, wait);
  189. unsigned long flags = (unsigned long)key;
  190. struct kvm_kernel_irq_routing_entry *irq;
  191. struct kvm *kvm = irqfd->kvm;
  192. if (flags & POLLIN) {
  193. rcu_read_lock();
  194. irq = rcu_dereference(irqfd->irq_entry);
  195. /* An event has been signaled, inject an interrupt */
  196. if (irq)
  197. kvm_set_msi(irq, kvm, KVM_USERSPACE_IRQ_SOURCE_ID, 1);
  198. else
  199. schedule_work(&irqfd->inject);
  200. rcu_read_unlock();
  201. }
  202. if (flags & POLLHUP) {
  203. /* The eventfd is closing, detach from KVM */
  204. unsigned long flags;
  205. spin_lock_irqsave(&kvm->irqfds.lock, flags);
  206. /*
  207. * We must check if someone deactivated the irqfd before
  208. * we could acquire the irqfds.lock since the item is
  209. * deactivated from the KVM side before it is unhooked from
  210. * the wait-queue. If it is already deactivated, we can
  211. * simply return knowing the other side will cleanup for us.
  212. * We cannot race against the irqfd going away since the
  213. * other side is required to acquire wqh->lock, which we hold
  214. */
  215. if (irqfd_is_active(irqfd))
  216. irqfd_deactivate(irqfd);
  217. spin_unlock_irqrestore(&kvm->irqfds.lock, flags);
  218. }
  219. return 0;
  220. }
  221. static void
  222. irqfd_ptable_queue_proc(struct file *file, wait_queue_head_t *wqh,
  223. poll_table *pt)
  224. {
  225. struct _irqfd *irqfd = container_of(pt, struct _irqfd, pt);
  226. add_wait_queue(wqh, &irqfd->wait);
  227. }
  228. /* Must be called under irqfds.lock */
  229. static void irqfd_update(struct kvm *kvm, struct _irqfd *irqfd,
  230. struct kvm_irq_routing_table *irq_rt)
  231. {
  232. struct kvm_kernel_irq_routing_entry *e;
  233. if (irqfd->gsi >= irq_rt->nr_rt_entries) {
  234. rcu_assign_pointer(irqfd->irq_entry, NULL);
  235. return;
  236. }
  237. hlist_for_each_entry(e, &irq_rt->map[irqfd->gsi], link) {
  238. /* Only fast-path MSI. */
  239. if (e->type == KVM_IRQ_ROUTING_MSI)
  240. rcu_assign_pointer(irqfd->irq_entry, e);
  241. else
  242. rcu_assign_pointer(irqfd->irq_entry, NULL);
  243. }
  244. }
  245. static int
  246. kvm_irqfd_assign(struct kvm *kvm, struct kvm_irqfd *args)
  247. {
  248. struct kvm_irq_routing_table *irq_rt;
  249. struct _irqfd *irqfd, *tmp;
  250. struct file *file = NULL;
  251. struct eventfd_ctx *eventfd = NULL, *resamplefd = NULL;
  252. int ret;
  253. unsigned int events;
  254. irqfd = kzalloc(sizeof(*irqfd), GFP_KERNEL);
  255. if (!irqfd)
  256. return -ENOMEM;
  257. irqfd->kvm = kvm;
  258. irqfd->gsi = args->gsi;
  259. INIT_LIST_HEAD(&irqfd->list);
  260. INIT_WORK(&irqfd->inject, irqfd_inject);
  261. INIT_WORK(&irqfd->shutdown, irqfd_shutdown);
  262. file = eventfd_fget(args->fd);
  263. if (IS_ERR(file)) {
  264. ret = PTR_ERR(file);
  265. goto fail;
  266. }
  267. eventfd = eventfd_ctx_fileget(file);
  268. if (IS_ERR(eventfd)) {
  269. ret = PTR_ERR(eventfd);
  270. goto fail;
  271. }
  272. irqfd->eventfd = eventfd;
  273. if (args->flags & KVM_IRQFD_FLAG_RESAMPLE) {
  274. struct _irqfd_resampler *resampler;
  275. resamplefd = eventfd_ctx_fdget(args->resamplefd);
  276. if (IS_ERR(resamplefd)) {
  277. ret = PTR_ERR(resamplefd);
  278. goto fail;
  279. }
  280. irqfd->resamplefd = resamplefd;
  281. INIT_LIST_HEAD(&irqfd->resampler_link);
  282. mutex_lock(&kvm->irqfds.resampler_lock);
  283. list_for_each_entry(resampler,
  284. &kvm->irqfds.resampler_list, link) {
  285. if (resampler->notifier.gsi == irqfd->gsi) {
  286. irqfd->resampler = resampler;
  287. break;
  288. }
  289. }
  290. if (!irqfd->resampler) {
  291. resampler = kzalloc(sizeof(*resampler), GFP_KERNEL);
  292. if (!resampler) {
  293. ret = -ENOMEM;
  294. mutex_unlock(&kvm->irqfds.resampler_lock);
  295. goto fail;
  296. }
  297. resampler->kvm = kvm;
  298. INIT_LIST_HEAD(&resampler->list);
  299. resampler->notifier.gsi = irqfd->gsi;
  300. resampler->notifier.irq_acked = irqfd_resampler_ack;
  301. INIT_LIST_HEAD(&resampler->link);
  302. list_add(&resampler->link, &kvm->irqfds.resampler_list);
  303. kvm_register_irq_ack_notifier(kvm,
  304. &resampler->notifier);
  305. irqfd->resampler = resampler;
  306. }
  307. list_add_rcu(&irqfd->resampler_link, &irqfd->resampler->list);
  308. synchronize_rcu();
  309. mutex_unlock(&kvm->irqfds.resampler_lock);
  310. }
  311. /*
  312. * Install our own custom wake-up handling so we are notified via
  313. * a callback whenever someone signals the underlying eventfd
  314. */
  315. init_waitqueue_func_entry(&irqfd->wait, irqfd_wakeup);
  316. init_poll_funcptr(&irqfd->pt, irqfd_ptable_queue_proc);
  317. spin_lock_irq(&kvm->irqfds.lock);
  318. ret = 0;
  319. list_for_each_entry(tmp, &kvm->irqfds.items, list) {
  320. if (irqfd->eventfd != tmp->eventfd)
  321. continue;
  322. /* This fd is used for another irq already. */
  323. ret = -EBUSY;
  324. spin_unlock_irq(&kvm->irqfds.lock);
  325. goto fail;
  326. }
  327. irq_rt = rcu_dereference_protected(kvm->irq_routing,
  328. lockdep_is_held(&kvm->irqfds.lock));
  329. irqfd_update(kvm, irqfd, irq_rt);
  330. events = file->f_op->poll(file, &irqfd->pt);
  331. list_add_tail(&irqfd->list, &kvm->irqfds.items);
  332. /*
  333. * Check if there was an event already pending on the eventfd
  334. * before we registered, and trigger it as if we didn't miss it.
  335. */
  336. if (events & POLLIN)
  337. schedule_work(&irqfd->inject);
  338. spin_unlock_irq(&kvm->irqfds.lock);
  339. /*
  340. * do not drop the file until the irqfd is fully initialized, otherwise
  341. * we might race against the POLLHUP
  342. */
  343. fput(file);
  344. return 0;
  345. fail:
  346. if (irqfd->resampler)
  347. irqfd_resampler_shutdown(irqfd);
  348. if (resamplefd && !IS_ERR(resamplefd))
  349. eventfd_ctx_put(resamplefd);
  350. if (eventfd && !IS_ERR(eventfd))
  351. eventfd_ctx_put(eventfd);
  352. if (!IS_ERR(file))
  353. fput(file);
  354. kfree(irqfd);
  355. return ret;
  356. }
  357. #endif
  358. void
  359. kvm_eventfd_init(struct kvm *kvm)
  360. {
  361. #ifdef __KVM_HAVE_IOAPIC
  362. spin_lock_init(&kvm->irqfds.lock);
  363. INIT_LIST_HEAD(&kvm->irqfds.items);
  364. INIT_LIST_HEAD(&kvm->irqfds.resampler_list);
  365. mutex_init(&kvm->irqfds.resampler_lock);
  366. #endif
  367. INIT_LIST_HEAD(&kvm->ioeventfds);
  368. }
  369. #ifdef __KVM_HAVE_IOAPIC
  370. /*
  371. * shutdown any irqfd's that match fd+gsi
  372. */
  373. static int
  374. kvm_irqfd_deassign(struct kvm *kvm, struct kvm_irqfd *args)
  375. {
  376. struct _irqfd *irqfd, *tmp;
  377. struct eventfd_ctx *eventfd;
  378. eventfd = eventfd_ctx_fdget(args->fd);
  379. if (IS_ERR(eventfd))
  380. return PTR_ERR(eventfd);
  381. spin_lock_irq(&kvm->irqfds.lock);
  382. list_for_each_entry_safe(irqfd, tmp, &kvm->irqfds.items, list) {
  383. if (irqfd->eventfd == eventfd && irqfd->gsi == args->gsi) {
  384. /*
  385. * This rcu_assign_pointer is needed for when
  386. * another thread calls kvm_irq_routing_update before
  387. * we flush workqueue below (we synchronize with
  388. * kvm_irq_routing_update using irqfds.lock).
  389. * It is paired with synchronize_rcu done by caller
  390. * of that function.
  391. */
  392. rcu_assign_pointer(irqfd->irq_entry, NULL);
  393. irqfd_deactivate(irqfd);
  394. }
  395. }
  396. spin_unlock_irq(&kvm->irqfds.lock);
  397. eventfd_ctx_put(eventfd);
  398. /*
  399. * Block until we know all outstanding shutdown jobs have completed
  400. * so that we guarantee there will not be any more interrupts on this
  401. * gsi once this deassign function returns.
  402. */
  403. flush_workqueue(irqfd_cleanup_wq);
  404. return 0;
  405. }
  406. int
  407. kvm_irqfd(struct kvm *kvm, struct kvm_irqfd *args)
  408. {
  409. if (args->flags & ~(KVM_IRQFD_FLAG_DEASSIGN | KVM_IRQFD_FLAG_RESAMPLE))
  410. return -EINVAL;
  411. if (args->flags & KVM_IRQFD_FLAG_DEASSIGN)
  412. return kvm_irqfd_deassign(kvm, args);
  413. return kvm_irqfd_assign(kvm, args);
  414. }
  415. /*
  416. * This function is called as the kvm VM fd is being released. Shutdown all
  417. * irqfds that still remain open
  418. */
  419. void
  420. kvm_irqfd_release(struct kvm *kvm)
  421. {
  422. struct _irqfd *irqfd, *tmp;
  423. spin_lock_irq(&kvm->irqfds.lock);
  424. list_for_each_entry_safe(irqfd, tmp, &kvm->irqfds.items, list)
  425. irqfd_deactivate(irqfd);
  426. spin_unlock_irq(&kvm->irqfds.lock);
  427. /*
  428. * Block until we know all outstanding shutdown jobs have completed
  429. * since we do not take a kvm* reference.
  430. */
  431. flush_workqueue(irqfd_cleanup_wq);
  432. }
  433. /*
  434. * Change irq_routing and irqfd.
  435. * Caller must invoke synchronize_rcu afterwards.
  436. */
  437. void kvm_irq_routing_update(struct kvm *kvm,
  438. struct kvm_irq_routing_table *irq_rt)
  439. {
  440. struct _irqfd *irqfd;
  441. spin_lock_irq(&kvm->irqfds.lock);
  442. rcu_assign_pointer(kvm->irq_routing, irq_rt);
  443. list_for_each_entry(irqfd, &kvm->irqfds.items, list)
  444. irqfd_update(kvm, irqfd, irq_rt);
  445. spin_unlock_irq(&kvm->irqfds.lock);
  446. }
  447. /*
  448. * create a host-wide workqueue for issuing deferred shutdown requests
  449. * aggregated from all vm* instances. We need our own isolated single-thread
  450. * queue to prevent deadlock against flushing the normal work-queue.
  451. */
  452. static int __init irqfd_module_init(void)
  453. {
  454. irqfd_cleanup_wq = create_singlethread_workqueue("kvm-irqfd-cleanup");
  455. if (!irqfd_cleanup_wq)
  456. return -ENOMEM;
  457. return 0;
  458. }
  459. static void __exit irqfd_module_exit(void)
  460. {
  461. destroy_workqueue(irqfd_cleanup_wq);
  462. }
  463. module_init(irqfd_module_init);
  464. module_exit(irqfd_module_exit);
  465. #endif
  466. /*
  467. * --------------------------------------------------------------------
  468. * ioeventfd: translate a PIO/MMIO memory write to an eventfd signal.
  469. *
  470. * userspace can register a PIO/MMIO address with an eventfd for receiving
  471. * notification when the memory has been touched.
  472. * --------------------------------------------------------------------
  473. */
  474. struct _ioeventfd {
  475. struct list_head list;
  476. u64 addr;
  477. int length;
  478. struct eventfd_ctx *eventfd;
  479. u64 datamatch;
  480. struct kvm_io_device dev;
  481. bool wildcard;
  482. };
  483. static inline struct _ioeventfd *
  484. to_ioeventfd(struct kvm_io_device *dev)
  485. {
  486. return container_of(dev, struct _ioeventfd, dev);
  487. }
  488. static void
  489. ioeventfd_release(struct _ioeventfd *p)
  490. {
  491. eventfd_ctx_put(p->eventfd);
  492. list_del(&p->list);
  493. kfree(p);
  494. }
  495. static bool
  496. ioeventfd_in_range(struct _ioeventfd *p, gpa_t addr, int len, const void *val)
  497. {
  498. u64 _val;
  499. if (!(addr == p->addr && len == p->length))
  500. /* address-range must be precise for a hit */
  501. return false;
  502. if (p->wildcard)
  503. /* all else equal, wildcard is always a hit */
  504. return true;
  505. /* otherwise, we have to actually compare the data */
  506. BUG_ON(!IS_ALIGNED((unsigned long)val, len));
  507. switch (len) {
  508. case 1:
  509. _val = *(u8 *)val;
  510. break;
  511. case 2:
  512. _val = *(u16 *)val;
  513. break;
  514. case 4:
  515. _val = *(u32 *)val;
  516. break;
  517. case 8:
  518. _val = *(u64 *)val;
  519. break;
  520. default:
  521. return false;
  522. }
  523. return _val == p->datamatch ? true : false;
  524. }
  525. /* MMIO/PIO writes trigger an event if the addr/val match */
  526. static int
  527. ioeventfd_write(struct kvm_io_device *this, gpa_t addr, int len,
  528. const void *val)
  529. {
  530. struct _ioeventfd *p = to_ioeventfd(this);
  531. if (!ioeventfd_in_range(p, addr, len, val))
  532. return -EOPNOTSUPP;
  533. eventfd_signal(p->eventfd, 1);
  534. return 0;
  535. }
  536. /*
  537. * This function is called as KVM is completely shutting down. We do not
  538. * need to worry about locking just nuke anything we have as quickly as possible
  539. */
  540. static void
  541. ioeventfd_destructor(struct kvm_io_device *this)
  542. {
  543. struct _ioeventfd *p = to_ioeventfd(this);
  544. ioeventfd_release(p);
  545. }
  546. static const struct kvm_io_device_ops ioeventfd_ops = {
  547. .write = ioeventfd_write,
  548. .destructor = ioeventfd_destructor,
  549. };
  550. /* assumes kvm->slots_lock held */
  551. static bool
  552. ioeventfd_check_collision(struct kvm *kvm, struct _ioeventfd *p)
  553. {
  554. struct _ioeventfd *_p;
  555. list_for_each_entry(_p, &kvm->ioeventfds, list)
  556. if (_p->addr == p->addr && _p->length == p->length &&
  557. (_p->wildcard || p->wildcard ||
  558. _p->datamatch == p->datamatch))
  559. return true;
  560. return false;
  561. }
  562. static int
  563. kvm_assign_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
  564. {
  565. int pio = args->flags & KVM_IOEVENTFD_FLAG_PIO;
  566. enum kvm_bus bus_idx = pio ? KVM_PIO_BUS : KVM_MMIO_BUS;
  567. struct _ioeventfd *p;
  568. struct eventfd_ctx *eventfd;
  569. int ret;
  570. /* must be natural-word sized */
  571. switch (args->len) {
  572. case 1:
  573. case 2:
  574. case 4:
  575. case 8:
  576. break;
  577. default:
  578. return -EINVAL;
  579. }
  580. /* check for range overflow */
  581. if (args->addr + args->len < args->addr)
  582. return -EINVAL;
  583. /* check for extra flags that we don't understand */
  584. if (args->flags & ~KVM_IOEVENTFD_VALID_FLAG_MASK)
  585. return -EINVAL;
  586. eventfd = eventfd_ctx_fdget(args->fd);
  587. if (IS_ERR(eventfd))
  588. return PTR_ERR(eventfd);
  589. p = kzalloc(sizeof(*p), GFP_KERNEL);
  590. if (!p) {
  591. ret = -ENOMEM;
  592. goto fail;
  593. }
  594. INIT_LIST_HEAD(&p->list);
  595. p->addr = args->addr;
  596. p->length = args->len;
  597. p->eventfd = eventfd;
  598. /* The datamatch feature is optional, otherwise this is a wildcard */
  599. if (args->flags & KVM_IOEVENTFD_FLAG_DATAMATCH)
  600. p->datamatch = args->datamatch;
  601. else
  602. p->wildcard = true;
  603. mutex_lock(&kvm->slots_lock);
  604. /* Verify that there isn't a match already */
  605. if (ioeventfd_check_collision(kvm, p)) {
  606. ret = -EEXIST;
  607. goto unlock_fail;
  608. }
  609. kvm_iodevice_init(&p->dev, &ioeventfd_ops);
  610. ret = kvm_io_bus_register_dev(kvm, bus_idx, p->addr, p->length,
  611. &p->dev);
  612. if (ret < 0)
  613. goto unlock_fail;
  614. list_add_tail(&p->list, &kvm->ioeventfds);
  615. mutex_unlock(&kvm->slots_lock);
  616. return 0;
  617. unlock_fail:
  618. mutex_unlock(&kvm->slots_lock);
  619. fail:
  620. kfree(p);
  621. eventfd_ctx_put(eventfd);
  622. return ret;
  623. }
  624. static int
  625. kvm_deassign_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
  626. {
  627. int pio = args->flags & KVM_IOEVENTFD_FLAG_PIO;
  628. enum kvm_bus bus_idx = pio ? KVM_PIO_BUS : KVM_MMIO_BUS;
  629. struct _ioeventfd *p, *tmp;
  630. struct eventfd_ctx *eventfd;
  631. int ret = -ENOENT;
  632. eventfd = eventfd_ctx_fdget(args->fd);
  633. if (IS_ERR(eventfd))
  634. return PTR_ERR(eventfd);
  635. mutex_lock(&kvm->slots_lock);
  636. list_for_each_entry_safe(p, tmp, &kvm->ioeventfds, list) {
  637. bool wildcard = !(args->flags & KVM_IOEVENTFD_FLAG_DATAMATCH);
  638. if (p->eventfd != eventfd ||
  639. p->addr != args->addr ||
  640. p->length != args->len ||
  641. p->wildcard != wildcard)
  642. continue;
  643. if (!p->wildcard && p->datamatch != args->datamatch)
  644. continue;
  645. kvm_io_bus_unregister_dev(kvm, bus_idx, &p->dev);
  646. ioeventfd_release(p);
  647. ret = 0;
  648. break;
  649. }
  650. mutex_unlock(&kvm->slots_lock);
  651. eventfd_ctx_put(eventfd);
  652. return ret;
  653. }
  654. int
  655. kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
  656. {
  657. if (args->flags & KVM_IOEVENTFD_FLAG_DEASSIGN)
  658. return kvm_deassign_ioeventfd(kvm, args);
  659. return kvm_assign_ioeventfd(kvm, args);
  660. }