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 5/5] scsi: sd: Fix truncation of WRITE SAME(10) requests that don't fit
Date: Sun, 29 Nov 2026 11:25:30 -0800	[thread overview]
Message-ID: <5c2386eb39d873c2a5e875d7221f7f32b0809300.1795967558.git.chris@chrisdown.name> (raw)
In-Reply-To: <cover.1795967558.git.chris@chrisdown.name>

The modes and the limits keep most requests that don't fit in WRITE
SAME(10) away from sd_setup_write_same10_cmnd(), but they can't stop
all of them. One that gets there is sent with its LBA or count
truncated.

For example, bio_check_eod() only checks a bio against its partition,
and it does that before the bio enters the queue. If a disk shrinks to
2^32 blocks or fewer while a partition on it still ends above that, a
discard to the end of the partition is sent as WRITE SAME(10) with its
LBA truncated. So is one that was checked before the shrink and entered
the queue after it.

The block layer also doesn't split a discard when max_discard_sectors
is zero or below the discard granularity. With scsi_debug, 512-byte
blocks and an unmap granularity of 0x10000 blocks, a 64 MiB discard in
writesame_10 mode goes out as a single WRITE SAME(10) with a count of
zero. scsi_debug doesn't unmap anything in that case, but in SBC a
count of zero is intended to imply that every block from the LBA to the
end of the disk should be unmapped unless the device sets WSNZ. Writing
0 to discard_max_bytes while a large discard is running sends the rest
of the bio unsplit as well.

sd_setup_read_write_cmnd() already fails reads and writes beyond the
end of the disk. Let's fail a request that doesn't fit in WRITE
SAME(10) too. The block layer still has to learn to split or refuse the
discards above, but that is a separate fix.

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 | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
index f4db971d7abe..ab264a829930 100644
--- a/drivers/scsi/sd.c
+++ b/drivers/scsi/sd.c
@@ -1188,6 +1188,9 @@ static blk_status_t sd_setup_write_same10_cmnd(struct scsi_cmnd *cmd,
 	u32 data_len = sdp->sector_size;
 	blk_status_t ret;
 
+	if (lba > U32_MAX || nr_blocks > U16_MAX)
+		return BLK_STS_TARGET;
+
 	if (!sd_set_special_bvec(cmd, data_len))
 		return BLK_STS_RESOURCE;
 
-- 
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 ` [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 ` Chris Down [this message]

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=5c2386eb39d873c2a5e875d7221f7f32b0809300.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®