]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
net: Hold __rtnl_net_lock() in (un)?register_netdevice_notifier().
authorKuniyuki Iwashima <kuniyu@amazon.com>
Mon, 6 Jan 2025 07:07:49 +0000 (16:07 +0900)
committerJakub Kicinski <kuba@kernel.org>
Wed, 8 Jan 2025 01:49:19 +0000 (17:49 -0800)
(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])

Link: https://lore.kernel.org/netdev/20250104063735.36945-2-kuniyu@amazon.com/
Link: https://lore.kernel.org/netdev/20250105075957.67334-1-kuniyu@amazon.com/
Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
Link: https://patch.msgid.link/20250106070751.63146-2-kuniyu@amazon.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
net/core/dev.c

index 2179da3dfffc05f7708fe67dc63c52cb3bf1a45a..34ded79746755cdfa876b53cd110b42d213989be 100644 (file)
@@ -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 @@ unlock:
        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();