Skip to contentSkip to conversation

x86/sev: Do not initialize SNP if missing CPUs

PATCH Superseded Tycho Andersen 3 participants 9 messages Apr 1 – Apr 3

Subsystems (4)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 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.

---
 arch/x86/include/asm/sev.h |  4 ++--
 arch/x86/virt/svm/sev.c    | 11 +++++++++--
 2 files changed, 11 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 +9 -2
index 41f76f15caa1..e9ded15dbe60 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 = -EOPNOTSUPP;
 	u64 val;
 
 	/*
@@ -521,12 +522,15 @@ 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_possible_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 +541,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");
 

base-commit: cf112712c193e837225d740ec3e139774f2496f2

Signature · 1 line
2.53.0
#1 lore raw

PATCH 2/2 crypto/ccp: skip SNP_INIT if preparation fails

From: "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 | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/crypto/ccp/sev-dev.c +4 -1
index 939fa8aa155c..1fc7ee432e28 100644
@@ -1374,7 +1374,10 @@ static int __sev_snp_init_locked(int *error, unsigned int max_snp_asid)
 		return -EOPNOTSUPP;
 	}
 
-	snp_prepare();
+	if (snp_prepare() < 0) {
+		dev_dbg(sev->dev, "SNP preparation failed, are all CPUs online?\n");
+		return -EOPNOTSUPP;
+	}
 
 	/*
 	 * Starting in SNP firmware v1.52, the SNP_INIT_EX command takes a list
Signature · 1 line
2.53.0
#2 lore raw

Re: [PATCH v1 2/2] crypto/ccp: skip SNP_INIT if preparation fails

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. If snp_prepare() failed because not all CPUs were online, SNP_INIT will fail, so skip it.

Heh, you can ignore this one. Somehow it got sent out when I fixed the capitalization in the commit message.

Tycho

maintainer
#3 lore raw

Re: [PATCH v1 2/2] crypto/ccp: skip SNP_INIT if preparation fails

In reply to Tycho Andersen
Quoted 16 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. If snp_prepare() failed because not all CPUs were online, SNP_INIT will fail, so skip it.

---
 drivers/crypto/ccp/sev-dev.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/crypto/ccp/sev-dev.c +0 -0
index 939fa8aa155c..1fc7ee432e28 100644

drivers/crypto/ccp/sev-dev.c static int __sev_snp_init_locked(int *error, unsigned int max_snp_asid) · line 1380

Quoted 8 lines
drivers/crypto/ccp/sev-dev.c +4 -1
@@ -1374,7 +1374,10 @@ static int __sev_snp_init_locked(int *error, unsigned int max_snp_asid)
 		return -EOPNOTSUPP;
 	}
 
-	snp_prepare();
+	if (snp_prepare() < 0) {
+		dev_dbg(sev->dev, "SNP preparation failed, are all CPUs online?\n");
+		return -EOPNOTSUPP;
+	}

Shouldn't this propagate the snp_prepare() return code:

	rc = snp_prepare();
	if (rc) {
		dev_dbg(...)
		return ret;
	}

Thanks,
Tom

Quoted 3 lines
drivers/crypto/ccp/sev-dev.c +0 -0
 	/*
 	 * Starting in SNP firmware v1.52, the SNP_INIT_EX command takes a list
#4 lore raw

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

In reply to 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. If snp_prepare() failed because not all CPUs were online, SNP_INIT will fail, so skip it.

---
 drivers/crypto/ccp/sev-dev.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/crypto/ccp/sev-dev.c +4 -1
index 939fa8aa155c..1fc7ee432e28 100644
@@ -1374,7 +1374,10 @@ static int __sev_snp_init_locked(int *error, unsigned int max_snp_asid)
 		return -EOPNOTSUPP;
 	}
 
-	snp_prepare();
+	if (snp_prepare() < 0) {
+		dev_dbg(sev->dev, "SNP preparation failed, are all CPUs online?\n");
+		return -EOPNOTSUPP;
+	}
 
 	/*
 	 * Starting in SNP firmware v1.52, the SNP_INIT_EX command takes a list
Signature · 1 line
2.53.0
maintainer
#5 lore raw
In reply to Tycho Andersen
Quoted 56 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_INIT that will fail.

---
 arch/x86/include/asm/sev.h |  4 ++--
 arch/x86/virt/svm/sev.c    | 11 +++++++++--
 2 files changed, 11 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 +2 -1
index 41f76f15caa1..e9ded15dbe60 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 = -EOPNOTSUPP;
 	u64 val;
 
 	/*
@@ -521,12 +522,15 @@ void snp_prepare(void)
 	 */

