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 30DC9C36000 for ; Wed, 19 Mar 2025 09:17:55 +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=WjxLAdM8gRP30Rp+Ttx0oXJVSr1bKdyw/D3cp2bGhlU=; b=d4jQG4O+mx8nx8g3pThb4VJu6m h+1r5nGw1jNFMSvclJ8DBrU79PwcK7vJhOhgWoMA9S5PKbGNlEe+IemukHaD3JAuNIFl9WdJreiZc KMdZ+CfQLSzKyP6kVSlZSQHtYo3hM6vwyeg5NCydvy0ufLHfCs4hoAOa8U6+vQK1Jp+aFfHL/+0l2 KACBBM4uSLUwl3hnapoTPp7G6EjWCQ0yDG2G21QDlSu/hTJuNuWgIUDcivXOYpDM6pxUiMP/ljqhl xlUetQjXpUSQ6Yxdot3DeFqGRBrK3gzLBYcXORhOHCIFa85Ze9XCQEFjnt1k3QrKlwH7V4pGkTCDr O4Go/RRA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tupYV-00000008SZK-0n0Q; Wed, 19 Mar 2025 09:17:43 +0000 Received: from tor.source.kernel.org ([172.105.4.254]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tupWo-00000008SIk-0F1a for linux-arm-kernel@lists.infradead.org; Wed, 19 Mar 2025 09:15:58 +0000 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by tor.source.kernel.org (Postfix) with ESMTP id 28677615CF; Wed, 19 Mar 2025 09:15:56 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 17242C4CEE9; Wed, 19 Mar 2025 09:15:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1742375757; bh=VFcDyvuCt7vsiepWpgHUSSaWYSKC23V508SsmP0qre4=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=HLOT7lcfwBmNcUq6yIpkjNGivfFTHjfKPA+fnS/O5DEy/y9kDJ0uI4fC46Nq9AFn7 +03a0nRHPbzHmeVXOZ34ICCWO8Xz0RvMSBwxvUKI7njmmPi2sXccF3YS3dKfAHFR+M Nj9N25i9iKzlglAz9Tzhxioza/En8gpwYFF+KRbzgznpNh+WUx0NbOfEdNiqiIpfca l7rTGKW9MX8/b/FcaMf2gGRMMuVPVH6eguCe9NLVFCcVAmJa2e7sIUVbsERDD9u2Hw cqIuJ0P9NJFJi/lB5AQHoK+cX+Q/d2BGkOKH/zMNqxPiQMoDf8hOVGYofERf/ADOvi /5afcVh01qMrA== 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.95) (envelope-from ) id 1tupWk-00EzPO-L3; Wed, 19 Mar 2025 09:15:54 +0000 Date: Wed, 19 Mar 2025 09:15:54 +0000 Message-ID: <86r02tmldh.wl-maz@kernel.org> From: Marc Zyngier To: Greg Kroah-Hartman , Mark Brown , Gavin Shan Cc: Oliver Upton , Joey Gouly , Suzuki K Poulose , Catalin Marinas , Will Deacon , linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org, stable@vger.kernel.org, Mark Rutland , Fuad Tabba Subject: Re: [PATCH 6.12 8/8] KVM: arm64: Eagerly switch ZCR_EL{1,2} In-Reply-To: <019afc2d-b047-4e33-971c-7debbbaec84d@redhat.com> References: <20250314-stable-sve-6-12-v1-0-ddc16609d9ba@kernel.org> <20250314-stable-sve-6-12-v1-8-ddc16609d9ba@kernel.org> <019afc2d-b047-4e33-971c-7debbbaec84d@redhat.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/29.4 (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: gregkh@linuxfoundation.org, broonie@kernel.org, gshan@redhat.com, oliver.upton@linux.dev, joey.gouly@arm.com, suzuki.poulose@arm.com, catalin.marinas@arm.com, will@kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org, stable@vger.kernel.org, mark.rutland@arm.com, tabba@google.com X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false 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 Wed, 19 Mar 2025 00:26:14 +0000, Gavin Shan wrote: > > Hi Mark, > > On 3/14/25 10:35 AM, Mark Brown wrote: > > From: Mark Rutland > > > > [ Upstream commit 59419f10045bc955d2229819c7cf7a8b0b9c5b59 ] > > > > In non-protected KVM modes, while the guest FPSIMD/SVE/SME state is live on the > > CPU, the host's active SVE VL may differ from the guest's maximum SVE VL: > > > > * For VHE hosts, when a VM uses NV, ZCR_EL2 contains a value constrained > > by the guest hypervisor, which may be less than or equal to that > > guest's maximum VL. > > > > Note: in this case the value of ZCR_EL1 is immaterial due to E2H. > > > > * For nVHE/hVHE hosts, ZCR_EL1 contains a value written by the guest, > > which may be less than or greater than the guest's maximum VL. > > > > Note: in this case hyp code traps host SVE usage and lazily restores > > ZCR_EL2 to the host's maximum VL, which may be greater than the > > guest's maximum VL. > > > > This can be the case between exiting a guest and kvm_arch_vcpu_put_fp(). > > If a softirq is taken during this period and the softirq handler tries > > to use kernel-mode NEON, then the kernel will fail to save the guest's > > FPSIMD/SVE state, and will pend a SIGKILL for the current thread. > > > > This happens because kvm_arch_vcpu_ctxsync_fp() binds the guest's live > > FPSIMD/SVE state with the guest's maximum SVE VL, and > > fpsimd_save_user_state() verifies that the live SVE VL is as expected > > before attempting to save the register state: > > > > | if (WARN_ON(sve_get_vl() != vl)) { > > | force_signal_inject(SIGKILL, SI_KERNEL, 0, 0); > > | return; > > | } > > > > Fix this and make this a bit easier to reason about by always eagerly > > switching ZCR_EL{1,2} at hyp during guest<->host transitions. With this > > happening, there's no need to trap host SVE usage, and the nVHE/nVHE > > __deactivate_cptr_traps() logic can be simplified to enable host access > > to all present FPSIMD/SVE/SME features. > > > > In protected nVHE/hVHE modes, the host's state is always saved/restored > > by hyp, and the guest's state is saved prior to exit to the host, so > > from the host's PoV the guest never has live FPSIMD/SVE/SME state, and > > the host's ZCR_EL1 is never clobbered by hyp. > > > > Fixes: 8c8010d69c132273 ("KVM: arm64: Save/restore SVE state for nVHE") > > Fixes: 2e3cf82063a00ea0 ("KVM: arm64: nv: Ensure correct VL is loaded before saving SVE state") > > Signed-off-by: Mark Rutland > > Reviewed-by: Mark Brown > > Tested-by: Mark Brown > > Cc: Catalin Marinas > > Cc: Fuad Tabba > > Cc: Marc Zyngier > > Cc: Oliver Upton > > Cc: Will Deacon > > Reviewed-by: Oliver Upton > > Link: https://lore.kernel.org/r/20250210195226.1215254-9-mark.rutland@arm.com > > Signed-off-by: Marc Zyngier > > Signed-off-by: Mark Brown > > --- > > arch/arm64/kvm/fpsimd.c | 30 ----------------- > > arch/arm64/kvm/hyp/entry.S | 5 +++ > > arch/arm64/kvm/hyp/include/hyp/switch.h | 59 +++++++++++++++++++++++++++++++++ > > arch/arm64/kvm/hyp/nvhe/hyp-main.c | 13 ++++---- > > arch/arm64/kvm/hyp/nvhe/switch.c | 33 +++++++++++++++--- > > arch/arm64/kvm/hyp/vhe/switch.c | 4 +++ > > 6 files changed, 103 insertions(+), 41 deletions(-) > > > > [...] > > > diff --git a/arch/arm64/kvm/hyp/nvhe/hyp-main.c b/arch/arm64/kvm/hyp/nvhe/hyp-main.c > > index 4e757a77322c9efc59cdff501745f7c80d452358..1c8e2ad32e8c396fc4b11d5fec2e86728f2829d9 100644 > > --- a/arch/arm64/kvm/hyp/nvhe/hyp-main.c > > +++ b/arch/arm64/kvm/hyp/nvhe/hyp-main.c > > @@ -5,6 +5,7 @@ > > */ > > #include > > +#include > > #include > > #include > > @@ -176,8 +177,12 @@ static void handle___kvm_vcpu_run(struct kvm_cpu_context *host_ctxt) > > sync_hyp_vcpu(hyp_vcpu); > > pkvm_put_hyp_vcpu(hyp_vcpu); > > } else { > > + struct kvm_vcpu *vcpu = kern_hyp_va(host_vcpu); > > + > > /* The host is fully trusted, run its vCPU directly. */ > > - ret = __kvm_vcpu_run(host_vcpu); > > + fpsimd_lazy_switch_to_guest(vcpu); > > + ret = __kvm_vcpu_run(vcpu); > > + fpsimd_lazy_switch_to_host(vcpu); > > } > > > > @host_vcpu should have been hypervisor's linear mapping address in v6.12. It looks > incorrect to assume it's a kernel's linear mapping address and convert it (@host_vcpu) > to the hypervisor's linear address agin, if I don't miss anything. host_vcpu is passed as a parameter to the hypercall, and is definitely a kernel address. However, at this stage, we have *already* converted it to a HYP VA: https://web.git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/arch/arm64/kvm/hyp/nvhe/hyp-main.c?h=linux-6.12.y#n147 The result is that this change is turning a perfectly valid HYP VA into... something. Odds are that the masking/patching will not mess up the address, but this is completely buggy anyway. In general, kern_hyp_va() is not an idempotent operation. Thanks for noticing that something was wrong. Broonie, can you please look into this? Greg, it may be more prudent to unstage this series from 6.12-stable until we know for sure this is the only problem. M. -- Without deviation from the norm, progress is not possible.