linux/drivers/block
Ye Bin e2daec488c nbd: Fix hungtask when nbd_config_put
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 #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>
2021-11-02 10:50:27 -06:00
..
aoe block: aoe: fixup coccinelle warnings 2021-10-21 08:54:15 -06:00
drbd drbd: add error handling support for add_disk() 2021-10-18 14:41:36 -06:00
mtip32xx mtip32xx: Remove redundant 'flush_workqueue()' calls 2021-10-18 14:50:37 -06:00
null_blk null_blk: Fix handling of submit_queues and poll_queues attributes 2021-10-29 06:55:39 -06:00
paride pf: fix error codes in pf_init_unit() 2021-10-18 14:42:16 -06:00
rnbd rnbd: add error handling support for add_disk() 2021-10-21 09:00:56 -06:00
rsxx block/rsxx: add error handling support for add_disk() 2021-10-18 14:41:36 -06:00
xen-blkback isystem: trim/fixup stdarg.h and other headers 2021-08-19 09:02:55 +09:00
zram zram: add error handling support for add_disk() 2021-10-30 11:03:37 -06:00
amiflop.c amiflop: add error handling support for add_disk() 2021-10-18 14:41:37 -06:00
ataflop.c block: ataflop: Fix warning comparing pointer to 0 2021-10-29 06:54:39 -06:00
brd.c block/brd: add error handling support for add_disk() 2021-10-30 11:07:40 -06:00
floppy.c floppy: add error handling support for add_disk() 2021-10-18 14:41:37 -06:00
Kconfig block: remove support for cryptoloop and the xor transfer 2021-10-22 08:34:58 -06:00
loop.c block: remove support for cryptoloop and the xor transfer 2021-10-22 08:34:58 -06:00
loop.h block: remove support for cryptoloop and the xor transfer 2021-10-22 08:34:58 -06:00
Makefile block: remove support for cryptoloop and the xor transfer 2021-10-22 08:34:58 -06:00
n64cart.c n64cart: add error handling support for add_disk() 2021-10-18 14:41:36 -06:00
nbd.c nbd: Fix hungtask when nbd_config_put 2021-11-02 10:50:27 -06:00
pktcdvd.c pktcdvd: add error handling support for add_disk() 2021-10-18 14:41:36 -06:00
ps3disk.c ps3disk: add error handling support for add_disk() 2021-10-30 11:03:37 -06:00
ps3vram.c ps3vram: add error handling support for add_disk() 2021-10-30 11:03:37 -06:00
rbd_types.h
rbd.c rbd: add add_disk() error handling 2021-10-18 14:41:37 -06:00
sunvdc.c Char / Misc driver updates for 5.14-rc1 2021-07-05 13:42:16 -07:00
swim3.c swim3: add missing major.h include 2021-10-18 14:42:44 -06:00
swim_asm.S
swim.c swim: add error handling support for add_disk() 2021-10-18 14:41:37 -06:00
sx8.c sx8: fix an error code in carm_init_one() 2021-10-18 14:42:30 -06:00
virtio_blk.c Revert "virtio-blk: Add validation for block size in config space" 2021-10-13 08:35:36 -04:00
xen-blkfront.c xen-blkfront: add error handling support for add_disk() 2021-10-21 09:00:56 -06:00
z2ram.c for-5.14/drivers-2021-06-29 2021-06-30 12:21:16 -07:00