Skip to contentSkip to conversation

ethernet: ice: avoid gcc-9 integer overflow warning

PATCH Superseded Arnd Bergmann 4 participants 4 messages Feb 14, 2023 – Feb 15, 2023

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ethernet: ice: avoid gcc-9 integer overflow warning

From: Arnd Bergmann <[email protected]>

With older compilers like gcc-9, the calculation of the vlan priority field causes a warning from the byteswap:

In file included from drivers/net/ethernet/intel/ice/ice_tc_lib.c:4:
drivers/net/ethernet/intel/ice/ice_tc_lib.c: In function 'ice_parse_cls_flower':
include/uapi/linux/swab.h:15:15: error: integer overflow in expression '(int)(short unsigned int)((int)match.key-><U67c8>.<U6698>.vlan_priority << 13) & 57344 & 255' of type 'int' results in '0' [-Werror=overflow]

   15 |  (((__u16)(x) & (__u16)0x00ffU) << 8) |   \
      |   ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
include/uapi/linux/swab.h:106:2: note: in expansion of macro '___constant_swab16'
  106 |  ___constant_swab16(x) :   \
      |  ^~~~~~~~~~~~~~~~~~

include/uapi/linux/byteorder/little_endian.h:42:43: note: in expansion of macro '__swab16'
42 | #define __cpu_to_be16(x) ((__force __be16)__swab16((x)))
| ^~~~~~~~
include/linux/byteorder/generic.h:96:21: note: in expansion of macro '__cpu_to_be16'
96 | #define cpu_to_be16 __cpu_to_be16

      |                     ^~~~~~~~~~~~~
drivers/net/ethernet/intel/ice/ice_tc_lib.c:1458:5: note: in expansion of macro 'cpu_to_be16'
 1458 |     cpu_to_be16((match.key->vlan_priority <<
      |     ^~~~~~~~~~~

The code looks correct to me, so just avoid the warning by replacing the macro expansion with an intermediate variable.

Fixes 34800178b302 ("ice: Add support for VLAN priority filters in switchdev")
---
 drivers/net/ethernet/intel/ice/ice_tc_lib.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
drivers/net/ethernet/intel/ice/ice_tc_lib.c +4 -6
index 6b48cbc049c6..e9932446185c 100644
@@ -1453,10 +1453,9 @@ ice_parse_cls_flower(struct net_device *filter_dev, struct ice_vsi *vsi,
 		}
 
 		if (match.mask->vlan_priority) {
+			u16 prio = (match.key->vlan_priority << VLAN_PRIO_SHIFT) & VLAN_PRIO_MASK;
 			fltr->flags |= ICE_TC_FLWR_FIELD_VLAN_PRIO;
-			headers->vlan_hdr.vlan_prio =
-				cpu_to_be16((match.key->vlan_priority <<
-					     VLAN_PRIO_SHIFT) & VLAN_PRIO_MASK);
+			headers->vlan_hdr.vlan_prio = cpu_to_be16(prio);
 		}
 
 		if (match.mask->vlan_tpid)
@@ -1487,10 +1486,9 @@ ice_parse_cls_flower(struct net_device *filter_dev, struct ice_vsi *vsi,
 		}
 
 		if (match.mask->vlan_priority) {
+			u16 prio = (match.key->vlan_priority << VLAN_PRIO_SHIFT) & VLAN_PRIO_MASK;
 			fltr->flags |= ICE_TC_FLWR_FIELD_CVLAN_PRIO;
-			headers->cvlan_hdr.vlan_prio =
-				cpu_to_be16((match.key->vlan_priority <<
-					     VLAN_PRIO_SHIFT) & VLAN_PRIO_MASK);
+			headers->cvlan_hdr.vlan_prio = cpu_to_be16(prio);
 		}
 	}
 
Signature · 1 line
2.39.1
#1 lore raw

From: Arnd Bergmann <[email protected]>
Date: Tue, 14 Feb 2023 14:19:49 +0100

Quoted 23 lines

From: Arnd Bergmann <[email protected]>

With older compilers like gcc-9, the calculation of the vlan priority field causes a warning from the byteswap:

In file included from drivers/net/ethernet/intel/ice/ice_tc_lib.c:4:
drivers/net/ethernet/intel/ice/ice_tc_lib.c: In function 'ice_parse_cls_flower':
include/uapi/linux/swab.h:15:15: error: integer overflow in expression '(int)(short unsigned int)((int)match.key-><U67c8>.<U6698>.vlan_priority << 13) & 57344 & 255' of type 'int' results in '0' [-Werror=overflow]

   15 |  (((__u16)(x) & (__u16)0x00ffU) << 8) |   \
      |   ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
include/uapi/linux/swab.h:106:2: note: in expansion of macro '___constant_swab16'
  106 |  ___constant_swab16(x) :   \
      |  ^~~~~~~~~~~~~~~~~~

include/uapi/linux/byteorder/little_endian.h:42:43: note: in expansion of macro '__swab16'
42 | #define __cpu_to_be16(x) ((__force __be16)__swab16((x)))
| ^~~~~~~~
include/linux/byteorder/generic.h:96:21: note: in expansion of macro '__cpu_to_be16'
96 | #define cpu_to_be16 __cpu_to_be16

      |                     ^~~~~~~~~~~~~
drivers/net/ethernet/intel/ice/ice_tc_lib.c:1458:5: note: in expansion of macro 'cpu_to_be16'
 1458 |     cpu_to_be16((match.key->vlan_priority <<
      |     ^~~~~~~~~~~
Quoted 4 lines

The code looks correct to me, so just avoid the warning by replacing the macro expansion with an intermediate variable.

Fixes 34800178b302 ("ice: Add support for VLAN priority filters in switchdev")

Should it have "Fixes:" tag if the code itself is correct?

Quoted 10 lines
---
 drivers/net/ethernet/intel/ice/ice_tc_lib.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
drivers/net/ethernet/intel/ice/ice_tc_lib.c +0 -0
index 6b48cbc049c6..e9932446185c 100644

drivers/net/ethernet/intel/ice/ice_tc_lib.c ice_parse_cls_flower(struct net_device *filter_dev, struct ice_vsi *vsi, · line 1456

Quoted 4 lines
drivers/net/ethernet/intel/ice/ice_tc_lib.c +1 -0
@@ -1453,10 +1453,9 @@ ice_parse_cls_flower(struct net_device *filter_dev, struct ice_vsi *vsi,
 		}
 
 		if (match.mask->vlan_priority) {
+			u16 prio = (match.key->vlan_priority << VLAN_PRIO_SHIFT) & VLAN_PRIO_MASK;

Maybe just u32?

Also, I'd go for FIELD_PREP() here. Even be16_encode_bits(). Or does it have the same problem on GCC 9?

			__be16 pri;

			pri = be16_encode_bits(match.key->vlan_priority,
					       VLAN_PRIO_MASK);
			headers->vlan_hdr.vlan_prio = pri;

But I suspect you (or somebody else) will say "please don't mix fixes and improvements" :D Altho I'd go for this one if it silences the warning.

Quoted 9 lines
drivers/net/ethernet/intel/ice/ice_tc_lib.c +1 -3
 			fltr->flags |= ICE_TC_FLWR_FIELD_VLAN_PRIO;
-			headers->vlan_hdr.vlan_prio =
-				cpu_to_be16((match.key->vlan_priority <<
-					     VLAN_PRIO_SHIFT) & VLAN_PRIO_MASK);
+			headers->vlan_hdr.vlan_prio = cpu_to_be16(prio);
 		}
 
 		if (match.mask->vlan_tpid)

drivers/net/ethernet/intel/ice/ice_tc_lib.c ice_parse_cls_flower(struct net_device *filter_dev, struct ice_vsi *vsi, · line 1491

Quoted 9 lines
drivers/net/ethernet/intel/ice/ice_tc_lib.c +2 -3
@@ -1487,10 +1486,9 @@ ice_parse_cls_flower(struct net_device *filter_dev, struct ice_vsi *vsi,
 		}
 
 		if (match.mask->vlan_priority) {
+			u16 prio = (match.key->vlan_priority << VLAN_PRIO_SHIFT) & VLAN_PRIO_MASK;
 			fltr->flags |= ICE_TC_FLWR_FIELD_CVLAN_PRIO;
-			headers->cvlan_hdr.vlan_prio =
-				cpu_to_be16((match.key->vlan_priority <<
-					     VLAN_PRIO_SHIFT) & VLAN_PRIO_MASK);
+			headers->cvlan_hdr.vlan_prio = cpu_to_be16(prio);

^

drivers/net/ethernet/intel/ice/ice_tc_lib.c ice_parse_cls_flower(struct net_device *filter_dev, struct ice_vsi *vsi, · line 1494

Quoted 3 lines
drivers/net/ethernet/intel/ice/ice_tc_lib.c +0 -0
 		}
 	}
 

Thanks,
Olek

Reviewed-by on v1 1/1
#2 lore raw
In reply to Arnd Bergmann
Quoted 23 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

With older compilers like gcc-9, the calculation of the vlan priority field causes a warning from the byteswap:

In file included from drivers/net/ethernet/intel/ice/ice_tc_lib.c:4:
drivers/net/ethernet/intel/ice/ice_tc_lib.c: In function 'ice_parse_cls_flower':
include/uapi/linux/swab.h:15:15: error: integer overflow in expression '(int)(short unsigned int)((int)match.key-><U67c8>.<U6698>.vlan_priority << 13) & 57344 & 255' of type 'int' results in '0' [-Werror=overflow]

   15 |  (((__u16)(x) & (__u16)0x00ffU) << 8) |   \
      |   ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
include/uapi/linux/swab.h:106:2: note: in expansion of macro '___constant_swab16'
  106 |  ___constant_swab16(x) :   \
      |  ^~~~~~~~~~~~~~~~~~

include/uapi/linux/byteorder/little_endian.h:42:43: note: in expansion of macro '__swab16'
42 | #define __cpu_to_be16(x) ((__force __be16)__swab16((x)))
| ^~~~~~~~
include/linux/byteorder/generic.h:96:21: note: in expansion of macro '__cpu_to_be16'
96 | #define cpu_to_be16 __cpu_to_be16

      |                     ^~~~~~~~~~~~~
drivers/net/ethernet/intel/ice/ice_tc_lib.c:1458:5: note: in expansion of macro 'cpu_to_be16'
 1458 |     cpu_to_be16((match.key->vlan_priority <<
      |     ^~~~~~~~~~~
Quoted 5 lines

The code looks correct to me, so just avoid the warning by replacing the macro expansion with an intermediate variable.

Fixes 34800178b302 ("ice: Add support for VLAN priority filters in switchdev")

drivers/net/ethernet/intel/ice/ice_tc_lib.c ice_parse_cls_flower(struct net_device *filter_dev, struct ice_vsi *vsi, · line 1456

Quoted 13 lines
---
 drivers/net/ethernet/intel/ice/ice_tc_lib.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
drivers/net/ethernet/intel/ice/ice_tc_lib.c +1 -0
index 6b48cbc049c6..e9932446185c 100644
@@ -1453,10 +1453,9 @@ ice_parse_cls_flower(struct net_device *filter_dev, struct ice_vsi *vsi,
 		}
 
 		if (match.mask->vlan_priority) {
+			u16 prio = (match.key->vlan_priority << VLAN_PRIO_SHIFT) & VLAN_PRIO_MASK;

nit: usually for such scoped stack var we follow the declaration with an empty line

Quoted 24 lines
drivers/net/ethernet/intel/ice/ice_tc_lib.c +3 -6
 			fltr->flags |= ICE_TC_FLWR_FIELD_VLAN_PRIO;
-			headers->vlan_hdr.vlan_prio =
-				cpu_to_be16((match.key->vlan_priority <<
-					     VLAN_PRIO_SHIFT) & VLAN_PRIO_MASK);
+			headers->vlan_hdr.vlan_prio = cpu_to_be16(prio);
 		}
 
 		if (match.mask->vlan_tpid)
@@ -1487,10 +1486,9 @@ ice_parse_cls_flower(struct net_device *filter_dev, struct ice_vsi *vsi,
 		}
 
 		if (match.mask->vlan_priority) {
+			u16 prio = (match.key->vlan_priority << VLAN_PRIO_SHIFT) & VLAN_PRIO_MASK;
 			fltr->flags |= ICE_TC_FLWR_FIELD_CVLAN_PRIO;
-			headers->cvlan_hdr.vlan_prio =
-				cpu_to_be16((match.key->vlan_priority <<
-					     VLAN_PRIO_SHIFT) & VLAN_PRIO_MASK);
+			headers->cvlan_hdr.vlan_prio = cpu_to_be16(prio);
 		}
 	}
 
Signature · 1 line
2.39.1

_______________________________________________
Intel-wired-lan mailing list
[email protected]
https://lists.osuosl.org/mailman/listinfo/intel-wired-lan

#3 lore raw
In reply to Arnd Bergmann

From: Arnd Bergmann

Quoted 7 lines

Sent: 14 February 2023 13:20

From: Arnd Bergmann <[email protected]>

With older compilers like gcc-9, the calculation of the vlan priority field causes a warning from the byteswap:

...

Quoted 12 lines
Fixes 34800178b302 ("ice: Add support for VLAN priority filters in switchdev")
---
 drivers/net/ethernet/intel/ice/ice_tc_lib.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
drivers/net/ethernet/intel/ice/ice_tc_lib.c +0 -0
index 6b48cbc049c6..e9932446185c 100644

drivers/net/ethernet/intel/ice/ice_tc_lib.c ice_parse_cls_flower(struct net_device *filter_dev, struct ice_vsi *vsi, · line 1459

Quoted 10 lines
drivers/net/ethernet/intel/ice/ice_tc_lib.c +2 -3
@@ -1453,10 +1453,9 @@ ice_parse_cls_flower(struct net_device *filter_dev, struct ice_vsi *vsi,
 		}
 
 		if (match.mask->vlan_priority) {
+			u16 prio = (match.key->vlan_priority << VLAN_PRIO_SHIFT) & VLAN_PRIO_MASK;
 			fltr->flags |= ICE_TC_FLWR_FIELD_VLAN_PRIO;
-			headers->vlan_hdr.vlan_prio =
-				cpu_to_be16((match.key->vlan_priority <<
-					     VLAN_PRIO_SHIFT) & VLAN_PRIO_MASK);
+			headers->vlan_hdr.vlan_prio = cpu_to_be16(prio);
 		}

Is there something that will do:

	unsigned int pri = match.key->vlan_priority & (VLAN_PRIO_MASK >> VLAN_PRIO_SHIFT);
	headers->vlan_hdr.vlan_prio = pri << (VLAN_PRIO_SHIFT ^ (le ? 8 : 0));

	David

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

_______________________________________________
Intel-wired-lan mailing list
[email protected]
https://lists.osuosl.org/mailman/listinfo/intel-wired-lan