* [PATCH 1/2] KVM: Disable preemption while pushing to the dirty ring
2026-11-23 15:32 [PATCH 0/2] KVM: Fix lost dirty ring entries when a vCPU is preempted Siddharth Mishra
@ 2026-11-23 15:32 ` Siddharth Mishra
2026-11-23 18:55 ` James Houghton
2026-11-23 15:32 ` [PATCH 2/2] KVM: selftests: Test dirty ring pushes from vCPU preemption Siddharth Mishra
1 sibling, 1 reply; 4+ messages in thread
From: Siddharth Mishra @ 2026-11-23 15:32 UTC (permalink / raw)
To: Paolo Bonzini, Sean Christopherson
Cc: Shuah Khan, Peter Xu, James Houghton, David Woodhouse, kvm,
linux-kselftest, linux-kernel, Siddharth Mishra, stable
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@vger.kernel.org
Signed-off-by: Siddharth Mishra <smish149@asu.edu>
---
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
^ permalink raw reply [flat|nested] 4+ messages in thread* [PATCH 2/2] KVM: selftests: Test dirty ring pushes from vCPU preemption
2026-11-23 15:32 [PATCH 0/2] KVM: Fix lost dirty ring entries when a vCPU is preempted Siddharth Mishra
2026-11-23 15:32 ` [PATCH 1/2] KVM: Disable preemption while pushing to the dirty ring Siddharth Mishra
@ 2026-11-23 15:32 ` Siddharth Mishra
1 sibling, 0 replies; 4+ messages in thread
From: Siddharth Mishra @ 2026-11-23 15:32 UTC (permalink / raw)
To: Paolo Bonzini, Sean Christopherson
Cc: Shuah Khan, Peter Xu, James Houghton, David Woodhouse, kvm,
linux-kselftest, linux-kernel, Siddharth Mishra
Add a test that dirties pages in a dirty-logged memslot that also holds
the vCPU's steal-time structure, while a busy thread shares the vCPU
thread's CPU so that the vCPU task is preempted often. After every
KVM_RUN, harvest the dirty ring and assert that no entry is left dirty,
which fails if kvm_steal_time_set_preempted() pushes to the ring in the
middle of kvm_dirty_ring_push().
Require PML, as flushing the PML buffer on VM-Exit is where KVM pushes
to the dirty ring with preemption enabled. Pass -S to run without steal
time, and -t to change the run time from the default of 60 seconds.
Signed-off-by: Siddharth Mishra <smish149@asu.edu>
---
tools/testing/selftests/kvm/Makefile.kvm | 1 +
.../kvm/x86/dirty_ring_steal_time_test.c | 146 ++++++++++++++++++
2 files changed, 147 insertions(+)
create mode 100644 tools/testing/selftests/kvm/x86/dirty_ring_steal_time_test.c
diff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selftests/kvm/Makefile.kvm
index 17a4256e9081..ab4dff45d306 100644
--- a/tools/testing/selftests/kvm/Makefile.kvm
+++ b/tools/testing/selftests/kvm/Makefile.kvm
@@ -78,6 +78,7 @@ TEST_GEN_PROGS_x86 = $(TEST_GEN_PROGS_COMMON)
TEST_GEN_PROGS_x86 += x86/cpuid_test
TEST_GEN_PROGS_x86 += x86/cr4_cpuid_sync_test
TEST_GEN_PROGS_x86 += x86/dirty_log_page_splitting_test
+TEST_GEN_PROGS_x86 += x86/dirty_ring_steal_time_test
TEST_GEN_PROGS_x86 += x86/feature_msrs_test
TEST_GEN_PROGS_x86 += x86/evmcs_smm_controls_test
TEST_GEN_PROGS_x86 += x86/exit_on_emulation_failure_test
diff --git a/tools/testing/selftests/kvm/x86/dirty_ring_steal_time_test.c b/tools/testing/selftests/kvm/x86/dirty_ring_steal_time_test.c
new file mode 100644
index 000000000000..6e65055e8c2d
--- /dev/null
+++ b/tools/testing/selftests/kvm/x86/dirty_ring_steal_time_test.c
@@ -0,0 +1,146 @@
+// SPDX-License-Identifier: GPL-2.0-only
+#define _GNU_SOURCE
+#include <pthread.h>
+#include <sched.h>
+#include <stdlib.h>
+#include <time.h>
+#include <unistd.h>
+#include <asm/barrier.h>
+
+#include "kvm_util.h"
+#include "processor.h"
+#include "ucall_common.h"
+
+#define TEST_SLOT 1
+#define TEST_GPA 0x10000000ul
+#define TEST_PAGES 16384
+#define RING_ENTRIES 65536u
+#define DEFAULT_SECONDS 60
+
+static bool stop_hog;
+static int test_cpu;
+
+static void guest_code(u64 gva)
+{
+ u64 gen = 0, i;
+
+ for (;;) {
+ gen++;
+ for (i = 1; i < TEST_PAGES; i++)
+ WRITE_ONCE(*(u64 *)(gva + i * PAGE_SIZE), gen);
+ GUEST_SYNC(gen);
+ }
+}
+
+static void pin_self(int cpu)
+{
+ cpu_set_t set;
+
+ CPU_ZERO(&set);
+ CPU_SET(cpu, &set);
+ TEST_ASSERT(!pthread_setaffinity_np(pthread_self(), sizeof(set), &set),
+ "Failed to pin to CPU %d", cpu);
+}
+
+static void *hog(void *arg)
+{
+ pin_self(test_cpu);
+ while (!READ_ONCE(stop_hog))
+ ;
+ return NULL;
+}
+
+static u32 harvest(struct kvm_dirty_gfn *ring, u32 *fetch)
+{
+ struct kvm_dirty_gfn *e;
+ u32 n = 0, i;
+
+ for (;;) {
+ e = &ring[*fetch % RING_ENTRIES];
+ if (smp_load_acquire(&e->flags) != KVM_DIRTY_GFN_F_DIRTY)
+ break;
+ smp_store_release(&e->flags, KVM_DIRTY_GFN_F_RESET);
+ (*fetch)++;
+ n++;
+ }
+
+ for (i = 0; i < RING_ENTRIES; i++) {
+ TEST_ASSERT(READ_ONCE(ring[i].flags) != KVM_DIRTY_GFN_F_DIRTY,
+ "Entry %u is dirty, but entry %u before it was never written",
+ i, *fetch % RING_ENTRIES);
+ }
+ return n;
+}
+
+int main(int argc, char *argv[])
+{
+ int seconds = DEFAULT_SECONDS, opt;
+ struct kvm_dirty_gfn *ring;
+ bool steal_time = true;
+ struct kvm_vcpu *vcpu;
+ struct kvm_vm *vm;
+ u32 fetch = 0, n;
+ pthread_t thread;
+ struct ucall uc;
+ time_t end;
+
+ test_cpu = sched_getcpu();
+ while ((opt = getopt(argc, argv, "c:t:S")) != -1) {
+ switch (opt) {
+ case 'c':
+ test_cpu = atoi(optarg);
+ break;
+ case 't':
+ seconds = atoi(optarg);
+ break;
+ case 'S':
+ steal_time = false;
+ break;
+ default:
+ TEST_FAIL("Usage: %s [-c cpu] [-t seconds] [-S]", argv[0]);
+ }
+ }
+
+ TEST_REQUIRE(kvm_has_cap(KVM_CAP_DIRTY_LOG_RING) ||
+ kvm_has_cap(KVM_CAP_DIRTY_LOG_RING_ACQ_REL));
+ TEST_REQUIRE(host_cpu_is_intel && get_kvm_intel_param_bool("pml"));
+ if (steal_time)
+ TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_KVM_STEAL_TIME));
+
+ pin_self(test_cpu);
+
+ vm = __vm_create(VM_SHAPE_DEFAULT, 1, 0);
+ vm_enable_dirty_ring(vm, RING_ENTRIES * sizeof(struct kvm_dirty_gfn));
+ vcpu = vm_vcpu_add(vm, 0, guest_code);
+ kvm_arch_vm_finalize_vcpus(vm);
+
+ vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, TEST_GPA,
+ TEST_SLOT, TEST_PAGES,
+ KVM_MEM_LOG_DIRTY_PAGES);
+ virt_map(vm, TEST_GPA, TEST_GPA, TEST_PAGES);
+ vcpu_args_set(vcpu, 1, TEST_GPA);
+ if (steal_time)
+ vcpu_set_msr(vcpu, MSR_KVM_STEAL_TIME,
+ TEST_GPA | KVM_MSR_ENABLED);
+
+ ring = vcpu_map_dirty_ring(vcpu);
+ TEST_ASSERT(!pthread_create(&thread, NULL, hog, NULL),
+ "Failed to create hog thread");
+
+ end = time(NULL) + seconds;
+ while (time(NULL) < end) {
+ vcpu_run(vcpu);
+ if (vcpu->run->exit_reason != KVM_EXIT_DIRTY_RING_FULL) {
+ TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);
+ if (get_ucall(vcpu, &uc) == UCALL_ABORT)
+ REPORT_GUEST_ASSERT(uc);
+ }
+ n = harvest(ring, &fetch);
+ TEST_ASSERT_EQ(kvm_vm_reset_dirty_ring(vm), n);
+ }
+
+ WRITE_ONCE(stop_hog, true);
+ pthread_join(thread, NULL);
+ kvm_vm_free(vm);
+ return 0;
+}
--
2.54.0
^ permalink raw reply [flat|nested] 4+ messages in thread