[PATCH v2 5/5] scsi: sd: Fix unsplit discards when the granularity exceeds the limit

From: Chris Down

Date: Sat Nov 28 2026 - 04:26:55 EST


The block layer doesn't split a discard when the discard granularity is
larger than max_discard_sectors. A disk in writesame_10 mode with an
unmap granularity of 0x10000 blocks then gets a 0x20000-block discard
as one request, and sends that as WRITE SAME(10) with a count of zero.
The other modes have room for the count, but the discard is still
larger than the limit we set.

mmc and drbd already make sure they don't report a granularity above
the discard limit. Let's do the same here and disable discard in that
case, as drbd_reconsider_queue_parameters() does.

A zero limit isn't enough on its own, because a discard that was
submitted before the limit changed still reaches us, and isn't split
either. So set provisioning_mode to disabled as well, which makes
sd_init_command() fail it.

Fixes: c98a0eb0e90d ("[SCSI] sd: Logical Block Provisioning update")
Cc: <stable@xxxxxxxxxxxxxxx>
Signed-off-by: Chris Down <chris@xxxxxxxxxxxxxx>
---
drivers/scsi/sd.c | 11 +++++++++++
1 file changed, 11 insertions(+)

diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
index 792058f52f65..ef9c2d55c933 100644
--- a/drivers/scsi/sd.c
+++ b/drivers/scsi/sd.c
@@ -206,6 +206,17 @@ static void sd_config_discard(struct scsi_disk *sdkp, struct queue_limits *lim,

lim->max_hw_discard_sectors = max_blocks *
(logical_block_size >> SECTOR_SHIFT);
+
+ /*
+ * The block layer doesn't split a discard if the limit is below the
+ * granularity, so we'd get discards of any size.
+ */
+ if (lim->max_hw_discard_sectors &&
+ lim->max_hw_discard_sectors <
+ lim->discard_granularity >> SECTOR_SHIFT) {
+ sdkp->provisioning_mode = SD_LBP_DISABLE;
+ lim->max_hw_discard_sectors = 0;
+ }
}

static void sd_set_flush_flag(struct scsi_disk *sdkp,
--
2.56.1