[PATCH NET-PREV 44/51] net: Call dellink with nd_lock is held

From: Kirill Tkhai
Date: Sat Mar 22 2025 - 10:56:46 EST


After previous patches all linked devices share the same lock.
Here we add nd_lock around dellink to start making calls of
unregister_netdevice() under nd_lock is locked.

One more good thing is many netdev_upper_dev_unlink() becomes
called under nd_lock is held, but not all yet.

Note, that ->dellink called from netdevice notifiers are not
braced yet.

Signed-off-by: Kirill Tkhai <tkhai@xxxxx>
---
net/core/dev.c | 3 +++
net/core/rtnetlink.c | 10 ++++++++++
2 files changed, 13 insertions(+)

diff --git a/net/core/dev.c b/net/core/dev.c
index 55df8157bca9..f0f93b5a2819 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -12399,6 +12399,7 @@ static void __net_exit default_device_exit_batch(struct list_head *net_list)
* Do this across as many network namespaces as possible to
* improve batching efficiency.
*/
+ struct nd_lock *nd_lock;
struct net_device *dev;
struct net *net;
LIST_HEAD(dev_kill_list);
@@ -12411,10 +12412,12 @@ static void __net_exit default_device_exit_batch(struct list_head *net_list)

list_for_each_entry(net, net_list, exit_list) {
for_each_netdev_reverse(net, dev) {
+ lock_netdev(dev, &nd_lock);
if (dev->rtnl_link_ops && dev->rtnl_link_ops->dellink)
dev->rtnl_link_ops->dellink(dev, &dev_kill_list);
else
unregister_netdevice_queue(dev, &dev_kill_list);
+ unlock_netdev(nd_lock);
}
}
unregister_netdevice_many(&dev_kill_list);
diff --git a/net/core/rtnetlink.c b/net/core/rtnetlink.c
index 67b4b0610d14..fdc06f0ecf31 100644
--- a/net/core/rtnetlink.c
+++ b/net/core/rtnetlink.c
@@ -449,12 +449,15 @@ EXPORT_SYMBOL_GPL(rtnl_link_register);

static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
{
+ struct nd_lock *nd_lock;
struct net_device *dev;
LIST_HEAD(list_kill);

for_each_netdev(net, dev) {
+ lock_netdev(dev, &nd_lock);
if (dev->rtnl_link_ops == ops)
ops->dellink(dev, &list_kill);
+ unlock_netdev(nd_lock);
}
unregister_netdevice_many(&list_kill);
}
@@ -3260,9 +3263,12 @@ static int rtnl_group_dellink(const struct net *net, int group)
for_each_netdev_safe(net, dev, aux) {
if (dev->group == group) {
const struct rtnl_link_ops *ops;
+ struct nd_lock *nd_lock;

ops = dev->rtnl_link_ops;
+ lock_netdev(dev, &nd_lock);
ops->dellink(dev, &list_kill);
+ unlock_netdev(nd_lock);
}
}
unregister_netdevice_many(&list_kill);
@@ -3273,13 +3279,17 @@ static int rtnl_group_dellink(const struct net *net, int group)
int rtnl_delete_link(struct net_device *dev, u32 portid, const struct nlmsghdr *nlh)
{
const struct rtnl_link_ops *ops;
+ struct nd_lock *nd_lock;
LIST_HEAD(list_kill);

ops = dev->rtnl_link_ops;
if (!ops || !ops->dellink)
return -EOPNOTSUPP;

+ lock_netdev(dev, &nd_lock);
ops->dellink(dev, &list_kill);
+ unlock_netdev(nd_lock);
+
unregister_netdevice_many_notify(&list_kill, portid, nlh);

return 0;