Skip to contentSkip to conversation

Fix the x86 defconfig build with GCC 17

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

Subsystems (7)
DRM drivers x86 Intel i915 Scheduler +3 more subsystems
lore
Share & tools
lore
#0 lore raw

cover Fix the x86 defconfig build with GCC 17

Fix build warnings and failures (CONFIG_WERROR=y) with a GCC 17 build. GCC 17 adds -Wconstant-logical-operand which is strict about operands of && or || that fold to a constant other than 0 or 1.

In the past similar fixes have been done for clang, e.g.

93f56de25937 2017-04-06 mac80211: Fix clang warning about constant operand in logical operation [v4.12-rc1]
...
dd7fe5d9fd6a 2024-01-17 rtc: max31335: Fix comparison in max31335_volatile_reg() [v6.8-rc1]

The reason this trips only with gcc is that clang's toggle is more permissive:
- it checks RHS only, not constant LHS.
- Macros are not checked.

Fix is to effectively make a macro or condition return 0 or 1.

The patches are split up per subsystem and can go independently via respective trees:

1/4 x86/cpufeatures: the awk-generated *_MASK_BIT_SET() returns 0 or 1. 2/4 wait: ___wait_is_interruptible() with a constant state. 3/4 iommupt: PT_SUPPORTED_FEATURE() in the fvalog2_*() helpers. 4/4 drm/i915: IS_POWER_OF_2() in reg_bits.h, and two Kconfig timeouts.

Tested on x86 defconfig with CONFIG_WERROR=y, at each patch:

- GCC 17 trunk (20261103): each patch removes only its own errors. The build has no warnings after 4/4.
- GCC 16.1: the build has no warnings. The .text of each object file does not change. The only differences come from the build timestamp and the release string.

Vineet Gupta (4):
x86/cpufeatures: Make {REQUIRED,DISABLED}_MASK_BIT_SET() return 0 or 1
wait: Make ___wait_is_interruptible() return 0 or 1
iommupt: Make PT_SUPPORTED_FEATURE() return 0 or 1 drm/i915: Avoid -Wconstant-logical-operand with GCC 17

 arch/x86/tools/cpufeaturemasks.awk          | 4 ++--
 drivers/gpu/drm/i915/gem/i915_gem_context.c | 2 +-
 drivers/gpu/drm/i915/i915_config.c          | 2 +-
 drivers/iommu/generic_pt/pt_defs.h          | 2 +-
 include/drm/intel/reg_bits.h                | 2 +-
 include/linux/wait.h                        | 2 +-
 6 files changed, 7 insertions(+), 7 deletions(-)
Signature · 1 line
2.53.0-Meta
#1 lore raw

PATCH 1/4 x86/cpufeatures: Make {REQUIRED,DISABLED}_MASK_BIT_SET() return 0 or 1

GCC 17 adds -Wconstant-logical-operand to -Wall. It warns when an operand of && or || folds to a constant other than 0 or 1. An x86 defconfig build with CONFIG_WERROR=y now fails in prepare0:

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

The *_MASK_BIT_SET() macros used to be || chains of CHECK_BIT_IN_MASK_WORD(), so the result was 0 or 1.

Commit da414d34b555 ("x86/cpufeatures: Use AWK to generate {REQUIRED|DISABLED}_MASK_BIT_SET in <asm/cpufeaturemasks.h>") changed them to a ternary chain masked with (1U << bit). The result is now the raw bit value. cpu_feature_enabled(), cpu_has() and this_cpu_has() use this value as an && operand. clang has the same warning, but it does not check an operator that comes from a macro expansion.

Wrap the generated expression in !!() to restore the 0 or 1 result. All users test the value as a truth value, so the generated code does not change.

Fixes da414d34b555 ("x86/cpufeatures: Use AWK to generate {REQUIRED|DISABLED}_MASK_BIT_SET in <asm/cpufeaturemasks.h>")
---
 arch/x86/tools/cpufeaturemasks.awk | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/tools/cpufeaturemasks.awk +2 -2
index 173d5bf2d999..a7856f874a6b 100755
@@ -75,13 +75,13 @@ END {
 			printf "#define %s_MASK%d\t0x%08xU\n", s, i, masks[i];
 
 		printf "\n#define %s_MASK_BIT_SET(x)\t\t\t\\\n", s;
-		printf "\t((\t\t\t\t\t";
+		printf "\t(!!((\t\t\t\t\t";
 		for (i = 0; i < ncapints; i++) {
 			if (masks[i])
 				printf "\t\\\n\t\t((x) >> 5) == %2d ? %s_MASK%d :", i, s, i;
 		}
 		printf " 0\t\\\n";
-		printf "\t) & (1U << ((x) & 31)))\n\n";
+		printf "\t) & (1U << ((x) & 31))))\n\n";
 	}
 
 	printf "#endif /* _ASM_X86_CPUFEATUREMASKS_H */\n";
Signature · 1 line
2.53.0-Meta
#2 lore raw

PATCH 2/4 wait: Make ___wait_is_interruptible() return 0 or 1

In reply to 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. With a constant state such as TASK_KILLABLE, ___wait_is_interruptible() breaks the build with CONFIG_WERROR=y:

include/linux/wait.h:285:39: error: use of logical '||' with constant operand '256' [-Werror=constant-logical-operand]

clang has the same warning, but it does not check an operator that comes from a macro expansion.

Compare the masked state against 0. All callers test the result as a truth value, so the behaviour does not change.

---
 include/linux/wait.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/wait.h +1 -1
index c2af98b0074d..ebabdfb5b30c 100644
@@ -283,7 +283,7 @@ static inline void wake_up_pollfree(struct wait_queue_head *wq_head)
 
 #define ___wait_is_interruptible(state)						\
 	(!__builtin_constant_p(state) ||					\
-	 (state & (TASK_INTERRUPTIBLE | TASK_WAKEKILL)))
+	 (state & (TASK_INTERRUPTIBLE | TASK_WAKEKILL)) != 0)
 
 extern void init_wait_entry(struct wait_queue_entry *wq_entry, int flags);
 
Signature · 1 line
2.53.0-Meta
maintainer
#3 lore raw

Re: [PATCH 2/4] wait: Make ___wait_is_interruptible() return 0 or 1

Quoted 23 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. With a constant state such as TASK_KILLABLE, ___wait_is_interruptible() breaks the build with CONFIG_WERROR=y:

include/linux/wait.h:285:39: error: use of logical '||' with constant operand '256' [-Werror=constant-logical-operand]

clang has the same warning, but it does not check an operator that comes from a macro expansion.

Compare the masked state against 0. All callers test the result as a truth value, so the behaviour does not change.

---
 include/linux/wait.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/wait.h +0 -0
index c2af98b0074d..ebabdfb5b30c 100644

include/linux/wait.h static inline void wake_up_pollfree(struct wait_queue_head *wq_head) · line 288

Quoted 7 lines
include/linux/wait.h +1 -1
@@ -283,7 +283,7 @@ static inline void wake_up_pollfree(struct wait_queue_head *wq_head)
 
 #define ___wait_is_interruptible(state)						\
 	(!__builtin_constant_p(state) ||					\
-	 (state & (TASK_INTERRUPTIBLE | TASK_WAKEKILL)))
+	 (state & (TASK_INTERRUPTIBLE | TASK_WAKEKILL)) != 0)
 
 extern void init_wait_entry(struct wait_queue_entry *wq_entry, int flags);

What is the actual purpose of this warning? This doesn't seem to serve any value. Any non-zero value is a true, why is this a problem?

GCC doing this, don't mean we can't turn if off again.

#4 lore raw

Re: [PATCH 2/4] wait: Make ___wait_is_interruptible() return 0 or 1

Quoted 25 lines · Peter Zijlstra
Quoted 24 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. With a constant state such as TASK_KILLABLE, ___wait_is_interruptible() breaks the build with CONFIG_WERROR=y:

include/linux/wait.h:285:39: error: use of logical '||' with constant operand '256' [-Werror=constant-logical-operand]

clang has the same warning, but it does not check an operator that comes from a macro expansion.

Compare the masked state against 0. All callers test the result as a truth value, so the behaviour does not change.

---
  include/linux/wait.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/wait.h +0 -0
index c2af98b0074d..ebabdfb5b30c 100644
@@ -283,7 +283,7 @@ static inline void wake_up_pollfree(struct wait_queue_head *wq_head)
 
Quoted 8 lines

include/linux/wait.h static inline void wake_up_pollfree(struct wait_queue_head *wq_head) · line 288

Quoted 6 lines
include/linux/wait.h +1 -1
  #define ___wait_is_interruptible(state)						\
  	(!__builtin_constant_p(state) ||					\
-	 (state & (TASK_INTERRUPTIBLE | TASK_WAKEKILL)))
+	 (state & (TASK_INTERRUPTIBLE | TASK_WAKEKILL)) != 0)
 
  extern void init_wait_entry(struct wait_queue_entry *wq_entry, int flags);

