page_track.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * Support KVM gust page tracking
  3. *
  4. * This feature allows us to track page access in guest. Currently, only
  5. * write access is tracked.
  6. *
  7. * Copyright(C) 2015 Intel Corporation.
  8. *
  9. * Author:
  10. * Xiao Guangrong <guangrong.xiao@linux.intel.com>
  11. *
  12. * This work is licensed under the terms of the GNU GPL, version 2. See
  13. * the COPYING file in the top-level directory.
  14. */
  15. #include <linux/kvm_host.h>
  16. #include <linux/rculist.h>
  17. #include <asm/kvm_host.h>
  18. #include <asm/kvm_page_track.h>
  19. #include "mmu.h"
  20. void kvm_page_track_free_memslot(struct kvm_memory_slot *free,
  21. struct kvm_memory_slot *dont)
  22. {
  23. int i;
  24. for (i = 0; i < KVM_PAGE_TRACK_MAX; i++)
  25. if (!dont || free->arch.gfn_track[i] !=
  26. dont->arch.gfn_track[i]) {
  27. kvfree(free->arch.gfn_track[i]);
  28. free->arch.gfn_track[i] = NULL;
  29. }
  30. }
  31. int kvm_page_track_create_memslot(struct kvm_memory_slot *slot,
  32. unsigned long npages)
  33. {
  34. int i;
  35. for (i = 0; i < KVM_PAGE_TRACK_MAX; i++) {
  36. slot->arch.gfn_track[i] =
  37. kvcalloc(npages, sizeof(*slot->arch.gfn_track[i]),
  38. GFP_KERNEL);
  39. if (!slot->arch.gfn_track[i])
  40. goto track_free;
  41. }
  42. return 0;
  43. track_free:
  44. kvm_page_track_free_memslot(slot, NULL);
  45. return -ENOMEM;
  46. }
  47. static inline bool page_track_mode_is_valid(enum kvm_page_track_mode mode)
  48. {
  49. if (mode < 0 || mode >= KVM_PAGE_TRACK_MAX)
  50. return false;
  51. return true;
  52. }
  53. static void update_gfn_track(struct kvm_memory_slot *slot, gfn_t gfn,
  54. enum kvm_page_track_mode mode, short count)
  55. {
  56. int index, val;
  57. index = gfn_to_index(gfn, slot->base_gfn, PT_PAGE_TABLE_LEVEL);
  58. val = slot->arch.gfn_track[mode][index];
  59. if (WARN_ON(val + count < 0 || val + count > USHRT_MAX))
  60. return;
  61. slot->arch.gfn_track[mode][index] += count;
  62. }
  63. /*
  64. * add guest page to the tracking pool so that corresponding access on that
  65. * page will be intercepted.
  66. *
  67. * It should be called under the protection both of mmu-lock and kvm->srcu
  68. * or kvm->slots_lock.
  69. *
  70. * @kvm: the guest instance we are interested in.
  71. * @slot: the @gfn belongs to.
  72. * @gfn: the guest page.
  73. * @mode: tracking mode, currently only write track is supported.
  74. */
  75. void kvm_slot_page_track_add_page(struct kvm *kvm,
  76. struct kvm_memory_slot *slot, gfn_t gfn,
  77. enum kvm_page_track_mode mode)
  78. {
  79. if (WARN_ON(!page_track_mode_is_valid(mode)))
  80. return;
  81. update_gfn_track(slot, gfn, mode, 1);
  82. /*
  83. * new track stops large page mapping for the
  84. * tracked page.
  85. */
  86. kvm_mmu_gfn_disallow_lpage(slot, gfn);
  87. if (mode == KVM_PAGE_TRACK_WRITE)
  88. if (kvm_mmu_slot_gfn_write_protect(kvm, slot, gfn))
  89. kvm_flush_remote_tlbs(kvm);
  90. }
  91. EXPORT_SYMBOL_GPL(kvm_slot_page_track_add_page);
  92. /*
  93. * remove the guest page from the tracking pool which stops the interception
  94. * of corresponding access on that page. It is the opposed operation of
  95. * kvm_slot_page_track_add_page().
  96. *
  97. * It should be called under the protection both of mmu-lock and kvm->srcu
  98. * or kvm->slots_lock.
  99. *
  100. * @kvm: the guest instance we are interested in.
  101. * @slot: the @gfn belongs to.
  102. * @gfn: the guest page.
  103. * @mode: tracking mode, currently only write track is supported.
  104. */
  105. void kvm_slot_page_track_remove_page(struct kvm *kvm,
  106. struct kvm_memory_slot *slot, gfn_t gfn,
  107. enum kvm_page_track_mode mode)
  108. {
  109. if (WARN_ON(!page_track_mode_is_valid(mode)))
  110. return;
  111. update_gfn_track(slot, gfn, mode, -1);
  112. /*
  113. * allow large page mapping for the tracked page
  114. * after the tracker is gone.
  115. */
  116. kvm_mmu_gfn_allow_lpage(slot, gfn);
  117. }
  118. EXPORT_SYMBOL_GPL(kvm_slot_page_track_remove_page);
  119. /*
  120. * check if the corresponding access on the specified guest page is tracked.
  121. */
  122. bool kvm_page_track_is_active(struct kvm_vcpu *vcpu, gfn_t gfn,
  123. enum kvm_page_track_mode mode)
  124. {
  125. struct kvm_memory_slot *slot;
  126. int index;
  127. if (WARN_ON(!page_track_mode_is_valid(mode)))
  128. return false;
  129. slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
  130. if (!slot)
  131. return false;
  132. index = gfn_to_index(gfn, slot->base_gfn, PT_PAGE_TABLE_LEVEL);
  133. return !!READ_ONCE(slot->arch.gfn_track[mode][index]);
  134. }
  135. void kvm_page_track_cleanup(struct kvm *kvm)
  136. {
  137. struct kvm_page_track_notifier_head *head;
  138. head = &kvm->arch.track_notifier_head;
  139. cleanup_srcu_struct(&head->track_srcu);
  140. }
  141. void kvm_page_track_init(struct kvm *kvm)
  142. {
  143. struct kvm_page_track_notifier_head *head;
  144. head = &kvm->arch.track_notifier_head;
  145. init_srcu_struct(&head->track_srcu);
  146. INIT_HLIST_HEAD(&head->track_notifier_list);
  147. }
  148. /*
  149. * register the notifier so that event interception for the tracked guest
  150. * pages can be received.
  151. */
  152. void
  153. kvm_page_track_register_notifier(struct kvm *kvm,
  154. struct kvm_page_track_notifier_node *n)
  155. {
  156. struct kvm_page_track_notifier_head *head;
  157. head = &kvm->arch.track_notifier_head;
  158. spin_lock(&kvm->mmu_lock);
  159. hlist_add_head_rcu(&n->node, &head->track_notifier_list);
  160. spin_unlock(&kvm->mmu_lock);
  161. }
  162. EXPORT_SYMBOL_GPL(kvm_page_track_register_notifier);
  163. /*
  164. * stop receiving the event interception. It is the opposed operation of
  165. * kvm_page_track_register_notifier().
  166. */
  167. void
  168. kvm_page_track_unregister_notifier(struct kvm *kvm,
  169. struct kvm_page_track_notifier_node *n)
  170. {
  171. struct kvm_page_track_notifier_head *head;
  172. head = &kvm->arch.track_notifier_head;
  173. spin_lock(&kvm->mmu_lock);
  174. hlist_del_rcu(&n->node);
  175. spin_unlock(&kvm->mmu_lock);
  176. synchronize_srcu(&head->track_srcu);
  177. }
  178. EXPORT_SYMBOL_GPL(kvm_page_track_unregister_notifier);
  179. /*
  180. * Notify the node that write access is intercepted and write emulation is
  181. * finished at this time.
  182. *
  183. * The node should figure out if the written page is the one that node is
  184. * interested in by itself.
  185. */
  186. void kvm_page_track_write(struct kvm_vcpu *vcpu, gpa_t gpa, const u8 *new,
  187. int bytes)
  188. {
  189. struct kvm_page_track_notifier_head *head;
  190. struct kvm_page_track_notifier_node *n;
  191. int idx;
  192. head = &vcpu->kvm->arch.track_notifier_head;
  193. if (hlist_empty(&head->track_notifier_list))
  194. return;
  195. idx = srcu_read_lock(&head->track_srcu);
  196. hlist_for_each_entry_rcu(n, &head->track_notifier_list, node)
  197. if (n->track_write)
  198. n->track_write(vcpu, gpa, new, bytes, n);
  199. srcu_read_unlock(&head->track_srcu, idx);
  200. }
  201. /*
  202. * Notify the node that memory slot is being removed or moved so that it can
  203. * drop write-protection for the pages in the memory slot.
  204. *
  205. * The node should figure out it has any write-protected pages in this slot
  206. * by itself.
  207. */
  208. void kvm_page_track_flush_slot(struct kvm *kvm, struct kvm_memory_slot *slot)
  209. {
  210. struct kvm_page_track_notifier_head *head;
  211. struct kvm_page_track_notifier_node *n;
  212. int idx;
  213. head = &kvm->arch.track_notifier_head;
  214. if (hlist_empty(&head->track_notifier_list))
  215. return;
  216. idx = srcu_read_lock(&head->track_srcu);
  217. hlist_for_each_entry_rcu(n, &head->track_notifier_list, node)
  218. if (n->track_flush_slot)
  219. n->track_flush_slot(kvm, slot, n);
  220. srcu_read_unlock(&head->track_srcu, idx);
  221. }