Skip to contentSkip to conversation

Move SNP initialization to the CCP driver

PATCH Superseded Tycho Andersen 3 participants 16 messages Mar 17 – Mar 23

Subsystems (4)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover Move SNP initialization to the CCP driver

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

Here's v3 of the series. Changes are:
* rebase on tip/master and drop the three patches that are already there
* prep -> preparation in one more place that I missed
* snp_clear_rmp() -> clear_rmp()
* move the create snp_x86_shutdown() patch earlier, so the order is now: x86 only stuff, x86 + crypto stuff, crypto only stuff. Hopefully that makes it easier to apply in the event of patch tetris.

v2 is here: https://lore.kernel.org/all/[email protected]/

Thanks,

Tycho

Tom Lendacky (2):
x86/snp: Create a function to clear/zero the RMP
crypto: ccp - Update HV_FIXED page states to allow freeing of memory

Tycho Andersen (AMD) (5):
x86/snp: create snp_prepare_for_snp_init()
x86/snp: create snp_x86_shutdown()
x86/snp, crypto: move SNP init to ccp driver
x86/snp, crypto: move HSAVE_PA setup to arch/
crypto: ccp - implement SNP x86 shutdown

 arch/x86/include/asm/sev.h   |   4 ++
 arch/x86/virt/svm/sev.c      | 112 ++++++++++++++++++++++++-----------
 drivers/crypto/ccp/sev-dev.c |  62 ++++++++++---------
 include/linux/psp-sev.h      |   4 +-
 4 files changed, 120 insertions(+), 62 deletions(-)

base-commit: 270b06a2452b0a20b149591ee90e22e3d8d55358

Signature · 1 line
2.53.0
#1 lore raw

PATCH v3 1/7 x86/snp: Create a function to clear/zero the RMP

From: Tom Lendacky <[email protected]>

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.

---
 arch/x86/virt/svm/sev.c | 41 +++++++++++++++++++++++++++--------------
 1 file changed, 27 insertions(+), 14 deletions(-)
arch/x86/virt/svm/sev.c +27 -14
index e35fac0a8a3d..025606969823 100644
@@ -242,6 +242,32 @@ void __init snp_fixup_e820_tables(void)
 	}
 }
 
+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 = 0; i < rst_max_index; i++) {
+		struct rmp_segment_desc *desc;
+
+		desc = 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;
 
 	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;
 
-	/* Zero out the RMP bookkeeping area */
-	memset(rmp_bookkeeping, 0, RMPTABLE_CPU_BOOKKEEPING_SZ);
-
-	/* Zero out the RMP entries */
-	for (i = 0; i < rst_max_index; i++) {
-		struct rmp_segment_desc *desc;
-
-		desc = rmp_segment_table[i];
-		if (!desc)
-			continue;
-
-		memset(desc->rmp_entry, 0, desc->size);
-	}
+	clear_rmp();
 
 	/* MtrrFixDramModEn must be enabled on all the CPUs prior to enabling SNP. */
 	on_each_cpu(mfd_enable, NULL, 1);
Signature · 1 line
2.53.0
#2 lore raw

PATCH v3 2/7 x86/snp: create snp_prepare_for_snp_init()

In reply to Tycho Andersen

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

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.

---
 arch/x86/include/asm/sev.h |  2 ++
 arch/x86/virt/svm/sev.c    | 46 ++++++++++++++++++++++----------------
 2 files changed, 29 insertions(+), 19 deletions(-)
arch/x86/include/asm/sev.h +2 -0
index 0e6c0940100f..0bcd89d4fe90 100644
@@ -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 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_for_snp_init(void) {}
 #endif
 
 #endif
arch/x86/virt/svm/sev.c +27 -19

77 lines not on this page · raw message

Signature · 1 line
2.53.0
maintainer
#3 lore raw

Re: [PATCH v3 2/7] x86/snp: create snp_prepare_for_snp_init()

Quoted 1 line · Tycho Andersen

+void snp_prepare_for_snp_init(void)

What's wrong with "snp_prepare" simply?

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#4 lore raw

PATCH v3 3/7 x86/snp: create snp_x86_shutdown()

In reply to Tycho Andersen

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

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.

---
 arch/x86/include/asm/sev.h |  2 ++
 arch/x86/virt/svm/sev.c    | 23 ++++++++++++++++++++---
 2 files changed, 22 insertions(+), 3 deletions(-)
arch/x86/include/asm/sev.h +2 -0
index 0bcd89d4fe90..36d2b1ea19c0 100644
@@ -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
 
 #endif
arch/x86/virt/svm/sev.c +20 -3
index 88cb4a548701..85091d663f18 100644
@@ -132,12 +132,15 @@ static unsigned long snp_nr_leaked_pages;
 #undef pr_fmt
 #define pr_fmt(fmt)	"SEV-SNP: " fmt
 
-static void mfd_enable(void *arg)
+static void mfd_reconfigure(void *arg)
 {
 	if (!cc_platform_has(CC_ATTR_HOST_SEV_SNP))
 		return;
 
-	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);
 }
 
 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);
 
 	on_each_cpu(snp_enable, NULL, 1);
 }
 EXPORT_SYMBOL_FOR_MODULES(snp_prepare_for_snp_init, "ccp");
 
