Skip to contentSkip to conversation

nvme/080: test write zeroes larger than 65536 logical blocks

PATCH In review Ankit Gupta 3 participants 4 messages Nov 17 – Nov 21

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nvme/080: test write zeroes larger than 65536 logical blocks

The NVMe Write Zeroes command encodes the number of logical blocks in a zero-based 16-bit field. When the controller reports neither MDTS nor WZSL, as nvmet-tcp does by default, the host advertised an unbounded write zeroes limit, truncated the block count when building the command and completed the request successfully after zeroing only a prefix.

Add a regression test for the kernel patch "nvme: limit Write Zeroes to 65536 logical blocks" [1]. Fill 65536 logical blocks plus 1 MiB with a non-zero pattern, zero that range with blkdiscard -z and check that the whole range reads back as zeroes. The length scales with the logical block size, so the test covers both 512-byte and 4096-byte namespaces.

[1] https://lore.kernel.org/linux-nvme/[email protected]/

---
Tested on arm64 with v7.4-rc3 (aebb72744764), NVMET_TRTYPES="loop tcp" and NVMET_BLKDEV_TYPES="device file". Without the kernel patch the test fails for tcp with both block device types ("zeroed range still contains non-zero data"). With the patch it passes for tcp and loop with both types. The device type gives 512-byte logical blocks (33 MiB zeroed) and the file type 4096-byte logical blocks (257 MiB zeroed). With NVME_IMG_SIZE=64m the device type still runs and the file type is skipped.

Claude and Codex assisted with writing and validating this test.

 tests/nvme/080     | 90 ++++++++++++++++++++++++++++++++++++++++++++++
 tests/nvme/080.out |  2 ++
 2 files changed, 92 insertions(+)
 create mode 100755 tests/nvme/080
 create mode 100644 tests/nvme/080.out
tests/nvme/080 new file +90 -0
new file mode 100755
index 0000000..bbd3031
@@ -0,0 +1,90 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-3.0+
+# Copyright (C) 2026 Ankit Gupta
+#
+# Test that a Write Zeroes request larger than a single NVMe Write Zeroes
+# command can describe zeroes the whole requested range. The command encodes
+# the number of logical blocks in a zero-based 16-bit field, so one command
+# covers at most 65536 logical blocks. When the controller reports neither
+# MDTS nor WZSL, as nvmet-tcp does by default, the host used to advertise an
+# unbounded write zeroes limit, truncate the block count when building the
+# command and complete the request successfully after zeroing only a prefix.
+#
+# Regression test for the patch "nvme: limit Write Zeroes to 65536 logical
+# blocks".
+
+. tests/nvme/rc
+
+DESCRIPTION="test write zeroes larger than 65536 logical blocks"
+QUICK=1
+
+requires() {
+	_nvme_requires
+	_have_loop
+	_have_program blkdiscard
+	_require_nvme_trtype_is_fabrics
+}
+
+set_conditions() {
+	_set_combined_conditions _set_nvme_trtype _set_nvmet_blkdev_type "$@"
+}
+
+test_write_zeroes() {
+	local ns dev bs size len_mb
+
+	if ! ns=$(_find_nvme_ns "${def_subsys_uuid}"); then
+		echo "FAIL: failed to find the namespace"
+		return 1
+	fi
+	dev="/dev/${ns}"
+
+	if ! bs=$(blockdev --getss "${dev}") ||
+			! size=$(blockdev --getsize64 "${dev}"); then
+		echo "FAIL: failed to get the namespace block size and capacity"
+		return 1
+	fi
+
+	# 65536 logical blocks plus 1 MiB: 33 MiB with 512-byte logical blocks
+	# and 257 MiB with 4096-byte logical blocks.
+	len_mb=$((bs * 65536 / 1024 / 1024 + 1))
+	if ((len_mb * 1024 * 1024 > size)); then
+		SKIP_REASONS+=("namespace is smaller than ${len_mb} MiB")
+		return 1
+	fi
+
+	# Fill the range with non-zero data so that stale data is detectable.
+	if ! head -c "${len_mb}M" /dev/zero | tr '\0' '\377' |
+			dd of="${dev}" bs=1M iflag=fullblock oflag=direct \
+			status=none; then
+		echo "FAIL: failed to fill ${len_mb} MiB with a pattern"
+	fi
+
+	if ! blkdiscard -z -o 0 -l "${len_mb}M" "${dev}"; then
+		echo "FAIL: blkdiscard -z failed"
+	fi
+
+	if ! dd if="${dev}" bs=1M count="${len_mb}" iflag=direct status=none |
+			cmp -s -n "$((len_mb * 1024 * 1024))" - /dev/zero; then
+		echo "FAIL: zeroed range still contains non-zero data"
+	fi
+}
+
+test() {
+	echo "Running ${TEST_NAME}"
+
+	_setup_nvmet
+
+	_nvmet_target_setup
+
+	if _nvme_connect_subsys; then
+		test_write_zeroes
+	else
+		echo "FAIL: failed to connect to the subsystem"
+	fi
+
+	_nvme_disconnect_subsys
+
+	_nvmet_target_cleanup
+
+	echo "Test complete"
+}
tests/nvme/080.out new file +2 -0
new file mode 100644
index 0000000..15119b8
@@ -0,0 +1,2 @@
+Running nvme/080
+Test complete
Signature · 1 line
2.50.1 (Apple Git-155)
Reviewed-by on v1 1/1
#1 lore raw
Quoted 25 lines · Ankit Gupta

