Skip to contentSkip to conversation

net: Hold per-netns RTNL during netdev notifier registration.

PATCH Applied Kuniyuki Iwashima 2 participants 5 messages Jan 6, 2025 – Jan 8, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover net: Hold per-netns RTNL during netdev notifier registration.

This series adds per-netns RTNL for registration of the global and per-netns netdev notifiers.

Changes:
v2:
* Drop patch 1 (leave global netdev_chain raw_notifier as is)

v1: https://lore.kernel.org/netdev/[email protected]/

Kuniyuki Iwashima (3):
net: Hold __rtnl_net_lock() in (un)?register_netdevice_notifier().
net: Hold rtnl_net_lock() in (un)?register_netdevice_notifier_net().
net: Hold rtnl_net_lock() in
(un)?register_netdevice_notifier_dev_net().

 net/core/dev.c | 41 +++++++++++++++++++++++++++++------------
 1 file changed, 29 insertions(+), 12 deletions(-)
Signature · 1 line
2.39.5 (Apple Git-154)
#1 lore raw

PATCH v2 1/3 net: Hold __rtnl_net_lock() in (un)?register_netdevice_notifier().

(un)?register_netdevice_notifier() hold pernet_ops_rwsem and RTNL, iterate all netns, and trigger the notifier for all netdev.

Let's hold __rtnl_net_lock() before triggering the notifier.

Note that we will need protection for netdev_chain when RTNL is removed. (e.g. blocking_notifier conversion [0] with a lockdep annotation [1])

---
 net/core/dev.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
net/core/dev.c +13 -2
index e7223972b9aa..0096a81b474f 100644
@@ -1769,14 +1769,19 @@ int register_netdevice_notifier(struct notifier_block *nb)
 
 	/* Close race with setup_net() and cleanup_net() */
 	down_write(&pernet_ops_rwsem);
+
+	/* When RTNL is removed, we need protection for netdev_chain. */
 	rtnl_lock();
+
 	err = raw_notifier_chain_register(&netdev_chain, nb);
 	if (err)
 		goto unlock;
 	if (dev_boot_phase)
 		goto unlock;
 	for_each_net(net) {
+		__rtnl_net_lock(net);
 		err = call_netdevice_register_net_notifiers(nb, net);
+		__rtnl_net_unlock(net);
 		if (err)
 			goto rollback;
 	}
@@ -1787,8 +1792,11 @@ int register_netdevice_notifier(struct notifier_block *nb)
 	return err;
 
 rollback:
-	for_each_net_continue_reverse(net)
+	for_each_net_continue_reverse(net) {
+		__rtnl_net_lock(net);
 		call_netdevice_unregister_net_notifiers(nb, net);
+		__rtnl_net_unlock(net);
+	}
 
 	raw_notifier_chain_unregister(&netdev_chain, nb);
 	goto unlock;
@@ -1821,8 +1829,11 @@ int unregister_netdevice_notifier(struct notifier_block *nb)
 	if (err)
 		goto unlock;
 
-	for_each_net(net)
+	for_each_net(net) {
+		__rtnl_net_lock(net);
 		call_netdevice_unregister_net_notifiers(nb, net);
+		__rtnl_net_unlock(net);
+	}
 
 unlock:
 	rtnl_unlock();
Signature · 1 line
2.39.5 (Apple Git-154)
#2 lore raw

PATCH v2 2/3 net: Hold rtnl_net_lock() in (un)?register_netdevice_notifier_net().

In reply to Kuniyuki Iwashima

(un)?register_netdevice_notifier_net() hold RTNL before triggering the notifier for all netdev in the netns.

Let's convert the RTNL to rtnl_net_lock().

Note that the per-netns netdev notifier is protected by per-netns RTNL.

---
 net/core/dev.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