+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
Signature · 1 line
2.53.0
maintainer
#5 lore raw

Re: [PATCH v3 3/7] x86/snp: create snp_x86_shutdown()

Quoted 1 line · Tycho Andersen

Subject: Re: [PATCH v3 3/7] x86/snp: create snp_x86_shutdown()

"x86/sev: ..."

The tip tree preferred format for patch subject prefixes is 'subsys/component:', e.g. 'x86/apic:', 'x86/mm/fault:', 'sched/fair:', 'genirq/core:'. Please do not use file names or complete file paths as prefix. 'git log path/to/file' should give you a reasonable hint in most cases.

The condensed patch description in the subject line should start with a uppercase letter and should be written in imperative tone.

Check your whole set pls.

Quoted 3 lines

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

After SNP_SHUTDOWN, two architecture-level things should be done:

"architecture-level things"?

Quoted 17 lines

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.

---
 arch/x86/include/asm/sev.h |  2 ++
 arch/x86/virt/svm/sev.c    | 23 ++++++++++++++++++++---
 2 files changed, 22 insertions(+), 3 deletions(-)

...

arch/x86/virt/svm/sev.c void snp_prepare_for_snp_init(void) · line 527

Quoted 6 lines
arch/x86/virt/svm/sev.c +1 -1
@@ -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);
				     ^^^^^^^^^

ew.

arch/x86/virt/svm/sev.c void snp_prepare_for_snp_init(void) · line 533

Quoted 5 lines
arch/x86/virt/svm/sev.c +1 -0
 	on_each_cpu(snp_enable, NULL, 1);
 }
 EXPORT_SYMBOL_FOR_MODULES(snp_prepare_for_snp_init, "ccp");
 
+void snp_x86_shutdown(void)

"snp" and "x86" prefixes?

arch/x86/virt/svm/sev.c void snp_prepare_for_snp_init(void) · line 538

Quoted 5 lines
arch/x86/virt/svm/sev.c +5 -0
+{
+	u64 syscfg;
+
+	rdmsrq(MSR_AMD64_SYSCFG, syscfg);
+

^ Superfluous newline.

arch/x86/virt/svm/sev.c void snp_prepare_for_snp_init(void) · line 543

Quoted 5 lines
arch/x86/virt/svm/sev.c +5 -0
+	if (syscfg & MSR_AMD64_SYSCFG_SNP_EN)
+		return;
+
+	clear_rmp();
+	on_each_cpu(mfd_reconfigure, 0, 1);

s/0/NULL/

Quoted 9 lines
arch/x86/virt/svm/sev.c +3 -0
+}
+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
Signature · 1 line
2.53.0
Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#6 lore raw

Re: [PATCH v3 3/7] x86/snp: create snp_x86_shutdown()

Hi Boris,

Quoted 11 lines · Borislav Petkov
Quoted 1 line · Tycho Andersen

Subject: Re: [PATCH v3 3/7] x86/snp: create snp_x86_shutdown()

"x86/sev: ..."

The tip tree preferred format for patch subject prefixes is 'subsys/component:', e.g. 'x86/apic:', 'x86/mm/fault:', 'sched/fair:', 'genirq/core:'. Please do not use file names or complete file paths as prefix. 'git log path/to/file' should give you a reasonable hint in most cases.

Quoted 4 lines

The condensed patch description in the subject line should start with a uppercase letter and should be written in imperative tone.

Check your whole set pls.

Will do.

Quoted 5 lines
Quoted 3 lines

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

After SNP_SHUTDOWN, two architecture-level things should be done:

"architecture-level things"?

I'll just drop this entirely, i.e. "two things should be done:"

Quoted 21 lines
Quoted 18 lines

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.

