x86/idle: Remove barriers for X86_BUG_CLFLUSH_MONITOR
Commit 7e98b7192046 ("x86, idle: Use static_cpu_has() for CLFLUSH workaround, add barriers") adds barriers, justified with:
... and add memory barriers around it since the documentation is explicit that CLFLUSH is only ordered with respect to MFENCE.
The SDM currently states:
Executions of the CLFLUSH instruction are ordered with respect to each other and with respect to writes, locked read-modify-write instructions, and fence instructions[1].
With footnote 1 reading:
Earlier versions of this manual specified that executions of the CLFLUSH instruction were ordered only by the MFENCE instruction. All processors implementing the CLFLUSH instruction also order it relative to the other operations enumerated above.
i.e. The SDM was incorrect at the time, and barriers should not have been inserted. Double checking the original AAI65 errata (not available from intel.com any more) shows no mention of barriers either.
Additionally, drop the static_cpu_has_bug() and use a plain alternative. The workaround is a single instruction, with identical address setup to the MONITOR instruction.
---
CC: Thomas Gleixner <[email protected]>
CC: Ingo Molnar <[email protected]>
CC: Borislav Petkov <[email protected]>
CC: Dave Hansen <[email protected]>
CC: [email protected]
CC: "H. Peter Anvin" <[email protected]>
CC: [email protected]
arch/x86/include/asm/mwait.h +4 -6
@@ -116,13 +116,11 @@ static __always_inline void __sti_mwait(unsigned long eax, unsigned long ecx) static __always_inline void mwait_idle_with_hints(unsigned long eax, unsigned long ecx) {if (static_cpu_has_bug (X86_BUG_MONITOR ) || !current_set_polling_and_test ()) { -if (static_cpu_has_bug (X86_BUG_CLFLUSH_MONITOR )) { -mb (); -clflush ((void *)¤t_thread_info ()->flags); -mb (); - } +const void *addr = ¤t_thread_info ()->flags; -__monitor ((void *)¤t_thread_info ()->flags,0 ,0 ); + alternative_input("", "clflush (%[addr])", X86_BUG_CLFLUSH_MONITOR, + [addr] "a" (addr)); +__monitor (addr,0 ,0 );if (!need_resched ()) {if (ecx &1 ) {
Quoted 51 lines · Andrew Cooper
Commit
7e98b7192046("x86, idle: Usestatic_cpu_has()for CLFLUSH workaround, add barriers") adds barriers, justified with:... and add memory barriers around it since the documentation is explicit that CLFLUSH is only ordered with respect to MFENCE.
The SDM currently states:
Executions of the CLFLUSH instruction are ordered with respect to each other and with respect to writes, locked read-modify-write instructions, and fence instructions[1].
With footnote 1 reading:
Earlier versions of this manual specified that executions of the CLFLUSH instruction were ordered only by the MFENCE instruction. All processors implementing the CLFLUSH instruction also order it relative to the other operations enumerated above.
i.e. The SDM was incorrect at the time, and barriers should not have been inserted. Double checking the original AAI65 errata (not available from intel.com any more) shows no mention of barriers either.
Additionally, drop the
static_cpu_has_bug()and use a plain alternative. The workaround is a single instruction, with identical address setup to the MONITOR instruction.Fixes 7e98b7192046 ("x86, idle: Use static_cpu_has() for CLFLUSH workaround, add barriers")Signed-off-by Andrew Cooper <[email protected]>---
CC: Thomas Gleixner <[email protected]>
CC: Ingo Molnar <[email protected]>
CC: Borislav Petkov <[email protected]>
CC: Dave Hansen <[email protected]>
CC: [email protected]
CC: "H. Peter Anvin" <[email protected]>
CC: [email protected]arch/x86/include/asm/mwait.h +0
-4@@ -116,13 +116,11 @@ static __always_inline void __sti_mwait(unsigned long eax, unsigned long ecx) static __always_inline void mwait_idle_with_hints(unsigned long eax, unsigned long ecx) {if (static_cpu_has_bug (X86_BUG_MONITOR ) || !current_set_polling_and_test ()) { -if (static_cpu_has_bug (X86_BUG_CLFLUSH_MONITOR )) { -mb (); -clflush ((void *)¤t_thread_info ()->flags); -mb ();
arch/x86/include/asm/mwait.h static __always_inline void __sti_mwait(unsigned long eax, unsigned long ecx) · line 126
Quoted 10 lines
arch/x86/include/asm/mwait.h +4
-2- } +const void *addr = ¤t_thread_info ()->flags; -__monitor ((void *)¤t_thread_info ()->flags,0 ,0 ); + alternative_input("", "clflush (%[addr])", X86_BUG_CLFLUSH_MONITOR, + [addr] "a" (addr)); +__monitor (addr,0 ,0 );if (!need_resched ()) {if (ecx &1 ) {
LGTM.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquette[tip: x86/mm] x86/idle: Remove mb() barriers for X86_BUG_CLFLUSH_MONITOR in mwait_idle_with_hints()
In reply to Andrew CooperThe 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: 90a22a5f841490790ecb17166633582681d44945 Gitweb: https://git.kernel.org/tip/90a22a5f841490790ecb17166633582681d44945 Author: Andrew Cooper <[email protected]> AuthorDate: Wed, 02 Apr 2025 10:10:17 +01:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Wed, 02 Apr 2025 11:54:51 +02:00
x86/idle: Remove mb() barriers for X86_BUG_CLFLUSH_MONITOR in mwait_idle_with_hints()
The following commit, 12 years ago:
7e98b7192046 ("x86, idle: Use static_cpu_has() for CLFLUSH workaround, add barriers")
added barriers around the CLFLUSH in mwait_idle_with_hints(), justified with:
... and add memory barriers around it since the documentation is explicit that CLFLUSH is only ordered with respect to MFENCE.
The SDM currently states:
Executions of the CLFLUSH instruction are ordered with respect to each other and with respect to writes, locked read-modify-write instructions, and fence instructions.
With footnote 1 reading:
Earlier versions of this manual specified that executions of the CLFLUSH instruction were ordered only by the MFENCE instruction. All processors implementing the CLFLUSH instruction also order it relative to the other operations enumerated above.
I.e. The SDM was incorrect at the time, and barriers should not have been inserted. Double checking the original AAI65 errata (not available from intel.com any more) shows no mention of barriers either.
Additionally, drop the static_cpu_has_bug() and use a plain alternative(). The workaround is a single instruction, with identical address setup to the MONITOR instruction.
--- arch/x86/include/asm/mwait.h | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-)
arch/x86/include/asm/mwait.h +3 -6
@@ -116,13 +116,10 @@ static __always_inline void __sti_mwait(unsigned long eax, unsigned long ecx) static __always_inline void mwait_idle_with_hints(unsigned long eax, unsigned long ecx) {if (static_cpu_has_bug (X86_BUG_MONITOR ) || !current_set_polling_and_test ()) { -if (static_cpu_has_bug (X86_BUG_CLFLUSH_MONITOR )) { -mb (); -clflush ((void *)¤t_thread_info ()->flags); -mb (); - } +const void *addr = ¤t_thread_info ()->flags; -__monitor ((void *)¤t_thread_info ()->flags,0 ,0 ); + alternative_input("", "clflush (%[addr])", X86_BUG_CLFLUSH_MONITOR, [addr] "a" (addr)); +__monitor (addr,0 ,0 );if (!need_resched ()) {if (ecx &1 ) {
Re: [tip: x86/mm] x86/idle: Remove mb() barriers for X86_BUG_CLFLUSH_MONITOR in mwait_idle_with_hints()
Quoted 20 lines · On 02/04/2025 11:10 am, tip-bot2 for Andrew Cooper wrote:
The following commit has been merged into the x86/mm branch of tip:
Commit-ID: 90a22a5f841490790ecb17166633582681d44945 Gitweb: https://git.kernel.org/tip/90a22a5f841490790ecb17166633582681d44945 Author: Andrew Cooper <[email protected]> AuthorDate: Wed, 02 Apr 2025 10:10:17 +01:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Wed, 02 Apr 2025 11:54:51 +02:00x86/idle: Remove
mb()barriers forX86_BUG_CLFLUSH_MONITORinmwait_idle_with_hints()The following commit, 12 years ago:
7e98b7192046("x86, idle: Usestatic_cpu_has()for CLFLUSH workaround, add barriers")added barriers around the CLFLUSH in
mwait_idle_with_hints(), justified with:... and add memory barriers around it since the documentation is explicit that CLFLUSH is only ordered with respect to MFENCE.
Quoted 7 lines
The SDM currently states:
Executions of the CLFLUSH instruction are ordered with respect to each other and with respect to writes, locked read-modify-write instructions, and fence instructions.
This link isn't the SDM. Its ...
Quoted 11 lines
With footnote 1 reading:
Earlier versions of this manual specified that executions of the CLFLUSH instruction were ordered only by the MFENCE instruction. All processors implementing the CLFLUSH instruction also order it relative to the other operations enumerated above.
I.e. The SDM was incorrect at the time, and barriers should not have been inserted. Double checking the original AAI65 errata (not available from intel.com any more) shows no mention of barriers either.
... this errata link, no longer hosted on intel.com.
~Andrew
Quoted 3 lines · Andrew Cooper
i.e. The SDM was incorrect at the time, and barriers should not have been inserted. Double checking the original AAI65 errata (not available from intel.com any more) shows no mention of barriers either.
There's a near copy-and-paste of that code here:
Quoted 8 lines
static
__cpuidlevoidmwait_idle(void){ if (!current_set_polling_and_test()) { if (this_cpu_has(X86_BUG_CLFLUSH_MONITOR)) { mb(); /* quirk */ clflush((void *)¤t_thread_info()->flags); mb(); /* quirk */ }
Any reason it can't get the same treatment?
Quoted 14 lines · Dave Hansen
Quoted 3 lines · Andrew Cooper
i.e. The SDM was incorrect at the time, and barriers should not have been inserted. Double checking the original AAI65 errata (not available from intel.com any more) shows no mention of barriers either.
There's a near copy-and-paste of that code here:
Quoted 8 lines
static
__cpuidlevoidmwait_idle(void){ if (!current_set_polling_and_test()) { if (this_cpu_has(X86_BUG_CLFLUSH_MONITOR)) { mb(); /* quirk */ clflush((void *)¤t_thread_info()->flags); mb(); /* quirk */ }
Quoted 1 line
Any reason it can't get the same treatment?
Oh, yes that should get the same treatment. (Sorry, too may cross-ports between Xen and Linux trying to untangle this mess).
I'll submit a v2.
~Andrew