From patchwork Mon Jul 8 14:19:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13726667 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 86C23C3271E for ; Mon, 8 Jul 2024 14:22: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: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=BBngTtrENLiuwKGJgnVKMpB6+6gfpmn2p5MKjDnT+a0=; b=lK/jpcIC7dDgAVMbb7aUanD2yQ o5yLROSffkqb9z/NkcFm6Ows4hyk3PqrSvLmb534uhGlTlDILD9tfd5ai32/hRWRt3Ji/y++nmWBi 0iGq/mlXqSuMELULM2HaIPTTPbMk68nTBtlahKkWE+QoD8DYtdJlg7hVzCJ6h66hWwwG+lGURXJJw kT56B6jiw9lgwqz3CNJNM86nYsaY4JbI3urPXp+VBeC1z+blbecbXdmT5CcLUFFOv5aoI+X1WKcuC e4XTAaa5S5Nh0v8o5AelAowjXDfYraHe2GfCP05adq8KJPZte7BtLGRyMOHBVT/ppSx5nKJVQ6JI+ ahg/oGfw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sQpFk-000000043Ah-3LlL; Mon, 08 Jul 2024 14:22:04 +0000 Received: from sin.source.kernel.org ([145.40.73.55]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sQpF9-000000042uK-0GLO for linux-arm-kernel@lists.infradead.org; Mon, 08 Jul 2024 14:21:28 +0000 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 3ED97CE0CEB; Mon, 8 Jul 2024 14:21:25 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 24F07C32786; Mon, 8 Jul 2024 14:21:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720448484; bh=lND+9Wrq6h84wg/7dcgVXzhKhNM8RJROdnPLpTYN0xk=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=GaWkGbdRkfWJTCn44B+jyvf7DEOkcAdjP+Q7GAUwynSRjyx9/oK2thfv35LHDzCvm LYTBJ8gvAyWR5q7Oy7t8KOS0eWJcadvlbfybA7R3n5UiBMbyGppxz14sAYPcAgNjeE 9vv2yWC38y6FDr/UNDQ7jH0iHtrQ/lCvWeK6A+GUChkp0knVOJr56y4MOPJDIrAd3c cCjqoZC6nIELBzT2AVVpx/VONrU/AxJaHZgItiDtm/tZovejftMgsIzKrhip76HKQV hMfjf4vGpHXO7cdGBfeLKZOX/MyZGv3nnjUFVfR762Eor+pX0VOB3lvwXtQl8MjZyF hBuOX6nsaSXMA== From: Mark Brown Date: Mon, 08 Jul 2024 15:19:15 +0100 Subject: [PATCH v5 3/4] KVM: arm64: Fix FFR offset calculation for pKVM host state save and restore MIME-Version: 1.0 Message-Id: <20240708-kvm-arm64-fix-pkvm-sve-vl-v5-3-d2175738456b@kernel.org> References: <20240708-kvm-arm64-fix-pkvm-sve-vl-v5-0-d2175738456b@kernel.org> In-Reply-To: <20240708-kvm-arm64-fix-pkvm-sve-vl-v5-0-d2175738456b@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.14-dev-d4707 X-Developer-Signature: v=1; a=openpgp-sha256; l=3206; i=broonie@kernel.org; h=from:subject:message-id; bh=lND+9Wrq6h84wg/7dcgVXzhKhNM8RJROdnPLpTYN0xk=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBmi/XLR+gOeEj+oJx6GbLGY6GRKjPkxOMypVwTg0mg cp5Wv+KJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZov1ywAKCRAk1otyXVSH0N2cB/ 0XusVxwjo05ZSzJHcrIrNFF/cFKAlf/3cx4Slc8IPYSDz/JSPi5w5FT/2F5CzMoliN9owVuPNDYWxF IQcpTCybn0vX+fzS4PB+odCHagG475eG2J/t76uh4P4DCoHN+v65XJsUgi795PrcYfc+RMUx75YNgX 1vNZzZOT6lXrCqybWKKMqzl/pj9TJdPPqj/+6ILmBGmqLrVi0+iWKgLu9IvGcpBx9vnzwHVrqYJVKj rge+WgNfvRWuAg+iA7A92qsjLJKXFwHo8WJgBgyQBvFKSKBDLg5pF8BIgO1vP6eQj6/bCDAd6SGdx6 NNXfx6NdzOZolCACd146CtIzfRGCDi 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-20240708_072127_498921_E699F565 X-CRM114-Status: GOOD ( 17.70 ) 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 b05bceca3385..7510383d78a6 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 0c4de44534b7..06efcca765cc 100644 --- a/arch/arm64/kvm/hyp/include/hyp/switch.h +++ b/arch/arm64/kvm/hyp/include/hyp/switch.h @@ -327,7 +327,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 f43d845f3c4e..bd8f671e848c 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);