---
 arch/x86/include/asm/sev.h |  2 ++
 arch/x86/virt/svm/sev.c    | 23 ++++++++++++++++++++---
 2 files changed, 22 insertions(+), 3 deletions(-)

...

Quoted 8 lines

arch/x86/virt/svm/sev.c void snp_prepare_for_snp_init(void) · line 527

Quoted 6 lines
arch/x86/virt/svm/sev.c +1 -1
@@ -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);
				     ^^^^^^^^^

ew.

:) I can add a macro for this. Let me know if you want a full args struct instead.

Quoted 7 lines

arch/x86/virt/svm/sev.c void snp_prepare_for_snp_init(void) · line 533

Quoted 5 lines
arch/x86/virt/svm/sev.c +1 -0
 	on_each_cpu(snp_enable, NULL, 1);
 }
 EXPORT_SYMBOL_FOR_MODULES(snp_prepare_for_snp_init, "ccp");
 
+void snp_x86_shutdown(void)

"snp" and "x86" prefixes?

My intent here was to indicate that it's doing the x86 (i.e. non-firmware) parts of the SNP shutdown process. I will change it to just snp_shutdown(), but that will still have a prefix. Just shutdown() seemed to generic...

Will fix the rest.

Thanks,

Tycho

maintainer
#7 lore raw

Re: [PATCH v3 3/7] x86/snp: create snp_x86_shutdown()

Quoted 2 lines · Tycho Andersen

:) I can add a macro for this. Let me know if you want a full args struct instead.

"ew" meant: this is not nice but I don't have a better idea...

A full args struct doesn't really change anything AFAICT. Maybe play with it a bit and see what the resulting asm is but I'm sceptical it would be any different...

Quoted 4 lines

My intent here was to indicate that it's doing the x86 (i.e. non-firmware) parts of the SNP shutdown process. I will change it to just snp_shutdown(), but that will still have a prefix. Just shutdown() seemed to generic...

Yeah, snp_shutdown() - just one prefix :) - is enough.

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#8 lore raw

PATCH v3 4/7 x86/snp, crypto: move SNP init to ccp driver

In reply to Tycho Andersen

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

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

---
 arch/x86/virt/svm/sev.c      | 2 --
 drivers/crypto/ccp/sev-dev.c | 2 ++
 2 files changed, 2 insertions(+), 2 deletions(-)
arch/x86/virt/svm/sev.c +0 -2
index 85091d663f18..b73ea987c69c 100644
@@ -560,8 +560,6 @@ int __init snp_rmptable_init(void)
 	if (!setup_rmptable())
 		return -ENOSYS;
 
-	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.
drivers/crypto/ccp/sev-dev.c +2 -0
index 8b2dfc11289b..07c4736a1f0a 100644
@@ -1373,6 +1373,8 @@ static int __sev_snp_init_locked(int *error, unsigned int max_snp_asid)
 		return -EOPNOTSUPP;
 	}
 
+	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);
 
Signature · 1 line
2.53.0
#9 lore raw

PATCH v3 5/7 x86/snp, crypto: move HSAVE_PA setup to arch/

In reply to Tycho Andersen

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

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.

---
 arch/x86/virt/svm/sev.c      | 8 ++++++++
 drivers/crypto/ccp/sev-dev.c | 8 --------
 2 files changed, 8 insertions(+), 8 deletions(-)
arch/x86/virt/svm/sev.c +8 -0
index b73ea987c69c..e856c13cfdaa 100644
@@ -506,6 +506,11 @@ static bool __init setup_rmptable(void)
 	return true;
 }
 
+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);
 
 	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");
 
drivers/crypto/ccp/sev-dev.c +0 -8
index 07c4736a1f0a..b10104f243b9 100644
@@ -1076,11 +1076,6 @@ static inline int __sev_do_init_locked(int *psp_ret)
 		return __sev_init_locked(psp_ret);
 }
 
-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)
 
 	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);
