Add RMPOPT support.
Subsystems (6)
Ashish Kalra
maintainerv6 cover Add RMPOPT support.
From: Ashish Kalra <[email protected]>
In the SEV-SNP architecture, hypervisor and non-SNP guests are subject to RMP checks on writes to provide integrity of SEV-SNP guest memory.
The RMPOPT architecture enables optimizations whereby the RMP checks can be skipped if 1GB regions of memory are known to not contain any SNP guest memory.
RMPOPT is a new instruction designed to minimize the performance overhead of RMP checks for the hypervisor and non-SNP guests.
RMPOPT instruction currently supports two functions. In case of the verify and report status function the CPU will read the RMP contents, verify the entire 1GB region starting at the provided SPA is HV-owned. For the entire 1GB region it checks that all RMP entries in this region are HV-owned (i.e, not in assigned state) and then accordingly updates the RMPOPT table to indicate if optimization has been enabled and provide indication to software if the optimization was successful.
In case of report status function, the CPU returns the optimization status for the 1GB region.
The RMPOPT table is managed by a combination of software and hardware. Software uses the RMPOPT instruction to set bits in the table, indicating that regions of memory are entirely HV-owned. Hardware automatically clears bits in the RMPOPT table when RMP contents are changed during RMPUPDATE instruction.
For more information on the RMPOPT instruction, see the AMD64 RMPOPT technical documentation.
As SNP is enabled by default the hypervisor and non-SNP guests are subject to RMP write checks to provide integrity of SNP guest memory.
This patch-series adds support to enable RMP optimizations for up to 2TB of system RAM across the system and allow RMPUPDATE to disable those optimizations as SNP guests are launched.
Support for RAM larger than 2 TB will be added in follow-on series.
This series also introduces support to re-enable RMP optimizations during SNP guest termination, after guest pages have been converted back to shared.
RMP optimizations are performed asynchronously by queuing work on a dedicated workqueue after a 10 second delay.
Delaying work allows batching of multiple SNP guest terminations.
Once 1GB hugetlb guest_memfd support is merged, support for re-enabling RMPOPT optimizations during 1GB page cleanup will be added in follow-on series.
Additionally add debugfs interface to report per-CPU RMPOPT status across all system RAM.
v6:
- Drop wrmsrq_on_cpus() helper; use for_each_cpu() with wrmsrq_on_cpu() instead, as RMPOPT_BASE MSR programming is not performance-critical.
- Rewrite rmpopt_work_handler() leader selection to use a local follower_mask copy instead of modifying the global rmpopt_cpumask. This eliminates the current_cpu_cleared tracking and the restore at the end, and removes the need for synchronization comments about transient cpumask inconsistency.
- Add three-way leader selection in rmpopt_work_handler():
1. Current CPU is a primary thread in cpumask: run leader locally.
2. Current CPU is a sibling thread whose primary is in cpumask:
run leader locally (RMPOPT_BASE MSR is per-core), remove the primary from followers via cpumask_andnot(topology_sibling_cpumask).
3. Current CPU's core has no RMPOPT_BASE MSR programmed: pick an explicit leader via cpumask_first() + smp_call_function_single() to avoid #UD, with cpus_read_lock() around the IPI loop.
- Add WARN_ON_ONCE guard for empty cpumask in the explicit leader fallback path, with migrate_enable() before goto out.
- Add .llseek = seq_lseek to rmpopt_table_fops for consistency with other seq_file-based debugfs files and to support tools like "less".
- Change debugfs file permissions from 0444 to 0400 to restrict access to root only.
- Add comment in rmpopt_table_seq_show() explaining why cpu_online_mask is safe: RMPOPT_BASE MSR is per-core and snp_prepare() ensures all CPUs are online when the MSR is programmed.
Sashiko AI code review identified several of the above issues.
v5:
- Introduce rmpopt_cleanup() to tear down workqueue, debugfs, cpumask, and MSR state, called from snp_shutdown().
- Introduce rmpopt_wq_mutex to serialize snp_setup_rmpopt(), snp_rmpopt_all_physmem(), and rmpopt_cleanup().
- Introduce rmpopt_show_mutex to serialize debugfs reporting of rmpopt_report_cpumask.
- Move snp_rmpopt_all_physmem() call after SNP DECOMMISSION during guest shutdown.
- Use migrate_disable()/migrate_enable() for CPU pinning in the rmpopt_work_handler() leader loop to maintain CPU affinity without disabling preemption for the entire RMPOPT scan.
- Add cpus_read_lock()/cpus_read_unlock() around the follower on_each_cpu_mask() loop in rmpopt_work_handler().
- Guard snp_setup_rmpopt() against re-initialization when SNP_SHUTDOWN_EX with x86_snp_shutdown=0 skips rmpopt_cleanup() but clears snp_initialized, preventing workqueue and resource leaks on repeated init/shutdown cycles.
- Replace setup_clear_cpu_cap() with pr_err() on alloc_workqueue() failure in snp_setup_rmpopt(), as setup_clear_cpu_cap() cannot be used after alternatives are patched; callers check rmpopt_wq != NULL as the runtime guard instead.
- Add pr_info() when RMPOPT coverage is capped at 2TB.
- Add comments noting CPU hotplug is not supported with SNP enabled and only online primary threads are covered by rmpopt_cpumask.
- Add comment in setup_rmptable() noting Segmented RMP must be enabled to enable RMPOPT.
- Simplify cpumask setup loop to set if primary thread rather than skip if not primary.
- Improve grammar and clarity in snp_setup_rmpopt() comments.
- Added Reviewed-by's.
Sashiko AI code review identified several of the above issues.
v4:
- Add new wrmsrq_on_cpus() helper to write same u64 value to a per-CPU MSR across a cpumask without per-cpu struct allocation overhead.
- Rename configure_and_enable_rmpopt() to snp_setup_rmpopt().
- Use wrmsrq_on_cpus() instead of wrmsrq_on_cpu() loop for programming RMPOPT_BASE MSRs.
- Add setup_clear_cpu_cap(X86_FEATURE_RMPOPT) if segmented RMP setup fails or workqueue allocation fails.
- Add X86_FEATURE_RMPOPT feature clear logic in amd_cc_platform_clear() for CC_ATTR_HOST_SEV_SNP.
- All of the above allow checking for only X86_FEATURE_RMPOPT for both RMPOPT setup/enable and RMP re-optimizations.
- Rename snp_perform_rmp_optimization() to snp_rmpopt_all_physmem().
- Split rmpopt() into rmpopt() and rmpopt_smp() for SMP callback use.
- Introduce separate rmpopt_report_cpumask for debugfs reporting, distinct from rmpopt_cpumask used for primary thread tracking.
- Remove snp_perform_rmp_optimization() call from __sev_snp_init_locked() and instead setup and enable RMPOPT after SNP is enabled and initialized.
v3:
- Drop all RMPOPT kthread support and introduce adding custom and dedicated workqueue to schedule delayed and asynchronous RMPOPT work.
- Drop the guest_memfd inode cleanup interface and add support to re-enable RMP optimizations during guest shutdown using the asynchronous and delayed workqueue interface.
- Introduce new __rmpopt() helper and rmpopt() and rmpopt_report_status() wrappers on top which use rax and rcx parameters to closely match RMPOPT specs.
- Use new optimized RMPOPT loop to issue RMPOPT instructions on all system RAM upto 2TB and all CPUs, by optimizing each range on one CPU first, then let other CPUs execute RMPOPT in parallel so they can skip most work as the range has already been optimized.
- Also add support for running the optimized RMPOPT loop only on one thread per core.
- Replace all PUD_SIZE references with SZ_1G to conform to 1GB regions as specified by RMPOPT specifications and not be dependent on PUD_SIZE which makes the RMPOPT patch-set independent of x86 page table sizes.
- Use wrmsrq_on_cpu() to program the RMPOPT_BASE MSR registers on all CPUs that removes all ugly casting to use on_each_cpu_mask().
- Fix inline commits and patch commit messages
v2:
- Drop all NUMA and Socket configuration and enablement support and enable RMPOPT support for up to 2TB of system RAM.
- Drop get_cpumask_of_primary_threads() and enable per-core RMPOPT base MSRs and issue RMPOPT instruction on all CPUs.
- Drop the configfs interface to manually re-enable RMP optimizations.
- Add new guest_memfd cleanup interface to automatically re-enable RMP optimizations during guest shutdown.
- Include references to the public RMPOPT documentation.
- Move debugfs directory for RMPOPT under architecuture specific parent directory.
Ashish Kalra (6):
x86/cpufeatures: Add X86_FEATURE_AMD_RMPOPT feature flag x86/sev: Initialize RMPOPT configuration MSRs x86/sev: Add support to perform RMP optimizations asynchronously x86/sev: Add interface to re-enable RMP optimizations.
KVM: SEV: Perform RMP optimizations on SNP guest shutdown x86/sev: Add debugfs support for RMPOPT
arch/x86/coco/core.c | 1 + arch/x86/include/asm/cpufeatures.h | 2 +- arch/x86/include/asm/msr-index.h | 3 + arch/x86/include/asm/sev.h | 4 + arch/x86/kernel/cpu/scattered.c | 1 + arch/x86/kvm/svm/sev.c | 2 + arch/x86/virt/svm/sev.c | 398 ++++++++++++++++++++++++++++- drivers/crypto/ccp/sev-dev.c | 3 + 8 files changed, 412 insertions(+), 2 deletions(-)
Signature · 1 line
2.43.0
Ashish Kalra
maintainerPATCH v6 1/6 x86/cpufeatures: Add X86_FEATURE_AMD_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 + 2 files changed, 2 insertions(+), 1 deletion(-)
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 } };
Signature · 1 line
2.43.0
Ashish Kalra
maintainerPATCH v6 2/6 x86/sev: Initialize RMPOPT configuration MSRs
In reply to Ashish KalraFrom: 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 | 1 + arch/x86/include/asm/msr-index.h | 3 ++ arch/x86/include/asm/sev.h | 2 + arch/x86/virt/svm/sev.c | 65 +++++++++++++++++++++++++++++++- drivers/crypto/ccp/sev-dev.c | 3 ++ 5 files changed, 73 insertions(+), 1 deletion(-)
arch/x86/coco/core.c +1 -0
@@ -172,6 +172,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 ; +setup_clear_cpu_cap (X86_FEATURE_RMPOPT );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 +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_setup_rmpopt (void );void snp_shutdown (void );#else static inline bool snp_probe_rmptable_info (void ) {return false; }
@@ -680,6 +681,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_setup_rmpopt (void ) {}static inline void snp_shutdown (void ) {}#endif
arch/x86/virt/svm/sev.c +64 -1
96 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
Ashish Kalra
maintainerPATCH v6 3/6 x86/sev: Add support to perform RMP optimizations asynchronously
In reply to Ashish KalraFrom: 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 | 196 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 193 insertions(+), 3 deletions(-)
arch/x86/virt/svm/sev.c +193 -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,7 +126,18 @@ 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; + +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);
@@ -566,6 +578,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)
@@ -574,7 +594,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 )
@@ -592,6 +613,134 @@ void snp_shutdown(void) }EXPORT_SYMBOL_FOR_MODULES (snp_shutdown,"ccp" ); +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); +} + +/* +* 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); + }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); + }else { +/* +* Current CPU does not have RMPOPT_BASE MSR programmed. +* Pick an explicit leader from the cpumask to avoid #UD. +*/ +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); + +cpus_read_lock (); +for (pa = rmpopt_pa_start; pa < rmpopt_pa_end; pa +=SZ_1G ) +smp_call_function_single (leader_cpu, rmpopt_smp, + (void *)pa, true); +cpus_read_unlock (); + } + +migrate_enable (); + +/* Followers: run RMPOPT on remaining cores */ +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;
@@ -600,11 +749,35 @@ void snp_setup_rmpopt(void)if (!cpu_feature_enabled (X86_FEATURE_RMPOPT ))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 ; + } +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
@@ -628,6 +801,23 @@ void snp_setup_rmpopt(void)wrmsrq_on_cpu (cpu,MSR_AMD64_RMPOPT_BASE , rmpopt_base);cpus_read_unlock (); + +INIT_DELAYED_WORK (&rmpopt_delayed_work, rmpopt_work_handler); + + 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
Ashish Kalra
maintainerPATCH v6 4/6 x86/sev: Add interface to re-enable RMP optimizations.
In reply to Ashish KalraFrom: 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.
--- 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_shutdown (void );#else
@@ -681,6 +682,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_shutdown (void ) {}#endif
arch/x86/virt/svm/sev.c +15 -0
@@ -741,6 +741,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 )) +return ; + +guard (mutex)(&rmpopt_wq_mutex); + +if (!rmpopt_wq) +return ; + +queue_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
Ashish Kalra
maintainerPATCH v6 5/6 KVM: SEV: Perform RMP optimizations on SNP guest shutdown
In reply to Ashish KalraFrom: 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
Ashish Kalra
maintainerPATCH v6 6/6 x86/sev: Add debugfs support for RMPOPT
In reply to Ashish KalraFrom: 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>
@@ -144,6 +146,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
@@ -585,6 +596,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 ();
@@ -622,6 +635,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; }
@@ -641,6 +658,115 @@ static void rmpopt_smp(void *val)rmpopt ((u64 )val); } +/* +* '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.
@@ -833,6 +959,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 v6 1/6] x86/cpufeatures: Add X86_FEATURE_AMD_RMPOPT feature flag
In reply to Ashish KalraThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] Mismatch between the feature flag name in the commit message title and the actual code. - [Low]…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] Mismatch between the feature flag name in the commit message title and the actual code.
- [Low] Missing synchronization of arch/x86/include/asm/cpufeatures.h with the corresponding tools copy. --
commit c36f3189418bf1ed5bdfdcfed5326214cecfd96a
Author: Ashish Kalra <[email protected]>
x86/cpufeatures: Add X86_FEATURE_AMD_RMPOPT feature flag
This commit adds a new feature flag indicating whether the RMPOPT instruction is supported. It allows RMP checks to be skipped when 1-GB memory regions are known to contain no SEV-SNP guest memory.
Quoted 1 line
x86/cpufeatures: Add
X86_FEATURE_AMD_RMPOPTfeature flag
[Severity: Medium]
The commit message title states it adds the X86_FEATURE_AMD_RMPOPT feature flag, but the code implementation defines it as X86_FEATURE_RMPOPT (without the AMD_ prefix). Should the commit title be updated to accurately reflect the macro name defined in the code?
arch/x86/include/asm/cpufeatures.h line 79
Quoted 10 lines
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 */
[Severity: Low]
Does this update need to be mirrored intools/arch/x86/include/asm/cpufeatures.h?
Adding a new CPU feature flag to the kernel header without updating the tools copy typically causes tools like perf and the check-headers.sh script to emit build warnings about the kernel ABI header differing from the latest version.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
[email protected]
AI reviewRe: [PATCH v6 2/6] x86/sev: Initialize RMPOPT configuration MSRs
In reply to Ashish KalraThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Late clearing of X86_FEATURE_RMPOPT via setup_clear_cpu_cap() fails to un-patch CPU alternatives…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Late clearing of X86_FEATURE_RMPOPT via setup_clear_cpu_cap() fails to un-patch CPU alternatives, causing snp_setup_rmpopt() to dangerously enable RMPOPT on unsupported Contiguous RMP configurations. --
commit 551ddbd8061d6e0326ab620a808e849941500d13
Author: Ashish Kalra <[email protected]>
x86/sev: Initialize RMPOPT configuration MSRs
This commit introduces RMPOPT instruction support to manage per-CPU RMP optimization structures. It sets up the RMPOPT table base to enable optimizations for up to 2TB of system RAM once SNP is enabled. It also attempts to clear the RMPOPT CPU capability if Segmented RMP is disabled.
Quoted 12 lines
arch/x86/coco/core.c +1
-0@@ -172,6 +172,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 ; +setup_clear_cpu_cap (X86_FEATURE_RMPOPT );break ;default :break ;
[ ... ]
Quoted 4 lines
arch/x86/virt/svm/sev.c +0
-0
[ ... ]
arch/x86/virt/svm/sev.c static bool __init setup_segmented_rmptable(void) · line 500
Quoted 12 lines
arch/x86/virt/svm/sev.c +5
-1@@ -488,9 +491,13 @@ static bool __init setup_segmented_rmptable(void)static bool __init setup_rmptable (void ) {if (rmp_cfg &MSR_AMD64_SEG_RMP_ENABLED ) { -if (!setup_segmented_rmptable ()) +if (!setup_segmented_rmptable ()) { +setup_clear_cpu_cap (X86_FEATURE_RMPOPT );return false; + } }else { +/* Note that Segmented RMP must be enabled to enable RMPOPT. */ +setup_clear_cpu_cap (X86_FEATURE_RMPOPT );
[Severity: High]
Will calling setup_clear_cpu_cap() this late in the boot sequence effectively disable the feature?
Since setup_rmptable() and amd_cc_platform_clear() are invoked during late IOMMU initialization, this executes after check_bugs() has already permanently patched the alternative instructions.
While do_clear_cpu_cap() will trigger a kernel WARNING and update the in-memory boot_cpu_data array, it cannot undo the static code patching of static_cpu_has().
Quoted 3 lines
arch/x86/virt/svm/sev.c +0
-0if (!setup_contiguous_rmptable ())return false; }
[ ... ]
arch/x86/virt/svm/sev.c void snp_shutdown(void) · line 601
Quoted 7 lines
arch/x86/virt/svm/sev.c +7
-0+void snp_setup_rmpopt (void ) +{ +u64 rmpopt_base; +int cpu; + +if (!cpu_feature_enabled (X86_FEATURE_RMPOPT )) +return ;
[Severity: High]
If setup_clear_cpu_cap() failed to un-patch the alternatives earlier, will cpu_feature_enabled() incorrectly return true here even when falling back to Contiguous RMP?
Because cpu_feature_enabled() relies on static_cpu_has(), it seems it will execute the previously patched instruction and erroneously bypass this check.
Would this result in snp_setup_rmpopt() wrongly configuring MSR_AMD64_RMPOPT_BASE and dangerously enabling RMPOPT on unsupported configurations, risking CPU exceptions or memory corruption?
Could a global boolean flag be used to track the RMPOPT state instead of dynamically mutating a CPU feature after alternatives are patched?
Quoted 2 lines
arch/x86/virt/svm/sev.c +2
-0+ +cpus_read_lock ();
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
[email protected]
AI reviewRe: [PATCH v6 3/6] x86/sev: Add support to perform RMP optimizations asynchronously
In reply to Ashish KalraThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] Missing `cond_resched()` in the leader RMPOPT loops inside `rmpopt_work_handler` can trigger soft…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Missing cond_resched() in the leader RMPOPT loops inside rmpopt_work_handler can trigger soft lockups.
- [High] The inline assembly for __rmpopt() fails to mark the RAX register as clobbered or output, which can corrupt the caller's loop variable. --
commit dea16dc76b50a8acabc16915c0d451449f0d1849
Author: Ashish Kalra <[email protected]>
x86/sev: Add support to perform RMP optimizations asynchronously
This patch introduces support for the RMPOPT instruction to minimize RMP check overhead by skipping 1GB regions not containing SEV-SNP guest memory. It adds a dedicated workqueue to process up to 2TB of RAM asynchronously.
Quoted 4 lines
arch/x86/virt/svm/sev.c +0
-0
[ ... ]
arch/x86/virt/svm/sev.c void snp_shutdown(void) · line 623
Quoted 8 lines
arch/x86/virt/svm/sev.c +8
-0+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" );
[Severity: High]
Does this inline assembly need to mark the RAX register as an output or clobber?
Like other SEV-SNP instructions like PVALIDATE or RMPUPDATE, RMPOPT returns a status code in RAX. Since the inline assembly block does not mark RAX as modified by using "+a" (pa_start), the compiler is unaware that the register's value has changed.
When rmpopt is inlined into the scanning loop, the compiler might map the loop variable pa to RAX. If the hardware overwrites RAX with a status code, could this corrupt the memory scan logic by resetting the physical address being scanned?
Quoted 3 lines
arch/x86/virt/svm/sev.c +3
-0+ +return optimized; +}
[ ... ]
arch/x86/virt/svm/sev.c void snp_shutdown(void) · line 691
Quoted 9 lines
arch/x86/virt/svm/sev.c +9
-0+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);
[Severity: High]
Could this loop run for a long time without yielding and trigger soft lockups?
Since rmpopt_pa_end can be up to 2TB, this loop might execute up to 2048 times. The follower loop further down includes a cond_resched(), but this leader loop and the sibling thread leader loop below omit it entirely.
Quoted 22 lines
arch/x86/virt/svm/sev.c +22
-0+ }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); + }else { +/* +* Current CPU does not have RMPOPT_BASE MSR programmed. +* Pick an explicit leader from the cpumask to avoid #UD. +*/ +int leader_cpu =cpumask_first (follower_mask); + +if (WARN_ON_ONCE (leader_cpu >= nr_cpu_ids)) {
arch/x86/virt/svm/sev.c void snp_shutdown(void) · line 723
Quoted 10 lines
arch/x86/virt/svm/sev.c +10
-0+migrate_enable (); + goto out; + } + +cpumask_clear_cpu (leader_cpu, follower_mask); + +cpus_read_lock (); +for (pa = rmpopt_pa_start; pa < rmpopt_pa_end; pa +=SZ_1G ) +smp_call_function_single (leader_cpu, rmpopt_smp, + (void *)pa, true);
[Severity: High]
Does this loop also need to yield?
It sends an IPI and busy-waits with wait=true for the expensive scan up to 2048 times without yielding.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
[email protected]
AI reviewRe: [PATCH v6 4/6] x86/sev: Add interface to re-enable RMP optimizations.
In reply to Ashish KalraThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] The leader execution branches in rmpopt_work_handler lack cond_resched(), potentially causing softlockups on non-preemptible kernels during the expensive RMP scan. --
commit 083ce9f1c90cf08302bc96625ddfd8a7dbfd5a45
Author: Ashish Kalra <[email protected]>
x86/sev: Add interface to re-enable RMP optimizations.
This commit adds an interface to explicitly re-enable RMP optimizations. It allows the processor to recover optimizations that may have been cleared by events like RMPUPDATE by re-queuing the RMPOPT delayed work.
arch/x86/virt/svm/sev.c static void rmpopt_work_handler(struct work_struct *work) · line 742
Quoted 7 lines
arch/x86/virt/svm/sev.c +0
-0@@ -741,6 +741,21 @@ static void rmpopt_work_handler(struct work_struct *work)free_cpumask_var (follower_mask); }
[Severity: High]
This is a pre-existing issue, but since the new code provides an interface to explicitly re-queue this work, is there a risk of CPU stalls in the work handler?
In rmpopt_work_handler() in arch/x86/virt/svm/sev.c, the leader execution paths loop over up to 2TB of memory in 1GB chunks (up to 2048 iterations) without yielding:
for (pa = rmpopt_pa_start; pa < rmpopt_pa_end; pa += SZ_1G) rmpopt(pa);
It also happens in the IPI loop:
for (pa = rmpopt_pa_start; pa < rmpopt_pa_end; pa += SZ_1G) smp_call_function_single(leader_cpu, rmpopt_smp, (void *)pa, true);
Since RMPOPT is an expensive hardware instruction, could executing up to 2048 iterations sequentially without cond_resched() cause workqueue lockups and softlockup warnings on non-preemptible kernels?
Quoted 14 lines
arch/x86/virt/svm/sev.c +13
-0+void snp_rmpopt_all_physmem (void ) +{ +if (!cpu_feature_enabled (X86_FEATURE_RMPOPT )) +return ; + +guard (mutex)(&rmpopt_wq_mutex); + +if (!rmpopt_wq) +return ; + +queue_delayed_work (rmpopt_wq, &rmpopt_delayed_work, +msecs_to_jiffies (RMPOPT_WORK_TIMEOUT )); +}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4