Skip to content

Commit 79db4d2

Browse files
committed
Merge tag 'clang-lto-v5.12-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux
Pull clang LTO updates from Kees Cook: "Clang Link Time Optimization. This is built on the work done preparing for LTO by arm64 folks, tracing folks, etc. This includes the core changes as well as the remaining pieces for arm64 (LTO has been the default build method on Android for about 3 years now, as it is the prerequisite for the Control Flow Integrity protections). While x86 LTO enablement is done, it depends on some pending objtool clean-ups. It's possible that I'll send a "part 2" pull request for LTO that includes x86 support. For merge log posterity, and as detailed in commit dc5723b ("kbuild: add support for Clang LTO"), here is the lt;dr to do an LTO build: make LLVM=1 LLVM_IAS=1 defconfig scripts/config -e LTO_CLANG_THIN make LLVM=1 LLVM_IAS=1 (To do a cross-compile of arm64, add "CROSS_COMPILE=aarch64-linux-gnu-" and "ARCH=arm64" to the "make" command lines.) Summary: - Clang LTO build infrastructure and arm64-specific enablement (Sami Tolvanen) - Recursive build CC_FLAGS_LTO fix (Alexander Lobakin)" * tag 'clang-lto-v5.12-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux: kbuild: prevent CC_FLAGS_LTO self-bloating on recursive rebuilds arm64: allow LTO to be selected arm64: disable recordmcount with DYNAMIC_FTRACE_WITH_REGS arm64: vdso: disable LTO drivers/misc/lkdtm: disable LTO for rodata.o efi/libstub: disable LTO scripts/mod: disable LTO for empty.c modpost: lto: strip .lto from module names PCI: Fix PREL32 relocations for LTO init: lto: fix PREL32 relocations init: lto: ensure initcall ordering kbuild: lto: add a default list of used symbols kbuild: lto: merge module sections kbuild: lto: limit inlining kbuild: lto: fix module versioning kbuild: add support for Clang LTO tracing: move function tracer options to Kconfig
2 parents 3b9cdaf + 2b86895 commit 79db4d2

24 files changed

Lines changed: 704 additions & 59 deletions

File tree

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
*.so.dbg
4343
*.su
4444
*.symtypes
45+
*.symversions
4546
*.tab.[ch]
4647
*.tar
4748
*.xz

‎Makefile‎

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -853,19 +853,21 @@ KBUILD_CFLAGS += $(DEBUG_CFLAGS)
853853
export DEBUG_CFLAGS
854854

855855
ifdef CONFIG_FUNCTION_TRACER
856-
ifdef CONFIG_FTRACE_MCOUNT_RECORD
857-
# gcc 5 supports generating the mcount tables directly
858-
ifeq ($(call cc-option-yn,-mrecord-mcount),y)
859-
CC_FLAGS_FTRACE += -mrecord-mcount
860-
export CC_USING_RECORD_MCOUNT := 1
861-
endif
856+
ifdef CONFIG_FTRACE_MCOUNT_USE_CC
857+
CC_FLAGS_FTRACE += -mrecord-mcount
862858
ifdef CONFIG_HAVE_NOP_MCOUNT
863859
ifeq ($(call cc-option-yn, -mnop-mcount),y)
864860
CC_FLAGS_FTRACE += -mnop-mcount
865861
CC_FLAGS_USING += -DCC_USING_NOP_MCOUNT
866862
endif
867863
endif
868864
endif
865+
ifdef CONFIG_FTRACE_MCOUNT_USE_RECORDMCOUNT
866+
ifdef CONFIG_HAVE_C_RECORDMCOUNT
867+
BUILD_C_RECORDMCOUNT := y
868+
export BUILD_C_RECORDMCOUNT
869+
endif
870+
endif
869871
ifdef CONFIG_HAVE_FENTRY
870872
ifeq ($(call cc-option-yn, -mfentry),y)
871873
CC_FLAGS_FTRACE += -mfentry
@@ -875,12 +877,6 @@ endif
875877
export CC_FLAGS_FTRACE
876878
KBUILD_CFLAGS += $(CC_FLAGS_FTRACE) $(CC_FLAGS_USING)
877879
KBUILD_AFLAGS += $(CC_FLAGS_USING)
878-
ifdef CONFIG_DYNAMIC_FTRACE
879-
ifdef CONFIG_HAVE_C_RECORDMCOUNT
880-
BUILD_C_RECORDMCOUNT := y
881-
export BUILD_C_RECORDMCOUNT
882-
endif
883-
endif
884880
endif
885881

