Skip to content

Commit 5270316

Browse files
petrpavlumasahir0y
authored andcommitted
kbuild: Use -fmin-function-alignment when available
GCC recently added option -fmin-function-alignment, which should appear in GCC 14. Unlike -falign-functions, this option causes all functions to be aligned at the specified value, including the cold ones. In particular, when an arm64 kernel is built with DYNAMIC_FTRACE_WITH_CALL_OPS=y, the 8-byte function alignment is required for correct functionality. This was done by -falign-functions=8 and having workarounds in the kernel to force the compiler to follow this alignment. The new -fmin-function-alignment option directly guarantees it. Detect availability of -fmin-function-alignment and use it instead of -falign-functions when present. Introduce CC_HAS_SANE_FUNCTION_ALIGNMENT and enable __cold to work as expected when it is set. Signed-off-by: Petr Pavlu <[email protected]> Reviewed-by: Nathan Chancellor <[email protected]> Acked-by: Mark Rutland <[email protected]> Signed-off-by: Masahiro Yamada <[email protected]>
1 parent c31f96a commit 5270316

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

Diff for: Makefile

+7
Original file line numberDiff line numberDiff line change
@@ -974,8 +974,15 @@ export CC_FLAGS_CFI
974974
endif
975975

976976
ifneq ($(CONFIG_FUNCTION_ALIGNMENT),0)
977+
# Set the minimal function alignment. Use the newer GCC option
978+
# -fmin-function-alignment if it is available, or fall back to -falign-funtions.
979+
# See also CONFIG_CC_HAS_SANE_FUNCTION_ALIGNMENT.
980+
ifdef CONFIG_CC_HAS_MIN_FUNCTION_ALIGNMENT
981+
KBUILD_CFLAGS += -fmin-function-alignment=$(CONFIG_FUNCTION_ALIGNMENT)
982+
else
977983
KBUILD_CFLAGS += -falign-functions=$(CONFIG_FUNCTION_ALIGNMENT)
978984
endif
985+
endif
979986

980987
# arch Makefile may override CC so keep this after arch Makefile is included
981988
NOSTDINC_FLAGS += -nostdinc

Diff for: arch/Kconfig

+12
Original file line numberDiff line numberDiff line change
@@ -1507,4 +1507,16 @@ config FUNCTION_ALIGNMENT
15071507
default 4 if FUNCTION_ALIGNMENT_4B
15081508
default 0
15091509

1510+
config CC_HAS_MIN_FUNCTION_ALIGNMENT
1511+
# Detect availability of the GCC option -fmin-function-alignment which
1512+
# guarantees minimal alignment for all functions, unlike
1513+
# -falign-functions which the compiler ignores for cold functions.
1514+
def_bool $(cc-option, -fmin-function-alignment=8)
1515+
1516+
config CC_HAS_SANE_FUNCTION_ALIGNMENT
1517+
# Set if the guaranteed alignment with -fmin-function-alignment is
1518+
# available or extra care is required in the kernel. Clang provides
1519+
# strict alignment always, even with -falign-functions.
1520+
def_bool CC_HAS_MIN_FUNCTION_ALIGNMENT || CC_IS_CLANG
1521+
15101522
endmenu

Diff for: include/linux/compiler_types.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,17 @@ static inline void __chk_io_ptr(const volatile void __iomem *ptr) { }
9999
* gcc: https://gcc.gnu.org/onlinedocs/gcc/Label-Attributes.html#index-cold-label-attribute
100100
*
101101
* When -falign-functions=N is in use, we must avoid the cold attribute as
102-
* contemporary versions of GCC drop the alignment for cold functions. Worse,
103-
* GCC can implicitly mark callees of cold functions as cold themselves, so
104-
* it's not sufficient to add __function_aligned here as that will not ensure
105-
* that callees are correctly aligned.
102+
* GCC drops the alignment for cold functions. Worse, GCC can implicitly mark
103+
* callees of cold functions as cold themselves, so it's not sufficient to add
104+
* __function_aligned here as that will not ensure that callees are correctly
105+
* aligned.
106106
*
107107
* See:
108108
*
109109
* https://lore.kernel.org/lkml/Y77%2FqVgvaJidFpYt@FVFF77S0Q05N
110110
* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88345#c9
111111
*/
112-
#if !defined(CONFIG_CC_IS_GCC) || (CONFIG_FUNCTION_ALIGNMENT == 0)
112+
#if defined(CONFIG_CC_HAS_SANE_FUNCTION_ALIGNMENT) || (CONFIG_FUNCTION_ALIGNMENT == 0)
113113
#define __cold __attribute__((__cold__))
114114
#else
115115
#define __cold

0 commit comments

Comments
 (0)