1 // SPDX-License-Identifier: GPL-2.0-only
3 * KVM dirty ring implementation
5 * Copyright 2019 Red Hat, Inc.
7 #include <linux/kvm_host.h>
9 #include <linux/vmalloc.h>
10 #include <linux/kvm_dirty_ring.h>
11 #include <trace/events/kvm.h>
14 int __weak
kvm_cpu_dirty_log_size(void)
19 u32
kvm_dirty_ring_get_rsvd_entries(void)
21 return KVM_DIRTY_RING_RSVD_ENTRIES
+ kvm_cpu_dirty_log_size();
24 bool kvm_use_dirty_bitmap(struct kvm
*kvm
)
26 lockdep_assert_held(&kvm
->slots_lock
);
28 return !kvm
->dirty_ring_size
|| kvm
->dirty_ring_with_bitmap
;
31 #ifndef CONFIG_NEED_KVM_DIRTY_RING_WITH_BITMAP
32 bool kvm_arch_allow_write_without_running_vcpu(struct kvm
*kvm
)
38 static u32
kvm_dirty_ring_used(struct kvm_dirty_ring
*ring
)
40 return READ_ONCE(ring
->dirty_index
) - READ_ONCE(ring
->reset_index
);
43 static bool kvm_dirty_ring_soft_full(struct kvm_dirty_ring
*ring
)
45 return kvm_dirty_ring_used(ring
) >= ring
->soft_limit
;
48 static bool kvm_dirty_ring_full(struct kvm_dirty_ring
*ring
)
50 return kvm_dirty_ring_used(ring
) >= ring
->size
;
53 static void kvm_reset_dirty_gfn(struct kvm
*kvm
, u32 slot
, u64 offset
, u64 mask
)
55 struct kvm_memory_slot
*memslot
;
64 if (as_id
>= kvm_arch_nr_memslot_as_ids(kvm
) || id
>= KVM_USER_MEM_SLOTS
)
67 memslot
= id_to_memslot(__kvm_memslots(kvm
, as_id
), id
);
69 if (!memslot
|| (offset
+ __fls(mask
)) >= memslot
->npages
)
73 kvm_arch_mmu_enable_log_dirty_pt_masked(kvm
, memslot
, offset
, mask
);
77 int kvm_dirty_ring_alloc(struct kvm_dirty_ring
*ring
, int index
, u32 size
)
79 ring
->dirty_gfns
= vzalloc(size
);
80 if (!ring
->dirty_gfns
)
83 ring
->size
= size
/ sizeof(struct kvm_dirty_gfn
);
84 ring
->soft_limit
= ring
->size
- kvm_dirty_ring_get_rsvd_entries();
85 ring
->dirty_index
= 0;
86 ring
->reset_index
= 0;
92 static inline void kvm_dirty_gfn_set_invalid(struct kvm_dirty_gfn
*gfn
)
94 smp_store_release(&gfn
->flags
, 0);
97 static inline void kvm_dirty_gfn_set_dirtied(struct kvm_dirty_gfn
*gfn
)
99 gfn
->flags
= KVM_DIRTY_GFN_F_DIRTY
;
102 static inline bool kvm_dirty_gfn_harvested(struct kvm_dirty_gfn
*gfn
)
104 return smp_load_acquire(&gfn
->flags
) & KVM_DIRTY_GFN_F_RESET
;
107 int kvm_dirty_ring_reset(struct kvm
*kvm
, struct kvm_dirty_ring
*ring
)
109 u32 cur_slot
, next_slot
;
110 u64 cur_offset
, next_offset
;
113 struct kvm_dirty_gfn
*entry
;
114 bool first_round
= true;
116 /* This is only needed to make compilers happy */
117 cur_slot
= cur_offset
= mask
= 0;
120 entry
= &ring
->dirty_gfns
[ring
->reset_index
& (ring
->size
- 1)];
122 if (!kvm_dirty_gfn_harvested(entry
))
125 next_slot
= READ_ONCE(entry
->slot
);
126 next_offset
= READ_ONCE(entry
->offset
);
128 /* Update the flags to reflect that this GFN is reset */
129 kvm_dirty_gfn_set_invalid(entry
);
134 * Try to coalesce the reset operations when the guest is
135 * scanning pages in the same slot.
137 if (!first_round
&& next_slot
== cur_slot
) {
138 s64 delta
= next_offset
- cur_offset
;
140 if (delta
>= 0 && delta
< BITS_PER_LONG
) {
141 mask
|= 1ull << delta
;
145 /* Backwards visit, careful about overflows! */
146 if (delta
> -BITS_PER_LONG
&& delta
< 0 &&
147 (mask
<< -delta
>> -delta
) == mask
) {
148 cur_offset
= next_offset
;
149 mask
= (mask
<< -delta
) | 1;
153 kvm_reset_dirty_gfn(kvm
, cur_slot
, cur_offset
, mask
);
154 cur_slot
= next_slot
;
155 cur_offset
= next_offset
;
160 kvm_reset_dirty_gfn(kvm
, cur_slot
, cur_offset
, mask
);
163 * The request KVM_REQ_DIRTY_RING_SOFT_FULL will be cleared
164 * by the VCPU thread next time when it enters the guest.
167 trace_kvm_dirty_ring_reset(ring
);
172 void kvm_dirty_ring_push(struct kvm_vcpu
*vcpu
, u32 slot
, u64 offset
)
174 struct kvm_dirty_ring
*ring
= &vcpu
->dirty_ring
;
175 struct kvm_dirty_gfn
*entry
;
177 /* It should never get full */
178 WARN_ON_ONCE(kvm_dirty_ring_full(ring
));
180 entry
= &ring
->dirty_gfns
[ring
->dirty_index
& (ring
->size
- 1)];
183 entry
->offset
= offset
;
185 * Make sure the data is filled in before we publish this to
186 * the userspace program. There's no paired kernel-side reader.
189 kvm_dirty_gfn_set_dirtied(entry
);
191 trace_kvm_dirty_ring_push(ring
, slot
, offset
);
193 if (kvm_dirty_ring_soft_full(ring
))
194 kvm_make_request(KVM_REQ_DIRTY_RING_SOFT_FULL
, vcpu
);
197 bool kvm_dirty_ring_check_request(struct kvm_vcpu
*vcpu
)
200 * The VCPU isn't runnable when the dirty ring becomes soft full.
201 * The KVM_REQ_DIRTY_RING_SOFT_FULL event is always set to prevent
202 * the VCPU from running until the dirty pages are harvested and
203 * the dirty ring is reset by userspace.
205 if (kvm_check_request(KVM_REQ_DIRTY_RING_SOFT_FULL
, vcpu
) &&
206 kvm_dirty_ring_soft_full(&vcpu
->dirty_ring
)) {
207 kvm_make_request(KVM_REQ_DIRTY_RING_SOFT_FULL
, vcpu
);
208 vcpu
->run
->exit_reason
= KVM_EXIT_DIRTY_RING_FULL
;
209 trace_kvm_dirty_ring_exit(vcpu
);
216 struct page
*kvm_dirty_ring_get_page(struct kvm_dirty_ring
*ring
, u32 offset
)
218 return vmalloc_to_page((void *)ring
->dirty_gfns
+ offset
* PAGE_SIZE
);
221 void kvm_dirty_ring_free(struct kvm_dirty_ring
*ring
)
223 vfree(ring
->dirty_gfns
);
224 ring
->dirty_gfns
= NULL
;