nvme: limit Write Zeroes to 65536 logical blocks
Subsystems (2)
PATCH v2 1/1 nvme: limit Write Zeroes to 65536 logical blocks
With nvme-tcp connected to nvmet-tcp at default settings, the target reports MDTS 0 and WZSL 0. A 33 MiB Write Zeroes request on a 512-byte-LBA namespace then completes successfully after zeroing only the first 1 MiB.
The native Write Zeroes command encodes the number of logical blocks in a zero-based 16-bit field. The namespace queue can advertise a larger limit, either through the MDTS fallback or through WZSL. In that case the host truncates the count when building the command and reports success after zeroing only part of the requested range.
Cap the native Write Zeroes limit at the number of logical blocks the command can encode. Convert that limit to sectors for each namespace and retain any smaller controller limit. The DSM deallocation quirk continues to use its own limit.
---
Tested on arm64 at v7.4-rc3 (aebb72744764), over TCP with the stock nvmet target. Full-range read-back passed for 1, 32, 33, 64, 256 and 1024 MiB with 512-byte LBAs, and 256, 257, 512 and 1024 MiB with 4 KiB LBAs. Host and target traces confirmed the expected command splitting. A smaller 1 MiB MDTS-derived limit was preserved, and XFS formatted and mounted successfully.
Claude and Codex assisted with the investigation, patch preparation, and validation. I also reproduced the 33 MiB failure and checked the trace by hand on an unpatched kernel.
I will follow up with a blktests regression test for this, as Christoph suggested.
Changes since v1 (sent to the maintainers off-list):
- Move the calculation into nvme_max_write_zeroes_sectors() and derive the maximum block count from the command's length field.
- Describe the affected configuration and Cc stable.
drivers/nvme/host/core.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-)
drivers/nvme/host/core.c +12 -1
@@ -2093,6 +2093,16 @@ static void nvme_set_ctrl_limits(struct nvme_ctrl *ctrl, lim->dma_alignment =3 ; } +static sector_t nvme_max_write_zeroes_sectors (struct nvme_ns *ns) +{ +const size_t nlb_bytes = + sizeof_field(struct nvme_write_zeroes_cmd, length); +const u64 max_lbas =BIT_ULL (BYTES_TO_BITS (nlb_bytes)); + +return min_t (sector_t, ns->ctrl->max_zeroes_sectors, +nvme_lba_to_sect (ns->head, max_lbas)); +} +static bool nvme_update_disk_info (struct nvme_ns *ns,struct nvme_id_ns *id,struct nvme_id_ns_nvm *nvm,struct queue_limits *lim) {
@@ -2143,7 +2153,8 @@ static bool nvme_update_disk_info(struct nvme_ns *ns, struct nvme_id_ns *id, (ctrl->oncs & NVME_CTRL_ONCS_DSM)) lim->max_write_zeroes_sectors =UINT_MAX ; else - lim->max_write_zeroes_sectors = ctrl->max_zeroes_sectors; + lim->max_write_zeroes_sectors = +nvme_max_write_zeroes_sectors (ns);if (ctrl->dmrsl && ctrl->dmrsl <=nvme_sect_to_lba (ns->head,UINT_MAX )) lim->max_hw_discard_sectors =
Quoted 11 lines · Ankit Gupta
With nvme-tcp connected to nvmet-tcp at default settings, the target reports MDTS 0 and WZSL 0. A 33 MiB Write Zeroes request on a 512-byte-LBA namespace then completes successfully after zeroing only the first 1 MiB.
The native Write Zeroes command encodes the number of logical blocks in a zero-based 16-bit field. The namespace queue can advertise a larger limit, either through the MDTS fallback or through WZSL. In that case the host truncates the count when building the command and reports success after zeroing only part of the requested range.
Quoted 8 lines
Cap the native Write Zeroes limit at the number of logical blocks the command can encode. Convert that limit to sectors for each namespace and retain any smaller controller limit. The DSM deallocation quirk continues to use its own limit.
Fixes b94e8cd2e6a9 ("nvme: fix Write Zeroes limitations")Signed-off-by Ankit Gupta<[email protected]>
Thanks a lot for the fix, looks good to me.
-ck
Quoted 5 lines · Ankit Gupta
With nvme-tcp connected to nvmet-tcp at default settings, the target reports MDTS 0 and WZSL 0. A 33 MiB Write Zeroes request on a 512-byte-LBA namespace then completes successfully after zeroing only the first 1 MiB.
Quoted 7 lines
The native Write Zeroes command encodes the number of logical blocks in a zero-based 16-bit field. The namespace queue can advertise a larger limit, either through the MDTS fallback or through WZSL. In that case the host truncates the count when building the command and reports success after zeroing only part of the requested range.
[...]
Thanks, applied to nvme-7.4.
[1/1] nvme: limit Write Zeroes to 65536 logical blocks
commit: 49a768272ae1445714efc6ab2b23f9de1398d61e
--
Keith Busch <[email protected]>