Move SEV-SNP initialization to ccp driver
Subsystems (4)
cover Move SEV-SNP initialization to ccp driver
From: "Tycho Andersen (AMD)" <[email protected]>
The SEV firmware has support for disabling SNP when doing a SNP_SHUTDOWN_EX, which will turn off the SNPEn bit. If setting SNPEn is also delayed until module load, this means that we can control the lifecycle of SNP enablement with module load and unload. This way, the SNP checks are only done while the module is actually loaded.
Tom Lendacky (3):
x86/snp: Keep the RMP table bookkeeping area mapped 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) (8):
x86/snp: drop support for SNP hotplug
x86/snp: drop WBINVD before setting SNPEn
x86/snp: create snp_prepare_for_snp_init()
x86/snp, crypto: move SNP init to ccp driver
x86/snp, crypto: move HSAVE_PA setup to arch/
x86/snp: allow disabling MFDM
x86/snp: create snp_x86_shutdown()
crypto: ccp - implement SNP x86 shutdown
arch/x86/include/asm/sev.h | 4 + arch/x86/virt/svm/sev.c | 148 +++++++++++++++++++---------------- drivers/crypto/ccp/sev-dev.c | 65 ++++++++------- include/linux/psp-sev.h | 4 +- 4 files changed, 125 insertions(+), 96 deletions(-)
base-commit: 11439c4635edd669ae435eec308f4ab8a0804808
Signature · 1 line
2.53.0
PATCH 1/11 x86/snp: drop support for SNP hotplug
From: "Tycho Andersen (AMD)" <[email protected]>
During an SNP_INIT(_EX), the SEV firmware checks that all CPUs have SNPEn set, and fails if they do not. As such, it does not make sense to have offline CPUs: the firmware will fail initialization because of the offlined ones that the kernel did not initialize.
Futher, there is a bug: SNP_INIT(_EX) require MFDM to be set in addition to SNPEn which the previous hotplug code did not do. Since k8_check_syscfg_dram_mod_en() enforces this be cleared, hotplug wouldn't work.
Drop the hotplug code. Collapse the __{mfd,snp}__enable() wrappers into their non-__ versions, since the cpu number argument is no longer needed.
--- arch/x86/virt/svm/sev.c | 24 ++++-------------------- 1 file changed, 4 insertions(+), 20 deletions(-)
arch/x86/virt/svm/sev.c +4 -20
@@ -130,33 +130,26 @@ static unsigned long snp_nr_leaked_pages;#undef pr_fmt#define pr_fmt (fmt) "SEV-SNP: " fmt -static int __mfd_enable (unsigned int cpu) +static __init void mfd_enable (void *arg) {u64 val;if (!cc_platform_has (CC_ATTR_HOST_SEV_SNP )) -return 0 ; +return ;rdmsrq (MSR_AMD64_SYSCFG , val); val |=MSR_AMD64_SYSCFG_MFDM ;wrmsrq (MSR_AMD64_SYSCFG , val); - -return 0 ; } -static __init void mfd_enable (void *arg) -{ -__mfd_enable (smp_processor_id ()); -} - -static int __snp_enable (unsigned int cpu) +static __init void snp_enable (void *arg) {u64 val;if (!cc_platform_has (CC_ATTR_HOST_SEV_SNP )) -return 0 ; +return ;rdmsrq (MSR_AMD64_SYSCFG , val);
@@ -164,13 +157,6 @@ static int __snp_enable(unsigned int cpu) val |=MSR_AMD64_SYSCFG_SNP_VMPL_EN ;wrmsrq (MSR_AMD64_SYSCFG , val); - -return 0 ; -} - -static __init void snp_enable (void *arg) -{ -__snp_enable (smp_processor_id ()); }static void __init __snp_fixup_e820_tables (u64 pa)
@@ -553,8 +539,6 @@ int __init snp_rmptable_init(void)on_each_cpu (snp_enable,NULL ,1 ); skip_enable: -cpuhp_setup_state (CPUHP_AP_ONLINE_DYN ,"x86/rmptable_init:online" , __snp_enable,NULL ); -/* * Setting crash_kexec_post_notifiers to 'true' to ensure that SNP panic * notifier is invoked to do SNP IOMMU shutdown before kdump.
Signature · 1 line
2.53.0
PATCH 2/11 x86/snp: Keep the RMP table bookkeeping area mapped
In reply to Tycho AndersenFrom: Tom Lendacky <[email protected]>
In prep for delayed SNP initialization and disablement on shutdown, the RMP will need to be cleared each time SNP is disabled. Maintain the the mapping to the RMP bookkeeping area to avoid mapping and unmapping it each time and any possible errors that may arise from that.
--- arch/x86/virt/svm/sev.c | 40 +++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 23 deletions(-)
arch/x86/virt/svm/sev.c +17 -23
@@ -117,6 +117,8 @@ static u64 rmp_segment_mask;static u64 rmp_cfg; +static void *rmp_bookkeeping__ro_after_init ; +/* Mask to apply to a PFN to get the first PFN of a 2MB page */ #define PFN_PMD_MASK GENMASK_ULL(63, PMD_SHIFT - PAGE_SHIFT)
@@ -246,23 +248,6 @@ void __init snp_fixup_e820_tables(void) } } -static bool __init clear_rmptable_bookkeeping (void ) -{ -void *bk; - - bk =memremap (probed_rmp_base,RMPTABLE_CPU_BOOKKEEPING_SZ ,MEMREMAP_WB ); -if (!bk) { -pr_err ("Failed to map RMP bookkeeping area\n" ); -return false; - } - -memset (bk,0 ,RMPTABLE_CPU_BOOKKEEPING_SZ ); - -memunmap (bk); - -return true; -} -static bool __init alloc_rmp_segment_desc (u64 segment_pa,u64 segment_size,u64 pa) {u64 rst_index, rmp_segment_size_max;
@@ -480,10 +465,22 @@ static bool __init setup_segmented_rmptable(void)static bool __init setup_rmptable (void ) {if (rmp_cfg &MSR_AMD64_SEG_RMP_ENABLED ) { -return setup_segmented_rmptable (); +if (!setup_segmented_rmptable ()) +return false; }else { -return setup_contiguous_rmptable (); +if (!setup_contiguous_rmptable ()) +return false; } + + rmp_bookkeeping =memremap (probed_rmp_base,RMPTABLE_CPU_BOOKKEEPING_SZ ,MEMREMAP_WB ); +if (!rmp_bookkeeping) { +pr_err ("Failed to map RMP bookkeeping area\n" ); +free_rmp_segment_table (); + +return false; + } + +return true; }/*
@@ -514,10 +511,7 @@ int __init snp_rmptable_init(void) goto skip_enable;/* Zero out the RMP bookkeeping area */ -if (!clear_rmptable_bookkeeping ()) { -free_rmp_segment_table (); -return -ENOSYS ; - } +memset (rmp_bookkeeping,0 ,RMPTABLE_CPU_BOOKKEEPING_SZ );/* Zero out the RMP entries */ for (i =0 ; i < rst_max_index; i++) {
Signature · 1 line
2.53.0
PATCH 3/11 x86/snp: Create a function to clear/zero the RMP
In reply to Tycho AndersenFrom: Tom Lendacky <[email protected]>
In prep for delayed SNP initialization and disablement on shutdown, create a function, snp_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
@@ -248,6 +248,32 @@ void __init snp_fixup_e820_tables(void) } } +static void snp_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;
@@ -490,7 +516,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 )))
@@ -510,19 +535,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); - } +snp_clear_rmp ();/* Flush the caches to ensure that data is written before SNP is enabled. */ wbinvd_on_all_cpus ();
Signature · 1 line
2.53.0
PATCH 4/11 x86/snp: drop WBINVD before setting SNPEn
In reply to Tycho AndersenFrom: "Tycho Andersen (AMD)" <[email protected]>
WBINVD is required before SNP_INIT(_EX), but not before setting SNPEn, since the ccp driver already does its own WBINVD before SNP_INIT (and this one would be too early for that anyway...).
--- arch/x86/virt/svm/sev.c | 3 --- 1 file changed, 3 deletions(-)
arch/x86/virt/svm/sev.c +0 -3
@@ -537,9 +537,6 @@ int __init snp_rmptable_init(void)snp_clear_rmp (); -/* Flush the caches to ensure that data is written before SNP is enabled. */ -wbinvd_on_all_cpus (); -/* 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
PATCH 5/11 x86/snp: create snp_prepare_for_snp_init()
In reply to Tycho AndersenFrom: "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
@@ -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
PATCH 6/11 x86/snp, crypto: move SNP init to ccp driver
In reply to Tycho AndersenFrom: "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
@@ -549,8 +549,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
@@ -1375,6 +1375,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
PATCH 7/11 x86/snp, crypto: move HSAVE_PA setup to arch/
In reply to Tycho AndersenFrom: "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
@@ -509,6 +509,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;
@@ -530,6 +535,9 @@ void snp_prepare_for_snp_init(void)on_each_cpu (mfd_enable,NULL ,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
@@ -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)
@@ -1377,9 +1372,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
PATCH 8/11 x86/snp: allow disabling MFDM
In reply to Tycho AndersenFrom: "Tycho Andersen (AMD)" <[email protected]>
The SNP x86 shutdown path needs to disable MFDM, since as the comment for k8_check_syscfg_dram_mod_en(), the "BIOS" is supposed clear it, or the kernel in the case of module unload and shutdown followed by kexec.
Change this helper to allow for disabling it.
--- arch/x86/virt/svm/sev.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
arch/x86/virt/svm/sev.c +6 -3
@@ -132,7 +132,7 @@ 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) {u64 val;
@@ -141,7 +141,10 @@ static void mfd_enable(void *arg)rdmsrq (MSR_AMD64_SYSCFG , val); - val |=MSR_AMD64_SYSCFG_MFDM ; +if (arg) + val |=MSR_AMD64_SYSCFG_MFDM ; +else + val &= ~MSR_AMD64_SYSCFG_MFDM ;wrmsrq (MSR_AMD64_SYSCFG , val); }
@@ -532,7 +535,7 @@ 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 );
Signature · 1 line
2.53.0
PATCH 9/11 x86/snp: create snp_x86_shutdown()
In reply to Tycho AndersenFrom: "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.
--- arch/x86/include/asm/sev.h | 2 ++ arch/x86/virt/svm/sev.c | 7 +++++++ 2 files changed, 9 insertions(+)
arch/x86/include/asm/sev.h +2 -0
@@ -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 +7 -0
@@ -544,6 +544,13 @@ void snp_prepare_for_snp_init(void) }EXPORT_SYMBOL_FOR_MODULES (snp_prepare_for_snp_init,"ccp" ); +void snp_x86_shutdown (void ) +{ +snp_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
PATCH 10/11 crypto: ccp - implement SNP x86 shutdown
In reply to Tycho AndersenFrom: "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.
--- drivers/crypto/ccp/sev-dev.c | 44 ++++++++++++++++++++++-------------- include/linux/psp-sev.h | 4 +++- 2 files changed, 30 insertions(+), 18 deletions(-)
drivers/crypto/ccp/sev-dev.c +27 -17
@@ -2028,6 +2028,7 @@ static int __sev_snp_shutdown_locked(int *error, bool panic)struct psp_device *psp = psp_master;struct sev_device *sev;struct sev_data_snp_shutdown_ex data; +u64 syscfg;int ret;if (!psp || !psp->sev_data)
@@ -2041,6 +2042,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
@@ -2074,23 +2077,30 @@ 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; +rdmsrq (MSR_AMD64_SYSCFG , syscfg); +if (data.x86_snp_shutdown && + !WARN_ON_ONCE (syscfg &MSR_AMD64_SYSCFG_SNP_EN )) { +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
@@ -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
PATCH 11/11 crypto: ccp - Update HV_FIXED page states to allow freeing of memory
In reply to Tycho AndersenFrom: 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
@@ -1221,7 +1221,7 @@ static void snp_add_hv_fixed_pages(struct sev_device *sev, struct sev_data_rangestatic 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);
@@ -1229,10 +1229,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 )
@@ -2082,6 +2088,7 @@ static int __sev_snp_shutdown_locked(int *error, bool panic) !WARN_ON_ONCE(syscfg & MSR_AMD64_SYSCFG_SNP_EN)) { 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
Re: [PATCH 01/11] x86/snp: drop support for SNP hotplug
In reply to Tycho AndersenQuoted 14 lines · Tycho Andersen
[Some people who received this message don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]
From: "Tycho Andersen (AMD)" <[email protected]>
During an
SNP_INIT(_EX), the SEV firmware checks that all CPUs have SNPEn set, and fails if they do not. As such, it does not make sense to have offline CPUs: the firmware will fail initialization because of the offlined ones that the kernel did not initialize.Futher, there is a bug:
SNP_INIT(_EX)require MFDM to be set in addition to SNPEn which the previous hotplug code did not do. Sincek8_check_syscfg_dram_mod_en()enforces this be cleared, hotplug wouldn't work.
Quoted 4 lines
Drop the hotplug code. Collapse the __{mfd,snp}__enable() wrappers into their non-__ versions, since the cpu number argument is no longer needed.
Signed-off-by Tycho Andersen (AMD) <[email protected]>
Quoted 72 lines
--- arch/x86/virt/svm/sev.c | 24 ++++-------------------- 1 file changed, 4 insertions(+), 20 deletions(-)arch/x86/virt/svm/sev.c +4
-21@@ -130,33 +130,26 @@ static unsigned long snp_nr_leaked_pages;#undef pr_fmt#define pr_fmt (fmt) "SEV-SNP: " fmt -static int __mfd_enable (unsigned int cpu) +static __init void mfd_enable (void *arg) {u64 val;if (!cc_platform_has (CC_ATTR_HOST_SEV_SNP )) -return 0 ; +return ;rdmsrq (MSR_AMD64_SYSCFG , val); val |=MSR_AMD64_SYSCFG_MFDM ;wrmsrq (MSR_AMD64_SYSCFG , val); - -return 0 ; } -static __init void mfd_enable (void *arg) -{ -__mfd_enable (smp_processor_id ()); -} - -static int __snp_enable (unsigned int cpu) +static __init void snp_enable (void *arg) {u64 val;if (!cc_platform_has (CC_ATTR_HOST_SEV_SNP )) -return 0 ; +return ;rdmsrq (MSR_AMD64_SYSCFG , val);@@ -164,13 +157,6 @@ static int __snp_enable(unsigned int cpu) val |=MSR_AMD64_SYSCFG_SNP_VMPL_EN ;wrmsrq (MSR_AMD64_SYSCFG , val); - -return 0 ; -} - -static __init void snp_enable (void *arg) -{ -__snp_enable (smp_processor_id ()); }static void __init __snp_fixup_e820_tables (u64 pa)@@ -553,8 +539,6 @@ int __init snp_rmptable_init(void)on_each_cpu (snp_enable,NULL ,1 ); skip_enable: -cpuhp_setup_state (CPUHP_AP_ONLINE_DYN ,"x86/rmptable_init:online" , __snp_enable,NULL ); -/* * Setting crash_kexec_post_notifiers to 'true' to ensure that SNP panic * notifier is invoked to do SNP IOMMU shutdown before kdump. -- 2.53.0
Re: [PATCH 04/11] x86/snp: drop WBINVD before setting SNPEn
In reply to Tycho AndersenQuoted 7 lines · Tycho Andersen
From: "Tycho Andersen (AMD)" <[email protected]>
WBINVD is required before
SNP_INIT(_EX), but not before setting SNPEn, since the ccp driver already does its own WBINVD beforeSNP_INIT(and this one would be too early for that anyway...).Signed-off-by Tycho Andersen (AMD) <[email protected]>
Quoted 18 lines
--- arch/x86/virt/svm/sev.c | 3 --- 1 file changed, 3 deletions(-)arch/x86/virt/svm/sev.c +0
-3@@ -537,9 +537,6 @@ int __init snp_rmptable_init(void)snp_clear_rmp (); -/* Flush the caches to ensure that data is written before SNP is enabled. */ -wbinvd_on_all_cpus (); -/* MtrrFixDramModEn must be enabled on all the CPUs prior to enabling SNP. */ on_each_cpu (mfd_enable,NULL ,1 );
Re: [PATCH 05/11] x86/snp: create snp_prepare_for_snp_init()
In reply to Tycho AndersenQuoted 11 lines · 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
__initsection, since these will be called later.Signed-off-by Tycho Andersen (AMD) <[email protected]>
Quoted 111 lines
--- 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@@ -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@@ -132,7 +132,7 @@ static unsigned long snp_nr_leaked_pages;#undef pr_fmt#define pr_fmt (fmt) "SEV-SNP: " fmt -static __init void mfd_enable (void *arg) +static void mfd_enable (void *arg) {u64 val;@@ -146,7 +146,7 @@ static __init void mfd_enable(void *arg)wrmsrq (MSR_AMD64_SYSCFG , val); } -static __init void snp_enable (void *arg) +static void snp_enable (void *arg) {u64 val;@@ -509,6 +509,30 @@ static bool __init setup_rmptable(void)return true; } +void snp_prepare_for_snp_init (void ) +{ +u64 val; + +/* +* Check if SEV-SNP is already enabled, this can happen in case of +* kexec boot. +*/ +rdmsrq (MSR_AMD64_SYSCFG , val); +if (val &MSR_AMD64_SYSCFG_SNP_EN ) +return ; + +snp_clear_rmp (); + +/* +* 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 (snp_enable,NULL ,1 ); +} +EXPORT_SYMBOL_FOR_MODULES (snp_prepare_for_snp_init,"ccp" ); +/* * Do the necessary preparations which are verified by the firmware as * described in the SNP_INIT_EX firmware command description in the SNP @@ -516,8 +540,6 @@ static bool __init setup_rmptable(void)*/ int __init snp_rmptable_init (void ) { -u64 val; -if (WARN_ON_ONCE (!cc_platform_has (CC_ATTR_HOST_SEV_SNP )))return -ENOSYS ;@@ -527,22 +549,8 @@ int __init snp_rmptable_init(void)if (!setup_rmptable ())return -ENOSYS ; -/* -* Check if SEV-SNP is already enabled, this can happen in case of -* kexec boot. -*/ -rdmsrq (MSR_AMD64_SYSCFG , val); -if (val &MSR_AMD64_SYSCFG_SNP_EN ) - goto skip_enable; - -snp_clear_rmp (); - -/* MtrrFixDramModEn must be enabled on all the CPUs prior to enabling SNP. */ -on_each_cpu (mfd_enable,NULL ,1 ); - -on_each_cpu (snp_enable,NULL ,1 ); +snp_prepare_for_snp_init (); -skip_enable:/* * Setting crash_kexec_post_notifiers to 'true' to ensure that SNP panic * notifier is invoked to do SNP IOMMU shutdown before kdump.
Re: [PATCH 06/11] x86/snp, crypto: move SNP init to ccp driver
In reply to Tycho AndersenQuoted 8 lines · 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_amdloads the ccp driver automatically).Signed-off-by Tycho Andersen (AMD) <[email protected]>
Quoted 31 lines
--- 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@@ -549,8 +549,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@@ -1375,6 +1375,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 );
Re: [PATCH 07/11] x86/snp, crypto: move HSAVE_PA setup to arch/
In reply to Tycho AndersenQuoted 7 lines · 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 forSNP_INIT(_EX), move this architecture-specific bit of code to a more sensible place.Signed-off-by Tycho Andersen (AMD) <[email protected]>
Quoted 57 lines
--- 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@@ -509,6 +509,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;@@ -530,6 +535,9 @@ void snp_prepare_for_snp_init(void)on_each_cpu (mfd_enable,NULL ,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@@ -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)@@ -1377,9 +1372,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
Re: [PATCH 08/11] x86/snp: allow disabling MFDM
In reply to Tycho AndersenQuoted 9 lines · Tycho Andersen
From: "Tycho Andersen (AMD)" <[email protected]>
The SNP x86 shutdown path needs to disable MFDM, since as the comment for
k8_check_syscfg_dram_mod_en(), the "BIOS" is supposed clear it, or the kernel in the case of module unload and shutdown followed by kexec.Change this helper to allow for disabling it.
Signed-off-by Tycho Andersen (AMD) <[email protected]>
This could be squashed with the next patch, but either way:
Quoted 38 lines
--- arch/x86/virt/svm/sev.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)arch/x86/virt/svm/sev.c +6
-3@@ -132,7 +132,7 @@ 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) {u64 val;@@ -141,7 +141,10 @@ static void mfd_enable(void *arg)rdmsrq (MSR_AMD64_SYSCFG , val); - val |=MSR_AMD64_SYSCFG_MFDM ; +if (arg) + val |=MSR_AMD64_SYSCFG_MFDM ; +else + val &= ~MSR_AMD64_SYSCFG_MFDM ;wrmsrq (MSR_AMD64_SYSCFG , val); }@@ -532,7 +535,7 @@ 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 );
Re: [PATCH 09/11] x86/snp: create snp_x86_shutdown()
In reply to Tycho AndersenQuoted 41 lines · 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 theFW_WARNink8_check_syscfg_dram_mod_en()in the event of a kexecCreate and export to the CCP driver a function that does them.
Signed-off-by Tycho Andersen (AMD) <[email protected]>--- arch/x86/include/asm/sev.h | 2 ++ arch/x86/virt/svm/sev.c | 7 +++++++ 2 files changed, 9 insertions(+)arch/x86/include/asm/sev.h +2
-0@@ -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 +0
-0
arch/x86/virt/svm/sev.c void snp_prepare_for_snp_init(void) · line 548
Quoted 5 lines
arch/x86/virt/svm/sev.c +2
-0@@ -544,6 +544,13 @@ void snp_prepare_for_snp_init(void) }EXPORT_SYMBOL_FOR_MODULES (snp_prepare_for_snp_init,"ccp" ); +void snp_x86_shutdown (void ) +{
Would it make sense to check for SNP being enabled before calling the functions below? I realize each of the functions in question will do that, but it could save a bunch of IPI's with the on_each_cpu() if SNP is still enabled. Not a big deal either way, so:
Thanks,
Tom
Quoted 8 lines
arch/x86/virt/svm/sev.c +5
-0+snp_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
Re: [PATCH 10/11] crypto: ccp - implement SNP x86 shutdown
In reply to Tycho AndersenQuoted 9 lines · Tycho Andersen
From: "Tycho Andersen (AMD)" <[email protected]>
The SEV firmware has support to disable SNP during an
SNP_SHUTDOWN_EXcommand. 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 toamd_iommu_snp_disable(), as all of the IOMMU pages have already been made shared.Signed-off-by Tycho Andersen (AMD) <[email protected]>
Quoted 96 lines
--- drivers/crypto/ccp/sev-dev.c | 44 ++++++++++++++++++++++-------------- include/linux/psp-sev.h | 4 +++- 2 files changed, 30 insertions(+), 18 deletions(-)drivers/crypto/ccp/sev-dev.c +27
-17@@ -2028,6 +2028,7 @@ static int __sev_snp_shutdown_locked(int *error, bool panic)struct psp_device *psp = psp_master;struct sev_device *sev;struct sev_data_snp_shutdown_ex data; +u64 syscfg;int ret;if (!psp || !psp->sev_data)@@ -2041,6 +2042,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 @@ -2074,23 +2077,30 @@ 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; +rdmsrq (MSR_AMD64_SYSCFG , syscfg); +if (data.x86_snp_shutdown && + !WARN_ON_ONCE (syscfg &MSR_AMD64_SYSCFG_SNP_EN )) { +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@@ -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)
Re: [PATCH 09/11] x86/snp: create snp_x86_shutdown()
In reply to Tom LendackyHi Tom,
Quoted 7 lines · Tom Lendacky
arch/x86/virt/svm/sev.c void snp_prepare_for_snp_init(void) · line 548
Quoted 2 lines
arch/x86/virt/svm/sev.c +2
-0+void snp_x86_shutdown (void ) +{Would it make sense to check for SNP being enabled before calling the functions below? I realize each of the functions in question will do that, but it could save a bunch of IPI's with the
on_each_cpu()if SNP is still enabled. Not a big deal either way, so:
It is guarded at the call site by:
if (data.x86_snp_shutdown &&
!WARN_ON_ONCE(syscfg & MSR_AMD64_SYSCFG_SNP_EN)) {
if (!panic)
snp_x86_shutdown();but we could push that into here to protect any future callers.
If we require a v2 I will make the fix.
Quoted 1 line
Reviewed-by Tom Lendacky <[email protected]>
Thanks for this and the others!
Tycho
Borislav Petkov
maintainerRe: [PATCH 01/11] x86/snp: drop support for SNP hotplug
In reply to Tycho AndersenQuoted 3 lines · Tycho Andersen
From: "Tycho Andersen (AMD)" <[email protected]>
During an
SNP_INIT(_EX), the SEV firmware checks that all CPUs have SNPEn
Please write those in a human-readable form - not as code in text. Commit messages are still predominantly read by humans.
:)
Quoted 11 lines
set, and fails if they do not. As such, it does not make sense to have offline CPUs: the firmware will fail initialization because of the offlined ones that the kernel did not initialize.
Futher, there is a bug:
SNP_INIT(_EX)require MFDM to be set in addition to SNPEn which the previous hotplug code did not do. Sincek8_check_syscfg_dram_mod_en()enforces this be cleared, hotplug wouldn't work.Drop the hotplug code. Collapse the __{mfd,snp}__enable() wrappers into their non-__ versions, since the cpu number argument is no longer needed.
Please, do not talk about *what* the patch is doing in the commit message
- that should be obvious from the diff itself. Rather, concentrate on the *why* it needs to be done and why your patch exists.
Quoted 20 lines
Signed-off-by Tycho Andersen (AMD) <[email protected]>--- arch/x86/virt/svm/sev.c | 24 ++++-------------------- 1 file changed, 4 insertions(+), 20 deletions(-)arch/x86/virt/svm/sev.c +1
-2@@ -130,33 +130,26 @@ static unsigned long snp_nr_leaked_pages;#undef pr_fmt#define pr_fmt (fmt) "SEV-SNP: " fmt -static int __mfd_enable (unsigned int cpu) +static __init void mfd_enable (void *arg) {u64 val;if (!cc_platform_has (CC_ATTR_HOST_SEV_SNP )) -return 0 ;
arch/x86/virt/svm/sev.c static unsigned long snp_nr_leaked_pages; · line 145
Quoted 10 lines
arch/x86/virt/svm/sev.c +1
-2+return ;rdmsrq (MSR_AMD64_SYSCFG , val); val |=MSR_AMD64_SYSCFG_MFDM ;wrmsrq (MSR_AMD64_SYSCFG , val); - -return 0 ; }
While at it:
arch/x86/virt/svm/sev.c +1 -7
@@ -132,16 +132,10 @@ static unsigned long snp_nr_leaked_pages;static __init void mfd_enable (void *arg) { -u64 val; -if (!cc_platform_has (CC_ATTR_HOST_SEV_SNP ))return ; -rdmsrq (MSR_AMD64_SYSCFG , val); - - val |=MSR_AMD64_SYSCFG_MFDM ; - -wrmsrq (MSR_AMD64_SYSCFG , val); +msr_set_bit (MSR_AMD64_SYSCFG ,MSR_AMD64_SYSCFG_MFDM_BIT ); }static __init void snp_enable (void *arg)
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteBorislav Petkov
maintainerRe: [PATCH 01/11] x86/snp: drop support for SNP hotplug
In reply to Tycho AndersenQuoted 12 lines · Tycho Andersen
From: "Tycho Andersen (AMD)" <[email protected]>
During an
SNP_INIT(_EX), the SEV firmware checks that all CPUs have SNPEn set, and fails if they do not. As such, it does not make sense to have offline CPUs: the firmware will fail initialization because of the offlined ones that the kernel did not initialize.Futher, there is a bug:
SNP_INIT(_EX)require MFDM to be set in addition to SNPEn which the previous hotplug code did not do. Sincek8_check_syscfg_dram_mod_en()enforces this be cleared, hotplug wouldn't work.
Quoted 7 lines
Drop the hotplug code. Collapse the __{mfd,snp}__enable() wrappers into their non-__ versions, since the cpu number argument is no longer needed.
Signed-off-by Tycho Andersen (AMD) <[email protected]>--- arch/x86/virt/svm/sev.c | 24 ++++-------------------- 1 file changed, 4 insertions(+), 20 deletions(-)
Btw, this one conflicts with Ashish's
Considering how yours is removing code and is almost ready, I'd suggest you send a new version of it now-ish, as a reply to this thread and after incorporating all feedback, so that I can apply it first and then Ashish can base his stuff ontop.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteBorislav Petkov
maintainerRe: [PATCH 02/11] x86/snp: Keep the RMP table bookkeeping area mapped
In reply to Tycho AndersenQuoted 3 lines · Tycho Andersen
From: Tom Lendacky <[email protected]>
In prep for delayed SNP initialization and disablement on shutdown, the
^^^^
Yeah, write it out, you can do it... :-P
And yes, this sounds like Tom to me. :-P
Quoted 1 line
RMP will need to be cleared each time SNP is disabled. Maintain the
s/the //
Quoted 8 lines
the mapping to the RMP bookkeeping area to avoid mapping and unmapping it each time and any possible errors that may arise from that.
Signed-off-by Tom Lendacky <[email protected]>Signed-off-by Tycho Andersen (AMD) <[email protected]>--- arch/x86/virt/svm/sev.c | 40 +++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 23 deletions(-)
Otherwise LGTM.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquettev2 cover Move SNP initialization to the CCP driver
In reply to Borislav PetkovFrom: "Tycho Andersen (AMD)" <[email protected]>
Here's v2 of the series. Changes are:
* squash MFDM helper into snp_x86_shutdown()
* move MFDM helper to msr_set/clear_bit()
* move SNP bit test during shutdown from ccp into snp_x86_shutdown()
* reorder so that the two code deletion patches come first
* commit message rewrites, carry Tom's reivews as appropriate
Tom Lendacky (3):
x86/snp: Keep the RMP table bookkeeping area mapped 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) (7):
x86/snp: drop support for SNP hotplug
x86/snp: drop WBINVD before setting SNPEn
x86/snp: create snp_prepare_for_snp_init()
x86/snp, crypto: move SNP init to ccp driver
x86/snp, crypto: move HSAVE_PA setup to arch/
x86/snp: create snp_x86_shutdown()
crypto: ccp - implement SNP x86 shutdown
arch/x86/include/asm/sev.h | 4 + arch/x86/virt/svm/sev.c | 161 +++++++++++++++++++---------------- drivers/crypto/ccp/sev-dev.c | 62 ++++++++------ include/linux/psp-sev.h | 4 +- 4 files changed, 129 insertions(+), 102 deletions(-)
base-commit: 59f9bfe4641c408c08824a9b52e9f7839bde57d8
Signature · 1 line
2.53.0
PATCH v2 1/10 x86/snp: drop support for SNP hotplug
From: "Tycho Andersen (AMD)" <[email protected]>
During an SNP_INIT(_EX), the SEV firmware checks that all CPUs have the SNP syscfg bit set, and fails if they do not. As such, it does not make sense to have offline CPUs: the firmware will fail initialization because of the offlined ones that the kernel did not initialize.
Futher, there is a bug: during SNP_INIT(_EX) the firmware requires the MFDM syscfg bit to be set in addition to having SNP enabled, which the previous hotplug code did not do. Since k8_check_syscfg_dram_mod_en() enforces this be cleared, hotplug wouldn't work.
--- arch/x86/virt/svm/sev.c | 32 +++++--------------------------- 1 file changed, 5 insertions(+), 27 deletions(-)
arch/x86/virt/svm/sev.c +5 -27
@@ -130,33 +130,20 @@ static unsigned long snp_nr_leaked_pages;#undef pr_fmt#define pr_fmt (fmt) "SEV-SNP: " fmt -static int __mfd_enable (unsigned int cpu) +static __init void mfd_enable (void *arg) { -u64 val; -if (!cc_platform_has (CC_ATTR_HOST_SEV_SNP )) -return 0 ; - -rdmsrq (MSR_AMD64_SYSCFG , val); - - val |=MSR_AMD64_SYSCFG_MFDM ; - -wrmsrq (MSR_AMD64_SYSCFG , val); +return ; -return 0 ; +msr_set_bit (MSR_AMD64_SYSCFG ,MSR_AMD64_SYSCFG_MFDM_BIT ); } -static __init void mfd_enable (void *arg) -{ -__mfd_enable (smp_processor_id ()); -} - -static int __snp_enable (unsigned int cpu) +static __init void snp_enable (void *arg) {u64 val;if (!cc_platform_has (CC_ATTR_HOST_SEV_SNP )) -return 0 ; +return ;rdmsrq (MSR_AMD64_SYSCFG , val);
@@ -164,13 +151,6 @@ static int __snp_enable(unsigned int cpu) val |=MSR_AMD64_SYSCFG_SNP_VMPL_EN ;wrmsrq (MSR_AMD64_SYSCFG , val); - -return 0 ; -} - -static __init void snp_enable (void *arg) -{ -__snp_enable (smp_processor_id ()); }static void __init __snp_fixup_e820_tables (u64 pa)
@@ -553,8 +533,6 @@ int __init snp_rmptable_init(void)on_each_cpu (snp_enable,NULL ,1 ); skip_enable: -cpuhp_setup_state (CPUHP_AP_ONLINE_DYN ,"x86/rmptable_init:online" , __snp_enable,NULL ); -/* * Setting crash_kexec_post_notifiers to 'true' to ensure that SNP panic * notifier is invoked to do SNP IOMMU shutdown before kdump.
Signature · 1 line
2.53.0
PATCH v2 2/10 x86/snp: drop WBINVD before setting SNPEn
In reply to Tycho AndersenFrom: "Tycho Andersen (AMD)" <[email protected]>
WBINVD is required before SNP_INIT(_EX), but not before setting SNPEn, since the ccp driver already does its own WBINVD before SNP_INIT (and this one would be too early for that anyway...).
--- arch/x86/virt/svm/sev.c | 3 --- 1 file changed, 3 deletions(-)
arch/x86/virt/svm/sev.c +0 -3
@@ -524,9 +524,6 @@ int __init snp_rmptable_init(void)memset (desc->rmp_entry,0 , desc->size); } -/* Flush the caches to ensure that data is written before SNP is enabled. */ -wbinvd_on_all_cpus (); -/* 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
PATCH v2 3/10 x86/snp: Keep the RMP table bookkeeping area mapped
In reply to Tycho AndersenFrom: Tom Lendacky <[email protected]>
In preparation for delayed SNP initialization and disablement on shutdown, the RMP will need to be cleared each time SNP is disabled. Maintain the mapping to the RMP bookkeeping area to avoid mapping and unmapping it each time and any possible errors that may arise from that.
--- arch/x86/virt/svm/sev.c | 40 +++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 23 deletions(-)
arch/x86/virt/svm/sev.c +17 -23
@@ -117,6 +117,8 @@ static u64 rmp_segment_mask;static u64 rmp_cfg; +static void *rmp_bookkeeping__ro_after_init ; +/* Mask to apply to a PFN to get the first PFN of a 2MB page */ #define PFN_PMD_MASK GENMASK_ULL(63, PMD_SHIFT - PAGE_SHIFT)
@@ -240,23 +242,6 @@ void __init snp_fixup_e820_tables(void) } } -static bool __init clear_rmptable_bookkeeping (void ) -{ -void *bk; - - bk =memremap (probed_rmp_base,RMPTABLE_CPU_BOOKKEEPING_SZ ,MEMREMAP_WB ); -if (!bk) { -pr_err ("Failed to map RMP bookkeeping area\n" ); -return false; - } - -memset (bk,0 ,RMPTABLE_CPU_BOOKKEEPING_SZ ); - -memunmap (bk); - -return true; -} -static bool __init alloc_rmp_segment_desc (u64 segment_pa,u64 segment_size,u64 pa) {u64 rst_index, rmp_segment_size_max;
@@ -474,10 +459,22 @@ static bool __init setup_segmented_rmptable(void)static bool __init setup_rmptable (void ) {if (rmp_cfg &MSR_AMD64_SEG_RMP_ENABLED ) { -return setup_segmented_rmptable (); +if (!setup_segmented_rmptable ()) +return false; }else { -return setup_contiguous_rmptable (); +if (!setup_contiguous_rmptable ()) +return false; } + + rmp_bookkeeping =memremap (probed_rmp_base,RMPTABLE_CPU_BOOKKEEPING_SZ ,MEMREMAP_WB ); +if (!rmp_bookkeeping) { +pr_err ("Failed to map RMP bookkeeping area\n" ); +free_rmp_segment_table (); + +return false; + } + +return true; }/*
@@ -508,10 +505,7 @@ int __init snp_rmptable_init(void) goto skip_enable;/* Zero out the RMP bookkeeping area */ -if (!clear_rmptable_bookkeeping ()) { -free_rmp_segment_table (); -return -ENOSYS ; - } +memset (rmp_bookkeeping,0 ,RMPTABLE_CPU_BOOKKEEPING_SZ );/* Zero out the RMP entries */ for (i =0 ; i < rst_max_index; i++) {
Signature · 1 line
2.53.0
PATCH v2 4/10 x86/snp: Create a function to clear/zero the RMP
In reply to Tycho AndersenFrom: Tom Lendacky <[email protected]>
In prep for delayed SNP initialization and disablement on shutdown, create a function, snp_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
@@ -242,6 +242,32 @@ void __init snp_fixup_e820_tables(void) } } +static void snp_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); - } +snp_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
PATCH v2 5/10 x86/snp: create snp_prepare_for_snp_init()
In reply to Tycho AndersenFrom: "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
@@ -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
PATCH v2 6/10 x86/snp, crypto: move SNP init to ccp driver
In reply to Tycho AndersenFrom: "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
@@ -543,8 +543,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
@@ -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
PATCH v2 7/10 x86/snp, crypto: move HSAVE_PA setup to arch/
In reply to Tycho AndersenFrom: "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
@@ -503,6 +503,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;
@@ -524,6 +529,9 @@ void snp_prepare_for_snp_init(void)on_each_cpu (mfd_enable,NULL ,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
@@ -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
PATCH v2 8/10 x86/snp: create snp_x86_shutdown()
In reply to Tycho AndersenFrom: "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
@@ -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
@@ -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)
@@ -526,7 +529,7 @@ 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 );
@@ -535,6 +538,20 @@ void snp_prepare_for_snp_init(void) }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 ; + +snp_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
PATCH v2 9/10 crypto: ccp - implement SNP x86 shutdown
In reply to Tycho AndersenFrom: "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
@@ -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
@@ -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
PATCH v2 10/10 crypto: ccp - Update HV_FIXED page states to allow freeing of memory
In reply to Tycho AndersenFrom: 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
@@ -1219,7 +1219,7 @@ static void snp_add_hv_fixed_pages(struct sev_device *sev, struct sev_data_rangestatic 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
[tip: x86/sev] x86/virt/sev: Drop WBINVD before setting MSR_AMD64_SYSCFG_SNP_EN
In reply to Tycho AndersenThe following commit has been merged into the x86/sev branch of tip:
The following commit has been merged into the x86/sev branch of tip:
Commit-ID: 99cf1fb58e68291d408b4c4484181cf88f081857 Gitweb: https://git.kernel.org/tip/99cf1fb58e68291d408b4c4484181cf88f081857 Author: Tycho Andersen (AMD) <[email protected]> AuthorDate: Mon, 09 Mar 2026 12:00:44 -06:00 Committer: Borislav Petkov (AMD) <[email protected]> CommitterDate: Mon, 09 Mar 2026 21:48:46 +01:00
x86/virt/sev: Drop WBINVD before setting MSR_AMD64_SYSCFG_SNP_EN
WBINVD is required before SNP_INIT(_EX), but not before setting MSR_AMD64_SYSCFG_SNP_EN, since the ccp driver already does its own WBINVD before SNP_INIT (and this one would be too early for that anyway...).
--- arch/x86/virt/svm/sev.c | 3 --- 1 file changed, 3 deletions(-)
arch/x86/virt/svm/sev.c +0 -3
@@ -524,9 +524,6 @@ int __init snp_rmptable_init(void)memset (desc->rmp_entry,0 , desc->size); } -/* Flush the caches to ensure that data is written before SNP is enabled. */ -wbinvd_on_all_cpus (); -/* MtrrFixDramModEn must be enabled on all the CPUs prior to enabling SNP. */ on_each_cpu (mfd_enable,NULL ,1 );
[tip: x86/sev] x86/virt/sev: Keep the RMP table bookkeeping area mapped
In reply to Tycho AndersenThe following commit has been merged into the x86/sev branch of tip:
The following commit has been merged into the x86/sev branch of tip:
Commit-ID: cca149429956745293ea94865ff8be6f77cc003d Gitweb: https://git.kernel.org/tip/cca149429956745293ea94865ff8be6f77cc003d Author: Tom Lendacky <[email protected]> AuthorDate: Mon, 09 Mar 2026 12:00:45 -06:00 Committer: Borislav Petkov (AMD) <[email protected]> CommitterDate: Mon, 09 Mar 2026 21:49:18 +01:00
x86/virt/sev: Keep the RMP table bookkeeping area mapped
In preparation for delayed SNP initialization and disablement on shutdown, the RMP will need to be cleared each time SNP is disabled. Maintain the mapping to the RMP bookkeeping area to avoid mapping and unmapping it each time and any possible errors that may arise from that.
--- arch/x86/virt/svm/sev.c | 40 +++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 23 deletions(-)
arch/x86/virt/svm/sev.c +17 -23
@@ -117,6 +117,8 @@ static u64 rmp_segment_mask;static u64 rmp_cfg; +static void *rmp_bookkeeping__ro_after_init ; +/* Mask to apply to a PFN to get the first PFN of a 2MB page */ #define PFN_PMD_MASK GENMASK_ULL(63, PMD_SHIFT - PAGE_SHIFT)
@@ -240,23 +242,6 @@ void __init snp_fixup_e820_tables(void) } } -static bool __init clear_rmptable_bookkeeping (void ) -{ -void *bk; - - bk =memremap (probed_rmp_base,RMPTABLE_CPU_BOOKKEEPING_SZ ,MEMREMAP_WB ); -if (!bk) { -pr_err ("Failed to map RMP bookkeeping area\n" ); -return false; - } - -memset (bk,0 ,RMPTABLE_CPU_BOOKKEEPING_SZ ); - -memunmap (bk); - -return true; -} -static bool __init alloc_rmp_segment_desc (u64 segment_pa,u64 segment_size,u64 pa) {u64 rst_index, rmp_segment_size_max;
@@ -474,10 +459,22 @@ e_free:static bool __init setup_rmptable (void ) {if (rmp_cfg &MSR_AMD64_SEG_RMP_ENABLED ) { -return setup_segmented_rmptable (); +if (!setup_segmented_rmptable ()) +return false; }else { -return setup_contiguous_rmptable (); +if (!setup_contiguous_rmptable ()) +return false; } + + rmp_bookkeeping =memremap (probed_rmp_base,RMPTABLE_CPU_BOOKKEEPING_SZ ,MEMREMAP_WB ); +if (!rmp_bookkeeping) { +pr_err ("Failed to map RMP bookkeeping area\n" ); +free_rmp_segment_table (); + +return false; + } + +return true; }/*
@@ -508,10 +505,7 @@ int __init snp_rmptable_init(void) goto skip_enable;/* Zero out the RMP bookkeeping area */ -if (!clear_rmptable_bookkeeping ()) { -free_rmp_segment_table (); -return -ENOSYS ; - } +memset (rmp_bookkeeping,0 ,RMPTABLE_CPU_BOOKKEEPING_SZ );/* Zero out the RMP entries */ for (i =0 ; i < rst_max_index; i++) {
[tip: x86/sev] x86/virt/sev: Drop support for SNP hotplug
In reply to Tycho AndersenThe following commit has been merged into the x86/sev branch of tip:
The following commit has been merged into the x86/sev branch of tip:
Commit-ID: 959d3f7565352fab844ae73a722631c5f615427f Gitweb: https://git.kernel.org/tip/959d3f7565352fab844ae73a722631c5f615427f Author: Tycho Andersen (AMD) <[email protected]> AuthorDate: Mon, 09 Mar 2026 12:00:43 -06:00 Committer: Borislav Petkov (AMD) <[email protected]> CommitterDate: Mon, 09 Mar 2026 21:48:33 +01:00
x86/virt/sev: Drop support for SNP hotplug
During an SNP_INIT(_EX), the SEV firmware checks that all CPUs have the SNP syscfg bit set, and fails if they do not. As such, it does not make sense to have offline CPUs: the firmware will fail initialization because of the offlined ones that the kernel did not initialize.
Further, there is a bug: during SNP_INIT(_EX) the firmware requires the MFDM syscfg bit to be set in addition to having SNP enabled, which the previous hotplug code did not do. Since k8_check_syscfg_dram_mod_en() enforces this be cleared, hotplug wouldn't work.
--- arch/x86/virt/svm/sev.c | 32 +++++--------------------------- 1 file changed, 5 insertions(+), 27 deletions(-)
arch/x86/virt/svm/sev.c +5 -27
@@ -130,33 +130,20 @@ static unsigned long snp_nr_leaked_pages;#undef pr_fmt#define pr_fmt (fmt) "SEV-SNP: " fmt -static int __mfd_enable (unsigned int cpu) +static __init void mfd_enable (void *arg) { -u64 val; -if (!cc_platform_has (CC_ATTR_HOST_SEV_SNP )) -return 0 ; - -rdmsrq (MSR_AMD64_SYSCFG , val); - - val |=MSR_AMD64_SYSCFG_MFDM ; - -wrmsrq (MSR_AMD64_SYSCFG , val); +return ; -return 0 ; +msr_set_bit (MSR_AMD64_SYSCFG ,MSR_AMD64_SYSCFG_MFDM_BIT ); } -static __init void mfd_enable (void *arg) -{ -__mfd_enable (smp_processor_id ()); -} - -static int __snp_enable (unsigned int cpu) +static __init void snp_enable (void *arg) {u64 val;if (!cc_platform_has (CC_ATTR_HOST_SEV_SNP )) -return 0 ; +return ;rdmsrq (MSR_AMD64_SYSCFG , val);
@@ -164,13 +151,6 @@ static int __snp_enable(unsigned int cpu) val |=MSR_AMD64_SYSCFG_SNP_VMPL_EN ;wrmsrq (MSR_AMD64_SYSCFG , val); - -return 0 ; -} - -static __init void snp_enable (void *arg) -{ -__snp_enable (smp_processor_id ()); }static void __init __snp_fixup_e820_tables (u64 pa)
@@ -553,8 +533,6 @@ int __init snp_rmptable_init(void)on_each_cpu (snp_enable,NULL ,1 ); skip_enable: -cpuhp_setup_state (CPUHP_AP_ONLINE_DYN ,"x86/rmptable_init:online" , __snp_enable,NULL ); -/* * Setting crash_kexec_post_notifiers to 'true' to ensure that SNP panic * notifier is invoked to do SNP IOMMU shutdown before kdump.
Borislav Petkov
maintainerRe: [PATCH v2 04/10] x86/snp: Create a function to clear/zero the RMP
In reply to Tycho AndersenQuoted 17 lines · Tycho Andersen
From: Tom Lendacky <[email protected]>
In prep for delayed SNP initialization and disablement on shutdown, create a function,
snp_clear_rmp(), that clears the RMP bookkeeping area and the RMP entries.Signed-off-by Tom Lendacky <[email protected]>Signed-off-by Tycho Andersen (AMD) <[email protected]>--- arch/x86/virt/svm/sev.c | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-)arch/x86/virt/svm/sev.c +0
-0
arch/x86/virt/svm/sev.c void __init snp_fixup_e820_tables(void) · line 245
Quoted 4 lines
arch/x86/virt/svm/sev.c +1
-0@@ -242,6 +242,32 @@ void __init snp_fixup_e820_tables(void) } } +static void snp_clear_rmp (void )
No need for "snp_" prefix on static functions.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquette