Skip to content

Commit 6252009

Browse files
committed
Merge branch 'net-core-improvements-to-device-lookup-by-hardware-address'
Breno Leitao says: ==================== net: core: improvements to device lookup by hardware address. The first patch adds a new dev_getbyhwaddr() helper function for finding devices by hardware address when the rtnl lock is held. This prevents PROVE_LOCKING warnings that occurred when rtnl lock was held but the RCU read lock wasn't. The common address comparison logic is extracted into dev_comp_addr() to avoid code duplication. The second coverts arp_req_set_public() to the new helper. ==================== Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
2 parents 606572e + 4eae0ee commit 6252009

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

include/linux/netdevice.h

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

32773277
int netdev_boot_setup_check(struct net_device *dev);
3278+
struct net_device *dev_getbyhwaddr(struct net *net, unsigned short type,
3279+
const char *hwaddr);
32783280
struct net_device *dev_getbyhwaddr_rcu(struct net *net, unsigned short type,
32793281
const char *hwaddr);
32803282
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
@@ -1121,6 +1121,12 @@ int netdev_get_name(struct net *net, char *name, int ifindex)
11211121
return ret;
11221122
}
11231123

1124+
static bool dev_addr_cmp(struct net_device *dev, unsigned short type,
1125+
const char *ha)
1126+
{
1127+
return dev->type == type && !memcmp(dev->dev_addr, ha, dev->addr_len);
1128+
}
1129+
11241130
/**
11251131
* dev_getbyhwaddr_rcu - find a device by its hardware address
11261132
* @net: the applicable net namespace
@@ -1129,7 +1135,7 @@ int netdev_get_name(struct net *net, char *name, int ifindex)
11291135
*
11301136
* Search for an interface by MAC address. Returns NULL if the device
11311137
* is not found or a pointer to the device.
1132-
* The caller must hold RCU or RTNL.
1138+
* The caller must hold RCU.
11331139
* The returned device has not had its ref count increased
11341140
* and the caller must therefore be careful about locking
11351141
*
@@ -1141,14 +1147,39 @@ struct net_device *dev_getbyhwaddr_rcu(struct net *net, unsigned short type,
11411147
struct net_device *dev;
11421148

11431149
for_each_netdev_rcu(net, dev)
1144-
if (dev->type == type &&
1145-
!memcmp(dev->dev_addr, ha, dev->addr_len))
1150+
if (dev_addr_cmp(dev, type, ha))
11461151
return dev;
11471152

11481153
return NULL;
11491154
}
11501155
EXPORT_SYMBOL(dev_getbyhwaddr_rcu);
11511156

1157+
/**
1158+
* dev_getbyhwaddr() - find a device by its hardware address
1159+
* @net: the applicable net namespace
1160+
* @type: media type of device
1161+
* @ha: hardware address
1162+
*
1163+
* Similar to dev_getbyhwaddr_rcu(), but the owner needs to hold
1164+
* rtnl_lock.
1165+
*
1166+
* Context: rtnl_lock() must be held.
1167+
* Return: pointer to the net_device, or NULL if not found
1168+
*/
1169+
struct net_device *dev_getbyhwaddr(struct net *net, unsigned short type,
1170+
const char *ha)
1171+
{
1172+
struct net_device *dev;
1173+
1174+
ASSERT_RTNL();
1175+
for_each_netdev(net, dev)
1176+
if (dev_addr_cmp(dev, type, ha))
1177+
return dev;
1178+
1179+
return NULL;
1180+
}
1181+
EXPORT_SYMBOL(dev_getbyhwaddr);
1182+
11521183
struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type)
11531184
{
11541185
struct net_device *dev, *ret = NULL;

net/ipv4/arp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1077,7 +1077,7 @@ static int arp_req_set_public(struct net *net, struct arpreq *r,
10771077
__be32 mask = ((struct sockaddr_in *)&r->arp_netmask)->sin_addr.s_addr;
10781078

10791079
if (!dev && (r->arp_flags & ATF_COM)) {
1080-
dev = dev_getbyhwaddr_rcu(net, r->arp_ha.sa_family,
1080+
dev = dev_getbyhwaddr(net, r->arp_ha.sa_family,
10811081
r->arp_ha.sa_data);
10821082
if (!dev)
10831083
return -ENODEV;

0 commit comments

Comments
 (0)