All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Lorenzo Stoakes (ARM)" <ljs@kernel.org>
To: Marc Zyngier <maz@kernel.org>
Cc: Jia He <justin.he@arm.com>, Oliver Upton <oupton@kernel.org>,
	 Fuad Tabba <fuad.tabba@linux.dev>,
	Joey Gouly <joey.gouly@arm.com>,
	 Steffen Eiden <seiden@linux.ibm.com>,
	Suzuki K Poulose <suzuki.poulose@arm.com>,
	 Zenghui Yu <yuzenghui@huawei.com>,
	Catalin Marinas <catalin.marinas@arm.com>,
	 Will Deacon <will@kernel.org>,
	Mark Rutland <mark.rutland@arm.com>,
	 Paolo Bonzini <pbonzini@redhat.com>,
	Sean Christopherson <seanjc@google.com>,
	 Shuah Khan <shuah@kernel.org>,
	Sascha Bischoff <Sascha.Bischoff@arm.com>,
	 linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev,
	linux-kernel@vger.kernel.org,  kvm@vger.kernel.org,
	linux-kselftest@vger.kernel.org
Subject: Re: [PATCH 1/3] KVM: arm64: Don't load hardware vCPU state before first run
Date: Mon, 23 Nov 2026 17:50:46 +0000	[thread overview]
Message-ID: <awR4AzA5xeyNmz7i@gremlin> (raw)
In-Reply-To: <86eccbzcqh.wl-maz@kernel.org>

On Mon, Nov 23, 2026 at 05:20:06PM +0000, Marc Zyngier wrote:
> [+ Lorenzo]
>
> Hey Jia,
>
> On Mon, 23 Nov 2026 15:09:15 +0000,
> Jia He <justin.he@arm.com> wrote:
> >
> > 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.

Ack, will prioritise looking through Jia's series.

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

>
> > and check vcpu_has_run_once() in vgic_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?

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

--
Cheers, Lorenzo

  reply	other threads:[~2026-11-23 17:50 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23 15:09 [PATCH 0/3] KVM: arm64: Fix pre-first-run KVM_PRE_FAULT_MEMORY vcpu_load() issues Jia He
2026-11-23 15:09 ` [PATCH 1/3] KVM: arm64: Don't load hardware vCPU state before first run Jia He
2026-11-23 17:20   ` Marc Zyngier
2026-11-23 17:50     ` Lorenzo Stoakes (ARM) [this message]
2026-11-24  1:39     ` Justin He
2026-11-24 12:50       ` Marc Zyngier
2026-11-24 13:18         ` Justin He
2026-11-24 16:53         ` Lorenzo Stoakes (ARM)
2026-11-24  9:06     ` Justin He
2026-11-24 13:28       ` Marc Zyngier
2026-11-23 15:09 ` [PATCH 2/3] KVM: arm64: nv: Don't request or attempt L1 VNCR mapping without vncr_tlb Jia He
2026-11-23 17:36   ` Marc Zyngier
2026-11-24  1:42     ` Justin He
2026-11-23 15:09 ` [PATCH 3/3] KVM: selftests: arm64: Test KVM_PRE_FAULT_MEMORY before first KVM_RUN Jia He
2026-11-24  7:11   ` Yao Yuan
2026-11-24  8:12     ` Justin He

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=awR4AzA5xeyNmz7i@gremlin \
    --to=ljs@kernel.org \
    --cc=Sascha.Bischoff@arm.com \
    --cc=catalin.marinas@arm.com \
    --cc=fuad.tabba@linux.dev \
    --cc=joey.gouly@arm.com \
    --cc=justin.he@arm.com \
    --cc=kvm@vger.kernel.org \
    --cc=kvmarm@lists.linux.dev \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=maz@kernel.org \
    --cc=oupton@kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=seanjc@google.com \
    --cc=seiden@linux.ibm.com \
    --cc=shuah@kernel.org \
    --cc=suzuki.poulose@arm.com \
    --cc=will@kernel.org \
    --cc=yuzenghui@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.