Skip to contentSkip to conversation

hv_netvsc: fix race of netvsc, VF register, and slave bit

PATCH Superseded Haiyang Zhang 4 participants 10 messages Nov 10, 2023 – Nov 15, 2023

Subsystems (5)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Haiyang Zhang

author maintainer
#0 lore raw

v4 cover hv_netvsc: fix race of netvsc, VF register, and slave bit

There are some races between netvsc probe, set notifier, VF register, and slave bit setting.
This patch set fixes them.

Haiyang Zhang (2):
hv_netvsc: fix race of netvsc and VF register_netdevice
hv_netvsc: Fix race of register_netdevice_notifier and VF register

Long Li (1):
hv_netvsc: Mark VF as slave before exposing it to user-mode

 drivers/net/hyperv/netvsc_drv.c | 64 ++++++++++++++++++++++-----------
 1 file changed, 43 insertions(+), 21 deletions(-)
Signature · 1 line
2.25.1
author maintainer
#1 lore raw

PATCH v4 1/3 hv_netvsc: fix race of netvsc and VF register_netdevice

The rtnl lock also needs to be held before rndis_filter_device_add() which advertises nvsp_2_vsc_capability / sriov bit, and triggers VF NIC offering and registering. If VF NIC finished register_netdev() earlier it may cause name based config failure.

To fix this issue, move the call to rtnl_lock() before rndis_filter_device_add(), so VF will be registered later than netvsc / synthetic NIC, and gets a name numbered (ethX) after netvsc.

Fixes e04e7a7bbd4b ("hv_netvsc: Fix a deadlock by getting rtnl lock earlier in netvsc_probe()")

---
v3:
Divide it into two patches, suggested by Jakub Kicinski.
v2:
Fix rtnl_unlock() in error handling as found by Wojciech Drewek.

---
 drivers/net/hyperv/netvsc_drv.c | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)
drivers/net/hyperv/netvsc_drv.c +15 -10
index 3ba3c8fb28a5..5e528a76f5f5 100644
@@ -2531,15 +2531,6 @@ static int netvsc_probe(struct hv_device *dev,
 		goto devinfo_failed;
 	}
 
-	nvdev = rndis_filter_device_add(dev, device_info);
-	if (IS_ERR(nvdev)) {
-		ret = PTR_ERR(nvdev);
-		netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
-		goto rndis_failed;
-	}
-
-	eth_hw_addr_set(net, device_info->mac_adr);
-
 	/* We must get rtnl lock before scheduling nvdev->subchan_work,
 	 * otherwise netvsc_subchan_work() can get rtnl lock first and wait
 	 * all subchannels to show up, but that may not happen because
@@ -2547,9 +2538,23 @@ static int netvsc_probe(struct hv_device *dev,
 	 * -> ... -> device_add() -> ... -> __device_attach() can't get
 	 * the device lock, so all the subchannels can't be processed --
 	 * finally netvsc_subchan_work() hangs forever.
+	 *
+	 * The rtnl lock also needs to be held before rndis_filter_device_add()
+	 * which advertises nvsp_2_vsc_capability / sriov bit, and triggers
+	 * VF NIC offering and registering. If VF NIC finished register_netdev()
+	 * earlier it may cause name based config failure.
 	 */
 	rtnl_lock();
 
+	nvdev = rndis_filter_device_add(dev, device_info);
+	if (IS_ERR(nvdev)) {
+		ret = PTR_ERR(nvdev);
+		netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
+		goto rndis_failed;
+	}
+
+	eth_hw_addr_set(net, device_info->mac_adr);
+
 	if (nvdev->num_chn > 1)
 		schedule_work(&nvdev->subchan_work);
 
@@ -2586,9 +2591,9 @@ static int netvsc_probe(struct hv_device *dev,
 	return 0;
 
 register_failed:
-	rtnl_unlock();
 	rndis_filter_device_remove(dev, nvdev);
 rndis_failed:
+	rtnl_unlock();
 	netvsc_devinfo_put(device_info);
 devinfo_failed:
 	free_percpu(net_device_ctx->vf_stats);
Signature · 1 line
2.25.1
author maintainer
#4 lore raw

PATCH v4 2/3 hv_netvsc: Fix race of register_netdevice_notifier and VF register

In reply to Haiyang Zhang

If VF NIC is registered earlier, NETDEV_REGISTER event is replayed, but NETDEV_POST_INIT is not.

Move register_netdevice_notifier() earlier, so the call back function is set before probing.

Fixes e04e7a7bbd4b ("hv_netvsc: Fix a deadlock by getting rtnl lock earlier in netvsc_probe()")

---
v3:
Divide it into two patches, suggested by Jakub Kicinski.
v2:
Fix rtnl_unlock() in error handling as found by Wojciech Drewek.

---
 drivers/net/hyperv/netvsc_drv.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/net/hyperv/netvsc_drv.c +5 -2
index 5e528a76f5f5..1d1491da303b 100644
@@ -2793,11 +2793,14 @@ static int __init netvsc_drv_init(void)
 	}
 	netvsc_ring_bytes = ring_size * PAGE_SIZE;
 
