* [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; 10+ 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 [flat|nested] 10+ 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-29 2:38 ` Bart Van Assche
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, 1 reply; 10+ 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 [flat|nested] 10+ messages in thread* Re: [PATCH v2 2/5] scsi: sd: Fix data corruption in WRITE SAME(10) write zeroes
2026-11-28 9:26 ` [PATCH v2 2/5] scsi: sd: Fix data corruption in WRITE SAME(10) write zeroes Chris Down
@ 2026-11-29 2:38 ` Bart Van Assche
0 siblings, 0 replies; 10+ messages in thread
From: Bart Van Assche @ 2026-11-29 2:38 UTC (permalink / raw)
To: Chris Down, Martin K. Petersen, James E.J. Bottomley
Cc: Christoph Hellwig, linux-scsi, linux-kernel, kernel-team
On 11/28/26 1:26 AM, Chris Down wrote:
> +/* 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;
> +}
(sector_t)U32_MAX + 1 is easier to read than `1 + (sector_t)0xffffffff`.
> @@ -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;
Unlike provisioning_mode_store() in patches 3 and 4, zeroing_mode_store()
neither acquires q->limits_lock nor freezes the queue (nor updates
max_hw_wzeroes_unmap_sectors). That leaves several races:
1. Race with sd_revalidate_disk(): zeroing_mode_store("writesame_10_unmap")
can pass the check while capacity <= 2^32 and max_ws_blocks <= 0xffff,
get preempted, and write sdkp->zeroing_mode = SD_ZERO_WS10_UNMAP after
sd_revalidate_disk() grows the disk past 2^32 blocks (or raises
max_ws_blocks > 0xffff) and switches away from SD_ZERO_WS10_UNMAP.
2. Race with max_write_same_blocks_store():
- Growing max_ws_blocks past 0xffff has the same TOCTOU, leaving
SD_ZERO_WS10_UNMAP active with max_write_zeroes_sectors > 0xffff
blocks.
- Shrinking max_ws_blocks while large write zeroes are queued:
max_write_same_blocks_store() writes sdkp->max_ws_blocks before
taking q->limits_lock or freezing the queue, then waits in
queue_limits_commit_update_frozen() for queued requests to drain.
A concurrent zeroing_mode_store("writesame_10_unmap") sees the new
smaller sdkp->max_ws_blocks and switches sdkp->zeroing_mode while
larger requests are still draining, causing count truncation.
If zeroing_mode_store() uses queue_limits_start_update() +
blk_mq_freeze_queue() + queue_limits_commit_update() + unfreeze (like
provisioning_mode_store()) and updates lim.max_hw_wzeroes_unmap_sectors,
it closes these races and fixes the stale unmap limit after sysfs
writes.
Thanks,
Bart.
^ permalink raw reply [flat|nested] 10+ 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-29 2:39 ` Bart Van Assche
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, 1 reply; 10+ 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 [flat|nested] 10+ messages in thread* Re: [PATCH v2 3/5] scsi: sd: Fix data corruption in WRITE SAME(10) discards
2026-11-28 9:26 ` [PATCH v2 3/5] scsi: sd: Fix data corruption in WRITE SAME(10) discards Chris Down
@ 2026-11-29 2:39 ` Bart Van Assche
0 siblings, 0 replies; 10+ messages in thread
From: Bart Van Assche @ 2026-11-29 2:39 UTC (permalink / raw)
To: Chris Down, Martin K. Petersen, James E.J. Bottomley
Cc: Christoph Hellwig, linux-scsi, linux-kernel, kernel-team
On 11/28/26 1:26 AM, Chris Down wrote:
> 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;
> + }
That comment isn't correct for removable devices:
set_media_not_present() sets sdkp->capacity = 0 without holding
q->limits_lock and without updating get_capacity(sdkp->disk). It is
called from sd_check_events() and from sd_spinup_disk() (which runs in
sd_revalidate_disk() before queue_limits_start_update()).
So on a removable disk with capacity > 2^32 blocks, sd_can_use_ws10()
can see sdkp->capacity == 0 and allow SD_LBP_WS10 / SD_LBP_ZERO while
get_capacity(sdkp->disk) is still > 2^32.
Thanks,
Bart.
^ permalink raw reply [flat|nested] 10+ 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-29 2:40 ` Bart Van Assche
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; 10+ 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 [flat|nested] 10+ 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-29 2:40 ` Bart Van Assche
0 siblings, 0 replies; 10+ messages in thread
From: Bart Van Assche @ 2026-11-29 2:40 UTC (permalink / raw)
To: Chris Down, Martin K. Petersen, James E.J. Bottomley
Cc: Christoph Hellwig, linux-scsi, linux-kernel, kernel-team
On 11/28/26 1:26 AM, Chris Down wrote:
> @@ -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;
A few issues with this patch:
1. max_write_same_blocks_store() was missed. It still modifies
sdp->no_write_same and sdkp->max_ws_blocks before
queue_limits_start_update(), and calls sd_config_write_same() before
queue_limits_commit_update_frozen() freezes the queue.
2. Both here and in sd_revalidate_disk(), sd_config_discard() and
sd_config_write_same() mutate sdkp->provisioning_mode,
sdkp->zeroing_mode, and sdkp->max_ws_blocks before
queue_limits_commit_update() validates lim. If
queue_limits_commit_update() returns an error, q->limits keeps the
old (potentially larger) limits while sdkp has already switched to
the new mode.
> + /*
> + * 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;
3. As sashiko-bot also pointed out, in the `grown` path:
- `if (err) goto out;` is checked after `set_capacity_and_notify()`,
so a failed limits commit still publishes the larger capacity and
skips sd_zbc_revalidate_zones().
- queue_limits_commit_update() unlocks q->limits_lock, so
`if (grown) set_capacity_and_notify(disk, sectors);` runs unlocked
and can race with a concurrent sd_revalidate_disk() (from
sd_rescan(), sd_open(), or cache_type_store()) that shrinks the
disk back <= 2^32 and re-enables WRITE SAME(10) modes.
4. Updating capacity before freezing on shrink still does not prevent
LBAs >= 2^32 from reaching sd_setup_write_same10_cmnd():
- In submit_bio_noacct(), bio_check_eod() runs before
blk_throtl_bio() and before bio_queue_enter() in
blk_mq_submit_bio(). Freezing the queue only drains bios already
past bio_queue_enter(); a bio that already passed bio_check_eod()
can enter the queue after unfreeze without re-checking EOD.
- Existing partitions are not shrunk by set_capacity_and_notify(),
and bio_check_eod() only checks the partition size, not
disk->part0. Any discard or write zeroes to a partition extending
past 2^32 blocks still wraps its LBA in
sd_setup_write_same10_cmnd().
That's another strong reason to also enforce the LBA/count bounds in
sd_setup_write_same10_cmnd().
Thanks,
Bart.
^ permalink raw reply [flat|nested] 10+ 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
2026-11-29 2:41 ` Bart Van Assche
4 siblings, 1 reply; 10+ 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 [flat|nested] 10+ messages in thread* Re: [PATCH v2 5/5] scsi: sd: Fix unsplit discards when the granularity exceeds the limit
2026-11-28 9:26 ` [PATCH v2 5/5] scsi: sd: Fix unsplit discards when the granularity exceeds the limit Chris Down
@ 2026-11-29 2:41 ` Bart Van Assche
0 siblings, 0 replies; 10+ messages in thread
From: Bart Van Assche @ 2026-11-29 2:41 UTC (permalink / raw)
To: Chris Down, Martin K. Petersen, James E.J. Bottomley
Cc: Christoph Hellwig, linux-scsi, linux-kernel, kernel-team
On 11/28/26 1:26 AM, Chris Down wrote:
> @@ -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;
> + }
> }
My concerns about the above changes are as follows:
1. This only checks lim->max_hw_discard_sectors, whereas
blk_validate_limits() sets:
lim->max_discard_sectors =
min(lim->max_hw_discard_sectors,
lim->max_user_discard_sectors);
and __bio_split_discard() splits against lim->max_discard_sectors.
There are still multiple ways to hit `max_discard_sectors < granularity`
(or 0) with sdkp->provisioning_mode still set to SD_LBP_WS10 /
SD_LBP_ZERO:
- If userspace previously lowered discard_max_bytes and a revalidate
later raises unmap_granularity above max_user_discard_sectors (but
still <= max_hw_discard_sectors), Patch 5 doesn't trigger,
max_discard_sectors remains non-zero, and __bio_split_discard()
returns every discard unsplit.
- SCSI VPD 0xb0 unmap_granularity does not have to be a power of 2,
while queue_max_discard_sectors_store() checks alignment with
`max_discard_bytes & (discard_granularity - 1)`. For example, with
discard_granularity = 0x5000 (40 sectors), writing 0x2000 (16
sectors) passes `0x2000 & 0x4fff == 0`, leaving
0 < max_discard_sectors < granularity and unsplit discards.
- Writing 0 to discard_max_bytes while a large discard is in flight
or being split leaves sdkp->provisioning_mode unchanged while
__bio_split_discard() returns the remaining bio unsplit.
Since this is a block-layer invariant (already worked around in mmc
and drbd as well), shouldn't blk_validate_limits() and/or
__bio_split_discard() handle it centrally?
2. In provisioning_mode_store(), if the user explicitly writes
"writesame_10" or "writesame_zero" and this check overrides
sdkp->provisioning_mode to SD_LBP_DISABLE, the sysfs store still
returns success (`count`) instead of -EINVAL (inconsistent with
patch 3).
3. Pre-existing, but right above this hunk,
`sdkp->unmap_granularity * logical_block_size` is computed in 32-bit
unsigned int from a raw u32 VPD field. A large unmap_granularity
(e.g. >= 0x800000 for 512-byte blocks) overflows and wraps
lim->discard_granularity to a small value, defeating this check.
Thanks,
Bart.
^ permalink raw reply [flat|nested] 10+ messages in thread