[PATCH 2/2] KVM: selftests: Test dirty ring pushes from vCPU preemption

From: Siddharth Mishra

Date: Mon Nov 23 2026 - 10:33:30 EST


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@xxxxxxx>
---
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