eventfd.c 13 KB

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