linux/drivers/block
Hou Tao 68c9417b19 nbd: do del_gendisk() asynchronously for NBD_DESTROY_ON_DISCONNECT
Now open_mutex is used to synchronize partition operations (e.g,
blk_drop_partitions() and blkdev_reread_part()), however it makes
nbd driver broken, because nbd may call del_gendisk() in nbd_release()
or nbd_genl_disconnect() if NBD_CFLAG_DESTROY_ON_DISCONNECT is enabled,
and deadlock occurs, as shown below:

// AB-BA dead-lock
nbd_genl_disconnect            blkdev_open
  nbd_disconnect_and_put
                                 lock bd_mutex
  // last ref
  nbd_put
    lock nbd_index_mutex
      del_gendisk
                                   nbd_open
                                     try lock nbd_index_mutex
        try lock bd_mutex

 or

// AA dead-lock
nbd_release
  lock bd_mutex
    nbd_put
      try lock bd_mutex

Instead of fixing block layer (e.g, introduce another lock), fixing
the nbd driver to call del_gendisk() in a kworker when
NBD_DESTROY_ON_DISCONNECT is enabled. When NBD_DESTROY_ON_DISCONNECT
is disabled, nbd device will always be destroy through module removal,
and there is no risky of deadlock.

To ensure the reuse of nbd index succeeds, moving the calling of
idr_remove() after del_gendisk(), so if the reused index is not found
in nbd_index_idr, the old disk must have been deleted. And reusing
the existing destroy_complete mechanism to ensure nbd_genl_connect()
will wait for the completion of del_gendisk().

Also adding a new workqueue for nbd removal, so nbd_cleanup()
can ensure all removals complete before exits.

Reported-by: syzbot+0fe7752e52337864d29b@syzkaller.appspotmail.com
Fixes: c76f48eb5c ("block: take bd_mutex around delete_partitions in del_gendisk")
Signed-off-by: Hou Tao <houtao1@huawei.com>
Signed-off-by: Christoph Hellwig <hch@lst.de>
Link: https://lore.kernel.org/r/20210811124428.2368491-2-hch@lst.de
Reviewed-by: Josef Bacik <josef@toxicpanda.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
2021-08-13 14:17:16 -06:00
..
aoe for-5.14/drivers-2021-06-29 2021-06-30 12:21:16 -07:00
drbd for-5.14/drivers-2021-06-29 2021-06-30 12:21:16 -07:00
mtip32xx mtip32xx: use blk_mq_alloc_disk and blk_cleanup_disk 2021-06-30 15:34:04 -06:00
null_blk null_blk: remove an unused variable assignment in null_add_dev 2021-06-30 15:34:04 -06:00
paride pd: fix order of cleaning up the queue and freeing the tagset 2021-07-15 09:29:22 -06:00
rnbd block/rnbd: Use sysfs_emit instead of s*printf function for sysfs show 2021-08-02 13:37:40 -06:00
rsxx for-5.14/drivers-2021-06-29 2021-06-30 12:21:16 -07:00
xen-blkback xen-blkback: fix compatibility bug with single page rings 2021-04-23 09:34:07 +02:00
zram Merge branch 'akpm' (patches from Andrew) 2021-07-02 12:08:10 -07:00
amiflop.c amiflop: use blk_mq_alloc_disk and blk_cleanup_disk 2021-06-11 11:54:43 -06:00
ataflop.c ataflop: use blk_mq_alloc_disk and blk_cleanup_disk 2021-06-11 11:54:43 -06:00
brd.c brd: convert to blk_alloc_disk/blk_cleanup_disk 2021-06-01 07:42:23 -06:00
cryptoloop.c treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 30 2019-05-24 17:27:10 +02:00
floppy.c for-5.14/drivers-2021-06-29 2021-06-30 12:21:16 -07:00
Kconfig swim: don't call blk_queue_bounce_limit 2021-04-06 09:29:47 -06:00
loop.c loop: raise media_change event 2021-08-02 13:37:29 -06:00
loop.h loop: charge i/o to mem and blk cg 2021-06-29 10:53:50 -07:00
Makefile drivers/block: remove the umem driver 2021-03-24 06:57:40 -06:00
n64cart.c n64cart: convert to blk_alloc_disk 2021-06-01 07:42:24 -06:00
nbd.c nbd: do del_gendisk() asynchronously for NBD_DESTROY_ON_DISCONNECT 2021-08-13 14:17:16 -06:00
pktcdvd.c block: remove REQ_OP_SCSI_{IN,OUT} 2021-06-30 15:34:19 -06:00
ps3disk.c ps3disk: use memcpy_{from,to}_bvec 2021-08-02 13:37:27 -06:00
ps3vram.c ps3vram: convert to blk_alloc_disk/blk_cleanup_disk 2021-06-01 07:42:24 -06:00
rbd.c rbd: use memzero_bvec 2021-08-02 13:37:27 -06:00
rbd_types.h libceph, rbd: replace zero-length array with flexible-array 2020-06-01 13:22:53 +02:00
sunvdc.c Char / Misc driver updates for 5.14-rc1 2021-07-05 13:42:16 -07:00
swim.c swim: use blk_mq_alloc_disk 2021-06-11 11:53:03 -06:00
swim3.c swim3: use blk_mq_alloc_disk 2021-06-11 11:53:02 -06:00
swim_asm.S treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 152 2019-05-30 11:26:32 -07:00
sx8.c for-5.14/drivers-2021-06-29 2021-06-30 12:21:16 -07:00
virtio_blk.c virtio-blk: limit seg_max to a safe value 2021-07-03 04:50:53 -04:00
xen-blkfront.c xen-blkfront: Remove redundant assignment to variable err 2021-08-09 20:04:46 -06:00
z2ram.c for-5.14/drivers-2021-06-29 2021-06-30 12:21:16 -07:00