The NVMe Write Zeroes command encodes the number of logical blocks in a zero-based 16-bit field. When the controller reports neither MDTS nor WZSL, as nvmet-tcp does by default, the host advertised an unbounded write zeroes limit, truncated the block count when building the command and completed the request successfully after zeroing only a prefix.

Add a regression test for the kernel patch "nvme: limit Write Zeroes to 65536 logical blocks" [1]. Fill 65536 logical blocks plus 1 MiB with a non-zero pattern, zero that range with blkdiscard -z and check that the whole range reads back as zeroes. The length scales with the logical block size, so the test covers both 512-byte and 4096-byte namespaces.

[1]https://lore.kernel.org/linux-nvme/[email protected]/

---
Tested on arm64 with v7.4-rc3 (aebb72744764), NVMET_TRTYPES="loop tcp" and NVMET_BLKDEV_TYPES="device file". Without the kernel patch the test fails for tcp with both block device types ("zeroed range still contains non-zero data"). With the patch it passes for tcp and loop with both types. The device type gives 512-byte logical blocks (33 MiB zeroed) and the file type 4096-byte logical blocks (257 MiB zeroed). With NVME_IMG_SIZE=64m the device type still runs and the file type is skipped.

Quoted 1 line

Claude and Codex assisted with writing and validating this test.

Thanks a lot for the test, overall looks good to me.

-ck

#2 lore raw
In reply to Ankit Gupta
Quoted 12 lines · Ankit Gupta

The NVMe Write Zeroes command encodes the number of logical blocks in a zero-based 16-bit field. When the controller reports neither MDTS nor WZSL, as nvmet-tcp does by default, the host advertised an unbounded write zeroes limit, truncated the block count when building the command and completed the request successfully after zeroing only a prefix.

Add a regression test for the kernel patch "nvme: limit Write Zeroes to 65536 logical blocks" [1]. Fill 65536 logical blocks plus 1 MiB with a non-zero pattern, zero that range with blkdiscard -z and check that the whole range reads back as zeroes. The length scales with the logical block size, so the test covers both 512-byte and 4096-byte namespaces.

Quoted 3 lines

[1] https://lore.kernel.org/linux-nvme/[email protected]/

Ankit, thank you for the patch. Overall, it looks good to me. I also did a trial run, and the new test case fails with the kernel v7.4-rc3, and the test case passes with the kernel fix patch. Good.

I made minor nit comments in line below. They are not critical. If you are okay with it, I can make the changes when I apply this patch. Or feel free to respin the patch. Please let me know your preference.

...

