Skip to content

Commit 64742cf

Browse files
Ye Bingregkh
authored andcommitted
nbd: Fix hungtask when nbd_config_put
[ Upstream commit e2daec4 ] I got follow issue: [ 247.381177] INFO: task kworker/u10:0:47 blocked for more than 120 seconds. [ 247.382644] Not tainted 4.19.90-dirty raspberrypi#140 [ 247.383502] "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message. [ 247.385027] Call Trace: [ 247.388384] schedule+0xb8/0x3c0 [ 247.388966] schedule_timeout+0x2b4/0x380 [ 247.392815] wait_for_completion+0x367/0x510 [ 247.397713] flush_workqueue+0x32b/0x1340 [ 247.402700] drain_workqueue+0xda/0x3c0 [ 247.403442] destroy_workqueue+0x7b/0x690 [ 247.405014] nbd_config_put.cold+0x2f9/0x5b6 [ 247.405823] recv_work+0x1fd/0x2b0 [ 247.406485] process_one_work+0x70b/0x1610 [ 247.407262] worker_thread+0x5a9/0x1060 [ 247.408699] kthread+0x35e/0x430 [ 247.410918] ret_from_fork+0x1f/0x30 We can reproduce issue as follows: 1. Inject memory fault in nbd_start_device -1244,10 +1248,18 @@ static int nbd_start_device(struct nbd_device *nbd) nbd_dev_dbg_init(nbd); for (i = 0; i < num_connections; i++) { struct recv_thread_args *args; - - args = kzalloc(sizeof(*args), GFP_KERNEL); + + if (i == 1) { + args = NULL; + printk("%s: inject malloc error\n", __func__); + } + else + args = kzalloc(sizeof(*args), GFP_KERNEL); 2. Inject delay in recv_work -757,6 +760,8 @@ static void recv_work(struct work_struct *work) blk_mq_complete_request(blk_mq_rq_from_pdu(cmd)); } + printk("%s: comm=%s pid=%d\n", __func__, current->comm, current->pid); + mdelay(5 * 1000); nbd_config_put(nbd); atomic_dec(&config->recv_threads); wake_up(&config->recv_wq); 3. Create nbd server nbd-server 8000 /tmp/disk 4. Create nbd client nbd-client localhost 8000 /dev/nbd1 Then will trigger above issue. Reason is when add delay in recv_work, lead to release the last reference of 'nbd->config_refs'. nbd_config_put will call flush_workqueue to make all work finish. Obviously, it will lead to deadloop. To solve this issue, according to Josef's suggestion move 'recv_work' init from start device to nbd_dev_add, then destroy 'recv_work'when nbd device teardown. Signed-off-by: Ye Bin <yebin10@huawei.com> Reviewed-by: Josef Bacik <josef@toxicpanda.com> Link: https://lore.kernel.org/r/20211102015237.2309763-5-yebin10@huawei.com Signed-off-by: Jens Axboe <axboe@kernel.dk> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 694b5a3 commit 64742cf

File tree

1 file changed

+16
-20
lines changed

1 file changed

+16
-20
lines changed

drivers/block/nbd.c

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ static void nbd_dev_remove(struct nbd_device *nbd)
254254
mutex_lock(&nbd_index_mutex);
255255
idr_remove(&nbd_index_idr, nbd->index);
256256
mutex_unlock(&nbd_index_mutex);
257-
257+
destroy_workqueue(nbd->recv_workq);
258258
kfree(nbd);
259259
}
260260

@@ -1260,10 +1260,6 @@ static void nbd_config_put(struct nbd_device *nbd)
12601260
kfree(nbd->config);
12611261
nbd->config = NULL;
12621262

1263-
if (nbd->recv_workq)
1264-
destroy_workqueue(nbd->recv_workq);
1265-
nbd->recv_workq = NULL;
1266-
12671263
nbd->tag_set.timeout = 0;
12681264
nbd->disk->queue->limits.discard_granularity = 0;
12691265
nbd->disk->queue->limits.discard_alignment = 0;
@@ -1292,14 +1288,6 @@ static int nbd_start_device(struct nbd_device *nbd)
12921288
return -EINVAL;
12931289
}
12941290

1295-
nbd->recv_workq = alloc_workqueue("knbd%d-recv",
1296-
WQ_MEM_RECLAIM | WQ_HIGHPRI |
1297-
WQ_UNBOUND, 0, nbd->index);
1298-
if (!nbd->recv_workq) {
1299-
dev_err(disk_to_dev(nbd->disk), "Could not allocate knbd recv work queue.\n");
1300-
return -ENOMEM;
1301-
}
1302-
13031291
blk_mq_update_nr_hw_queues(&nbd->tag_set, config->num_connections);
13041292
nbd->pid = task_pid_nr(current);
13051293

@@ -1725,6 +1713,15 @@ static struct nbd_device *nbd_dev_add(int index, unsigned int refs)
17251713
}
17261714
nbd->disk = disk;
17271715

1716+
nbd->recv_workq = alloc_workqueue("nbd%d-recv",
1717+
WQ_MEM_RECLAIM | WQ_HIGHPRI |
1718+
WQ_UNBOUND, 0, nbd->index);
1719+
if (!nbd->recv_workq) {
1720+
dev_err(disk_to_dev(nbd->disk), "Could not allocate knbd recv work queue.\n");
1721+
err = -ENOMEM;
1722+
goto out_err_disk;
1723+
}
1724+
17281725
/*
17291726
* Tell the block layer that we are not a rotational device
17301727
*/
@@ -1755,7 +1752,7 @@ static struct nbd_device *nbd_dev_add(int index, unsigned int refs)
17551752
disk->first_minor = index << part_shift;
17561753
if (disk->first_minor < index || disk->first_minor > MINORMASK) {
17571754
err = -EINVAL;
1758-
goto out_err_disk;
1755+
goto out_free_work;
17591756
}
17601757

17611758
disk->minors = 1 << part_shift;
@@ -1764,7 +1761,7 @@ static struct nbd_device *nbd_dev_add(int index, unsigned int refs)
17641761
sprintf(disk->disk_name, "nbd%d", index);
17651762
err = add_disk(disk);
17661763
if (err)
1767-
goto out_err_disk;
1764+
goto out_free_work;
17681765

17691766
/*
17701767
* Now publish the device.
@@ -1773,6 +1770,8 @@ static struct nbd_device *nbd_dev_add(int index, unsigned int refs)
17731770
nbd_total_devices++;
17741771
return nbd;
17751772

1773+
out_free_work:
1774+
destroy_workqueue(nbd->recv_workq);
17761775
out_err_disk:
17771776
blk_cleanup_disk(disk);
17781777
out_free_idr:
@@ -2028,13 +2027,10 @@ static void nbd_disconnect_and_put(struct nbd_device *nbd)
20282027
nbd_disconnect(nbd);
20292028
sock_shutdown(nbd);
20302029
/*
2031-
* Make sure recv thread has finished, so it does not drop the last
2032-
* config ref and try to destroy the workqueue from inside the work
2033-
* queue. And this also ensure that we can safely call nbd_clear_que()
2030+
* Make sure recv thread has finished, we can safely call nbd_clear_que()
20342031
* to cancel the inflight I/Os.
20352032
*/
2036-
if (nbd->recv_workq)
2037-
flush_workqueue(nbd->recv_workq);
2033+
flush_workqueue(nbd->recv_workq);
20382034
nbd_clear_que(nbd);
20392035
nbd->task_setup = NULL;
20402036
mutex_unlock(&nbd->config_lock);

0 commit comments

Comments
 (0)