netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH RFC net-next] net: Add dev_getbyhwaddr_rtnl() helper
@ 2025-02-07 12:11 Breno Leitao
  2025-02-08  0:27 ` Jakub Kicinski
  0 siblings, 1 reply; 3+ messages in thread
From: Breno Leitao @ 2025-02-07 12:11 UTC (permalink / raw)
  To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Simon Horman
  Cc: linux-kernel, netdev, kernel-team, kuniyu, ushankar, Breno Leitao

Add dedicated helper for finding devices by hardware address when
holding RTNL, similar to existing dev_getbyhwaddr_rcu(). This prevents
PROVE_LOCKING warnings when RTNL is held but RCU read lock is not.

Extract common address comparison logic into dev_comp_addr().

The context about this change could be found in the following
discussion:

Link: https://lore.kernel.org/all/20250206-scarlet-ermine-of-improvement-1fcac5@leitao/

Cc: kuniyu@amazon.com
Cc: ushankar@purestorage.com
Suggested-by: Eric Dumazet <edumazet@google.com>
Signed-off-by: Breno Leitao <leitao@debian.org>
---
 net/core/dev.c | 39 ++++++++++++++++++++++++++++++++++++---
 1 file changed, 36 insertions(+), 3 deletions(-)

diff --git a/net/core/dev.c b/net/core/dev.c
index c41d1e1cbf62e0c5778c472cdb947b6f140f6064..75f0c533ff10e7188aa55345cd8140b88a7d09ca 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -1121,6 +1121,16 @@ int netdev_get_name(struct net *net, char *name, int ifindex)
 	return ret;
 }
 
+static bool dev_comp_addr(struct net_device *dev,
+			  unsigned short type,
+			  const char *ha)
+{
+	if (dev->type == type && !memcmp(dev->dev_addr, ha, dev->addr_len))
+		return true;
+
+	return false;
+}
+
 /**
  *	dev_getbyhwaddr_rcu - find a device by its hardware address
  *	@net: the applicable net namespace
@@ -1129,7 +1139,7 @@ int netdev_get_name(struct net *net, char *name, int ifindex)
  *
  *	Search for an interface by MAC address. Returns NULL if the device
  *	is not found or a pointer to the device.
- *	The caller must hold RCU or RTNL.
+ *	The caller must hold RCU.
  *	The returned device has not had its ref count increased
  *	and the caller must therefore be careful about locking
  *
@@ -1141,14 +1151,37 @@ struct net_device *dev_getbyhwaddr_rcu(struct net *net, unsigned short type,
 	struct net_device *dev;
 
 	for_each_netdev_rcu(net, dev)
-		if (dev->type == type &&
-		    !memcmp(dev->dev_addr, ha, dev->addr_len))
+		if (dev_comp_addr(dev, type, ha))
 			return dev;
 
 	return NULL;
 }
 EXPORT_SYMBOL(dev_getbyhwaddr_rcu);
 
+/**
+ *	dev_getbyhwaddr_rtnl - find a device by its hardware address
+ *	@net: the applicable net namespace
+ *	@type: media type of device
+ *	@ha: hardware address
+ *
+ *	Similar to dev_getbyhwaddr_rcu(), but, the owner needs to hold
+ *	RTNL.
+ *
+ */
+struct net_device *dev_getbyhwaddr_rtnl(struct net *net, unsigned short type,
+					const char *ha)
+{
+	struct net_device *dev;
+
+	ASSERT_RTNL();
+	for_each_netdev(net, dev)
+		if (dev_comp_addr(dev, type, ha))
+			return dev;
+
+	return NULL;
+}
+EXPORT_SYMBOL(dev_getbyhwaddr_rtnl);
+
 struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type)
 {
 	struct net_device *dev, *ret = NULL;

---
base-commit: 0d5248724ed8bc68c867c4c65dda625277f68fbc
change-id: 20250207-arm_fix_selftest-ee29dbc33a06

Best regards,
-- 
Breno Leitao <leitao@debian.org>


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH RFC net-next] net: Add dev_getbyhwaddr_rtnl() helper
  2025-02-07 12:11 [PATCH RFC net-next] net: Add dev_getbyhwaddr_rtnl() helper Breno Leitao
@ 2025-02-08  0:27 ` Jakub Kicinski
  2025-02-10 11:40   ` Breno Leitao
  0 siblings, 1 reply; 3+ messages in thread
From: Jakub Kicinski @ 2025-02-08  0:27 UTC (permalink / raw)
  To: Breno Leitao
  Cc: David S. Miller, Eric Dumazet, Paolo Abeni, Simon Horman,
	linux-kernel, netdev, kernel-team, kuniyu, ushankar

On Fri, 07 Feb 2025 04:11:34 -0800 Breno Leitao wrote:
> +static bool dev_comp_addr(struct net_device *dev,
> +			  unsigned short type,
> +			  const char *ha)

Weird indentation.

static bool 
dev_comp_addr(struct net_device *dev,  unsigned short type, const char *ha)

or

static bool dev_comp_addr(struct net_device *dev, unsigned short type,
			  const char *ha)

> +{
> +	if (dev->type == type && !memcmp(dev->dev_addr, ha, dev->addr_len))
> +		return true;
> +
> +	return false;

	return dev->type == type && !memcmp(dev->dev_addr, ha, dev->addr_len);

> +}

> +/**
> + *	dev_getbyhwaddr_rtnl - find a device by its hardware address

I guess Eric suggested the _rtnl() suffix, tho it's quite uncommon.
Most function are either function() or function_rcu() in networking.

> + *	@net: the applicable net namespace
> + *	@type: media type of device
> + *	@ha: hardware address
> + *
> + *	Similar to dev_getbyhwaddr_rcu(), but, the owner needs to hold

unnecessary , after but

> + *	RTNL.

rtnl_lock. RTNL is short for RTNetLink

> + *

document the return value kdoc style:

	Return: pointer to the net_device, or NULL if not found

> + */
> +struct net_device *dev_getbyhwaddr_rtnl(struct net *net, unsigned short type,
> +					const char *ha)

You missed adding this to a header file?

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH RFC net-next] net: Add dev_getbyhwaddr_rtnl() helper
  2025-02-08  0:27 ` Jakub Kicinski
