irqfd.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * ACRN HSM irqfd: use eventfd objects to inject virtual interrupts
  4. *
  5. * Copyright (C) 2020 Intel Corporation. All rights reserved.
  6. *
  7. * Authors:
  8. * Shuo Liu <shuo.a.liu@intel.com>
  9. * Yakui Zhao <yakui.zhao@intel.com>
  10. */
  11. #include <linux/eventfd.h>
  12. #include <linux/file.h>
  13. #include <linux/poll.h>
  14. #include <linux/slab.h>
  15. #include "acrn_drv.h"
  16. /**
  17. * struct hsm_irqfd - Properties of HSM irqfd
  18. * @vm: Associated VM pointer
  19. * @wait: Entry of wait-queue
  20. * @shutdown: Async shutdown work
  21. * @eventfd: Associated eventfd
  22. * @list: Entry within &acrn_vm.irqfds of irqfds of a VM
  23. * @pt: Structure for select/poll on the associated eventfd
  24. * @msi: MSI data
  25. */
  26. struct hsm_irqfd {
  27. struct acrn_vm *vm;
  28. wait_queue_entry_t wait;
  29. struct work_struct shutdown;
  30. struct eventfd_ctx *eventfd;
  31. struct list_head list;
  32. poll_table pt;
  33. struct acrn_msi_entry msi;
  34. };
  35. static void acrn_irqfd_inject(struct hsm_irqfd *irqfd)
  36. {
  37. struct acrn_vm *vm = irqfd->vm;
  38. acrn_msi_inject(vm, irqfd->msi.msi_addr,
  39. irqfd->msi.msi_data);
  40. }
  41. static void hsm_irqfd_shutdown(struct hsm_irqfd *irqfd)
  42. {
  43. u64 cnt;
  44. lockdep_assert_held(&irqfd->vm->irqfds_lock);
  45. /* remove from wait queue */
  46. list_del_init(&irqfd->list);
  47. eventfd_ctx_remove_wait_queue(irqfd->eventfd, &irqfd->wait, &cnt);
  48. eventfd_ctx_put(irqfd->eventfd);
  49. kfree(irqfd);
  50. }
  51. static void hsm_irqfd_shutdown_work(struct work_struct *work)
  52. {
  53. struct hsm_irqfd *irqfd;
  54. struct acrn_vm *vm;
  55. irqfd = container_of(work, struct hsm_irqfd, shutdown);
  56. vm = irqfd->vm;
  57. mutex_lock(&vm->irqfds_lock);
  58. if (!list_empty(&irqfd->list))
  59. hsm_irqfd_shutdown(irqfd);
  60. mutex_unlock(&vm->irqfds_lock);
  61. }
  62. /* Called with wqh->lock held and interrupts disabled */
  63. static int hsm_irqfd_wakeup(wait_queue_entry_t *wait, unsigned int mode,
  64. int sync, void *key)
  65. {
  66. unsigned long poll_bits = (unsigned long)key;
  67. struct hsm_irqfd *irqfd;
  68. struct acrn_vm *vm;
  69. irqfd = container_of(wait, struct hsm_irqfd, wait);
  70. vm = irqfd->vm;
  71. if (poll_bits & POLLIN)
  72. /* An event has been signaled, inject an interrupt */
  73. acrn_irqfd_inject(irqfd);
  74. if (poll_bits & POLLHUP)
  75. /* Do shutdown work in thread to hold wqh->lock */
  76. queue_work(vm->irqfd_wq, &irqfd->shutdown);
  77. return 0;
  78. }
  79. static void hsm_irqfd_poll_func(struct file *file, wait_queue_head_t *wqh,
  80. poll_table *pt)
  81. {
  82. struct hsm_irqfd *irqfd;
  83. irqfd = container_of(pt, struct hsm_irqfd, pt);
  84. add_wait_queue(wqh, &irqfd->wait);
  85. }
  86. /*
  87. * Assign an eventfd to a VM and create a HSM irqfd associated with the
  88. * eventfd. The properties of the HSM irqfd are built from a &struct
  89. * acrn_irqfd.
  90. */
  91. static int acrn_irqfd_assign(struct acrn_vm *vm, struct acrn_irqfd *args)
  92. {
  93. struct eventfd_ctx *eventfd = NULL;
  94. struct hsm_irqfd *irqfd, *tmp;
  95. __poll_t events;
  96. int ret = 0;
  97. irqfd = kzalloc_obj(*irqfd);
  98. if (!irqfd)
  99. return -ENOMEM;
  100. irqfd->vm = vm;
  101. memcpy(&irqfd->msi, &args->msi, sizeof(args->msi));
  102. INIT_LIST_HEAD(&irqfd->list);
  103. INIT_WORK(&irqfd->shutdown, hsm_irqfd_shutdown_work);
  104. CLASS(fd, f)(args->fd);
  105. if (fd_empty(f)) {
  106. ret = -EBADF;
  107. goto out;
  108. }
  109. eventfd = eventfd_ctx_fileget(fd_file(f));
  110. if (IS_ERR(eventfd)) {
  111. ret = PTR_ERR(eventfd);
  112. goto out;
  113. }
  114. irqfd->eventfd = eventfd;
  115. /*
  116. * Install custom wake-up handling to be notified whenever underlying
  117. * eventfd is signaled.
  118. */
  119. init_waitqueue_func_entry(&irqfd->wait, hsm_irqfd_wakeup);
  120. init_poll_funcptr(&irqfd->pt, hsm_irqfd_poll_func);
  121. mutex_lock(&vm->irqfds_lock);
  122. list_for_each_entry(tmp, &vm->irqfds, list) {
  123. if (irqfd->eventfd != tmp->eventfd)
  124. continue;
  125. ret = -EBUSY;
  126. mutex_unlock(&vm->irqfds_lock);
  127. goto fail;
  128. }
  129. list_add_tail(&irqfd->list, &vm->irqfds);
  130. mutex_unlock(&vm->irqfds_lock);
  131. /* Check the pending event in this stage */
  132. events = vfs_poll(fd_file(f), &irqfd->pt);
  133. if (events & EPOLLIN)
  134. acrn_irqfd_inject(irqfd);
  135. return 0;
  136. fail:
  137. eventfd_ctx_put(eventfd);
  138. out:
  139. kfree(irqfd);
  140. return ret;
  141. }
  142. static int acrn_irqfd_deassign(struct acrn_vm *vm,
  143. struct acrn_irqfd *args)
  144. {
  145. struct hsm_irqfd *irqfd, *tmp;
  146. struct eventfd_ctx *eventfd;
  147. eventfd = eventfd_ctx_fdget(args->fd);
  148. if (IS_ERR(eventfd))
  149. return PTR_ERR(eventfd);
  150. mutex_lock(&vm->irqfds_lock);
  151. list_for_each_entry_safe(irqfd, tmp, &vm->irqfds, list) {
  152. if (irqfd->eventfd == eventfd) {
  153. hsm_irqfd_shutdown(irqfd);
  154. break;
  155. }
  156. }
  157. mutex_unlock(&vm->irqfds_lock);
  158. eventfd_ctx_put(eventfd);
  159. return 0;
  160. }
  161. int acrn_irqfd_config(struct acrn_vm *vm, struct acrn_irqfd *args)
  162. {
  163. int ret;
  164. if (args->flags & ACRN_IRQFD_FLAG_DEASSIGN)
  165. ret = acrn_irqfd_deassign(vm, args);
  166. else
  167. ret = acrn_irqfd_assign(vm, args);
  168. return ret;
  169. }
  170. int acrn_irqfd_init(struct acrn_vm *vm)
  171. {
  172. INIT_LIST_HEAD(&vm->irqfds);
  173. mutex_init(&vm->irqfds_lock);
  174. vm->irqfd_wq = alloc_workqueue("acrn_irqfd-%u", 0, 0, vm->vmid);
  175. if (!vm->irqfd_wq)
  176. return -ENOMEM;
  177. dev_dbg(acrn_dev.this_device, "VM %u irqfd init.\n", vm->vmid);
  178. return 0;
  179. }
  180. void acrn_irqfd_deinit(struct acrn_vm *vm)
  181. {
  182. struct hsm_irqfd *irqfd, *next;
  183. dev_dbg(acrn_dev.this_device, "VM %u irqfd deinit.\n", vm->vmid);
  184. destroy_workqueue(vm->irqfd_wq);
  185. mutex_lock(&vm->irqfds_lock);
  186. list_for_each_entry_safe(irqfd, next, &vm->irqfds, list)
  187. hsm_irqfd_shutdown(irqfd);
  188. mutex_unlock(&vm->irqfds_lock);
  189. }