Validate logical block size in blk_validate_limits()
Subsystems (3)
v2 cover Validate logical block size in blk_validate_limits()
This series adds validation of the logical block size in blk_validate_limits().
Some drivers had already been validating this themselves. As such, we can mostly drop that driver validation.
nbd is problematic, as we cannot only change to just stop calling blk_validate_limits(). This is because the LBS is updated in a 2-stage process:
a. update block size in the driver and validate
b. update queue limits
So if we stop validating the limits in a., there is a user-visible change in behaviour (as we stop rejecting invalid limits from the NBD_SET_BLKSIZE ioctl). So I left that untouched.
This topic was originally mentioned in [0] and then again in [1] by Keith.
I have also included a related virtio_blk change to deal with blk_size config fallback.
[0] https://lore.kernel.org/linux-block/[email protected]/
[1] https://lore.kernel.org/linux-block/[email protected]/
Differences to v1:
- Add RB tags (thanks!)
- Update comment on blk_validate_block_size() and add print in blk_validate_limits()
John Garry (5):
virtio_blk: Fix default logical block size fallback
block: Validate logical block size in blk_validate_limits()
null_blk: Don't bother validating blocksize
virtio_blk: Don't bother validating blocksize
loop: Don't bother validating blocksize
block/blk-settings.c | 4 ++++ drivers/block/loop.c | 12 +----------- drivers/block/null_blk/main.c | 3 --- drivers/block/virtio_blk.c | 31 +++++++++++-------------------- include/linux/blkdev.h | 1 + 5 files changed, 17 insertions(+), 34 deletions(-)
Signature · 1 line
2.31.1
PATCH v2 1/5 virtio_blk: Fix default logical block size fallback
If we fail to read a logical block size in virtblk_read_limits() -> virtio_cread_feature(), then we default to what is in lim->logical_block_size, but that would be 0.
We can deal with lim->logical_block_size = 0 later in the blk_mq_alloc_disk(), but the code in virtblk_read_limits() needs a proper default, so give a default of SECTOR_SIZE.
--- drivers/block/virtio_blk.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-)
drivers/block/virtio_blk.c +13 -13
@@ -1250,7 +1250,7 @@ static int virtblk_read_limits(struct virtio_blk *vblk, struct queue_limits *lim) {struct virtio_device *vdev = vblk->vdev; -u32 v, blk_size, max_size, sg_elems, opt_io_size; +u32 v, max_size, sg_elems, opt_io_size;u32 max_discard_segs =0 ;u32 discard_granularity =0 ;u16 min_io_size;
@@ -1291,44 +1291,43 @@ static int virtblk_read_limits(struct virtio_blk *vblk,/* Host can optionally specify the block size of the device */ err =virtio_cread_feature (vdev,VIRTIO_BLK_F_BLK_SIZE , struct virtio_blk_config, blk_size, - &blk_size); + &lim->logical_block_size);if (!err) { - err =blk_validate_block_size (blk_size); + err =blk_validate_block_size (lim->logical_block_size);if (err) {dev_err (&vdev->dev,"virtio_blk: invalid block size: 0x%x\n" , - blk_size); + lim->logical_block_size);return err; } - - lim->logical_block_size = blk_size; - }else - blk_size = lim->logical_block_size; + }/* Use topology information if available */ err =virtio_cread_feature (vdev,VIRTIO_BLK_F_TOPOLOGY , struct virtio_blk_config, physical_block_exp, &physical_block_exp);if (!err && physical_block_exp) - lim->physical_block_size = blk_size * (1 << physical_block_exp); + lim->physical_block_size = + lim->logical_block_size * (1 << physical_block_exp); err =virtio_cread_feature (vdev,VIRTIO_BLK_F_TOPOLOGY , struct virtio_blk_config, alignment_offset, &alignment_offset);if (!err && alignment_offset) - lim->alignment_offset = blk_size * alignment_offset; + lim->alignment_offset = + lim->logical_block_size * alignment_offset; err =virtio_cread_feature (vdev,VIRTIO_BLK_F_TOPOLOGY , struct virtio_blk_config, min_io_size, &min_io_size);if (!err && min_io_size) - lim->io_min = blk_size * min_io_size; + lim->io_min = lim->logical_block_size * min_io_size; err =virtio_cread_feature (vdev,VIRTIO_BLK_F_TOPOLOGY , struct virtio_blk_config, opt_io_size, &opt_io_size);if (!err && opt_io_size) - lim->io_opt = blk_size * opt_io_size; + lim->io_opt = lim->logical_block_size * opt_io_size;if (virtio_has_feature (vdev,VIRTIO_BLK_F_DISCARD )) { virtio_cread(vdev, struct virtio_blk_config,
@@ -1422,7 +1421,7 @@ static int virtblk_read_limits(struct virtio_blk *vblk, lim->discard_granularity = discard_granularity <<SECTOR_SHIFT ; else - lim->discard_granularity = blk_size; + lim->discard_granularity = lim->logical_block_size; }if (virtio_has_feature (vdev,VIRTIO_BLK_F_ZONED )) {
@@ -1453,6 +1452,7 @@ static int virtblk_probe(struct virtio_device *vdev)struct virtio_blk *vblk;struct queue_limits lim = { .features =BLK_FEAT_ROTATIONAL , + .logical_block_size =SECTOR_SIZE , };int err, index;unsigned int queue_depth;
Signature · 1 line
2.31.1
PATCH v2 2/5 block: Validate logical block size in blk_validate_limits()
In reply to John GarrySome drivers validate that their own logical block size. It is no harm to always do this, so validate in blk_validate_limits().
This allows us to remove the validation in most of those drivers.
Add a comment to blk_validate_block_size() to inform users that self- validation of LBS is usually unnecessary.
--- block/blk-settings.c | 4 ++++ include/linux/blkdev.h | 1 + 2 files changed, 5 insertions(+)
block/blk-settings.c +4 -0
@@ -235,6 +235,10 @@ static int blk_validate_limits(struct queue_limits *lim)*/ if (!lim->logical_block_size) lim->logical_block_size =SECTOR_SIZE ; +else if (blk_validate_block_size (lim->logical_block_size)) { +pr_warn ("Invalid logical block size (%d)\n" , lim->logical_block_size); +return -EINVAL ; + }if (lim->physical_block_size < lim->logical_block_size) lim->physical_block_size = lim->logical_block_size;
include/linux/blkdev.h +1 -0
@@ -268,6 +268,7 @@ static inline dev_t disk_devt(struct gendisk *disk)return MKDEV (disk->major, disk->first_minor); } +/* blk_validate_limits() validates bsize, so drivers don't usually need to */ static inline int blk_validate_block_size (unsigned long bsize) {if (bsize <512 || bsize >PAGE_SIZE || !is_power_of_2 (bsize))
Signature · 1 line
2.31.1
Re: [PATCH v2 2/5] block: Validate logical block size in blk_validate_limits()
Quoted 19 lines · John Garry
Some drivers validate that their own logical block size. It is no harm to always do this, so validate in
blk_validate_limits().This allows us to remove the validation in most of those drivers.
Add a comment to
blk_validate_block_size()to inform users that self- validation of LBS is usually unnecessary.Signed-off-by John Garry <[email protected]>--- block/blk-settings.c | 4 ++++ include/linux/blkdev.h | 1 + 2 files changed, 5 insertions(+)block/blk-settings.c +0
-0
block/blk-settings.c static int blk_validate_limits(struct queue_limits *lim) · line 239
Quoted 5 lines
block/blk-settings.c +2
-0@@ -235,6 +235,10 @@ static int blk_validate_limits(struct queue_limits *lim)*/ if (!lim->logical_block_size) lim->logical_block_size =SECTOR_SIZE ; +else if (blk_validate_block_size (lim->logical_block_size)) { +pr_warn ("Invalid logical block size (%d)\n" , lim->logical_block_size);
logical_block_size is an unsigned int so this needs to use %u.
With this nit fixed, feel free to add:
Quoted 17 lines
block/blk-settings.c +2
-0+return -EINVAL ; + }if (lim->physical_block_size < lim->logical_block_size) lim->physical_block_size = lim->logical_block_size;include/linux/blkdev.h +1
-0@@ -268,6 +268,7 @@ static inline dev_t disk_devt(struct gendisk *disk)return MKDEV (disk->major, disk->first_minor); } +/* blk_validate_limits() validates bsize, so drivers don't usually need to */ static inline int blk_validate_block_size (unsigned long bsize) {if (bsize <512 || bsize >PAGE_SIZE || !is_power_of_2 (bsize))
Signature · 2 lines
Damien Le Moal Western Digital Research
PATCH v2 3/5 null_blk: Don't bother validating blocksize
In reply to John GarryThe block queue limits validation does this for us now.
--- drivers/block/null_blk/main.c | 3 --- 1 file changed, 3 deletions(-)
drivers/block/null_blk/main.c +0 -3
@@ -1831,9 +1831,6 @@ static int null_validate_conf(struct nullb_device *dev) dev->queue_mode =NULL_Q_MQ ; } -if (blk_validate_block_size (dev->blocksize)) -return -EINVAL ; -if (dev->use_per_node_hctx) {if (dev->submit_queues != nr_online_nodes) dev->submit_queues = nr_online_nodes;
Signature · 1 line
2.31.1
Re: [PATCH v2 3/5] null_blk: Don't bother validating blocksize
Quoted 13 lines · John Garry
The block queue limits validation does this for us now.
Reviewed-by Christoph Hellwig <[email protected]>Signed-off-by John Garry <[email protected]>--- drivers/block/null_blk/main.c | 3 --- 1 file changed, 3 deletions(-)drivers/block/null_blk/main.c +0
-0
drivers/block/null_blk/main.c static int null_validate_conf(struct nullb_device *dev) · line 1835
Quoted 5 lines
drivers/block/null_blk/main.c +0
-2@@ -1831,9 +1831,6 @@ static int null_validate_conf(struct nullb_device *dev) dev->queue_mode =NULL_Q_MQ ; } -if (blk_validate_block_size (dev->blocksize)) -return -EINVAL ;
Thanks,
Zhu Yanjun
Quoted 4 lines
drivers/block/null_blk/main.c +0
-1-if (dev->use_per_node_hctx) {if (dev->submit_queues != nr_online_nodes) dev->submit_queues = nr_online_nodes;
Re: [PATCH v2 3/5] null_blk: Don't bother validating blocksize
In reply to John GarryQuoted 4 lines · John Garry
The block queue limits validation does this for us now.
Reviewed-by Christoph Hellwig <[email protected]>Signed-off-by John Garry <[email protected]>
Signature · 2 lines
Damien Le Moal Western Digital Research
PATCH v2 4/5 virtio_blk: Don't bother validating blocksize
In reply to John GarryThe block queue limits validation does this for us now.
--- drivers/block/virtio_blk.c | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-)
drivers/block/virtio_blk.c +1 -10
@@ -1289,18 +1289,9 @@ static int virtblk_read_limits(struct virtio_blk *vblk, lim->max_segment_size = max_size;/* Host can optionally specify the block size of the device */ - err =virtio_cread_feature (vdev,VIRTIO_BLK_F_BLK_SIZE , +virtio_cread_feature (vdev,VIRTIO_BLK_F_BLK_SIZE , struct virtio_blk_config, blk_size, &lim->logical_block_size); -if (!err) { - err =blk_validate_block_size (lim->logical_block_size); -if (err) { -dev_err (&vdev->dev, -"virtio_blk: invalid block size: 0x%x\n" , - lim->logical_block_size); -return err; - } - }/* Use topology information if available */ err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
Signature · 1 line
2.31.1
Re: [PATCH v2 4/5] virtio_blk: Don't bother validating blocksize
Quoted 15 lines · John Garry
The block queue limits validation does this for us now.
Reviewed-by Christoph Hellwig <[email protected]>Reviewed-by Stefan Hajnoczi <[email protected]>Signed-off-by John Garry <[email protected]>--- drivers/block/virtio_blk.c | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-)drivers/block/virtio_blk.c +0
-0
drivers/block/virtio_blk.c static int virtblk_read_limits(struct virtio_blk *vblk, · line 1294
Quoted 7 lines
drivers/block/virtio_blk.c +1
-1@@ -1289,18 +1289,9 @@ static int virtblk_read_limits(struct virtio_blk *vblk, lim->max_segment_size = max_size;/* Host can optionally specify the block size of the device */ - err =virtio_cread_feature (vdev,VIRTIO_BLK_F_BLK_SIZE , +virtio_cread_feature (vdev,VIRTIO_BLK_F_BLK_SIZE , struct virtio_blk_config, blk_size, &lim->logical_block_size);
I really wonder why this does not check that the VIRTIO_BLK_F_BLK_SIZE feature exists... But that is not the fault of this patch :)
Quoted 12 lines
drivers/block/virtio_blk.c +0
-9-if (!err) { - err =blk_validate_block_size (lim->logical_block_size); -if (err) { -dev_err (&vdev->dev, -"virtio_blk: invalid block size: 0x%x\n" , - lim->logical_block_size); -return err; - } - }/* Use topology information if available */ err = virtio_cread_feature(vdev, VIRTIO_BLK_F_TOPOLOGY,
Signature · 2 lines
Damien Le Moal Western Digital Research
PATCH v2 5/5 loop: Don't bother validating blocksize
In reply to John GarryThe block queue limits validation does this for us now.
The loop_configure() -> WARN_ON_ONCE() call is dropped, as an invalid block size would trigger this now. We don't want userspace to be able to directly trigger WARNs.
--- drivers/block/loop.c | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-)
drivers/block/loop.c +1 -11
@@ -1061,12 +1061,6 @@ static int loop_configure(struct loop_device *lo, blk_mode_t mode, goto out_unlock; } -if (config->block_size) { - error =blk_validate_block_size (config->block_size); -if (error) - goto out_unlock; - } - error =loop_set_status_from_info (lo, &config->info);if (error) goto out_unlock;
@@ -1098,7 +1092,7 @@ static int loop_configure(struct loop_device *lo, blk_mode_t mode,mapping_set_gfp_mask (mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS)); error =loop_reconfigure_limits (lo, config->block_size); -if (WARN_ON_ONCE (error)) +if (error) goto out_unlock;loop_update_dio (lo);
@@ -1470,10 +1464,6 @@ static int loop_set_block_size(struct loop_device *lo, unsigned long arg)if (lo->lo_state != Lo_bound)return -ENXIO ; - err =blk_validate_block_size (arg); -if (err) -return err; -if (lo->lo_queue->limits.logical_block_size == arg)return 0 ;
Signature · 1 line
2.31.1
Re: [PATCH v2 5/5] loop: Don't bother validating blocksize
Quoted 8 lines · John Garry
The block queue limits validation does this for us now.
The
loop_configure()->WARN_ON_ONCE()call is dropped, as an invalid block size would trigger this now. We don't want userspace to be able to directly trigger WARNs.Reviewed-by Christoph Hellwig <[email protected]>Signed-off-by John Garry <[email protected]>
Signature · 2 lines
Damien Le Moal Western Digital Research
Quoted 6 lines · John Garry
This series adds validation of the logical block size in
blk_validate_limits().Some drivers had already been validating this themselves. As such, we can mostly drop that driver validation.
Quoted 7 lines
nbd is problematic, as we cannot only change to just stop calling
blk_validate_limits(). This is because the LBS is updated in a 2-stage process:
a. update block size in the driver and validate
b. update queue limits[...]
Applied, thanks!
[1/5] virtio_blk: Fix default logical block size fallback
commit: 4ff3d01275dee10ed1f40c314ba5133e3c7a6e1b
[2/5] block: Validate logical block size in blk_validate_limits()
commit: fe3d508ba95bc63adba661355115be340275c0d1
[3/5] null_blk: Don't bother validating blocksize
commit: addc3a68de850fa25dcf937705e721d8eec22470
[4/5] virtio_blk: Don't bother validating blocksize
commit: af2817229158cea7960b9132e0a8c4470ebbfef5
[5/5] loop: Don't bother validating blocksize
commit: 9423c653fe611070d875b374fb322dc44acce3f2Best regards,
Signature · 1 line
Jens Axboe