eventfd.c 22 KB

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