async_pf.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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, NULL,
  76. FOLL_WRITE | FOLL_REMOTE);
  77. kvm_async_page_present_sync(vcpu, apf);
  78. spin_lock(&vcpu->async_pf.lock);
  79. list_add_tail(&apf->link, &vcpu->async_pf.done);
  80. spin_unlock(&vcpu->async_pf.lock);
  81. /*
  82. * apf may be freed by kvm_check_async_pf_completion() after
  83. * this point
  84. */
  85. trace_kvm_async_pf_completed(addr, gva);
  86. /*
  87. * This memory barrier pairs with prepare_to_wait's set_current_state()
  88. */
  89. smp_mb();
  90. if (swait_active(&vcpu->wq))
  91. swake_up(&vcpu->wq);
  92. mmput(mm);
  93. kvm_put_kvm(vcpu->kvm);
  94. }
  95. void kvm_clear_async_pf_completion_queue(struct kvm_vcpu *vcpu)
  96. {
  97. /* cancel outstanding work queue item */
  98. while (!list_empty(&vcpu->async_pf.queue)) {
  99. struct kvm_async_pf *work =
  100. list_first_entry(&vcpu->async_pf.queue,
  101. typeof(*work), queue);
  102. list_del(&work->queue);
  103. #ifdef CONFIG_KVM_ASYNC_PF_SYNC
  104. flush_work(&work->work);
  105. #else
  106. if (cancel_work_sync(&work->work)) {
  107. mmput(work->mm);
  108. kvm_put_kvm(vcpu->kvm); /* == work->vcpu->kvm */
  109. kmem_cache_free(async_pf_cache, work);
  110. }
  111. #endif
  112. }
  113. spin_lock(&vcpu->async_pf.lock);
  114. while (!list_empty(&vcpu->async_pf.done)) {
  115. struct kvm_async_pf *work =
  116. list_first_entry(&vcpu->async_pf.done,
  117. typeof(*work), link);
  118. list_del(&work->link);
  119. kmem_cache_free(async_pf_cache, work);
  120. }
  121. spin_unlock(&vcpu->async_pf.lock);
  122. vcpu->async_pf.queued = 0;
  123. }
  124. void kvm_check_async_pf_completion(struct kvm_vcpu *vcpu)
  125. {
  126. struct kvm_async_pf *work;
  127. while (!list_empty_careful(&vcpu->async_pf.done) &&
  128. kvm_arch_can_inject_async_page_present(vcpu)) {
  129. spin_lock(&vcpu->async_pf.lock);
  130. work = list_first_entry(&vcpu->async_pf.done, typeof(*work),
  131. link);
  132. list_del(&work->link);
  133. spin_unlock(&vcpu->async_pf.lock);
  134. kvm_arch_async_page_ready(vcpu, work);
  135. kvm_async_page_present_async(vcpu, work);
  136. list_del(&work->queue);
  137. vcpu->async_pf.queued--;
  138. kmem_cache_free(async_pf_cache, work);
  139. }
  140. }
  141. int kvm_setup_async_pf(struct kvm_vcpu *vcpu, gva_t gva, unsigned long hva,
  142. struct kvm_arch_async_pf *arch)
  143. {
  144. struct kvm_async_pf *work;
  145. if (vcpu->async_pf.queued >= ASYNC_PF_PER_VCPU)
  146. return 0;
  147. /* setup delayed work */
  148. /*
  149. * do alloc nowait since if we are going to sleep anyway we
  150. * may as well sleep faulting in page
  151. */
  152. work = kmem_cache_zalloc(async_pf_cache, GFP_NOWAIT | __GFP_NOWARN);
  153. if (!work)
  154. return 0;
  155. work->wakeup_all = false;
  156. work->vcpu = vcpu;
  157. work->gva = gva;
  158. work->addr = hva;
  159. work->arch = *arch;
  160. work->mm = current->mm;
  161. atomic_inc(&work->mm->mm_users);
  162. kvm_get_kvm(work->vcpu->kvm);
  163. /* this can't really happen otherwise gfn_to_pfn_async
  164. would succeed */
  165. if (unlikely(kvm_is_error_hva(work->addr)))
  166. goto retry_sync;
  167. INIT_WORK(&work->work, async_pf_execute);
  168. if (!schedule_work(&work->work))
  169. goto retry_sync;
  170. list_add_tail(&work->queue, &vcpu->async_pf.queue);
  171. vcpu->async_pf.queued++;
  172. kvm_arch_async_page_not_present(vcpu, work);
  173. return 1;
  174. retry_sync:
  175. kvm_put_kvm(work->vcpu->kvm);
  176. mmput(work->mm);
  177. kmem_cache_free(async_pf_cache, work);
  178. return 0;
  179. }
  180. int kvm_async_pf_wakeup_all(struct kvm_vcpu *vcpu)
  181. {
  182. struct kvm_async_pf *work;
  183. if (!list_empty_careful(&vcpu->async_pf.done))
  184. return 0;
  185. work = kmem_cache_zalloc(async_pf_cache, GFP_ATOMIC);
  186. if (!work)
  187. return -ENOMEM;
  188. work->wakeup_all = true;
  189. INIT_LIST_HEAD(&work->queue); /* for list_del to work */
  190. spin_lock(&vcpu->async_pf.lock);
  191. list_add_tail(&work->link, &vcpu->async_pf.done);
  192. spin_unlock(&vcpu->async_pf.lock);
  193. vcpu->async_pf.queued++;
  194. return 0;
  195. }