-
 	/*
 	 * 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
Signature · 1 line
2.53.0
maintainer
#10 lore raw

Re: [PATCH v3 5/7] x86/snp, crypto: move HSAVE_PA setup to arch/

Quoted 1 line · Tycho Andersen

Subject: Re: [PATCH v3 5/7] x86/snp, crypto: move HSAVE_PA setup to arch/

		... arch/x86/

There are other arches, you know. :)

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#11 lore raw

PATCH v3 6/7 crypto: ccp - implement SNP x86 shutdown

In reply to Tycho Andersen

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

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.

---
 drivers/crypto/ccp/sev-dev.c | 41 +++++++++++++++++++++---------------
 include/linux/psp-sev.h      |  4 +++-
 2 files changed, 27 insertions(+), 18 deletions(-)
drivers/crypto/ccp/sev-dev.c +24 -17
index b10104f243b9..be6f3720e929 100644
@@ -2039,6 +2039,8 @@ static int __sev_snp_shutdown_locked(int *error, bool panic)
 	memset(&data, 0, sizeof(data));
 	data.len = sizeof(data);
 	data.iommu_snp_shutdown = 1;
+	if (sev->snp_feat_info_0.ecx & SNP_X86_SHUTDOWN_SUPPORTED)
+		data.x86_snp_shutdown = 1;
 
 	/*
 	 * If invoked during panic handling, local interrupts are disabled
@@ -2072,23 +2074,28 @@ static int __sev_snp_shutdown_locked(int *error, bool panic)
 		return ret;
 	}
 
-	/*
-	 * 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 = 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 = amd_iommu_snp_disable();
+		if (ret) {
+			dev_err(sev->dev, "SNP IOMMU shutdown failed\n");
+			return ret;
+		}
 	}
 
 	snp_leak_hv_fixed_pages();
include/linux/psp-sev.h +3 -1
index 69ffa4b4d1fa..2adb990189c1 100644
@@ -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;
 
 /**
@@ -891,6 +892,7 @@ struct snp_feature_info {
 } __packed;
 
 /* 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)
Signature · 1 line
2.53.0
maintainer Acked-by on v3 6/7
#12 lore raw

Re: [PATCH v3 6/7] crypto: ccp - implement SNP x86 shutdown

Quoted 9 lines · Tycho Andersen

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

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.

Quoted 6 lines
---
 drivers/crypto/ccp/sev-dev.c | 41 +++++++++++++++++++++---------------
 include/linux/psp-sev.h      |  4 +++-
 2 files changed, 27 insertions(+), 18 deletions(-)

Thanks,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#13 lore raw

PATCH v3 7/7 crypto: ccp - Update HV_FIXED page states to allow freeing of memory

In reply to Tycho Andersen

From: Tom Lendacky <[email protected]>

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.

---
 drivers/crypto/ccp/sev-dev.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
drivers/crypto/ccp/sev-dev.c +10 -3
index be6f3720e929..eac1181c2f6a 100644
@@ -1219,7 +1219,7 @@ static void snp_add_hv_fixed_pages(struct sev_device *sev, struct sev_data_range
 
 static void snp_leak_hv_fixed_pages(void)
 {
-	struct snp_hv_fixed_pages_entry *entry;
+	struct snp_hv_fixed_pages_entry *entry, *nentry;
 
 	/* 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;
 
-	list_for_each_entry(entry, &snp_hv_fixed_pages, list)
-		if (entry->page_state == HV_FIXED)
+	list_for_each_entry_safe(entry, nentry, &snp_hv_fixed_pages, list) {
+		if (entry->free && entry->page_state != 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);
+	}
 }
 
 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
Signature · 1 line
2.53.0
maintainer Acked-by on v3 7/7
#14 lore raw

Re: [PATCH v3 7/7] crypto: ccp - Update HV_FIXED page states to allow freeing of memory

Quoted 11 lines · Tycho Andersen

From: Tom Lendacky <[email protected]>

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.

---
 drivers/crypto/ccp/sev-dev.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

Thanks,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
maintainer
#15 lore raw
In reply to Tycho Andersen
Quoted 11 lines · Tycho Andersen

Tom Lendacky (2):
x86/snp: Create a function to clear/zero the RMP
crypto: ccp - Update HV_FIXED page states to allow freeing of memory

Tycho Andersen (AMD) (5):
x86/snp: create snp_prepare_for_snp_init()
x86/snp: create snp_x86_shutdown()
x86/snp, crypto: move SNP init to ccp driver
x86/snp, crypto: move HSAVE_PA setup to arch/
crypto: ccp - implement SNP x86 shutdown

Quoted 5 lines
 arch/x86/include/asm/sev.h   |   4 ++
 arch/x86/virt/svm/sev.c      | 112 ++++++++++++++++++++++++-----------
 drivers/crypto/ccp/sev-dev.c |  62 ++++++++++---------
 include/linux/psp-sev.h      |   4 +-
 4 files changed, 120 insertions(+), 62 deletions(-)

Ok, I think the next version of this will be ready, pls incorporate all review feedback and I can pick it up.

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

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