+	register_netdevice_notifier(&netvsc_netdev_notifier);
+
 	ret = vmbus_driver_register(&netvsc_drv);
-	if (ret)
+	if (ret) {
+		unregister_netdevice_notifier(&netvsc_netdev_notifier);
 		return ret;
+	}
 
-	register_netdevice_notifier(&netvsc_netdev_notifier);
 	return 0;
 }
 
Signature · 1 line
2.25.1
author maintainer
#8 lore raw

PATCH v4 3/3 hv_netvsc: Mark VF as slave before exposing it to user-mode

In reply to Haiyang Zhang

From: Long Li <[email protected]>

When a VF is being exposed form the kernel, it should be marked as "slave" before exposing to the user-mode. The VF is not usable without netvsc running as master. The user-mode should never see a VF without the "slave" flag.

This commit moves the code of setting the slave flag to the time before VF is exposed to user-mode.

Fixes 0c195567a8f6 ("netvsc: transparent VF management")

---
v4:
Add comments in get_netvsc_byslot() explaining the need to check dev_addr v2:
Use a new function to handle NETDEV_POST_INIT.

---
 drivers/net/hyperv/netvsc_drv.c | 32 +++++++++++++++++++++++---------
 1 file changed, 23 insertions(+), 9 deletions(-)
drivers/net/hyperv/netvsc_drv.c +23 -9
index 1d1491da303b..52dbf1de7347 100644
@@ -2206,9 +2206,6 @@ static int netvsc_vf_join(struct net_device *vf_netdev,
 		goto upper_link_failed;
 	}
 
-	/* set slave flag before open to prevent IPv6 addrconf */
-	vf_netdev->flags |= IFF_SLAVE;
-
 	schedule_delayed_work(&ndev_ctx->vf_takeover, VF_TAKEOVER_INT);
 
 	call_netdevice_notifiers(NETDEV_JOIN, vf_netdev);
@@ -2315,16 +2312,18 @@ static struct net_device *get_netvsc_byslot(const struct net_device *vf_netdev)
 
 	}
 
-	/* Fallback path to check synthetic vf with
-	 * help of mac addr
+	/* Fallback path to check synthetic vf with help of mac addr.
+	 * Because this function can be called before vf_netdev is
+	 * initialized (NETDEV_POST_INIT) when its perm_addr has not been copied
+	 * from dev_addr, also try to match to its dev_addr.
+	 * Note: On Hyper-V and Azure, it's not possible to set a MAC address
+	 * on a VF that matches to the MAC of a unrelated NETVSC device.
 	 */
 	list_for_each_entry(ndev_ctx, &netvsc_dev_list, list) {
 		ndev = hv_get_drvdata(ndev_ctx->device_ctx);
-		if (ether_addr_equal(vf_netdev->perm_addr, ndev->perm_addr)) {
-			netdev_notice(vf_netdev,
-				      "falling back to mac addr based matching\n");
+		if (ether_addr_equal(vf_netdev->perm_addr, ndev->perm_addr) ||
+		    ether_addr_equal(vf_netdev->dev_addr, ndev->perm_addr))
 			return ndev;
-		}
 	}
 
 	netdev_notice(vf_netdev,
@@ -2332,6 +2331,19 @@ static struct net_device *get_netvsc_byslot(const struct net_device *vf_netdev)
 	return NULL;
 }
 
