eventfd.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. /*
  2. * kvm eventfd support - use eventfd objects to signal various KVM events
  3. *
  4. * Copyright 2009 Novell. All Rights Reserved.
  5. * Copyright 2010 Red Hat, Inc. and/or its affiliates.
  6. *
  7. * Author:
  8. * Gregory Haskins <ghaskins@novell.com>
  9. *
  10. * This file is free software; you can redistribute it and/or modify
  11. * it under the terms of version 2 of the GNU General Public License
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software Foundation,
  21. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
  22. */
  23. #include <linux/kvm_host.h>
  24. #include <linux/kvm.h>
  25. #include <linux/workqueue.h>
  26. #include <linux/syscalls.h>
  27. #include <linux/wait.h>
  28. #include <linux/poll.h>
  29. #include <linux/file.h>
  30. #include <linux/list.h>
  31. #include <linux/eventfd.h>
  32. #include <linux/kernel.h>
  33. #include <linux/slab.h>
  34. #include "iodev.h"
  35. /*
  36. * --------------------------------------------------------------------
  37. * irqfd: Allows an fd to be used to inject an interrupt to the guest
  38. *
  39. * Credit goes to Avi Kivity for the original idea.
  40. * --------------------------------------------------------------------
  41. */
  42. struct _irqfd {
  43. /* Used for MSI fast-path */
  44. struct kvm *kvm;
  45. wait_queue_t wait;
  46. /* Update side is protected by irqfds.lock */
  47. struct kvm_kernel_irq_routing_entry __rcu *irq_entry;
  48. /* Used for level IRQ fast-path */
  49. int gsi;
  50. struct work_struct inject;
  51. /* Used for setup/shutdown */
  52. struct eventfd_ctx *eventfd;
  53. struct list_head list;
  54. poll_table pt;
  55. struct work_struct shutdown;
  56. };
  57. static struct workqueue_struct *irqfd_cleanup_wq;
  58. static void
  59. irqfd_inject(struct work_struct *work)
  60. {
  61. struct _irqfd *irqfd = container_of(work, struct _irqfd, inject);
  62. struct kvm *kvm = irqfd->kvm;
  63. kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, irqfd->gsi, 1);
  64. kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, irqfd->gsi, 0);
  65. }
  66. /*
  67. * Race-free decouple logic (ordering is critical)
  68. */
  69. static void
  70. irqfd_shutdown(struct work_struct *work)
  71. {
  72. struct _irqfd *irqfd = container_of(work, struct _irqfd, shutdown);
  73. u64 cnt;
  74. /*
  75. * Synchronize with the wait-queue and unhook ourselves to prevent
  76. * further events.
  77. */
  78. eventfd_ctx_remove_wait_queue(irqfd->eventfd, &irqfd->wait, &cnt);
  79. /*
  80. * We know no new events will be scheduled at this point, so block
  81. * until all previously outstanding events have completed
  82. */
  83. flush_work_sync(&irqfd->inject);
  84. /*
  85. * It is now safe to release the object's resources
  86. */
  87. eventfd_ctx_put(irqfd->eventfd);
  88. kfree(irqfd);
  89. }
  90. /* assumes kvm->irqfds.lock is held */
  91. static bool
  92. irqfd_is_active(struct _irqfd *irqfd)
  93. {
  94. return list_empty(&irqfd->list) ? false : true;
  95. }
  96. /*
  97. * Mark the irqfd as inactive and schedule it for removal
  98. *
  99. * assumes kvm->irqfds.lock is held
  100. */
  101. static void
  102. irqfd_deactivate(struct _irqfd *irqfd)
  103. {
  104. BUG_ON(!irqfd_is_active(irqfd));
  105. list_del_init(&irqfd->list);
  106. queue_work(irqfd_cleanup_wq, &irqfd->shutdown);
  107. }
  108. /*
  109. * Called with wqh->lock held and interrupts disabled
  110. */
  111. static int
  112. irqfd_wakeup(wait_queue_t *wait, unsigned mode, int sync, void *key)
  113. {
  114. struct _irqfd *irqfd = container_of(wait, struct _irqfd, wait);
  115. unsigned long flags = (unsigned long)key;
  116. struct kvm_kernel_irq_routing_entry *irq;
  117. struct kvm *kvm = irqfd->kvm;
  118. if (flags & POLLIN) {
  119. rcu_read_lock();
  120. irq = rcu_dereference(irqfd->irq_entry);
  121. /* An event has been signaled, inject an interrupt */
  122. if (irq)
  123. kvm_set_msi(irq, kvm, KVM_USERSPACE_IRQ_SOURCE_ID, 1);
  124. else
  125. schedule_work(&irqfd->inject);
  126. rcu_read_unlock();
  127. }
  128. if (flags & POLLHUP) {
  129. /* The eventfd is closing, detach from KVM */
  130. unsigned long flags;
  131. spin_lock_irqsave(&kvm->irqfds.lock, flags);
  132. /*
  133. * We must check if someone deactivated the irqfd before
  134. * we could acquire the irqfds.lock since the item is
  135. * deactivated from the KVM side before it is unhooked from
  136. * the wait-queue. If it is already deactivated, we can
  137. * simply return knowing the other side will cleanup for us.
  138. * We cannot race against the irqfd going away since the
  139. * other side is required to acquire wqh->lock, which we hold
  140. */
  141. if (irqfd_is_active(irqfd))
  142. irqfd_deactivate(irqfd);
  143. spin_unlock_irqrestore(&kvm->irqfds.lock, flags);
  144. }
  145. return 0;
  146. }
  147. static void
  148. irqfd_ptable_queue_proc(struct file *file, wait_queue_head_t *wqh,
  149. poll_table *pt)
  150. {
  151. struct _irqfd *irqfd = container_of(pt, struct _irqfd, pt);
  152. add_wait_queue(wqh, &irqfd->wait);
  153. }
  154. /* Must be called under irqfds.lock */
  155. static void irqfd_update(struct kvm *kvm, struct _irqfd *irqfd,
  156. struct kvm_irq_routing_table *irq_rt)
  157. {
  158. struct kvm_kernel_irq_routing_entry *e;
  159. struct hlist_node *n;
  160. if (irqfd->gsi >= irq_rt->nr_rt_entries) {
  161. rcu_assign_pointer(irqfd->irq_entry, NULL);
  162. return;
  163. }
  164. hlist_for_each_entry(e, n, &irq_rt->map[irqfd->gsi], link) {
  165. /* Only fast-path MSI. */
  166. if (e->type == KVM_IRQ_ROUTING_MSI)
  167. rcu_assign_pointer(irqfd->irq_entry, e);
  168. else
  169. rcu_assign_pointer(irqfd->irq_entry, NULL);
  170. }
  171. }
  172. static int
  173. kvm_irqfd_assign(struct kvm *kvm, int fd, int gsi)
  174. {
  175. struct kvm_irq_routing_table *irq_rt;
  176. struct _irqfd *irqfd, *tmp;
  177. struct file *file = NULL;
  178. struct eventfd_ctx *eventfd = NULL;
  179. int ret;
  180. unsigned int events;
  181. irqfd = kzalloc(sizeof(*irqfd), GFP_KERNEL);
  182. if (!irqfd)
  183. return -ENOMEM;
  184. irqfd->kvm = kvm;
  185. irqfd->gsi = gsi;
  186. INIT_LIST_HEAD(&irqfd->list);
  187. INIT_WORK(&irqfd->inject, irqfd_inject);
  188. INIT_WORK(&irqfd->shutdown, irqfd_shutdown);
  189. file = eventfd_fget(fd);
  190. if (IS_ERR(file)) {
  191. ret = PTR_ERR(file);
  192. goto fail;
  193. }
  194. eventfd = eventfd_ctx_fileget(file);
  195. if (IS_ERR(eventfd)) {
  196. ret = PTR_ERR(eventfd);
  197. goto fail;
  198. }
  199. irqfd->eventfd = eventfd;
  200. /*
  201. * Install our own custom wake-up handling so we are notified via
  202. * a callback whenever someone signals the underlying eventfd
  203. */
  204. init_waitqueue_func_entry(&irqfd->wait, irqfd_wakeup);
  205. init_poll_funcptr(&irqfd->pt, irqfd_ptable_queue_proc);
  206. spin_lock_irq(&kvm->irqfds.lock);
  207. ret = 0;
  208. list_for_each_entry(tmp, &kvm->irqfds.items, list) {
  209. if (irqfd->eventfd != tmp->eventfd)
  210. continue;
  211. /* This fd is used for another irq already. */
  212. ret = -EBUSY;
  213. spin_unlock_irq(&kvm->irqfds.lock);
  214. goto fail;
  215. }
  216. irq_rt = rcu_dereference_protected(kvm->irq_routing,
  217. lockdep_is_held(&kvm->irqfds.lock));
  218. irqfd_update(kvm, irqfd, irq_rt);
  219. events = file->f_op->poll(file, &irqfd->pt);
  220. list_add_tail(&irqfd->list, &kvm->irqfds.items);
  221. /*
  222. * Check if there was an event already pending on the eventfd
  223. * before we registered, and trigger it as if we didn't miss it.
  224. */
  225. if (events & POLLIN)
  226. schedule_work(&irqfd->inject);
  227. spin_unlock_irq(&kvm->irqfds.lock);
  228. /*
  229. * do not drop the file until the irqfd is fully initialized, otherwise
  230. * we might race against the POLLHUP
  231. */
  232. fput(file);
  233. return 0;
  234. fail:
  235. if (eventfd && !IS_ERR(eventfd))
  236. eventfd_ctx_put(eventfd);
  237. if (!IS_ERR(file))
  238. fput(file);
  239. kfree(irqfd);
  240. return ret;
  241. }
  242. void
  243. kvm_eventfd_init(struct kvm *kvm)
  244. {
  245. spin_lock_init(&kvm->irqfds.lock);
  246. INIT_LIST_HEAD(&kvm->irqfds.items);
  247. INIT_LIST_HEAD(&kvm->ioeventfds);
  248. }
  249. /*
  250. * shutdown any irqfd's that match fd+gsi
  251. */
  252. static int
  253. kvm_irqfd_deassign(struct kvm *kvm, int fd, int gsi)
  254. {
  255. struct _irqfd *irqfd, *tmp;
  256. struct eventfd_ctx *eventfd;
  257. eventfd = eventfd_ctx_fdget(fd);
  258. if (IS_ERR(eventfd))
  259. return PTR_ERR(eventfd);
  260. spin_lock_irq(&kvm->irqfds.lock);
  261. list_for_each_entry_safe(irqfd, tmp, &kvm->irqfds.items, list) {
  262. if (irqfd->eventfd == eventfd && irqfd->gsi == gsi) {
  263. /*
  264. * This rcu_assign_pointer is needed for when
  265. * another thread calls kvm_irq_routing_update before
  266. * we flush workqueue below (we synchronize with
  267. * kvm_irq_routing_update using irqfds.lock).
  268. * It is paired with synchronize_rcu done by caller
  269. * of that function.
  270. */
  271. rcu_assign_pointer(irqfd->irq_entry, NULL);
  272. irqfd_deactivate(irqfd);
  273. }
  274. }
  275. spin_unlock_irq(&kvm->irqfds.lock);
  276. eventfd_ctx_put(eventfd);
  277. /*
  278. * Block until we know all outstanding shutdown jobs have completed
  279. * so that we guarantee there will not be any more interrupts on this
  280. * gsi once this deassign function returns.
  281. */
  282. flush_workqueue(irqfd_cleanup_wq);
  283. return 0;
  284. }
  285. int
  286. kvm_irqfd(struct kvm *kvm, int fd, int gsi, int flags)
  287. {
  288. if (flags & KVM_IRQFD_FLAG_DEASSIGN)
  289. return kvm_irqfd_deassign(kvm, fd, gsi);
  290. return kvm_irqfd_assign(kvm, fd, gsi);
  291. }
  292. /*
  293. * This function is called as the kvm VM fd is being released. Shutdown all
  294. * irqfds that still remain open
  295. */
  296. void
  297. kvm_irqfd_release(struct kvm *kvm)
  298. {
  299. struct _irqfd *irqfd, *tmp;
  300. spin_lock_irq(&kvm->irqfds.lock);
  301. list_for_each_entry_safe(irqfd, tmp, &kvm->irqfds.items, list)
  302. irqfd_deactivate(irqfd);
  303. spin_unlock_irq(&kvm->irqfds.lock);
  304. /*
  305. * Block until we know all outstanding shutdown jobs have completed
  306. * since we do not take a kvm* reference.
  307. */
  308. flush_workqueue(irqfd_cleanup_wq);
  309. }
  310. /*
  311. * Change irq_routing and irqfd.
  312. * Caller must invoke synchronize_rcu afterwards.
  313. */
  314. void kvm_irq_routing_update(struct kvm *kvm,
  315. struct kvm_irq_routing_table *irq_rt)
  316. {
  317. struct _irqfd *irqfd;
  318. spin_lock_irq(&kvm->irqfds.lock);
  319. rcu_assign_pointer(kvm->irq_routing, irq_rt);
  320. list_for_each_entry(irqfd, &kvm->irqfds.items, list)
  321. irqfd_update(kvm, irqfd, irq_rt);
  322. spin_unlock_irq(&kvm->irqfds.lock);
  323. }
  324. /*
  325. * create a host-wide workqueue for issuing deferred shutdown requests
  326. * aggregated from all vm* instances. We need our own isolated single-thread
  327. * queue to prevent deadlock against flushing the normal work-queue.
  328. */
  329. static int __init irqfd_module_init(void)
  330. {
  331. irqfd_cleanup_wq = create_singlethread_workqueue("kvm-irqfd-cleanup");
  332. if (!irqfd_cleanup_wq)
  333. return -ENOMEM;
  334. return 0;
  335. }
  336. static void __exit irqfd_module_exit(void)
  337. {
  338. destroy_workqueue(irqfd_cleanup_wq);
  339. }
  340. module_init(irqfd_module_init);
  341. module_exit(irqfd_module_exit);
  342. /*
  343. * --------------------------------------------------------------------
  344. * ioeventfd: translate a PIO/MMIO memory write to an eventfd signal.
  345. *
  346. * userspace can register a PIO/MMIO address with an eventfd for receiving
  347. * notification when the memory has been touched.
  348. * --------------------------------------------------------------------
  349. */
  350. struct _ioeventfd {
  351. struct list_head list;
  352. u64 addr;
  353. int length;
  354. struct eventfd_ctx *eventfd;
  355. u64 datamatch;
  356. struct kvm_io_device dev;
  357. bool wildcard;
  358. };
  359. static inline struct _ioeventfd *
  360. to_ioeventfd(struct kvm_io_device *dev)
  361. {
  362. return container_of(dev, struct _ioeventfd, dev);
  363. }
  364. static void
  365. ioeventfd_release(struct _ioeventfd *p)
  366. {
  367. eventfd_ctx_put(p->eventfd);
  368. list_del(&p->list);
  369. kfree(p);
  370. }
  371. static bool
  372. ioeventfd_in_range(struct _ioeventfd *p, gpa_t addr, int len, const void *val)
  373. {
  374. u64 _val;
  375. if (!(addr == p->addr && len == p->length))
  376. /* address-range must be precise for a hit */
  377. return false;
  378. if (p->wildcard)
  379. /* all else equal, wildcard is always a hit */
  380. return true;
  381. /* otherwise, we have to actually compare the data */
  382. BUG_ON(!IS_ALIGNED((unsigned long)val, len));
  383. switch (len) {
  384. case 1:
  385. _val = *(u8 *)val;
  386. break;
  387. case 2:
  388. _val = *(u16 *)val;
  389. break;
  390. case 4:
  391. _val = *(u32 *)val;
  392. break;
  393. case 8:
  394. _val = *(u64 *)val;
  395. break;
  396. default:
  397. return false;
  398. }
  399. return _val == p->datamatch ? true : false;
  400. }
  401. /* MMIO/PIO writes trigger an event if the addr/val match */
  402. static int
  403. ioeventfd_write(struct kvm_io_device *this, gpa_t addr, int len,
  404. const void *val)
  405. {
  406. struct _ioeventfd *p = to_ioeventfd(this);
  407. if (!ioeventfd_in_range(p, addr, len, val))
  408. return -EOPNOTSUPP;
  409. eventfd_signal(p->eventfd, 1);
  410. return 0;
  411. }
  412. /*
  413. * This function is called as KVM is completely shutting down. We do not
  414. * need to worry about locking just nuke anything we have as quickly as possible
  415. */
  416. static void
  417. ioeventfd_destructor(struct kvm_io_device *this)
  418. {
  419. struct _ioeventfd *p = to_ioeventfd(this);
  420. ioeventfd_release(p);
  421. }
  422. static const struct kvm_io_device_ops ioeventfd_ops = {
  423. .write = ioeventfd_write,
  424. .destructor = ioeventfd_destructor,
  425. };
  426. /* assumes kvm->slots_lock held */
  427. static bool
  428. ioeventfd_check_collision(struct kvm *kvm, struct _ioeventfd *p)
  429. {
  430. struct _ioeventfd *_p;
  431. list_for_each_entry(_p, &kvm->ioeventfds, list)
  432. if (_p->addr == p->addr && _p->length == p->length &&
  433. (_p->wildcard || p->wildcard ||
  434. _p->datamatch == p->datamatch))
  435. return true;
  436. return false;
  437. }
  438. static int
  439. kvm_assign_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
  440. {
  441. int pio = args->flags & KVM_IOEVENTFD_FLAG_PIO;
  442. enum kvm_bus bus_idx = pio ? KVM_PIO_BUS : KVM_MMIO_BUS;
  443. struct _ioeventfd *p;
  444. struct eventfd_ctx *eventfd;
  445. int ret;
  446. /* must be natural-word sized */
  447. switch (args->len) {
  448. case 1:
  449. case 2:
  450. case 4:
  451. case 8:
  452. break;
  453. default:
  454. return -EINVAL;
  455. }
  456. /* check for range overflow */
  457. if (args->addr + args->len < args->addr)
  458. return -EINVAL;
  459. /* check for extra flags that we don't understand */
  460. if (args->flags & ~KVM_IOEVENTFD_VALID_FLAG_MASK)
  461. return -EINVAL;
  462. eventfd = eventfd_ctx_fdget(args->fd);
  463. if (IS_ERR(eventfd))
  464. return PTR_ERR(eventfd);
  465. p = kzalloc(sizeof(*p), GFP_KERNEL);
  466. if (!p) {
  467. ret = -ENOMEM;
  468. goto fail;
  469. }
  470. INIT_LIST_HEAD(&p->list);
  471. p->addr = args->addr;
  472. p->length = args->len;
  473. p->eventfd = eventfd;
  474. /* The datamatch feature is optional, otherwise this is a wildcard */
  475. if (args->flags & KVM_IOEVENTFD_FLAG_DATAMATCH)
  476. p->datamatch = args->datamatch;
  477. else
  478. p->wildcard = true;
  479. mutex_lock(&kvm->slots_lock);
  480. /* Verify that there isn't a match already */
  481. if (ioeventfd_check_collision(kvm, p)) {
  482. ret = -EEXIST;
  483. goto unlock_fail;
  484. }
  485. kvm_iodevice_init(&p->dev, &ioeventfd_ops);
  486. ret = kvm_io_bus_register_dev(kvm, bus_idx, &p->dev);
  487. if (ret < 0)
  488. goto unlock_fail;
  489. list_add_tail(&p->list, &kvm->ioeventfds);
  490. mutex_unlock(&kvm->slots_lock);
  491. return 0;
  492. unlock_fail:
  493. mutex_unlock(&kvm->slots_lock);
  494. fail:
  495. kfree(p);
  496. eventfd_ctx_put(eventfd);
  497. return ret;
  498. }
  499. static int
  500. kvm_deassign_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
  501. {
  502. int pio = args->flags & KVM_IOEVENTFD_FLAG_PIO;
  503. enum kvm_bus bus_idx = pio ? KVM_PIO_BUS : KVM_MMIO_BUS;
  504. struct _ioeventfd *p, *tmp;
  505. struct eventfd_ctx *eventfd;
  506. int ret = -ENOENT;
  507. eventfd = eventfd_ctx_fdget(args->fd);
  508. if (IS_ERR(eventfd))
  509. return PTR_ERR(eventfd);
  510. mutex_lock(&kvm->slots_lock);
  511. list_for_each_entry_safe(p, tmp, &kvm->ioeventfds, list) {
  512. bool wildcard = !(args->flags & KVM_IOEVENTFD_FLAG_DATAMATCH);
  513. if (p->eventfd != eventfd ||
  514. p->addr != args->addr ||
  515. p->length != args->len ||
  516. p->wildcard != wildcard)
  517. continue;
  518. if (!p->wildcard && p->datamatch != args->datamatch)
  519. continue;
  520. kvm_io_bus_unregister_dev(kvm, bus_idx, &p->dev);
  521. ioeventfd_release(p);
  522. ret = 0;
  523. break;
  524. }
  525. mutex_unlock(&kvm->slots_lock);
  526. eventfd_ctx_put(eventfd);
  527. return ret;
  528. }
  529. int
  530. kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)
  531. {
  532. if (args->flags & KVM_IOEVENTFD_FLAG_DEASSIGN)
  533. return kvm_deassign_ioeventfd(kvm, args);
  534. return kvm_assign_ioeventfd(kvm, args);
  535. }