1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * KVM dirty ring implementation
4 *
5 * Copyright 2019 Red Hat, Inc.
6 */
7 #include <linux/kvm_host.h>
8 #include <linux/kvm.h>
9 #include <linux/vmalloc.h>
10 #include <linux/kvm_dirty_ring.h>
11 #include <trace/events/kvm.h>
12 #include "kvm_mm.h"
13
kvm_cpu_dirty_log_size(struct kvm * kvm)14 int __weak kvm_cpu_dirty_log_size(struct kvm *kvm)
15 {
16 return 0;
17 }
18
kvm_dirty_ring_get_rsvd_entries(struct kvm * kvm)19 u32 kvm_dirty_ring_get_rsvd_entries(struct kvm *kvm)
20 {
21 return KVM_DIRTY_RING_RSVD_ENTRIES + kvm_cpu_dirty_log_size(kvm);
22 }
23
kvm_use_dirty_bitmap(struct kvm * kvm)24 bool kvm_use_dirty_bitmap(struct kvm *kvm)
25 {
26 lockdep_assert_held(&kvm->slots_lock);
27
28 return !kvm->dirty_ring_size || kvm->dirty_ring_with_bitmap;
29 }
30
31 #ifndef CONFIG_NEED_KVM_DIRTY_RING_WITH_BITMAP
kvm_arch_allow_write_without_running_vcpu(struct kvm * kvm)32 bool kvm_arch_allow_write_without_running_vcpu(struct kvm *kvm)
33 {
34 return false;
35 }
36 #endif
37
kvm_dirty_ring_used(struct kvm_dirty_ring * ring)38 static u32 kvm_dirty_ring_used(struct kvm_dirty_ring *ring)
39 {
40 return READ_ONCE(ring->dirty_index) - READ_ONCE(ring->reset_index);
41 }
42
kvm_dirty_ring_soft_full(struct kvm_dirty_ring * ring)43 static bool kvm_dirty_ring_soft_full(struct kvm_dirty_ring *ring)
44 {
45 return kvm_dirty_ring_used(ring) >= ring->soft_limit;
46 }
47
kvm_dirty_ring_full(struct kvm_dirty_ring * ring)48 static bool kvm_dirty_ring_full(struct kvm_dirty_ring *ring)
49 {
50 return kvm_dirty_ring_used(ring) >= ring->size;
51 }
52
kvm_reset_dirty_gfn(struct kvm * kvm,u32 slot,u64 offset,u64 mask)53 static void kvm_reset_dirty_gfn(struct kvm *kvm, u32 slot, u64 offset, u64 mask)
54 {
55 struct kvm_memory_slot *memslot;
56 int as_id, id;
57
58 as_id = slot >> 16;
59 id = (u16)slot;
60
61 if (as_id >= kvm_arch_nr_memslot_as_ids(kvm) || id >= KVM_USER_MEM_SLOTS)
62 return;
63
64 memslot = id_to_memslot(__kvm_memslots(kvm, as_id), id);
65
66 if (!memslot || offset >= memslot->npages ||
67 offset + __fls(mask) >= memslot->npages)
68 return;
69
70 KVM_MMU_LOCK(kvm);
71 kvm_arch_mmu_enable_log_dirty_pt_masked(kvm, memslot, offset, mask);
72 KVM_MMU_UNLOCK(kvm);
73 }
74
kvm_dirty_ring_alloc(struct kvm * kvm,struct kvm_dirty_ring * ring,int index,u32 size)75 int kvm_dirty_ring_alloc(struct kvm *kvm, struct kvm_dirty_ring *ring,
76 int index, u32 size)
77 {
78 ring->dirty_gfns = vzalloc(size);
79 if (!ring->dirty_gfns)
80 return -ENOMEM;
81
82 ring->size = size / sizeof(struct kvm_dirty_gfn);
83 ring->soft_limit = ring->size - kvm_dirty_ring_get_rsvd_entries(kvm);
84 ring->dirty_index = 0;
85 ring->reset_index = 0;
86 ring->index = index;
87
88 return 0;
89 }
90
kvm_dirty_gfn_set_invalid(struct kvm_dirty_gfn * gfn)91 static inline void kvm_dirty_gfn_set_invalid(struct kvm_dirty_gfn *gfn)
92 {
93 smp_store_release(&gfn->flags, 0);
94 }
95
kvm_dirty_gfn_set_dirtied(struct kvm_dirty_gfn * gfn)96 static inline void kvm_dirty_gfn_set_dirtied(struct kvm_dirty_gfn *gfn)
97 {
98 gfn->flags = KVM_DIRTY_GFN_F_DIRTY;
99 }
100
kvm_dirty_gfn_harvested(struct kvm_dirty_gfn * gfn)101 static inline bool kvm_dirty_gfn_harvested(struct kvm_dirty_gfn *gfn)
102 {
103 return smp_load_acquire(&gfn->flags) & KVM_DIRTY_GFN_F_RESET;
104 }
105
kvm_dirty_ring_reset(struct kvm * kvm,struct kvm_dirty_ring * ring,int * nr_entries_reset)106 int kvm_dirty_ring_reset(struct kvm *kvm, struct kvm_dirty_ring *ring,
107 int *nr_entries_reset)
108 {
109 /*
110 * To minimize mmu_lock contention, batch resets for harvested entries
111 * whose gfns are in the same slot, and are within N frame numbers of
112 * each other, where N is the number of bits in an unsigned long. For
113 * simplicity, process the current set of entries when the next entry
114 * can't be included in the batch.
115 *
116 * Track the current batch slot, the gfn offset into the slot for the
117 * batch, and the bitmask of gfns that need to be reset (relative to
118 * offset). Note, the offset may be adjusted backwards, e.g. so that
119 * a sequence of gfns X, X-1, ... X-N-1 can be batched.
120 */
121 u32 cur_slot, next_slot;
122 u64 cur_offset, next_offset;
123 unsigned long mask = 0;
124 struct kvm_dirty_gfn *entry;
125
126 /*
127 * Ensure concurrent calls to KVM_RESET_DIRTY_RINGS are serialized,
128 * e.g. so that KVM fully resets all entries processed by a given call
129 * before returning to userspace. Holding slots_lock also protects
130 * the various memslot accesses.
131 */
132 lockdep_assert_held(&kvm->slots_lock);
133
134 while (likely((*nr_entries_reset) < INT_MAX)) {
135 if (signal_pending(current))
136 return -EINTR;
137
138 entry = &ring->dirty_gfns[ring->reset_index & (ring->size - 1)];
139
140 if (!kvm_dirty_gfn_harvested(entry))
141 break;
142
143 next_slot = READ_ONCE(entry->slot);
144 next_offset = READ_ONCE(entry->offset);
145
146 /* Update the flags to reflect that this GFN is reset */
147 kvm_dirty_gfn_set_invalid(entry);
148
149 ring->reset_index++;
150 (*nr_entries_reset)++;
151
152 if (mask) {
153 /*
154 * While the size of each ring is fixed, it's possible
155 * for the ring to be constantly re-dirtied/harvested
156 * while the reset is in-progress (the hard limit exists
157 * only to guard against the count becoming negative).
158 */
159 cond_resched();
160
161 /*
162 * Try to coalesce the reset operations when the guest
163 * is scanning pages in the same slot.
164 */
165 if (next_slot == cur_slot) {
166 s64 delta = next_offset - cur_offset;
167
168 if (delta >= 0 && delta < BITS_PER_LONG) {
169 mask |= 1ull << delta;
170 continue;
171 }
172
173 /* Backwards visit, careful about overflows! */
174 if (delta > -BITS_PER_LONG && delta < 0 &&
175 (mask << -delta >> -delta) == mask) {
176 cur_offset = next_offset;
177 mask = (mask << -delta) | 1;
178 continue;
179 }
180 }
181
182 /*
183 * Reset the slot for all the harvested entries that
184 * have been gathered, but not yet fully processed.
185 */
186 kvm_reset_dirty_gfn(kvm, cur_slot, cur_offset, mask);
187 }
188
189 /*
190 * The current slot was reset or this is the first harvested
191 * entry, (re)initialize the metadata.
192 */
193 cur_slot = next_slot;
194 cur_offset = next_offset;
195 mask = 1;
196 }
197
198 /*
199 * Perform a final reset if there are harvested entries that haven't
200 * been processed, which is guaranteed if at least one harvested was
201 * found. The loop only performs a reset when the "next" entry can't
202 * be batched with the "current" entry(s), and that reset processes the
203 * _current_ entry(s); i.e. the last harvested entry, a.k.a. next, will
204 * always be left pending.
205 */
206 if (mask)
207 kvm_reset_dirty_gfn(kvm, cur_slot, cur_offset, mask);
208
209 /*
210 * The request KVM_REQ_DIRTY_RING_SOFT_FULL will be cleared
211 * by the VCPU thread next time when it enters the guest.
212 */
213
214 trace_kvm_dirty_ring_reset(ring);
215
216 return 0;
217 }
218
kvm_dirty_ring_push(struct kvm_vcpu * vcpu,u32 slot,u64 offset)219 void kvm_dirty_ring_push(struct kvm_vcpu *vcpu, u32 slot, u64 offset)
220 {
221 struct kvm_dirty_ring *ring = &vcpu->dirty_ring;
222 struct kvm_dirty_gfn *entry;
223
224 /* It should never get full */
225 WARN_ON_ONCE(kvm_dirty_ring_full(ring));
226
227 entry = &ring->dirty_gfns[ring->dirty_index & (ring->size - 1)];
228
229 entry->slot = slot;
230 entry->offset = offset;
231 /*
232 * Make sure the data is filled in before we publish this to
233 * the userspace program. There's no paired kernel-side reader.
234 */
235 smp_wmb();
236 kvm_dirty_gfn_set_dirtied(entry);
237 ring->dirty_index++;
238 trace_kvm_dirty_ring_push(ring, slot, offset);
239
240 if (kvm_dirty_ring_soft_full(ring))
241 kvm_make_request(KVM_REQ_DIRTY_RING_SOFT_FULL, vcpu);
242 }
243
kvm_dirty_ring_check_request(struct kvm_vcpu * vcpu)244 bool kvm_dirty_ring_check_request(struct kvm_vcpu *vcpu)
245 {
246 /*
247 * The VCPU isn't runnable when the dirty ring becomes soft full.
248 * The KVM_REQ_DIRTY_RING_SOFT_FULL event is always set to prevent
249 * the VCPU from running until the dirty pages are harvested and
250 * the dirty ring is reset by userspace.
251 */
252 if (kvm_check_request(KVM_REQ_DIRTY_RING_SOFT_FULL, vcpu) &&
253 kvm_dirty_ring_soft_full(&vcpu->dirty_ring)) {
254 kvm_make_request(KVM_REQ_DIRTY_RING_SOFT_FULL, vcpu);
255 vcpu->run->exit_reason = KVM_EXIT_DIRTY_RING_FULL;
256 trace_kvm_dirty_ring_exit(vcpu);
257 return true;
258 }
259
260 return false;
261 }
262
kvm_dirty_ring_get_page(struct kvm_dirty_ring * ring,u32 offset)263 struct page *kvm_dirty_ring_get_page(struct kvm_dirty_ring *ring, u32 offset)
264 {
265 return vmalloc_to_page((void *)ring->dirty_gfns + offset * PAGE_SIZE);
266 }
267
kvm_dirty_ring_free(struct kvm_dirty_ring * ring)268 void kvm_dirty_ring_free(struct kvm_dirty_ring *ring)
269 {
270 vfree(ring->dirty_gfns);
271 ring->dirty_gfns = NULL;
272 }
273