kbuild: avoid re-running compiler and linker probes

Each kernel make invocation begins with ~30 compiler and linker runs each
of which performs duplicate probe for a number of compiler and linker
options.

This is useless work - the compiler and its version is known, so use these
to determine which options are available, once.

Using the newly introduced cc-option-str, track the result of each probe in
CC_OPT_*, LD_OPT_* variables and have Makefile's simply append the symbol.

Some care has to be taken around negative options, i.e. -Wno-xxx, as gcc
accepts an unknown -Wno- without complaint and only complains later, so
instead probe the positive form for these cases.

Use CC_HAS_* For those probes that gate a Makefile conditional rather than
a simple option, as per the established pattern for this.

This change avoids a subtle issue with ppc32 (big endian) - clang doesn't
accept -fno-stack-clash-protection on this sub-target, so keep this option
in the Makefile.

With gcc and clang, defconfig and allmodconfig, the recorded command
lines are unchanged, a build with nothing to do rebuilds nothing and W=1
continues to function correctly.

Doing this improves all builds, but has a particularly positive impact on
no-op builds (builds where nothing has changed).

Whole build, 128-thread Threadripper 9980X, best of N runs:

                                         before   after     delta
                                         -------------------------------
  x86 defconfig, no-op make, gcc           0.78s    0.60s    -0.18s (-23%)
  x86 defconfig, no-op make, clang         0.94s    0.66s    -0.28s (-30%)
  x86 defconfig, touch mm/vma.c, gcc        8.6s     8.5s    -0.16s (-2%)
  x86 defconfig, touch mm/vma.c, clang      8.0s     7.7s    -0.24s (-3%)
  x86 defconfig, clean, clang              29.8s    29.4s    -0.37s (-1%)
  x86 allmodconfig, no-op make, gcc        12.9s    12.7s    -0.23s (-2%)
  x86 allmodconfig, no-op make, clang      14.2s    13.9s    -0.29s (-2%)
  x86 allmodconfig, touch mm/vma.c, clang  32.2s    31.9s    -0.30s (-1%)

Assisted-by: LLM
Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
Link: https://patch.msgid.link/20260923-build-speedup-v4-12-73128809a4a4@kernel.org
Signed-off-by: Nathan Chancellor <nathan@kernel.org>
This commit is contained in:
Lorenzo Stoakes (ARM) 2026-09-23 18:18:01 +01:00 committed by Nathan Chancellor
parent f85147b291
commit 454e00cf8e
No known key found for this signature in database
GPG Key ID: 1D6B269171C01A96
8 changed files with 126 additions and 34 deletions

View File

