Skip to contentSkip to conversation

block: ensure discard_granularity is zero when discard is not supported

PATCH Applied Christoph Hellwig 3 participants 3 messages Jul 31, 2025 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 block: ensure discard_granularity is zero when discard is not supported

Documentation/ABI/stable/sysfs-block states:

What: /sys/block/<disk>/queue/discard_granularity [...]
A discard_granularity of 0 means that the device does not support discard functionality.

but this got broken when sorting out the block limits updates. Fix this by setting the discard_granularity limit to zero when the combined max_discard_sectors is zero.

Fixes 3c407dc723bb ("block: default the discard granularity to sector size")
---
 block/blk-settings.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
block/blk-settings.c +10 -3
index a000daafbfb4..20f8b207d5e8 100644
@@ -336,12 +336,19 @@ int blk_validate_limits(struct queue_limits *lim)
 	lim->max_discard_sectors =
 		min(lim->max_hw_discard_sectors, lim->max_user_discard_sectors);
 
+	/*
+	 * When discard is not supported, discard_granularity should be reported
+	 * as 0 to userspace.
+	 */
+	if (lim->max_discard_sectors)
+		lim->discard_granularity =
+			max(lim->discard_granularity, lim->physical_block_size);
+	else
+		lim->discard_granularity = 0;
+
 	if (!lim->max_discard_segments)
 		lim->max_discard_segments = 1;
 
-	if (lim->discard_granularity < lim->physical_block_size)
-		lim->discard_granularity = lim->physical_block_size;
-
 	/*
 	 * By default there is no limit on the segment boundary alignment,
 	 * but if there is one it can't be smaller than the page size as
Signature · 1 line
2.47.2
Reviewed-by on v1 1/1
#1 lore raw

Christoph,

block/blk-settings.c int blk_validate_limits(struct queue_limits *lim) · line 348

Quoted 10 lines
block/blk-settings.c +10 -0
+	/*
+	 * When discard is not supported, discard_granularity should be reported
+	 * as 0 to userspace.
+	 */
+	if (lim->max_discard_sectors)
+		lim->discard_granularity =
+			max(lim->discard_granularity, lim->physical_block_size);
+	else
+		lim->discard_granularity = 0;
+

Looks good to me!

Signature · 1 line
Martin K. Petersen
maintainer Applied
#2 lore raw
In reply to Christoph Hellwig
Quoted 8 lines · Christoph Hellwig

Documentation/ABI/stable/sysfs-block states:

What: /sys/block/<disk>/queue/discard_granularity [...]
A discard_granularity of 0 means that the device does not support discard functionality.

[...]

Applied, thanks!

[1/1] block: ensure discard_granularity is zero when discard is not supported

      commit: fad6551fcf537375702b9af012508156a16a1ff7

Best regards,

Signature · 1 line
Jens Axboe