Skip to content

Commit e04e7a7

Browse files
dcuidavem330
authored andcommitted
hv_netvsc: Fix a deadlock by getting rtnl lock earlier in netvsc_probe()
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]>
1 parent 9ad716b commit e04e7a7

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
@@ -2206,6 +2206,16 @@ static int netvsc_probe(struct hv_device *dev,
22062206

22072207
memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN);
22082208

2209+
/* We must get rtnl lock before scheduling nvdev->subchan_work,
2210+
* otherwise netvsc_subchan_work() can get rtnl lock first and wait
2211+
* all subchannels to show up, but that may not happen because
2212+
* netvsc_probe() can't get rtnl lock and as a result vmbus_onoffer()
2213+
* -> ... -> device_add() -> ... -> __device_attach() can't get
2214+
* the device lock, so all the subchannels can't be processed --
2215+
* finally netvsc_subchan_work() hangs for ever.
2216+
*/
2217+
rtnl_lock();
2218+
22092219
if (nvdev->num_chn > 1)
22102220
schedule_work(&nvdev->subchan_work);
22112221

@@ -2224,7 +2234,6 @@ static int netvsc_probe(struct hv_device *dev,
22242234
else
22252235
net->max_mtu = ETH_DATA_LEN;
22262236

2227-
rtnl_lock();
22282237
ret = register_netdevice(net);
22292238
if (ret != 0) {
22302239
pr_err("Unable to register netdev.\n");

0 commit comments

Comments
 (0)