Re: [PATCH v2 4/5] scsi: sd: Fix WRITE SAME(10) count truncation when the mode changes
From: Bart Van Assche
Date: Sat Nov 28 2026 - 21:41:02 EST
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.