async_pf.c 5.9 KB

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