eventfd.c 13 KB

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