886882
# We trigger additional mismatches with less inlining
@@ -899,6 +895,24 @@ KBUILD_CFLAGS += $(CC_FLAGS_SCS)
899895
export CC_FLAGS_SCS
900896
endif
901897

898+
ifdef CONFIG_LTO_CLANG
899+
ifdef CONFIG_LTO_CLANG_THIN
900+
CC_FLAGS_LTO := -flto=thin -fsplit-lto-unit
901+
KBUILD_LDFLAGS += --thinlto-cache-dir=$(extmod-prefix).thinlto-cache
902+
else
903+
CC_FLAGS_LTO := -flto
904+
endif
905+
CC_FLAGS_LTO += -fvisibility=hidden
906+
907+
# Limit inlining across translation units to reduce binary size
908+
KBUILD_LDFLAGS += -mllvm -import-instr-limit=5
909+
endif
910+
911+
ifdef CONFIG_LTO
912+
KBUILD_CFLAGS += $(CC_FLAGS_LTO)
913+
export CC_FLAGS_LTO
914+
endif
915+
902916
ifdef CONFIG_DEBUG_FORCE_FUNCTION_ALIGN_32B
903917
KBUILD_CFLAGS += -falign-functions=32
904918
endif
@@ -1493,7 +1507,7 @@ MRPROPER_FILES += include/config include/generated \
14931507
*.spec
14941508

14951509
# Directories & files removed with 'make distclean'
1496-
DISTCLEAN_FILES += tags TAGS cscope* GPATH GTAGS GRTAGS GSYMS
1510+
DISTCLEAN_FILES += tags TAGS cscope* GPATH GTAGS GRTAGS GSYMS .thinlto-cache
14971511

14981512
# clean - Delete most, but leave enough to build external modules
14991513
#
@@ -1739,7 +1753,7 @@ PHONY += compile_commands.json
17391753

17401754
clean-dirs := $(KBUILD_EXTMOD)
17411755
clean: rm-files := $(KBUILD_EXTMOD)/Module.symvers $(KBUILD_EXTMOD)/modules.nsdeps \
1742-
$(KBUILD_EXTMOD)/compile_commands.json
1756+
$(KBUILD_EXTMOD)/compile_commands.json $(KBUILD_EXTMOD)/.thinlto-cache
17431757

17441758
PHONY += help
17451759
help:
@@ -1836,7 +1850,8 @@ clean: $(clean-dirs)
18361850
-o -name '.tmp_*.o.*' \
18371851
-o -name '*.c.[012]*.*' \
18381852
-o -name '*.ll' \
1839-
-o -name '*.gcno' \) -type f -print | xargs rm -f
1853+
-o -name '*.gcno' \
1854+
-o -name '*.*.symversions' \) -type f -print | xargs rm -f
18401855

18411856
# Generate tags for editors
18421857
# ---------------------------------------------------------------------------

‎arch/Kconfig‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,96 @@ config SHADOW_CALL_STACK
603603
reading and writing arbitrary memory may be able to locate them
604604
and hijack control flow by modifying the stacks.
605605

