Skip to contentSkip to conversation

KVM: Bound KVM_PRE_FAULT_MEMORY by the vCPU's dirty ring

PATCH Superseded Fuad Tabba 4 participants 19 messages Nov 10 – Nov 16

Subsystems (5)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Fuad Tabba

author reviewer
#0 lore raw

cover KVM: Bound KVM_PRE_FAULT_MEMORY by the vCPU's dirty ring

Hi folks,

KVM_PRE_FAULT_MEMORY never checks the vCPU's dirty ring, so one call over enough populated, dirty-logged pages overflows it. The first patch stops the loop at the soft limit, as KVM_RUN does. A stopped call returns its partial progress, or, if it stopped before any, -ENOSPC rather than the retryable -EAGAIN, since no retry can succeed until the ring is harvested. The second patch adds the case to pre_fault_memory_test.

On x86 a read fault normally maps a host-writable page writable and logs it at once, so pre-faulting overflows the ring. Sean's auto-prefetch series [1] keeps that behaviour for a pre-fault, so this series bounds the loop rather than have x86 write-protect a pre-faulted page. On arm64, which gained the ioctl in 7.4, pre-faulting RAM logged with the ring alone overflows it too, until Wei-Lin's fix [2] is in mainline.

Pre-faulting isn't the only ioctl that can push past the ring's reserve. On x86, KVM_SET_MSRS does too when it writes the wall clock or the Hyper-V hypercall page: up to a few hundred entries in a single call, with no bound across calls. KVM_SET_MSRS can't take the same stop, since a short count from it means the MSR failed and QEMU asserts on it.

What those paths should do when the ring fills is an open question for the dirty ring API, which I don't think belongs in this series. Pre-faulting a large logged range is a supported use of the ioctl, so the loop should handle a full ring rather than lose entries.

Tested on x86 and arm64 under QEMU: without the first patch, the new case hits the WARN on both.

Based on Linux 7.4-rc2 (7475ea8b58616).

Cheers,
/fuad

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/

Fuad Tabba (2):
KVM: Stop KVM_PRE_FAULT_MEMORY when the vCPU's dirty ring is full
KVM: selftests: Test KVM_PRE_FAULT_MEMORY against a full dirty ring

 Documentation/virt/kvm/api.rst                |  9 +-
 include/linux/kvm_dirty_ring.h                |  6 ++
 .../selftests/kvm/pre_fault_memory_test.c     | 85 +++++++++++++++++++
 virt/kvm/dirty_ring.c                         |  2 +-
 virt/kvm/kvm_main.c                           |  6 ++
 5 files changed, 105 insertions(+), 3 deletions(-)

base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb

Signature · 1 line
2.47.3
author reviewer
#1 lore raw

PATCH 1/2 KVM: Stop KVM_PRE_FAULT_MEMORY when the vCPU's dirty ring is full

Pre-faulting can map a dirty-logged page writable, which pushes it to the vCPU's dirty ring. KVM_RUN exits with KVM_EXIT_DIRTY_RING_FULL before the ring overflows, but the pre-fault loop never checks it, so one call over more such pages than the ring has free trips the WARN in kvm_dirty_ring_push() and overwrites entries userspace hasn't harvested. On x86:

  WARNING: arch/x86/kvm/../../../virt/kvm/dirty_ring.c:225 at kvm_dirty_ring_push+0x7a/0xd0
  Call Trace:
   make_spte+0x2cc/0x450
   kvm_tdp_mmu_map+0x5e1/0x7f0
   kvm_tdp_page_fault+0x12c/0x1b0
   kvm_mmu_do_page_fault+0x181/0x250
   kvm_arch_vcpu_pre_fault_memory+0x136/0x1d0
   kvm_vcpu_pre_fault_memory+0xcf/0x180
   kvm_vcpu_ioctl+0x455/0x770

Stop the loop once the ring is soft full, returning the pages already processed, or -ENOSPC when there are none, since a retry can't succeed until userspace harvests the ring. kvm_dirty_ring_check_request() fills in a KVM_RUN exit, so test the ring's state directly. The stub for !HAVE_KVM_DIRTY_RING is for s390, which has the ioctl but no ring.

Fixes bc1a5cd002116 ("KVM: Add KVM_PRE_FAULT_MEMORY vcpu ioctl to pre-populate guest memory")
---
 Documentation/virt/kvm/api.rst | 9 +++++++--
 include/linux/kvm_dirty_ring.h | 6 ++++++
 virt/kvm/dirty_ring.c          | 2 +-
 virt/kvm/kvm_main.c            | 6 ++++++
 4 files changed, 20 insertions(+), 3 deletions(-)
Documentation/virt/kvm/api.rst +7 -2
index e95c9cda085d9..89eb6be9bc6e3 100644
@@ -6544,6 +6544,7 @@ Errors:
   ENOENT     The specified `gpa` is outside defined memslots.
   ENOEXEC    The vCPU has not been initialised (arm64).
   EINTR      An unmasked signal is pending and no page was processed.
+  ENOSPC     The vCPU's dirty ring is full and no page was processed.
   EFAULT     The parameter address was invalid.
   EHWPOISON  A poisoned host page was encountered.
   EOPNOTSUPP Mapping memory for a GPA is unsupported by the
@@ -6588,7 +6589,9 @@ case, the ioctl can be called in parallel.
 
 When the ioctl returns, the input values are updated to point to the
 remaining range.  If `size` > 0 on return, the caller can just issue
-the ioctl again with the same `struct kvm_map_memory` argument.
+the ioctl again with the same `struct kvm_pre_fault_memory` argument.
+If that fails with ENOSPC, the vCPU's dirty ring has to be harvested
+and reset first.
 
 On x86, shadow page tables cannot support this ioctl because they
 are indexed by virtual address or nested guest physical address.
@@ -8957,7 +8960,9 @@ Therefore, the ioctl must be called *before* reading the content of
 the dirty pages.
 
 The dirty ring can get full.  When it happens, the KVM_RUN of the
-vcpu will return with exit reason KVM_EXIT_DIRTY_RING_FULL.
+vcpu will return with exit reason KVM_EXIT_DIRTY_RING_FULL, and
+KVM_PRE_FAULT_MEMORY on the vcpu will process no more pages until the
+ring has been harvested and reset.
 
 The dirty ring interface has a major difference comparing to the
 KVM_GET_DIRTY_LOG interface in that, when reading the dirty ring from
