Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: Jia He <justin.he@arm.com>
To: Marc Zyngier <maz@kernel.org>, Oliver Upton <oupton@kernel.org>,
	Fuad Tabba <fuad.tabba@linux.dev>
Cc: Jia He <justin.he@arm.com>, 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>,
	Lorenzo Stoakes <ljs@kernel.org>, Dan Carpenter <dan@pereq.eu>,
	Yao Yuan <yaoyuan@linux.alibaba.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: [PATCH v2 1/2] KVM: arm64: Don't load hardware vCPU state before first run
Date: Fri,  4 Dec 2026 09:09:13 +0000	[thread overview]
Message-ID: <20261204090914.459990-2-justin.he@arm.com> (raw)
In-Reply-To: <20261204090914.459990-1-justin.he@arm.com>

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

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 <justin.he@arm.com>
---
 arch/arm64/kvm/arm.c         | 17 +++++++++++++++--
 arch/arm64/kvm/hyp/vhe/tlb.c |  3 ++-
 2 files changed, 17 insertions(+), 3 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
-- 
2.34.1


  reply	other threads:[~2026-12-04  9:09 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04  9:09 [PATCH v2 0/2] KVM: arm64: Don't load hardware vCPU state before first run Jia He
2026-12-04  9:09 ` Jia He [this message]
2026-12-04  9:38   ` [PATCH v2 1/2] " sashiko-bot
2026-12-04  9:09 ` [PATCH v2 2/2] KVM: selftests: arm64: Test KVM_PRE_FAULT_MEMORY before first KVM_RUN Jia He
2026-12-04  9:25   ` sashiko-bot

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=20261204090914.459990-2-justin.he@arm.com \
    --to=justin.he@arm.com \
    --cc=Sascha.Bischoff@arm.com \
    --cc=catalin.marinas@arm.com \
    --cc=dan@pereq.eu \
    --cc=fuad.tabba@linux.dev \
    --cc=joey.gouly@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=ljs@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=yaoyuan@linux.alibaba.com \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox