Skip to contentSkip to conversation

Skip SNP inititalization if CPUs are offlined

PATCH New Tycho Andersen 1 participant 3 messages Apr 29

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v4 cover Skip SNP inititalization if CPUs are offlined

From: "Tycho Andersen (AMD)" <[email protected]>

The sole change from v3 is to fix the error message.

v3 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      | 18 ++++++++++++++++--
 drivers/crypto/ccp/sev-dev.c |  4 +++-
 3 files changed, 21 insertions(+), 5 deletions(-)

base-commit: 8f1aacb683ef4a49b83dcc40bfce022aaa4aa597

Signature · 1 line
2.54.0
#1 lore raw

PATCH v4 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    | 18 ++++++++++++++++--
 2 files changed, 18 insertions(+), 4 deletions(-)
arch/x86/include/asm/sev.h +2 -2
index 09e605c85de4..594cfa19cbd4 100644
@@ -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 +16 -2
index 41f76f15caa1..8bcdce98f6dc 100644
@@ -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");
 
Signature · 1 line
2.54.0
#2 lore raw

PATCH v4 2/2 crypto/ccp: Skip SNP_INIT if preparation fails

In reply to Tycho Andersen

From: "Tycho Andersen (AMD)" <[email protected]>

If snp_prepare() failed SNP_INIT will fail, so skip it and return early. Note that this is not a change in initialization behavior: if SNP_INIT failed before this patch, it will still return an error __sev_snp_init_locked() and fail initialization of other SEV modes.

---
 drivers/crypto/ccp/sev-dev.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/crypto/ccp/sev-dev.c +3 -1
index d1e9e0ac63b6..78f98aee7a66 100644
@@ -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)
+		return rc;
 
 	/*
 	 * Starting in SNP firmware v1.52, the SNP_INIT_EX command takes a list
Signature · 1 line
2.54.0