Skip to contentSkip to conversation

net: core: Fix the loop in default_device_exit_net()

PATCH In review Haiyang Zhang 5 participants 6 messages Jul 18, 2025 – Jul 22, 2025

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: core: Fix the loop in default_device_exit_net()

From: Haiyang Zhang <[email protected]>

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.

---
 net/core/dev.c | 31 +++++++++++++++++++++----------
 1 file changed, 21 insertions(+), 10 deletions(-)
net/core/dev.c +21 -10
index 621a639aeba1..d83f5f12cf70 100644
@@ -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))
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 7 lines · On Fri, 18 Jul 2025 13:20:14 -0700 Haiyang Zhang wrote:

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.

FTR I think that what the driver is trying to do is way too hacky, and it should be fixed instead. But I defer to Kuniyuki for the final word, maybe this change is useful for other reasons..

#2 lore raw
Quoted 6 lines · Jakub Kicinski
Quoted 4 lines · On Fri, 18 Jul 2025 13:20:14 -0700 Haiyang Zhang wrote:

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.

Quoted 7 lines
Quoted 3 lines

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.

FTR I think that what the driver is trying to do is way too hacky, and it should be fixed instead. But I defer to Kuniyuki for the final word, maybe this change is useful for other reasons..

I agree that it should be fixed on the driver side. I don't think of a good reason for the change.

#4 lore raw
In reply to Haiyang Zhang
Quoted 7 lines · Haiyang Zhang

From: Haiyang Zhang <[email protected]>

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.

Quoted 5 lines

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.

We (network maintainers) prefer a Fixes: tag, so that we can look at the blamed patch, rather than trusting your '5.4' hint.

Without a Fixes tag, you are forcing each reviewer to do the archeology work, and possibly completely miss your point.

maintainer
#5 lore raw
Quoted 32 lines

-----Original Message-----
From: Eric Dumazet <[email protected]>
Sent: Tuesday, July 22, 2025 2:52 AM
To: Haiyang Zhang <[email protected]>
Cc: [email protected]; [email protected]; Haiyang Zhang <[email protected]>; KY Srinivasan <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; linux- [email protected]; [email protected]; #@linux.microsoft.com; [email protected]
Subject: [EXTERNAL] Re: [PATCH net] net: core: Fix the loop in default_device_exit_net()

Quoted 4 lines · Haiyang Zhang

From: Haiyang Zhang <[email protected]>

The loop in default_device_exit_net() won't be able to properly detect

the

Quoted 1 line

head then stop, and will hit NULL pointer, when a driver, like

hv_netvsc,

Quoted 4 lines

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

Quoted 2 lines

in the last iteration.

Quoted 7 lines
Quoted 1 line

We (network maintainers) prefer a Fixes: tag, so that we can look at the blamed patch, rather than trusting your '5.4' hint.

Without a Fixes tag, you are forcing each reviewer to do the archeology work, and possibly completely miss your point.

Thanks. I will have the Fixes tag in the new patch.

- Haiyang

maintainer
#3 lore raw
In reply to Kuniyuki Iwashima
Quoted 32 lines

-----Original Message-----
From: Kuniyuki Iwashima <[email protected]>
Sent: Saturday, July 19, 2025 4:48 PM
To: Jakub Kicinski <[email protected]>
Cc: Haiyang Zhang <[email protected]>; linux- [email protected]; [email protected]; Haiyang Zhang <[email protected]>; KY Srinivasan <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; linux- [email protected]
Subject: [EXTERNAL] Re: [PATCH net] net: core: Fix the loop in default_device_exit_net()

[You don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

Quoted 3 lines · Jakub Kicinski
Quoted 1 line · On Fri, 18 Jul 2025 13:20:14 -0700 Haiyang Zhang wrote:

The loop in default_device_exit_net() won't be able to properly detect

the

Quoted 1 line
Quoted 1 line

head then stop, and will hit NULL pointer, when a driver, like

hv_netvsc,

Quoted 3 lines
Quoted 3 lines

automatically moves the slave device together with the master device.

To fix this, add a helper function to return the first migratable

netdev

Quoted 1 line
Quoted 1 line

correctly, no matter one or two devices were removed from this net's

list

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

in the last iteration.

FTR I think that what the driver is trying to do is way too hacky, and it should be fixed instead. But I defer to Kuniyuki for the final word, maybe this change is useful for other reasons..

I agree that it should be fixed on the driver side. I don't think of a good reason for the change.

Kuniyuki and Jakub:

Thanks for the reviews. I'm working on a patch that will fix the driver side.

- Haiyang