Skip to contentSkip to conversation

x86: In x86-64 barrier_nospec can always be lfence

PATCH New David Laight 1 participant 1 message Feb 9, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 x86: In x86-64 barrier_nospec can always be lfence

When barrier_nospec() was added the defintion was copied from the one used to synchronise rdtsc.

On very old cpu rdtsc was a synchronising instruction.
When this change X86_FEATURE_LFENCE_RDTSC (and a MFENCE copy) were (probably) added so lfence/mfence could be added to synchronise rdtsc. For old cpu (I think the code checks XMM2) no barrier was added.

I'm not sure why that code was used for barrier_nospec(). Or why rdtsc ended up being synchronised by barrier_nospec(). lfence is the right instruction (well as good as you get).

In any case all x86-64 cpu support XMM2 and lfence so there is to point using alternative().
Separate the 32bit and 64bit definitions but leave the barrier missing on old 32bit cpu.

v2: use a explicit lfence rather than __rmb().
Update commit message text w.r.t rdtsc.

---
 arch/x86/include/asm/barrier.h | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
arch/x86/include/asm/barrier.h +5 -1
index 7b44b3c4cce1..b9af75624cf5 100644
@@ -45,7 +45,11 @@
 	__mask; })
 
 /* Prevent speculative execution past this barrier. */
-#define barrier_nospec() alternative("", "lfence", X86_FEATURE_LFENCE_RDTSC)
+#ifdef CONFIG_X86_32
+#define barrier_nospec() alternative("", "lfence", X86_FEATURE_XMM2)
+#else
+#define barrier_nospec() asm volatile("lfence":::"memory")
+#endif
 
 #define __dma_rmb()	barrier()
 #define __dma_wmb()	barrier()
Signature · 1 line
2.39.5