public inbox for netdev@vger.kernel.org 
 help / color / mirror / Atom feed
From: Haiyang Zhang <haiyangz@linux•microsoft.com>
To: linux-hyperv@vger•kernel.org, netdev@vger•kernel.org
Cc: haiyangz@microsoft•com, kys@microsoft•com, wei.liu@kernel•org,
	edumazet@google•com, kuba@kernel•org, pabeni@redhat•com,
	horms@kernel•org, davem@davemloft•net, sdf@fomichev•me,
	kuniyu@google•com, ahmed.zaki@intel•com,
	aleksander.lobakin@intel•com, linux-kernel@vger•kernel.org,
	stable@vger•kernel.org, #@linux.microsoft.com,
	5.4+@linux•microsoft.com
Subject: [PATCH net] net: core: Fix the loop in default_device_exit_net()
Date: Fri, 18 Jul 2025 13:20:14 -0700	[thread overview]
Message-ID: <1752870014-28909-1-git-send-email-haiyangz@linux.microsoft.com> (raw)

From: Haiyang Zhang <haiyangz@microsoft•com>

The loop in default_device_exit_net() won't be able to properly detect the
head then stop, and will hit NULL pointer, when a driver, like hv_netvsc,
automatically moves the slave device together with the master device.

To fix this, add a helper function to return the first migratable netdev
correctly, no matter one or two devices were removed from this net's list
in the last iteration.

Cc: stable@vger•kernel.org # 5.4+
Signed-off-by: Haiyang Zhang <haiyangz@microsoft•com>
---
 net/core/dev.c | 31 +++++++++++++++++++++----------
 1 file changed, 21 insertions(+), 10 deletions(-)

diff --git a/net/core/dev.c b/net/core/dev.c
index 621a639aeba1..d83f5f12cf70 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -12629,19 +12629,11 @@ static struct pernet_operations __net_initdata netdev_net_ops = {
 	.exit = netdev_exit,
 };
 
-static void __net_exit default_device_exit_net(struct net *net)
+static inline struct net_device *first_migratable_netdev(struct net *net)
 {
-	struct netdev_name_node *name_node, *tmp;
 	struct net_device *dev, *aux;
-	/*
-	 * Push all migratable network devices back to the
-	 * initial network namespace
-	 */
-	ASSERT_RTNL();
-	for_each_netdev_safe(net, dev, aux) {
-		int err;
-		char fb_name[IFNAMSIZ];
 
+	for_each_netdev_safe(net, dev, aux) {
 		/* Ignore unmoveable devices (i.e. loopback) */
 		if (dev->netns_immutable)
 			continue;
@@ -12650,6 +12642,25 @@ static void __net_exit default_device_exit_net(struct net *net)
 		if (dev->rtnl_link_ops && !dev->rtnl_link_ops->netns_refund)
 			continue;
 
+		return dev;
+	}
+
+	return NULL;
+}
+
+static void __net_exit default_device_exit_net(struct net *net)
+{
+	struct netdev_name_node *name_node, *tmp;
+	struct net_device *dev;
+	/*
+	 * Push all migratable network devices back to the
+	 * initial network namespace
+	 */
+	ASSERT_RTNL();
+	while ((dev = first_migratable_netdev(net)) != NULL) {
+		int err;
+		char fb_name[IFNAMSIZ];
+
 		/* Push remaining network devices to init_net */
 		snprintf(fb_name, IFNAMSIZ, "dev%d", dev->ifindex);
 		if (netdev_name_in_use(&init_net, fb_name))
-- 
2.34.1


             reply	other threads:[~2025-07-18 20:21 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-18 20:20 Haiyang Zhang [this message]
2025-07-18 23:37 ` [PATCH net] net: core: Fix the loop in default_device_exit_net() Jakub Kicinski
2025-07-19 20:47   ` Kuniyuki Iwashima
2025-07-22 16:20     ` [EXTERNAL] " Haiyang Zhang
2025-07-22  6:51 ` Eric Dumazet
2025-07-22 16:15   ` [EXTERNAL] " Haiyang Zhang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1752870014-28909-1-git-send-email-haiyangz@linux.microsoft.com \
    --to=haiyangz@linux$(echo .)microsoft.com \
    --cc=#@linux.microsoft.com \
    --cc=5.4+@linux$(echo .)microsoft.com \
    --cc=ahmed.zaki@intel$(echo .)com \
    --cc=aleksander.lobakin@intel$(echo .)com \
    --cc=davem@davemloft$(echo .)net \
    --cc=edumazet@google$(echo .)com \
    --cc=haiyangz@microsoft$(echo .)com \
    --cc=horms@kernel$(echo .)org \
    --cc=kuba@kernel$(echo .)org \
    --cc=kuniyu@google$(echo .)com \
    --cc=kys@microsoft$(echo .)com \
    --cc=linux-hyperv@vger$(echo .)kernel.org \
    --cc=linux-kernel@vger$(echo .)kernel.org \
    --cc=netdev@vger$(echo .)kernel.org \
    --cc=pabeni@redhat$(echo .)com \
    --cc=sdf@fomichev$(echo .)me \
    --cc=stable@vger$(echo .)kernel.org \
    --cc=wei.liu@kernel$(echo .)org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox