mirror of https://lore.kernel.org/lkml/
 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 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

* Re: [PATCH 1/2] KVM: Disable preemption while pushing to the dirty ring
  2026-11-23 15:32 ` [PATCH 1/2] KVM: Disable preemption while pushing to the dirty ring Siddharth Mishra
@ 2026-11-23 18:55   ` James Houghton
  0 siblings, 0 replies; 4+ messages in thread
From: James Houghton @ 2026-11-23 18:55 UTC (permalink / raw)
  To: Siddharth Mishra
  Cc: Paolo Bonzini, Sean Christopherson, Shuah Khan, Peter Xu,
	David Woodhouse, kvm, linux-kselftest, linux-kernel, stable

On Mon, Nov 23, 2026 at 10:33 AM Siddharth Mishra <smish149@asu.edu> wrote:
>
> 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>

Feel free to add:

Reviewed-by: James Houghton <jthoughton@google.com>

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

end of thread, other threads:[~2026-11-23 18:55 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 18:55   ` James Houghton
2026-11-23 15:32 ` [PATCH 2/2] KVM: selftests: Test dirty ring pushes from vCPU preemption Siddharth Mishra

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

all inboxes | Powered by JetHome®