[PATCH v2 4/5] scsi: sd: Fix WRITE SAME(10) count truncation when the mode changes

From: Chris Down

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


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@xxxxxxxxxxxxxxx>
Signed-off-by: Chris Down <chris@xxxxxxxxxxxxxx>
---
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