From patchwork Tue Oct 1 11:29:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13817887 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 B1549CE7D10 for ; Tue, 1 Oct 2024 11:38:59 +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:Cc:To:In-Reply-To:References :Message-Id:Content-Transfer-Encoding:Content-Type:MIME-Version:Subject:Date: From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=EMn+G/Hqtr1xLFXhEJrUZRizkVtxOr6MSEo1E8c+y6k=; b=Hod8RuG/ByIUOoP2BJVK2lgnpS Tm92yZdBldAs3WggV709TGFLCuwsHOImB0UzBRBSd66PXhjiZq7g2pTOsaKX7TGw90MZtFIY9eoXl 5NyEok1sHwx7v8BI0DI62s4o7c5xs8SjW2B4MnbmPqPn8QHSsZ5Ptzdn1WdZyh63mGhV47y4o56wm KLAhAoXsuj74yvfZ68YrRUAy1l4EC0KH730S0xNBHGm7JEGLuuCYY2kSG/99Qg/QGY4Tr66ofqAZh SdXLVqYxj/NmWURmLwiipzk5GJXHEsZ26/PdRcei9SDwlf6oAQBy3j99PmZn6e3Q/iRvwToaqp4sa TLsQRgWQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1svbDM-00000002YCf-22MR; Tue, 01 Oct 2024 11:38:48 +0000 Received: from nyc.source.kernel.org ([2604:1380:45d1:ec00::3]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1svb6A-00000002WHt-245F for linux-arm-kernel@lists.infradead.org; Tue, 01 Oct 2024 11:31:24 +0000 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 70631A435A6; Tue, 1 Oct 2024 11:31:13 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 96B0CC4CEC6; Tue, 1 Oct 2024 11:31:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1727782281; bh=KBunKuidQbGsl1sTPDqT1ibl0GGL5dnEQFUfFDn0MP8=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=f2utS53aPNMtAdR2kLn87aubYqKj6soMe/MmzTiwRNusjBY9Kvtd6t8lsrYC66qcT 2caAWmgTZjnL+MHHlShC8N1E5MZh1IxjUtqpkiWJaPvzht7rXS+SsapTNPfR7AnDBL kd0KFzAU0ScB+S3JML/yRqKEEu9pw3uN7bwg+5xpWUHyNm1helyuJvmXq9TPBplb8A nPPMnzlWOtlnFGdwgGboF1PdVpxfvmyVjzv6qNv1tFMQljPYocbhF/KUky0C4mpNup HMgVOpIx79OsWdmel+ctIodRvkcMSPFpOxF+QM8QYCqtz6ulqYb8ZUfQ85kJvuaUhG HMwZEG1sWDMQQ== From: Mark Brown Date: Tue, 01 Oct 2024 12:29:08 +0100 Subject: [PATCH v7 3/4] KVM: arm64: Fix FFR offset calculation for pKVM host state save and restore MIME-Version: 1.0 Message-Id: <20241001-kvm-arm64-fix-pkvm-sve-vl-v7-3-7b0171a36695@kernel.org> References: <20241001-kvm-arm64-fix-pkvm-sve-vl-v7-0-7b0171a36695@kernel.org> In-Reply-To: <20241001-kvm-arm64-fix-pkvm-sve-vl-v7-0-7b0171a36695@kernel.org> To: Catalin Marinas , Will Deacon , Marc Zyngier , Oliver Upton , James Morse , Suzuki K Poulose , Fuad Tabba Cc: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, kvmarm@lists.linux.dev, Mark Brown X-Mailer: b4 0.15-dev-99b12 X-Developer-Signature: v=1; a=openpgp-sha256; l=3206; i=broonie@kernel.org; h=from:subject:message-id; bh=KBunKuidQbGsl1sTPDqT1ibl0GGL5dnEQFUfFDn0MP8=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBm+91952Fz2ONJP3ZDAU2Oql7Admcy1BKuIxh9+12i 7GtyhbuJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZvvdfQAKCRAk1otyXVSH0PTkB/ 407zY0DEiqK+eF3/i3xkdy/woU0hcDfHxTybQd/T6pvgXBawysTNgYjQ2CogGCSBbw2pDvtNcedfI3 0IVNF17OOUZiVB7M0DGQjX4C85sN4i+JFZ/Hm0uVzONX/hAWX3wPafc6YTKZc2kzsmQGz4IE7PnNNt sVBUmV/EeSNHYzoqbJfdHp2+sb36lNGBL3fzSUOPs8Ubt16DA8/+rYRzbtQcM+RjSw/cJ0kY8k0FTA rax6ye6OXZGFwiJGCDaYpnZlUDwxpl9QKgYDr11tRok3EbtntYmW6c4HzPhv1i/k7JWARv+cB3Dod7 6JZh3CHiXQ9r4iQB/izB/VgNa4fdJm X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241001_043122_709771_7D139D37 X-CRM114-Status: GOOD ( 17.78 ) 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 When saving and restoring the SVE state for the host we configure the hardware for the maximum VL it supports, but when calculating offsets in memory we use the maximum usable VL for the host. Since these two values may not be the same this may result in data corruption in the case where the PE supports a VL larger than the maximum usable VL for the host. We can just read the current VL from the hardware with an instruction so use that instead of a saved value, we need to correct the value used to lay out the stored data and this makes it clear that the layout is consistent with the hardware configuration. Fixes: b5b9955617bc ("KVM: arm64: Eagerly restore host fpsimd/sve state in pKVM") Signed-off-by: Mark Brown --- arch/arm64/include/asm/kvm_hyp.h | 1 + arch/arm64/kvm/hyp/fpsimd.S | 5 +++++ arch/arm64/kvm/hyp/include/hyp/switch.h | 2 +- arch/arm64/kvm/hyp/nvhe/hyp-main.c | 2 +- 4 files changed, 8 insertions(+), 2 deletions(-) diff --git a/arch/arm64/include/asm/kvm_hyp.h b/arch/arm64/include/asm/kvm_hyp.h index c838309e4ec4..6b074f4d48b2 100644 --- a/arch/arm64/include/asm/kvm_hyp.h +++ b/arch/arm64/include/asm/kvm_hyp.h @@ -113,6 +113,7 @@ void __fpsimd_save_state(struct user_fpsimd_state *fp_regs); void __fpsimd_restore_state(struct user_fpsimd_state *fp_regs); void __sve_save_state(void *sve_pffr, u32 *fpsr, int save_ffr); void __sve_restore_state(void *sve_pffr, u32 *fpsr, int restore_ffr); +int __sve_get_vl(void); u64 __guest_enter(struct kvm_vcpu *vcpu); diff --git a/arch/arm64/kvm/hyp/fpsimd.S b/arch/arm64/kvm/hyp/fpsimd.S index e950875e31ce..d272dbf36da8 100644 --- a/arch/arm64/kvm/hyp/fpsimd.S +++ b/arch/arm64/kvm/hyp/fpsimd.S @@ -31,3 +31,8 @@ SYM_FUNC_START(__sve_save_state) sve_save 0, x1, x2, 3 ret SYM_FUNC_END(__sve_save_state) + +SYM_FUNC_START(__sve_get_vl) + _sve_rdvl 0, 1 + ret +SYM_FUNC_END(__sve_get_vl) diff --git a/arch/arm64/kvm/hyp/include/hyp/switch.h b/arch/arm64/kvm/hyp/include/hyp/switch.h index 46d52e8a3df3..58f60ea95a35 100644 --- a/arch/arm64/kvm/hyp/include/hyp/switch.h +++ b/arch/arm64/kvm/hyp/include/hyp/switch.h @@ -339,7 +339,7 @@ static inline void __hyp_sve_save_host(void) sve_state->zcr_el1 = read_sysreg_el1(SYS_ZCR); write_sysreg_s(ZCR_ELx_LEN_MASK, SYS_ZCR_EL2); - __sve_save_state(sve_state->sve_regs + sve_ffr_offset(kvm_host_sve_max_vl), + __sve_save_state(sve_state->sve_regs + sve_ffr_offset(__sve_get_vl()), &sve_state->fpsr, true); } diff --git a/arch/arm64/kvm/hyp/nvhe/hyp-main.c b/arch/arm64/kvm/hyp/nvhe/hyp-main.c index 87692b566d90..48adcd005079 100644 --- a/arch/arm64/kvm/hyp/nvhe/hyp-main.c +++ b/arch/arm64/kvm/hyp/nvhe/hyp-main.c @@ -49,7 +49,7 @@ static void __hyp_sve_restore_host(void) * supported by the system (or limited at EL3). */ write_sysreg_s(ZCR_ELx_LEN_MASK, SYS_ZCR_EL2); - __sve_restore_state(sve_state->sve_regs + sve_ffr_offset(kvm_host_sve_max_vl), + __sve_restore_state(sve_state->sve_regs + sve_ffr_offset(__sve_get_vl()), &sve_state->fpsr, true); write_sysreg_el1(sve_state->zcr_el1, SYS_ZCR);