diff mbox series

bitfield: suppress "dubious: x & !y" sparse warning

Message ID 20240223100146.d243b6b1a9a1.I033828b1187c6bccf086e31400f7e933bb8373e7@changeid
State New
Headers show
Series bitfield: suppress "dubious: x & !y" sparse warning | expand

Commit Message

Johannes Berg Feb. 23, 2024, 9:01 a.m. UTC
From: Johannes Berg <johannes.berg@intel.com>

There's a somewhat common pattern of using FIELD_PREP()
even for single bits, e.g.

 cmd->info1 |= FIELD_PREP(HTT_SRNG_SETUP_CMD_INFO1_RING_FLAGS_MSI_SWAP,
                          !!(params.flags & HAL_SRNG_FLAGS_MSI_SWAP));

which might as well be written as

 if (params.flags & HAL_SRNG_FLAGS_MSI_SWAP)
   cmd->info1 |= HTT_SRNG_SETUP_CMD_INFO1_RING_FLAGS_MSI_SWAP;

(since info1 is fully initialized to start with), but in
a long chain of FIELD_PREP() this really seems fine.

However, it triggers a sparse warning, in the check in
the macro for whether a constant value fits into the mask,
as this contains a "& (_val)". In this case, this really
is always intentional, so just suppress the warning by
adding "0+" to the expression, indicating explicitly that
this is correct.

Signed-off-by: Johannes Berg <johannes.berg@intel.com>
---
 include/linux/bitfield.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Comments

Kalle Valo Feb. 28, 2024, 11:54 a.m. UTC | #1
Johannes Berg <johannes@sipsolutions.net> wrote:

> From: Johannes Berg <johannes.berg@intel.com>
> 
> There's a somewhat common pattern of using FIELD_PREP()
> even for single bits, e.g.
> 
>  cmd->info1 |= FIELD_PREP(HTT_SRNG_SETUP_CMD_INFO1_RING_FLAGS_MSI_SWAP,
>                           !!(params.flags & HAL_SRNG_FLAGS_MSI_SWAP));
> 
> which might as well be written as
> 
>  if (params.flags & HAL_SRNG_FLAGS_MSI_SWAP)
>    cmd->info1 |= HTT_SRNG_SETUP_CMD_INFO1_RING_FLAGS_MSI_SWAP;
> 
> (since info1 is fully initialized to start with), but in
> a long chain of FIELD_PREP() this really seems fine.
> 
> However, it triggers a sparse warning, in the check in
> the macro for whether a constant value fits into the mask,
> as this contains a "& (_val)". In this case, this really
> is always intentional, so just suppress the warning by
> adding "0+" to the expression, indicating explicitly that
> this is correct.
> 
> Signed-off-by: Johannes Berg <johannes.berg@intel.com>

Patch applied to wireless-next.git, thanks.

416eb60317c6 bitfield: suppress "dubious: x & !y" sparse warning
diff mbox series

Patch

diff --git a/include/linux/bitfield.h b/include/linux/bitfield.h
index ebfa12f69501..63928f173223 100644
--- a/include/linux/bitfield.h
+++ b/include/linux/bitfield.h
@@ -66,7 +66,8 @@ 
 				 _pfx "mask is not constant");		\
 		BUILD_BUG_ON_MSG((_mask) == 0, _pfx "mask is zero");	\
 		BUILD_BUG_ON_MSG(__builtin_constant_p(_val) ?		\
-				 ~((_mask) >> __bf_shf(_mask)) & (_val) : 0, \
+				 ~((_mask) >> __bf_shf(_mask)) &	\
+					(0 + (_val)) : 0,		\
 				 _pfx "value too large for the field"); \
 		BUILD_BUG_ON_MSG(__bf_cast_unsigned(_mask, _mask) >	\
 				 __bf_cast_unsigned(_reg, ~0ull),	\