606+
config LTO
607+
bool
608+
help
609+
Selected if the kernel will be built using the compiler's LTO feature.
610+
611+
config LTO_CLANG
612+
bool
613+
select LTO
614+
help
615+
Selected if the kernel will be built using Clang's LTO feature.
616+
617+
config ARCH_SUPPORTS_LTO_CLANG
618+
bool
619+
help
620+
An architecture should select this option if it supports:
621+
- compiling with Clang,
622+
- compiling inline assembly with Clang's integrated assembler,
623+
- and linking with LLD.
624+
625+
config ARCH_SUPPORTS_LTO_CLANG_THIN
626+
bool
627+
help
628+
An architecture should select this option if it can support Clang's
629+
ThinLTO mode.
630+
631+
config HAS_LTO_CLANG
632+
def_bool y
633+
# Clang >= 11: https://github.com/ClangBuiltLinux/linux/issues/510
634+
depends on CC_IS_CLANG && CLANG_VERSION >= 110000 && LD_IS_LLD
635+
depends on $(success,test $(LLVM) -eq 1)
636+
depends on $(success,test $(LLVM_IAS) -eq 1)
637+
depends on $(success,$(NM) --help | head -n 1 | grep -qi llvm)
638+
depends on $(success,$(AR) --help | head -n 1 | grep -qi llvm)
639+
depends on ARCH_SUPPORTS_LTO_CLANG
640+
depends on !FTRACE_MCOUNT_USE_RECORDMCOUNT
641+
depends on !KASAN
642+
depends on !GCOV_KERNEL
643+
help
644+
The compiler and Kconfig options support building with Clang's
645+
LTO.
646+
647+
choice
648+
prompt "Link Time Optimization (LTO)"
649+
default LTO_NONE
650+
help
651+
This option enables Link Time Optimization (LTO), which allows the
652+
compiler to optimize binaries globally.
653+
654+
If unsure, select LTO_NONE. Note that LTO is very resource-intensive
655+
so it's disabled by default.
656+
657+
config LTO_NONE
658+
bool "None"
659+
help
660+
Build the kernel normally, without Link Time Optimization (LTO).
661+
662+
config LTO_CLANG_FULL
663+
bool "Clang Full LTO (EXPERIMENTAL)"
664+
depends on HAS_LTO_CLANG
665+
depends on !COMPILE_TEST
666+
select LTO_CLANG
667+
help
668+
This option enables Clang's full Link Time Optimization (LTO), which
669+
allows the compiler to optimize the kernel globally. If you enable
670+
this option, the compiler generates LLVM bitcode instead of ELF
671+
object files, and the actual compilation from bitcode happens at
672+
the LTO link step, which may take several minutes depending on the
673+
kernel configuration. More information can be found from LLVM's
674+
documentation:
675+
676+
https://llvm.org/docs/LinkTimeOptimization.html
677+
678+
During link time, this option can use a large amount of RAM, and
679+
may take much longer than the ThinLTO option.
680+
681+
config LTO_CLANG_THIN
682+
bool "Clang ThinLTO (EXPERIMENTAL)"
683+
depends on HAS_LTO_CLANG && ARCH_SUPPORTS_LTO_CLANG_THIN
684+
select LTO_CLANG
685+
help
686+
This option enables Clang's ThinLTO, which allows for parallel
687+
optimization and faster incremental compiles compared to the
688+
CONFIG_LTO_CLANG_FULL option. More information can be found
689+
from Clang's documentation:
690+
691+
https://clang.llvm.org/docs/ThinLTO.html
692+
693+
If unsure, say Y.
694+
endchoice
695+
606696
config HAVE_ARCH_WITHIN_STACK_FRAMES
607697
bool
608698
help

‎arch/arm64/Kconfig‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ config ARM64
7373
select ARCH_SUPPORTS_DEBUG_PAGEALLOC
7474
select ARCH_SUPPORTS_MEMORY_FAILURE
7575
select ARCH_SUPPORTS_SHADOW_CALL_STACK if CC_HAVE_SHADOW_CALL_STACK
76+
select ARCH_SUPPORTS_LTO_CLANG if CPU_LITTLE_ENDIAN
77+
select ARCH_SUPPORTS_LTO_CLANG_THIN
7678
select ARCH_SUPPORTS_ATOMIC_RMW
7779
select ARCH_SUPPORTS_INT128 if CC_HAS_INT128 && (GCC_VERSION >= 50000 || CC_IS_CLANG)
7880
select ARCH_SUPPORTS_NUMA_BALANCING
@@ -162,6 +164,8 @@ config ARM64
162164
select HAVE_DYNAMIC_FTRACE
163165
select HAVE_DYNAMIC_FTRACE_WITH_REGS \
164166
if $(cc-option,-fpatchable-function-entry=2)
167+
select FTRACE_MCOUNT_USE_PATCHABLE_FUNCTION_ENTRY \
168+
if DYNAMIC_FTRACE_WITH_REGS
165169
select HAVE_EFFICIENT_UNALIGNED_ACCESS
166170
select HAVE_FAST_GUP
167171
select HAVE_FTRACE_MCOUNT_RECORD

