On Thu, Jul 22, 2021 at 09:53:58AM +0200, Christoph Hellwig wrote: > Instead of acquiring an inode reference on open make sure partitions > always hold device model references to the disk while alive, and switch > open to grab only a device model reference to the opened block device. > If that is a partition the disk reference is transitively held by the > partition already. > --- > block/partitions/core.c | 9 ++++++- > fs/block_dev.c | 60 ++++++++++++++++------------------------- > 2 files changed, 31 insertions(+), 38 deletions(-) > > diff --git a/block/partitions/core.c b/block/partitions/core.c > index 09c58a110a89..4f7a1a9cd544 100644 > --- a/block/partitions/core.c > +++ b/block/partitions/core.c > @@ -261,6 +261,7 @@ static void part_release(struct device *dev) > { > if (MAJOR(dev->devt) == BLOCK_EXT_MAJOR) > blk_free_ext_minor(MINOR(dev->devt)); > + put_disk(dev_to_bdev(dev)->bd_disk); > bdput(dev_to_bdev(dev)); > } > > @@ -349,9 +350,13 @@ static struct block_device *add_partition(struct gendisk *disk, int partno, > if (xa_load(&disk->part_tbl, partno)) > return ERR_PTR(-EBUSY); > > + /* ensure we always have a reference to the whole disk */ > + get_device(disk_to_dev(disk)); > + > + err = -ENOMEM; > bdev = bdev_alloc(disk, partno); > if (!bdev) > - return ERR_PTR(-ENOMEM); > + goto out_put_disk; > > bdev->bd_start_sect = start; > bdev_set_nr_sectors(bdev, len); > @@ -420,6 +425,8 @@ static struct block_device *add_partition(struct gendisk *disk, int partno, > device_del(pdev); > out_put: > put_device(pdev); > +out_put_disk: > + put_disk(disk); put_disk() is only needed for failure of bdev_alloc(). Once bdev->bd_device is initialized, the disk reference will be dropped via part_release(). Thanks, Ming