blob: 4a1c13eaa518335320c995ee325c8800002b1cf0 [file] [log] [blame]
Xiao Guangrong21ebbed2016-02-24 17:51:09 +08001/*
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
16#include <linux/kvm_host.h>
17#include <asm/kvm_host.h>
18#include <asm/kvm_page_track.h>
19
20#include "mmu.h"
21
22void kvm_page_track_free_memslot(struct kvm_memory_slot *free,
23 struct kvm_memory_slot *dont)
24{
25 int i;
26
27 for (i = 0; i < KVM_PAGE_TRACK_MAX; i++)
28 if (!dont || free->arch.gfn_track[i] !=
29 dont->arch.gfn_track[i]) {
30 kvfree(free->arch.gfn_track[i]);
31 free->arch.gfn_track[i] = NULL;
32 }
33}
34
35int kvm_page_track_create_memslot(struct kvm_memory_slot *slot,
36 unsigned long npages)
37{
38 int i;
39
40 for (i = 0; i < KVM_PAGE_TRACK_MAX; i++) {
41 slot->arch.gfn_track[i] = kvm_kvzalloc(npages *
42 sizeof(*slot->arch.gfn_track[i]));
43 if (!slot->arch.gfn_track[i])
44 goto track_free;
45 }
46
47 return 0;
48
49track_free:
50 kvm_page_track_free_memslot(slot, NULL);
51 return -ENOMEM;
52}
Xiao Guangrongf29d4d72016-02-24 17:51:10 +080053
54static inline bool page_track_mode_is_valid(enum kvm_page_track_mode mode)
55{
56 if (mode < 0 || mode >= KVM_PAGE_TRACK_MAX)
57 return false;
58
59 return true;
60}
61
62static void update_gfn_track(struct kvm_memory_slot *slot, gfn_t gfn,
63 enum kvm_page_track_mode mode, short count)
64{
65 int index, val;
66
67 index = gfn_to_index(gfn, slot->base_gfn, PT_PAGE_TABLE_LEVEL);
68
69 val = slot->arch.gfn_track[mode][index];
70
71 if (WARN_ON(val + count < 0 || val + count > USHRT_MAX))
72 return;
73
74 slot->arch.gfn_track[mode][index] += count;
75}
76
77/*
78 * add guest page to the tracking pool so that corresponding access on that
79 * page will be intercepted.
80 *
81 * It should be called under the protection both of mmu-lock and kvm->srcu
82 * or kvm->slots_lock.
83 *
84 * @kvm: the guest instance we are interested in.
85 * @slot: the @gfn belongs to.
86 * @gfn: the guest page.
87 * @mode: tracking mode, currently only write track is supported.
88 */
89void kvm_slot_page_track_add_page(struct kvm *kvm,
90 struct kvm_memory_slot *slot, gfn_t gfn,
91 enum kvm_page_track_mode mode)
92{
93
94 if (WARN_ON(!page_track_mode_is_valid(mode)))
95 return;
96
97 update_gfn_track(slot, gfn, mode, 1);
98
99 /*
100 * new track stops large page mapping for the
101 * tracked page.
102 */
103 kvm_mmu_gfn_disallow_lpage(slot, gfn);
104
105 if (mode == KVM_PAGE_TRACK_WRITE)
106 if (kvm_mmu_slot_gfn_write_protect(kvm, slot, gfn))
107 kvm_flush_remote_tlbs(kvm);
108}
Jike Song871b7ef2016-10-25 15:50:43 +0800109EXPORT_SYMBOL_GPL(kvm_slot_page_track_add_page);
Xiao Guangrongf29d4d72016-02-24 17:51:10 +0800110
111/*
112 * remove the guest page from the tracking pool which stops the interception
113 * of corresponding access on that page. It is the opposed operation of
114 * kvm_slot_page_track_add_page().
115 *
116 * It should be called under the protection both of mmu-lock and kvm->srcu
117 * or kvm->slots_lock.
118 *
119 * @kvm: the guest instance we are interested in.
120 * @slot: the @gfn belongs to.
121 * @gfn: the guest page.
122 * @mode: tracking mode, currently only write track is supported.
123 */
124void kvm_slot_page_track_remove_page(struct kvm *kvm,
125 struct kvm_memory_slot *slot, gfn_t gfn,
126 enum kvm_page_track_mode mode)
127{
128 if (WARN_ON(!page_track_mode_is_valid(mode)))
129 return;
130
131 update_gfn_track(slot, gfn, mode, -1);
132
133 /*
134 * allow large page mapping for the tracked page
135 * after the tracker is gone.
136 */
137 kvm_mmu_gfn_allow_lpage(slot, gfn);
138}
Jike Song871b7ef2016-10-25 15:50:43 +0800139EXPORT_SYMBOL_GPL(kvm_slot_page_track_remove_page);
Xiao Guangrong3d0c27a2016-02-24 17:51:11 +0800140
141/*
142 * check if the corresponding access on the specified guest page is tracked.
143 */
144bool kvm_page_track_is_active(struct kvm_vcpu *vcpu, gfn_t gfn,
145 enum kvm_page_track_mode mode)
146{
Paolo Bonzinia6adb102016-03-22 17:25:42 +0100147 struct kvm_memory_slot *slot;
148 int index;
Xiao Guangrong3d0c27a2016-02-24 17:51:11 +0800149
150 if (WARN_ON(!page_track_mode_is_valid(mode)))
151 return false;
152
Paolo Bonzinia6adb102016-03-22 17:25:42 +0100153 slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
154 if (!slot)
155 return false;
156
157 index = gfn_to_index(gfn, slot->base_gfn, PT_PAGE_TABLE_LEVEL);
Xiao Guangrong3d0c27a2016-02-24 17:51:11 +0800158 return !!ACCESS_ONCE(slot->arch.gfn_track[mode][index]);
159}
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +0800160
161void kvm_page_track_init(struct kvm *kvm)
162{
163 struct kvm_page_track_notifier_head *head;
164
165 head = &kvm->arch.track_notifier_head;
166 init_srcu_struct(&head->track_srcu);
167 INIT_HLIST_HEAD(&head->track_notifier_list);
168}
169
170/*
171 * register the notifier so that event interception for the tracked guest
172 * pages can be received.
173 */
174void
175kvm_page_track_register_notifier(struct kvm *kvm,
176 struct kvm_page_track_notifier_node *n)
177{
178 struct kvm_page_track_notifier_head *head;
179
180 head = &kvm->arch.track_notifier_head;
181
182 spin_lock(&kvm->mmu_lock);
183 hlist_add_head_rcu(&n->node, &head->track_notifier_list);
184 spin_unlock(&kvm->mmu_lock);
185}
Jike Song871b7ef2016-10-25 15:50:43 +0800186EXPORT_SYMBOL_GPL(kvm_page_track_register_notifier);
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +0800187
188/*
189 * stop receiving the event interception. It is the opposed operation of
190 * kvm_page_track_register_notifier().
191 */
192void
193kvm_page_track_unregister_notifier(struct kvm *kvm,
194 struct kvm_page_track_notifier_node *n)
195{
196 struct kvm_page_track_notifier_head *head;
197
198 head = &kvm->arch.track_notifier_head;
199
200 spin_lock(&kvm->mmu_lock);
201 hlist_del_rcu(&n->node);
202 spin_unlock(&kvm->mmu_lock);
203 synchronize_srcu(&head->track_srcu);
204}
Jike Song871b7ef2016-10-25 15:50:43 +0800205EXPORT_SYMBOL_GPL(kvm_page_track_unregister_notifier);
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +0800206
207/*
208 * Notify the node that write access is intercepted and write emulation is
209 * finished at this time.
210 *
211 * The node should figure out if the written page is the one that node is
212 * interested in by itself.
213 */
214void kvm_page_track_write(struct kvm_vcpu *vcpu, gpa_t gpa, const u8 *new,
215 int bytes)
216{
217 struct kvm_page_track_notifier_head *head;
218 struct kvm_page_track_notifier_node *n;
219 int idx;
220
221 head = &vcpu->kvm->arch.track_notifier_head;
222
223 if (hlist_empty(&head->track_notifier_list))
224 return;
225
226 idx = srcu_read_lock(&head->track_srcu);
227 hlist_for_each_entry_rcu(n, &head->track_notifier_list, node)
228 if (n->track_write)
Jike Songd1263632016-10-25 15:50:42 +0800229 n->track_write(vcpu, gpa, new, bytes, n);
Xiao Guangrong0eb05bf2016-02-24 17:51:13 +0800230 srcu_read_unlock(&head->track_srcu, idx);
231}
Xiaoguang Chenae7cd872016-10-09 15:41:44 +0800232
233/*
234 * Notify the node that memory slot is being removed or moved so that it can
235 * drop write-protection for the pages in the memory slot.
236 *
237 * The node should figure out it has any write-protected pages in this slot
238 * by itself.
239 */
240void kvm_page_track_flush_slot(struct kvm *kvm, struct kvm_memory_slot *slot)
241{
242 struct kvm_page_track_notifier_head *head;
243 struct kvm_page_track_notifier_node *n;
244 int idx;
245
246 head = &kvm->arch.track_notifier_head;
247
248 if (hlist_empty(&head->track_notifier_list))
249 return;
250
251 idx = srcu_read_lock(&head->track_srcu);
252 hlist_for_each_entry_rcu(n, &head->track_notifier_list, node)
253 if (n->track_flush_slot)
Jike Songd1263632016-10-25 15:50:42 +0800254 n->track_flush_slot(kvm, slot, n);
Xiaoguang Chenae7cd872016-10-09 15:41:44 +0800255 srcu_read_unlock(&head->track_srcu, idx);
256}