diff mbox series

[2/5] block: Validate logical block size in blk_validate_limits()

Message ID 20240705115127.3417539-3-john.g.garry@oracle.com (mailing list archive)
State New, archived
Headers show
Series Validate logical block size in blk_validate_limits() | expand

Commit Message

John Garry July 5, 2024, 11:51 a.m. UTC
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 those drivers.

Add a comment to blk_validate_block_size() to inform users that self-
validation of LBS is unnecessary.

Signed-off-by: John Garry <john.g.garry@oracle.com>
---
 block/blk-settings.c   | 2 ++
 include/linux/blkdev.h | 1 +
 2 files changed, 3 insertions(+)

Comments

Christoph Hellwig July 5, 2024, 12:16 p.m. UTC | #1
>  	if (!lim->logical_block_size)
>  		lim->logical_block_size = SECTOR_SIZE;
> +	else if (blk_validate_block_size(lim->logical_block_size))
> +		return -EINVAL;

This should print a message.  Unfortunately we don't have the device
name here (for that we'd need to set it at disk/queue allocation time,
which will require a bit of work), but even without that it will be
very useful.

> +/* blk_validate_limits() validates bsize, so drivers don't need to */

maybe throw in a usually or normally?
John Garry July 5, 2024, 12:34 p.m. UTC | #2
On 05/07/2024 13:16, Christoph Hellwig wrote:
>>   	if (!lim->logical_block_size)
>>   		lim->logical_block_size = SECTOR_SIZE;
>> +	else if (blk_validate_block_size(lim->logical_block_size))
>> +		return -EINVAL;
> 
> This should print a message.  Unfortunately we don't have the device
> name here (for that we'd need to set it at disk/queue allocation time,
> which will require a bit of work), but even without that it will be
> very useful.

Ok, I can print a message, like:

	pr_warn("Invalid logical block size (%d)\n", bsize);

I am wary though that userspace could trigger this message from the 
various ioctls to set the bsize.

> 
>> +/* blk_validate_limits() validates bsize, so drivers don't need to */
> 
> maybe throw in a usually or normally?
> 

fine
Christoph Hellwig July 5, 2024, 12:38 p.m. UTC | #3
On Fri, Jul 05, 2024 at 01:34:37PM +0100, John Garry wrote:
>> This should print a message.  Unfortunately we don't have the device
>> name here (for that we'd need to set it at disk/queue allocation time,
>> which will require a bit of work), but even without that it will be
>> very useful.
>
> Ok, I can print a message, like:
>
> 	pr_warn("Invalid logical block size (%d)\n", bsize);
>
> I am wary though that userspace could trigger this message from the various 
> ioctls to set the bsize.

As anything ending up here is a configuration interface, such a message
should be perfectly fine.
diff mbox series

Patch

diff --git a/block/blk-settings.c b/block/blk-settings.c
index 9fa4eed4df06..55eef9541ce1 100644
--- a/block/blk-settings.c
+++ b/block/blk-settings.c
@@ -235,6 +235,8 @@  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))
+		return -EINVAL;
 	if (lim->physical_block_size < lim->logical_block_size)
 		lim->physical_block_size = lim->logical_block_size;
 
diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
index 02e04df27282..7eb165bcc069 100644
--- a/include/linux/blkdev.h
+++ b/include/linux/blkdev.h
@@ -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 need to */
 static inline int blk_validate_block_size(unsigned long bsize)
 {
 	if (bsize < 512 || bsize > PAGE_SIZE || !is_power_of_2(bsize))