1 // SPDX-License-Identifier: GPL-2.0-only
3 * Support KVM gust page tracking
5 * This feature allows us to track page access in guest. Currently, only
6 * write access is tracked.
8 * Copyright(C) 2015 Intel Corporation.
11 * Xiao Guangrong <guangrong.xiao@linux.intel.com>
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/kvm_host.h>
16 #include <linux/rculist.h>
18 #include <asm/kvm_page_track.h>
21 #include "mmu_internal.h"
23 bool kvm_page_track_write_tracking_enabled(struct kvm *kvm)
25 return IS_ENABLED(CONFIG_KVM_EXTERNAL_WRITE_TRACKING) ||
26 !tdp_enabled || kvm_shadow_root_allocated(kvm);
29 void kvm_page_track_free_memslot(struct kvm_memory_slot *slot)
33 for (i = 0; i < KVM_PAGE_TRACK_MAX; i++) {
34 kvfree(slot->arch.gfn_track[i]);
35 slot->arch.gfn_track[i] = NULL;
39 int kvm_page_track_create_memslot(struct kvm *kvm,
40 struct kvm_memory_slot *slot,
45 for (i = 0; i < KVM_PAGE_TRACK_MAX; i++) {
46 if (i == KVM_PAGE_TRACK_WRITE &&
47 !kvm_page_track_write_tracking_enabled(kvm))
50 slot->arch.gfn_track[i] =
51 __vcalloc(npages, sizeof(*slot->arch.gfn_track[i]),
53 if (!slot->arch.gfn_track[i])
60 kvm_page_track_free_memslot(slot);
64 static inline bool page_track_mode_is_valid(enum kvm_page_track_mode mode)
66 if (mode < 0 || mode >= KVM_PAGE_TRACK_MAX)
72 int kvm_page_track_write_tracking_alloc(struct kvm_memory_slot *slot)
74 unsigned short *gfn_track;
76 if (slot->arch.gfn_track[KVM_PAGE_TRACK_WRITE])
79 gfn_track = __vcalloc(slot->npages, sizeof(*gfn_track),
81 if (gfn_track == NULL)
84 slot->arch.gfn_track[KVM_PAGE_TRACK_WRITE] = gfn_track;
88 static void update_gfn_track(struct kvm_memory_slot *slot, gfn_t gfn,
89 enum kvm_page_track_mode mode, short count)
93 index = gfn_to_index(gfn, slot->base_gfn, PG_LEVEL_4K);
95 val = slot->arch.gfn_track[mode][index];
97 if (WARN_ON(val + count < 0 || val + count > USHRT_MAX))
100 slot->arch.gfn_track[mode][index] += count;
104 * add guest page to the tracking pool so that corresponding access on that
105 * page will be intercepted.
107 * It should be called under the protection both of mmu-lock and kvm->srcu
108 * or kvm->slots_lock.
110 * @kvm: the guest instance we are interested in.
111 * @slot: the @gfn belongs to.
112 * @gfn: the guest page.
113 * @mode: tracking mode, currently only write track is supported.
115 void kvm_slot_page_track_add_page(struct kvm *kvm,
116 struct kvm_memory_slot *slot, gfn_t gfn,
117 enum kvm_page_track_mode mode)
120 if (WARN_ON(!page_track_mode_is_valid(mode)))
123 if (WARN_ON(mode == KVM_PAGE_TRACK_WRITE &&
124 !kvm_page_track_write_tracking_enabled(kvm)))
127 update_gfn_track(slot, gfn, mode, 1);
130 * new track stops large page mapping for the
133 kvm_mmu_gfn_disallow_lpage(slot, gfn);
135 if (mode == KVM_PAGE_TRACK_WRITE)
136 if (kvm_mmu_slot_gfn_write_protect(kvm, slot, gfn, PG_LEVEL_4K))
137 kvm_flush_remote_tlbs(kvm);
139 EXPORT_SYMBOL_GPL(kvm_slot_page_track_add_page);
142 * remove the guest page from the tracking pool which stops the interception
143 * of corresponding access on that page. It is the opposed operation of
144 * kvm_slot_page_track_add_page().
146 * It should be called under the protection both of mmu-lock and kvm->srcu
147 * or kvm->slots_lock.
149 * @kvm: the guest instance we are interested in.
150 * @slot: the @gfn belongs to.
151 * @gfn: the guest page.
152 * @mode: tracking mode, currently only write track is supported.
154 void kvm_slot_page_track_remove_page(struct kvm *kvm,
155 struct kvm_memory_slot *slot, gfn_t gfn,
156 enum kvm_page_track_mode mode)
158 if (WARN_ON(!page_track_mode_is_valid(mode)))
161 if (WARN_ON(mode == KVM_PAGE_TRACK_WRITE &&
162 !kvm_page_track_write_tracking_enabled(kvm)))
165 update_gfn_track(slot, gfn, mode, -1);
168 * allow large page mapping for the tracked page
169 * after the tracker is gone.
171 kvm_mmu_gfn_allow_lpage(slot, gfn);
173 EXPORT_SYMBOL_GPL(kvm_slot_page_track_remove_page);
176 * check if the corresponding access on the specified guest page is tracked.
178 bool kvm_slot_page_track_is_active(struct kvm *kvm,
179 const struct kvm_memory_slot *slot,
180 gfn_t gfn, enum kvm_page_track_mode mode)
184 if (WARN_ON(!page_track_mode_is_valid(mode)))
190 if (mode == KVM_PAGE_TRACK_WRITE &&
191 !kvm_page_track_write_tracking_enabled(kvm))
194 index = gfn_to_index(gfn, slot->base_gfn, PG_LEVEL_4K);
195 return !!READ_ONCE(slot->arch.gfn_track[mode][index]);
198 void kvm_page_track_cleanup(struct kvm *kvm)
200 struct kvm_page_track_notifier_head *head;
202 head = &kvm->arch.track_notifier_head;
203 cleanup_srcu_struct(&head->track_srcu);
206 int kvm_page_track_init(struct kvm *kvm)
208 struct kvm_page_track_notifier_head *head;
210 head = &kvm->arch.track_notifier_head;
211 INIT_HLIST_HEAD(&head->track_notifier_list);
212 return init_srcu_struct(&head->track_srcu);
216 * register the notifier so that event interception for the tracked guest
217 * pages can be received.
220 kvm_page_track_register_notifier(struct kvm *kvm,
221 struct kvm_page_track_notifier_node *n)
223 struct kvm_page_track_notifier_head *head;
225 head = &kvm->arch.track_notifier_head;
227 write_lock(&kvm->mmu_lock);
228 hlist_add_head_rcu(&n->node, &head->track_notifier_list);
229 write_unlock(&kvm->mmu_lock);
231 EXPORT_SYMBOL_GPL(kvm_page_track_register_notifier);
234 * stop receiving the event interception. It is the opposed operation of
235 * kvm_page_track_register_notifier().
238 kvm_page_track_unregister_notifier(struct kvm *kvm,
239 struct kvm_page_track_notifier_node *n)
241 struct kvm_page_track_notifier_head *head;
243 head = &kvm->arch.track_notifier_head;
245 write_lock(&kvm->mmu_lock);
246 hlist_del_rcu(&n->node);
247 write_unlock(&kvm->mmu_lock);
248 synchronize_srcu(&head->track_srcu);
250 EXPORT_SYMBOL_GPL(kvm_page_track_unregister_notifier);
253 * Notify the node that write access is intercepted and write emulation is
254 * finished at this time.
256 * The node should figure out if the written page is the one that node is
257 * interested in by itself.
259 void kvm_page_track_write(struct kvm_vcpu *vcpu, gpa_t gpa, const u8 *new,
262 struct kvm_page_track_notifier_head *head;
263 struct kvm_page_track_notifier_node *n;
266 head = &vcpu->kvm->arch.track_notifier_head;
268 if (hlist_empty(&head->track_notifier_list))
271 idx = srcu_read_lock(&head->track_srcu);
272 hlist_for_each_entry_srcu(n, &head->track_notifier_list, node,
273 srcu_read_lock_held(&head->track_srcu))
275 n->track_write(vcpu, gpa, new, bytes, n);
276 srcu_read_unlock(&head->track_srcu, idx);
280 * Notify the node that memory slot is being removed or moved so that it can
281 * drop write-protection for the pages in the memory slot.
283 * The node should figure out it has any write-protected pages in this slot
286 void kvm_page_track_flush_slot(struct kvm *kvm, struct kvm_memory_slot *slot)
288 struct kvm_page_track_notifier_head *head;
289 struct kvm_page_track_notifier_node *n;
292 head = &kvm->arch.track_notifier_head;
294 if (hlist_empty(&head->track_notifier_list))
297 idx = srcu_read_lock(&head->track_srcu);
298 hlist_for_each_entry_srcu(n, &head->track_notifier_list, node,
299 srcu_read_lock_held(&head->track_srcu))
300 if (n->track_flush_slot)
301 n->track_flush_slot(kvm, slot, n);
302 srcu_read_unlock(&head->track_srcu, idx);