Skip to contentSkip to conversation

linkmode: better use bitmap API

PATCH In review Yury Norov 6 participants 24 messages Sep 7 – Sep 9

Subsystems (11)
lore
Share & tools
lore

Yury Norov

author maintainer
#0 lore raw

cover linkmode: better use bitmap API

Several linkmode users discard the return value of bitmap_and() and then call linkmode_empty() on the result. Propagate the return value through linkmode_and() and use it to avoid redundant bitmap scans.

Add bitmap_and_and() and bitmap_and_andnot() for callers that combine three masks. The new helpers perform the operation and emptiness check in a single pass.

Yury Norov (9):
bitmap: add bitmap_and_and() and bitmap_and_andnot()
linkmode: make linkmode_and() return boolean
net: amd: xgbe: use linkmode_and() return value in
xgbe_set_link_ksettings()
ixgbe: use linkmode_and() return value in ixgbe_get_eee_fw()
net: phy: use linkmode_and() return value in

    genphy_c45_eee_is_active()

net: avoid copies before linkmode_and()
net: phy: use linkmode operation return values in phy_device.c
r8152: use linkmode_and_and() in EEE checks
MAINTAINERS: co-maintain linkmode.h under BITMAP

 MAINTAINERS                                   |  1 +
 drivers/net/ethernet/amd/xgbe/xgbe-ethtool.c  |  9 ++-
 .../hisilicon/hns3/hns3pf/hclge_mdio.c        |  5 +-
 .../net/ethernet/intel/ixgbe/ixgbe_ethtool.c  |  3 +-
 drivers/net/phy/phy-c45.c                     |  3 +-
 drivers/net/phy/phy.c                         |  7 +-
 drivers/net/phy/phy_device.c                  | 14 ++--
 drivers/net/usb/r8152.c                       | 10 +--
 include/linux/bitmap.h                        | 32 ++++++++
 include/linux/linkmode.h                      | 21 ++++-
 lib/bitmap.c                                  | 46 ++++++++---
 lib/test_bitmap.c                             | 78 +++++++++++++++++++
 12 files changed, 188 insertions(+), 41 deletions(-)
Signature · 1 line
2.53.0
author maintainer
#1 lore raw

PATCH 1/9 bitmap: add bitmap_and_and() and bitmap_and_andnot()

Add bitmap_and_and() and bitmap_and_andnot() to combine three bitmaps in a single pass. Both helpers return whether the resulting bitmap is non-empty.

Introduce BITMAP_OP() to share the word iteration with bitmap_and(), and add tests for small constants, multiword bitmaps, aliases, tail masking, empty results and zero-sized bitmaps.

---
 include/linux/bitmap.h | 32 +++++++++++++++++
 lib/bitmap.c           | 46 +++++++++++++++++++------
 lib/test_bitmap.c      | 78 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 146 insertions(+), 10 deletions(-)
include/linux/bitmap.h +32 -0
index 7df1573a409c..d524ef6b0300 100644
@@ -44,6 +44,10 @@ struct device;
  *  bitmap_fill(dst, nbits)                     *dst = ~0UL
  *  bitmap_copy(dst, src, nbits)                *dst = *src
  *  bitmap_and(dst, src1, src2, nbits)          *dst = *src1 & *src2
+ *  bitmap_and_and(dst, src1, src2, src3, nbits)
+ *                                               *dst = *src1 & *src2 & *src3
+ *  bitmap_and_andnot(dst, src1, src2, src3, nbits)
+ *                                               *dst = *src1 & *src2 & ~(*src3)
  *  bitmap_or(dst, src1, src2, nbits)           *dst = *src1 | *src2
  *  bitmap_weighted_or(dst, src1, src2, nbits)	*dst = *src1 | *src2. Returns Hamming Weight of dst
  *  bitmap_weighted_xor(dst, src1, src2, nbits)	*dst = *src1 ^ *src2. Returns Hamming Weight of dst
@@ -166,6 +170,12 @@ void bitmap_cut(unsigned long *dst, const unsigned long *src,
 		unsigned int first, unsigned int cut, unsigned int nbits);
 bool __bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
 		 const unsigned long *bitmap2, unsigned int nbits);
+bool __bitmap_and_and(unsigned long *dst, const unsigned long *bitmap1,
+		      const unsigned long *bitmap2,
+		      const unsigned long *bitmap3, unsigned int nbits);
+bool __bitmap_and_andnot(unsigned long *dst, const unsigned long *bitmap1,
+			 const unsigned long *bitmap2,
+			 const unsigned long *bitmap3, unsigned int nbits);
 void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
 		 const unsigned long *bitmap2, unsigned int nbits);
 unsigned int __bitmap_weighted_or(unsigned long *dst, const unsigned long *bitmap1,
@@ -337,6 +347,28 @@ bool bitmap_and(unsigned long *dst, const unsigned long *src1,
 	return __bitmap_and(dst, src1, src2, nbits);
 }
 
+static __always_inline
+bool bitmap_and_and(unsigned long *dst, const unsigned long *src1,
+		    const unsigned long *src2, const unsigned long *src3,
+		    unsigned int nbits)
+{
+	if (small_const_nbits(nbits))
+		return (*dst = *src1 & *src2 & *src3 &
+			       BITMAP_LAST_WORD_MASK(nbits)) != 0;
+	return __bitmap_and_and(dst, src1, src2, src3, nbits);
+}
+
+static __always_inline
+bool bitmap_and_andnot(unsigned long *dst, const unsigned long *src1,
+		       const unsigned long *src2, const unsigned long *src3,
+		       unsigned int nbits)
+{
+	if (small_const_nbits(nbits))
+		return (*dst = *src1 & *src2 & ~(*src3) &
+			       BITMAP_LAST_WORD_MASK(nbits)) != 0;
+	return __bitmap_and_andnot(dst, src1, src2, src3, nbits);
+}
+
 static __always_inline
 void bitmap_or(unsigned long *dst, const unsigned long *src1,
 	       const unsigned long *src2, unsigned int nbits)
lib/bitmap.c +36 -10

61 lines not on this page · raw message

lib/test_bitmap.c +78 -0

96 lines not on this page · raw message

Signature · 1 line
2.53.0
author maintainer
#2 lore raw

PATCH 2/9 linkmode: make linkmode_and() return boolean

In reply to Yury Norov

bitmap_and() returns true if the resulting bitmap is not empty. Propagate that return value through linkmode_and() so callers can use it where applicable.

---
 include/linux/linkmode.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
include/linux/linkmode.h +2 -2
index 3b9de09871f6..c08632c10c3d 100644
@@ -20,10 +20,10 @@ static inline void linkmode_copy(unsigned long *dst, const unsigned long *src)
 	bitmap_copy(dst, src, __ETHTOOL_LINK_MODE_MASK_NBITS);
 }
 
