eventfd.c 20 KB

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