‎arch/arm64/kernel/vdso/Makefile‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ ldflags-y := -shared -nostdlib -soname=linux-vdso.so.1 --hash-style=sysv \
2929
ccflags-y := -fno-common -fno-builtin -fno-stack-protector -ffixed-x18
3030
ccflags-y += -DDISABLE_BRANCH_PROFILING -DBUILD_VDSO
3131

32-
CFLAGS_REMOVE_vgettimeofday.o = $(CC_FLAGS_FTRACE) -Os $(CC_FLAGS_SCS) $(GCC_PLUGINS_CFLAGS)
32+
CFLAGS_REMOVE_vgettimeofday.o = $(CC_FLAGS_FTRACE) -Os $(CC_FLAGS_SCS) $(GCC_PLUGINS_CFLAGS) \
33+
$(CC_FLAGS_LTO)
3334
KASAN_SANITIZE := n
3435
UBSAN_SANITIZE := n
3536
OBJECT_FILES_NON_STANDARD := y

‎drivers/firmware/efi/libstub/Makefile‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ KBUILD_CFLAGS := $(cflags-y) -Os -DDISABLE_BRANCH_PROFILING \
3838

3939
# remove SCS flags from all objects in this directory
4040
KBUILD_CFLAGS := $(filter-out $(CC_FLAGS_SCS), $(KBUILD_CFLAGS))
41+
# disable LTO
42+
KBUILD_CFLAGS := $(filter-out $(CC_FLAGS_LTO), $(KBUILD_CFLAGS))
4143

4244
GCOV_PROFILE := n
4345
# Sanitizer runtimes are unavailable and cannot be linked here.

‎drivers/misc/lkdtm/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ lkdtm-$(CONFIG_PPC_BOOK3S_64) += powerpc.o
1616
KASAN_SANITIZE_rodata.o := n
1717
KASAN_SANITIZE_stackleak.o := n
1818
KCOV_INSTRUMENT_rodata.o := n
19+
CFLAGS_REMOVE_rodata.o += $(CC_FLAGS_LTO)
1920

2021
OBJCOPYFLAGS :=
2122
OBJCOPYFLAGS_rodata_objcopy.o := \

‎include/asm-generic/vmlinux.lds.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,13 @@
9090
* .data. We don't want to pull in .data..other sections, which Linux
9191
* has defined. Same for text and bss.
9292
*
93+
* With LTO_CLANG, the linker also splits sections by default, so we need
94+
* these macros to combine the sections during the final link.
95+
*
9396
* RODATA_MAIN is not used because existing code already defines .rodata.x
9497
* sections to be brought in with rodata.
9598
*/
96-
#ifdef CONFIG_LD_DEAD_CODE_DATA_ELIMINATION
99+
#if defined(CONFIG_LD_DEAD_CODE_DATA_ELIMINATION) || defined(CONFIG_LTO_CLANG)
97100
#define TEXT_MAIN .text .text.[0-9a-zA-Z_]*
98101
#define DATA_MAIN .data .data.[0-9a-zA-Z_]* .data..L* .data..compoundliteral* .data.$__unnamed_* .data.$Lubsan_*
99102
#define SDATA_MAIN .sdata .sdata.[0-9a-zA-Z_]*

‎include/linux/init.h‎

Lines changed: 70 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -184,19 +184,80 @@ extern bool initcall_debug;
184184
* as KEEP() in the linker script.
185185
*/
186186

