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

From: Jia He

Date: Fri Dec 04 2026 - 04:09:40 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 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/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 +-
.../kvm/arm64/nv_pre_fault_memory_test.c | 338 ++++++++++++++++--
3 files changed, 318 insertions(+), 40 deletions(-)

--
2.34.1