From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 3D998EDF150 for ; Fri, 13 Feb 2026 11:03:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:MIME-Version: References:In-Reply-To:Subject:Cc:To:From:Message-ID:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=ssNm+HFJ91dogFWpdSJaDDRz71I7Swkas/gtKhf8gnc=; b=AQ1QwCy9nbvPtj3iuX893sD4jW 3b66C1wZw0ykplzH9bHBQz+WAWGOaX9YjLIQBvPqn240PeuSW9ea1JzE65P8FFdkurf3E1dKwVdGR ea5Qwtm76CiOZbir3UYZlnz6sM5DGp7vkPF0q8dt0nSDseM78qdkKcWOuPVbQ0MBePI0AqnbvjMtW VefuChQEwagvsvNuPIoLGXPzy2qxRR0hsI0O/KortHa5Qx3dsXv3NCVykdClWNpIxuo49ILEaKf7h mcGCG3oiT0PSmhldqpFalt//fV7j8z2t8aeafQyj2C00Tjxck81sIBQ40owELh8R4UjNdSD7fribw Uz4Jy/xw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98.2 #2 (Red Hat Linux)) id 1vqqx3-00000003MJ3-3sxy; Fri, 13 Feb 2026 11:03:09 +0000 Received: from sea.source.kernel.org ([172.234.252.31]) by bombadil.infradead.org with esmtps (Exim 4.98.2 #2 (Red Hat Linux)) id 1vqqx1-00000003MId-2lPB for linux-arm-kernel@lists.infradead.org; Fri, 13 Feb 2026 11:03:08 +0000 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sea.source.kernel.org (Postfix) with ESMTP id 5DF2540B67; Fri, 13 Feb 2026 11:03:06 +0000 (UTC) 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) 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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20260213_030307_771616_71D7281F X-CRM114-Status: GOOD ( 27.39 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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.