What is the actual purpose of this warning? This doesn't seem to serve any value. Any non-zero value is a true, why is this a problem?

I think the intent of the toggle was to catch && vs. & mixups in source (and it only catches that in 1 direction). Clearly that is not applicable here and it seems it has never caught this in the kernel anyway.

Quoted 1 line

GCC doing this, don't mean we can't turn if off again.

I will send a v2 which turns the GCC version off.

Thx,
-Vineet

#5 lore raw

PATCH 3/4 iommupt: Make PT_SUPPORTED_FEATURE() return 0 or 1

In reply to 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. PT_SUPPORTED_FEATURE() returns the raw feature bit, and the fvalog2_*() helpers use it as an && operand. The build fails with CONFIG_WERROR=y:

drivers/iommu/generic_pt/fmt/../pt_defs.h:255:51: error: use of logical '&&' with constant operand '2' [-Werror=constant-logical-operand]

clang has the same warning, but it checks only a constant right operand.

Compare the masked value against 0. All users test the result as a truth value, so the behaviour does not change.

---
 drivers/iommu/generic_pt/pt_defs.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/generic_pt/pt_defs.h +1 -1
index 707b3b0282fa..51a9c1c5fcc9 100644
@@ -200,7 +200,7 @@ static inline bool pt_table_install32(struct pt_state *pts, u32 table_entry)
 	return ret;
 }
 
-#define PT_SUPPORTED_FEATURE(feature_nr) (PT_SUPPORTED_FEATURES & BIT(feature_nr))
+#define PT_SUPPORTED_FEATURE(feature_nr) ((PT_SUPPORTED_FEATURES & BIT(feature_nr)) != 0)
 
 static __always_inline bool pt_feature(const struct pt_common *common,
 			      unsigned int feature_nr)
Signature · 1 line
2.53.0-Meta
#6 lore raw

PATCH 4/4 drm/i915: Avoid -Wconstant-logical-operand with GCC 17

In reply to 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. Three places break the build with CONFIG_WERROR=y:

include/drm/intel/reg_bits.h:27:48: error: use of logical '&&' with constant operand '256' [-Werror=constant-logical-operand] drivers/gpu/drm/i915/i915_config.c:13:43: error: use of logical '&&' with constant operand '10000' [-Werror=constant-logical-operand] drivers/gpu/drm/i915/gem/i915_gem_context.c:999:45: error: use of logical '&&' with constant operand '20000' [-Werror=constant-logical-operand]

clang has the same warning, but it checks only a constant right operand outside a macro. Here each constant is a left operand, and IS_POWER_OF_2() is also a macro.

Commit 1a839e016e49 ("drm/i915: remove IS_ACTIVE") moved CONFIG_DRM_I915_FENCE_TIMEOUT to the left of && to avoid the clang warning. That does not help with GCC 17.

Compare each constant against 0, as commit aeedd3a82678 ("drm/i915: Avoid -Wconstant-logical-operand in nsecs_to_jiffies_timeout()") did.

---
 drivers/gpu/drm/i915/gem/i915_gem_context.c | 2 +-
 drivers/gpu/drm/i915/i915_config.c          | 2 +-
 include/drm/intel/reg_bits.h                | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)
drivers/gpu/drm/i915/gem/i915_gem_context.c +1 -1
index c58ffa5a8fa6..1639639dd290 100644
@@ -996,7 +996,7 @@ static int intel_context_set_gem(struct intel_context *ce,
 	    intel_engine_has_semaphores(ce->engine))
 		__set_bit(CONTEXT_USE_SEMAPHORES, &ce->flags);
 