include/linux/kvm_dirty_ring.h +6 -0
index eb10d87adf7d5..88b4f2a009274 100644
@@ -60,6 +60,11 @@ static inline void kvm_dirty_ring_push(struct kvm_vcpu *vcpu,
 {
 }
 
+static inline bool kvm_dirty_ring_soft_full(struct kvm_dirty_ring *ring)
+{
+	return false;
+}
+
 static inline struct page *kvm_dirty_ring_get_page(struct kvm_dirty_ring *ring,
 						   u32 offset)
 {
@@ -81,6 +86,7 @@ int kvm_dirty_ring_alloc(struct kvm *kvm, struct kvm_dirty_ring *ring,
 int kvm_dirty_ring_reset(struct kvm *kvm, struct kvm_dirty_ring *ring,
 			 int *nr_entries_reset);
 void kvm_dirty_ring_push(struct kvm_vcpu *vcpu, u32 slot, u64 offset);
+bool kvm_dirty_ring_soft_full(struct kvm_dirty_ring *ring);
 
 bool kvm_dirty_ring_check_request(struct kvm_vcpu *vcpu);
 
virt/kvm/dirty_ring.c +1 -1
index 572b854edf740..f73f92102cb7a 100644
@@ -40,7 +40,7 @@ static u32 kvm_dirty_ring_used(struct kvm_dirty_ring *ring)
 	return READ_ONCE(ring->dirty_index) - READ_ONCE(ring->reset_index);
 }
 
-static bool kvm_dirty_ring_soft_full(struct kvm_dirty_ring *ring)
+bool kvm_dirty_ring_soft_full(struct kvm_dirty_ring *ring)
 {
 	return kvm_dirty_ring_used(ring) >= ring->soft_limit;
 }
virt/kvm/kvm_main.c +6 -0
index cf4cb586f1407..da1b76ceb65e3 100644
@@ -4461,6 +4461,12 @@ static int kvm_vcpu_pre_fault_memory(struct kvm_vcpu *vcpu,
 			break;
 		}
 
+		/* Like KVM_RUN: only the iteration crossing the soft limit uses the reserve. */
+		if (vcpu->kvm->dirty_ring_size && kvm_dirty_ring_soft_full(&vcpu->dirty_ring)) {
+			r = -ENOSPC;
+			break;
+		}
+
 		r = kvm_arch_vcpu_pre_fault_memory(vcpu, range);
 		if (WARN_ON_ONCE(r == 0 || r == -EIO))
 			break;
Signature · 1 line
2.47.3
author reviewer
#9 lore raw

PATCH 2/2 KVM: selftests: Test KVM_PRE_FAULT_MEMORY against a full dirty ring

In reply to Fuad Tabba

Pre-fault more populated, dirty-logged pages than the vCPU's dirty ring holds, and check that each call stops before the ring overflows, that a retry returns ENOSPC without progress, and that the harvested entries are the pages in order. A read pre-fault can map a page read-only, or access-tracked on x86 without EPT A/D bits, so skip the case if pre-faulting dirties nothing.

---
 .../selftests/kvm/pre_fault_memory_test.c     | 85 +++++++++++++++++++
 1 file changed, 85 insertions(+)
tools/testing/selftests/kvm/pre_fault_memory_test.c +85 -0
index 3082fe09b95fa..18fc1d69acbf0 100644
@@ -5,6 +5,7 @@
  * Author:
  * Isaku Yamahata <isaku.yamahata at gmail.com>
  */
+#include <asm/barrier.h>
 #include <linux/sizes.h>
 
 #include <test_util.h>
@@ -17,6 +18,8 @@
 /* Arbitrarily chosen values */
 #define TEST_BASE_SIZE		SZ_2M
 #define TEST_SLOT		10
+/* Large enough for the entries every architecture reserves. */
+#define TEST_RING_SIZE		SZ_64K
 
 /* Storage of test info to share with guest code */
 struct test_config {
@@ -281,6 +284,87 @@ static void test_pre_fault_memory(unsigned long vm_type, enum vm_mem_backing_src
 	for_each_guest_mode(__test_pre_fault_memory, &p);
 }
 
+/*
+ * Pre-fault more dirty-logged pages than the ring holds: each call has to stop
+ * before the ring overflows, and no entry may be lost.
+ */
+static void test_pre_fault_dirty_ring(void)
+{
+	const u64 entries = TEST_RING_SIZE / sizeof(struct kvm_dirty_gfn);
+	const u64 npages = entries + entries / 4;
+	struct kvm_pre_fault_memory range, left;
+	struct kvm_dirty_gfn *gfns, *cur;
+	u64 harvested = 0, n, size;
+	struct kvm_vcpu *vcpu;
+	int ret, stops = 0;
+	struct kvm_vm *vm;
+	u32 fetch = 0;
+	gpa_t gpa;
+
+	if (!kvm_has_cap(KVM_CAP_DIRTY_LOG_RING) && !kvm_has_cap(KVM_CAP_DIRTY_LOG_RING_ACQ_REL)) {
+		pr_info("Skipping the dirty ring test: no dirty ring\n");
+		return;
+	}
+
+	vm = vm_create(1);
+	vm_enable_dirty_ring(vm, TEST_RING_SIZE);
+	vcpu = vm_vcpu_add(vm, 0, NULL);
+	gfns = vcpu_map_dirty_ring(vcpu);
+
+	size = npages * vm->page_size;
+	gpa = align_down(vm->max_gfn * vm->page_size - size, SZ_2M);
+	vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, gpa, TEST_SLOT, npages,
+				    KVM_MEM_LOG_DIRTY_PAGES);
+
+	/* Populate the pages, so that pre-faulting can map them writable. */
+	memset(addr_gpa2hva(vm, gpa), 1, size);
+
+	range = (struct kvm_pre_fault_memory) { .gpa = gpa, .size = size };
+	while (range.size) {
+		vcpu_ioctl(vcpu, KVM_PRE_FAULT_MEMORY, &range);
+
+		if (range.size) {
+			left = range;
+			ret = __vcpu_ioctl(vcpu, KVM_PRE_FAULT_MEMORY, &range);
+			TEST_ASSERT_VM_VCPU_IOCTL(ret == -1 && errno == ENOSPC,
+						  KVM_PRE_FAULT_MEMORY, ret, vm);
+			TEST_ASSERT(range.gpa == left.gpa && range.size == left.size,
+				    "KVM_PRE_FAULT_MEMORY made progress on a full ring");
+			stops++;
+		}
+
+		for (n = 0; ; n++) {
+			cur = &gfns[fetch % entries];
+			if (smp_load_acquire(&cur->flags) != KVM_DIRTY_GFN_F_DIRTY)
+				break;
+
+			TEST_ASSERT(cur->slot == TEST_SLOT && cur->offset == harvested + n,
+				    "Ring entry %u: slot %u offset 0x%llx, expected slot %u offset 0x%lx",
+				    fetch, cur->slot, cur->offset, TEST_SLOT, harvested + n);
+			smp_store_release(&cur->flags, KVM_DIRTY_GFN_F_RESET);
+			fetch++;
+		}
+
+		/* A read pre-fault can map a page read-only, or access-tracked on x86. */
+		if (!n && !harvested && !range.size) {
+			pr_info("Skipping the dirty ring test: pre-faulting dirtied no pages\n");
+			kvm_vm_free(vm);
+			return;
+		}
+		TEST_ASSERT(n, "KVM_PRE_FAULT_MEMORY pushed nothing to the ring");
+		TEST_ASSERT(kvm_vm_reset_dirty_ring(vm) == n,
+			    "KVM_RESET_DIRTY_RINGS didn't reset %lu entries", n);
+		harvested += n;
+	}
+
+	TEST_ASSERT(harvested == npages, "%lu ring entries for %lu pages", harvested, npages);
+	TEST_ASSERT(stops, "KVM_PRE_FAULT_MEMORY never stopped for a %lu-entry ring", entries);
+	pr_info("Pre-faulted %lu pages through a %lu-entry ring, stopping %d times\n",
+		npages, entries, stops);
+
+	kvm_vm_free(vm);
+}
+
 static void help(char *name)
 {
 	puts("");
@@ -320,5 +404,6 @@ int main(int argc, char *argv[])
 	test_pre_fault_memory(KVM_X86_SW_PROTECTED_VM, backing, false);
 	test_pre_fault_memory(KVM_X86_SW_PROTECTED_VM, backing, true);
 #endif
+	test_pre_fault_dirty_ring();
 	return 0;
 }
Signature · 1 line
2.47.3
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] KVM: Stop KVM_PRE_FAULT_MEMORY when the vCPU's dirty ring is full

In reply to Fuad Tabba
Quoted 7 lines · Fuad Tabba

Pre-faulting can map a dirty-logged page writable, which pushes it to the vCPU's dirty ring. KVM_RUN exits with KVM_EXIT_DIRTY_RING_FULL before the ring overflows, but the pre-fault loop never checks it, so one call over more such pages than the ring has free trips the WARN in kvm_dirty_ring_push() and overwrites entries userspace hasn't harvested. On x86:

Quoted 9 lines
  WARNING: arch/x86/kvm/../../../virt/kvm/dirty_ring.c:225 at kvm_dirty_ring_push+0x7a/0xd0
  Call Trace:
   make_spte+0x2cc/0x450
   kvm_tdp_mmu_map+0x5e1/0x7f0
   kvm_tdp_page_fault+0x12c/0x1b0
   kvm_mmu_do_page_fault+0x181/0x250
   kvm_arch_vcpu_pre_fault_memory+0x136/0x1d0
   kvm_vcpu_pre_fault_memory+0xcf/0x180
   kvm_vcpu_ioctl+0x455/0x770

If you have a local repro maybe worth sending a regression test for this?... and I type that, then check the 2/2 and see that you already did :) ok haha thanks!

