Commit d563792c authored by Yu Kuai's avatar Yu Kuai Committed by Jens Axboe

dm: make sure create and remove dm device won't race with open and close table

open_table_device() and close_table_device() is protected by
table_devices_lock, hence use it to protect add_disk() and
del_gendisk().

Prepare to track per-add_disk holder relations in dm.
Signed-off-by: default avatarYu Kuai <yukuai3@huawei.com>
Reviewed-by: default avatarMike Snitzer <snitzer@kernel.org>
Reviewed-by: default avatarChristoph Hellwig <hch@lst.de>
Link: https://lore.kernel.org/r/20221115141054.1051801-6-yukuai1@huaweicloud.comSigned-off-by: default avatarJens Axboe <axboe@kernel.dk>
parent 7b586583
...@@ -1952,7 +1952,14 @@ static void cleanup_mapped_device(struct mapped_device *md) ...@@ -1952,7 +1952,14 @@ static void cleanup_mapped_device(struct mapped_device *md)
spin_unlock(&_minor_lock); spin_unlock(&_minor_lock);
if (dm_get_md_type(md) != DM_TYPE_NONE) { if (dm_get_md_type(md) != DM_TYPE_NONE) {
dm_sysfs_exit(md); dm_sysfs_exit(md);
/*
* Hold lock to make sure del_gendisk() won't concurrent
* with open/close_table_device().
*/
mutex_lock(&md->table_devices_lock);
del_gendisk(md->disk); del_gendisk(md->disk);
mutex_unlock(&md->table_devices_lock);
} }
dm_queue_destroy_crypto_profile(md->queue); dm_queue_destroy_crypto_profile(md->queue);
put_disk(md->disk); put_disk(md->disk);
...@@ -2312,15 +2319,24 @@ int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t) ...@@ -2312,15 +2319,24 @@ int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t)
if (r) if (r)
return r; return r;
/*
* Hold lock to make sure add_disk() and del_gendisk() won't concurrent
* with open_table_device() and close_table_device().
*/
mutex_lock(&md->table_devices_lock);
r = add_disk(md->disk); r = add_disk(md->disk);
mutex_unlock(&md->table_devices_lock);
if (r) if (r)
return r; return r;
r = dm_sysfs_init(md); r = dm_sysfs_init(md);
if (r) { if (r) {
mutex_lock(&md->table_devices_lock);
del_gendisk(md->disk); del_gendisk(md->disk);
mutex_unlock(&md->table_devices_lock);
return r; return r;
} }
md->type = type; md->type = type;
return 0; return 0;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment