Commit 0de74d1c authored by Jakub Kicinski's avatar Jakub Kicinski Committed by Zheng Zengkai
Browse files

net: allow out-of-order netdev unregistration

net-next inclusion
from net-next-v5.17-rc5
commit faab39f6
category: bugfix
bugzilla: https://gitee.com/openeuler/kernel/issues/I4VZN0?from=project-issue

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/netdev/net-next.git/commit/?id=faab39f63c1fc4bcdf135690f03bd596b578c67e



--------------------------------

Sprinkle for each loops to allow netdevices to be unregistered
out of order, as their refs are released.

This prevents problems caused by dependencies between netdevs
which want to release references in their ->priv_destructor.
See commit d6ff94af ("vlan: move dev_put into vlan_dev_uninit")
for example.

Eric has removed the only known ordering requirement in
commit c002496b ("Merge branch 'ipv6-loopback'")
so let's try this and see if anything explodes...

Reviewed-by: default avatarEric Dumazet <edumazet@google.com>
Reviewed-by: default avatarXin Long <lucien.xin@gmail.com>
Link: https://lore.kernel.org/r/20220215225310.3679266-2-kuba@kernel.org


Signed-off-by: default avatarJakub Kicinski <kuba@kernel.org>
Conflicts:
	net/core/dev.c
Signed-off-by: default avatarZiyang Xuan <william.xuanziyang@huawei.com>
Reviewed-by: default avatarWei Yongjun <weiyongjun1@huawei.com>
Signed-off-by: default avatarZheng Zengkai <zhengzengkai@huawei.com>
parent 0f7438db
Loading
Loading
Loading
Loading
+34 −25
Original line number Diff line number Diff line
@@ -10244,8 +10244,8 @@ EXPORT_SYMBOL(netdev_refcnt_read);
#define WAIT_REFS_MIN_MSECS 1
#define WAIT_REFS_MAX_MSECS 250
/**
 * netdev_wait_allrefs - wait until all references are gone.
 * @dev: target net_device
 * netdev_wait_allrefs_any - wait until all references are gone.
 * @list: list of net_devices to wait on
 *
 * This is called when unregistering network devices.
 *
@@ -10255,27 +10255,34 @@ EXPORT_SYMBOL(netdev_refcnt_read);
 * We can get stuck here if buggy protocols don't correctly
 * call dev_put.
 */
static void netdev_wait_allrefs(struct net_device *dev)
static struct net_device *netdev_wait_allrefs_any(struct list_head *list)
{
	unsigned long rebroadcast_time, warning_time;
	int wait = 0, refcnt;
	struct net_device *dev;
	int wait = 0;

	list_for_each_entry(dev, list, todo_list)
		linkwatch_forget_dev(dev);

	rebroadcast_time = warning_time = jiffies;
	refcnt = netdev_refcnt_read(dev);

	while (refcnt != 0) {
	list_for_each_entry(dev, list, todo_list)
		if (netdev_refcnt_read(dev) == 0)
			return dev;

	while (true) {
		if (time_after(jiffies, rebroadcast_time + 1 * HZ)) {
			rtnl_lock();

			/* Rebroadcast unregister notification */
			list_for_each_entry(dev, list, todo_list)
				call_netdevice_notifiers(NETDEV_UNREGISTER, dev);

			__rtnl_unlock();
			rcu_barrier();
			rtnl_lock();

			list_for_each_entry(dev, list, todo_list)
				if (test_bit(__LINK_STATE_LINKWATCH_PENDING,
					     &dev->state)) {
					/* We must not have linkwatch events
@@ -10285,6 +10292,7 @@ static void netdev_wait_allrefs(struct net_device *dev)
					 * for this device.
					 */
					linkwatch_run_queue();
					break;
				}

			__rtnl_unlock();
@@ -10300,11 +10308,14 @@ static void netdev_wait_allrefs(struct net_device *dev)
			wait = min(wait << 1, WAIT_REFS_MAX_MSECS);
		}

		refcnt = netdev_refcnt_read(dev);
		list_for_each_entry(dev, list, todo_list)
			if (netdev_refcnt_read(dev) == 0)
				return dev;

		if (refcnt && time_after(jiffies, warning_time + 10 * HZ)) {
		if (time_after(jiffies, warning_time + 10 * HZ)) {
			list_for_each_entry(dev, list, todo_list)
				pr_emerg("unregister_netdevice: waiting for %s to become free. Usage count = %d\n",
				 dev->name, refcnt);
					 dev->name, netdev_refcnt_read(dev));
			warning_time = jiffies;
		}
	}
@@ -10372,11 +10383,9 @@ void netdev_run_todo(void)
	}

	while (!list_empty(&list)) {
		dev = list_first_entry(&list, struct net_device, todo_list);
		dev = netdev_wait_allrefs_any(&list);
		list_del(&dev->todo_list);

		netdev_wait_allrefs(dev);

		/* paranoia */
		BUG_ON(netdev_refcnt_read(dev));
		BUG_ON(!list_empty(&dev->ptype_all));