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.
14 #include <linux/kvm_host.h>
15 #include <linux/rculist.h>
17 #include <asm/kvm_page_track.h>
21 void kvm_page_track_free_memslot(struct kvm_memory_slot *slot)
25 for (i = 0; i < KVM_PAGE_TRACK_MAX; i++) {
26 kvfree(slot->arch.gfn_track[i]);
27 slot->arch.gfn_track[i] = NULL;
31 int kvm_page_track_create_memslot(struct kvm_memory_slot *slot,
36 for (i = 0; i < KVM_PAGE_TRACK_MAX; i++) {
37 slot->arch.gfn_track[i] =
38 kvcalloc(npages, sizeof(*slot->arch.gfn_track[i]),
40 if (!slot->arch.gfn_track[i])
47 kvm_page_track_free_memslot(slot);
51 static inline bool page_track_mode_is_valid(enum kvm_page_track_mode mode)
53 if (mode < 0 || mode >= KVM_PAGE_TRACK_MAX)
59 static void update_gfn_track(struct kvm_memory_slot *slot, gfn_t gfn,
60 enum kvm_page_track_mode mode, short count)
64 index = gfn_to_index(gfn, slot->base_gfn, PT_PAGE_TABLE_LEVEL);
66 val = slot->arch.gfn_track[mode][index];
68 if (WARN_ON(val + count < 0 || val + count > USHRT_MAX))
71 slot->arch.gfn_track[mode][index] += count;
75 * add guest page to the tracking pool so that corresponding access on that
76 * page will be intercepted.
78 * It should be called under the protection both of mmu-lock and kvm->srcu
81 * @kvm: the guest instance we are interested in.
82 * @slot: the @gfn belongs to.
83 * @gfn: the guest page.
84 * @mode: tracking mode, currently only write track is supported.
86 void kvm_slot_page_track_add_page(struct kvm *kvm,
87 struct kvm_memory_slot *slot, gfn_t gfn,
88 enum kvm_page_track_mode mode)
91 if (WARN_ON(!page_track_mode_is_valid(mode)))
94 update_gfn_track(slot, gfn, mode, 1);
97 * new track stops large page mapping for the
100 kvm_mmu_gfn_disallow_lpage(slot, gfn);
102 if (mode == KVM_PAGE_TRACK_WRITE)
103 if (kvm_mmu_slot_gfn_write_protect(kvm, slot, gfn))
104 kvm_flush_remote_tlbs(kvm);
106 EXPORT_SYMBOL_GPL(kvm_slot_page_track_add_page);
109 * remove the guest page from the tracking pool which stops the interception
110 * of corresponding access on that page. It is the opposed operation of
111 * kvm_slot_page_track_add_page().
113 * It should be called under the protection both of mmu-lock and kvm->srcu
114 * or kvm->slots_lock.
116 * @kvm: the guest instance we are interested in.
117 * @slot: the @gfn belongs to.
118 * @gfn: the guest page.
119 * @mode: tracking mode, currently only write track is supported.
121 void kvm_slot_page_track_remove_page(struct kvm *kvm,
122 struct kvm_memory_slot *slot, gfn_t gfn,
123 enum kvm_page_track_mode mode)
125 if (WARN_ON(!page_track_mode_is_valid(mode)))
128 update_gfn_track(slot, gfn, mode, -1);
131 * allow large page mapping for the tracked page
132 * after the tracker is gone.
134 kvm_mmu_gfn_allow_lpage(slot, gfn);
136 EXPORT_SYMBOL_GPL(kvm_slot_page_track_remove_page);
139 * check if the corresponding access on the specified guest page is tracked.
141 bool kvm_page_track_is_active(struct kvm_vcpu *vcpu, gfn_t gfn,
142 enum kvm_page_track_mode mode)
144 struct kvm_memory_slot *slot;
147 if (WARN_ON(!page_track_mode_is_valid(mode)))
150 slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
154 index = gfn_to_index(gfn, slot->base_gfn, PT_PAGE_TABLE_LEVEL);
155 return !!READ_ONCE(slot->arch.gfn_track[mode][index]);
158 void kvm_page_track_cleanup(struct kvm *kvm)
160 struct kvm_page_track_notifier_head *head;
162 head = &kvm->arch.track_notifier_head;
163 cleanup_srcu_struct(&head->track_srcu);
166 void kvm_page_track_init(struct kvm *kvm)
168 struct kvm_page_track_notifier_head *head;
170 head = &kvm->arch.track_notifier_head;
171 init_srcu_struct(&head->track_srcu);
172 INIT_HLIST_HEAD(&head->track_notifier_list);
176 * register the notifier so that event interception for the tracked guest
177 * pages can be received.
180 kvm_page_track_register_notifier(struct kvm *kvm,
181 struct kvm_page_track_notifier_node *n)
183 struct kvm_page_track_notifier_head *head;
185 head = &kvm->arch.track_notifier_head;
187 spin_lock(&kvm->mmu_lock);
188 hlist_add_head_rcu(&n->node, &head->track_notifier_list);
189 spin_unlock(&kvm->mmu_lock);
191 EXPORT_SYMBOL_GPL(kvm_page_track_register_notifier);
194 * stop receiving the event interception. It is the opposed operation of
195 * kvm_page_track_register_notifier().
198 kvm_page_track_unregister_notifier(struct kvm *kvm,
199 struct kvm_page_track_notifier_node *n)
201 struct kvm_page_track_notifier_head *head;
203 head = &kvm->arch.track_notifier_head;
205 spin_lock(&kvm->mmu_lock);
206 hlist_del_rcu(&n->node);
207 spin_unlock(&kvm->mmu_lock);
208 synchronize_srcu(&head->track_srcu);
210 EXPORT_SYMBOL_GPL(kvm_page_track_unregister_notifier);
213 * Notify the node that write access is intercepted and write emulation is
214 * finished at this time.
216 * The node should figure out if the written page is the one that node is
217 * interested in by itself.
219 void kvm_page_track_write(struct kvm_vcpu *vcpu, gpa_t gpa, const u8 *new,
222 struct kvm_page_track_notifier_head *head;
223 struct kvm_page_track_notifier_node *n;
226 head = &vcpu->kvm->arch.track_notifier_head;
228 if (hlist_empty(&head->track_notifier_list))
231 idx = srcu_read_lock(&head->track_srcu);
232 hlist_for_each_entry_rcu(n, &head->track_notifier_list, node)
234 n->track_write(vcpu, gpa, new, bytes, n);
235 srcu_read_unlock(&head->track_srcu, idx);
239 * Notify the node that memory slot is being removed or moved so that it can
240 * drop write-protection for the pages in the memory slot.
242 * The node should figure out it has any write-protected pages in this slot
245 void kvm_page_track_flush_slot(struct kvm *kvm, struct kvm_memory_slot *slot)
247 struct kvm_page_track_notifier_head *head;
248 struct kvm_page_track_notifier_node *n;
251 head = &kvm->arch.track_notifier_head;
253 if (hlist_empty(&head->track_notifier_list))
256 idx = srcu_read_lock(&head->track_srcu);
257 hlist_for_each_entry_rcu(n, &head->track_notifier_list, node)
258 if (n->track_flush_slot)
259 n->track_flush_slot(kvm, slot, n);
260 srcu_read_unlock(&head->track_srcu, idx);