@ -946,8 +946,8 @@ KBUILD_RUSTFLAGS += -Coverflow-checks=$(if $(CONFIG_RUST_OVERFLOW_CHECKS),y,n)
ifdef CONFIG_CC_IS_GCC
# gcc-10 renamed --param=allow-store-data-races=0 to
# -fno-allow-store-data-races.
KBUILD_CFLAGS += $(call cc-option,--param=allow-store-data-races=0)
KBUILD_CFLAGS += $(call cc-option,-fno-allow-store-data-races)
KBUILD_CFLAGS += $(CONFIG_CC_OPT_ALLOW_STORE_DATA_RACES_PARAM)
KBUILD_CFLAGS += $(CONFIG_CC_OPT_NO_ALLOW_STORE_DATA_RACES)
endif
ifdef CONFIG_READABLE_ASM
@ -1011,18 +1011,20 @@ endif
endif
# Explicitly clear padding bits during variable initialization
KBUILD_CFLAGS += $(call cc-option,-fzero-init-padding-bits=all)
KBUILD_CFLAGS += $(CONFIG_CC_OPT_ZERO_INIT_PADDING_BITS)
# While VLAs have been removed, GCC produces unreachable stack probes
# for the randomize_kstack_offset feature. Disable it for all compilers.
# Probed here rather than in Kconfig as clang only accepts it for some
# sub-targets, so the architecture's flags matter.
KBUILD_CFLAGS += $(call cc-option, -fno-stack-clash-protection)
# Get details on warnings generated due to GCC value tracking.
KBUILD_CFLAGS += $(call cc-option, -fdiagnostics-show-context=2)
KBUILD_CFLAGS += $(CONFIG_CC_OPT_DIAGNOSTICS_SHOW_CONTEXT)
# Show inlining notes for __attribute__((warning/error)) call chains.
# GCC supports this unconditionally while Clang 23+ provides a flag.
KBUILD_CFLAGS += $(call cc-option, -fdiagnostics-show-inlining-chain)
KBUILD_CFLAGS += $(CONFIG_CC_OPT_DIAGNOSTICS_SHOW_INLINING_CHAIN)
# Clear used registers at func exit (to reduce data lifetime and ROP gadgets).
ifdef CONFIG_ZERO_CALL_USED_REGS
@ -1033,7 +1035,7 @@ ifdef CONFIG_FUNCTION_TRACER
ifdef CONFIG_FTRACE_MCOUNT_USE_CC
CC_FLAGS_FTRACE += -mrecord-mcount
ifdef CONFIG_HAVE_NOP_MCOUNT
ifeq ($(call cc-option-yn, -mnop-mcount),y)
ifdef CONFIG_CC_HAS_MNOP_MCOUNT
CC_FLAGS_FTRACE += -mnop-mcount
CC_FLAGS_USING += -DCC_USING_NOP_MCOUNT
endif
@ -1051,8 +1053,7 @@ ifdef CONFIG_FTRACE_MCOUNT_USE_RECORDMCOUNT
endif
endif
ifdef CONFIG_HAVE_FENTRY
# s390-linux-gnu-gcc did not support -mfentry until gcc-9.
ifeq ($(call cc-option-yn, -mfentry),y)
ifdef CONFIG_CC_HAS_MFENTRY
CC_FLAGS_FTRACE += -mfentry
CC_FLAGS_USING += -DCC_USING_FENTRY
endif
@ -1160,7 +1161,7 @@ NOSTDINC_FLAGS += -nostdinc
# the kernel uses only C99 flexible arrays for dynamically sized trailing
# arrays. Enforce this for everything that may examine structure sizes and
# perform bounds checking.
KBUILD_CFLAGS += $(call cc-option, -fstrict-flex-arrays=3)
KBUILD_CFLAGS += $(CONFIG_CC_OPT_STRICT_FLEX_ARRAYS)
# disable invalid "can't wrap" optimizations for signed / pointers
KBUILD_CFLAGS += -fno-strict-overflow
@ -1243,11 +1244,11 @@ LDFLAGS_vmlinux += --build-id=sha1
# COMDAT-deduplicated sections. Use --force-group-allocation to resolve these
# groups when linking modules. The option is available from ld.bfd 2.29 and
# ld.lld 19.1.0.
KBUILD_LDFLAGS_MODULE += $(call ld-option,--force-group-allocation)
KBUILD_LDFLAGS_MODULE += $(CONFIG_LD_OPT_FORCE_GROUP_ALLOCATION)
KBUILD_LDFLAGS += -z noexecstack
ifeq ($(CONFIG_LD_IS_BFD),y)
KBUILD_LDFLAGS += $(call ld-option,--no-warn-rwx-segments)
KBUILD_LDFLAGS += $(CONFIG_LD_OPT_NO_WARN_RWX_SEGMENTS)
endif
ifeq ($(CONFIG_STRIP_ASM_SYMS),y)

View File

@ -9,7 +9,7 @@ KBUILD_CFLAGS := $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) -fpie \
-include $(srctree)/include/linux/hidden.h \
-D__DISABLE_EXPORTS -ffreestanding -D__NO_FORTIFY \
-fno-asynchronous-unwind-tables -fno-unwind-tables \
$(call cc-option,-fno-addrsig)
$(CONFIG_CC_OPT_NO_ADDRSIG)
# this code may run with the MMU off so disable unaligned accesses
CFLAGS_map_range.o += -mstrict-align

