public inbox for linux-block@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCHv3 1/2] block: make BLK_DEF_MAX_SECTORS unsigned
@ 2022-12-27 19:10 Keith Busch
  2022-12-27 19:10 ` [PATCHv3 2/2] block: save user max_sectors limit Keith Busch
  2022-12-28 15:53 ` [PATCHv3 1/2] block: make BLK_DEF_MAX_SECTORS unsigned Christoph Hellwig
  0 siblings, 2 replies; 5+ messages in thread
From: Keith Busch @ 2022-12-27 19:10 UTC (permalink / raw)
  To: linux-block, axboe; +Cc: hch, martin.petersen, Damien Le Moal, Keith Busch

From: Keith Busch <kbusch@kernel.org>

This is used as an unsigned value, so define it that way to avoid
having to cast it.

Suggested-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Keith Busch <kbusch@kernel.org>
---
 block/blk-settings.c          | 2 +-
 drivers/block/null_blk/main.c | 3 +--
 include/linux/blkdev.h        | 3 ++-
 3 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/block/blk-settings.c b/block/blk-settings.c
index 0477c4d527fee..9875ca131eb0c 100644
--- a/block/blk-settings.c
+++ b/block/blk-settings.c
@@ -135,7 +135,7 @@ void blk_queue_max_hw_sectors(struct request_queue *q, unsigned int max_hw_secto
 	limits->max_hw_sectors = max_hw_sectors;
 
 	max_sectors = min_not_zero(max_hw_sectors, limits->max_dev_sectors);
-	max_sectors = min_t(unsigned int, max_sectors, BLK_DEF_MAX_SECTORS);
+	max_sectors = min(max_sectors, BLK_DEF_MAX_SECTORS);
 	max_sectors = round_down(max_sectors,
 				 limits->logical_block_size >> SECTOR_SHIFT);
 	limits->max_sectors = max_sectors;
diff --git a/drivers/block/null_blk/main.c b/drivers/block/null_blk/main.c
index 7d28e3aa406c2..4c601ca9552a0 100644
--- a/drivers/block/null_blk/main.c
+++ b/drivers/block/null_blk/main.c
@@ -2123,8 +2123,7 @@ static int null_add_dev(struct nullb_device *dev)
 	blk_queue_physical_block_size(nullb->q, dev->blocksize);
 	if (!dev->max_sectors)
 		dev->max_sectors = queue_max_hw_sectors(nullb->q);
-	dev->max_sectors = min_t(unsigned int, dev->max_sectors,
-				 BLK_DEF_MAX_SECTORS);
+	dev->max_sectors = min(dev->max_sectors, BLK_DEF_MAX_SECTORS);
 	blk_queue_max_hw_sectors(nullb->q, dev->max_sectors);
 
 	if (dev->virt_boundary)
diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
index 301cf1cf4f2fa..69f7199d38da8 100644
--- a/include/linux/blkdev.h
+++ b/include/linux/blkdev.h
@@ -1095,11 +1095,12 @@ static inline bool bdev_is_partition(struct block_device *bdev)
 enum blk_default_limits {
 	BLK_MAX_SEGMENTS	= 128,
 	BLK_SAFE_MAX_SECTORS	= 255,
-	BLK_DEF_MAX_SECTORS	= 2560,
 	BLK_MAX_SEGMENT_SIZE	= 65536,
 	BLK_SEG_BOUNDARY_MASK	= 0xFFFFFFFFUL,
 };
 
+#define BLK_DEF_MAX_SECTORS 2560u
+
 static inline unsigned long queue_segment_boundary(const struct request_queue *q)
 {
 	return q->limits.seg_boundary_mask;
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCHv3 2/2] block: save user max_sectors limit
  2022-12-27 19:10 [PATCHv3 1/2] block: make BLK_DEF_MAX_SECTORS unsigned Keith Busch
@ 2022-12-27 19:10 ` Keith Busch
       [not found]   ` <202212280931.RDVmPBnX-lkp@intel.com>
  2022-12-28 15:53 ` [PATCHv3 1/2] block: make BLK_DEF_MAX_SECTORS unsigned Christoph Hellwig
  1 sibling, 1 reply; 5+ messages in thread
From: Keith Busch @ 2022-12-27 19:10 UTC (permalink / raw)
  To: linux-block, axboe; +Cc: hch, martin.petersen, Damien Le Moal, Keith Busch

From: Keith Busch <kbusch@kernel.org>

The user can set the max_sectors limit to any valid value via sysfs
/sys/block/<dev>/queue/max_sectors_kb attribute. If the device limits
are ever rescanned, though, the limit reverts back to the potentially
artificially low BLK_DEF_MAX_SECTORS value.

Preserve the user's setting as the max_sectors limit as long as it's
valid. The user can reset back to defaults by writing 0 to the sysfs
file.

Signed-off-by: Keith Busch <kbusch@kernel.org>
---
v2->v3:

  Added documentation update (Damien)

  Using the unsigned BLK_DEF_MAX_SECTORS (Christoph)

 Documentation/ABI/stable/sysfs-block |  3 ++-
 block/blk-settings.c                 |  9 +++++++--
 block/blk-sysfs.c                    | 13 ++++++++++---
 include/linux/blkdev.h               |  1 +
 4 files changed, 20 insertions(+), 6 deletions(-)

diff --git a/Documentation/ABI/stable/sysfs-block b/Documentation/ABI/stable/sysfs-block
index cd14ecb3c9a5a..ac1e519272aa2 100644
--- a/Documentation/ABI/stable/sysfs-block
+++ b/Documentation/ABI/stable/sysfs-block
@@ -432,7 +432,8 @@ Contact:	linux-block@vger.kernel.org
 Description:
 		[RW] This is the maximum number of kilobytes that the block
 		layer will allow for a filesystem request. Must be smaller than
-		or equal to the maximum size allowed by the hardware.
+		or equal to the maximum size allowed by the hardware. Write 0
+		to use default kernel settings.
 
 
 What:		/sys/block/<disk>/queue/max_segment_size
diff --git a/block/blk-settings.c b/block/blk-settings.c
index 9875ca131eb0c..9c9713c9269cc 100644
--- a/block/blk-settings.c
+++ b/block/blk-settings.c
@@ -40,7 +40,7 @@ void blk_set_default_limits(struct queue_limits *lim)
 	lim->virt_boundary_mask = 0;
 	lim->max_segment_size = BLK_MAX_SEGMENT_SIZE;
 	lim->max_sectors = lim->max_hw_sectors = BLK_SAFE_MAX_SECTORS;
-	lim->max_dev_sectors = 0;
+	lim->max_user_sectors = lim->max_dev_sectors = 0;
 	lim->chunk_sectors = 0;
 	lim->max_write_zeroes_sectors = 0;
 	lim->max_zone_append_sectors = 0;
@@ -135,7 +135,12 @@ void blk_queue_max_hw_sectors(struct request_queue *q, unsigned int max_hw_secto
 	limits->max_hw_sectors = max_hw_sectors;
 
 	max_sectors = min_not_zero(max_hw_sectors, limits->max_dev_sectors);
-	max_sectors = min(max_sectors, BLK_DEF_MAX_SECTORS);
+
+	if (limits->max_user_sectors)
+		max_sectors = min(max_sectors, limits->max_user_sectors);
+	else
+		max_sectors = min(max_sectors, BLK_DEF_MAX_SECTORS);
+
 	max_sectors = round_down(max_sectors,
 				 limits->logical_block_size >> SECTOR_SHIFT);
 	limits->max_sectors = max_sectors;
diff --git a/block/blk-sysfs.c b/block/blk-sysfs.c
index 93d9e9c9a6ea8..e67acd859d072 100644
--- a/block/blk-sysfs.c
+++ b/block/blk-sysfs.c
@@ -249,9 +249,16 @@ queue_max_sectors_store(struct request_queue *q, const char *page, size_t count)
 
 	max_hw_sectors_kb = min_not_zero(max_hw_sectors_kb, (unsigned long)
 					 q->limits.max_dev_sectors >> 1);
-
-	if (max_sectors_kb > max_hw_sectors_kb || max_sectors_kb < page_kb)
-		return -EINVAL;
+	if (max_sectors_kb == 0) {
+		q->limits.max_user_sectors = 0;
+		max_sectors_kb = min(max_hw_sectors_kb,
+				     BLK_DEF_MAX_SECTORS >> 1);
+	} else {
+		if (max_sectors_kb > max_hw_sectors_kb ||
+		    max_sectors_kb < page_kb)
+			return -EINVAL;
+		q->limits.max_user_sectors = max_sectors_kb << 1;
+	}
 
 	spin_lock_irq(&q->queue_lock);
 	q->limits.max_sectors = max_sectors_kb << 1;
diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
index 69f7199d38da8..8f5bb00d12ece 100644
--- a/include/linux/blkdev.h
+++ b/include/linux/blkdev.h
@@ -288,6 +288,7 @@ struct queue_limits {
 	unsigned int		max_dev_sectors;
 	unsigned int		chunk_sectors;
 	unsigned int		max_sectors;
+	unsigned int		max_user_sectors;
 	unsigned int		max_segment_size;
 	unsigned int		physical_block_size;
 	unsigned int		logical_block_size;
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCHv3 2/2] block: save user max_sectors limit
       [not found]   ` <202212280931.RDVmPBnX-lkp@intel.com>
@ 2022-12-28 15:51     ` Christoph Hellwig
  2022-12-28 16:06       ` Keith Busch
  0 siblings, 1 reply; 5+ messages in thread
From: Christoph Hellwig @ 2022-12-28 15:51 UTC (permalink / raw)
  To: kernel test robot
  Cc: Keith Busch, linux-block, axboe, llvm, oe-kbuild-all, hch,
	martin.petersen, Damien Le Moal, Keith Busch

On Wed, Dec 28, 2022 at 10:04:16AM +0800, kernel test robot wrote:
> >> block/blk-sysfs.c:254:20: warning: comparison of distinct pointer types ('typeof (max_hw_sectors_kb) *' (aka 'unsigned long *') and 'typeof (2560U >> 1) *' (aka 'unsigned int *')) [-Wcompare-distinct-pointer-types]

Seems like the local max_sectors_kb variable also needs to be turned
into an unsigned int from the current unsigned long.

And btw, while I'm getting philosophical during my early morning
coffee here, I wonder if we need to actually get rid of or at least
bump BLK_DEF_MAX_SECTORS.  It's pretty ridiculous for modern devices,
and with CDL support that defintion would also include hard drives
eventually.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCHv3 1/2] block: make BLK_DEF_MAX_SECTORS unsigned
  2022-12-27 19:10 [PATCHv3 1/2] block: make BLK_DEF_MAX_SECTORS unsigned Keith Busch
  2022-12-27 19:10 ` [PATCHv3 2/2] block: save user max_sectors limit Keith Busch
@ 2022-12-28 15:53 ` Christoph Hellwig
  1 sibling, 0 replies; 5+ messages in thread
