Linux SCSI Discards Can Report Success While Erasing Data in the Wrong Region
On November 28, Chris Down posted a second revision of five fixes for the Linux kernel's SCSI disk driver.
Down used scsi_debug, which emulates a SCSI device, to reproduce the failure. The discard request reported success, but the test found that data at the start of the disk had been discarded instead of the requested region.
The driver truncated information while converting a request into WRITE SAME(10). SCSI identifies positions on disk with logical block addresses (LBAs). WRITE SAME(10) writes the same contents to a specified range of blocks and can also request UNMAP, or logical block deallocation, on devices that support it. The sg3_utils manual describes the command.
WRITE SAME(10) uses a 32-bit starting address. When an address exceeds the field's range and the driver discards the high bits, the resulting address can point to a different part of the disk.
The caller sees a successful result. That response gives no indication that anything went wrong, even if the driver discarded data it was supposed to preserve.
Truncating the block count can also defeat zeroing
The command specifies a block count as well as a starting address. In WRITE SAME(10), the block count occupies a 16-bit field.
Down also reported a problem with zeroing. In the test, truncating an oversized block count left the target region unchanged.
Linux SCSI: truncation in WRITE SAME(10)
| Measure | Starting address | Block count |
|---|---|---|
| Operation | Discard | Zeroing |
| Field width | 32 bits | 16 bits |
| Test conditions | Simulated disk Logical block sizeStandard disk configuration | Large requestlimit |
| Request | Discard outside address range | Zeroing beyond block count range |
| After conversion | Wrong starting address | Truncated block count |
| Result | Reported success Data at start of disk discarded | Target region unchanged |
An incorrect starting address can affect the wrong location. An incorrect block count can leave the requested region untouched. Both failures arise when the driver loses information while converting a request into a command.
From checking each request to checking the entire disk
In the first revision, posted November 8, Down proposed that when an address or block count did not fit in WRITE SAME(10), the driver should switch zeroing requests to WRITE SAME(16) and reject discards.
Linux SCSI: WRITE SAME(10) patch proposal v2
| Measure | Proposed changes in v2 | Concerns with v2 |
|---|---|---|
| Affected modes | Discard: writesame_10 and writesame_zero Zeroing: writesame_10_unmap | |
| Mode selection | Only when all blocks are addressable in 32 bits | Race with capacity or limit changes Capacity mismatch with removable media |
| Request limits | Adjust the zeroing limit Reset discard-related limits in non-discard modes | Failed update leaves old limits |
| Mode changes | Freeze the queue first | Zeroing mode setup lacks queue freezing and locking |
| Capacity changes | Growth: change mode first Shrinkage: update capacity first | Previously checked requests and requests to existing partitions may still refer to old ranges |
| Discard | Hardware limit (nonzero) Disable if below discard granularity | Also consider the user limit |
| Further proposal | Check address and block count when constructing the command | |
Following a suggestion from Christoph Hellwig, the second revision picks the mode based on disk capacity. It uses the affected WRITE SAME(10) mode only if the entire disk can be addressed in 32 bits.
The series would avoid selecting writesame_10_unmap, which zeroes blocks through deallocation, for disks too large to address in 32 bits. Plain WRITE SAME without UNMAP can use WRITE SAME(16) where needed. The proposal would disable discard on large disks that support it only through WRITE SAME(10).
The proposed changes go beyond choosing a command. They adjust request size limits for the active mode and reset discard-related limits when switching to a zeroing mode that does not deallocate blocks.
Queued requests may reflect old limits, so the proposal freezes the queue before changing modes. When capacity grows, it switches modes before publishing the new capacity. When capacity shrinks, it updates the capacity first. It also disables discard if the discard granularity exceeds a nonzero hardware request limit, since the block layer can otherwise fail to split requests.
The proposed changes add 66 lines and remove nine in drivers/scsi/sd.c. The series is marked for backporting to stable kernels.
Protecting the target range during capacity changes
On November 29, Bart Van Assche raised several concerns about the second revision.
Van Assche said the path for setting the zeroing mode lacked queue freezing and locking for updates to request limits. If disk capacity or limits change between a check and the mode update, the selected mode may no longer be valid. For removable media, the driver may also check capacity while its internal value differs from the one published to the system.
If updating the limits fails, the driver may already have changed modes, leaving the queue with old limits. Van Assche also noted that the effective discard limit is the lower of the hardware limit and the user-configured limit. Comparing discard granularity with the hardware limit alone is insufficient.
Shrinking capacity can also leave requests that passed an earlier capacity check to arrive after the queue is unfrozen. Updating disk capacity does not resize existing partitions, so requests can continue to refer to the previous range.
Van Assche proposed checking the address and block count bounds in WRITE SAME(10) command construction itself, in sd_setup_write_same10_cmnd().
The proposed patches cover configurations using writesame_10 or writesame_zero for discard, or writesame_10_unmap for zeroing. The largest disk that fits within the 32-bit address range also depends on logical block size.
However carefully the driver handles changes in capacity or settings, the final device command can express only a fixed range. Success must mean the requested region was handled correctly. A final bounds check helps preserve that guarantee.
KioskNews shows a cleaned-up reading view extracted from the publisher’s page — the original always lives on their site, not ours.
