Skip to content

Commit

Permalink
net: make free_netdev() more lenient with unregistering devices
Browse files Browse the repository at this point in the history
There are two flavors of handling netdev registration:
 - ones called without holding rtnl_lock: register_netdev() and
   unregister_netdev(); and
 - those called with rtnl_lock held: register_netdevice() and
   unregister_netdevice().

While the semantics of the former are pretty clear, the same can't
be said about the latter. The netdev_todo mechanism is utilized to
perform some of the device unregistering tasks and it hooks into
rtnl_unlock() so the locked variants can't actually finish the work.
In general free_netdev() does not mix well with locked calls. Most
drivers operating under rtnl_lock set dev->needs_free_netdev to true
and expect core to make the free_netdev() call some time later.

The part where this becomes most problematic is error paths. There is
no way to unwind the state cleanly after a call to register_netdevice(),
since unreg can't be performed fully without dropping locks.

Make free_netdev() more lenient, and defer the freeing if device
is being unregistered. This allows error paths to simply call
free_netdev() both after register_netdevice() failed, and after
a call to unregister_netdevice() but before dropping rtnl_lock.

Simplify the error paths which are currently doing gymnastics
around free_netdev() handling.

Signed-off-by: Jakub Kicinski <[email protected]>
  • Loading branch information
kuba-moo committed Jan 9, 2021
1 parent 2b446e6 commit c269a24
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
4 changes: 1 addition & 3 deletions net/8021q/vlan.c
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,7 @@ static int register_vlan_device(struct net_device *real_dev, u16 vlan_id)
return 0;

out_free_newdev:
if (new_dev->reg_state == NETREG_UNINITIALIZED ||
new_dev->reg_state == NETREG_UNREGISTERED)
free_netdev(new_dev);
free_netdev(new_dev);
return err;
}

Expand Down
11 changes: 11 additions & 0 deletions net/core/dev.c
Original file line number Diff line number Diff line change
Expand Up @@ -10631,6 +10631,17 @@ void free_netdev(struct net_device *dev)
struct napi_struct *p, *n;

might_sleep();

/* When called immediately after register_netdevice() failed the unwind
* handling may still be dismantling the device. Handle that case by
* deferring the free.
*/
if (dev->reg_state == NETREG_UNREGISTERING) {
ASSERT_RTNL();
dev->needs_free_netdev = true;
return;
}

netif_free_tx_queues(dev);
netif_free_rx_queues(dev);

Expand Down
23 changes: 6 additions & 17 deletions net/core/rtnetlink.c
Original file line number Diff line number Diff line change
Expand Up @@ -3439,26 +3439,15 @@ static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,

dev->ifindex = ifm->ifi_index;

if (ops->newlink) {
if (ops->newlink)
err = ops->newlink(link_net ? : net, dev, tb, data, extack);
/* Drivers should set dev->needs_free_netdev
* and unregister it on failure after registration
* so that device could be finally freed in rtnl_unlock.
*/
if (err < 0) {
/* If device is not registered at all, free it now */
if (dev->reg_state == NETREG_UNINITIALIZED ||
dev->reg_state == NETREG_UNREGISTERED)
free_netdev(dev);
goto out;
}
} else {
else
err = register_netdevice(dev);
if (err < 0) {
free_netdev(dev);
goto out;
}
if (err < 0) {
free_netdev(dev);
goto out;
}

err = rtnl_configure_link(dev, ifm);
if (err < 0)
goto out_unregister;
Expand Down

0 comments on commit c269a24

Please sign in to comment.