Skip to contentSkip to conversation

x86/asm: Improve clflush alternatives assembler template

PATCH RESEND New Uros Bizjak 1 participant 1 message May 5 x86

lore
Share & tools
lore
#0 lore raw

PATCH RESEND v2 1/1 x86/asm: Improve clflush alternatives assembler template

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.

Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Dave Hansen <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc David Laight <[email protected]>

---
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(-)
arch/x86/include/asm/mwait.h +2 -1
index e4815e15dc9a..644ada6c0b3b 100644
@@ -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())
arch/x86/kernel/process.c +2 -1
index 4c718f8adc59..63581789f7e7 100644
@@ -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;
Signature · 1 line
2.54.0