Skip to contentSkip to conversation

x86/idle: Fix argument type for MONITOR{,X} and MWAIT{,X} instructions

PATCH Applied Uros Bizjak 5 participants 18 messages Apr 2, 2025 – Apr 3, 2025 x86

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 x86/idle: Fix argument type for MONITOR{,X} and MWAIT{,X} instructions

MONITOR and MONITORX expect 32-bit unsigned integer argument in %ecx and %edx registers. MWAIT and MWAITX expect 32-bit usigned int argument in %eax and %ecx registers.

Fix the wrong argument type by changing the type of the corresponding function argument from "unsigned long" to u32.

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]>
---
 arch/x86/include/asm/mwait.h | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)
arch/x86/include/asm/mwait.h +5 -8
index 54dc313bcdf0..3377869ff2e8 100644
@@ -25,23 +25,21 @@
 #define TPAUSE_C01_STATE		1
 #define TPAUSE_C02_STATE		0
 
-static __always_inline void __monitor(const void *eax, unsigned long ecx,
-			     unsigned long edx)
+static __always_inline void __monitor(const void *eax, u32 ecx, u32 edx)
 {
 	/* "monitor %eax, %ecx, %edx;" */
 	asm volatile(".byte 0x0f, 0x01, 0xc8;"
 		     :: "a" (eax), "c" (ecx), "d"(edx));
 }
 
-static __always_inline void __monitorx(const void *eax, unsigned long ecx,
-			      unsigned long edx)
+static __always_inline void __monitorx(const void *eax, u32 ecx, u32 edx)
 {
 	/* "monitorx %eax, %ecx, %edx;" */
 	asm volatile(".byte 0x0f, 0x01, 0xfa;"
 		     :: "a" (eax), "c" (ecx), "d"(edx));
 }
 
-static __always_inline void __mwait(unsigned long eax, unsigned long ecx)
+static __always_inline void __mwait(u32 eax, u32 ecx)
 {
 	mds_idle_clear_cpu_buffers();
 
@@ -76,8 +74,7 @@ static __always_inline void __mwait(unsigned long eax, unsigned long ecx)
  * EAX                     (logical) address to monitor
  * ECX                     #GP if not zero
  */
-static __always_inline void __mwaitx(unsigned long eax, unsigned long ebx,
-				     unsigned long ecx)
+static __always_inline void __mwaitx(u32 eax, u32 ebx, u32 ecx)
 {
 	/* No MDS buffer clear as this is AMD/HYGON only */
 
@@ -95,7 +92,7 @@ static __always_inline void __mwaitx(unsigned long eax, unsigned long ebx,
  * executing mwait, it would otherwise go unnoticed and the next tick
  * would not be reprogrammed accordingly before mwait ever wakes up.
  */
-static __always_inline void __sti_mwait(unsigned long eax, unsigned long ecx)
+static __always_inline void __sti_mwait(u32 eax, u32 ecx)
 {
 	mds_idle_clear_cpu_buffers();
 	/* "mwait %eax, %ecx;" */
Signature · 1 line
2.42.0
#1 lore raw

PATCH 2/4 x86/idle: Use MONITOR and MWAIT mnemonics in <asm/mwait.h>

Current minimum required version of binutils is 2.25, which supports MONITOR and MWAIT instruction mnemonics.

Replace the byte-wise specification of MONITOR and MWAIT with these proper mnemonics.

No functional change intended.

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]>
---
 arch/x86/include/asm/mwait.h | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
arch/x86/include/asm/mwait.h +4 -9
index 3377869ff2e8..006b150bcb5f 100644
@@ -27,9 +27,7 @@
 
 static __always_inline void __monitor(const void *eax, u32 ecx, u32 edx)
 {
-	/* "monitor %eax, %ecx, %edx;" */
-	asm volatile(".byte 0x0f, 0x01, 0xc8;"
-		     :: "a" (eax), "c" (ecx), "d"(edx));
+	asm volatile("monitor %0, %1, %2" :: "a" (eax), "c" (ecx), "d" (edx));
 }
 
 static __always_inline void __monitorx(const void *eax, u32 ecx, u32 edx)
@@ -43,9 +41,7 @@ static __always_inline void __mwait(u32 eax, u32 ecx)
 {
 	mds_idle_clear_cpu_buffers();
 
-	/* "mwait %eax, %ecx;" */
-	asm volatile(".byte 0x0f, 0x01, 0xc9;"
-		     :: "a" (eax), "c" (ecx));
+	asm volatile("mwait %0, %1" :: "a" (eax), "c" (ecx));
 }
 
 /*
@@ -95,9 +91,8 @@ static __always_inline void __mwaitx(u32 eax, u32 ebx, u32 ecx)
 static __always_inline void __sti_mwait(u32 eax, u32 ecx)
 {
 	mds_idle_clear_cpu_buffers();
-	/* "mwait %eax, %ecx;" */
-	asm volatile("sti; .byte 0x0f, 0x01, 0xc9;"
-		     :: "a" (eax), "c" (ecx));
+
+	asm volatile("sti; mwait %0, %1" :: "a" (eax), "c" (ecx));
 }
 
 /*
Signature · 1 line
2.42.0
merge bot Applied
#2 lore raw

[tip: x86/mm] x86/idle: Use MONITOR and MWAIT mnemonics in <asm/mwait.h>

The following commit has been merged into the x86/mm branch of tip:

The following commit has been merged into the x86/mm branch of tip:

Commit-ID:     cd3b85b27542968198e3d588a2bc0591930ee2ee
Gitweb:        https://git.kernel.org/tip/cd3b85b27542968198e3d588a2bc0591930ee2ee
Author:        Uros Bizjak <[email protected]>
AuthorDate:    Wed, 02 Apr 2025 20:08:06 +02:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Wed, 02 Apr 2025 22:26:17 +02:00

x86/idle: Use MONITOR and MWAIT mnemonics in <asm/mwait.h>

Current minimum required version of binutils is 2.25, which supports MONITOR and MWAIT instruction mnemonics.

Replace the byte-wise specification of MONITOR and MWAIT with these proper mnemonics.

No functional change intended.

Cc Andy Lutomirski <[email protected]>
Cc Brian Gerst <[email protected]>
Cc Juergen Gross <[email protected]>
Cc Andrew Cooper <[email protected]>
Cc Rik van Riel <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Rafael J. Wysocki <[email protected]>
Cc Linus Torvalds <[email protected]>
---
 arch/x86/include/asm/mwait.h | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
arch/x86/include/asm/mwait.h +4 -9
index 3377869..006b150 100644
@@ -27,9 +27,7 @@
 
 static __always_inline void __monitor(const void *eax, u32 ecx, u32 edx)
 {
-	/* "monitor %eax, %ecx, %edx;" */
-	asm volatile(".byte 0x0f, 0x01, 0xc8;"
-		     :: "a" (eax), "c" (ecx), "d"(edx));
+	asm volatile("monitor %0, %1, %2" :: "a" (eax), "c" (ecx), "d" (edx));
 }
 
 static __always_inline void __monitorx(const void *eax, u32 ecx, u32 edx)
@@ -43,9 +41,7 @@ static __always_inline void __mwait(u32 eax, u32 ecx)
 {
 	mds_idle_clear_cpu_buffers();
 
-	/* "mwait %eax, %ecx;" */
-	asm volatile(".byte 0x0f, 0x01, 0xc9;"
-		     :: "a" (eax), "c" (ecx));
+	asm volatile("mwait %0, %1" :: "a" (eax), "c" (ecx));
 }
 
 /*
@@ -95,9 +91,8 @@ static __always_inline void __mwaitx(u32 eax, u32 ebx, u32 ecx)
 static __always_inline void __sti_mwait(u32 eax, u32 ecx)
 {
 	mds_idle_clear_cpu_buffers();
-	/* "mwait %eax, %ecx;" */
-	asm volatile("sti; .byte 0x0f, 0x01, 0xc9;"
-		     :: "a" (eax), "c" (ecx));
+
+	asm volatile("sti; mwait %0, %1" :: "a" (eax), "c" (ecx));
 }
 
 /*
#3 lore raw

Re: [PATCH -tip 2/4] x86/idle: Use MONITOR and MWAIT mnemonics in <asm/mwait.h>

In reply to Uros Bizjak
Quoted 7 lines · Uros Bizjak

Current minimum required version of binutils is 2.25, which supports MONITOR and MWAIT instruction mnemonics.

Replace the byte-wise specification of MONITOR and MWAIT with these proper mnemonics.

No functional change intended.

Hmm... Is it only me who gets these, please?

In file included from acpi/cstate.c:18:
asm/mwait.h:30:15: error: invalid operand for instruction
30 | asm volatile("monitor %0, %1, %2" :: "a" (eax), "c" (ecx), "d" (edx));
| ^
<inline asm>:1:16: note: instantiated into assembly here
1 | monitor %rax, %ecx, %edx
| ^~~~~
In file included from acpi/cstate.c:18:
asm/mwait.h:95:15: error: instruction requires: Not 64-bit mode
95 | asm volatile("sti; mwait %0, %1" :: "a" (eax), "c" (ecx));
| ^
<inline asm>:1:7: note: instantiated into assembly here
1 | sti; mwait %eax, %ecx
| ^

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw

Re: [PATCH -tip 2/4] x86/idle: Use MONITOR and MWAIT mnemonics in <asm/mwait.h>

Quoted 11 lines · Andy Shevchenko
Quoted 7 lines · Uros Bizjak

Current minimum required version of binutils is 2.25, which supports MONITOR and MWAIT instruction mnemonics.

Replace the byte-wise specification of MONITOR and MWAIT with these proper mnemonics.

No functional change intended.

Hmm... Is it only me who gets these, please?

Quoted 14 lines

In file included from acpi/cstate.c:18:
asm/mwait.h:30:15: error: invalid operand for instruction
30 | asm volatile("monitor %0, %1, %2" :: "a" (eax), "c" (ecx), "d" (edx));
| ^
<inline asm>:1:16: note: instantiated into assembly here
1 | monitor %rax, %ecx, %edx
| ^~~~~
In file included from acpi/cstate.c:18:
asm/mwait.h:95:15: error: instruction requires: Not 64-bit mode
95 | asm volatile("sti; mwait %0, %1" :: "a" (eax), "c" (ecx));
| ^
<inline asm>:1:7: note: instantiated into assembly here
1 | sti; mwait %eax, %ecx
| ^

FWIW, revert fixes the issue.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw

Re: [PATCH -tip 2/4] x86/idle: Use MONITOR and MWAIT mnemonics in <asm/mwait.h>

Quoted 27 lines · Andy Shevchenko
Quoted 11 lines · Andy Shevchenko
Quoted 7 lines · Uros Bizjak

Current minimum required version of binutils is 2.25, which supports MONITOR and MWAIT instruction mnemonics.

Replace the byte-wise specification of MONITOR and MWAIT with these proper mnemonics.

No functional change intended.

Hmm... Is it only me who gets these, please?

Quoted 14 lines

In file included from acpi/cstate.c:18:
asm/mwait.h:30:15: error: invalid operand for instruction
30 | asm volatile("monitor %0, %1, %2" :: "a" (eax), "c" (ecx), "d" (edx));
| ^
<inline asm>:1:16: note: instantiated into assembly here
1 | monitor %rax, %ecx, %edx
| ^~~~~
In file included from acpi/cstate.c:18:
asm/mwait.h:95:15: error: instruction requires: Not 64-bit mode
95 | asm volatile("sti; mwait %0, %1" :: "a" (eax), "c" (ecx));
| ^
<inline asm>:1:7: note: instantiated into assembly here
1 | sti; mwait %eax, %ecx
| ^

Quoted 1 line

FWIW, revert fixes the issue.

All gone already, tomorrow's -next should be fine.

Thanks,

Ingo

#6 lore raw

Re: [PATCH -tip 2/4] x86/idle: Use MONITOR and MWAIT mnemonics in <asm/mwait.h>

Quoted 3 lines · Ingo Molnar
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Andy Shevchenko

On Wed, Apr 02, 2025 at 08:08:06PM +0200, Uros Bizjak wrote:

...

Quoted 19 lines
Quoted 19 lines
Quoted 18 lines
Quoted 1 line

No functional change intended.

Hmm... Is it only me who gets these, please?

In file included from acpi/cstate.c:18:
asm/mwait.h:30:15: error: invalid operand for instruction
30 | asm volatile("monitor %0, %1, %2" :: "a" (eax), "c" (ecx), "d" (edx));
| ^
<inline asm>:1:16: note: instantiated into assembly here
1 | monitor %rax, %ecx, %edx
| ^~~~~
In file included from acpi/cstate.c:18:
asm/mwait.h:95:15: error: instruction requires: Not 64-bit mode
95 | asm volatile("sti; mwait %0, %1" :: "a" (eax), "c" (ecx));
| ^
<inline asm>:1:7: note: instantiated into assembly here
1 | sti; mwait %eax, %ecx
| ^

Quoted 3 lines
Quoted 1 line

FWIW, revert fixes the issue.

All gone already, tomorrow's -next should be fine.

Thanks!

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#7 lore raw

PATCH 3/4 x86/idle: Remove unneeded delimiters from asm templates

In reply to Uros Bizjak

Delimiters at the end of asm templates are not needed. Remove them to make asm instruction length calculations more accurate.

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]>
---
 arch/x86/include/asm/mwait.h | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
arch/x86/include/asm/mwait.h +7 -7
index 006b150bcb5f..65228861b894 100644
@@ -32,8 +32,8 @@ static __always_inline void __monitor(const void *eax, u32 ecx, u32 edx)
 
 static __always_inline void __monitorx(const void *eax, u32 ecx, u32 edx)
 {
-	/* "monitorx %eax, %ecx, %edx;" */
-	asm volatile(".byte 0x0f, 0x01, 0xfa;"
+	/* "monitorx %eax, %ecx, %edx" */
+	asm volatile(".byte 0x0f, 0x01, 0xfa"
 		     :: "a" (eax), "c" (ecx), "d"(edx));
 }
 
