Skip to contentSkip to conversation

kbuild: enable more warnings by default

PATCH In review Arnd Bergmann 3 participants 12 messages Apr 15, 2024 – May 28, 2024 Kbuild

lore
Share & tools
lore
#0 lore raw

v3 cover kbuild: enable more warnings by default

From: Arnd Bergmann <[email protected]>

Hi Andrew,

All the warning fixes I sent for these warnings have been merged into mainline or linux-next, so let's turn them on by default.

Since some of the bugfixes are still in flight in maintainer trees, I have rebased this on top of the mm/mm-nonmm-unstable branch for the purpose of merging these during the second half of the 6.10 merge window.

I did thorough testing of the changes on x86, arm64 and arm, as well as allmodconfig testing across all architectures and compilers. It is still likely that this will catch more warnings that show up in configurations I did not test, or in newly merged code, and I plan to send fixes for those as well.

I also sent patches to enable -Wunused-const-variable and -Wtautological-constant-out-of-range-compare, but those still need additional bugfixes to get merged first.

Arnd Bergmann (6):
[v3] kbuild: turn on -Wextra by default
[v3] kbuild: remove redundant extra warning flags [v3] kbuild: turn on -Wrestrict by default
[v3] kbuild: enable -Wformat-truncation on clang
[v3] kbuild: enable -Wcast-function-type-strict unconditionally [v3] kbuild: enable -Wstringop-truncation globally

 scripts/Makefile.extrawarn | 30 +++++++++++-------------------
 1 file changed, 11 insertions(+), 19 deletions(-)
Signature · 8 lines
2.39.2

Cc: Masahiro Yamada <[email protected]>
Cc: Nathan Chancellor <[email protected]>
Cc: Nicolas Schier <[email protected]>
Cc: Kees Cook <[email protected]>
Cc: [email protected]
Cc: [email protected]
#1 lore raw

PATCH v3 1/6 kbuild: turn on -Wextra by default

From: Arnd Bergmann <[email protected]>

The -Wextra option controls a number of different warnings that differ slightly by compiler version. Some are useful in general, others are better left at W=1 or higher. Based on earlier work, the ones that should be disabled by default are left for the higher warning levels already, and a lot of the useful ones have no remaining output when enabled.

Move the -Wextra option up into the set of default-enabled warnings and just rely on the individual ones getting disabled as needed.

The -Wunused warning was always grouped with this, so turn it on by default as well, except for the -Wunused-parameter warning that really has no value at all for the kernel since many interfaces have intentionally unused arguments.

---
 scripts/Makefile.extrawarn | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
scripts/Makefile.extrawarn +4 -1
index c5af566e911a..c247552c192c 100644
@@ -82,12 +82,14 @@ KBUILD_CFLAGS += $(call cc-option,-Werror=designated-init)
 # Warn if there is an enum types mismatch
 KBUILD_CFLAGS += $(call cc-option,-Wenum-conversion)
 
+KBUILD_CFLAGS += -Wextra
+KBUILD_CFLAGS += -Wunused
+
 #
 # W=1 - warnings which may be relevant and do not occur too often
 #
 ifneq ($(findstring 1, $(KBUILD_EXTRA_WARN)),)
 
-KBUILD_CFLAGS += -Wextra -Wunused -Wno-unused-parameter
 KBUILD_CFLAGS += $(call cc-option, -Wrestrict)
 KBUILD_CFLAGS += -Wmissing-format-attribute
 KBUILD_CFLAGS += -Wold-style-definition
@@ -190,6 +192,7 @@ else
 
 # The following turn off the warnings enabled by -Wextra
 KBUILD_CFLAGS += -Wno-sign-compare
+KBUILD_CFLAGS += -Wno-unused-parameter
 
 endif
 
Signature · 1 line
2.39.2
Reviewed-by on v3 1/6
#2 lore raw

Re: [PATCH 1/6] [v3] kbuild: turn on -Wextra by default

Quoted 12 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

The -Wextra option controls a number of different warnings that differ slightly by compiler version. Some are useful in general, others are better left at W=1 or higher. Based on earlier work, the ones that should be disabled by default are left for the higher warning levels already, and a lot of the useful ones have no remaining output when enabled.

Move the -Wextra option up into the set of default-enabled warnings and just rely on the individual ones getting disabled as needed.

Quoted 7 lines

The -Wunused warning was always grouped with this, so turn it on by default as well, except for the -Wunused-parameter warning that really has no value at all for the kernel since many interfaces have intentionally unused arguments.

