Skip to contentSkip to conversation

KVM: Fix lost dirty ring entries when a vCPU is preempted

PATCH In review Siddharth Mishra 3 participants 5 messages Nov 23

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover KVM: Fix lost dirty ring entries when a vCPU is preempted

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/[email protected]/

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

Signature · 1 line
2.54.0
#1 lore raw

PATCH 1/2 KVM: Disable preemption while pushing to the dirty ring

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")
---
 virt/kvm/dirty_ring.c | 2 ++
 1 file changed, 2 insertions(+)
virt/kvm/dirty_ring.c +2 -0
index 572b854edf74..1ed8b8771c3e 100644
@@ -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));
 
Signature · 1 line
2.54.0
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] KVM: Disable preemption while pushing to the dirty ring

Quoted 22 lines · Siddharth Mishra

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.

Quoted 3 lines
Fixes fb04a1eddb1a ("KVM: X86: Implement ring-based dirty memory tracking")

Feel free to add:

Quoted 20 lines
---
 virt/kvm/dirty_ring.c | 2 ++
 1 file changed, 2 insertions(+)
virt/kvm/dirty_ring.c +2 -1
index 572b854edf74..1ed8b8771c3e 100644
@@ -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

#3 lore raw

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

In reply to 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.

---
 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
tools/testing/selftests/kvm/Makefile.kvm +1 -0
index 17a4256e9081..ab4dff45d306 100644
@@ -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
tools/testing/selftests/kvm/x86/dirty_ring_steal_time_test.c new file +146 -0

146 lines not on this page · raw message

Signature · 1 line
2.54.0
#4 lore raw

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

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…

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 <[email protected]>

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.

Quoted 14 lines
tools/testing/selftests/kvm/x86/dirty_ring_steal_time_test.c new file +8 -0
new file mode 100644
index 0000000000000..6e65055e8c2dc
@@ -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>

tools/testing/selftests/kvm/x86/dirty_ring_steal_time_test.c line 18

Quoted 10 lines
tools/testing/selftests/kvm/x86/dirty_ring_steal_time_test.c new file +10 -0
+
+#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.

[ ... ]

Quoted 9 lines
tools/testing/selftests/kvm/x86/dirty_ring_steal_time_test.c new file +9 -0
+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);
+}

[ ... ]

Quoted 8 lines
tools/testing/selftests/kvm/x86/dirty_ring_steal_time_test.c new file +8 -0
+	while ((opt = getopt(argc, argv, "c:t:S")) != -1) {
+		switch (opt) {
+		case 'c':
+			test_cpu = atoi(optarg);
+			break;
+		case 't':
+			seconds = atoi(optarg);
+			break;

[ ... ]

Quoted 3 lines
tools/testing/selftests/kvm/x86/dirty_ring_steal_time_test.c new file +3 -0
+	pin_self(test_cpu);
+
+	vm = __vm_create(VM_SHAPE_DEFAULT, 1, 0);

[ ... ]

Quoted 3 lines
tools/testing/selftests/kvm/x86/dirty_ring_steal_time_test.c new file +3 -0
+	ring = vcpu_map_dirty_ring(vcpu);
+	TEST_ASSERT(!pthread_create(&thread, NULL, hog, NULL),
+		    "Failed to create hog thread");

[ ... ]

tools/testing/selftests/kvm/x86/dirty_ring_steal_time_test.c line 146

Quoted 5 lines
tools/testing/selftests/kvm/x86/dirty_ring_steal_time_test.c new file +5 -0
+	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.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2