Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

block: fix deadlock between bd_link_disk_holder and partition scan

'open_mutex' of gendisk is used to protect open/close block devices. But
in bd_link_disk_holder(), it is used to protect the creation of symlink
between holding disk and slave bdev, which introduces some issues.

When bd_link_disk_holder() is called, the driver is usually in the process
of initialization/modification and may suspend submitting io. At this
time, any io hold 'open_mutex', such as scanning partitions, can cause
deadlocks. For example, in raid:

T1 T2
bdev_open_by_dev
lock open_mutex [1]
...
efi_partition
...
md_submit_bio
md_ioctl mddev_syspend
-> suspend all io
md_add_new_disk
bind_rdev_to_array
bd_link_disk_holder
try lock open_mutex [2]
md_handle_request
-> wait mddev_resume

T1 scan partition, T2 add a new device to raid. T1 waits for T2 to resume
mddev, but T2 waits for open_mutex held by T1. Deadlock occurs.

Fix it by introducing a local mutex 'blk_holder_mutex' to replace
'open_mutex'.

Fixes: 1b0a2d950ee2 ("md: use new apis to suspend array for ioctls involed array reconfiguration")
Reported-by: mgperkow@gmail.com
Closes: https://bugzilla.kernel.org/show_bug.cgi?id=218459
Signed-off-by: Li Nan <linan122@huawei.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Yu Kuai <yukuai3@huawei.com>
Link: https://lore.kernel.org/r/20240221090122.1281868-1-linan666@huaweicloud.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>

authored by

Li Nan and committed by
Jens Axboe
03f12122 522d7352

+7 -5
+7 -5
block/holder.c
··· 8 8 int refcnt; 9 9 }; 10 10 11 + static DEFINE_MUTEX(blk_holder_mutex); 12 + 11 13 static struct bd_holder_disk *bd_find_holder_disk(struct block_device *bdev, 12 14 struct gendisk *disk) 13 15 { ··· 82 80 kobject_get(bdev->bd_holder_dir); 83 81 mutex_unlock(&bdev->bd_disk->open_mutex); 84 82 85 - mutex_lock(&disk->open_mutex); 83 + mutex_lock(&blk_holder_mutex); 86 84 WARN_ON_ONCE(!bdev->bd_holder); 87 85 88 86 holder = bd_find_holder_disk(bdev, disk); ··· 110 108 goto out_del_symlink; 111 109 list_add(&holder->list, &disk->slave_bdevs); 112 110 113 - mutex_unlock(&disk->open_mutex); 111 + mutex_unlock(&blk_holder_mutex); 114 112 return 0; 115 113 116 114 out_del_symlink: ··· 118 116 out_free_holder: 119 117 kfree(holder); 120 118 out_unlock: 121 - mutex_unlock(&disk->open_mutex); 119 + mutex_unlock(&blk_holder_mutex); 122 120 if (ret) 123 121 kobject_put(bdev->bd_holder_dir); 124 122 return ret; ··· 142 140 if (WARN_ON_ONCE(!disk->slave_dir)) 143 141 return; 144 142 145 - mutex_lock(&disk->open_mutex); 143 + mutex_lock(&blk_holder_mutex); 146 144 holder = bd_find_holder_disk(bdev, disk); 147 145 if (!WARN_ON_ONCE(holder == NULL) && !--holder->refcnt) { 148 146 del_symlink(disk->slave_dir, bdev_kobj(bdev)); ··· 151 149 list_del_init(&holder->list); 152 150 kfree(holder); 153 151 } 154 - mutex_unlock(&disk->open_mutex); 152 + mutex_unlock(&blk_holder_mutex); 155 153 } 156 154 EXPORT_SYMBOL_GPL(bd_unlink_disk_holder);