blktests: add tests with offsets
v2 cover blktests: add tests with offsets
From: Keith Busch <[email protected]>
Here are the tests I've been using to check the kernel's handling for IO using non-block aligned memory offsets for data and metadata.
Changes for the previous version:
- Combining the io_uring metadata test in one series
- Removed all the extra code that tries to discover the queue limits from the file path. The test case will simply pass these through from sysfs attributes that it knows about.
- Removed the linux kernel version specific checks to decide if a test should pass or fail. We just assume a failure means the kernel doesn't support it and carry on.
- Fixed up the test dependencies for the metadata test, and wording
- Fixed up requests from previous review comments.
Keith Busch (2):
blktests: test direct io offsets
blktests: test io_uring user metadata offsets
src/.gitignore | 2 + src/Makefile | 17 +- src/dio-offsets.c | 708 ++++++++++++++++++++++++++++++++++++++++++++ src/metadata.c | 488 ++++++++++++++++++++++++++++++ tests/block/042 | 26 ++ tests/block/042.out | 2 + tests/block/043 | 33 +++ tests/block/043.out | 2 + 8 files changed, 1270 insertions(+), 8 deletions(-) create mode 100644 src/dio-offsets.c create mode 100644 src/metadata.c create mode 100644 tests/block/042 create mode 100644 tests/block/042.out create mode 100755 tests/block/043 create mode 100644 tests/block/043.out
Signature · 1 line
2.47.3
PATCH v2 1/2 blktests: test direct io offsets
From: Keith Busch <[email protected]>
Tests various direct IO memory and length alignments against the device's queue limits reported from sysfs.
--- src/.gitignore | 1 + src/Makefile | 1 + src/dio-offsets.c | 708 ++++++++++++++++++++++++++++++++++++++++++++ tests/block/042 | 26 ++ tests/block/042.out | 2 + 5 files changed, 738 insertions(+) create mode 100644 src/dio-offsets.c create mode 100644 tests/block/042 create mode 100644 tests/block/042.out
src/.gitignore +1 -0
@@ -1,3 +1,4 @@ +/dio-offsets /discontiguous-io /loblksize /loop_change_fd
src/Makefile +1 -0
@@ -9,6 +9,7 @@ HAVE_C_MACRO = $(shell if echo "$(H)include <$(1)>" | \ then echo 1;else echo 0; fi) C_TARGETS := \ + dio-offsets \ loblksize \ loop_change_fd \ loop_get_status_null \
src/dio-offsets.c new file +708 -0
708 lines not on this page · raw message
tests/block/042 new file +26 -0
@@ -0,0 +1,26 @@ +#!/bin/bash + +. tests/block/rc + +DESCRIPTION="Test unusual direct-io offsets" +QUICK=1 + +device_requires() { + _require_test_dev_sysfs +} + +test_device() { + echo "Running ${TEST_NAME}" + + sys_max_segments=$(cat "${TEST_DEV_SYSFS}/queue/max_segments") + sys_dma_alignment=$(cat "${TEST_DEV_SYSFS}/queue/dma_alignment") + sys_virt_boundary_mask=$(cat "${TEST_DEV_SYSFS}/queue/virt_boundary_mask") + sys_logical_block_size=$(cat "${TEST_DEV_SYSFS}/queue/logical_block_size") + sys_max_sectors_kb=$(cat "${TEST_DEV_SYSFS}/queue/max_sectors_kb") + + if ! src/dio-offsets ${TEST_DEV} $sys_max_segments $sys_max_sectors_kb $sys_dma_alignment $sys_virt_boundary_mask $sys_logical_block_size; then + echo "src/dio-offsets failed" + fi + + echo "Test complete" +}
tests/block/042.out new file +2 -0
@@ -0,0 +1,2 @@ +Running block/042 +Test complete
Signature · 1 line
2.47.3
Re: [PATCHv2 1/2] blktests: test direct io offsets
Quoted 7 lines · Keith Busch
From: Keith Busch <[email protected]>
Tests various direct IO memory and length alignments against the device's queue limits reported from sysfs.
Signed-off-by Keith Busch <[email protected]>---
[ ... ]
src/dio-offsets.c line 177
Quoted 7 lines
src/dio-offsets.c new file +7
-0+static void test_page_aligned_vectors () +{ +const int vecs =4 ; + +int i, ret, offset; +struct iovec iov[vecs]; +
every const int vecs has unnecessary new line like above all over this code.
otherwise, Looks good.
-ck
Re: [PATCHv2 1/2] blktests: test direct io offsets
In reply to Keith BuschQuoted 37 lines · Keith Busch
src/dio-offsets.c new file +37
-0+static void test_invalid_dma_vector_length () +{ +const int vecs =4 ; + +bool should_fail = dma_alignment >1 ; +struct iovec iov[vecs]; +int ret; + + iov[0 ].iov_base = out_buf; + iov[0 ].iov_len = max_bytes *2 - max_bytes /2 ; + + iov[1 ].iov_base = out_buf + max_bytes *4 ; + iov[1 ].iov_len = logical_block_size *2 - (dma_alignment +1 ); + + iov[2 ].iov_base = out_buf + max_bytes *8 ; + iov[2 ].iov_len = logical_block_size *2 + (dma_alignment +1 ); + + iov[3 ].iov_base = out_buf + max_bytes *12 ; + iov[3 ].iov_len = max_bytes - max_bytes /2 ; + + ret =pwritev (test_fd, iov, vecs,0 ); +if (ret <0 ) { +if (should_fail) +return ; +err (errno,"%s: failed to write buf" , __func__); + } + +if (should_fail) +err (ENOTSUP ,"%s: write buf unexpectedly succeeded with invalid DMA offset addressret:%d" , + __func__, ret); + + iov[0 ].iov_base = in_buf; + iov[0 ].iov_len = max_bytes *2 - max_bytes /2 ; + + iov[1 ].iov_base = in_buf + max_bytes *4 ; + iov[1 ].iov_len = logical_block_size *2 - (dma_alignment +1 ); +
src/dio-offsets.c line 671
Quoted 10 lines
src/dio-offsets.c new file +10
-0+ iov[2 ].iov_base = in_buf + max_bytes *8 ; + iov[2 ].iov_len = logical_block_size *2 + (dma_alignment +1 ); + + iov[3 ].iov_base = in_buf + max_bytes *12 ; + iov[3 ].iov_len = max_bytes - max_bytes /2 ; + + ret =pwritev (test_fd, iov, vecs,0 ); +if (ret <0 ) +err (errno,"%s: failed to read buf" , __func__); +
is pwritev correct above or it should be preadv () ?
src/dio-offsets.c line 677
Quoted 6 lines
src/dio-offsets.c new file +6
-0+compare (out_buf, in_buf, iov[0 ].iov_len); +compare (out_buf + max_bytes *4 , in_buf + max_bytes *4 , iov[1 ].iov_len); +compare (out_buf + max_bytes *8 , in_buf + max_bytes *8 , iov[2 ].iov_len); +compare (out_buf + max_bytes *12 , in_buf + max_bytes *12 , iov[3 ].iov_len); +} +
-ck
Re: [PATCHv2 1/2] blktests: test direct io offsets
Quoted 8 lines · Chaitanya Kulkarni
src/dio-offsets.c line 198
Quoted 4 lines · Keith Busch
src/dio-offsets.c new file +4
-0+ ret =pwritev (test_fd, iov, vecs,0 ); +if (ret <0 ) +err (errno,"%s: failed to read buf" , __func__); +is pwritev correct above or it should be preadv () ?
Good eye, it should have been preadv. This part is currently unreachable though, as it requires byte-aligned dma limits and the kernel doesn't report such a value. But if this were to run, the test would have falsely declared data corruption.
Re: [PATCHv2 1/2] blktests: test direct io offsets
Quoted 8 lines · Keith Busch
Quoted 7 lines · Chaitanya Kulkarni
src/dio-offsets.c line 198
Quoted 4 lines · Keith Busch
src/dio-offsets.c new file +4
-0+ ret =pwritev (test_fd, iov, vecs,0 ); +if (ret <0 ) +err (errno,"%s: failed to read buf" , __func__); +is pwritev correct above or it should be preadv () ?
Quoted 4 lines
Good eye, it should have been preadv. This part is currently unreachable though, as it requires byte-aligned dma limits and the kernel doesn't report such a value. But if this were to run, the test would have falsely declared data corruption.
how about ?
1. Keep the code but disable it until kernel gets that support ? OR
2. add an ability to autodetect if kernel has a support before running the test else skip test_invalid_dma_vector_length() ?
-ck
Re: [PATCHv2 1/2] blktests: test direct io offsets
Quoted 14 lines · Chaitanya Kulkarni
Quoted 8 lines · Keith Busch
Quoted 7 lines · Chaitanya Kulkarni
src/dio-offsets.c line 198
Quoted 4 lines · Keith Busch
src/dio-offsets.c new file +4
-0+ ret =pwritev (test_fd, iov, vecs,0 ); +if (ret <0 ) +err (errno,"%s: failed to read buf" , __func__); +is pwritev correct above or it should be preadv () ?
Quoted 4 lines
Good eye, it should have been preadv. This part is currently unreachable though, as it requires byte-aligned dma limits and the kernel doesn't report such a value. But if this were to run, the test would have falsely declared data corruption.
Quoted 5 lines
how about ?
1. Keep the code but disable it until kernel gets that support ? OR
2. add an ability to autodetect if kernel has a support before running the test else skiptest_invalid_dma_vector_length()?
It already does the second one. This test function checks the device's dma alignment requirements and purposefully dispatches a command that is expected to fail before it reaches this part.
Re: [PATCHv2 1/2] blktests: test direct io offsets
In reply to Keith BuschQuoted 7 lines · Keith Busch
From: Keith Busch <[email protected]>
Tests various direct IO memory and length alignments against the device's queue limits reported from sysfs.
Signed-off-by Keith Busch <[email protected]>---
[...]
Quoted 5 lines
src/dio-offsets.c new file +0
-0
[...]
Quoted 4 lines
src/dio-offsets.c new file +4
-0+static void init_buffers () +{ +unsigned long lb_mask = logical_block_size -1 ; +int fd, ret;
src/dio-offsets.c
Quoted 10 lines
src/dio-offsets.c new file +10
-0+ + buf_size = max_bytes * max_segments /2 ; +if (buf_size < logical_block_size * max_segments) +err (EINVAL ,"%s: logical block size is too big" , __func__); + +if (buf_size < logical_block_size *1024 *4 ) + buf_size = logical_block_size *1024 *4 ; + +if (buf_size & lb_mask) + buf_size = (buf_size + lb_mask) & ~(lb_mask);
I investigated why this new test case fails with my QEMU SATA device and noticed that the device has 128MiB size. The calculation above sets buf_size = 192MiB. Then pwrite() in test_full_size_aligned() is called with 192MiB size and it was truncated to 128MiB. Then the following compare() check failed.
Is it okay to cap the buf_size with the device size? If so, I suggest the change below. With this change, the test case passes with the device. If you are okay with the change, I can fold it in when I apply the patch.
-------------------- diff start -------------------
src/dio-offsets.c +10 -0
@@ -22,6 +22,8 @@#include <sys/stat.h> #include <sys/statfs.h> #include <sys/uio.h> +#include <sys/ioctl.h> +#include <sys/mount.h> #define power_of_2 (x) ((x) && !((x) & ((x) - 1)))
@@ -81,6 +83,7 @@ static void init_buffers() {unsigned long lb_mask = logical_block_size -1 ;int fd, ret; +unsigned long long dev_bytes; buf_size = max_bytes * max_segments /2 ;if (buf_size < logical_block_size * max_segments)
@@ -92,6 +95,13 @@ static void init_buffers()if (buf_size & lb_mask) buf_size = (buf_size + lb_mask) & ~(lb_mask); + ret =ioctl (test_fd,BLKGETSIZE64 , &dev_bytes); +if (ret <0 ) +err (ret,"%s: ioctl BLKGETSIZE64 failed" , __func__); + +if (dev_bytes < buf_size) + buf_size = dev_bytes; + ret =posix_memalign ((void **)&in_buf, pagesize, buf_size);if (ret)err (EINVAL ,"%s: failed to allocate in-buf" , __func__);
-------------------- diff end -------------------
Quoted 19 lines
src/dio-offsets.c new file +19
-0+ + ret =posix_memalign ((void **)&in_buf, pagesize, buf_size); +if (ret) +err (EINVAL ,"%s: failed to allocate in-buf" , __func__); + + ret =posix_memalign ((void **)&out_buf, pagesize, buf_size); +if (ret) +err (EINVAL ,"%s: failed to allocate out-buf" , __func__); + + fd =open ("/dev/urandom" ,O_RDONLY ); +if (fd <0 ) +err (EINVAL ,"%s: failed to open urandom" , __func__); + + ret =read (fd, out_buf, buf_size); +if (ret <0 ) +err (EINVAL ,"%s: failed to randomize output buffer" , __func__); + +close (fd); +}
[...]
Quoted 4 lines
src/dio-offsets.c new file +4
-0+/* ./$prog-name file */ +int main (int argc,char **argv) +{ +if (argc <2 )
src/dio-offsets.c
Quoted 10 lines
src/dio-offsets.c new file +10
-0+errx (EINVAL ,"expect argments: file" ); + +init_args (argv); +init_buffers (); +run_tests (); +close (test_fd); +free (out_buf); +free (in_buf); + +return 0 ;
Nit: last "}" is missing here. I can add it when I apply this patch.
tests/block/042 line 1
Quoted 7 lines
tests/block/042 new file +1
-0@@ -0,0 +1,26 @@ +#!/bin/bash
Is it okay to add any GPL SPDX license and your copyright here? It is not ideal that I add your copyright and license, but if you specify them in this thread, I will add the Link to this thread in the commit message and fold-in the specifed SPDX license and your copyright.
Quoted 25 lines
tests/block/042 new file +25
-0+ +. tests/block/rc + +DESCRIPTION="Test unusual direct-io offsets" +QUICK=1 + +device_requires() { + _require_test_dev_sysfs +} + +test_device() { + echo "Running ${TEST_NAME}" + + sys_max_segments=$(cat "${TEST_DEV_SYSFS}/queue/max_segments") + sys_dma_alignment=$(cat "${TEST_DEV_SYSFS}/queue/dma_alignment") + sys_virt_boundary_mask=$(cat "${TEST_DEV_SYSFS}/queue/virt_boundary_mask") + sys_logical_block_size=$(cat "${TEST_DEV_SYSFS}/queue/logical_block_size") + sys_max_sectors_kb=$(cat "${TEST_DEV_SYSFS}/queue/max_sectors_kb") + + if ! src/dio-offsets ${TEST_DEV} $sys_max_segments $sys_max_sectors_kb $sys_dma_alignment $sys_virt_boundary_mask $sys_logical_block_size; then + echo "src/dio-offsets failed" + fi + + echo "Test complete" +}
Re: [PATCHv2 1/2] blktests: test direct io offsets
Quoted 35 lines · Shinichiro Kawasaki
Quoted 7 lines · Keith Busch
From: Keith Busch <[email protected]>
Tests various direct IO memory and length alignments against the device's queue limits reported from sysfs.
Signed-off-by Keith Busch <[email protected]>---
[...]
Quoted 5 lines
src/dio-offsets.c new file +0
-0[...]
Quoted 4 lines
src/dio-offsets.c new file +4
-0+static void init_buffers () +{ +unsigned long lb_mask = logical_block_size -1 ; +int fd, ret;src/dio-offsets.c
Quoted 10 lines
src/dio-offsets.c new file +10
-0+ + buf_size = max_bytes * max_segments /2 ; +if (buf_size < logical_block_size * max_segments) +err (EINVAL ,"%s: logical block size is too big" , __func__); + +if (buf_size < logical_block_size *1024 *4 ) + buf_size = logical_block_size *1024 *4 ; + +if (buf_size & lb_mask) + buf_size = (buf_size + lb_mask) & ~(lb_mask);I investigated why this new test case fails with my QEMU SATA device and noticed that the device has 128MiB size. The calculation above sets
buf_size= 192MiB. Thenpwrite()intest_full_size_aligned()is called with 192MiB size and it was truncated to 128MiB. Then the followingcompare()check failed.
Quoted 3 lines
Is it okay to cap the
buf_sizewith the device size? If so, I suggest the change below. With this change, the test case passes with the device. If you are okay with the change, I can fold it in when I apply the patch.
Yeah, totally fine. Much of the sizes were just made up as I went along, but it should probably be programatically calculated based on the device under test.
Quoted 6 lines
Quoted 1 line
+#!/bin/bash
Is it okay to add any GPL SPDX license and your copyright here? It is not ideal that I add your copyright and license, but if you specify them in this thread, I will add the Link to this thread in the commit message and fold-in the specifed SPDX license and your copyright.
Yes, sorry, it should have been there. I added those to some files, but missed this one.
Re: [PATCHv2 1/2] blktests: test direct io offsets
Quoted 46 lines · Keith Busch
Quoted 35 lines · Shinichiro Kawasaki
Quoted 7 lines · Keith Busch
From: Keith Busch <[email protected]>
Tests various direct IO memory and length alignments against the device's queue limits reported from sysfs.
Signed-off-by Keith Busch <[email protected]>---
[...]
Quoted 5 lines
src/dio-offsets.c new file +0
-0[...]
Quoted 4 lines
src/dio-offsets.c new file +4
-0+static void init_buffers () +{ +unsigned long lb_mask = logical_block_size -1 ; +int fd, ret;src/dio-offsets.c
Quoted 10 lines
src/dio-offsets.c new file +10
-0+ + buf_size = max_bytes * max_segments /2 ; +if (buf_size < logical_block_size * max_segments) +err (EINVAL ,"%s: logical block size is too big" , __func__); + +if (buf_size < logical_block_size *1024 *4 ) + buf_size = logical_block_size *1024 *4 ; + +if (buf_size & lb_mask) + buf_size = (buf_size + lb_mask) & ~(lb_mask);I investigated why this new test case fails with my QEMU SATA device and noticed that the device has 128MiB size. The calculation above sets
buf_size= 192MiB. Thenpwrite()intest_full_size_aligned()is called with 192MiB size and it was truncated to 128MiB. Then the followingcompare()check failed.Quoted 3 lines
Is it okay to cap the
buf_sizewith the device size? If so, I suggest the change below. With this change, the test case passes with the device. If you are okay with the change, I can fold it in when I apply the patch.Yeah, totally fine. Much of the sizes were just made up as I went along, but it should probably be programatically calculated based on the device under test.
Quoted 2 lines
Quoted 1 line
+#!/bin/bash
Quoted 7 lines
Quoted 4 lines
Is it okay to add any GPL SPDX license and your copyright here? It is not ideal that I add your copyright and license, but if you specify them in this thread, I will add the Link to this thread in the commit message and fold-in the specifed SPDX license and your copyright.
Yes, sorry, it should have been there. I added those to some files, but missed this one.
Thanks for the confirmations. I have applied the patches. I took the liberty to make follow-up commits for minor clean-ups, hoping they are fine for you.
PATCH v2 2/2 blktests: test io_uring user metadata offsets
In reply to Keith BuschFrom: Keith Busch <[email protected]>
For devices with metadata, tests various userspace offsets with io_uring capabilities. If the metadata is formatted with ref tag protection information, test various seed offsets as well.
--- src/.gitignore | 1 + src/Makefile | 16 +- src/metadata.c | 488 ++++++++++++++++++++++++++++++++++++++++++++ tests/block/043 | 33 +++ tests/block/043.out | 2 + 5 files changed, 532 insertions(+), 8 deletions(-) create mode 100644 src/metadata.c create mode 100755 tests/block/043 create mode 100644 tests/block/043.out
src/.gitignore +1 -0
@@ -3,6 +3,7 @@ /loblksize /loop_change_fd /loop_get_status_null +/metadata /mount_clear_sock /nbdsetsize /openclose
src/Makefile +8 -8
@@ -22,7 +22,8 @@ C_TARGETS := \ sg/syzkaller1 \ zbdioctl -C_MINIUBLK := miniublk +C_URING_TARGETS := miniublk \ + metadata HAVE_LIBURING := $(call HAVE_C_MACRO,liburing.h,IORING_OP_URING_CMD) HAVE_UBLK_HEADER := $(call HAVE_C_HEADER,linux/ublk_cmd.h,1)
@@ -31,9 +32,9 @@ CXX_TARGETS := \ discontiguous-io ifeq ($(HAVE_LIBURING)$(HAVE_UBLK_HEADER), 11) -TARGETS := $(C_TARGETS) $(CXX_TARGETS) $(C_MINIUBLK) +TARGETS := $(C_TARGETS) $(CXX_TARGETS) $(C_URING_TARGETS) else -$(info Skip $(C_MINIUBLK) build due to missing kernel header(v6.0+) or liburing(2.2+)) +$(info Skip $(C_URING_TARGETS) build due to missing kernel header(v6.0+) or liburing(2.2+)) TARGETS := $(C_TARGETS) $(CXX_TARGETS) endif
@@ -42,8 +43,8 @@ CONFIG_DEFS := $(call HAVE_C_HEADER,linux/blkzoned.h,-DHAVE_LINUX_BLKZONED_H) override CFLAGS := -O2 -Wall -Wshadow $(CFLAGS) $(CONFIG_DEFS) override CXXFLAGS := -O2 -std=c++11 -Wall -Wextra -Wshadow -Wno-sign-compare \ -Werror $(CXXFLAGS) $(CONFIG_DEFS) -MINIUBLK_FLAGS := -D_GNU_SOURCE -MINIUBLK_LIBS := -lpthread -luring +URING_FLAGS := -D_GNU_SOURCE +URING_LIBS := -lpthread -luring LDFLAGS ?= all: $(TARGETS)
@@ -61,8 +62,7 @@ $(C_TARGETS): %: %.c $(CXX_TARGETS): %: %.cpp $(CXX) $(CPPFLAGS) $(CXXFLAGS) $(LDFLAGS) -o $@ $^ -$(C_MINIUBLK): %: miniublk.c - $(CC) $(CFLAGS) $(LDFLAGS) $(MINIUBLK_FLAGS) -o $@ miniublk.c \ - $(MINIUBLK_LIBS) +$(C_URING_TARGETS): %: %.c + $(CC) $(CFLAGS) $(LDFLAGS) $(URING_FLAGS) -o $@ $^ $(URING_LIBS) .PHONY: all clean install
src/metadata.c new file +488 -0
488 lines not on this page · raw message
tests/block/043 new file +33 -0
@@ -0,0 +1,33 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 +# Copyright 2025 Keith Busch <[email protected]> +# +# Tests various user space metadata offsets with io_uring capabilities. If the +# format uses ref tag protection, test various seed offsets as well. + +. tests/block/rc +. common/nvme + +DESCRIPTION="Test userspace metadata offsets" +QUICK=1 + +device_requires() { + _test_dev_has_metadata + _test_dev_disables_extended_lba +} + +requires() { + _have_kernel_option IO_URING + _have_kernel_option BLK_DEV_INTEGRITY +} + +test_device() { + echo "Running ${TEST_NAME}" + + if ! src/metadata ${TEST_DEV}; then + echo "src/metadata failed" + fi + + echo "Test complete" +} +
tests/block/043.out new file +2 -0
@@ -0,0 +1,2 @@ +Running block/043 +Test complete
Signature · 1 line
2.47.3
Re: [PATCHv2 2/2] blktests: test io_uring user metadata offsets
Quoted 7 lines · Keith Busch
From: Keith Busch<[email protected]>
For devices with metadata, tests various userspace offsets with
io_uringcapabilities. If the metadata is formatted with ref tag protection information, test various seed offsets as well.Signed-off-by Keith Busch<[email protected]>
Looks good.
-ck