[PATCH v2 3/5] scsi: sd: Fix data corruption in WRITE SAME(10) discards

From: Chris Down

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


We already cap the discard length in writesame_10 and writesame_zero
mode, but we still truncate the starting LBA to 32 bits. A discard at
or above block 2^32 can corrupt live data near the start of the disk
and still report success.

The reason is that both modes send WRITE SAME(10), which only has a
32-bit LBA. Given this, let's only select writesame_10 when a 32-bit
LBA can address the whole disk, and not accept either of them in
provisioning_mode for a bigger disk. This means that a bigger disk that
only reports LBPWS10 ends up with discard disabled.

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

diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
index 88b1c1d56d2f..d995650bc8a1 100644
--- a/drivers/scsi/sd.c
+++ b/drivers/scsi/sd.c
@@ -597,6 +597,12 @@ provisioning_mode_store(struct device *dev, struct device_attribute *attr,
return -EINVAL;

lim = queue_limits_start_update(sdkp->disk->queue);
+ /* A revalidate updates the capacity under the limits lock. */
+ if ((mode == SD_LBP_WS10 || mode == SD_LBP_ZERO) &&
+ !sd_can_use_ws10(sdkp)) {
+ queue_limits_cancel_update(sdkp->disk->queue);
+ return -EINVAL;
+ }
sd_config_discard(sdkp, &lim, mode);
err = queue_limits_commit_update_frozen(sdkp->disk->queue, &lim);
if (err)
@@ -3431,7 +3437,7 @@ static unsigned int sd_discard_mode(struct scsi_disk *sdkp)
return SD_LBP_UNMAP;
if (sdkp->lbpws)
return SD_LBP_WS16;
- if (sdkp->lbpws10)
+ if (sdkp->lbpws10 && sd_can_use_ws10(sdkp))
return SD_LBP_WS10;
return SD_LBP_DISABLE;
}
--
2.56.1