Skip to contentSkip to conversation

KVM ARM64 pre_fault_memory

PATCH Superseded Jack Thomson 4 participants 10 messages Nov 19, 2025 – Dec 15, 2025

Subsystems (5)
Documentation KVM Kselftest arm64 +1 more subsystems

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover KVM ARM64 pre_fault_memory

From: Jack Thomson <[email protected]>

This patch series adds ARM64 support for the KVM_PRE_FAULT_MEMORY feature, which was previously only available on x86 [1]. This allows us to reduce the number of stage-2 faults during execution. This is of benefit in post-copy migration scenarios, particularly in memory intensive applications, where we are experiencing high latencies due to the stage-2 faults.

Patch Overview:

- The first patch adds support for the KVM_PRE_FAULT_MEMORY ioctl on arm64.

- The second patch updates the pre_fault_memory_test to support arm64.

- The last patch extends the pre_fault_memory_test to cover different vm memory backings.

=== Changes Since v2 [2] ===

- Update fault info synthesize value. Thanks Suzuki
- Remove change to selftests for unaligned mmap allocations. Thanks Sean

[1]: https://lore.kernel.org/kvm/[email protected]
[2]: https://lore.kernel.org/linux-arm-kernel/[email protected]

Jack Thomson (3):
KVM: arm64: Add pre_fault_memory implementation
KVM: selftests: Enable pre_fault_memory_test for arm64
KVM: selftests: Add option for different backing in pre-fault tests

 Documentation/virt/kvm/api.rst                |   3 +-
 arch/arm64/kvm/Kconfig                        |   1 +
 arch/arm64/kvm/arm.c                          |   1 +
 arch/arm64/kvm/mmu.c                          |  73 +++++++++++-
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 .../selftests/kvm/pre_fault_memory_test.c     | 110 ++++++++++++++----
 6 files changed, 159 insertions(+), 30 deletions(-)

base-commit: 8a4821412cf2c1429fffa07c012dd150f2edf78c

Signature · 1 line
2.43.0
#1 lore raw

PATCH v3 1/3 KVM: arm64: Add pre_fault_memory implementation

From: Jack Thomson <[email protected]>

Add kvm_arch_vcpu_pre_fault_memory() for arm64. The implementation hands off the stage-2 faulting logic to either gmem_abort() or user_mem_abort().

Add an optional page_size output parameter to user_mem_abort() to return the VMA page size, which is needed when pre-faulting.

Update the documentation to clarify x86 specific behaviour.

---
 Documentation/virt/kvm/api.rst |  3 +-
 arch/arm64/kvm/Kconfig         |  1 +
 arch/arm64/kvm/arm.c           |  1 +
 arch/arm64/kvm/mmu.c           | 73 ++++++++++++++++++++++++++++++++--
 4 files changed, 73 insertions(+), 5 deletions(-)
Documentation/virt/kvm/api.rst +2 -1
index 57061fa29e6a..30872d080511 100644
@@ -6493,7 +6493,8 @@ Errors:
 KVM_PRE_FAULT_MEMORY populates KVM's stage-2 page tables used to map memory
 for the current vCPU state.  KVM maps memory as if the vCPU generated a
 stage-2 read page fault, e.g. faults in memory as needed, but doesn't break
-CoW.  However, KVM does not mark any newly created stage-2 PTE as Accessed.
+CoW.  However, on x86, KVM does not mark any newly created stage-2 PTE as
+Accessed.
 
 In the case of confidential VM types where there is an initial set up of
 private guest memory before the guest is 'finalized'/measured, this ioctl
arch/arm64/kvm/Kconfig +1 -0
index 4f803fd1c99a..6872aaabe16c 100644
@@ -25,6 +25,7 @@ menuconfig KVM
 	select HAVE_KVM_CPU_RELAX_INTERCEPT
 	select KVM_MMIO
 	select KVM_GENERIC_DIRTYLOG_READ_PROTECT
+	select KVM_GENERIC_PRE_FAULT_MEMORY
 	select VIRT_XFER_TO_GUEST_WORK
 	select KVM_VFIO
 	select HAVE_KVM_DIRTY_RING_ACQ_REL
arch/arm64/kvm/arm.c +1 -0
index 870953b4a8a7..88c5dc2b4ee8 100644
@@ -327,6 +327,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 	case KVM_CAP_IRQFD_RESAMPLE:
 	case KVM_CAP_COUNTER_OFFSET:
 	case KVM_CAP_ARM_WRITABLE_IMP_ID_REGS:
+	case KVM_CAP_PRE_FAULT_MEMORY:
 		r = 1;
 		break;
 	case KVM_CAP_SET_GUEST_DEBUG2:
arch/arm64/kvm/mmu.c +69 -4