View File

@ -8,7 +8,7 @@ KBUILD_CFLAGS := $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) -fpie \
-include $(srctree)/include/linux/hidden.h \
-D__DISABLE_EXPORTS -ffreestanding \
-fno-asynchronous-unwind-tables -fno-unwind-tables \
$(call cc-option,-fno-addrsig)
$(CONFIG_CC_OPT_NO_ADDRSIG)
# Disable LTO
KBUILD_CFLAGS := $(filter-out $(CC_FLAGS_LTO), $(KBUILD_CFLAGS))

View File

@ -2364,6 +2364,23 @@ config CC_HAS_KCFI_ARITY
def_bool $(cc-option,-fsanitize=kcfi -fsanitize-kcfi-arity)
depends on CC_IS_CLANG && !RUST
config CC_OPT_INDIRECT_BRANCH_CS_PREFIX
def_string "$(cc-option-str,-mindirect-branch-cs-prefix)"
config CC_OPT_CF_PROTECTION_NONE
def_string "$(cc-option-str,-fcf-protection=none)"
# with jump tables off: the compilers use NOTRACK for them, which kernel IBT
# does not allow
config CC_OPT_CF_PROTECTION_BRANCH
def_string "$(cc-option-str,-fcf-protection=branch -fno-jump-tables)"
config CC_OPT_FALIGN_JUMPS
def_string "$(cc-option-str,-falign-jumps=1)"
config CC_OPT_FALIGN_LOOPS
def_string "$(cc-option-str,-falign-loops=1)"
config FUNCTION_PADDING_CFI
int
default 59 if FUNCTION_ALIGNMENT_64B

View File

@ -20,7 +20,7 @@ ifdef CONFIG_CC_IS_CLANG
RETPOLINE_CFLAGS := -mretpoline-external-thunk
RETPOLINE_VDSO_CFLAGS := -mretpoline
endif
RETPOLINE_CFLAGS += $(call cc-option,-mindirect-branch-cs-prefix)
RETPOLINE_CFLAGS += $(CONFIG_CC_OPT_INDIRECT_BRANCH_CS_PREFIX)
ifdef CONFIG_MITIGATION_RETHUNK
RETHUNK_CFLAGS := -mfunction-return=thunk-extern
@ -52,7 +52,7 @@ REALMODE_CFLAGS := $(CC_FLAGS_DIALECT) -m16 -g -Os \
-DDISABLE_BRANCH_PROFILING -D__DISABLE_EXPORTS \
-Wall -Wstrict-prototypes -march=i386 -mregparm=3 \
-fno-strict-aliasing -fomit-frame-pointer -fno-pic \
-mno-mmx -mno-sse $(call cc-option,-fcf-protection=none)
-mno-mmx -mno-sse $(CONFIG_CC_OPT_CF_PROTECTION_NONE)
REALMODE_CFLAGS += -ffreestanding
REALMODE_CFLAGS += -fno-stack-protector
@ -99,10 +99,10 @@ ifeq ($(CONFIG_X86_KERNEL_IBT),y)
#
# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104816
#
KBUILD_CFLAGS += $(call cc-option,-fcf-protection=branch -fno-jump-tables)
KBUILD_CFLAGS += $(CONFIG_CC_OPT_CF_PROTECTION_BRANCH)
KBUILD_RUSTFLAGS += -Zcf-protection=branch $(if $(call rustc-min-version,109300),-Cjump-tables=n,-Zno-jump-tables)
else
KBUILD_CFLAGS += $(call cc-option,-fcf-protection=none)
KBUILD_CFLAGS += $(CONFIG_CC_OPT_CF_PROTECTION_NONE)
endif
ifeq ($(CONFIG_X86_32),y)
@ -138,10 +138,10 @@ else
KBUILD_CFLAGS += -m64
# Align jump targets to 1 byte, not the default 16 bytes:
KBUILD_CFLAGS += $(call cc-option,-falign-jumps=1)
KBUILD_CFLAGS += $(CONFIG_CC_OPT_FALIGN_JUMPS)
# Pack loops tightly as well:
KBUILD_CFLAGS += $(call cc-option,-falign-loops=1)
KBUILD_CFLAGS += $(CONFIG_CC_OPT_FALIGN_LOOPS)
# Don't autogenerate traditional x87 instructions
KBUILD_CFLAGS += -mno-80387

