Re: [PATCH v2 2/5] scsi: sd: Fix data corruption in WRITE SAME(10) write zeroes

From: Bart Van Assche

Date: Sat Nov 28 2026 - 21:39:04 EST


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.