[PATCH 1/2] KVM: Disable preemption while pushing to the dirty ring
From: Siddharth Mishra
Date: Mon Nov 23 2026 - 10:33:28 EST
Disable preemption in kvm_dirty_ring_push() so that another push to the
same dirty ring cannot run in the middle of it.
kvm_dirty_ring_push() selects an entry with dirty_index, fills the entry,
and only then increments dirty_index, all with preemption enabled, e.g.
when vmx_flush_pml_buffer() pushes the logged GPAs on VM-Exit. If the
vCPU task is preempted in that window, kvm_sched_out() calls
kvm_arch_vcpu_put() while kvm_running_vcpu still points at the vCPU, and
kvm_steal_time_set_preempted() or kvm_xen_runstate_set_preempted() marks
a page dirty, which pushes to the same ring. That push fills the
selected entry and increments dirty_index, then the interrupted push
overwrites the entry and increments dirty_index again. The entry after
it is never written, its flags stay zero, and the entries after that one
are dirty.
Userspace harvests the ring in order and stops at the first entry that
is not dirty, so it never harvests any later entry, and the GFN of the
overwritten push is lost. A VMM that tracks dirty memory with the dirty
ring, e.g. for live migration or to restore a snapshot, then misses every
page that the vCPU dirties afterwards.
Fixes: fb04a1eddb1a ("KVM: X86: Implement ring-based dirty memory tracking")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Siddharth Mishra <smish149@xxxxxxx>
---
virt/kvm/dirty_ring.c | 2 ++
1 file changed, 2 insertions(+)
diff --git a/virt/kvm/dirty_ring.c b/virt/kvm/dirty_ring.c
index 572b854edf74..1ed8b8771c3e 100644
--- a/virt/kvm/dirty_ring.c
+++ b/virt/kvm/dirty_ring.c
@@ -221,6 +221,8 @@ void kvm_dirty_ring_push(struct kvm_vcpu *vcpu, u32 slot, u64 offset)
struct kvm_dirty_ring *ring = &vcpu->dirty_ring;
struct kvm_dirty_gfn *entry;
+ guard(preempt)();
+
/* It should never get full */
WARN_ON_ONCE(kvm_dirty_ring_full(ring));
--
2.54.0