View File

@ -40,7 +40,7 @@ KBUILD_CFLAGS := $(subst $(CC_FLAGS_FTRACE),,$(cflags-y)) \
-D__NO_FORTIFY \
-ffreestanding \
-fno-stack-protector \
$(call cc-option,-fno-addrsig) \
$(CONFIG_CC_OPT_NO_ADDRSIG) \
-D__DISABLE_EXPORTS
#

View File

@ -144,6 +144,80 @@ config CC_HAS_ASSUME
config CC_HAS_NO_PROFILE_FN_ATTR
def_bool $(success,echo '__attribute__((no_profile_instrument_function)) int x();' | $(CC) -x c - -c -o /dev/null -Werror)
config CC_OPT_ZERO_INIT_PADDING_BITS
def_string "$(cc-option-str,-fzero-init-padding-bits=all)"
config CC_OPT_NO_ADDRSIG
def_string "$(cc-option-str,-fno-addrsig)"
config CC_OPT_DIAGNOSTICS_SHOW_CONTEXT
def_string "$(cc-option-str,-fdiagnostics-show-context=2)"
config CC_OPT_DIAGNOSTICS_SHOW_INLINING_CHAIN
def_string "$(cc-option-str,-fdiagnostics-show-inlining-chain)"
config CC_OPT_STRICT_FLEX_ARRAYS
def_string "$(cc-option-str,-fstrict-flex-arrays=3)"
# gcc-10 renamed --param=allow-store-data-races=0 to -fno-allow-store-data-races
config CC_OPT_ALLOW_STORE_DATA_RACES_PARAM
def_string "$(cc-option-str,--param=allow-store-data-races=0)" if CC_IS_GCC
config CC_OPT_NO_ALLOW_STORE_DATA_RACES
def_string "$(cc-option-str,-fno-allow-store-data-races)" if CC_IS_GCC
config CC_HAS_MNOP_MCOUNT
def_bool $(cc-option,-mnop-mcount)
# s390-linux-gnu-gcc did not support -mfentry until gcc-9.
config CC_HAS_MFENTRY
def_bool $(cc-option,-mfentry)
config LD_OPT_FORCE_GROUP_ALLOCATION
def_string "$(ld-option-str,--force-group-allocation)"
config LD_OPT_NO_WARN_RWX_SEGMENTS
def_string "$(ld-option-str,--no-warn-rwx-segments)"
config CC_OPT_WNO_ADDRESS_OF_PACKED_MEMBER
def_string "-Wno-address-of-packed-member" if $(cc-option,-Waddress-of-packed-member)
config CC_OPT_WNO_FORMAT_OVERFLOW_NON_KPRINTF
def_string "-Wno-format-overflow-non-kprintf" if CC_IS_CLANG && $(cc-option,-Wformat-overflow-non-kprintf)
config CC_OPT_WNO_FORMAT_TRUNCATION_NON_KPRINTF
def_string "-Wno-format-truncation-non-kprintf" if CC_IS_CLANG && $(cc-option,-Wformat-truncation-non-kprintf)
config CC_OPT_WNO_DEFAULT_CONST_INIT_UNSAFE
def_string "-Wno-default-const-init-unsafe" if CC_IS_CLANG && $(cc-option,-Wdefault-const-init-unsafe)
config CC_OPT_WNO_DANGLING_POINTER
def_string "-Wno-dangling-pointer" if $(cc-option,-Wdangling-pointer)
config CC_OPT_WVLA_LARGER_THAN
def_string "$(cc-option-str,-Wvla-larger-than=1)"
config CC_HAS_WSTRINGOP_OVERFLOW
def_bool $(cc-option,-Wstringop-overflow)
config CC_OPT_WNO_UNTERMINATED_STRING_INITIALIZATION
def_string "-Wno-unterminated-string-initialization" if $(cc-option,-Wunterminated-string-initialization)
config CC_OPT_WERROR_DESIGNATED_INIT
def_string "$(cc-option-str,-Werror=designated-init)"
config CC_OPT_WENUM_CONVERSION
def_string "$(cc-option-str,-Wenum-conversion)"
config CC_OPT_WNO_PACKED_NOT_ALIGNED
def_string "-Wno-packed-not-aligned" if $(cc-option,-Wpacked-not-aligned)
config CC_OPT_WNO_FORMAT_OVERFLOW
def_string "-Wno-format-overflow" if $(cc-option,-Wformat-overflow)
config CC_OPT_WNO_STRINGOP_TRUNCATION
def_string "-Wno-stringop-truncation" if $(cc-option,-Wstringop-truncation)
config CC_HAS_COUNTED_BY
bool
# clang needs to be at least 20.1.0 to avoid potential crashes

