eventfd.c 23 KB

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