async_pf.c 5.5 KB

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