-static inline void linkmode_and(unsigned long *dst, const unsigned long *a,
+static inline bool linkmode_and(unsigned long *dst, const unsigned long *a,
 				const unsigned long *b)
 {
-	bitmap_and(dst, a, b, __ETHTOOL_LINK_MODE_MASK_NBITS);
+	return bitmap_and(dst, a, b, __ETHTOOL_LINK_MODE_MASK_NBITS);
 }
 
 static inline void linkmode_or(unsigned long *dst, const unsigned long *a,
Signature · 1 line
2.53.0
Reviewed-by on v1 2/9
#3 lore raw

RE: [PATCH 2/9] linkmode: make linkmode_and() return boolean

Quoted 50 lines

-----Original Message-----
From: Yury Norov <[email protected]>
Sent: Monday, September 7, 2026 11:55 PM
To: Andrew Lunn <[email protected]>; Heiner Kallweit <[email protected]>; Russell King <[email protected]>; Raju Rangoju <[email protected]>; Prashanth Kumar K R <[email protected]>; Nguyen, Anthony L <[email protected]>; Kitszel, Przemyslaw <[email protected]>; Jian Shen <[email protected]>; Jijie Shao <[email protected]>; David S. Miller <[email protected]>; Eric Dumazet <[email protected]>; Jakub Kicinski <[email protected]>; Paolo Abeni <[email protected]>; linux- [email protected]; [email protected]; intel-wired- [email protected]; [email protected]
Cc: Yury Norov <[email protected]>; Yury Norov <[email protected]>; Rasmus Villemoes <[email protected]>; Andrew Morton <[email protected]>
Subject: [PATCH 2/9] linkmode: make linkmode_and() return boolean

bitmap_and() returns true if the resulting bitmap is not empty. Propagate that return value through linkmode_and() so callers can use it where applicable.

---
 include/linux/linkmode.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
include/linux/linkmode.h index renamed from include/linux/linkmode.h +0 -0

3b9de09871f6..c08632c10c3d 100644

include/linux/linkmode.h +1 -1
@@ -20,10 +20,10 @@ static inline void linkmode_copy(unsigned long
 *dst, const unsigned long *src)
 	bitmap_copy(dst, src, __ETHTOOL_LINK_MODE_MASK_NBITS);  }
 
-static inline void linkmode_and(unsigned long *dst, const unsigned
 long *a,
+static inline bool linkmode_and(unsigned long *dst, const unsigned

long

include/linux/linkmode.h +2 -1
+*a,
 				const unsigned long *b)
 {
-	bitmap_and(dst, a, b, __ETHTOOL_LINK_MODE_MASK_NBITS);
+	return bitmap_and(dst, a, b, __ETHTOOL_LINK_MODE_MASK_NBITS);
 }
 
 static inline void linkmode_or(unsigned long *dst, const unsigned
Quoted 3 lines

long *a,
--
2.53.0

author maintainer
#4 lore raw

PATCH 3/9 net: amd: xgbe: use linkmode_and() return value in xgbe_set_link_ksettings()

In reply to Yury Norov

bitmap_empty() is O(N).

Use the return value of linkmode_and() when validating the requested advertisement instead of scanning the result bitmap separately.

---
 drivers/net/ethernet/amd/xgbe/xgbe-ethtool.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/net/ethernet/amd/xgbe/xgbe-ethtool.c +5 -4
index a9f4fcc4daae..113a10855938 100644
@@ -231,6 +231,7 @@ static int xgbe_set_link_ksettings(struct net_device *netdev,
 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
 	struct ethtool_link_ksettings *lks = &pdata->phy.lks;
 	__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
+	bool has_advertising;
 	u32 speed;
 	int ret;
 
@@ -267,11 +268,11 @@ static int xgbe_set_link_ksettings(struct net_device *netdev,
 		  __ETHTOOL_LINK_MODE_MASK_NBITS, cmd->link_modes.advertising,
 		  __ETHTOOL_LINK_MODE_MASK_NBITS, lks->link_modes.supported);
 
-	linkmode_and(advertising, cmd->link_modes.advertising,
-		     lks->link_modes.supported);
+	has_advertising = linkmode_and(advertising,
+				       cmd->link_modes.advertising,
+				       lks->link_modes.supported);
 
-	if ((cmd->base.autoneg == AUTONEG_ENABLE) &&
-	    bitmap_empty(advertising, __ETHTOOL_LINK_MODE_MASK_NBITS)) {
+	if (!has_advertising && cmd->base.autoneg == AUTONEG_ENABLE) {
 		netdev_err(netdev,
 			   "unsupported requested advertisement\n");
 		return -EINVAL;
Signature · 1 line
2.53.0
Reviewed-by on v1 3/9
#5 lore raw

RE: [PATCH 3/9] net: amd: xgbe: use linkmode_and() return value in xgbe_set_link_ksettings()

Quoted 67 lines

-----Original Message-----
From: Yury Norov <[email protected]>
Sent: Monday, September 7, 2026 11:55 PM
To: Andrew Lunn <[email protected]>; Heiner Kallweit <[email protected]>; Russell King <[email protected]>; Raju Rangoju <[email protected]>; Prashanth Kumar K R <[email protected]>; Nguyen, Anthony L <[email protected]>; Kitszel, Przemyslaw <[email protected]>; Jian Shen <[email protected]>; Jijie Shao <[email protected]>; David S. Miller <[email protected]>; Eric Dumazet <[email protected]>; Jakub Kicinski <[email protected]>; Paolo Abeni <[email protected]>; linux- [email protected]; [email protected]; intel-wired- [email protected]; [email protected]
Cc: Yury Norov <[email protected]>; Yury Norov <[email protected]>; Rasmus Villemoes <[email protected]>; Andrew Morton <[email protected]>
Subject: [PATCH 3/9] net: amd: xgbe: use linkmode_and() return value in xgbe_set_link_ksettings()

bitmap_empty() is O(N).

Use the return value of linkmode_and() when validating the requested advertisement instead of scanning the result bitmap separately.

---
 drivers/net/ethernet/amd/xgbe/xgbe-ethtool.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/net/ethernet/amd/xgbe/xgbe-ethtool.c +4 -4
index a9f4fcc4daae..113a10855938 100644
@@ -231,6 +231,7 @@ static int xgbe_set_link_ksettings(struct
 net_device *netdev,
 	struct xgbe_prv_data *pdata = netdev_priv(netdev);
 	struct ethtool_link_ksettings *lks = &pdata->phy.lks;
 	__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
+	bool has_advertising;
 	u32 speed;
 	int ret;
@@ -267,11 +268,11 @@ static int xgbe_set_link_ksettings(struct
 net_device *netdev,
 		  __ETHTOOL_LINK_MODE_MASK_NBITS, cmd-
 >link_modes.advertising,
 		  __ETHTOOL_LINK_MODE_MASK_NBITS, lks-
 >link_modes.supported);
 
-	linkmode_and(advertising, cmd->link_modes.advertising,
-		     lks->link_modes.supported);
+	has_advertising = linkmode_and(advertising,
+				       cmd->link_modes.advertising,
+				       lks->link_modes.supported);
 
-	if ((cmd->base.autoneg == AUTONEG_ENABLE) &&
-	    bitmap_empty(advertising, __ETHTOOL_LINK_MODE_MASK_NBITS))

{

drivers/net/ethernet/amd/xgbe/xgbe-ethtool.c +1 -1
+	if (!has_advertising && cmd->base.autoneg == AUTONEG_ENABLE) {
 		netdev_err(netdev,
 			   "unsupported requested advertisement\n");
 		return -EINVAL;
--
Quoted 1 line

2.53.0

author maintainer
#6 lore raw

PATCH 4/9 ixgbe: use linkmode_and() return value in ixgbe_get_eee_fw()

In reply to Yury Norov

Use the return value of linkmode_and() to determine whether EEE is active instead of scanning the common link mode bitmap separately.

---
 drivers/net/ethernet/intel/ixgbe/ixgbe_ethtool.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/net/ethernet/intel/ixgbe/ixgbe_ethtool.c +1 -2
index 36e43b5e88d1..a7b7f89f29e4 100644
@@ -3751,8 +3751,7 @@ ixgbe_get_eee_fw(struct ixgbe_adapter *adapter, struct ethtool_keee *edata)
 	edata->eee_enabled = !linkmode_empty(edata->advertised);
 	edata->tx_lpi_enabled = edata->eee_enabled;
 
-	linkmode_and(common, edata->advertised, edata->lp_advertised);
-	edata->eee_active = !linkmode_empty(common);
+	edata->eee_active = linkmode_and(common, edata->advertised, edata->lp_advertised);
 
 	return 0;
 }
Signature · 1 line
2.53.0
Reviewed-by on v1 4/9
#7 lore raw

RE: [PATCH 4/9] ixgbe: use linkmode_and() return value in ixgbe_get_eee_fw()

Quoted 47 lines

-----Original Message-----
From: Yury Norov <[email protected]>
Sent: Monday, September 7, 2026 11:55 PM
To: Andrew Lunn <[email protected]>; Heiner Kallweit <[email protected]>; Russell King <[email protected]>; Raju Rangoju <[email protected]>; Prashanth Kumar K R <[email protected]>; Nguyen, Anthony L <[email protected]>; Kitszel, Przemyslaw <[email protected]>; Jian Shen <[email protected]>; Jijie Shao <[email protected]>; David S. Miller <[email protected]>; Eric Dumazet <[email protected]>; Jakub Kicinski <[email protected]>; Paolo Abeni <[email protected]>; [email protected]; [email protected]; [email protected]; linux- [email protected]
Cc: Yury Norov <[email protected]>; Yury Norov <[email protected]>; Rasmus Villemoes <[email protected]>; Andrew Morton <akpm@linux- foundation.org>
Subject: [PATCH 4/9] ixgbe: use linkmode_and() return value in ixgbe_get_eee_fw()

Use the return value of linkmode_and() to determine whether EEE is active instead of scanning the common link mode bitmap separately.

---
 drivers/net/ethernet/intel/ixgbe/ixgbe_ethtool.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/net/ethernet/intel/ixgbe/ixgbe_ethtool.c +2 -3
index 36e43b5e88d1..a7b7f89f29e4 100644
@@ -3751,8 +3751,7 @@ ixgbe_get_eee_fw(struct ixgbe_adapter *adapter,
 struct ethtool_keee *edata)
 	edata->eee_enabled = !linkmode_empty(edata->advertised);
 	edata->tx_lpi_enabled = edata->eee_enabled;
 
-	linkmode_and(common, edata->advertised, edata->lp_advertised);
-	edata->eee_active = !linkmode_empty(common);
+	edata->eee_active = linkmode_and(common, edata->advertised,
+edata->lp_advertised);
 
 	return 0;
 }
--
Quoted 1 line

2.53.0

Reviewed-by on v1 4/9
#8 lore raw

RE: [PATCH 4/9] ixgbe: use linkmode_and() return value in ixgbe_get_eee_fw()

In reply to Yury Norov
Quoted 48 lines

-----Original Message-----
From: Yury Norov <[email protected]>
Sent: Monday, September 7, 2026 11:55 PM
To: Andrew Lunn <[email protected]>; Heiner Kallweit <[email protected]>; Russell King <[email protected]>; Raju Rangoju <[email protected]>; Prashanth Kumar K R <[email protected]>; Nguyen, Anthony L <[email protected]>; Kitszel, Przemyslaw <[email protected]>; Jian Shen <[email protected]>; Jijie Shao <[email protected]>; David S. Miller <[email protected]>; Eric Dumazet <[email protected]>; Jakub Kicinski <[email protected]>; Paolo Abeni <[email protected]>; linux- [email protected]; [email protected]; intel-wired- [email protected]; [email protected]
Cc: Yury Norov <[email protected]>; Yury Norov <[email protected]>; Rasmus Villemoes <[email protected]>; Andrew Morton <[email protected]>
Subject: [PATCH 4/9] ixgbe: use linkmode_and() return value in ixgbe_get_eee_fw()

Use the return value of linkmode_and() to determine whether EEE is active instead of scanning the common link mode bitmap separately.

---
 drivers/net/ethernet/intel/ixgbe/ixgbe_ethtool.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/net/ethernet/intel/ixgbe/ixgbe_ethtool.c +2 -3
index 36e43b5e88d1..a7b7f89f29e4 100644
@@ -3751,8 +3751,7 @@ ixgbe_get_eee_fw(struct ixgbe_adapter *adapter,
 struct ethtool_keee *edata)
 	edata->eee_enabled = !linkmode_empty(edata->advertised);
 	edata->tx_lpi_enabled = edata->eee_enabled;
 
-	linkmode_and(common, edata->advertised, edata->lp_advertised);
-	edata->eee_active = !linkmode_empty(common);
+	edata->eee_active = linkmode_and(common, edata->advertised,
+edata->lp_advertised);
 
 	return 0;
 }
--
Quoted 1 line

2.53.0

author maintainer
#9 lore raw

PATCH 5/9 net: phy: use linkmode_and() return value in genphy_c45_eee_is_active()

In reply to Yury Norov

Use the return value of linkmode_and() to detect an empty set of common EEE modes and get rid of a separate linkmode_empty() call.

---
 drivers/net/phy/phy-c45.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/net/phy/phy-c45.c +1 -2
index 870920311f9a..27da1b9ddbc5 100644
@@ -1623,8 +1623,7 @@ int genphy_c45_eee_is_active(struct phy_device *phydev, unsigned long *lp)
 	if (lp)
 		linkmode_copy(lp, tmp_lp);
 
-	linkmode_and(common, phydev->advertising_eee, tmp_lp);
-	if (linkmode_empty(common))
+	if (!linkmode_and(common, phydev->advertising_eee, tmp_lp))
 		return 0;
 
 	return phy_check_valid(phydev->speed, phydev->duplex, common);
Signature · 1 line
2.53.0
Reviewed-by on v1 5/9
#10 lore raw

RE: [PATCH 5/9] net: phy: use linkmode_and() return value in genphy_c45_eee_is_active()

Quoted 44 lines

-----Original Message-----
From: Yury Norov <[email protected]>
Sent: Monday, September 7, 2026 11:55 PM
To: Andrew Lunn <[email protected]>; Heiner Kallweit <[email protected]>; Russell King <[email protected]>; Raju Rangoju <[email protected]>; Prashanth Kumar K R <[email protected]>; Nguyen, Anthony L <[email protected]>; Kitszel, Przemyslaw <[email protected]>; Jian Shen <[email protected]>; Jijie Shao <[email protected]>; David S. Miller <[email protected]>; Eric Dumazet <[email protected]>; Jakub Kicinski <[email protected]>; Paolo Abeni <[email protected]>; linux- [email protected]; [email protected]; intel-wired- [email protected]; [email protected]
Cc: Yury Norov <[email protected]>; Yury Norov <[email protected]>; Rasmus Villemoes <[email protected]>; Andrew Morton <[email protected]>
Subject: [PATCH 5/9] net: phy: use linkmode_and() return value in genphy_c45_eee_is_active()

Use the return value of linkmode_and() to detect an empty set of common EEE modes and get rid of a separate linkmode_empty() call.

---
 drivers/net/phy/phy-c45.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/net/phy/phy-c45.c +1 -2
index 870920311f9a..27da1b9ddbc5 100644
@@ -1623,8 +1623,7 @@ int genphy_c45_eee_is_active(struct phy_device
 *phydev, unsigned long *lp)
 	if (lp)
 		linkmode_copy(lp, tmp_lp);
 
-	linkmode_and(common, phydev->advertising_eee, tmp_lp);
-	if (linkmode_empty(common))
+	if (!linkmode_and(common, phydev->advertising_eee, tmp_lp))
 		return 0;
 
Quoted 3 lines

return phy_check_valid(phydev->speed, phydev->duplex, common);
--
2.53.0

author maintainer
#11 lore raw

PATCH 6/9 net: avoid copies before linkmode_and()

In reply to Yury Norov

Avoid separate linkmode_copy() calls by passing the original source bitmap directly to linkmode_and().

In phy_ethtool_ksettings_set(), also use the return value of linkmode_and() when validating that the requested advertisement contains a supported link mode.

---
 drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_mdio.c | 5 ++---
 drivers/net/phy/phy.c                                   | 7 +++----
 2 files changed, 5 insertions(+), 7 deletions(-)
drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_mdio.c +2 -3
index cf881108fa57..5801da6100b8 100644
@@ -205,7 +205,6 @@ int hclge_mac_connect_phy(struct hnae3_handle *handle)
 	struct hclge_dev *hdev = vport->back;
 	struct net_device *netdev = hdev->vport[0].nic.netdev;
 	struct phy_device *phydev = hdev->hw.mac.phydev;
-	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
 	int ret;
 
 	if (!phydev)
@@ -223,8 +222,8 @@ int hclge_mac_connect_phy(struct hnae3_handle *handle)
 		return ret;
 	}
 
-	linkmode_copy(mask, hdev->hw.mac.supported);
-	linkmode_and(phydev->supported, phydev->supported, mask);
+	linkmode_and(phydev->supported, phydev->supported,
+		     hdev->hw.mac.supported);
 	linkmode_copy(phydev->advertising, phydev->supported);
 
 	/* supported flag is Pause and Asym Pause, but default advertising
drivers/net/phy/phy.c +3 -4
index fce9bc7be330..cd71186cd842 100644
@@ -1160,6 +1160,7 @@ int phy_ethtool_ksettings_set(struct phy_device *phydev,
 			      const struct ethtool_link_ksettings *cmd)
 {
 	__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
+	bool has_advertising;
 	u8 autoneg = cmd->base.autoneg;
 	u8 duplex = cmd->base.duplex;
 	u32 speed = cmd->base.speed;
@@ -1167,17 +1168,15 @@ int phy_ethtool_ksettings_set(struct phy_device *phydev,
 	if (cmd->base.phy_address != phydev->mdio.addr)
 		return -EINVAL;
 
-	linkmode_copy(advertising, cmd->link_modes.advertising);
-
 	/* We make sure that we don't pass unsupported values in to the PHY */
-	linkmode_and(advertising, advertising, phydev->supported);
+	has_advertising = linkmode_and(advertising, cmd->link_modes.advertising, phydev->supported);
 
 	/* Verify the settings we care about. */
 	if (autoneg != AUTONEG_ENABLE && autoneg != AUTONEG_DISABLE)
 		return -EINVAL;
 
 	if (autoneg == AUTONEG_ENABLE &&
-	    (linkmode_empty(advertising) ||
+	    (!has_advertising ||
 	     !linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
 				phydev->supported)))
 		return -EINVAL;
Signature · 1 line
2.53.0
Reviewed-by on v1 6/9
#12 lore raw

RE: [PATCH 6/9] net: avoid copies before linkmode_and()

Quoted 97 lines

-----Original Message-----
From: Yury Norov <[email protected]>
Sent: Monday, September 7, 2026 11:55 PM
To: Andrew Lunn <[email protected]>; Heiner Kallweit <[email protected]>; Russell King <[email protected]>; Raju Rangoju <[email protected]>; Prashanth Kumar K R <[email protected]>; Nguyen, Anthony L <[email protected]>; Kitszel, Przemyslaw <[email protected]>; Jian Shen <[email protected]>; Jijie Shao <[email protected]>; David S. Miller <[email protected]>; Eric Dumazet <[email protected]>; Jakub Kicinski <[email protected]>; Paolo Abeni <[email protected]>; linux- [email protected]; [email protected]; intel-wired- [email protected]; [email protected]
Cc: Yury Norov <[email protected]>; Yury Norov <[email protected]>; Rasmus Villemoes <[email protected]>; Andrew Morton <[email protected]>
Subject: [PATCH 6/9] net: avoid copies before linkmode_and()

Avoid separate linkmode_copy() calls by passing the original source bitmap directly to linkmode_and().

In phy_ethtool_ksettings_set(), also use the return value of linkmode_and() when validating that the requested advertisement contains a supported link mode.

---
 drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_mdio.c | 5 ++---
 drivers/net/phy/phy.c                                   | 7 +++----
 2 files changed, 5 insertions(+), 7 deletions(-)
drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_mdio.c +0 -1
index cf881108fa57..5801da6100b8 100644
@@ -205,7 +205,6 @@ int hclge_mac_connect_phy(struct hnae3_handle
 *handle)
 	struct hclge_dev *hdev = vport->back;
 	struct net_device *netdev = hdev->vport[0].nic.netdev;
 	struct phy_device *phydev = hdev->hw.mac.phydev;
-	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
 	int ret;
 
drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_mdio.c +2 -2
 	if (!phydev)
@@ -223,8 +222,8 @@ int hclge_mac_connect_phy(struct hnae3_handle
 *handle)
 		return ret;
 	}
 
-	linkmode_copy(mask, hdev->hw.mac.supported);
-	linkmode_and(phydev->supported, phydev->supported, mask);
+	linkmode_and(phydev->supported, phydev->supported,
+		     hdev->hw.mac.supported);
 	linkmode_copy(phydev->advertising, phydev->supported);
 

/* supported flag is Pause and Asym Pause, but default
advertising diff --git a/drivers/net/phy/phy.c b/drivers/net/phy/phy.c index fce9bc7be330..cd71186cd842 100644

drivers/net/phy/phy.c +1 -0
@@ -1160,6 +1160,7 @@ int phy_ethtool_ksettings_set(struct phy_device
 *phydev,
 			      const struct ethtool_link_ksettings *cmd)
 {
 	__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
+	bool has_advertising;
 	u8 autoneg = cmd->base.autoneg;
 	u8 duplex = cmd->base.duplex;
drivers/net/phy/phy.c +0 -2
 	u32 speed = cmd->base.speed;
@@ -1167,17 +1168,15 @@ int phy_ethtool_ksettings_set(struct
 phy_device *phydev,
 	if (cmd->base.phy_address != phydev->mdio.addr)
 		return -EINVAL;
 
-	linkmode_copy(advertising, cmd->link_modes.advertising);
-
 	/* We make sure that we don't pass unsupported values in to the

PHY */

drivers/net/phy/phy.c +3 -3
-	linkmode_and(advertising, advertising, phydev->supported);
+	has_advertising = linkmode_and(advertising,
+cmd->link_modes.advertising, phydev->supported);
 
 	/* Verify the settings we care about. */
 	if (autoneg != AUTONEG_ENABLE && autoneg != AUTONEG_DISABLE)
 		return -EINVAL;
 
 	if (autoneg == AUTONEG_ENABLE &&
-	    (linkmode_empty(advertising) ||
+	    (!has_advertising ||
 	     !linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
 				phydev->supported)))
 		return -EINVAL;
--
Quoted 1 line

2.53.0

maintainer
#13 lore raw

Re: [PATCH 6/9] net: avoid copies before linkmode_and()

In reply to Yury Norov
Quoted 6 lines · Yury Norov

Avoid separate linkmode_copy() calls by passing the original source bitmap directly to linkmode_and().

In phy_ethtool_ksettings_set(), also use the return value of linkmode_and() when validating that the requested advertisement contains a supported link mode.

Please include "hns3" in the subject prefix (e.g., "net: hns3: ...") so it's clear which driver this patch belongs to. I didn't immediately find the hns3 driver modifications from the current title.

Quoted 43 lines
---
  drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_mdio.c | 5 ++---
  drivers/net/phy/phy.c                                   | 7 +++----
  2 files changed, 5 insertions(+), 7 deletions(-)
drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_mdio.c +2 -3
index cf881108fa57..5801da6100b8 100644
@@ -205,7 +205,6 @@ int hclge_mac_connect_phy(struct hnae3_handle *handle)
  	struct hclge_dev *hdev = vport->back;
  	struct net_device *netdev = hdev->vport[0].nic.netdev;
  	struct phy_device *phydev = hdev->hw.mac.phydev;
-	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
  	int ret;
 
  	if (!phydev)
@@ -223,8 +222,8 @@ int hclge_mac_connect_phy(struct hnae3_handle *handle)
  		return ret;
  	}
 
-	linkmode_copy(mask, hdev->hw.mac.supported);
-	linkmode_and(phydev->supported, phydev->supported, mask);
+	linkmode_and(phydev->supported, phydev->supported,
+		     hdev->hw.mac.supported);
  	linkmode_copy(phydev->advertising, phydev->supported);
 
  	/* supported flag is Pause and Asym Pause, but default advertising
drivers/net/phy/phy.c +1 -0
index fce9bc7be330..cd71186cd842 100644
@@ -1160,6 +1160,7 @@ int phy_ethtool_ksettings_set(struct phy_device *phydev,
  			      const struct ethtool_link_ksettings *cmd)
  {
  	__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
+	bool has_advertising;
  	u8 autoneg = cmd->base.autoneg;
  	u8 duplex = cmd->base.duplex;
  	u32 speed = cmd->base.speed;

drivers/net/phy/phy.c int phy_ethtool_ksettings_set(struct phy_device *phydev, · line 1172

Quoted 8 lines
drivers/net/phy/phy.c +1 -3
@@ -1167,17 +1168,15 @@ int phy_ethtool_ksettings_set(struct phy_device *phydev,
  	if (cmd->base.phy_address != phydev->mdio.addr)
  		return -EINVAL;
 
-	linkmode_copy(advertising, cmd->link_modes.advertising);
-
  	/* We make sure that we don't pass unsupported values in to the PHY */
-	linkmode_and(advertising, advertising, phydev->supported);
+	has_advertising = linkmode_and(advertising, cmd->link_modes.advertising, phydev->supported);

To keep the file clean, could you please update this part to match thesurrounding code style? Specifically, we order local variables fromlongest to shortest (reverse Christmas tree) and enforce an 80-characterline limit.

Thanks,
Jijie Shao

Quoted 11 lines
drivers/net/phy/phy.c +1 -1
  	/* Verify the settings we care about. */
  	if (autoneg != AUTONEG_ENABLE && autoneg != AUTONEG_DISABLE)
  		return -EINVAL;
 
  	if (autoneg == AUTONEG_ENABLE &&
-	    (linkmode_empty(advertising) ||
+	    (!has_advertising ||
  	     !linkmode_test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
  				phydev->supported)))
  		return -EINVAL;
author maintainer
#14 lore raw

PATCH 7/9 net: phy: use linkmode operation return values in phy_device.c

In reply to Yury Norov

Use the return value of linkmode_and() where the resulting mask was previously scanned for emptiness. In phy_probe(), combine the AND and AND-NOT operations and use the return value of linkmode_and_andnot().

---
 drivers/net/phy/phy_device.c | 14 ++++++--------
 include/linux/linkmode.h     |  9 +++++++++
 2 files changed, 15 insertions(+), 8 deletions(-)
drivers/net/phy/phy_device.c +6 -8
index 94b2e85e00a3..3430605695c5 100644
@@ -1565,8 +1565,7 @@ static int phy_sfp_module_insert(void *upstream, const struct sfp_eeprom_id *id)
 
 	caps = sfp_get_module_caps(phydev->sfp_bus);
 
-	linkmode_and(sfp_support, port->supported, caps->link_modes);
-	if (linkmode_empty(sfp_support)) {
+	if (!linkmode_and(sfp_support, port->supported, caps->link_modes)) {
 		dev_err(&phydev->mdio.dev, "incompatible SFP module inserted, no common linkmode\n");
 		return -EINVAL;
 	}
@@ -3761,15 +3760,14 @@ static int phy_probe(struct device *dev)
 	/* Some PHYs may advertise, by default, not support EEE modes. So,
 	 * we need to clean them. In addition remove all disabled EEE modes.
 	 */
-	linkmode_and(phydev->advertising_eee, phydev->supported_eee,
-		     phydev->advertising_eee);
-	linkmode_andnot(phydev->advertising_eee, phydev->advertising_eee,
-			phydev->eee_disabled_modes);
-
 	/* There is no "enabled" flag. If PHY is advertising, assume it is
 	 * kind of enabled.
 	 */
-	phydev->eee_cfg.eee_enabled = !linkmode_empty(phydev->advertising_eee);
+	phydev->eee_cfg.eee_enabled =
+		linkmode_and_andnot(phydev->advertising_eee,
+				    phydev->advertising_eee,
+				    phydev->supported_eee,
+				    phydev->eee_disabled_modes);
 
 	/* Get master/slave strap overrides */
 	of_set_phy_timing_role(phydev);
include/linux/linkmode.h +9 -0
index c08632c10c3d..49587a2e0f69 100644
@@ -26,6 +26,15 @@ static inline bool linkmode_and(unsigned long *dst, const unsigned long *a,
 	return bitmap_and(dst, a, b, __ETHTOOL_LINK_MODE_MASK_NBITS);
 }
 
+static inline bool linkmode_and_andnot(unsigned long *dst,
+				       const unsigned long *a,
+				       const unsigned long *b,
+				       const unsigned long *c)
+{
+	return bitmap_and_andnot(dst, a, b, c,
+				 __ETHTOOL_LINK_MODE_MASK_NBITS);
+}
+
 static inline void linkmode_or(unsigned long *dst, const unsigned long *a,
 				const unsigned long *b)
 {
Signature · 1 line
2.53.0
Reviewed-by on v1 7/9
#15 lore raw

RE: [PATCH 7/9] net: phy: use linkmode operation return values in phy_device.c

Quoted 95 lines

-----Original Message-----
From: Yury Norov <[email protected]>
Sent: Monday, September 7, 2026 11:55 PM
To: Andrew Lunn <[email protected]>; Heiner Kallweit <[email protected]>; Russell King <[email protected]>; Raju Rangoju <[email protected]>; Prashanth Kumar K R <[email protected]>; Nguyen, Anthony L <[email protected]>; Kitszel, Przemyslaw <[email protected]>; Jian Shen <[email protected]>; Jijie Shao <[email protected]>; David S. Miller <[email protected]>; Eric Dumazet <[email protected]>; Jakub Kicinski <[email protected]>; Paolo Abeni <[email protected]>; linux- [email protected]; [email protected]; intel-wired- [email protected]; [email protected]
Cc: Yury Norov <[email protected]>; Yury Norov <[email protected]>; Rasmus Villemoes <[email protected]>; Andrew Morton <[email protected]>
Subject: [PATCH 7/9] net: phy: use linkmode operation return values in phy_device.c

Use the return value of linkmode_and() where the resulting mask was previously scanned for emptiness. In phy_probe(), combine the AND and AND-NOT operations and use the return value of linkmode_and_andnot().

---
 drivers/net/phy/phy_device.c | 14 ++++++--------
 include/linux/linkmode.h     |  9 +++++++++
 2 files changed, 15 insertions(+), 8 deletions(-)
(quoted diff) +0 -0

b/drivers/net/phy/phy_device.c index 94b2e85e00a3..3430605695c5 100644

drivers/net/phy/phy_device.c +1 -2
@@ -1565,8 +1565,7 @@ static int phy_sfp_module_insert(void *upstream,
 const struct sfp_eeprom_id *id)
 
 	caps = sfp_get_module_caps(phydev->sfp_bus);
 
-	linkmode_and(sfp_support, port->supported, caps->link_modes);
-	if (linkmode_empty(sfp_support)) {
+	if (!linkmode_and(sfp_support, port->supported, caps-
 >link_modes)) {
 		dev_err(&phydev->mdio.dev, "incompatible SFP module

inserted, no common linkmode\n");

drivers/net/phy/phy_device.c +0 -0
 		return -EINVAL;
 	}
@@ -3761,15 +3760,14 @@ static int phy_probe(struct device *dev)
 	/* Some PHYs may advertise, by default, not support EEE modes.
 So,
 	 * we need to clean them. In addition remove all disabled EEE

modes.

drivers/net/phy/phy_device.c +0 -3
 	 */
-	linkmode_and(phydev->advertising_eee, phydev->supported_eee,
-		     phydev->advertising_eee);
-	linkmode_andnot(phydev->advertising_eee, phydev-
Quoted 1 line

advertising_eee,

drivers/net/phy/phy_device.c +0 -2
-			phydev->eee_disabled_modes);
-
 	/* There is no "enabled" flag. If PHY is advertising, assume it

is

drivers/net/phy/phy_device.c +0 -1
 	 * kind of enabled.
 	 */
-	phydev->eee_cfg.eee_enabled = !linkmode_empty(phydev-
Quoted 1 line

advertising_eee);

drivers/net/phy/phy_device.c +5 -0
+	phydev->eee_cfg.eee_enabled =
+		linkmode_and_andnot(phydev->advertising_eee,
+				    phydev->advertising_eee,
+				    phydev->supported_eee,
+				    phydev->eee_disabled_modes);
 
 	/* Get master/slave strap overrides */
 	of_set_phy_timing_role(phydev);
include/linux/linkmode.h index renamed from include/linux/linkmode.h +0 -0

c08632c10c3d..49587a2e0f69 100644

include/linux/linkmode.h +9 -0
@@ -26,6 +26,15 @@ static inline bool linkmode_and(unsigned long *dst,
 const unsigned long *a,
 	return bitmap_and(dst, a, b, __ETHTOOL_LINK_MODE_MASK_NBITS);
 }
 
+static inline bool linkmode_and_andnot(unsigned long *dst,
+				       const unsigned long *a,
+				       const unsigned long *b,
+				       const unsigned long *c)
+{
+	return bitmap_and_andnot(dst, a, b, c,
+				 __ETHTOOL_LINK_MODE_MASK_NBITS);
+}
+
 static inline void linkmode_or(unsigned long *dst, const unsigned
 long *a,
Quoted 4 lines

const unsigned long *b)
{
--
2.53.0

maintainer
#16 lore raw

Re: [PATCH 7/9] net: phy: use linkmode operation return values in phy_device.c

In reply to Yury Norov

drivers/net/phy/phy_device.c static int phy_sfp_module_insert(void *upstream, const struct sfp_eeprom_id *id) · line 1569

Quoted 4 lines
drivers/net/phy/phy_device.c +1 -2
-	linkmode_and(sfp_support, port->supported, caps->link_modes);
-	if (linkmode_empty(sfp_support)) {
+	if (!linkmode_and(sfp_support, port->supported, caps->link_modes)) {
 		dev_err(&phydev->mdio.dev, "incompatible SFP module inserted, no common linkmode\n");

From a readability perspective, i like linkmode_empty(). It is more obvious than !linkmode_and().

None of this code is in the hot path. So we should put readability above performance.

Quoted 7 lines
drivers/net/phy/phy_device.c +0 -4
 	/* Some PHYs may advertise, by default, not support EEE modes. So,
 	 * we need to clean them. In addition remove all disabled EEE modes.
 	 */
-	linkmode_and(phydev->advertising_eee, phydev->supported_eee,
-		     phydev->advertising_eee);
-	linkmode_andnot(phydev->advertising_eee, phydev->advertising_eee,
-			phydev->eee_disabled_modes);

drivers/net/phy/phy_device.c static int phy_probe(struct device *dev) · line 3770

Quoted 10 lines
drivers/net/phy/phy_device.c +5 -2
-
 	/* There is no "enabled" flag. If PHY is advertising, assume it is
 	 * kind of enabled.
 	 */
-	phydev->eee_cfg.eee_enabled = !linkmode_empty(phydev->advertising_eee);
+	phydev->eee_cfg.eee_enabled =
+		linkmode_and_andnot(phydev->advertising_eee,
+				    phydev->advertising_eee,
+				    phydev->supported_eee,
+				    phydev->eee_disabled_modes);

So, which is more readable, the original or this?

Andrew

author maintainer
#17 lore raw

Re: [PATCH 7/9] net: phy: use linkmode operation return values in phy_device.c

Quoted 7 lines · Andrew Lunn

drivers/net/phy/phy_device.c static int phy_sfp_module_insert(void *upstream, const struct sfp_eeprom_id *id) · line 1569

Quoted 4 lines
drivers/net/phy/phy_device.c +1 -2
-	linkmode_and(sfp_support, port->supported, caps->link_modes);
-	if (linkmode_empty(sfp_support)) {
+	if (!linkmode_and(sfp_support, port->supported, caps->link_modes)) {
 		dev_err(&phydev->mdio.dev, "incompatible SFP module inserted, no common linkmode\n");

From a readability perspective, i like linkmode_empty(). It is more obvious than !linkmode_and().

OK, a function returning 2 values is not something obvious in C. What about this?

	sfp_supported = linkmode_and(sfp_support, port->supported, caps->link_modes);
        if (!sfp_supported)
		dev_err(&phydev->mdio.dev, "incompatible SFP module inserted, no common linkmode\n");
Quoted 14 lines

None of this code is in the hot path. So we should put readability above performance.

Quoted 11 lines
drivers/net/phy/phy_device.c +0 -5
 	/* Some PHYs may advertise, by default, not support EEE modes. So,
 	 * we need to clean them. In addition remove all disabled EEE modes.
 	 */
-	linkmode_and(phydev->advertising_eee, phydev->supported_eee,
-		     phydev->advertising_eee);
-	linkmode_andnot(phydev->advertising_eee, phydev->advertising_eee,
-			phydev->eee_disabled_modes);
-
 	/* There is no "enabled" flag. If PHY is advertising, assume it is
 	 * kind of enabled.
 	 */
Quoted 8 lines

drivers/net/phy/phy_device.c static int phy_probe(struct device *dev) · line 3770

Quoted 6 lines
drivers/net/phy/phy_device.c +5 -1
-	phydev->eee_cfg.eee_enabled = !linkmode_empty(phydev->advertising_eee);
+	phydev->eee_cfg.eee_enabled =
+		linkmode_and_andnot(phydev->advertising_eee,
+				    phydev->advertising_eee,
+				    phydev->supported_eee,
+				    phydev->eee_disabled_modes);

So, which is more readable, the original or this?

The new version is more readable to me. What about this:

        eee_advertised = linkmode_and_andnot(phydev->advertising_eee,
	                                     phydev->advertising_eee,
                                             phydev->supported_eee,
		                             phydev->eee_disabled_modes);

/* There is no "enabled" flag. If PHY is advertising, assume it is
* kind of enabled.
*/

	phydev->eee_cfg.eee_enabled = eee_advertised;

Thanks,
Yury

maintainer
#18 lore raw

Re: [PATCH 7/9] net: phy: use linkmode operation return values in phy_device.c

Quoted 9 lines · Yury Norov
Quoted 7 lines · Andrew Lunn

drivers/net/phy/phy_device.c static int phy_sfp_module_insert(void *upstream, const struct sfp_eeprom_id *id) · line 1569

Quoted 4 lines
drivers/net/phy/phy_device.c +1 -2
-	linkmode_and(sfp_support, port->supported, caps->link_modes);
-	if (linkmode_empty(sfp_support)) {
+	if (!linkmode_and(sfp_support, port->supported, caps->link_modes)) {
 		dev_err(&phydev->mdio.dev, "incompatible SFP module inserted, no common linkmode\n");

From a readability perspective, i like linkmode_empty(). It is more obvious than !linkmode_and().

Quoted 7 lines

OK, a function returning 2 values is not something obvious in C. What about this?

	sfp_supported = linkmode_and(sfp_support, port->supported, caps->link_modes);
        if (!sfp_supported)
		dev_err(&phydev->mdio.dev, "incompatible SFP module inserted, no common linkmode\n");

Why not keep it as it is? If the link mode is empty, we know we have a problem.

Quoted 24 lines
Quoted 15 lines

None of this code is in the hot path. So we should put readability above performance.

Quoted 11 lines
drivers/net/phy/phy_device.c +0 -5
 	/* Some PHYs may advertise, by default, not support EEE modes. So,
 	 * we need to clean them. In addition remove all disabled EEE modes.
 	 */
-	linkmode_and(phydev->advertising_eee, phydev->supported_eee,
-		     phydev->advertising_eee);
-	linkmode_andnot(phydev->advertising_eee, phydev->advertising_eee,
-			phydev->eee_disabled_modes);
-
 	/* There is no "enabled" flag. If PHY is advertising, assume it is
 	 * kind of enabled.
 	 */
Quoted 8 lines

drivers/net/phy/phy_device.c static int phy_probe(struct device *dev) · line 3770

Quoted 6 lines
drivers/net/phy/phy_device.c +5 -1
-	phydev->eee_cfg.eee_enabled = !linkmode_empty(phydev->advertising_eee);
+	phydev->eee_cfg.eee_enabled =
+		linkmode_and_andnot(phydev->advertising_eee,
+				    phydev->advertising_eee,
+				    phydev->supported_eee,
+				    phydev->eee_disabled_modes);

So, which is more readable, the original or this?

Quoted 6 lines

The new version is more readable to me. What about this:

        eee_advertised = linkmode_and_andnot(phydev->advertising_eee,
	                                     phydev->advertising_eee,
                                             phydev->supported_eee,
		                             phydev->eee_disabled_modes);

I still prefer the original version.

Where do you stop with these functions? Why not add linkmode_and_andnot_or()? linkmode_and_nand_xor_nor()?

If complex expressions were used on the fast path, every packet needing some bitmap logic, i can see the benefit of such complex functions, if they can be optimised. But this is slow path, probe time, or when the link goes up. Human readability comes first.

Just out of interest, did you look at the disassembly for both versions? Isn't it the inline linkmode_ wrapper function which drops the return value, which you are adding back. But the compiler/optimizer sees it and could make use of it? Is gcc/clang clever enough to do that?

Andrew

Reviewed-by on v1 7/9
#19 lore raw

RE: [PATCH 7/9] net: phy: use linkmode operation return values in phy_device.c

In reply to Yury Norov
Quoted 95 lines

-----Original Message-----
From: Yury Norov <[email protected]>
Sent: Monday, September 7, 2026 11:55 PM
To: Andrew Lunn <[email protected]>; Heiner Kallweit <[email protected]>; Russell King <[email protected]>; Raju Rangoju <[email protected]>; Prashanth Kumar K R <[email protected]>; Nguyen, Anthony L <[email protected]>; Kitszel, Przemyslaw <[email protected]>; Jian Shen <[email protected]>; Jijie Shao <[email protected]>; David S. Miller <[email protected]>; Eric Dumazet <[email protected]>; Jakub Kicinski <[email protected]>; Paolo Abeni <[email protected]>; linux- [email protected]; [email protected]; intel-wired- [email protected]; [email protected]
Cc: Yury Norov <[email protected]>; Yury Norov <[email protected]>; Rasmus Villemoes <[email protected]>; Andrew Morton <[email protected]>
Subject: [PATCH 7/9] net: phy: use linkmode operation return values in phy_device.c

Use the return value of linkmode_and() where the resulting mask was previously scanned for emptiness. In phy_probe(), combine the AND and AND-NOT operations and use the return value of linkmode_and_andnot().

---
 drivers/net/phy/phy_device.c | 14 ++++++--------
 include/linux/linkmode.h     |  9 +++++++++
 2 files changed, 15 insertions(+), 8 deletions(-)
(quoted diff) +0 -0

b/drivers/net/phy/phy_device.c index 94b2e85e00a3..3430605695c5 100644

drivers/net/phy/phy_device.c +1 -2
@@ -1565,8 +1565,7 @@ static int phy_sfp_module_insert(void *upstream,
 const struct sfp_eeprom_id *id)
 
 	caps = sfp_get_module_caps(phydev->sfp_bus);
 
-	linkmode_and(sfp_support, port->supported, caps->link_modes);
-	if (linkmode_empty(sfp_support)) {
+	if (!linkmode_and(sfp_support, port->supported, caps-
 >link_modes)) {
 		dev_err(&phydev->mdio.dev, "incompatible SFP module

inserted, no common linkmode\n");

drivers/net/phy/phy_device.c +0 -0
 		return -EINVAL;
 	}
@@ -3761,15 +3760,14 @@ static int phy_probe(struct device *dev)
 	/* Some PHYs may advertise, by default, not support EEE modes.
 So,
 	 * we need to clean them. In addition remove all disabled EEE

modes.

drivers/net/phy/phy_device.c +0 -3
 	 */
-	linkmode_and(phydev->advertising_eee, phydev->supported_eee,
-		     phydev->advertising_eee);
-	linkmode_andnot(phydev->advertising_eee, phydev-
Quoted 1 line

advertising_eee,

drivers/net/phy/phy_device.c +0 -2
-			phydev->eee_disabled_modes);
-
 	/* There is no "enabled" flag. If PHY is advertising, assume it

is

drivers/net/phy/phy_device.c +0 -1
 	 * kind of enabled.
 	 */
-	phydev->eee_cfg.eee_enabled = !linkmode_empty(phydev-
Quoted 1 line

advertising_eee);

drivers/net/phy/phy_device.c +5 -0
+	phydev->eee_cfg.eee_enabled =
+		linkmode_and_andnot(phydev->advertising_eee,
+				    phydev->advertising_eee,
+				    phydev->supported_eee,
+				    phydev->eee_disabled_modes);
 
 	/* Get master/slave strap overrides */
 	of_set_phy_timing_role(phydev);
include/linux/linkmode.h index renamed from include/linux/linkmode.h +0 -0

c08632c10c3d..49587a2e0f69 100644

include/linux/linkmode.h +9 -0
@@ -26,6 +26,15 @@ static inline bool linkmode_and(unsigned long *dst,
 const unsigned long *a,
 	return bitmap_and(dst, a, b, __ETHTOOL_LINK_MODE_MASK_NBITS);
 }
 
+static inline bool linkmode_and_andnot(unsigned long *dst,
+				       const unsigned long *a,
+				       const unsigned long *b,
+				       const unsigned long *c)
+{
+	return bitmap_and_andnot(dst, a, b, c,
+				 __ETHTOOL_LINK_MODE_MASK_NBITS);
+}
+
 static inline void linkmode_or(unsigned long *dst, const unsigned
 long *a,
Quoted 4 lines

const unsigned long *b)
{
--
2.53.0

author maintainer
#20 lore raw

PATCH 8/9 r8152: use linkmode_and_and() in EEE checks

In reply to Yury Norov

Combine the three link mode masks in a single operation and use its return value to determine whether EEE is active.

---
 drivers/net/usb/r8152.c  | 10 ++++------
 include/linux/linkmode.h |  8 ++++++++
 2 files changed, 12 insertions(+), 6 deletions(-)
drivers/net/usb/r8152.c +4 -6
index f61686433031..75f0574fe1d4 100644
@@ -9149,9 +9149,8 @@ static int r8152_get_eee(struct r8152 *tp, struct ethtool_keee *eee)
 	if (speed & _100bps)
 		linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, common);
 
-	linkmode_and(common, common, eee->advertised);
-	linkmode_and(common, common, eee->lp_advertised);
-	eee->eee_active = !linkmode_empty(common);
+	eee->eee_active = linkmode_and_and(common, common, eee->advertised,
+					   eee->lp_advertised);
 
 	return 0;
 }
@@ -9222,9 +9221,8 @@ static int r8153_get_eee(struct r8152 *tp, struct ethtool_keee *eee)
 	if (speed & _100bps)
 		linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT, common);
 
-	linkmode_and(common, common, eee->advertised);
-	linkmode_and(common, common, eee->lp_advertised);
-	eee->eee_active = !linkmode_empty(common);
+	eee->eee_active = linkmode_and_and(common, common, eee->advertised,
+					   eee->lp_advertised);
 
 	return 0;
 }
include/linux/linkmode.h +8 -0
index 49587a2e0f69..efd392dc62ae 100644
@@ -26,6 +26,14 @@ static inline bool linkmode_and(unsigned long *dst, const unsigned long *a,
 	return bitmap_and(dst, a, b, __ETHTOOL_LINK_MODE_MASK_NBITS);
 }
 
+static inline bool linkmode_and_and(unsigned long *dst,
+				    const unsigned long *a,
+				    const unsigned long *b,
+				    const unsigned long *c)
+{
+	return bitmap_and_and(dst, a, b, c, __ETHTOOL_LINK_MODE_MASK_NBITS);
+}
+
 static inline bool linkmode_and_andnot(unsigned long *dst,
 				       const unsigned long *a,
 				       const unsigned long *b,
Signature · 1 line
2.53.0
Reviewed-by on v1 8/9
#21 lore raw

RE: [PATCH 8/9] r8152: use linkmode_and_and() in EEE checks

Quoted 85 lines

-----Original Message-----
From: Yury Norov <[email protected]>
Sent: Monday, September 7, 2026 11:55 PM
To: Andrew Lunn <[email protected]>; Heiner Kallweit <[email protected]>; Russell King <[email protected]>; Raju Rangoju <[email protected]>; Prashanth Kumar K R <[email protected]>; Nguyen, Anthony L <[email protected]>; Kitszel, Przemyslaw <[email protected]>; Jian Shen <[email protected]>; Jijie Shao <[email protected]>; David S. Miller <[email protected]>; Eric Dumazet <[email protected]>; Jakub Kicinski <[email protected]>; Paolo Abeni <[email protected]>; linux- [email protected]; [email protected]; intel-wired- [email protected]; [email protected]
Cc: Yury Norov <[email protected]>; Yury Norov <[email protected]>; Rasmus Villemoes <[email protected]>; Andrew Morton <[email protected]>
Subject: [PATCH 8/9] r8152: use linkmode_and_and() in EEE checks

Combine the three link mode masks in a single operation and use its return value to determine whether EEE is active.

---
drivers/net/usb/r8152.c | 10 ++++------ include/linux/linkmode.h |
8 ++++++++

 2 files changed, 12 insertions(+), 6 deletions(-)
drivers/net/usb/r8152.c index renamed from drivers/net/usb/r8152.c +0 -0

f61686433031..75f0574fe1d4 100644

drivers/net/usb/r8152.c +2 -3
@@ -9149,9 +9149,8 @@ static int r8152_get_eee(struct r8152 *tp,
 struct ethtool_keee *eee)
 	if (speed & _100bps)
 		linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT,
 common);
 
-	linkmode_and(common, common, eee->advertised);
-	linkmode_and(common, common, eee->lp_advertised);
-	eee->eee_active = !linkmode_empty(common);
+	eee->eee_active = linkmode_and_and(common, common, eee-
 >advertised,
+					   eee->lp_advertised);
drivers/net/usb/r8152.c +2 -3
 	return 0;
 }
@@ -9222,9 +9221,8 @@ static int r8153_get_eee(struct r8152 *tp,
 struct ethtool_keee *eee)
 	if (speed & _100bps)
 		linkmode_set_bit(ETHTOOL_LINK_MODE_100baseT_Full_BIT,
 common);
 
-	linkmode_and(common, common, eee->advertised);
-	linkmode_and(common, common, eee->lp_advertised);
-	eee->eee_active = !linkmode_empty(common);
+	eee->eee_active = linkmode_and_and(common, common, eee-
 >advertised,
+					   eee->lp_advertised);
drivers/net/usb/r8152.c +0 -0
 	return 0;
 }
include/linux/linkmode.h index renamed from include/linux/linkmode.h +0 -0

49587a2e0f69..efd392dc62ae 100644

include/linux/linkmode.h +7 -1
@@ -26,6 +26,14 @@ static inline bool linkmode_and(unsigned long *dst,
 const unsigned long *a,
 	return bitmap_and(dst, a, b, __ETHTOOL_LINK_MODE_MASK_NBITS);
 }
 
+static inline bool linkmode_and_and(unsigned long *dst,
+				    const unsigned long *a,
+				    const unsigned long *b,
+				    const unsigned long *c)
+{
+	return bitmap_and_and(dst, a, b, c,
 __ETHTOOL_LINK_MODE_MASK_NBITS); }
+
 static inline bool linkmode_and_andnot(unsigned long *dst,
 				       const unsigned long *a,
 				       const unsigned long *b,
--
Quoted 1 line

2.53.0

author maintainer
#22 lore raw

PATCH 9/9 MAINTAINERS: co-maintain linkmode.h under BITMAP

In reply to Yury Norov

The file is largely a wrapper around bitmap.h. List it under BITMAP section for co-maintenance.

---
 MAINTAINERS | 1 +
 1 file changed, 1 insertion(+)
MAINTAINERS +1 -0
index 3a19da74d00c..b78f0bccff54 100644
@@ -4646,6 +4646,7 @@ F:	include/linux/cpumask.h
 F:	include/linux/cpumask_types.h
 F:	include/linux/find.h
 F:	include/linux/hw_bitfield.h
+F:	include/linux/linkmode.h
 F:	include/linux/nodemask.h
 F:	include/linux/nodemask_types.h
 F:	include/uapi/linux/bits.h
Signature · 1 line
2.53.0
maintainer
#23 lore raw

Re: [PATCH 9/9] MAINTAINERS: co-maintain linkmode.h under BITMAP

Quoted 2 lines · On Mon, 7 Sep 2026 17:54:38 -0400 Yury Norov wrote:

The file is largely a wrapper around bitmap.h. List it under BITMAP section for co-maintenance.

I don't think this is appropriate, configure lore+lei if you want to see the changes.