Skip to content

Commit 77f48ec

Browse files
anadavIngo Molnar
authored andcommitted
x86/alternatives: Macrofy lock prefixes to work around GCC inlining bugs
As described in: 77b0bf5: ("kbuild/Makefile: Prepare for using macros in inline assembly code to work around asm() related GCC inlining bugs") GCC's inlining heuristics are broken with common asm() patterns used in kernel code, resulting in the effective disabling of inlining. The workaround is to set an assembly macro and call it from the inline assembly block - i.e. to macrify the affected 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 18140140 10225284 2957312 31322736 1ddf270 ./vmlinux before 18146889 10225380 2957312 31329581 1de0d2d ./vmlinux after (+6845) This is the reduction in non-inlined functions: Before: 40286 After: 40218 (-68) Tested-by: Kees Cook <keescook@chromium.org> Signed-off-by: Nadav Amit <namit@vmware.com> Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org> Cc: Andy Lutomirski <luto@amacapital.net> Cc: Borislav Petkov <bp@alien8.de> Cc: Brian Gerst <brgerst@gmail.com> Cc: Denys Vlasenko <dvlasenk@redhat.com> Cc: H. Peter Anvin <hpa@zytor.com> Cc: Josh Poimboeuf <jpoimboe@redhat.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Link: http://lkml.kernel.org/r/20181003213100.189959-6-namit@vmware.com Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 9e1725b commit 77f48ec

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

arch/x86/include/asm/alternative-asm.h

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,24 @@
77
#include <asm/asm.h>
88

99
#ifdef CONFIG_SMP
10-
.macro LOCK_PREFIX
11-
672: lock
10+
.macro LOCK_PREFIX_HERE
1211
.pushsection .smp_locks,"a"
1312
.balign 4
14-
.long 672b - .
13+
.long 671f - . # offset
1514
.popsection
16-
.endm
15+
671:
16+
.endm
17+
18+
.macro LOCK_PREFIX insn:vararg
19+
LOCK_PREFIX_HERE
20+
lock \insn
21+
.endm
1722
#else
18-
.macro LOCK_PREFIX
19-
.endm
23+
.macro LOCK_PREFIX_HERE
24+
.endm
25+
26+
.macro LOCK_PREFIX insn:vararg
27+
.endm
2028
#endif
2129

2230
/*

arch/x86/include/asm/alternative.h

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,8 @@
3131
*/
3232

3333
#ifdef CONFIG_SMP
34-
#define LOCK_PREFIX_HERE \
35-
".pushsection .smp_locks,\"a\"\n" \
36-
".balign 4\n" \
37-
".long 671f - .\n" /* offset */ \
38-
".popsection\n" \
39-
"671:"
40-
41-
#define LOCK_PREFIX LOCK_PREFIX_HERE "\n\tlock; "
42-
34+
#define LOCK_PREFIX_HERE "LOCK_PREFIX_HERE\n\t"
35+
#define LOCK_PREFIX "LOCK_PREFIX "
4336
#else /* ! CONFIG_SMP */
4437
#define LOCK_PREFIX_HERE ""
4538
#define LOCK_PREFIX ""

arch/x86/kernel/macros.S

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88

99
#include <linux/compiler.h>
1010
#include <asm/refcount.h>
11+
#include <asm/alternative-asm.h>

0 commit comments

Comments
 (0)