* [PATCH v2 1/5] scsi: sd: Fix stale max_hw_wzeroes_unmap_sectors after a mode change
2026-11-28 9:26 [PATCH v2 0/5] scsi: sd: Fix WRITE SAME(10) data corruption Chris Down
@ 2026-11-28 9:26 ` Chris Down
2026-11-28 9:26 ` [PATCH v2 2/5] scsi: sd: Fix data corruption in WRITE SAME(10) write zeroes Chris Down
` (3 subsequent siblings)
4 siblings, 0 replies; 7+ messages in thread
From: Chris Down @ 2026-11-28 9:26 UTC (permalink / raw)
To: Martin K. Petersen, James E.J. Bottomley
Cc: Christoph Hellwig, linux-scsi, linux-kernel, kernel-team
sd_config_write_same() sets max_hw_wzeroes_unmap_sectors when the
zeroing mode requires unmap. However, it does not clear it later. If
the mode changes to one that doesn't unmap, the old value stays, and
the queue still reports that write zeroes will unmap.
blk_validate_limits() also wants max_hw_wzeroes_unmap_sectors to be
either zero or equal to max_write_zeroes_sectors. This means that if
the write zeroes limit changes as well, the whole update is rejected
and none of the new limits are applied.
This can already be triggered from sysfs. On a disk that was in one of
the unmap modes, writing "write" to zeroing_mode and then 0 to
max_write_same_blocks makes that store fail with -EINVAL. From that
point forward, so does the limits update in each revalidate. The next
patch makes the zeroing mode depend on the capacity as well, and then
resizing a disk can trigger it too.
So, let's clear the limit when the mode doesn't unmap.
Fixes: 6dffe079fe36 ("scsi: sd: set max_hw_wzeroes_unmap_sectors if device supports SD_ZERO_*_UNMAP")
Cc: <stable@vger.kernel.org>
Signed-off-by: Chris Down <chris@chrisdown.name>
---
drivers/scsi/sd.c | 2 ++
1 file changed, 2 insertions(+)
diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
index dc45df819c42..c7018a5de847 100644
--- a/drivers/scsi/sd.c
+++ b/drivers/scsi/sd.c
@@ -1249,6 +1249,8 @@ out:
sdkp->zeroing_mode == SD_ZERO_WS10_UNMAP)
lim->max_hw_wzeroes_unmap_sectors =
lim->max_write_zeroes_sectors;
+ else
+ lim->max_hw_wzeroes_unmap_sectors = 0;
}
static blk_status_t sd_setup_flush_cmnd(struct scsi_cmnd *cmd)
--
2.56.1
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH v2 2/5] scsi: sd: Fix data corruption in WRITE SAME(10) write zeroes
2026-11-28 9:26 [PATCH v2 0/5] scsi: sd: Fix WRITE SAME(10) data corruption Chris Down
2026-11-28 9:26 ` [PATCH v2 1/5] scsi: sd: Fix stale max_hw_wzeroes_unmap_sectors after a mode change Chris Down
@ 2026-11-28 9:26 ` Chris Down
2026-11-28 9:26 ` [PATCH v2 3/5] scsi: sd: Fix data corruption in WRITE SAME(10) discards Chris Down
` (2 subsequent siblings)
4 siblings, 0 replies; 7+ messages in thread
From: Chris Down @ 2026-11-28 9:26 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.
The LBA doesn't always fit because WRITE SAME(10) only has 32 bits for
it. Given that, we should only select writesame_10_unmap when a 32-bit
LBA can address the whole disk, and we shouldn't accept it in
zeroing_mode for a bigger disk either. A bigger disk gets WRITE SAME
without UNMAP instead, which already uses the 16-byte command where it
has to.
For the count, let's cap the queue limit when sd selects this mode so
large requests can still unmap in chunks. Writing to zeroing_mode
doesn't update the limits, so don't accept writesame_10_unmap there if
max_write_same_blocks is too big for it.
Fixes: e4b878371b85 ("sd: implement unmapping Write Zeroes")
Cc: <stable@vger.kernel.org>
Signed-off-by: Chris Down <chris@chrisdown.name>
---
drivers/scsi/sd.c | 18 +++++++++++++++++-
1 file changed, 17 insertions(+), 1 deletion(-)
diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
index c7018a5de847..88b1c1d56d2f 100644
--- a/drivers/scsi/sd.c
+++ b/drivers/scsi/sd.c
@@ -146,6 +146,12 @@ static void sd_large_pool_destroy(void)
mutex_unlock(&sd_mutex_lock);
}
+/* WRITE SAME(10) only has a 32-bit LBA field. */
+static bool sd_can_use_ws10(struct scsi_disk *sdkp)
+{
+ return sdkp->capacity <= 1 + (sector_t)0xffffffff;
+}
+
static void sd_disable_discard(struct scsi_disk *sdkp)
{
sdkp->provisioning_mode = SD_LBP_DISABLE;
@@ -630,6 +636,11 @@ zeroing_mode_store(struct device *dev, struct device_attribute *attr,
if (mode < 0)
return -EINVAL;
+ if (mode == SD_ZERO_WS10_UNMAP &&
+ (!sd_can_use_ws10(sdkp) ||
+ sdkp->max_ws_blocks > SD_MAX_WS10_BLOCKS))
+ return -EINVAL;
+
sdkp->zeroing_mode = mode;
return count;
@@ -1214,13 +1225,18 @@ static void sd_config_write_same(struct scsi_disk *sdkp,
if (sdkp->lbprz && sdkp->lbpws)
sdkp->zeroing_mode = SD_ZERO_WS16_UNMAP;
- else if (sdkp->lbprz && sdkp->lbpws10)
+ else if (sdkp->lbprz && sdkp->lbpws10 && sd_can_use_ws10(sdkp))
sdkp->zeroing_mode = SD_ZERO_WS10_UNMAP;
else if (sdkp->max_ws_blocks)
sdkp->zeroing_mode = SD_ZERO_WS;
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.56.1
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH v2 3/5] scsi: sd: Fix data corruption in WRITE SAME(10) discards
2026-11-28 9:26 [PATCH v2 0/5] scsi: sd: Fix WRITE SAME(10) data corruption Chris Down
2026-11-28 9:26 ` [PATCH v2 1/5] scsi: sd: Fix stale max_hw_wzeroes_unmap_sectors after a mode change Chris Down
2026-11-28 9:26 ` [PATCH v2 2/5] scsi: sd: Fix data corruption in WRITE SAME(10) write zeroes Chris Down
@ 2026-11-28 9:26 ` Chris Down
2026-11-28 9:26 ` [PATCH v2 4/5] scsi: sd: Fix WRITE SAME(10) count truncation when the mode changes Chris Down
2026-11-28 9:26 ` [PATCH v2 5/5] scsi: sd: Fix unsplit discards when the granularity exceeds the limit Chris Down
4 siblings, 0 replies; 7+ messages in thread
From: Chris Down @ 2026-11-28 9:26 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.
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@vger.kernel.org>
Signed-off-by: Chris Down <chris@chrisdown.name>
---
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
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH v2 4/5] scsi: sd: Fix WRITE SAME(10) count truncation when the mode changes
2026-11-28 9:26 [PATCH v2 0/5] scsi: sd: Fix WRITE SAME(10) data corruption Chris Down
` (2 preceding siblings ...)
2026-11-28 9:26 ` [PATCH v2 3/5] scsi: sd: Fix data corruption in WRITE SAME(10) discards Chris Down
@ 2026-11-28 9:26 ` Chris Down
2026-11-28 9:46 ` sashiko-bot
2026-11-28 9:26 ` [PATCH v2 5/5] scsi: sd: Fix unsplit discards when the granularity exceeds the limit Chris Down
4 siblings, 1 reply; 7+ messages in thread
From: Chris Down @ 2026-11-28 9:26 UTC (permalink / raw)
To: Martin K. Petersen, James E.J. Bottomley
Cc: Christoph Hellwig, linux-scsi, linux-kernel, kernel-team
sd sets provisioning_mode and zeroing_mode before it freezes the queue
to commit the new limits. Until then, requests are still sized for the
old mode, including the ones that are already queued. For example,
take a slow disk in unmap mode with a backlog of 0x20000-block
discards. Writing writesame_10 to provisioning_mode sends the queued
ones as WRITE SAME(10) with a count of zero. Now that the zeroing mode
depends on the capacity, the same happens to queued write zeroes when a
disk shrinks and sd moves it to writesame_10_unmap.
A request is only sized once it has entered the queue, so none that
were sized for the old limits are left once the queue is frozen. Let's
change the modes at that point, as nvme_update_ns_info_block() does for
its own state.
sd_revalidate_disk() can't freeze the queue while it's still sending
commands to the disk, so move sd_config_discard() down to where the
limits are committed. That is after the new capacity is set, though.
If the disk has just grown past 2^32 blocks, a request to the new
blocks would then go out as WRITE SAME(10) with its LBA truncated until
the mode changes. So, set the capacity after the modes when the disk
has grown. When it has shrunk it still has to come first, because the
modes for the smaller disk can't address all of the old one.
You might wonder why we don't set the capacity with the queue frozen,
as nvme does. That sends the uevent with the queue frozen, which
lockdep complains about.
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 | 36 +++++++++++++++++++++++++++++-------
1 file changed, 29 insertions(+), 7 deletions(-)
diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
index d995650bc8a1..792058f52f65 100644
--- a/drivers/scsi/sd.c
+++ b/drivers/scsi/sd.c
@@ -584,6 +584,7 @@ provisioning_mode_store(struct device *dev, struct device_attribute *attr,
struct scsi_disk *sdkp = to_scsi_disk(dev);
struct scsi_device *sdp = sdkp->device;
struct queue_limits lim;
+ unsigned int memflags;
int mode, err;
if (!capable(CAP_SYS_ADMIN))
@@ -603,8 +604,12 @@ provisioning_mode_store(struct device *dev, struct device_attribute *attr,
queue_limits_cancel_update(sdkp->disk->queue);
return -EINVAL;
}
+
+ /* Queued discards are sized for the old mode. */
+ memflags = blk_mq_freeze_queue(sdkp->disk->queue);
sd_config_discard(sdkp, &lim, mode);
- err = queue_limits_commit_update_frozen(sdkp->disk->queue, &lim);
+ err = queue_limits_commit_update(sdkp->disk->queue, &lim);
+ blk_mq_unfreeze_queue(sdkp->disk->queue, memflags);
if (err)
return err;
return count;
@@ -3803,7 +3808,9 @@ static void sd_revalidate_disk(struct gendisk *disk)
sector_t old_capacity = sdkp->capacity;
struct queue_limits *lim = NULL;
unsigned char *buffer = NULL;
- unsigned int dev_max;
+ unsigned int dev_max, memflags;
+ sector_t sectors;
+ bool grown;
int err;
SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp,
@@ -3857,8 +3864,6 @@ static void sd_revalidate_disk(struct gendisk *disk)
sd_zbc_read_zones(sdkp, lim, buffer);
}
- sd_config_discard(sdkp, lim, sd_discard_mode(sdkp));
-
sd_print_capacity(sdkp, old_capacity);
sd_read_write_protect_flag(sdkp, buffer);
@@ -3901,10 +3906,27 @@ static void sd_revalidate_disk(struct gendisk *disk)
sdkp->first_scan = 0;
- set_capacity_and_notify(disk, logical_to_sectors(sdp, sdkp->capacity));
- sd_config_write_same(sdkp, lim);
+ /*
+ * The WRITE SAME(10) modes can't address all of a disk with more than
+ * 2^32 blocks, so don't show a bigger capacity until they are gone.
+ */
+ sectors = logical_to_sectors(sdp, sdkp->capacity);
+ grown = sectors > get_capacity(disk);
+ if (!grown)
+ set_capacity_and_notify(disk, sectors);
- err = queue_limits_commit_update_frozen(sdkp->disk->queue, lim);
+ /*
+ * Queued requests are sized for the old modes, so only change the
+ * modes once the queue is frozen.
+ */
+ memflags = blk_mq_freeze_queue(sdkp->disk->queue);
+ if (sdkp->media_present)
+ sd_config_discard(sdkp, lim, sd_discard_mode(sdkp));
+ sd_config_write_same(sdkp, lim);
+ err = queue_limits_commit_update(sdkp->disk->queue, lim);
+ blk_mq_unfreeze_queue(sdkp->disk->queue, memflags);
+ if (grown)
+ set_capacity_and_notify(disk, sectors);
if (err)
goto out;
--
2.56.1
^ permalink raw reply related [flat|nested] 7+ messages in thread* Re: [PATCH v2 4/5] scsi: sd: Fix WRITE SAME(10) count truncation when the mode changes
2026-11-28 9:26 ` [PATCH v2 4/5] scsi: sd: Fix WRITE SAME(10) count truncation when the mode changes Chris Down
@ 2026-11-28 9:46 ` sashiko-bot
0 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-11-28 9:46 UTC (permalink / raw)
To: Chris Down; +Cc: linux-scsi
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] scsi: sd: unlocked set_capacity_and_notify() in
sd_revalidate_disk()
--
commit c8bf97f59b4ffaebd830de4ab08208e4fd13924d
Author: Chris Down <chris@chrisdown.name>
scsi: sd: Fix WRITE SAME(10) count truncation when the mode changes
This commit updates provisioning_mode and zeroing_mode only while the
request queue is frozen, and defers increasing the gendisk capacity in
sd_revalidate_disk() until after the modes and queue limits are updated.
> diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
> index d995650bc8a10..792058f52f65e 100644
> --- a/drivers/scsi/sd.c
> +++ b/drivers/scsi/sd.c
[ ... ]
> @@ -3901,10 +3906,27 @@ static void sd_revalidate_disk(struct gendisk *disk)
>
> sdkp->first_scan = 0;
>
> - set_capacity_and_notify(disk, logical_to_sectors(sdp, sdkp->capacity));
> - sd_config_write_same(sdkp, lim);
> + /*
> + * The WRITE SAME(10) modes can't address all of a disk with more than
> + * 2^32 blocks, so don't show a bigger capacity until they are gone.
> + */
> + sectors = logical_to_sectors(sdp, sdkp->capacity);
> + grown = sectors > get_capacity(disk);
> + if (!grown)
> + set_capacity_and_notify(disk, sectors);
>
> - err = queue_limits_commit_update_frozen(sdkp->disk->queue, lim);
> + /*
> + * Queued requests are sized for the old modes, so only change the
> + * modes once the queue is frozen.
> + */
> + memflags = blk_mq_freeze_queue(sdkp->disk->queue);
> + if (sdkp->media_present)
> + sd_config_discard(sdkp, lim, sd_discard_mode(sdkp));
> + sd_config_write_same(sdkp, lim);
> + err = queue_limits_commit_update(sdkp->disk->queue, lim);
> + blk_mq_unfreeze_queue(sdkp->disk->queue, memflags);
> + if (grown)
> + set_capacity_and_notify(disk, sectors);
> if (err)
> goto out;
[Severity: Medium]
Can calling set_capacity_and_notify() in the grown path after
queue_limits_commit_update() unlocks q->limits_lock race with a
concurrent sd_revalidate_disk() or update the capacity after a failed
limits commit?
sd_revalidate_disk() can be entered concurrently from sd_rescan(),
sd_open(), and cache_type_store(), relying on q->limits_lock to serialize
updates. When the disk grows past 2^32 blocks and then shrinks back:
CPU0 CPU1
sd_revalidate_disk()
queue_limits_start_update()
sd_read_capacity() (> 2^32)
grown = true
queue_limits_commit_update()
mutex_unlock(&q->limits_lock)
sd_revalidate_disk()
queue_limits_start_update()
sd_read_capacity() (<= 2^32)
grown = false
set_capacity_and_notify()
sd_config_discard() (SD_LBP_WS10)
queue_limits_commit_update()
set_capacity_and_notify()
<-- overwrites capacity with > 2^32 while in WRITE SAME(10) mode
In addition, because err is checked after set_capacity_and_notify(), a
failed queue_limits_commit_update() still publishes the new capacity while
leaving the old queue limits in place and skipping
sd_zbc_revalidate_zones().
--
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1795842371.git.chris@chrisdown.name?part=4
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v2 5/5] scsi: sd: Fix unsplit discards when the granularity exceeds the limit
2026-11-28 9:26 [PATCH v2 0/5] scsi: sd: Fix WRITE SAME(10) data corruption Chris Down
` (3 preceding siblings ...)
2026-11-28 9:26 ` [PATCH v2 4/5] scsi: sd: Fix WRITE SAME(10) count truncation when the mode changes Chris Down
@ 2026-11-28 9:26 ` Chris Down
4 siblings, 0 replies; 7+ messages in thread
From: Chris Down @ 2026-11-28 9:26 UTC (permalink / raw)
To: Martin K. Petersen, James E.J. Bottomley
Cc: Christoph Hellwig, linux-scsi, linux-kernel, kernel-team
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@vger.kernel.org>
Signed-off-by: Chris Down <chris@chrisdown.name>
---
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
^ permalink raw reply related [flat|nested] 7+ messages in thread