From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 59476353EDB; Fri, 13 Feb 2026 11:03:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770980586; cv=none; b=r8LNljIN+xVrARtLnqsKC1KAGPIf+FGeBSnRMsgXILWAkmBOufI3ueVOk7/18v5YAcV7NT8MDhYSJB+hJHf+ZR5YhfdVdCnbu8JYaC7Eqi2ig4cculjQxMEwly36jj72O5tCbHMCi3nR0JwKUozyHZvnPPxfF8xVjd5+p8R7T2o= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770980586; c=relaxed/simple; bh=iM4yoFDfsz9ptDHd2dIQ4m/xGPnBFa7Wx99OdkLGUUs=; h=Date:Message-ID:From:To:Cc:Subject:In-Reply-To:References: MIME-Version:Content-Type; b=NGBZir0D+h6thwRxG+6KaIcCpqMvNi5mHU16il5uKfRBeWk/WO2Iq9lH/g2Efldk2lGENcezg+sM6GWoX3PKheabknGfcihqDdIvP3jQ98QIMA+VT9hp3olSNQp+gfHDtoPPlOcnQ8IVOTGbl4qgdEoAHynKuRlrX8HNR3JJaII= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=KMh8b0qX; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="KMh8b0qX" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 30E51C116C6; Fri, 13 Feb 2026 11:03:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1770980586; bh=iM4yoFDfsz9ptDHd2dIQ4m/xGPnBFa7Wx99OdkLGUUs=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=KMh8b0qXB4oYN3kle1w4+vgjR8gj0/jKRE+jwPyg4IAxxfBpODm834q2JfS0QR0uO AH8GyymTyfVF0NfWEtyxlkNN1Orex3zpCE8SKSb0X6uhzoPM+5lIKi2lfbvxEXkkBu czJ824HU3Jg6zDhbbsoWst8dbVT1LSmr+HFN0x2MS81lzczTa1h01bLJLTyYrkse2H kdshFrq+2ppzaTnAdN0/628+DxA+j5DQTlLp3UVpQGv9aHwcEDaxmW91tjHuJo3SHq FjJpX7TZ0yrGiGEQ5RCd5Xp9V9MQ7LVlN/pqiPR9441lYp/qDr2NwriXO4GqEa6cAb oD7u5a19Esx6A== Received: from sofa.misterjones.org ([185.219.108.64] helo=goblin-girl.misterjones.org) by disco-boy.misterjones.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1vqqwx-0000000ArRO-2W0g; Fri, 13 Feb 2026 11:03:03 +0000 Date: Fri, 13 Feb 2026 11:03:03 +0000 Message-ID: <86ecmoc3dk.wl-maz@kernel.org> From: Marc Zyngier To: Fuad Tabba Cc: kvm@vger.kernel.org, kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, oliver.upton@linux.dev, joey.gouly@arm.com, suzuki.poulose@arm.com, yuzenghui@huawei.com, catalin.marinas@arm.com, will@kernel.org, stable@vger.kernel.org Subject: Re: [PATCH v1 2/3] KVM: arm64: Fix ID register initialization for non-protected pKVM guests In-Reply-To: <20260212090252.158689-3-tabba@google.com> References: <20260212090252.158689-1-tabba@google.com> <20260212090252.158689-3-tabba@google.com> User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI-EPG/1.14.7 (Harue) FLIM-LB/1.14.9 (=?UTF-8?B?R29qxY0=?=) APEL-LB/10.8 EasyPG/1.0.0 Emacs/30.1 (aarch64-unknown-linux-gnu) MULE/6.0 (HANACHIRUSATO) Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") Content-Type: text/plain; charset=US-ASCII X-SA-Exim-Connect-IP: 185.219.108.64 X-SA-Exim-Rcpt-To: tabba@google.com, kvm@vger.kernel.org, kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, oliver.upton@linux.dev, joey.gouly@arm.com, suzuki.poulose@arm.com, yuzenghui@huawei.com, catalin.marinas@arm.com, will@kernel.org, stable@vger.kernel.org X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false On Thu, 12 Feb 2026 09:02:51 +0000, Fuad Tabba wrote: > > In protected mode, the hypervisor maintains a separate instance of > the `kvm` structure for each VM. For non-protected VMs, this structure is > initialized from the host's `kvm` state. > > Currently, `pkvm_init_features_from_host()` copies the > `KVM_ARCH_FLAG_ID_REGS_INITIALIZED` flag from the host without the > underlying `id_regs` data being initialized. This results in the > hypervisor seeing the flag as set while the ID registers remain zeroed. > > Consequently, `kvm_has_feat()` checks at EL2 fail (return 0) for > non-protected VMs. This breaks logic that relies on feature detection, > such as `ctxt_has_tcrx()` for TCR2_EL1 support. As a result, certain > system registers (e.g., TCR2_EL1, PIR_EL1, POR_EL1) are not > saved/restored during the world switch, which could lead to state > corruption. > > Fix this by explicitly copying the ID registers from the host `kvm` to > the hypervisor `kvm` for non-protected VMs during vCPU initialization, > since we trust the host with its non-protected guests' features. Also > ensure `KVM_ARCH_FLAG_ID_REGS_INITIALIZED` is cleared initially in > `pkvm_init_features_from_host` so that `vm_copy_id_regs` can properly > initialize them and set the flag once done. > > Fixes: 41d6028e28bd ("KVM: arm64: Convert the SVE guest vcpu flag to a vm flag") > Signed-off-by: Fuad Tabba > --- > arch/arm64/kvm/hyp/nvhe/pkvm.c | 37 ++++++++++++++++++++++++++++++++-- > 1 file changed, 35 insertions(+), 2 deletions(-) > > diff --git a/arch/arm64/kvm/hyp/nvhe/pkvm.c b/arch/arm64/kvm/hyp/nvhe/pkvm.c > index 12b2acfbcfd1..267854ed29c8 100644 > --- a/arch/arm64/kvm/hyp/nvhe/pkvm.c > +++ b/arch/arm64/kvm/hyp/nvhe/pkvm.c > @@ -344,6 +344,8 @@ static void pkvm_init_features_from_host(struct pkvm_hyp_vm *hyp_vm, const struc > > /* No restrictions for non-protected VMs. */ > if (!kvm_vm_is_protected(kvm)) { > + clear_bit(KVM_ARCH_FLAG_ID_REGS_INITIALIZED, &host_arch_flags); > + > hyp_vm->kvm.arch.flags = host_arch_flags; Can't you just have hyp_vm->kvm.arch.flags &= ~BIT_ULL(KVM_ARCH_FLAG_ID_REGS_INITIALIZED); since there are no atomicity requirements here? > > bitmap_copy(kvm->arch.vcpu_features, > @@ -471,6 +473,36 @@ static int pkvm_vcpu_init_sve(struct pkvm_hyp_vcpu *hyp_vcpu, struct kvm_vcpu *h > return ret; > } > > +static int vm_copy_id_regs(struct pkvm_hyp_vcpu *hyp_vcpu) > +{ > + struct pkvm_hyp_vm *hyp_vm = pkvm_hyp_vcpu_to_hyp_vm(hyp_vcpu); > + const struct kvm *host_kvm = hyp_vm->host_kvm; > + struct kvm *kvm = &hyp_vm->kvm; > + > + if (!test_bit(KVM_ARCH_FLAG_ID_REGS_INITIALIZED, &host_kvm->arch.flags)) > + return -EINVAL; > + > + if (test_bit(KVM_ARCH_FLAG_ID_REGS_INITIALIZED, &kvm->arch.flags)) > + return 0; > + > + memcpy(kvm->arch.id_regs, host_kvm->arch.id_regs, sizeof(kvm->arch.id_regs)); > + set_bit(KVM_ARCH_FLAG_ID_REGS_INITIALIZED, &kvm->arch.flags); This looks a bit odd. Can you have another vcpu doing this in parallel? You seem to be holding vm_table_lock at this stage, so that's probably OK, but I'd have expected something like: if (test_and_set_bit(KVM_ARCH_FLAG_ID_REGS_INITIALIZED, &kvm->arch.flags)) return 0; memcpy(kvm->arch.id_regs, host_kvm->arch.id_regs, sizeof(kvm->arch.id_regs)); which makes the intent slightly clearer. Thanks, M. -- Without deviation from the norm, progress is not possible.