94 lines not on this page · raw message

Signature · 1 line
2.43.0
#7 lore raw

PATCH v3 2/3 KVM: selftests: Enable pre_fault_memory_test for arm64

In reply to Jack Thomson

From: Jack Thomson <[email protected]>

Enable the pre_fault_memory_test to run on arm64 by making it work with different guest page sizes and testing multiple guest configurations.

Update the test_assert to compare against the UCALL_EXIT_REASON, for portability, as arm64 exits with KVM_EXIT_MMIO while x86 uses KVM_EXIT_IO.

---
 tools/testing/selftests/kvm/Makefile.kvm      |  1 +
 .../selftests/kvm/pre_fault_memory_test.c     | 78 ++++++++++++++-----
 2 files changed, 58 insertions(+), 21 deletions(-)
tools/testing/selftests/kvm/Makefile.kvm +1 -0
index 148d427ff24b..0ddd8db60197 100644
@@ -183,6 +183,7 @@ TEST_GEN_PROGS_arm64 += memslot_perf_test
 TEST_GEN_PROGS_arm64 += mmu_stress_test
 TEST_GEN_PROGS_arm64 += rseq_test
 TEST_GEN_PROGS_arm64 += steal_time
+TEST_GEN_PROGS_arm64 += pre_fault_memory_test
 
 TEST_GEN_PROGS_s390 = $(TEST_GEN_PROGS_COMMON)
 TEST_GEN_PROGS_s390 += s390/memop
tools/testing/selftests/kvm/pre_fault_memory_test.c +57 -21

127 lines not on this page · raw message

Signature · 1 line
2.43.0
#9 lore raw

PATCH v3 3/3 KVM: selftests: Add option for different backing in pre-fault tests

In reply to Jack Thomson

From: Jack Thomson <[email protected]>

Add a -m option to specify different memory backing types for the pre-fault tests (e.g., anonymous, hugetlb), allowing testing of the pre-fault functionality across different memory configurations.

---
 .../selftests/kvm/pre_fault_memory_test.c     | 42 +++++++++++++++----
 1 file changed, 33 insertions(+), 9 deletions(-)
tools/testing/selftests/kvm/pre_fault_memory_test.c +33 -9
index 674931e7bb3a..e1111c4df748 100644
@@ -172,6 +172,7 @@ static void pre_fault_memory(struct kvm_vcpu *vcpu, u64 base_gpa, u64 offset,
 struct test_params {
 	unsigned long vm_type;
 	bool private;
+	enum vm_mem_backing_src_type mem_backing_src;
 };
 
 static void __test_pre_fault_memory(enum vm_guest_mode guest_mode, void *arg)
@@ -190,14 +191,19 @@ static void __test_pre_fault_memory(enum vm_guest_mode guest_mode, void *arg)
 	uint64_t guest_test_virt_mem;
 	uint64_t alignment, guest_page_size;
 
+	size_t backing_src_pagesz = get_backing_src_pagesz(p->mem_backing_src);
+
 	pr_info("Testing guest mode: %s\n", vm_guest_mode_string(guest_mode));
+	pr_info("Testing memory backing src type: %s\n",
+		vm_mem_backing_src_alias(p->mem_backing_src)->name);
 
 	vm = vm_create_shape_with_one_vcpu(shape, &vcpu, guest_code);
 
 	guest_page_size = vm_guest_mode_params[guest_mode].page_size;
 
 	test_config.page_size = guest_page_size;
-	test_config.test_size = TEST_BASE_SIZE + test_config.page_size;
+	test_config.test_size = align_up(TEST_BASE_SIZE + test_config.page_size,
+					 backing_src_pagesz);
 	test_config.test_num_pages = vm_calc_num_guest_pages(vm->mode, test_config.test_size);
 
 	guest_test_phys_mem = (vm->max_gfn - test_config.test_num_pages) * test_config.page_size;
@@ -206,20 +212,23 @@ static void __test_pre_fault_memory(enum vm_guest_mode guest_mode, void *arg)
 #else
 	alignment = SZ_2M;
 #endif
+	alignment = max(alignment, backing_src_pagesz);
 	guest_test_phys_mem = align_down(guest_test_phys_mem, alignment);
 	guest_test_virt_mem = guest_test_phys_mem & ((1ULL << (vm->va_bits - 1)) - 1);
 
-	vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS,
+	vm_userspace_mem_region_add(vm, p->mem_backing_src,
 				    guest_test_phys_mem, TEST_SLOT, test_config.test_num_pages,
 				    p->private ? KVM_MEM_GUEST_MEMFD : 0);
 	virt_map(vm, guest_test_virt_mem, guest_test_phys_mem, test_config.test_num_pages);
 
 	if (p->private)
 		vm_mem_set_private(vm, guest_test_phys_mem, test_config.test_size);
-	pre_fault_memory(vcpu, guest_test_phys_mem, TEST_BASE_SIZE, 0, p->private);
+
+	pre_fault_memory(vcpu, guest_test_phys_mem, test_config.test_size, 0, p->private);
 	/* Test pre-faulting over an already faulted range */
-	pre_fault_memory(vcpu, guest_test_phys_mem, TEST_BASE_SIZE, 0, p->private);
-	pre_fault_memory(vcpu, guest_test_phys_mem + TEST_BASE_SIZE,
+	pre_fault_memory(vcpu, guest_test_phys_mem, test_config.test_size, 0, p->private);
+	pre_fault_memory(vcpu, guest_test_phys_mem +
+			 test_config.test_size - test_config.page_size,
 			 test_config.page_size * 2, test_config.page_size, p->private);
 	pre_fault_memory(vcpu, guest_test_phys_mem + test_config.test_size,
 			 test_config.page_size, test_config.page_size, p->private);
@@ -251,7 +260,8 @@ static void __test_pre_fault_memory(enum vm_guest_mode guest_mode, void *arg)
 	kvm_vm_free(vm);
 }
 
-static void test_pre_fault_memory(unsigned long vm_type, bool private)
+static void test_pre_fault_memory(unsigned long vm_type, enum vm_mem_backing_src_type backing_src,
+				  bool private)
 {
 	if (vm_type && !(kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(vm_type))) {
 		pr_info("Skipping tests for vm_type 0x%lx\n", vm_type);
@@ -261,6 +271,7 @@ static void test_pre_fault_memory(unsigned long vm_type, bool private)
 	struct test_params p = {
 		.vm_type = vm_type,
 		.private = private,
+		.mem_backing_src = backing_src,
 	};
 
 	for_each_guest_mode(__test_pre_fault_memory, &p);
@@ -270,10 +281,23 @@ int main(int argc, char *argv[])
 {
 	TEST_REQUIRE(kvm_check_cap(KVM_CAP_PRE_FAULT_MEMORY));
 
-	test_pre_fault_memory(0, false);
+	int opt;
+	enum vm_mem_backing_src_type backing = VM_MEM_SRC_ANONYMOUS;
+
+	while ((opt = getopt(argc, argv, "m:")) != -1) {
+		switch (opt) {
+		case 'm':
+			backing = parse_backing_src_type(optarg);
+			break;
+		default:
+			break;
+		}
+	}
+
+	test_pre_fault_memory(0, backing, false);
 #ifdef __x86_64__
-	test_pre_fault_memory(KVM_X86_SW_PROTECTED_VM, false);
-	test_pre_fault_memory(KVM_X86_SW_PROTECTED_VM, true);
+	test_pre_fault_memory(KVM_X86_SW_PROTECTED_VM, backing, false);
+	test_pre_fault_memory(KVM_X86_SW_PROTECTED_VM, backing, true);
 #endif
 	return 0;
 }
Signature · 1 line
2.43.0
Tested-by on v3 1/3
#2 lore raw

Re: [PATCH v3 1/3] KVM: arm64: Add pre_fault_memory implementation

In reply to Jack Thomson

Hi Jack,

Quoted 6 lines · Jack Thomson

From: Jack Thomson <[email protected]>

Add kvm_arch_vcpu_pre_fault_memory() for arm64. The implementation hands off the stage-2 faulting logic to either gmem_abort() or user_mem_abort().

Quoted 6 lines

Add an optional page_size output parameter to user_mem_abort() to return the VMA page size, which is needed when pre-faulting.

Update the documentation to clarify x86 specific behaviour.

It works quite well for a few cases I have, so FWIW

Cheers
Vladimir

maintainer
#3 lore raw

Re: [PATCH v3 1/3] KVM: arm64: Add pre_fault_memory implementation

In reply to Jack Thomson
Quoted 51 lines · Jack Thomson

From: Jack Thomson <[email protected]>

Add kvm_arch_vcpu_pre_fault_memory() for arm64. The implementation hands off the stage-2 faulting logic to either gmem_abort() or user_mem_abort().

Add an optional page_size output parameter to user_mem_abort() to return the VMA page size, which is needed when pre-faulting.

Update the documentation to clarify x86 specific behaviour.

---
 Documentation/virt/kvm/api.rst |  3 +-
 arch/arm64/kvm/Kconfig         |  1 +
 arch/arm64/kvm/arm.c           |  1 +
 arch/arm64/kvm/mmu.c           | 73 ++++++++++++++++++++++++++++++++--
 4 files changed, 73 insertions(+), 5 deletions(-)
Documentation/virt/kvm/api.rst +2 -1
index 57061fa29e6a..30872d080511 100644
@@ -6493,7 +6493,8 @@ Errors:
 KVM_PRE_FAULT_MEMORY populates KVM's stage-2 page tables used to map memory
 for the current vCPU state.  KVM maps memory as if the vCPU generated a
 stage-2 read page fault, e.g. faults in memory as needed, but doesn't break
-CoW.  However, KVM does not mark any newly created stage-2 PTE as Accessed.
+CoW.  However, on x86, KVM does not mark any newly created stage-2 PTE as
+Accessed.
 
 In the case of confidential VM types where there is an initial set up of
 private guest memory before the guest is 'finalized'/measured, this ioctl
arch/arm64/kvm/Kconfig +1 -0
index 4f803fd1c99a..6872aaabe16c 100644
@@ -25,6 +25,7 @@ menuconfig KVM
 	select HAVE_KVM_CPU_RELAX_INTERCEPT
 	select KVM_MMIO
 	select KVM_GENERIC_DIRTYLOG_READ_PROTECT
+	select KVM_GENERIC_PRE_FAULT_MEMORY
 	select VIRT_XFER_TO_GUEST_WORK
 	select KVM_VFIO
 	select HAVE_KVM_DIRTY_RING_ACQ_REL
arch/arm64/kvm/arm.c +0 -0
index 870953b4a8a7..88c5dc2b4ee8 100644

arch/arm64/kvm/arm.c int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) · line 331

Quoted 5 lines
arch/arm64/kvm/arm.c +1 -0
@@ -327,6 +327,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
 	case KVM_CAP_IRQFD_RESAMPLE:
 	case KVM_CAP_COUNTER_OFFSET:
 	case KVM_CAP_ARM_WRITABLE_IMP_ID_REGS:
+	case KVM_CAP_PRE_FAULT_MEMORY:
 		r = 1;

How does with pKVM, where the host is not in charge of dealing with stage-2?

arch/arm64/kvm/mmu.c static int gmem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, · line 1602

Quoted 13 lines
arch/arm64/kvm/arm.c +0 -0
 		break;
 	case KVM_CAP_SET_GUEST_DEBUG2:
arch/arm64/kvm/mmu.c +1 -2
index 7cc964af8d30..cba09168fc6d 100644
@@ -1599,8 +1599,8 @@ static int gmem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 
 static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 			  struct kvm_s2_trans *nested,
-			  struct kvm_memory_slot *memslot, unsigned long hva,
-			  bool fault_is_perm)
+			  struct kvm_memory_slot *memslot, long *page_size,

Why is page_size a signed type? A page size is never negative.

arch/arm64/kvm/mmu.c static int gmem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, · line 1603

Quoted 1 line
arch/arm64/kvm/mmu.c +1 -0
+			  unsigned long hva, bool fault_is_perm)

