async_pf.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 struct kmem_cache *async_pf_cache;
  29. int kvm_async_pf_init(void)
  30. {
  31. async_pf_cache = KMEM_CACHE(kvm_async_pf, 0);
  32. if (!async_pf_cache)
  33. return -ENOMEM;
  34. return 0;
  35. }
  36. void kvm_async_pf_deinit(void)
  37. {
  38. if (async_pf_cache)
  39. kmem_cache_destroy(async_pf_cache);
  40. async_pf_cache = NULL;
  41. }
  42. void kvm_async_pf_vcpu_init(struct kvm_vcpu *vcpu)
  43. {
  44. INIT_LIST_HEAD(&vcpu->async_pf.done);
  45. INIT_LIST_HEAD(&vcpu->async_pf.queue);
  46. spin_lock_init(&vcpu->async_pf.lock);
  47. }
  48. static void async_pf_execute(struct work_struct *work)
  49. {
  50. struct page *page = NULL;
  51. struct kvm_async_pf *apf =
  52. container_of(work, struct kvm_async_pf, work);
  53. struct mm_struct *mm = apf->mm;
  54. struct kvm_vcpu *vcpu = apf->vcpu;
  55. unsigned long addr = apf->addr;
  56. gva_t gva = apf->gva;
  57. might_sleep();
  58. use_mm(mm);
  59. down_read(&mm->mmap_sem);
  60. get_user_pages(current, mm, addr, 1, 1, 0, &page, NULL);
  61. up_read(&mm->mmap_sem);
  62. unuse_mm(mm);
  63. spin_lock(&vcpu->async_pf.lock);
  64. list_add_tail(&apf->link, &vcpu->async_pf.done);
  65. apf->page = page;
  66. apf->done = true;
  67. spin_unlock(&vcpu->async_pf.lock);
  68. /*
  69. * apf may be freed by kvm_check_async_pf_completion() after
  70. * this point
  71. */
  72. trace_kvm_async_pf_completed(addr, page, gva);
  73. if (waitqueue_active(&vcpu->wq))
  74. wake_up_interruptible(&vcpu->wq);
  75. mmdrop(mm);
  76. kvm_put_kvm(vcpu->kvm);
  77. }
  78. void kvm_clear_async_pf_completion_queue(struct kvm_vcpu *vcpu)
  79. {
  80. /* cancel outstanding work queue item */
  81. while (!list_empty(&vcpu->async_pf.queue)) {
  82. struct kvm_async_pf *work =
  83. list_entry(vcpu->async_pf.queue.next,
  84. typeof(*work), queue);
  85. cancel_work_sync(&work->work);
  86. list_del(&work->queue);
  87. if (!work->done) /* work was canceled */
  88. kmem_cache_free(async_pf_cache, work);
  89. }
  90. spin_lock(&vcpu->async_pf.lock);
  91. while (!list_empty(&vcpu->async_pf.done)) {
  92. struct kvm_async_pf *work =
  93. list_entry(vcpu->async_pf.done.next,
  94. typeof(*work), link);
  95. list_del(&work->link);
  96. if (work->page)
  97. put_page(work->page);
  98. kmem_cache_free(async_pf_cache, work);
  99. }
  100. spin_unlock(&vcpu->async_pf.lock);
  101. vcpu->async_pf.queued = 0;
  102. }
  103. void kvm_check_async_pf_completion(struct kvm_vcpu *vcpu)
  104. {
  105. struct kvm_async_pf *work;
  106. while (!list_empty_careful(&vcpu->async_pf.done) &&
  107. kvm_arch_can_inject_async_page_present(vcpu)) {
  108. spin_lock(&vcpu->async_pf.lock);
  109. work = list_first_entry(&vcpu->async_pf.done, typeof(*work),
  110. link);
  111. list_del(&work->link);
  112. spin_unlock(&vcpu->async_pf.lock);
  113. if (work->page)
  114. kvm_arch_async_page_ready(vcpu, work);
  115. kvm_arch_async_page_present(vcpu, work);
  116. list_del(&work->queue);
  117. vcpu->async_pf.queued--;
  118. if (work->page)
  119. put_page(work->page);
  120. kmem_cache_free(async_pf_cache, work);
  121. }
  122. }
  123. int kvm_setup_async_pf(struct kvm_vcpu *vcpu, gva_t gva, gfn_t gfn,
  124. struct kvm_arch_async_pf *arch)
  125. {
  126. struct kvm_async_pf *work;
  127. if (vcpu->async_pf.queued >= ASYNC_PF_PER_VCPU)
  128. return 0;
  129. /* setup delayed work */
  130. /*
  131. * do alloc nowait since if we are going to sleep anyway we
  132. * may as well sleep faulting in page
  133. */
  134. work = kmem_cache_zalloc(async_pf_cache, GFP_NOWAIT);
  135. if (!work)
  136. return 0;
  137. work->page = NULL;
  138. work->done = false;
  139. work->vcpu = vcpu;
  140. work->gva = gva;
  141. work->addr = gfn_to_hva(vcpu->kvm, gfn);
  142. work->arch = *arch;
  143. work->mm = current->mm;
  144. atomic_inc(&work->mm->mm_count);
  145. kvm_get_kvm(work->vcpu->kvm);
  146. /* this can't really happen otherwise gfn_to_pfn_async
  147. would succeed */
  148. if (unlikely(kvm_is_error_hva(work->addr)))
  149. goto retry_sync;
  150. INIT_WORK(&work->work, async_pf_execute);
  151. if (!schedule_work(&work->work))
  152. goto retry_sync;
  153. list_add_tail(&work->queue, &vcpu->async_pf.queue);
  154. vcpu->async_pf.queued++;
  155. kvm_arch_async_page_not_present(vcpu, work);
  156. return 1;
  157. retry_sync:
  158. kvm_put_kvm(work->vcpu->kvm);
  159. mmdrop(work->mm);
  160. kmem_cache_free(async_pf_cache, work);
  161. return 0;
  162. }
  163. int kvm_async_pf_wakeup_all(struct kvm_vcpu *vcpu)
  164. {
  165. struct kvm_async_pf *work;
  166. if (!list_empty_careful(&vcpu->async_pf.done))
  167. return 0;
  168. work = kmem_cache_zalloc(async_pf_cache, GFP_ATOMIC);
  169. if (!work)
  170. return -ENOMEM;
  171. work->page = bad_page;
  172. get_page(bad_page);
  173. INIT_LIST_HEAD(&work->queue); /* for list_del to work */
  174. spin_lock(&vcpu->async_pf.lock);
  175. list_add_tail(&work->link, &vcpu->async_pf.done);
  176. spin_unlock(&vcpu->async_pf.lock);
  177. vcpu->async_pf.queued++;
  178. return 0;
  179. }