x86/cpufeatures: Add X86_FEATURE_RMPOPT feature flag
Subsystems (6)
A message that is not in the archive (sent off-list or not yet received): [email protected]
Ashish Kalra
maintainerPATCH v8 1/7 x86/cpufeatures: Add X86_FEATURE_RMPOPT feature flag
From: Ashish Kalra <[email protected]>
Add a flag indicating whether RMPOPT instruction is supported.
RMPOPT is a new instruction that reduces the performance overhead of RMP checks for the hypervisor and non-SNP guests by allowing those checks to be skipped when 1-GB memory regions are known to contain no SEV-SNP guest memory.
For more information on the RMPOPT instruction, see the AMD64 RMPOPT technical documentation.
--- arch/x86/include/asm/cpufeatures.h | 2 +- arch/x86/kernel/cpu/scattered.c | 1 + tools/arch/x86/include/asm/cpufeatures.h | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-)
arch/x86/include/asm/cpufeatures.h +1 -1
@@ -76,7 +76,7 @@#define X86_FEATURE_K8 ( 3*32+ 4)/* Opteron, Athlon64 */ #define X86_FEATURE_ZEN5 ( 3*32+ 5)/* CPU based on Zen5 microarchitecture */ #define X86_FEATURE_ZEN6 ( 3*32+ 6)/* CPU based on Zen6 microarchitecture */ -/* Free ( 3*32+ 7) */ +#define X86_FEATURE_RMPOPT ( 3*32+ 7)/* Support for AMD RMPOPT instruction */ #define X86_FEATURE_CONSTANT_TSC ( 3*32+ 8)/* "constant_tsc" TSC ticks at a constant rate */ #define X86_FEATURE_UP ( 3*32+ 9)/* "up" SMP kernel running on UP */ #define X86_FEATURE_ART ( 3*32+10)/* "art" Always running timer (ART) */
arch/x86/kernel/cpu/scattered.c +1 -0
@@ -67,6 +67,7 @@ static const struct cpuid_bit cpuid_bits[] = { { X86_FEATURE_PERFMON_V2, CPUID_EAX, 0, 0x80000022, 0 }, { X86_FEATURE_AMD_LBR_V2, CPUID_EAX, 1, 0x80000022, 0 }, { X86_FEATURE_AMD_LBR_PMC_FREEZE, CPUID_EAX, 2, 0x80000022, 0 }, + { X86_FEATURE_RMPOPT, CPUID_EDX, 0, 0x80000025, 0 }, { X86_FEATURE_AMD_HTR_CORES, CPUID_EAX, 30, 0x80000026, 0 }, { 0, 0, 0, 0, 0 } };
tools/arch/x86/include/asm/cpufeatures.h +1 -1
@@ -76,7 +76,7 @@#define X86_FEATURE_K8 ( 3*32+ 4)/* Opteron, Athlon64 */ #define X86_FEATURE_ZEN5 ( 3*32+ 5)/* CPU based on Zen5 microarchitecture */ #define X86_FEATURE_ZEN6 ( 3*32+ 6)/* CPU based on Zen6 microarchitecture */ -/* Free ( 3*32+ 7) */ +#define X86_FEATURE_RMPOPT ( 3*32+ 7)/* Support for AMD RMPOPT instruction */ #define X86_FEATURE_CONSTANT_TSC ( 3*32+ 8)/* "constant_tsc" TSC ticks at a constant rate */ #define X86_FEATURE_UP ( 3*32+ 9)/* "up" SMP kernel running on UP */ #define X86_FEATURE_ART ( 3*32+10)/* "art" Always running timer (ART) */
Signature · 1 line
2.43.0
Ashish Kalra
maintainerPATCH v8 2/7 x86/sev: Initialize RMPOPT configuration MSRs
In reply to a message not in the archiveFrom: Ashish Kalra <[email protected]>
The new RMPOPT instruction helps manage per-CPU RMP optimization structures inside the CPU. It takes a 1GB-aligned physical address and either returns the status of the optimizations or tries to enable the optimizations.
Per-CPU RMPOPT tables support at most 2 TB of addressable memory for RMP optimizations.
Initialize the per-CPU RMPOPT table base to the starting physical address. This enables RMP optimization for up to 2 TB of system RAM on all CPUs.
Additionally, add support to setup and enable RMPOPT once SNP is enabled and initialized.
--- arch/x86/coco/core.c | 2 + arch/x86/include/asm/msr-index.h | 3 ++ arch/x86/include/asm/sev.h | 4 ++ arch/x86/virt/svm/sev.c | 70 ++++++++++++++++++++++++++++++++ drivers/crypto/ccp/sev-dev.c | 3 ++ 5 files changed, 82 insertions(+)
arch/x86/coco/core.c +2 -0
@@ -16,6 +16,7 @@#include <asm/archrandom.h> #include <asm/coco.h> #include <asm/processor.h> +#include <asm/sev.h> enum cc_vendor cc_vendor__ro_after_init =CC_VENDOR_NONE ;SYM_PIC_ALIAS (cc_vendor);
@@ -172,6 +173,7 @@ static void amd_cc_platform_clear(enum cc_attr attr)switch (attr) {case CC_ATTR_HOST_SEV_SNP : cc_flags.host_sev_snp =0 ; +snp_clear_rmpopt_configured ();break ;default :break ;
arch/x86/include/asm/msr-index.h +3 -0
@@ -761,6 +761,9 @@#define MSR_AMD64_SEG_RMP_ENABLED_BIT 0#define MSR_AMD64_SEG_RMP_ENABLED BIT_ULL(MSR_AMD64_SEG_RMP_ENABLED_BIT)#define MSR_AMD64_RMP_SEGMENT_SHIFT (x) (((x) & GENMASK_ULL(13, 8)) >> 8) +#define MSR_AMD64_RMPOPT_BASE 0xc0010139 +#define MSR_AMD64_RMPOPT_ENABLE_BIT 0 +#define MSR_AMD64_RMPOPT_ENABLE BIT_ULL(MSR_AMD64_RMPOPT_ENABLE_BIT)#define MSR_SVSM_CAA 0xc001f000
arch/x86/include/asm/sev.h +4 -0
@@ -662,6 +662,8 @@ static inline void snp_leak_pages(u64 pfn, unsigned int pages) __snp_leak_pages(pfn, pages, true); }int snp_prepare (void ); +void snp_setup_rmpopt (void ); +void snp_clear_rmpopt_configured (void );void snp_shutdown (void );#else static inline bool snp_probe_rmptable_info (void ) {return false; }
@@ -680,6 +682,8 @@ 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 int snp_prepare (void ) {return -ENODEV ; } +static inline void snp_setup_rmpopt (void ) {} +static inline void snp_clear_rmpopt_configured (void ) {}static inline void snp_shutdown (void ) {}#endif
arch/x86/virt/svm/sev.c +70 -0
100 lines not on this page · raw message
drivers/crypto/ccp/sev-dev.c +3 -0
@@ -1478,6 +1478,9 @@ static int __sev_snp_init_locked(int *error, unsigned int max_snp_asid) }snp_hv_fixed_pages_state_update (sev,HV_FIXED ); + +snp_setup_rmpopt (); + sev->snp_initialized = true;dev_dbg (sev->dev,"SEV-SNP firmware initialized, SEV-TIO is %s\n" , data.tio_en ?"enabled" :"disabled" );
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v8 2/7] x86/sev: Initialize RMPOPT configuration MSRs
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] MSR_AMD64_RMPOPT_BASE is not restored after suspend/resume, silently disabling the RMPOPT feature…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] MSR_AMD64_RMPOPT_BASE is not restored after suspend/resume, silently disabling the RMPOPT feature and leading to unhandled faults.
- [High] wrmsrq_on_cpu() silently swallows hardware faults, making WARN_ON_ONCE() checks ineffective and masking MSR setup failures. --
commit 205a673f128c777438687610b84781bd7b8a3533
Author: Ashish Kalra <[email protected]>
x86/sev: Initialize RMPOPT configuration MSRs
The new RMPOPT instruction helps manage per-CPU RMP optimization structures inside the CPU. Initialize the per-CPU RMPOPT table base to the starting physical address to enable RMP optimization for up to 2 TB of system RAM on all CPUs.
Quoted 4 lines
arch/x86/virt/svm/sev.c +0
-0
[ ... ]
arch/x86/virt/svm/sev.c int snp_prepare(void) · line 574
Quoted 12 lines
arch/x86/virt/svm/sev.c +8
-0@@ -555,6 +564,21 @@ int snp_prepare(void) }EXPORT_SYMBOL_FOR_MODULES (snp_prepare,"ccp" ); +static void rmpopt_cleanup (void ) +{ +int cpu; + +cpus_read_lock (); + +for_each_cpu (cpu, &rmpopt_cpumask) +WARN_ON_ONCE (wrmsrq_on_cpu (cpu,MSR_AMD64_RMPOPT_BASE ,0 ));
[Severity: High]
Does wrmsrq_on_cpu() return an error if the MSR write causes a hardware fault?
Looking at wrmsrq_on_cpu(), it uses wrmsr() which relies on the exception table handler to log the error and resume execution, and always returns 0 if the IPI succeeds.
If the MSR write causes a fault, it seems this WARN_ON_ONCE() won't trigger, masking a potential disablement failure. Should wrmsrq_safe_on_cpu() be used here instead?
Quoted 7 lines
arch/x86/virt/svm/sev.c +7
-0+ +cpus_read_unlock (); + +cpumask_clear (&rmpopt_cpumask); + rmpopt_pa_start =0 ; +} +
[ ... ]
Quoted 4 lines
arch/x86/virt/svm/sev.c +4
-0+void snp_setup_rmpopt (void ) +{ +u64 rmpopt_base; +int cpu;
[ ... ]
arch/x86/virt/svm/sev.c void snp_shutdown(void) · line 635
Quoted 8 lines
arch/x86/virt/svm/sev.c +8
-0+/* +* Per-CPU RMPOPT tables support at most 2 TB of addressable memory +* for RMP optimizations. Initialize the per-CPU RMPOPT table base +* to the starting physical address to enable RMP optimizations for +* up to 2 TB of system RAM on all CPUs. +*/ +for_each_cpu (cpu, &rmpopt_cpumask) +WARN_ON_ONCE (wrmsrq_on_cpu (cpu,MSR_AMD64_RMPOPT_BASE , rmpopt_base));
[Severity: High]
If a system goes into a sleep state like suspend-to-RAM (S3), CPU MSRs are typically reset.
Since this MSR is initialized here during boot, how is it restored when the system wakes up?
Without a power management callback like syscore_ops resume hooks or CPU PM notifiers, will the RMPOPT feature be silently disabled in hardware? While a later patch disables CPU hotplug, S3 suspend via freeze_secondary_cpus() explicitly bypasses the cpu_hotplug_disabled check, making this a reachable path.
Also, similar to rmpopt_cleanup(), if this MSR setup silently fails because wrmsrq_on_cpu() swallows hardware faults, could subsequent RMPOPT instructions cause unhandled faults and lead to a kernel panic?
Quoted 4 lines
arch/x86/virt/svm/sev.c +4
-0+ +cpus_read_unlock (); +} +EXPORT_SYMBOL_FOR_MODULES (snp_setup_rmpopt,"ccp" );
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH v8 2/7] x86/sev: Initialize RMPOPT configuration MSRs
In reply to Ashish KalraHello Ashish,
arch/x86/virt/svm/sev.c static void *rmp_bookkeeping __ro_after_init; · line 127
Quoted 9 lines · Ashish Kalra
arch/x86/virt/svm/sev.c +1
-0@@ -124,6 +124,10 @@ static void *rmp_bookkeeping __ro_after_init;static u64 probed_rmp_base, probed_rmp_size; +static cpumask_t rmpopt_cpumask;
nit.
I believe you can use cpumask_var_t here and do a zalloc_cpumask_var() during snp_setup_rmpopt(). That way !X86_FEATURE_RMPOPT configs don't have to needlessly waste space to keep a redundant cpumask around.
Same comment for rmpopt_report_cpumask in Patch 7 which can be allocated dynamically during rmpopt_debugfs_setup().
Quoted 20 lines
arch/x86/virt/svm/sev.c +8
-0+static phys_addr_t rmpopt_pa_start; +static bool rmpopt_configured; +static LIST_HEAD (snp_leaked_pages_list);static DEFINE_SPINLOCK (snp_leaked_pages_list_lock);@@ -490,7 +494,12 @@ static bool __init setup_rmptable(void)if (rmp_cfg &MSR_AMD64_SEG_RMP_ENABLED ) {if (!setup_segmented_rmptable ())return false; + rmpopt_configured = true; }else { +/* +* RMPOPT requires a segmented RMP table, so leave +* rmpopt_configured clear on contiguous RMP systems. +*/ if (!setup_contiguous_rmptable ())return false; }
arch/x86/virt/svm/sev.c int snp_prepare(void) · line 571
Quoted 8 lines
arch/x86/virt/svm/sev.c +5
-0@@ -555,6 +564,21 @@ int snp_prepare(void) }EXPORT_SYMBOL_FOR_MODULES (snp_prepare,"ccp" ); +static void rmpopt_cleanup (void ) +{ +int cpu; + +cpus_read_lock ();
nit.
You can use guard(cpus_read_lock)() unless there is a complicated locking pattern where you need to drop and re-acquire the read lock.
Quoted 13 lines
arch/x86/virt/svm/sev.c +10
-0+ +for_each_cpu (cpu, &rmpopt_cpumask) +WARN_ON_ONCE (wrmsrq_on_cpu (cpu,MSR_AMD64_RMPOPT_BASE ,0 )); + +cpus_read_unlock (); + +cpumask_clear (&rmpopt_cpumask); + rmpopt_pa_start =0 ; +} +void snp_shutdown (void ) {u64 syscfg;
Signature · 2 lines
Thanks and Regards, Prateek
Kalra, Ashish
maintainerRe: [PATCH v8 2/7] x86/sev: Initialize RMPOPT configuration MSRs
Hello Prateek,
Quoted 15 lines · K Prateek Nayak
Hello Ashish,
arch/x86/virt/svm/sev.c static void *rmp_bookkeeping __ro_after_init; · line 127
Quoted 9 lines · Ashish Kalra
arch/x86/virt/svm/sev.c +1
-0@@ -124,6 +124,10 @@ static void *rmp_bookkeeping __ro_after_init;static u64 probed_rmp_base, probed_rmp_size; +static cpumask_t rmpopt_cpumask;nit.
Quoted 7 lines
I believe you can use
cpumask_var_there and do azalloc_cpumask_var()duringsnp_setup_rmpopt(). That way !X86_FEATURE_RMPOPT configs don't have to needlessly waste space to keep a redundant cpumask around.Same comment for
rmpopt_report_cpumaskin Patch 7 which can be allocated dynamically duringrmpopt_debugfs_setup().
Yes.
Quoted 31 lines
Quoted 20 lines
arch/x86/virt/svm/sev.c +8
-0+static phys_addr_t rmpopt_pa_start; +static bool rmpopt_configured; +static LIST_HEAD (snp_leaked_pages_list);static DEFINE_SPINLOCK (snp_leaked_pages_list_lock);@@ -490,7 +494,12 @@ static bool __init setup_rmptable(void)if (rmp_cfg &MSR_AMD64_SEG_RMP_ENABLED ) {if (!setup_segmented_rmptable ())return false; + rmpopt_configured = true; }else { +/* +* RMPOPT requires a segmented RMP table, so leave +* rmpopt_configured clear on contiguous RMP systems. +*/ if (!setup_contiguous_rmptable ())return false; }arch/x86/virt/svm/sev.c int snp_prepare(void) · line 571
Quoted 8 lines
arch/x86/virt/svm/sev.c +5
-0@@ -555,6 +564,21 @@ int snp_prepare(void) }EXPORT_SYMBOL_FOR_MODULES (snp_prepare,"ccp" ); +static void rmpopt_cleanup (void ) +{ +int cpu; + +cpus_read_lock ();nit.
Quoted 2 lines
You can use guard(cpus_read_lock)() unless there is a complicated locking pattern where you need to drop and re-acquire the read lock.
But if i use guard(cpus_read_lock)(), cpus_read_lock stays held across as it is function-scope, so it will be still held for code following the wrmsrq_on_cpu(), which is harmless but still changes code behavior.
Probably, the other option is to use scoped_guard form ?
Thanks,
Ashish
Quoted 15 lines
Quoted 14 lines
arch/x86/virt/svm/sev.c +10
-0+ +for_each_cpu (cpu, &rmpopt_cpumask) +WARN_ON_ONCE (wrmsrq_on_cpu (cpu,MSR_AMD64_RMPOPT_BASE ,0 )); + +cpus_read_unlock (); + +cpumask_clear (&rmpopt_cpumask); + rmpopt_pa_start =0 ; +} +void snp_shutdown (void ) {u64 syscfg;
Tom Lendacky
maintainerRe: [PATCH v8 2/7] x86/sev: Initialize RMPOPT configuration MSRs
In reply to Ashish KalraQuoted 90 lines · Ashish Kalra
From: Ashish Kalra <[email protected]>
The new RMPOPT instruction helps manage per-CPU RMP optimization structures inside the CPU. It takes a 1GB-aligned physical address and either returns the status of the optimizations or tries to enable the optimizations.
Per-CPU RMPOPT tables support at most 2 TB of addressable memory for RMP optimizations.
Initialize the per-CPU RMPOPT table base to the starting physical address. This enables RMP optimization for up to 2 TB of system RAM on all CPUs.
Additionally, add support to setup and enable RMPOPT once SNP is enabled and initialized.
Suggested-by Thomas Lendacky <[email protected]>Suggested-by Dave Hansen <[email protected]>Reviewed-by Dave Hansen <[email protected]>Signed-off-by Ashish Kalra <[email protected]>--- arch/x86/coco/core.c | 2 + arch/x86/include/asm/msr-index.h | 3 ++ arch/x86/include/asm/sev.h | 4 ++ arch/x86/virt/svm/sev.c | 70 ++++++++++++++++++++++++++++++++ drivers/crypto/ccp/sev-dev.c | 3 ++ 5 files changed, 82 insertions(+)arch/x86/coco/core.c +2
-0@@ -16,6 +16,7 @@#include <asm/archrandom.h> #include <asm/coco.h> #include <asm/processor.h> +#include <asm/sev.h> enum cc_vendor cc_vendor__ro_after_init =CC_VENDOR_NONE ;SYM_PIC_ALIAS (cc_vendor);@@ -172,6 +173,7 @@ static void amd_cc_platform_clear(enum cc_attr attr)switch (attr) {case CC_ATTR_HOST_SEV_SNP : cc_flags.host_sev_snp =0 ; +snp_clear_rmpopt_configured ();break ;default :break ;arch/x86/include/asm/msr-index.h +3
-0@@ -761,6 +761,9 @@#define MSR_AMD64_SEG_RMP_ENABLED_BIT 0#define MSR_AMD64_SEG_RMP_ENABLED BIT_ULL(MSR_AMD64_SEG_RMP_ENABLED_BIT)#define MSR_AMD64_RMP_SEGMENT_SHIFT (x) (((x) & GENMASK_ULL(13, 8)) >> 8) +#define MSR_AMD64_RMPOPT_BASE 0xc0010139 +#define MSR_AMD64_RMPOPT_ENABLE_BIT 0 +#define MSR_AMD64_RMPOPT_ENABLE BIT_ULL(MSR_AMD64_RMPOPT_ENABLE_BIT)#define MSR_SVSM_CAA 0xc001f000arch/x86/include/asm/sev.h +4
-0@@ -662,6 +662,8 @@ static inline void snp_leak_pages(u64 pfn, unsigned int pages) __snp_leak_pages(pfn, pages, true); }int snp_prepare (void ); +void snp_setup_rmpopt (void ); +void snp_clear_rmpopt_configured (void );void snp_shutdown (void );#else static inline bool snp_probe_rmptable_info (void ) {return false; }@@ -680,6 +682,8 @@ 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 int snp_prepare (void ) {return -ENODEV ; } +static inline void snp_setup_rmpopt (void ) {} +static inline void snp_clear_rmpopt_configured (void ) {}static inline void snp_shutdown (void ) {}#endif arch/x86/virt/svm/sev.c +0
-0
arch/x86/virt/svm/sev.c static void *rmp_bookkeeping __ro_after_init; · line 129
Quoted 6 lines
arch/x86/virt/svm/sev.c +3
-0@@ -124,6 +124,10 @@ static void *rmp_bookkeeping __ro_after_init;static u64 probed_rmp_base, probed_rmp_size; +static cpumask_t rmpopt_cpumask; +static phys_addr_t rmpopt_pa_start; +static bool rmpopt_configured;
The usage of this isn't doesn't imply what the name says. How about changing it to rmpopt_capable ?
Quoted 5 lines
arch/x86/virt/svm/sev.c +1
-0+static LIST_HEAD (snp_leaked_pages_list);static DEFINE_SPINLOCK (snp_leaked_pages_list_lock);
arch/x86/virt/svm/sev.c static bool __init setup_rmptable(void) · line 502
Quoted 9 lines
arch/x86/virt/svm/sev.c +5
-0@@ -490,7 +494,12 @@ static bool __init setup_rmptable(void)if (rmp_cfg &MSR_AMD64_SEG_RMP_ENABLED ) {if (!setup_segmented_rmptable ())return false; + rmpopt_configured = true; }else { +/* +* RMPOPT requires a segmented RMP table, so leave +* rmpopt_configured clear on contiguous RMP systems. +*/
I think this comment should be above where rmpopt_configured is set, slightly changed to
RMPOPT requires a segmented RMP, so indicate that the system is capable of configuring and running RMPOPT.
Thanks,
Tom
Quoted 97 lines
arch/x86/virt/svm/sev.c +61
-0if (!setup_contiguous_rmptable ())return false; }@@ -555,6 +564,21 @@ int snp_prepare(void) }EXPORT_SYMBOL_FOR_MODULES (snp_prepare,"ccp" ); +static void rmpopt_cleanup (void ) +{ +int cpu; + +cpus_read_lock (); + +for_each_cpu (cpu, &rmpopt_cpumask) +WARN_ON_ONCE (wrmsrq_on_cpu (cpu,MSR_AMD64_RMPOPT_BASE ,0 )); + +cpus_read_unlock (); + +cpumask_clear (&rmpopt_cpumask); + rmpopt_pa_start =0 ; +} +void snp_shutdown (void ) {u64 syscfg;@@ -563,11 +587,57 @@ void snp_shutdown(void)if (syscfg &MSR_AMD64_SYSCFG_SNP_EN )return ; +rmpopt_cleanup (); +clear_rmp ();on_each_cpu (mfd_reconfigure,NULL ,1 ); }EXPORT_SYMBOL_FOR_MODULES (snp_shutdown,"ccp" ); +void snp_clear_rmpopt_configured (void ) +{ + rmpopt_configured = false; +} + +void snp_setup_rmpopt (void ) +{ +u64 rmpopt_base; +int cpu; + +if (!cpu_feature_enabled (X86_FEATURE_RMPOPT ) || !rmpopt_configured) +return ; + +cpus_read_lock (); + +/* +* The RMPOPT_BASE MSR is per-core, so only one thread per core needs +* to set up the RMPOPT_BASE MSR. +* +* Note: only online primary threads are included. If a core's +* primary thread is offline, that core is not covered. CPU hotplug +* is not currently supported with SNP enabled. +*/ + +for_each_online_cpu (cpu) +if (topology_is_primary_thread (cpu)) +cpumask_set_cpu (cpu, &rmpopt_cpumask); + + rmpopt_pa_start =ALIGN_DOWN (PFN_PHYS (min_low_pfn),SZ_1G ); + rmpopt_base = rmpopt_pa_start |MSR_AMD64_RMPOPT_ENABLE ; + +/* +* Per-CPU RMPOPT tables support at most 2 TB of addressable memory +* for RMP optimizations. Initialize the per-CPU RMPOPT table base +* to the starting physical address to enable RMP optimizations for +* up to 2 TB of system RAM on all CPUs. +*/ +for_each_cpu (cpu, &rmpopt_cpumask) +WARN_ON_ONCE (wrmsrq_on_cpu (cpu,MSR_AMD64_RMPOPT_BASE , rmpopt_base)); + +cpus_read_unlock (); +} +EXPORT_SYMBOL_FOR_MODULES (snp_setup_rmpopt,"ccp" ); +/* * Do the necessary preparations which are verified by the firmware as * described in the SNP_INIT_EX firmware command description in the SNP drivers/crypto/ccp/sev-dev.c +3
-0@@ -1478,6 +1478,9 @@ static int __sev_snp_init_locked(int *error, unsigned int max_snp_asid) }snp_hv_fixed_pages_state_update (sev,HV_FIXED ); + +snp_setup_rmpopt (); + sev->snp_initialized = true;dev_dbg (sev->dev,"SEV-SNP firmware initialized, SEV-TIO is %s\n" , data.tio_en ?"enabled" :"disabled" );
Ashish Kalra
maintainerPATCH v8 3/7 crypto/ccp: Disable CPU hotplug while SNP is active
In reply to a message not in the archiveFrom: Ashish Kalra <[email protected]>
The SEV firmware enumerates the CPUs at SNP initialization and is not aware of the OS bringing CPUs online or offline afterwards, so OS CPU hotplug can diverge from the firmware's expectations and break SNP. Disable CPU hotplug while SNP is active.
SNP is fully torn down only on the SNP_SHUTDOWN_EX x86_snp_shutdown path; the legacy path leaves SNP enabled in hardware while clearing snp_initialized, so __sev_snp_init_locked() can run again. Track the disable with a flag so it is balanced by a matching enable rather than stacked, and re-enable hotplug only on the x86_snp_shutdown path, after snp_shutdown() has cleared the per-core RMPOPT_BASE MSRs with hotplug still disabled.
This also keeps the CPU set stable for the asynchronous RMPOPT scan added later in this series, and ensures cpus_read_lock() in the scan is uncontended.
--- drivers/crypto/ccp/sev-dev.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-)
drivers/crypto/ccp/sev-dev.c +28 -1
@@ -106,6 +106,9 @@ struct snp_hv_fixed_pages_entry {static LIST_HEAD (snp_hv_fixed_pages); +/* Set while SNP has CPU hotplug disabled. */ +static bool snp_cpu_hotplug_disabled; +/* Trusted Memory Region (TMR): * The TMR is a 1MB area that must be 1MB aligned. Use the page allocator * to allocate the memory, which will return aligned memory for the specified
@@ -1479,6 +1482,17 @@ static int __sev_snp_init_locked(int *error, unsigned int max_snp_asid)snp_hv_fixed_pages_state_update (sev,HV_FIXED ); +/* +* Disable CPU hotplug while SNP is active. Guard against stacking +* the disable count: the legacy SNP_SHUTDOWN_EX path clears +* snp_initialized without re-enabling hotplug, so this can run +* again while hotplug is already disabled. +*/ +if (!snp_cpu_hotplug_disabled) { +cpu_hotplug_disable (); + snp_cpu_hotplug_disabled = true; + } +snp_setup_rmpopt (); sev->snp_initialized = true;
@@ -2083,8 +2097,21 @@ static int __sev_snp_shutdown_locked(int *error, bool panic) }if (data.x86_snp_shutdown) { -if (!panic) +if (!panic) {snp_shutdown (); +/* +* snp_shutdown() fully tears SNP down (clear_rmp()) and +* has already cleared the per-core RMPOPT_BASE MSRs via +* rmpopt_cleanup() with hotplug still disabled. Re-enable +* CPU hotplug now. On the legacy path SNP stays +* enabled in hardware, so hotplug is correctly left +* disabled. +*/ +if (snp_cpu_hotplug_disabled) { +cpu_hotplug_enable (); + snp_cpu_hotplug_disabled = false; + } + }snp_hv_fixed_pages_state_update (sev,ALLOCATED ); }else {/*
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v8 3/7] crypto/ccp: Disable CPU hotplug while SNP is active
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
New issues:
- [Medium] The snp_cpu_hotplug_disabled flag is a module-local static variable used to track the global cpu_hotplug_disabled counter. On systems with legacy firmware, CPU hotplug is intentionally left disabled when shutting down SNP. However, unloading and reloading the ccp module resets this static flag, causing subsequent SNP initializations to call cpu_hotplug_disable() again. This unintentionally stacks the global counter, leading to a reference leak over multiple load/unload cycles.
Pre-existing issues:
- [High] If SEV_CMD_SNP_DF_FLUSH fails during SNP initialization, the firmware state of HV_FIXED pages is leaked, potentially leading to RMP faults and host crashes when the pages are freed. --
commit f12f8db5e522836adb88644cf2b682abf1545290
Author: Ashish Kalra <[email protected]>
crypto/ccp: Disable CPU hotplug while SNP is active
This commit disables CPU hotplug during SNP execution to prevent divergence between OS and firmware CPU expectations. It adds a static flag to balance enable/disable calls and avoids re-enabling hotplug on the legacy shutdown path.
Quoted 4 lines
drivers/crypto/ccp/sev-dev.c +0
-0
[ ... ]
drivers/crypto/ccp/sev-dev.c static int __sev_snp_init_locked(int *error, unsigned int max_snp_asid) · line 1482
Quoted 3 lines
drivers/crypto/ccp/sev-dev.c +0
-0@@ -1478,6 +1481,17 @@ static int __sev_snp_init_locked(int *error, unsigned int max_snp_asid)snp_hv_fixed_pages_state_update (sev,HV_FIXED );
[Severity: High]
This isn't a bug introduced by this patch, but if the preceding SEV_CMD_SNP_DF_FLUSH command fails, the function returns early and bypasses updating the software state of the HV_FIXED pages.
If the caller handles the initialization failure and cleans up via snp_free_hv_fixed_pages(), will it see the state as ALLOCATED instead of HV_FIXED?
drivers/crypto/ccp/sev-dev.c:snp_free_hv_fixed_pages() {
...
if (entry->page_state == HV_FIXED) {
entry->free = true;
} else {
__free_pages(page, entry->order);
list_del(&entry->list);
kfree(entry);
}
...
}Could this incorrectly free firmware-locked pages to the buddy allocator, potentially leading to RMP violations and host crashes later when those pages are accessed?
drivers/crypto/ccp/sev-dev.c static int __sev_snp_init_locked(int *error, unsigned int max_snp_asid) · line 1496
Quoted 13 lines
drivers/crypto/ccp/sev-dev.c +11
-0+/* +* Disable CPU hotplug while SNP is active. Guard against stacking +* the disable count: the legacy SNP_SHUTDOWN_EX path clears +* snp_initialized without re-enabling hotplug, so this can run +* again while hotplug is already disabled. +*/ +if (!snp_cpu_hotplug_disabled) { +cpu_hotplug_disable (); + snp_cpu_hotplug_disabled = true; + } +snp_setup_rmpopt ();
[Severity: Medium]
Since snp_cpu_hotplug_disabled is a module-local static variable, does it get destroyed and reset to false when the ccp module is unloaded?
If the legacy shutdown path intentionally keeps hotplug disabled, would unloading and reloading the module cause this code to evaluate to true?
If so, won't cpu_hotplug_disable() be called again, stacking the global counter and causing a reference leak over multiple load/unload cycles?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Re: [PATCH v8 3/7] crypto/ccp: Disable CPU hotplug while SNP is active
In reply to Ashish KalraHello Ashish,
Quoted 6 lines · Ashish Kalra
From: Ashish Kalra <[email protected]>
The SEV firmware enumerates the CPUs at SNP initialization and is not aware of the OS bringing CPUs online or offline afterwards, so OS CPU hotplug can diverge from the firmware's expectations and break SNP. Disable CPU hotplug while SNP is active.
Dumb question: Is this specific to RMPOPT? Otherwise ...
Quoted 38 lines
SNP is fully torn down only on the
SNP_SHUTDOWN_EXx86_snp_shutdownpath; the legacy path leaves SNP enabled in hardware while clearingsnp_initialized, so__sev_snp_init_locked()can run again. Track the disable with a flag so it is balanced by a matching enable rather than stacked, and re-enable hotplug only on thex86_snp_shutdownpath, aftersnp_shutdown()has cleared the per-coreRMPOPT_BASEMSRs with hotplug still disabled.This also keeps the CPU set stable for the asynchronous RMPOPT scan added later in this series, and ensures
cpus_read_lock()in the scan is uncontended.Suggested-by Thomas Lendacky <[email protected]>Signed-off-by Ashish Kalra <[email protected]>--- drivers/crypto/ccp/sev-dev.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-)drivers/crypto/ccp/sev-dev.c +4
-0@@ -106,6 +106,9 @@ struct snp_hv_fixed_pages_entry {static LIST_HEAD (snp_hv_fixed_pages); +/* Set while SNP has CPU hotplug disabled. */ +static bool snp_cpu_hotplug_disabled; +/* Trusted Memory Region (TMR): * The TMR is a 1MB area that must be 1MB aligned. Use the page allocator * to allocate the memory, which will return aligned memory for the specified @@ -1479,6 +1482,17 @@ static int __sev_snp_init_locked(int *error, unsigned int max_snp_asid)snp_hv_fixed_pages_state_update (sev,HV_FIXED ); +/*
drivers/crypto/ccp/sev-dev.c static int __sev_snp_init_locked(int *error, unsigned int max_snp_asid) · line 1495
Quoted 10 lines
drivers/crypto/ccp/sev-dev.c +10
-0+* Disable CPU hotplug while SNP is active. Guard against stacking +* the disable count: the legacy SNP_SHUTDOWN_EX path clears +* snp_initialized without re-enabling hotplug, so this can run +* again while hotplug is already disabled. +*/ +if (!snp_cpu_hotplug_disabled) { +cpu_hotplug_disable (); + snp_cpu_hotplug_disabled = true; + } +
... should this be done before __sev_do_cmd_locked(SEV_CMD_SNP_INIT_EX) is issued?
I'm assuming that is when the firmware enumerates the CPUs during SNP initialization and any hotplug after that should be disallowed?
Quoted 3 lines
drivers/crypto/ccp/sev-dev.c +0
-0snp_setup_rmpopt (); sev->snp_initialized = true;
Signature · 2 lines
Thanks and Regards, Prateek
Kalra, Ashish
maintainerRe: [PATCH v8 3/7] crypto/ccp: Disable CPU hotplug while SNP is active
Hello Prateek,
Quoted 11 lines · K Prateek Nayak
Hello Ashish,
Quoted 6 lines · Ashish Kalra
From: Ashish Kalra <[email protected]>
The SEV firmware enumerates the CPUs at SNP initialization and is not aware of the OS bringing CPUs online or offline afterwards, so OS CPU hotplug can diverge from the firmware's expectations and break SNP. Disable CPU hotplug while SNP is active.
Dumb question: Is this specific to RMPOPT? Otherwise ...
The actual reason is purely about the SEV firmware: it enumerates the BIOS-enabled CPUs at SNP_INIT_EX and has no knowledge of OS hotplug afterward. That's true whether or not RMPOPT exists. RMPOPT only benefits from the side effect, which is a stable rmpopt_cpumask and an uncontended cpus_read_lock() in the work handler.
So it is specific to SNP, but RMPOPT patches that come later in the series rely on it, therefore it is a pre-patch here.
Quoted 53 lines
Quoted 39 lines
SNP is fully torn down only on the
SNP_SHUTDOWN_EXx86_snp_shutdownpath; the legacy path leaves SNP enabled in hardware while clearingsnp_initialized, so__sev_snp_init_locked()can run again. Track the disable with a flag so it is balanced by a matching enable rather than stacked, and re-enable hotplug only on thex86_snp_shutdownpath, aftersnp_shutdown()has cleared the per-coreRMPOPT_BASEMSRs with hotplug still disabled.This also keeps the CPU set stable for the asynchronous RMPOPT scan added later in this series, and ensures
cpus_read_lock()in the scan is uncontended.Suggested-by Thomas Lendacky <[email protected]>Signed-off-by Ashish Kalra <[email protected]>--- drivers/crypto/ccp/sev-dev.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-)drivers/crypto/ccp/sev-dev.c +4
-0@@ -106,6 +106,9 @@ struct snp_hv_fixed_pages_entry {static LIST_HEAD (snp_hv_fixed_pages); +/* Set while SNP has CPU hotplug disabled. */ +static bool snp_cpu_hotplug_disabled; +/* Trusted Memory Region (TMR): * The TMR is a 1MB area that must be 1MB aligned. Use the page allocator * to allocate the memory, which will return aligned memory for the specified @@ -1479,6 +1482,17 @@ static int __sev_snp_init_locked(int *error, unsigned int max_snp_asid)snp_hv_fixed_pages_state_update (sev,HV_FIXED ); +/* drivers/crypto/ccp/sev-dev.c static int __sev_snp_init_locked(int *error, unsigned int max_snp_asid) · line 1495
Quoted 10 lines
drivers/crypto/ccp/sev-dev.c +10
-0+* Disable CPU hotplug while SNP is active. Guard against stacking +* the disable count: the legacy SNP_SHUTDOWN_EX path clears +* snp_initialized without re-enabling hotplug, so this can run +* again while hotplug is already disabled. +*/ +if (!snp_cpu_hotplug_disabled) { +cpu_hotplug_disable (); + snp_cpu_hotplug_disabled = true; + } +... should this be done before
__sev_do_cmd_locked(SEV_CMD_SNP_INIT_EX)is issued?
Quoted 2 lines
I'm assuming that is when the firmware enumerates the CPUs during SNP initialization and any hotplug after that should be disallowed?
Yes, it makes sense to do it before SNP_INIT_EX is issued.
Thanks,
Ashish
Quoted 4 lines
Quoted 4 lines
drivers/crypto/ccp/sev-dev.c +0
-0snp_setup_rmpopt (); sev->snp_initialized = true;
Tom Lendacky
maintainerRe: [PATCH v8 3/7] crypto/ccp: Disable CPU hotplug while SNP is active
In reply to K Prateek NayakQuoted 64 lines · K Prateek Nayak
Hello Ashish,
Quoted 6 lines · Ashish Kalra
From: Ashish Kalra <[email protected]>
The SEV firmware enumerates the CPUs at SNP initialization and is not aware of the OS bringing CPUs online or offline afterwards, so OS CPU hotplug can diverge from the firmware's expectations and break SNP. Disable CPU hotplug while SNP is active.
Dumb question: Is this specific to RMPOPT? Otherwise ...
Quoted 38 lines
SNP is fully torn down only on the
SNP_SHUTDOWN_EXx86_snp_shutdownpath; the legacy path leaves SNP enabled in hardware while clearingsnp_initialized, so__sev_snp_init_locked()can run again. Track the disable with a flag so it is balanced by a matching enable rather than stacked, and re-enable hotplug only on thex86_snp_shutdownpath, aftersnp_shutdown()has cleared the per-coreRMPOPT_BASEMSRs with hotplug still disabled.This also keeps the CPU set stable for the asynchronous RMPOPT scan added later in this series, and ensures
cpus_read_lock()in the scan is uncontended.Suggested-by Thomas Lendacky <[email protected]>Signed-off-by Ashish Kalra <[email protected]>--- drivers/crypto/ccp/sev-dev.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-)drivers/crypto/ccp/sev-dev.c +4
-0@@ -106,6 +106,9 @@ struct snp_hv_fixed_pages_entry {static LIST_HEAD (snp_hv_fixed_pages); +/* Set while SNP has CPU hotplug disabled. */ +static bool snp_cpu_hotplug_disabled; +/* Trusted Memory Region (TMR): * The TMR is a 1MB area that must be 1MB aligned. Use the page allocator * to allocate the memory, which will return aligned memory for the specified @@ -1479,6 +1482,17 @@ static int __sev_snp_init_locked(int *error, unsigned int max_snp_asid)snp_hv_fixed_pages_state_update (sev,HV_FIXED ); +/* drivers/crypto/ccp/sev-dev.c static int __sev_snp_init_locked(int *error, unsigned int max_snp_asid) · line 1495
Quoted 10 lines
drivers/crypto/ccp/sev-dev.c +10
-0+* Disable CPU hotplug while SNP is active. Guard against stacking +* the disable count: the legacy SNP_SHUTDOWN_EX path clears +* snp_initialized without re-enabling hotplug, so this can run +* again while hotplug is already disabled. +*/ +if (!snp_cpu_hotplug_disabled) { +cpu_hotplug_disable (); + snp_cpu_hotplug_disabled = true; + } +... should this be done before
__sev_do_cmd_locked(SEV_CMD_SNP_INIT_EX)is issued?
Quoted 2 lines
I'm assuming that is when the firmware enumerates the CPUs during SNP initialization and any hotplug after that should be disallowed?
Any hotplug before would be bad, too. SEV firmware understands what CPUs are physically available based on the installed processor and BIOS/UEFI settings (e.g. disabling SMT from the BIOS), not what Linux has online at the time of SNP_INIT_EX.
So maybe the commit message needs updating about that.
Thanks,
Tom
Quoted 4 lines
Quoted 4 lines
drivers/crypto/ccp/sev-dev.c +0
-0snp_setup_rmpopt (); sev->snp_initialized = true;
Re: [PATCH v8 3/7] crypto/ccp: Disable CPU hotplug while SNP is active
In reply to Ashish Kalradrivers/crypto/ccp/sev-dev.c static int __sev_snp_init_locked(int *error, unsigned int max_snp_asid) · line 1494
Quoted 10 lines · Ashish Kalra
drivers/crypto/ccp/sev-dev.c +10
-0+/* +* Disable CPU hotplug while SNP is active. Guard against stacking +* the disable count: the legacy SNP_SHUTDOWN_EX path clears +* snp_initialized without re-enabling hotplug, so this can run +* again while hotplug is already disabled. +*/ +if (!snp_cpu_hotplug_disabled) { +cpu_hotplug_disable (); + snp_cpu_hotplug_disabled = true; + }
This seems like a hack, guys.
cpu_hotplug_disable() seems like more of a temporary lock than enforcing basically permanent system state.
This seems like it would be better implemented by registering a CPU hotplug callback and then refusing to offline if sev->snp_initialized is set.
snp_setup_rmpopt() can be run any time, right? It doesn't need to be after sev->snp_initialized=1.
Kalra, Ashish
maintainerRe: [PATCH v8 3/7] crypto/ccp: Disable CPU hotplug while SNP is active
Quoted 17 lines · Dave Hansen
drivers/crypto/ccp/sev-dev.c static int __sev_snp_init_locked(int *error, unsigned int max_snp_asid) · line 1494
Quoted 10 lines · Ashish Kalra
drivers/crypto/ccp/sev-dev.c +10
-0+/* +* Disable CPU hotplug while SNP is active. Guard against stacking +* the disable count: the legacy SNP_SHUTDOWN_EX path clears +* snp_initialized without re-enabling hotplug, so this can run +* again while hotplug is already disabled. +*/ +if (!snp_cpu_hotplug_disabled) { +cpu_hotplug_disable (); + snp_cpu_hotplug_disabled = true; + }This seems like a hack, guys.
cpu_hotplug_disable()seems like more of a temporary lock than enforcing basically permanent system state.
Quoted 6 lines
This seems like it would be better implemented by registering a CPU hotplug callback and then refusing to offline if
sev->snp_initializedis set.
snp_setup_rmpopt()can be run any time, right? It doesn't need to be after sev->snp_initialized=1.
Yes, snp_setup_rmpopt() doesn't depend on snp_initialized. Programming RMPOPT_BASE only needs the CPU online and the system rmpopt_capable.
Based on Dave's feedback, i am going to drop this cpu_hotplug_disable()/cpu_hotplug_enable() and instead implementing and registering the CPU hotplug callback and then refusing to go offline if SNP is enabled, unless anyone else here has a different thought/suggestion.
Thanks,
Ashish
Kalra, Ashish
maintainerRe: [PATCH v8 3/7] crypto/ccp: Disable CPU hotplug while SNP is active
Quoted 23 lines · Ashish
Quoted 21 lines · Dave Hansen
drivers/crypto/ccp/sev-dev.c static int __sev_snp_init_locked(int *error, unsigned int max_snp_asid) · line 1494
Quoted 10 lines · Ashish Kalra
drivers/crypto/ccp/sev-dev.c +10
-0+/* +* Disable CPU hotplug while SNP is active. Guard against stacking +* the disable count: the legacy SNP_SHUTDOWN_EX path clears +* snp_initialized without re-enabling hotplug, so this can run +* again while hotplug is already disabled. +*/ +if (!snp_cpu_hotplug_disabled) { +cpu_hotplug_disable (); + snp_cpu_hotplug_disabled = true; + }This seems like a hack, guys.
cpu_hotplug_disable()seems like more of a temporary lock than enforcing basically permanent system state.This seems like it would be better implemented by registering a CPU hotplug callback and then refusing to offline if
sev->snp_initializedis set.
Quoted 5 lines
Quoted 2 lines
snp_setup_rmpopt()can be run any time, right? It doesn't need to be after sev->snp_initialized=1.Yes,
snp_setup_rmpopt()doesn't depend onsnp_initialized. ProgrammingRMPOPT_BASEonly needs the CPU online and the systemrmpopt_capable.
After feedback from Tom, adding here that RMPOPT_BASE (RMPOPT_EN) can only be set if SNP and SegmentedRMP are enabled.
Therefore, we can only call snp_setup_rmpopt() after snp_prepare() has enabled SNP in __sev_snp_init_locked() (CCP module).
Additionally, as SNP_INIT, clears the RMPOPT table contents to 0, therefore we call it after SNP_INIT_EX.
Thanks,
Ashish
Quoted 7 lines
Based on Dave's feedback, i am going to drop this cpu_hotplug_disable()/cpu_hotplug_enable() and instead implementing and registering the CPU hotplug callback and then refusing to go offline if SNP is enabled, unless anyone else here has a different thought/suggestion.
Thanks,
Ashish
Borislav Petkov
maintainerRe: [PATCH v8 3/7] crypto/ccp: Disable CPU hotplug while SNP is active
In reply to Kalra, AshishQuoted 4 lines · Ashish
Based on Dave's feedback, i am going to drop this cpu_hotplug_disable()/cpu_hotplug_enable() and instead implementing and registering the CPU hotplug callback and then refusing to go offline if SNP is enabled, unless anyone else here has a different thought/suggestion.
What happened to using cpu_hotplug_disable_offlining() as I've been saying a bunch of times now?
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteKalra, Ashish
maintainerRe: [PATCH v8 3/7] crypto/ccp: Disable CPU hotplug while SNP is active
Hello Boris,
Quoted 9 lines · Borislav Petkov
Quoted 4 lines · Ashish
Based on Dave's feedback, i am going to drop this cpu_hotplug_disable()/cpu_hotplug_enable() and instead implementing and registering the CPU hotplug callback and then refusing to go offline if SNP is enabled, unless anyone else here has a different thought/suggestion.
What happened to using
cpu_hotplug_disable_offlining()as I've been saying a bunch of times now?
One thing about cpu_hotplug_disable_offlining() is that it is permanent and one-way (__ro_after_init).
Once SNP host RMP/SNP is enabled at boot, offlining is disabled for the entire boot — no re-enable, even if SNP is fully shut down later. In comparison, there is the possibility to re-enable CPU hotplugging during SNP shutdown path by calling cpuhp_remove_state_nocalls().
It has to invoked at boot-time, so it's tied to "RMP/SNP host enabled at boot". So on a host with SNP/RMP enabled but where SNP firmware is never initialized (KVM/SEV never used), it would still permanently disable CPU offlining — which is arguably wrong, since SNP isn't in use there.
It is otherwise a clean interface, the offline path returns -EOPNOTSUPP, distinct from an -EBUSY return via the cpuhp interface.
To summarize, using cpu_hotplug_disable_offlining() is simpler than the cpuhp interface, but the trade-offs are (a) coarser granularity (SNP enabled vs SNP initialized) and (b) no re-enable.
Thanks,
Ashish
Borislav Petkov
maintainerRe: [PATCH v8 3/7] crypto/ccp: Disable CPU hotplug while SNP is active
Quoted 4 lines · Ashish
Once SNP host RMP/SNP is enabled at boot, offlining is disabled for the entire boot — no re-enable, even if SNP is fully shut down later. In comparison, there is the possibility to re-enable CPU hotplugging during SNP shutdown path by calling
cpuhp_remove_state_nocalls().
I'd let tglx maybe give a better idea but this cpu_hotplug_disable static var in kernel/cpu.c could get a getter function and be used instead of you reinventing the wheel in here.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteKalra, Ashish
maintainerRe: [PATCH v8 3/7] crypto/ccp: Disable CPU hotplug while SNP is active
Quoted 3 lines · Borislav Petkov
I'd let tglx maybe give a better idea but this
cpu_hotplug_disablestatic var inkernel/cpu.ccould get a getter function and be used instead of you reinventing the wheel in here.
I don't follow — I'm not reinventing anything here. Patch 3 will use the existing CPU-hotplug callback interface: cpuhp_setup_state() with a down callback that returns -EBUSY to refuse the offline while SNP is active. That's the standard mechanism for conditionally preventing a CPU offline, and it keeps no private "hotplug disabled" state of its own — so there's nothing here for a getter on cpu_hotplug_disabled to replace.
I chose the cpuhp callback specifically over cpu_hotplug_disable_offlining(): the callback can be torn down with cpuhp_remove_state() when SNP is fully shut down, which re-enables CPU offlining. cpu_hotplug_disable_offlining() sets cpu_hotplug_offline_disabled, which is __ro_after_init and one-way — there's no interface to clear it, and adding one would mean dropping the __ro_after_init marking and a new core "re-enable offlining" API. So that route can't re-enable offlining on SNP shutdown without new core plumbing, whereas the cpuhp callback gives me that for free.
Happy to go whichever way you/tglx prefers — if there's a specific variable/getter you had in mind, please point me at it.
Thanks,
Ashish
Thomas Gleixner
maintainerRe: [PATCH v8 3/7] crypto/ccp: Disable CPU hotplug while SNP is active
Quoted 5 lines · Ashish Kalra
Quoted 3 lines · Borislav Petkov
I'd let tglx maybe give a better idea but this
cpu_hotplug_disablestatic var inkernel/cpu.ccould get a getter function and be used instead of you reinventing the wheel in here.
Quoted 7 lines
I don't follow — I'm not reinventing anything here. Patch 3 will use the existing CPU-hotplug callback interface:
cpuhp_setup_state()with a down callback that returns-EBUSYto refuse the offline while SNP is active. That's the standard mechanism for conditionally preventing a CPU offline, and it keeps no private "hotplug disabled" state of its own — so there's nothing here for a getter oncpu_hotplug_disabledto replace.
That's not a standard mechanism. That's a hack as you have to start the offlining operation in order to prevent something you already know.
The return code which prevents offlining is there for situations where the subsystem/driver is momentarily in a state which cannot be resolved.
That's a very different story than knowing that state at the point of installing the callback already.
Quoted 9 lines
I chose the cpuhp callback specifically over
cpu_hotplug_disable_offlining(): the callback can be torn down withcpuhp_remove_state()when SNP is fully shut down, which re-enables CPU offlining.cpu_hotplug_disable_offlining()setscpu_hotplug_offline_disabled, which is__ro_after_initand one-way — there's no interface to clear it, and adding one would mean dropping the__ro_after_initmarking and a new core "re-enable offlining" API. So that route can't re-enable offlining on SNP shutdown without new core plumbing, whereas the cpuhp callback gives me that for free.
That's exactly the wrong attitude. Hack around a core limitation in a random driver by abusing the provided mechanism instead of sitting down and doing the extra five lines of code which makes it entirely clear what's going on.
When Boris asked me how to disable hotplug, I had the impression that this is about permanently preventing it. So I pointed him to cpu_hotplug_disable_offlining().
If I had known that it's about temporary prevention during runtime of something, then I'd pointed him to cpu_hotplug_disable()/enable() which is five lines farther down in cpu.c. It's not rocket science to find them. The first AI chatbot I asked pointed me to it immediately.
cpu_hotplug_disable()/enable() is _the_ standard mechanism to prevent hotplug operations temporarily. They return -EBUSY without invoking any callback or changing any related state.
So what's exactly the new core plumbing you need?
Thanks,
tglx
Kalra, Ashish
maintainerRe: [PATCH v8 3/7] crypto/ccp: Disable CPU hotplug while SNP is active
Quoted 17 lines · Thomas Gleixner
Quoted 5 lines · Ashish Kalra
Quoted 3 lines · Borislav Petkov
I'd let tglx maybe give a better idea but this
cpu_hotplug_disablestatic var inkernel/cpu.ccould get a getter function and be used instead of you reinventing the wheel in here.Quoted 7 lines
I don't follow — I'm not reinventing anything here. Patch 3 will use the existing CPU-hotplug callback interface:
cpuhp_setup_state()with a down callback that returns-EBUSYto refuse the offline while SNP is active. That's the standard mechanism for conditionally preventing a CPU offline, and it keeps no private "hotplug disabled" state of its own — so there's nothing here for a getter oncpu_hotplug_disabledto replace.That's not a standard mechanism. That's a hack as you have to start the offlining operation in order to prevent something you already know.
Quoted 7 lines
The return code which prevents offlining is there for situations where the subsystem/driver is momentarily in a state which cannot be resolved.
That's a very different story than knowing that state at the point of installing the callback already.
Sure.
Quoted 19 lines
Quoted 9 lines
I chose the cpuhp callback specifically over
cpu_hotplug_disable_offlining(): the callback can be torn down withcpuhp_remove_state()when SNP is fully shut down, which re-enables CPU offlining.cpu_hotplug_disable_offlining()setscpu_hotplug_offline_disabled, which is__ro_after_initand one-way — there's no interface to clear it, and adding one would mean dropping the__ro_after_initmarking and a new core "re-enable offlining" API. So that route can't re-enable offlining on SNP shutdown without new core plumbing, whereas the cpuhp callback gives me that for free.That's exactly the wrong attitude. Hack around a core limitation in a random driver by abusing the provided mechanism instead of sitting down and doing the extra five lines of code which makes it entirely clear what's going on.
When Boris asked me how to disable hotplug, I had the impression that this is about permanently preventing it. So I pointed him to
cpu_hotplug_disable_offlining().
Quoted 9 lines
If I had known that it's about temporary prevention during runtime of something, then I'd pointed him to cpu_hotplug_disable()/enable() which is five lines farther down in
cpu.c. It's not rocket science to find them. The first AI chatbot I asked pointed me to it immediately.cpu_hotplug_disable()/enable() is _the_ standard mechanism to prevent hotplug operations temporarily. They return
-EBUSYwithout invoking any callback or changing any related state.
This is the interface i have been using, in fact this current patch (v8) is based on cpu_hotplug_disable()/enable(), but then this thread started from review feedback on the v8 patch using cpu_hotplug_disable()/enable() that it looks like a hack — the concern being that cpu_hotplug_disable() reads as a temporary lock rather than a way to enforce a basically-permanent system state.
That's what led me to look at cpu_hotplug_disable_offlining() and a cpuhp down-callback as alternatives.
Your point that cpu_hotplug_disable()/enable() is the standard mechanism to prevent hotplug operations temporarily settles it, and the disable/enable pair being reversible is exactly what's wanted here: it's undone when SNP is fully shut down, so it isn't actually permanent (unlike cpu_hotplug_disable_offlining(), which is one-way). So I'll stay with cpu_hotplug_disable()/enable() and drop the alternatives.
Thanks,
Ashish
Quoted 6 lines
So what's exactly the new core plumbing you need?
Thanks,
tglx
Re: [PATCH v8 3/7] crypto/ccp: Disable CPU hotplug while SNP is active
In reply to Ashish KalraQuoted 6 lines · Ashish Kalra
From: Ashish Kalra <[email protected]>
The SEV firmware enumerates the CPUs at SNP initialization and is not aware of the OS bringing CPUs online or offline afterwards, so OS CPU hotplug can diverge from the firmware's expectations and break SNP. Disable CPU hotplug while SNP is active.
I think this is too broad. If I have a hypervisor that supports SNP virtualization, a (non-confidential) L1 guest running Linux should still support CPU hotplug while also running confidential L2 guests.
--
Jethro Beekman | CTO | Fortanix
Quoted 74 lines
SNP is fully torn down only on the
SNP_SHUTDOWN_EXx86_snp_shutdownpath; the legacy path leaves SNP enabled in hardware while clearingsnp_initialized, so__sev_snp_init_locked()can run again. Track the disable with a flag so it is balanced by a matching enable rather than stacked, and re-enable hotplug only on thex86_snp_shutdownpath, aftersnp_shutdown()has cleared the per-coreRMPOPT_BASEMSRs with hotplug still disabled.This also keeps the CPU set stable for the asynchronous RMPOPT scan added later in this series, and ensures
cpus_read_lock()in the scan is uncontended.Suggested-by Thomas Lendacky <[email protected]>Signed-off-by Ashish Kalra <[email protected]>--- drivers/crypto/ccp/sev-dev.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-)drivers/crypto/ccp/sev-dev.c +28
-1@@ -106,6 +106,9 @@ struct snp_hv_fixed_pages_entry {static LIST_HEAD (snp_hv_fixed_pages); +/* Set while SNP has CPU hotplug disabled. */ +static bool snp_cpu_hotplug_disabled; +/* Trusted Memory Region (TMR): * The TMR is a 1MB area that must be 1MB aligned. Use the page allocator * to allocate the memory, which will return aligned memory for the specified @@ -1479,6 +1482,17 @@ static int __sev_snp_init_locked(int *error, unsigned int max_snp_asid)snp_hv_fixed_pages_state_update (sev,HV_FIXED ); +/* +* Disable CPU hotplug while SNP is active. Guard against stacking +* the disable count: the legacy SNP_SHUTDOWN_EX path clears +* snp_initialized without re-enabling hotplug, so this can run +* again while hotplug is already disabled. +*/ +if (!snp_cpu_hotplug_disabled) { +cpu_hotplug_disable (); + snp_cpu_hotplug_disabled = true; + } +snp_setup_rmpopt (); sev->snp_initialized = true;@@ -2083,8 +2097,21 @@ static int __sev_snp_shutdown_locked(int *error, bool panic) }if (data.x86_snp_shutdown) { -if (!panic) +if (!panic) {snp_shutdown (); +/* +* snp_shutdown() fully tears SNP down (clear_rmp()) and +* has already cleared the per-core RMPOPT_BASE MSRs via +* rmpopt_cleanup() with hotplug still disabled. Re-enable +* CPU hotplug now. On the legacy path SNP stays +* enabled in hardware, so hotplug is correctly left +* disabled. +*/ +if (snp_cpu_hotplug_disabled) { +cpu_hotplug_enable (); + snp_cpu_hotplug_disabled = false; + } + }snp_hv_fixed_pages_state_update (sev,ALLOCATED ); }else {/*
Re: [PATCH v8 3/7] crypto/ccp: Disable CPU hotplug while SNP is active
Quoted 8 lines · Jethro Beekman
Quoted 6 lines · Ashish Kalra
From: Ashish Kalra <[email protected]>
The SEV firmware enumerates the CPUs at SNP initialization and is not aware of the OS bringing CPUs online or offline afterwards, so OS CPU hotplug can diverge from the firmware's expectations and break SNP. Disable CPU hotplug while SNP is active.
Quoted 5 lines
I think this is too broad. If I have a hypervisor that supports SNP virtualization, a (non-confidential) L1 guest running Linux should still support CPU hotplug while also running confidential L2 guests.
--
Jethro Beekman | CTO | Fortanix
Were any other solutions considered other than disabling CPU hotplug?
Is this temporary until something else is implemented?
I'm not sure how commonly CPU hotplug is used, and if people are okay with trading in CPU hotplug to get SNP.
Is it that fundamentally the SEV firmware can't support hotplug, so there's no point in keeping it enabled anyway?
Is there some way of supporting hotplug for CPUs that won't be used with SNP, for serving non-SNP VMs on the same host as SNP VMs, or is that too complicated?
Quoted 3 lines
Quoted 3 lines
[...snip...]
Kalra, Ashish
maintainerRe: [PATCH v8 3/7] crypto/ccp: Disable CPU hotplug while SNP is active
Hello Ackerley,
Quoted 18 lines · Ackerley Tng
Quoted 8 lines · Jethro Beekman
Quoted 6 lines · Ashish Kalra
From: Ashish Kalra <[email protected]>
The SEV firmware enumerates the CPUs at SNP initialization and is not aware of the OS bringing CPUs online or offline afterwards, so OS CPU hotplug can diverge from the firmware's expectations and break SNP. Disable CPU hotplug while SNP is active.
Quoted 5 lines
I think this is too broad. If I have a hypervisor that supports SNP virtualization, a (non-confidential) L1 guest running Linux should still support CPU hotplug while also running confidential L2 guests.
--
Jethro Beekman | CTO | FortanixWere any other solutions considered other than disabling CPU hotplug?
Quoted 7 lines
Is this temporary until something else is implemented?
I'm not sure how commonly CPU hotplug is used, and if people are okay with trading in CPU hotplug to get SNP.
Is it that fundamentally the SEV firmware can't support hotplug, so there's no point in keeping it enabled anyway?
Yes, essentially. The SEV firmware knows nothing about when the OS takes CPUs online or offline. At SNP_INIT it accounts for all the CPUs enabled via the BIOS/UEFI and establishes the per-core SNP state for them; it has no notion of the OS bringing CPUs up or down afterwards. So OS hotplug actions can diverge from the firmware's expectations and break SNP. Disabling hotplug just makes that constraint explicit — there's nothing useful to keep it enabled for: a hot-removed core still "exists" as far as the firmware and the per-core RMP/RMPOPT state are concerned, and a core brought online later was never set up for SNP.
Quoted 5 lines
Is there some way of supporting hotplug for CPUs that won't be used with SNP, for serving non-SNP VMs on the same host as SNP VMs, or is that too complicated?
Not really. SNP's memory-integrity guarantee rests on a single invariant: every memory write is subject to RMP checks to protect against corruption of SEV-SNP guest memory. The moment any CPU can issue writes that aren't RMP-checked, that protection is broken for the whole system — it's not something that can be confined to "that one core."
That's because SNP isn't per-core in that sense — it's a system-wide mode. SYSCFG[SNP_EN] is set on every core, the RMP covers all of physical memory, and once SNP is enabled every memory write is subject to RMP checks on every core. A non-SNP guest sharing the host still runs on cores that are part of the SNP-enabled system.
By the SNP architecture there simply can't be a CPU that isn't doing RMP checks while SNP is active, so SNP_EN has to be enforced on every core. RMP enforcement is gated per-core by SYSCFG[SNP_EN] and it must be set on every core before SNP_INIT; a core with SNP_EN clear performs no RMP checks at all, which the architecture doesn't allow once SNP is up. A newly hotplugged CPU comes up without SNP_EN (SNP not enabled on it), and since it wasn't present when SNP_INIT ran it isn't part of the initialized SNP configuration either — so it does no RMP checking. And because an SNP guest's vCPUs (or any guest for that matter) can be scheduled on any online CPU, the guest could end up running on that core, accessing memory with no RMP enforcement and breaking SNP's memory integrity. There's no way to prevent that: KVM doesn't fence SNP guests (or any guests for that matter) off particular online cores. And carving out cores for non-SNP-only use isn't possible by the architecture: SNP requires RMP checks on every CPU, so there's no valid configuration with SNP active and a subset of cores exempt.
Thanks,
Ashish
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
[...snip...]
Ashish Kalra
maintainerPATCH v8 4/7 x86/sev: Add support to perform RMP optimizations asynchronously
In reply to a message not in the archiveFrom: Ashish Kalra <[email protected]>
When SEV-SNP is enabled, all writes to memory are checked to ensure integrity of SNP guest memory. This imposes performance overhead on the whole system.
RMPOPT is a new instruction that minimizes the performance overhead of RMP checks on the hypervisor and on non-SNP guests by allowing RMP checks to be skipped for 1GB regions of memory that are known not to contain any SEV-SNP guest memory.
Add support for performing RMP optimizations asynchronously using a dedicated workqueue.
Enable RMPOPT optimizations for up to 2TB of system RAM starting from the lowest physical memory address aligned down to a 1GB boundary at RMP initialization time. RMP checks can initially be skipped for 1GB memory ranges that do not contain SEV-SNP guest memory (excluding preassigned pages such as the RMP table and firmware pages). As SNP guests are launched, RMPUPDATE will disable the corresponding RMPOPT optimizations.
--- arch/x86/virt/svm/sev.c | 230 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 227 insertions(+), 3 deletions(-)
arch/x86/virt/svm/sev.c +227 -3
@@ -19,6 +19,7 @@#include <linux/iommu.h> #include <linux/amd-iommu.h> #include <linux/nospec.h> +#include <linux/workqueue.h> #include <asm/sev.h> #include <asm/processor.h>
@@ -125,9 +126,20 @@ static void *rmp_bookkeeping __ro_after_init;static u64 probed_rmp_base, probed_rmp_size;static cpumask_t rmpopt_cpumask; -static phys_addr_t rmpopt_pa_start; +static phys_addr_t rmpopt_pa_start, rmpopt_pa_end;static bool rmpopt_configured; +enum rmpopt_function { +RMPOPT_FUNC_VERIFY_AND_REPORT_STATUS , +RMPOPT_FUNC_REPORT_STATUS +}; + +#define RMPOPT_WORK_TIMEOUT 10000 + +static struct workqueue_struct *rmpopt_wq; +static struct delayed_work rmpopt_delayed_work; +static DEFINE_MUTEX (rmpopt_wq_mutex); +static LIST_HEAD (snp_leaked_pages_list);static DEFINE_SPINLOCK (snp_leaked_pages_list_lock);
@@ -568,6 +580,14 @@ static void rmpopt_cleanup(void) {int cpu; +guard (mutex)(&rmpopt_wq_mutex); + +if (!rmpopt_wq) +return ; + +cancel_delayed_work_sync (&rmpopt_delayed_work); +destroy_workqueue (rmpopt_wq); +cpus_read_lock ();for_each_cpu (cpu, &rmpopt_cpumask)
@@ -576,7 +596,8 @@ static void rmpopt_cleanup(void)cpus_read_unlock ();cpumask_clear (&rmpopt_cpumask); - rmpopt_pa_start =0 ; + rmpopt_pa_start = rmpopt_pa_end =0 ; + rmpopt_wq =NULL ; }void snp_shutdown (void )
@@ -599,6 +620,168 @@ void snp_clear_rmpopt_configured(void) rmpopt_configured = false; } +/* +* RMPOPT: F2 0F 01 FC +* Input: RAX = system physical address (1GB aligned) +* RCX = operation type +* Output: CF set if the range was optimized +*/ +static inline bool __rmpopt (u64 pa_start,u64 op_type) +{ +bool optimized; + + asmvolatile (".byte 0xf2, 0x0f, 0x01, 0xfc" + :"=@ccc" (optimized) + :"a" (pa_start),"c" (op_type) + :"memory" ,"cc" ); + +return optimized; +} + +static void rmpopt (u64 pa) +{ +u64 pa_start =ALIGN_DOWN (pa,SZ_1G ); +u64 op_type =RMPOPT_FUNC_VERIFY_AND_REPORT_STATUS ; + +__rmpopt (pa_start, op_type); +} + +/* +* 'val' is a system physical address. +*/ +static void rmpopt_smp (void *val) +{ +rmpopt ((u64 )val); +} + +/* +* Leader function for work_on_cpu(): runs the full RMPOPT scan in +* process context on a CPU that has RMPOPT_BASE MSR programmed. +*/ +static long rmpopt_leader_fn (void *arg) +{ +phys_addr_t pa; + +for (pa = rmpopt_pa_start; pa < rmpopt_pa_end; pa +=SZ_1G ) { +rmpopt (pa); +cond_resched (); + } +return 0 ; +} + +/* +* RMPOPT optimizations skip RMP checks at 1GB granularity if this +* range of memory does not contain any SNP guest memory. +*/ +static void rmpopt_work_handler (struct work_struct *work) +{ +cpumask_var_t follower_mask; +phys_addr_t pa; +int this_cpu; + +pr_info ("Attempt RMP optimizations on physical address range @1GB alignment [0x%016llx - 0x%016llx]\n" , + rmpopt_pa_start, rmpopt_pa_end); + +if (!alloc_cpumask_var (&follower_mask,GFP_KERNEL )) +return ; + +/* +* RMPOPT scans the RMP table, stores the result of the scan in the +* reserved processor memory. The RMP scan is the most expensive +* part. If a second RMPOPT occurs, it can skip the expensive scan +* if they can see a cached result in the reserved processor memory. +* +* Do RMPOPT on one CPU alone. Then, follow that up with RMPOPT +* on every other primary thread. Followers are "designed to" +* skip the scan if they see the "cached" scan results. +*/ +cpumask_copy (follower_mask, &rmpopt_cpumask); + +/* +* Pin the worker to the current CPU for the leader loop so that +* this_cpu remains valid and the RMPOPT instruction executes on +* the correct CPU. +* +* Use migrate_disable() rather than get_cpu() to prevent +* migration while still allowing preemption. +*/ +migrate_disable (); + this_cpu =smp_processor_id (); + +if (cpumask_test_cpu (this_cpu, follower_mask)) { +/* +* Current CPU is a primary thread in rmpopt_cpumask. +* Run leader locally and remove from follower mask. +*/ +cpumask_clear_cpu (this_cpu, follower_mask); + +for (pa = rmpopt_pa_start; pa < rmpopt_pa_end; pa +=SZ_1G ) { +rmpopt (pa); +cond_resched (); + } + }else if (cpumask_intersects (topology_sibling_cpumask (this_cpu), + follower_mask)) { +/* +* Current CPU is a sibling thread whose primary is in +* rmpopt_cpumask. RMPOPT_BASE MSR is per-core, so it +* is safe to run the leader locally. Remove the sibling's +* primary from the follower mask as this core is already +* covered by the leader. +*/ +cpumask_andnot (follower_mask, follower_mask, +topology_sibling_cpumask (this_cpu)); + +for (pa = rmpopt_pa_start; pa < rmpopt_pa_end; pa +=SZ_1G ) { +rmpopt (pa); +cond_resched (); + } + }else { +/* +* Current CPU does not have RMPOPT_BASE MSR programmed. +* Pick an explicit leader from the cpumask to avoid #UD. +* Use work_on_cpu() to run in process context on the leader, +* avoiding IPI latency. +*/ +int leader_cpu =cpumask_first (follower_mask); + +if (WARN_ON_ONCE (leader_cpu >= nr_cpu_ids)) { +migrate_enable (); + goto out; + } + +cpumask_clear_cpu (leader_cpu, follower_mask); + +/* Release migration pin before work_on_cpu(). */ +migrate_enable (); + +work_on_cpu (leader_cpu, rmpopt_leader_fn,NULL ); + + goto followers; + } + +migrate_enable (); + +followers: +/* +* Followers: run RMPOPT on remaining cores. +* CPU hotplug is disabled while SNP is active +* (cpu_hotplug_disable() in __sev_snp_init_locked()), +* so cpus_read_lock() is uncontended. +*/ +cpus_read_lock (); +for (pa = rmpopt_pa_start; pa < rmpopt_pa_end; pa +=SZ_1G ) { +on_each_cpu_mask (follower_mask, rmpopt_smp, + (void *)pa, true); + +/* Give a chance for other threads to run */ +cond_resched (); + } +cpus_read_unlock (); + +out: +free_cpumask_var (follower_mask); +} +void snp_setup_rmpopt (void ) {u64 rmpopt_base;
@@ -607,11 +790,37 @@ void snp_setup_rmpopt(void)if (!cpu_feature_enabled (X86_FEATURE_RMPOPT ) || !rmpopt_configured)return ; +guard (mutex)(&rmpopt_wq_mutex); + +/* +* Guard against re-initialization. When SNP_SHUTDOWN_EX is issued +* with x86_snp_shutdown=0, snp_shutdown() is not called and +* rmpopt_cleanup() is skipped, but snp_initialized is still cleared. +* A subsequent __sev_snp_init_locked() would call snp_setup_rmpopt() +* again, leaking the existing workqueue, delayed work, debugfs +* entries, and cpumask state. +*/ +if (rmpopt_wq) +return ; + +/* +* Create an RMPOPT-specific workqueue to avoid scheduling +* RMPOPT workitem on the global system workqueue. +*/ + rmpopt_wq =alloc_workqueue ("rmpopt_wq" ,WQ_UNBOUND ,1 ); +if (!rmpopt_wq) { +pr_err ("Failed to allocate RMPOPT workqueue\n" ); +return ; + } + +INIT_DELAYED_WORK (&rmpopt_delayed_work, rmpopt_work_handler); +cpus_read_lock ();/* * The RMPOPT_BASE MSR is per-core, so only one thread per core needs -* to set up the RMPOPT_BASE MSR. +* to set up the RMPOPT_BASE MSR. Likewise, only one thread per core +* needs to issue the RMPOPT instruction. * * Note: only online primary threads are included. If a core's * primary thread is offline, that core is not covered. CPU hotplug
@@ -635,6 +844,21 @@ void snp_setup_rmpopt(void)WARN_ON_ONCE (wrmsrq_on_cpu (cpu,MSR_AMD64_RMPOPT_BASE , rmpopt_base));cpus_read_unlock (); + + rmpopt_pa_end =ALIGN (PFN_PHYS (max_pfn),SZ_1G ); + +/* Limit memory scanning to 2TB of RAM */ +if ((rmpopt_pa_end - rmpopt_pa_start) >SZ_2T ) { +pr_info ("RMPOPT coverage limited to 2TB; memory above 0x%llx not optimized\n" , + rmpopt_pa_start +SZ_2T ); + rmpopt_pa_end = rmpopt_pa_start +SZ_2T ; + } + +/* +* Once all per-CPU RMPOPT tables have been configured, enable RMPOPT +* optimizations on all physical memory. +*/ +queue_delayed_work (rmpopt_wq, &rmpopt_delayed_work,0 ); }EXPORT_SYMBOL_FOR_MODULES (snp_setup_rmpopt,"ccp" );
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v8 4/7] x86/sev: Add support to perform RMP optimizations asynchronously
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Unsynchronized MSR teardown causes #UD panic when reading RMPOPT debugfs file. --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Unsynchronized MSR teardown causes #UD panic when reading RMPOPT debugfs file. --
commit f092a06fad8eb0834da2fe3bbc80d3b029232484
Author: Ashish Kalra <[email protected]>
x86/sev: Add support to perform RMP optimizations asynchronously
When SEV-SNP is enabled, all writes to memory are checked to ensure integrity of SNP guest memory. This imposes performance overhead on the whole system. This patch adds support for performing RMP optimizations asynchronously using a dedicated workqueue.
Quoted 4 lines
arch/x86/virt/svm/sev.c +0
-0
[ ... ]
Quoted 16 lines
arch/x86/virt/svm/sev.c +8
-0@@ -568,6 +580,14 @@ static void rmpopt_cleanup(void) {int cpu; +guard (mutex)(&rmpopt_wq_mutex); + +if (!rmpopt_wq) +return ; + +cancel_delayed_work_sync (&rmpopt_delayed_work); +destroy_workqueue (rmpopt_wq); +cpus_read_lock ();for_each_cpu (cpu, &rmpopt_cpumask)
arch/x86/virt/svm/sev.c static void rmpopt_cleanup(void) · line 601
Quoted 7 lines
arch/x86/virt/svm/sev.c +2
-1@@ -576,7 +596,8 @@ static void rmpopt_cleanup(void)cpus_read_unlock ();cpumask_clear (&rmpopt_cpumask); - rmpopt_pa_start =0 ; + rmpopt_pa_start = rmpopt_pa_end =0 ; + rmpopt_wq =NULL ; }
[Severity: High]
Could this MSR teardown cause a #UD panic if it races with the debugfs interface introduced later in this series?
rmpopt_table_seq_show() unconditionally executes the RMPOPT instruction on all online CPUs to display their optimization status, protected only by rmpopt_show_mutex.
If system operations like module unload or kexec trigger snp_shutdown(), it invokes rmpopt_cleanup() which tears down the feature by writing 0 to the MSR_AMD64_RMPOPT_BASE MSR on all CPUs:
arch/x86/virt/svm/sev.c:rmpopt_cleanup() {
...
for_each_cpu(cpu, &rmpopt_cpumask)
WARN_ON_ONCE(wrmsrq_on_cpu(cpu, MSR_AMD64_RMPOPT_BASE, 0));
...
}However, rmpopt_cleanup() does not acquire rmpopt_show_mutex, and the call to debugfs_remove_recursive() does not block or abort in-progress read() operations.
If rmpopt_cleanup() zeroes the MSR while rmpopt_table_seq_show() is still executing on_each_cpu_mask(), could any CPU executing RMPOPT with the MSR disabled generate an unhandled #UD (Invalid Opcode) exception?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Re: [PATCH v8 4/7] x86/sev: Add support to perform RMP optimizations asynchronously
In reply to Ashish KalraHello Ashish,
arch/x86/virt/svm/sev.c void snp_clear_rmpopt_configured(void) · line 698
Quoted 11 lines · Ashish Kalra
arch/x86/virt/svm/sev.c +11
-0+/* +* RMPOPT scans the RMP table, stores the result of the scan in the +* reserved processor memory. The RMP scan is the most expensive +* part. If a second RMPOPT occurs, it can skip the expensive scan +* if they can see a cached result in the reserved processor memory. +* +* Do RMPOPT on one CPU alone. Then, follow that up with RMPOPT +* on every other primary thread. Followers are "designed to" +* skip the scan if they see the "cached" scan results. +*/ +cpumask_copy (follower_mask, &rmpopt_cpumask);
rmpopt_cpumask is constructed after hotplug is disabled but ...
Quoted 36 lines
arch/x86/virt/svm/sev.c +36
-0+ +/* +* Pin the worker to the current CPU for the leader loop so that +* this_cpu remains valid and the RMPOPT instruction executes on +* the correct CPU. +* +* Use migrate_disable() rather than get_cpu() to prevent +* migration while still allowing preemption. +*/ +migrate_disable (); + this_cpu =smp_processor_id (); + +if (cpumask_test_cpu (this_cpu, follower_mask)) { +/* +* Current CPU is a primary thread in rmpopt_cpumask. +* Run leader locally and remove from follower mask. +*/ +cpumask_clear_cpu (this_cpu, follower_mask); + +for (pa = rmpopt_pa_start; pa < rmpopt_pa_end; pa +=SZ_1G ) { +rmpopt (pa); +cond_resched (); + } + }else if (cpumask_intersects (topology_sibling_cpumask (this_cpu), + follower_mask)) { +/* +* Current CPU is a sibling thread whose primary is in +* rmpopt_cpumask. RMPOPT_BASE MSR is per-core, so it +* is safe to run the leader locally. Remove the sibling's +* primary from the follower mask as this core is already +* covered by the leader. +*/ +cpumask_andnot (follower_mask, follower_mask, +topology_sibling_cpumask (this_cpu)); + +for (pa = rmpopt_pa_start; pa < rmpopt_pa_end; pa +=SZ_1G ) {
arch/x86/virt/svm/sev.c void snp_clear_rmpopt_configured(void) · line 744
Quoted 10 lines
arch/x86/virt/svm/sev.c +10
-0+rmpopt (pa); +cond_resched (); + } + }else { +/* +* Current CPU does not have RMPOPT_BASE MSR programmed. +* Pick an explicit leader from the cpumask to avoid #UD. +* Use work_on_cpu() to run in process context on the leader, +* avoiding IPI latency. +*/
... this_cpu is neither in the "rmpopt_cpumask", nor is any of its siblings on "rmpopt_cpumask".
How does that happen?
arch/x86/virt/svm/sev.c void snp_clear_rmpopt_configured(void) · line 757
Quoted 13 lines
arch/x86/virt/svm/sev.c +13
-0+int leader_cpu =cpumask_first (follower_mask); + +if (WARN_ON_ONCE (leader_cpu >= nr_cpu_ids)) { +migrate_enable (); + goto out; + } + +cpumask_clear_cpu (leader_cpu, follower_mask); + +/* Release migration pin before work_on_cpu(). */ +migrate_enable (); + +work_on_cpu (leader_cpu, rmpopt_leader_fn,NULL );
This creates a delayed work and also waits for it to finish execution which will add more latency than a simple IPI if the comment about IPI latency above is accurate.
I think there is some corner case in construction of the "rmpopt_cpumask" that requires this not-so-pretty else block. Can you elaborate why this is required?
Perhaps the "rmpopt_cpumask" construction needs:
for_each_online_cpu(cpu) {
/* Nominate the first CPU on the sibling mask for RMPOPT */
if (cpu != cpumask_first(topology_sibling_cpumask(cpu)))
continue;
cpumask_set_cpu(cpu, &rmpopt_cpumask);
}and all you need here is:
/* Do RMPOPt for local core */
for (pa = rmpopt_pa_start; pa < rmpopt_pa_end; pa += SZ_1G)
rmpopt(pa);
/* Skip this core from concurrent RMPOPT */
cpumask_and_not(follower_mask, &rmpopt_cpumask, topology_sibling_cpumask(cpu));No?
Quoted 6 lines
arch/x86/virt/svm/sev.c +6
-0+ + goto followers; + } + +migrate_enable (); +
Signature · 2 lines
Thanks and Regards, Prateek
Kalra, Ashish
maintainerRe: [PATCH v8 4/7] x86/sev: Add support to perform RMP optimizations asynchronously
Hello Prateek,
Quoted 67 lines · K Prateek Nayak
Hello Ashish,
arch/x86/virt/svm/sev.c void snp_clear_rmpopt_configured(void) · line 698
Quoted 11 lines · Ashish Kalra
arch/x86/virt/svm/sev.c +11
-0+/* +* RMPOPT scans the RMP table, stores the result of the scan in the +* reserved processor memory. The RMP scan is the most expensive +* part. If a second RMPOPT occurs, it can skip the expensive scan +* if they can see a cached result in the reserved processor memory. +* +* Do RMPOPT on one CPU alone. Then, follow that up with RMPOPT +* on every other primary thread. Followers are "designed to" +* skip the scan if they see the "cached" scan results. +*/ +cpumask_copy (follower_mask, &rmpopt_cpumask);
rmpopt_cpumaskis constructed after hotplug is disabled but ...Quoted 36 lines
arch/x86/virt/svm/sev.c +36
-0+ +/* +* Pin the worker to the current CPU for the leader loop so that +* this_cpu remains valid and the RMPOPT instruction executes on +* the correct CPU. +* +* Use migrate_disable() rather than get_cpu() to prevent +* migration while still allowing preemption. +*/ +migrate_disable (); + this_cpu =smp_processor_id (); + +if (cpumask_test_cpu (this_cpu, follower_mask)) { +/* +* Current CPU is a primary thread in rmpopt_cpumask. +* Run leader locally and remove from follower mask. +*/ +cpumask_clear_cpu (this_cpu, follower_mask); + +for (pa = rmpopt_pa_start; pa < rmpopt_pa_end; pa +=SZ_1G ) { +rmpopt (pa); +cond_resched (); + } + }else if (cpumask_intersects (topology_sibling_cpumask (this_cpu), + follower_mask)) { +/* +* Current CPU is a sibling thread whose primary is in +* rmpopt_cpumask. RMPOPT_BASE MSR is per-core, so it +* is safe to run the leader locally. Remove the sibling's +* primary from the follower mask as this core is already +* covered by the leader. +*/ +cpumask_andnot (follower_mask, follower_mask, +topology_sibling_cpumask (this_cpu)); + +for (pa = rmpopt_pa_start; pa < rmpopt_pa_end; pa +=SZ_1G ) {arch/x86/virt/svm/sev.c void snp_clear_rmpopt_configured(void) · line 744
Quoted 10 lines
arch/x86/virt/svm/sev.c +10
-0+rmpopt (pa); +cond_resched (); + } + }else { +/* +* Current CPU does not have RMPOPT_BASE MSR programmed. +* Pick an explicit leader from the cpumask to avoid #UD. +* Use work_on_cpu() to run in process context on the leader, +* avoiding IPI latency. +*/ ...
this_cpuis neither in the "rmpopt_cpumask", nor is any of its siblings on "rmpopt_cpumask".
Quoted 1 line
How does that happen?
Actually, this was the implementation before the CPU hotplug disable enforcement code was implemented and added in v8, and i should have fixed this rmpopt_work_handler() accordingly for v8.
With the enforced cpu hotplug disable support, case #3 here (above) is now dead code, and removing it lets cases #1 and #2 collapse too.
snp_prepare() requires cpu_online_mask == cpu_present_mask before SNP init — so when snp_setup_rmpopt() programs the MSRs, every core's primary is online -> every core is in rmpopt_cpumask.
So now the work handler always runs on a CPU whose core is programmed. topology_sibling_cpumask(this_cpu) therefore always intersects rmpopt_cpumask -> case #1 or #2 always matches.
So i should actually drop case #3 here - which is: "this_cpu is neither in the "rmpopt_cpumask", nor is any of its siblings on rmpopt_cpumask"
Quoted 35 lines
Quoted 4 lines
arch/x86/virt/svm/sev.c +3
-0+int leader_cpu =cpumask_first (follower_mask); + +if (WARN_ON_ONCE (leader_cpu >= nr_cpu_ids)) {arch/x86/virt/svm/sev.c void snp_clear_rmpopt_configured(void) · line 757
Quoted 10 lines
arch/x86/virt/svm/sev.c +10
-0+migrate_enable (); + goto out; + } + +cpumask_clear_cpu (leader_cpu, follower_mask); + +/* Release migration pin before work_on_cpu(). */ +migrate_enable (); + +work_on_cpu (leader_cpu, rmpopt_leader_fn,NULL );This creates a delayed work and also waits for it to finish execution which will add more latency than a simple IPI if the comment about IPI latency above is accurate.
I think there is some corner case in construction of the "
rmpopt_cpumask" that requires this not-so-pretty else block. Can you elaborate why this is required?Perhaps the "
rmpopt_cpumask" construction needs:for_each_online_cpu(cpu) { /* Nominate the first CPU on the sibling mask for RMPOPT */ if (cpu != cpumask_first(topology_sibling_cpumask(cpu))) continue; cpumask_set_cpu(cpu, &rmpopt_cpumask); }and all you need here is:
Quoted 9 lines
/* Do RMPOPt for local core */ for (pa = rmpopt_pa_start; pa < rmpopt_pa_end; pa += SZ_1G) rmpopt(pa); /* Skip this core from concurrent RMPOPT */ cpumask_and_not(follower_mask, &rmpopt_cpumask, topology_sibling_cpumask(cpu));No?
Yes, a simpler implementation will be like this:
...
if (!alloc_cpumask_var(&follower_mask, GFP_KERNEL))
return;
cpumask_copy(follower_mask, &rmpopt_cpumask);
/*
* The current CPU's core always has RMPOPT_BASE programmed
* (snp_prepare() required all CPUs online at setup and CPU hotplug
* is disabled while SNP is active), so it can always be the leader.
* RMPOPT_BASE is per-core; exclude this core from the followers.
*/
migrate_disable();
cpumask_andnot(follower_mask, follower_mask,
topology_sibling_cpumask(smp_processor_id()));
for (pa = rmpopt_pa_start; pa < rmpopt_pa_end; pa += SZ_1G) {
rmpopt(pa);
cond_resched();
}
migrate_enable();
cpus_read_lock();
for (pa = rmpopt_pa_start; pa < rmpopt_pa_end; pa += SZ_1G) {
on_each_cpu_mask(follower_mask, rmpopt_smp, (void *)pa, true);
cond_resched();
}
cpus_read_unlock();
free_cpumask_var(follower_mask); Here, the leader exclusion must use the sibling mask, not clear_cpu(this_cpu). That's why my collapsed version uses:
cpumask_andnot(follower_mask, follower_mask,
topology_sibling_cpumask(smp_processor_id())); - If this_cpu is a primary: its sibling mask contains itself (the primary) -> andnot removes this core's primary from the followers.
- If this_cpu is a secondary: it isn't in follower_mask at all, but its sibling mask contains its primary, which is in follower_mask -> andnot still removes this core's primary.
So either way the current core is dropped from the followers. (The old code needed two branches because case #1 used clear_cpu(this_cpu) — only correct when this_cpu is the primary — while case #2 used the sibling andnot. The single andnot works for both cases).
Thanks,
Ashish
Quoted 5 lines
Quoted 5 lines
arch/x86/virt/svm/sev.c +5
-0+ goto followers; + } + +migrate_enable (); +
Re: [PATCH v8 4/7] x86/sev: Add support to perform RMP optimizations asynchronously
Hello Ashish,
Quoted 78 lines · Ashish
Hello Prateek,
Quoted 67 lines · K Prateek Nayak
Hello Ashish,
arch/x86/virt/svm/sev.c void snp_clear_rmpopt_configured(void) · line 698
Quoted 11 lines · Ashish Kalra
arch/x86/virt/svm/sev.c +11
-0+/* +* RMPOPT scans the RMP table, stores the result of the scan in the +* reserved processor memory. The RMP scan is the most expensive +* part. If a second RMPOPT occurs, it can skip the expensive scan +* if they can see a cached result in the reserved processor memory. +* +* Do RMPOPT on one CPU alone. Then, follow that up with RMPOPT +* on every other primary thread. Followers are "designed to" +* skip the scan if they see the "cached" scan results. +*/ +cpumask_copy (follower_mask, &rmpopt_cpumask);
rmpopt_cpumaskis constructed after hotplug is disabled but ...Quoted 36 lines
arch/x86/virt/svm/sev.c +36
-0+ +/* +* Pin the worker to the current CPU for the leader loop so that +* this_cpu remains valid and the RMPOPT instruction executes on +* the correct CPU. +* +* Use migrate_disable() rather than get_cpu() to prevent +* migration while still allowing preemption. +*/ +migrate_disable (); + this_cpu =smp_processor_id (); + +if (cpumask_test_cpu (this_cpu, follower_mask)) { +/* +* Current CPU is a primary thread in rmpopt_cpumask. +* Run leader locally and remove from follower mask. +*/ +cpumask_clear_cpu (this_cpu, follower_mask); + +for (pa = rmpopt_pa_start; pa < rmpopt_pa_end; pa +=SZ_1G ) { +rmpopt (pa); +cond_resched (); + } + }else if (cpumask_intersects (topology_sibling_cpumask (this_cpu), + follower_mask)) { +/* +* Current CPU is a sibling thread whose primary is in +* rmpopt_cpumask. RMPOPT_BASE MSR is per-core, so it +* is safe to run the leader locally. Remove the sibling's +* primary from the follower mask as this core is already +* covered by the leader. +*/ +cpumask_andnot (follower_mask, follower_mask, +topology_sibling_cpumask (this_cpu)); + +for (pa = rmpopt_pa_start; pa < rmpopt_pa_end; pa +=SZ_1G ) {arch/x86/virt/svm/sev.c void snp_clear_rmpopt_configured(void) · line 744
Quoted 10 lines
arch/x86/virt/svm/sev.c +10
-0+rmpopt (pa); +cond_resched (); + } + }else { +/* +* Current CPU does not have RMPOPT_BASE MSR programmed. +* Pick an explicit leader from the cpumask to avoid #UD. +* Use work_on_cpu() to run in process context on the leader, +* avoiding IPI latency. +*/ ...
this_cpuis neither in the "rmpopt_cpumask", nor is any of its siblings on "rmpopt_cpumask".Quoted 1 line
How does that happen?
Actually, this was the implementation before the CPU hotplug disable enforcement code was implemented and added in v8, and i should have fixed this
rmpopt_work_handler()accordingly for v8.With the enforced cpu hotplug disable support, case #3 here (above) is now dead code, and removing it lets cases #1 and #2 collapse too.
Quoted 8 lines
snp_prepare()requirescpu_online_mask==cpu_present_maskbefore SNP init — so whensnp_setup_rmpopt()programs the MSRs, every core's primary is online -> every core is inrmpopt_cpumask.So now the work handler always runs on a CPU whose core is programmed.
topology_sibling_cpumask(this_cpu)therefore always intersectsrmpopt_cpumask-> case #1 or #2 always matches.So i should actually drop case #3 here - which is: "
this_cpuis neither in the "rmpopt_cpumask", nor is any of its siblings onrmpopt_cpumask"
Ack.
Also the fact that cpu_mark_primary_thread() uses LSBs of APICID and if you have some insanely weird configuration - like boot with maxcpus=1, online all the secondary threads (CPUs 256-511 on a 256C/512T system), launch an SNP guest - it can actually leave everything except CORE0 out of the "rmpopt_cpumask".
Quoted 44 lines
Quoted 44 lines
Quoted 6 lines
arch/x86/virt/svm/sev.c +3
-0+int leader_cpu =cpumask_first (follower_mask); + +if (WARN_ON_ONCE (leader_cpu >= nr_cpu_ids)) {arch/x86/virt/svm/sev.c void snp_clear_rmpopt_configured(void) · line 757
Quoted 10 lines
arch/x86/virt/svm/sev.c +10
-0+migrate_enable (); + goto out; + } + +cpumask_clear_cpu (leader_cpu, follower_mask); + +/* Release migration pin before work_on_cpu(). */ +migrate_enable (); + +work_on_cpu (leader_cpu, rmpopt_leader_fn,NULL );This creates a delayed work and also waits for it to finish execution which will add more latency than a simple IPI if the comment about IPI latency above is accurate.
I think there is some corner case in construction of the "
rmpopt_cpumask" that requires this not-so-pretty else block. Can you elaborate why this is required?Perhaps the "
rmpopt_cpumask" construction needs:for_each_online_cpu(cpu) { /* Nominate the first CPU on the sibling mask for RMPOPT */ if (cpu != cpumask_first(topology_sibling_cpumask(cpu))) continue; cpumask_set_cpu(cpu, &rmpopt_cpumask); }and all you need here is:
/* Do RMPOPt for local core */ for (pa = rmpopt_pa_start; pa < rmpopt_pa_end; pa += SZ_1G) rmpopt(pa); /* Skip this core from concurrent RMPOPT */ cpumask_and_not(follower_mask, &rmpopt_cpumask, topology_sibling_cpumask(cpu));
Quoted 9 lines
Quoted 2 lines
No?
Yes, a simpler implementation will be like this:
...if (!alloc_cpumask_var(&follower_mask,
GFP_KERNEL))return;
If you move the migrate_disable() here, you can simply do an andnot without needing to copy the rmpopt_cpumask beforehand and save on one cpumask iteration.
Quoted 12 lines
cpumask_copy(follower_mask, &rmpopt_cpumask);/* * The current CPU's core always has RMPOPT_BASE programmed * (snp_prepare() required all CPUs online at setup and CPU hotplug * is disabled while SNP is active), so it can always be the leader. * RMPOPT_BASE is per-core; exclude this core from the followers. */ migrate_disable(); cpumask_andnot(follower_mask, follower_mask, topology_sibling_cpumask(smp_processor_id()));
Quoted 7 lines
for (pa = rmpopt_pa_start; pa < rmpopt_pa_end; pa += SZ_1G) {
rmpopt(pa);
cond_resched();
}
migrate_enable();
cpus_read_lock();I think you can even skip the cpus_read_lock() since we know for a fact that hotplug is disabled when we are here.
Perhaps we can have a lockdep_assert_cpu_hotplug_disabled() which ensures we'll get a splat if that assumption ever changes when running with LOCKDEP?
I'll let others comment if that is a good idea or not.
Quoted 14 lines
for (pa = rmpopt_pa_start; pa < rmpopt_pa_end; pa += SZ_1G) { on_each_cpu_mask(follower_mask, rmpopt_smp, (void *)pa, true); cond_resched(); } cpus_read_unlock(); free_cpumask_var(follower_mask);Here, the leader exclusion must use the sibling mask, not
clear_cpu(this_cpu). That's why my collapsed version uses:cpumask_andnot(follower_mask, follower_mask, topology_sibling_cpumask(smp_processor_id()));
Quoted 8 lines
- If
this_cpuis a primary: its sibling mask contains itself (the primary) -> andnot removes this core's primary from the followers.- If
this_cpuis a secondary: it isn't infollower_maskat all, but its sibling mask contains its primary, which is infollower_mask-> andnot still removes this core's primary.So either way the current core is dropped from the followers. (The old code needed two branches because case #1 used
clear_cpu(this_cpu)— only correct whenthis_cpuis the primary — while case #2 used the sibling andnot. The single andnot works for both cases).
Ack! And I think this looks much cleaner (to my eyes at least ;-)
Signature · 2 lines
Thanks and Regards, Prateek
Kalra, Ashish
maintainerRe: [PATCH v8 4/7] x86/sev: Add support to perform RMP optimizations asynchronously
Hello Prateek,
Quoted 148 lines · K Prateek Nayak
Hello Ashish,
Quoted 78 lines · Ashish
Hello Prateek,
Quoted 67 lines · K Prateek Nayak
Hello Ashish,
arch/x86/virt/svm/sev.c void snp_clear_rmpopt_configured(void) · line 698
Quoted 11 lines · Ashish Kalra
arch/x86/virt/svm/sev.c +11
-0+/* +* RMPOPT scans the RMP table, stores the result of the scan in the +* reserved processor memory. The RMP scan is the most expensive +* part. If a second RMPOPT occurs, it can skip the expensive scan +* if they can see a cached result in the reserved processor memory. +* +* Do RMPOPT on one CPU alone. Then, follow that up with RMPOPT +* on every other primary thread. Followers are "designed to" +* skip the scan if they see the "cached" scan results. +*/ +cpumask_copy (follower_mask, &rmpopt_cpumask);
rmpopt_cpumaskis constructed after hotplug is disabled but ...Quoted 36 lines
arch/x86/virt/svm/sev.c +36
-0+ +/* +* Pin the worker to the current CPU for the leader loop so that +* this_cpu remains valid and the RMPOPT instruction executes on +* the correct CPU. +* +* Use migrate_disable() rather than get_cpu() to prevent +* migration while still allowing preemption. +*/ +migrate_disable (); + this_cpu =smp_processor_id (); + +if (cpumask_test_cpu (this_cpu, follower_mask)) { +/* +* Current CPU is a primary thread in rmpopt_cpumask. +* Run leader locally and remove from follower mask. +*/ +cpumask_clear_cpu (this_cpu, follower_mask); + +for (pa = rmpopt_pa_start; pa < rmpopt_pa_end; pa +=SZ_1G ) { +rmpopt (pa); +cond_resched (); + } + }else if (cpumask_intersects (topology_sibling_cpumask (this_cpu), + follower_mask)) { +/* +* Current CPU is a sibling thread whose primary is in +* rmpopt_cpumask. RMPOPT_BASE MSR is per-core, so it +* is safe to run the leader locally. Remove the sibling's +* primary from the follower mask as this core is already +* covered by the leader. +*/ +cpumask_andnot (follower_mask, follower_mask, +topology_sibling_cpumask (this_cpu)); + +for (pa = rmpopt_pa_start; pa < rmpopt_pa_end; pa +=SZ_1G ) {arch/x86/virt/svm/sev.c void snp_clear_rmpopt_configured(void) · line 744
Quoted 10 lines
arch/x86/virt/svm/sev.c +10
-0+rmpopt (pa); +cond_resched (); + } + }else { +/* +* Current CPU does not have RMPOPT_BASE MSR programmed. +* Pick an explicit leader from the cpumask to avoid #UD. +* Use work_on_cpu() to run in process context on the leader, +* avoiding IPI latency. +*/ ...
this_cpuis neither in the "rmpopt_cpumask", nor is any of its siblings on "rmpopt_cpumask".Quoted 1 line
How does that happen?
Actually, this was the implementation before the CPU hotplug disable enforcement code was implemented and added in v8, and i should have fixed this
rmpopt_work_handler()accordingly for v8.With the enforced cpu hotplug disable support, case #3 here (above) is now dead code, and removing it lets cases #1 and #2 collapse too.
Quoted 8 lines
snp_prepare()requirescpu_online_mask==cpu_present_maskbefore SNP init — so whensnp_setup_rmpopt()programs the MSRs, every core's primary is online -> every core is inrmpopt_cpumask.So now the work handler always runs on a CPU whose core is programmed.
topology_sibling_cpumask(this_cpu)therefore always intersectsrmpopt_cpumask-> case #1 or #2 always matches.So i should actually drop case #3 here - which is: "
this_cpuis neither in the "rmpopt_cpumask", nor is any of its siblings onrmpopt_cpumask"Ack.
Also the fact that
cpu_mark_primary_thread()uses LSBs of APICID and if you have some insanely weird configuration - like boot with maxcpus=1, online all the secondary threads (CPUs 256-511 on a 256C/512T system), launch an SNP guest - it can actually leave everything except CORE0 out of the "rmpopt_cpumask".Quoted 50 lines
Quoted 37 lines
Quoted 6 lines
arch/x86/virt/svm/sev.c +3
-0+int leader_cpu =cpumask_first (follower_mask); + +if (WARN_ON_ONCE (leader_cpu >= nr_cpu_ids)) {arch/x86/virt/svm/sev.c void snp_clear_rmpopt_configured(void) · line 757
Quoted 10 lines
arch/x86/virt/svm/sev.c +10
-0+migrate_enable (); + goto out; + } + +cpumask_clear_cpu (leader_cpu, follower_mask); + +/* Release migration pin before work_on_cpu(). */ +migrate_enable (); + +work_on_cpu (leader_cpu, rmpopt_leader_fn,NULL );This creates a delayed work and also waits for it to finish execution which will add more latency than a simple IPI if the comment about IPI latency above is accurate.
I think there is some corner case in construction of the "
rmpopt_cpumask" that requires this not-so-pretty else block. Can you elaborate why this is required?Perhaps the "
rmpopt_cpumask" construction needs:for_each_online_cpu(cpu) { /* Nominate the first CPU on the sibling mask for RMPOPT */ if (cpu != cpumask_first(topology_sibling_cpumask(cpu))) continue; cpumask_set_cpu(cpu, &rmpopt_cpumask); }and all you need here is:
Quoted 9 lines
/* Do RMPOPt for local core */ for (pa = rmpopt_pa_start; pa < rmpopt_pa_end; pa += SZ_1G) rmpopt(pa); /* Skip this core from concurrent RMPOPT */ cpumask_and_not(follower_mask, &rmpopt_cpumask, topology_sibling_cpumask(cpu));No?
Yes, a simpler implementation will be like this:
...
Quoted 7 lines
Quoted 3 lines
if (!alloc_cpumask_var(&follower_mask,
GFP_KERNEL))return;If you move the
migrate_disable()here, you can simply do an andnot without needing to copy thermpopt_cpumaskbeforehand and save on one cpumask iteration.
Yes, that's a nice optimization, we can read directly from rmpopt_cpumask and write follower_mask in one pass.
Quoted 19 lines
Quoted 19 lines
cpumask_copy(follower_mask, &rmpopt_cpumask);/* * The current CPU's core always has RMPOPT_BASE programmed * (snp_prepare() required all CPUs online at setup and CPU hotplug * is disabled while SNP is active), so it can always be the leader. * RMPOPT_BASE is per-core; exclude this core from the followers. */ migrate_disable(); cpumask_andnot(follower_mask, follower_mask, topology_sibling_cpumask(smp_processor_id())); for (pa = rmpopt_pa_start; pa < rmpopt_pa_end; pa += SZ_1G) { rmpopt(pa); cond_resched(); } migrate_enable();
Quoted 8 lines
Quoted 1 line
cpus_read_lock();I think you can even skip the
cpus_read_lock()since we know for a fact that hotplug is disabled when we are here.Perhaps we can have a
lockdep_assert_cpu_hotplug_disabled()which ensures we'll get a splat if that assumption ever changes when running with LOCKDEP?
Yes, that is true when we have made sure that hotplug is disabled, but i think it is Ok to keep cpus_read_lock() here as it keeps Sashiko happy.
Quoted 19 lines
I'll let others comment if that is a good idea or not.
Quoted 16 lines
for (pa = rmpopt_pa_start; pa < rmpopt_pa_end; pa += SZ_1G) { on_each_cpu_mask(follower_mask, rmpopt_smp, (void *)pa, true); cond_resched(); } cpus_read_unlock(); free_cpumask_var(follower_mask);Here, the leader exclusion must use the sibling mask, not
clear_cpu(this_cpu). That's why my collapsed version uses:cpumask_andnot(follower_mask, follower_mask, topology_sibling_cpumask(smp_processor_id()));- If
this_cpuis a primary: its sibling mask contains itself (the primary) -> andnot removes this core's primary from the followers.
Quoted 9 lines
Quoted 6 lines
- If
this_cpuis a secondary: it isn't infollower_maskat all, but its sibling mask contains its primary, which is infollower_mask-> andnot still removes this core's primary.So either way the current core is dropped from the followers. (The old code needed two branches because case #1 used
clear_cpu(this_cpu)— only correct whenthis_cpuis the primary — while case #2 used the sibling andnot. The single andnot works for both cases).Ack! And I think this looks much cleaner (to my eyes at least ;-)
Thanks,
Ashish
Re: [PATCH v8 4/7] x86/sev: Add support to perform RMP optimizations asynchronously
In reply to Kalra, AshishQuoted 8 lines · Ashish
[...snip...]
Yes, a simpler implementation will be like this:
...if (!alloc_cpumask_var(&follower_mask,
GFP_KERNEL))
Perhaps have a WARN_ON_ONCE() here so we know rmpopt was not performed? Not a huge deal without though.
Quoted 20 lines
return;
cpumask_copy(follower_mask, &rmpopt_cpumask);/* * The current CPU's core always has RMPOPT_BASE programmed * (snp_prepare() required all CPUs online at setup and CPU hotplug * is disabled while SNP is active), so it can always be the leader. * RMPOPT_BASE is per-core; exclude this core from the followers. */ migrate_disable(); cpumask_andnot(follower_mask, follower_mask, topology_sibling_cpumask(smp_processor_id())); for (pa = rmpopt_pa_start; pa < rmpopt_pa_end; pa += SZ_1G) { rmpopt(pa); cond_resched(); } migrate_enable();
Quoted 10 lines
cpus_read_lock();
for (pa = rmpopt_pa_start; pa < rmpopt_pa_end; pa += SZ_1G) {
on_each_cpu_mask(follower_mask, rmpopt_smp, (void *)pa, true);
cond_resched();
}
cpus_read_unlock();
free_cpumask_var(follower_mask);Definitely better than the version in the original patch :) Thanks!
Quoted 22 lines
Here, the leader exclusion must use the sibling mask, not
clear_cpu(this_cpu). That's why my collapsed version uses:cpumask_andnot(follower_mask, follower_mask, topology_sibling_cpumask(smp_processor_id()));- If
this_cpuis a primary: its sibling mask contains itself (the primary) -> andnot removes this core's primary from the followers.- If
this_cpuis a secondary: it isn't infollower_maskat all, but its sibling mask contains its primary, which is infollower_mask-> andnot still removes this core's primary.So either way the current core is dropped from the followers. (The old code needed two branches because case #1 used
clear_cpu(this_cpu)— only correct whenthis_cpuis the primary — while case #2 used the sibling andnot. The single andnot works for both cases).Thanks,
AshishQuoted 5 lines
Quoted 5 lines
arch/x86/virt/svm/sev.c +5
-0+ goto followers; + } + +migrate_enable (); +
Ashish Kalra
maintainerPATCH v8 5/7 x86/sev: Add interface to re-enable RMP optimizations.
In reply to a message not in the archiveFrom: Ashish Kalra <[email protected]>
RMPOPT table is a per-CPU table which indicates if 1GB regions of physical memory are entirely hypervisor-owned or not.
When performing host memory accesses in hypervisor mode as well as non-SNP guest mode, the processor may consult the RMPOPT table to potentially skip an RMP access and improve performance.
Events such as RMPUPDATE can clear RMP optimizations. Add an interface to re-enable those optimizations.
The interface uses mod_delayed_work() instead of queue_delayed_work() so that the delay timer is reset on each call. This provides proper batching semantics: re-optimization runs 10 seconds after the *last* VM termination rather than after the first. mod_delayed_work() also re-queues work that is already in-flight, so a re-scan request during an active scan is not silently dropped.
--- arch/x86/include/asm/sev.h | 2 ++ arch/x86/virt/svm/sev.c | 15 +++++++++++++++ 2 files changed, 17 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); }int snp_prepare (void ); +void snp_rmpopt_all_physmem (void );void snp_setup_rmpopt (void );void snp_clear_rmpopt_configured (void );void snp_shutdown (void );
@@ -682,6 +683,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 int snp_prepare (void ) {return -ENODEV ; } +static inline void snp_rmpopt_all_physmem (void ) {}static inline void snp_setup_rmpopt (void ) {}static inline void snp_clear_rmpopt_configured (void ) {}static inline void snp_shutdown (void ) {}
arch/x86/virt/svm/sev.c +15 -0
@@ -782,6 +782,21 @@ static void rmpopt_work_handler(struct work_struct *work)free_cpumask_var (follower_mask); } +void snp_rmpopt_all_physmem (void ) +{ +if (!cpu_feature_enabled (X86_FEATURE_RMPOPT ) || !rmpopt_configured) +return ; + +guard (mutex)(&rmpopt_wq_mutex); + +if (!rmpopt_wq) +return ; + +mod_delayed_work (rmpopt_wq, &rmpopt_delayed_work, +msecs_to_jiffies (RMPOPT_WORK_TIMEOUT )); +} +EXPORT_SYMBOL_GPL (snp_rmpopt_all_physmem); +void snp_setup_rmpopt (void ) {u64 rmpopt_base;
Signature · 1 line
2.43.0
Re: [PATCH v8 5/7] x86/sev: Add interface to re-enable RMP optimizations.
Quoted 11 lines · Ashish Kalra
From: Ashish Kalra <[email protected]>
RMPOPT table is a per-CPU table which indicates if 1GB regions of physical memory are entirely hypervisor-owned or not.
When performing host memory accesses in hypervisor mode as well as non-SNP guest mode, the processor may consult the RMPOPT table to potentially skip an RMP access and improve performance.
Events such as RMPUPDATE can clear RMP optimizations. Add an interface to re-enable those optimizations.
This doesn't really help me understand when or how this function might be called.
Normal guest evens like splitting and collapsing large pages can clear RMP optimizations. Without some intervention, all RMP optimizations would eventually be lost. Periodically re-optimize the system.
Quoted 6 lines
The interface uses
mod_delayed_work()instead ofqueue_delayed_work()so that the delay timer is reset on each call. This provides proper batching semantics: re-optimization runs 10 seconds after the *last* VM termination rather than after the first.mod_delayed_work()also re-queues work that is already in-flight, so a re-scan request during an active scan is not silently dropped.
This seems sane.
Quoted 4 lines
arch/x86/virt/svm/sev.c +4
-0+void snp_rmpopt_all_physmem (void ) +{ +if (!cpu_feature_enabled (X86_FEATURE_RMPOPT ) || !rmpopt_configured) +return ;
arch/x86/virt/svm/sev.c static void rmpopt_work_handler(struct work_struct *work) · line 798
Quoted 10 lines
arch/x86/virt/svm/sev.c +10
-0+ +guard (mutex)(&rmpopt_wq_mutex); + +if (!rmpopt_wq) +return ; + +mod_delayed_work (rmpopt_wq, &rmpopt_delayed_work, +msecs_to_jiffies (RMPOPT_WORK_TIMEOUT )); +} +EXPORT_SYMBOL_GPL (snp_rmpopt_all_physmem);
Does this need to be globally exported? Or can it be exported to a single module namespace?
I'm close to being able to ack this, but it's still got a few too many nits to ack.
Ashish Kalra
maintainerPATCH v8 6/7 KVM: SEV: Perform RMP optimizations on SNP guest shutdown
In reply to a message not in the archiveFrom: Ashish Kalra <[email protected]>
Pages are converted from shared to private as SNP guests are launched. This destroys exisiting RMPOPT optimizations in the regions where pages are converted.
Conversely, guest pages are converted back to shared during SNP guest termination and their region may become eligible for RMPOPT optimization.
To take advantage of this, perform RMPOPT after guest termination. Do it after a delay so that a single RMPOPT pass can be done if multiple guests terminate in a short period of time.
--- arch/x86/kvm/svm/sev.c | 2 ++ 1 file changed, 2 insertions(+)
arch/x86/kvm/svm/sev.c +2 -0
@@ -3005,6 +3005,8 @@ void sev_vm_destroy(struct kvm *kvm)*/ if (snp_decommission_context (kvm))return ; + +snp_rmpopt_all_physmem (); }else {sev_unbind_asid (kvm, sev->handle); }
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v8 6/7] KVM: SEV: Perform RMP optimizations on SNP guest shutdown
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] RMPOPT optimization is skipped for large SNP VMs because the 10-second delay starts before memory…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] RMPOPT optimization is skipped for large SNP VMs because the 10-second delay starts before memory is converted to shared. --
commit b9cfe7528db8b373ee160983c730e4dd4a7d1b8a
Author: Ashish Kalra <[email protected]>
KVM: SEV: Perform RMP optimizations on SNP guest shutdown
This commit introduces an optimization to perform RMPOPT after an SNP guest terminates. It calls snp_rmpopt_all_physmem() in sev_vm_destroy(), which queues a scan after a 10-second delay so multiple terminations can be batched.
arch/x86/kvm/svm/sev.c void sev_vm_destroy(struct kvm *kvm) · line 2996
Quoted 10 lines
arch/x86/kvm/svm/sev.c +2
-0@@ -2992,6 +2992,8 @@ void sev_vm_destroy(struct kvm *kvm)*/ if (snp_decommission_context (kvm))return ; + +snp_rmpopt_all_physmem ();
[Severity: High]
Does this delay start too early for large VMs?
In VFS, when the last reference to a guest_memfd is dropped, __fput() calls kvm_gmem_release(). This drops the KVM reference, triggering sev_vm_destroy() which starts the 10-second delay here.
After kvm_gmem_release() returns, VFS drops the inode, triggering evict_inode(). This frees folios via sev_gmem_invalidate():
arch/x86/kvm/svm/sev.c:sev_gmem_invalidate() {
...
for (pfn = start; pfn < end;) {
...
rc = rmp_make_shared(...);
...
clflush_cache_range(...);
...
}
}For large VMs, sequentially converting millions of pages back to shared takes significantly longer than 10 seconds. The RMPOPT scan will execute while sev_gmem_invalidate() is still running, observe the remaining pages as private, and skip optimizing them.
Will this result in permanently missing the optimization opportunity for those pages on large guests?
Quoted 3 lines
arch/x86/kvm/svm/sev.c +0
-0}else {sev_unbind_asid (kvm, sev->handle); }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
Re: [PATCH v8 6/7] KVM: SEV: Perform RMP optimizations on SNP guest shutdown
In reply to Ashish KalraQuoted 9 lines · Ashish Kalra
From: Ashish Kalra <[email protected]>
Pages are converted from shared to private as SNP guests are launched. This destroys exisiting RMPOPT optimizations in the regions where pages are converted.
Conversely, guest pages are converted back to shared during SNP guest termination and their region may become eligible for RMPOPT optimization.
Oh, actually that would be good text for the *previous* patch too. You might want to move some of it there.
Ashish Kalra
maintainerPATCH v8 7/7 x86/sev: Add debugfs support for RMPOPT
In reply to a message not in the archiveFrom: Ashish Kalra <[email protected]>
Add a debugfs interface to report per-CPU RMPOPT status across all system RAM.
To dump the per-CPU RMPOPT status for all system RAM:
/sys/kernel/debug/rmpopt# cat rmpopt-table
Memory @ 0GB: CPU(s): none Memory @ 1GB: CPU(s): none Memory @ 2GB: CPU(s): 0-1023 Memory @ 3GB: CPU(s): 0-1023 Memory @ 4GB: CPU(s): none Memory @ 5GB: CPU(s): 0-1023 Memory @ 6GB: CPU(s): 0-1023 Memory @ 7GB: CPU(s): 0-1023
...
Memory @1025GB: CPU(s): 0-1023 Memory @1026GB: CPU(s): 0-1023 Memory @1027GB: CPU(s): 0-1023 Memory @1028GB: CPU(s): 0-1023 Memory @1029GB: CPU(s): 0-1023 Memory @1030GB: CPU(s): 0-1023 Memory @1031GB: CPU(s): 0-1023 Memory @1032GB: CPU(s): 0-1023 Memory @1033GB: CPU(s): 0-1023 Memory @1034GB: CPU(s): 0-1023 Memory @1035GB: CPU(s): 0-1023 Memory @1036GB: CPU(s): 0-1023 Memory @1037GB: CPU(s): 0-1023 Memory @1038GB: CPU(s): none
--- arch/x86/virt/svm/sev.c | 128 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+)
arch/x86/virt/svm/sev.c +128 -0
@@ -20,6 +20,8 @@#include <linux/amd-iommu.h> #include <linux/nospec.h> #include <linux/workqueue.h> +#include <linux/debugfs.h> +#include <linux/seq_file.h> #include <asm/sev.h> #include <asm/processor.h>
@@ -145,6 +147,15 @@ static DEFINE_SPINLOCK(snp_leaked_pages_list_lock);static unsigned long snp_nr_leaked_pages; +/* All users of rmpopt_report_cpumask must hold rmpopt_show_mutex. */ +static cpumask_t rmpopt_report_cpumask; +static struct dentry *rmpopt_debugfs; +static DEFINE_MUTEX (rmpopt_show_mutex); + +struct seq_paddr { +phys_addr_t next_seq_paddr; +}; +#undef pr_fmt#define pr_fmt (fmt) "SEV-SNP: " fmt
@@ -587,6 +598,8 @@ static void rmpopt_cleanup(void)cancel_delayed_work_sync (&rmpopt_delayed_work);destroy_workqueue (rmpopt_wq); +debugfs_remove_recursive (rmpopt_debugfs); + rmpopt_debugfs =NULL ;cpus_read_lock ();
@@ -635,6 +648,10 @@ static inline bool __rmpopt(u64 pa_start, u64 op_type) : "a" (pa_start), "c" (op_type) : "memory", "cc"); +if (op_type ==RMPOPT_FUNC_REPORT_STATUS ) +assign_cpu (smp_processor_id (), &rmpopt_report_cpumask, + optimized); +return optimized; }
@@ -669,6 +686,115 @@ static long rmpopt_leader_fn(void *arg)return 0 ; } +/* +* 'val' is a system physical address. +*/ +static void rmpopt_report_status (void *val) +{ +u64 pa_start =ALIGN_DOWN ((u64 )val,SZ_1G ); +u64 op_type =RMPOPT_FUNC_REPORT_STATUS ; + +__rmpopt (pa_start, op_type); +} + +/* +* start() can be called multiple times if allocated buffer has overflowed +* and bigger buffer is allocated. +*/ +static void *rmpopt_table_seq_start (struct seq_file *seq,loff_t *pos) +{ +phys_addr_t end_paddr = rmpopt_pa_end; +struct seq_paddr *p = seq->private; + +if (*pos ==0 ) { + p->next_seq_paddr = rmpopt_pa_start; +if (p->next_seq_paddr >= end_paddr) +return NULL ; +return &p->next_seq_paddr; + } + +if (p->next_seq_paddr >= end_paddr) +return NULL ; + +return &p->next_seq_paddr; +} + +static void *rmpopt_table_seq_next (struct seq_file *seq,void *v,loff_t *pos) +{ +phys_addr_t end_paddr = rmpopt_pa_end; +phys_addr_t *curr_paddr = v; + + (*pos)++; + *curr_paddr +=SZ_1G ; +if (*curr_paddr >= end_paddr) +return NULL ; + +return curr_paddr; +} + +static void rmpopt_table_seq_stop (struct seq_file *seq,void *v) +{ +} + +static int rmpopt_table_seq_show (struct seq_file *seq,void *v) +{ +phys_addr_t *curr_paddr = v; + +guard (mutex)(&rmpopt_show_mutex); + +seq_printf (seq,"Memory @%3lluGB: " , + *curr_paddr >> (get_order (SZ_1G ) +PAGE_SHIFT )); + +/* +* Query all online CPUs rather than just rmpopt_cpumask (primary +* threads only). The RMPOPT instruction only needs to run on one +* thread per core for the optimization to take effect, but debugfs +* reporting requires the RMPOPT status across all CPUs. +* Performance is not a concern for this diagnostic interface. +* +* This is safe because RMPOPT_BASE MSR is per-core and +* snp_prepare() ensures all CPUs are online when the MSR is +* programmed during snp_setup_rmpopt(). +*/ +cpumask_clear (&rmpopt_report_cpumask); +on_each_cpu_mask (cpu_online_mask, rmpopt_report_status, + (void *)*curr_paddr, true); + +if (cpumask_empty (&rmpopt_report_cpumask)) +seq_puts (seq,"CPU(s): none\n" ); +else +seq_printf (seq,"CPU(s): %*pbl\n" ,cpumask_pr_args (&rmpopt_report_cpumask)); + +return 0 ; +} + +static const struct seq_operations rmpopt_table_seq_ops = { + .start = rmpopt_table_seq_start, + .next = rmpopt_table_seq_next, + .stop = rmpopt_table_seq_stop, + .show = rmpopt_table_seq_show +}; + +static int rmpopt_table_open (struct inode *inode,struct file *file) +{ +return seq_open_private (file, &rmpopt_table_seq_ops,sizeof (struct seq_paddr )); +} + +static const struct file_operations rmpopt_table_fops = { + .open = rmpopt_table_open, + .read = seq_read, + .llseek = seq_lseek, + .release = seq_release_private, +}; + +static void rmpopt_debugfs_setup (void ) +{ + rmpopt_debugfs =debugfs_create_dir ("rmpopt" , arch_debugfs_dir); + +debugfs_create_file ("rmpopt-table" ,0400 , rmpopt_debugfs, +NULL , &rmpopt_table_fops); +} +/* * RMPOPT optimizations skip RMP checks at 1GB granularity if this * range of memory does not contain any SNP guest memory.
@@ -874,6 +1000,8 @@ void snp_setup_rmpopt(void)* optimizations on all physical memory. */ queue_delayed_work (rmpopt_wq, &rmpopt_delayed_work,0 ); + +rmpopt_debugfs_setup (); }EXPORT_SYMBOL_FOR_MODULES (snp_setup_rmpopt,"ccp" );
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v8 7/7] x86/sev: Add debugfs support for RMPOPT
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Broadcasting the RMPOPT instruction to cpu_online_mask in debugfs can trigger a kernel panic…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Broadcasting the RMPOPT instruction to cpu_online_mask in debugfs can trigger a kernel panic (#UD/#GP) if a core's primary thread is offline. --
commit bd48c96c2b885c0673d20119de172ea1dd26cdf2
Author: Ashish Kalra <[email protected]>
x86/sev: Add debugfs support for RMPOPT
This commit adds a debugfs interface to report per-CPU RMPOPT status across all system RAM.
Quoted 3 lines
arch/x86/virt/svm/sev.c +0
-0
[ ... ]
Quoted 1 line
arch/x86/virt/svm/sev.c +0
-0@@ -669,6 +686,115 @@ static long rmpopt_leader_fn(void *arg)
[ ... ]
Quoted 13 lines
arch/x86/virt/svm/sev.c +13
-0+static int rmpopt_table_seq_show (struct seq_file *seq,void *v) +{ +phys_addr_t *curr_paddr = v; + +guard (mutex)(&rmpopt_show_mutex); + +seq_printf (seq,"Memory @%3lluGB: " , + *curr_paddr >> (get_order (SZ_1G ) +PAGE_SHIFT )); + +/* +* Query all online CPUs rather than just rmpopt_cpumask (primary +* threads only). The RMPOPT instruction only needs to run on one +* thread per core for the optimization to take effect, but debugfs
arch/x86/virt/svm/sev.c static long rmpopt_leader_fn(void *arg) · line 761
Quoted 10 lines
arch/x86/virt/svm/sev.c +10
-0+* reporting requires the RMPOPT status across all CPUs. +* Performance is not a concern for this diagnostic interface. +* +* This is safe because RMPOPT_BASE MSR is per-core and +* snp_prepare() ensures all CPUs are online when the MSR is +* programmed during snp_setup_rmpopt(). +*/ +cpumask_clear (&rmpopt_report_cpumask); +on_each_cpu_mask (cpu_online_mask, rmpopt_report_status, + (void *)*curr_paddr, true);
[Severity: High]
Could broadcasting to cpu_online_mask here trigger a kernel panic if a core's primary thread is offline?
In snp_setup_rmpopt(), the base msr is only programmed on cpus in rmpopt_cpumask, which is strictly limited to online primary threads:
if (topology_is_primary_thread(cpu))
cpumask_set_cpu(cpu, &rmpopt_cpumask);
If a core's primary thread is offline but its secondary thread is online, the secondary thread will be included in cpu_online_mask, but the msr for its core will remain unprogrammed.
When this debugfs file is read, that secondary thread will receive the ipi and execute the rmpopt instruction in rmpopt_report_status(). Because its core's msr was never enabled, won't executing the instruction raise a fatal invalid opcode exception and panic the kernel?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
Borislav Petkov
maintainerRe: [PATCH v8 7/7] x86/sev: Add debugfs support for RMPOPT
In reply to Ashish KalraQuoted 33 lines · Ashish Kalra
From: Ashish Kalra <[email protected]>
Add a debugfs interface to report per-CPU RMPOPT status across all system RAM.
To dump the per-CPU RMPOPT status for all system RAM:
/sys/kernel/debug/rmpopt# cat rmpopt-table
Memory @ 0GB: CPU(s): none Memory @ 1GB: CPU(s): none Memory @ 2GB: CPU(s): 0-1023 Memory @ 3GB: CPU(s): 0-1023 Memory @ 4GB: CPU(s): none Memory @ 5GB: CPU(s): 0-1023 Memory @ 6GB: CPU(s): 0-1023 Memory @ 7GB: CPU(s): 0-1023...
Memory @1025GB: CPU(s): 0-1023 Memory @1026GB: CPU(s): 0-1023 Memory @1027GB: CPU(s): 0-1023 Memory @1028GB: CPU(s): 0-1023 Memory @1029GB: CPU(s): 0-1023 Memory @1030GB: CPU(s): 0-1023 Memory @1031GB: CPU(s): 0-1023 Memory @1032GB: CPU(s): 0-1023 Memory @1033GB: CPU(s): 0-1023 Memory @1034GB: CPU(s): 0-1023 Memory @1035GB: CPU(s): 0-1023 Memory @1036GB: CPU(s): 0-1023 Memory @1037GB: CPU(s): 0-1023 Memory @1038GB: CPU(s): none
Quoted 5 lines
Suggested-by Thomas Lendacky <[email protected]>Signed-off-by Ashish Kalra <[email protected]>--- arch/x86/virt/svm/sev.c | 128 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+)
https://lwn.net/Articles/309298/
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteKalra, Ashish
maintainerRe: [PATCH v8 7/7] x86/sev: Add debugfs support for RMPOPT
Quoted 34 lines · Borislav Petkov
Quoted 33 lines · Ashish Kalra
From: Ashish Kalra <[email protected]>
Add a debugfs interface to report per-CPU RMPOPT status across all system RAM.
To dump the per-CPU RMPOPT status for all system RAM:
/sys/kernel/debug/rmpopt# cat rmpopt-table
Memory @ 0GB: CPU(s): none Memory @ 1GB: CPU(s): none Memory @ 2GB: CPU(s): 0-1023 Memory @ 3GB: CPU(s): 0-1023 Memory @ 4GB: CPU(s): none Memory @ 5GB: CPU(s): 0-1023 Memory @ 6GB: CPU(s): 0-1023 Memory @ 7GB: CPU(s): 0-1023...
Memory @1025GB: CPU(s): 0-1023 Memory @1026GB: CPU(s): 0-1023 Memory @1027GB: CPU(s): 0-1023 Memory @1028GB: CPU(s): 0-1023 Memory @1029GB: CPU(s): 0-1023 Memory @1030GB: CPU(s): 0-1023 Memory @1031GB: CPU(s): 0-1023 Memory @1032GB: CPU(s): 0-1023 Memory @1033GB: CPU(s): 0-1023 Memory @1034GB: CPU(s): 0-1023 Memory @1035GB: CPU(s): 0-1023 Memory @1036GB: CPU(s): 0-1023 Memory @1037GB: CPU(s): 0-1023 Memory @1038GB: CPU(s): none
Quoted 8 lines
Quoted 5 lines
Suggested-by Thomas Lendacky <[email protected]>Signed-off-by Ashish Kalra <[email protected]>--- arch/x86/virt/svm/sev.c | 128 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+)
Since the RMPOPT file is a diagnostic (verify the optimization took effect), debugfs is arguably the right home for it and we are not claiming it to be an API (there is no Documentation/ABI entry for it) and we are not presenting it as something tools should depend on, it is a self-contained diagnostic/debug interface.
Maybe i can add a line to this patch's commit message stating it's a debug-only interface with no stability guarantee.
We have to provide some method/interface for users to verify if RMP optimizations are enabled for a GB range of memory.
Thanks,
Ashish
Borislav Petkov
maintainerRe: [PATCH v8 7/7] x86/sev: Add debugfs support for RMPOPT
Quoted 2 lines · Ashish
Maybe i can add a line to this patch's commit message stating it's a debug-only interface with no stability guarantee.
Sounds to me like you didn't really read that article.
Quoted 2 lines
We have to provide some method/interface for users to verify if RMP optimizations are enabled for a GB range of memory.
Sounds to me like this wants to be a facility which is present in the kernel and it is going to be an ABI.
I am unclear on the real use case but I'm open to being persuaded otherwise.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH v8 7/7] x86/sev: Add debugfs support for RMPOPT
In reply to Kalra, AshishQuoted 2 lines · Ashish
Maybe i can add a line to this patch's commit message stating it's a debug-only interface with no stability guarantee.
I'd highly suggest reading the lwn article Boris referenced.
In the meantime, drop this patch from the series. Please. Let's revisit debugging interfaces *after* this gets merged. That way, you can concentrate on functionality and not debug interfaces that aren't critically needed.
Kalra, Ashish
maintainerRe: [PATCH v8 7/7] x86/sev: Add debugfs support for RMPOPT
Quoted 10 lines · Dave Hansen
Quoted 2 lines · Ashish
Maybe i can add a line to this patch's commit message stating it's a debug-only interface with no stability guarantee.
I'd highly suggest reading the lwn article Boris referenced.
In the meantime, drop this patch from the series. Please. Let's revisit debugging interfaces *after* this gets merged. That way, you can concentrate on functionality and not debug interfaces that aren't critically needed.
I will drop this patch from the series and then revisit the debugging/ observability interface after this series gets merged.
Thanks,
Ashish