mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
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 3/5] scsi: sd: Fix data corruption in WRITE SAME(10) discards
Date: Sun, 29 Nov 2026 11:25:26 -0800	[thread overview]
Message-ID: <c4b1dd375268c744b4d72ca40c1150b145669c01.1795967558.git.chris@chrisdown.name> (raw)
In-Reply-To: <cover.1795967558.git.chris@chrisdown.name>

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 26f40db3fd75..09cb64ca6db4 100644
--- a/drivers/scsi/sd.c
+++ b/drivers/scsi/sd.c
@@ -616,6 +616,12 @@ provisioning_mode_store(struct device *dev, struct device_attribute *attr,
 		return -EINVAL;
 
 	lim = queue_limits_start_update(sdkp->disk->queue);
+	/* A revalidate can change the capacity, so check it under the 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)
@@ -3453,7 +3459,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


  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 ` [PATCH v3 2/5] scsi: sd: Fix data corruption in WRITE SAME(10) write zeroes Chris Down
2026-11-29 19:25 ` Chris Down [this message]
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=c4b1dd375268c744b4d72ca40c1150b145669c01.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®