Skip to contentSkip to conversation

kbuild: Use -fmin-function-alignment when available

PATCH Superseded Petr Pavlu 2 participants 3 messages Feb 12, 2024 – Feb 13, 2024 Kbuild

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Petr Pavlu

author
#0 lore raw

PATCH 1/1 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.

Detect availability of -fmin-function-alignment and use it instead of -falign-functions when present. Introduce CC_HAS_SANE_FUNCTION_ALIGNMENT and make the workarounds for the broken function alignment conditional on this setting.

---
 Makefile                       |  7 ++++++-
 arch/Kconfig                   |  8 ++++++++
 include/linux/compiler_types.h | 10 +++++-----
 kernel/exit.c                  |  5 ++++-
 4 files changed, 23 insertions(+), 7 deletions(-)
Makefile +6 -1
index 7e0b2ad98905..9516e43f6e45 100644
@@ -974,7 +974,12 @@ export CC_FLAGS_CFI
 endif
 
 ifneq ($(CONFIG_FUNCTION_ALIGNMENT),0)
-KBUILD_CFLAGS += -falign-functions=$(CONFIG_FUNCTION_ALIGNMENT)
+# Set the minimal function alignment. Try to use the newer GCC option
+# -fmin-function-alignment, or fall back to -falign-funtions. See also
+# CONFIG_CC_HAS_SANE_FUNCTION_ALIGNMENT.
+KBUILD_CFLAGS += $(call cc-option, \
+	-fmin-function-alignment=$(CONFIG_FUNCTION_ALIGNMENT), \
+	-falign-functions=$(CONFIG_FUNCTION_ALIGNMENT))
 endif
 
 # arch Makefile may override CC so keep this after arch Makefile is included
arch/Kconfig +8 -0
index a5af0edd3eb8..e2448f927fae 100644
@@ -1507,4 +1507,12 @@ config FUNCTION_ALIGNMENT
 	default 4 if FUNCTION_ALIGNMENT_4B
 	default 0
 
+config CC_HAS_SANE_FUNCTION_ALIGNMENT
+	# Detect availability of the GCC option -fmin-function-alignment which
+	# guarantees minimal alignment for all functions. GCC 13 and older have
+	# only -falign-functions which the compiler ignores for cold functions
+	# and this hence requires extra care in the kernel. Clang provides
+	# strict alignment always when using -falign-functions.
+	def_bool $(cc-option, -fmin-function-alignment=8) || CC_IS_CLANG
+
 endmenu
include/linux/compiler_types.h +5 -5
index 663d8791c871..f0152165e83c 100644
@@ -99,17 +99,17 @@ static inline void __chk_io_ptr(const volatile void __iomem *ptr) { }
  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Label-Attributes.html#index-cold-label-attribute
  *
  * When -falign-functions=N is in use, we must avoid the cold attribute as
- * contemporary versions of GCC drop the alignment for cold functions. Worse,
- * GCC can implicitly mark callees of cold functions as cold themselves, so
- * it's not sufficient to add __function_aligned here as that will not ensure
- * that callees are correctly aligned.
+ * GCC drops the alignment for cold functions. Worse, GCC can implicitly mark
+ * callees of cold functions as cold themselves, so it's not sufficient to add
+ * __function_aligned here as that will not ensure that callees are correctly
+ * aligned.
  *
  * See:
  *
  *   https://lore.kernel.org/lkml/Y77%2FqVgvaJidFpYt@FVFF77S0Q05N
  *   https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88345#c9
  */
-#if !defined(CONFIG_CC_IS_GCC) || (CONFIG_FUNCTION_ALIGNMENT == 0)
+#if defined(CONFIG_CC_HAS_SANE_FUNCTION_ALIGNMENT) || (CONFIG_FUNCTION_ALIGNMENT == 0)
 #define __cold				__attribute__((__cold__))
 #else
 #define __cold
kernel/exit.c +4 -1
index dfb963d2f862..5a6fed4ad3df 100644
@@ -1920,7 +1920,10 @@ EXPORT_SYMBOL(thread_group_exited);
  *
  * See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88345#c11
  */
