Message ID | 20250123002422.1632517-2-kevinloughlin@google.com (mailing list archive) |
---|---|
State | New |
Headers | show |
Series | KVM: SEV: Prefer WBNOINVD over WBINVD for cache maintenance efficiency | expand |
On 1/22/25 16:24, Kevin Loughlin wrote: > +static __always_inline void wbnoinvd(void) > +{ > + /* > + * WBNOINVD is encoded as 0xf3 0x0f 0x09. Making this > + * encoding explicit ensures compatibility with older versions of > + * binutils, which may not know about WBNOINVD. This kinda pokes at one of my pet peeves. It's writing a comment where code would do. I'd *much* rather write a function that explains to you in code that "WBNOINVD is encoded as 0xf3 0x0f 0x09": static __always_inline void native_wbnoinvd(void) { asm volatile(".byte 0xf3,0x0f,0x09\n\t": : :"memory"); } instead of writing out a comment. It's kinda silly to have to write out the encoding explicitly in a comment and then have to rewrite it in the code.
On Wed, Jan 22, 2025 at 4:36 PM Dave Hansen <dave.hansen@intel.com> wrote: > > On 1/22/25 16:24, Kevin Loughlin wrote: > > +static __always_inline void wbnoinvd(void) > > +{ > > + /* > > + * WBNOINVD is encoded as 0xf3 0x0f 0x09. Making this > > + * encoding explicit ensures compatibility with older versions of > > + * binutils, which may not know about WBNOINVD. > > This kinda pokes at one of my pet peeves. It's writing a comment where > code would do. I'd *much* rather write a function that explains to you > in code that "WBNOINVD is encoded as 0xf3 0x0f 0x09": > > static __always_inline void native_wbnoinvd(void) > { > asm volatile(".byte 0xf3,0x0f,0x09\n\t": : :"memory"); > } > > instead of writing out a comment. It's kinda silly to have to write out > the encoding explicitly in a comment and then have to rewrite it in the > code. Yeah, I see your point. I will add this native_wbnoinvd() wrapper in v6; thanks!
diff --git a/arch/x86/include/asm/smp.h b/arch/x86/include/asm/smp.h index ca073f40698f..ecf93a243b83 100644 --- a/arch/x86/include/asm/smp.h +++ b/arch/x86/include/asm/smp.h @@ -112,6 +112,7 @@ void native_play_dead(void); void play_dead_common(void); void wbinvd_on_cpu(int cpu); int wbinvd_on_all_cpus(void); +int wbnoinvd_on_all_cpus(void); void smp_kick_mwait_play_dead(void); @@ -160,6 +161,12 @@ static inline int wbinvd_on_all_cpus(void) return 0; } +static inline int wbnoinvd_on_all_cpus(void) +{ + wbnoinvd(); + return 0; +} + static inline struct cpumask *cpu_llc_shared_mask(int cpu) { return (struct cpumask *)cpumask_of(0); diff --git a/arch/x86/include/asm/special_insns.h b/arch/x86/include/asm/special_insns.h index 03e7c2d49559..fb3ddb2ddea9 100644 --- a/arch/x86/include/asm/special_insns.h +++ b/arch/x86/include/asm/special_insns.h @@ -117,7 +117,25 @@ static inline void wrpkru(u32 pkru) static __always_inline void wbinvd(void) { - asm volatile("wbinvd": : :"memory"); + asm volatile("wbinvd" : : : "memory"); +} + +/* + * Cheaper version of wbinvd(). Call when caches + * need to be written back but not invalidated. + */ +static __always_inline void wbnoinvd(void) +{ + /* + * WBNOINVD is encoded as 0xf3 0x0f 0x09. Making this + * encoding explicit ensures compatibility with older versions of + * binutils, which may not know about WBNOINVD. + * + * If WBNOINVD is unavailable, fall back to the compatible but + * more destructive WBINVD (which still writes the caches back + * but also invalidates them). + */ + alternative("wbinvd", ".byte 0xf3,0x0f,0x09", X86_FEATURE_WBNOINVD); } static inline unsigned long __read_cr4(void) diff --git a/arch/x86/lib/cache-smp.c b/arch/x86/lib/cache-smp.c index 7af743bd3b13..7ac5cca53031 100644 --- a/arch/x86/lib/cache-smp.c +++ b/arch/x86/lib/cache-smp.c @@ -20,3 +20,15 @@ int wbinvd_on_all_cpus(void) return 0; } EXPORT_SYMBOL(wbinvd_on_all_cpus); + +static void __wbnoinvd(void *dummy) +{ + wbnoinvd(); +} + +int wbnoinvd_on_all_cpus(void) +{ + on_each_cpu(__wbnoinvd, NULL, 1); + return 0; +} +EXPORT_SYMBOL(wbnoinvd_on_all_cpus);
In line with WBINVD usage, add WBONINVD helper functions. For the wbnoinvd() helper, fall back to WBINVD if X86_FEATURE_WBNOINVD is not present. Signed-off-by: Kevin Loughlin <kevinloughlin@google.com> --- arch/x86/include/asm/smp.h | 7 +++++++ arch/x86/include/asm/special_insns.h | 20 +++++++++++++++++++- arch/x86/lib/cache-smp.c | 12 ++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-)