Skip to contentSkip to conversation

KVM: arm64: Don't load hardware vCPU state before first run

PATCH New Jia He 2 participants 5 messages Dec 4

Subsystems (4)
lore
Share & tools
lore

Jia He

author
#0 lore raw

v2 cover KVM: arm64: Don't load hardware vCPU state before first run

KVM_PRE_FAULT_MEMORY and KVM_SET_VCPU_EVENTS call vcpu_load() before the first KVM_RUN, while arm64 only finalises VM and vCPU state on the first KVM_RUN. On NV, this lets userspace queue KVM_REQ_MAP_L1_VNCR_EL2 without a vncr_tlb and dereference NULL in kvm_map_l1_vncr() on the first KVM_RUN. With 5651590be08a ("KVM: arm64: Load the vcpu in KVM_SET_VCPU_EVENTS"), an all-zero KVM_SET_VCPU_EVENTS before the first KVM_RUN is enough, even with pre-faulting disabled.

Patch 1 skips the arch vcpu_load()/vcpu_put() callbacks until the vCPU has run once, and switches VHE enter_vmid_context() to __hyp_running_vcpu. Patch 2 extends nv_pre_fault_memory_test.

KVM_SET_VCPU_EVENTS before the first run still evaluates injection against unfinalised state; that is left for a separate change.

Tested on a Neoverse-N2 host (with lockdep enabled):
- With pre-faulting disabled, an all-zero KVM_SET_VCPU_EVENTS before the first KVM_RUN reproduces the NULL vncr_tlb dereference.
- With this series applied, neither KVM_PRE_FAULT_MEMORY nor KVM_SET_VCPU_EVENTS triggers the oops (10 runs each), and nv_pre_fault_memory_test passes.

Based on kvmarm/fixes (kvmarm-fixes-7.4-3).

Changes since v1:
- Rebased on kvmarm/fixes.
- Patch 1: cover KVM_SET_VCPU_EVENTS as well.
- Patch 1: keep the VHE enter_vmid_context() change and explain the MMU notifier/reclaim case (Marc, Lorenzo).
- Patch 1: drop the vgic_maintenance_handler() change (Marc, Lorenzo).
- Patch 1: tighten commit message and move test notes to cover letter.
- Drop v1 patch 2 (Marc).
- Patch 2: list the test cases in the commit message, fix two comments and a message (Yao Yuan).

v1: https://lore.kernel.org/r/[email protected]

Jia He (2):
KVM: arm64: Don't load hardware vCPU state before first run
KVM: selftests: arm64: Test KVM_PRE_FAULT_MEMORY before first KVM_RUN

 arch/arm64/kvm/arm.c                          |  17 +-
 arch/arm64/kvm/hyp/vhe/tlb.c                  |   3 +-
 .../kvm/arm64/nv_pre_fault_memory_test.c      | 338 ++++++++++++++++--
 3 files changed, 318 insertions(+), 40 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v2 1/2 KVM: arm64: Don't load hardware vCPU state before first run

KVM_PRE_FAULT_MEMORY and KVM_SET_VCPU_EVENTS call vcpu_load() before the first KVM_RUN, whereas arm64 only initialises sysreg_masks, trap configuration, vncr_tlb and nested vGIC state on the first KVM_RUN in kvm_arch_vcpu_run_pid_change().

Running the full vcpu_load() path early reads unsanitised guest sysregs and touches uninitialised nested state. For instance, if userspace clears ID_AA64MMFR4_EL1.NV_frac to 0 (so vncr_tlb is never allocated), sets HCR_EL2.NV=1 at EL1 and calls either ioctl, kvm_vcpu_load_hw_mmu() queues KVM_REQ_MAP_L1_VNCR_EL2. The first KVM_RUN then dereferences a NULL vncr_tlb in kvm_map_l1_vncr() with preemption disabled and kvm->mmu_lock held for read:

  [   73.636014] Unable to handle kernel NULL pointer dereference at virtual address 0000000000000078
  [   73.801972] pc : kvm_map_l1_vncr+0x48/0x2d0
  [   73.884820] Call trace:
  [   73.887253]  kvm_map_l1_vncr+0x48/0x2d0 (P)
  [   73.891422]  check_nested_vcpu_requests+0xb8/0x168
  [   73.896200]  check_vcpu_requests+0x2a0/0x428
  [   73.900458]  kvm_arch_vcpu_ioctl_run+0x2d8/0x6d8
  [   73.905062]  kvm_vcpu_ioctl+0x388/0xaf0

Neither ioctl needs hardware vCPU state loaded before the first run. kvm_arch_vcpu_pre_fault_memory() only populates &kvm->arch.mmu and only needs generic vcpu_load() to set kvm_running_vcpu for dirty-ring tracking. KVM_SET_VCPU_EVENTS has nothing to put before the first run, and the first KVM_RUN loads the updated state.

Skip the arch vcpu_load()/vcpu_put() callbacks until vcpu_has_run_once() is true. In KVM_RUN, vcpu->pid is set under vcpu->mutex after kvm_arch_vcpu_run_pid_change() succeeds and before vcpu_load().

With kvm_running_vcpu now set without VHE state on the CPU, an MMU notifier triggered during pre-faulting (e.g. by concurrent or direct reclaim) for another stage-2 MMU would cause VHE enter_vmid_context()/ exit_vmid_context() to save and load vcpu->arch.hw_mmu even though it was never loaded. Use host_ctxt->__hyp_running_vcpu instead, matching nVHE.

Fixes 34cfa9b3af17 ("KVM: arm64: Implement KVM_PRE_FAULT_MEMORY")
Fixes 5651590be08a ("KVM: arm64: Load the vcpu in KVM_SET_VCPU_EVENTS")
---
 arch/arm64/kvm/arm.c         | 17 +++++++++++++++--
 arch/arm64/kvm/hyp/vhe/tlb.c |  3 ++-
 2 files changed, 17 insertions(+), 3 deletions(-)
arch/arm64/kvm/arm.c +15 -2
index 21cd872ea394..a18e2ee26583 100644
@@ -811,6 +811,16 @@ static void pkvm_vcpu_load(struct kvm_vcpu *vcpu)
 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
 {
 	vcpu->cpu = cpu;
+
+	/*
+	 * KVM_PRE_FAULT_MEMORY and KVM_SET_VCPU_EVENTS call vcpu_load()
+	 * before the first KVM_RUN has finalised the VM and vCPU state in
+	 * kvm_arch_vcpu_run_pid_change(). Neither needs the hardware vCPU
+	 * state on the CPU.
+	 */
+	if (unlikely(!vcpu_has_run_once(vcpu)))
+		return;
+
 	vcpu->arch.vcpu_ops->vcpu_load(vcpu);
 
 	if (!cpumask_test_cpu(cpu, vcpu->kvm->arch.supported_cpus))
@@ -862,8 +872,11 @@ static void pkvm_vcpu_put(struct kvm_vcpu *vcpu)
 
 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
 {
-	vcpu->arch.vcpu_ops->vcpu_put(vcpu);
-	vcpu_clear_on_unsupported_cpu(vcpu);
+	if (likely(vcpu_has_run_once(vcpu))) {
+		vcpu->arch.vcpu_ops->vcpu_put(vcpu);
+		vcpu_clear_on_unsupported_cpu(vcpu);
+	}
+
 	vcpu->cpu = -1;
 }
 
arch/arm64/kvm/hyp/vhe/tlb.c +2 -1
index 33ac9a8c954e..98df0bb0a05c 100644
@@ -20,11 +20,12 @@ struct tlb_inv_context {
 static void enter_vmid_context(struct kvm_s2_mmu *mmu,
 			       struct tlb_inv_context *cxt)
 {
-	struct kvm_vcpu *vcpu = kvm_get_running_vcpu();
+	struct kvm_vcpu *vcpu;
 	u64 val;
 
 	local_irq_save(cxt->flags);
 
+	vcpu = host_data_ptr(host_ctxt)->__hyp_running_vcpu;
 	if (vcpu && mmu != vcpu->arch.hw_mmu)
 		cxt->mmu = vcpu->arch.hw_mmu;
 	else
Signature · 1 line
2.34.1
#2 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] KVM: arm64: kvm_inject_nested() leaks VHE PMU state before first KVM_RUN --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] KVM: arm64: kvm_inject_nested() leaks VHE PMU state before first KVM_RUN
--

commit f447e42779fe8bcecaa4f6aa6298f73ee8e89cf9
Author: Jia He <[email protected]>

KVM: arm64: Don't load hardware vCPU state before first run

This commit skips the arch vcpu_load() and vcpu_put() callbacks until vcpu_has_run_once() is true, and updates VHE's enter_vmid_context() to check host_ctxt->__hyp_running_vcpu instead of kvm_get_running_vcpu().

Quoted 4 lines
arch/arm64/kvm/arm.c +0 -0
index 157bf4b812020..720367868f6c8 100644

[ ... ]

arch/arm64/kvm/arm.c static void pkvm_vcpu_put(struct kvm_vcpu *vcpu) · line 881

Quoted 13 lines
arch/arm64/kvm/arm.c +5 -2
@@ -862,8 +872,11 @@ static void pkvm_vcpu_put(struct kvm_vcpu *vcpu)
 
 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
 {
-	vcpu->arch.vcpu_ops->vcpu_put(vcpu);
-	vcpu_clear_on_unsupported_cpu(vcpu);
+	if (likely(vcpu_has_run_once(vcpu))) {
+		vcpu->arch.vcpu_ops->vcpu_put(vcpu);
+		vcpu_clear_on_unsupported_cpu(vcpu);
+	}
+
 	vcpu->cpu = -1;
 }

[Severity: High]
Can skipping vcpu_ops->vcpu_put() here leak VHE PMU EL0 counter filtering on the host CPU when KVM_SET_VCPU_EVENTS injects a nested exception before the first KVM_RUN?

When KVM_SET_VCPU_EVENTS injects a nested SError (e.g. from L2 EL1h with HCR_EL2.AMO and the cycle counter enabled in PMCNTENSET_EL0), kvm_inject_nested() still calls kvm_pmu_nested_transition() unconditionally whenever kvm_vcpu_has_pmu(vcpu) is true.

kvm_pmu_nested_transition() calls kvm_pmu_create_perf_event() and kvm_vcpu_pmu_restore_guest(), modifying the physical VHE PMU EL0 filter registers. Because kvm_arch_vcpu_put() now skips vcpu_ops->vcpu_put() and kvm_vcpu_pmu_restore_host(), host EL0 PMU filtering is never restored.

In addition, kvm_pmu_create_perf_event() runs before kvm_arm_pmu_v3_init() initializes vcpu->arch.pmu.overflow_work, so an NMI overflow on the leaked event makes kvm_pmu_perf_overflow() queue an uninitialized irq_work and trigger a NULL pointer dereference in irq_work_single().

Guarding kvm_pmu_nested_transition() on vcpu_has_run_once(vcpu) would avoid touching PMU hardware or creating perf events before the first KVM_RUN.

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

PATCH v2 2/2 KVM: selftests: arm64: Test KVM_PRE_FAULT_MEMORY before first KVM_RUN

In reply to Jia He

Extend nv_pre_fault_memory_test to exercise KVM_PRE_FAULT_MEMORY before the first KVM_RUN.

Without EL2:
1. immediate_exit on the first KVM_RUN.
2. Retrying the first KVM_RUN after it fails.
3. Moving the vCPU between two threads around its first run.

With EL2:
1. ID_AA64MMFR4_EL1.NV_frac=0 with HCR_EL2.NV=1 at EL1h (NULL vncr_tlb reproducer).
2. Staged vEL1 context with asymmetric HCR_EL2.IMO/FMO before the first run (triggers a kernel WARN on an unfixed kernel; the test still passes).
3. Thread handoff as above.

---
 .../kvm/arm64/nv_pre_fault_memory_test.c      | 338 ++++++++++++++++--
 1 file changed, 301 insertions(+), 37 deletions(-)
tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c +301 -37
index 09c1db303856..57e3aaeea894 100644
@@ -1,15 +1,13 @@
 // SPDX-License-Identifier: GPL-2.0-only
 /*
- * nv_pre_fault_memory_test - Test KVM_PRE_FAULT_MEMORY on a vCPU whose
- * last-run context is nested.
- *
- * The guest enters vEL2, sets up its EL2 translation configuration into the
- * real EL1 registers then ERETs to vEL1 and exits to userspace so its vCPU
- * last-run context is nested backed by a shadow stage 2 MMU.
- *
- * Assert that pre-faulting ignores that and targets the canonical stage-2
- * page tables only.
+ * nv_pre_fault_memory_test - Test KVM_PRE_FAULT_MEMORY on arm64 before the
+ * first KVM_RUN, across vCPU thread changes, and, with nested virtualisation,
+ * from a live nested context.
  */
+#include <pthread.h>
+#include <sched.h>
+
+#include "kvm_syscalls.h"
 #include "kvm_util.h"
 #include "processor.h"
 #include "test_util.h"
@@ -22,6 +20,18 @@
 #define TEST_MEM_SIZE		SZ_2M
 #define TEST_MEM_GPA		SZ_1G
 
+static void guest_el1_touch_and_done(void)
+{
+	u64 offset;
+
+	GUEST_ASSERT_EQ(get_current_el(), 1);
+
+	for (offset = 0; offset < TEST_MEM_SIZE; offset += SZ_4K)
+		READ_ONCE(*(u64 *)(TEST_MEM_GPA + offset));
+
+	GUEST_DONE();
+}
+
 static void guest_el1_code(void)
 {
 	u64 offset;
@@ -97,22 +107,56 @@ static void pre_fault(struct kvm_vcpu *vcpu, u64 gpa, u64 size)
 	TEST_ASSERT_EQ(range.size, 0);
 }
 
-int main(void)
+static void expect_ucall_sync(struct kvm_vcpu *vcpu, u64 expected)
+{
+	struct ucall uc;
+
+	switch (get_ucall(vcpu, &uc)) {
+	case UCALL_SYNC:
+		TEST_ASSERT_EQ(uc.args[1], expected);
+		break;
+	case UCALL_ABORT:
+		REPORT_GUEST_ASSERT(uc);
+		break;
+	default:
+		TEST_FAIL("Unhandled ucall: %ld", uc.cmd);
+	}
+}
+
+static void expect_ucall_done(struct kvm_vcpu *vcpu)
+{
+	struct ucall uc;
+
+	switch (get_ucall(vcpu, &uc)) {
+	case UCALL_DONE:
+		break;
+	case UCALL_ABORT:
+		REPORT_GUEST_ASSERT(uc);
+		break;
+	default:
+		TEST_FAIL("Unhandled ucall: %ld", uc.cmd);
+	}
+}
+
+static struct kvm_vm *setup_test_vm(bool has_el2, bool has_sve,
+				    void *guest_fn, struct kvm_vcpu **vcpu_out)
 {
 	struct kvm_vcpu_init init;
 	struct kvm_vcpu *vcpu;
 	struct kvm_vm *vm;
-	struct ucall uc;
 	u64 npages;
 
-	TEST_REQUIRE(test_supports_el2());
-	TEST_REQUIRE(kvm_check_cap(KVM_CAP_PRE_FAULT_MEMORY));
-
 	vm = vm_create(1);
 
 	kvm_get_default_vcpu_target(vm, &init);
-	init.features[0] |= BIT(KVM_ARM_VCPU_HAS_EL2);
-	vcpu = aarch64_vcpu_add(vm, 0, &init, guest_code);
+	if (has_el2)
+		init.features[0] |= BIT(KVM_ARM_VCPU_HAS_EL2);
+	else
+		init.features[0] &= ~BIT(KVM_ARM_VCPU_HAS_EL2);
+	if (has_sve)
+		init.features[0] |= BIT(KVM_ARM_VCPU_SVE);
+
+	vcpu = aarch64_vcpu_add(vm, 0, &init, guest_fn);
 	kvm_arch_vm_finalize_vcpus(vm);
 
 	npages = TEST_MEM_SIZE / vm->page_size;
@@ -120,39 +164,259 @@ int main(void)
 				    TEST_MEM_SLOT, npages, 0);
 	virt_map(vm, TEST_MEM_GPA, TEST_MEM_GPA, npages);
 
+	*vcpu_out = vcpu;
+	return vm;
+}
+
+static void test_nested_post_run_pre_fault(void)
+{
+	struct kvm_vcpu *vcpu;
+	struct kvm_vm *vm;
+
+	pr_info("Testing post-run pre-fault from nested vEL1 context\n");
+	vm = setup_test_vm(true, false, guest_code, &vcpu);
+
 	/* Run the guest until it has ERET'd from vEL2 to vEL1. */
 	vcpu_run(vcpu);
-	switch (get_ucall(vcpu, &uc)) {
-	case UCALL_SYNC:
-		TEST_ASSERT_EQ(uc.args[1], 1);
-		break;
-	case UCALL_ABORT:
-		REPORT_GUEST_ASSERT(uc);
-		break;
-	default:
-		TEST_FAIL("Unhandled ucall: %ld", uc.cmd);
-	}
+	expect_ucall_sync(vcpu, 1);
 
 	/*
 	 * The vCPU's last-run context is vEL1, so its hw_mmu is a shadow
-	 * stage-2 MMU.
-	 *
-	 * Pre-faulting must ignore that and populate the canonical stage-2.
+	 * stage-2 MMU. Pre-faulting must ignore that and populate the
+	 * canonical stage-2.
 	 */
 	pre_fault(vcpu, TEST_MEM_GPA, TEST_MEM_SIZE);
 
 	/* Resume at vEL1 and touch the prefaulted range. */
 	vcpu_run(vcpu);
-	switch (get_ucall(vcpu, &uc)) {
-	case UCALL_DONE:
-		break;
-	case UCALL_ABORT:
-		REPORT_GUEST_ASSERT(uc);
-		break;
-	default:
-		TEST_FAIL("Unhandled ucall: %ld", uc.cmd);
+	expect_ucall_done(vcpu);
+
+	kvm_vm_free(vm);
+}
+
+/*
+ * Pre-fault before the first KVM_RUN with ID_AA64MMFR4_EL1.NV_frac == 0
+ * (no vncr_tlb) and HCR_EL2.NV=1 at EL1h. Loading the vCPU state for the
+ * pre-fault used to queue an L1 VNCR mapping request, and the first
+ * KVM_RUN then dereferenced the NULL vncr_tlb.
+ */
+static void test_pre_first_run_no_nv2(void)
+{
+	struct kvm_vcpu *vcpu;
+	struct kvm_vm *vm;
+	u64 val;
+
+	pr_info("Testing pre-first-run pre-fault with NV_frac=0 and HCR_EL2.NV=1\n");
+	vm = setup_test_vm(true, false, guest_el1_touch_and_done, &vcpu);
+
+	/* Copy the EL2 S1 configuration into the EL1 sysregs for vEL1. */
+	vcpu_set_reg(vcpu, KVM_ARM64_SYS_REG(SYS_SCTLR_EL1),
+		     vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(SYS_SCTLR_EL2)));
+	vcpu_set_reg(vcpu, KVM_ARM64_SYS_REG(SYS_TCR_EL1),
+		     vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(SYS_TCR_EL2)));
+	vcpu_set_reg(vcpu, KVM_ARM64_SYS_REG(SYS_TTBR0_EL1),
+		     vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(SYS_TTBR0_EL2)));
+	vcpu_set_reg(vcpu, KVM_ARM64_SYS_REG(SYS_MAIR_EL1),
+		     vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(SYS_MAIR_EL2)));
+	vcpu_set_reg(vcpu, KVM_ARM64_SYS_REG(SYS_CPACR_EL1), 3ULL << 20);
+	vcpu_set_reg(vcpu, KVM_ARM64_SYS_REG(SYS_VBAR_EL1),
+		     vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(SYS_VBAR_EL2)));
+	vcpu_set_reg(vcpu, ARM64_CORE_REG(sp_el1),
+		     vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(SYS_SP_EL2)));
+
+	val = vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(SYS_ID_AA64MMFR4_EL1));
+	vcpu_set_reg(vcpu, KVM_ARM64_SYS_REG(SYS_ID_AA64MMFR4_EL1),
+		     val & ~ID_AA64MMFR4_EL1_NV_frac);
+
+	val = vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(SYS_HCR_EL2));
+	vcpu_set_reg(vcpu, KVM_ARM64_SYS_REG(SYS_HCR_EL2),
+		     (val & ~HCR_EL2_TGE) | HCR_EL2_NV | HCR_EL2_RW);
+	vcpu_set_reg(vcpu, ARM64_CORE_REG(regs.pstate),
+		     PSR_MODE_EL1h | PSR_F_BIT | PSR_I_BIT | PSR_A_BIT | PSR_D_BIT);
+
+	pre_fault(vcpu, TEST_MEM_GPA, TEST_MEM_SIZE);
+
+	vcpu_run(vcpu);
+	expect_ucall_done(vcpu);
+
+	val = vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(SYS_HCR_EL2));
+	TEST_ASSERT(!(val & HCR_EL2_NV),
+		    "Expected HCR_EL2.NV to be masked RES0, got 0x%lx", val);
+
+	kvm_vm_free(vm);
+}
+
+/*
+ * Stage a vEL1 context with asymmetric HCR_EL2.IMO/FMO before the first
+ * KVM_RUN. If pre-faulting loads this context onto the CPU, the nested vGIC
+ * code hits a WARN in the kernel log. The test itself still passes, so this
+ * case needs a kernel log check.
+ */
+static void test_pre_first_run_nested_ctxt(void)
+{
+	u64 orig_hcr, orig_pstate;
+	struct kvm_vcpu *vcpu;
+	struct kvm_vm *vm;
+
+	pr_info("Testing pre-first-run pre-fault with staged vEL1 context\n");
+	vm = setup_test_vm(true, false, guest_code, &vcpu);
+
+	orig_hcr = vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(SYS_HCR_EL2));
+	orig_pstate = vcpu_get_reg(vcpu, ARM64_CORE_REG(regs.pstate));
+
+	vcpu_set_reg(vcpu, KVM_ARM64_SYS_REG(SYS_HCR_EL2),
+		     HCR_EL2_E2H | HCR_EL2_RW | HCR_EL2_VM | HCR_EL2_IMO);
+	vcpu_set_reg(vcpu, ARM64_CORE_REG(regs.pstate),
+		     PSR_MODE_EL1h | PSR_F_BIT | PSR_I_BIT | PSR_A_BIT | PSR_D_BIT);
+
+	pre_fault(vcpu, TEST_MEM_GPA, TEST_MEM_SIZE);
+
+	vcpu_set_reg(vcpu, KVM_ARM64_SYS_REG(SYS_HCR_EL2), orig_hcr);
+	vcpu_set_reg(vcpu, ARM64_CORE_REG(regs.pstate), orig_pstate);
+
+	vcpu_run(vcpu);
+	expect_ucall_sync(vcpu, 1);
+	vcpu_run(vcpu);
+	expect_ucall_done(vcpu);
+
+	kvm_vm_free(vm);
+}
+
+struct handoff_args {
+	struct kvm_vcpu *vcpu;
+	int cpu;
+};
+
+static void maybe_pin_self_to_cpu(int cpu)
+{
+	if (cpu >= 0)
+		pin_self_to_cpu(cpu);
+}
+
+static void *first_run_worker(void *arg)
+{
+	struct handoff_args *h = arg;
+
+	maybe_pin_self_to_cpu(h->cpu);
+	pre_fault(h->vcpu, TEST_MEM_GPA + SZ_1M, TEST_MEM_SIZE - SZ_1M);
+
+	vcpu_run(h->vcpu);
+	expect_ucall_sync(h->vcpu, 1);
+	return NULL;
+}
+
+/*
+ * Pre-fault in the main thread, then pre-fault and run the vCPU for the
+ * first time in a worker thread, then pre-fault and run it again in the
+ * main thread. vcpu->pid changes on both KVM_RUN calls: from NULL to the
+ * worker, then from the worker to the main thread. If at least two CPUs
+ * are available, the two threads are pinned to different CPUs.
+ */
+static void test_thread_handoff(bool has_el2)
+{
+	struct handoff_args h = { .cpu = -1 };
+	struct kvm_vcpu *vcpu;
+	struct kvm_vm *vm;
+	cpu_set_t allowed;
+	int cpu, cpu0 = -1;
+	pthread_t thr;
+
+	pr_info("Testing pre-fault across a thread handoff (%s vCPU)\n",
+		has_el2 ? "EL2" : "EL1");
+
+	CPU_ZERO(&allowed);
+	kvm_pthread_getaffinity(pthread_self(), &allowed);
+	for (cpu = 0; cpu < CPU_SETSIZE; cpu++) {
+		if (!CPU_ISSET(cpu, &allowed))
+			continue;
+		if (cpu0 < 0) {
+			cpu0 = cpu;
+		} else {
+			h.cpu = cpu;
+			break;
+		}
+	}
+	if (h.cpu < 0)
+		pr_info("Only one CPU available, not testing CPU migration\n");
+
+	maybe_pin_self_to_cpu(cpu0);
+	vm = setup_test_vm(has_el2, false,
+			   has_el2 ? guest_code : guest_el1_code, &vcpu);
+	pre_fault(vcpu, TEST_MEM_GPA, SZ_1M);
+
+	h.vcpu = vcpu;
+	kvm_pthread_create(&thr, NULL, first_run_worker, &h);
+	kvm_pthread_join(thr, NULL);
+
+	pre_fault(vcpu, TEST_MEM_GPA, TEST_MEM_SIZE);
+	vcpu_run(vcpu);
+	expect_ucall_done(vcpu);
+
+	kvm_pthread_setaffinity(pthread_self(), &allowed);
+
+	kvm_vm_free(vm);
+}
+
+static void test_pre_first_run_el1(void)
+{
+	int feature = KVM_ARM_VCPU_SVE;
+	struct kvm_vcpu *vcpu;
+	struct kvm_vm *vm;
+	int ret;
+
+	pr_info("Testing pre-fault, then immediate_exit on the first KVM_RUN\n");
+	vm = setup_test_vm(false, false, guest_el1_touch_and_done, &vcpu);
+	pre_fault(vcpu, TEST_MEM_GPA, vm->page_size);
+
+	vcpu->run->immediate_exit = 1;
+	ret = __vcpu_run(vcpu);
+	TEST_ASSERT(ret == -1 && errno == EINTR,
+		    "Expected -EINTR on immediate_exit, got ret=%d errno=%d",
+		    ret, errno);
+	vcpu->run->immediate_exit = 0;
+
+	pre_fault(vcpu, TEST_MEM_GPA, TEST_MEM_SIZE);
+	vcpu_run(vcpu);
+	expect_ucall_done(vcpu);
+	kvm_vm_free(vm);
+
+	if (!kvm_check_cap(KVM_CAP_ARM_SVE)) {
+		pr_info("SVE not supported, skipping failed first KVM_RUN test\n");
+		return;
 	}
 
+	pr_info("Testing pre-fault after a failed first KVM_RUN\n");
+	vm = setup_test_vm(false, true, guest_el1_touch_and_done, &vcpu);
+	pre_fault(vcpu, TEST_MEM_GPA, vm->page_size);
+
+	ret = __vcpu_run(vcpu);
+	TEST_ASSERT(ret == -1 && errno == EPERM,
+		    "Expected -EPERM before SVE finalisation, got ret=%d errno=%d",
+		    ret, errno);
+
+	pre_fault(vcpu, TEST_MEM_GPA + vm->page_size, vm->page_size);
+	vcpu_ioctl(vcpu, KVM_ARM_VCPU_FINALIZE, &feature);
+	vcpu_run(vcpu);
+	expect_ucall_done(vcpu);
 	kvm_vm_free(vm);
+}
+
+int main(void)
+{
+	TEST_REQUIRE(kvm_check_cap(KVM_CAP_PRE_FAULT_MEMORY));
+
+	test_pre_first_run_el1();
+	test_thread_handoff(false);
+
+	if (!test_supports_el2()) {
+		pr_info("EL2 not supported, skipping nested tests\n");
+		return 0;
+	}
+
+	test_nested_post_run_pre_fault();
+	test_pre_first_run_no_nv2();
+	test_pre_first_run_nested_ctxt();
+	test_thread_handoff(true);
+
 	return 0;
 }
Signature · 1 line
2.34.1
#4 lore raw

Re: [PATCH v2 2/2] KVM: selftests: arm64: Test KVM_PRE_FAULT_MEMORY before first KVM_RUN

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] KVM: selftests: test_pre_first_run_el1() uses guest page size for pre-fault --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] KVM: selftests: test_pre_first_run_el1() uses guest page size for pre-fault
--

commit efb55a38dc35f83285043845cf36eea7648a5fbe
Author: Jia He <[email protected]>

KVM: selftests: arm64: Test KVM_PRE_FAULT_MEMORY before first KVM_RUN

This commit extends nv_pre_fault_memory_test to exercise KVM_PRE_FAULT_MEMORY before the first KVM_RUN and across vCPU thread handoffs, both with and without EL2 enabled.

Quoted 4 lines
tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c +0 -0
index 09c1db3038566..57e3aaeea8947 100644

[ ... ]

Quoted 28 lines
tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c +26 -0
+static void test_pre_first_run_el1(void)
+{
+	int feature = KVM_ARM_VCPU_SVE;
+	struct kvm_vcpu *vcpu;
+	struct kvm_vm *vm;
+	int ret;
+
+	pr_info("Testing pre-fault, then immediate_exit on the first KVM_RUN\n");
+	vm = setup_test_vm(false, false, guest_el1_touch_and_done, &vcpu);
+	pre_fault(vcpu, TEST_MEM_GPA, vm->page_size);
+
+	vcpu->run->immediate_exit = 1;
+	ret = __vcpu_run(vcpu);
+	TEST_ASSERT(ret == -1 && errno == EINTR,
+		    "Expected -EINTR on immediate_exit, got ret=%d errno=%d",
+		    ret, errno);
+	vcpu->run->immediate_exit = 0;
+
+	pre_fault(vcpu, TEST_MEM_GPA, TEST_MEM_SIZE);
+	vcpu_run(vcpu);
+	expect_ucall_done(vcpu);
+	kvm_vm_free(vm);
+
+	if (!kvm_check_cap(KVM_CAP_ARM_SVE)) {
+		pr_info("SVE not supported, skipping failed first KVM_RUN test\n");
+		return;
 	}
 

tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c int main(void) · line 397

Quoted 10 lines
tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c +10 -0
+	pr_info("Testing pre-fault after a failed first KVM_RUN\n");
+	vm = setup_test_vm(false, true, guest_el1_touch_and_done, &vcpu);
+	pre_fault(vcpu, TEST_MEM_GPA, vm->page_size);
+
+	ret = __vcpu_run(vcpu);
+	TEST_ASSERT(ret == -1 && errno == EPERM,
+		    "Expected -EPERM before SVE finalisation, got ret=%d errno=%d",
+		    ret, errno);
+
+	pre_fault(vcpu, TEST_MEM_GPA + vm->page_size, vm->page_size);

[Severity: Medium]
Can these pre_fault() calls fail with -EINVAL on arm64 hosts with 16 KiB or 64 KiB pages?

setup_test_vm() creates a VM with vm_create(1), which defaults to VM_MODE_P40V48_4K (a 4 KiB vm->page_size) regardless of the host page size.

kvm_vcpu_pre_fault_memory() requires both range->gpa and range->size to be host PAGE_ALIGNED(). Passing vm->page_size as the size and TEST_MEM_GPA + vm->page_size as the address returns -EINVAL and aborts the test on 16 KiB and 64 KiB host kernels.

Using getpagesize() instead of vm->page_size (as pre_fault_memory_test.c does) would keep the address and size aligned to the host page size.

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