eventfd.c 22 KB

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