From: Chris Down <chris@chrisdown.name>
To: "Martin K. Petersen" <mkp@kernel.org>,
"James E.J. Bottomley" <James.Bottomley@hansenpartnership.com>
Cc: Christoph Hellwig <hch@lst.de>,
linux-scsi@vger.kernel.org, linux-kernel@vger.kernel.org,
kernel-team@meta.com
Subject: [PATCH v2 4/5] scsi: sd: Fix WRITE SAME(10) count truncation when the mode changes
Date: Sat, 28 Nov 2026 01:26:37 -0800 [thread overview]
Message-ID: <a1acc1de43d8ee89d64e345da817f7ac3585e86d.1795842371.git.chris@chrisdown.name> (raw)
In-Reply-To: <cover.1795842371.git.chris@chrisdown.name>
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
next prev parent reply other threads:[~2026-11-28 9:26 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
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 ` [PATCH v2 3/5] scsi: sd: Fix data corruption in WRITE SAME(10) discards Chris Down
2026-11-28 9:26 ` Chris Down [this message]
2026-11-28 9:26 ` [PATCH v2 5/5] scsi: sd: Fix unsplit discards when the granularity exceeds the limit Chris Down
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=a1acc1de43d8ee89d64e345da817f7ac3585e86d.1795842371.git.chris@chrisdown.name \
--to=chris@chrisdown.name \
--cc=James.Bottomley@hansenpartnership.com \
--cc=hch@lst.de \
--cc=kernel-team@meta.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-scsi@vger.kernel.org \
--cc=mkp@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®