Quoted 20 lines
tests/nvme/080 new file +14 -0
new file mode 100755
index 0000000..bbd3031
@@ -0,0 +1,90 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-3.0+
+# Copyright (C) 2026 Ankit Gupta
+#
+# Test that a Write Zeroes request larger than a single NVMe Write Zeroes
+# command can describe zeroes the whole requested range. The command encodes
+# the number of logical blocks in a zero-based 16-bit field, so one command
+# covers at most 65536 logical blocks. When the controller reports neither
+# MDTS nor WZSL, as nvmet-tcp does by default, the host used to advertise an
+# unbounded write zeroes limit, truncate the block count when building the
+# command and complete the request successfully after zeroing only a prefix.
+#
+# Regression test for the patch "nvme: limit Write Zeroes to 65536 logical
+# blocks".

tests/nvme/080 line 24

Quoted 10 lines
tests/nvme/080 new file +10 -0
+
+. tests/nvme/rc
+
+DESCRIPTION="test write zeroes larger than 65536 logical blocks"
+QUICK=1
+
+requires() {
+	_nvme_requires
+	_have_loop
+	_have_program blkdiscard

Nit: blkdiscard is a part of util-linux, util-linux is the fundamental
requirement to run blktests as noted in README.md. Then I think it is fine to drop the line above.

Quoted 22 lines
tests/nvme/080 new file +22 -0
+	_require_nvme_trtype_is_fabrics
+}
+
+set_conditions() {
+	_set_combined_conditions _set_nvme_trtype _set_nvmet_blkdev_type "$@"
+}
+
+test_write_zeroes() {
+	local ns dev bs size len_mb
+
+	if ! ns=$(_find_nvme_ns "${def_subsys_uuid}"); then
+		echo "FAIL: failed to find the namespace"
+		return 1
+	fi
+	dev="/dev/${ns}"
+
+	if ! bs=$(blockdev --getss "${dev}") ||
+			! size=$(blockdev --getsize64 "${dev}"); then
+		echo "FAIL: failed to get the namespace block size and capacity"
+		return 1
+	fi
+

tests/nvme/080 line 56

Quoted 10 lines
tests/nvme/080 new file +10 -0
+	# 65536 logical blocks plus 1 MiB: 33 MiB with 512-byte logical blocks
+	# and 257 MiB with 4096-byte logical blocks.
+	len_mb=$((bs * 65536 / 1024 / 1024 + 1))
+	if ((len_mb * 1024 * 1024 > size)); then
+		SKIP_REASONS+=("namespace is smaller than ${len_mb} MiB")
+		return 1
+	fi
+
+	# Fill the range with non-zero data so that stale data is detectable.
+	if ! head -c "${len_mb}M" /dev/zero | tr '\0' '\377' |

Nit: it is recommended to use the long option --bytes= instead of the short
option -c here.

tests/nvme/080 line 62

Quoted 6 lines
tests/nvme/080 new file +6 -0
+			dd of="${dev}" bs=1M iflag=fullblock oflag=direct \
+			status=none; then
+		echo "FAIL: failed to fill ${len_mb} MiB with a pattern"
+	fi
+
+	if ! blkdiscard -z -o 0 -l "${len_mb}M" "${dev}"; then

Nit: same here, with long options, the line above can be,

        if ! blkdiscard --zeroout --offset 0 --length "${len_mb}M" "$dev"; then

tests/nvme/080 line 67

Quoted 5 lines
tests/nvme/080 new file +5 -0
+		echo "FAIL: blkdiscard -z failed"
+	fi
+
+	if ! dd if="${dev}" bs=1M count="${len_mb}" iflag=direct status=none |
+			cmp -s -n "$((len_mb * 1024 * 1024))" - /dev/zero; then

Nit: same here:

cmp --silent --bytes "$((len_mb * 1024 * 1024))" \
- /dev/zero; then

#3 lore raw
Quoted 3 lines · Shin'ichiro Kawasaki

I made minor nit comments in line below. They are not critical. If you are okay with it, I can make the changes when I apply this patch. Or feel free to respin the patch. Please let me know your preference.

Hi Shin'ichiro,

Thank you for the review and for the trial run. I agree with all four comments. Please go ahead and make the changes when you apply the patch.

Thanks,
Ankit