page_track.c 6.7 KB

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