ethtool: use bits.h defines
Subsystems (3)
cover ethtool: use bits.h defines
Change the ethtool files in the kernel, including uapi header files, to use the kernel style BIT() and BIT_ULL() functions instead of open-coding bit shift operations.
Making this change results in a more consistent presentation of bit- shift operations as well as reduces the further likelihood of mistaken (1 << 31) usage which omits the 1UL that is necessary to get an unsigned result of the shift.
Jesse Brandeburg (2):
ethtool/uapi: use BIT for bit-shifts
ethtool: refactor bit-shifts
include/linux/ethtool.h | 2 +- include/uapi/linux/ethtool.h | 112 ++++++++++++++++----------- include/uapi/linux/ethtool_netlink.h | 6 +- net/ethtool/bitset.c | 14 ++-- net/ethtool/ioctl.c | 4 +- net/ethtool/strset.c | 6 +- 6 files changed, 84 insertions(+), 60 deletions(-)
base-commit: 01d0e110f2365151d8e69ca4978128112637642d
Signature · 1 line
2.31.1
PATCH 1/2 ethtool/uapi: use BIT for bit-shifts
Open-coded bit-shifts such as (1 << foo) are able to be accidentally coded to shift << 31 into the sign bit. The way to avoid this is to use the BIT() and BIT_ULL() macros which correctly define the shift as (1UL << foo) and (1ULL << foo), respectively.
Most of the kernel is using these already, but a few of the ethtool files had been avoided because they touch UAPI and the port wasn't quite clear.
This change matches one made to the userspace ethtool app that shows that this "small copy" of the code from bits.h allows us to use the modern and safe way in the ethtool UAPI files but have a way that still compiles both in the kernel (and for header checks) and in user-space, and avoids compilation dependencies by defining the macros in the same way as they are defined in the kernel proper header files.
Now we can use BIT() and friends in ethtool headers as well, so refactor the code to use BIT().
NOTES:
These duplicated macro definitions may need to be updated if the kernel definitions change, but it will generate warnings so we should be able to catch it.
This change is known to generate some unavoidable checkpatch errors due to keeping the code exactly the same as defined in other header files.
--- include/linux/ethtool.h | 2 +- include/uapi/linux/ethtool.h | 112 ++++++++++++++++----------- include/uapi/linux/ethtool_netlink.h | 6 +- net/ethtool/ioctl.c | 4 +- net/ethtool/strset.c | 6 +- 5 files changed, 76 insertions(+), 54 deletions(-)
include/linux/ethtool.h +1 -1
@@ -93,7 +93,7 @@ enum ethtool_supported_ring_param { ETHTOOL_RING_USE_TX_PUSH = BIT(2), }; -#define __ETH_RSS_HASH_BIT (bit) ((u32)1 << (bit)) +#define __ETH_RSS_HASH_BIT (bit) BIT((bit))#define __ETH_RSS_HASH (name) __ETH_RSS_HASH_BIT(ETH_RSS_HASH_##name##_BIT)#define ETH_RSS_HASH_TOP __ETH_RSS_HASH(TOP)
include/uapi/linux/ethtool.h +67 -45
179 lines not on this page · raw message
include/uapi/linux/ethtool_netlink.h +3 -3
@@ -108,11 +108,11 @@ enum {/* request header */ /* use compact bitsets in reply */ -#define ETHTOOL_FLAG_COMPACT_BITSETS (1 << 0) +#define ETHTOOL_FLAG_COMPACT_BITSETS BIT(0)/* provide optional reply for SET or ACT requests */ -#define ETHTOOL_FLAG_OMIT_REPLY (1 << 1) +#define ETHTOOL_FLAG_OMIT_REPLY BIT(1)/* request statistics, if supported by the driver */ -#define ETHTOOL_FLAG_STATS (1 << 2) +#define ETHTOOL_FLAG_STATS BIT(2)#define ETHTOOL_FLAG_ALL (ETHTOOL_FLAG_COMPACT_BITSETS | \ ETHTOOL_FLAG_OMIT_REPLY | \
net/ethtool/ioctl.c +2 -2
@@ -794,12 +794,12 @@ static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,* get_sset_count return */ for (i =0 ; i <64 ; i++) { -if (!(sset_mask & (1ULL << i))) +if (!(sset_mask &BIT_ULL (i)))continue ; rc =__ethtool_get_sset_count (dev, i);if (rc >=0 ) { - info.sset_mask |= (1ULL << i); + info.sset_mask |=BIT_ULL (i); info_buf[idx++] = rc; } }
net/ethtool/strset.c +3 -3
@@ -149,7 +149,7 @@ static bool strset_include(const struct strset_req_info *info,BUILD_BUG_ON (ETH_SS_COUNT >=BITS_PER_BYTE *sizeof (info->req_ids));if (info->req_ids) -return info->req_ids & (1U << id); +return info->req_ids &BIT (id); per_dev = data->sets[id].per_dev;if (!per_dev && !data->sets[id].strings)return false;
@@ -213,7 +213,7 @@ static int strset_parse_request(struct ethnl_req_info *req_base,return -EOPNOTSUPP ; } - req_info->req_ids |= (1U << id); + req_info->req_ids |=BIT (id); }return 0 ;
@@ -287,7 +287,7 @@ static int strset_prepare_data(const struct ethnl_req_info *req_base,if (!dev) {for (i =0 ; i <ETH_SS_COUNT ; i++) { -if ((req_info->req_ids & (1U << i)) && +if ((req_info->req_ids &BIT (i)) && data->sets[i].per_dev) {if (info)GENL_SET_ERR_MSG (info,"requested per device strings without dev" );
Signature · 1 line
2.31.1
Jakub Kicinski
maintainerRe: [PATCH net-next v1 1/2] ethtool/uapi: use BIT for bit-shifts
include/uapi/linux/ethtool.h line 12
Quoted 10 lines · On Wed, 7 Dec 2022 15:17:27 -0800 Jesse Brandeburg wrote:
#define
ETH_RSS_HASH_TOP__ETH_RSS_HASH(TOP)include/uapi/linux/ethtool.h +1
-0@@ -9,6 +9,7 @@* [email protected], * [email protected]) * Portions Copyright (C) Sun Microsystems 2008 +* Portions Copyright (C) 2022 Intel Corporation
Is that appropriate?
Quoted 10 lines
include/uapi/linux/ethtool.h +10
-0+/* BIT() and BIT_ULL() are defined in include/linux/bits.h but we need a +* local version to clean up this file and not break simultaneous +* kernel/userspace where userspace doesn't have the BIT and BIT_ULL +* defined. To avoid compiler issues we use the exact same definitions here +* of the macros as defined in the file noted below, so that we don't get +* 'duplicate define' or 'redefinition' errors. +*/ +/* include/uapi/linux/const.h */ +#define __AC (X ,Y ) (X##Y) +#define _AC (X ,Y ) __AC(X,Y)
include/uapi/linux/ethtool.h line 45
Quoted 10 lines
include/uapi/linux/ethtool.h +10
-0+#define _AT (T ,X ) ((T)(X)) +#define _UL (x) (_AC(x, UL)) +#define _ULL (x) (_AC(x, ULL)) +/* include/vdso/linux/const.h */ +#define UL (x) (_UL(x)) +#define ULL (x) (_ULL(x)) +/* include/vdso/bits.h */ +#define BIT (nr) (UL(1) << (nr)) +/* include/linux/bits.h */ +#define BIT_ULL (nr) (ULL(1) << (nr))
include/uapi/linux/const.h
Re: [PATCH net-next v1 1/2] ethtool/uapi: use BIT for bit-shifts
Quoted 13 lines · Jakub Kicinski
include/uapi/linux/ethtool.h line 12
Quoted 10 lines · On Wed, 7 Dec 2022 15:17:27 -0800 Jesse Brandeburg wrote:
#define ETH_RSS_HASH_TOP __ETH_RSS_HASH(TOP)include/uapi/linux/ethtool.h +1
-0@@ -9,6 +9,7 @@* [email protected], * [email protected]) * Portions Copyright (C) Sun Microsystems 2008 +* Portions Copyright (C) 2022 Intel Corporation Is that appropriate?
I added it when I thought I had to make more changes. Since these changes are now minimal I'll remove it, thanks!
Quoted 5 lines
include/uapi/linux/ethtool.h line 45
Quoted 3 lines
include/uapi/linux/ethtool.h +3
-0+#define BIT (nr) (UL(1) << (nr)) +/* include/linux/bits.h */ +#define BIT_ULL (nr) (ULL(1) << (nr))
include/uapi/linux/const.h
Wow, that was really not obvious that file was there, I searched all up and down for BIT() and BIT_ULL versions in the include/* and Documentation directory and totally missed that. I'll respin with the const.h versions! That file should really mention BIT or BIT_ULL in the comments so it would be caught by a grep!
PATCH 2/2 ethtool: refactor bit-shifts
In reply to Jesse BrandeburgWhile coding up the BIT() conversions for ethtool, some ugly conversions and code were noticed and we can fix them with a little GENMASK and a small refactor to use local variables to simplify some bitset.c code.
These changes should have no functional effect.
--- net/ethtool/bitset.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)
net/ethtool/bitset.c +8 -6
@@ -14,12 +14,12 @@static u32 ethnl_lower_bits (unsigned int n) { -return ~(u32 )0 >> (32 - n %32 ); +return GENMASK ((n %32 ),0 ); }static u32 ethnl_upper_bits (unsigned int n) { -return ~(u32 )0 << (n %32 ); +return GENMASK (31 , (n %32 )); }/**
@@ -452,8 +452,8 @@ ethnl_update_bitset32_verbose(u32 *bitmap, unsigned int nbits,ethnl_bitmap32_clear (bitmap,0 , nbits, mod);nla_for_each_nested (bit_attr, tb[ETHTOOL_A_BITSET_BITS ], rem) { +unsigned int idx, bitmap_idx, mod_idx;bool old_val, new_val; -unsigned int idx;if (nla_type (bit_attr) !=ETHTOOL_A_BITSET_BITS_BIT ) { NL_SET_ERR_MSG_ATTR(extack, bit_attr,
@@ -464,12 +464,14 @@ ethnl_update_bitset32_verbose(u32 *bitmap, unsigned int nbits, names, extack);if (ret <0 )return ret; - old_val = bitmap[idx /32 ] & ((u32 )1 << (idx %32 )); + bitmap_idx = idx /32 ; + mod_idx = idx %32 ; + old_val = bitmap[bitmap_idx] &BIT (mod_idx);if (new_val != old_val) {if (new_val) - bitmap[idx /32 ] |= ((u32 )1 << (idx %32 )); + bitmap[bitmap_idx] |=BIT (mod_idx);else - bitmap[idx /32 ] &= ~((u32 )1 << (idx %32 )); + bitmap[bitmap_idx] &= ~BIT (mod_idx); *mod = true; } }
Signature · 1 line
2.31.1
Jakub Kicinski
maintainerQuoted 8 lines · On Wed, 7 Dec 2022 15:17:26 -0800 Jesse Brandeburg wrote:
Change the ethtool files in the kernel, including uapi header files, to use the kernel style
BIT()andBIT_ULL()functions instead of open-coding bit shift operations.Making this change results in a more consistent presentation of bit- shift operations as well as reduces the further likelihood of mistaken (1 << 31) usage which omits the 1UL that is necessary to get an unsigned result of the shift.
Let's hear some opinions but the BIT / GENMASK macros are not universally loved so conversion == cleanup may not obvious.
Quoted 5 lines · Jakub Kicinski
Quoted 4 lines · On Wed, 7 Dec 2022 15:17:26 -0800 Jesse Brandeburg wrote:
Change the ethtool files in the kernel, including uapi header files, to use the kernel style
BIT()andBIT_ULL()functions instead of open-coding bit shift operations.
Quoted 7 lines
Quoted 4 lines
Making this change results in a more consistent presentation of bit- shift operations as well as reduces the further likelihood of mistaken (1 << 31) usage which omits the 1UL that is necessary to get an unsigned result of the shift.
Let's hear some opinions but the BIT / GENMASK macros are not universally loved so conversion == cleanup may not obvious.
Fair enough, I'm open to feedback but haven't seen any so far.
I'm planning to wait past the merge window now and submit the next version of this series in a couple of weeks.