eventfd.c 21 KB

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