arch/x86/virt/svm/sev.c void snp_prepare(void) · line 531

Quoted 10 lines
arch/x86/virt/svm/sev.c +2 -1
 	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_possible_mask))

If CONFIG_INIT_ALL_POSSIBLE is set, won't that set cpu_possible_mask to include all CPUs up to NR_CPUS? That would result in this always failing.

Not sure if this change is worth it.

Thanks,
Tom

Quoted 18 lines
arch/x86/virt/svm/sev.c +5 -0
+		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 +541,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");
 

base-commit: cf112712c193e837225d740ec3e139774f2496f2

maintainer
#6 lore raw
Quoted 4 lines · Tom Lendacky

arch/x86/virt/svm/sev.c void snp_prepare(void) · line 531

Quoted 1 line
arch/x86/virt/svm/sev.c +1 -0
+	if (!cpumask_equal(cpu_online_mask, cpu_possible_mask))

If CONFIG_INIT_ALL_POSSIBLE is set, won't that set cpu_possible_mask to include all CPUs up to NR_CPUS? That would result in this always failing.

Yah,

sashiko gave another possible situation where this can fail:

https://sashiko.dev/#/patchset/20260401143552.3038979-1-tycho%40kernel.org

Quoted 1 line

Not sure if this change is worth it.

Well, it would save us a lot of effort if we can check at module load time whether some CPUs are offlined. A very simple use case:

echo 0 > /sys/devices/system/cpu.../online
modprobe ccp

<boom>.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#7 lore raw
Quoted 5 lines · Borislav Petkov
Quoted 4 lines · Tom Lendacky

arch/x86/virt/svm/sev.c void snp_prepare(void) · line 531

Quoted 1 line
arch/x86/virt/svm/sev.c +1 -0
+	if (!cpumask_equal(cpu_online_mask, cpu_possible_mask))

If CONFIG_INIT_ALL_POSSIBLE is set, won't that set cpu_possible_mask to include all CPUs up to NR_CPUS? That would result in this always failing.

Thanks, I didn't know about this config.

Quoted 7 lines

Yah,

sashiko gave another possible situation where this can fail:

https://sashiko.dev/#/patchset/20260401143552.3038979-1-tycho%40kernel.org

disabled ACPI MADT entries for hotplug

I suppose depends on how the firmware reasons about such things too. But it seems like the suggestion of cpus_present_mask is right.

Quoted 7 lines
Quoted 1 line

Not sure if this change is worth it.

Well, it would save us a lot of effort if we can check at module load time whether some CPUs are offlined. A very simple use case:

echo 0 > /sys/devices/system/cpu.../online
modprobe ccp

This is how I was testing this, but I'll see about testing the ACPI thing too.

Tycho

maintainer
#8 lore raw
Quoted 2 lines · Tycho Andersen

I suppose depends on how the firmware reasons about such things too. But it seems like the suggestion of cpus_present_mask is right.

Yes, I think that should work.

And in that check you could dump both masks if they're unequal - hint *%pb or %pbl as printk format strings:

Documentation/core-api/printk-formats.rst

so that we can get more debug info on what the mismatch actually is.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette