async_pf.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * kvm asynchronous fault support
  3. *
  4. * Copyright 2010 Red Hat, Inc.
  5. *
  6. * Author:
  7. * Gleb Natapov <gleb@redhat.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/slab.h>
  24. #include <linux/module.h>
  25. #include <linux/mmu_context.h>
  26. #include <linux/sched/mm.h>
  27. #include "async_pf.h"
  28. #include <trace/events/kvm.h>
  29. static inline void kvm_async_page_present_sync(struct kvm_vcpu *vcpu,
  30. struct kvm_async_pf *work)
  31. {
  32. #ifdef CONFIG_KVM_ASYNC_PF_SYNC
  33. kvm_arch_async_page_present(vcpu, work);
  34. #endif
  35. }
  36. static inline void kvm_async_page_present_async(struct kvm_vcpu *vcpu,
  37. struct kvm_async_pf *work)
  38. {
  39. #ifndef CONFIG_KVM_ASYNC_PF_SYNC
  40. kvm_arch_async_page_present(vcpu, work);
  41. #endif
  42. }
  43. static struct kmem_cache *async_pf_cache;
  44. int kvm_async_pf_init(void)
  45. {
  46. async_pf_cache = KMEM_CACHE(kvm_async_pf, 0);
  47. if (!async_pf_cache)
  48. return -ENOMEM;
  49. return 0;
  50. }
  51. void kvm_async_pf_deinit(void)
  52. {
  53. kmem_cache_destroy(async_pf_cache);
  54. async_pf_cache = NULL;
  55. }
  56. void kvm_async_pf_vcpu_init(struct kvm_vcpu *vcpu)
  57. {
  58. INIT_LIST_HEAD(&vcpu->async_pf.done);
  59. INIT_LIST_HEAD(&vcpu->async_pf.queue);
  60. spin_lock_init(&vcpu->async_pf.lock);
  61. }
  62. static void async_pf_execute(struct work_struct *work)
  63. {
  64. struct kvm_async_pf *apf =
  65. container_of(work, struct kvm_async_pf, work);
  66. struct mm_struct *mm = apf->mm;
  67. struct kvm_vcpu *vcpu = apf->vcpu;
  68. unsigned long addr = apf->addr;
  69. gpa_t cr2_or_gpa = apf->cr2_or_gpa;
  70. int locked = 1;
  71. might_sleep();
  72. /*
  73. * This work is run asynchromously to the task which owns
  74. * mm and might be done in another context, so we must
  75. * access remotely.
  76. */
  77. down_read(&mm->mmap_sem);
  78. get_user_pages_remote(NULL, mm, addr, 1, FOLL_WRITE, NULL, NULL,
  79. &locked);
  80. if (locked)
  81. up_read(&mm->mmap_sem);
  82. kvm_async_page_present_sync(vcpu, apf);
  83. spin_lock(&vcpu->async_pf.lock);
  84. list_add_tail(&apf->link, &vcpu->async_pf.done);
  85. apf->vcpu = NULL;
  86. spin_unlock(&vcpu->async_pf.lock);
  87. /*
  88. * apf may be freed by kvm_check_async_pf_completion() after
  89. * this point
  90. */
  91. trace_kvm_async_pf_completed(addr, cr2_or_gpa);
  92. if (swq_has_sleeper(&vcpu->wq))
  93. swake_up_one(&vcpu->wq);
  94. mmput(mm);
  95. kvm_put_kvm(vcpu->kvm);
  96. }
  97. void kvm_clear_async_pf_completion_queue(struct kvm_vcpu *vcpu)
  98. {
  99. spin_lock(&vcpu->async_pf.lock);
  100. /* cancel outstanding work queue item */
  101. while (!list_empty(&vcpu->async_pf.queue)) {
  102. struct kvm_async_pf *work =
  103. list_first_entry(&vcpu->async_pf.queue,
  104. typeof(*work), queue);
  105. list_del(&work->queue);
  106. /*
  107. * We know it's present in vcpu->async_pf.done, do
  108. * nothing here.
  109. */
  110. if (!work->vcpu)
  111. continue;
  112. spin_unlock(&vcpu->async_pf.lock);
  113. #ifdef CONFIG_KVM_ASYNC_PF_SYNC
  114. flush_work(&work->work);
  115. #else
  116. if (cancel_work_sync(&work->work)) {
  117. mmput(work->mm);
  118. kvm_put_kvm(vcpu->kvm); /* == work->vcpu->kvm */
  119. kmem_cache_free(async_pf_cache, work);
  120. }
  121. #endif
  122. spin_lock(&vcpu->async_pf.lock);
  123. }
  124. while (!list_empty(&vcpu->async_pf.done)) {
  125. struct kvm_async_pf *work =
  126. list_first_entry(&vcpu->async_pf.done,
  127. typeof(*work), link);
  128. list_del(&work->link);
  129. kmem_cache_free(async_pf_cache, work);
  130. }
  131. spin_unlock(&vcpu->async_pf.lock);
  132. vcpu->async_pf.queued = 0;
  133. }
  134. void kvm_check_async_pf_completion(struct kvm_vcpu *vcpu)
  135. {
  136. struct kvm_async_pf *work;
  137. while (!list_empty_careful(&vcpu->async_pf.done) &&
  138. kvm_arch_can_inject_async_page_present(vcpu)) {
  139. spin_lock(&vcpu->async_pf.lock);
  140. work = list_first_entry(&vcpu->async_pf.done, typeof(*work),
  141. link);
  142. list_del(&work->link);
  143. spin_unlock(&vcpu->async_pf.lock);
  144. kvm_arch_async_page_ready(vcpu, work);
  145. kvm_async_page_present_async(vcpu, work);
  146. list_del(&work->queue);
  147. vcpu->async_pf.queued--;
  148. kmem_cache_free(async_pf_cache, work);
  149. }
  150. }
  151. int kvm_setup_async_pf(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
  152. unsigned long hva, struct kvm_arch_async_pf *arch)
  153. {
  154. struct kvm_async_pf *work;
  155. if (vcpu->async_pf.queued >= ASYNC_PF_PER_VCPU)
  156. return 0;
  157. /* setup delayed work */
  158. /*
  159. * do alloc nowait since if we are going to sleep anyway we
  160. * may as well sleep faulting in page
  161. */
  162. work = kmem_cache_zalloc(async_pf_cache, GFP_NOWAIT | __GFP_NOWARN);
  163. if (!work)
  164. return 0;
  165. work->wakeup_all = false;
  166. work->vcpu = vcpu;
  167. work->cr2_or_gpa = cr2_or_gpa;
  168. work->addr = hva;
  169. work->arch = *arch;
  170. work->mm = current->mm;
  171. mmget(work->mm);
  172. kvm_get_kvm(work->vcpu->kvm);
  173. /* this can't really happen otherwise gfn_to_pfn_async
  174. would succeed */
  175. if (unlikely(kvm_is_error_hva(work->addr)))
  176. goto retry_sync;
  177. INIT_WORK(&work->work, async_pf_execute);
  178. if (!schedule_work(&work->work))
  179. goto retry_sync;
  180. list_add_tail(&work->queue, &vcpu->async_pf.queue);
  181. vcpu->async_pf.queued++;
  182. kvm_arch_async_page_not_present(vcpu, work);
  183. return 1;
  184. retry_sync:
  185. kvm_put_kvm(work->vcpu->kvm);
  186. mmput(work->mm);
  187. kmem_cache_free(async_pf_cache, work);
  188. return 0;
  189. }
  190. int kvm_async_pf_wakeup_all(struct kvm_vcpu *vcpu)
  191. {
  192. struct kvm_async_pf *work;
  193. if (!list_empty_careful(&vcpu->async_pf.done))
  194. return 0;
  195. work = kmem_cache_zalloc(async_pf_cache, GFP_ATOMIC);
  196. if (!work)
  197. return -ENOMEM;
  198. work->wakeup_all = true;
  199. INIT_LIST_HEAD(&work->queue); /* for list_del to work */
  200. spin_lock(&vcpu->async_pf.lock);
  201. list_add_tail(&work->link, &vcpu->async_pf.done);
  202. spin_unlock(&vcpu->async_pf.lock);
  203. vcpu->async_pf.queued++;
  204. return 0;
  205. }