Quoted 10 lines

Stop the loop once the ring is soft full, returning the pages already processed, or -ENOSPC when there are none, since a retry can't succeed until userspace harvests the ring. kvm_dirty_ring_check_request() fills in a KVM_RUN exit, so test the ring's state directly. The stub for !HAVE_KVM_DIRTY_RING is for s390, which has the ioctl but no ring.

Fixes bc1a5cd002116 ("KVM: Add KVM_PRE_FAULT_MEMORY vcpu ioctl to pre-populate guest memory")

Yikes, nasty. Thanks for addressing this!

LGTM so:

Quoted 96 lines
---
 Documentation/virt/kvm/api.rst | 9 +++++++--
 include/linux/kvm_dirty_ring.h | 6 ++++++
 virt/kvm/dirty_ring.c          | 2 +-
 virt/kvm/kvm_main.c            | 6 ++++++
 4 files changed, 20 insertions(+), 3 deletions(-)
Documentation/virt/kvm/api.rst +7 -2
index e95c9cda085d9..89eb6be9bc6e3 100644
@@ -6544,6 +6544,7 @@ Errors:
   ENOENT     The specified `gpa` is outside defined memslots.
   ENOEXEC    The vCPU has not been initialised (arm64).
   EINTR      An unmasked signal is pending and no page was processed.
+  ENOSPC     The vCPU's dirty ring is full and no page was processed.
   EFAULT     The parameter address was invalid.
   EHWPOISON  A poisoned host page was encountered.
   EOPNOTSUPP Mapping memory for a GPA is unsupported by the
@@ -6588,7 +6589,9 @@ case, the ioctl can be called in parallel.
 
 When the ioctl returns, the input values are updated to point to the
 remaining range.  If `size` > 0 on return, the caller can just issue
-the ioctl again with the same `struct kvm_map_memory` argument.
+the ioctl again with the same `struct kvm_pre_fault_memory` argument.
+If that fails with ENOSPC, the vCPU's dirty ring has to be harvested
+and reset first.
 
 On x86, shadow page tables cannot support this ioctl because they
 are indexed by virtual address or nested guest physical address.
@@ -8957,7 +8960,9 @@ Therefore, the ioctl must be called *before* reading the content of
 the dirty pages.
 
 The dirty ring can get full.  When it happens, the KVM_RUN of the
-vcpu will return with exit reason KVM_EXIT_DIRTY_RING_FULL.
+vcpu will return with exit reason KVM_EXIT_DIRTY_RING_FULL, and
+KVM_PRE_FAULT_MEMORY on the vcpu will process no more pages until the
+ring has been harvested and reset.
 
 The dirty ring interface has a major difference comparing to the
 KVM_GET_DIRTY_LOG interface in that, when reading the dirty ring from
