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