Skip to content

Commit

Permalink
net: Do not take net_rwsem in __rtnl_link_unregister()
Browse files Browse the repository at this point in the history
This function calls call_netdevice_notifier(), which also
may take net_rwsem. So, we can't use net_rwsem here.

This patch makes callers of this functions take pernet_ops_rwsem,
like register_netdevice_notifier() does. This will protect
the modifications of net_namespace_list, and allows notifiers
to take it (they won't have to care about context).

Since __rtnl_link_unregister() is used on module load
and unload (which are not frequent operations), this looks
for me better, than make all call_netdevice_notifier()
always executing in "protected net_namespace_list" context.

Also, this fixes the problem we had a deal in 328fbe7
"Close race between {un, }register_netdevice_notifier and ...",
and guarantees __rtnl_link_unregister() does not skip
exitting net.

Signed-off-by: Kirill Tkhai <ktkhai@virtuozzo.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
Kirill Tkhai authored and David S. Miller committed Apr 1, 2018
1 parent fc1dd36 commit 554873e
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 3 deletions.
2 changes: 2 additions & 0 deletions drivers/net/dummy.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ static int __init dummy_init_module(void)
{
int i, err = 0;

down_write(&pernet_ops_rwsem);
rtnl_lock();
err = __rtnl_link_register(&dummy_link_ops);
if (err < 0)
Expand All @@ -233,6 +234,7 @@ static int __init dummy_init_module(void)

out:
rtnl_unlock();
up_write(&pernet_ops_rwsem);

return err;
}
Expand Down
2 changes: 2 additions & 0 deletions drivers/net/ifb.c
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ static int __init ifb_init_module(void)
{
int i, err;

down_write(&pernet_ops_rwsem);
rtnl_lock();
err = __rtnl_link_register(&ifb_link_ops);
if (err < 0)
Expand All @@ -344,6 +345,7 @@ static int __init ifb_init_module(void)

out:
rtnl_unlock();
up_write(&pernet_ops_rwsem);

return err;
}
Expand Down
1 change: 1 addition & 0 deletions net/core/net_namespace.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ static bool init_net_initialized;
* outside.
*/
DECLARE_RWSEM(pernet_ops_rwsem);
EXPORT_SYMBOL_GPL(pernet_ops_rwsem);

#define MIN_PERNET_OPS_ID \
((sizeof(struct net_generic) + sizeof(void *) - 1) / sizeof(void *))
Expand Down
6 changes: 3 additions & 3 deletions net/core/rtnetlink.c
Original file line number Diff line number Diff line change
Expand Up @@ -412,17 +412,17 @@ static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
* __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
* @ops: struct rtnl_link_ops * to unregister
*
* The caller must hold the rtnl_mutex.
* The caller must hold the rtnl_mutex and guarantee net_namespace_list
* integrity (hold pernet_ops_rwsem for writing to close the race
* with setup_net() and cleanup_net()).
*/
void __rtnl_link_unregister(struct rtnl_link_ops *ops)
{
struct net *net;

down_read(&net_rwsem);
for_each_net(net) {
__rtnl_kill_links(net, ops);
}
up_read(&net_rwsem);
list_del(&ops->list);
}
EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
Expand Down

0 comments on commit 554873e

Please sign in to comment.