Skip to content

Commit c269a24

Browse files
committed
net: make free_netdev() more lenient with unregistering devices
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 <kuba@kernel.org>
1 parent 2b446e6 commit c269a24

File tree

3 files changed

+18
-20
lines changed

3 files changed

+18
-20
lines changed

net/8021q/vlan.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,7 @@ static int register_vlan_device(struct net_device *real_dev, u16 vlan_id)
284284
return 0;
285285

286286
out_free_newdev:
287-
if (new_dev->reg_state == NETREG_UNINITIALIZED ||
288-
new_dev->reg_state == NETREG_UNREGISTERED)
289-
free_netdev(new_dev);
287+
free_netdev(new_dev);
290288
return err;
291289
}
292290

net/core/dev.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10631,6 +10631,17 @@ void free_netdev(struct net_device *dev)
1063110631
struct napi_struct *p, *n;
1063210632

1063310633
might_sleep();
10634+
10635+
/* When called immediately after register_netdevice() failed the unwind
10636+
* handling may still be dismantling the device. Handle that case by
10637+
* deferring the free.
10638+
*/
10639+
if (dev->reg_state == NETREG_UNREGISTERING) {
10640+
ASSERT_RTNL();
10641+
dev->needs_free_netdev = true;
10642+
return;
10643+
}
10644+
1063410645
netif_free_tx_queues(dev);
1063510646
netif_free_rx_queues(dev);
1063610647

net/core/rtnetlink.c

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3439,26 +3439,15 @@ static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
34393439

34403440
dev->ifindex = ifm->ifi_index;
34413441

3442-
if (ops->newlink) {
3442+
if (ops->newlink)
34433443
err = ops->newlink(link_net ? : net, dev, tb, data, extack);
3444-
/* Drivers should set dev->needs_free_netdev
3445-
* and unregister it on failure after registration
3446-
* so that device could be finally freed in rtnl_unlock.
3447-
*/
3448-
if (err < 0) {
3449-
/* If device is not registered at all, free it now */
3450-
if (dev->reg_state == NETREG_UNINITIALIZED ||
3451-
dev->reg_state == NETREG_UNREGISTERED)
3452-
free_netdev(dev);
3453-
goto out;
3454-
}
3455-
} else {
3444+
else
34563445
err = register_netdevice(dev);
3457-
if (err < 0) {
3458-
free_netdev(dev);
3459-
goto out;
3460-
}
3446+
if (err < 0) {
3447+
free_netdev(dev);
3448+
goto out;
34613449
}
3450+
34623451
err = rtnl_configure_link(dev, ifm);
34633452
if (err < 0)
34643453
goto out_unregister;

0 commit comments

Comments
 (0)