linux-arch.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/1] x86: In x86-64 barrier_nospec can always be lfence
@ 2025-02-09 19:10 David Laight
  2025-02-09 19:32 ` Linus Torvalds
  0 siblings, 1 reply; 7+ messages in thread
From: David Laight @ 2025-02-09 19:10 UTC (permalink / raw)
  To: x86, linux-kernel, Linus Torvalds
  Cc: David Laight, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen, H. Peter Anvin, Catalin Marinas, Mathieu Desnoyers,
	Josh Poimboeuf, Andi Kleen, Dan Williams, linux-arch, Kees Cook,
	kernel-hardening

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 lflence/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().
I'm sure it should actually be rmb() with the fallback to a
locked memory access on old cpu.

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.

Signed-off-by: David Laight <david.laight.linux@gmail.com>
---
 arch/x86/include/asm/barrier.h | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/arch/x86/include/asm/barrier.h b/arch/x86/include/asm/barrier.h
index 7b44b3c4cce1..7eecce9bf4fe 100644
--- a/arch/x86/include/asm/barrier.h
+++ b/arch/x86/include/asm/barrier.h
@@ -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() __rmb()
+#endif
 
 #define __dma_rmb()	barrier()
 #define __dma_wmb()	barrier()
-- 
2.39.5


^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2025-02-10  4:29 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-02-09 19:10 [PATCH 1/1] x86: In x86-64 barrier_nospec can always be lfence David Laight
2025-02-09 19:32 ` Linus Torvalds
2025-02-09 21:40   ` David Laight
2025-02-09 21:57     ` Linus Torvalds
2025-02-10  1:09       ` Rik van Riel
2025-02-10  2:15         ` H. Peter Anvin
2025-02-10  4:29       ` Andi Kleen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).