Kernel KVM virtualization development
 help / color / mirror / Atom feed
* [PATCH 0/2] KVM: Fix lost dirty ring entries when a vCPU is preempted
@ 2026-11-23 15:32 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 ` [PATCH 2/2] KVM: selftests: Test dirty ring pushes from vCPU preemption Siddharth Mishra
  0 siblings, 2 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

A push to a vCPU's dirty ring can be interrupted by another push to the
same ring when the vCPU task is preempted, which leaves one entry
unwritten and makes userspace stop harvesting the ring.  Patch 1 fixes
kvm_dirty_ring_push(), patch 2 adds a regression test.

Found while fuzzing. An AI tool helped with the analysis and the patches.

The series is independent of James' series that changes the same
function, and applies with or without it:
https://lore.kernel.org/all/20261117212913.3634487-1-jthoughton@google.com/

Tested on Intel with PML, in a nested VM on mainline at
commit 4843f6873f7c ("Merge tag 'sysctl-7.04-fixes-rc4' of
git://git.kernel.org/pub/scm/linux/kernel/git/sysctl/sysctl") and on the
v7.2.8 host:
- Patch 2 fails within seconds without patch 1 (11 of 12 runs), and
  passes with it (0 failures in 15 runs).
- All x86 and common KVM selftests, with PREEMPT, DEBUG_PREEMPT,
  DEBUG_ATOMIC_SLEEP, PROVE_LOCKING, PROVE_RCU, DEBUG_PAGEALLOC,
  SLUB_DEBUG, DEBUG_OBJECTS_RCU_HEAD, KVM_SMM and KVM_XEN: the same
  results with and without the series.  hardware_disable_test fails and
  xen_shinfo_test triggers a lockdep "Invalid wait context" report in
  kvm_xen_set_evtchn_fast() on both.
- The dirty logging tests with kvm_intel.pml=0: the same results with and
  without the series.
- Builds with W=1: KVM_INTEL=m KVM_AMD=m KVM_WERROR=y, SMP=n,
  allnoconfig, gcc allmodconfig and allyesconfig, clang allmodconfig, and
  clang for arm64, riscv and loongarch.
- KVM-unit-tests (a2891d9) for x86 in the same nested VM: the same
  results with and without the series.  vmx_apicv_test times out and
  vmx reports "Guest didn't run to completion" and a too high RDTSC to
  VM-exit delta on both.
- sparse (84ec065) with C=2 on arch/x86/kvm/: no reports in
  virt/kvm/dirty_ring.c.

Not tested: AMD, the Xen runstate path.

Siddharth Mishra (2):
  KVM: Disable preemption while pushing to the dirty ring
  KVM: selftests: Test dirty ring pushes from vCPU preemption

 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 .../kvm/x86/dirty_ring_steal_time_test.c      | 146 ++++++++++++++++++
 virt/kvm/dirty_ring.c                         |   2 +
 3 files changed, 149 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/x86/dirty_ring_steal_time_test.c


base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb
-- 
2.54.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [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 15:32 ` [PATCH 2/2] KVM: selftests: Test dirty ring pushes from vCPU preemption 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, 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 related	[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
  2026-11-23 15:46   ` sashiko-bot
  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

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 related	[flat|nested] 4+ messages in thread

* Re: [PATCH 2/2] KVM: selftests: Test dirty ring pushes from vCPU preemption
  2026-11-23 15:32 ` [PATCH 2/2] KVM: selftests: Test dirty ring pushes from vCPU preemption Siddharth Mishra
@ 2026-11-23 15:46   ` sashiko-bot
  0 siblings, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-11-23 15:46 UTC (permalink / raw)
  To: Siddharth Mishra; +Cc: kvm

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Low] KVM: selftests: duplicate CPU pinning, pthread, and atoi helpers
- [Low] KVM: selftests: 60-second default runtime in
  dirty_ring_steal_time_test
--

commit ed3801d4ab73947519efff505ae4499cdb0daaef
Author: Siddharth Mishra <smish149@asu.edu>

KVM: selftests: Test dirty ring pushes from vCPU preemption

This commit adds an x86 KVM selftest that exercises dirty ring pushes when
a vCPU thread flushing its PML buffer is preempted and updates its
steal-time structure in a dirty-logged memslot.

> 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 0000000000000..6e65055e8c2dc
> --- /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

[Severity: Low]
Is a 60-second default runtime too long for a test in TEST_GEN_PROGS_x86?

Because the test is added to TEST_GEN_PROGS_x86 in Makefile.kvm, default
kselftest runs will spin two threads on one CPU for a full minute, using
half of the 120-second timeout in tools/testing/selftests/kvm/settings.
Other race tests in TEST_GEN_PROGS_x86 (such as x86/xapic_ipi_test.c,
x86/recalc_apic_map_test.c, and x86/xen_shinfo_test.c) default to 2-5
seconds and rely on a command-line flag for longer stress runs.

[ ... ]

> +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);
> +}

[ ... ]

> +	while ((opt = getopt(argc, argv, "c:t:S")) != -1) {
> +		switch (opt) {
> +		case 'c':
> +			test_cpu = atoi(optarg);
> +			break;
> +		case 't':
> +			seconds = atoi(optarg);
> +			break;

[ ... ]

> +	pin_self(test_cpu);
> +
> +	vm = __vm_create(VM_SHAPE_DEFAULT, 1, 0);

[ ... ]

> +	ring = vcpu_map_dirty_ring(vcpu);
> +	TEST_ASSERT(!pthread_create(&thread, NULL, hog, NULL),
> +		    "Failed to create hog thread");

[ ... ]

> +	WRITE_ONCE(stop_hog, true);
> +	pthread_join(thread, NULL);
> +	kvm_vm_free(vm);
> +	return 0;
> +}

[Severity: Low]
This isn't a bug, but several helpers here duplicate existing KVM selftest
utilities already pulled in via kvm_util.h:

- pin_self() duplicates pin_self_to_cpu() in kvm_util.h.
- atoi(optarg) for -c and -t skips input validation that
  atoi_non_negative() and atoi_positive() in test_util.h provide (for
  example, rejecting a negative CPU index before passing it to CPU_SET()).
- __vm_create(VM_SHAPE_DEFAULT, 1, 0) can use vm_create(1), and the raw
  pthread_create() and pthread_join() calls can use kvm_pthread_create()
  and kvm_pthread_join() from kvm_syscalls.h.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261123153238.1036972-1-smish149@asu.edu?part=2

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-23 15:46 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [PATCH 2/2] KVM: selftests: Test dirty ring pushes from vCPU preemption Siddharth Mishra
2026-11-23 15:46   ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox