mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Bart Van Assche <bvanassche@acm.org>
To: Chris Down <chris@chrisdown.name>,
	"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: Re: [PATCH v2 2/5] scsi: sd: Fix data corruption in WRITE SAME(10) write zeroes
Date: Sat, 28 Nov 2026 18:38:47 -0800	[thread overview]
Message-ID: <79cccf57-7539-41c5-a4d0-dd71336f1111@acm.org> (raw)
In-Reply-To: <d7cba111662e11f810404d5f39cc72b4a357f442.1795842371.git.chris@chrisdown.name>

On 11/28/26 1:26 AM, Chris Down wrote:
 > +/* WRITE SAME(10) only has a 32-bit LBA field. */
 > +static bool sd_can_use_ws10(struct scsi_disk *sdkp)
 > +{
 > +	return sdkp->capacity <= 1 + (sector_t)0xffffffff;
 > +}

(sector_t)U32_MAX + 1 is easier to read than `1 + (sector_t)0xffffffff`.

 > @@ -630,6 +636,11 @@ zeroing_mode_store(struct device *dev, struct 
device_attribute *attr,
 >  	if (mode < 0)
 >  		return -EINVAL;
 >
 > +	if (mode == SD_ZERO_WS10_UNMAP &&
 > +	    (!sd_can_use_ws10(sdkp) ||
 > +	     sdkp->max_ws_blocks > SD_MAX_WS10_BLOCKS))
 > +		return -EINVAL;
 > +
 >  	sdkp->zeroing_mode = mode;

Unlike provisioning_mode_store() in patches 3 and 4, zeroing_mode_store()
neither acquires q->limits_lock nor freezes the queue (nor updates
max_hw_wzeroes_unmap_sectors). That leaves several races:

1. Race with sd_revalidate_disk(): zeroing_mode_store("writesame_10_unmap")
    can pass the check while capacity <= 2^32 and max_ws_blocks <= 0xffff,
    get preempted, and write sdkp->zeroing_mode = SD_ZERO_WS10_UNMAP after
    sd_revalidate_disk() grows the disk past 2^32 blocks (or raises
    max_ws_blocks > 0xffff) and switches away from SD_ZERO_WS10_UNMAP.

2. Race with max_write_same_blocks_store():
    - Growing max_ws_blocks past 0xffff has the same TOCTOU, leaving
      SD_ZERO_WS10_UNMAP active with max_write_zeroes_sectors > 0xffff
      blocks.
    - Shrinking max_ws_blocks while large write zeroes are queued:
      max_write_same_blocks_store() writes sdkp->max_ws_blocks before
      taking q->limits_lock or freezing the queue, then waits in
      queue_limits_commit_update_frozen() for queued requests to drain.
      A concurrent zeroing_mode_store("writesame_10_unmap") sees the new
      smaller sdkp->max_ws_blocks and switches sdkp->zeroing_mode while
      larger requests are still draining, causing count truncation.

If zeroing_mode_store() uses queue_limits_start_update() +
blk_mq_freeze_queue() + queue_limits_commit_update() + unfreeze (like
provisioning_mode_store()) and updates lim.max_hw_wzeroes_unmap_sectors,
it closes these races and fixes the stale unmap limit after sysfs
writes.

Thanks,

Bart.

  reply	other threads:[~2026-11-29  2:38 UTC|newest]

Thread overview: 10+ 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-29  2:38   ` Bart Van Assche [this message]
2026-11-28  9:26 ` [PATCH v2 3/5] scsi: sd: Fix data corruption in WRITE SAME(10) discards Chris Down
2026-11-29  2:39   ` Bart Van Assche
2026-11-28  9:26 ` [PATCH v2 4/5] scsi: sd: Fix WRITE SAME(10) count truncation when the mode changes Chris Down
2026-11-29  2:40   ` Bart Van Assche
2026-11-28  9:26 ` [PATCH v2 5/5] scsi: sd: Fix unsplit discards when the granularity exceeds the limit Chris Down
2026-11-29  2:41   ` Bart Van Assche

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=79cccf57-7539-41c5-a4d0-dd71336f1111@acm.org \
    --to=bvanassche@acm.org \
    --cc=James.Bottomley@hansenpartnership.com \
    --cc=chris@chrisdown.name \
    --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®