-__weak __function_aligned void abort(void)
+#ifndef CONFIG_CC_HAS_SANE_FUNCTION_ALIGNMENT
+__function_aligned
+#endif
+__weak void abort(void)
 {
 	BUG();
 

base-commit: 841c35169323cd833294798e58b9bf63fa4fa1de

Signature · 1 line
2.35.3
#1 lore raw

Hi Petr,

Quoted 41 lines · Petr Pavlu

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.

Detect availability of -fmin-function-alignment and use it instead of -falign-functions when present. Introduce CC_HAS_SANE_FUNCTION_ALIGNMENT and make the workarounds for the broken function alignment conditional on this setting.

---
 Makefile                       |  7 ++++++-
 arch/Kconfig                   |  8 ++++++++
 include/linux/compiler_types.h | 10 +++++-----
 kernel/exit.c                  |  5 ++++-
 4 files changed, 23 insertions(+), 7 deletions(-)
Makefile +6 -1
index 7e0b2ad98905..9516e43f6e45 100644
@@ -974,7 +974,12 @@ export CC_FLAGS_CFI
 endif
 
 ifneq ($(CONFIG_FUNCTION_ALIGNMENT),0)
-KBUILD_CFLAGS += -falign-functions=$(CONFIG_FUNCTION_ALIGNMENT)
+# Set the minimal function alignment. Try to use the newer GCC option
+# -fmin-function-alignment, or fall back to -falign-funtions. See also
+# CONFIG_CC_HAS_SANE_FUNCTION_ALIGNMENT.
+KBUILD_CFLAGS += $(call cc-option, \
+	-fmin-function-alignment=$(CONFIG_FUNCTION_ALIGNMENT), \
+	-falign-functions=$(CONFIG_FUNCTION_ALIGNMENT))
 endif
 
 # arch Makefile may override CC so keep this after arch Makefile is included
arch/Kconfig +0 -0
index a5af0edd3eb8..e2448f927fae 100644
@@ -1507,4 +1507,12 @@ config FUNCTION_ALIGNMENT
 	default 4 if FUNCTION_ALIGNMENT_4B

arch/Kconfig config FUNCTION_ALIGNMENT · line 1517

Quoted 10 lines
arch/Kconfig +8 -0
 	default 0
 
+config CC_HAS_SANE_FUNCTION_ALIGNMENT
+	# Detect availability of the GCC option -fmin-function-alignment which
+	# guarantees minimal alignment for all functions. GCC 13 and older have
+	# only -falign-functions which the compiler ignores for cold functions
+	# and this hence requires extra care in the kernel. Clang provides
+	# strict alignment always when using -falign-functions.
+	def_bool $(cc-option, -fmin-function-alignment=8) || CC_IS_CLANG
+

I think this configuration should be split into something like CONFIG_CC_HAS_MIN_FUNCTION_ALIGNMENT that has the cc-option check then CONFIG_CC_HAS_SANE_FUNCTION_ALIGNMENT can depend on that configuration or Clang as you have it here, so that we can drop the cc-option check in the main Makefile and have it be:

ifdef CONFIG_CC_HAS_MIN_FUNCTION_ALIGNMENT
KBUILD_CFLAGS += -fmin-function-alignment=$(CONFIG_FUNCTION_ALIGNMENT) else
KBUILD_CFLAGS += -falign-functions=$(CONFIG_FUNCTION_ALIGNMENT) endif

It is wasteful to call cc-option twice in my opinion, especially if we are checking it in Kconfig.

Quoted 49 lines
arch/Kconfig +0 -0
 endmenu
include/linux/compiler_types.h +5 -5
index 663d8791c871..f0152165e83c 100644
@@ -99,17 +99,17 @@ static inline void __chk_io_ptr(const volatile void __iomem *ptr) { }
  *   gcc: https://gcc.gnu.org/onlinedocs/gcc/Label-Attributes.html#index-cold-label-attribute
  *
  * When -falign-functions=N is in use, we must avoid the cold attribute as
- * contemporary versions of GCC drop the alignment for cold functions. Worse,
- * GCC can implicitly mark callees of cold functions as cold themselves, so
- * it's not sufficient to add __function_aligned here as that will not ensure
- * that callees are correctly aligned.
+ * GCC drops the alignment for cold functions. Worse, GCC can implicitly mark
+ * callees of cold functions as cold themselves, so it's not sufficient to add
+ * __function_aligned here as that will not ensure that callees are correctly
+ * aligned.
  *
  * See:
  *
  *   https://lore.kernel.org/lkml/Y77%2FqVgvaJidFpYt@FVFF77S0Q05N
  *   https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88345#c9
  */
-#if !defined(CONFIG_CC_IS_GCC) || (CONFIG_FUNCTION_ALIGNMENT == 0)
+#if defined(CONFIG_CC_HAS_SANE_FUNCTION_ALIGNMENT) || (CONFIG_FUNCTION_ALIGNMENT == 0)
 #define __cold				__attribute__((__cold__))
 #else
 #define __cold
kernel/exit.c +4 -1
index dfb963d2f862..5a6fed4ad3df 100644
@@ -1920,7 +1920,10 @@ EXPORT_SYMBOL(thread_group_exited);
  *
  * See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88345#c11
  */
-__weak __function_aligned void abort(void)
+#ifndef CONFIG_CC_HAS_SANE_FUNCTION_ALIGNMENT
+__function_aligned
+#endif
+__weak void abort(void)
 {
 	BUG();
 

base-commit: 841c35169323cd833294798e58b9bf63fa4fa1de

Signature · 1 line
2.35.3
author
#2 lore raw
Quoted 61 lines · Nathan Chancellor

Hi Petr,

Quoted 41 lines · Petr Pavlu

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.

Detect availability of -fmin-function-alignment and use it instead of -falign-functions when present. Introduce CC_HAS_SANE_FUNCTION_ALIGNMENT and make the workarounds for the broken function alignment conditional on this setting.

---
 Makefile                       |  7 ++++++-
 arch/Kconfig                   |  8 ++++++++
 include/linux/compiler_types.h | 10 +++++-----
 kernel/exit.c                  |  5 ++++-
 4 files changed, 23 insertions(+), 7 deletions(-)
Makefile +6 -1
index 7e0b2ad98905..9516e43f6e45 100644
@@ -974,7 +974,12 @@ export CC_FLAGS_CFI
 endif
 
 ifneq ($(CONFIG_FUNCTION_ALIGNMENT),0)
-KBUILD_CFLAGS += -falign-functions=$(CONFIG_FUNCTION_ALIGNMENT)
+# Set the minimal function alignment. Try to use the newer GCC option
+# -fmin-function-alignment, or fall back to -falign-funtions. See also
+# CONFIG_CC_HAS_SANE_FUNCTION_ALIGNMENT.
+KBUILD_CFLAGS += $(call cc-option, \
+	-fmin-function-alignment=$(CONFIG_FUNCTION_ALIGNMENT), \
+	-falign-functions=$(CONFIG_FUNCTION_ALIGNMENT))
 endif
 
 # arch Makefile may override CC so keep this after arch Makefile is included
arch/Kconfig +0 -0
index a5af0edd3eb8..e2448f927fae 100644
@@ -1507,4 +1507,12 @@ config FUNCTION_ALIGNMENT
 	default 4 if FUNCTION_ALIGNMENT_4B

arch/Kconfig config FUNCTION_ALIGNMENT · line 1517

Quoted 10 lines
arch/Kconfig +8 -0
 	default 0
 
+config CC_HAS_SANE_FUNCTION_ALIGNMENT
+	# Detect availability of the GCC option -fmin-function-alignment which
+	# guarantees minimal alignment for all functions. GCC 13 and older have
+	# only -falign-functions which the compiler ignores for cold functions
+	# and this hence requires extra care in the kernel. Clang provides
+	# strict alignment always when using -falign-functions.
+	def_bool $(cc-option, -fmin-function-alignment=8) || CC_IS_CLANG
+

I think this configuration should be split into something like CONFIG_CC_HAS_MIN_FUNCTION_ALIGNMENT that has the cc-option check then CONFIG_CC_HAS_SANE_FUNCTION_ALIGNMENT can depend on that configuration or Clang as you have it here, so that we can drop the cc-option check in the main Makefile and have it be:

Quoted 8 lines

ifdef CONFIG_CC_HAS_MIN_FUNCTION_ALIGNMENT
KBUILD_CFLAGS += -fmin-function-alignment=$(CONFIG_FUNCTION_ALIGNMENT) else
KBUILD_CFLAGS += -falign-functions=$(CONFIG_FUNCTION_ALIGNMENT) endif

It is wasteful to call cc-option twice in my opinion, especially if we are checking it in Kconfig.

Makes sense, thanks.

-- Petr