I really wish we'd stop adding parameters to this function, as it has long stopped being readable. It would make a lot more sense if we passed a descriptor for the fault, containing the ipa, hva, memslot and fault type.

Quoted 31 lines
arch/arm64/kvm/mmu.c +8 -2
 {
 	int ret = 0;
 	bool topup_memcache;
@@ -1878,6 +1878,9 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 	kvm_release_faultin_page(kvm, page, !!ret, writable);
 	kvm_fault_unlock(kvm);
 
+	if (page_size)
+		*page_size = vma_pagesize;
+
 	/* Mark the page dirty only if the fault is handled successfully */
 	if (writable && !ret)
 		mark_page_dirty_in_slot(kvm, memslot, gfn);
@@ -2080,8 +2083,8 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu)
 		ret = gmem_abort(vcpu, fault_ipa, nested, memslot,
 				 esr_fsc_is_permission_fault(esr));
 	else
-		ret = user_mem_abort(vcpu, fault_ipa, nested, memslot, hva,
-				     esr_fsc_is_permission_fault(esr));
+		ret = user_mem_abort(vcpu, fault_ipa, nested, memslot, NULL,
+				     hva, esr_fsc_is_permission_fault(esr));
 	if (ret == 0)
 		ret = 1;
 out:
@@ -2457,3 +2460,65 @@ void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled)
 
 	trace_kvm_toggle_cache(*vcpu_pc(vcpu), was_enabled, now_enabled);
 }
+
+long kvm_arch_vcpu_pre_fault_memory(struct kvm_vcpu *vcpu,
+				    struct kvm_pre_fault_memory *range)

arch/arm64/kvm/mmu.c void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled) · line 2475

Quoted 10 lines
arch/arm64/kvm/mmu.c +10 -0
+{
+	int ret, idx;
+	hva_t hva;
+	phys_addr_t end;
+	struct kvm_memory_slot *memslot;
+	struct kvm_vcpu_fault_info stored_fault, *fault_info;
+
+	long page_size = PAGE_SIZE;
+	phys_addr_t ipa = range->gpa;
+	gfn_t gfn = gpa_to_gfn(range->gpa);

nit: Please order this in a more readable way, preferably with long line first.

arch/arm64/kvm/mmu.c void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled) · line 2477

Quoted 2 lines
arch/arm64/kvm/mmu.c +2 -0
+
+	idx = srcu_read_lock(&vcpu->kvm->srcu);

??? Aren't we already guaranteed to be under the SRCU read lock?

Quoted 4 lines
arch/arm64/kvm/mmu.c +4 -0
+
+	if (ipa >= kvm_phys_size(vcpu->arch.hw_mmu)) {
+		ret = -ENOENT;
+		goto out_unlock;

arch/arm64/kvm/mmu.c void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled) · line 2491

Quoted 10 lines
arch/arm64/kvm/mmu.c +10 -0
+	}
+
+	memslot = gfn_to_memslot(vcpu->kvm, gfn);
+	if (!memslot) {
+		ret = -ENOENT;
+		goto out_unlock;
+	}
+
+	fault_info = &vcpu->arch.fault;
+	stored_fault = *fault_info;

If this is a vcpu ioctl, can the fault information be actually valid while userspace is issuing an ioctl? Wouldn't that mean that we are exiting to userspace in the middle of handling an exception?

arch/arm64/kvm/mmu.c void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled) · line 2495

Quoted 4 lines
arch/arm64/kvm/mmu.c +4 -0
+
+	/* Generate a synthetic abort for the pre-fault address */
+	fault_info->esr_el2 = ESR_ELx_EC_DABT_LOW;
+	fault_info->esr_el2 &= ~ESR_ELx_ISV;

You are constructing this from scratch. How can ISV be set?

Quoted 11 lines
arch/arm64/kvm/mmu.c +11 -0
+	fault_info->esr_el2 |= ESR_ELx_FSC_FAULT_L(KVM_PGTABLE_LAST_LEVEL);
+
+	fault_info->hpfar_el2 = HPFAR_EL2_NS |
+		FIELD_PREP(HPFAR_EL2_FIPA, ipa >> 12);
+
+	if (kvm_slot_has_gmem(memslot)) {
+		ret = gmem_abort(vcpu, ipa, NULL, memslot, false);
+	} else {
+		hva = gfn_to_hva_memslot_prot(memslot, gfn, NULL);
+		if (kvm_is_error_hva(hva)) {
+			ret = -EFAULT;

arch/arm64/kvm/mmu.c void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled) · line 2516

Quoted 10 lines
arch/arm64/kvm/mmu.c +10 -0
+			goto out;
+		}
+		ret = user_mem_abort(vcpu, ipa, NULL, memslot, &page_size, hva,
+				     false);
+	}
+
+	if (ret < 0)
+		goto out;
+
+	end = (range->gpa & ~(page_size - 1)) + page_size;

This suspiciously looks like one of the __ALIGN_KERNEL* macros.

arch/arm64/kvm/mmu.c void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled) · line 2524

Quoted 8 lines
arch/arm64/kvm/mmu.c +8 -0
+	ret = min(range->size, end - range->gpa);
+
+out:
+	*fault_info = stored_fault;
+out_unlock:
+	srcu_read_unlock(&vcpu->kvm->srcu, idx);
+	return ret;
+}

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
maintainer
#4 lore raw

Re: [PATCH v3 1/3] KVM: arm64: Add pre_fault_memory implementation

Quoted 4 lines · Marc Zyngier
Quoted 1 line · Jack Thomson

[...]

Quoted 6 lines
Quoted 6 lines
arch/arm64/kvm/mmu.c +6 -0
+	fault_info->hpfar_el2 = HPFAR_EL2_NS |
+		FIELD_PREP(HPFAR_EL2_FIPA, ipa >> 12);
+
+	if (kvm_slot_has_gmem(memslot)) {
+		ret = gmem_abort(vcpu, ipa, NULL, memslot, false);
+	} else {
Quoted 8 lines

arch/arm64/kvm/mmu.c void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled) · line 2511

Quoted 8 lines
arch/arm64/kvm/mmu.c +8 -0
+		hva = gfn_to_hva_memslot_prot(memslot, gfn, NULL);
+		if (kvm_is_error_hva(hva)) {
+			ret = -EFAULT;
+			goto out;
+		}
+		ret = user_mem_abort(vcpu, ipa, NULL, memslot, &page_size, hva,
+				     false);
+	}

And thinking of it a bit more, this is completely broken. What happens if the vcpu is in a nested context? You just populate random pages in an IPA space that is not relevant at all, corrupting the guest state.

You must correctly handle the context the vcpu is in, instead of assuming that this is the canonical context. This means going via the *guest's* S2 translation, just like handle_mem_abort() does.

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#5 lore raw

Re: [PATCH v3 1/3] KVM: arm64: Add pre_fault_memory implementation

Quoted 28 lines · Marc Zyngier
Quoted 4 lines · Marc Zyngier
Quoted 1 line · Jack Thomson

[...]

Quoted 6 lines
Quoted 6 lines
arch/arm64/kvm/mmu.c +6 -0
+	fault_info->hpfar_el2 = HPFAR_EL2_NS |
+		FIELD_PREP(HPFAR_EL2_FIPA, ipa >> 12);
+
+	if (kvm_slot_has_gmem(memslot)) {
+		ret = gmem_abort(vcpu, ipa, NULL, memslot, false);
+	} else {
Quoted 8 lines

arch/arm64/kvm/mmu.c void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled) · line 2511

Quoted 8 lines
arch/arm64/kvm/mmu.c +8 -0
+		hva = gfn_to_hva_memslot_prot(memslot, gfn, NULL);
+		if (kvm_is_error_hva(hva)) {
+			ret = -EFAULT;
+			goto out;
+		}
+		ret = user_mem_abort(vcpu, ipa, NULL, memslot, &page_size, hva,
+				     false);
+	}

And thinking of it a bit more, this is completely broken. What happens if the vcpu is in a nested context? You just populate random pages in an IPA space that is not relevant at all, corrupting the guest state.

Quoted 6 lines

You must correctly handle the context the vcpu is in, instead of assuming that this is the canonical context. This means going via the *guest's* S2 translation, just like handle_mem_abort() does.

	M.

Thanks Marc for taking a look, I'll update to fix these issues and address the other comments.

Signature · 2 lines
Thanks,
Jack
#8 lore raw

Re: [PATCH v3 2/3] KVM: selftests: Enable pre_fault_memory_test for arm64

In reply to Jack Thomson
Quoted 32 lines · Jack Thomson

From: Jack Thomson <[email protected]>

Enable the pre_fault_memory_test to run on arm64 by making it work with different guest page sizes and testing multiple guest configurations.

Update the test_assert to compare against the UCALL_EXIT_REASON, for portability, as arm64 exits with KVM_EXIT_MMIO while x86 uses KVM_EXIT_IO.

---
 tools/testing/selftests/kvm/Makefile.kvm      |  1 +
 .../selftests/kvm/pre_fault_memory_test.c     | 78 ++++++++++++++-----
 2 files changed, 58 insertions(+), 21 deletions(-)
tools/testing/selftests/kvm/Makefile.kvm +1 -0
index 148d427ff24b..0ddd8db60197 100644
@@ -183,6 +183,7 @@ TEST_GEN_PROGS_arm64 += memslot_perf_test
 TEST_GEN_PROGS_arm64 += mmu_stress_test
 TEST_GEN_PROGS_arm64 += rseq_test
 TEST_GEN_PROGS_arm64 += steal_time
+TEST_GEN_PROGS_arm64 += pre_fault_memory_test
 
 TEST_GEN_PROGS_s390 = $(TEST_GEN_PROGS_COMMON)
 TEST_GEN_PROGS_s390 += s390/memop
tools/testing/selftests/kvm/pre_fault_memory_test.c +0 -0
index f04768c1d2e4..674931e7bb3a 100644

tools/testing/selftests/kvm/pre_fault_memory_test.c line 17

Quoted 8 lines
tools/testing/selftests/kvm/pre_fault_memory_test.c +1 -2
@@ -11,19 +11,29 @@
 #include <kvm_util.h>
 #include <processor.h>
 #include <pthread.h>
+#include <guest_modes.h>
 
 /* Arbitrarily chosen values */
-#define TEST_SIZE		(SZ_2M + PAGE_SIZE)
-#define TEST_NPAGES		(TEST_SIZE / PAGE_SIZE)

After applying on top of the base commit 8a4821412cf2c1429fffa07c012dd150f2edf78c, it does not build.

That TEST_NPAGES seems to still be used in delete_slot_worker(). I believe that's because of

"KVM: selftests: Test prefault memory during concurrent memslot removal"

Related issues with the pre_fault_memory() prototype.

Is that the correct base-commit or shall I use something else?

Quoted 3 lines
tools/testing/selftests/kvm/pre_fault_memory_test.c +1 -0
+#define TEST_BASE_SIZE		SZ_2M
 #define TEST_SLOT		10
 

[...]

#6 lore raw

Re: [PATCH v3 1/3] KVM: arm64: Add pre_fault_memory implementation

In reply to Marc Zyngier
Quoted 53 lines · Marc Zyngier
Quoted 51 lines · Jack Thomson

From: Jack Thomson <[email protected]>

Add kvm_arch_vcpu_pre_fault_memory() for arm64. The implementation hands off the stage-2 faulting logic to either gmem_abort() or user_mem_abort().

Add an optional page_size output parameter to user_mem_abort() to return the VMA page size, which is needed when pre-faulting.

Update the documentation to clarify x86 specific behaviour.

---
  Documentation/virt/kvm/api.rst |  3 +-
  arch/arm64/kvm/Kconfig         |  1 +
  arch/arm64/kvm/arm.c           |  1 +
  arch/arm64/kvm/mmu.c           | 73 ++++++++++++++++++++++++++++++++--
  4 files changed, 73 insertions(+), 5 deletions(-)
Documentation/virt/kvm/api.rst +2 -1
index 57061fa29e6a..30872d080511 100644
@@ -6493,7 +6493,8 @@ Errors:
  KVM_PRE_FAULT_MEMORY populates KVM's stage-2 page tables used to map memory
  for the current vCPU state.  KVM maps memory as if the vCPU generated a
  stage-2 read page fault, e.g. faults in memory as needed, but doesn't break
-CoW.  However, KVM does not mark any newly created stage-2 PTE as Accessed.
+CoW.  However, on x86, KVM does not mark any newly created stage-2 PTE as
+Accessed.
 
  In the case of confidential VM types where there is an initial set up of
  private guest memory before the guest is 'finalized'/measured, this ioctl
arch/arm64/kvm/Kconfig +1 -0
index 4f803fd1c99a..6872aaabe16c 100644
@@ -25,6 +25,7 @@ menuconfig KVM
  	select HAVE_KVM_CPU_RELAX_INTERCEPT
  	select KVM_MMIO
  	select KVM_GENERIC_DIRTYLOG_READ_PROTECT
+	select KVM_GENERIC_PRE_FAULT_MEMORY
  	select VIRT_XFER_TO_GUEST_WORK
  	select KVM_VFIO
  	select HAVE_KVM_DIRTY_RING_ACQ_REL
arch/arm64/kvm/arm.c +0 -0
index 870953b4a8a7..88c5dc2b4ee8 100644
@@ -327,6 +327,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Quoted 8 lines

arch/arm64/kvm/arm.c int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) · line 331

Quoted 5 lines
arch/arm64/kvm/arm.c +1 -0
  	case KVM_CAP_IRQFD_RESAMPLE:
  	case KVM_CAP_COUNTER_OFFSET:
  	case KVM_CAP_ARM_WRITABLE_IMP_ID_REGS:
+	case KVM_CAP_PRE_FAULT_MEMORY:
  		r = 1;

How does with pKVM, where the host is not in charge of dealing with stage-2?

For the pKVM case would

    if (vcpu_is_protected(vcpu))
        return -EPERM;

be the appropriate way to handle this?

Quoted 16 lines

arch/arm64/kvm/mmu.c static int gmem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, · line 1602

Quoted 13 lines
arch/arm64/kvm/arm.c +0 -0
  		break;
  	case KVM_CAP_SET_GUEST_DEBUG2:
arch/arm64/kvm/mmu.c +1 -2
index 7cc964af8d30..cba09168fc6d 100644
@@ -1599,8 +1599,8 @@ static int gmem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
 
  static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
  			  struct kvm_s2_trans *nested,
-			  struct kvm_memory_slot *memslot, unsigned long hva,
-			  bool fault_is_perm)
+			  struct kvm_memory_slot *memslot, long *page_size,

Why is page_size a signed type? A page size is never negative.

Quoted 6 lines

arch/arm64/kvm/mmu.c static int gmem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, · line 1603

Quoted 1 line
arch/arm64/kvm/mmu.c +1 -0
+			  unsigned long hva, bool fault_is_perm)

I really wish we'd stop adding parameters to this function, as it has long stopped being readable. It would make a lot more sense if we passed a descriptor for the fault, containing the ipa, hva, memslot and fault type.

I found a patch series which looks to address this [1]. Would you like this fixed for this series?

[1] https://lore.kernel.org/linux-arm-kernel/[email protected]/

Quoted 113 lines
Quoted 31 lines
arch/arm64/kvm/mmu.c +8 -2
  {
  	int ret = 0;
  	bool topup_memcache;
@@ -1878,6 +1878,9 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,
  	kvm_release_faultin_page(kvm, page, !!ret, writable);
  	kvm_fault_unlock(kvm);
 
+	if (page_size)
+		*page_size = vma_pagesize;
+
  	/* Mark the page dirty only if the fault is handled successfully */
  	if (writable && !ret)
  		mark_page_dirty_in_slot(kvm, memslot, gfn);
@@ -2080,8 +2083,8 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu)
  		ret = gmem_abort(vcpu, fault_ipa, nested, memslot,
  				 esr_fsc_is_permission_fault(esr));
  	else
-		ret = user_mem_abort(vcpu, fault_ipa, nested, memslot, hva,
-				     esr_fsc_is_permission_fault(esr));
+		ret = user_mem_abort(vcpu, fault_ipa, nested, memslot, NULL,
+				     hva, esr_fsc_is_permission_fault(esr));
  	if (ret == 0)
  		ret = 1;
  out:
@@ -2457,3 +2460,65 @@ void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled)
 
  	trace_kvm_toggle_cache(*vcpu_pc(vcpu), was_enabled, now_enabled);
  }
+
+long kvm_arch_vcpu_pre_fault_memory(struct kvm_vcpu *vcpu,
+				    struct kvm_pre_fault_memory *range)

arch/arm64/kvm/mmu.c void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled) · line 2475

Quoted 10 lines
arch/arm64/kvm/mmu.c +10 -0
+{
+	int ret, idx;
+	hva_t hva;
+	phys_addr_t end;
+	struct kvm_memory_slot *memslot;
+	struct kvm_vcpu_fault_info stored_fault, *fault_info;
+
+	long page_size = PAGE_SIZE;
+	phys_addr_t ipa = range->gpa;
+	gfn_t gfn = gpa_to_gfn(range->gpa);

nit: Please order this in a more readable way, preferably with long line first.

arch/arm64/kvm/mmu.c void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled) · line 2477

Quoted 2 lines
arch/arm64/kvm/mmu.c +2 -0
+
+	idx = srcu_read_lock(&vcpu->kvm->srcu);

??? Aren't we already guaranteed to be under the SRCU read lock?

Quoted 4 lines
arch/arm64/kvm/mmu.c +4 -0
+
+	if (ipa >= kvm_phys_size(vcpu->arch.hw_mmu)) {
+		ret = -ENOENT;
+		goto out_unlock;

arch/arm64/kvm/mmu.c void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled) · line 2491

Quoted 10 lines
arch/arm64/kvm/mmu.c +10 -0
+	}
+
+	memslot = gfn_to_memslot(vcpu->kvm, gfn);
+	if (!memslot) {
+		ret = -ENOENT;
+		goto out_unlock;
+	}
+
+	fault_info = &vcpu->arch.fault;
+	stored_fault = *fault_info;

If this is a vcpu ioctl, can the fault information be actually valid while userspace is issuing an ioctl? Wouldn't that mean that we are exiting to userspace in the middle of handling an exception?

arch/arm64/kvm/mmu.c void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled) · line 2495

Quoted 4 lines
arch/arm64/kvm/mmu.c +4 -0
+
+	/* Generate a synthetic abort for the pre-fault address */
+	fault_info->esr_el2 = ESR_ELx_EC_DABT_LOW;
+	fault_info->esr_el2 &= ~ESR_ELx_ISV;

You are constructing this from scratch. How can ISV be set?

Quoted 11 lines
arch/arm64/kvm/mmu.c +11 -0
+	fault_info->esr_el2 |= ESR_ELx_FSC_FAULT_L(KVM_PGTABLE_LAST_LEVEL);
+
+	fault_info->hpfar_el2 = HPFAR_EL2_NS |
+		FIELD_PREP(HPFAR_EL2_FIPA, ipa >> 12);
+
+	if (kvm_slot_has_gmem(memslot)) {
+		ret = gmem_abort(vcpu, ipa, NULL, memslot, false);
+	} else {
+		hva = gfn_to_hva_memslot_prot(memslot, gfn, NULL);
+		if (kvm_is_error_hva(hva)) {
+			ret = -EFAULT;

arch/arm64/kvm/mmu.c void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled) · line 2516

Quoted 10 lines
arch/arm64/kvm/mmu.c +10 -0
+			goto out;
+		}
+		ret = user_mem_abort(vcpu, ipa, NULL, memslot, &page_size, hva,
+				     false);
+	}
+
+	if (ret < 0)
+		goto out;
+
+	end = (range->gpa & ~(page_size - 1)) + page_size;

This suspiciously looks like one of the __ALIGN_KERNEL* macros.

arch/arm64/kvm/mmu.c void kvm_toggle_cache(struct kvm_vcpu *vcpu, bool was_enabled) · line 2524

Quoted 8 lines
arch/arm64/kvm/mmu.c +8 -0
+	ret = min(range->size, end - range->gpa);
+
+out:
+	*fault_info = stored_fault;
+out_unlock:
+	srcu_read_unlock(&vcpu->kvm->srcu, idx);
+	return ret;
+}

Thanks,

	M.
Signature · 2 lines
Thanks,
Jack