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>
14 #include <linux/kvm_host.h>
15 #include <linux/rculist.h>
17 #include <asm/kvm_page_track.h>
20 #include "mmu_internal.h"
22 bool kvm_page_track_write_tracking_enabled(struct kvm *kvm)
24 return IS_ENABLED(CONFIG_KVM_EXTERNAL_WRITE_TRACKING) ||
25 !tdp_enabled || kvm_shadow_root_allocated(kvm);
28 void kvm_page_track_free_memslot(struct kvm_memory_slot *slot)
32 for (i = 0; i < KVM_PAGE_TRACK_MAX; i++) {
33 kvfree(slot->arch.gfn_track[i]);
34 slot->arch.gfn_track[i] = NULL;
38 int kvm_page_track_create_memslot(struct kvm *kvm,
39 struct kvm_memory_slot *slot,
44 for (i = 0; i < KVM_PAGE_TRACK_MAX; i++) {
45 if (i == KVM_PAGE_TRACK_WRITE &&
46 !kvm_page_track_write_tracking_enabled(kvm))
49 slot->arch.gfn_track[i] =
50 __vcalloc(npages, sizeof(*slot->arch.gfn_track[i]),
52 if (!slot->arch.gfn_track[i])
59 kvm_page_track_free_memslot(slot);
63 static inline bool page_track_mode_is_valid(enum kvm_page_track_mode mode)
65 if (mode < 0 || mode >= KVM_PAGE_TRACK_MAX)
71 int kvm_page_track_write_tracking_alloc(struct kvm_memory_slot *slot)
73 unsigned short *gfn_track;
75 if (slot->arch.gfn_track[KVM_PAGE_TRACK_WRITE])
78 gfn_track = __vcalloc(slot->npages, sizeof(*gfn_track),
80 if (gfn_track == NULL)
83 slot->arch.gfn_track[KVM_PAGE_TRACK_WRITE] = gfn_track;
87 static void update_gfn_track(struct kvm_memory_slot *slot, gfn_t gfn,
88 enum kvm_page_track_mode mode, short count)
92 index = gfn_to_index(gfn, slot->base_gfn, PG_LEVEL_4K);
94 val = slot->arch.gfn_track[mode][index];
96 if (WARN_ON(val + count < 0 || val + count > USHRT_MAX))
99 slot->arch.gfn_track[mode][index] += count;
103 * add guest page to the tracking pool so that corresponding access on that
104 * page will be intercepted.
106 * It should be called under the protection both of mmu-lock and kvm->srcu
107 * or kvm->slots_lock.
109 * @kvm: the guest instance we are interested in.
110 * @slot: the @gfn belongs to.
111 * @gfn: the guest page.
112 * @mode: tracking mode, currently only write track is supported.
114 void kvm_slot_page_track_add_page(struct kvm *kvm,
115 struct kvm_memory_slot *slot, gfn_t gfn,
116 enum kvm_page_track_mode mode)
119 if (WARN_ON(!page_track_mode_is_valid(mode)))
122 if (WARN_ON(mode == KVM_PAGE_TRACK_WRITE &&
123 !kvm_page_track_write_tracking_enabled(kvm)))
126 update_gfn_track(slot, gfn, mode, 1);
129 * new track stops large page mapping for the
132 kvm_mmu_gfn_disallow_lpage(slot, gfn);
134 if (mode == KVM_PAGE_TRACK_WRITE)
135 if (kvm_mmu_slot_gfn_write_protect(kvm, slot, gfn, PG_LEVEL_4K))
136 kvm_flush_remote_tlbs(kvm);
138 EXPORT_SYMBOL_GPL(kvm_slot_page_track_add_page);
141 * remove the guest page from the tracking pool which stops the interception
142 * of corresponding access on that page. It is the opposed operation of
143 * kvm_slot_page_track_add_page().
145 * It should be called under the protection both of mmu-lock and kvm->srcu
146 * or kvm->slots_lock.
148 * @kvm: the guest instance we are interested in.
149 * @slot: the @gfn belongs to.
150 * @gfn: the guest page.
151 * @mode: tracking mode, currently only write track is supported.
153 void kvm_slot_page_track_remove_page(struct kvm *kvm,
154 struct kvm_memory_slot *slot, gfn_t gfn,
155 enum kvm_page_track_mode mode)
157 if (WARN_ON(!page_track_mode_is_valid(mode)))
160 if (WARN_ON(mode == KVM_PAGE_TRACK_WRITE &&
161 !kvm_page_track_write_tracking_enabled(kvm)))
164 update_gfn_track(slot, gfn, mode, -1);
167 * allow large page mapping for the tracked page
168 * after the tracker is gone.
170 kvm_mmu_gfn_allow_lpage(slot, gfn);
172 EXPORT_SYMBOL_GPL(kvm_slot_page_track_remove_page);
175 * check if the corresponding access on the specified guest page is tracked.
177 bool kvm_slot_page_track_is_active(struct kvm *kvm,
178 const struct kvm_memory_slot *slot,
179 gfn_t gfn, enum kvm_page_track_mode mode)
183 if (WARN_ON(!page_track_mode_is_valid(mode)))
189 if (mode == KVM_PAGE_TRACK_WRITE &&
190 !kvm_page_track_write_tracking_enabled(kvm))
193 index = gfn_to_index(gfn, slot->base_gfn, PG_LEVEL_4K);
194 return !!READ_ONCE(slot->arch.gfn_track[mode][index]);
197 void kvm_page_track_cleanup(struct kvm *kvm)
199 struct kvm_page_track_notifier_head *head;
201 head = &kvm->arch.track_notifier_head;
202 cleanup_srcu_struct(&head->track_srcu);
205 int kvm_page_track_init(struct kvm *kvm)
207 struct kvm_page_track_notifier_head *head;
209 head = &kvm->arch.track_notifier_head;
210 INIT_HLIST_HEAD(&head->track_notifier_list);
211 return init_srcu_struct(&head->track_srcu);
215 * register the notifier so that event interception for the tracked guest
216 * pages can be received.
219 kvm_page_track_register_notifier(struct kvm *kvm,
220 struct kvm_page_track_notifier_node *n)
222 struct kvm_page_track_notifier_head *head;
224 head = &kvm->arch.track_notifier_head;
226 write_lock(&kvm->mmu_lock);
227 hlist_add_head_rcu(&n->node, &head->track_notifier_list);
228 write_unlock(&kvm->mmu_lock);
230 EXPORT_SYMBOL_GPL(kvm_page_track_register_notifier);
233 * stop receiving the event interception. It is the opposed operation of
234 * kvm_page_track_register_notifier().
237 kvm_page_track_unregister_notifier(struct kvm *kvm,
238 struct kvm_page_track_notifier_node *n)
240 struct kvm_page_track_notifier_head *head;
242 head = &kvm->arch.track_notifier_head;
244 write_lock(&kvm->mmu_lock);
245 hlist_del_rcu(&n->node);
246 write_unlock(&kvm->mmu_lock);
247 synchronize_srcu(&head->track_srcu);
249 EXPORT_SYMBOL_GPL(kvm_page_track_unregister_notifier);
252 * Notify the node that write access is intercepted and write emulation is
253 * finished at this time.
255 * The node should figure out if the written page is the one that node is
256 * interested in by itself.
258 void kvm_page_track_write(struct kvm_vcpu *vcpu, gpa_t gpa, const u8 *new,
261 struct kvm_page_track_notifier_head *head;
262 struct kvm_page_track_notifier_node *n;
265 head = &vcpu->kvm->arch.track_notifier_head;
267 if (hlist_empty(&head->track_notifier_list))
270 idx = srcu_read_lock(&head->track_srcu);
271 hlist_for_each_entry_srcu(n, &head->track_notifier_list, node,
272 srcu_read_lock_held(&head->track_srcu))
274 n->track_write(vcpu, gpa, new, bytes, n);
275 srcu_read_unlock(&head->track_srcu, idx);
279 * Notify the node that memory slot is being removed or moved so that it can
280 * drop write-protection for the pages in the memory slot.
282 * The node should figure out it has any write-protected pages in this slot
285 void kvm_page_track_flush_slot(struct kvm *kvm, struct kvm_memory_slot *slot)
287 struct kvm_page_track_notifier_head *head;
288 struct kvm_page_track_notifier_node *n;
291 head = &kvm->arch.track_notifier_head;
293 if (hlist_empty(&head->track_notifier_list))
296 idx = srcu_read_lock(&head->track_srcu);
297 hlist_for_each_entry_srcu(n, &head->track_notifier_list, node,
298 srcu_read_lock_held(&head->track_srcu))
299 if (n->track_flush_slot)
300 n->track_flush_slot(kvm, slot, n);
301 srcu_read_unlock(&head->track_srcu, idx);