eventfd.c 13 KB

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