From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=217.140.110.172 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796526692; b=CJ0T/AEwu7GHIQs3EvKub/9NI4f8vPEV36WuZVDzRgX2ipgIF4vaeQ27wKL+Y8qI7cMB /bE0l55zwN/kWiwu2ohpVYiY146F3HE8aOOVPjSKVIiw/BHsRkzZ/rmWmbxaMCXMxn3Vk ggpBe2v/XMpltKsKx3q2GjXLQNr6YdKsty0EBxIYT6qvGg2AjjAkbWswS7TUerGrih5/f ScM51KoWhnNLwK0JYPuTi2obzRmHOHY8t4lUQqa0TQD+QeMmQ5fiHlzhlcYMyJB8JdGD1 A/TWXJsjN3aQYFkAZsO8UnWvXSUXk4QYbin7HsjsQK8JH2kx/EOfMs6yY5T9U+YBe/A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796526692; h=DMARC-Filter:Received:Received:DKIM-Signature:From:To:Cc:Subject: Date:Message-Id:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=kZsrAaWo2j3Dp+TdOtfzw73iQ/yybhMLs++Zua+n9ro=; b=JHM0bPnudvrAt8OEh+ASiPe0CQQz2ilDkrMlApaZs/XTNiGvWtzTx/0tHHYwTcIB0kTQ gxru8iylfvG72BMmtNmFn3zWLKxTuc1sSsBt9KIuH6uHWWpFO67T8abmw81V1k2qJqS4m koNl+4klGwzc4sgMLuonm5OGXy6ruTsDUnIsGtZ3tgpzi7qzzxnhyTFoI+yLhRW05ZB2/ fRLtDyQndCRsrxPYKKsW/aqbQDWVPPSKNzPRayHljFHoG9oXAQ5nCGl+kLBXzWXiKpPpW 0u+Ox1RP090c3J9nMg02vCppNozeNg96U8m34VAXaSQpTbWRjcB/7t9Em2BkqD1grQw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=arm.com; spf=pass smtp.mailfrom=arm.com; dkim=pass header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=lPJpYSr8; arc=none smtp.remote-ip=217.140.110.172 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 31B63606A335; Sun, 6 Dec 2026 03:11:31 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 31B63606A335 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=arm.com header.i=@arm.com header.a=rsa-sha256 header.s=foss header.b=lPJpYSr8 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id B40ED14BF; Sat, 5 Dec 2026 19:11:27 -0800 (PST) Received: from entos-yitian-01.Arm.com (unknown [10.168.196.233]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 8481D3F86F; Sat, 5 Dec 2026 19:11:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=arm.com; s=foss; t=1796526690; bh=1V1kltsSFeun/tnmyIiOXL85Wp5MT2Hfr4uVLZXUjPs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lPJpYSr8LUBzjjlU5CnA35OVGrMbeSsEUrhKLvwk6Hpsqfj1xJD47IHGQEd3pBPHe BHykYela2ZkE875wv7xrOK0hrq2N6nutf+N5sG+cmOAwGuHJ4/rY4H9fcjt+/1G8KA nHvFUYOiHTT+lvtND8SEhA+ZXEyPEyWAWnKs7UrY= From: Jia He To: Marc Zyngier , Oliver Upton , Fuad Tabba Cc: nd@arm.com, Jia He , Joey Gouly , Steffen Eiden , Suzuki K Poulose , Zenghui Yu , Catalin Marinas , Will Deacon , Mark Rutland , Paolo Bonzini , Sean Christopherson , Shuah Khan , Sascha Bischoff , Lorenzo Stoakes , Dan Carpenter , Yao Yuan , 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: [PATCH v3 1/2] KVM: arm64: Don't load hardware vCPU state before first run Date: Sun, 6 Dec 2026 03:11:04 +0000 Message-Id: <20261206031105.497206-2-justin.he@arm.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261206031105.497206-1-justin.he@arm.com> References: <20261206031105.497206-1-justin.he@arm.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit KVM_PRE_FAULT_MEMORY and KVM_SET_VCPU_EVENTS call vcpu_load() before the first KVM_RUN, whereas arm64 only initialises sysreg_masks, trap configuration, vncr_tlb and nested vGIC state on the first KVM_RUN in kvm_arch_vcpu_run_pid_change(). Running the full vcpu_load() path early reads unsanitised guest sysregs and touches uninitialised nested state. For instance, if userspace clears ID_AA64MMFR4_EL1.NV_frac to 0 (so vncr_tlb is never allocated), sets HCR_EL2.NV=1 at EL1 and calls either ioctl, kvm_vcpu_load_hw_mmu() queues KVM_REQ_MAP_L1_VNCR_EL2. The first 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 Neither ioctl needs hardware vCPU state loaded before the first run. kvm_arch_vcpu_pre_fault_memory() only populates &kvm->arch.mmu and only needs generic vcpu_load() to set kvm_running_vcpu for dirty-ring tracking. KVM_SET_VCPU_EVENTS has nothing to put before the first run, and the first KVM_RUN loads the updated 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 after kvm_arch_vcpu_run_pid_change() succeeds and before vcpu_load(). Also skip kvm_pmu_nested_transition() before the first run, otherwise KVM_SET_VCPU_EVENTS injecting a nested exception calls kvm_vcpu_pmu_restore_guest() while kvm_arch_vcpu_put() skips kvm_vcpu_pmu_restore_host(), leaking VHE PMU EL0 counter filtering. With kvm_running_vcpu now set without VHE state on the CPU, an MMU notifier triggered during pre-faulting (e.g. by concurrent or direct reclaim) for another stage-2 MMU would cause VHE enter_vmid_context()/ exit_vmid_context() to save and load vcpu->arch.hw_mmu even though it was never loaded. Use host_ctxt->__hyp_running_vcpu instead, matching nVHE. Fixes: 34cfa9b3af17 ("KVM: arm64: Implement KVM_PRE_FAULT_MEMORY") Fixes: 5651590be08a ("KVM: arm64: Load the vcpu in KVM_SET_VCPU_EVENTS") Signed-off-by: Jia He --- arch/arm64/kvm/arm.c | 17 +++++++++++++++-- arch/arm64/kvm/hyp/vhe/tlb.c | 3 ++- arch/arm64/kvm/pmu-emul.c | 2 +- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index 21cd872ea394..a18e2ee26583 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -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 and KVM_SET_VCPU_EVENTS call vcpu_load() + * before the first KVM_RUN has finalised the VM and vCPU state in + * kvm_arch_vcpu_run_pid_change(). Neither needs the 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; } diff --git a/arch/arm64/kvm/hyp/vhe/tlb.c b/arch/arm64/kvm/hyp/vhe/tlb.c index 33ac9a8c954e..98df0bb0a05c 100644 --- a/arch/arm64/kvm/hyp/vhe/tlb.c +++ b/arch/arm64/kvm/hyp/vhe/tlb.c @@ -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 diff --git a/arch/arm64/kvm/pmu-emul.c b/arch/arm64/kvm/pmu-emul.c index 388a6244e7b2..cea7ac065125 100644 --- a/arch/arm64/kvm/pmu-emul.c +++ b/arch/arm64/kvm/pmu-emul.c @@ -1342,7 +1342,7 @@ void kvm_pmu_nested_transition(struct kvm_vcpu *vcpu) unsigned long mask; int i; - if (!vcpu->arch.pmu.created) + if (!vcpu->arch.pmu.created || !vcpu_has_run_once(vcpu)) return; mask = __vcpu_sys_reg(vcpu, PMCNTENSET_EL0); -- 2.34.1