eventfd.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  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. struct hlist_node *n;
  234. if (irqfd->gsi >= irq_rt->nr_rt_entries) {
  235. rcu_assign_pointer(irqfd->irq_entry, NULL);
  236. return;
  237. }
  238. hlist_for_each_entry(e, n, &irq_rt->map[irqfd->gsi], link) {
  239. /* Only fast-path MSI. */
  240. if (e->type == KVM_IRQ_ROUTING_MSI)
  241. rcu_assign_pointer(irqfd->irq_entry, e);
  242. else
  243. rcu_assign_pointer(irqfd->irq_entry, NULL);
  244. }
  245. }
  246. static int
  247. kvm_irqfd_assign(struct kvm *kvm, struct kvm_irqfd *args)
  248. {
  249. struct kvm_irq_routing_table *irq_rt;
  250. struct _irqfd *irqfd, *tmp;
  251. struct file *file = NULL;
  252. struct eventfd_ctx *eventfd = NULL, *resamplefd = NULL;
  253. int ret;
  254. unsigned int events;
  255. irqfd = kzalloc(sizeof(*irqfd), GFP_KERNEL);
  256. if (!irqfd)
  257. return -ENOMEM;
  258. irqfd->kvm = kvm;
  259. irqfd->gsi = args->gsi;
  260. INIT_LIST_HEAD(&irqfd->list);
  261. INIT_WORK(&irqfd->inject, irqfd_inject);
  262. INIT_WORK(&irqfd->shutdown, irqfd_shutdown);
  263. file = eventfd_fget(args->fd);
  264. if (IS_ERR(file)) {
  265. ret = PTR_ERR(file);
  266. goto fail;
  267. }
  268. eventfd = eventfd_ctx_fileget(file);
  269. if (IS_ERR(eventfd)) {
  270. ret = PTR_ERR(eventfd);
  271. goto fail;
  272. }
  273. irqfd->eventfd = eventfd;
  274. if (args->flags & KVM_IRQFD_FLAG_RESAMPLE) {
  275. struct _irqfd_resampler *resampler;
  276. resamplefd = eventfd_ctx_fdget(args->resamplefd);
  277. if (IS_ERR(resamplefd)) {
  278. ret = PTR_ERR(resamplefd);
  279. goto fail;
  280. }
  281. irqfd->resamplefd = resamplefd;
  282. INIT_LIST_HEAD(&irqfd->resampler_link);
  283. mutex_lock(&kvm->irqfds.resampler_lock);
  284. list_for_each_entry(resampler,
  285. &kvm->irqfds.resampler_list, link) {
  286. if (resampler->notifier.gsi == irqfd->gsi) {
  287. irqfd->resampler = resampler;
  288. break;
  289. }
  290. }
  291. if (!irqfd->resampler) {
  292. resampler = kzalloc(sizeof(*resampler), GFP_KERNEL);
  293. if (!resampler) {
  294. ret = -ENOMEM;
  295. mutex_unlock(&kvm->irqfds.resampler_lock);
  296. goto fail;
  297. }
  298. resampler->kvm = kvm;
  299. INIT_LIST_HEAD(&resampler->list);
  300. resampler->notifier.gsi = irqfd->gsi;
  301. resampler->notifier.irq_acked = irqfd_resampler_ack;
  302. INIT_LIST_HEAD(&resampler->link);
  303. list_add(&resampler->link, &kvm->irqfds.resampler_list);
  304. kvm_register_irq_ack_notifier(kvm,
  305. &resampler->notifier);
  306. irqfd->resampler = resampler;
  307. }
  308. list_add_rcu(&irqfd->resampler_link, &irqfd->resampler->list);
  309. synchronize_rcu();
  310. mutex_unlock(&kvm->irqfds.resampler_lock);
  311. }
  312. /*
  313. * Install our own custom wake-up handling so we are notified via
  314. * a callback whenever someone signals the underlying eventfd
  315. */
  316. init_waitqueue_func_entry(&irqfd->wait, irqfd_wakeup);
  317. init_poll_funcptr(&irqfd->pt, irqfd_ptable_queue_proc);
  318. spin_lock_irq(&kvm->irqfds.lock);
  319. ret = 0;
  320. list_for_each_entry(tmp, &kvm->irqfds.items, list) {
  321. if (irqfd->eventfd != tmp->eventfd)
  322. continue;
  323. /* This fd is used for another irq already. */
  324. ret = -EBUSY;
  325. spin_unlock_irq(&kvm->irqfds.lock);
  326. goto fail;
  327. }
  328. irq_rt = rcu_dereference_protected(kvm->irq_routing,
  329. lockdep_is_held(&kvm->irqfds.lock));
  330. irqfd_update(kvm, irqfd, irq_rt);
  331. events = file->f_op->poll(file, &irqfd->pt);
  332. list_add_tail(&irqfd->list, &kvm->irqfds.items);
  333. /*
  334. * Check if there was an event already pending on the eventfd
  335. * before we registered, and trigger it as if we didn't miss it.
  336. */
  337. if (events & POLLIN)
  338. schedule_work(&irqfd->inject);
  339. spin_unlock_irq(&kvm->irqfds.lock);
  340. /*
  341. * do not drop the file until the irqfd is fully initialized, otherwise
  342. * we might race against the POLLHUP
  343. */
  344. fput(file);
  345. return 0;
  346. fail:
  347. if (irqfd->resampler)
  348. irqfd_resampler_shutdown(irqfd);
  349. if (resamplefd && !IS_ERR(resamplefd))
  350. eventfd_ctx_put(resamplefd);
  351. if (eventfd && !IS_ERR(eventfd))
  352. eventfd_ctx_put(eventfd);
  353. if (!IS_ERR(file))
  354. fput(file);
  355. kfree(irqfd);
  356. return ret;
  357. }
  358. #endif
  359. void
  360. kvm_eventfd_init(struct kvm *kvm)
  361. {
  362. #ifdef __KVM_HAVE_IOAPIC
  363. spin_lock_init(&kvm->irqfds.lock);
  364. INIT_LIST_HEAD(&kvm->irqfds.items);
  365. INIT_LIST_HEAD(&kvm->irqfds.resampler_list);
  366. mutex_init(&kvm->irqfds.resampler_lock);
  367. #endif
  368. INIT_LIST_HEAD(&kvm->ioeventfds);
  369. }
  370. #ifdef __KVM_HAVE_IOAPIC
  371. /*
  372. * shutdown any irqfd's that match fd+gsi
  373. */
  374. static int
  375. kvm_irqfd_deassign(struct kvm *kvm, struct kvm_irqfd *args)
  376. {
  377. struct _irqfd *irqfd, *tmp;
  378. struct eventfd_ctx *eventfd;
  379. eventfd = eventfd_ctx_fdget(args->fd);
  380. if (IS_ERR(eventfd))
  381. return PTR_ERR(eventfd);
  382. spin_lock_irq(&kvm->irqfds.lock);
  383. list_for_each_entry_safe(irqfd, tmp, &kvm->irqfds.items, list) {
  384. if (irqfd->eventfd == eventfd && irqfd->gsi == args->gsi) {
  385. /*
  386. * This rcu_assign_pointer is needed for when
  387. * another thread calls kvm_irq_routing_update before
  388. * we flush workqueue below (we synchronize with
  389. * kvm_irq_routing_update using irqfds.lock).
  390. * It is paired with synchronize_rcu done by caller
  391. * of that function.
  392. */
  393. rcu_assign_pointer(irqfd->irq_entry, NULL);
  394. irqfd_deactivate(irqfd);
  395. }
  396. }
  397. spin_unlock_irq(&kvm->irqfds.lock);
  398. eventfd_ctx_put(eventfd);
  399. /*
  400. * Block until we know all outstanding shutdown jobs have completed
  401. * so that we guarantee there will not be any more interrupts on this
  402. * gsi once this deassign function returns.
  403. */
  404. flush_workqueue(irqfd_cleanup_wq);
  405. return 0;
  406. }
  407. int
  408. kvm_irqfd(struct kvm *kvm, struct kvm_irqfd *args)
  409. {
  410. if (args->flags & ~(KVM_IRQFD_FLAG_DEASSIGN | KVM_IRQFD_FLAG_RESAMPLE))
  411. return -EINVAL;
  412. if (args->flags & KVM_IRQFD_FLAG_DEASSIGN)
  413. return kvm_irqfd_deassign(kvm, args);
  414. return kvm_irqfd_assign(kvm, args);
  415. }
  416. /*
  417. * This function is called as the kvm VM fd is being released. Shutdown all
  418. * irqfds that still remain open
  419. */
  420. void
  421. kvm_irqfd_release(struct kvm *kvm)
  422. {
  423. struct _irqfd *irqfd, *tmp;
  424. spin_lock_irq(&kvm->irqfds.lock);
  425. list_for_each_entry_safe(irqfd, tmp, &kvm->irqfds.items, list)
  426. irqfd_deactivate(irqfd);
  427. spin_unlock_irq(&kvm->irqfds.lock);
  428. /*
  429. * Block until we know all outstanding shutdown jobs have completed
  430. * since we do not take a kvm* reference.
  431. */
  432. flush_workqueue(irqfd_cleanup_wq);
  433. }
  434. /*
  435. * Change irq_routing and irqfd.
  436. * Caller must invoke synchronize_rcu afterwards.
  437. */
  438. void kvm_irq_routing_update(struct kvm *kvm,
  439. struct kvm_irq_routing_table *irq_rt)
  440. {
  441. struct _irqfd *irqfd;
  442. spin_lock_irq(&kvm->irqfds.lock);
  443. rcu_assign_pointer(kvm->irq_routing, irq_rt);
  444. list_for_each_entry(irqfd, &kvm->irqfds.items, list)
  445. irqfd_update(kvm, irqfd, irq_rt);
  446. spin_unlock_irq(&kvm->irqfds.lock);
  447. }
  448. /*
  449. * create a host-wide workqueue for issuing deferred shutdown requests
  450. * aggregated from all vm* instances. We need our own isolated single-thread
  451. * queue to prevent deadlock against flushing the normal work-queue.
  452. */
  453. static int __init irqfd_module_init(void)
  454. {
  455. irqfd_cleanup_wq = create_singlethread_workqueue("kvm-irqfd-cleanup");
  456. if (!irqfd_cleanup_wq)
  457. return -ENOMEM;
  458. return 0;
  459. }
  460. static void __exit irqfd_module_exit(void)
  461. {
  462. destroy_workqueue(irqfd_cleanup_wq);
  463. }
  464. module_init(irqfd_module_init);
  465. module_exit(irqfd_module_exit);
  466. #endif
  467. /*
  468. * --------------------------------------------------------------------
  469. * ioeventfd: translate a PIO/MMIO memory write to an eventfd signal.
  470. *
  471. * userspace can register a PIO/MMIO address with an eventfd for receiving
  472. * notification when the memory has been touched.
  473. * --------------------------------------------------------------------
  474. */
  475. struct _ioeventfd {
  476. struct list_head list;
  477. u64 addr;
  478. int length;
  479. struct eventfd_ctx *eventfd;
  480. u64 datamatch;
  481. struct kvm_io_device dev;
  482. bool wildcard;
  483. };
  484. static inline struct _ioeventfd *
  485. to_ioeventfd(struct kvm_io_device *dev)
  486. {
  487. return container_of(dev, struct _ioeventfd, dev);
  488. }
  489. static void
  490. ioeventfd_release(struct _ioeventfd *p)
  491. {
  492. eventfd_ctx_put(p->eventfd);
  493. list_del(&p->list);
  494. kfree(p);
  495. }
  496. static bool
  497. ioeventfd_in_range(struct _ioeventfd *p, gpa_t addr, int len, const void *val)
  498. {
  499. u64 _val;
  500. if (!(addr == p->addr && len == p->length))
  501. /* address-range must be precise for a hit */
  502. return false;
  503. if (p->wildcard)
  504. /* all else equal, wildcard is always a hit */
  505. return true;
  506. /* otherwise, we have to actually compare the data */
  507. BUG_ON(!IS_ALIGNED((unsigned long)val, len));
  508. switch (len) {
  509. case 1:
  510. _val = *(u8 *)val;
  511. break;
  512. case 2:
  513. _val = *(u16 *)val;
  514. break;
  515. case 4:
  516. _val = *(u32 *)val;
  517. break;
  518. case 8:
  519. _val = *(u64 *)val;
  520. break;
  521. default:
  522. return false;
  523. }
  524. return _val == p->datamatch ? true : false;
  525. }
  526. /* MMIO/PIO writes trigger an event if the addr/val match */
  527. static int
  528. ioeventfd_write(struct kvm_io_device *this, gpa_t addr, int len,
  529. const void *val)
  530. {
  531. struct _ioeventfd *p = to_ioeventfd(this);
  532. if (!ioeventfd_in_range(p, addr, len, val))
  533. return -EOPNOTSUPP;
  534. eventfd_signal(p->eventfd, 1);
  535. return 0;
  536. }
  537. /*
  538. * This function is called as KVM is completely shutting down. We do not
  539. * need to worry about locking just nuke anything we have as quickly as possible
  540. */
  541. static void
  542. ioeventfd_destructor(struct kvm_io_device *this)
  543. {
  544. struct _ioeventfd *p = to_ioeventfd(this);
  545. ioeventfd_release(p);
  546. }
  547. static const struct kvm_io_device_ops ioeventfd_ops = {
  548. .write = ioeventfd_write,
  549. .destructor = ioeventfd_destructor,
  550. };
  551. /* assumes kvm->slots_lock held */
  552. static bool
  553. ioeventfd_check_collision(struct kvm *kvm, struct _ioeventfd *p)
  554. {
  555. struct _ioeventfd *_p;
  556. list_for_each_entry(_p, &kvm->ioeventfds, list)
  557. if (_p->addr == p->addr && _p->length == p->length &&
  558. (_p->wildcard || p->wildcard ||
  559. _p->datamatch == p->datamatch))
  560. return true;
  561. return false;
  562. }
  563. static int
  564. kvm_assign_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
  565. {
  566. int pio = args->flags & KVM_IOEVENTFD_FLAG_PIO;
  567. enum kvm_bus bus_idx = pio ? KVM_PIO_BUS : KVM_MMIO_BUS;
  568. struct _ioeventfd *p;
  569. struct eventfd_ctx *eventfd;
  570. int ret;
  571. /* must be natural-word sized */
  572. switch (args->len) {
  573. case 1:
  574. case 2:
  575. case 4:
  576. case 8:
  577. break;
  578. default:
  579. return -EINVAL;
  580. }
  581. /* check for range overflow */
  582. if (args->addr + args->len < args->addr)
  583. return -EINVAL;
  584. /* check for extra flags that we don't understand */
  585. if (args->flags & ~KVM_IOEVENTFD_VALID_FLAG_MASK)
  586. return -EINVAL;
  587. eventfd = eventfd_ctx_fdget(args->fd);
  588. if (IS_ERR(eventfd))
  589. return PTR_ERR(eventfd);
  590. p = kzalloc(sizeof(*p), GFP_KERNEL);
  591. if (!p) {
  592. ret = -ENOMEM;
  593. goto fail;
  594. }
  595. INIT_LIST_HEAD(&p->list);
  596. p->addr = args->addr;
  597. p->length = args->len;
  598. p->eventfd = eventfd;
  599. /* The datamatch feature is optional, otherwise this is a wildcard */
  600. if (args->flags & KVM_IOEVENTFD_FLAG_DATAMATCH)
  601. p->datamatch = args->datamatch;
  602. else
  603. p->wildcard = true;
  604. mutex_lock(&kvm->slots_lock);
  605. /* Verify that there isn't a match already */
  606. if (ioeventfd_check_collision(kvm, p)) {
  607. ret = -EEXIST;
  608. goto unlock_fail;
  609. }
  610. kvm_iodevice_init(&p->dev, &ioeventfd_ops);
  611. ret = kvm_io_bus_register_dev(kvm, bus_idx, p->addr, p->length,
  612. &p->dev);
  613. if (ret < 0)
  614. goto unlock_fail;
  615. list_add_tail(&p->list, &kvm->ioeventfds);
  616. mutex_unlock(&kvm->slots_lock);
  617. return 0;
  618. unlock_fail:
  619. mutex_unlock(&kvm->slots_lock);
  620. fail:
  621. kfree(p);
  622. eventfd_ctx_put(eventfd);
  623. return ret;
  624. }
  625. static int
  626. kvm_deassign_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
  627. {
  628. int pio = args->flags & KVM_IOEVENTFD_FLAG_PIO;
  629. enum kvm_bus bus_idx = pio ? KVM_PIO_BUS : KVM_MMIO_BUS;
  630. struct _ioeventfd *p, *tmp;
  631. struct eventfd_ctx *eventfd;
  632. int ret = -ENOENT;
  633. eventfd = eventfd_ctx_fdget(args->fd);
  634. if (IS_ERR(eventfd))
  635. return PTR_ERR(eventfd);
  636. mutex_lock(&kvm->slots_lock);
  637. list_for_each_entry_safe(p, tmp, &kvm->ioeventfds, list) {
  638. bool wildcard = !(args->flags & KVM_IOEVENTFD_FLAG_DATAMATCH);
  639. if (p->eventfd != eventfd ||
  640. p->addr != args->addr ||
  641. p->length != args->len ||
  642. p->wildcard != wildcard)
  643. continue;
  644. if (!p->wildcard && p->datamatch != args->datamatch)
  645. continue;
  646. kvm_io_bus_unregister_dev(kvm, bus_idx, &p->dev);
  647. ioeventfd_release(p);
  648. ret = 0;
  649. break;
  650. }
  651. mutex_unlock(&kvm->slots_lock);
  652. eventfd_ctx_put(eventfd);
  653. return ret;
  654. }
  655. int
  656. kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
  657. {
  658. if (args->flags & KVM_IOEVENTFD_FLAG_DEASSIGN)
  659. return kvm_deassign_ioeventfd(kvm, args);
  660. return kvm_assign_ioeventfd(kvm, args);
  661. }