Skip to contentSkip to conversation

kbuild: move gcc's -Wconstant-logical-operand to W=2

PATCH In review Vineet Gupta 3 participants 4 messages Nov 5 – Nov 7

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 kbuild: move gcc's -Wconstant-logical-operand to W=2

GCC 17 adds -Wconstant-logical-operand to -Wall. It warns when an operand of && or || folds to a constant other than 0 or 1. Unlike clang's version, it also checks constant left operands and operators that come from a macro expansion. With CONFIG_WERROR=y, an x86 defconfig build fails in prepare0:

arch/x86/include/asm/cpufeature.h:72:36: error: use of logical '&&' with constant operand '4194304' [-Werror=constant-logical-operand]

The warning is meant to catch && written for &. Kernel history has no such catch. Of the 13 earlier kernel fixes for clang's version, only commit dd7fe5d9fd6a ("rtc: max31335: Fix comparison in max31335_volatile_reg()") fixed a bug that the warning found. Commit 394b52462020 ("drm/i915/gem: fix bitwise and logical AND mixup") fixed a & written for &&, which the warning cannot see. The other fixes rewrote correct code. In GCC's own tree, the warning found two sites since it was added, and both were false positives.

Disable the warning for GCC unless W=2 is given. Keep clang's version, which skips macros and constant left operands.

---
v1 -> v2:
- Replace the four per-site fixes with this one change (Borislav, Peter)
- Link to v1: https://lore.kernel.org/all/[email protected]/

 scripts/Kconfig.toolchain | 3 +++
 scripts/Makefile.warn     | 1 +
 2 files changed, 4 insertions(+)
scripts/Kconfig.toolchain +3 -0
index 2acbd40a38f1..1d68075f0469 100644
@@ -233,6 +233,9 @@ config CC_OPT_WNO_FORMAT_OVERFLOW
 config CC_OPT_WNO_STRINGOP_TRUNCATION
 	def_string "$(cc-option-str,-Wno-stringop-truncation)"
 
+config CC_OPT_WNO_CONSTANT_LOGICAL_OPERAND
+	def_string "$(cc-option-str,-Wno-constant-logical-operand)"
+
 config CC_HAS_COUNTED_BY
 	bool
 	# clang needs to be at least 20.1.0 to avoid potential crashes
scripts/Makefile.warn +1 -0
index 556fa249ca07..03b677bdbb0a 100644
@@ -167,6 +167,7 @@ endif
 
 ifdef CONFIG_CC_IS_GCC
 KBUILD_CFLAGS += -Wno-maybe-uninitialized
+KBUILD_CFLAGS += $(CONFIG_CC_OPT_WNO_CONSTANT_LOGICAL_OPERAND)
 endif
 
 endif
Signature · 1 line
2.53.0-Meta
Acked-by on v2 1/1
#1 lore raw
Quoted 18 lines · Vineet Gupta

GCC 17 adds -Wconstant-logical-operand to -Wall. It warns when an operand of && or || folds to a constant other than 0 or 1. Unlike clang's version, it also checks constant left operands and operators that come from a macro expansion. With CONFIG_WERROR=y, an x86 defconfig build fails in prepare0:

arch/x86/include/asm/cpufeature.h:72:36: error: use of logical '&&' with constant operand '4194304' [-Werror=constant-logical-operand]

The warning is meant to catch && written for &. Kernel history has no such catch. Of the 13 earlier kernel fixes for clang's version, only commit dd7fe5d9fd6a ("rtc: max31335: Fix comparison in max31335_volatile_reg()") fixed a bug that the warning found. Commit 394b52462020 ("drm/i915/gem: fix bitwise and logical AND mixup") fixed a & written for &&, which the warning cannot see. The other fixes rewrote correct code. In GCC's own tree, the warning found two sites since it was added, and both were false positives.

Quoted 2 lines

Disable the warning for GCC unless W=2 is given. Keep clang's version, which skips macros and constant left operands.

Quoted 42 lines

---
v1 -> v2:
- Replace the four per-site fixes with this one change (Borislav, Peter)
- Link to v1: https://lore.kernel.org/all/[email protected]/

 scripts/Kconfig.toolchain | 3 +++
 scripts/Makefile.warn     | 1 +
 2 files changed, 4 insertions(+)
scripts/Kconfig.toolchain +3 -0
index 2acbd40a38f1..1d68075f0469 100644
@@ -233,6 +233,9 @@ config CC_OPT_WNO_FORMAT_OVERFLOW
 config CC_OPT_WNO_STRINGOP_TRUNCATION
        def_string "$(cc-option-str,-Wno-stringop-truncation)"
 
+config CC_OPT_WNO_CONSTANT_LOGICAL_OPERAND
+       def_string "$(cc-option-str,-Wno-constant-logical-operand)"
+
 config CC_HAS_COUNTED_BY
        bool
        # clang needs to be at least 20.1.0 to avoid potential crashes
scripts/Makefile.warn +1 -1
index 556fa249ca07..03b677bdbb0a 100644
@@ -167,6 +167,7 @@ endif
 
 ifdef CONFIG_CC_IS_GCC
 KBUILD_CFLAGS += -Wno-maybe-uninitialized
+KBUILD_CFLAGS += $(CONFIG_CC_OPT_WNO_CONSTANT_LOGICAL_OPERAND)
 endif
 
 endif
--

2.53.0-Meta

Signature · 2 lines
Thanks,
~Nick Desaulniers
maintainer Reviewed-by on v2 1/1
#2 lore raw
In reply to Vineet Gupta
Quoted 17 lines · Vineet Gupta

GCC 17 adds -Wconstant-logical-operand to -Wall. It warns when an operand of && or || folds to a constant other than 0 or 1. Unlike clang's version, it also checks constant left operands and operators that come from a macro expansion. With CONFIG_WERROR=y, an x86 defconfig build fails in prepare0:

arch/x86/include/asm/cpufeature.h:72:36: error: use of logical '&&' with constant operand '4194304' [-Werror=constant-logical-operand]

The warning is meant to catch && written for &. Kernel history has no such catch. Of the 13 earlier kernel fixes for clang's version, only commit dd7fe5d9fd6a ("rtc: max31335: Fix comparison in max31335_volatile_reg()") fixed a bug that the warning found. Commit 394b52462020 ("drm/i915/gem: fix bitwise and logical AND mixup") fixed a & written for &&, which the warning cannot see. The other fixes rewrote correct code. In GCC's own tree, the warning found two sites since it was added, and both were false positives.

Quoted 6 lines

Disable the warning for GCC unless W=2 is given. Keep clang's version, which skips macros and constant left operands.

This probably wants a 'Cc: stable' so that older kernels can build warning free with GCC 17.

Signature · 2 lines
Cheers,
Nathan
#3 lore raw
In reply to Vineet Gupta
Quoted 1 line · Vineet Gupta

kbuild: move gcc's -Wconstant-logical-operand to W=2

I added a 'Cc: stable' to help older trees.

Applied to

https://git.kernel.org/pub/scm/linux/kernel/git/kbuild/linux.git kbuild-fixes-unstable

Thanks!

[1/1] kbuild: move gcc's -Wconstant-logical-operand to W=2

      https://git.kernel.org/kbuild/c/4d8996081dd19

Please look out for regression or issue reports or other follow up comments, as they may result in the patch/series getting dropped or reverted. Patches applied to an "unstable" branch are accepted pending wider testing in -next and any post-commit review; they will generally be moved to the main branch in a week if no issues are found.

Best regards,

Signature · 2 lines
Cheers,
Nathan