Commit 79db4d22 authored by Linus Torvalds's avatar Linus Torvalds
Browse files
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 dc5723b0
  ("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
parents 3b9cdafb 2b868952
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -42,6 +42,7 @@
*.so.dbg
*.su
*.symtypes
*.symversions
*.tab.[ch]
*.tar
*.xz
+30 −15
Original line number Diff line number Diff line
@@ -853,12 +853,8 @@ KBUILD_CFLAGS += $(DEBUG_CFLAGS)
export DEBUG_CFLAGS

ifdef CONFIG_FUNCTION_TRACER
ifdef CONFIG_FTRACE_MCOUNT_RECORD
  # gcc 5 supports generating the mcount tables directly
  ifeq ($(call cc-option-yn,-mrecord-mcount),y)
ifdef CONFIG_FTRACE_MCOUNT_USE_CC
  CC_FLAGS_FTRACE	+= -mrecord-mcount
    export CC_USING_RECORD_MCOUNT := 1
  endif
  ifdef CONFIG_HAVE_NOP_MCOUNT
    ifeq ($(call cc-option-yn, -mnop-mcount),y)
      CC_FLAGS_FTRACE	+= -mnop-mcount
@@ -866,6 +862,12 @@ ifdef CONFIG_FTRACE_MCOUNT_RECORD
    endif
  endif
endif
ifdef CONFIG_FTRACE_MCOUNT_USE_RECORDMCOUNT
  ifdef CONFIG_HAVE_C_RECORDMCOUNT
    BUILD_C_RECORDMCOUNT := y
    export BUILD_C_RECORDMCOUNT
  endif
endif
ifdef CONFIG_HAVE_FENTRY
  ifeq ($(call cc-option-yn, -mfentry),y)
    CC_FLAGS_FTRACE	+= -mfentry
@@ -875,12 +877,6 @@ endif
export CC_FLAGS_FTRACE
KBUILD_CFLAGS	+= $(CC_FLAGS_FTRACE) $(CC_FLAGS_USING)
KBUILD_AFLAGS	+= $(CC_FLAGS_USING)
ifdef CONFIG_DYNAMIC_FTRACE
	ifdef CONFIG_HAVE_C_RECORDMCOUNT
		BUILD_C_RECORDMCOUNT := y
		export BUILD_C_RECORDMCOUNT
	endif
endif
endif

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

ifdef CONFIG_LTO_CLANG
ifdef CONFIG_LTO_CLANG_THIN
CC_FLAGS_LTO	:= -flto=thin -fsplit-lto-unit
KBUILD_LDFLAGS	+= --thinlto-cache-dir=$(extmod-prefix).thinlto-cache
else
CC_FLAGS_LTO	:= -flto
endif
CC_FLAGS_LTO	+= -fvisibility=hidden

# Limit inlining across translation units to reduce binary size
KBUILD_LDFLAGS += -mllvm -import-instr-limit=5
endif

ifdef CONFIG_LTO
KBUILD_CFLAGS	+= $(CC_FLAGS_LTO)
export CC_FLAGS_LTO
endif

ifdef CONFIG_DEBUG_FORCE_FUNCTION_ALIGN_32B
KBUILD_CFLAGS += -falign-functions=32
endif
@@ -1493,7 +1507,7 @@ MRPROPER_FILES += include/config include/generated \
		  *.spec

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

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

clean-dirs := $(KBUILD_EXTMOD)
clean: rm-files := $(KBUILD_EXTMOD)/Module.symvers $(KBUILD_EXTMOD)/modules.nsdeps \
	$(KBUILD_EXTMOD)/compile_commands.json
	$(KBUILD_EXTMOD)/compile_commands.json $(KBUILD_EXTMOD)/.thinlto-cache

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

# Generate tags for editors
# ---------------------------------------------------------------------------
+90 −0
Original line number Diff line number Diff line
@@ -603,6 +603,96 @@ config SHADOW_CALL_STACK
	  reading and writing arbitrary memory may be able to locate them
	  and hijack control flow by modifying the stacks.

config LTO
	bool
	help
	  Selected if the kernel will be built using the compiler's LTO feature.

config LTO_CLANG
	bool
	select LTO
	help
	  Selected if the kernel will be built using Clang's LTO feature.

config ARCH_SUPPORTS_LTO_CLANG
	bool
	help
	  An architecture should select this option if it supports:
	  - compiling with Clang,
	  - compiling inline assembly with Clang's integrated assembler,
	  - and linking with LLD.

config ARCH_SUPPORTS_LTO_CLANG_THIN
	bool
	help
	  An architecture should select this option if it can support Clang's
	  ThinLTO mode.

config HAS_LTO_CLANG
	def_bool y
	# Clang >= 11: https://github.com/ClangBuiltLinux/linux/issues/510
	depends on CC_IS_CLANG && CLANG_VERSION >= 110000 && LD_IS_LLD
	depends on $(success,test $(LLVM) -eq 1)
	depends on $(success,test $(LLVM_IAS) -eq 1)
	depends on $(success,$(NM) --help | head -n 1 | grep -qi llvm)
	depends on $(success,$(AR) --help | head -n 1 | grep -qi llvm)
	depends on ARCH_SUPPORTS_LTO_CLANG
	depends on !FTRACE_MCOUNT_USE_RECORDMCOUNT
	depends on !KASAN
	depends on !GCOV_KERNEL
	help
	  The compiler and Kconfig options support building with Clang's
	  LTO.

choice
	prompt "Link Time Optimization (LTO)"
	default LTO_NONE
	help
	  This option enables Link Time Optimization (LTO), which allows the
	  compiler to optimize binaries globally.

	  If unsure, select LTO_NONE. Note that LTO is very resource-intensive
	  so it's disabled by default.

config LTO_NONE
	bool "None"
	help
	  Build the kernel normally, without Link Time Optimization (LTO).

config LTO_CLANG_FULL
	bool "Clang Full LTO (EXPERIMENTAL)"
	depends on HAS_LTO_CLANG
	depends on !COMPILE_TEST
	select LTO_CLANG
	help
          This option enables Clang's full Link Time Optimization (LTO), which
          allows the compiler to optimize the kernel globally. If you enable
          this option, the compiler generates LLVM bitcode instead of ELF
          object files, and the actual compilation from bitcode happens at
          the LTO link step, which may take several minutes depending on the
          kernel configuration. More information can be found from LLVM's
          documentation:

	    https://llvm.org/docs/LinkTimeOptimization.html

	  During link time, this option can use a large amount of RAM, and
	  may take much longer than the ThinLTO option.

config LTO_CLANG_THIN
	bool "Clang ThinLTO (EXPERIMENTAL)"
	depends on HAS_LTO_CLANG && ARCH_SUPPORTS_LTO_CLANG_THIN
	select LTO_CLANG
	help
	  This option enables Clang's ThinLTO, which allows for parallel
	  optimization and faster incremental compiles compared to the
	  CONFIG_LTO_CLANG_FULL option. More information can be found
	  from Clang's documentation:

	    https://clang.llvm.org/docs/ThinLTO.html

	  If unsure, say Y.
endchoice

config HAVE_ARCH_WITHIN_STACK_FRAMES
	bool
	help
+4 −0
Original line number Diff line number Diff line
@@ -73,6 +73,8 @@ config ARM64
	select ARCH_SUPPORTS_DEBUG_PAGEALLOC
	select ARCH_SUPPORTS_MEMORY_FAILURE
	select ARCH_SUPPORTS_SHADOW_CALL_STACK if CC_HAVE_SHADOW_CALL_STACK
	select ARCH_SUPPORTS_LTO_CLANG if CPU_LITTLE_ENDIAN
	select ARCH_SUPPORTS_LTO_CLANG_THIN
	select ARCH_SUPPORTS_ATOMIC_RMW
	select ARCH_SUPPORTS_INT128 if CC_HAS_INT128 && (GCC_VERSION >= 50000 || CC_IS_CLANG)
	select ARCH_SUPPORTS_NUMA_BALANCING
@@ -162,6 +164,8 @@ config ARM64
	select HAVE_DYNAMIC_FTRACE
	select HAVE_DYNAMIC_FTRACE_WITH_REGS \
		if $(cc-option,-fpatchable-function-entry=2)
	select FTRACE_MCOUNT_USE_PATCHABLE_FUNCTION_ENTRY \
		if DYNAMIC_FTRACE_WITH_REGS
	select HAVE_EFFICIENT_UNALIGNED_ACCESS
	select HAVE_FAST_GUP
	select HAVE_FTRACE_MCOUNT_RECORD
+2 −1
Original line number Diff line number Diff line
@@ -29,7 +29,8 @@ ldflags-y := -shared -nostdlib -soname=linux-vdso.so.1 --hash-style=sysv \
ccflags-y := -fno-common -fno-builtin -fno-stack-protector -ffixed-x18
ccflags-y += -DDISABLE_BRANCH_PROFILING -DBUILD_VDSO

CFLAGS_REMOVE_vgettimeofday.o = $(CC_FLAGS_FTRACE) -Os $(CC_FLAGS_SCS) $(GCC_PLUGINS_CFLAGS)
CFLAGS_REMOVE_vgettimeofday.o = $(CC_FLAGS_FTRACE) -Os $(CC_FLAGS_SCS) $(GCC_PLUGINS_CFLAGS) \
				$(CC_FLAGS_LTO)
KASAN_SANITIZE			:= n
UBSAN_SANITIZE			:= n
OBJECT_FILES_NON_STANDARD	:= y
Loading