1
2
3
4
5
6
7
8
9
10
11
12
13
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}
53
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
79
80
81
82
83
84
85
86
87
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
101
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}
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123void kvm_slot_page_track_remove_page(struct kvm *kvm,
124 struct kvm_memory_slot *slot, gfn_t gfn,
125 enum kvm_page_track_mode mode)
126{
127 if (WARN_ON(!page_track_mode_is_valid(mode)))
128 return;
129
130 update_gfn_track(slot, gfn, mode, -1);
131
132
133
134
135
136 kvm_mmu_gfn_allow_lpage(slot, gfn);
137}
138
139
140
141
142bool kvm_page_track_is_active(struct kvm_vcpu *vcpu, gfn_t gfn,
143 enum kvm_page_track_mode mode)
144{
145 struct kvm_memory_slot *slot;
146 int index;
147
148 if (WARN_ON(!page_track_mode_is_valid(mode)))
149 return false;
150
151 slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
152 if (!slot)
153 return false;
154
155 index = gfn_to_index(gfn, slot->base_gfn, PT_PAGE_TABLE_LEVEL);
156 return !!ACCESS_ONCE(slot->arch.gfn_track[mode][index]);
157}
158
159void kvm_page_track_init(struct kvm *kvm)
160{
161 struct kvm_page_track_notifier_head *head;
162
163 head = &kvm->arch.track_notifier_head;
164 init_srcu_struct(&head->track_srcu);
165 INIT_HLIST_HEAD(&head->track_notifier_list);
166}
167
168
169
170
171
172void
173kvm_page_track_register_notifier(struct kvm *kvm,
174 struct kvm_page_track_notifier_node *n)
175{
176 struct kvm_page_track_notifier_head *head;
177
178 head = &kvm->arch.track_notifier_head;
179
180 spin_lock(&kvm->mmu_lock);
181 hlist_add_head_rcu(&n->node, &head->track_notifier_list);
182 spin_unlock(&kvm->mmu_lock);
183}
184
185
186
187
188
189void
190kvm_page_track_unregister_notifier(struct kvm *kvm,
191 struct kvm_page_track_notifier_node *n)
192{
193 struct kvm_page_track_notifier_head *head;
194
195 head = &kvm->arch.track_notifier_head;
196
197 spin_lock(&kvm->mmu_lock);
198 hlist_del_rcu(&n->node);
199 spin_unlock(&kvm->mmu_lock);
200 synchronize_srcu(&head->track_srcu);
201}
202
203
204
205
206
207
208
209
210void kvm_page_track_write(struct kvm_vcpu *vcpu, gpa_t gpa, const u8 *new,
211 int bytes)
212{
213 struct kvm_page_track_notifier_head *head;
214 struct kvm_page_track_notifier_node *n;
215 int idx;
216
217 head = &vcpu->kvm->arch.track_notifier_head;
218
219 if (hlist_empty(&head->track_notifier_list))
220 return;
221
222 idx = srcu_read_lock(&head->track_srcu);
223 hlist_for_each_entry_rcu(n, &head->track_notifier_list, node)
224 if (n->track_write)
225 n->track_write(vcpu, gpa, new, bytes);
226 srcu_read_unlock(&head->track_srcu, idx);
227}
228