@ 2025-02-10 11:40   ` Breno Leitao
  0 siblings, 0 replies; 3+ messages in thread
From: Breno Leitao @ 2025-02-10 11:40 UTC (permalink / raw)
  To: Jakub Kicinski
  Cc: David S. Miller, Eric Dumazet, Paolo Abeni, Simon Horman,
	linux-kernel, netdev, kernel-team, kuniyu, ushankar

Hello Jakub,

On Fri, Feb 07, 2025 at 04:27:18PM -0800, Jakub Kicinski wrote:
> On Fri, 07 Feb 2025 04:11:34 -0800 Breno Leitao wrote:
> > +static bool dev_comp_addr(struct net_device *dev,
> > +			  unsigned short type,
> > +			  const char *ha)

> static bool dev_comp_addr(struct net_device *dev, unsigned short type,
> 			  const char *ha)

This one aligns better with the other functions in the file. Example:

  struct net_device *dev_getbyhwaddr_rcu(struct net *net, unsigned short type,
                                         const char *ha)

> > + */
> > +struct net_device *dev_getbyhwaddr_rtnl(struct net *net, unsigned short type,
> > +					const char *ha)
> 
> You missed adding this to a header file?

Yes. NIPA caught this one beautifully:

	https://patchwork.kernel.org/project/netdevbpf/patch/20250207-arm_fix_selftest-v1-1-487518d2fd1c@debian.org/

I will send a v2 soon,

Thanks for the review
--breno

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-02-10 11:40 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-02-07 12:11 [PATCH RFC net-next] net: Add dev_getbyhwaddr_rtnl() helper Breno Leitao
2025-02-08  0:27 ` Jakub Kicinski
2025-02-10 11:40   ` Breno Leitao

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).