is it required to unlock net_device structure before calling free_netdev? The code I encountered does the following:
static void delete_dev(struct net_device *dev)
{
ASSERT_RTNL();
...
unregister_netdevice(dev);
...
rtnl_unlock();
free_netdev(dev);
rtnl_lock();
}
int foo()
{
struct net_device *dev;
rtnl_lock();
...
delete_dev(dev);
rtnl_unlock();
return 0;
}
Is this the right way to do the things? Thanks.
You should not unlock-lock rtnl one more time.
Here is why: there is not a word in Network Drivers API in
free_netdevsection about lock required. Nevertheless,unregister_netdevicerequires lock to be held and it has been wrapped in API inunregister_netdevfunction, which is stated in the document.Anyway, if you look into some popular driver’s sources, e1000e for example, you will see this:
As you can see, there is no unlock-lock taken.
Moreover, they use
unregister_netdevfunction so that it would only be locked insideunregister_netdeviceitself, and all dozens of deinitialinizations there would go outside the lock. Therefore, consider using simpleunregister_netdevas they(netdev kernel developers) recommend in comments to it’s source, if you think you can afford that.