diff options
author | Yu Kuai | 2022-11-15 22:10:49 +0800 |
---|---|---|
committer | Greg Kroah-Hartman | 2022-12-31 13:32:12 +0100 |
commit | ce65ef11d37e8342f17e7c822c43a2ff9317c016 (patch) | |
tree | 4bba954f8c07d1f192c43f38a2d576541f7e03ef /drivers/md | |
parent | 365c351efd4a34c139dcb40fd6519f1521d94db0 (diff) |
dm: make sure create and remove dm device won't race with open and close table
[ Upstream commit d563792c8933a810d28ce0f2831f0726c2b15a31 ]
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: Yu Kuai <yukuai3@huawei.com>
Reviewed-by: Mike Snitzer <snitzer@kernel.org>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Link: https://lore.kernel.org/r/20221115141054.1051801-6-yukuai1@huaweicloud.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
Stable-dep-of: 1a581b721699 ("dm: track per-add_disk holder relations in DM")
Signed-off-by: Sasha Levin <sashal@kernel.org>
Diffstat (limited to 'drivers/md')
-rw-r--r-- | drivers/md/dm.c | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/drivers/md/dm.c b/drivers/md/dm.c index 8fb0b97b2df1..f10ac680cef4 100644 --- a/drivers/md/dm.c +++ b/drivers/md/dm.c @@ -1965,7 +1965,14 @@ static void cleanup_mapped_device(struct mapped_device *md) spin_unlock(&_minor_lock); if (dm_get_md_type(md) != DM_TYPE_NONE) { 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); + mutex_unlock(&md->table_devices_lock); } dm_queue_destroy_crypto_profile(md->queue); put_disk(md->disk); @@ -2325,15 +2332,24 @@ int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t) if (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); + mutex_unlock(&md->table_devices_lock); if (r) return r; r = dm_sysfs_init(md); if (r) { + mutex_lock(&md->table_devices_lock); del_gendisk(md->disk); + mutex_unlock(&md->table_devices_lock); return r; } + md->type = type; return 0; } |