KVM: arm64: Fix pre-first-run KVM_PRE_FAULT_MEMORY vcpu_load() issues
cover KVM: arm64: Fix pre-first-run KVM_PRE_FAULT_MEMORY vcpu_load() issues
KVM_PRE_FAULT_MEMORY calls vcpu_load() as soon as KVM_ARM_VCPU_INIT has completed. On arm64, VM and vCPU state such as kvm->arch.sysreg_masks, trap controls, vncr_tlb and nested vGIC setup is only initialised on the first KVM_RUN in kvm_arch_vcpu_run_pid_change().
Running the full arch vcpu_load() path before then reads unsanitised guest sysregs and touches uninitialised nested state. In particular, if a VMM creates an EL2 vCPU with ID_AA64MMFR4_EL1.NV_frac cleared to 0, sets HCR_EL2.NV=1 and an EL1 PSTATE, and calls KVM_PRE_FAULT_MEMORY, kvm_vcpu_load_hw_mmu() queues KVM_REQ_MAP_L1_VNCR_EL2 without a vncr_tlb. The next KVM_RUN then dereferences NULL in kvm_map_l1_vncr() with preemption disabled and kvm->mmu_lock held for read.
Patch 1 skips the arch vcpu_load()/vcpu_put() callbacks until vcpu_has_run_once() is true, and adjusts VHE enter_vmid_context() and vgic_maintenance_handler() accordingly. Generic vcpu_load() still sets kvm_running_vcpu for dirty-ring tracking, and pre-faulting only populates &kvm->arch.mmu.
Patch 2 checks that vncr_tlb is allocated before setting KVM_REQ_MAP_L1_VNCR_EL2 and adds a WARN_ON_ONCE(!vt) check in kvm_map_l1_vncr().
Patch 3 extends nv_pre_fault_memory_test to cover pre-first-run pre-faulting. The cases that do not need EL2 run on all hosts. On an unfixed kernel, the staged nested context case triggers the "Separate virtual IRQ/FIQ settings not supported" WARN while the test still passes, so the kernel log must be checked as well.
Jia He (3):
KVM: arm64: Don't load hardware vCPU state before first run
KVM: arm64: nv: Don't request or attempt L1 VNCR mapping without
vncr_tlb
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 +- arch/arm64/kvm/nested.c | 6 +- arch/arm64/kvm/vgic/vgic-init.c | 2 +- .../kvm/arm64/nv_pre_fault_memory_test.c | 335 ++++++++++++++++-- 5 files changed, 321 insertions(+), 42 deletions(-)
base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3
Signature · 1 line
2.34.1
PATCH 1/3 KVM: arm64: Don't load hardware vCPU state before first run
KVM_PRE_FAULT_MEMORY calls vcpu_load() as soon as KVM_ARM_VCPU_INIT has completed. On arm64, however, kvm->arch.sysreg_masks, the trap configuration, vncr_tlb and nested vGIC state are only set up on the first KVM_RUN in kvm_arch_vcpu_run_pid_change().
Running the full vcpu_load() path before then reads unsanitised guest sysregs and touches uninitialised nested state: get_s2_mmu_nested() populates nested_mmus[] from raw VTTBR_EL2/VTCR_EL2/HCR_EL2 values, FGT and HCRX_EL2 are written before kvm_calculate_traps() has run, and kvm_vgic_load() can enter vgic_v3_load_nested() early. In particular, if userspace creates an EL2 vCPU with ID_AA64MMFR4_EL1.NV_frac cleared to 0 (so vncr_tlb is never allocated), sets HCR_EL2.NV=1 and an EL1 PSTATE, and calls KVM_PRE_FAULT_MEMORY, kvm_vcpu_load_hw_mmu() queues KVM_REQ_MAP_L1_VNCR_EL2. The next 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
KVM_PRE_FAULT_MEMORY still needs generic vcpu_load() to set kvm_running_vcpu for dirty-ring tracking in mark_page_dirty_in_slot(), but kvm_arch_vcpu_pre_fault_memory() only walks and populates &kvm->arch.mmu and does not use any hardware vCPU 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 right after kvm_arch_vcpu_run_pid_change() succeeds and before kvm_arch_vcpu_ioctl_run() calls vcpu_load().
Since kvm_running_vcpu can now be set without VHE state on the CPU, use host_ctxt->__hyp_running_vcpu in VHE enter_vmid_context() (matching nVHE) and check vcpu_has_run_once() in vgic_maintenance_handler().
--- arch/arm64/kvm/arm.c | 17 +++++++++++++++-- arch/arm64/kvm/hyp/vhe/tlb.c | 3 ++- arch/arm64/kvm/vgic/vgic-init.c | 2 +- 3 files changed, 18 insertions(+), 4 deletions(-)
arch/arm64/kvm/arm.c +15 -2
@@ -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 calls vcpu_load() before the first KVM_RUN has +* finalised the VM and vCPU state in kvm_arch_vcpu_run_pid_change(). +* Pre-faulting only populates the canonical stage-2 and needs no +* 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
@@ -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
arch/arm64/kvm/vgic/vgic-init.c +1 -1
@@ -825,7 +825,7 @@ static irqreturn_t vgic_maintenance_handler(int irq, void *data)* Of course, NV throws a wrench in this plan, and needs * something special. */ -if (vcpu &&vgic_state_is_nested (vcpu)) +if (vcpu &&vcpu_has_run_once (vcpu) &&vgic_state_is_nested (vcpu))vgic_v3_handle_nested_maint_irq (vcpu);return IRQ_HANDLED ;
Signature · 1 line
2.34.1
Marc Zyngier
maintainerRe: [PATCH 1/3] KVM: arm64: Don't load hardware vCPU state before first run
[+ Lorenzo]
Hey Jia,
Quoted 5 lines · Jia He
KVM_PRE_FAULT_MEMORYcallsvcpu_load()as soon asKVM_ARM_VCPU_INIThas completed. On arm64, however,kvm->arch.sysreg_masks, the trap configuration,vncr_tlband nested vGIC state are only set up on the firstKVM_RUNinkvm_arch_vcpu_run_pid_change().
I think I'm going to queue a patch disabling prefaulting immediately, and then we can fix this crap.
Quoted 31 lines
Running the full
vcpu_load()path before then reads unsanitised guest sysregs and touches uninitialised nested state:get_s2_mmu_nested()populates nested_mmus[] from raw VTTBR_EL2/VTCR_EL2/HCR_EL2 values, FGT andHCRX_EL2are written beforekvm_calculate_traps()has run, andkvm_vgic_load()can entervgic_v3_load_nested()early. In particular, if userspace creates an EL2 vCPU with ID_AA64MMFR4_EL1.NV_frac cleared to 0 (sovncr_tlbis never allocated), sets HCR_EL2.NV=1 and an EL1 PSTATE, and callsKVM_PRE_FAULT_MEMORY,kvm_vcpu_load_hw_mmu()queuesKVM_REQ_MAP_L1_VNCR_EL2. The nextKVM_RUNthen dereferences a NULLvncr_tlbinkvm_map_l1_vncr()with preemption disabled andkvm->mmu_lockheld 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
KVM_PRE_FAULT_MEMORYstill needs genericvcpu_load()to setkvm_running_vcpufor dirty-ring tracking inmark_page_dirty_in_slot(), butkvm_arch_vcpu_pre_fault_memory()only walks and populates&kvm->arch.mmuand does not use any hardware vCPU state. Skip the arch vcpu_load()/vcpu_put() callbacks untilvcpu_has_run_once()is true. InKVM_RUN,vcpu->pidis set undervcpu->mutexright afterkvm_arch_vcpu_run_pid_change()succeeds and beforekvm_arch_vcpu_ioctl_run()callsvcpu_load().
Quoted 3 lines
Since
kvm_running_vcpucan now be set without VHE state on the CPU, usehost_ctxt->__hyp_running_vcpuin VHEenter_vmid_context()(matching nVHE)
Can you clarify under which circumstances we end-up on the TLB invalidation path? Simply faulting things in should not result in invalidation. Unless we end-up with MMU notifiers firing?
Quoted 1 line
and check
vcpu_has_run_once()invgic_maintenance_handler().
I have similar questions here. It isn't obvious how you end-up getting a VGIC MI if you don't run and don't load the state on the CPU.
Thanks,
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Re: [PATCH 1/3] KVM: arm64: Don't load hardware vCPU state before first run
Quoted 7 lines · Marc Zyngier
[+ Lorenzo]
Hey Jia,
Quoted 1 line · Jia He
Quoted 7 lines
Quoted 4 lines
KVM_PRE_FAULT_MEMORYcallsvcpu_load()as soon asKVM_ARM_VCPU_INIThas completed. On arm64, however,kvm->arch.sysreg_masks, the trap configuration,vncr_tlband nested vGIC state are only set up on the firstKVM_RUNinkvm_arch_vcpu_run_pid_change().I think I'm going to queue a patch disabling prefaulting immediately, and then we can fix this crap.
Ack, will prioritise looking through Jia's series.
Quoted 32 lines
Quoted 32 lines
Running the full
vcpu_load()path before then reads unsanitised guest sysregs and touches uninitialised nested state:get_s2_mmu_nested()populates nested_mmus[] from raw VTTBR_EL2/VTCR_EL2/HCR_EL2 values, FGT andHCRX_EL2are written beforekvm_calculate_traps()has run, andkvm_vgic_load()can entervgic_v3_load_nested()early. In particular, if userspace creates an EL2 vCPU with ID_AA64MMFR4_EL1.NV_frac cleared to 0 (sovncr_tlbis never allocated), sets HCR_EL2.NV=1 and an EL1 PSTATE, and callsKVM_PRE_FAULT_MEMORY,kvm_vcpu_load_hw_mmu()queuesKVM_REQ_MAP_L1_VNCR_EL2. The nextKVM_RUNthen dereferences a NULLvncr_tlbinkvm_map_l1_vncr()with preemption disabled andkvm->mmu_lockheld 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
KVM_PRE_FAULT_MEMORYstill needs genericvcpu_load()to setkvm_running_vcpufor dirty-ring tracking inmark_page_dirty_in_slot(), butkvm_arch_vcpu_pre_fault_memory()only walks and populates&kvm->arch.mmuand does not use any hardware vCPU state. Skip the arch vcpu_load()/vcpu_put() callbacks untilvcpu_has_run_once()is true. InKVM_RUN,vcpu->pidis set undervcpu->mutexright afterkvm_arch_vcpu_run_pid_change()succeeds and beforekvm_arch_vcpu_ioctl_run()callsvcpu_load().
Quoted 7 lines
Quoted 3 lines
Since
kvm_running_vcpucan now be set without VHE state on the CPU, usehost_ctxt->__hyp_running_vcpuin VHEenter_vmid_context()(matching nVHE)Can you clarify under which circumstances we end-up on the TLB invalidation path? Simply faulting things in should not result in invalidation. Unless we end-up with MMU notifiers firing?
Yeah I think the issue is that an enter_vmid_context() / exit_vmid_context() pair flushing some other MMU could end up writing the vCPU's (canonical) hw_mmu into VTTBR_EL2/VTCR_EL2 on exit, even though it was never loaded in the first place.
But that would just get overwritten when next loaded anyway?
So I think it's probably the right change but makes no difference in practice ATM?
Quoted 5 lines
Quoted 2 lines
and check
vcpu_has_run_once()invgic_maintenance_handler().I have similar questions here. It isn't obvious how you end-up getting a VGIC MI if you don't run and don't load the state on the CPU.
Yeah I don't think there is a way? Maybe this is just defensive?
Quoted 7 lines
Thanks,
M.--
Without deviation from the norm, progress is not possible.
--
Cheers, Lorenzo
RE: [PATCH 1/3] KVM: arm64: Don't load hardware vCPU state before first run
In reply to Marc ZyngierHi, Marc
Quoted 1 line
-----Original Message-----
[...]
Quoted 9 lines
Quoted 1 line
KVM_PRE_FAULT_MEMORYcallsvcpu_load()as soon as
KVM_ARM_VCPU_INIThasQuoted 3 lines
completed. On arm64, however,
kvm->arch.sysreg_masks, the trap configuration,vncr_tlband nested vGIC state are only set up on the firstKVM_RUNinkvm_arch_vcpu_run_pid_change().I think I'm going to queue a patch disabling prefaulting immediately, and then we can fix this crap.
Fine with me. I'll rebase on top of it and have v2 re-enable pre-faulting together with the load/put change.
Quoted 36 lines
Quoted 4 lines
Running the full
vcpu_load()path before then reads unsanitised guest sysregs and touches uninitialised nested state:get_s2_mmu_nested()populates nested_mmus[] from raw VTTBR_EL2/VTCR_EL2/HCR_EL2 values,FGT
Quoted 3 lines
and
HCRX_EL2are written beforekvm_calculate_traps()has run, andkvm_vgic_load()can entervgic_v3_load_nested()early. In particular, if userspace creates an EL2 vCPU with ID_AA64MMFR4_EL1.NV_frac clearedto 0
Quoted 3 lines
(so
vncr_tlbis never allocated), sets HCR_EL2.NV=1 and an EL1 PSTATE, and callsKVM_PRE_FAULT_MEMORY,kvm_vcpu_load_hw_mmu()queuesKVM_REQ_MAP_L1_VNCR_EL2. The nextKVM_RUNthen dereferences aNULL
Quoted 5 lines
vncr_tlbinkvm_map_l1_vncr()with preemption disabled and kvm-mmu_lock
held for read:[ 73.636014] Unable to handle kernel NULL pointer dereference at virtualaddress 0000000000000078
Quoted 17 lines
[ 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
KVM_PRE_FAULT_MEMORYstill needs genericvcpu_load()to setkvm_running_vcpufor dirty-ring tracking inmark_page_dirty_in_slot(), butkvm_arch_vcpu_pre_fault_memory()only walks and populates&kvm->arch.mmuand does not use any hardware vCPU state. Skip the arch vcpu_load()/vcpu_put() callbacks untilvcpu_has_run_once()is true. InKVM_RUN,vcpu->pidis set undervcpu->mutexright afterkvm_arch_vcpu_run_pid_change()succeeds and beforekvm_arch_vcpu_ioctl_run()callsvcpu_load().
Quoted 8 lines
Quoted 3 lines
Since
kvm_running_vcpucan now be set without VHE state on the CPU, usehost_ctxt->__hyp_running_vcpuin VHEenter_vmid_context()(matching nVHE)Can you clarify under which circumstances we end-up on the TLB invalidation path? Simply faulting things in should not result in invalidation. Unless we end-up with MMU notifiers firing?
I found this by code inspection, not from a failure. A TLBI from a stage-2 BBM or an MMU notifier can hit this path during pre-faulting, but before the first run hw_mmu is canonical, so it makes no difference. As Lorenzo said, I'll drop this hunk.
Quoted 4 lines
Quoted 1 line
and check
vcpu_has_run_once()invgic_maintenance_handler().I have similar questions here. It isn't obvious how you end-up getting a VGIC MI if you don't run and don't load the state on the CPU.
I can't construct a case either. It was defensive, so I'll drop it.
Thanks,
Jia He (Justin)
RE: [PATCH 1/3] KVM: arm64: Don't load hardware vCPU state before first run
In reply to Marc ZyngierQuoted 3 lines
-----Original Message-----
[...]
Quoted 11 lines
Quoted 2 lines · Jia He
KVM_PRE_FAULT_MEMORYcallsvcpu_load()as soon as
KVM_ARM_VCPU_INIThasQuoted 3 lines
completed. On arm64, however,
kvm->arch.sysreg_masks, the trap configuration,vncr_tlband nested vGIC state are only set up on the firstKVM_RUNinkvm_arch_vcpu_run_pid_change().I think I'm going to queue a patch disabling prefaulting immediately, and then we can fix this crap.
I can still reproduce the same vncr_tlb NULL dereference with prefaulting disabled. On kvmarm/fixes, commit 5651590be08a ("KVM: arm64: Load the vcpu in KVM_SET_VCPU_EVENTS") added a vcpu_load() call to KVM_SET_VCPU_EVENTS. A single KVM_SET_VCPU_EVENTS with an all-zero struct kvm_vcpu_events, issued before the first KVM_RUN, is enough to trigger it.
For v2, I'll build on your patch and use vcpu_has_run_once() to gate the arch load/put paths for both ioctls.
___
Best Regards,
Jia He (Justin)
PATCH 2/3 KVM: arm64: nv: Don't request or attempt L1 VNCR mapping without vncr_tlb
In reply to Jia Hevcpu->arch.vncr_tlb is only allocated when ID_AA64MMFR4_EL1.NV_frac advertises FEAT_NV2. When NV_frac is cleared to 0 (or E2H0 is selected), vncr_tlb is NULL for the lifetime of the vCPU.
Don't set KVM_REQ_MAP_L1_VNCR_EL2 in kvm_vcpu_load_hw_mmu() when vncr_tlb is NULL. The only other place that sets this request is kvm_translate_vncr(), which only runs on a FEAT_NV2 VNCR abort. Warn and return early in kvm_map_l1_vncr() if vt is NULL before disabling preemption and taking kvm->mmu_lock.
--- arch/arm64/kvm/nested.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
arch/arm64/kvm/nested.c +5 -1
@@ -979,7 +979,8 @@ void kvm_vcpu_load_hw_mmu(struct kvm_vcpu *vcpu) vcpu->arch.hw_mmu =get_s2_mmu_nested (vcpu); } -if (__vcpu_sys_reg (vcpu,HCR_EL2 ) &HCR_EL2_NV ) +if (vcpu->arch.vncr_tlb && + (__vcpu_sys_reg (vcpu,HCR_EL2 ) &HCR_EL2_NV ))kvm_make_request (KVM_REQ_MAP_L1_VNCR_EL2 , vcpu); } }
@@ -1761,6 +1762,9 @@ static void kvm_map_l1_vncr(struct kvm_vcpu *vcpu)struct vncr_tlb *vt = vcpu->arch.vncr_tlb;pgprot_t prot; +if (WARN_ON_ONCE (!vt)) +return ; +guard (preempt)();guard (read_lock)(&vcpu->kvm->mmu_lock);
Signature · 1 line
2.34.1
Marc Zyngier
maintainerRe: [PATCH 2/3] KVM: arm64: nv: Don't request or attempt L1 VNCR mapping without vncr_tlb
Quoted 21 lines · Jia He
vcpu->arch.vncr_tlbis only allocated when ID_AA64MMFR4_EL1.NV_frac advertisesFEAT_NV2. WhenNV_fracis cleared to 0 (or E2H0 is selected),vncr_tlbis NULL for the lifetime of the vCPU.Don't set
KVM_REQ_MAP_L1_VNCR_EL2inkvm_vcpu_load_hw_mmu()whenvncr_tlbis NULL. The only other place that sets this request iskvm_translate_vncr(), which only runs on aFEAT_NV2VNCR abort. Warn and return early inkvm_map_l1_vncr()if vt is NULL before disabling preemption and takingkvm->mmu_lock.Signed-off-by Jia He <[email protected]>--- arch/arm64/kvm/nested.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)arch/arm64/kvm/nested.c +0
-0
arch/arm64/kvm/nested.c void kvm_vcpu_load_hw_mmu(struct kvm_vcpu *vcpu) · line 984
Quoted 7 lines
arch/arm64/kvm/nested.c +2
-1@@ -979,7 +979,8 @@ void kvm_vcpu_load_hw_mmu(struct kvm_vcpu *vcpu) vcpu->arch.hw_mmu =get_s2_mmu_nested (vcpu); } -if (__vcpu_sys_reg (vcpu,HCR_EL2 ) &HCR_EL2_NV ) +if (vcpu->arch.vncr_tlb && + (__vcpu_sys_reg (vcpu,HCR_EL2 ) &HCR_EL2_NV ))kvm_make_request (KVM_REQ_MAP_L1_VNCR_EL2 , vcpu);
How do we even get here? Now that you have short-circuited the vcpu state loading, I can't see how this code is reachable before we've actually initialised the relevant state.
What am I missing?
arch/arm64/kvm/nested.c static void kvm_map_l1_vncr(struct kvm_vcpu *vcpu) · line 1767
Quoted 9 lines
arch/arm64/kvm/nested.c +3
-0} }@@ -1761,6 +1762,9 @@ static void kvm_map_l1_vncr(struct kvm_vcpu *vcpu)struct vncr_tlb *vt = vcpu->arch.vncr_tlb;pgprot_t prot; +if (WARN_ON_ONCE (!vt)) +return ; +
I don't think we want this. Either the code is rock solid, and the warning pointless, or we have terrible problems, and I really want to see the kernel exploding, specially given that NV is still an experimental feature.
Thanks,
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
RE: [PATCH 2/3] KVM: arm64: nv: Don't request or attempt L1 VNCR mapping without vncr_tlb
Quoted 3 lines
-----Original Message-----
[...]
Quoted 49 lines
Quoted 6 lines · Jia He
vcpu->arch.vncr_tlbis only allocated when ID_AA64MMFR4_EL1.NV_frac advertisesFEAT_NV2. WhenNV_fracis cleared to 0 (or E2H0 is selected),vncr_tlbis NULL for the lifetime of the vCPU.Don't set
KVM_REQ_MAP_L1_VNCR_EL2inkvm_vcpu_load_hw_mmu()when
Quoted 2 lines
vncr_tlbis NULL. The only other place that sets this request iskvm_translate_vncr(), which only runs on aFEAT_NV2VNCR abort. Warnand
arch/arm64/kvm/nested.c void kvm_vcpu_load_hw_mmu(struct kvm_vcpu
Quoted 13 lines
return early in
kvm_map_l1_vncr()if vt is NULL before disabling preemption and takingkvm->mmu_lock.Signed-off-by Jia He <[email protected]>--- arch/arm64/kvm/nested.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)arch/arm64/kvm/nested.c +0
-0@@ -979,7 +979,8 @@ void kvm_vcpu_load_hw_mmu(struct kvm_vcpu*vcpu)
arch/arm64/kvm/nested.c void kvm_vcpu_load_hw_mmu(struct kvm_vcpu · line 979
Quoted 1 line
arch/arm64/kvm/nested.c +0
-0vcpu->arch.hw_mmu =
get_s2_mmu_nested(vcpu);arch/arm64/kvm/nested.c void kvm_vcpu_load_hw_mmu(struct kvm_vcpu *vcpu)
Quoted 6 lines
arch/arm64/kvm/nested.c +2
-1} -if (__vcpu_sys_reg (vcpu,HCR_EL2 ) &HCR_EL2_NV ) +if (vcpu->arch.vncr_tlb && + (__vcpu_sys_reg (vcpu,HCR_EL2 ) &HCR_EL2_NV )) kvm_make_request(KVM_REQ_MAP_L1_VNCR_EL2,vcpu);
How do we even get here? Now that you have short-circuited the vcpu state loading, I can't see how this code is reachable before we've actually initialised the relevant state.
What am I missing?
arch/arm64/kvm/nested.c static void kvm_map_l1_vncr(struct kvm_vcpu
Quoted 3 lines
arch/arm64/kvm/nested.c +0
-0} }@@ -1761,6 +1762,9 @@ static void kvm_map_l1_vncr(struct kvm_vcpu*vcpu)
Quoted 3 lines
arch/arm64/kvm/nested.c +0
-0struct vncr_tlb *vt = vcpu->arch.vncr_tlb;pgprot_t prot;
Quoted 9 lines
arch/arm64/kvm/nested.c static void kvm_map_l1_vncr(struct kvm_vcpu *vcpu) · line 1767
Quoted 3 lines
arch/arm64/kvm/nested.c +3
-0+if (WARN_ON_ONCE (!vt)) +return ; +I don't think we want this. Either the code is rock solid, and the warning pointless, or we have terrible problems, and I really want to see the kernel exploding, specially given that NV is still an experimental feature.
Agreed. I'll drop this patch in v2. Patch 2/3 was only meant as a defensive check, and patch 1/3 should be sufficient.
Thanks,
Jia He (Justin)
PATCH 3/3 KVM: selftests: arm64: Test KVM_PRE_FAULT_MEMORY before first KVM_RUN
In reply to Jia HeExtend nv_pre_fault_memory_test to exercise KVM_PRE_FAULT_MEMORY before the first KVM_RUN.
Without EL2, cover immediate_exit on the first KVM_RUN, retrying after a failed first KVM_RUN, and pre-faulting while the vCPU moves from the main thread to a worker thread for its first run and back to the main thread for its second run. The two threads are pinned to different CPUs when at least two are available.
With EL2, additionally cover an EL2 vCPU with ID_AA64MMFR4_EL1.NV_frac cleared to 0 and HCR_EL2.NV=1 at EL1h (the NULL vncr_tlb reproducer), an EL2 vCPU with a vEL1 context and asymmetric HCR_EL2.IMO/FMO staged before the first run, and the same thread handoff. An unfixed kernel reports the staged context case only as a WARN in the kernel log; the test itself still passes.
--- .../kvm/arm64/nv_pre_fault_memory_test.c | 335 ++++++++++++++++-- 1 file changed, 298 insertions(+), 37 deletions(-)
tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c +298 -37
@@ -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,256 @@ 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. +*/ +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, run the vCPU for the first time in a worker +* thread, then pre-fault and run it again in the main thread. This changes +* vcpu->pid both before and after the first run. 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-first-run pre-fault with immediate_exit\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
Re: [PATCH 3/3] KVM: selftests: arm64: Test KVM_PRE_FAULT_MEMORY before first KVM_RUN
Quoted 3 lines · Jia He
Extend
nv_pre_fault_memory_testto exerciseKVM_PRE_FAULT_MEMORYbefore the firstKVM_RUN.
Hi Jia He,
Quoted 5 lines
Without EL2, cover
immediate_exiton the firstKVM_RUN, retrying after a failed firstKVM_RUN, and pre-faulting while the vCPU moves from the main thread to a worker thread for its first run and back to the main thread for its second run. The two threads are pinned to different CPUs when at least two are available.
I suggest to list them for better readability e.g:
Test cases for w/o EL2:
1. Immediate_exit on the first KVM_RUN
2. Retrying pre-fault after a failed first KVM_RUN.
3. Pre-faulting while moving the vCPU between 2 threads. (more details of 3 can be given in code implementation there)
Quoted 16 lines
With EL2, additionally cover an EL2 vCPU with ID_AA64MMFR4_EL1.NV_frac cleared to 0 and HCR_EL2.NV=1 at EL1h (the NULL
vncr_tlbreproducer), an EL2 vCPU with a vEL1 context and asymmetric HCR_EL2.IMO/FMO staged before the first run, and the same thread handoff. An unfixed kernel reports the staged context case only as a WARN in the kernel log; the test itself still passes.Signed-off-by Jia He <[email protected]>--- .../kvm/arm64/nv_pre_fault_memory_test.c | 335 ++++++++++++++++-- 1 file changed, 298 insertions(+), 37 deletions(-)tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c +0
-0
...
tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c int main(void) · line 201
Quoted 6 lines
tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c +6
-0+ +/* +* Pre-fault before the first KVM_RUN with ID_AA64MMFR4_EL1.NV_frac == 0 +* (no vncr_tlb) and HCR_EL2.NV=1 at EL1h. +*/ +static void test_pre_first_run_no_nv2 (void )
This may not necessary per Marc's comment on patch 2 ?
Quoted 13 lines
tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c +13
-0+{ +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 )));
...
Quoted 5 lines
tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c +5
-0+ +static void *first_run_worker (void *arg) +{ +struct handoff_args *h = arg; +
tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c int main(void) · line 307
Quoted 10 lines
tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c +10
-0+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, run the vCPU for the first time in a worker
Minor: the pre-fault happens in worker thead yet, see first_run_worker().
Quoted 47 lines
+ * thread, then pre-fault and run it again in the main thread. This changes
+ *vcpu->pidboth before and after the first run. If at least two CPUs are
+ * available, the two threads are pinned to different CPUs.
+ */tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c +43
-0+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);
tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c int main(void) · line 364
Quoted 10 lines
tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c +10
-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-first-run pre-fault with immediate_exit\n" );
^^^^
The vcpu->run->immediate_exit = 1 happens afte the pre_fault, thus s/with/then/ looks more accurate here if this is the desired behavior.
Quoted 58 lines
tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c +50
-1+ 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 ; } --2.34.1
RE: [PATCH 3/3] KVM: selftests: arm64: Test KVM_PRE_FAULT_MEMORY before first KVM_RUN
Hi Yao,
Quoted 1 line
-----Original Message-----
[...]
Quoted 7 lines
Quoted 6 lines
Without EL2, cover
immediate_exiton the firstKVM_RUN, retrying after a failed firstKVM_RUN, and pre-faulting while the vCPU moves from the main thread to a worker thread for its first run and back to the main thread for its second run. The two threads are pinned to different CPUs when at least two are available.
Quoted 8 lines
I suggest to list them for better readability e.g:
Test cases for w/o EL2:
1.Immediate_exiton the firstKVM_RUN
2. Retrying pre-fault after a failed firstKVM_RUN.
3. Pre-faulting while moving the vCPU between 2 threads. (more details of 3 can be given in code implementation there)
Will do in v2.
Quoted 25 lines
Quoted 3 lines
With EL2, additionally cover an EL2 vCPU with ID_AA64MMFR4_EL1.NV_frac cleared to 0 and HCR_EL2.NV=1 at EL1h (the NULL
vncr_tlbreproducer), an EL2 vCPU with a vEL1 context and asymmetric HCR_EL2.IMO/FMO stagedbefore
Review
Quoted 10 lines
the first run, and the same thread handoff. An unfixed kernel reports the staged context case only as a WARN in the kernel log; the test itself still passes.
Signed-off-by Jia He <[email protected]>--- .../kvm/arm64/nv_pre_fault_memory_test.c | 335 ++++++++++++++++-- 1 file changed, 298 insertions(+), 37 deletions(-)(quoted diff) +0
-0
b/tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.cQuoted 3 lines
index
09c1db303856..6dbcf3b0ef13100644tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c +0
-0...
tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c int main(void)
Quoted 3 lines
tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c +3
-0+ +/* +* Pre-fault before the first KVM_RUN with ID_AA64MMFR4_EL1.NV_frac == 0
Quoted 6 lines
Quoted 3 lines
+ * (no
vncr_tlb) and HCR_EL2.NV=1 at EL1h.
+ */
+static voidtest_pre_first_run_no_nv2(void)This may not necessary per Marc's comment on patch 2 ?
I'd like to keep it. It is the reproducer for the NULL vncr_tlb dereference that patch 1 fixes, not a test for patch 2. With patch 2 dropped, it becomes the regression test for patch 1. I'll add that to the comment.
Quoted 26 lines
tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c int main(void)
Quoted 6 lines
tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c +6
-0+{ +struct kvm_vcpu *vcpu; +struct kvm_vm *vm; +u64 val; + + pr_info("Testing pre-first-run pre-fault with NV_frac=0 andHCR_EL2.NV=1\n");
tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c int main(void)
Quoted 5 lines
tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c +5
-0+ 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)));Quoted 2 lines
tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c +2
-0+vcpu_set_reg (vcpu,KVM_ARM64_SYS_REG (SYS_TCR_EL1 ), +vcpu_get_reg (vcpu,KVM_ARM64_SYS_REG (SYS_TCR_EL2 )));...
tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c int main(void)
Quoted 7 lines
tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c +7
-0+ +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);
Quoted 11 lines
tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c int main(void) · line 307
Quoted 8 lines
tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c +8
-0+ +vcpu_run (h->vcpu); +expect_ucall_sync (h->vcpu,1 ); +return NULL ; +} + +/* +* Pre-fault in the main thread, run the vCPU for the first time in a worker Minor: the pre-fault happens in worker thead yet, see
first_run_worker().
Right, both threads pre-fault. I'll fix the comment.
Quoted 59 lines
Quoted 20 lines
+ * thread, then pre-fault and run it again in the main thread. This changes
+ *vcpu->pidboth before and after the first run. If at least two CPUs are
+ * available, the two threads are pinned to different CPUs.
+ */tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c +16
-0+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))tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c int main(void)
Quoted 10 lines
tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c +10
-0+continue ; +if (cpu0 <0 ) { + cpu0 = cpu; + }else { + h.cpu = cpu; +break ; + } + } +if (h.cpu <0 ) + pr_info("Only one CPU available, not testing CPUmigration\n");
Quoted 17 lines
tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c +17
-0+ +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);tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c int main(void) · line 364
Quoted 10 lines
tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c +10
-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-first-run pre-fault with immediate_exit\n" );^^^^
Quoted 3 lines
The
vcpu->run->immediate_exit= 1 happens afte thepre_fault, thus s/with/then/ looks more accurate here if this is the desired behavior.
Yes, will fix.
Thanks,
Jia He (Justin)