eventfd.c 13 KB

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