Signature · 1 line
Kees Cook
#3 lore raw

PATCH v3 2/6 kbuild: remove redundant extra warning flags

In reply to Arnd Bergmann

From: Arnd Bergmann <[email protected]>

There is no point in turning individual options off and then on again, or vice versa, as the last one always wins. Now that -Wextra always gets passed first, remove all the redundant lines about warnings that are implied by either -Wall or -Wextra, and keep only the last one that disables it in some configurations.

This should not have any effect but keep the Makefile more readable and the command line shorter.

---
 scripts/Makefile.extrawarn | 15 ---------------
 1 file changed, 15 deletions(-)
scripts/Makefile.extrawarn +0 -15
index c247552c192c..8b3f5b62b837 100644
@@ -37,11 +37,6 @@ else
 KBUILD_CFLAGS += -Wno-main
 endif
 
-# These warnings generated too much noise in a regular build.
-# Use make W=1 to enable them (see scripts/Makefile.extrawarn)
-KBUILD_CFLAGS += $(call cc-disable-warning, unused-but-set-variable)
-KBUILD_CFLAGS += $(call cc-disable-warning, unused-const-variable)
-
 # These result in bogus false positives
 KBUILD_CFLAGS += $(call cc-disable-warning, dangling-pointer)
 
@@ -90,16 +85,9 @@ KBUILD_CFLAGS += -Wunused
 #
 ifneq ($(findstring 1, $(KBUILD_EXTRA_WARN)),)
 
-KBUILD_CFLAGS += $(call cc-option, -Wrestrict)
 KBUILD_CFLAGS += -Wmissing-format-attribute
-KBUILD_CFLAGS += -Wold-style-definition
 KBUILD_CFLAGS += -Wmissing-include-dirs
-KBUILD_CFLAGS += $(call cc-option, -Wunused-but-set-variable)
 KBUILD_CFLAGS += $(call cc-option, -Wunused-const-variable)
-KBUILD_CFLAGS += $(call cc-option, -Wpacked-not-aligned)
-KBUILD_CFLAGS += $(call cc-option, -Wformat-overflow)
-KBUILD_CFLAGS += $(call cc-option, -Wformat-truncation)
-KBUILD_CFLAGS += $(call cc-option, -Wstringop-truncation)
 
 KBUILD_CPPFLAGS += -Wundef
 KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN1
@@ -150,9 +138,6 @@ ifneq ($(findstring 2, $(KBUILD_EXTRA_WARN)),)
 KBUILD_CFLAGS += -Wdisabled-optimization
 KBUILD_CFLAGS += -Wshadow
 KBUILD_CFLAGS += $(call cc-option, -Wlogical-op)
-KBUILD_CFLAGS += -Wmissing-field-initializers
-KBUILD_CFLAGS += -Wtype-limits
-KBUILD_CFLAGS += $(call cc-option, -Wmaybe-uninitialized)
 KBUILD_CFLAGS += $(call cc-option, -Wunused-macros)
 
 KBUILD_CPPFLAGS += -DKBUILD_EXTRA_WARN2
Signature · 1 line
2.39.2
#4 lore raw

PATCH v3 3/6 kbuild: turn on -Wrestrict by default

In reply to Arnd Bergmann

From: Arnd Bergmann <[email protected]>

All known -Wrestrict warnings are addressed now, so don't disable the warning any more.

---
 scripts/Makefile.extrawarn | 1 -
 1 file changed, 1 deletion(-)
scripts/Makefile.extrawarn +0 -1
index 8b3f5b62b837..95466a04d51b 100644
@@ -98,7 +98,6 @@ else
 # Suppress them by using -Wno... except for W=1.
 KBUILD_CFLAGS += $(call cc-disable-warning, unused-but-set-variable)
 KBUILD_CFLAGS += $(call cc-disable-warning, unused-const-variable)
-KBUILD_CFLAGS += $(call cc-disable-warning, restrict)
 KBUILD_CFLAGS += $(call cc-disable-warning, packed-not-aligned)
 KBUILD_CFLAGS += $(call cc-disable-warning, format-overflow)
 KBUILD_CFLAGS += $(call cc-disable-warning, format-truncation)
Signature · 1 line
2.39.2
#5 lore raw

Re: [PATCH 3/6] [v3] kbuild: turn on -Wrestrict by default

Quoted 4 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

All known -Wrestrict warnings are addressed now, so don't disable the warning any more.

