* [PATCH 0/2] scsi: sd: Fix WRITE SAME(10) data corruption
@ 2026-11-07 16:35 Chris Down
2026-11-07 16:35 ` [PATCH 1/2] scsi: sd: Fix data corruption in WRITE SAME(10) write zeroes Chris Down
2026-11-07 16:35 ` [PATCH 2/2] scsi: sd: Fix data corruption in WRITE SAME(10) discards Chris Down
0 siblings, 2 replies; 7+ messages in thread
From: Chris Down @ 2026-11-07 16:35 UTC (permalink / raw)
To: Martin K. Petersen, James E.J. Bottomley
Cc: Christoph Hellwig, linux-scsi, linux-kernel, kernel-team
With scsi_debug and 512-byte logical blocks, a 1 MiB discard at 2 TiB
succeeds, but it's the first MiB of the disk that gets discarded. This
happens because the starting LBA wraps to zero, because it doesn't fit
in WRITE SAME(10).
Write zeroes can also run out of bits for the block count. With a
0x20000-block WRITE SAME limit, a 64 MiB hole punch sends a count of
zero and leaves the whole range as it was.
Let's use plain WRITE SAME(16) for write zeroes when the LBA or count
won't fit in WRITE SAME(10). For discard, we can just refuse the request
and leave the data alone.
Chris Down (2):
scsi: sd: Fix data corruption in WRITE SAME(10) write zeroes
scsi: sd: Fix data corruption in WRITE SAME(10) discards
drivers/scsi/sd.c | 13 ++++++++++++-
1 file changed, 12 insertions(+), 1 deletion(-)
base-commit: 04feb5fd2c23894ebc2cdaf4a4cc38b8667b17ac
--
2.54.0
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 1/2] scsi: sd: Fix data corruption in WRITE SAME(10) write zeroes
2026-11-07 16:35 [PATCH 0/2] scsi: sd: Fix WRITE SAME(10) data corruption Chris Down
@ 2026-11-07 16:35 ` Chris Down
2026-11-11 16:46 ` Christoph Hellwig
2026-11-07 16:35 ` [PATCH 2/2] scsi: sd: Fix data corruption in WRITE SAME(10) discards Chris Down
1 sibling, 1 reply; 7+ messages in thread
From: Chris Down @ 2026-11-07 16:35 UTC (permalink / raw)
To: Martin K. Petersen, James E.J. Bottomley
Cc: Christoph Hellwig, linux-scsi, linux-kernel, kernel-team
In writesame_10_unmap mode, we send write zeroes as WRITE SAME(10).
If the LBA or count doesn't fit, we truncate it and can return
success without zeroing the requested range. If the LBA wraps, we
can also corrupt live data near the start of the disk.
Cap the queue limit when sd selects this mode so large requests can
still unmap in chunks. If the LBA or count still won't fit, fall back
to WRITE SAME(16) with UNMAP clear. All the caller needs is for the
requested blocks to read back as zeroes.
Fixes: e4b878371b85 ("sd: implement unmapping Write Zeroes")
Cc: <stable@vger.kernel.org>
Signed-off-by: Chris Down <chris@chrisdown.name>
---
drivers/scsi/sd.c | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)
diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
index dc45df819c42..a28d11886aca 100644
--- a/drivers/scsi/sd.c
+++ b/drivers/scsi/sd.c
@@ -1164,7 +1164,10 @@ static blk_status_t sd_setup_write_zeroes_cmnd(struct scsi_cmnd *cmd)
case SD_ZERO_WS16_UNMAP:
return sd_setup_write_same16_cmnd(cmd, true);
case SD_ZERO_WS10_UNMAP:
- return sd_setup_write_same10_cmnd(cmd, true);
+ if (lba <= 0xffffffff && nr_blocks <= 0xffff)
+ return sd_setup_write_same10_cmnd(cmd, true);
+ /* Unmapping is optional for write zeroes. */
+ break;
}
}
@@ -1221,6 +1224,11 @@ static void sd_config_write_same(struct scsi_disk *sdkp,
else
sdkp->zeroing_mode = SD_ZERO_WRITE;
+ /* Split large requests so they can still unmap. */
+ if (sdkp->zeroing_mode == SD_ZERO_WS10_UNMAP)
+ sdkp->max_ws_blocks = min(sdkp->max_ws_blocks,
+ (u32)SD_MAX_WS10_BLOCKS);
+
if (sdkp->max_ws_blocks &&
sdkp->physical_block_size > logical_block_size) {
/*
--
2.54.0
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 2/2] scsi: sd: Fix data corruption in WRITE SAME(10) discards
2026-11-07 16:35 [PATCH 0/2] scsi: sd: Fix WRITE SAME(10) data corruption Chris Down
2026-11-07 16:35 ` [PATCH 1/2] scsi: sd: Fix data corruption in WRITE SAME(10) write zeroes Chris Down
@ 2026-11-07 16:35 ` Chris Down
2026-11-11 16:47 ` Christoph Hellwig
1 sibling, 1 reply; 7+ messages in thread
From: Chris Down @ 2026-11-07 16:35 UTC (permalink / raw)
To: Martin K. Petersen, James E.J. Bottomley
Cc: Christoph Hellwig, linux-scsi, linux-kernel, kernel-team
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.
Discard is advisory, so return BLK_STS_NOTSUPP if the request won't
fit in WRITE SAME(10). Discards that fit can still go through.
Fixes: c98a0eb0e90d ("[SCSI] sd: Logical Block Provisioning update")
Cc: <stable@vger.kernel.org>
Signed-off-by: Chris Down <chris@chrisdown.name>
---
drivers/scsi/sd.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
index a28d11886aca..9b8530267b4a 100644
--- a/drivers/scsi/sd.c
+++ b/drivers/scsi/sd.c
@@ -1131,6 +1131,9 @@ static blk_status_t sd_setup_write_same10_cmnd(struct scsi_cmnd *cmd,
u32 data_len = sdp->sector_size;
blk_status_t ret;
+ if (lba > 0xffffffff || nr_blocks > 0xffff)
+ return BLK_STS_NOTSUPP;
+
if (!sd_set_special_bvec(cmd, data_len))
return BLK_STS_RESOURCE;
--
2.54.0
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 1/2] scsi: sd: Fix data corruption in WRITE SAME(10) write zeroes
2026-11-07 16:35 ` [PATCH 1/2] scsi: sd: Fix data corruption in WRITE SAME(10) write zeroes Chris Down
@ 2026-11-11 16:46 ` Christoph Hellwig
2026-11-28 9:26 ` Chris Down
0 siblings, 1 reply; 7+ messages in thread
From: Christoph Hellwig @ 2026-11-11 16:46 UTC (permalink / raw)
To: Chris Down
Cc: Martin K. Petersen, James E.J. Bottomley, Christoph Hellwig,
linux-scsi, linux-kernel, kernel-team
On Sat, Nov 07, 2026 at 08:35:14AM -0800, Chris Down wrote:
> In writesame_10_unmap mode, we send write zeroes as WRITE SAME(10).
> If the LBA or count doesn't fit, we truncate it and can return
> success without zeroing the requested range. If the LBA wraps, we
> can also corrupt live data near the start of the disk.
>
> Cap the queue limit when sd selects this mode so large requests can
> still unmap in chunks. If the LBA or count still won't fit, fall back
> to WRITE SAME(16) with UNMAP clear. All the caller needs is for the
> requested blocks to read back as zeroes.
Shoudn't we simply refuse to use writesame_10_unmap for devices
where 10-byte commands can't fit the entire capacity instead?
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 2/2] scsi: sd: Fix data corruption in WRITE SAME(10) discards
2026-11-07 16:35 ` [PATCH 2/2] scsi: sd: Fix data corruption in WRITE SAME(10) discards Chris Down
@ 2026-11-11 16:47 ` Christoph Hellwig
2026-11-28 9:26 ` Chris Down
0 siblings, 1 reply; 7+ messages in thread
From: Christoph Hellwig @ 2026-11-11 16:47 UTC (permalink / raw)
To: Chris Down
Cc: Martin K. Petersen, James E.J. Bottomley, Christoph Hellwig,
linux-scsi, linux-kernel, kernel-team
On Sat, Nov 07, 2026 at 08:35:29AM -0800, Chris Down wrote:
> 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.
>
> Discard is advisory, so return BLK_STS_NOTSUPP if the request won't
> fit in WRITE SAME(10). Discards that fit can still go through.
Nah, that's not really how things work. See my reply to patch 1
for the suggested proper fix.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 1/2] scsi: sd: Fix data corruption in WRITE SAME(10) write zeroes
2026-11-11 16:46 ` Christoph Hellwig
@ 2026-11-28 9:26 ` Chris Down
0 siblings, 0 replies; 7+ messages in thread
From: Chris Down @ 2026-11-28 9:26 UTC (permalink / raw)
To: Christoph Hellwig
Cc: Martin K. Petersen, James E.J. Bottomley, linux-scsi,
linux-kernel, kernel-team
Hi Christoph,
Christoph Hellwig writes:
>Shoudn't we simply refuse to use writesame_10_unmap for devices
>where 10-byte commands can't fit the entire capacity instead?
Yeah, that's simpler. I'll do that in v2, both when sd picks the mode
and when it's written to zeroing_mode in sysfs.
That only takes care of the LBA, though. sd_config_write_same() keeps a
write zeroes limit above 0xffff blocks if the device reports one, so
the count can still be truncated in writesame_10_unmap mode, even on a
small disk. I think we should still cap the limit in that mode, as
sd_config_discard() already does for the discard limit in writesame_10
mode. Does that sound ok?
Thanks!
Chris
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 2/2] scsi: sd: Fix data corruption in WRITE SAME(10) discards
2026-11-11 16:47 ` Christoph Hellwig
@ 2026-11-28 9:26 ` Chris Down
0 siblings, 0 replies; 7+ messages in thread
From: Chris Down @ 2026-11-28 9:26 UTC (permalink / raw)
To: Christoph Hellwig
Cc: Martin K. Petersen, James E.J. Bottomley, linux-scsi,
linux-kernel, kernel-team
Hi Christoph,
Christoph Hellwig writes:
>Nah, that's not really how things work. See my reply to patch 1
>for the suggested proper fix.
Sure, I'll do the same for writesame_10 and writesame_zero in v2.
Even with that, a discard can still reach sd_setup_write_same10_cmnd()
with a count that doesn't fit, though.
One case is a discard that is already queued when the mode changes. sd
sets provisioning_mode before it freezes the queue to commit the new
limits, so that request is still sized for the old mode. For that one
I'll set the mode with the queue frozen instead, as
nvme_update_ns_info_block() does for its own state.
The other is that __bio_split_discard() doesn't split a discard at all
when max_discard_sectors is zero or below the discard granularity. With
scsi_debug, 512-byte blocks and an unmap granularity of 0x10000 blocks,
a 64 MiB discard in writesame_10 mode goes out as a single WRITE
SAME(10) with a count of zero. sd can avoid that one by disabling
discard when the granularity is above its limit, as drbd does. But
writing 0 to discard_max_bytes while a large discard is running also
sends the rest of the bio to sd unsplit, and I don't see how sd can
prevent that with its limits.
An LBA that doesn't fit can still get there as well. bio_check_eod()
only checks a bio against the partition it was submitted to. If a disk
shrinks to 2^32 blocks or fewer while a partition on it still ends
above that, a discard to the end of that partition is sent as WRITE
SAME(10) with its LBA truncated. sd_setup_read_write_cmnd() fails reads
and writes there with "access beyond end of device", but discards and
write zeroes have no such check.
Would you rather I fix those in the block layer, or have sd check the
LBA and count before it builds the command?
Chris
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-11-28 9:26 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-07 16:35 [PATCH 0/2] scsi: sd: Fix WRITE SAME(10) data corruption Chris Down
2026-11-07 16:35 ` [PATCH 1/2] scsi: sd: Fix data corruption in WRITE SAME(10) write zeroes Chris Down
2026-11-11 16:46 ` Christoph Hellwig
2026-11-28 9:26 ` Chris Down
2026-11-07 16:35 ` [PATCH 2/2] scsi: sd: Fix data corruption in WRITE SAME(10) discards Chris Down
2026-11-11 16:47 ` Christoph Hellwig
2026-11-28 9:26 ` Chris Down
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®