static void rollback_registered_many(struct list_head *head)
{
struct net_device *dev, *tmp;
BUG_ON(dev_boot_phase);
ASSERT_RTNL(); //检查是否获取了rtnl互斥量
list_for_each_entry_safe(dev, tmp, head, unreg_list) {
/* Some devices call without registering
* for initialization unwind. Remove those
* devices and proceed with the remaining.
*/
if (dev->reg_state == NETREG_UNINITIALIZED) { //处理在注册过程中失败的设备
pr_debug("unregister_netdevice: device %s/%p never "
"was registered\n", dev->name, dev);
WARN_ON(1);
list_del(&dev->unreg_list);//将其从链表删除即可
continue;
}
BUG_ON(dev->reg_state != NETREG_REGISTERED); //程序到这里则设备不可能不处于已注册状态
}
/* If device is running, close it first. */
dev_close_many(head); //关闭在运行的设备
list_for_each_entry(dev, head, unreg_list) {
/* And unlink it from device chain. */
unlist_netdevice(dev);
/*
这里说明,只是将其从系统中的三个链表上移除
static void unlist_netdevice(struct net_device *dev)
{
ASSERT_RTNL();
/* Unlink dev from the device chain */
write_lock_bh(&dev_base_lock); //dev_base_lock是保证这三个链表互斥的读写锁
list_del_rcu(&dev->dev_list);
hlist_del_rcu(&dev->name_hlist);
hlist_del_rcu(&dev->index_hlist);
write_unlock_bh(&dev_base_lock);
}
*/
dev->reg_state = NETREG_UNREGISTERING; //然后,更新设备的注册状态
}
synchronize_net();
list_for_each_entry(dev, head, unreg_list) {
/* Shutdown queueing discipline. */
dev_shutdown(dev); //处理设备的接收队列等
/* Notify protocols, that we are about to destroy
this device. They should clean all the things.
*/
call_netdevice_notifiers(NETDEV_UNREGISTER, dev); //发出注销通知
if (!dev->rtnl_link_ops ||
dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);
/*
* Flush the unicast and multicast chains
*/
dev_uc_flush(dev);
dev_mc_flush(dev);
if (dev->netdev_ops->ndo_uninit) //处理私有数据区
dev->netdev_ops->ndo_uninit(dev);
/* Notifier chain MUST detach us from master device. */
WARN_ON(dev->master);
/* Remove entries from kobject tree */
netdev_unregister_kobject(dev); //从内核移除对象,涉及到内核设备管理层的东西
}
/* Process any work delayed until the end of the batch */
dev = list_first_entry(head, struct net_device, unreg_list);
call_netdevice_notifiers(NETDEV_UNREGISTER_BATCH, dev);
rcu_barrier();
list_for_each_entry(dev, head, unreg_list)
dev_put(dev); //释放设备,对其引用计数减一
}
这里在获取锁的时间范围内的注销操作就完成了,这时设备已经和内核的设备链脱离了关系,也就是内核已经不知道这个设备的存在了。但这个设备可以还被内核中的其他模块,因此,剩余的操作需要在释放了rtnl互斥量后,在net_run_todo函数中处理。
//在这里,释放了互斥量后,可以在等待设备的引用计数归零过程中睡眠
void netdev_run_todo(void)
{
struct list_head list;
/* Snapshot list, allow later requests */
list_replace_init(&net_todo_list, &list); //复制全局变量net_todo_list的值,然后初始化
__rtnl_unlock(); //释放互斥量
while (!list_empty(&list)) { //对链表中的元素进行处理
struct net_device *dev
= list_first_entry(&list, struct net_device, todo_list); //处理一个,移除一个
list_del(&dev->todo_list);
if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) { //内核出现重大BUG
printk(KERN_ERR "network todo '%s' but state %d\n",
dev->name, dev->reg_state);
dump_stack();
continue;
}
dev->reg_state = NETREG_UNREGISTERED; //更新注册状态
on_each_cpu(flush_backlog, dev, 1);
netdev_wait_allrefs(dev); //等待引用计数归零,可能睡眠
/* paranoia */
BUG_ON(netdev_refcnt_read(dev));
WARN_ON(rcu_dereference_raw(dev->ip_ptr));
WARN_ON(rcu_dereference_raw(dev->ip6_ptr));
WARN_ON(dev->dn_ptr);
if (dev->destructor)
dev->destructor(dev);
/* Free network device */
kobject_put(&dev->dev.kobj); //释放这个结构,到这里设备的注销完全完成,net_device结构将被释放
}
}
其实查看netdev_wait_allrefs函数,就是定时查看设备的引用计数是否为0,不为0则再次向其他模块发设备注销通知,让它们释放这个设备,然后进入休眠等待的过程。
static void netdev_wait_allrefs(struct net_device *dev)
{
unsigned long rebroadcast_time, warning_time;
int refcnt;
linkwatch_forget_dev(dev);
rebroadcast_time = warning_time = jiffies;
refcnt = netdev_refcnt_read(dev);
while (refcnt != 0) {
if (time_after(jiffies, rebroadcast_time + 1 * HZ)) { //过一秒
rtnl_lock();
/* Rebroadcast unregister notification */
call_netdevice_notifiers(NETDEV_UNREGISTER, dev); //发注销广播通知
/* don't resend NETDEV_UNREGISTER_BATCH, _BATCH users
* should have already handle it the first time */
if (test_bit(__LINK_STATE_LINKWATCH_PENDING,
&dev->state)) {
/* We must not have linkwatch events
* pending on unregister. If this
* happens, we simply run the queue
* unscheduled, resulting in a noop
* for this device.
*/
linkwatch_run_queue();
}
__rtnl_unlock();
rebroadcast_time = jiffies;
}
msleep(250); //睡眠250毫秒
refcnt = netdev_refcnt_read(dev); //夺取引用计数
if (time_after(jiffies, warning_time + 10 * HZ)) { //等待10秒
printk(KERN_EMERG "unregister_netdevice: "
"waiting for %s to become free. Usage "
"count = %d\n",
dev->name, refcnt);
warning_time = jiffies;
}
}
}