eventfd.c 13 KB

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