187+
/* Format: <modname>__<counter>_<line>_<fn> */
188+
#define __initcall_id(fn) \
189+
__PASTE(__KBUILD_MODNAME, \
190+
__PASTE(__, \
191+
__PASTE(__COUNTER__, \
192+
__PASTE(_, \
193+
__PASTE(__LINE__, \
194+
__PASTE(_, fn))))))
195+
196+
/* Format: __<prefix>__<iid><id> */
197+
#define __initcall_name(prefix, __iid, id) \
198+
__PASTE(__, \
199+
__PASTE(prefix, \
200+
__PASTE(__, \
201+
__PASTE(__iid, id))))
202+
203+
#ifdef CONFIG_LTO_CLANG
204+
/*
205+
* With LTO, the compiler doesn't necessarily obey link order for
206+
* initcalls. In order to preserve the correct order, we add each
207+
* variable into its own section and generate a linker script (in
208+
* scripts/link-vmlinux.sh) to specify the order of the sections.
209+
*/
210+
#define __initcall_section(__sec, __iid) \
211+
#__sec ".init.." #__iid
212+
213+
/*
214+
* With LTO, the compiler can rename static functions to avoid
215+
* global naming collisions. We use a global stub function for
216+
* initcalls to create a stable symbol name whose address can be
217+
* taken in inline assembly when PREL32 relocations are used.
218+
*/
219+
#define __initcall_stub(fn, __iid, id) \
220+
__initcall_name(initstub, __iid, id)
221+
222+
#define __define_initcall_stub(__stub, fn) \
223+
int __init __stub(void); \
224+
int __init __stub(void) \
225+
{ \
226+
return fn(); \
227+
} \
228+
__ADDRESSABLE(__stub)
229+
#else
230+
#define __initcall_section(__sec, __iid) \
231+
#__sec ".init"
232+
233+
#define __initcall_stub(fn, __iid, id) fn
234+
235+
#define __define_initcall_stub(__stub, fn) \
236+
__ADDRESSABLE(fn)
237+
#endif
238+
187239
#ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS
188-
#define ___define_initcall(fn, id, __sec) \
189-
__ADDRESSABLE(fn) \
190-
asm(".section \"" #__sec ".init\", \"a\" \n" \
191-
"__initcall_" #fn #id ": \n" \
192-
".long " #fn " - . \n" \
240+
#define ____define_initcall(fn, __stub, __name, __sec) \
241+
__define_initcall_stub(__stub, fn) \
242+
asm(".section \"" __sec "\", \"a\" \n" \
243+
__stringify(__name) ": \n" \
244+
".long " __stringify(__stub) " - . \n" \
193245
".previous \n");
194246
#else
195-
#define ___define_initcall(fn, id, __sec) \
196-
static initcall_t __initcall_##fn##id __used \
197-
__attribute__((__section__(#__sec ".init"))) = fn;
247+
#define ____define_initcall(fn, __unused, __name, __sec) \
248+
static initcall_t __name __used \
249+
__attribute__((__section__(__sec))) = fn;
198250
#endif
199251

252+
#define __unique_initcall(fn, id, __sec, __iid) \
253+
____define_initcall(fn, \
254+
__initcall_stub(fn, __iid, id), \
255+
__initcall_name(initcall, __iid, id), \
256+
__initcall_section(__sec, __iid))
257+
258+
#define ___define_initcall(fn, id, __sec) \
259+
__unique_initcall(fn, id, __sec, __initcall_id(fn))
260+
200261
#define __define_initcall(fn, id) ___define_initcall(fn, id, .initcall##id)
201262

202263
/*
@@ -236,7 +297,7 @@ extern bool initcall_debug;
236297
#define __exitcall(fn) \
237298
static exitcall_t __exitcall_##fn __exit_call = fn
238299

239-
#define console_initcall(fn) ___define_initcall(fn,, .con_initcall)
300+
#define console_initcall(fn) ___define_initcall(fn, con, .con_initcall)
240301

241302
struct obs_kernel_param {
242303
const char *str;

0 commit comments

Comments
 (0)