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, sashiko-bot@kernel.org,
Bart Van Assche <bvanassche@acm.org>
Subject: [PATCH v3 2/5] scsi: sd: Fix data corruption in WRITE SAME(10) write zeroes
Date: Sun, 29 Nov 2026 11:25:24 -0800 [thread overview]
Message-ID: <acdcf31694b29dd80fc9f10a2509121ece0b37de.1795967558.git.chris@chrisdown.name> (raw)
In-Reply-To: <cover.1795967558.git.chris@chrisdown.name>
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 change the write zeroes limit, so don't accept
writesame_10_unmap there if max_write_same_blocks is too big for it.
A revalidate changes the capacity and max_write_same_blocks under the
limits lock, so zeroing_mode_store() checks them under it too.
sdkp->capacity is also set to zero as soon as the medium of a removable
disk is found to be gone, while the gendisk keeps the old capacity
until the next revalidate. So, check the gendisk capacity as well.
Fixes: e4b878371b85 ("sd: implement unmapping Write Zeroes")
Cc: stable@vger.kernel.org
Signed-off-by: Chris Down <chris@chrisdown.name>
---
drivers/scsi/sd.c | 29 ++++++++++++++++++++++++++++-
1 file changed, 28 insertions(+), 1 deletion(-)
diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
index 13e399631fa8..26f40db3fd75 100644
--- a/drivers/scsi/sd.c
+++ b/drivers/scsi/sd.c
@@ -146,6 +146,20 @@ 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)
+{
+ sector_t max_blocks = (sector_t)U32_MAX + 1;
+
+ /*
+ * A removable disk without a medium has an sdkp->capacity of zero,
+ * but the gendisk keeps its old capacity until the next revalidate.
+ */
+ return sdkp->capacity <= max_blocks &&
+ get_capacity(sdkp->disk) <=
+ logical_to_sectors(sdkp->device, max_blocks);
+}
+
static void sd_config_wzeroes_unmap(struct scsi_disk *sdkp,
struct queue_limits *lim)
{
@@ -643,6 +657,14 @@ zeroing_mode_store(struct device *dev, struct device_attribute *attr,
return -EINVAL;
lim = queue_limits_start_update(sdkp->disk->queue);
+ /* A revalidate can change these, so check them under the lock. */
+ if (mode == SD_ZERO_WS10_UNMAP &&
+ (!sd_can_use_ws10(sdkp) ||
+ sdkp->max_ws_blocks > SD_MAX_WS10_BLOCKS)) {
+ queue_limits_cancel_update(sdkp->disk->queue);
+ return -EINVAL;
+ }
+
sdkp->zeroing_mode = mode;
sd_config_wzeroes_unmap(sdkp, &lim);
err = queue_limits_commit_update_frozen(sdkp->disk->queue, &lim);
@@ -1230,13 +1252,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
next prev parent reply other threads:[~2026-11-29 19:25 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-29 19:25 [PATCH v3 0/5] scsi: sd: Fix WRITE SAME(10) data corruption Chris Down
2026-11-29 19:25 ` [PATCH v3 1/5] scsi: sd: Fix stale max_hw_wzeroes_unmap_sectors after a mode change Chris Down
2026-11-29 19:25 ` Chris Down [this message]
2026-11-29 19:25 ` [PATCH v3 3/5] scsi: sd: Fix data corruption in WRITE SAME(10) discards Chris Down
2026-11-29 19:25 ` [PATCH v3 4/5] scsi: sd: Fix WRITE SAME(10) count truncation when the mode changes Chris Down
2026-11-29 19:25 ` [PATCH v3 5/5] scsi: sd: Fix truncation of WRITE SAME(10) requests that don't fit 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=acdcf31694b29dd80fc9f10a2509121ece0b37de.1795967558.git.chris@chrisdown.name \
--to=chris@chrisdown.name \
--cc=James.Bottomley@hansenpartnership.com \
--cc=bvanassche@acm.org \
--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 \
--cc=sashiko-bot@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®