GCC considers the number of statements in inlined assembly blocks,
according to new-lines and semicolons, as an indication to the cost of
the block in time and space. This data is distorted by the kernel code,
which puts information in alternative sections. As a result, the
compiler may perform incorrect inlining and branch optimizations.
The solution is to set an assembly macro and call it from the inlined
assembly block. As a result GCC considers the inline assembly block as
a single instruction.
This patch handles the LOCK prefix, allowing more aggresive inlining.
text data bss dec hex filename
18127205 10068388 2936832 31132425 1db0b09 ./vmlinux before
18131468 10068488 2936832 31136788 1db1c14 ./vmlinux after (+4363)
Static text symbols:
After: 39788 (-72)
Cc: Thomas Gleixner <t...@linutronix.de>
Cc: Ingo Molnar <mi...@redhat.com>
Cc: "H. Peter Anvin" <h...@zytor.com>
Cc: Josh Poimboeuf <jpoim...@redhat.com>
Signed-off-by: Nadav Amit <na...@vmware.com>
arch/x86/include/asm/alternative.h | 34 +++++++++++++++++++++++-------
1 file changed, 26 insertions(+), 8 deletions(-)
diff --git a/arch/x86/include/asm/alternative.h
index 4cd6a3b71824..1dc47c9fd480 100644
@@ -28,17 +28,35 @@
* The very common lock prefix is handled as special case in a
* separate table which is a pure address list without replacement ptr
* and size information. That keeps the table sizes small.
+ * Saving the lock data is encapsulated within an assembly macro, which is then
+ * called on each use. This hack is necessary to prevent GCC from considering
+ * the inline assembly blocks as costly in time and space, which can prevent
+ * function inlining and lead to other bad compilation decisions. GCC computes
+ * inline assembly cost according to the number of perceived number of assembly
+ * instruction, based on the number of new-lines and semicolons in the assembly
+ * block. The macro will eventually be compiled into a single instruction (and
+ * some data). This scheme allows GCC to better understand the inline asm cost.
-#define LOCK_PREFIX_HERE \
- ".pushsection .smp_locks,\"a\"\n" \
- ".balign 4\n" \
- ".long 671f - .\n" /* offset */ \
- ".popsection\n" \
-#define LOCK_PREFIX LOCK_PREFIX_HERE "\n\tlock; "
+ ".pushsection .smp_locks,\"a\"\n\t"
+ ".balign 4\n\t"
+ ".long 671f - .\n\t" /* offset */
+#define LOCK_PREFIX_HERE "__LOCK_PREFIX_HERE\n\t"
+asm(".macro __LOCK_PREFIX ins:vararg\n\t"
+ "lock; \\ins\n\t"
+#define LOCK_PREFIX "__LOCK_PREFIX "
#else /* ! CONFIG_SMP */
#define LOCK_PREFIX_HERE ""