Skip to contentSkip to conversation

x86/alternatives: Force inline stac() and clac()

PATCH In review Andrew Cooper 2 participants 3 messages Aug 18, 2014

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/alternatives: Force inline stac() and clac()

In this case, we know better than the compiler.

gcc 4.7 (Debian Wheezy) chooses to create translation-unit-local functions (even for non-debug builds) named stac() and clac(), and calls them.

$ objdump -d xen-syms | grep -c "<stac>:"
6
$ objdump -d xen-syms | grep -o "callq  [0-9a-f]\+ <stac>" | uniq -c
      5 callq  ffff82d0801166c9 <stac>
     20 callq  ffff82d08015ef99 <stac>
      4 callq  ffff82d080165169 <stac>
      8 callq  ffff82d080188cb9 <stac>
      3 callq  ffff82d080228779 <stac>
      4 callq  ffff82d08022c5c9 <stac>

Forcing always_inline removes these functions, and replaces each of the callqs with the expected 3byte nops.

---
 xen/include/asm-x86/asm_defns.h |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
xen/include/asm-x86/asm_defns.h +2 -2
index f8ef1f8..1674c7c 100644
@@ -179,13 +179,13 @@ void ret_from_intr(void);
 #define ASM_STAC ASM_AC(STAC)
 #define ASM_CLAC ASM_AC(CLAC)
 #else
-static inline void clac(void)
+static always_inline void clac(void)
 {
     /* Note: a barrier is implicit in alternative() */
     alternative(ASM_NOP3, __stringify(__ASM_CLAC), X86_FEATURE_SMAP);
 }
 
-static inline void stac(void)
+static always_inline void stac(void)
 {
     /* Note: a barrier is implicit in alternative() */
     alternative(ASM_NOP3, __stringify(__ASM_STAC), X86_FEATURE_SMAP);
Signature · 1 line
1.7.10.4
#1 lore raw
Quoted 10 lines
Quoted 1 line
Quoted 1 line

Andrew Cooper <[email protected]> 08/18/14 6:16 PM >>>

In this case, we know better than the compiler.

gcc 4.7 (Debian Wheezy) chooses to create translation-unit-local functions (even for non-debug builds) named stac() and clac(), and calls them.

$ objdump -d xen-syms | grep -c "<stac>:"
6
$ objdump -d xen-syms | grep -o "callq  [0-9a-f]\+ <stac>" | uniq -c
      >5 callq  ffff82d0801166c9 <stac>
     >20 callq  ffff82d08015ef99 <stac>
      >4 callq  ffff82d080165169 <stac>
      >8 callq  ffff82d080188cb9 <stac>
      >3 callq  ffff82d080228779 <stac>
      >4 callq  ffff82d08022c5c9 <stac>
Quoted 3 lines

Forcing always_inline removes these functions, and replaces each of the callqs with the expected 3byte nops.

I'm fine putting the patch in, but isn't this a compiler bug? Creating a 5-byte call instruction instead of a 3-byte inline expansion should even preclude out of line placement under -Os (which otherwise is the most likely reason for functions not getting inlined).

Jan

#2 lore raw
Quoted 16 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line

Andrew Cooper <[email protected]> 08/18/14 6:16 PM >>>

In this case, we know better than the compiler.

gcc 4.7 (Debian Wheezy) chooses to create translation-unit-local functions (even for non-debug builds) named stac() and clac(), and calls them.

$ objdump -d xen-syms | grep -c "<stac>:"
6
$ objdump -d xen-syms | grep -o "callq  [0-9a-f]\+ <stac>" | uniq -c
      >5 callq  ffff82d0801166c9 <stac>
     >20 callq  ffff82d08015ef99 <stac>
      >4 callq  ffff82d080165169 <stac>
      >8 callq  ffff82d080188cb9 <stac>
      >3 callq  ffff82d080228779 <stac>
      >4 callq  ffff82d08022c5c9 <stac>
Quoted 6 lines
Quoted 2 lines

Forcing always_inline removes these functions, and replaces each of the callqs with the expected 3byte nops.

I'm fine putting the patch in, but isn't this a compiler bug? Creating a 5-byte call instruction instead of a 3-byte inline expansion should even preclude outdoe of line placement under -Os (which otherwise is the most likely reason for functions not getting inlined).

I am not sure. A static inline function in a header file is never a guarantee for forcing the inlining the function, which is why always_inline exists.

The ALTERNATIVE() macro does contain three push/pop sections, and the alternative() macro contains a memory clobber. It is entirely possible that gcc has decided early on that abstracting this as a local function is the easiest automated way to deal with the potential side effects.

It might even be rather more clever, and deciding to optimise fewer entries being placed in the .altinstructions section, which is an optimisation we specifically wish to avoid.

Either way, this is a grey area, and I wouldn't say for certain that this is a compiler bug, but certainly an outcome which we would wish to avoid.

~Andrew