Skip to content

Commit d5a581d

Browse files
anadavIngo Molnar
authored andcommitted
x86/cpufeature: Macrofy inline assembly code 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 - which is pretty pointless indirection in the static_cpu_has() case, but is worth it to improve overall inlining quality. The patch slightly increases the kernel size: text data bss dec hex filename 18162879 10226256 2957312 31346447 1de4f0f ./vmlinux before 18163528 10226300 2957312 31347140 1de51c4 ./vmlinux after (+693) And enables the inlining of function such as free_ldt_pgtables(). 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: Linus Torvalds <torvalds@linux-foundation.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Link: http://lkml.kernel.org/r/20181005202718.229565-3-namit@vmware.com Link: https://lore.kernel.org/lkml/20181003213100.189959-10-namit@vmware.com/T/#u Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 0474d5d commit d5a581d

File tree

2 files changed

+48
-35
lines changed

2 files changed

+48
-35
lines changed

arch/x86/include/asm/cpufeature.h

Lines changed: 47 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
#ifndef _ASM_X86_CPUFEATURE_H
33
#define _ASM_X86_CPUFEATURE_H
44

5-
#include <asm/processor.h>
6-
7-
#if defined(__KERNEL__) && !defined(__ASSEMBLY__)
5+
#ifdef __KERNEL__
6+
#ifndef __ASSEMBLY__
87

8+
#include <asm/processor.h>
99
#include <asm/asm.h>
1010
#include <linux/bitops.h>
1111

@@ -161,37 +161,10 @@ extern void clear_cpu_cap(struct cpuinfo_x86 *c, unsigned int bit);
161161
*/
162162
static __always_inline __pure bool _static_cpu_has(u16 bit)
163163
{
164-
asm_volatile_goto("1: jmp 6f\n"
165-
"2:\n"
166-
".skip -(((5f-4f) - (2b-1b)) > 0) * "
167-
"((5f-4f) - (2b-1b)),0x90\n"
168-
"3:\n"
169-
".section .altinstructions,\"a\"\n"
170-
" .long 1b - .\n" /* src offset */
171-
" .long 4f - .\n" /* repl offset */
172-
" .word %P[always]\n" /* always replace */
173-
" .byte 3b - 1b\n" /* src len */
174-
" .byte 5f - 4f\n" /* repl len */
175-
" .byte 3b - 2b\n" /* pad len */
176-
".previous\n"
177-
".section .altinstr_replacement,\"ax\"\n"
178-
"4: jmp %l[t_no]\n"
179-
"5:\n"
180-
".previous\n"
181-
".section .altinstructions,\"a\"\n"
182-
" .long 1b - .\n" /* src offset */
183-
" .long 0\n" /* no replacement */
184-
" .word %P[feature]\n" /* feature bit */
185-
" .byte 3b - 1b\n" /* src len */
186-
" .byte 0\n" /* repl len */
187-
" .byte 0\n" /* pad len */
188-
".previous\n"
189-
".section .altinstr_aux,\"ax\"\n"
190-
"6:\n"
191-
" testb %[bitnum],%[cap_byte]\n"
192-
" jnz %l[t_yes]\n"
193-
" jmp %l[t_no]\n"
194-
".previous\n"
164+
asm_volatile_goto("STATIC_CPU_HAS bitnum=%[bitnum] "
165+
"cap_byte=\"%[cap_byte]\" "
166+
"feature=%P[feature] t_yes=%l[t_yes] "
167+
"t_no=%l[t_no] always=%P[always]"
195168
: : [feature] "i" (bit),
196169
[always] "i" (X86_FEATURE_ALWAYS),
197170
[bitnum] "i" (1 << (bit & 7)),
@@ -226,5 +199,44 @@ static __always_inline __pure bool _static_cpu_has(u16 bit)
226199
#define CPU_FEATURE_TYPEVAL boot_cpu_data.x86_vendor, boot_cpu_data.x86, \
227200
boot_cpu_data.x86_model
228201

229-
#endif /* defined(__KERNEL__) && !defined(__ASSEMBLY__) */
202+
#else /* __ASSEMBLY__ */
203+
204+
.macro STATIC_CPU_HAS bitnum:req cap_byte:req feature:req t_yes:req t_no:req always:req
205+
1:
206+
jmp 6f
207+
2:
208+
.skip -(((5f-4f) - (2b-1b)) > 0) * ((5f-4f) - (2b-1b)),0x90
209+
3:
210+
.section .altinstructions,"a"
211+
.long 1b - . /* src offset */
212+
.long 4f - . /* repl offset */
213+
.word \always /* always replace */
214+
.byte 3b - 1b /* src len */
215+
.byte 5f - 4f /* repl len */
216+
.byte 3b - 2b /* pad len */
217+
.previous
218+
.section .altinstr_replacement,"ax"
219+
4:
220+
jmp \t_no
221+
5:
222+
.previous
223+
.section .altinstructions,"a"
224+
.long 1b - . /* src offset */
225+
.long 0 /* no replacement */
226+
.word \feature /* feature bit */
227+
.byte 3b - 1b /* src len */
228+
.byte 0 /* repl len */
229+
.byte 0 /* pad len */
230+
.previous
231+
.section .altinstr_aux,"ax"
232+
6:
233+
testb \bitnum,\cap_byte
234+
jnz \t_yes
235+
jmp \t_no
236+
.previous
237+
.endm
238+
239+
#endif /* __ASSEMBLY__ */
240+
241+
#endif /* __KERNEL__ */
230242
#endif /* _ASM_X86_CPUFEATURE_H */

arch/x86/kernel/macros.S

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@
1212
#include <asm/bug.h>
1313
#include <asm/paravirt.h>
1414
#include <asm/asm.h>
15+
#include <asm/cpufeature.h>

0 commit comments

Comments
 (0)