Ah, great! Yes, I know Gatlin was also looking at this and doing a bunch of builds across compiler versions to verify this was ready to do too. Gatlin, are you able to provide a "Tested-by:" tag for this patch?

-Kees

Quoted 21 lines
---
 scripts/Makefile.extrawarn | 1 -
 1 file changed, 1 deletion(-)
scripts/Makefile.extrawarn +0 -1
index 8b3f5b62b837..95466a04d51b 100644
@@ -98,7 +98,6 @@ else
 # Suppress them by using -Wno... except for W=1.
 KBUILD_CFLAGS += $(call cc-disable-warning, unused-but-set-variable)
 KBUILD_CFLAGS += $(call cc-disable-warning, unused-const-variable)
-KBUILD_CFLAGS += $(call cc-disable-warning, restrict)
 KBUILD_CFLAGS += $(call cc-disable-warning, packed-not-aligned)
 KBUILD_CFLAGS += $(call cc-disable-warning, format-overflow)
 KBUILD_CFLAGS += $(call cc-disable-warning, format-truncation)
Signature · 1 line
2.39.2
Signature · 1 line
Kees Cook
#6 lore raw

Re: [PATCH 3/6] [v3] kbuild: turn on -Wrestrict by default

In reply to Arnd Bergmann

Hi Arnd,

Quoted 6 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

All known -Wrestrict warnings are addressed now, so don't disable the warning any more.

Thanks for your patch, which is now commit 06bb7fc0feee32d9 ("kbuild:
turn on -Wrestrict by default") in v6.10-rc1.

With shmobile_defconfig and gcc version 11.4.0 (Ubuntu 11.4.0-1ubuntu1~22.04):

kernel/kallsyms.c: In function ‘__sprint_symbol.constprop’:
kernel/kallsyms.c:492:17: warning: ‘strcpy’ source argument is the
same as destination [-Werror=restrict]

      492 |                 strcpy(buffer, name);
          |                 ^~~~~~~~~~~~~~~~~~~~

Reverting the commit fixes the issue.

I assume you just forgot that this depends on "[PATCH] [v5] kallsyms:
rework symbol lookup return codes"?
https://lore.kernel.org/r/[email protected]

scripts/Makefile.extrawarn else · line 103

Quoted 10 lines
scripts/Makefile.extrawarn +0 -1
@@ -98,7 +98,6 @@ else
 # Suppress them by using -Wno... except for W=1.
 KBUILD_CFLAGS += $(call cc-disable-warning, unused-but-set-variable)
 KBUILD_CFLAGS += $(call cc-disable-warning, unused-const-variable)
-KBUILD_CFLAGS += $(call cc-disable-warning, restrict)
 KBUILD_CFLAGS += $(call cc-disable-warning, packed-not-aligned)
 KBUILD_CFLAGS += $(call cc-disable-warning, format-overflow)
 KBUILD_CFLAGS += $(call cc-disable-warning, format-truncation)

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
#7 lore raw

PATCH v3 4/6 kbuild: enable -Wformat-truncation on clang

In reply to Arnd Bergmann

From: Arnd Bergmann <[email protected]>

This warning option still produces output on gcc but is now clean when building with clang, so enable it conditionally on the compiler for now.

As far as I can tell, the remaining warnings with gcc are the result of analysing the code more deeply across inlining, while clang only does this within a function.

---
 scripts/Makefile.extrawarn | 7 +++++++
 1 file changed, 7 insertions(+)
scripts/Makefile.extrawarn +7 -0
index 95466a04d51b..202e26e6f29f 100644
@@ -100,7 +100,14 @@ KBUILD_CFLAGS += $(call cc-disable-warning, unused-but-set-variable)
 KBUILD_CFLAGS += $(call cc-disable-warning, unused-const-variable)
 KBUILD_CFLAGS += $(call cc-disable-warning, packed-not-aligned)
 KBUILD_CFLAGS += $(call cc-disable-warning, format-overflow)
+ifdef CONFIG_CC_IS_GCC
 KBUILD_CFLAGS += $(call cc-disable-warning, format-truncation)
+else
+# 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-disable-warning, format-overflow-non-kprintf)
+KBUILD_CFLAGS += $(call cc-disable-warning, format-truncation-non-kprintf)
+endif
 KBUILD_CFLAGS += $(call cc-disable-warning, stringop-truncation)
 
 KBUILD_CFLAGS += -Wno-override-init # alias for -Wno-initializer-overrides in clang
Signature · 1 line
2.39.2
#8 lore raw

PATCH v3 5/6 kbuild: enable -Wcast-function-type-strict unconditionally

In reply to Arnd Bergmann

From: Arnd Bergmann <[email protected]>

All known function cast warnings are now addressed, so the warning can be enabled globally to catch new ones more quickly.

---
 scripts/Makefile.extrawarn | 1 -
 1 file changed, 1 deletion(-)
scripts/Makefile.extrawarn +0 -1
index 202e26e6f29f..1d13cecc7cc7 100644
@@ -129,7 +129,6 @@ endif
 KBUILD_CFLAGS += $(call cc-disable-warning, pointer-to-enum-cast)
 KBUILD_CFLAGS += -Wno-tautological-constant-out-of-range-compare
 KBUILD_CFLAGS += $(call cc-disable-warning, unaligned-access)
-KBUILD_CFLAGS += $(call cc-disable-warning, cast-function-type-strict)
 KBUILD_CFLAGS += -Wno-enum-compare-conditional
 KBUILD_CFLAGS += -Wno-enum-enum-conversion
 endif
Signature · 1 line
2.39.2
Reviewed-by on v3 5/6
#9 lore raw

Re: [PATCH 5/6] [v3] kbuild: enable -Wcast-function-type-strict unconditionally

Quoted 4 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

All known function cast warnings are now addressed, so the warning can be enabled globally to catch new ones more quickly.

Also yay! :)

