* [PATCH net v5 0/2] net: core: improvements to device lookup by hardware address.
@ 2025-02-18 13:49 Breno Leitao
2025-02-18 13:49 ` [PATCH net v5 1/2] net: Add non-RCU dev_getbyhwaddr() helper Breno Leitao
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Breno Leitao @ 2025-02-18 13:49 UTC (permalink / raw)
To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Simon Horman, Andrew Lunn, David Ahern
Cc: linux-kernel, netdev, Eric Dumazet, Breno Leitao, kuniyu,
ushankar
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.
Signed-off-by: Breno Leitao <leitao@debian.org>
---
Changes in v5:
- Change the function name from dev_comp_addr() to dev_addr_cmp()
(Jakub).
- Fix the kerneldoc function name.
- Link to v4: https://lore.kernel.org/r/20250213-arm_fix_selftest-v4-0-26714529a6cf@debian.org
Changes in v4:
- Split the patchset in two, and now targeting `net` instead of
`net-next` (Kuniyuki Iwashima)
- Identended the kernel-doc in the new way. The other functions will
come in a separate patchset. (Kuniyuki Iwashima)
- Link to v3: https://lore.kernel.org/r/20250212-arm_fix_selftest-v3-0-72596cb77e44@debian.org
Changes in v3:
- Fixed the cover letter (Kuniyuki Iwashima)
- Added a third patch converting arp_req_set_public() to the new helper
(Kuniyuki Iwashima)
- Link to v2:
https://lore.kernel.org/r/20250210-arm_fix_selftest-v2-0-ba84b5bc58c8@debian.org
Changes in v2:
- Fixed the documentation (Jakub)
- Renamed the function from dev_getbyhwaddr_rtnl() to dev_getbyhwaddr()
(Jakub)
- Exported the function in the header (Jakub)
- Link to v1: https://lore.kernel.org/r/20250207-arm_fix_selftest-v1-1-487518d2fd1c@debian.org
---
Breno Leitao (2):
net: Add non-RCU dev_getbyhwaddr() helper
arp: switch to dev_getbyhwaddr() in arp_req_set_public()
include/linux/netdevice.h | 2 ++
net/core/dev.c | 37 ++++++++++++++++++++++++++++++++++---
net/ipv4/arp.c | 2 +-
3 files changed, 37 insertions(+), 4 deletions(-)
---
base-commit: 0469b410c888414c3505d8d2b5814eb372404638
change-id: 20250207-arm_fix_selftest-ee29dbc33a06
Best regards,
--
Breno Leitao <leitao@debian.org>
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH net v5 1/2] net: Add non-RCU dev_getbyhwaddr() helper
2025-02-18 13:49 [PATCH net v5 0/2] net: core: improvements to device lookup by hardware address Breno Leitao
@ 2025-02-18 13:49 ` Breno Leitao
2025-02-18 13:49 ` [PATCH net v5 2/2] arp: switch to dev_getbyhwaddr() in arp_req_set_public() Breno Leitao
2025-02-20 3:10 ` [PATCH net v5 0/2] net: core: improvements to device lookup by hardware address patchwork-bot+netdevbpf
2 siblings, 0 replies; 4+ messages in thread
From: Breno Leitao @ 2025-02-18 13:49 UTC (permalink / raw)
To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Simon Horman, Andrew Lunn, David Ahern
Cc: linux-kernel, netdev, Eric Dumazet, Breno Leitao, kuniyu,
ushankar, Kuniyuki Iwashima
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: kuniyu@amazon.com
Cc: ushankar@purestorage.com
Suggested-by: Eric Dumazet <edumazet@google.com>
Signed-off-by: Breno Leitao <leitao@debian.org>
Reviewed-by: Kuniyuki Iwashima <kuniyu@amazon.com>
---
include/linux/netdevice.h | 2 ++
net/core/dev.c | 37 ++++++++++++++++++++++++++++++++++---
2 files changed, 36 insertions(+), 3 deletions(-)
diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h
index 365f0e2098d13f40ce6d8865962678b052b39a16..ab550a89b9bfaa5682e65f1dcc7f5f99ce90eb94 100644
--- a/include/linux/netdevice.h
+++ b/include/linux/netdevice.h
@@ -3275,6 +3275,8 @@ static inline struct net_device *first_net_device_rcu(struct net *net)
}
int netdev_boot_setup_check(struct net_device *dev);
+struct net_device *dev_getbyhwaddr(struct net *net, unsigned short type,
+ const char *hwaddr);
struct net_device *dev_getbyhwaddr_rcu(struct net *net, unsigned short type,
const char *hwaddr);
struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type);
diff --git a/net/core/dev.c b/net/core/dev.c
index 55e356a68db667982e7e62d09d07feecc14deebe..ec92fd2465c23de35187b2a9eeee0f49f98f0757 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -1121,6 +1121,12 @@ int netdev_get_name(struct net *net, char *name, int ifindex)
return ret;
}
+static bool dev_addr_cmp(struct net_device *dev, unsigned short type,
+ const char *ha)
+{
+ return dev->type == type && !memcmp(dev->dev_addr, ha, dev->addr_len);
+}
+
/**
* dev_getbyhwaddr_rcu - find a device by its hardware address
* @net: the applicable net namespace
@@ -1129,7 +1135,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 +1147,39 @@ 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_addr_cmp(dev, type, ha))
return dev;
return NULL;
}
EXPORT_SYMBOL(dev_getbyhwaddr_rcu);
+/**
+ * dev_getbyhwaddr() - 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_lock.
+ *
+ * Context: rtnl_lock() must be held.
+ * Return: pointer to the net_device, or NULL if not found
+ */
+struct net_device *dev_getbyhwaddr(struct net *net, unsigned short type,
+ const char *ha)
+{
+ struct net_device *dev;
+
+ ASSERT_RTNL();
+ for_each_netdev(net, dev)
+ if (dev_addr_cmp(dev, type, ha))
+ return dev;
+
+ return NULL;
+}
+EXPORT_SYMBOL(dev_getbyhwaddr);
+
struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type)
{
struct net_device *dev, *ret = NULL;
--
2.43.5
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH net v5 2/2] arp: switch to dev_getbyhwaddr() in arp_req_set_public()
2025-02-18 13:49 [PATCH net v5 0/2] net: core: improvements to device lookup by hardware address Breno Leitao
2025-02-18 13:49 ` [PATCH net v5 1/2] net: Add non-RCU dev_getbyhwaddr() helper Breno Leitao
@ 2025-02-18 13:49 ` Breno Leitao
2025-02-20 3:10 ` [PATCH net v5 0/2] net: core: improvements to device lookup by hardware address patchwork-bot+netdevbpf
2 siblings, 0 replies; 4+ messages in thread
From: Breno Leitao @ 2025-02-18 13:49 UTC (permalink / raw)
To: David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Simon Horman, Andrew Lunn, David Ahern
Cc: linux-kernel, netdev, Eric Dumazet, Breno Leitao,
Kuniyuki Iwashima
The arp_req_set_public() function is called with the rtnl lock held,
which provides enough synchronization protection. This makes the RCU
variant of dev_getbyhwaddr() unnecessary. Switch to using the simpler
dev_getbyhwaddr() function since we already have the required rtnl
locking.
This change helps maintain consistency in the networking code by using
the appropriate helper function for the existing locking context.
Fixes: 941666c2e3e0 ("net: RCU conversion of dev_getbyhwaddr() and arp_ioctl()")
Suggested-by: Kuniyuki Iwashima <kuniyu@amazon.com>
Reviewed-by: Kuniyuki Iwashima <kuniyu@amazon.com>
Signed-off-by: Breno Leitao <leitao@debian.org>
---
net/ipv4/arp.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/net/ipv4/arp.c b/net/ipv4/arp.c
index f23a1ec6694cb2f1bd60f28faa357fcad83c165a..814300eee39de12b959caf225f65d9190594bba9 100644
--- a/net/ipv4/arp.c
+++ b/net/ipv4/arp.c
@@ -1077,7 +1077,7 @@ static int arp_req_set_public(struct net *net, struct arpreq *r,
__be32 mask = ((struct sockaddr_in *)&r->arp_netmask)->sin_addr.s_addr;
if (!dev && (r->arp_flags & ATF_COM)) {
- dev = dev_getbyhwaddr_rcu(net, r->arp_ha.sa_family,
+ dev = dev_getbyhwaddr(net, r->arp_ha.sa_family,
r->arp_ha.sa_data);
if (!dev)
return -ENODEV;
--
2.43.5
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH net v5 0/2] net: core: improvements to device lookup by hardware address.
2025-02-18 13:49 [PATCH net v5 0/2] net: core: improvements to device lookup by hardware address Breno Leitao
2025-02-18 13:49 ` [PATCH net v5 1/2] net: Add non-RCU dev_getbyhwaddr() helper Breno Leitao
2025-02-18 13:49 ` [PATCH net v5 2/2] arp: switch to dev_getbyhwaddr() in arp_req_set_public() Breno Leitao
@ 2025-02-20 3:10 ` patchwork-bot+netdevbpf
2 siblings, 0 replies; 4+ messages in thread
From: patchwork-bot+netdevbpf @ 2025-02-20 3:10 UTC (permalink / raw)
To: Breno Leitao
Cc: davem, edumazet, kuba, pabeni, horms, andrew+netdev, dsahern,
linux-kernel, netdev, eric.dumazet, kuniyu, ushankar
Hello:
This series was applied to netdev/net.git (main)
by Jakub Kicinski <kuba@kernel.org>:
On Tue, 18 Feb 2025 05:49:29 -0800 you wrote:
> 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.
>
> [...]
Here is the summary with links:
- [net,v5,1/2] net: Add non-RCU dev_getbyhwaddr() helper
https://git.kernel.org/netdev/net/c/4b5a28b38c4a
- [net,v5,2/2] arp: switch to dev_getbyhwaddr() in arp_req_set_public()
https://git.kernel.org/netdev/net/c/4eae0ee0f1e6
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2025-02-20 3:10 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-02-18 13:49 [PATCH net v5 0/2] net: core: improvements to device lookup by hardware address Breno Leitao
2025-02-18 13:49 ` [PATCH net v5 1/2] net: Add non-RCU dev_getbyhwaddr() helper Breno Leitao
2025-02-18 13:49 ` [PATCH net v5 2/2] arp: switch to dev_getbyhwaddr() in arp_req_set_public() Breno Leitao
2025-02-20 3:10 ` [PATCH net v5 0/2] net: core: improvements to device lookup by hardware address patchwork-bot+netdevbpf
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).