dm: track per-add_disk holder relations in DM

dm is a bit special in that it opens the underlying devices.  Commit
89f871af1b ("dm: delay registering the gendisk") tried to accommodate
that by allowing to add the holder to the list before add_gendisk and
then just add them to sysfs once add_disk is called.  But that leads to
really odd lifetime problems and error handling problems as we can't
know the state of the kobjects and don't unwind properly.  To fix this
switch to just registering all existing table_devices with the holder
code right after add_disk, and remove them before calling del_gendisk.

Fixes: 89f871af1b ("dm: delay registering the gendisk")
Reported-by: Yu Kuai <yukuai3@huawei.com>
Signed-off-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Yu Kuai <yukuai3@huawei.com>
Reviewed-by: Mike Snitzer <snitzer@kernel.org>
Link: https://lore.kernel.org/r/20221115141054.1051801-7-yukuai1@huaweicloud.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
This commit is contained in:
Christoph Hellwig 2022-11-15 22:10:50 +08:00 committed by Jens Axboe
parent d563792c89
commit 1a581b7216

View File

@ -751,9 +751,16 @@ static struct table_device *open_table_device(struct mapped_device *md,
goto out_free_td; goto out_free_td;
} }
r = bd_link_disk_holder(bdev, dm_disk(md)); /*
if (r) * We can be called before the dm disk is added. In that case we can't
goto out_blkdev_put; * register the holder relation here. It will be done once add_disk was
* called.
*/
if (md->disk->slave_dir) {
r = bd_link_disk_holder(bdev, md->disk);
if (r)
goto out_blkdev_put;
}
td->dm_dev.mode = mode; td->dm_dev.mode = mode;
td->dm_dev.bdev = bdev; td->dm_dev.bdev = bdev;
@ -774,7 +781,8 @@ out_free_td:
*/ */
static void close_table_device(struct table_device *td, struct mapped_device *md) static void close_table_device(struct table_device *td, struct mapped_device *md)
{ {
bd_unlink_disk_holder(td->dm_dev.bdev, dm_disk(md)); if (md->disk->slave_dir)
bd_unlink_disk_holder(td->dm_dev.bdev, md->disk);
blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL); blkdev_put(td->dm_dev.bdev, td->dm_dev.mode | FMODE_EXCL);
put_dax(td->dm_dev.dax_dev); put_dax(td->dm_dev.dax_dev);
list_del(&td->list); list_del(&td->list);
@ -1951,7 +1959,13 @@ static void cleanup_mapped_device(struct mapped_device *md)
md->disk->private_data = NULL; md->disk->private_data = NULL;
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) {
struct table_device *td;
dm_sysfs_exit(md); dm_sysfs_exit(md);
list_for_each_entry(td, &md->table_devices, list) {
bd_unlink_disk_holder(td->dm_dev.bdev,
md->disk);
}
/* /*
* Hold lock to make sure del_gendisk() won't concurrent * Hold lock to make sure del_gendisk() won't concurrent
@ -2291,6 +2305,7 @@ int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t)
{ {
enum dm_queue_mode type = dm_table_get_type(t); enum dm_queue_mode type = dm_table_get_type(t);
struct queue_limits limits; struct queue_limits limits;
struct table_device *td;
int r; int r;
switch (type) { switch (type) {
@ -2329,16 +2344,30 @@ int dm_setup_md_queue(struct mapped_device *md, struct dm_table *t)
if (r) if (r)
return r; return r;
r = dm_sysfs_init(md); /*
if (r) { * Register the holder relationship for devices added before the disk
mutex_lock(&md->table_devices_lock); * was live.
del_gendisk(md->disk); */
mutex_unlock(&md->table_devices_lock); list_for_each_entry(td, &md->table_devices, list) {
return r; r = bd_link_disk_holder(td->dm_dev.bdev, md->disk);
if (r)
goto out_undo_holders;
} }
r = dm_sysfs_init(md);
if (r)
goto out_undo_holders;
md->type = type; md->type = type;
return 0; return 0;
out_undo_holders:
list_for_each_entry_continue_reverse(td, &md->table_devices, list)
bd_unlink_disk_holder(td->dm_dev.bdev, md->disk);
mutex_lock(&md->table_devices_lock);
del_gendisk(md->disk);
mutex_unlock(&md->table_devices_lock);
return r;
} }
struct mapped_device *dm_get_md(dev_t dev) struct mapped_device *dm_get_md(dev_t dev)