]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blob - releases/4.4.165/x86-build-specify-stack-alignment-for-clang.patch
Fixes for 4.19
[thirdparty/kernel/stable-queue.git] / releases / 4.4.165 / x86-build-specify-stack-alignment-for-clang.patch
1 From foo@baz Wed Nov 21 18:50:39 CET 2018
2 From: Matthias Kaehlcke <mka@chromium.org>
3 Date: Wed, 21 Jun 2017 16:28:05 -0700
4 Subject: x86/build: Specify stack alignment for clang
5
6 From: Matthias Kaehlcke <mka@chromium.org>
7
8 commit d77698df39a512911586834d303275ea5fda74d0 upstream.
9
10 For gcc stack alignment is configured with -mpreferred-stack-boundary=N,
11 clang has the option -mstack-alignment=N for that purpose. Use the same
12 alignment as with gcc.
13
14 If the alignment is not specified clang assumes an alignment of
15 16 bytes, as required by the standard ABI. However as mentioned in
16 d9b0cde91c60 ("x86-64, gcc: Use -mpreferred-stack-boundary=3 if
17 supported") the standard kernel entry on x86-64 leaves the stack
18 on an 8-byte boundary, as a consequence clang will keep the stack
19 misaligned.
20
21 Signed-off-by: Matthias Kaehlcke <mka@chromium.org>
22 Acked-by: Ingo Molnar <mingo@kernel.org>
23 Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
24 Signed-off-by: Nathan Chancellor <natechancellor@gmail.com>
25 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
26 ---
27 arch/x86/Makefile | 26 +++++++++++++++++++++-----
28 1 file changed, 21 insertions(+), 5 deletions(-)
29
30 --- a/arch/x86/Makefile
31 +++ b/arch/x86/Makefile
32 @@ -11,6 +11,14 @@ else
33 KBUILD_DEFCONFIG := $(ARCH)_defconfig
34 endif
35
36 +# For gcc stack alignment is specified with -mpreferred-stack-boundary,
37 +# clang has the option -mstack-alignment for that purpose.
38 +ifneq ($(call cc-option, -mpreferred-stack-boundary=4),)
39 + cc_stack_align_opt := -mpreferred-stack-boundary
40 +else ifneq ($(call cc-option, -mstack-alignment=4),)
41 + cc_stack_align_opt := -mstack-alignment
42 +endif
43 +
44 # How to compile the 16-bit code. Note we always compile for -march=i386;
45 # that way we can complain to the user if the CPU is insufficient.
46 #
47 @@ -28,7 +36,7 @@ REALMODE_CFLAGS := $(M16_CFLAGS) -g -Os
48
49 REALMODE_CFLAGS += $(call __cc-option, $(CC), $(REALMODE_CFLAGS), -ffreestanding)
50 REALMODE_CFLAGS += $(call __cc-option, $(CC), $(REALMODE_CFLAGS), -fno-stack-protector)
51 -REALMODE_CFLAGS += $(call __cc-option, $(CC), $(REALMODE_CFLAGS), -mpreferred-stack-boundary=2)
52 +REALMODE_CFLAGS += $(call __cc-option, $(CC), $(REALMODE_CFLAGS), $(cc_stack_align_opt)=2)
53 export REALMODE_CFLAGS
54
55 # BITS is used as extension for files which are available in a 32 bit
56 @@ -65,8 +73,10 @@ ifeq ($(CONFIG_X86_32),y)
57 # with nonstandard options
58 KBUILD_CFLAGS += -fno-pic
59
60 - # prevent gcc from keeping the stack 16 byte aligned
61 - KBUILD_CFLAGS += $(call cc-option,-mpreferred-stack-boundary=2)
62 + # Align the stack to the register width instead of using the default
63 + # alignment of 16 bytes. This reduces stack usage and the number of
64 + # alignment instructions.
65 + KBUILD_CFLAGS += $(call cc-option,$(cc_stack_align_opt)=2)
66
67 # Disable unit-at-a-time mode on pre-gcc-4.0 compilers, it makes gcc use
68 # a lot more stack due to the lack of sharing of stacklots:
69 @@ -98,8 +108,14 @@ else
70 KBUILD_CFLAGS += $(call cc-option,-mno-80387)
71 KBUILD_CFLAGS += $(call cc-option,-mno-fp-ret-in-387)
72
73 - # Use -mpreferred-stack-boundary=3 if supported.
74 - KBUILD_CFLAGS += $(call cc-option,-mpreferred-stack-boundary=3)
75 + # By default gcc and clang use a stack alignment of 16 bytes for x86.
76 + # However the standard kernel entry on x86-64 leaves the stack on an
77 + # 8-byte boundary. If the compiler isn't informed about the actual
78 + # alignment it will generate extra alignment instructions for the
79 + # default alignment which keep the stack *mis*aligned.
80 + # Furthermore an alignment to the register width reduces stack usage
81 + # and the number of alignment instructions.
82 + KBUILD_CFLAGS += $(call cc-option,$(cc_stack_align_opt)=3)
83
84 # Use -mskip-rax-setup if supported.
85 KBUILD_CFLAGS += $(call cc-option,-mskip-rax-setup)