Skip to contentSkip to conversation

bitfield: avoid gcc-8 -Wint-in-bool-context warning

PATCH In review Arnd Bergmann 5 participants 11 messages Aug 13, 2018 – Aug 14, 2018 Bitmap API

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bitfield: avoid gcc-8 -Wint-in-bool-context warning

Passing an enum into FIELD_GET() produces a long but harmless warning on newer compilers:

from include/linux/linkage.h:7,
from include/linux/kernel.h:7,
from include/linux/skbuff.h:17,
from include/linux/if_ether.h:23,
from include/linux/etherdevice.h:25,
from drivers/net/wireless/intel/iwlwifi/mvm/rxmq.c:63:
drivers/net/wireless/intel/iwlwifi/mvm/rxmq.c: In function 'iwl_mvm_rx_mpdu_mq':
include/linux/bitfield.h:56:20: error: enum constant in boolean context [-Werror=int-in-bool-context]

   BUILD_BUG_ON_MSG(!(_mask), _pfx "mask is zero"); \
                    ^

...
include/linux/bitfield.h:103:3: note: in expansion of macro '__BF_FIELD_CHECK'
__BF_FIELD_CHECK(_mask, _reg, 0U, "FIELD_GET: "); \
^~~~~~~~~~~~~~~~
drivers/net/wireless/intel/iwlwifi/mvm/rxmq.c:1025:21: note: in expansion of macro 'FIELD_GET'

    le16_encode_bits(FIELD_GET(IWL_RX_HE_PHY_SIBG_SYM_OR_USER_NUM_MASK,

The problem here is that the caller has no idea how the macro gets expanding, leading to a false-positive. It can be trivially avoided by doing a comparison against zero.

This only recently started appearing as the iwlwifi driver was patched to use FIELD_GET.

Fixes 514c30696fbc ("iwlwifi: add support for IEEE802.11ax")
---
 include/linux/bitfield.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/bitfield.h +1 -1
index 65a6981eef7b..3f1ef4450a7c 100644
@@ -53,7 +53,7 @@
 	({								\
 		BUILD_BUG_ON_MSG(!__builtin_constant_p(_mask),		\
 				 _pfx "mask is not constant");		\
-		BUILD_BUG_ON_MSG(!(_mask), _pfx "mask is zero");	\
+		BUILD_BUG_ON_MSG((_mask) == 0, _pfx "mask is zero");	\
 		BUILD_BUG_ON_MSG(__builtin_constant_p(_val) ?		\
 				 ~((_mask) >> __bf_shf(_mask)) & (_val) : 0, \
 				 _pfx "value too large for the field"); \
Signature · 1 line
2.18.0
#1 lore raw

2018-08-14 7:09 GMT+09:00 Arnd Bergmann <[email protected]>:

Quoted 11 lines

Passing an enum into FIELD_GET() produces a long but harmless warning on newer compilers:

from include/linux/linkage.h:7,
from include/linux/kernel.h:7,
from include/linux/skbuff.h:17,
from include/linux/if_ether.h:23,
from include/linux/etherdevice.h:25,
from drivers/net/wireless/intel/iwlwifi/mvm/rxmq.c:63:
drivers/net/wireless/intel/iwlwifi/mvm/rxmq.c: In function 'iwl_mvm_rx_mpdu_mq':
include/linux/bitfield.h:56:20: error: enum constant in boolean context [-Werror=int-in-bool-context]

Quoted 8 lines
   BUILD_BUG_ON_MSG(!(_mask), _pfx "mask is zero"); \
                    ^
...
include/linux/bitfield.h:103:3: note: in expansion of macro '__BF_FIELD_CHECK'
   __BF_FIELD_CHECK(_mask, _reg, 0U, "FIELD_GET: "); \
   ^~~~~~~~~~~~~~~~
drivers/net/wireless/intel/iwlwifi/mvm/rxmq.c:1025:21: note: in expansion of macro 'FIELD_GET'
    le16_encode_bits(FIELD_GET(IWL_RX_HE_PHY_SIBG_SYM_OR_USER_NUM_MASK,

How about fixing the root cause
in drivers/net/wireless/intel/iwlwifi/fw/api/rx.h ?

#define IWL_RX_HE_PHY_SIBG_SYM_OR_USER_NUM_MASK 0x1e00000000ULL

enum iwl_rx_he_phy looks really strange.

Passing enum to FIELD_GET is odd,
so I prefer keeping this warned.

Quoted 29 lines

The problem here is that the caller has no idea how the macro gets expanding, leading to a false-positive. It can be trivially avoided by doing a comparison against zero.

This only recently started appearing as the iwlwifi driver was patched to use FIELD_GET.

Fixes 514c30696fbc ("iwlwifi: add support for IEEE802.11ax")
---
 include/linux/bitfield.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/bitfield.h +1 -2
index 65a6981eef7b..3f1ef4450a7c 100644
@@ -53,7 +53,7 @@
        ({                                                              \
                BUILD_BUG_ON_MSG(!__builtin_constant_p(_mask),          \
                                 _pfx "mask is not constant");          \
-               BUILD_BUG_ON_MSG(!(_mask), _pfx "mask is zero");        \
+               BUILD_BUG_ON_MSG((_mask) == 0, _pfx "mask is zero");    \
                BUILD_BUG_ON_MSG(__builtin_constant_p(_val) ?           \
                                 ~((_mask) >> __bf_shf(_mask)) & (_val) : 0, \
                                 _pfx "value too large for the field"); \
--

2.18.0

Signature · 2 lines
Best Regards
Masahiro Yamada
#2 lore raw
Quoted 22 lines · Masahiro Yamada

2018-08-14 7:09 GMT+09:00 Arnd Bergmann <[email protected]>:

Quoted 11 lines

Passing an enum into FIELD_GET() produces a long but harmless warning on newer compilers:

from include/linux/linkage.h:7,
from include/linux/kernel.h:7,
from include/linux/skbuff.h:17,
from include/linux/if_ether.h:23,
from include/linux/etherdevice.h:25,
from drivers/net/wireless/intel/iwlwifi/mvm/rxmq.c:63:
drivers/net/wireless/intel/iwlwifi/mvm/rxmq.c: In function 'iwl_mvm_rx_mpdu_mq':
include/linux/bitfield.h:56:20: error: enum constant in boolean context [-Werror=int-in-bool-context]

Quoted 8 lines
   BUILD_BUG_ON_MSG(!(_mask), _pfx "mask is zero"); \
                    ^
...
include/linux/bitfield.h:103:3: note: in expansion of macro '__BF_FIELD_CHECK'
   __BF_FIELD_CHECK(_mask, _reg, 0U, "FIELD_GET: "); \
   ^~~~~~~~~~~~~~~~
drivers/net/wireless/intel/iwlwifi/mvm/rxmq.c:1025:21: note: in expansion of macro 'FIELD_GET'
    le16_encode_bits(FIELD_GET(IWL_RX_HE_PHY_SIBG_SYM_OR_USER_NUM_MASK,
Quoted 8 lines

How about fixing the root cause
in drivers/net/wireless/intel/iwlwifi/fw/api/rx.h ?

#define IWL_RX_HE_PHY_SIBG_SYM_OR_USER_NUM_MASK 0x1e00000000ULL

enum iwl_rx_he_phy looks really strange.

Why? I don't think this is a problem, the enum is used here to get constants so that we can also have documentation for them. That's a common and accepted technique.

Quoted 2 lines

Passing enum to FIELD_GET is odd,
so I prefer keeping this warned.

What for?

I think we should go with Arend's patch, and I hope Andrew will pick it up, but otherwise I guess we can also put it through any other tree.

johannes

#3 lore raw
Quoted 11 lines · Johannes Berg
Quoted 10 lines · Masahiro Yamada

2018-08-14 7:09 GMT+09:00 Arnd Bergmann <[email protected]>:

Quoted 2 lines

drivers/net/wireless/intel/iwlwifi/mvm/rxmq.c:1025:21: note: in expansion of macro 'FIELD_GET'

    le16_encode_bits(FIELD_GET(IWL_RX_HE_PHY_SIBG_SYM_OR_USER_NUM_MASK,

How about fixing the root cause
in drivers/net/wireless/intel/iwlwifi/fw/api/rx.h ?

Quoted 8 lines
Quoted 4 lines

#define IWL_RX_HE_PHY_SIBG_SYM_OR_USER_NUM_MASK 0x1e00000000ULL

enum iwl_rx_he_phy looks really strange.

Why? I don't think this is a problem, the enum is used here to get constants so that we can also have documentation for them. That's a common and accepted technique.

I think using #define is more common overall, but I also prefer using enum for this in my own code.

Arnd

#4 lore raw
In reply to Johannes Berg

2018-08-14 16:56 GMT+09:00 Johannes Berg <[email protected]>:

Quoted 27 lines · Masahiro Yamada
Quoted 26 lines

2018-08-14 7:09 GMT+09:00 Arnd Bergmann <[email protected]>:

Quoted 11 lines

Passing an enum into FIELD_GET() produces a long but harmless warning on newer compilers:

from include/linux/linkage.h:7,
from include/linux/kernel.h:7,
from include/linux/skbuff.h:17,
from include/linux/if_ether.h:23,
from include/linux/etherdevice.h:25,
from drivers/net/wireless/intel/iwlwifi/mvm/rxmq.c:63:
drivers/net/wireless/intel/iwlwifi/mvm/rxmq.c: In function 'iwl_mvm_rx_mpdu_mq':
include/linux/bitfield.h:56:20: error: enum constant in boolean context [-Werror=int-in-bool-context]

Quoted 8 lines
   BUILD_BUG_ON_MSG(!(_mask), _pfx "mask is zero"); \
                    ^
...
include/linux/bitfield.h:103:3: note: in expansion of macro '__BF_FIELD_CHECK'
   __BF_FIELD_CHECK(_mask, _reg, 0U, "FIELD_GET: "); \
   ^~~~~~~~~~~~~~~~
drivers/net/wireless/intel/iwlwifi/mvm/rxmq.c:1025:21: note: in expansion of macro 'FIELD_GET'
    le16_encode_bits(FIELD_GET(IWL_RX_HE_PHY_SIBG_SYM_OR_USER_NUM_MASK,

How about fixing the root cause
in drivers/net/wireless/intel/iwlwifi/fw/api/rx.h ?

Quoted 8 lines
Quoted 4 lines

#define IWL_RX_HE_PHY_SIBG_SYM_OR_USER_NUM_MASK 0x1e00000000ULL

enum iwl_rx_he_phy looks really strange.

Why? I don't think this is a problem, the enum is used here to get constants so that we can also have documentation for them. That's a common and accepted technique.

I do not see any variable declared as 'enum iwl_rx_he_phy'.

This is not legitimate usage of enum.

The mask macros must have a particular value,
hence

#define IWL_RX_HE_PHY_SIBG_SYM_OR_USER_NUM_MASK 0x1e00000000ULL

is a straightforward way.

Quoted 5 lines
Quoted 3 lines

Passing enum to FIELD_GET is odd,
so I prefer keeping this warned.

What for?

If you pass enum to FIELD_GET,
it is very like to be _abuse_ of enum.

Quoted 4 lines

I think we should go with Arend's patch, and I hope Andrew will pick it up, but otherwise I guess we can also put it through any other tree.

johannes

Signature · 2 lines
Best Regards
Masahiro Yamada
#5 lore raw
In reply to Johannes Berg

From: Johannes Berg

Quoted 1 line

Sent: 14 August 2018 08:57

...

Quoted 4 lines
Quoted 4 lines

How about fixing the root cause
in drivers/net/wireless/intel/iwlwifi/fw/api/rx.h ?

Quoted 8 lines
Quoted 4 lines

#define IWL_RX_HE_PHY_SIBG_SYM_OR_USER_NUM_MASK 0x1e00000000ULL

enum iwl_rx_he_phy looks really strange.

Why? I don't think this is a problem, the enum is used here to get constants so that we can also have documentation for them. That's a common and accepted technique.

It would be much more useful to indicate where the values are used. Such a field/parameter could (probably) have the type of the enum. But, at some point, the compiler might start barfing at that at well.

There are also a whole load of crappy __packed in that header file. There might be one or two 64bit items on 32bit boundaries but that can be solved without using __packed.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

#6 lore raw
Quoted 13 lines · David Laight

From: Johannes Berg

Quoted 1 line

Sent: 14 August 2018 08:57

...

Quoted 9 lines
Quoted 8 lines

How about fixing the root cause
in drivers/net/wireless/intel/iwlwifi/fw/api/rx.h ?

#define IWL_RX_HE_PHY_SIBG_SYM_OR_USER_NUM_MASK 0x1e00000000ULL

enum iwl_rx_he_phy looks really strange.

Quoted 7 lines
Quoted 3 lines

Why? I don't think this is a problem, the enum is used here to get constants so that we can also have documentation for them. That's a common and accepted technique.

It would be much more useful to indicate where the values are used. Such a field/parameter could (probably) have the type of the enum. But, at some point, the compiler might start barfing at that at well.

I think the compiler warning here only happens because one uses a compile-time constant expression that is not a numeric literal value into a boolean operator. That doesn't mean that there is something wrong with the enum in particular, or that enums cause a lot of issues elsewhere.

I would also argue that generally speaking the BUILD_BUG_ON_MSG() should try to either produce the specific build failure it was designed for, or not produce any output at all, rather than something that is more confusing to developers. If we want to enforce passing in number literals here, we should make that an explicit check, or otherwise allow any compile-time constant values.

Quoted 3 lines

There are also a whole load of crappy __packed in that header file. There might be one or two 64bit items on 32bit boundaries but that can be solved without using __packed.

Agreed, this likely causes problems on architectures without unaligned load/store instructions that end up doing byte accesses to the descriptor fields, which in turn can confuse the hardware, and can become very slow when they live in dma_alloc_coherent() memory. That looks like a completely unrelated issue though.

Arnd

#7 lore raw
Quoted 10 lines · Arnd Bergmann
Quoted 3 lines

It would be much more useful to indicate where the values are used. Such a field/parameter could (probably) have the type of the enum. But, at some point, the compiler might start barfing at that at well.

I think the compiler warning here only happens because one uses a compile-time constant expression that is not a numeric literal value into a boolean operator. That doesn't mean that there is something wrong with the enum in particular, or that enums cause a lot of issues elsewhere.

Quoted 6 lines

I would also argue that generally speaking the BUILD_BUG_ON_MSG() should try to either produce the specific build failure it was designed for, or not produce any output at all, rather than something that is more confusing to developers. If we want to enforce passing in number literals here, we should make that an explicit check, or otherwise allow any compile-time constant values.

I don't see why we should only be allowed to pass a number literal (perhaps after pre-processing), rather than any constant integer value. Clearly it needs to be a constant for all the constant-folding to work, but nothing else is required.

Quoted 9 lines
Quoted 3 lines

There are also a whole load of crappy __packed in that header file. There might be one or two 64bit items on 32bit boundaries but that can be solved without using __packed.

Agreed, this likely causes problems on architectures without unaligned load/store instructions that end up doing byte accesses to the descriptor fields, which in turn can confuse the hardware, and can become very slow when they live in dma_alloc_coherent() memory. That looks like a completely unrelated issue though.

We know about this, but it doesn't cause issues apart from perhaps somewhat slower access on some architectures, since all of the structures live only in DRAM and are not usually used with coherent memory, just sent to/from the device.

Since the devices really only ship on x86 systems, we decided to ignore that slight performance issue (for now).

johannes

#8 lore raw
In reply to Arnd Bergmann

From: Arnd Bergmann

Quoted 1 line

Sent: 14 August 2018 12:08

...

Quoted 9 lines
Quoted 3 lines

There are also a whole load of crappy __packed in that header file. There might be one or two 64bit items on 32bit boundaries but that can be solved without using __packed.

Agreed, this likely causes problems on architectures without unaligned load/store instructions that end up doing byte accesses to the descriptor fields, which in turn can confuse the hardware, and can become very slow when they live in dma_alloc_coherent() memory. That looks like a completely unrelated issue though.

If you ever define a variable of one of those types (or embed one in another structure that contains non-word sized items) you'll get the entire structure misaligned.
I doubt that is what you had in mind.

Maybe you could use __packed __attribute__((aligned(4))).
But it is much better just to use a 64bit type with only 4 byte alignment (I think there is a standard one in one of the header files).

I'd also add a compile-time assert on the length of the non-trivial structures. That will detect all sorts of errors.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

#9 lore raw
In reply to Arnd Bergmann
Quoted 11 lines · On Tue, 14 Aug 2018 00:09:34 +0200 Arnd Bergmann <[email protected]> wrote:

Passing an enum into FIELD_GET() produces a long but harmless warning on newer compilers:

from include/linux/linkage.h:7,
from include/linux/kernel.h:7,
from include/linux/skbuff.h:17,
from include/linux/if_ether.h:23,
from include/linux/etherdevice.h:25,
from drivers/net/wireless/intel/iwlwifi/mvm/rxmq.c:63:
drivers/net/wireless/intel/iwlwifi/mvm/rxmq.c: In function 'iwl_mvm_rx_mpdu_mq':
include/linux/bitfield.h:56:20: error: enum constant in boolean context [-Werror=int-in-bool-context]

Quoted 8 lines
   BUILD_BUG_ON_MSG(!(_mask), _pfx "mask is zero"); \
                    ^
...
include/linux/bitfield.h:103:3: note: in expansion of macro '__BF_FIELD_CHECK'
   __BF_FIELD_CHECK(_mask, _reg, 0U, "FIELD_GET: "); \
   ^~~~~~~~~~~~~~~~
drivers/net/wireless/intel/iwlwifi/mvm/rxmq.c:1025:21: note: in expansion of macro 'FIELD_GET'
    le16_encode_bits(FIELD_GET(IWL_RX_HE_PHY_SIBG_SYM_OR_USER_NUM_MASK,

Newer compilers will previously be used on older kernels, so I'll add a cc:stable here.

Quoted 18 lines

The problem here is that the caller has no idea how the macro gets expanding, leading to a false-positive. It can be trivially avoided by doing a comparison against zero.

This only recently started appearing as the iwlwifi driver was patched to use FIELD_GET.

Fixes 514c30696fbc ("iwlwifi: add support for IEEE802.11ax")
---
 include/linux/bitfield.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/bitfield.h +0 -0
index 65a6981eef7b..3f1ef4450a7c 100644

include/linux/bitfield.h line 58

Quoted 7 lines
include/linux/bitfield.h +1 -1
@@ -53,7 +53,7 @@
 	({								\
 		BUILD_BUG_ON_MSG(!__builtin_constant_p(_mask),		\
 				 _pfx "mask is not constant");		\
-		BUILD_BUG_ON_MSG(!(_mask), _pfx "mask is zero");	\
+		BUILD_BUG_ON_MSG((_mask) == 0, _pfx "mask is zero");	\
 		BUILD_BUG_ON_MSG(__builtin_constant_p(_val) ?		\
 				 ~((_mask) >> __bf_shf(_mask)) & (_val) : 0, \

I'm not understanding how a switch from !x to x==0 can fix anything. Help!

#10 lore raw
Quoted 8 lines · On Tue, 14 Aug 2018 16:11:06 -0700 Andrew Morton <[email protected]>…
Quoted 5 lines

include/linux/bitfield.h:103:3: note: in expansion of macro '__BF_FIELD_CHECK'

   __BF_FIELD_CHECK(_mask, _reg, 0U, "FIELD_GET: "); \
   ^~~~~~~~~~~~~~~~
drivers/net/wireless/intel/iwlwifi/mvm/rxmq.c:1025:21: note: in expansion of macro 'FIELD_GET'
    le16_encode_bits(FIELD_GET(IWL_RX_HE_PHY_SIBG_SYM_OR_USER_NUM_MASK,

Newer compilers will previously be used on older kernels, so I'll add a cc:stable here.

No I won't. Older kernels don't have the iwlwifi patch.