Skip to contentSkip to conversation

x86/fsgsbase: Remove unnecessary "memory" clobbers from FS/GS base accessors

PATCH Superseded Uros Bizjak 1 participant 3 messages Mar 11 x86

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/3 x86/fsgsbase: Remove unnecessary "memory" clobbers from FS/GS base accessors

The rdfsbase() and rdgsbase() helpers currently include a "memory" clobber in their inline assembly definitions. However, the RDFSBASE and RDGSBASE instructions only read the FS/GS base MSRs into a general-purpose register and do not access memory. The "memory" clobber, which acts as a compiler barrier and may inhibit optimization, is therefore unnecessary.

The "memory" clobber was historically used as a scheduling constraint to prevent the compiler from moving the instructions before preceding segment register loads. This is not required because both the segment register loads and the RDFSBASE/RDGSBASE accessors are implemented with asm volatile, which already prevents reordering between them.

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]>

---
v2: Note that the "memory" clobber was historically
used as a scheduling constraint.

---
 arch/x86/include/asm/fsgsbase.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/include/asm/fsgsbase.h +2 -2
index ab2547f97c2c..70ff4ef457b1 100644
@@ -25,7 +25,7 @@ static __always_inline unsigned long rdfsbase(void)
 {
 	unsigned long fsbase;
 
-	asm volatile("rdfsbase %0" : "=r" (fsbase) :: "memory");
+	asm volatile("rdfsbase %0" : "=r" (fsbase));
 
 	return fsbase;
 }
@@ -34,7 +34,7 @@ static __always_inline unsigned long rdgsbase(void)
 {
 	unsigned long gsbase;
 
-	asm volatile("rdgsbase %0" : "=r" (gsbase) :: "memory");
+	asm volatile("rdgsbase %0" : "=r" (gsbase));
 
 	return gsbase;
 }
Signature · 1 line
2.53.0
#1 lore raw

PATCH v2 2/3 x86/segment: Remove unnecessary memory clobber from savesegment()

The savesegment() macro uses inline assembly to copy a segment register into a general-purpose register:

movl %seg, reg

This instruction does not access memory, yet the inline asm currently declares a "memory" clobber, which unnecessarily acts as a compiler barrier and may inhibit optimization.

Remove the "memory" clobber and mark the asm as asm volatile instead. Segment register loads in the kernel are implemented using asm volatile, so the compiler will not schedule segment register reads before those loads. Using asm volatile preserves the intended ordering with other segment register operations without imposing an unnecessary global memory barrier.

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/segment.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/include/asm/segment.h +1 -1
index 9f5be2bbd291..3fe3a310844c 100644
@@ -348,7 +348,7 @@ static inline void __loadsegment_fs(unsigned short value)
  * Save a segment register away:
  */
 #define savesegment(seg, value)				\
-	asm("movl %%" #seg ",%k0" : "=r" (value) : : "memory")
+	asm volatile("movl %%" #seg ",%k0" : "=r" (value))
 
 #endif /* !__ASSEMBLER__ */
 #endif /* __KERNEL__ */
Signature · 1 line
2.53.0
#2 lore raw

PATCH v2 3/3 x86/segment: Use ASM_INPUT_RM in __loadsegment_fs()

In reply to Uros Bizjak

Use the ASM_INPUT_RM macro in __loadsegment_fs() to work around clang problems with "rm" asm constraint. clang seems to always chose the memory input, while it is almost always the worst choice.

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 Nathan Chancellor <[email protected]>
Cc Nick Desaulniers <[email protected]>
Cc Bill Wendling <[email protected]>
Cc Justin Stitt <[email protected]>
---
 arch/x86/include/asm/segment.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/include/asm/segment.h +1 -1
index 3fe3a310844c..0f4283dcd0c4 100644
@@ -335,7 +335,7 @@ static inline void __loadsegment_fs(unsigned short value)
 
 		     _ASM_EXTABLE_TYPE(1b, 2b, EX_TYPE_CLEAR_FS)
 
-		     : : "rm" (value) : "memory");
+		     : : ASM_INPUT_RM (value) : "memory");
 }
 
 /* __loadsegment_gs is intentionally undefined.  Use load_gs_index instead. */
Signature · 1 line
2.53.0