From: Christoph Hellwig @ 2022-12-28 15:53 UTC (permalink / raw)
  To: Keith Busch
  Cc: linux-block, axboe, hch, martin.petersen, Damien Le Moal,
	Keith Busch

>  enum blk_default_limits {
>  	BLK_MAX_SEGMENTS	= 128,
>  	BLK_SAFE_MAX_SECTORS	= 255,
> -	BLK_DEF_MAX_SECTORS	= 2560,
>  	BLK_MAX_SEGMENT_SIZE	= 65536,
>  	BLK_SEG_BOUNDARY_MASK	= 0xFFFFFFFFUL,
>  };

Looking at the enum all these really should be unsigned values anyway,
so we might as well keep the enum.

Either way this looks good:

Reviewed-by: Christoph Hellwig <hch@lst.de>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCHv3 2/2] block: save user max_sectors limit
  2022-12-28 15:51     ` Christoph Hellwig
@ 2022-12-28 16:06       ` Keith Busch
  0 siblings, 0 replies; 5+ messages in thread
From: Keith Busch @ 2022-12-28 16:06 UTC (permalink / raw)
  To: Christoph Hellwig
  Cc: kernel test robot, Keith Busch, linux-block, axboe, llvm,
	oe-kbuild-all, martin.petersen, Damien Le Moal

On Wed, Dec 28, 2022 at 04:51:11PM +0100, Christoph Hellwig wrote:
> On Wed, Dec 28, 2022 at 10:04:16AM +0800, kernel test robot wrote:
> > >> block/blk-sysfs.c:254:20: warning: comparison of distinct pointer types ('typeof (max_hw_sectors_kb) *' (aka 'unsigned long *') and 'typeof (2560U >> 1) *' (aka 'unsigned int *')) [-Wcompare-distinct-pointer-types]
> 
> Seems like the local max_sectors_kb variable also needs to be turned
> into an unsigned int from the current unsigned long.

Yeah, and gcc didn't complain about it, so looks like I need to add
clang to my dev setup.
 
> And btw, while I'm getting philosophical during my early morning
> coffee here, I wonder if we need to actually get rid of or at least
> bump BLK_DEF_MAX_SECTORS.  It's pretty ridiculous for modern devices,
> and with CDL support that defintion would also include hard drives
> eventually.

It looks like there's a bit of history around that value. I was hoping
we could just get rid of it entirely and let drivers choose their own
defaults.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-12-28 16:07 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-12-27 19:10 [PATCHv3 1/2] block: make BLK_DEF_MAX_SECTORS unsigned Keith Busch
2022-12-27 19:10 ` [PATCHv3 2/2] block: save user max_sectors limit Keith Busch
     [not found]   ` <202212280931.RDVmPBnX-lkp@intel.com>
2022-12-28 15:51     ` Christoph Hellwig
2022-12-28 16:06       ` Keith Busch
2022-12-28 15:53 ` [PATCHv3 1/2] block: make BLK_DEF_MAX_SECTORS unsigned Christoph Hellwig

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox