[PATCH v3 0/2] KVM: arm64: Don't load hardware vCPU state before first run
From: Jia He
Date: Sat Dec 05 2026 - 22:11:44 EST
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 and
kvm_pmu_nested_transition() 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 (4 KiB pages, lockdep enabled) and on
4 KiB / 64 KiB guest kernels for selftest page-size alignment:
- 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 on both 4 KiB and 64 KiB kernels.
Based on kvmarm/fixes (kvmarm-fixes-7.4-3).
Changes since v2:
- Patch 1: skip kvm_pmu_nested_transition() before the first run to
avoid leaking VHE PMU EL0 counter filtering (sashiko-bot).
- Patch 2: use getpagesize() instead of vm->page_size for pre-fault
alignment on 16 KiB / 64 KiB host kernels (sashiko-bot).
v2: https://lore.kernel.org/r/20261204090914.459990-1-justin.he@xxxxxxx
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/20261123150917.2679600-1-justin.he@xxxxxxx
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 +-
arch/arm64/kvm/pmu-emul.c | 2 +-
.../kvm/arm64/nv_pre_fault_memory_test.c | 339 ++++++++++++++++--
4 files changed, 320 insertions(+), 41 deletions(-)
--
2.34.1