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 void kvm_page_track_free_memslot(struct kvm_memory_slot *slot)
26 for (i = 0; i < KVM_PAGE_TRACK_MAX; i++) {
27 kvfree(slot->arch.gfn_track[i]);
28 slot->arch.gfn_track[i] = NULL;
32 int kvm_page_track_create_memslot(struct kvm_memory_slot *slot,
37 for (i = 0; i < KVM_PAGE_TRACK_MAX; i++) {
38 slot->arch.gfn_track[i] =
39 __vcalloc(npages, sizeof(*slot->arch.gfn_track[i]),
41 if (!slot->arch.gfn_track[i])
48 kvm_page_track_free_memslot(slot);
52 static inline bool page_track_mode_is_valid(enum kvm_page_track_mode mode)
54 if (mode < 0 || mode >= KVM_PAGE_TRACK_MAX)
60 static void update_gfn_track(struct kvm_memory_slot *slot, gfn_t gfn,
61 enum kvm_page_track_mode mode, short count)
65 index = gfn_to_index(gfn, slot->base_gfn, PG_LEVEL_4K);
67 val = slot->arch.gfn_track[mode][index];
69 if (WARN_ON(val + count < 0 || val + count > USHRT_MAX))
72 slot->arch.gfn_track[mode][index] += count;
76 * add guest page to the tracking pool so that corresponding access on that
77 * page will be intercepted.
79 * It should be called under the protection both of mmu-lock and kvm->srcu
82 * @kvm: the guest instance we are interested in.
83 * @slot: the @gfn belongs to.
84 * @gfn: the guest page.
85 * @mode: tracking mode, currently only write track is supported.
87 void kvm_slot_page_track_add_page(struct kvm *kvm,
88 struct kvm_memory_slot *slot, gfn_t gfn,
89 enum kvm_page_track_mode mode)
92 if (WARN_ON(!page_track_mode_is_valid(mode)))
95 update_gfn_track(slot, gfn, mode, 1);
98 * new track stops large page mapping for the
101 kvm_mmu_gfn_disallow_lpage(slot, gfn);
103 if (mode == KVM_PAGE_TRACK_WRITE)
104 if (kvm_mmu_slot_gfn_write_protect(kvm, slot, gfn, PG_LEVEL_4K))
105 kvm_flush_remote_tlbs(kvm);
107 EXPORT_SYMBOL_GPL(kvm_slot_page_track_add_page);
110 * remove the guest page from the tracking pool which stops the interception
111 * of corresponding access on that page. It is the opposed operation of
112 * kvm_slot_page_track_add_page().
114 * It should be called under the protection both of mmu-lock and kvm->srcu
115 * or kvm->slots_lock.
117 * @kvm: the guest instance we are interested in.
118 * @slot: the @gfn belongs to.
119 * @gfn: the guest page.
120 * @mode: tracking mode, currently only write track is supported.
122 void kvm_slot_page_track_remove_page(struct kvm *kvm,
123 struct kvm_memory_slot *slot, gfn_t gfn,
124 enum kvm_page_track_mode mode)
126 if (WARN_ON(!page_track_mode_is_valid(mode)))
129 update_gfn_track(slot, gfn, mode, -1);
132 * allow large page mapping for the tracked page
133 * after the tracker is gone.
135 kvm_mmu_gfn_allow_lpage(slot, gfn);
137 EXPORT_SYMBOL_GPL(kvm_slot_page_track_remove_page);
140 * check if the corresponding access on the specified guest page is tracked.
142 bool kvm_page_track_is_active(struct kvm_vcpu *vcpu, gfn_t gfn,
143 enum kvm_page_track_mode mode)
145 struct kvm_memory_slot *slot;
148 if (WARN_ON(!page_track_mode_is_valid(mode)))
151 slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
155 index = gfn_to_index(gfn, slot->base_gfn, PG_LEVEL_4K);
156 return !!READ_ONCE(slot->arch.gfn_track[mode][index]);
159 void kvm_page_track_cleanup(struct kvm *kvm)
161 struct kvm_page_track_notifier_head *head;
163 head = &kvm->arch.track_notifier_head;
164 cleanup_srcu_struct(&head->track_srcu);
167 int kvm_page_track_init(struct kvm *kvm)
169 struct kvm_page_track_notifier_head *head;
171 head = &kvm->arch.track_notifier_head;
172 INIT_HLIST_HEAD(&head->track_notifier_list);
173 return init_srcu_struct(&head->track_srcu);
177 * register the notifier so that event interception for the tracked guest
178 * pages can be received.
181 kvm_page_track_register_notifier(struct kvm *kvm,
182 struct kvm_page_track_notifier_node *n)
184 struct kvm_page_track_notifier_head *head;
186 head = &kvm->arch.track_notifier_head;
188 write_lock(&kvm->mmu_lock);
189 hlist_add_head_rcu(&n->node, &head->track_notifier_list);
190 write_unlock(&kvm->mmu_lock);
192 EXPORT_SYMBOL_GPL(kvm_page_track_register_notifier);
195 * stop receiving the event interception. It is the opposed operation of
196 * kvm_page_track_register_notifier().
199 kvm_page_track_unregister_notifier(struct kvm *kvm,
200 struct kvm_page_track_notifier_node *n)
202 struct kvm_page_track_notifier_head *head;
204 head = &kvm->arch.track_notifier_head;
206 write_lock(&kvm->mmu_lock);
207 hlist_del_rcu(&n->node);
208 write_unlock(&kvm->mmu_lock);
209 synchronize_srcu(&head->track_srcu);
211 EXPORT_SYMBOL_GPL(kvm_page_track_unregister_notifier);
214 * Notify the node that write access is intercepted and write emulation is
215 * finished at this time.
217 * The node should figure out if the written page is the one that node is
218 * interested in by itself.
220 void kvm_page_track_write(struct kvm_vcpu *vcpu, gpa_t gpa, const u8 *new,
223 struct kvm_page_track_notifier_head *head;
224 struct kvm_page_track_notifier_node *n;
227 head = &vcpu->kvm->arch.track_notifier_head;
229 if (hlist_empty(&head->track_notifier_list))
232 idx = srcu_read_lock(&head->track_srcu);
233 hlist_for_each_entry_srcu(n, &head->track_notifier_list, node,
234 srcu_read_lock_held(&head->track_srcu))
236 n->track_write(vcpu, gpa, new, bytes, n);
237 srcu_read_unlock(&head->track_srcu, idx);
241 * Notify the node that memory slot is being removed or moved so that it can
242 * drop write-protection for the pages in the memory slot.
244 * The node should figure out it has any write-protected pages in this slot
247 void kvm_page_track_flush_slot(struct kvm *kvm, struct kvm_memory_slot *slot)
249 struct kvm_page_track_notifier_head *head;
250 struct kvm_page_track_notifier_node *n;
253 head = &kvm->arch.track_notifier_head;
255 if (hlist_empty(&head->track_notifier_list))
258 idx = srcu_read_lock(&head->track_srcu);
259 hlist_for_each_entry_srcu(n, &head->track_notifier_list, node,
260 srcu_read_lock_held(&head->track_srcu))
261 if (n->track_flush_slot)
262 n->track_flush_slot(kvm, slot, n);
263 srcu_read_unlock(&head->track_srcu, idx);