async_pf.c 5.6 KB

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