partition iteration simplifications
Subsystems (4)
Hi all,
with the switch to an xarray for storing the partitions of a gendisk we can now use the xarray iterators for iterating over the partitions and don't really need the disk_part_iter_* scheme. Also clean up the teardown of partitions where I think we have a (very unlikely to hit) race currently.
Diffstat:
block/blk.h | 1 block/genhd.c | 183 +++++++++++----------------------------- block/partitions/core.c | 54 +++++------ drivers/s390/block/dasd_genhd.c | 3 fs/block_dev.c | 8 + include/linux/genhd.h | 21 ---- 6 files changed, 84 insertions(+), 186 deletions(-)
PATCH 1/11 dasd: use bdev_disk_changed instead of blk_drop_partitions
Use the more general interface - the behavior is the same except that now a change uevent is sent, which is the right thing to do when the device becomes unusable.
--- block/partitions/core.c | 4 ---- drivers/s390/block/dasd_genhd.c | 3 +-- 2 files changed, 1 insertion(+), 6 deletions(-)
block/partitions/core.c +0 -4
@@ -544,10 +544,6 @@ int blk_drop_partitions(struct block_device *bdev)return 0 ; } -#ifdef CONFIG_S390 -/* for historic reasons in the DASD driver */ -EXPORT_SYMBOL_GPL (blk_drop_partitions); -#endif static bool blk_add_partition (struct gendisk *disk,struct block_device *bdev,struct parsed_partitions *state,int p)
drivers/s390/block/dasd_genhd.c +1 -2
@@ -146,12 +146,11 @@ void dasd_destroy_partitions(struct dasd_block *block) block->bdev =NULL ;mutex_lock (&bdev->bd_mutex); -blk_drop_partitions (bdev); +bdev_disk_changed (bdev, true);mutex_unlock (&bdev->bd_mutex);/* Matching blkdev_put to the blkdev_get in dasd_scan_partitions. */ blkdev_put (bdev,FMODE_READ ); -set_capacity (block->gdp,0 ); }int dasd_gendisk_init (void )
Signature · 1 line
2.30.1
Re: [PATCH 01/11] dasd: use bdev_disk_changed instead of blk_drop_partitions
Am 06.04.21 um 08:22 schrieb Christoph Hellwig:
Quoted 9 lines
Use the more general interface - the behavior is the same except that now a change uevent is sent, which is the right thing to do when the device becomes unusable.
Signed-off-by Christoph Hellwig <[email protected]>--- block/partitions/core.c | 4 ---- drivers/s390/block/dasd_genhd.c | 3 +-- 2 files changed, 1 insertion(+), 6 deletions(-)
Thanks,
Stefan
PATCH 2/11 block: remove invalidate_partition
In reply to Christoph Hellwiginvalidate_partition has two callers, one of which already performs the remove_inode_hash just after the call. Just open code the function in the two callsites.
--- block/genhd.c | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-)
block/genhd.c +11 -14
@@ -646,18 +646,6 @@ void device_add_disk_no_queue_reg(struct device *parent, struct gendisk *disk) }EXPORT_SYMBOL (device_add_disk_no_queue_reg); -static void invalidate_partition (struct block_device *bdev) -{ -fsync_bdev (bdev); -__invalidate_device (bdev, 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 (bdev->bd_inode); -} -/** * del_gendisk - remove the gendisk * @disk: the struct gendisk to remove
@@ -699,12 +687,21 @@ void del_gendisk(struct gendisk *disk)/* invalidate stuff */ disk_part_iter_init (&piter, disk,DISK_PITER_INCL_EMPTY );while ((part =disk_part_iter_next (&piter))) { -invalidate_partition (part); +fsync_bdev (part); +__invalidate_device (part, true);delete_partition (part); }disk_part_iter_exit (&piter); -invalidate_partition (disk->part0); +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);
Signature · 1 line
2.30.1
PATCH 3/11 block: move more syncing and invalidation to delete_partition
In reply to Christoph HellwigMove the calls to fsync_bdev and __invalidate_device from del_gendisk to delete_partition. For the other two callers that check that there are no openers for the delete partitions(s) the callouts are a no-op as no file system can be mounted, but this keeps all the cleanup in one place.
--- block/genhd.c | 5 +---- block/partitions/core.c | 6 +++--- 2 files changed, 4 insertions(+), 7 deletions(-)
block/genhd.c +1 -4
@@ -686,11 +686,8 @@ void del_gendisk(struct gendisk *disk)/* invalidate stuff */ disk_part_iter_init (&piter, disk,DISK_PITER_INCL_EMPTY ); -while ((part =disk_part_iter_next (&piter))) { -fsync_bdev (part); -__invalidate_device (part, true); +while ((part =disk_part_iter_next (&piter)))delete_partition (part); - }disk_part_iter_exit (&piter);fsync_bdev (disk->part0);
block/partitions/core.c +3 -3
@@ -287,6 +287,9 @@ struct device_type part_type = {*/ void delete_partition (struct block_device *part) { +fsync_bdev (part); +__invalidate_device (part, true); +xa_erase (&part->bd_disk->part_tbl, part->bd_partno);kobject_put (part->bd_holder_dir);device_del (&part->bd_device);
@@ -468,9 +471,6 @@ int bdev_del_partition(struct block_device *bdev, int partno)if (part->bd_openers) goto out_unlock; -sync_blockdev (part); -invalidate_bdev (part); -delete_partition (part); ret =0 ; out_unlock:
Signature · 1 line
2.30.1
PATCH 4/11 block: refactor blk_drop_partitions
In reply to Christoph HellwigMove the busy check and disk-wide sync into the only caller, so that the remainder can be shared with del_gendisk. Also pass the gendisk instead of the bdev as that is all that is needed.
--- block/blk.h | 1 - block/genhd.c | 11 +---------- block/partitions/core.c | 14 +++----------- fs/block_dev.c | 8 +++++--- include/linux/genhd.h | 2 +- 5 files changed, 10 insertions(+), 26 deletions(-)
block/blk.h +0 -1
@@ -346,7 +346,6 @@ char *disk_name(struct gendisk *hd, int partno, char *buf);#define ADDPART_FLAG_NONE 0#define ADDPART_FLAG_RAID 1#define ADDPART_FLAG_WHOLEDISK 2 -void delete_partition (struct block_device *part);int bdev_add_partition (struct block_device *bdev,int partno,sector_t start,sector_t length);int bdev_del_partition (struct block_device *bdev,int partno);
block/genhd.c +1 -10
@@ -667,9 +667,6 @@ EXPORT_SYMBOL(device_add_disk_no_queue_reg);*/ void del_gendisk (struct gendisk *disk) { -struct disk_part_iter piter; -struct block_device *part; -might_sleep ();if (WARN_ON_ONCE (!disk->queue))
@@ -683,13 +680,7 @@ void del_gendisk(struct gendisk *disk)* disk is marked as dead (GENHD_FL_UP cleared). */ down_write (&bdev_lookup_sem); - -/* invalidate stuff */ -disk_part_iter_init (&piter, disk,DISK_PITER_INCL_EMPTY ); -while ((part =disk_part_iter_next (&piter))) -delete_partition (part); -disk_part_iter_exit (&piter); - +blk_drop_partitions (disk);fsync_bdev (disk->part0);__invalidate_device (disk->part0, true);
block/partitions/core.c +3 -11
@@ -285,7 +285,7 @@ struct device_type part_type = {* Must be called either with bd_mutex held, before a disk can be opened or * after all disk users are gone. */ -void delete_partition (struct block_device *part) +static void delete_partition (struct block_device *part) {fsync_bdev (part);__invalidate_device (part, true);
@@ -526,23 +526,15 @@ static bool disk_unlock_native_capacity(struct gendisk *disk) } } -int blk_drop_partitions (struct block_device *bdev) +void blk_drop_partitions (struct gendisk *disk) {struct disk_part_iter piter;struct block_device *part; -if (bdev->bd_part_count) -return -EBUSY ; - -sync_blockdev (bdev); -invalidate_bdev (bdev); - -disk_part_iter_init (&piter, bdev->bd_disk,DISK_PITER_INCL_EMPTY ); +disk_part_iter_init (&piter, disk,DISK_PITER_INCL_EMPTY );while ((part =disk_part_iter_next (&piter)))delete_partition (part);disk_part_iter_exit (&piter); - -return 0 ; } static bool blk_add_partition(struct gendisk *disk, struct block_device *bdev,
fs/block_dev.c +5 -3
@@ -1243,9 +1243,11 @@ int bdev_disk_changed(struct block_device *bdev, bool invalidate)clear_bit (GD_NEED_PART_SCAN , &bdev->bd_disk->state); rescan: - ret =blk_drop_partitions (bdev); -if (ret) -return ret; +if (bdev->bd_part_count) +return -EBUSY ; +sync_blockdev (bdev); +invalidate_bdev (bdev); +blk_drop_partitions (disk);/* * Historically we only set the capacity to zero for devices that
include/linux/genhd.h +1 -1
@@ -273,7 +273,7 @@ static inline sector_t get_capacity(struct gendisk *disk)int bdev_disk_changed (struct block_device *bdev,bool invalidate);int blk_add_partitions (struct gendisk *disk,struct block_device *bdev); -int blk_drop_partitions (struct block_device *bdev); +void blk_drop_partitions (struct gendisk *disk);extern struct gendisk *__alloc_disk_node (int minors,int node_id);extern void put_disk (struct gendisk *disk);
Signature · 1 line
2.30.1
PATCH 5/11 block: take bd_mutex around delete_partitions in del_gendisk
In reply to Christoph HellwigThere is nothing preventing an ioctl from trying do delete partition concurrenly with del_gendisk, so take open_mutex to serialize against that.
--- block/genhd.c | 4 ++++ block/partitions/core.c | 2 ++ 2 files changed, 6 insertions(+)
block/genhd.c +4 -0
@@ -680,7 +680,11 @@ void del_gendisk(struct gendisk *disk)* disk is marked as dead (GENHD_FL_UP cleared). */ down_write (&bdev_lookup_sem); + +mutex_lock (&disk->part0->bd_mutex);blk_drop_partitions (disk); +mutex_unlock (&disk->part0->bd_mutex); +fsync_bdev (disk->part0);__invalidate_device (disk->part0, true);
block/partitions/core.c +2 -0
@@ -531,6 +531,8 @@ void blk_drop_partitions(struct gendisk *disk)struct disk_part_iter piter;struct block_device *part; +lockdep_assert_held (&disk->part0->bd_mutex); +disk_part_iter_init (&piter, disk,DISK_PITER_INCL_EMPTY );while ((part =disk_part_iter_next (&piter)))delete_partition (part);
Signature · 1 line
2.30.1
PATCH 6/11 block: simplify partition removal
In reply to Christoph HellwigAlways look up the first available entry instead of the complicated stateful traversal.
--- block/partitions/core.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
block/partitions/core.c +6 -4
@@ -528,15 +528,17 @@ static bool disk_unlock_native_capacity(struct gendisk *disk)void blk_drop_partitions (struct gendisk *disk) { -struct disk_part_iter piter;struct block_device *part; +unsigned long idx;lockdep_assert_held (&disk->part0->bd_mutex); -disk_part_iter_init (&piter, disk,DISK_PITER_INCL_EMPTY ); -while ((part =disk_part_iter_next (&piter))) +xa_for_each_start (&disk->part_tbl, idx, part,1 ) { +if (!bdgrab (part)) +continue ;delete_partition (part); -disk_part_iter_exit (&piter); +bdput (part); + } } static bool blk_add_partition(struct gendisk *disk, struct block_device *bdev,
Signature · 1 line
2.30.1
PATCH 7/11 block: simplify partition_overlaps
In reply to Christoph HellwigJust use xa_for_each to iterate over the partitions as there is no need to grab a reference to each partition.
--- block/partitions/core.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-)
block/partitions/core.c +10 -10
@@ -420,21 +420,21 @@ static struct block_device *add_partition(struct gendisk *disk, int partno,static bool partition_overlaps (struct gendisk *disk,sector_t start,sector_t length,int skip_partno) { -struct disk_part_iter piter;struct block_device *part;bool overlap = false; +unsigned long idx; -disk_part_iter_init (&piter, disk,DISK_PITER_INCL_EMPTY ); -while ((part =disk_part_iter_next (&piter))) { -if (part->bd_partno == skip_partno || - start >= part->bd_start_sect +bdev_nr_sectors (part) || - start + length <= part->bd_start_sect) -continue ; - overlap = true; -break ; +rcu_read_lock (); +xa_for_each_start (&disk->part_tbl, idx, part,1 ) { +if (part->bd_partno != skip_partno && + start < part->bd_start_sect +bdev_nr_sectors (part) && + start + length > part->bd_start_sect) { + overlap = true; +break ; + } } +rcu_read_unlock (); -disk_part_iter_exit (&piter);return overlap; }
Signature · 1 line
2.30.1
PATCH 8/11 block: simplify printk_all_partitions
In reply to Christoph HellwigJust use xa_for_each to iterate over the partitions as there is no need to grab a reference to each partition.
--- block/genhd.c | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-)
block/genhd.c +16 -17
@@ -806,10 +806,10 @@ void __init printk_all_partitions(void)class_dev_iter_init (&iter, &block_class,NULL , &disk_type);while ((dev =class_dev_iter_next (&iter))) {struct gendisk *disk =dev_to_disk (dev); -struct disk_part_iter piter;struct block_device *part;char name_buf[BDEVNAME_SIZE ];char devt_buf[BDEVT_SIZE ]; +unsigned long idx;/* * Don't show empty devices or things that have been
@@ -820,30 +820,29 @@ void __init printk_all_partitions(void)continue ;/* -* Note, unlike /proc/partitions, I am showing the -* numbers in hex - the same format as the root= -* option takes. +* Note, unlike /proc/partitions, I am showing the numbers in +* hex - the same format as the root= option takes. */ -disk_part_iter_init (&piter, disk,DISK_PITER_INCL_PART0 ); -while ((part =disk_part_iter_next (&piter))) { -bool is_part0 = part == disk->part0; - -printk ("%s%s %10llu %s %s" , is_part0 ?"" :" " , +rcu_read_lock (); +xa_for_each (&disk->part_tbl, idx, part) { +if (!bdev_nr_sectors (part)) +continue ; +printk ("%s%s %10llu %s %s" , +bdev_is_partition (part) ?" " :"" ,bdevt_str (part->bd_dev, devt_buf),bdev_nr_sectors (part) >>1 ,disk_name (disk, part->bd_partno, name_buf), part->bd_meta_info ? part->bd_meta_info->uuid :"" ); -if (is_part0) { -if (dev->parent && dev->parent->driver) -printk (" driver: %s\n" , - dev->parent->driver->name); -else -printk (" (driver?)\n" ); - }else +if (bdev_is_partition (part))printk ("\n" ); +else if (dev->parent && dev->parent->driver) +printk (" driver: %s\n" , + dev->parent->driver->name); +else +printk (" (driver?)\n" ); } -disk_part_iter_exit (&piter); +rcu_read_unlock (); }class_dev_iter_exit (&iter); }
Signature · 1 line
2.30.1
PATCH 9/11 block: simplify show_partition
In reply to Christoph HellwigJust use xa_for_each to iterate over the partitions as there is no need to grab a reference to each partition.
--- block/genhd.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-)
block/genhd.c +7 -6
@@ -907,8 +907,8 @@ static void *show_partition_start(struct seq_file *seqf, loff_t *pos)static int show_partition (struct seq_file *seqf,void *v) {struct gendisk *sgp = v; -struct disk_part_iter piter;struct block_device *part; +unsigned long idx;char buf[BDEVNAME_SIZE ];/* Don't show non-partitionable removeable devices or empty devices */
@@ -918,15 +918,16 @@ static int show_partition(struct seq_file *seqf, void *v)if (sgp->flags &GENHD_FL_SUPPRESS_PARTITION_INFO )return 0 ; -/* show the full disk and all non-0 size partitions of it */ -disk_part_iter_init (&piter, sgp,DISK_PITER_INCL_PART0 ); -while ((part =disk_part_iter_next (&piter))) +rcu_read_lock (); +xa_for_each (&sgp->part_tbl, idx, part) { +if (!bdev_nr_sectors (part)) +continue ;seq_printf (seqf,"%4d %7d %10llu %s\n" ,MAJOR (part->bd_dev),MINOR (part->bd_dev),bdev_nr_sectors (part) >>1 ,disk_name (sgp, part->bd_partno, buf)); -disk_part_iter_exit (&piter); - + } +rcu_read_unlock ();return 0 ; }
Signature · 1 line
2.30.1
PATCH 10/11 block: simplify diskstats_show
In reply to Christoph HellwigJust use xa_for_each to iterate over the partitions as there is no need to grab a reference to each partition.
--- block/genhd.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
block/genhd.c +6 -4
@@ -1236,11 +1236,11 @@ const struct device_type disk_type = {static int diskstats_show (struct seq_file *seqf,void *v) {struct gendisk *gp = v; -struct disk_part_iter piter;struct block_device *hd;char buf[BDEVNAME_SIZE ];unsigned int inflight;struct disk_stats stat; +unsigned long idx;/* if (&disk_to_dev(gp)->kobj.entry == block_class.devices.next)
@@ -1250,8 +1250,10 @@ static int diskstats_show(struct seq_file *seqf, void *v)"\n\n"); */ -disk_part_iter_init (&piter, gp,DISK_PITER_INCL_EMPTY_PART0 ); -while ((hd =disk_part_iter_next (&piter))) { +rcu_read_lock (); +xa_for_each (&gp->part_tbl, idx, hd) { +if (bdev_is_partition (hd) && !bdev_nr_sectors (hd)) +continue ;part_stat_read_all (hd, &stat);if (queue_is_mq (gp->queue)) inflight =blk_mq_in_flight (gp->queue, hd);
@@ -1294,7 +1296,7 @@ static int diskstats_show(struct seq_file *seqf, void *v) NSEC_PER_MSEC) ); } -disk_part_iter_exit (&piter); +rcu_read_unlock ();return 0 ; }
Signature · 1 line
2.30.1
PATCH 11/11 block: remove disk_part_iter
In reply to Christoph HellwigJust open code the xa_for_each in the remaining user.
--- block/genhd.c | 92 ++++++------------------------------------- include/linux/genhd.h | 19 --------- 2 files changed, 13 insertions(+), 98 deletions(-)
block/genhd.c +13 -79
@@ -161,81 +161,6 @@ static void part_in_flight_rw(struct block_device *part, inflight[1 ] =0 ; } -/** -* disk_part_iter_init - initialize partition iterator -* @piter: iterator to initialize -* @disk: disk to iterate over -* @flags: DISK_PITER_* flags -* -* Initialize @piter so that it iterates over partitions of @disk. -* -* CONTEXT: -* Don't care. -*/ -void disk_part_iter_init (struct disk_part_iter *piter,struct gendisk *disk, -unsigned int flags) -{ - piter->disk = disk; - piter->part =NULL ; -if (flags & (DISK_PITER_INCL_PART0 |DISK_PITER_INCL_EMPTY_PART0 )) - piter->idx =0 ; -else - piter->idx =1 ; - piter->flags = flags; -} - -/** -* disk_part_iter_next - proceed iterator to the next partition and return it -* @piter: iterator of interest -* -* Proceed @piter to the next partition and return it. -* -* CONTEXT: -* Don't care. -*/ -struct block_device *disk_part_iter_next (struct disk_part_iter *piter) -{ -struct block_device *part; -unsigned long idx; - -/* put the last partition */ -disk_part_iter_exit (piter); - -rcu_read_lock (); -xa_for_each_start (&piter->disk->part_tbl, idx, part, piter->idx) { -if (!bdev_nr_sectors (part) && - !(piter->flags &DISK_PITER_INCL_EMPTY ) && - !(piter->flags &DISK_PITER_INCL_EMPTY_PART0 && - piter->idx ==0 )) -continue ; - - piter->part =bdgrab (part); -if (!piter->part) -continue ; - piter->idx = idx +1 ; -break ; - } -rcu_read_unlock (); - -return piter->part; -} - -/** -* disk_part_iter_exit - finish up partition iteration -* @piter: iter of interest -* -* Called when iteration is over. Cleans up @piter. -* -* CONTEXT: -* Don't care. -*/ -void disk_part_iter_exit (struct disk_part_iter *piter) -{ -if (piter->part) -bdput (piter->part); - piter->part =NULL ; -} -/* * Can be deleted altogether. Later. *
@@ -472,13 +397,22 @@ static char *bdevt_str(dev_t devt, char *buf)void disk_uevent (struct gendisk *disk,enum kobject_action action) { -struct disk_part_iter piter;struct block_device *part; +unsigned long idx; + +rcu_read_lock (); +xa_for_each (&disk->part_tbl, idx, part) { +if (bdev_is_partition (part) && !bdev_nr_sectors (part)) +continue ; +if (!bdgrab (part)) +continue ; -disk_part_iter_init (&piter, disk,DISK_PITER_INCL_EMPTY_PART0 ); -while ((part =disk_part_iter_next (&piter))) +rcu_read_unlock ();kobject_uevent (bdev_kobj (part), action); -disk_part_iter_exit (&piter); +bdput (part); +rcu_read_lock (); + } +rcu_read_unlock (); }EXPORT_SYMBOL_GPL (disk_uevent);
include/linux/genhd.h +0 -19
@@ -204,25 +204,6 @@ static inline dev_t disk_devt(struct gendisk *disk)void disk_uevent (struct gendisk *disk,enum kobject_action action); -/* -* Smarter partition iterator without context limits. -*/ -#define DISK_PITER_INCL_EMPTY (1 << 1)/* include 0-sized parts */ -#define DISK_PITER_INCL_PART0 (1 << 2)/* include partition 0 */ -#define DISK_PITER_INCL_EMPTY_PART0 (1 << 3)/* include empty partition 0 */ - -struct disk_part_iter { -struct gendisk *disk; -struct block_device *part; -unsigned long idx; -unsigned int flags; -}; - -extern void disk_part_iter_init (struct disk_part_iter *piter, -struct gendisk *disk,unsigned int flags); -struct block_device *disk_part_iter_next (struct disk_part_iter *piter); -extern void disk_part_iter_exit (struct disk_part_iter *piter); -/* block/genhd.c */ extern void device_add_disk (struct device *parent,struct gendisk *disk,const struct attribute_group **groups);
Signature · 1 line
2.30.1
Quoted 6 lines · Christoph Hellwig
Hi all,
with the switch to an xarray for storing the partitions of a gendisk we can now use the xarray iterators for iterating over the partitions and don't really need the disk_part_iter_* scheme. Also clean up the teardown of partitions where I think we have a (very unlikely to hit) race currently.
Applied, thanks.
Signature · 1 line
Jens Axboe