Updated Link-Time-Optimization patchkit
Subsystems (9)
This is the updated LTO patchkit for 3.14-rc2. LTO allows the compiler to do global optimization over the whole kernel.
All problems/comments reported last week have been addressed, plus a lot of improvements to the documentation and the Kconfig description. Plus some random improvements.
See the individual patches for a detailed description
Dependencies: asmlinkage patchkit (posted last week), kallsyms patchkit
Full git tree is ingit://git.kernel.org/pub/scm/linux/kernel/git/ak/linux-misc lto-3.14
-Andi
PATCH 1/19 x86, lto: Disable LTO for the x86 VDSO
From: Andi Kleen <[email protected]>
The VDSO does not play well with LTO, so just disable LTO for it. Also pass a 32bit linker flag for the 32bit version.
--- arch/x86/vdso/Makefile | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
arch/x86/vdso/Makefile +7 -3
@@ -2,6 +2,8 @@ # Building vDSO images for x86. # +KBUILD_CFLAGS += ${DISABLE_LTO} + VDSO64-$(CONFIG_X86_64) := y VDSOX32-$(CONFIG_X86_X32_ABI) := y VDSO32-$(CONFIG_X86_32) := y
@@ -35,7 +37,8 @@ export CPPFLAGS_vdso.lds += -P -C VDSO_LDFLAGS_vdso.lds = -m64 -Wl,-soname=linux-vdso.so.1 \ -Wl,--no-undefined \ - -Wl,-z,max-page-size=4096 -Wl,-z,common-page-size=4096 + -Wl,-z,max-page-size=4096 -Wl,-z,common-page-size=4096 \ + $(DISABLE_LTO) $(obj)/vdso.o: $(src)/vdso.S $(obj)/vdso.so
@@ -127,7 +130,7 @@ vdso32.so-$(VDSO32-y) += sysenter vdso32-images = $(vdso32.so-y:%=vdso32-%.so) CPPFLAGS_vdso32.lds = $(CPPFLAGS_vdso.lds) -VDSO_LDFLAGS_vdso32.lds = -m32 -Wl,-soname=linux-gate.so.1 +VDSO_LDFLAGS_vdso32.lds = -m32 -Wl,-m,elf_i386 -Wl,-soname=linux-gate.so.1 # This makes sure the $(obj) subdirectory exists even though vdso32/ # is not a kbuild sub-make subdirectory.
@@ -181,7 +184,8 @@ quiet_cmd_vdso = VDSO $@ -Wl,-T,$(filter %.lds,$^) $(filter %.o,$^) && \ sh $(srctree)/$(src)/checkundef.sh '$(NM)' '$@' -VDSO_LDFLAGS = -fPIC -shared $(call cc-ldoption, -Wl$(comma)--hash-style=sysv) +VDSO_LDFLAGS = -fPIC -shared $(call cc-ldoption, -Wl$(comma)--hash-style=sysv) \ + ${LTO_CFLAGS} GCOV_PROFILE := n #
Signature · 1 line
1.8.5.2
Re: [PATCH 01/19] x86, lto: Disable LTO for the x86 VDSO
Quoted 16 lines · Andi Kleen
From: Andi Kleen <[email protected]>
The VDSO does not play well with LTO, so just disable LTO for it. Also pass a 32bit linker flag for the 32bit version.
Signed-off-by Andi Kleen <[email protected]>--- arch/x86/vdso/Makefile | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)arch/x86/vdso/Makefile +0
-0
arch/x86/vdso/Makefile line 6
Quoted 5 lines
arch/x86/vdso/Makefile +2
-0@@ -2,6 +2,8 @@ # Building vDSO images for x86. # +KBUILD_CFLAGS += ${DISABLE_LTO} +
In kbuild files $() is usally preferred.
Sam
PATCH 2/19 lto: Disable LTO for hweight functions
In reply to Andi KleenFrom: Andi Kleen <[email protected]>
x86 calls the hweight library functions with special calling conventions. LTO doesn't support compiling individual files with special options. Just disable LTO for the file.
--- lib/Makefile | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
lib/Makefile +3 -1
@@ -46,7 +46,9 @@ obj-$(CONFIG_CHECK_SIGNATURE) += check_signature.o obj-$(CONFIG_DEBUG_LOCKING_API_SELFTESTS) += locking-selftest.o GCOV_PROFILE_hweight.o := n -CFLAGS_hweight.o = $(subst $(quote),,$(CONFIG_ARCH_HWEIGHT_CFLAGS)) +CFLAGS_hweight.o = $(subst $(quote),,$(CONFIG_ARCH_HWEIGHT_CFLAGS)) \ + $(DISABLE_LTO) + obj-$(CONFIG_GENERIC_HWEIGHT) += hweight.o obj-$(CONFIG_BTREE) += btree.o
Signature · 1 line
1.8.5.2
PATCH 3/19 lto: Make asmlinkage __visible
In reply to Andi KleenFrom: Andi Kleen <[email protected]>
Note this causes warnings for static asmlinkage, which is used in some places. These can be later cleaned up. static asmlinkage usually makes no sense.
--- include/linux/linkage.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
include/linux/linkage.h +2 -2
@@ -12,9 +12,9 @@#endif #ifdef __cplusplus -#define CPP_ASMLINKAGE extern "C" +#define CPP_ASMLINKAGE extern "C" __visible#else -#define CPP_ASMLINKAGE +#define CPP_ASMLINKAGE __visible#endif #ifndef asmlinkage
Signature · 1 line
1.8.5.2
Re: [PATCH 03/19] lto: Make asmlinkage __visible
Quoted 6 lines · Andi Kleen
From: Andi Kleen <[email protected]>
Note this causes warnings for static asmlinkage, which is used in some places. These can be later cleaned up. static asmlinkage usually makes no sense.
Reading the above changelog - it does not justify *why* asmlinkage is made visible.
Sam
PATCH 4/19 lto, workaround: Add workaround for initcall reordering
In reply to Andi KleenFrom: Andi Kleen <[email protected]>
Work around a LTO gcc problem: when there is no reference to a variable in a module it will be moved to the end of the program. This causes reordering of initcalls which the kernel does not like.
Add a dummy reference function to avoid this. The function is deleted by the linker.
This replaces a previous much slower workaround.
Thanks to Honza Hubicka for suggesting this technique.
--- include/linux/init.h | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-)
include/linux/init.h +19 -1
@@ -163,6 +163,23 @@ extern bool initcall_debug;#ifndef __ASSEMBLY__ +#ifdef CONFIG_LTO +/* Work around a LTO gcc problem: when there is no reference to a variable +* in a module it will be moved to the end of the program. This causes +* reordering of initcalls which the kernel does not like. +* Add a dummy reference function to avoid this. The function is +* deleted by the linker. +*/ +#define LTO_REFERENCE_INITCALL(x) \ + ; /* yes this is needed */ \ + static __used __exit void *reference_##x(void) \ + { \ + return &x; \ + } +#else +#define LTO_REFERENCE_INITCALL (x) +#endif +/* initcalls are now grouped by functionality into separate * subsections. Ordering inside the subsections is determined * by link order.
@@ -175,7 +192,8 @@ extern bool initcall_debug;#define __define_initcall (fn, id) \ static initcall_t __initcall_##fn##id __used \ - __attribute__((__section__(".initcall" #id ".init"))) = fn + __attribute__((__section__(".initcall" #id ".init"))) = fn; \ + LTO_REFERENCE_INITCALL(__initcall_##fn##id)/* * Early initcalls run before initializing SMP.
Signature · 1 line
1.8.5.2
PATCH 5/19 lto: Handle LTO common symbols in module loader
In reply to Andi KleenFrom: Joe Mario <[email protected]>
Here is the workaround I made for having the kernel not reject modules built with -flto. The clean solution would be to get the compiler to not emit the symbol. Or if it has to emit the symbol, then emit it as initialized data but put it into a comdat/linkonce section.
Minor tweaks by AK over Joe's patch.
--- kernel/module.c | 4 ++++ 1 file changed, 4 insertions(+)
kernel/module.c +4 -0
@@ -1948,6 +1948,10 @@ static int simplify_symbols(struct module *mod, const struct load_info *info)switch (sym[i].st_shndx) {case SHN_COMMON : +/* Ignore common symbols */ +if (!strncmp (name,"__gnu_lto" ,9 )) +break ; +/* We compiled with -fno-common. These are not supposed to happen. */ pr_debug ("Common symbol: %s\n" , name);
Signature · 1 line
1.8.5.2
PATCH 6/19 lto: Disable LTO for sys_ni
In reply to Andi KleenFrom: Andi Kleen <[email protected]>
The assembler alias code in cond_syscall does not work when compiled for LTO. Just disable LTO for that file.
--- kernel/Makefile | 3 +++ 1 file changed, 3 insertions(+)
kernel/Makefile +3 -0
@@ -18,6 +18,9 @@ CFLAGS_REMOVE_cgroup-debug.o = -pg CFLAGS_REMOVE_irq_work.o = -pg endif +# cond_syscall is currently not LTO compatible +CFLAGS_sys_ni.o = $(DISABLE_LTO) + obj-y += sched/ obj-y += locking/ obj-y += power/
Signature · 1 line
1.8.5.2
PATCH 7/19 lto: Don't let LATENCYTOP and LOCKDEP select KALLSYMS_ALL
In reply to Andi KleenFrom: Andi Kleen <[email protected]>
KALLSYMS_ALL enables including data variables into KALLSYMS. With plain KALLSYMS only functions are included.
LATENCYTOP and LOCKDEP select KALLSYMS_ALL in addition to KALLSYMS. It's unclear what they actually need _ALL for; they should only need function backtraces and afaik never touch variables.
LTO currently does not support KALLSYMS_ALL, which prevents LATENCYTOP and LOCKDEP from working and gives Kconfig errors. Disable the requirement for KALLSYMS_ALL for them, just use KALLSYMS.
--- lib/Kconfig.debug | 2 -- 1 file changed, 2 deletions(-)
lib/Kconfig.debug +0 -2
@@ -928,7 +928,6 @@ config LOCKDEP select STACKTRACE select FRAME_POINTER if !MIPS && !PPC && !ARM_UNWIND && !S390 && !MICROBLAZE && !ARC select KALLSYMS - select KALLSYMS_ALL config LOCK_STAT bool "Lock usage statistics"
@@ -1396,7 +1395,6 @@ config LATENCYTOP depends on PROC_FS select FRAME_POINTER if !MIPS && !PPC && !S390 && !MICROBLAZE && !ARM_UNWIND && !ARC select KALLSYMS - select KALLSYMS_ALL select STACKTRACE select SCHEDSTATS select SCHED_DEBUG
Signature · 1 line
1.8.5.2
PATCH 8/19 Kbuild, lto, workaround: Don't warn for initcall_reference in modpost
In reply to Andi KleenFrom: Andi Kleen <[email protected]>
This reference is discarded, but can cause warnings when it refers to exit. Ignore for now.
This is a workaround and can be removed once we get rid of -fno-toplevel-reorder
--- scripts/mod/modpost.c | 4 ++++ 1 file changed, 4 insertions(+)
scripts/mod/modpost.c +4 -0
@@ -1455,6 +1455,10 @@ static void check_section_mismatch(const char *modname, struct elf_info *elf, to =find_elf_symbol (elf, r->r_addend, sym); tosym =sym_name (elf, to); +if (!strncmp (fromsym,"reference___initcall" , +sizeof ("reference___initcall" )-1 )) +return ; +/* check whitelist - we may ignore it */ if (secref_whitelist (mismatch, fromsec, fromsym, tosec, tosym)) {
Signature · 1 line
1.8.5.2
Re: [PATCH 08/19] Kbuild, lto, workaround: Don't warn for initcall_reference in modpost
Quoted 18 lines · Andi Kleen
From: Andi Kleen <[email protected]>
This reference is discarded, but can cause warnings when it refers to exit. Ignore for now.
This is a workaround and can be removed once we get rid of -fno-toplevel-reorder
Signed-off-by Andi Kleen <[email protected]>--- scripts/mod/modpost.c | 4 ++++ 1 file changed, 4 insertions(+)scripts/mod/modpost.c +0
-0
scripts/mod/modpost.c static void check_section_mismatch(const char *modname, struct elf_info *elf, · line 1459
Quoted 5 lines
scripts/mod/modpost.c +2
-0@@ -1455,6 +1455,10 @@ static void check_section_mismatch(const char *modname, struct elf_info *elf, to =find_elf_symbol (elf, r->r_addend, sym); tosym =sym_name (elf, to); +if (!strncmp (fromsym,"reference___initcall" , +sizeof ("reference___initcall" )-1 ))
Please use spaces around "-"
Sam
PATCH 9/19 Kbuild, lto: Drop .number postfixes in modpost
In reply to Andi KleenFrom: Andi Kleen <[email protected]>
LTO turns all global symbols effectively into statics. This has the side effect that they all have a .NUMBER postfix to make them unique. In modpost drop this postfix because it confuses it.
--- scripts/mod/modpost.c | 15 ++++++++++++++- scripts/mod/modpost.h | 2 +- 2 files changed, 15 insertions(+), 2 deletions(-)
scripts/mod/modpost.c +14 -1
@@ -1684,6 +1684,19 @@ static void check_sec_ref(struct module *mod, const char *modname, } } +static char *remove_dot (char *s) +{ +char *end; +int n =strcspn (s,"." ); + +if (n >0 && s[n] !=0 ) { +strtoul (s + n +1 , &end,10 ); +if (end > s + n +1 && (*end =='.' || *end ==0 )) + s[n] =0 ; + } +return s; +} +static void read_symbols (char *modname) {const char *symname;
@@ -1722,7 +1735,7 @@ static void read_symbols(char *modname) }for (sym = info.symtab_start; sym < info.symtab_stop; sym++) { - symname = info.strtab + sym->st_name; + symname =remove_dot (info.strtab + sym->st_name);handle_modversions (mod, &info, sym, symname);handle_moddevtable (mod, &info, sym, symname);
scripts/mod/modpost.h +1 -1
@@ -127,7 +127,7 @@ struct elf_info {Elf_Section export_gpl_sec;Elf_Section export_unused_gpl_sec;Elf_Section export_gpl_future_sec; -const char *strtab; +char *strtab;char *modinfo;unsigned int modinfo_len;
Signature · 1 line
1.8.5.2
PATCH 10/19 Kbuild, lto: add ld-version and ld-ifversion macros
In reply to Andi KleenFrom: Andi Kleen <[email protected]>
To check the linker version. Used by the LTO makefile.
--- scripts/Kbuild.include | 9 +++++++++ scripts/ld-version.sh | 8 ++++++++ 2 files changed, 17 insertions(+) create mode 100755 scripts/ld-version.sh
scripts/Kbuild.include +9 -0
@@ -155,6 +155,15 @@ ld-option = $(call try-run,\ # Important: no spaces around options ar-option = $(call try-run, $(AR) rc$(1) "$$TMP",$(1),$(2)) +# ld-version +# Usage: $(call ld-version) +# Note this is mainly for HJ Lu's 3 number binutil versions +ld-version = $(shell $(LD) --version | $(srctree)/scripts/ld-version.sh) + +# ld-ifversion +# Usage: $(call ld-ifversion, -ge, 22252, y) +ld-ifversion = $(shell [ $(call ld-version) $(1) $(2) ] && echo $(3)) + ###### ###
scripts/ld-version.sh new file +8 -0
@@ -0,0 +1,8 @@ +#!/usr/bin/awk -f +# extract linker version number from stdin and turn into single number + { + gsub(".*)", ""); + split($1,a, "."); + print a[1]*10000000 + a[2]*100000 + a[3]*10000 + a[4]*100 + a[5]; + exit + }
Signature · 1 line
1.8.5.2
Re: [PATCH 10/19] Kbuild, lto: add ld-version and ld-ifversion macros
Quoted 3 lines · Andi Kleen
From: Andi Kleen <[email protected]>
To check the linker version. Used by the LTO makefile.
Everything in Kbuild.include is supposed to be documented in Documentation/kbuild/* Please add documentation too.
Sam
PATCH 11/19 Kbuild, lto: Add a gcc-ld script to let run gcc as ld
In reply to Andi KleenFrom: Andi Kleen <[email protected]>
For LTO we need to run the link step with gcc, not ld.
Since there are a lot of linker options passed to it, add a gcc-ld wrapper that wraps them as -Wl,
--- scripts/gcc-ld | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 scripts/gcc-ld
scripts/gcc-ld new file +34 -0
@@ -0,0 +1,34 @@ +#!/bin/sh +# run gcc with ld options +# used as a wrapper to execute link time optimizations +# yes virginia, this is not pretty + +case "${KBUILD_VERBOSE}" in +*1*) set -x ;; +esac + +ARGS="-nostdlib" + +while [ "$1" != "" ] ; do + case "$1" in + -save-temps|-m32|-m64) N="$1" ;; + -r) N="$1" ;; + -[Wg]*) N="$1" ;; + -[olv]|-[Ofd]*|-nostdlib) N="$1" ;; + --end-group|--start-group) + N="-Wl,$1" ;; + -[RTFGhIezcbyYu]*|\ +--script|--defsym|-init|-Map|--oformat|-rpath|\ +-rpath-link|--sort-section|--section-start|-Tbss|-Tdata|-Ttext|\ +--version-script|--dynamic-list|--version-exports-symbol|--wrap|-m) + A="$1" ; shift ; N="-Wl,$A,$1" ;; + --param) shift ; N="--param $1" ;; + -[m]*) N="$1" ;; + -*) N="-Wl,$1" ;; + *) N="$1" ;; + esac + ARGS="$ARGS $N" + shift +done + +exec $CC $ARGS
Signature · 1 line
1.8.5.2
PATCH 12/19 Kbuild, lto: Disable LTO for asm-offsets.c
In reply to Andi KleenFrom: Andi Kleen <[email protected]>
The asm-offset.c technique to fish data out of the assembler file does not work with LTO. Just disable for the asm-offset.c build.
--- scripts/Makefile.build | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/Makefile.build +1 -1
@@ -198,7 +198,7 @@ $(multi-objs-y:.o=.s) : modname = $(modname-multi) $(multi-objs-y:.o=.lst) : modname = $(modname-multi) quiet_cmd_cc_s_c = CC $(quiet_modtag) $@ -cmd_cc_s_c = $(CC) $(c_flags) -fverbose-asm -S -o $@ $< +cmd_cc_s_c = $(CC) $(c_flags) $(DISABLE_LTO) -fverbose-asm -S -o $@ $< $(obj)/%.s: $(src)/%.c FORCE $(call if_changed_dep,cc_s_c)
Signature · 1 line
1.8.5.2
PATCH 13/19 Kbuild, lto: Set TMPDIR for LTO v2
In reply to Andi KleenFrom: Andi Kleen <[email protected]>
LTO gcc puts a lot of data into $TMPDIR, essentially another copy of the object directory to pass the repartitioned object files to the code generation processes.
TMPDIR defaults to /tmp With /tmp as tmpfs it's easy to drive systems to out of memory, because they will compete with the already high anonymous memory consumption of the wpa LTO pass.
When LTO is set always set TMPDIR to the object directory. This could be slightly slower, but is far safer and eliminates another parameter the LTO user would need to set manually.
I made it conditional on LTO for now.
v2: Allow user to override (H. Peter Anvin)
--- Makefile | 8 ++++++++ 1 file changed, 8 insertions(+)
Makefile +8 -0
@@ -407,6 +407,14 @@ export KBUILD_AFLAGS_MODULE KBUILD_CFLAGS_MODULE KBUILD_LDFLAGS_MODULE export KBUILD_AFLAGS_KERNEL KBUILD_CFLAGS_KERNEL export KBUILD_ARFLAGS +ifdef CONFIG_LTO +# LTO gcc creates a lot of files in TMPDIR, and with /tmp as tmpfs +# it's easy to drive the machine OOM. Use the object directory +# instead +TMPDIR ?= ${objtree} +export TMPDIR +endif + # When compiling out-of-tree modules, put MODVERDIR in the module # tree rather than in the kernel tree. The kernel tree might # even be read-only.
Signature · 1 line
1.8.5.2
PATCH 14/19 Kbuild, lto: Handle basic LTO in modpost
In reply to Andi KleenFrom: Andi Kleen <[email protected]>
- Don't warn about LTO marker symbols. modpost runs before the linker, so the module is not necessarily LTOed yet.
- Don't complain about .gnu.lto* sections
--- scripts/mod/modpost.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
scripts/mod/modpost.c +5 -1
@@ -623,7 +623,10 @@ static void handle_modversions(struct module *mod, struct elf_info *info,switch (sym->st_shndx) {case SHN_COMMON : -warn ("\"%s\" [%s] is COMMON symbol\n" , symname, mod->name); +if (!strncmp (symname,"__gnu_lto_" ,sizeof ("__gnu_lto_" )-1 )) { +/* Should warn here, but modpost runs before the linker */ + }else +warn ("\"%s\" [%s] is COMMON symbol\n" , symname, mod->name);break ;case SHN_UNDEF :/* undefined symbol */
@@ -849,6 +852,7 @@ static const char *section_white_list[] =".xt.lit" ,/* xtensa */ ".arcextmap*" ,/* arc */ ".gnu.linkonce.arcext*" ,/* arc : modules */ +".gnu.lto*" , NULL };
Signature · 1 line
1.8.5.2
Re: [PATCH 14/19] Kbuild, lto: Handle basic LTO in modpost
Quoted 16 lines · Andi Kleen
From: Andi Kleen <[email protected]>
- Don't warn about LTO marker symbols. modpost runs before the linker, so the module is not necessarily LTOed yet.
- Don't complain about .gnu.lto* sectionsSigned-off-by Andi Kleen <[email protected]>--- scripts/mod/modpost.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)scripts/mod/modpost.c +0
-0
scripts/mod/modpost.c static void handle_modversions(struct module *mod, struct elf_info *info, · line 626
Quoted 5 lines
scripts/mod/modpost.c +1
-1@@ -623,7 +623,10 @@ static void handle_modversions(struct module *mod, struct elf_info *info,switch (sym->st_shndx) {case SHN_COMMON : -warn ("\"%s\" [%s] is COMMON symbol\n" , symname, mod->name); +if (!strncmp (symname,"__gnu_lto_" ,sizeof ("__gnu_lto_" )-1 )) {
Please use space around "-".
Sam
PATCH 15/19 Kbuild, lto: Fix single pass kallsyms for LTO
In reply to Andi KleenFrom: Andi Kleen <[email protected]>
gcc-nm on slim LTO objects does not output static functions or variables.
This causes the first pass estimation of the kallsyms table to be off too much. Add a hack using the LTO function sections to retrieve all functions instead. I wrote that hack in perl, as it exceeded my awk-fu (this adds a build dependency on perl, but only if LTO is active). The hack also doesn't support variables, but we handle that by disable KALLSYMS_ALL with LTO. The hack is also somewhat depending on the internal LTO object format.
Hopefully at some future point gcc-nm will be fixed and this won't be necessary anymore.
One issue is that LTO can generate new symbols in the final link, for example when cloning functions. These clones are just copies of existing names with a postfix (which we remove), so they compress well in the kallsyms compression.
With that we can get away by just adding a 5% safety factor to the first pass kallsyms estimation, and hope all clones fit into that. If some obscure build has more clones than that the PAD_RATIO value in kallsyms.c can be lowered to increase it.
--- init/Kconfig | 5 ++++- scripts/link-vmlinux.sh | 24 +++++++++++++++++++++--- 2 files changed, 25 insertions(+), 4 deletions(-)
init/Kconfig +4 -1
@@ -1317,7 +1317,10 @@ config KALLSYMS config KALLSYMS_ALL bool "Include all symbols in kallsyms" - depends on DEBUG_KERNEL && KALLSYMS + # the method LTO uses to predict the symbol table + # only supports functions for now + # This can be removed once http://gcc.gnu.org/PR60016 is fixed + depends on DEBUG_KERNEL && KALLSYMS && !LTO help Normally kallsyms only contains the symbols of functions for nicer OOPS messages and backtraces (i.e., symbols from the text and inittext
scripts/link-vmlinux.sh +21 -3
@@ -90,10 +90,28 @@ kallsyms() local aflags="${KBUILD_AFLAGS} ${KBUILD_AFLAGS_KERNEL} \ ${NOSTDINC_FLAGS} ${LINUXINCLUDE} ${KBUILD_CPPFLAGS}" - ${NM} -n ${1} | \ - awk 'NF == 3 { print}' | - scripts/kallsyms ${kallsymopt} | \ + # workaround for slim LTO gcc-nm not outputing static symbols + # http://gcc.gnu.org/PR60016 + # generate a fake symbol table based on the LTO function sections. + # This unfortunately "knows" about the internal LTO file format + # and only works for functions + # needs perl for now when building for LTO + ( + if $OBJDUMP --section-headers ${1} | grep -q \.gnu\.lto_ ; then + ${OBJDUMP} --section-headers ${1} | + perl -ne ' +@n = split; +next unless $n[1] =~ /\.gnu\.lto_([_a-zA-Z][^.]+)/; +next if $n[1] eq $prev; +$prev = $n[1]; +print "0 T ",$1,"\n"' + fi + ${NM} -n ${1} | awk 'NF == 3 { print }' + ) > ${2}_sym + # run without pipe to make kallsyms errors stop the script + ./scripts/kallsyms ${kallsymopt} < ${2}_sym | ${CC} ${aflags} -c -o ${2} -x assembler-with-cpp - + } # Create map file with all symbols from ${1}
Signature · 1 line
1.8.5.2
PATCH 16/19 Kbuild, lto: Add Link Time Optimization support v2
In reply to Andi KleenFrom: Andi Kleen <[email protected]>
With LTO gcc will do whole program optimizations for the whole kernel and each module. This increases compile time, but can generate faster and smaller code and allows the compiler to do global checking. For example the compiler can complain now about type mismatches for symbols between different files.
LTO allows gcc to inline functions between different files and do various other optimization across the whole binary.
It might also trigger bugs due to more aggressive optimizations. It allows gcc to drop unused code. It also allows it to check types over the whole program.
The compile time is definitely slower. For gcc 4.8 on a typical monolithic config it is about 58% slower. 4.9 drastically improved performance, with slowdown being 38% or so. Also incremenential rebuilds are somewhat slower, as the whole kernel always needs to be reoptimized. Very modular kernels have less build time slow down, as the LTO will run for each module individually.
This adds the basic Kbuild plumbing for LTO:
- In Kbuild add a new scripts/Makefile.lto that checks
the tool chain (note the checks may not be fully bulletproof) and when the tests pass sets the LTO options
Currently LTO is very finicky about the tool chain.
- Add a new LDFINAL variable that controls the final link for vmlinux or module. In this case we call gcc-ld instead of ld, to run the LTO step.
- For slim LTO builds (object files containing no backup executable) force AR to gcc-ar
- Theoretically LTO should pass through compiler options from the compiler to the link step, but this doesn't work for all options. So the Makefile sets most of these options manually.
- Kconfigs:
Since LTO with allyesconfig needs more than 4G of memory (~8G) and has the potential to makes people's system swap to death. I used a nested config that ensures that a simple allyesconfig disables LTO. It has to be explicitely
enabled.
- Some depencies on other Kconfigs:
MODVERSIONS, GCOV, FUNCTION_TRACER, KALLSYMS_ALL, single chain WCHAN are incompatible with LTO currently, mostly because they
they require setting special compiler options
for specific files, which LTO currently doesn't support. MODVERSIONS should in principle work with gcc 4.9, but still disabled. FUNCTION_TRACER/GCOV can be fixed with a unmerged gcc patch.
- Also disable strict copy user checks because they trigger errors with LTO.
- modpost symbol checking is downgraded to a warning,
as in some cases modpost runs before the final link
and it cannot resolve LTO symbols at this point.
For more information see Documentation/lto-build
Thanks to HJ Lu, Joe Mario, Honza Hubicka, Richard Guenther, Don Zickus, Changlong Xie who helped with this project (and probably some more who I forgot, sorry)
v2:
Merge documentation file into this patch
Improve documentation and Kconfig, fix a lot of obsolete comments. Exclude READABLE_ASM
Some random fixes
--- Documentation/lto-build | 173 +++++++++++++++++++++++++++++++++++++++++++++++ Makefile | 9 ++- arch/x86/Kconfig | 2 +- init/Kconfig | 81 ++++++++++++++++++++++ kernel/gcov/Kconfig | 2 +- lib/Kconfig.debug | 2 +- scripts/Makefile.lto | 85 +++++++++++++++++++++++ scripts/Makefile.modpost | 7 +- scripts/link-vmlinux.sh | 2 +- 9 files changed, 355 insertions(+), 8 deletions(-) create mode 100644 Documentation/lto-build create mode 100644 scripts/Makefile.lto
Documentation/lto-build new file +173 -0
@@ -0,0 +1,173 @@ +Link time optimization (LTO) for the Linux kernel + +This is an experimental feature. + +Link Time Optimization allows the compiler to optimize the complete program +instead of just each file. LTO requires at least gcc 4.8 (but +works more efficiently with 4.9+) LTO requires Linux binutils (the normal FSF +releases used in many distributions do not work at the moment) + +The compiler can inline functions between files and do various other global +optimizations, like specializing functions for common parameters, +determing when global variables are clobbered, making functions pure/const, +propagating constants globally, removing unneeded data and others. + +It will also drop unused functions which can make the kernel +image smaller in some circumstances, in particular for small kernel +configurations. + +For small monolithic kernels it can throw away unused code very effectively +(especially when modules are disabled) and usually shrinks +the code size. + +Build time and memory consumption at build time will increase, depending +on the size of the largest binary. Modular kernels are less affected. +With LTO incremental builds are less incremental, as always the whole +binary needs to be re-optimized (but not re-parsed) + +Oops can be somewhat more difficult to read, due to the more aggressive +inlining. + +Normal "reasonable" builds work with less than 4GB of RAM, but very large +configurations like allyesconfig may need more memory. The actual +memory needed depends on the available memory (gcc sizes its garbage +collector pools based on that or on the ulimit -m limits) and +the compiler version. + +gcc 4.9+ has much better build performance and less memory consumption + +- A few kernel features are currently incompatible with LTO, in particular +function tracing, because they require special compiler flags for +specific files, which is not supported in LTO right now. +- Jobserver control for -j does not work correctly for the final +LTO phase due to some problems with the kernel's pipe code. +The makefiles hard codes -j<number of online cpus> for the final +LTO phase to work around for this + +Configuration: +- Enable CONFIG_LTO_MENU and then disable CONFIG_LTO_DISABLE. +This is mainly to not have allyesconfig default to LTO. +- FUNCTION_TRACER, STACK_TRACER, FUNCTION_GRAPH_TRACER, KALLSYMS_ALL, GCOV +have to disabled because they are currently incompatible with LTO. +- MODVERSIONS have to be disabled (may work with 4.9+) + +Requirements: +- Enough memory: 4GB for a standard build, more for allyesconfig +The peak memory usage happens single threaded (when lto-wpa merges types), +so dialing back -j options will not help much. + +A 32bit compiler is unlikely to work due to the memory requirements. +You can however build a kernel targeted at 32bit on a 64bit host. + +Example build procedure: + +Simplified procedure for distributions that have gcc 4.8, but not +the Linux binutils (for example openSUSE 13.1 or FC20): + +The LTO builds requires gcc-nm/gcc-ar. Some distributions ship +those in separate packages, which may need to be explicitely installed. + +- Get the latest Linux binutils from +http://www.kernel.org/pub/linux/devel/binutils/ +and unpack it. + +We install it in a separate directory to not overwrite the system binutils. + +# replace VERSION with respective version numbers + +cd binutils* +# don't forget the --enable-plugins! +./configure --prefix=/opt/binutils-VERSION --enable-plugins +make -j $(getconf _NPROCESSORS_ONLN) && sudo make install + +Fix up the kernel configuration to allow LTO: + +<start with a suitable kernel configuration> +./source/scripts/config --disable function_tracer \ + --disable function_graph_tracer \ + --disable stack_tracer --enable lto_menu \ + --disable lto_disable \ + --disable gcov \ + --disable kallsyms_all \ + --disable modversions +make oldconfig + +Then you can build with + +# The COMPILER_PATH is needed to let gcc use the new binutils +# as the LTO plugin linker +# if you installed gcc in a separate directory like below also +# add it to the PATH line below before the regular $PATH +# The COMPILER_PATH setting is only needed if the gcc was not built +# with --with-plugin-ld pointing to the Linux binutils ld +# The AR/NM setting works around a Makefile bug +COMPILER_PATH=/opt/binutils-VERSION/bin PATH=$COMPILER_PATH:$PATH \ +make -j$(getconf _NPROCESSORS_ONLN) AR=gcc-ar NM=gcc-nm + +If you don't have gcc 4.8+ as system compiler you would also need +to install that compiler. In this case I recommend getting +a gcc 4.9+ snapshot from http://gcc.gnu.org (or release when available), +as it builds much faster for LTO than 4.8. + +Here's an example build procedure: + +Assuming gcc is unpacked in gcc-VERSION + +cd gcc-VERSION +./contrib/download_preqrequisites +cd .. + +mkdir obj-gcc +# please don't skip this cd. the build will not work correctly in the +# source dir, you have to use the separate object dir +cd obj-gcc +../gcc-VERSION/configure --prefix=/opt/gcc-VERSION --enable-lto \ +--with-plugin-ld=/opt/binutils-VERSION/bin/ld +--disable-nls --enable-languages=c,c++ \ +--disable-libstdcxx-pch +make -j$(getconf _NPROCESSORS_ONLN) +sudo make install-no-fixedincludes + +FAQs: + +Q: I get a section type attribute conflict +A: Usually because of someone doing +const __initdata (should be const __initconst) or const __read_mostly +(should be just const). Check both symbols reported by gcc. + +Q: I see lots of undefined symbols for memcmp etc. +A: Usually because NM=gcc-nm AR=gcc-ar are missing. +The Makefile tries to set those automatically, but it doesn't always +work. Better to set it manually on the make command line. + +Q: It's quite slow / uses too much memory. +A: Consider a gcc 4.9 snapshot/release (not released yet) +The main problem in 4.8 is the type merging in the single threaded WPA pass, +which has been improved considerably in 4.9 by running it distributed. + +Q: It's still slow +A: It'll always be somewhat slower than non LTO sorry. + +Q: What's up with .XXXXX numeric post fixes +A: This is due LTO turning (near) all symbols to static +Use gcc 4.9, it avoids them in most cases. They are also filtered out +in kallsyms. + +References: + +Presentation on Kernel LTO +(note, performance numbers/details outdated. In particular gcc 4.9 fixed +most of the build time problems): +http://halobates.de/kernel-lto.pdf + +Generic gcc LTO: +http://www.ucw.cz/~hubicka/slides/labs2013.pdf +http://www.hipeac.net/system/files/barcelona.pdf + +Somewhat outdated too: +http://gcc.gnu.org/projects/lto/lto.pdf +http://gcc.gnu.org/projects/lto/whopr.pdf + +Happy Link-Time-Optimizing! + +Andi Kleen
Makefile +8 -1
@@ -335,9 +335,14 @@ include $(srctree)/scripts/Kbuild.include AS = $(CROSS_COMPILE)as LD = $(CROSS_COMPILE)ld +LDFINAL = $(LD) CC = $(CROSS_COMPILE)gcc CPP = $(CC) -E +ifdef CONFIG_LTO_SLIM +AR = $(CROSS_COMPILE)gcc-ar +else AR = $(CROSS_COMPILE)ar +endif NM = $(CROSS_COMPILE)nm STRIP = $(CROSS_COMPILE)strip OBJCOPY = $(CROSS_COMPILE)objcopy
@@ -396,7 +401,7 @@ KERNELVERSION = $(VERSION)$(if $(PATCHLEVEL),.$(PATCHLEVEL)$(if $(SUBLEVEL),.$(S export VERSION PATCHLEVEL SUBLEVEL KERNELRELEASE KERNELVERSION export ARCH SRCARCH CONFIG_SHELL HOSTCC HOSTCFLAGS CROSS_COMPILE AS LD CC -export CPP AR NM STRIP OBJCOPY OBJDUMP +export CPP AR NM STRIP OBJCOPY OBJDUMP LDFINAL export MAKE AWK GENKSYMS INSTALLKERNEL PERL UTS_MACHINE export HOSTCXX HOSTCXXFLAGS LDFLAGS_MODULE CHECK CHECKFLAGS
@@ -707,6 +712,8 @@ ifeq ($(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-goto.sh $(CC)), y) KBUILD_CFLAGS += -DCC_HAVE_ASM_GOTO endif +include ${srctree}/scripts/Makefile.lto + # Add user supplied CPPFLAGS, AFLAGS and CFLAGS as the last assignments KBUILD_CPPFLAGS += $(KCPPFLAGS) KBUILD_AFLAGS += $(KAFLAGS)
arch/x86/Kconfig +1 -1
@@ -577,7 +577,7 @@ config X86_32_IRIS config SCHED_OMIT_FRAME_POINTER def_bool y - prompt "Single-depth WCHAN output" + prompt "Single-depth WCHAN output" if !LTO && !FRAME_POINTER depends on X86 ---help--- Calculate simpler /proc/<PID>/wchan values. If this option
init/Kconfig +81 -0
93 lines not on this page · raw message
kernel/gcov/Kconfig +1 -1
@@ -2,7 +2,7 @@ menu "GCOV-based kernel profiling" config GCOV_KERNEL bool "Enable gcov-based kernel profiling" - depends on DEBUG_FS + depends on DEBUG_FS && !LTO select CONSTRUCTORS if !UML default n ---help---
lib/Kconfig.debug +1 -1
@@ -180,7 +180,7 @@ config STRIP_ASM_SYMS config READABLE_ASM bool "Generate readable assembler code" - depends on DEBUG_KERNEL + depends on DEBUG_KERNEL && !LTO help Disable some compiler optimizations that tend to generate human unreadable assembler output. This may make the kernel slightly slower, but it helps
scripts/Makefile.lto new file +85 -0
85 lines not on this page · raw message
scripts/Makefile.modpost +4 -3
@@ -77,7 +77,8 @@ modpost = scripts/mod/modpost \ $(if $(KBUILD_EXTRA_SYMBOLS), $(patsubst %, -e %,$(KBUILD_EXTRA_SYMBOLS))) \ $(if $(KBUILD_EXTMOD),-o $(modulesymfile)) \ $(if $(CONFIG_DEBUG_SECTION_MISMATCH),,-S) \ - $(if $(KBUILD_EXTMOD)$(KBUILD_MODPOST_WARN),-w) + $(if $(KBUILD_EXTMOD)$(KBUILD_MODPOST_WARN),-w) \ + $(if $(CONFIG_LTO),-w) MODPOST_OPT=$(subst -i,-n,$(filter -i,$(MAKEFLAGS)))
@@ -115,8 +116,8 @@ $(modules:.ko=.mod.o): %.mod.o: %.mod.c FORCE targets += $(modules:.ko=.mod.o) # Step 6), final link of the modules -quiet_cmd_ld_ko_o = LD [M] $@ - cmd_ld_ko_o = $(LD) -r $(LDFLAGS) \ +quiet_cmd_ld_ko_o = LDFINAL [M] $@ + cmd_ld_ko_o = $(LDFINAL) -r $(LDFLAGS) \ $(KBUILD_LDFLAGS_MODULE) $(LDFLAGS_MODULE) \ -o $@ $(filter-out FORCE,$^)
scripts/link-vmlinux.sh +1 -1
@@ -53,7 +53,7 @@ vmlinux_link() local lds="${objtree}/${KBUILD_LDS}" if [ "${SRCARCH}" != "um" ]; then - ${LD} ${LDFLAGS} ${LDFLAGS_vmlinux} -o ${2} \ + ${LDFINAL} ${LDFLAGS} ${LDFLAGS_vmlinux} -o ${2} \ -T ${lds} ${KBUILD_VMLINUX_INIT} \ --start-group ${KBUILD_VMLINUX_MAIN} --end-group ${1} else
Signature · 1 line
1.8.5.2
PATCH 17/19 lto: Mark spinlocks noinline when inline spinlocks are disabled
In reply to Andi KleenFrom: Andi Kleen <[email protected]>
Otherwise LTO will inline them anyways
--- kernel/locking/spinlock.c | 56 +++++++++++++++++++++++------------------------ 1 file changed, 28 insertions(+), 28 deletions(-)
kernel/locking/spinlock.c +28 -28
@@ -130,7 +130,7 @@ BUILD_LOCK_OPS(write, rwlock);#endif #ifndef CONFIG_INLINE_SPIN_TRYLOCK -int __lockfunc _raw_spin_trylock(raw_spinlock_t *lock) +noinline int __lockfunc _raw_spin_trylock(raw_spinlock_t *lock) {return __raw_spin_trylock (lock); }
@@ -138,7 +138,7 @@ EXPORT_SYMBOL(_raw_spin_trylock);#endif #ifndef CONFIG_INLINE_SPIN_TRYLOCK_BH -int __lockfunc _raw_spin_trylock_bh(raw_spinlock_t *lock) +noinline int __lockfunc _raw_spin_trylock_bh(raw_spinlock_t *lock) {return __raw_spin_trylock_bh (lock); }
@@ -146,7 +146,7 @@ EXPORT_SYMBOL(_raw_spin_trylock_bh);#endif #ifndef CONFIG_INLINE_SPIN_LOCK -void __lockfunc _raw_spin_lock(raw_spinlock_t *lock) +noinline void __lockfunc _raw_spin_lock(raw_spinlock_t *lock) {__raw_spin_lock (lock); }
@@ -154,7 +154,7 @@ EXPORT_SYMBOL(_raw_spin_lock);#endif #ifndef CONFIG_INLINE_SPIN_LOCK_IRQSAVE -unsigned long __lockfunc _raw_spin_lock_irqsave (raw_spinlock_t *lock) +noinline unsigned long __lockfunc _raw_spin_lock_irqsave(raw_spinlock_t *lock) {return __raw_spin_lock_irqsave (lock); }
@@ -162,7 +162,7 @@ EXPORT_SYMBOL(_raw_spin_lock_irqsave);#endif #ifndef CONFIG_INLINE_SPIN_LOCK_IRQ -void __lockfunc _raw_spin_lock_irq(raw_spinlock_t *lock) +noinline void __lockfunc _raw_spin_lock_irq(raw_spinlock_t *lock) {__raw_spin_lock_irq (lock); }
@@ -170,7 +170,7 @@ EXPORT_SYMBOL(_raw_spin_lock_irq);#endif #ifndef CONFIG_INLINE_SPIN_LOCK_BH -void __lockfunc _raw_spin_lock_bh(raw_spinlock_t *lock) +noinline void __lockfunc _raw_spin_lock_bh(raw_spinlock_t *lock) {__raw_spin_lock_bh (lock); }
@@ -178,7 +178,7 @@ EXPORT_SYMBOL(_raw_spin_lock_bh);#endif #ifdef CONFIG_UNINLINE_SPIN_UNLOCK -void __lockfunc _raw_spin_unlock(raw_spinlock_t *lock) +noinline void __lockfunc _raw_spin_unlock(raw_spinlock_t *lock) {__raw_spin_unlock (lock); }
@@ -186,7 +186,7 @@ EXPORT_SYMBOL(_raw_spin_unlock);#endif #ifndef CONFIG_INLINE_SPIN_UNLOCK_IRQRESTORE -void __lockfunc _raw_spin_unlock_irqrestore(raw_spinlock_t *lock, unsigned long flags) +noinline void __lockfunc _raw_spin_unlock_irqrestore(raw_spinlock_t *lock, unsigned long flags) {__raw_spin_unlock_irqrestore (lock, flags); }
@@ -194,7 +194,7 @@ EXPORT_SYMBOL(_raw_spin_unlock_irqrestore);#endif #ifndef CONFIG_INLINE_SPIN_UNLOCK_IRQ -void __lockfunc _raw_spin_unlock_irq(raw_spinlock_t *lock) +noinline void __lockfunc _raw_spin_unlock_irq(raw_spinlock_t *lock) {__raw_spin_unlock_irq (lock); }
@@ -202,7 +202,7 @@ EXPORT_SYMBOL(_raw_spin_unlock_irq);#endif #ifndef CONFIG_INLINE_SPIN_UNLOCK_BH -void __lockfunc _raw_spin_unlock_bh(raw_spinlock_t *lock) +noinline void __lockfunc _raw_spin_unlock_bh(raw_spinlock_t *lock) {__raw_spin_unlock_bh (lock); }
@@ -210,7 +210,7 @@ EXPORT_SYMBOL(_raw_spin_unlock_bh);#endif #ifndef CONFIG_INLINE_READ_TRYLOCK -int __lockfunc _raw_read_trylock(rwlock_t *lock) +noinline int __lockfunc _raw_read_trylock(rwlock_t *lock) {return __raw_read_trylock (lock); }
@@ -218,7 +218,7 @@ EXPORT_SYMBOL(_raw_read_trylock);#endif #ifndef CONFIG_INLINE_READ_LOCK -void __lockfunc _raw_read_lock(rwlock_t *lock) +noinline void __lockfunc _raw_read_lock(rwlock_t *lock) {__raw_read_lock (lock); }
@@ -226,7 +226,7 @@ EXPORT_SYMBOL(_raw_read_lock);#endif #ifndef CONFIG_INLINE_READ_LOCK_IRQSAVE -unsigned long __lockfunc _raw_read_lock_irqsave (rwlock_t *lock) +noinline unsigned long __lockfunc _raw_read_lock_irqsave(rwlock_t *lock) {return __raw_read_lock_irqsave (lock); }
@@ -234,7 +234,7 @@ EXPORT_SYMBOL(_raw_read_lock_irqsave);#endif #ifndef CONFIG_INLINE_READ_LOCK_IRQ -void __lockfunc _raw_read_lock_irq(rwlock_t *lock) +noinline void __lockfunc _raw_read_lock_irq(rwlock_t *lock) {__raw_read_lock_irq (lock); }
@@ -242,7 +242,7 @@ EXPORT_SYMBOL(_raw_read_lock_irq);#endif #ifndef CONFIG_INLINE_READ_LOCK_BH -void __lockfunc _raw_read_lock_bh(rwlock_t *lock) +noinline void __lockfunc _raw_read_lock_bh(rwlock_t *lock) {__raw_read_lock_bh (lock); }
@@ -250,7 +250,7 @@ EXPORT_SYMBOL(_raw_read_lock_bh);#endif #ifndef CONFIG_INLINE_READ_UNLOCK -void __lockfunc _raw_read_unlock(rwlock_t *lock) +noinline void __lockfunc _raw_read_unlock(rwlock_t *lock) {__raw_read_unlock (lock); }
@@ -258,7 +258,7 @@ EXPORT_SYMBOL(_raw_read_unlock);#endif #ifndef CONFIG_INLINE_READ_UNLOCK_IRQRESTORE -void __lockfunc _raw_read_unlock_irqrestore(rwlock_t *lock, unsigned long flags) +noinline void __lockfunc _raw_read_unlock_irqrestore(rwlock_t *lock, unsigned long flags) {__raw_read_unlock_irqrestore (lock, flags); }
@@ -266,7 +266,7 @@ EXPORT_SYMBOL(_raw_read_unlock_irqrestore);#endif #ifndef CONFIG_INLINE_READ_UNLOCK_IRQ -void __lockfunc _raw_read_unlock_irq(rwlock_t *lock) +noinline void __lockfunc _raw_read_unlock_irq(rwlock_t *lock) {__raw_read_unlock_irq (lock); }
@@ -274,7 +274,7 @@ EXPORT_SYMBOL(_raw_read_unlock_irq);#endif #ifndef CONFIG_INLINE_READ_UNLOCK_BH -void __lockfunc _raw_read_unlock_bh(rwlock_t *lock) +noinline void __lockfunc _raw_read_unlock_bh(rwlock_t *lock) {__raw_read_unlock_bh (lock); }
@@ -282,7 +282,7 @@ EXPORT_SYMBOL(_raw_read_unlock_bh);#endif #ifndef CONFIG_INLINE_WRITE_TRYLOCK -int __lockfunc _raw_write_trylock(rwlock_t *lock) +noinline int __lockfunc _raw_write_trylock(rwlock_t *lock) {return __raw_write_trylock (lock); }
@@ -290,7 +290,7 @@ EXPORT_SYMBOL(_raw_write_trylock);#endif #ifndef CONFIG_INLINE_WRITE_LOCK -void __lockfunc _raw_write_lock(rwlock_t *lock) +noinline void __lockfunc _raw_write_lock(rwlock_t *lock) {__raw_write_lock (lock); }
@@ -298,7 +298,7 @@ EXPORT_SYMBOL(_raw_write_lock);#endif #ifndef CONFIG_INLINE_WRITE_LOCK_IRQSAVE -unsigned long __lockfunc _raw_write_lock_irqsave (rwlock_t *lock) +noinline unsigned long __lockfunc _raw_write_lock_irqsave(rwlock_t *lock) {return __raw_write_lock_irqsave (lock); }
@@ -306,7 +306,7 @@ EXPORT_SYMBOL(_raw_write_lock_irqsave);#endif #ifndef CONFIG_INLINE_WRITE_LOCK_IRQ -void __lockfunc _raw_write_lock_irq(rwlock_t *lock) +noinline void __lockfunc _raw_write_lock_irq(rwlock_t *lock) {__raw_write_lock_irq (lock); }
@@ -314,7 +314,7 @@ EXPORT_SYMBOL(_raw_write_lock_irq);#endif #ifndef CONFIG_INLINE_WRITE_LOCK_BH -void __lockfunc _raw_write_lock_bh(rwlock_t *lock) +noinline void __lockfunc _raw_write_lock_bh(rwlock_t *lock) {__raw_write_lock_bh (lock); }
@@ -322,7 +322,7 @@ EXPORT_SYMBOL(_raw_write_lock_bh);#endif #ifndef CONFIG_INLINE_WRITE_UNLOCK -void __lockfunc _raw_write_unlock(rwlock_t *lock) +noinline void __lockfunc _raw_write_unlock(rwlock_t *lock) {__raw_write_unlock (lock); }
@@ -330,7 +330,7 @@ EXPORT_SYMBOL(_raw_write_unlock);#endif #ifndef CONFIG_INLINE_WRITE_UNLOCK_IRQRESTORE -void __lockfunc _raw_write_unlock_irqrestore(rwlock_t *lock, unsigned long flags) +noinline void __lockfunc _raw_write_unlock_irqrestore(rwlock_t *lock, unsigned long flags) {__raw_write_unlock_irqrestore (lock, flags); }
@@ -338,7 +338,7 @@ EXPORT_SYMBOL(_raw_write_unlock_irqrestore);#endif #ifndef CONFIG_INLINE_WRITE_UNLOCK_IRQ -void __lockfunc _raw_write_unlock_irq(rwlock_t *lock) +noinline void __lockfunc _raw_write_unlock_irq(rwlock_t *lock) {__raw_write_unlock_irq (lock); }
@@ -346,7 +346,7 @@ EXPORT_SYMBOL(_raw_write_unlock_irq);#endif #ifndef CONFIG_INLINE_WRITE_UNLOCK_BH -void __lockfunc _raw_write_unlock_bh(rwlock_t *lock) +noinline void __lockfunc _raw_write_unlock_bh(rwlock_t *lock) {__raw_write_unlock_bh (lock); }
Signature · 1 line
1.8.5.2
PATCH 18/19 lto, module: Warn about modules that are not fully LTOed
In reply to Andi KleenFrom: Andi Kleen <[email protected]>
When __gnu_lto_* is present that means that the module hasn't run with LTO yet.
--- kernel/module.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
kernel/module.c +4 -1
@@ -1949,8 +1949,11 @@ static int simplify_symbols(struct module *mod, const struct load_info *info)switch (sym[i].st_shndx) {case SHN_COMMON :/* Ignore common symbols */ -if (!strncmp (name,"__gnu_lto" ,9 )) +if (!strncmp (name,"__gnu_lto" ,9 )) { +pr_info ("%s: module not link time optimized\n" , + mod->name);break ; + }/* We compiled with -fno-common. These are not supposed to happen. */
Signature · 1 line
1.8.5.2
PATCH 19/19 lto: Don't inline __const_udelay
In reply to Andi KleenFrom: Andi Kleen <[email protected]>
__const_udelay is marked inline, and LTO will happily inline it everywhere Dropping the inline saves ~44k text in a non LTO build.
13999560 1740864 1499136 17239560 1070e08 vmlinux-with-udelay-inline 13954764 1736768 1499136 17190668 1064f0c vmlinux-wo-udelay-inline
--- arch/x86/lib/delay.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/lib/delay.c +1 -1
@@ -112,7 +112,7 @@ void __delay(unsigned long loops) }EXPORT_SYMBOL (__delay); -inline void __const_udelay (unsigned long xloops) +void __const_udelay (unsigned long xloops) {int d0;
Signature · 1 line
1.8.5.2
Quoted 2 lines · Andi Kleen
This is the updated LTO patchkit for 3.14-rc2. LTO allows the compiler to do global optimization over the whole kernel.
It is mildly annoying that one couldn't use vanilla binutils. Have you already opened bugs on sourceware.org/bugzilla/ to get this fixed for gold and ld.bfd?
Signature · 1 line
Markus
Quoted 7 lines · Markus Trippelsdorf
Quoted 2 lines · Andi Kleen
This is the updated LTO patchkit for 3.14-rc2. LTO allows the compiler to do global optimization over the whole kernel.
It is mildly annoying that one couldn't use vanilla binutils. Have you already opened bugs on sourceware.org/bugzilla/ to get this fixed for gold and ld.bfd?
The problem is supporting "pass through" of both pure (.S) assembler code and LTO through ld -r, because the kernel makefiles use ld -r heavily. Standard binutils would throw all the assembler away when in plugin LTO mode.
HJ did the work (and fixed a couple other bugs that blocked kernel LTO), but the mainline binutils people rejected the patches. You can judge for yourself if their reasons make sense:
https://sourceware.org/ml/binutils/2012-05/msg00057.html
Noone has done the work for gold so far.
There was some effort from the Fedora binutils maintainers to add the necessary patches to their version, but at least what is in FC20 doesn't seem to be enough.
-Andi
Signature · 1 line
[email protected] -- Speaking for myself only.
Quoted 18 lines · Andi Kleen
Quoted 7 lines · Markus Trippelsdorf
Quoted 2 lines · Andi Kleen
This is the updated LTO patchkit for 3.14-rc2. LTO allows the compiler to do global optimization over the whole kernel.
It is mildly annoying that one couldn't use vanilla binutils. Have you already opened bugs on sourceware.org/bugzilla/ to get this fixed for gold and ld.bfd?
The problem is supporting "pass through" of both pure (.S) assembler code and LTO through ld -r, because the kernel makefiles use ld -r heavily. Standard binutils would throw all the assembler away when in plugin LTO mode.
HJ did the work (and fixed a couple other bugs that blocked kernel LTO), but the mainline binutils people rejected the patches. You can judge for yourself if their reasons make sense:
Quoted 7 lines
https://sourceware.org/ml/binutils/2012-05/msg00057.html
Noone has done the work for gold so far.
There was some effort from the Fedora binutils maintainers to add the necessary patches to their version, but at least what is in FC20 doesn't seem to be enough.
There are some additional LTO bug fixes in my Linux binutils.
Signature · 1 line
H.J.
Hi Andi.
Quoted 5 lines · Andi Kleen
Quoted 4 lines · Markus Trippelsdorf
Quoted 2 lines · Andi Kleen
This is the updated LTO patchkit for 3.14-rc2. LTO allows the compiler to do global optimization over the whole kernel.
Quoted 8 lines
Quoted 3 lines
It is mildly annoying that one couldn't use vanilla binutils. Have you already opened bugs on sourceware.org/bugzilla/ to get this fixed for gold and ld.bfd?
The problem is supporting "pass through" of both pure (.S) assembler code and LTO through ld -r, because the kernel makefiles use ld -r heavily. Standard binutils would throw all the assembler away when in plugin LTO mode.
Long time ago I looked at eliminating the use of -r in the kernel build. I lost the patch - but the attached patch managed to build
a "make defconfig; make" kernel.
I have not event tried boot with this - it only managed to build!
What the patch does is for each directory visited a built-in.link file is created which is really a linker script.
It uses INPUT (file file file) to specify all the object files.
And in the final link the files are all read and the link is performed.
This limit the depth to 10 levels due to a restriction in ld.
The binutils people have suggested some other methods that I did not look into.
Half of the patch is fixes to the security makefiles which I will submit anyway as this needs to be cleaned up indendent on this patch.
The patch drops $(cmd_secanalysis) because I did not look into this.
If this could make it easier to enable LTO then this would be a nice win.
Sam
Makefile +5 -5
@@ -788,12 +788,12 @@ vmlinux-alldirs := $(sort $(vmlinux-dirs) $(patsubst %/,%,$(filter %/, \ $(core-n) $(core-) $(drivers-n) $(drivers-) \ $(net-n) $(net-) $(libs-n) $(libs-)))) -init-y := $(patsubst %/, %/built-in.o, $(init-y)) -core-y := $(patsubst %/, %/built-in.o, $(core-y)) -drivers-y := $(patsubst %/, %/built-in.o, $(drivers-y)) -net-y := $(patsubst %/, %/built-in.o, $(net-y)) +init-y := $(patsubst %/, %/built-in.link, $(init-y)) +core-y := $(patsubst %/, %/built-in.link, $(core-y)) +drivers-y := $(patsubst %/, %/built-in.link, $(drivers-y)) +net-y := $(patsubst %/, %/built-in.link, $(net-y)) libs-y1 := $(patsubst %/, %/lib.a, $(libs-y)) -libs-y2 := $(patsubst %/, %/built-in.o, $(libs-y)) +libs-y2 := $(patsubst %/, %/built-in.link, $(libs-y)) libs-y := $(libs-y1) $(libs-y2) # Externally visible symbols (used by link-vmlinux.sh)
scripts/Makefile.build +2 -5
@@ -135,7 +135,7 @@ lib-target := $(obj)/lib.a endif ifneq ($(strip $(obj-y) $(obj-m) $(obj-n) $(obj-) $(subdir-m) $(lib-target)),) -builtin-target := $(obj)/built-in.o +builtin-target := $(obj)/built-in.link endif modorder-target := $(obj)/modules.order
@@ -378,10 +378,7 @@ $(sort $(subdir-obj-y)): $(subdir-ym) ; ifdef builtin-target quiet_cmd_link_o_target = LD $@ # If the list of objects to link is empty, just create an empty built-in.o -cmd_link_o_target = $(if $(strip $(obj-y)),\ - $(LD) $(ld_flags) -r -o $@ $(filter $(obj-y), $^) \ - $(cmd_secanalysis),\ - rm -f $@; $(AR) rcs$(KBUILD_ARFLAGS) $@) +cmd_link_o_target = $(if $(filter $(obj-y), $^), echo INPUT\($(filter $(obj-y), $^)\) > $@, echo "/* empty */" > $@) $(builtin-target): $(obj-y) FORCE $(call if_changed,link_o_target)
scripts/Makefile.lib +2 -2
@@ -39,7 +39,7 @@ __subdir-y := $(patsubst %/,%,$(filter %/, $(obj-y))) subdir-y += $(__subdir-y) __subdir-m := $(patsubst %/,%,$(filter %/, $(obj-m))) subdir-m += $(__subdir-m) -obj-y := $(patsubst %/, %/built-in.o, $(obj-y)) +obj-y := $(patsubst %/, %/built-in.link, $(obj-y)) obj-m := $(filter-out %/, $(obj-m)) # Subdirectories we need to descend into
@@ -60,7 +60,7 @@ multi-objs := $(multi-objs-y) $(multi-objs-m) # $(subdir-obj-y) is the list of objects in $(obj-y) which uses dir/ to # tell kbuild to descend -subdir-obj-y := $(filter %/built-in.o, $(obj-y)) +subdir-obj-y := $(filter %/built-in.link, $(obj-y)) # $(obj-dirs) is a list of directories that contain object files obj-dirs := $(dir $(multi-objs) $(obj-y))
security/Makefile +6 -6
@@ -16,14 +16,14 @@ obj-$(CONFIG_MMU) += min_addr.o # Object file lists obj-$(CONFIG_SECURITY) += security.o capability.o obj-$(CONFIG_SECURITYFS) += inode.o -obj-$(CONFIG_SECURITY_SELINUX) += selinux/built-in.o -obj-$(CONFIG_SECURITY_SMACK) += smack/built-in.o +obj-$(CONFIG_SECURITY_SELINUX) += selinux/ +obj-$(CONFIG_SECURITY_SMACK) += smack/ obj-$(CONFIG_AUDIT) += lsm_audit.o -obj-$(CONFIG_SECURITY_TOMOYO) += tomoyo/built-in.o -obj-$(CONFIG_SECURITY_APPARMOR) += apparmor/built-in.o -obj-$(CONFIG_SECURITY_YAMA) += yama/built-in.o +obj-$(CONFIG_SECURITY_TOMOYO) += tomoyo/ +obj-$(CONFIG_SECURITY_APPARMOR) += apparmor/ +obj-$(CONFIG_SECURITY_YAMA) += yama/ obj-$(CONFIG_CGROUP_DEVICE) += device_cgroup.o # Object integrity file lists subdir-$(CONFIG_INTEGRITY) += integrity -obj-$(CONFIG_INTEGRITY) += integrity/built-in.o +obj-$(CONFIG_INTEGRITY) += integrity/
security/integrity/Makefile +2 -2
@@ -10,6 +10,6 @@ obj-$(CONFIG_INTEGRITY_ASYMMETRIC_KEYS) += digsig_asymmetric.o integrity-y := iint.o subdir-$(CONFIG_IMA) += ima -obj-$(CONFIG_IMA) += ima/built-in.o +obj-$(CONFIG_IMA) += ima/ subdir-$(CONFIG_EVM) += evm -obj-$(CONFIG_EVM) += evm/built-in.o +obj-$(CONFIG_EVM) += evm/
Quoted 31 lines · Sam Ravnborg
Hi Andi.
Quoted 5 lines · Andi Kleen
Quoted 4 lines · Markus Trippelsdorf
Quoted 2 lines · Andi Kleen
This is the updated LTO patchkit for 3.14-rc2. LTO allows the compiler to do global optimization over the whole kernel.
Quoted 8 lines
Quoted 3 lines
It is mildly annoying that one couldn't use vanilla binutils. Have you already opened bugs on sourceware.org/bugzilla/ to get this fixed for gold and ld.bfd?
The problem is supporting "pass through" of both pure (.S) assembler code and LTO through ld -r, because the kernel makefiles use ld -r heavily. Standard binutils would throw all the assembler away when in plugin LTO mode.
Long time ago I looked at eliminating the use of -r in the kernel build. I lost the patch - but the attached patch managed to build
a "make defconfig; make" kernel.I have not event tried boot with this - it only managed to build!
What the patch does is for each directory visited a built-in.link file is created which is really a linker script.
It uses INPUT (file file file) to specify all the object files.
And in the final link the files are all read and the link is performed.This limit the depth to 10 levels due to a restriction in ld.
The binutils people have suggested some other methods that I did not look into.
Quoted 8 lines
Half of the patch is fixes to the security makefiles which I will submit anyway as this needs to be cleaned up indendent on this patch.
The patch drops $(cmd_secanalysis) because I did not look into this.
If this could make it easier to enable LTO then this would be a nice win.
I gave it a quick try and it failed like below with LTO
Right now using the Linux binutils is ok for me.
-Andi
+ ld -m elf_x86_64 -r -o vmlinux.o arch/x86/kernel/head_64.o arch/x86/kernel/head64.o arch/x86/kernel/head.o init/built-in.link --start-group usr/built-in.link arch/x86/built-in.link kernel/built-in.link mm/built-in.link fs/built-in.link ipc/built-in.link security/built-in.link crypto/built-in.link block/built-in.link lib/lib.a arch/x86/lib/lib.a lib/built-in.link arch/x86/lib/built-in.link drivers/built-in.link sound/built-in.link firmware/built-in.link arch/x86/pci/built-in.link arch/x86/oprofile/built-in.link arch/x86/power/built-in.link arch/x86/video/built-in.link net/built-in.link --end-group lib/lib.a: could not read symbols: Bad value