x86/fsgsbase: Remove unnecessary "memory" clobbers from FS/GS base accessors
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.
---
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
@@ -25,7 +25,7 @@ static __always_inline unsigned long rdfsbase(void) {unsigned long fsbase; - asmvolatile ("rdfsbase %0" :"=r" (fsbase) ::"memory" ); + asmvolatile ("rdfsbase %0" :"=r" (fsbase));return fsbase; }
@@ -34,7 +34,7 @@ static __always_inline unsigned long rdgsbase(void) {unsigned long gsbase; - asmvolatile ("rdgsbase %0" :"=r" (gsbase) ::"memory" ); + asmvolatile ("rdgsbase %0" :"=r" (gsbase));return gsbase; }
Signature · 1 line
2.53.0
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.
--- arch/x86/include/asm/segment.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/include/asm/segment.h +1 -1
@@ -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
PATCH v2 3/3 x86/segment: Use ASM_INPUT_RM in __loadsegment_fs()
In reply to Uros BizjakUse 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.
--- arch/x86/include/asm/segment.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/include/asm/segment.h +1 -1
@@ -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