fix a race between del_gendisk and BLKRRPART
Subsystems (2)
Hi all,
this is based of a patch from Gulam and suggestions from Ming and fixes a race between del_gendisk and BLKRRPART while also removing a global lock.
Diffstat:
block/genhd.c | 19 ++----------------- fs/block_dev.c | 12 ++++-------- include/linux/genhd.h | 2 -- 3 files changed, 6 insertions(+), 27 deletions(-)
PATCH 1/2 block: prevent block device lookups at the beginning of del_gendisk
As an aritfact of how gendisk lookup used to work in earlier kernels, GENHD_FL_UP is only cleared very late in del_gendisk, and instead a global lock is used to prevent opens from actually succeeding. Clear the flag, and remove the bdev inode from the inode hash earlier to ensure lookups fail from the very beginning of del_gendisk, and remove the now not needed bdev_lookup_sem.
--- block/genhd.c | 19 ++----------------- fs/block_dev.c | 9 +-------- include/linux/genhd.h | 2 -- 3 files changed, 3 insertions(+), 27 deletions(-)
block/genhd.c +2 -17
@@ -29,8 +29,6 @@static struct kobject *block_depr; -DECLARE_RWSEM (bdev_lookup_sem); -/* for extended dynamic devt allocation, currently only one major is used */ #define NR_EXT_DEVT (1 << MINORBITS)static DEFINE_IDA (ext_devt_ida);
@@ -609,28 +607,15 @@ void del_gendisk(struct gendisk *disk)blk_integrity_del (disk);disk_del_events (disk); -/* -* Block lookups of the disk until all bdevs are unhashed and the -* disk is marked as dead (GENHD_FL_UP cleared). -*/ -down_write (&bdev_lookup_sem); -mutex_lock (&disk->part0->bd_mutex); + disk->flags &= ~GENHD_FL_UP ; +remove_inode_hash (disk->part0->bd_inode);blk_drop_partitions (disk);mutex_unlock (&disk->part0->bd_mutex);fsync_bdev (disk->part0);__invalidate_device (disk->part0, true); - -/* -* Unhash the bdev inode for this device so that it can't be looked -* up any more even if openers still hold references to it. -*/ -remove_inode_hash (disk->part0->bd_inode); -set_capacity (disk,0 ); - disk->flags &= ~GENHD_FL_UP ; -up_write (&bdev_lookup_sem);if (!(disk->flags &GENHD_FL_HIDDEN )) {sysfs_remove_link (&disk_to_dev (disk)->kobj,"bdi" );
fs/block_dev.c +1 -8
@@ -1364,16 +1364,12 @@ struct block_device *blkdev_get_no_open(dev_t dev)struct block_device *bdev;struct gendisk *disk; -down_read (&bdev_lookup_sem); bdev =bdget (dev);if (!bdev) { -up_read (&bdev_lookup_sem);blk_request_module (dev); -down_read (&bdev_lookup_sem); - bdev =bdget (dev);if (!bdev) - goto unlock; +return NULL ; } disk = bdev->bd_disk;
@@ -1383,14 +1379,11 @@ struct block_device *blkdev_get_no_open(dev_t dev) goto put_disk;if (!try_module_get (bdev->bd_disk->fops->owner)) goto put_disk; -up_read (&bdev_lookup_sem);return bdev; put_disk:put_disk (disk); bdput:bdput (bdev); -unlock: -up_read (&bdev_lookup_sem);return NULL ; }
include/linux/genhd.h +0 -2
@@ -306,8 +306,6 @@ static inline void bd_unlink_disk_holder(struct block_device *bdev, }#endif /* CONFIG_SYSFS */ -extern struct rw_semaphore bdev_lookup_sem; -dev_t blk_lookup_devt (const char *name,int partno);void blk_request_module (dev_t devt);#ifdef CONFIG_BLOCK
Signature · 1 line
2.30.2
Re: [PATCH 1/2] block: prevent block device lookups at the beginning of del_gendisk
Quoted 32 lines · Christoph Hellwig
As an aritfact of how gendisk lookup used to work in earlier kernels,
GENHD_FL_UPis only cleared very late indel_gendisk, and instead a global lock is used to prevent opens from actually succeeding. Clear the flag, and remove the bdev inode from the inode hash earlier to ensure lookups fail from the very beginning ofdel_gendisk, and remove the now not neededbdev_lookup_sem.Signed-off-by Christoph Hellwig <[email protected]>--- block/genhd.c | 19 ++----------------- fs/block_dev.c | 9 +-------- include/linux/genhd.h | 2 -- 3 files changed, 3 insertions(+), 27 deletions(-)block/genhd.c +0
-3@@ -29,8 +29,6 @@static struct kobject *block_depr; -DECLARE_RWSEM (bdev_lookup_sem); -/* for extended dynamic devt allocation, currently only one major is used */ #define NR_EXT_DEVT (1 << MINORBITS)static DEFINE_IDA (ext_devt_ida);@@ -609,28 +607,15 @@ void del_gendisk(struct gendisk *disk)blk_integrity_del (disk);disk_del_events (disk); -/*
block/genhd.c void del_gendisk(struct gendisk *disk) · line 614
Quoted 10 lines
block/genhd.c +2
-5-* Block lookups of the disk until all bdevs are unhashed and the -* disk is marked as dead (GENHD_FL_UP cleared). -*/ -down_write (&bdev_lookup_sem); -mutex_lock (&disk->part0->bd_mutex); + disk->flags &= ~GENHD_FL_UP ; +remove_inode_hash (disk->part0->bd_inode);blk_drop_partitions (disk);mutex_unlock (&disk->part0->bd_mutex);
Both bdget() and checking FL_UP is done without holding ->bd_mutex, so del_gendisk() may be run after checking FL_UP is completed in blkdev_get_no_open(), then new opener is still allowed even after the bdev is invalidated.
Given this patch is for 5.13, I'd suggest to not move remove_inode_hash() inside the lock block, which isn't necessary, and it is enough to move clearing FL_UP with the following change together:
fs/block_dev.c +4 -2
@@ -1298,6 +1298,9 @@ static int __blkdev_get(struct block_device *bdev, fmode_t mode)struct gendisk *disk = bdev->bd_disk;int ret =0 ; +if (!(disk->flags &GENHD_FL_UP )) +return -ENXIO ; +if (!bdev->bd_openers) {if (!bdev_is_partition (bdev)) { ret =0 ;
@@ -1332,8 +1335,7 @@ static int __blkdev_get(struct block_device *bdev, fmode_t mode) whole->bd_part_count++;mutex_unlock (&whole->bd_mutex); -if (!(disk->flags &GENHD_FL_UP ) || - !bdev_nr_sectors (bdev)) { +if (!bdev_nr_sectors (bdev)) {__blkdev_put (whole, mode,1 );bdput (whole);return -ENXIO ;
Thanks,
Ming
PATCH 2/2 block: fix a race between del_gendisk and BLKRRPART
In reply to Christoph HellwigFrom: Gulam Mohamed <[email protected]>
When BLKRRPART is called concurrently with del_gendisk, the partitions rescan can create a stale partition that will never be be cleaned up.
Fix this by checking the the disk is up before rescanning partitions while under bd_mutex.
--- fs/block_dev.c | 3 +++ 1 file changed, 3 insertions(+)
fs/block_dev.c +3 -0
@@ -1244,6 +1244,9 @@ int bdev_disk_changed(struct block_device *bdev, bool invalidate)lockdep_assert_held (&bdev->bd_mutex); +if (!(disk->flags &GENHD_FL_UP )) +return -ENXIO ; + rescan:if (bdev->bd_part_count)return -EBUSY ;
Signature · 1 line
2.30.2