fix block device size update serialization
Subsystems (8)
Christoph Hellwig
maintainerHi Jens,
this series fixes how we update i_size for the block device inodes (and thus the block device). Different helpers use two different locks (bd_mutex and i_rwsem) to protect the update, and it appears device mapper uses yet another internal lock. A lot of the drivers do the update handcrafted in often crufty ways. And in addition to that mess it turns out that the "main" lock, bd_mutex is pretty dead lock prone vs other spots in the block layer that acquire it during revalidation operations, as reported by Xianting.
Fix all that by adding a dedicated spinlock just for the size updates.
Christoph Hellwig
maintainerPATCH 1/2 block: replace bd_set_size with bd_set_nr_sectors
Replace bd_set_size with a version that takes the number of sectors instead, as that fits most of the current and future callers much better.
--- drivers/block/loop.c | 4 ++-- drivers/block/nbd.c | 7 ++++--- drivers/block/pktcdvd.c | 2 +- drivers/nvme/host/nvme.h | 2 +- fs/block_dev.c | 10 +++++----- include/linux/genhd.h | 2 +- 6 files changed, 14 insertions(+), 13 deletions(-)
drivers/block/loop.c +2 -2
@@ -253,7 +253,7 @@ static void loop_set_size(struct loop_device *lo, loff_t size) {struct block_device *bdev = lo->lo_device; -bd_set_size (bdev, size <<SECTOR_SHIFT ); +bd_set_nr_sectors (bdev, size);set_capacity_revalidate_and_notify (lo->lo_disk, size, false); }
@@ -1248,7 +1248,7 @@ static int __loop_clr_fd(struct loop_device *lo, bool release)set_capacity (lo->lo_disk,0 );loop_sysfs_exit (lo);if (bdev) { -bd_set_size (bdev,0 ); +bd_set_nr_sectors (bdev,0 );/* let user-space know about this change */ kobject_uevent (&disk_to_dev (bdev->bd_disk)->kobj,KOBJ_CHANGE ); }
drivers/block/nbd.c +4 -3
@@ -300,6 +300,7 @@ static void nbd_size_update(struct nbd_device *nbd) {struct nbd_config *config = nbd->config;struct block_device *bdev =bdget_disk (nbd->disk,0 ); +sector_t nr_sectors = config->bytesize >>9 ;if (config->flags &NBD_FLAG_SEND_TRIM ) { nbd->disk->queue->limits.discard_granularity = config->blksize;
@@ -308,10 +309,10 @@ static void nbd_size_update(struct nbd_device *nbd) }blk_queue_logical_block_size (nbd->disk->queue, config->blksize);blk_queue_physical_block_size (nbd->disk->queue, config->blksize); -set_capacity (nbd->disk, config->bytesize >>9 ); +set_capacity (nbd->disk, nr_sectors);if (bdev) {if (bdev->bd_disk) { -bd_set_size (bdev, config->bytesize); +bd_set_nr_sectors (bdev, nr_sectors);set_blocksize (bdev, config->blksize); }else bdev->bd_invalidated =1 ;
@@ -1138,7 +1139,7 @@ static void nbd_bdev_reset(struct block_device *bdev) {if (bdev->bd_openers >1 )return ; -bd_set_size (bdev,0 ); +bd_set_nr_sectors (bdev,0 ); }static void nbd_parse_flags (struct nbd_device *nbd)
drivers/block/pktcdvd.c +1 -1
@@ -2192,7 +2192,7 @@ static int pkt_open_dev(struct pktcdvd_device *pd, fmode_t write)set_capacity (pd->disk, lba <<2 );set_capacity (pd->bdev->bd_disk, lba <<2 ); -bd_set_size (pd->bdev, (loff_t )lba <<11 ); +bd_set_nr_sectors (pd->bdev, lba <<2 ); q =bdev_get_queue (pd->bdev);if (write) {
drivers/nvme/host/nvme.h +1 -1
@@ -664,7 +664,7 @@ static inline void nvme_mpath_update_disk_size(struct gendisk *disk)struct block_device *bdev =bdget_disk (disk,0 );if (bdev) { -bd_set_size (bdev,get_capacity (disk) <<SECTOR_SHIFT ); +bd_set_nr_sectors (bdev,get_capacity (disk));bdput (bdev); } }
fs/block_dev.c +5 -5
@@ -1371,13 +1371,13 @@ int check_disk_change(struct block_device *bdev)EXPORT_SYMBOL (check_disk_change); -void bd_set_size (struct block_device *bdev,loff_t size) +void bd_set_nr_sectors (struct block_device *bdev,sector_t sectors) {inode_lock (bdev->bd_inode); -i_size_write (bdev->bd_inode, size); +i_size_write (bdev->bd_inode, (loff_t )sectors <<SECTOR_SHIFT );inode_unlock (bdev->bd_inode); } -EXPORT_SYMBOL (bd_set_size); +EXPORT_SYMBOL (bd_set_nr_sectors);static void __blkdev_put (struct block_device *bdev,fmode_t mode,int for_part);
@@ -1514,7 +1514,7 @@ static int __blkdev_get(struct block_device *bdev, fmode_t mode, void *holder, }if (!ret) { -bd_set_size (bdev,(loff_t )get_capacity (disk)<<9 ); +bd_set_nr_sectors (bdev,get_capacity (disk));set_init_blocksize (bdev); }
@@ -1542,7 +1542,7 @@ static int __blkdev_get(struct block_device *bdev, fmode_t mode, void *holder, ret = -ENXIO ; goto out_clear; } -bd_set_size (bdev, (loff_t )bdev->bd_part->nr_sects <<9 ); +bd_set_nr_sectors (bdev, bdev->bd_part->nr_sects);set_init_blocksize (bdev); }
include/linux/genhd.h +1 -1
@@ -375,7 +375,7 @@ void unregister_blkdev(unsigned int major, const char *name);int revalidate_disk (struct gendisk *disk);int check_disk_change (struct block_device *bdev);int __invalidate_device (struct block_device *bdev,bool kill_dirty); -void bd_set_size (struct block_device *bdev,loff_t size); +void bd_set_nr_sectors (struct block_device *bdev,sector_t sectors);/* for drivers/char/raw.c: */ int blkdev_ioctl (struct block_device *,fmode_t ,unsigned ,unsigned long );
Signature · 1 line
2.28.0
Re: [PATCH 1/2] block: replace bd_set_size with bd_set_nr_sectors
Quoted 4 lines · Christoph Hellwig
Replace
bd_set_sizewith a version that takes the number of sectors instead, as that fits most of the current and future callers much better.Signed-off-by Christoph Hellwig <[email protected]>
Quoted 9 lines
--- drivers/block/loop.c | 4 ++-- drivers/block/nbd.c | 7 ++++--- drivers/block/pktcdvd.c | 2 +- drivers/nvme/host/nvme.h | 2 +- fs/block_dev.c | 10 +++++----- include/linux/genhd.h | 2 +- 6 files changed, 14 insertions(+), 13 deletions(-)
Cheers,
Hannes
Signature · 4 lines
Dr. Hannes Reinecke Kernel Storage Architect [email protected] +49 911 74053 688 SUSE Software Solutions Germany GmbH, Maxfeldstr. 5, 90409 Nürnberg HRB 36809 (AG Nürnberg), GF: Felix Imendörffer
Christoph Hellwig
maintainerPATCH 2/2 block: fix locking for struct block_device size updates
In reply to Christoph HellwigTwo different callers use two different mutexes for updating the block device size, which obviously doesn't help to actually protect against concurrent updates from the different callers. In addition one of the locks, bd_mutex is rather prone to deadlocks with other parts of the block stack that use it for high level synchronization.
Switch to using a new spinlock protecting just the size updates, as that is all we need, and make sure everyone does the update through the proper helper.
This fixeѕ a bug reported with the nvme revalidating disks during a hot removal operation.
--- block/partitions/core.c | 4 ++-- drivers/block/aoe/aoecmd.c | 4 +--- drivers/md/dm.c | 15 ++------------- drivers/s390/block/dasd_ioctl.c | 9 ++------- fs/block_dev.c | 18 +++++++++--------- include/linux/blk_types.h | 1 + 6 files changed, 17 insertions(+), 34 deletions(-)
block/partitions/core.c +2 -2
@@ -585,8 +585,8 @@ int bdev_resize_partition(struct block_device *bdev, int partno,if (partition_overlaps (bdev->bd_disk, start, length, partno)) goto out_unlock; -part_nr_sects_write (part, (sector_t )length); -i_size_write (bdevp->bd_inode, length <<SECTOR_SHIFT ); +part_nr_sects_write (part, length); +bd_set_nr_sectors (bdevp, length); ret =0 ; out_unlock:
drivers/block/aoe/aoecmd.c +1 -3
@@ -900,9 +900,7 @@ aoecmd_sleepwork(struct work_struct *work) ssize =get_capacity (d->gd); bd =bdget_disk (d->gd,0 );if (bd) { -inode_lock (bd->bd_inode); -i_size_write (bd->bd_inode, (loff_t )ssize<<9 ); -inode_unlock (bd->bd_inode); +bd_set_nr_sectors (bd, ssize);bdput (bd); }spin_lock_irq (&d->lock);
drivers/md/dm.c +2 -13
@@ -2097,18 +2097,6 @@ static void event_callback(void *context)dm_issue_global_event (); } -/* -* Protected by md->suspend_lock obtained by dm_swap_table(). -*/ -static void __set_size (struct mapped_device *md,sector_t size) -{ -lockdep_assert_held (&md->suspend_lock); - -set_capacity (md->disk, size); - -i_size_write (md->bdev->bd_inode, (loff_t )size <<SECTOR_SHIFT ); -} -/* * Returns old map, which caller must destroy. */
@@ -2131,7 +2119,8 @@ static struct dm_table *__bind(struct mapped_device *md, struct dm_table *t,if (size !=dm_get_size (md))memset (&md->geometry,0 ,sizeof (md->geometry)); -__set_size (md, size); +set_capacity (md->disk, size); +bd_set_nr_sectors (md->bdev, size);dm_table_event_callback (t, event_callback, md);
drivers/s390/block/dasd_ioctl.c +2 -7
@@ -55,10 +55,7 @@ dasd_ioctl_enable(struct block_device *bdev)dasd_enable_device (base);/* Formatting the dasd device can change the capacity. */ -mutex_lock (&bdev->bd_mutex); -i_size_write (bdev->bd_inode, - (loff_t )get_capacity (base->block->gdp) <<9 ); -mutex_unlock (&bdev->bd_mutex); +bd_set_nr_sectors (bdev,get_capacity (base->block->gdp));dasd_put_device (base);return 0 ; }
@@ -91,9 +88,7 @@ dasd_ioctl_disable(struct block_device *bdev)* Set i_size to zero, since read, write, etc. check against this * value. */ -mutex_lock (&bdev->bd_mutex); -i_size_write (bdev->bd_inode,0 ); -mutex_unlock (&bdev->bd_mutex); +bd_set_nr_sectors (bdev,0 );dasd_put_device (base);return 0 ; }
fs/block_dev.c +9 -9
@@ -876,6 +876,7 @@ struct block_device *bdget(dev_t dev) bdev = &BDEV_I (inode)->bdev;if (inode->i_state &I_NEW ) { +spin_lock_init (&bdev->bd_size_lock); bdev->bd_contains =NULL ; bdev->bd_super =NULL ; bdev->bd_inode = inode;
@@ -1290,6 +1291,7 @@ static void check_disk_size_change(struct gendisk *disk, {loff_t disk_size, bdev_size; +spin_lock (&bdev->bd_size_lock); disk_size = (loff_t )get_capacity (disk) <<9 ; bdev_size =i_size_read (bdev->bd_inode);if (disk_size != bdev_size) {
@@ -1304,6 +1306,7 @@ static void check_disk_size_change(struct gendisk *disk, disk->disk_name); } bdev->bd_invalidated =0 ; +spin_unlock (&bdev->bd_size_lock); }/**
@@ -1328,13 +1331,10 @@ int revalidate_disk(struct gendisk *disk)if (!(disk->flags &GENHD_FL_HIDDEN )) {struct block_device *bdev =bdget_disk (disk,0 ); -if (!bdev) -return ret; - -mutex_lock (&bdev->bd_mutex); -check_disk_size_change (disk, bdev, ret ==0 ); -mutex_unlock (&bdev->bd_mutex); -bdput (bdev); +if (bdev) { +check_disk_size_change (disk, bdev, ret ==0 ); +bdput (bdev); + } }return ret; }
@@ -1373,9 +1373,9 @@ EXPORT_SYMBOL(check_disk_change);void bd_set_nr_sectors (struct block_device *bdev,sector_t sectors) { -inode_lock (bdev->bd_inode); +spin_lock (&bdev->bd_size_lock);i_size_write (bdev->bd_inode, (loff_t )sectors <<SECTOR_SHIFT ); -inode_unlock (bdev->bd_inode); +spin_unlock (&bdev->bd_size_lock); }EXPORT_SYMBOL (bd_set_nr_sectors);
include/linux/blk_types.h +1 -0
@@ -38,6 +38,7 @@ struct block_device {/* number of times partitions within this device have been opened. */ unsigned bd_part_count;int bd_invalidated; +spinlock_t bd_size_lock;/* for bd_inode->i_size updates */ struct gendisk * bd_disk;struct backing_dev_info *bd_bdi;
Signature · 1 line
2.28.0
Re: [PATCH 2/2] block: fix locking for struct block_device size updates
Quoted 15 lines · Christoph Hellwig
Two different callers use two different mutexes for updating the block device size, which obviously doesn't help to actually protect against concurrent updates from the different callers. In addition one of the locks,
bd_mutexis rather prone to deadlocks with other parts of the block stack that use it for high level synchronization.Switch to using a new spinlock protecting just the size updates, as that is all we need, and make sure everyone does the update through the proper helper.
This fixeѕ a bug reported with the nvme revalidating disks during a hot removal operation.
Reported-by Xianting Tian <[email protected]>Signed-off-by Christoph Hellwig <[email protected]>
Quoted 9 lines
--- block/partitions/core.c | 4 ++-- drivers/block/aoe/aoecmd.c | 4 +--- drivers/md/dm.c | 15 ++------------- drivers/s390/block/dasd_ioctl.c | 9 ++------- fs/block_dev.c | 18 +++++++++--------- include/linux/blk_types.h | 1 + 6 files changed, 17 insertions(+), 34 deletions(-)
Cheers,
Hannes
Signature · 4 lines
Dr. Hannes Reinecke Kernel Storage Architect [email protected] +49 911 74053 688 SUSE Software Solutions Germany GmbH, Maxfeldstr. 5, 90409 Nürnberg HRB 36809 (AG Nürnberg), GF: Felix Imendörffer
田
Reviewed-by Hannes ReineckeRe: [PATCH 2/2] block: fix locking for struct block_device size updates
thanks Hellwig for your kindly reply and your fix and add report by me :)
On 08/21/2020 17:26, Hannes Reinecke wrote:
Quoted 15 lines · Christoph Hellwig
Two different callers use two different mutexes for updating the block device size, which obviously doesn't help to actually protect against concurrent updates from the different callers. In addition one of the locks,
bd_mutexis rather prone to deadlocks with other parts of the block stack that use it for high level synchronization.Switch to using a new spinlock protecting just the size updates, as that is all we need, and make sure everyone does the update through the proper helper.
This fixeѕ a bug reported with the nvme revalidating disks during a hot removal operation.
Reported-by Xianting Tian <[email protected]>Signed-off-by Christoph Hellwig <[email protected]>
Quoted 9 lines
--- block/partitions/core.c | 4 ++-- drivers/block/aoe/aoecmd.c | 4 +--- drivers/md/dm.c | 15 ++------------- drivers/s390/block/dasd_ioctl.c | 9 ++------- fs/block_dev.c | 18 +++++++++--------- include/linux/blk_types.h | 1 + 6 files changed, 17 insertions(+), 34 deletions(-)
Cheers,
Hannes
--
Dr. Hannes Reinecke Kernel Storage Architect
[email protected] +49 911 74053 688
SUSE Software Solutions Germany GmbH, Maxfeldstr. 5, 90409 Nürnberg HRB 36809 (AG Nürnberg), GF: Felix Imendörffer
Christoph Hellwig
maintainerRe: [PATCH 2/2] block: fix locking for struct block_device size updates
Quoted 1 line · On Fri, Aug 21, 2020 at 06:02:32PM +0800, 田 wrote:
thanks Hellwig for your kindly reply and your fix and add report by me :)
I found an issue with the patch, and will send a new version in a bit. If this works for your reproducer, can you add a Tested-by: tag?