+static int netvsc_prepare_slave(struct net_device *vf_netdev)
+{
+	struct net_device *ndev;
+
+	ndev = get_netvsc_byslot(vf_netdev);
+	if (!ndev)
+		return NOTIFY_DONE;
+
+	/* set slave flag before open to prevent IPv6 addrconf */
+	vf_netdev->flags |= IFF_SLAVE;
+	return NOTIFY_DONE;
+}
+
 static int netvsc_register_vf(struct net_device *vf_netdev)
 {
 	struct net_device_context *net_device_ctx;
@@ -2758,6 +2770,8 @@ static int netvsc_netdev_event(struct notifier_block *this,
 		return NOTIFY_DONE;
 
 	switch (event) {
+	case NETDEV_POST_INIT:
+		return netvsc_prepare_slave(event_dev);
 	case NETDEV_REGISTER:
 		return netvsc_register_vf(event_dev);
 	case NETDEV_UNREGISTER:
Signature · 1 line
2.25.1
reviewer
#5 lore raw

Re: [PATCH net,v4, 2/3] hv_netvsc: Fix race of register_netdevice_notifier and VF register

In reply to Haiyang Zhang
Quoted 31 lines · Haiyang Zhang

If VF NIC is registered earlier, NETDEV_REGISTER event is replayed, but NETDEV_POST_INIT is not.

Move register_netdevice_notifier() earlier, so the call back function is set before probing.

Fixes e04e7a7bbd4b ("hv_netvsc: Fix a deadlock by getting rtnl lock earlier in netvsc_probe()")

---
v3:
Divide it into two patches, suggested by Jakub Kicinski.
v2:
Fix rtnl_unlock() in error handling as found by Wojciech Drewek.

---
 drivers/net/hyperv/netvsc_drv.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/net/hyperv/netvsc_drv.c +2 -0
index 5e528a76f5f5..1d1491da303b 100644
@@ -2793,11 +2793,14 @@ static int __init netvsc_drv_init(void)
 	}
 	netvsc_ring_bytes = ring_size * PAGE_SIZE;
 
+	register_netdevice_notifier(&netvsc_netdev_notifier);
+

drivers/net/hyperv/netvsc_drv.c static int __init netvsc_drv_init(void) · line 2805

Quoted 10 lines
drivers/net/hyperv/netvsc_drv.c +3 -2
 	ret = vmbus_driver_register(&netvsc_drv);
-	if (ret)
+	if (ret) {
+		unregister_netdevice_notifier(&netvsc_netdev_notifier);
 		return ret;
+	}
 
-	register_netdevice_notifier(&netvsc_netdev_notifier);
 	return 0;
 }

Hi Haiyang Zhang,

functionally this change looks good to me, thanks!

I'm wondering if we could improve things slightly by using a more idiomatic form for the error path. Something like the following (completely untested!).

My reasoning is that this way things are less likely go to wrong if more error conditions are added to this function later.

	...

	register_netdevice_notifier(&netvsc_netdev_notifier);

	ret = vmbus_driver_register(&netvsc_drv);
	if (ret)
		goto err_unregister_netdevice_notifier;

	return 0;

err_unregister_netdevice_notifier:

	unregister_netdevice_notifier(&netvsc_netdev_notifier);
	return ret;
}
reviewer Reviewed-by on v4 1/3
#2 lore raw

Re: [PATCH net,v4, 1/3] hv_netvsc: fix race of netvsc and VF register_netdevice

In reply to Haiyang Zhang
Quoted 9 lines · Haiyang Zhang

The rtnl lock also needs to be held before rndis_filter_device_add() which advertises nvsp_2_vsc_capability / sriov bit, and triggers VF NIC offering and registering. If VF NIC finished register_netdev() earlier it may cause name based config failure.

To fix this issue, move the call to rtnl_lock() before rndis_filter_device_add(), so VF will be registered later than netvsc / synthetic NIC, and gets a name numbered (ethX) after netvsc.

Quoted 5 lines
Fixes e04e7a7bbd4b ("hv_netvsc: Fix a deadlock by getting rtnl lock earlier in netvsc_probe()")
maintainer Reviewed-by on v4 1/3
#3 lore raw

RE: [PATCH net,v4, 1/3] hv_netvsc: fix race of netvsc and VF register_netdevice

In reply to Haiyang Zhang
Quoted 13 lines

From: LKML haiyangz <[email protected]> On Behalf Of Haiyang Zhang
Sent: Friday, November 10, 2023 9:39 AM
[...]

The rtnl lock also needs to be held before rndis_filter_device_add() which advertises nvsp_2_vsc_capability / sriov bit, and triggers VF NIC offering and registering. If VF NIC finished register_netdev() earlier it may cause name based config failure.

To fix this issue, move the call to rtnl_lock() before rndis_filter_device_add(), so VF will be registered later than netvsc / synthetic NIC, and gets a name numbered (ethX) after netvsc.

