[tip: x86/sev] x86/sev: Do not initialize SNP if missing CPUs

From: tip-bot2 for Tycho Andersen (AMD)

Date: Fri May 08 2026 - 14:42:44 EST


The following commit has been merged into the x86/sev branch of tip:

Commit-ID: 39f1de2fffb3dc1751153e4c3d9138ccd958e8b1
Gitweb: https://git.kernel.org/tip/39f1de2fffb3dc1751153e4c3d9138ccd958e8b1
Author: Tycho Andersen (AMD) <tycho@xxxxxxxxxx>
AuthorDate: Wed, 29 Apr 2026 09:56:35 -06:00
Committer: Borislav Petkov (AMD) <bp@xxxxxxxxx>
CommitterDate: Fri, 08 May 2026 20:28:49 +02:00

x86/sev: Do not initialize SNP if missing CPUs

The SEV firmware checks that the SNP enable bit is set on each CPU during SNP
initialization, and will fail if 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 in order to leave
breadcrumbs so people can figure out what happened.

[ bp: Massage commit message. ]

Suggested-by: Borislav Petkov (AMD) <bp@xxxxxxxxx>
Signed-off-by: Tycho Andersen (AMD) <tycho@xxxxxxxxxx>
Signed-off-by: Borislav Petkov (AMD) <bp@xxxxxxxxx>
Reviewed-by: Nikunj A Dadhania <nikunj@xxxxxxx>
Reviewed-by: Tom Lendacky <thomas.lendacky@xxxxxxx>
Link: https://20260429155636.540040-1-tycho@xxxxxxxxxx
---
arch/x86/include/asm/sev.h | 4 ++--
arch/x86/virt/svm/sev.c | 18 ++++++++++++++++--
2 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h
index 09e605c..594cfa1 100644
--- a/arch/x86/include/asm/sev.h
+++ b/arch/x86/include/asm/sev.h
@@ -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

diff --git a/arch/x86/virt/svm/sev.c b/arch/x86/virt/svm/sev.c
index 41f76f1..8bcdce9 100644
--- a/arch/x86/virt/svm/sev.c
+++ b/arch/x86/virt/svm/sev.c
@@ -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;
u64 val;

/*
@@ -521,12 +522,20 @@ 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)) {
+ ret = -EOPNOTSUPP;
+ pr_warn("SNP init failed: not all CPUs online. (%*pbl online <-> %*pbl present masks).\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 +546,12 @@ 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");