View File

@ -17,7 +17,7 @@ KBUILD_CFLAGS += -Werror=strict-prototypes
KBUILD_CFLAGS += -Wno-format-security
KBUILD_CFLAGS += -Wno-trigraphs
KBUILD_CFLAGS += -Wno-frame-address
KBUILD_CFLAGS += $(call cc-option, -Wno-address-of-packed-member)
KBUILD_CFLAGS += $(CONFIG_CC_OPT_WNO_ADDRESS_OF_PACKED_MEMBER)
KBUILD_CFLAGS += -Wmissing-declarations
KBUILD_CFLAGS += -Wmissing-prototypes
@ -30,8 +30,8 @@ KBUILD_CFLAGS-$(CONFIG_CC_NO_ARRAY_BOUNDS) += -Wno-array-bounds
ifdef CONFIG_CC_IS_CLANG
# Clang checks for overflow/truncation with '%p', while GCC does not:
# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111219
KBUILD_CFLAGS += $(call cc-option, -Wno-format-overflow-non-kprintf)
KBUILD_CFLAGS += $(call cc-option, -Wno-format-truncation-non-kprintf)
KBUILD_CFLAGS += $(CONFIG_CC_OPT_WNO_FORMAT_OVERFLOW_NON_KPRINTF)
KBUILD_CFLAGS += $(CONFIG_CC_OPT_WNO_FORMAT_TRUNCATION_NON_KPRINTF)
# Clang may emit a warning when a const variable, such as the dummy variables
# in typecheck(), or const member of an aggregate type are not initialized,
@ -43,7 +43,7 @@ KBUILD_CFLAGS += $(call cc-option, -Wno-format-truncation-non-kprintf)
# disabled with this same switch, there should not be too much coverage lost
# because -Wuninitialized will still flag when an uninitialized const variable
# is used.
KBUILD_CFLAGS += $(call cc-option, -Wno-default-const-init-unsafe)
KBUILD_CFLAGS += $(CONFIG_CC_OPT_WNO_DEFAULT_CONST_INIT_UNSAFE)
else
# gcc inanely warns about local variables called 'main'
@ -54,7 +54,7 @@ endif
KBUILD_CFLAGS += -Wno-type-limits
# These result in bogus false positives
KBUILD_CFLAGS += $(call cc-option, -Wno-dangling-pointer)
KBUILD_CFLAGS += $(CONFIG_CC_OPT_WNO_DANGLING_POINTER)
# Stack Variable Length Arrays (VLAs) must not be used in the kernel.
# Function array parameters should, however, be usable, but -Wvla will
@ -62,7 +62,7 @@ KBUILD_CFLAGS += $(call cc-option, -Wno-dangling-pointer)
# types, so depend on GCC for now to keep stack VLAs out of the tree.
# https://github.com/llvm/llvm-project/issues/57098
# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98217
KBUILD_CFLAGS += $(call cc-option,-Wvla-larger-than=1)
KBUILD_CFLAGS += $(CONFIG_CC_OPT_WVLA_LARGER_THAN)
# disable pointer signed / unsigned warnings in gcc 4.0
KBUILD_CFLAGS += -Wno-pointer-sign
@ -73,11 +73,11 @@ KBUILD_CFLAGS += -Wno-pointer-sign
KBUILD_CFLAGS += -Wcast-function-type
# Currently, disable -Wstringop-overflow for GCC 11, globally.
KBUILD_CFLAGS-$(CONFIG_CC_NO_STRINGOP_OVERFLOW) += $(call cc-option, -Wno-stringop-overflow)
KBUILD_CFLAGS-$(CONFIG_CC_STRINGOP_OVERFLOW) += $(call cc-option, -Wstringop-overflow)
KBUILD_CFLAGS-$(CONFIG_CC_NO_STRINGOP_OVERFLOW) += $(if $(CONFIG_CC_HAS_WSTRINGOP_OVERFLOW),-Wno-stringop-overflow)
KBUILD_CFLAGS-$(CONFIG_CC_STRINGOP_OVERFLOW) += $(if $(CONFIG_CC_HAS_WSTRINGOP_OVERFLOW),-Wstringop-overflow)
# Currently, disable -Wunterminated-string-initialization as broken
KBUILD_CFLAGS += $(call cc-option, -Wno-unterminated-string-initialization)
KBUILD_CFLAGS += $(CONFIG_CC_OPT_WNO_UNTERMINATED_STRING_INITIALIZATION)
# The allocators already balk at large sizes, so silence the compiler
# warnings for bounds checks involving those possible values. While
@ -100,10 +100,10 @@ KBUILD_CFLAGS += -Werror=date-time
KBUILD_CFLAGS += -Werror=incompatible-pointer-types
# Require designated initializers for all marked structures
KBUILD_CFLAGS += $(call cc-option,-Werror=designated-init)
KBUILD_CFLAGS += $(CONFIG_CC_OPT_WERROR_DESIGNATED_INIT)
# Warn if there is an enum types mismatch
KBUILD_CFLAGS += $(call cc-option,-Wenum-conversion)
KBUILD_CFLAGS += $(CONFIG_CC_OPT_WENUM_CONVERSION)
KBUILD_CFLAGS += -Wunused
@ -125,12 +125,12 @@ else
# Suppress them by using -Wno... except for W=1.
KBUILD_CFLAGS += -Wno-unused-but-set-variable
KBUILD_CFLAGS += -Wno-unused-const-variable
KBUILD_CFLAGS += $(call cc-option, -Wno-packed-not-aligned)
KBUILD_CFLAGS += $(call cc-option, -Wno-format-overflow)
KBUILD_CFLAGS += $(CONFIG_CC_OPT_WNO_PACKED_NOT_ALIGNED)
KBUILD_CFLAGS += $(CONFIG_CC_OPT_WNO_FORMAT_OVERFLOW)
ifdef CONFIG_CC_IS_GCC
KBUILD_CFLAGS += -Wno-format-truncation
endif
KBUILD_CFLAGS += $(call cc-option, -Wno-stringop-truncation)
KBUILD_CFLAGS += $(CONFIG_CC_OPT_WNO_STRINGOP_TRUNCATION)
KBUILD_CFLAGS += -Wno-override-init # alias for -Wno-initializer-overrides in clang