Quoted 21 lines
---
 scripts/Makefile.extrawarn | 1 -
 1 file changed, 1 deletion(-)
scripts/Makefile.extrawarn +0 -1
index 202e26e6f29f..1d13cecc7cc7 100644
@@ -129,7 +129,6 @@ endif
 KBUILD_CFLAGS += $(call cc-disable-warning, pointer-to-enum-cast)
 KBUILD_CFLAGS += -Wno-tautological-constant-out-of-range-compare
 KBUILD_CFLAGS += $(call cc-disable-warning, unaligned-access)
-KBUILD_CFLAGS += $(call cc-disable-warning, cast-function-type-strict)
 KBUILD_CFLAGS += -Wno-enum-compare-conditional
 KBUILD_CFLAGS += -Wno-enum-enum-conversion
 endif
Signature · 1 line
2.39.2
Signature · 1 line
Kees Cook
#10 lore raw

PATCH v3 6/6 kbuild: enable -Wstringop-truncation globally

In reply to Arnd Bergmann

From: Arnd Bergmann <[email protected]>

The remaining warnings of this type have been addressed, so it can now be enabled by default, rather than only for W=1.

---
v3: no changes
v2: no changes

---
 scripts/Makefile.extrawarn | 1 -
 1 file changed, 1 deletion(-)
scripts/Makefile.extrawarn +0 -1
index 1d13cecc7cc7..17c511ddf48a 100644
@@ -108,7 +108,6 @@ else
 KBUILD_CFLAGS += $(call cc-disable-warning, format-overflow-non-kprintf)
 KBUILD_CFLAGS += $(call cc-disable-warning, format-truncation-non-kprintf)
 endif
-KBUILD_CFLAGS += $(call cc-disable-warning, stringop-truncation)
 
 KBUILD_CFLAGS += -Wno-override-init # alias for -Wno-initializer-overrides in clang
 
Signature · 1 line
2.39.2
Reviewed-by on v3 6/6
#11 lore raw

Re: [PATCH 6/6] [v3] kbuild: enable -Wstringop-truncation globally

Quoted 4 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

The remaining warnings of this type have been addressed, so it can now be enabled by default, rather than only for W=1.

Yeah, I know Gustavo had been working on these too. Yay!

Quoted 24 lines

---
v3: no changes
v2: no changes

---
 scripts/Makefile.extrawarn | 1 -
 1 file changed, 1 deletion(-)
scripts/Makefile.extrawarn +0 -1
index 1d13cecc7cc7..17c511ddf48a 100644
@@ -108,7 +108,6 @@ else
 KBUILD_CFLAGS += $(call cc-disable-warning, format-overflow-non-kprintf)
 KBUILD_CFLAGS += $(call cc-disable-warning, format-truncation-non-kprintf)
 endif
-KBUILD_CFLAGS += $(call cc-disable-warning, stringop-truncation)
 
 KBUILD_CFLAGS += -Wno-override-init # alias for -Wno-initializer-overrides in clang
 
Signature · 1 line
2.39.2
Signature · 1 line
Kees Cook