include/linux/kvm_dirty_ring.h +6 -0
index eb10d87adf7d5..88b4f2a009274 100644
@@ -60,6 +60,11 @@ static inline void kvm_dirty_ring_push(struct kvm_vcpu *vcpu,
 {
 }
 
+static inline bool kvm_dirty_ring_soft_full(struct kvm_dirty_ring *ring)
+{
+	return false;
+}
+
 static inline struct page *kvm_dirty_ring_get_page(struct kvm_dirty_ring *ring,
 						   u32 offset)
 {
@@ -81,6 +86,7 @@ int kvm_dirty_ring_alloc(struct kvm *kvm, struct kvm_dirty_ring *ring,
 int kvm_dirty_ring_reset(struct kvm *kvm, struct kvm_dirty_ring *ring,
 			 int *nr_entries_reset);
 void kvm_dirty_ring_push(struct kvm_vcpu *vcpu, u32 slot, u64 offset);
+bool kvm_dirty_ring_soft_full(struct kvm_dirty_ring *ring);
 
 bool kvm_dirty_ring_check_request(struct kvm_vcpu *vcpu);
 
virt/kvm/dirty_ring.c +1 -1
index 572b854edf740..f73f92102cb7a 100644
@@ -40,7 +40,7 @@ static u32 kvm_dirty_ring_used(struct kvm_dirty_ring *ring)
 	return READ_ONCE(ring->dirty_index) - READ_ONCE(ring->reset_index);
 }
 
-static bool kvm_dirty_ring_soft_full(struct kvm_dirty_ring *ring)
+bool kvm_dirty_ring_soft_full(struct kvm_dirty_ring *ring)
 {
 	return kvm_dirty_ring_used(ring) >= ring->soft_limit;
 }
virt/kvm/kvm_main.c +6 -1
index cf4cb586f1407..da1b76ceb65e3 100644
@@ -4461,6 +4461,12 @@ static int kvm_vcpu_pre_fault_memory(struct kvm_vcpu *vcpu,
 			break;
 		}
 
+		/* Like KVM_RUN: only the iteration crossing the soft limit uses the reserve. */
+		if (vcpu->kvm->dirty_ring_size && kvm_dirty_ring_soft_full(&vcpu->dirty_ring)) {
+			r = -ENOSPC;
+			break;
+		}
+
 		r = kvm_arch_vcpu_pre_fault_memory(vcpu, range);
 		if (WARN_ON_ONCE(r == 0 || r == -EIO))
 			break;
--
Quoted 2 lines

2.47.3

--
Cheers, Lorenzo

#10 lore raw

Re: [PATCH 2/2] KVM: selftests: Test KVM_PRE_FAULT_MEMORY against a full dirty ring

In reply to Fuad Tabba
Quoted 4 lines · Fuad Tabba

Pre-fault more populated, dirty-logged pages than the vCPU's dirty ring holds, and check that each call stops before the ring overflows, that a retry returns ENOSPC without progress, and that the harvested entries are the pages in order. A read pre-fault can map a page read-only, or

Are we asserting an invariant that prefaulting happens in address order here? Is that explicitly presumed in the prefault API, or just how it happens to pan out?

I wonder if we might decide for whatever reason in future to do things differently there. Bit nitty I know, but wonder if it's asserting an implementation detail?

Quoted 56 lines

access-tracked on x86 without EPT A/D bits, so skip the case if pre-faulting dirties nothing.

---
 .../selftests/kvm/pre_fault_memory_test.c     | 85 +++++++++++++++++++
 1 file changed, 85 insertions(+)
tools/testing/selftests/kvm/pre_fault_memory_test.c +26 -0
index 3082fe09b95fa..18fc1d69acbf0 100644
@@ -5,6 +5,7 @@
  * Author:
  * Isaku Yamahata <isaku.yamahata at gmail.com>
  */
+#include <asm/barrier.h>
 #include <linux/sizes.h>
 
 #include <test_util.h>
@@ -17,6 +18,8 @@
 /* Arbitrarily chosen values */
 #define TEST_BASE_SIZE		SZ_2M
 #define TEST_SLOT		10
+/* Large enough for the entries every architecture reserves. */
+#define TEST_RING_SIZE		SZ_64K
 
 /* Storage of test info to share with guest code */
 struct test_config {
@@ -281,6 +284,87 @@ static void test_pre_fault_memory(unsigned long vm_type, enum vm_mem_backing_src
 	for_each_guest_mode(__test_pre_fault_memory, &p);
 }
 
+/*
+ * Pre-fault more dirty-logged pages than the ring holds: each call has to stop
+ * before the ring overflows, and no entry may be lost.
+ */
+static void test_pre_fault_dirty_ring(void)
+{
+	const u64 entries = TEST_RING_SIZE / sizeof(struct kvm_dirty_gfn);
+	const u64 npages = entries + entries / 4;
+	struct kvm_pre_fault_memory range, left;
+	struct kvm_dirty_gfn *gfns, *cur;
+	u64 harvested = 0, n, size;
+	struct kvm_vcpu *vcpu;
+	int ret, stops = 0;
+	struct kvm_vm *vm;
+	u32 fetch = 0;
+	gpa_t gpa;
+
+	if (!kvm_has_cap(KVM_CAP_DIRTY_LOG_RING) && !kvm_has_cap(KVM_CAP_DIRTY_LOG_RING_ACQ_REL)) {
+		pr_info("Skipping the dirty ring test: no dirty ring\n");
+		return;
+	}
+
+	vm = vm_create(1);

tools/testing/selftests/kvm/pre_fault_memory_test.c static void test_pre_fault_memory(unsigned long vm_type, enum vm_mem_backing_src · line 319

Quoted 10 lines
tools/testing/selftests/kvm/pre_fault_memory_test.c +10 -0
+	vm_enable_dirty_ring(vm, TEST_RING_SIZE);
+	vcpu = vm_vcpu_add(vm, 0, NULL);
+	gfns = vcpu_map_dirty_ring(vcpu);
+
+	size = npages * vm->page_size;
+	gpa = align_down(vm->max_gfn * vm->page_size - size, SZ_2M);
+	vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, gpa, TEST_SLOT, npages,
+				    KVM_MEM_LOG_DIRTY_PAGES);
+
+	/* Populate the pages, so that pre-faulting can map them writable. */

Reviewed below, but I wonder how x86 could then read fault these?

tools/testing/selftests/kvm/pre_fault_memory_test.c static void test_pre_fault_memory(unsigned long vm_type, enum vm_mem_backing_src · line 323

Quoted 4 lines
tools/testing/selftests/kvm/pre_fault_memory_test.c +4 -0
+	memset(addr_gpa2hva(vm, gpa), 1, size);
+
+	range = (struct kvm_pre_fault_memory) { .gpa = gpa, .size = size };
+	while (range.size) {

NIT: n could be scoped to this block instead.

tools/testing/selftests/kvm/pre_fault_memory_test.c static void test_pre_fault_memory(unsigned long vm_type, enum vm_mem_backing_src · line 336

Quoted 13 lines
tools/testing/selftests/kvm/pre_fault_memory_test.c +13 -0
+		vcpu_ioctl(vcpu, KVM_PRE_FAULT_MEMORY, &range);
+
+		if (range.size) {
+			left = range;
+			ret = __vcpu_ioctl(vcpu, KVM_PRE_FAULT_MEMORY, &range);
+			TEST_ASSERT_VM_VCPU_IOCTL(ret == -1 && errno == ENOSPC,
+						  KVM_PRE_FAULT_MEMORY, ret, vm);
+			TEST_ASSERT(range.gpa == left.gpa && range.size == left.size,
+				    "KVM_PRE_FAULT_MEMORY made progress on a full ring");
+			stops++;
+		}
+
+		for (n = 0; ; n++) {

Could this loop forever if cur->flags always == KVM_DIRTY_GFN_F_DIRTY? Or is that guaranteed not to be the case?

tools/testing/selftests/kvm/pre_fault_memory_test.c static void test_pre_fault_memory(unsigned long vm_type, enum vm_mem_backing_src · line 338

Quoted 2 lines
tools/testing/selftests/kvm/pre_fault_memory_test.c +2 -0
+			cur = &gfns[fetch % entries];
+			if (smp_load_acquire(&cur->flags) != KVM_DIRTY_GFN_F_DIRTY)

Is this and the below release paired with equivalent stuff in the dirty ring code?

tools/testing/selftests/kvm/pre_fault_memory_test.c static void test_pre_fault_memory(unsigned long vm_type, enum vm_mem_backing_src · line 348

Quoted 10 lines
tools/testing/selftests/kvm/pre_fault_memory_test.c +10 -0
+				break;
+
+			TEST_ASSERT(cur->slot == TEST_SLOT && cur->offset == harvested + n,
+				    "Ring entry %u: slot %u offset 0x%llx, expected slot %u offset 0x%lx",
+				    fetch, cur->slot, cur->offset, TEST_SLOT, harvested + n);
+			smp_store_release(&cur->flags, KVM_DIRTY_GFN_F_RESET);
+			fetch++;
+		}
+
+		/* A read pre-fault can map a page read-only, or access-tracked on x86. */

Hmm under what circumstances would that happen? Is there no way to ensure pages are dirtied by prefaulting? I imagine not though!

tools/testing/selftests/kvm/pre_fault_memory_test.c static void test_pre_fault_memory(unsigned long vm_type, enum vm_mem_backing_src · line 349

Quoted 1 line
tools/testing/selftests/kvm/pre_fault_memory_test.c +1 -0
+		if (!n && !harvested && !range.size) {

Hmm, so !n && (harvested || range_size) will pass this, but then you assert n immediately below, so it seems like n is an unconditional requirement.

So I wonder whether this should just be !n in this conditional or maybe the assert afterwards should be dropped?

Quoted 29 lines
tools/testing/selftests/kvm/pre_fault_memory_test.c +19 -1
+			pr_info("Skipping the dirty ring test: pre-faulting dirtied no pages\n");
+			kvm_vm_free(vm);
+			return;
+		}
+		TEST_ASSERT(n, "KVM_PRE_FAULT_MEMORY pushed nothing to the ring");
+		TEST_ASSERT(kvm_vm_reset_dirty_ring(vm) == n,
+			    "KVM_RESET_DIRTY_RINGS didn't reset %lu entries", n);
+		harvested += n;
+	}
+
+	TEST_ASSERT(harvested == npages, "%lu ring entries for %lu pages", harvested, npages);
+	TEST_ASSERT(stops, "KVM_PRE_FAULT_MEMORY never stopped for a %lu-entry ring", entries);
+	pr_info("Pre-faulted %lu pages through a %lu-entry ring, stopping %d times\n",
+		npages, entries, stops);
+
+	kvm_vm_free(vm);
+}
+
 static void help(char *name)
 {
 	puts("");
@@ -320,5 +404,6 @@ int main(int argc, char *argv[])
 	test_pre_fault_memory(KVM_X86_SW_PROTECTED_VM, backing, false);
 	test_pre_fault_memory(KVM_X86_SW_PROTECTED_VM, backing, true);
 #endif
+	test_pre_fault_dirty_ring();
 	return 0;
 }
--
Quoted 2 lines

2.47.3

--
Cheers, Lorenzo

author reviewer
#11 lore raw

Re: [PATCH 2/2] KVM: selftests: Test KVM_PRE_FAULT_MEMORY against a full dirty ring

Hi Lorenzo,

Quoted 6 lines · Lorenzo Stoakes (ARM)
Quoted 4 lines · Fuad Tabba

Pre-fault more populated, dirty-logged pages than the vCPU's dirty ring holds, and check that each call stops before the ring overflows, that a retry returns ENOSPC without progress, and that the harvested entries are the pages in order. A read pre-fault can map a page read-only, or

Quoted 7 lines

Are we asserting an invariant that prefaulting happens in address order here? Is that explicitly presumed in the prefault API, or just how it happens to pan out?

I wonder if we might decide for whatever reason in future to do things differently there. Bit nitty I know, but wonder if it's asserting an implementation detail?

Yes, the API doesn't guarantee an order. I'll assert that every page is reported exactly once instead.

[...]

Quoted 1 line
Quoted 1 line
tools/testing/selftests/kvm/pre_fault_memory_test.c +0 -0

[...]

Quoted 1 line
Quoted 1 line

+static void test_pre_fault_dirty_ring(void)

[...]

Quoted 3 lines
Quoted 1 line

+ /* Populate the pages, so that pre-faulting can map them writable. */

Reviewed below, but I wonder how x86 could then read fault these?

KVM_PRE_FAULT_MEMORY is defined as a read fault, but x86 maps the page writable when the host page is, and marks it dirty, unless it falls back to access tracking (below). Populating the pages makes sure they aren't backed by the zero page.

[...]

Quoted 3 lines
Quoted 1 line

+ while (range.size) {

NIT: n could be scoped to this block instead.

Will do.

[...]

Quoted 4 lines
Quoted 1 line

+ for (n = 0; ; n++) {

Could this loop forever if cur->flags always == KVM_DIRTY_GFN_F_DIRTY? Or is that guaranteed not to be the case?

It can't. Every harvested entry is set to KVM_DIRTY_GFN_F_RESET and stays that way until KVM_RESET_DIRTY_RINGS, which runs after the loop, so one pass stops after at most a full ring.

Quoted 4 lines

tools/testing/selftests/kvm/pre_fault_memory_test.c static void test_pre_fault_memory(unsigned long vm_type, enum vm_mem_backing_src · line 338

Quoted 2 lines
tools/testing/selftests/kvm/pre_fault_memory_test.c +2 -0
+            cur = &gfns[fetch % entries];
+            if (smp_load_acquire(&cur->flags) != KVM_DIRTY_GFN_F_DIRTY)

Is this and the below release paired with equivalent stuff in the dirty ring code?

Yes, the acquire pairs with the smp_wmb() in kvm_dirty_ring_push() ahead of the flag store, and the release with the smp_load_acquire() in kvm_dirty_gfn_harvested(). dirty_log_test does the same.

[...]

Quoted 4 lines
Quoted 1 line

+ /* A read pre-fault can map a page read-only, or access-tracked on x86. */

Hmm under what circumstances would that happen? Is there no way to ensure pages are dirtied by prefaulting? I imagine not though!

No, it's up to the architecture. arm64 maps a read fault read-only while the slot is logged with a bitmap, and x86 with EPT but no A/D bits maps it access-tracked. Neither pushes anything to the ring.

Quoted 7 lines
Quoted 1 line

+ if (!n && !harvested && !range.size) {

Hmm, so !n && (harvested || range_size) will pass this, but then you assert n immediately below, so it seems like n is an unconditional requirement.

So I wonder whether this should just be !n in this conditional or maybe the assert afterwards should be dropped?

The skip is for when pre-faulting dirties nothing at all: the first call maps the whole range without pushing a single entry, so there's nothing to test. Any other empty harvest is a bug, which the assert reports, so I'd rather keep both as they are.

Cheers,
/fuad

#3 lore raw

Re: [PATCH 1/2] KVM: Stop KVM_PRE_FAULT_MEMORY when the vCPU's dirty ring is full

In reply to Fuad Tabba

virt/kvm/kvm_main.c static int kvm_vcpu_pre_fault_memory(struct kvm_vcpu *vcpu, · line 4464

Quoted 9 lines · Fuad Tabba
virt/kvm/kvm_main.c +1 -0
index cf4cb586f1407..da1b76ceb65e3 100644
@@ -4461,6 +4461,12 @@ static int kvm_vcpu_pre_fault_memory(struct kvm_vcpu *vcpu,
 			break;
 		}
 
+		/* Like KVM_RUN: only the iteration crossing the soft limit uses the reserve. */

Please don't include AI comments verbatim.

virt/kvm/kvm_main.c static int kvm_vcpu_pre_fault_memory(struct kvm_vcpu *vcpu, · line 4465

Quoted 1 line
virt/kvm/kvm_main.c +1 -0
+		if (vcpu->kvm->dirty_ring_size && kvm_dirty_ring_soft_full(&vcpu->dirty_ring)) {

Eh, I vote to just use kvm_dirty_ring_check_request(), I don't see any harm in clobbering vcpu->run->exit_reason.

Side topic, kvm_dirty_ring_check_request() is rather confusing (clears and then re-sets the requests). AFAICT, it can be this instead:

	lockdep_assert_held(vcpu->mutex);

	/*
	 * The vCPU isn't runnable when the dirty ring becomes soft full.  Keep
	 * the requests set until the dirty pages are harvested and the dirty
	 * ring is reset by userspace.  Note, checking if the ring is soft-full
	 * can get a false positive, but not a false negative, because only the
	 * vCPU can add entries to the ring, and its mutex is held.
	 */
	if (kvm_test_request(KVM_REQ_DIRTY_RING_SOFT_FULL, vcpu) &&
	    kvm_dirty_ring_soft_full(&vcpu->dirty_ring)) {
		vcpu->run->exit_reason = KVM_EXIT_DIRTY_RING_FULL;
		trace_kvm_dirty_ring_exit(vcpu);
		return true;
	}

	kvm_clear_request(KVM_REQ_DIRTY_RING_SOFT_FULL, vcpu);
	return false;
Quoted 10 lines
virt/kvm/kvm_main.c +4 -0
+			r = -ENOSPC;
+			break;
+		}
+
 		r = kvm_arch_vcpu_pre_fault_memory(vcpu, range);
 		if (WARN_ON_ONCE(r == 0 || r == -EIO))
 			break;
Signature · 1 line
2.47.3
#12 lore raw

Re: [PATCH 2/2] KVM: selftests: Test KVM_PRE_FAULT_MEMORY against a full dirty ring

In reply to Fuad Tabba
Quoted 26 lines · Fuad Tabba

Hi Lorenzo,

Quoted 6 lines · Lorenzo Stoakes (ARM)
Quoted 4 lines · Fuad Tabba

Pre-fault more populated, dirty-logged pages than the vCPU's dirty ring holds, and check that each call stops before the ring overflows, that a retry returns ENOSPC without progress, and that the harvested entries are the pages in order. A read pre-fault can map a page read-only, or

Quoted 7 lines

Are we asserting an invariant that prefaulting happens in address order here? Is that explicitly presumed in the prefault API, or just how it happens to pan out?

I wonder if we might decide for whatever reason in future to do things differently there. Bit nitty I know, but wonder if it's asserting an implementation detail?

Yes, the API doesn't guarantee an order. I'll assert that every page is reported exactly once instead.

[...]

Quoted 1 line
Quoted 1 line
tools/testing/selftests/kvm/pre_fault_memory_test.c +0 -0

[...]

Quoted 1 line
Quoted 1 line

+static void test_pre_fault_dirty_ring(void)

[...]

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

+ /* Populate the pages, so that pre-faulting can map them writable. */

Reviewed below, but I wonder how x86 could then read fault these?

KVM_PRE_FAULT_MEMORY is defined as a read fault, but x86 maps the page writable when the host page is, and marks it dirty, unless it falls back to access tracking (below). Populating the pages makes sure they aren't backed by the zero page.

Before we effectively solidify the behavior, I think we should have an explicit discussion about the desired behavior of KVM_PRE_FAULT_MEMORY when dirty logging is enabled. E.g. the only reason I kept the existing behavior in my auto-prefault series was because I couldn't think of a good reason to change it. But we never made a concious decision in the first place, i.e. we never considered what would happen if prefaulting collided with dirty logging (as evidenced by this bug).

#17 lore raw

Re: [PATCH 2/2] KVM: selftests: Test KVM_PRE_FAULT_MEMORY against a full dirty ring

In reply to Fuad Tabba
Quoted 14 lines · Fuad Tabba

Hi Lorenzo,

Quoted 10 lines · Lorenzo Stoakes (ARM)
Quoted 4 lines · Fuad Tabba

Pre-fault more populated, dirty-logged pages than the vCPU's dirty ring holds, and check that each call stops before the ring overflows, that a retry returns ENOSPC without progress, and that the harvested entries are the pages in order. A read pre-fault can map a page read-only, or

Are we asserting an invariant that prefaulting happens in address order here? Is that explicitly presumed in the prefault API, or just how it happens to pan out?

Quoted 6 lines
Quoted 3 lines

I wonder if we might decide for whatever reason in future to do things differently there. Bit nitty I know, but wonder if it's asserting an implementation detail?

Yes, the API doesn't guarantee an order. I'll assert that every page is reported exactly once instead.

THanks!

Quoted 6 lines

[...]

Quoted 1 line
Quoted 1 line
tools/testing/selftests/kvm/pre_fault_memory_test.c +0 -0

[...]

Quoted 1 line
Quoted 1 line

+static void test_pre_fault_dirty_ring(void)

[...]

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

+ /* Populate the pages, so that pre-faulting can map them writable. */

Reviewed below, but I wonder how x86 could then read fault these?

KVM_PRE_FAULT_MEMORY is defined as a read fault, but x86 maps the page writable when the host page is, and marks it dirty, unless it falls back to access tracking (below). Populating the pages makes sure they aren't backed by the zero page.

Ack.

Quoted 7 lines

[...]

Quoted 3 lines
Quoted 1 line

+ while (range.size) {

NIT: n could be scoped to this block instead.

Will do.

Thanks!

Quoted 10 lines

[...]

Quoted 4 lines
Quoted 1 line

+ for (n = 0; ; n++) {

Could this loop forever if cur->flags always == KVM_DIRTY_GFN_F_DIRTY? Or is that guaranteed not to be the case?

It can't. Every harvested entry is set to KVM_DIRTY_GFN_F_RESET and stays that way until KVM_RESET_DIRTY_RINGS, which runs after the loop, so one pass stops after at most a full ring.

Ack OK.

Quoted 9 lines
Quoted 5 lines

tools/testing/selftests/kvm/pre_fault_memory_test.c static void test_pre_fault_memory(unsigned long vm_type, enum vm_mem_backing_src · line 338

Quoted 3 lines
tools/testing/selftests/kvm/pre_fault_memory_test.c +2 -0
+            cur = &gfns[fetch % entries];
+            if (smp_load_acquire(&cur->flags) != KVM_DIRTY_GFN_F_DIRTY)

Is this and the below release paired with equivalent stuff in the dirty ring code?

Yes, the acquire pairs with the smp_wmb() in kvm_dirty_ring_push() ahead of the flag store, and the release with the smp_load_acquire() in kvm_dirty_gfn_harvested(). dirty_log_test does the same.

I know it's test code, but general kernel practice is to add a comment saying what memory barriers pair with so it's easier to track, could you add those for these?

Quoted 10 lines

[...]

Quoted 4 lines
Quoted 1 line

+ /* A read pre-fault can map a page read-only, or access-tracked on x86. */

Hmm under what circumstances would that happen? Is there no way to ensure pages are dirtied by prefaulting? I imagine not though!

No, it's up to the architecture. arm64 maps a read fault read-only while the slot is logged with a bitmap, and x86 with EPT but no A/D bits maps it access-tracked. Neither pushes anything to the ring.

Right, OK, makes sense.

Quoted 6 lines
Quoted 6 lines
Quoted 2 lines

+ if (!n && !harvested && !range.size) {

Hmm, so !n && (harvested || range_size) will pass this, but then you assert n immediately below, so it seems like n is an unconditional requirement.

Quoted 7 lines
Quoted 2 lines

So I wonder whether this should just be !n in this conditional or maybe the assert afterwards should be dropped?

The skip is for when pre-faulting dirties nothing at all: the first call maps the whole range without pushing a single entry, so there's nothing to test. Any other empty harvest is a bug, which the assert reports, so I'd rather keep both as they are.

OK I see. Really the assert below is !n && (harvested || range.size), maybe clearer to be explicit but it's also a bit noisy so not a big deal.

--
Cheers, Lorenzo

#13 lore raw

Re: [PATCH 2/2] KVM: selftests: Test KVM_PRE_FAULT_MEMORY against a full dirty ring

In reply to Sean Christopherson
Quoted 36 lines · Sean Christopherson
Quoted 26 lines · Fuad Tabba

Hi Lorenzo,

Quoted 6 lines · Lorenzo Stoakes (ARM)
Quoted 4 lines · Fuad Tabba

Pre-fault more populated, dirty-logged pages than the vCPU's dirty ring holds, and check that each call stops before the ring overflows, that a retry returns ENOSPC without progress, and that the harvested entries are the pages in order. A read pre-fault can map a page read-only, or

Quoted 7 lines

Are we asserting an invariant that prefaulting happens in address order here? Is that explicitly presumed in the prefault API, or just how it happens to pan out?

I wonder if we might decide for whatever reason in future to do things differently there. Bit nitty I know, but wonder if it's asserting an implementation detail?

Yes, the API doesn't guarantee an order. I'll assert that every page is reported exactly once instead.

[...]

Quoted 1 line
Quoted 1 line
tools/testing/selftests/kvm/pre_fault_memory_test.c +0 -0

[...]

Quoted 1 line
Quoted 1 line

+static void test_pre_fault_dirty_ring(void)

[...]

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

+ /* Populate the pages, so that pre-faulting can map them writable. */

Reviewed below, but I wonder how x86 could then read fault these?

KVM_PRE_FAULT_MEMORY is defined as a read fault, but x86 maps the page writable when the host page is, and marks it dirty, unless it falls back to access tracking (below). Populating the pages makes sure they aren't backed by the zero page.

Quoted 6 lines

Before we effectively solidify the behavior, I think we should have an explicit discussion about the desired behavior of KVM_PRE_FAULT_MEMORY when dirty logging is enabled. E.g. the only reason I kept the existing behavior in my auto-prefault series was because I couldn't think of a good reason to change it. But we never made a concious decision in the first place, i.e. we never considered what would happen if prefaulting collided with dirty logging (as evidenced by this bug).

Yeah, I do worry about implicit assumptions (also commented re: ordering).

I guess what makes sense, given the general approach for prefaulting = injecting a synthetic read fault, is for the expectation to be that it matches the behaviour of an ordinary read fault.

Or do you feel that it makes sense for the pre-faulting to do something special in these cases and establish a sensible invariant?

If the pre-faulting does indeed dirty the pages, you'd need the dirty logging to do its usual thing (not familiar with the code, so I'm guessing logging the page :) though right?

--
Cheers, Lorenzo

author reviewer
#4 lore raw

Re: [PATCH 1/2] KVM: Stop KVM_PRE_FAULT_MEMORY when the vCPU's dirty ring is full

In reply to Sean Christopherson

Hi Sean,

Quoted 2 lines · Sean Christopherson
Quoted 1 line · Fuad Tabba
virt/kvm/kvm_main.c +0 -0

[...]

Quoted 7 lines

virt/kvm/kvm_main.c static int kvm_vcpu_pre_fault_memory(struct kvm_vcpu *vcpu, · line 4464

Quoted 5 lines
virt/kvm/kvm_main.c +1 -0
@@ -4461,6 +4461,12 @@ static int kvm_vcpu_pre_fault_memory(struct kvm_vcpu *vcpu,
                         break;
                 }
 
+                /* Like KVM_RUN: only the iteration crossing the soft limit uses the reserve. */

Please don't include AI comments verbatim.

The series is LLM-assisted, but the comment was actually me.

Quoted 4 lines

virt/kvm/kvm_main.c static int kvm_vcpu_pre_fault_memory(struct kvm_vcpu *vcpu, · line 4465

Quoted 1 line
virt/kvm/kvm_main.c +1 -0
+                if (vcpu->kvm->dirty_ring_size && kvm_dirty_ring_soft_full(&vcpu->dirty_ring)) {

Eh, I vote to just use kvm_dirty_ring_check_request(), I don't see any harm in clobbering vcpu->run->exit_reason.

Will do.

Quoted 13 lines

Side topic, kvm_dirty_ring_check_request() is rather confusing (clears and then re-sets the requests). AFAICT, it can be this instead:

        lockdep_assert_held(vcpu->mutex);

        /*
         * The vCPU isn't runnable when the dirty ring becomes soft full.  Keep
         * the requests set until the dirty pages are harvested and the dirty
         * ring is reset by userspace.  Note, checking if the ring is soft-full
         * can get a false positive, but not a false negative, because only the
         * vCPU can add entries to the ring, and its mutex is held.
         */
        if (kvm_test_request(KVM_REQ_DIRTY_RING_SOFT_FULL, vcpu) &&
Quoted 8 lines
            kvm_dirty_ring_soft_full(&vcpu->dirty_ring)) {
                vcpu->run->exit_reason = KVM_EXIT_DIRTY_RING_FULL;
                trace_kvm_dirty_ring_exit(vcpu);
                return true;
        }

        kvm_clear_request(KVM_REQ_DIRTY_RING_SOFT_FULL, vcpu);
        return false;

I'll send it as its own patch.

Cheers,
/fuad

author reviewer
#16 lore raw

Re: [PATCH 2/2] KVM: selftests: Test KVM_PRE_FAULT_MEMORY against a full dirty ring

In reply to Sean Christopherson

On Tue, 10 Nov 2026 15:53:35 +0000, Sean Christopherson <[email protected]> wrote:
[...]

Quoted 6 lines

Before we effectively solidify the behavior, I think we should have an explicit discussion about the desired behavior of KVM_PRE_FAULT_MEMORY when dirty logging is enabled. E.g. the only reason I kept the existing behavior in my auto-prefault series was because I couldn't think of a good reason to change it. But we never made a concious decision in the first place, i.e. we never considered what would happen if prefaulting collided with dirty logging (as evidenced by this bug).

FWIW, I'd lean towards a pre-fault not dirtying the page. arm64 (with a dirty bitmap) and riscv already map a read fault on a logged page read-only.

Tried it on x86 by not mapping a prefetch writable in a logged slot, so that the fast path logs the first write. That also changes async #PF completion, so I don't think it's a stable fix.

The loop has to stop for as long as x86 logs, so should patch 1 stay as the stable fix? The test skips when pre-faulting dirties no pages, so it doesn't pin either behavior.

Cheers,
/fuad

author reviewer
#18 lore raw

Re: [PATCH 2/2] KVM: selftests: Test KVM_PRE_FAULT_MEMORY against a full dirty ring

In reply to Lorenzo Stoakes (ARM)

On Tue, 10 Nov 2026 15:57:37 +0000, "Lorenzo Stoakes (ARM)" <[email protected]> wrote:
[...]

Quoted 7 lines
Quoted 3 lines

Yes, the acquire pairs with the smp_wmb() in kvm_dirty_ring_push() ahead of the flag store, and the release with the smp_load_acquire() in kvm_dirty_gfn_harvested(). dirty_log_test does the same.

I know it's test code, but general kernel practice is to add a comment saying what memory barriers pair with so it's easier to track, could you add those for these?

Sure.

/fuad

#5 lore raw

Re: [PATCH 1/2] KVM: Stop KVM_PRE_FAULT_MEMORY when the vCPU's dirty ring is full

In reply to Fuad Tabba
Quoted 11 lines · Fuad Tabba

Hi Sean,

Quoted 2 lines · Sean Christopherson
Quoted 1 line · Fuad Tabba
virt/kvm/kvm_main.c +0 -0

[...]

Quoted 4 lines
Quoted 4 lines
virt/kvm/kvm_main.c +0 -0
@@ -4461,6 +4461,12 @@ static int kvm_vcpu_pre_fault_memory(struct kvm_vcpu *vcpu,
                         break;
                 }
 
Quoted 5 lines
Quoted 3 lines

virt/kvm/kvm_main.c static int kvm_vcpu_pre_fault_memory(struct kvm_vcpu *vcpu, · line 4464

Quoted 1 line
virt/kvm/kvm_main.c +1 -0
+                /* Like KVM_RUN: only the iteration crossing the soft limit uses the reserve. */

Please don't include AI comments verbatim.

The series is LLM-assisted, but the comment was actually me.

LOL, I think all of us are getting a bit corrupted. :-)

#14 lore raw

Re: [PATCH 2/2] KVM: selftests: Test KVM_PRE_FAULT_MEMORY against a full dirty ring

In reply to Lorenzo Stoakes (ARM)

Hi all,

Quoted 44 lines · Lorenzo Stoakes (ARM)
Quoted 36 lines · Sean Christopherson
Quoted 26 lines · Fuad Tabba

Hi Lorenzo,

Quoted 6 lines · Lorenzo Stoakes (ARM)
Quoted 4 lines · Fuad Tabba

Pre-fault more populated, dirty-logged pages than the vCPU's dirty ring holds, and check that each call stops before the ring overflows, that a retry returns ENOSPC without progress, and that the harvested entries are the pages in order. A read pre-fault can map a page read-only, or

Quoted 7 lines

Are we asserting an invariant that prefaulting happens in address order here? Is that explicitly presumed in the prefault API, or just how it happens to pan out?

I wonder if we might decide for whatever reason in future to do things differently there. Bit nitty I know, but wonder if it's asserting an implementation detail?

Yes, the API doesn't guarantee an order. I'll assert that every page is reported exactly once instead.

[...]

Quoted 1 line
Quoted 1 line
tools/testing/selftests/kvm/pre_fault_memory_test.c +0 -0

[...]

Quoted 1 line
Quoted 1 line

+static void test_pre_fault_dirty_ring(void)

[...]

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

+ /* Populate the pages, so that pre-faulting can map them writable. */

Reviewed below, but I wonder how x86 could then read fault these?

KVM_PRE_FAULT_MEMORY is defined as a read fault, but x86 maps the page writable when the host page is, and marks it dirty, unless it falls back to access tracking (below). Populating the pages makes sure they aren't backed by the zero page.

Quoted 6 lines

Before we effectively solidify the behavior, I think we should have an explicit discussion about the desired behavior of KVM_PRE_FAULT_MEMORY when dirty logging is enabled. E.g. the only reason I kept the existing behavior in my auto-prefault series was because I couldn't think of a good reason to change it. But we never made a concious decision in the first place, i.e. we never considered what would happen if prefaulting collided with dirty logging (as evidenced by this bug).

Quoted 5 lines

Yeah, I do worry about implicit assumptions (also commented re: ordering).

I guess what makes sense, given the general approach for prefaulting = injecting a synthetic read fault, is for the expectation to be that it matches the behaviour of an ordinary read fault.

Unfortunately, what happens in this case is not as obvious as it sounds, because a read fault on a memslot backed by guest_memfd maps the GPA with read *and* write permission on arm64 (if the memslot is not readonly, haven't checked what x86 does).

Thanks,
Alex

Quoted 11 lines

Or do you feel that it makes sense for the pre-faulting to do something special in these cases and establish a sensible invariant?

If the pre-faulting does indeed dirty the pages, you'd need the dirty logging to do its usual thing (not familiar with the code, so I'm guessing logging the page :) though right?

--
Cheers, Lorenzo

#15 lore raw

Re: [PATCH 2/2] KVM: selftests: Test KVM_PRE_FAULT_MEMORY against a full dirty ring

Quoted 25 lines · Alexandru Elisei
Quoted 24 lines · Lorenzo Stoakes (ARM)
Quoted 14 lines · Sean Christopherson
Quoted 4 lines · Fuad Tabba
Quoted 1 line
Quoted 1 line
tools/testing/selftests/kvm/pre_fault_memory_test.c +0 -0

[...]

Quoted 1 line
Quoted 1 line

+static void test_pre_fault_dirty_ring(void)

[...]

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

+ /* Populate the pages, so that pre-faulting can map them writable. */

Reviewed below, but I wonder how x86 could then read fault these?

KVM_PRE_FAULT_MEMORY is defined as a read fault, but x86 maps the page writable when the host page is, and marks it dirty, unless it falls back to access tracking (below). Populating the pages makes sure they aren't backed by the zero page.

Quoted 6 lines

Before we effectively solidify the behavior, I think we should have an explicit discussion about the desired behavior of KVM_PRE_FAULT_MEMORY when dirty logging is enabled. E.g. the only reason I kept the existing behavior in my auto-prefault series was because I couldn't think of a good reason to change it. But we never made a concious decision in the first place, i.e. we never considered what would happen if prefaulting collided with dirty logging (as evidenced by this bug).

Yeah, I do worry about implicit assumptions (also commented re: ordering).

Quoted 8 lines
Quoted 3 lines

I guess what makes sense, given the general approach for prefaulting = injecting a synthetic read fault, is for the expectation to be that it matches the behaviour of an ordinary read fault.

Unfortunately, what happens in this case is not as obvious as it sounds, because a read fault on a memslot backed by guest_memfd maps the GPA with read *and* write permission on arm64 (if the memslot is not readonly, haven't checked what x86 does).

Ya, this isn't about read vs. write faults, it's about whether or not *any* "fault" from KVM_PRE_FAULT_MEMORY should be allowed if the target memslot has dirty logging enabled.

I'm leaning toward "yes", mainly because I suspect that fully preventing installing writable mappings when dirty logging is enabled across all architectures would be more difficult that ensuring generic code doesn't explode, and in practice I doubt any real VMM usage of KVM_PRE_FAULT_MEMORY will care about dirty logging interactions, i.e. any KVM behavior will be a-ok with userspace.

author reviewer
#6 lore raw

Re: [PATCH 1/2] KVM: Stop KVM_PRE_FAULT_MEMORY when the vCPU's dirty ring is full

In reply to Fuad Tabba

Hi Sean,

On Tue, 10 Nov 2026 17:18:22 +0000, Fuad Tabba <[email protected]> wrote:
[...]

Quoted 4 lines
Quoted 2 lines

Eh, I vote to just use kvm_dirty_ring_check_request(), I don't see any harm in clobbering vcpu->run->exit_reason.

Will do.

After taking a closer look, the clobber is problematic on arm64, where KVM_RUN completes a pending MMIO exit only if run->exit_reason is still KVM_EXIT_MMIO. If a pre-fault stops on a full ring after an MMIO exit, the helper overwrites exit_reason, so the next KVM_RUN doesn't complete the exit and the guest repeats the MMIO access. The ring can be full at that point if the pre-fault dirties pages, or with NV, if the stage-2 walk for the MMIO access does.

I don't think changing how arm64 completes an MMIO exit should be in a fix for stable, so should I keep testing kvm_dirty_ring_soft_full() directly in v2, or do you have another suggestion?

Cheers,
/fuad

#7 lore raw

Re: [PATCH 1/2] KVM: Stop KVM_PRE_FAULT_MEMORY when the vCPU's dirty ring is full

Quoted 9 lines · Fuad Tabba

Hi Sean,

On Tue, 10 Nov 2026 17:18:22 +0000, Fuad Tabba <[email protected]> wrote:
[...]

Quoted 4 lines
Quoted 2 lines

Eh, I vote to just use kvm_dirty_ring_check_request(), I don't see any harm in clobbering vcpu->run->exit_reason.

Will do.

Quoted 7 lines

After taking a closer look, the clobber is problematic on arm64, where KVM_RUN completes a pending MMIO exit only if run->exit_reason is still KVM_EXIT_MMIO. If a pre-fault stops on a full ring after an MMIO exit, the helper overwrites exit_reason, so the next KVM_RUN doesn't complete the exit and the guest repeats the MMIO access. The ring can be full at that point if the pre-fault dirties pages, or with NV, if the stage-2 walk for the MMIO access does.

Wouldn't that require userspace to do KVM_PRE_FAULT_MEMORY after KVM_RUN with a KVM_EXIT_MMIO return? If so, userspace gets to deal with the fallout of being stupid.

Quoted 7 lines

I don't think changing how arm64 completes an MMIO exit should be in a fix for stable, so should I keep testing kvm_dirty_ring_soft_full() directly in v2, or do you have another suggestion?

Cheers,
/fuad

author reviewer
#8 lore raw

Re: [PATCH 1/2] KVM: Stop KVM_PRE_FAULT_MEMORY when the vCPU's dirty ring is full

Quoted 19 lines · Sean Christopherson
Quoted 9 lines · Fuad Tabba

Hi Sean,

On Tue, 10 Nov 2026 17:18:22 +0000, Fuad Tabba <[email protected]> wrote:
[...]

Quoted 4 lines
Quoted 2 lines

Eh, I vote to just use kvm_dirty_ring_check_request(), I don't see any harm in clobbering vcpu->run->exit_reason.

Will do.

Quoted 7 lines

After taking a closer look, the clobber is problematic on arm64, where KVM_RUN completes a pending MMIO exit only if run->exit_reason is still KVM_EXIT_MMIO. If a pre-fault stops on a full ring after an MMIO exit, the helper overwrites exit_reason, so the next KVM_RUN doesn't complete the exit and the guest repeats the MMIO access. The ring can be full at that point if the pre-fault dirties pages, or with NV, if the stage-2 walk for the MMIO access does.

Quoted 3 lines

Wouldn't that require userspace to do KVM_PRE_FAULT_MEMORY after KVM_RUN with a KVM_EXIT_MMIO return? If so, userspace gets to deal with the fallout of being stupid.

Ack, so v2 will be as you suggested.

/fuad