-	if (CONFIG_DRM_I915_REQUEST_TIMEOUT &&
+	if (CONFIG_DRM_I915_REQUEST_TIMEOUT != 0 &&
 	    ctx->i915->params.request_timeout_ms) {
 		unsigned int timeout_ms = ctx->i915->params.request_timeout_ms;
 
drivers/gpu/drm/i915/i915_config.c +1 -1
index 9e13b1be407c..9bbebe0a6a70 100644
@@ -10,7 +10,7 @@
 
 unsigned long i915_fence_context_timeout(u64 context)
 {
-	if (CONFIG_DRM_I915_FENCE_TIMEOUT && context)
+	if (CONFIG_DRM_I915_FENCE_TIMEOUT != 0 && context)
 		return msecs_to_jiffies_timeout(CONFIG_DRM_I915_FENCE_TIMEOUT);
 
 	return 0;
include/drm/intel/reg_bits.h +1 -1
index 2a9066e1d808..f913c3e61f39 100644
@@ -24,7 +24,7 @@
 /*
  * Local integer constant expression version of is_power_of_2().
  */
-#define IS_POWER_OF_2(__x)		((__x) && (((__x) & ((__x) - 1)) == 0))
+#define IS_POWER_OF_2(__x)		((__x) != 0 && (((__x) & ((__x) - 1)) == 0))
 
 /**
  * REG_FIELD_PREP8() - Prepare a u8 bitfield value
Signature · 1 line
2.53.0-Meta
maintainer
#7 lore raw
In reply to Vineet Gupta
Quoted 3 lines · Vineet Gupta

Fix build warnings and failures (CONFIG_WERROR=y) with a GCC 17 build. GCC 17 adds -Wconstant-logical-operand which is strict about operands of && or || that fold to a constant other than 0 or 1.

Just from looking at it, my initial reaction would be to disable that option.

Because, frankly, I don't see the point.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#8 lore raw

+CC Jakub

Quoted 7 lines · Borislav Petkov
Quoted 3 lines · Vineet Gupta

Fix build warnings and failures (CONFIG_WERROR=y) with a GCC 17 build. GCC 17 adds -Wconstant-logical-operand which is strict about operands of && or || that fold to a constant other than 0 or 1.

Just from looking at it, my initial reaction would be to disable that option.

Because, frankly, I don't see the point.

Here's my 2 cents:

I wondered if this brings any codegen improvements, since we are telling the compiler that something is 0 or 1 vs. some other range before. And it doesn't, because of the way it is used. So focusing on just 1/4.

- Constant bit: __builtin_constant_p(bit) && MASK_BIT_SET(bit) folds at compile time to the same truth value, with or without the !! bool conversion.
- Variable bit: __builtin_constant_p(bit) is 0, so && short-circuits and the mask is never evaluated.

Basically, the masked value never reaches generated code, so compiler gets nothing new to work with.

So I did some digging, and turns out that it can be helpful to catch bugs in the source code itself. In fact GCC implemented the toggle at all, when Jakub spotted an issue with a typo: (data->flags && ff_genericize) vs. (data->flags & ff_genericize).

In kernel, the predicate like *_BIT_SET() not returning a bool is not clean IMO and it used to return 0/1 before anyway so its not like we are going out of the way here.

I'm mildly in the favor of keeping the warning, but ultimately your call, I can respin with it disabled.

Thx,
-Vineet

maintainer
#9 lore raw
Quoted 20 lines · Vineet Gupta

+CC Jakub

Quoted 7 lines · Borislav Petkov
Quoted 3 lines · Vineet Gupta

Fix build warnings and failures (CONFIG_WERROR=y) with a GCC 17 build. GCC 17 adds -Wconstant-logical-operand which is strict about operands of && or || that fold to a constant other than 0 or 1.

Just from looking at it, my initial reaction would be to disable that option.

Because, frankly, I don't see the point.

Here's my 2 cents:

I wondered if this brings any codegen improvements, since we are telling the compiler that something is 0 or 1 vs. some other range before. And it doesn't, because of the way it is used. So focusing on just 1/4.

- Constant bit: __builtin_constant_p(bit) && MASK_BIT_SET(bit) folds at compile time to the same truth value, with or without the !! bool conversion.
- Variable bit: __builtin_constant_p(bit) is 0, so && short-circuits and the mask is never evaluated.

Basically, the masked value never reaches generated code, so compiler gets nothing new to work with.

Quoted 8 lines

So I did some digging, and turns out that it can be helpful to catch bugs in the source code itself. In fact GCC implemented the toggle at all, when Jakub spotted an issue with a typo: (data->flags && ff_genericize) vs. (data->flags & ff_genericize).

In kernel, the predicate like *_BIT_SET() not returning a bool is not clean IMO and it used to return 0/1 before anyway so its not like we are going out of the way here.

I'm mildly in the favor of keeping the warning, but ultimately your call, I can respin with it disabled.

Thx,
-Vineet

You might wanna look at the W= switches we have for less popular and more noisy options

Signature · 1 line
Small device. Typos and formatting crap
maintainer
#10 lore raw
In reply to Borislav Petkov
Quoted 8 lines · Borislav Petkov
Quoted 3 lines · Vineet Gupta

Fix build warnings and failures (CONFIG_WERROR=y) with a GCC 17 build. GCC 17 adds -Wconstant-logical-operand which is strict about operands of && or || that fold to a constant other than 0 or 1.

Just from looking at it, my initial reaction would be to disable that option.

Because, frankly, I don't see the point.

Yes, this. This seems like a really pointless warning to me too.