Skip SNP initialization if CPUs are offlined
Subsystems (4)
v2 cover Skip SNP initialization if CPUs are offlined
From: "Tycho Andersen (AMD)" <[email protected]>
Here's a v2 of the series. Changes are:
* propagate the error from snp_prepare() through __sev_snp_init_locked()
* use cpus_present_mask instead of cpus_possible_mask to reason about the set of available CPUs
* print the CPU masks for easier debugging
It turns out that CONFIG_INIT_ALL_POSSIBLE is not user-settable, and only parisc selects it, so it shouldn't be a problem here.
v1 is here: https://lore.kernel.org/all/[email protected]/
Tycho Andersen (AMD) (2):
x86/sev: Do not initialize SNP if missing CPUs
crypto/ccp: Skip SNP_INIT if preparation fails
arch/x86/include/asm/sev.h | 4 ++-- arch/x86/virt/svm/sev.c | 15 +++++++++++++-- drivers/crypto/ccp/sev-dev.c | 4 +++- 3 files changed, 18 insertions(+), 5 deletions(-)
base-commit: 6c927e5ca9d238f8ae40b453a8382eb9cf4ee855
Signature · 1 line
2.53.0
PATCH v2 1/2 x86/sev: Do not initialize SNP if missing CPUs
From: "Tycho Andersen (AMD)" <[email protected]>
The SEV firmware checks that the SNP enable bit is set on each CPU during SNP initialization, and will fail if it is not. If there are some CPUs offline, they will not run the setup functions, so SNP initialization will always fail.
Skip the IPIs in this case and return an error so that the CCP driver can skip the SNP_INIT that will fail. Also print the CPU masks as a breadcrumb so people can figure out what happened.
--- arch/x86/include/asm/sev.h | 4 ++-- arch/x86/virt/svm/sev.c | 15 +++++++++++++-- 2 files changed, 15 insertions(+), 4 deletions(-)
arch/x86/include/asm/sev.h +2 -2
@@ -661,7 +661,7 @@ static inline void snp_leak_pages(u64 pfn, unsigned int pages) {__snp_leak_pages (pfn, pages, true); } -void snp_prepare (void ); +int snp_prepare (void );void snp_shutdown (void );#else static inline bool snp_probe_rmptable_info (void ) {return false; }
@@ -679,7 +679,7 @@ static inline void __snp_leak_pages(u64 pfn, unsigned int 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 (void ) {} +static inline int snp_prepare (void ) {return -ENODEV ; }static inline void snp_shutdown (void ) {}#endif
arch/x86/virt/svm/sev.c +13 -2
@@ -511,8 +511,9 @@ static void clear_hsave_pa(void *arg)wrmsrq (MSR_VM_HSAVE_PA ,0 ); } -void snp_prepare (void ) +int snp_prepare (void ) { +int ret = -EOPNOTSUPP ;u64 val;/*
@@ -521,12 +522,19 @@ void snp_prepare(void)*/ rdmsrq (MSR_AMD64_SYSCFG , val);if (val &MSR_AMD64_SYSCFG_SNP_EN ) -return ; +return 0 ;clear_rmp ();cpus_read_lock (); +if (!cpumask_equal (cpu_online_mask, cpu_present_mask)) { +pr_warn ("Skipping SNP initialization. CPUs online %*pbl, present %*pbl\n" , +cpumask_pr_args (cpu_online_mask), +cpumask_pr_args (cpu_present_mask)); + goto unlock; + } +/* * MtrrFixDramModEn is not shared between threads on a core, * therefore it must be set on all CPUs prior to enabling SNP.
@@ -537,7 +545,10 @@ void snp_prepare(void)/* SNP_INIT requires MSR_VM_HSAVE_PA to be cleared on all CPUs. */ on_each_cpu (clear_hsave_pa,NULL ,1 ); + ret =0 ; +unlock:cpus_read_unlock (); +return ret; }EXPORT_SYMBOL_FOR_MODULES (snp_prepare,"ccp" );
Signature · 1 line
2.53.0
PATCH v2 2/2 crypto/ccp: Skip SNP_INIT if preparation fails
In reply to Tycho AndersenFrom: "Tycho Andersen (AMD)" <[email protected]>
During SNP_INIT, the firmware checks to see that the SNP enable bit is set on all CPUs. If snp_prepare() failed because not all CPUs were online, SNP_INIT will fail, so skip it.
--- drivers/crypto/ccp/sev-dev.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/crypto/ccp/sev-dev.c +3 -1
@@ -1374,7 +1374,9 @@ static int __sev_snp_init_locked(int *error, unsigned int max_snp_asid)return -EOPNOTSUPP ; } -snp_prepare (); + rc =snp_prepare (); +if (rc <0 ) +return rc;/* * Starting in SNP firmware v1.52, the SNP_INIT_EX command takes a list
Signature · 1 line
2.53.0
Re: [PATCH v2 1/2] x86/sev: Do not initialize SNP if missing CPUs
In reply to Tycho AndersenQuoted 7 lines · Tycho Andersen
From: "Tycho Andersen (AMD)" <[email protected]>
The SEV firmware checks that the SNP enable bit is set on each CPU during SNP initialization, and will fail if it is not. If there are some CPUs offline, they will not run the setup functions, so SNP initialization will always fail.
Quoted 6 lines
Skip the IPIs in this case and return an error so that the CCP driver can skip the
SNP_INITthat will fail. Also print the CPU masks as a breadcrumb so people can figure out what happened.Suggested-by Borislav Petkov (AMD) <[email protected]>Signed-off-by Tycho Andersen (AMD) <[email protected]>
LGTM
Quoted 74 lines
--- arch/x86/include/asm/sev.h | 4 ++-- arch/x86/virt/svm/sev.c | 15 +++++++++++++-- 2 files changed, 15 insertions(+), 4 deletions(-)arch/x86/include/asm/sev.h +2
-2@@ -661,7 +661,7 @@ static inline void snp_leak_pages(u64 pfn, unsigned int pages) {__snp_leak_pages (pfn, pages, true); } -void snp_prepare (void ); +int snp_prepare (void );void snp_shutdown (void );#else static inline bool snp_probe_rmptable_info (void ) {return false; }@@ -679,7 +679,7 @@ static inline void __snp_leak_pages(u64 pfn, unsigned int 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 (void ) {} +static inline int snp_prepare (void ) {return -ENODEV ; }static inline void snp_shutdown (void ) {}#endif arch/x86/virt/svm/sev.c +13
-2@@ -511,8 +511,9 @@ static void clear_hsave_pa(void *arg)wrmsrq (MSR_VM_HSAVE_PA ,0 ); } -void snp_prepare (void ) +int snp_prepare (void ) { +int ret = -EOPNOTSUPP ;u64 val;/* @@ -521,12 +522,19 @@ void snp_prepare(void)*/ rdmsrq (MSR_AMD64_SYSCFG , val);if (val &MSR_AMD64_SYSCFG_SNP_EN ) -return ; +return 0 ;clear_rmp ();cpus_read_lock (); +if (!cpumask_equal (cpu_online_mask, cpu_present_mask)) { +pr_warn ("Skipping SNP initialization. CPUs online %*pbl, present %*pbl\n" , +cpumask_pr_args (cpu_online_mask), +cpumask_pr_args (cpu_present_mask)); + goto unlock; + } +/* * MtrrFixDramModEn is not shared between threads on a core, * therefore it must be set on all CPUs prior to enabling SNP. @@ -537,7 +545,10 @@ void snp_prepare(void)/* SNP_INIT requires MSR_VM_HSAVE_PA to be cleared on all CPUs. */ on_each_cpu (clear_hsave_pa,NULL ,1 ); + ret =0 ; +unlock:cpus_read_unlock (); +return ret; }EXPORT_SYMBOL_FOR_MODULES (snp_prepare,"ccp" );
Re: [PATCH v2 2/2] crypto/ccp: Skip SNP_INIT if preparation fails
In reply to Tycho AndersenQuoted 7 lines · Tycho Andersen
From: "Tycho Andersen (AMD)" <[email protected]>
During
SNP_INIT, the firmware checks to see that the SNP enable bit is set on all CPUs. Ifsnp_prepare()failed because not all CPUs were online,SNP_INITwill fail, so skip it.Signed-off-by Tycho Andersen (AMD) <[email protected]>
Quoted 19 lines
--- drivers/crypto/ccp/sev-dev.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)drivers/crypto/ccp/sev-dev.c +3
-1@@ -1374,7 +1374,9 @@ static int __sev_snp_init_locked(int *error, unsigned int max_snp_asid)return -EOPNOTSUPP ; } -snp_prepare (); + rc =snp_prepare (); +if (rc <0 ) +return rc;/* * Starting in SNP firmware v1.52, the SNP_INIT_EX command takes a list
Re: [PATCH v2 1/2] x86/sev: Do not initialize SNP if missing CPUs
In reply to Tycho AndersenQuoted 45 lines · Tycho Andersen
From: "Tycho Andersen (AMD)" <[email protected]>
The SEV firmware checks that the SNP enable bit is set on each CPU during SNP initialization, and will fail if it is not. If there are some CPUs offline, they will not run the setup functions, so SNP initialization will always fail.
Skip the IPIs in this case and return an error so that the CCP driver can skip the
SNP_INITthat will fail. Also print the CPU masks as a breadcrumb so people can figure out what happened.Suggested-by Borislav Petkov (AMD) <[email protected]>Signed-off-by Tycho Andersen (AMD) <[email protected]>--- arch/x86/include/asm/sev.h | 4 ++-- arch/x86/virt/svm/sev.c | 15 +++++++++++++-- 2 files changed, 15 insertions(+), 4 deletions(-)arch/x86/include/asm/sev.h +2
-2@@ -661,7 +661,7 @@ static inline void snp_leak_pages(u64 pfn, unsigned int pages) {__snp_leak_pages (pfn, pages, true); } -void snp_prepare (void ); +int snp_prepare (void );void snp_shutdown (void );#else static inline bool snp_probe_rmptable_info (void ) {return false; }@@ -679,7 +679,7 @@ static inline void __snp_leak_pages(u64 pfn, unsigned int 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 (void ) {} +static inline int snp_prepare (void ) {return -ENODEV ; }static inline void snp_shutdown (void ) {}#endif arch/x86/virt/svm/sev.c +0
-0
arch/x86/virt/svm/sev.c static void clear_hsave_pa(void *arg) · line 516
Quoted 7 lines
arch/x86/virt/svm/sev.c +2
-1@@ -511,8 +511,9 @@ static void clear_hsave_pa(void *arg)wrmsrq (MSR_VM_HSAVE_PA ,0 ); } -void snp_prepare (void ) +int snp_prepare (void ) { +int ret = -EOPNOTSUPP ;
I prefer seeing the return value set in the error condition, but that's just my preference.
Quoted 25 lines
arch/x86/virt/svm/sev.c +8
-1u64 val;/* @@ -521,12 +522,19 @@ void snp_prepare(void)*/ rdmsrq (MSR_AMD64_SYSCFG , val);if (val &MSR_AMD64_SYSCFG_SNP_EN ) -return ; +return 0 ;clear_rmp ();cpus_read_lock (); +if (!cpumask_equal (cpu_online_mask, cpu_present_mask)) { +pr_warn ("Skipping SNP initialization. CPUs online %*pbl, present %*pbl\n" , +cpumask_pr_args (cpu_online_mask), +cpumask_pr_args (cpu_present_mask)); + goto unlock; + } +/* * MtrrFixDramModEn is not shared between threads on a core, * therefore it must be set on all CPUs prior to enabling SNP.
arch/x86/virt/svm/sev.c void snp_prepare(void) · line 548
Quoted 4 lines
arch/x86/virt/svm/sev.c +1
-0@@ -537,7 +545,10 @@ void snp_prepare(void)/* SNP_INIT requires MSR_VM_HSAVE_PA to be cleared on all CPUs. */ on_each_cpu (clear_hsave_pa,NULL ,1 ); + ret =0 ;
I would also prefer a blank line here...
arch/x86/virt/svm/sev.c void snp_prepare(void) · line 550
Quoted 2 lines
arch/x86/virt/svm/sev.c +1
-0+unlock:cpus_read_unlock ();
and here. But, again, that's just me.
If you don't need another version, that's not enough to redo it.
Quoted 4 lines
arch/x86/virt/svm/sev.c +1
-0+return ret; }EXPORT_SYMBOL_FOR_MODULES (snp_prepare,"ccp" );
Tom Lendacky
maintainerRe: [PATCH v2 2/2] crypto/ccp: Skip SNP_INIT if preparation fails
In reply to Tycho AndersenQuoted 5 lines · Tycho Andersen
From: "Tycho Andersen (AMD)" <[email protected]>
During
SNP_INIT, the firmware checks to see that the SNP enable bit is set on all CPUs. Ifsnp_prepare()failed because not all CPUs were online,SNP_INITwill fail, so skip it.
This should probably be more generic and state that if snp_prepare() fails for any reason then SNP_INIT will fail, so skip it.
Quoted 11 lines
Signed-off-by Tycho Andersen (AMD) <[email protected]>--- drivers/crypto/ccp/sev-dev.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)drivers/crypto/ccp/sev-dev.c +0
-0
drivers/crypto/ccp/sev-dev.c static int __sev_snp_init_locked(int *error, unsigned int max_snp_asid) · line 1378
Quoted 6 lines
drivers/crypto/ccp/sev-dev.c +2
-1@@ -1374,7 +1374,9 @@ static int __sev_snp_init_locked(int *error, unsigned int max_snp_asid)return -EOPNOTSUPP ; } -snp_prepare (); + rc =snp_prepare (); +if (rc <0 )
If we are expecting that success only ever returns 0, I would say this should just be "if (rc)".
Thanks,
Tom
Quoted 4 lines
drivers/crypto/ccp/sev-dev.c +1
-0+return rc;/* * Starting in SNP firmware v1.52, the SNP_INIT_EX command takes a list
Re: [PATCH v2 2/2] crypto/ccp: Skip SNP_INIT if preparation fails
Quoted 9 lines · Tom Lendacky
Quoted 5 lines · Tycho Andersen
From: "Tycho Andersen (AMD)" <[email protected]>
During
SNP_INIT, the firmware checks to see that the SNP enable bit is set on all CPUs. Ifsnp_prepare()failed because not all CPUs were online,SNP_INITwill fail, so skip it.This should probably be more generic and state that if
snp_prepare()fails for any reason thenSNP_INITwill fail, so skip it.
Yep, thanks. I can send a v3 with all of these fixes.
And to preempt questions about sashiko:
https://sashiko.dev/#/patchset/20260407174713.439474-1-tycho%40kernel.org
The first one is bogus, but I also got it from AI when reviewing this
series: the previous code returned the error from the firmware as well and killed initialization completely.
The other two are ones that were previously reported, I have fixes for them but have not yet posted...
Tycho
Re: [PATCH v2 1/2] x86/sev: Do not initialize SNP if missing CPUs
In reply to Tom LendackyQuoted 12 lines · Tom Lendacky
Quoted 11 lines · Tycho Andersen
From: "Tycho Andersen (AMD)" <[email protected]>
The SEV firmware checks that the SNP enable bit is set on each CPU during SNP initialization, and will fail if it is not. If there are some CPUs offline, they will not run the setup functions, so SNP initialization will always fail.
Skip the IPIs in this case and return an error so that the CCP driver can skip the
SNP_INITthat will fail. Also print the CPU masks as a breadcrumb so people can figure out what happened.
Quoted 6 lines
Quoted 3 lines
Suggested-by Borislav Petkov (AMD) <[email protected]>Signed-off-by Tycho Andersen (AMD) <[email protected]>---
Reviewed-by Tom Lendacky <[email protected]>
Ugh, that's not the right email address:
At least I spelled my name right...
Quoted 6 lines
Quoted 5 lines
arch/x86/virt/svm/sev.c +1
-0+return ret; }EXPORT_SYMBOL_FOR_MODULES (snp_prepare,"ccp" );