net/core/dev.c +6 -4
index 0096a81b474f..6212c8b91fce 100644
@@ -1897,9 +1897,10 @@ int register_netdevice_notifier_net(struct net *net, struct notifier_block *nb)
 {
 	int err;
 
-	rtnl_lock();
+	rtnl_net_lock(net);
 	err = __register_netdevice_notifier_net(net, nb, false);
-	rtnl_unlock();
+	rtnl_net_unlock(net);
+
 	return err;
 }
 EXPORT_SYMBOL(register_netdevice_notifier_net);
@@ -1925,9 +1926,10 @@ int unregister_netdevice_notifier_net(struct net *net,
 {
 	int err;
 
-	rtnl_lock();
+	rtnl_net_lock(net);
 	err = __unregister_netdevice_notifier_net(net, nb);
-	rtnl_unlock();
+	rtnl_net_unlock(net);
+
 	return err;
 }
 EXPORT_SYMBOL(unregister_netdevice_notifier_net);
Signature · 1 line
2.39.5 (Apple Git-154)
#3 lore raw

PATCH v2 3/3 net: Hold rtnl_net_lock() in (un)?register_netdevice_notifier_dev_net().

In reply to Kuniyuki Iwashima

(un)?register_netdevice_notifier_dev_net() hold RTNL before triggering the notifier for all netdev in the netns.

Let's convert the RTNL to rtnl_net_lock().

Note that move_netdevice_notifiers_dev_net() is assumed to be (but not yet) protected by per-netns RTNL of both src and dst netns; we need to convert wireless and hyperv drivers that call dev_change_net_namespace().

---
 net/core/dev.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
net/core/dev.c +10 -6
index 6212c8b91fce..5086b5b55409 100644
@@ -1946,15 +1946,17 @@ int register_netdevice_notifier_dev_net(struct net_device *dev,
 					struct notifier_block *nb,
 					struct netdev_net_notifier *nn)
 {
+	struct net *net = dev_net(dev);
 	int err;
 
-	rtnl_lock();
-	err = __register_netdevice_notifier_net(dev_net(dev), nb, false);
+	rtnl_net_lock(net);
+	err = __register_netdevice_notifier_net(net, nb, false);
 	if (!err) {
 		nn->nb = nb;
 		list_add(&nn->list, &dev->net_notifier_list);
 	}
-	rtnl_unlock();
+	rtnl_net_unlock(net);
+
 	return err;
 }
 EXPORT_SYMBOL(register_netdevice_notifier_dev_net);
@@ -1963,12 +1965,14 @@ int unregister_netdevice_notifier_dev_net(struct net_device *dev,
 					  struct notifier_block *nb,
 					  struct netdev_net_notifier *nn)
 {
+	struct net *net = dev_net(dev);
 	int err;
 
-	rtnl_lock();
+	rtnl_net_lock(net);
 	list_del(&nn->list);
-	err = __unregister_netdevice_notifier_net(dev_net(dev), nb);
-	rtnl_unlock();
+	err = __unregister_netdevice_notifier_net(net, nb);
+	rtnl_net_unlock(net);
+
 	return err;
 }
 EXPORT_SYMBOL(unregister_netdevice_notifier_dev_net);
Signature · 1 line
2.39.5 (Apple Git-154)
#4 lore raw
In reply to Kuniyuki Iwashima
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Quoted 9 lines · On Mon, 6 Jan 2025 16:07:48 +0900 you wrote:

This series adds per-netns RTNL for registration of the global and per-netns netdev notifiers.

Changes:
v2:
* Drop patch 1 (leave global netdev_chain raw_notifier as is)

[...]

Here is the summary with links:
- [v2,net-next,1/3] net: Hold __rtnl_net_lock() in (un)?register_netdevice_notifier().

    https://git.kernel.org/netdev/net-next/c/a239e0625097
  - [v2,net-next,2/3] net: Hold rtnl_net_lock() in (un)?register_netdevice_notifier_net().
    https://git.kernel.org/netdev/net-next/c/ca779f40654a
  - [v2,net-next,3/3] net: Hold rtnl_net_lock() in (un)?register_netdevice_notifier_dev_net().
    https://git.kernel.org/netdev/net-next/c/7fb1073300a2

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html