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 D74A2C0218F for ; Wed, 22 Jan 2025 11:52:40 +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=hO41Ocdyf/ChwO05tp6UyjANudf9xBcH27M4qrYUe2M=; b=F0DZrzhDa33FOsz4UiD0bS0wcG UvGFUxHuxTwp5L8cr4YUwDJ7fkGa2uuvTOEwZ5xWsqtFCDeKl4BpHHqZP6eNIsNk1VbH9UC9cQav4 q5Sg9EKqFm0VzFNw9hxioArk2aCFJ5YHy2fgtL1zITpCQ1+ZRjBYFSsGdZsiFBMQrsRYtW5y0MbGN oRM+scrXHvEnhPXCRgNP3M8taxiPVtTK/Je6MYo7Rv3y1Z7dPVPPgog7SUp4ruqs3gBfllQubWVIM CVZk4/56ueLaK4+jqbEAs9eV3r+Ryn9LP6zwYEgIKWAgyVKr6Mfmc1P7EY3w/O/9epjO8DhXD8kmW NwPulJDQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1taZHW-0000000A64n-16gI; Wed, 22 Jan 2025 11:52:26 +0000 Received: from dfw.source.kernel.org ([139.178.84.217]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1taZGC-0000000A5qR-1ktF for linux-arm-kernel@lists.infradead.org; Wed, 22 Jan 2025 11:51:05 +0000 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 78FE65C5D59; Wed, 22 Jan 2025 11:50:23 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8E212C4CED6; Wed, 22 Jan 2025 11:51:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1737546663; bh=63j/7wGwXuaZ+NknazkPZcLSbOn14PDE1Z9XGe727YY=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=Bm1/RMxck4trtFrCjtNR5Hr3WVRwSzcTg8mHPDDsGFzqILgDQCTHYZPFCP7yEIW5T 2Ifx9T6vuaoMd9legXGRzIRj7H0qdadj21n1znxFORY07HA1E1c8qt79AKjdjNN/aU 5/gNpls7dNGiDQsBkdCgJwBBj/s6JcIMdilgZ+fobGSlYdAMQEOpFt3Fkg3oOXt7rW UuDU+Ltd4Bpi61+iEm1Sj7e5R/tPsOnmAyYIJP0udAQSHPbn40tDten9tSmjOVkMuE Do4Lh4anKNxBzar9MAqXULoRLOlFUkcdJQpusEeTZVsjcT2hPSCJFFBkG01NPHI1bk /Up+rN3Q+Ei4Q== 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 1taZG9-00ENwN-A9; Wed, 22 Jan 2025 11:51:01 +0000 Date: Wed, 22 Jan 2025 11:51:00 +0000 Message-ID: <86o6zzukwr.wl-maz@kernel.org> From: Marc Zyngier To: Mark Rutland Cc: Mark Brown , Oliver Upton , Joey Gouly , Catalin Marinas , Suzuki K Poulose , Will Deacon , Paolo Bonzini , Jonathan Corbet , Shuah Khan , Dave Martin , Fuad Tabba , linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: Re: [PATCH RFC v3 09/27] KVM: arm64: Factor SVE guest exit handling out into a function In-Reply-To: References: <20241220-kvm-arm64-sme-v3-0-05b018c1ffeb@kernel.org> <20241220-kvm-arm64-sme-v3-9-05b018c1ffeb@kernel.org> 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: mark.rutland@arm.com, broonie@kernel.org, oliver.upton@linux.dev, joey.gouly@arm.com, catalin.marinas@arm.com, suzuki.poulose@arm.com, will@kernel.org, pbonzini@redhat.com, corbet@lwn.net, shuah@kernel.org, Dave.Martin@arm.com, tabba@google.com, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@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-20250122_035104_547113_FE6DF733 X-CRM114-Status: GOOD ( 47.36 ) 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 Fri, 17 Jan 2025 11:34:09 +0000, Mark Rutland wrote: > > On Fri, Dec 20, 2024 at 04:46:34PM +0000, Mark Brown wrote: > > The SVE portion of kvm_vcpu_put() is quite large, especially given the > > comments required. When we add similar handling for SME the function > > will get even larger, in order to keep things managable factor the SVE > > portion out of the main kvm_vcpu_put(). > > While investigating some problems with SVE I spotted a latent bug in > this area where I suspect the fix will conflict with / supersede this > rework. Details below; IIUC the bug was introduced in commit: > > 8c8010d69c132273 ("KVM: arm64: Save/restore SVE state for nVHE") > > > Signed-off-by: Mark Brown > > --- > > arch/arm64/kvm/fpsimd.c | 67 +++++++++++++++++++++++++++---------------------- > > 1 file changed, 37 insertions(+), 30 deletions(-) > > > > diff --git a/arch/arm64/kvm/fpsimd.c b/arch/arm64/kvm/fpsimd.c > > index 09b65abaf9db60cc57dbc554ad2108a80c2dc46b..3c2e0b96877ac5b4f3b9d8dfa38975f11b74b60d 100644 > > --- a/arch/arm64/kvm/fpsimd.c > > +++ b/arch/arm64/kvm/fpsimd.c > > @@ -151,6 +151,41 @@ void kvm_arch_vcpu_ctxsync_fp(struct kvm_vcpu *vcpu) > > } > > } > > > > +static void kvm_vcpu_put_sve(struct kvm_vcpu *vcpu) > > +{ > > + u64 zcr; > > + > > + if (!vcpu_has_sve(vcpu)) > > + return; > > + > > + zcr = read_sysreg_el1(SYS_ZCR); > > + > > + /* > > + * If the vCPU is in the hyp context then ZCR_EL1 is loaded > > + * with its vEL2 counterpart. > > + */ > > + __vcpu_sys_reg(vcpu, vcpu_sve_zcr_elx(vcpu)) = zcr; > > + > > + /* > > + * Restore the VL that was saved when bound to the CPU, which > > + * is the maximum VL for the guest. Because the layout of the > > + * data when saving the sve state depends on the VL, we need > > + * to use a consistent (i.e., the maximum) VL. Note that this > > + * means that at guest exit ZCR_EL1 is not necessarily the > > + * same as on guest entry. > > + * > > + * ZCR_EL2 holds the guest hypervisor's VL when running a > > + * nested guest, which could be smaller than the max for the > > + * vCPU. Similar to above, we first need to switch to a VL > > + * consistent with the layout of the vCPU's SVE state. KVM > > + * support for NV implies VHE, so using the ZCR_EL1 alias is > > + * safe. > > + */ > > + if (!has_vhe() || (vcpu_has_nv(vcpu) && !is_hyp_ctxt(vcpu))) > > + sve_cond_update_zcr_vq(vcpu_sve_max_vq(vcpu) - 1, > > + SYS_ZCR_EL1); > > +} > > + > > /* > > * Write back the vcpu FPSIMD regs if they are dirty, and invalidate the > > * cpu FPSIMD regs so that they can't be spuriously reused if this vcpu > > @@ -179,38 +214,10 @@ void kvm_arch_vcpu_put_fp(struct kvm_vcpu *vcpu) > > } > > A little before this context, kvm_arch_vcpu_put_fp() calls > local_irq_save(), which indicates that IRQs can be taken before this > point, which is deeply suspicious. > > If IRQs are enabled, then it's possible to take an IRQ and potentially > run a softirq handler which uses kernel-mode NEON. That means > kernel_neon_begin() will try to save the live FPSIMD/SVE/SME state via > fpsimd_save_user_state(), using the live value of ZCR_ELx.LEN, which would not > be correct per the comment. > > Looking at kvm_arch_vcpu_ioctl_run(), the relevant logic is: > > vcpu_load(vcpu); // calls kvm_arch_vcpu_load_fp() > > while (ret > 0) { > preempt_disable(); > local_irq_disable(); > > kvm_arch_vcpu_ctxflush_fp(); > ret = kvm_arm_vcpu_enter_exit(vcpu); > kvm_arch_vcpu_ctxsync_fp(vcpu); > > local_irq_enable(); > preempt_enable(); > } > > vcpu_put(vcpu); // calls kvm_arch_vcpu_put_fp() > > ... and the problem can occur at any point after the vCPU has run where IRQs > are enabled, i.e, between local_irq_enable() and either local_irq_disable() or > vcpu_put()'s call to kvm_arch_vcpu_put_fp(). > > Note that kernel_neon_begin() calls: > > fpsimd_save_user_state(); > ... > fpsimd_flush_cpu_state(); > > ... and fpsimd_save_user_state() will see that the SVE VL is wrong: > > if (WARN_ON(sve_get_vl() != vl)) { > force_signal_inject(SIGKILL, SI_KERNEL, 0, 0); > return; > } > > ... pending a SIGKILL for the VMM thread without saving the vCPU's state > before unbinding the live state via fpsimd_flush_cpu_state(), which'll > set TIF_FOREIGN_FPSTATE. > > AFAICT it's possible to re-enter the vCPU after that happens, whereupon > stale vCPU FPSIMD/SVE state will be restored. Upon return to userspace > the SIGKILL will be delivered, killing the VMM. > > As above, it looks like that's been broken since the nVHE SVE > save/restore was introduced in commit: > > 8c8010d69c132273 ("KVM: arm64: Save/restore SVE state for nVHE") > > The TL;DR summary is that it's not sufficient for kvm_arch_vcpu_put_fp() > to fix up ZCR_ELx. Either: > > * That needs to be fixed up while IRQs are masked, e.g. by > saving/restoring the host and guest ZCR_EL1 and/or ZCR_ELx values in > kvm_arch_vcpu_ctxflush_fp() and kvm_arch_vcpu_ctxsync_fp() > > * The lazy save logic in fpsimd_save_user_state() needs to handle KVM > explicitly, saving the guest's ZCR_EL1 and restoring the host's > ZCR_ELx. > > I think we need to fix that before we extend this logic for SME. So save/restore ZCR_ELx eagerly? If that's what it takes, let's do that now. Thanks, M. -- Without deviation from the norm, progress is not possible.