Skip to content

Commit 8aa0762

Browse files
dcuigregkh
authored andcommitted
hv_netvsc: Fix a deadlock by getting rtnl lock earlier in netvsc_probe()
[ Upstream commit e04e7a7 ] This patch fixes the race between netvsc_probe() and rndis_set_subchannel(), which can cause a deadlock. These are the related 3 paths which show the deadlock: path #1: Workqueue: hv_vmbus_con vmbus_onmessage_work [hv_vmbus] Call Trace: schedule schedule_preempt_disabled __mutex_lock __device_attach bus_probe_device device_add vmbus_device_register vmbus_onoffer vmbus_onmessage_work process_one_work worker_thread kthread ret_from_fork path #2: schedule schedule_preempt_disabled __mutex_lock netvsc_probe vmbus_probe really_probe __driver_attach bus_for_each_dev driver_attach_async async_run_entry_fn process_one_work worker_thread kthread ret_from_fork path #3: Workqueue: events netvsc_subchan_work [hv_netvsc] Call Trace: schedule rndis_set_subchannel netvsc_subchan_work process_one_work worker_thread kthread ret_from_fork Before path #1 finishes, path #2 can start to run, because just before the "bus_probe_device(dev);" in device_add() in path #1, there is a line "object_uevent(&dev->kobj, KOBJ_ADD);", so systemd-udevd can immediately try to load hv_netvsc and hence path #2 can start to run. Next, path #2 offloads the subchannal's initialization to a workqueue, i.e. path #3, so we can end up in a deadlock situation like this: Path #2 gets the device lock, and is trying to get the rtnl lock; Path #3 gets the rtnl lock and is waiting for all the subchannel messages to be processed; Path #1 is trying to get the device lock, but since #2 is not releasing the device lock, path #1 has to sleep; since the VMBus messages are processed one by one, this means the sub-channel messages can't be procedded, so #3 has to sleep with the rtnl lock held, and finally #2 has to sleep... Now all the 3 paths are sleeping and we hit the deadlock. With the patch, we can make sure #2 gets both the device lock and the rtnl lock together, gets its job done, and releases the locks, so #1 and #3 will not be blocked for ever. Fixes: 8195b13 ("hv_netvsc: fix deadlock on hotplug") Signed-off-by: Dexuan Cui <[email protected]> Cc: Stephen Hemminger <[email protected]> Cc: K. Y. Srinivasan <[email protected]> Cc: Haiyang Zhang <[email protected]> Signed-off-by: David S. Miller <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent dadb011 commit 8aa0762

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

drivers/net/hyperv/netvsc_drv.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2044,6 +2044,16 @@ static int netvsc_probe(struct hv_device *dev,
20442044

20452045
memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN);
20462046

2047+
/* We must get rtnl lock before scheduling nvdev->subchan_work,
2048+
* otherwise netvsc_subchan_work() can get rtnl lock first and wait
2049+
* all subchannels to show up, but that may not happen because
2050+
* netvsc_probe() can't get rtnl lock and as a result vmbus_onoffer()
2051+
* -> ... -> device_add() -> ... -> __device_attach() can't get
2052+
* the device lock, so all the subchannels can't be processed --
2053+
* finally netvsc_subchan_work() hangs for ever.
2054+
*/
2055+
rtnl_lock();
2056+
20472057
if (nvdev->num_chn > 1)
20482058
schedule_work(&nvdev->subchan_work);
20492059

@@ -2062,7 +2072,6 @@ static int netvsc_probe(struct hv_device *dev,
20622072
else
20632073
net->max_mtu = ETH_DATA_LEN;
20642074

2065-
rtnl_lock();
20662075
ret = register_netdevice(net);
20672076
if (ret != 0) {
20682077
pr_err("Unable to register netdev.\n");

0 commit comments

Comments
 (0)