From nobody Mon Apr 6 21:59:16 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C3897312837; Tue, 17 Mar 2026 16:23:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773764597; cv=none; b=W9zGQiwQ7gFWy+ytUsi93/wPfnKTTqqyh43c5NzdaDnpChuSoAesnrKoUzQMwv/pkQgBp2ZGaBkdNrwauOggXyRPrvBBNiX2bPh0APqIgKnWisOxS7YSA6F/yWN84ZQ343LeD71yogPu1VjCbHEJQQDeRS97+++fpGzZDB/K9+s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773764597; c=relaxed/simple; bh=q3LEwBb48yBUwAP/JStffirx0olu9s+cHwEqdRIrL1A=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=s0W1oR2n1gBc1+uDIr5bA/+VZPjomowGANEj03UD7Ko249qrB/2C5xtYZ38xf2R+9vKkdjo4c+4YALbnLEzBLmO/brcNfwvAutJg329xdcBiAK2V8EtM+YySb21bVipkeepIMl4W7tCb7ti12i2O21VdzjFSnnpsz3NFJ24Ojpk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=oKHKocuS; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="oKHKocuS" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2D23DC19424; Tue, 17 Mar 2026 16:23:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1773764597; bh=q3LEwBb48yBUwAP/JStffirx0olu9s+cHwEqdRIrL1A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=oKHKocuSm6iSTQ9qKQBPFGQQaWXV2krpuRnmLUtuBtzbcrfgL0n+3I/utcv2Fn23Z invniLBcyYfTrnl7d3fLXMuo21YViIxu0bCk3bhcgBDinnE0EL4u8N/FQs96+sDoOj l0cJK4Pt2rrfesirbvBNIRBRB35GfcSKBcO5Kq6tzKGlQAFz9Mgok3v2UWbAb9XPXo xzLKy3XCy15GxMIEHSQ0WluCXvwIjHaiibkLSvrsIENORLQCngJJlD1a3r7vtSOHRR pVfKHWVxxqA9Exuw7yXcimUkWvJ3issoxtTPlebWREc/7rJSFL6If3QIM0r7Oifu01 aJ2EvPhJ2G+JA== From: Tycho Andersen To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Ashish Kalra , Tom Lendacky , John Allen , Herbert Xu , "David S. Miller" , Ard Biesheuvel , Neeraj Upadhyay , Kishon Vijay Abraham I , Alexey Kardashevskiy , Nikunj A Dadhania , "Peter Zijlstra (Intel)" , Kim Phillips , Sean Christopherson Cc: linux-kernel@vger.kernel.org, linux-crypto@vger.kernel.org, "Tycho Andersen (AMD)" Subject: [PATCH v3 1/7] x86/snp: Create a function to clear/zero the RMP Date: Tue, 17 Mar 2026 10:21:51 -0600 Message-ID: <20260317162157.150842-2-tycho@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260317162157.150842-1-tycho@kernel.org> References: <20260317162157.150842-1-tycho@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Tom Lendacky In preparation for delayed SNP initialization and disablement on shutdown, create a function, clear_rmp(), that clears the RMP bookkeeping area and the RMP entries. Signed-off-by: Tom Lendacky Signed-off-by: Tycho Andersen (AMD) --- arch/x86/virt/svm/sev.c | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/arch/x86/virt/svm/sev.c b/arch/x86/virt/svm/sev.c index e35fac0a8a3d..025606969823 100644 --- a/arch/x86/virt/svm/sev.c +++ b/arch/x86/virt/svm/sev.c @@ -242,6 +242,32 @@ void __init snp_fixup_e820_tables(void) } } =20 +static void clear_rmp(void) +{ + unsigned int i; + u64 val; + + if (!cc_platform_has(CC_ATTR_HOST_SEV_SNP)) + return; + + /* Clearing the RMP while SNP is enabled will cause an exception */ + rdmsrq(MSR_AMD64_SYSCFG, val); + if (WARN_ON_ONCE(val & MSR_AMD64_SYSCFG_SNP_EN)) + return; + + memset(rmp_bookkeeping, 0, RMPTABLE_CPU_BOOKKEEPING_SZ); + + for (i =3D 0; i < rst_max_index; i++) { + struct rmp_segment_desc *desc; + + desc =3D rmp_segment_table[i]; + if (!desc) + continue; + + memset(desc->rmp_entry, 0, desc->size); + } +} + static bool __init alloc_rmp_segment_desc(u64 segment_pa, u64 segment_size= , u64 pa) { u64 rst_index, rmp_segment_size_max; @@ -484,7 +510,6 @@ static bool __init setup_rmptable(void) */ int __init snp_rmptable_init(void) { - unsigned int i; u64 val; =20 if (WARN_ON_ONCE(!cc_platform_has(CC_ATTR_HOST_SEV_SNP))) @@ -504,19 +529,7 @@ int __init snp_rmptable_init(void) if (val & MSR_AMD64_SYSCFG_SNP_EN) goto skip_enable; =20 - /* Zero out the RMP bookkeeping area */ - memset(rmp_bookkeeping, 0, RMPTABLE_CPU_BOOKKEEPING_SZ); - - /* Zero out the RMP entries */ - for (i =3D 0; i < rst_max_index; i++) { - struct rmp_segment_desc *desc; - - desc =3D rmp_segment_table[i]; - if (!desc) - continue; - - memset(desc->rmp_entry, 0, desc->size); - } + clear_rmp(); =20 /* MtrrFixDramModEn must be enabled on all the CPUs prior to enabling SNP= . */ on_each_cpu(mfd_enable, NULL, 1); --=20 2.53.0 From nobody Mon Apr 6 21:59:16 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1338F313E29; Tue, 17 Mar 2026 16:23:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773764600; cv=none; b=nYon7mdqHJfzZooF27KD9XKO9LG41TKZYe4v8pYX7ROjRvlj2fn6EWNQGwevtw+vQh/fXKVG76Icz04n/7B++0JMVCrTU/zc6EyD+y32rDvDySEYj9GDr6FO3jVm8IuQRV8ZDJU83K/FwM2t36jqQgqNrYStZ3Ig7xlhwvjTGFM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773764600; c=relaxed/simple; bh=XougUI4HEdJfnql2zro3FAfti2nnSpIp8afYnCGP7F4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=OhFzY/d4TV2fwNSlwhAPjZI/fZQ+VASuN51pxdXWssbO3Qz9tls/27fKNNlIjw8UxHBmeDZKfwhwTQHsye9YxhZIcCfTjU2abulMrmqp1yUrFc8maI629epIKYkQRZbctz2hnnofO8zws/szgmq6ZKM2Ohq3ruNeRarSXI6a67w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=FPC+Cjac; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="FPC+Cjac" Received: by smtp.kernel.org (Postfix) with ESMTPSA id AA35AC4AF0C; Tue, 17 Mar 2026 16:23:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1773764599; bh=XougUI4HEdJfnql2zro3FAfti2nnSpIp8afYnCGP7F4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FPC+CjacKkODG+vpAkIw/2wJabxdxYD5v5rkbC2/oRD5XTo/odqiKQygCEiDax66x 7AGo/+XLmVlg46qVZGidvOEVXGk1Ca7PSk4dXVOTeRWXZSG8VBzl1E7IZlljwXURFY kLDVw+nS8W7IIPrEeK10LCKGd2wMS5sU/VwaT4LDVNygksGes+UZOjRVyD0kRA/ytX 04XBMdze02EXk6bLTDEI52CWEYGwANdwEqRDNovZcQQGYnVveTm7hcCwlHnrJFLnwl V4b2M+rcguotFbPA9/pzjqTXvTKQXOAi8RvSvRzaJRClS5S5beTxAuxzBB9/IJ5vta Oxjp0V75228Jg== From: Tycho Andersen To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Ashish Kalra , Tom Lendacky , John Allen , Herbert Xu , "David S. Miller" , Ard Biesheuvel , Neeraj Upadhyay , Kishon Vijay Abraham I , Alexey Kardashevskiy , Nikunj A Dadhania , "Peter Zijlstra (Intel)" , Kim Phillips , Sean Christopherson Cc: linux-kernel@vger.kernel.org, linux-crypto@vger.kernel.org, "Tycho Andersen (AMD)" Subject: [PATCH v3 2/7] x86/snp: create snp_prepare_for_snp_init() Date: Tue, 17 Mar 2026 10:21:52 -0600 Message-ID: <20260317162157.150842-3-tycho@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260317162157.150842-1-tycho@kernel.org> References: <20260317162157.150842-1-tycho@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: "Tycho Andersen (AMD)" In preparation for delayed SNP initialization, create a function snp_prepare_for_snp_init() that does the necessary architecture setup. Export this function for the ccp module to allow it to do the setup as necessary. Also move {mfd,snp}_enable out of the __init section, since these will be called later. Signed-off-by: Tycho Andersen (AMD) Reviewed-by: Tom Lendacky --- arch/x86/include/asm/sev.h | 2 ++ arch/x86/virt/svm/sev.c | 46 ++++++++++++++++++++++---------------- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index 0e6c0940100f..0bcd89d4fe90 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -661,6 +661,7 @@ static inline void snp_leak_pages(u64 pfn, unsigned int= pages) { __snp_leak_pages(pfn, pages, true); } +void snp_prepare_for_snp_init(void); #else static inline bool snp_probe_rmptable_info(void) { return false; } static inline int snp_rmptable_init(void) { return -ENOSYS; } @@ -677,6 +678,7 @@ static inline void __snp_leak_pages(u64 pfn, unsigned i= nt npages, bool dump_rmp) static inline void snp_leak_pages(u64 pfn, unsigned int npages) {} static inline void kdump_sev_callback(void) { } static inline void snp_fixup_e820_tables(void) {} +static inline void snp_prepare_for_snp_init(void) {} #endif =20 #endif diff --git a/arch/x86/virt/svm/sev.c b/arch/x86/virt/svm/sev.c index 025606969823..88cb4a548701 100644 --- a/arch/x86/virt/svm/sev.c +++ b/arch/x86/virt/svm/sev.c @@ -132,7 +132,7 @@ static unsigned long snp_nr_leaked_pages; #undef pr_fmt #define pr_fmt(fmt) "SEV-SNP: " fmt =20 -static __init void mfd_enable(void *arg) +static void mfd_enable(void *arg) { if (!cc_platform_has(CC_ATTR_HOST_SEV_SNP)) return; @@ -140,7 +140,7 @@ static __init void mfd_enable(void *arg) msr_set_bit(MSR_AMD64_SYSCFG, MSR_AMD64_SYSCFG_MFDM_BIT); } =20 -static __init void snp_enable(void *arg) +static void snp_enable(void *arg) { u64 val; =20 @@ -503,6 +503,30 @@ static bool __init setup_rmptable(void) return true; } =20 +void snp_prepare_for_snp_init(void) +{ + u64 val; + + /* + * Check if SEV-SNP is already enabled, this can happen in case of + * kexec boot. + */ + rdmsrq(MSR_AMD64_SYSCFG, val); + if (val & MSR_AMD64_SYSCFG_SNP_EN) + return; + + clear_rmp(); + + /* + * MtrrFixDramModEn is not shared between threads on a core, + * therefore it must be set on all CPUs prior to enabling SNP. + */ + on_each_cpu(mfd_enable, NULL, 1); + + on_each_cpu(snp_enable, NULL, 1); +} +EXPORT_SYMBOL_FOR_MODULES(snp_prepare_for_snp_init, "ccp"); + /* * Do the necessary preparations which are verified by the firmware as * described in the SNP_INIT_EX firmware command description in the SNP @@ -510,8 +534,6 @@ static bool __init setup_rmptable(void) */ int __init snp_rmptable_init(void) { - u64 val; - if (WARN_ON_ONCE(!cc_platform_has(CC_ATTR_HOST_SEV_SNP))) return -ENOSYS; =20 @@ -521,22 +543,8 @@ int __init snp_rmptable_init(void) if (!setup_rmptable()) return -ENOSYS; =20 - /* - * Check if SEV-SNP is already enabled, this can happen in case of - * kexec boot. - */ - rdmsrq(MSR_AMD64_SYSCFG, val); - if (val & MSR_AMD64_SYSCFG_SNP_EN) - goto skip_enable; - - clear_rmp(); - - /* MtrrFixDramModEn must be enabled on all the CPUs prior to enabling SNP= . */ - on_each_cpu(mfd_enable, NULL, 1); - - on_each_cpu(snp_enable, NULL, 1); + snp_prepare_for_snp_init(); =20 -skip_enable: /* * Setting crash_kexec_post_notifiers to 'true' to ensure that SNP panic * notifier is invoked to do SNP IOMMU shutdown before kdump. --=20 2.53.0 From nobody Mon Apr 6 21:59:16 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8A89531619D; Tue, 17 Mar 2026 16:23:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773764602; cv=none; b=m3/8sBE08UQqfQb2uwQS3Ombl3H9T4VaGpBWIKiFxXo1T47j7S+SSOWNklAbbErTQmD4qYUWX9Hy+jQEvEBMgcvMSxm56+y7WSmPdGTCMAb4ykSRnvzaSIToS5M2gdMLh1Llv+9oVHWdXvtluuepv4JDFrPSp1XO7NIAkDbyADY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773764602; c=relaxed/simple; bh=XU6lMFg6u5MrLH5FDokrBVqlq/G8SBUCZxzvGtE3k+w=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=bCicByVgumaZxeTh+CYDv3nAu1ssmAv7W+wakXX3kfVLEJdj8c5VJkgGFrQg1A1hn9uJJkS1xO6wNdyUtRpZ3Z9puDqy9iSOrh8bcVVtnnS2ryBGiOIvjOHekyGg1xk2LKvNL4y7G4lK53l11Xnyf4do/Z/f9Pby7w0vlT7+xL0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=qeVygaj+; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="qeVygaj+" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 31DFDC4CEF7; Tue, 17 Mar 2026 16:23:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1773764602; bh=XU6lMFg6u5MrLH5FDokrBVqlq/G8SBUCZxzvGtE3k+w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qeVygaj+LyUrVZcCyNdagGKreopjTZn7S4GYPQjfdoSxcoQI5WgMYzmRYWOtHz1I7 I20vhufey8HADCDgVI0lgI8RB4Cq0EC4jXb9vTqYl/+riahxt68+AzWkKQtej/ISto AFn+8xLrcHN0U9uKaF91OpHWCtVLQ99bWpjNhLO56TDyXucuseTKgPpITunjFslEFc asoRi4J1h6eEupL1fGteCjAq+BzBL2fdzubCNof4ePGeFtS8+Y3LeUpJvNb48yb5l0 6C5VKza3f/2ChsyXfCZrxp8gqT0ReKRbmt1LOnDzRgWH26cZduI2SwP5vBFhOJJEQg 6Qq41P61o/Wfw== From: Tycho Andersen To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Ashish Kalra , Tom Lendacky , John Allen , Herbert Xu , "David S. Miller" , Ard Biesheuvel , Neeraj Upadhyay , Kishon Vijay Abraham I , Alexey Kardashevskiy , Nikunj A Dadhania , "Peter Zijlstra (Intel)" , Kim Phillips , Sean Christopherson Cc: linux-kernel@vger.kernel.org, linux-crypto@vger.kernel.org, "Tycho Andersen (AMD)" Subject: [PATCH v3 3/7] x86/snp: create snp_x86_shutdown() Date: Tue, 17 Mar 2026 10:21:53 -0600 Message-ID: <20260317162157.150842-4-tycho@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260317162157.150842-1-tycho@kernel.org> References: <20260317162157.150842-1-tycho@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: "Tycho Andersen (AMD)" After SNP_SHUTDOWN, two architecture-level things should be done: 1. clear the RMP table 2. disable MFDM to prevent the FW_WARN in k8_check_syscfg_dram_mod_en() in the event of a kexec Create and export to the CCP driver a function that does them. Also change the MFDM helper to allow for disabling the bit, since the SNP x86 shutdown path needs to disable MFDM. The comment for k8_check_syscfg_dram_mod_en() notes, the "BIOS" is supposed clear it, or the kernel in the case of module unload and shutdown followed by kexec. Signed-off-by: Tycho Andersen (AMD) Reviewed-by: Tom Lendacky --- arch/x86/include/asm/sev.h | 2 ++ arch/x86/virt/svm/sev.c | 23 ++++++++++++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index 0bcd89d4fe90..36d2b1ea19c0 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -662,6 +662,7 @@ static inline void snp_leak_pages(u64 pfn, unsigned int= pages) __snp_leak_pages(pfn, pages, true); } void snp_prepare_for_snp_init(void); +void snp_x86_shutdown(void); #else static inline bool snp_probe_rmptable_info(void) { return false; } static inline int snp_rmptable_init(void) { return -ENOSYS; } @@ -679,6 +680,7 @@ static inline void snp_leak_pages(u64 pfn, unsigned int= npages) {} static inline void kdump_sev_callback(void) { } static inline void snp_fixup_e820_tables(void) {} static inline void snp_prepare_for_snp_init(void) {} +static inline void snp_x86_shutdown(void) {} #endif =20 #endif diff --git a/arch/x86/virt/svm/sev.c b/arch/x86/virt/svm/sev.c index 88cb4a548701..85091d663f18 100644 --- a/arch/x86/virt/svm/sev.c +++ b/arch/x86/virt/svm/sev.c @@ -132,12 +132,15 @@ static unsigned long snp_nr_leaked_pages; #undef pr_fmt #define pr_fmt(fmt) "SEV-SNP: " fmt =20 -static void mfd_enable(void *arg) +static void mfd_reconfigure(void *arg) { if (!cc_platform_has(CC_ATTR_HOST_SEV_SNP)) return; =20 - msr_set_bit(MSR_AMD64_SYSCFG, MSR_AMD64_SYSCFG_MFDM_BIT); + if (arg) + msr_set_bit(MSR_AMD64_SYSCFG, MSR_AMD64_SYSCFG_MFDM_BIT); + else + msr_clear_bit(MSR_AMD64_SYSCFG, MSR_AMD64_SYSCFG_MFDM_BIT); } =20 static void snp_enable(void *arg) @@ -521,12 +524,26 @@ void snp_prepare_for_snp_init(void) * MtrrFixDramModEn is not shared between threads on a core, * therefore it must be set on all CPUs prior to enabling SNP. */ - on_each_cpu(mfd_enable, NULL, 1); + on_each_cpu(mfd_reconfigure, (void *)1, 1); =20 on_each_cpu(snp_enable, NULL, 1); } EXPORT_SYMBOL_FOR_MODULES(snp_prepare_for_snp_init, "ccp"); =20 +void snp_x86_shutdown(void) +{ + u64 syscfg; + + rdmsrq(MSR_AMD64_SYSCFG, syscfg); + + if (syscfg & MSR_AMD64_SYSCFG_SNP_EN) + return; + + clear_rmp(); + on_each_cpu(mfd_reconfigure, 0, 1); +} +EXPORT_SYMBOL_FOR_MODULES(snp_x86_shutdown, "ccp"); + /* * Do the necessary preparations which are verified by the firmware as * described in the SNP_INIT_EX firmware command description in the SNP --=20 2.53.0 From nobody Mon Apr 6 21:59:16 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4EF1B315D49; Tue, 17 Mar 2026 16:23:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773764605; cv=none; b=s3BnRCUoKYCy3aG5nB4MWKR42ae2nnO6CYOzTijZFu2WllojW55qovlWBOvtXpqwfgM9lFYwUqHRHhyQfrZ1eNOOfuKQJcrhqysscNQoxjIo7krzF+rsk+UjU5CNhzIS1vTB5rZT69UELc+8+nfGSmf1ps8IgT2Ll32t8XcEnSc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773764605; c=relaxed/simple; bh=LTvMOHz8M1itT3zsBfeKdK/QodsxREQ0/QYy8N4j50o=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=oisF4VyTtZ4JvJoQ7kqW3LUUr+82uUupUk2O+mWu5OhRnsK1XAtPNSJCyMaffpEy8TGBGSV1dTEYNlUTjm+1CImf2rb+N4eesrwMc76JL6qvlebpXu/kt8IPFA9aun5oejXlSD/IXJBI7ZQkBjwtKEEVBXgEq4yGrzcN28GmsKw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=LQJFcbKp; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="LQJFcbKp" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B012FC2BCAF; Tue, 17 Mar 2026 16:23:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1773764604; bh=LTvMOHz8M1itT3zsBfeKdK/QodsxREQ0/QYy8N4j50o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LQJFcbKpxev2YH/BRuscFtU9+wN90LoTng7ZXqILsW2wT9lC+oUMTbSb+Jan2yP8f 0jqI6DGBCcklfz8S9oxjWs9ZzjUYf7T6g4xjM+xa69Rnyg1r07mV2phrxTczui5pB0 IM8KERoxUxe01nh+3R7C6FWi6yzoWTj3uU2ZhkeAzAnWROgRj7vIrTJuHjJ/c+fnUQ 7E17No9AY3DFqu0eV+D0SWHWx+oPUiuVL7rad7jon9km5hCT7q85E/SWxxt4PJALr7 qx0EfJN5Uai3lTllMKUWtyrANXpiqSF9uFsjvmw6KtLxri9DK9VGqH3qQw66HbALIw Mi0AMt52WdyZA== From: Tycho Andersen To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Ashish Kalra , Tom Lendacky , John Allen , Herbert Xu , "David S. Miller" , Ard Biesheuvel , Neeraj Upadhyay , Kishon Vijay Abraham I , Alexey Kardashevskiy , Nikunj A Dadhania , "Peter Zijlstra (Intel)" , Kim Phillips , Sean Christopherson Cc: linux-kernel@vger.kernel.org, linux-crypto@vger.kernel.org, "Tycho Andersen (AMD)" Subject: [PATCH v3 4/7] x86/snp, crypto: move SNP init to ccp driver Date: Tue, 17 Mar 2026 10:21:54 -0600 Message-ID: <20260317162157.150842-5-tycho@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260317162157.150842-1-tycho@kernel.org> References: <20260317162157.150842-1-tycho@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: "Tycho Andersen (AMD)" Use the new snp_prepare_for_snp_init() to initialize SNP from the ccp driver instead of at boot time. This means that SNP is not enabled unless it is really going to be used (i.e. kvm_amd loads the ccp driver automatically). Signed-off-by: Tycho Andersen (AMD) Reviewed-by: Tom Lendacky --- arch/x86/virt/svm/sev.c | 2 -- drivers/crypto/ccp/sev-dev.c | 2 ++ 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/arch/x86/virt/svm/sev.c b/arch/x86/virt/svm/sev.c index 85091d663f18..b73ea987c69c 100644 --- a/arch/x86/virt/svm/sev.c +++ b/arch/x86/virt/svm/sev.c @@ -560,8 +560,6 @@ int __init snp_rmptable_init(void) if (!setup_rmptable()) return -ENOSYS; =20 - snp_prepare_for_snp_init(); - /* * Setting crash_kexec_post_notifiers to 'true' to ensure that SNP panic * notifier is invoked to do SNP IOMMU shutdown before kdump. diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index 8b2dfc11289b..07c4736a1f0a 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -1373,6 +1373,8 @@ static int __sev_snp_init_locked(int *error, unsigned= int max_snp_asid) return -EOPNOTSUPP; } =20 + snp_prepare_for_snp_init(); + /* SNP_INIT requires MSR_VM_HSAVE_PA to be cleared on all CPUs. */ on_each_cpu(snp_set_hsave_pa, NULL, 1); =20 --=20 2.53.0 From nobody Mon Apr 6 21:59:16 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E307931985D; Tue, 17 Mar 2026 16:23:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773764608; cv=none; b=hiZ9Det66m7ADxBFa6z4XyKGIrSgTWoHvhqVvQXFi1bqH2oL5lSdVVqG0CvysreRDC/WxLfH8JrhQWyyurqwjXvDoOJwnG1UrutazJbccEYNSPhuyZh3pmUQXizNmeM20T8M7OWQlai1HgUGsmmEdS/qMzmgUKdjZG14bIj3mXk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773764608; c=relaxed/simple; bh=3cJD+C7Lp8IgqeilNWaoB/WV+RuJtFKWuxAfDC1i3Ls=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=LWgeOOY9Ywv3B5J5zKbabiEXmYyShLS6OSO9hKEZNdtRzldg/JGdmd6Dg15z2h7I1YA73/Rqs3tmJo4W+/1T3Qrb61fNsoTB7zEGaR2CRA6EM51TrhZ2cuoHKeyew2/Tlka1ar/Zeao4JLgymhSFd2pjwFerriV8IKZ8Zh99iXI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=UFEGU5v7; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="UFEGU5v7" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 393F6C19424; Tue, 17 Mar 2026 16:23:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1773764607; bh=3cJD+C7Lp8IgqeilNWaoB/WV+RuJtFKWuxAfDC1i3Ls=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UFEGU5v7VwloOgSdjLZ6ck7xY0EntsVmMaE93bOxGhGNyhusuAr1jiJCKHhixdT/o wUhZKbUgaOuHWiSpETxih2Z/WVtSH7wdPbT7m074lwyXF47LjwONhmFyqWpeK1BwSU TyxEbz8EALTu36dx/5LYSfANllFqgW16y+QmDpMgBsu4t0OGaQdy0jg73J5xtaTQNV hNGD6SGUfcCj00ZKf+YAzSoXJC9Wx+hbf7Vpg0+5AO4d4CX6Yll/Q3C7qT7xxDTsDg A64/pOWDige99ambQevkVlhBtFxWlbwF0FlBxbhnjXatBrhJd1FJhAq4ynC26KrTEz 0hEyWnppIR8yQ== From: Tycho Andersen To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Ashish Kalra , Tom Lendacky , John Allen , Herbert Xu , "David S. Miller" , Ard Biesheuvel , Neeraj Upadhyay , Kishon Vijay Abraham I , Alexey Kardashevskiy , Nikunj A Dadhania , "Peter Zijlstra (Intel)" , Kim Phillips , Sean Christopherson Cc: linux-kernel@vger.kernel.org, linux-crypto@vger.kernel.org, "Tycho Andersen (AMD)" Subject: [PATCH v3 5/7] x86/snp, crypto: move HSAVE_PA setup to arch/ Date: Tue, 17 Mar 2026 10:21:55 -0600 Message-ID: <20260317162157.150842-6-tycho@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260317162157.150842-1-tycho@kernel.org> References: <20260317162157.150842-1-tycho@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: "Tycho Andersen (AMD)" Now that there is snp_prepare_for_snp_init() that indicates when the CCP driver wants to prepare the architecture for SNP_INIT(_EX), move this architecture-specific bit of code to a more sensible place. Signed-off-by: Tycho Andersen (AMD) Reviewed-by: Tom Lendacky --- arch/x86/virt/svm/sev.c | 8 ++++++++ drivers/crypto/ccp/sev-dev.c | 8 -------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/arch/x86/virt/svm/sev.c b/arch/x86/virt/svm/sev.c index b73ea987c69c..e856c13cfdaa 100644 --- a/arch/x86/virt/svm/sev.c +++ b/arch/x86/virt/svm/sev.c @@ -506,6 +506,11 @@ static bool __init setup_rmptable(void) return true; } =20 +static void snp_set_hsave_pa(void *arg) +{ + wrmsrq(MSR_VM_HSAVE_PA, 0); +} + void snp_prepare_for_snp_init(void) { u64 val; @@ -527,6 +532,9 @@ void snp_prepare_for_snp_init(void) on_each_cpu(mfd_reconfigure, (void *)1, 1); =20 on_each_cpu(snp_enable, NULL, 1); + + /* SNP_INIT requires MSR_VM_HSAVE_PA to be cleared on all CPUs. */ + on_each_cpu(snp_set_hsave_pa, NULL, 1); } EXPORT_SYMBOL_FOR_MODULES(snp_prepare_for_snp_init, "ccp"); =20 diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index 07c4736a1f0a..b10104f243b9 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -1076,11 +1076,6 @@ static inline int __sev_do_init_locked(int *psp_ret) return __sev_init_locked(psp_ret); } =20 -static void snp_set_hsave_pa(void *arg) -{ - wrmsrq(MSR_VM_HSAVE_PA, 0); -} - /* Hypervisor Fixed pages API interface */ static void snp_hv_fixed_pages_state_update(struct sev_device *sev, enum snp_hv_fixed_pages_state page_state) @@ -1375,9 +1370,6 @@ static int __sev_snp_init_locked(int *error, unsigned= int max_snp_asid) =20 snp_prepare_for_snp_init(); =20 - /* SNP_INIT requires MSR_VM_HSAVE_PA to be cleared on all CPUs. */ - on_each_cpu(snp_set_hsave_pa, NULL, 1); - /* * Starting in SNP firmware v1.52, the SNP_INIT_EX command takes a list * of system physical address ranges to convert into HV-fixed page --=20 2.53.0 From nobody Mon Apr 6 21:59:16 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5A374326923; Tue, 17 Mar 2026 16:23:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773764610; cv=none; b=SRqzufP6Db0amXelKW7kuW0k+X0xKa+yQjh6O+qNJBA+2h/Y54HDxUPALEWbVeu5ShE56P4zfS5nVlbdmbK5z+7LxDiHrf1KHWavT5HSC9aCUgcPxrwR26wnOCQNXdHV7ccB/t65po14G75IlxNU6T8n23SOyb0/+3gprinuXQ4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773764610; c=relaxed/simple; bh=+CVA3mASejU0tJVdTlEmr5PknhwxAcNa6ZiN8WZxJf8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=sH5PHSrG1TgB8H/LGSggw/HLyXdridjcmIEhoM1PxB+ZkmKYUsjxSBkj74BFjYXIxJeTNz6Q+j/PH+0EPzx6EsrqBC7nYZJjUus/0OUxwzTj5dlnDJwKbQy1CYiGLzg6mIYwOM2kJwwvquAetc75s1NxKUfAD4f7k1gQ0av49KE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=QZc1YSFL; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="QZc1YSFL" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B673CC4CEF7; Tue, 17 Mar 2026 16:23:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1773764610; bh=+CVA3mASejU0tJVdTlEmr5PknhwxAcNa6ZiN8WZxJf8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QZc1YSFLF9cqfibLIbuN/CLkc2Kroxgu/+tcpMOqpS8RKJbI1KhwaKln4vpVUNXaN zpCcYCrx8+6hgIXMllLX+WTLVrrwJCuucozYhQsJ1l+Mz6Fq9UVKEUs1W7foktiJNS XodeAM0ykx1Ut/WPE2GYKTn8DYgbxkC/KcJW6khQ9GuJMjrU2sxXp5Be+nk4eZG6NT 22kJ5P15PoXhx6KfmkaJqfJYN2sEk6JZ7K7UVuwV5ATSfq5JfHUzbegVpesjkC5iN4 4IhcW4FXkmWHxYVYtWyfHCLG7SEQJWCCmvvBIxz56dWsvj8mY3NTfl+NugMrhEBMqE jlK3Pg9Ij0+Ag== From: Tycho Andersen To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Ashish Kalra , Tom Lendacky , John Allen , Herbert Xu , "David S. Miller" , Ard Biesheuvel , Neeraj Upadhyay , Kishon Vijay Abraham I , Alexey Kardashevskiy , Nikunj A Dadhania , "Peter Zijlstra (Intel)" , Kim Phillips , Sean Christopherson Cc: linux-kernel@vger.kernel.org, linux-crypto@vger.kernel.org, "Tycho Andersen (AMD)" Subject: [PATCH v3 6/7] crypto: ccp - implement SNP x86 shutdown Date: Tue, 17 Mar 2026 10:21:56 -0600 Message-ID: <20260317162157.150842-7-tycho@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260317162157.150842-1-tycho@kernel.org> References: <20260317162157.150842-1-tycho@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: "Tycho Andersen (AMD)" The SEV firmware has support to disable SNP during an SNP_SHUTDOWN_EX command. Verify that this support is available and set the flag so that SNP is disabled when it is not being used. In cases where SNP is disabled, skip the call to amd_iommu_snp_disable(), as all of the IOMMU pages have already been made shared. Also skip the panic case, since snp_x86_shutdown() does IPIs. Signed-off-by: Tycho Andersen (AMD) Reviewed-by: Tom Lendacky Acked-by: Herbert Xu --- drivers/crypto/ccp/sev-dev.c | 41 +++++++++++++++++++++--------------- include/linux/psp-sev.h | 4 +++- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index b10104f243b9..be6f3720e929 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -2039,6 +2039,8 @@ static int __sev_snp_shutdown_locked(int *error, bool= panic) memset(&data, 0, sizeof(data)); data.len =3D sizeof(data); data.iommu_snp_shutdown =3D 1; + if (sev->snp_feat_info_0.ecx & SNP_X86_SHUTDOWN_SUPPORTED) + data.x86_snp_shutdown =3D 1; =20 /* * If invoked during panic handling, local interrupts are disabled @@ -2072,23 +2074,28 @@ static int __sev_snp_shutdown_locked(int *error, bo= ol panic) return ret; } =20 - /* - * SNP_SHUTDOWN_EX with IOMMU_SNP_SHUTDOWN set to 1 disables SNP - * enforcement by the IOMMU and also transitions all pages - * associated with the IOMMU to the Reclaim state. - * Firmware was transitioning the IOMMU pages to Hypervisor state - * before version 1.53. But, accounting for the number of assigned - * 4kB pages in a 2M page was done incorrectly by not transitioning - * to the Reclaim state. This resulted in RMP #PF when later accessing - * the 2M page containing those pages during kexec boot. Hence, the - * firmware now transitions these pages to Reclaim state and hypervisor - * needs to transition these pages to shared state. SNP Firmware - * version 1.53 and above are needed for kexec boot. - */ - ret =3D amd_iommu_snp_disable(); - if (ret) { - dev_err(sev->dev, "SNP IOMMU shutdown failed\n"); - return ret; + if (data.x86_snp_shutdown) { + if (!panic) + snp_x86_shutdown(); + } else { + /* + * SNP_SHUTDOWN_EX with IOMMU_SNP_SHUTDOWN set to 1 disables SNP + * enforcement by the IOMMU and also transitions all pages + * associated with the IOMMU to the Reclaim state. + * Firmware was transitioning the IOMMU pages to Hypervisor state + * before version 1.53. But, accounting for the number of assigned + * 4kB pages in a 2M page was done incorrectly by not transitioning + * to the Reclaim state. This resulted in RMP #PF when later accessing + * the 2M page containing those pages during kexec boot. Hence, the + * firmware now transitions these pages to Reclaim state and hypervisor + * needs to transition these pages to shared state. SNP Firmware + * version 1.53 and above are needed for kexec boot. + */ + ret =3D amd_iommu_snp_disable(); + if (ret) { + dev_err(sev->dev, "SNP IOMMU shutdown failed\n"); + return ret; + } } =20 snp_leak_hv_fixed_pages(); diff --git a/include/linux/psp-sev.h b/include/linux/psp-sev.h index 69ffa4b4d1fa..2adb990189c1 100644 --- a/include/linux/psp-sev.h +++ b/include/linux/psp-sev.h @@ -834,7 +834,8 @@ struct sev_data_range_list { struct sev_data_snp_shutdown_ex { u32 len; u32 iommu_snp_shutdown:1; - u32 rsvd1:31; + u32 x86_snp_shutdown:1; + u32 rsvd1:30; } __packed; =20 /** @@ -891,6 +892,7 @@ struct snp_feature_info { } __packed; =20 /* Feature bits in ECX */ +#define SNP_X86_SHUTDOWN_SUPPORTED BIT(1) #define SNP_RAPL_DISABLE_SUPPORTED BIT(2) #define SNP_CIPHER_TEXT_HIDING_SUPPORTED BIT(3) #define SNP_AES_256_XTS_POLICY_SUPPORTED BIT(4) --=20 2.53.0 From nobody Mon Apr 6 21:59:16 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A090E30F957; Tue, 17 Mar 2026 16:23:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773764612; cv=none; b=bDTVVeTaT1C4+K57XmxuDSerUuZSpG2isFn5GUXryOEP1Ru+k6VLmIehVt5wbu0SVEPY11FDWMP7lo59DzDBJdjYGoSpcC5AHEFYNbB0l6u4ysvDDrXmYzIcqgRqQULyKPQZ9YT0osxdh6357vYMD9yvZ1psj1l3KYg5ZzYn8V8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773764612; c=relaxed/simple; bh=4qhPs0u0MDTJre/3DNAo+CNY3QBGUHi6CuPNrOVlC7k=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ZP3Ywdhw4zE3+ejWF3cXwMwrvkWAXEu1do/trM+liuBY4DaE78cAmYQVBC2Sjqz1I0C4VhpxoiDJKyG+jhAW2oviSvcYnNMZJRXIffNUtLfdojCCUv2aYsEcWFevLk+QMevTSWEtf68CP3SUi+tpc7SIx7ycCtPf7AWx/Uhglx4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=a1rkcNZI; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="a1rkcNZI" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3EC78C2BC86; Tue, 17 Mar 2026 16:23:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1773764612; bh=4qhPs0u0MDTJre/3DNAo+CNY3QBGUHi6CuPNrOVlC7k=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=a1rkcNZIMYXcSIZhq0m6fmSF2apMPmYwR33DLhOdI8og0z+RTdd3TXZ4jIIatZ1pJ xaQ0DopxjQWvmfXzC6GqzCaeQT9zikgouNNxSiWDyvzIWSPCEPAwJacyLOGJYabQj6 P3fGRqk6xGri3lrOcvpFUqeJQFSBsdZSpFw1esOHJdNeiV9cMPIqmZ7/bFlE+5Z7JO WiPBbgVSN2zkbOzCoOfng8M6IxFuLJYbsTMj0X2P+1I0BXXRuZT3FtIZqyyXYwYNDS WCelvq/pJTZbX6w4fcKf5NcgsZBzBotNxpNu2ixgd2EA9/U3v0SgzOW50t1AhM2L/p DKjBqIwOettIw== From: Tycho Andersen To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Ashish Kalra , Tom Lendacky , John Allen , Herbert Xu , "David S. Miller" , Ard Biesheuvel , Neeraj Upadhyay , Kishon Vijay Abraham I , Alexey Kardashevskiy , Nikunj A Dadhania , "Peter Zijlstra (Intel)" , Kim Phillips , Sean Christopherson Cc: linux-kernel@vger.kernel.org, linux-crypto@vger.kernel.org, "Tycho Andersen (AMD)" Subject: [PATCH v3 7/7] crypto: ccp - Update HV_FIXED page states to allow freeing of memory Date: Tue, 17 Mar 2026 10:21:57 -0600 Message-ID: <20260317162157.150842-8-tycho@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260317162157.150842-1-tycho@kernel.org> References: <20260317162157.150842-1-tycho@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Tom Lendacky After SNP is disabled, any pages allocated as HV_FIXED can now be freed. Update the page state of these pages and the snp_leak_hv_fixed_pages() function to free pages on SNP_SHUTDOWN. Signed-off-by: Tom Lendacky Signed-off-by: Tycho Andersen (AMD) Acked-by: Herbert Xu --- drivers/crypto/ccp/sev-dev.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index be6f3720e929..eac1181c2f6a 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -1219,7 +1219,7 @@ static void snp_add_hv_fixed_pages(struct sev_device = *sev, struct sev_data_range =20 static void snp_leak_hv_fixed_pages(void) { - struct snp_hv_fixed_pages_entry *entry; + struct snp_hv_fixed_pages_entry *entry, *nentry; =20 /* List is protected by sev_cmd_mutex */ lockdep_assert_held(&sev_cmd_mutex); @@ -1227,10 +1227,16 @@ static void snp_leak_hv_fixed_pages(void) if (list_empty(&snp_hv_fixed_pages)) return; =20 - list_for_each_entry(entry, &snp_hv_fixed_pages, list) - if (entry->page_state =3D=3D HV_FIXED) + list_for_each_entry_safe(entry, nentry, &snp_hv_fixed_pages, list) { + if (entry->free && entry->page_state !=3D HV_FIXED) + __free_pages(entry->page, entry->order); + else __snp_leak_pages(page_to_pfn(entry->page), 1 << entry->order, false); + + list_del(&entry->list); + kfree(entry); + } } =20 bool sev_is_snp_ciphertext_hiding_supported(void) @@ -2077,6 +2083,7 @@ static int __sev_snp_shutdown_locked(int *error, bool= panic) if (data.x86_snp_shutdown) { if (!panic) snp_x86_shutdown(); + snp_hv_fixed_pages_state_update(sev, ALLOCATED); } else { /* * SNP_SHUTDOWN_EX with IOMMU_SNP_SHUTDOWN set to 1 disables SNP --=20 2.53.0