@@ -74,8 +74,8 @@ static __always_inline void __mwaitx(u32 eax, u32 ebx, u32 ecx)
 {
 	/* No MDS buffer clear as this is AMD/HYGON only */
 
-	/* "mwaitx %eax, %ebx, %ecx;" */
-	asm volatile(".byte 0x0f, 0x01, 0xfb;"
+	/* "mwaitx %eax, %ebx, %ecx" */
+	asm volatile(".byte 0x0f, 0x01, 0xfb"
 		     :: "a" (eax), "b" (ebx), "c" (ecx));
 }
 
@@ -133,13 +133,13 @@ static __always_inline void mwait_idle_with_hints(unsigned long eax, unsigned lo
  */
 static inline void __tpause(u32 ecx, u32 edx, u32 eax)
 {
-	/* "tpause %ecx, %edx, %eax;" */
+	/* "tpause %ecx, %edx, %eax" */
 	#ifdef CONFIG_AS_TPAUSE
-	asm volatile("tpause %%ecx\n"
+	asm volatile("tpause %%ecx"
 		     :
 		     : "c"(ecx), "d"(edx), "a"(eax));
 	#else
-	asm volatile(".byte 0x66, 0x0f, 0xae, 0xf1\t\n"
+	asm volatile(".byte 0x66, 0x0f, 0xae, 0xf1"
 		     :
 		     : "c"(ecx), "d"(edx), "a"(eax));
 	#endif
Signature · 1 line
2.42.0
merge bot Applied
#8 lore raw

[tip: x86/mm] x86/idle: Remove .s output beautifying delimiters from simpler asm() templates

The following commit has been merged into the x86/mm branch of tip:

The following commit has been merged into the x86/mm branch of tip:

Commit-ID:     8ad521aaf7438e4fae996f51d02b960cc97a15a3
Gitweb:        https://git.kernel.org/tip/8ad521aaf7438e4fae996f51d02b960cc97a15a3
Author:        Uros Bizjak <[email protected]>
AuthorDate:    Wed, 02 Apr 2025 20:08:07 +02:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Wed, 02 Apr 2025 22:36:57 +02:00

x86/idle: Remove .s output beautifying delimiters from simpler asm() templates

Delimiters in asm() templates such as ';', '\t' or '\n' are not required syntactically, they were used historically in the Linux kernel to prettify the compiler's .s output for people who were looking at compiler generated .s output.

Most x86 developers these days are primarily looking at:

1) objdump --disassemble-all .o

2) perf top's live kernel function annotation and disassembler feature that uses /dev/mem.

... because:

- this kind of assembler output is standardized regardless of compiler used,

- it's generally less messy looking,

- it gives ground-truth instead of being some intermediate layer in the toolchain that might or might not be the real deal,

- and on a live kernel it also sees through the kernel's various layers of runtime patching code obfuscation facilities, also known as: alternative-instructions, tracepoints and jump labels.

There are some cases where the .s output is the most useful tool, such as alternatives() code generation, but other than that these delimiters used in simple asm() statements mostly add noise to the source code side, which isn't desirable for assembly code that is fragile enough already.

Remove the delimiters for <asm/mwait.h>, which also happens to make the GCC inliner's asm() instruction length heuristics more accurate...

[ mingo: Wrote a new changelog to give historic context and
to give people a chance to object. :-) ]

Cc Andy Lutomirski <[email protected]>
Cc Brian Gerst <[email protected]>
Cc Juergen Gross <[email protected]>
Cc Andrew Cooper <[email protected]>
Cc Rik van Riel <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Rafael J. Wysocki <[email protected]>
Cc Linus Torvalds <[email protected]>
---
 arch/x86/include/asm/mwait.h | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
arch/x86/include/asm/mwait.h +7 -7
index 006b150..6522886 100644
@@ -32,8 +32,8 @@ static __always_inline void __monitor(const void *eax, u32 ecx, u32 edx)
 
 static __always_inline void __monitorx(const void *eax, u32 ecx, u32 edx)
 {
-	/* "monitorx %eax, %ecx, %edx;" */
-	asm volatile(".byte 0x0f, 0x01, 0xfa;"
+	/* "monitorx %eax, %ecx, %edx" */
+	asm volatile(".byte 0x0f, 0x01, 0xfa"
 		     :: "a" (eax), "c" (ecx), "d"(edx));
 }
 
@@ -74,8 +74,8 @@ static __always_inline void __mwaitx(u32 eax, u32 ebx, u32 ecx)
 {
 	/* No MDS buffer clear as this is AMD/HYGON only */
 
-	/* "mwaitx %eax, %ebx, %ecx;" */
-	asm volatile(".byte 0x0f, 0x01, 0xfb;"
+	/* "mwaitx %eax, %ebx, %ecx" */
+	asm volatile(".byte 0x0f, 0x01, 0xfb"
 		     :: "a" (eax), "b" (ebx), "c" (ecx));
 }
 
@@ -133,13 +133,13 @@ static __always_inline void mwait_idle_with_hints(unsigned long eax, unsigned lo
  */
 static inline void __tpause(u32 ecx, u32 edx, u32 eax)
 {
-	/* "tpause %ecx, %edx, %eax;" */
+	/* "tpause %ecx, %edx, %eax" */
 	#ifdef CONFIG_AS_TPAUSE
-	asm volatile("tpause %%ecx\n"
+	asm volatile("tpause %%ecx"
 		     :
 		     : "c"(ecx), "d"(edx), "a"(eax));
 	#else
-	asm volatile(".byte 0x66, 0x0f, 0xae, 0xf1\t\n"
+	asm volatile(".byte 0x66, 0x0f, 0xae, 0xf1"
 		     :
 		     : "c"(ecx), "d"(edx), "a"(eax));
 	#endif
merge bot Applied
#9 lore raw

[tip: x86/mm] x86/idle: Remove .s output beautifying delimiters from simpler asm() templates

In reply to Uros Bizjak
The following commit has been merged into the x86/mm branch of tip:

The following commit has been merged into the x86/mm branch of tip:

Commit-ID:     19c3dcd953bc8961ab486cf05f5dd45455393c42
Gitweb:        https://git.kernel.org/tip/19c3dcd953bc8961ab486cf05f5dd45455393c42
Author:        Uros Bizjak <[email protected]>
AuthorDate:    Wed, 02 Apr 2025 20:08:07 +02:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Thu, 03 Apr 2025 13:19:18 +02:00

x86/idle: Remove .s output beautifying delimiters from simpler asm() templates

Delimiters in asm() templates such as ';', '\t' or '\n' are not required syntactically, they were used historically in the Linux kernel to prettify the compiler's .s output for people who were looking at compiler generated .s output.

Most x86 developers these days are primarily looking at:

1) objdump --disassemble-all .o

2) perf top's live kernel function annotation and disassembler feature that uses /dev/mem.

... because:

- this kind of assembler output is standardized regardless of compiler used,

- it's generally less messy looking,

- it gives ground-truth instead of being some intermediate layer in the toolchain that might or might not be the real deal,

- and on a live kernel it also sees through the kernel's various layers of runtime patching code obfuscation facilities, also known as: alternative-instructions, tracepoints and jump labels.

There are some cases where the .s output is the most useful tool, such as alternatives() code generation, but other than that these delimiters used in simple asm() statements mostly add noise to the source code side, which isn't desirable for assembly code that is fragile enough already.

Remove the delimiters for <asm/mwait.h>, which also happens to make the GCC inliner's asm() instruction length heuristics more accurate...

[ mingo: Wrote a new changelog to give historic context and
to give people a chance to object. :-) ]

Cc Andy Lutomirski <[email protected]>
Cc Brian Gerst <[email protected]>
Cc Juergen Gross <[email protected]>
Cc Andrew Cooper <[email protected]>
Cc Rik van Riel <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Rafael J. Wysocki <[email protected]>
Cc Linus Torvalds <[email protected]>
---
 arch/x86/include/asm/mwait.h | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
arch/x86/include/asm/mwait.h +7 -7
index 3377869..0e020a6 100644
@@ -34,8 +34,8 @@ static __always_inline void __monitor(const void *eax, u32 ecx, u32 edx)
 
 static __always_inline void __monitorx(const void *eax, u32 ecx, u32 edx)
 {
-	/* "monitorx %eax, %ecx, %edx;" */
-	asm volatile(".byte 0x0f, 0x01, 0xfa;"
+	/* "monitorx %eax, %ecx, %edx" */
+	asm volatile(".byte 0x0f, 0x01, 0xfa"
 		     :: "a" (eax), "c" (ecx), "d"(edx));
 }
 
@@ -78,8 +78,8 @@ static __always_inline void __mwaitx(u32 eax, u32 ebx, u32 ecx)
 {
 	/* No MDS buffer clear as this is AMD/HYGON only */
 
-	/* "mwaitx %eax, %ebx, %ecx;" */
-	asm volatile(".byte 0x0f, 0x01, 0xfb;"
+	/* "mwaitx %eax, %ebx, %ecx" */
+	asm volatile(".byte 0x0f, 0x01, 0xfb"
 		     :: "a" (eax), "b" (ebx), "c" (ecx));
 }
 
@@ -138,13 +138,13 @@ static __always_inline void mwait_idle_with_hints(unsigned long eax, unsigned lo
  */
 static inline void __tpause(u32 ecx, u32 edx, u32 eax)
 {
-	/* "tpause %ecx, %edx, %eax;" */
+	/* "tpause %ecx, %edx, %eax" */
 	#ifdef CONFIG_AS_TPAUSE
-	asm volatile("tpause %%ecx\n"
+	asm volatile("tpause %%ecx"
 		     :
 		     : "c"(ecx), "d"(edx), "a"(eax));
 	#else
-	asm volatile(".byte 0x66, 0x0f, 0xae, 0xf1\t\n"
+	asm volatile(".byte 0x66, 0x0f, 0xae, 0xf1"
 		     :
 		     : "c"(ecx), "d"(edx), "a"(eax));
 	#endif
#10 lore raw

PATCH 4/4 x86/idle: Remove CONFIG_AS_TPAUSE

In reply to Uros Bizjak

There is no point to have CONFIG_AS_TPAUSE when the emitted assembly is always the same. Remove TPAUSE insn mnemonic and leave only the equivalent byte-wise definition. This will be changed back to insn mnemonic once binutils 2.31.1 is the minimum version to build the kernel.

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]>
---
 arch/x86/Kconfig.assembler   |  4 ----
 arch/x86/include/asm/mwait.h | 11 ++---------
 2 files changed, 2 insertions(+), 13 deletions(-)
arch/x86/Kconfig.assembler +0 -4
index 6d20a6ce0507..fa8858546d5e 100644
@@ -15,10 +15,6 @@ config AS_SHA256_NI
 	def_bool $(as-instr,sha256msg1 %xmm0$(comma)%xmm1)
 	help
 	  Supported by binutils >= 2.24 and LLVM integrated assembler
-config AS_TPAUSE
-	def_bool $(as-instr,tpause %ecx)
-	help
-	  Supported by binutils >= 2.31.1 and LLVM integrated assembler >= V7
 
 config AS_GFNI
 	def_bool $(as-instr,vgf2p8mulb %xmm0$(comma)%xmm1$(comma)%xmm2)
arch/x86/include/asm/mwait.h +2 -9
index 65228861b894..5141d2acc0ef 100644
@@ -133,16 +133,9 @@ static __always_inline void mwait_idle_with_hints(unsigned long eax, unsigned lo
  */
 static inline void __tpause(u32 ecx, u32 edx, u32 eax)
 {
-	/* "tpause %ecx, %edx, %eax" */
-	#ifdef CONFIG_AS_TPAUSE
-	asm volatile("tpause %%ecx"
-		     :
-		     : "c"(ecx), "d"(edx), "a"(eax));
-	#else
+	/* "tpause %ecx" */
 	asm volatile(".byte 0x66, 0x0f, 0xae, 0xf1"
-		     :
-		     : "c"(ecx), "d"(edx), "a"(eax));
-	#endif
+		     :: "c" (ecx), "d" (edx), "a" (eax));
 }
 
 #endif /* _ASM_X86_MWAIT_H */
Signature · 1 line
2.42.0
merge bot Applied
#11 lore raw

[tip: x86/mm] x86/idle: Remove CONFIG_AS_TPAUSE

The following commit has been merged into the x86/mm branch of tip:

The following commit has been merged into the x86/mm branch of tip:

Commit-ID:     cdc018e6b1c403b6a94416acea4649f9778c68c0
Gitweb:        https://git.kernel.org/tip/cdc018e6b1c403b6a94416acea4649f9778c68c0
Author:        Uros Bizjak <[email protected]>
AuthorDate:    Wed, 02 Apr 2025 20:08:08 +02:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Wed, 02 Apr 2025 22:37:09 +02:00

x86/idle: Remove CONFIG_AS_TPAUSE

There is not much point in CONFIG_AS_TPAUSE at all when the emittedu assembly is always the same - it only obfuscates the __tpause() code in essence.

Remove the TPAUSE insn mnemonic from __tpause() and leave only the equivalent byte-wise definition. This can then be changed back to insn mnemonic once binutils 2.31.1 is the minimum version to build the kernel. (Right now it's 2.25.)

Cc Andy Lutomirski <[email protected]>
Cc Brian Gerst <[email protected]>
Cc Juergen Gross <[email protected]>
Cc Andrew Cooper <[email protected]>
Cc Rik van Riel <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Rafael J. Wysocki <[email protected]>
Cc Linus Torvalds <[email protected]>
---
 arch/x86/Kconfig.assembler   |  4 ----
 arch/x86/include/asm/mwait.h | 11 ++---------
 2 files changed, 2 insertions(+), 13 deletions(-)
arch/x86/Kconfig.assembler +0 -4
index 6d20a6c..fa88585 100644
@@ -15,10 +15,6 @@ config AS_SHA256_NI
 	def_bool $(as-instr,sha256msg1 %xmm0$(comma)%xmm1)
 	help
 	  Supported by binutils >= 2.24 and LLVM integrated assembler
-config AS_TPAUSE
-	def_bool $(as-instr,tpause %ecx)
-	help
-	  Supported by binutils >= 2.31.1 and LLVM integrated assembler >= V7
 
 config AS_GFNI
 	def_bool $(as-instr,vgf2p8mulb %xmm0$(comma)%xmm1$(comma)%xmm2)
arch/x86/include/asm/mwait.h +2 -9
index 6522886..5141d2a 100644
@@ -133,16 +133,9 @@ static __always_inline void mwait_idle_with_hints(unsigned long eax, unsigned lo
  */
 static inline void __tpause(u32 ecx, u32 edx, u32 eax)
 {
-	/* "tpause %ecx, %edx, %eax" */
-	#ifdef CONFIG_AS_TPAUSE
-	asm volatile("tpause %%ecx"
-		     :
-		     : "c"(ecx), "d"(edx), "a"(eax));
-	#else
+	/* "tpause %ecx" */
 	asm volatile(".byte 0x66, 0x0f, 0xae, 0xf1"
-		     :
-		     : "c"(ecx), "d"(edx), "a"(eax));
-	#endif
+		     :: "c" (ecx), "d" (edx), "a" (eax));
 }
 
 #endif /* _ASM_X86_MWAIT_H */
merge bot Applied
#12 lore raw

[tip: x86/mm] x86/idle: Remove CONFIG_AS_TPAUSE

In reply to Uros Bizjak
The following commit has been merged into the x86/mm branch of tip:

The following commit has been merged into the x86/mm branch of tip:

Commit-ID:     6c921984e75c78e1c509d99b8cde04f1eadd46fb
Gitweb:        https://git.kernel.org/tip/6c921984e75c78e1c509d99b8cde04f1eadd46fb
Author:        Uros Bizjak <[email protected]>
AuthorDate:    Wed, 02 Apr 2025 20:08:08 +02:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Wed, 02 Apr 2025 22:44:55 +02:00

x86/idle: Remove CONFIG_AS_TPAUSE

There is not much point in CONFIG_AS_TPAUSE at all when the emitted assembly is always the same - it only obfuscates the __tpause() code in essence.

Remove the TPAUSE insn mnemonic from __tpause() and leave only the equivalent byte-wise definition. This can then be changed back to insn mnemonic once binutils 2.31.1 is the minimum version to build the kernel. (Right now it's 2.25.)

Cc Andy Lutomirski <[email protected]>
Cc Brian Gerst <[email protected]>
Cc Juergen Gross <[email protected]>
Cc Andrew Cooper <[email protected]>
Cc Rik van Riel <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Rafael J. Wysocki <[email protected]>
Cc Linus Torvalds <[email protected]>
---
 arch/x86/Kconfig.assembler   |  4 ----
 arch/x86/include/asm/mwait.h | 11 ++---------
 2 files changed, 2 insertions(+), 13 deletions(-)
arch/x86/Kconfig.assembler +0 -4
index 6d20a6c..fa88585 100644
@@ -15,10 +15,6 @@ config AS_SHA256_NI
 	def_bool $(as-instr,sha256msg1 %xmm0$(comma)%xmm1)
 	help
 	  Supported by binutils >= 2.24 and LLVM integrated assembler
-config AS_TPAUSE
-	def_bool $(as-instr,tpause %ecx)
-	help
-	  Supported by binutils >= 2.31.1 and LLVM integrated assembler >= V7
 
 config AS_GFNI
 	def_bool $(as-instr,vgf2p8mulb %xmm0$(comma)%xmm1$(comma)%xmm2)
arch/x86/include/asm/mwait.h +2 -9
index 6522886..5141d2a 100644
@@ -133,16 +133,9 @@ static __always_inline void mwait_idle_with_hints(unsigned long eax, unsigned lo
  */
 static inline void __tpause(u32 ecx, u32 edx, u32 eax)
 {
-	/* "tpause %ecx, %edx, %eax" */
-	#ifdef CONFIG_AS_TPAUSE
-	asm volatile("tpause %%ecx"
-		     :
-		     : "c"(ecx), "d"(edx), "a"(eax));
-	#else
+	/* "tpause %ecx" */
 	asm volatile(".byte 0x66, 0x0f, 0xae, 0xf1"
-		     :
-		     : "c"(ecx), "d"(edx), "a"(eax));
-	#endif
+		     :: "c" (ecx), "d" (edx), "a" (eax));
 }
 
 #endif /* _ASM_X86_MWAIT_H */
merge bot Applied
#13 lore raw

[tip: x86/mm] x86/idle: Remove CONFIG_AS_TPAUSE

In reply to Uros Bizjak
The following commit has been merged into the x86/mm branch of tip:

The following commit has been merged into the x86/mm branch of tip:

Commit-ID:     a72d55dc3bd6555cc1f97459b42b7f62ae480f13
Gitweb:        https://git.kernel.org/tip/a72d55dc3bd6555cc1f97459b42b7f62ae480f13
Author:        Uros Bizjak <[email protected]>
AuthorDate:    Wed, 02 Apr 2025 20:08:08 +02:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Thu, 03 Apr 2025 13:19:18 +02:00

x86/idle: Remove CONFIG_AS_TPAUSE

There is not much point in CONFIG_AS_TPAUSE at all when the emitted assembly is always the same - it only obfuscates the __tpause() code in essence.

Remove the TPAUSE insn mnemonic from __tpause() and leave only the equivalent byte-wise definition. This can then be changed back to insn mnemonic once binutils 2.31.1 is the minimum version to build the kernel. (Right now it's 2.25.)

Cc Andy Lutomirski <[email protected]>
Cc Brian Gerst <[email protected]>
Cc Juergen Gross <[email protected]>
Cc Andrew Cooper <[email protected]>
Cc Rik van Riel <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Rafael J. Wysocki <[email protected]>
Cc Linus Torvalds <[email protected]>
---
 arch/x86/Kconfig.assembler   |  4 ----
 arch/x86/include/asm/mwait.h | 11 ++---------
 2 files changed, 2 insertions(+), 13 deletions(-)
arch/x86/Kconfig.assembler +0 -4
index 6d20a6c..fa88585 100644
@@ -15,10 +15,6 @@ config AS_SHA256_NI
 	def_bool $(as-instr,sha256msg1 %xmm0$(comma)%xmm1)
 	help
 	  Supported by binutils >= 2.24 and LLVM integrated assembler
-config AS_TPAUSE
-	def_bool $(as-instr,tpause %ecx)
-	help
-	  Supported by binutils >= 2.31.1 and LLVM integrated assembler >= V7
 
 config AS_GFNI
 	def_bool $(as-instr,vgf2p8mulb %xmm0$(comma)%xmm1$(comma)%xmm2)
arch/x86/include/asm/mwait.h +2 -9
index 0e020a6..6a2ec20 100644
@@ -138,16 +138,9 @@ static __always_inline void mwait_idle_with_hints(unsigned long eax, unsigned lo
  */
 static inline void __tpause(u32 ecx, u32 edx, u32 eax)
 {
-	/* "tpause %ecx, %edx, %eax" */
-	#ifdef CONFIG_AS_TPAUSE
-	asm volatile("tpause %%ecx"
-		     :
-		     : "c"(ecx), "d"(edx), "a"(eax));
-	#else
+	/* "tpause %ecx" */
 	asm volatile(".byte 0x66, 0x0f, 0xae, 0xf1"
-		     :
-		     : "c"(ecx), "d"(edx), "a"(eax));
-	#endif
+		     :: "c" (ecx), "d" (edx), "a" (eax));
 }
 
 #endif /* _ASM_X86_MWAIT_H */
merge bot Applied
#14 lore raw

[tip: x86/mm] x86/idle: Standardize argument types for MONITOR{,X} and MWAIT{,X} instruction wrappers on 'u32'

In reply to Uros Bizjak
The following commit has been merged into the x86/mm branch of tip:

The following commit has been merged into the x86/mm branch of tip:

Commit-ID:     1ae899e413105aa81068d0282ab6e22974891d74
Gitweb:        https://git.kernel.org/tip/1ae899e413105aa81068d0282ab6e22974891d74
Author:        Uros Bizjak <[email protected]>
AuthorDate:    Wed, 02 Apr 2025 20:08:05 +02:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Wed, 02 Apr 2025 22:26:17 +02:00

x86/idle: Standardize argument types for MONITOR{,X} and MWAIT{,X} instruction wrappers on 'u32'

MONITOR and MONITORX expect 32-bit unsigned integer arguments in the %ecx and %edx registers. MWAIT and MWAITX expect 32-bit usigned int argument in %eax and %ecx registers.

Some of the helpers around these instructions in <asm/mwait.h> are using too wide types (long), standardize on u32 instead that makes it clear that this is a hardware ABI.

[ mingo: Cleaned up the changelog. ]

Cc Andy Lutomirski <[email protected]>
Cc Brian Gerst <[email protected]>
Cc Juergen Gross <[email protected]>
Cc Andrew Cooper <[email protected]>
Cc Rik van Riel <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Rafael J. Wysocki <[email protected]>
Cc Linus Torvalds <[email protected]>
---
 arch/x86/include/asm/mwait.h | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)
arch/x86/include/asm/mwait.h +5 -8
index 54dc313..3377869 100644
@@ -25,23 +25,21 @@
 #define TPAUSE_C01_STATE		1
 #define TPAUSE_C02_STATE		0
 
-static __always_inline void __monitor(const void *eax, unsigned long ecx,
-			     unsigned long edx)
+static __always_inline void __monitor(const void *eax, u32 ecx, u32 edx)
 {
 	/* "monitor %eax, %ecx, %edx;" */
 	asm volatile(".byte 0x0f, 0x01, 0xc8;"
 		     :: "a" (eax), "c" (ecx), "d"(edx));
 }
 
-static __always_inline void __monitorx(const void *eax, unsigned long ecx,
-			      unsigned long edx)
+static __always_inline void __monitorx(const void *eax, u32 ecx, u32 edx)
 {
 	/* "monitorx %eax, %ecx, %edx;" */
 	asm volatile(".byte 0x0f, 0x01, 0xfa;"
 		     :: "a" (eax), "c" (ecx), "d"(edx));
 }
 
-static __always_inline void __mwait(unsigned long eax, unsigned long ecx)
+static __always_inline void __mwait(u32 eax, u32 ecx)
 {
 	mds_idle_clear_cpu_buffers();
 
@@ -76,8 +74,7 @@ static __always_inline void __mwait(unsigned long eax, unsigned long ecx)
  * EAX                     (logical) address to monitor
  * ECX                     #GP if not zero
  */
-static __always_inline void __mwaitx(unsigned long eax, unsigned long ebx,
-				     unsigned long ecx)
+static __always_inline void __mwaitx(u32 eax, u32 ebx, u32 ecx)
 {
 	/* No MDS buffer clear as this is AMD/HYGON only */
 
@@ -95,7 +92,7 @@ static __always_inline void __mwaitx(unsigned long eax, unsigned long ebx,
  * executing mwait, it would otherwise go unnoticed and the next tick
  * would not be reprogrammed accordingly before mwait ever wakes up.
  */
-static __always_inline void __sti_mwait(unsigned long eax, unsigned long ecx)
+static __always_inline void __sti_mwait(u32 eax, u32 ecx)
 {
 	mds_idle_clear_cpu_buffers();
 	/* "mwait %eax, %ecx;" */
#15 lore raw

Re: [tip: x86/mm] x86/idle: Standardize argument types for MONITOR{,X} and MWAIT{,X} instruction wrappers on 'u32'

Quoted 15 lines · On 02/04/2025 9:43 pm, tip-bot2 for Uros Bizjak wrote:

The following commit has been merged into the x86/mm branch of tip:

Commit-ID:     1ae899e413105aa81068d0282ab6e22974891d74
Gitweb:        https://git.kernel.org/tip/1ae899e413105aa81068d0282ab6e22974891d74
Author:        Uros Bizjak <[email protected]>
AuthorDate:    Wed, 02 Apr 2025 20:08:05 +02:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Wed, 02 Apr 2025 22:26:17 +02:00

x86/idle: Standardize argument types for MONITOR{,X} and MWAIT{,X} instruction wrappers on 'u32'

MONITOR and MONITORX expect 32-bit unsigned integer arguments in the %ecx and %edx registers. MWAIT and MWAITX expect 32-bit usigned int argument in %eax and %ecx registers.

Quoted 7 lines

Some of the helpers around these instructions in <asm/mwait.h> are using too wide types (long), standardize on u32 instead that makes it clear that this is a hardware ABI.

[ mingo: Cleaned up the changelog. ]

Something I noticed while looking over this is that you want to push the u32's up into the callers too.

mwait_idle_with_hints() with have (marginally) better code gen by swapping it's unsigned longs for u32's.

~Andrew

Applied
#16 lore raw
In reply to Uros Bizjak
Quoted 3 lines · Uros Bizjak

MONITOR and MONITORX expect 32-bit unsigned integer argument in %ecx and %edx registers. MWAIT and MWAITX expect 32-bit usigned int argument in %eax and %ecx registers.

Please always include a 0/4 cover letter as well for such series, which gives people a chance to reply to the whole series, instead of having to awkwardly pick a patch to reply to. :-)

Such as this general feedback:

I've applied this series with edits to the changelogs, note in particular:

patch #1:

- Changed verbiage from 'fix' to 'standardize to u32'. There was no bug to fix, using 'long' instead of 'int' is at worst an ineffiency.

patch #3:

- Provided much needed historic context behind ;, \t, \n beautifiers used in asm() statements. These aren't just random noise added in.

patch #4:

- Added in the current binutils version cutoff, to make it really clear why we cannot use the mnemonic yet, and how far away we are from doing so.

I've also extended the Cc: list in the commit to give people a chance to object, to patch #3 in particular:

8ad521aaf743 ("x86/idle: Remove .s output beautifying delimiters from simpler asm() templates")

Thanks,

Ingo

#17 lore raw
Quoted 14 lines · Ingo Molnar
Quoted 3 lines · Uros Bizjak

MONITOR and MONITORX expect 32-bit unsigned integer argument in %ecx and %edx registers. MWAIT and MWAITX expect 32-bit usigned int argument in %eax and %ecx registers.

Please always include a 0/4 cover letter as well for such series, which gives people a chance to reply to the whole series, instead of having to awkwardly pick a patch to reply to. :-)

Such as this general feedback:

Quoted 7 lines

I've applied this series with edits to the changelogs, note in particular:

patch #1:

- Changed verbiage from 'fix' to 'standardize to u32'. There was no bug to fix, using 'long' instead of 'int' is at worst an ineffiency.

Indeed, but when patch #2 introduces insn mnemonic:

asm volatile("monitor %0, %1, %2" :: "a" (eax), "c" (ecx), "d" (edx));

then the type decides between %rcx/%rdx and %ecx/%edx. Assembler checks arguments of MONITOR insn mnemonic and fails compilation with the former.

Quoted 4 lines

patch #3:

- Provided much needed historic context behind ;, \t, \n beautifiers used in asm() statements. These aren't just random noise added in.

Please note that removed delimiters were at the end of asm template. The compiler adds \n\t at the end on its own, as demonstrated with the following example:

void foo (unsigned int a, unsigned long b)
{
asm ("foo %0 %1" :: "r" (a), "r" (b));
}

gcc -S (unredacted dump):

foo:
.LFB0:
.cfi_startproc
#APP
# 3 "tt.c" 1
foo %edi %rsi
# 0 "" 2
#NO_APP
ret
.cfi_endproc
.LFE0:

Please note that asm instructions are perfectly aligned. So, at the end of the day, the delimiters at the end of asm templates *are* just noise.

Thanks,
Uros.