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 4/5] scsi: sd: Fix WRITE SAME(10) count truncation when the mode changes
Date: Sat, 28 Nov 2026 18:40:50 -0800	[thread overview]
Message-ID: <faa1aec5-8630-4c0a-b5b8-480447ad1fa2@acm.org> (raw)
In-Reply-To: <a1acc1de43d8ee89d64e345da817f7ac3585e86d.1795842371.git.chris@chrisdown.name>

On 11/28/26 1:26 AM, Chris Down wrote:
 > @@ -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;

A few issues with this patch:

1. max_write_same_blocks_store() was missed. It still modifies
    sdp->no_write_same and sdkp->max_ws_blocks before
    queue_limits_start_update(), and calls sd_config_write_same() before
    queue_limits_commit_update_frozen() freezes the queue.

2. Both here and in sd_revalidate_disk(), sd_config_discard() and
    sd_config_write_same() mutate sdkp->provisioning_mode,
    sdkp->zeroing_mode, and sdkp->max_ws_blocks before
    queue_limits_commit_update() validates lim. If
    queue_limits_commit_update() returns an error, q->limits keeps the
    old (potentially larger) limits while sdkp has already switched to
    the new mode.

 > +	/*
 > +	 * 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;

3. As sashiko-bot also pointed out, in the `grown` path:
    - `if (err) goto out;` is checked after `set_capacity_and_notify()`,
      so a failed limits commit still publishes the larger capacity and
      skips sd_zbc_revalidate_zones().
    - queue_limits_commit_update() unlocks q->limits_lock, so
      `if (grown) set_capacity_and_notify(disk, sectors);` runs unlocked
      and can race with a concurrent sd_revalidate_disk() (from
      sd_rescan(), sd_open(), or cache_type_store()) that shrinks the
      disk back <= 2^32 and re-enables WRITE SAME(10) modes.

4. Updating capacity before freezing on shrink still does not prevent
    LBAs >= 2^32 from reaching sd_setup_write_same10_cmnd():
    - In submit_bio_noacct(), bio_check_eod() runs before
      blk_throtl_bio() and before bio_queue_enter() in
      blk_mq_submit_bio(). Freezing the queue only drains bios already
      past bio_queue_enter(); a bio that already passed bio_check_eod()
      can enter the queue after unfreeze without re-checking EOD.
    - Existing partitions are not shrunk by set_capacity_and_notify(),
      and bio_check_eod() only checks the partition size, not
      disk->part0. Any discard or write zeroes to a partition extending
      past 2^32 blocks still wraps its LBA in
      sd_setup_write_same10_cmnd().

    That's another strong reason to also enforce the LBA/count bounds in
    sd_setup_write_same10_cmnd().

Thanks,

Bart.

  reply	other threads:[~2026-11-29  2:40 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
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 [this message]
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=faa1aec5-8630-4c0a-b5b8-480447ad1fa2@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®