Re: [PATCH 1/3] KVM: arm64: Don't load hardware vCPU state before first run

From: Marc Zyngier

Date: Tue Nov 24 2026 - 07:50:20 EST


Adding Dan to this loop, as I reckon something else is amiss.

On Tue, 24 Nov 2026 01:39:23 +0000,
Justin He <Justin.He@xxxxxxx> wrote:
>
> Hi, Marc
>
> > -----Original Message-----
> [...]
> > > 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().
> >
> > 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.

Don't worry about that. I can fix that myself when applying the
patches.


> > > 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)
> >
> > 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.

vcpu->arch.hw_mmu being the canonical one before the first run doesn't
hold anymore, since a7602ea542d4e ("KVM: arm64: nv: Initialize hw_mmu
to NULL for nested vCPUs"), as userspace can configure things for an
L2, before first run, and bad things happen otherwise.

But the observation remains: if an MMU notifier occurs for any reason
while pre-faulting (likely a concurrent reclaim), we get the "not
quite loaded" vcpu in enter_vmid_context(). If vcpu->arch.hw_mmu is a
valid (non-null) pointer, then we will restore whatever is in that
structure when reaching exit_vmid_context(), and that's not what is
intended.

So I think we need that sort of exclusion. Either we track what is
actually properly loaded (using __hyp_running_vcpu as you did before,
or by checking whether the vcpu is in a valid state).

I think we have a somewhat similar breakage with 5651590be08a6 ("KVM:
arm64: Load the vcpu in KVM_SET_VCPU_EVENTS"): as vcpu_load() now
becomes sensitive to vcpu_has_run_once() being true, we can't tell
anything of the state that is going to be evaluated by the event
injection helper.

We could prevent the non-running injection in KVM_SET_VCPU_EVENTS, but
that's also a change in the API...

M.

--
Without deviation from the norm, progress is not possible.