public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH RESEND -tip v2] x86/asm: Improve clflush alternatives assembler template
@ 2026-05-05  9:50 Uros Bizjak
  0 siblings, 0 replies; only message in thread
From: Uros Bizjak @ 2026-05-05  9:50 UTC (permalink / raw)
  To: x86, linux-kernel
  Cc: Uros Bizjak, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen, H. Peter Anvin, David Laight

The inline asm used with alternative_input() specifies the address
operand for clflush with the "a" input operand constraint and an
explicit "(%[addr])" dereference:

    "clflush (%[addr])", [addr] "a" (addr)

This forces the pointer into %rax and manually encodes the memory
operand in the template. Instead, use the %a address operand
modifier and relax the constraint from "a" to "r":

    "clflush %a[addr]", [addr] "r" (addr)

This lets the compiler choose the register used for the address and
formats the operand as a memory reference. Add a "memory" clobber
to ensure the flush is not reordered across memory operations.

Signed-off-by: Uros Bizjak <ubizjak@gmail.com>
Cc: Thomas Gleixner <tglx@kernel.org>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: David Laight <david.laight.linux@gmail.com>
---
v2: Add "memory" clobber to ensure flush is not reordered across
    memory operations.
---
 arch/x86/include/asm/mwait.h | 3 ++-
 arch/x86/kernel/process.c    | 3 ++-
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/arch/x86/include/asm/mwait.h b/arch/x86/include/asm/mwait.h
index e4815e15dc9a..644ada6c0b3b 100644
--- a/arch/x86/include/asm/mwait.h
+++ b/arch/x86/include/asm/mwait.h
@@ -116,7 +116,8 @@ static __always_inline void mwait_idle_with_hints(u32 eax, u32 ecx)
 	if (static_cpu_has_bug(X86_BUG_MONITOR) || !current_set_polling_and_test()) {
 		const void *addr = &current_thread_info()->flags;
 
-		alternative_input("", "clflush (%[addr])", X86_BUG_CLFLUSH_MONITOR, [addr] "a" (addr));
+		alternative_input("", "clflush %a[addr]", X86_BUG_CLFLUSH_MONITOR,
+				  [addr] "r" (addr) : "memory");
 		__monitor(addr, 0, 0);
 
 		if (need_resched())
diff --git a/arch/x86/kernel/process.c b/arch/x86/kernel/process.c
index 4c718f8adc59..63581789f7e7 100644
--- a/arch/x86/kernel/process.c
+++ b/arch/x86/kernel/process.c
@@ -921,7 +921,8 @@ static __cpuidle void mwait_idle(void)
 	if (!current_set_polling_and_test()) {
 		const void *addr = &current_thread_info()->flags;
 
-		alternative_input("", "clflush (%[addr])", X86_BUG_CLFLUSH_MONITOR, [addr] "a" (addr));
+		alternative_input("", "clflush %a[addr]", X86_BUG_CLFLUSH_MONITOR,
+				  [addr] "r" (addr) : "memory");
 		__monitor(addr, 0, 0);
 		if (need_resched())
 			goto out;
-- 
2.54.0


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-05-05  9:51 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-05  9:50 [PATCH RESEND -tip v2] x86/asm: Improve clflush alternatives assembler template Uros Bizjak

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox