Skip to content

Commit 36d5616

Browse files
leitaogregkh
authored andcommitted
net: Add non-RCU dev_getbyhwaddr() helper
[ Upstream commit 4b5a28b ] Add dedicated helper for finding devices by hardware address when holding rtnl_lock, similar to existing dev_getbyhwaddr_rcu(). This prevents PROVE_LOCKING warnings when rtnl_lock is held but RCU read lock is not. Extract common address comparison logic into dev_addr_cmp(). 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: [email protected] Cc: [email protected] Suggested-by: Eric Dumazet <[email protected]> Signed-off-by: Breno Leitao <[email protected]> Reviewed-by: Kuniyuki Iwashima <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]> Stable-dep-of: 4eae0ee ("arp: switch to dev_getbyhwaddr() in arp_req_set_public()") Signed-off-by: Sasha Levin <[email protected]>
1 parent cbd75b9 commit 36d5616

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

include/linux/netdevice.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3064,6 +3064,8 @@ static inline struct net_device *first_net_device_rcu(struct net *net)
30643064
}
30653065

30663066
int netdev_boot_setup_check(struct net_device *dev);
3067+
struct net_device *dev_getbyhwaddr(struct net *net, unsigned short type,
3068+
const char *hwaddr);
30673069
struct net_device *dev_getbyhwaddr_rcu(struct net *net, unsigned short type,
30683070
const char *hwaddr);
30693071
struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type);

net/core/dev.c

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,12 @@ int netdev_get_name(struct net *net, char *name, int ifindex)
10121012
return ret;
10131013
}
10141014

1015+
static bool dev_addr_cmp(struct net_device *dev, unsigned short type,
1016+
const char *ha)
1017+
{
1018+
return dev->type == type && !memcmp(dev->dev_addr, ha, dev->addr_len);
1019+
}
1020+
10151021
/**
10161022
* dev_getbyhwaddr_rcu - find a device by its hardware address
10171023
* @net: the applicable net namespace
@@ -1020,7 +1026,7 @@ int netdev_get_name(struct net *net, char *name, int ifindex)
10201026
*
10211027
* Search for an interface by MAC address. Returns NULL if the device
10221028
* is not found or a pointer to the device.
1023-
* The caller must hold RCU or RTNL.
1029+
* The caller must hold RCU.
10241030
* The returned device has not had its ref count increased
10251031
* and the caller must therefore be careful about locking
10261032
*
@@ -1032,14 +1038,39 @@ struct net_device *dev_getbyhwaddr_rcu(struct net *net, unsigned short type,
10321038
struct net_device *dev;
10331039

10341040
for_each_netdev_rcu(net, dev)
1035-
if (dev->type == type &&
1036-
!memcmp(dev->dev_addr, ha, dev->addr_len))
1041+
if (dev_addr_cmp(dev, type, ha))
10371042
return dev;
10381043

10391044
return NULL;
10401045
}
10411046
EXPORT_SYMBOL(dev_getbyhwaddr_rcu);
10421047

1048+
/**
1049+
* dev_getbyhwaddr() - find a device by its hardware address
1050+
* @net: the applicable net namespace
1051+
* @type: media type of device
1052+
* @ha: hardware address
1053+
*
1054+
* Similar to dev_getbyhwaddr_rcu(), but the owner needs to hold
1055+
* rtnl_lock.
1056+
*
1057+
* Context: rtnl_lock() must be held.
1058+
* Return: pointer to the net_device, or NULL if not found
1059+
*/
1060+
struct net_device *dev_getbyhwaddr(struct net *net, unsigned short type,
1061+
const char *ha)
1062+
{
1063+
struct net_device *dev;
1064+
1065+
ASSERT_RTNL();
1066+
for_each_netdev(net, dev)
1067+
if (dev_addr_cmp(dev, type, ha))
1068+
return dev;
1069+
1070+
return NULL;
1071+
}
1072+
EXPORT_SYMBOL(dev_getbyhwaddr);
1073+
10431074
struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type)
10441075
{
10451076
struct net_device *dev, *ret = NULL;

0 commit comments

Comments
 (0)