Quoted 6 lines
Fixes e04e7a7bbd4b ("hv_netvsc: Fix a deadlock by getting rtnl lock earlier in

netvsc_probe()")

maintainer Reviewed-by on v4 2/3
#7 lore raw

RE: [PATCH net,v4, 2/3] hv_netvsc: Fix race of register_netdevice_notifier and VF register

In reply to Haiyang Zhang
Quoted 9 lines

From: LKML haiyangz <[email protected]> On Behalf Of Haiyang Zhang
Sent: Friday, November 10, 2023 9:39 AM
[...]
If VF NIC is registered earlier, NETDEV_REGISTER event is replayed, but NETDEV_POST_INIT is not.

Move register_netdevice_notifier() earlier, so the call back function is set before probing.

Quoted 6 lines
Fixes e04e7a7bbd4b ("hv_netvsc: Fix a deadlock by getting rtnl lock earlier in

netvsc_probe()")

It's better to post a new version that follows Simon Horman's suggestion, i.e., use a more idiomatic form for the error path.

author maintainer
#6 lore raw

RE: [PATCH net,v4, 2/3] hv_netvsc: Fix race of register_netdevice_notifier and VF register

In reply to Simon Horman
Quoted 79 lines

-----Original Message-----
From: Simon Horman <[email protected]>
Sent: Sunday, November 12, 2023 4:41 AM
To: Haiyang Zhang <[email protected]>
Cc: [email protected]; [email protected]; KY Srinivasan <[email protected]>; [email protected]; Dexuan Cui <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH net,v4, 2/3] hv_netvsc: Fix race of register_netdevice_notifier and VF register

Quoted 8 lines · Haiyang Zhang

If VF NIC is registered earlier, NETDEV_REGISTER event is replayed, but NETDEV_POST_INIT is not.

Move register_netdevice_notifier() earlier, so the call back function is set before probing.

Fixes e04e7a7bbd4b ("hv_netvsc: Fix a deadlock by getting rtnl lock earlier

in netvsc_probe()")

Quoted 9 lines

---
v3:
Divide it into two patches, suggested by Jakub Kicinski.
v2:
Fix rtnl_unlock() in error handling as found by Wojciech Drewek.

Review

Quoted 5 lines
---
 drivers/net/hyperv/netvsc_drv.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
(quoted diff) +0 -0

b/drivers/net/hyperv/netvsc_drv.c

Quoted 9 lines

index 5e528a76f5f5..1d1491da303b 100644

drivers/net/hyperv/netvsc_drv.c +2 -0
@@ -2793,11 +2793,14 @@ static int __init netvsc_drv_init(void)
 	}
 	netvsc_ring_bytes = ring_size * PAGE_SIZE;
 
+	register_netdevice_notifier(&netvsc_netdev_notifier);
+

drivers/net/hyperv/netvsc_drv.c static int __init netvsc_drv_init(void) · line 2805

Quoted 10 lines
drivers/net/hyperv/netvsc_drv.c +3 -2
 	ret = vmbus_driver_register(&netvsc_drv);
-	if (ret)
+	if (ret) {
+		unregister_netdevice_notifier(&netvsc_netdev_notifier);
 		return ret;
+	}
 
-	register_netdevice_notifier(&netvsc_netdev_notifier);
 	return 0;
 }

Hi Haiyang Zhang,

functionally this change looks good to me, thanks!

I'm wondering if we could improve things slightly by using a more idiomatic form for the error path. Something like the following (completely untested!).

My reasoning is that this way things are less likely go to wrong if more error conditions are added to this function later.

	...

	register_netdevice_notifier(&netvsc_netdev_notifier);

	ret = vmbus_driver_register(&netvsc_drv);
	if (ret)
		goto err_unregister_netdevice_notifier;

	return 0;
Quoted 4 lines

err_unregister_netdevice_notifier:

	unregister_netdevice_notifier(&netvsc_netdev_notifier);
	return ret;
}

Thanks for the suggested idiomatic form. I will update it.

- Haiyang

#9 lore raw

Re: [PATCH net,v4, 3/3] hv_netvsc: Mark VF as slave before exposing it to user-mode

In reply to Haiyang Zhang
Quoted 1 line · On Fri, 10 Nov 2023 06:39:00 -0800 Haiyang Zhang <[email protected]> wrote:

+static int netvsc_prepare_slave(struct net_device *vf_netdev)

It would be good to not introduce another instance of non-inclusive naming in network code. Please think of a better term. Can't change IFF_SLAVE but the rest could change.