Skip to contentSkip to conversation

block: save user max_sectors limit

PATCH In review Keith Busch 4 participants 5 messages Dec 22, 2022 – Dec 27, 2022 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 block: save user max_sectors limit

From: Keith Busch <[email protected]>

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 kernel defined BLK_DEF_MAX_SECTORS limit.

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

---
 block/blk-settings.c   |  9 +++++++--
 block/blk-sysfs.c      | 10 +++++++++-
 include/linux/blkdev.h |  1 +
 3 files changed, 17 insertions(+), 3 deletions(-)
block/blk-settings.c +7 -2
index 0477c4d527fee..e75304f853bd5 100644
@@ -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_t(unsigned int, max_sectors, BLK_DEF_MAX_SECTORS);
+
+	if (limits->max_user_sectors)
+		max_sectors = min_not_zero(max_sectors, limits->max_user_sectors);
+	else
+		max_sectors = min_t(unsigned int, max_sectors, BLK_DEF_MAX_SECTORS);
+
 	max_sectors = round_down(max_sectors,
 				 limits->logical_block_size >> SECTOR_SHIFT);
 	limits->max_sectors = max_sectors;
block/blk-sysfs.c +9 -1
index 93d9e9c9a6ea8..db5d1d908f5d9 100644
@@ -250,8 +250,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)
+	if (max_sectors_kb == 0) {
+		q->limits.max_user_sectors = 0;
+		max_sectors_kb = min_t(unsigned int, 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;
+	} else {
+		q->limits.max_user_sectors = max_sectors_kb << 1;
+	}
 
 	spin_lock_irq(&q->queue_lock);
 	q->limits.max_sectors = max_sectors_kb << 1;
include/linux/blkdev.h +1 -0
index 301cf1cf4f2fa..71e97f0a87264 100644
@@ -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;
Signature · 1 line
2.30.2
#1 lore raw
Quoted 32 lines · Keith Busch

From: Keith Busch <[email protected]>

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 kernel defined BLK_DEF_MAX_SECTORS limit.

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

---
 block/blk-settings.c   |  9 +++++++--
 block/blk-sysfs.c      | 10 +++++++++-
 include/linux/blkdev.h |  1 +
 3 files changed, 17 insertions(+), 3 deletions(-)
block/blk-settings.c +1 -1
index 0477c4d527fee..e75304f853bd5 100644
@@ -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;

block/blk-settings.c void blk_queue_max_hw_sectors(struct request_queue *q, unsigned int max_hw_secto · line 140

Quoted 7 lines
block/blk-settings.c +3 -1
@@ -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_t(unsigned int, max_sectors, BLK_DEF_MAX_SECTORS);
+
+	if (limits->max_user_sectors)
+		max_sectors = min_not_zero(max_sectors, limits->max_user_sectors);

I don't think the min_not_zero here makes sense, as you just checked max_user_sectors for zero above, and max_sectors better not be zero.

block/blk-settings.c void blk_queue_max_hw_sectors(struct request_queue *q, unsigned int max_hw_secto · line 142

Quoted 2 lines
block/blk-settings.c +2 -0
+	else
+		max_sectors = min_t(unsigned int, max_sectors, BLK_DEF_MAX_SECTORS);

And please make BLK_DEF_MAX_SECTORS an unsigned constant as it should be and drop the min_t here. Instead of working around type mismatches just avoid them from the beginning..

block/blk-settings.c void blk_queue_max_hw_sectors(struct request_queue *q, unsigned int max_hw_secto · line 146

Quoted 4 lines
block/blk-settings.c +4 -0
+	if (max_sectors_kb == 0) {
+		q->limits.max_user_sectors = 0;
+		max_sectors_kb = min_t(unsigned int, max_hw_sectors_kb,
+				       BLK_DEF_MAX_SECTORS >> 1);

.. which will also pay off here.

block/blk-settings.c void blk_queue_max_hw_sectors(struct request_queue *q, unsigned int max_hw_secto

Quoted 3 lines
block/blk-settings.c +2 -0
+	} else if (max_sectors_kb > max_hw_sectors_kb ||
+		   max_sectors_kb < page_kb)  {
 		return -EINVAL;

And this check should probably move above the old one to keep the sanity checks first.

block/blk-settings.c

Quoted 3 lines
block/blk-settings.c +3 -0
+	} else {
+		q->limits.max_user_sectors = max_sectors_kb << 1;
+	}

i.e.

	if (max_sectors_kb > max_hw_sectors_kb || max_sectors_kb < page_kb)
		return -EINVAL;

	q->limits.max_user_sectors = max_sectors_kb << 1;

	/* reset to default when the user clears max_sectors_kb: */
	if (max_sectors_kb == 0) {
		max_sectors_kb =
			min(max_hw_sectors_kb, BLK_DEF_MAX_SECTORS >> 1);
	}
#2 lore raw
Quoted 6 lines · Christoph Hellwig

block/blk-settings.c void blk_queue_max_hw_sectors(struct request_queue *q, unsigned int max_hw_secto · line 142

Quoted 1 line · Keith Busch
block/blk-settings.c +1 -0
+		max_sectors = min_t(unsigned int, max_sectors, BLK_DEF_MAX_SECTORS);

And please make BLK_DEF_MAX_SECTORS an unsigned constant as it should be and drop the min_t here. Instead of working around type mismatches just avoid them from the beginning..

Let me just make sure I understand: you don't want BLK_DEF_MAX_SECTORS defined in an enum, and instead want it a 'const unsigned int'? Or #define?

Quoted 13 lines

block/blk-sysfs.c queue_max_sectors_store(struct request_queue *q, const char *page, size_t count) · line 259

Quoted 3 lines
block/blk-sysfs.c +2 -0
+	} else if (max_sectors_kb > max_hw_sectors_kb ||
+		   max_sectors_kb < page_kb)  {
 		return -EINVAL;

And this check should probably move above the old one to keep the sanity checks first.

block/blk-sysfs.c queue_max_sectors_store(struct request_queue *q, const char *page, size_t count) · line 262

Quoted 3 lines
block/blk-sysfs.c +3 -0
+	} else {
+		q->limits.max_user_sectors = max_sectors_kb << 1;
+	}

i.e.

Quoted 7 lines
	if (max_sectors_kb > max_hw_sectors_kb || max_sectors_kb < page_kb)
		return -EINVAL;

	q->limits.max_user_sectors = max_sectors_kb << 1;

	/* reset to default when the user clears max_sectors_kb: */
	if (max_sectors_kb == 0) {

We'll never reach here since 0 is < page_kb. But I think I can clean this up a little bit for the next version.

Quoted 3 lines
		max_sectors_kb =
			min(max_hw_sectors_kb, BLK_DEF_MAX_SECTORS >> 1);
	}
#3 lore raw
Quoted 6 lines · Keith Busch
Quoted 2 lines

and drop the min_t here. Instead of working around type mismatches just avoid them from the beginning..

Let me just make sure I understand: you don't want BLK_DEF_MAX_SECTORS defined in an enum, and instead want it a 'const unsigned int'? Or #define?

Just make sure it's marked as unsigned, either and enum or define works, just make sure it has the u suffix. Note that for enums recent gccs enforce the same type over different embers, so you might have to split it from an existing catchall enum into a separate one.

#4 lore raw
In reply to Keith Busch
Quoted 10 lines · Keith Busch

From: Keith Busch <[email protected]>

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 kernel defined BLK_DEF_MAX_SECTORS limit.

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

Updating Documentation/ABI/stable/sysfs-block about this change would be nice too.

Quoted 69 lines
---
 block/blk-settings.c   |  9 +++++++--
 block/blk-sysfs.c      | 10 +++++++++-
 include/linux/blkdev.h |  1 +
 3 files changed, 17 insertions(+), 3 deletions(-)
block/blk-settings.c +7 -2
index 0477c4d527fee..e75304f853bd5 100644
@@ -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_t(unsigned int, max_sectors, BLK_DEF_MAX_SECTORS);
+
+	if (limits->max_user_sectors)
+		max_sectors = min_not_zero(max_sectors, limits->max_user_sectors);
+	else
+		max_sectors = min_t(unsigned int, max_sectors, BLK_DEF_MAX_SECTORS);
+
 	max_sectors = round_down(max_sectors,
 				 limits->logical_block_size >> SECTOR_SHIFT);
 	limits->max_sectors = max_sectors;
block/blk-sysfs.c +9 -1
index 93d9e9c9a6ea8..db5d1d908f5d9 100644
@@ -250,8 +250,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)
+	if (max_sectors_kb == 0) {
+		q->limits.max_user_sectors = 0;
+		max_sectors_kb = min_t(unsigned int, 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;
+	} else {
+		q->limits.max_user_sectors = max_sectors_kb << 1;
+	}
 
 	spin_lock_irq(&q->queue_lock);
 	q->limits.max_sectors = max_sectors_kb << 1;
include/linux/blkdev.h +1 -0
index 301cf1cf4f2fa..71e97f0a87264 100644
@@ -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;
Signature · 2 lines
Damien Le Moal
Western Digital Research