Skip to contentSkip to conversation

selftests: add ublk selftests

PATCH Superseded Ming Lei 2 participants 6 messages Feb 26, 2025 – Feb 27, 2025

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Ming Lei

author
#0 lore raw

v2 cover selftests: add ublk selftests

Hello Guys,

The 1st patch adds one ublk utility and one entry test.

The 2nd patch adds test over file backed ublk.

The 3rd patch adds test for ublk zero copy.

How to run:

- make install
- make headers_install INSTALL_HDR_PATH=/usr # in case UAPI is changed
- reboot
- make -C tools/testing/selftests TARGETS=ublk run_test

Thanks,

V2:
- fix one sqe allocation bug, so ublk zero copy with io_link can pass
- dump log in case of error
- add one more test for mkfs/mount on zero copy

Ming Lei (3):
selftests: ublk: add kernel selftests for ublk
selftests: ublk: add file backed ublk
selftests: ublk: add ublk zero copy test

 MAINTAINERS                                  |    1 +
 tools/testing/selftests/Makefile             |    1 +
 tools/testing/selftests/ublk/.gitignore      |    3 +
 tools/testing/selftests/ublk/Makefile        |   15 +
 tools/testing/selftests/ublk/config          |    1 +
 tools/testing/selftests/ublk/kublk.c         | 1679 ++++++++++++++++++
 tools/testing/selftests/ublk/test_common.sh  |  112 ++
 tools/testing/selftests/ublk/test_loop_01.sh |   30 +
 tools/testing/selftests/ublk/test_loop_02.sh |   21 +
 tools/testing/selftests/ublk/test_loop_03.sh |   32 +
 tools/testing/selftests/ublk/test_loop_04.sh |   21 +
 tools/testing/selftests/ublk/test_null_01.sh |   18 +
 12 files changed, 1934 insertions(+)
 create mode 100644 tools/testing/selftests/ublk/.gitignore
 create mode 100644 tools/testing/selftests/ublk/Makefile
 create mode 100644 tools/testing/selftests/ublk/config
 create mode 100644 tools/testing/selftests/ublk/kublk.c
 create mode 100755 tools/testing/selftests/ublk/test_common.sh
 create mode 100755 tools/testing/selftests/ublk/test_loop_01.sh
 create mode 100755 tools/testing/selftests/ublk/test_loop_02.sh
 create mode 100755 tools/testing/selftests/ublk/test_loop_03.sh
 create mode 100755 tools/testing/selftests/ublk/test_loop_04.sh
 create mode 100755 tools/testing/selftests/ublk/test_null_01.sh
Signature · 1 line
2.47.0
author
#1 lore raw

PATCH v2 1/3 selftests: ublk: add kernel selftests for ublk

Both ublk driver and userspace heavily depends on io_uring subsystem, and tools/testing/selftests/ should be the best place for holding this cross-subsystem tests.

Add basic read/write IO test over this ublk null disk, and make sure ublk working.

More tests will be added.

---
 MAINTAINERS                                  |    1 +
 tools/testing/selftests/Makefile             |    1 +
 tools/testing/selftests/ublk/.gitignore      |    3 +
 tools/testing/selftests/ublk/Makefile        |   11 +
 tools/testing/selftests/ublk/config          |    1 +
 tools/testing/selftests/ublk/kublk.c         | 1363 ++++++++++++++++++
 tools/testing/selftests/ublk/test_common.sh  |   57 +
 tools/testing/selftests/ublk/test_null_01.sh |   18 +
 8 files changed, 1455 insertions(+)
 create mode 100644 tools/testing/selftests/ublk/.gitignore
 create mode 100644 tools/testing/selftests/ublk/Makefile
 create mode 100644 tools/testing/selftests/ublk/config
 create mode 100644 tools/testing/selftests/ublk/kublk.c
 create mode 100755 tools/testing/selftests/ublk/test_common.sh
 create mode 100755 tools/testing/selftests/ublk/test_null_01.sh
MAINTAINERS +1 -0
index efee40ea589f..7dd2fbfc8604 100644
@@ -24237,6 +24237,7 @@ S:	Maintained
 F:	Documentation/block/ublk.rst
 F:	drivers/block/ublk_drv.c
 F:	include/uapi/linux/ublk_cmd.h
+F:	tools/testing/selftests/ublk/
 
 UBSAN
 M:	Kees Cook <[email protected]>
tools/testing/selftests/Makefile +1 -0
index 8daac70c2f9d..52ba0eb5eaa7 100644
@@ -113,6 +113,7 @@ endif
 TARGETS += tmpfs
 TARGETS += tpm2
 TARGETS += tty
+TARGETS += ublk
 TARGETS += uevent
 TARGETS += user_events
 TARGETS += vDSO
tools/testing/selftests/ublk/.gitignore new file +3 -0
new file mode 100644
index 000000000000..8b2871ea7751
@@ -0,0 +1,3 @@
+kublk
+/tools
+*-verify.state
tools/testing/selftests/ublk/Makefile new file +11 -0
new file mode 100644
index 000000000000..e1fb6c804c90
@@ -0,0 +1,11 @@
+# SPDX-License-Identifier: GPL-2.0
+
+CFLAGS += -O3 -Wl,-no-as-needed -Wall -I $(top_srcdir)
+LDLIBS += -lpthread -lm -luring
+
+TEST_PROGS := test_null_01.sh
+
+# Order correspond to 'make run_tests' order
+TEST_GEN_PROGS_EXTENDED = kublk
+
+include ../lib.mk
tools/testing/selftests/ublk/config new file +1 -0
new file mode 100644
index 000000000000..592b0ba4d661
@@ -0,0 +1 @@
+CONFIG_BLK_DEV_UBLK=m
tools/testing/selftests/ublk/kublk.c new file +1363 -0

1,363 lines not on this page · raw message

tools/testing/selftests/ublk/test_common.sh new file +57 -0

57 lines not on this page · raw message

tools/testing/selftests/ublk/test_null_01.sh new file +18 -0
new file mode 100755
index 000000000000..867d4da53fe5
@@ -0,0 +1,18 @@
+#!/bin/bash
+
+. test_common.sh
+
+TID="null_01"
+ERR_CODE=0
+
+_prep_test "null" "basic IO test"
+
+dev_id=`_add_ublk_dev -t null`
+
+# run fio over the two disks
+fio --name=job1 --filename=/dev/ublkb${dev_id} --ioengine=libaio --rw=readwrite --iodepth=32 --size=256M > /dev/null 2>&1
+ERR_CODE=$?
+
+_cleanup_test ${dev_id} "null"
+
+_show_result $TID $ERR_CODE
Signature · 1 line
2.47.0
author
#2 lore raw

PATCH v2 2/3 selftests: ublk: add file backed ublk

In reply to Ming Lei

Add file backed ublk and IO verify test.

---
 tools/testing/selftests/ublk/Makefile        |   2 +
 tools/testing/selftests/ublk/kublk.c         | 172 ++++++++++++++++++-
 tools/testing/selftests/ublk/test_common.sh  |  47 +++++
 tools/testing/selftests/ublk/test_loop_01.sh |  30 ++++
 tools/testing/selftests/ublk/test_loop_02.sh |  21 +++
 5 files changed, 268 insertions(+), 4 deletions(-)
 create mode 100755 tools/testing/selftests/ublk/test_loop_01.sh
 create mode 100755 tools/testing/selftests/ublk/test_loop_02.sh
tools/testing/selftests/ublk/Makefile +2 -0
index e1fb6c804c90..270d2bbe232b 100644
@@ -4,6 +4,8 @@ CFLAGS += -O3 -Wl,-no-as-needed -Wall -I $(top_srcdir)
 LDLIBS += -lpthread -lm -luring
 
 TEST_PROGS := test_null_01.sh
+TEST_PROGS += test_loop_01.sh
+TEST_PROGS += test_loop_02.sh
 
 # Order correspond to 'make run_tests' order
 TEST_GEN_PROGS_EXTENDED = kublk
tools/testing/selftests/ublk/kublk.c +168 -4

214 lines not on this page · raw message

tools/testing/selftests/ublk/test_common.sh +47 -0

52 lines not on this page · raw message

tools/testing/selftests/ublk/test_loop_01.sh new file +30 -0
new file mode 100755
index 000000000000..d65424c8a0d3
@@ -0,0 +1,30 @@
+#!/bin/bash
+
+. test_common.sh
+
+TID="loop_01"
+ERR_CODE=0
+
+_prep_test "loop" "write and verify test"
+
+backfile_0=`_create_backfile 256M`
+
+dev_id=`_add_ublk_dev -t loop $backfile_0`
+
+# run fio over the ublk disk
+fio --name=write_and_verify \
+    --filename=/dev/ublkb${dev_id} \
+    --ioengine=libaio --iodepth=16 \
+    --rw=write \
+    --size=256M \
+    --direct=1 \
+    --verify=crc32c \
+    --do_verify=1 \
+    --bs=4k > /dev/null 2>&1
+ERR_CODE=$?
+
+_cleanup_test ${dev_id} "loop"
+
+_remove_backfile $backfile_0
+
+_show_result $TID $ERR_CODE
tools/testing/selftests/ublk/test_loop_02.sh new file +21 -0
new file mode 100755
index 000000000000..f2e375670c53
@@ -0,0 +1,21 @@
+#!/bin/bash
+
+. test_common.sh
+
+TID="loop_02"
+ERR_CODE=0
+
+_prep_test "loop" "mkfs & mount & umount"
+
+backfile_0=`_create_backfile 256M`
+
+dev_id=`_add_ublk_dev -t loop $backfile_0`
+
+_mkfs_mount_test /dev/ublkb${dev_id}
+ERR_CODE=$?
+
+_cleanup_test ${dev_id} "loop"
+
+_remove_backfile $backfile_0
+
+_show_result $TID $ERR_CODE
Signature · 1 line
2.47.0
author
#3 lore raw

PATCH v2 3/3 selftests: ublk: add ublk zero copy test

In reply to Ming Lei

Add selftests for covering ublk zero copy feature.

---
 tools/testing/selftests/ublk/Makefile        |   2 +
 tools/testing/selftests/ublk/kublk.c         | 196 ++++++++++++++++---
 tools/testing/selftests/ublk/test_common.sh  |   8 +
 tools/testing/selftests/ublk/test_loop_03.sh |  32 +++
 tools/testing/selftests/ublk/test_loop_04.sh |  21 ++
 5 files changed, 237 insertions(+), 22 deletions(-)
 create mode 100755 tools/testing/selftests/ublk/test_loop_03.sh
 create mode 100755 tools/testing/selftests/ublk/test_loop_04.sh
tools/testing/selftests/ublk/Makefile +2 -0
index 270d2bbe232b..c235f4ec8b15 100644
@@ -6,6 +6,8 @@ LDLIBS += -lpthread -lm -luring
 TEST_PROGS := test_null_01.sh
 TEST_PROGS += test_loop_01.sh
 TEST_PROGS += test_loop_02.sh
+TEST_PROGS += test_loop_03.sh
+TEST_PROGS += test_loop_04.sh
 
 # Order correspond to 'make run_tests' order
 TEST_GEN_PROGS_EXTENDED = kublk
tools/testing/selftests/ublk/kublk.c +174 -22

312 lines not on this page · raw message

tools/testing/selftests/ublk/test_common.sh +8 -0
index 2b894c7a8e2e..711538a2cbf5 100755
@@ -101,4 +101,12 @@ _add_ublk_dev() {
 	echo ${dev_id}
 }
 
+_have_feature()
+{
+	if  $UBLK_PROG "features" | grep $1 > /dev/null 2>&1; then
+		return 0
+	fi
+	return 1
+}
+
 export UBLK_PROG=$(pwd)/kublk
tools/testing/selftests/ublk/test_loop_03.sh new file +32 -0
new file mode 100755
index 000000000000..3aa98fe5137b
@@ -0,0 +1,32 @@
+#!/bin/bash
+
+. test_common.sh
+
+TID="loop_03"
+ERR_CODE=0
+
+_have_feature "ZERO_COPY" || exit 4
+
+_prep_test "loop" "write and verify over zero copy"
+
+backfile_0=`_create_backfile 256M`
+
+dev_id=`_add_ublk_dev -t loop $backfile_0 -z`
+
+# run fio over the ublk disk
+fio --name=write_and_verify \
+    --filename=/dev/ublkb${dev_id} \
+    --ioengine=libaio --iodepth=64 \
+    --rw=write \
+    --size=256M \
+    --direct=1 \
+    --verify=crc32c \
+    --do_verify=1 \
+    --bs=4k > /dev/null 2>&1
+ERR_CODE=$?
+
+_cleanup_test ${dev_id} "loop"
+
+_remove_backfile $backfile_0
+
+_show_result $TID $ERR_CODE
tools/testing/selftests/ublk/test_loop_04.sh new file +21 -0
new file mode 100755
index 000000000000..86c0c86c93a2
@@ -0,0 +1,21 @@
+#!/bin/bash
+
+. test_common.sh
+
+TID="loop_04"
+ERR_CODE=0
+
+_prep_test "loop" "mkfs & mount & umount with zero copy"
+
+backfile_0=`_create_backfile 256M`
+
+dev_id=`_add_ublk_dev -t loop -z $backfile_0`
+
+_mkfs_mount_test /dev/ublkb${dev_id}
+ERR_CODE=$?
+
+_cleanup_test ${dev_id} "loop"
+
+_remove_backfile $backfile_0
+
+_show_result $TID $ERR_CODE
Signature · 1 line
2.47.0
#4 lore raw

Re: [PATCH V2 3/3] selftests: ublk: add ublk zero copy test

Quoted 28 lines · Ming Lei
tools/testing/selftests/ublk/kublk.c +28 -0
+	struct io_uring_sqe *reg;
+	struct io_uring_sqe *rw;
+	struct io_uring_sqe *ureg;
+
+	if (!zc) {
+		rw = ublk_queue_alloc_sqe(q);
+		if (!rw)
+			return -ENOMEM;
+
+		io_uring_prep_rw(op, rw, 1 /*fds[1]*/,
+				(void *)iod->addr,
+				iod->nr_sectors << 9,
+				iod->start_sector << 9);
+		io_uring_sqe_set_flags(rw, IOSQE_FIXED_FILE);
+		q->io_inflight++;
+		/* bit63 marks us as tgt io */
+		rw->user_data = build_user_data(tag, op, UBLK_IO_TGT_NORMAL, 1);
+		return 0;
+	}
+
+	ublk_queue_alloc_sqe3(q, &reg, &rw, &ureg);
+
+	io_uring_prep_buf_register(reg, 0, tag, q->q_id, tag);
+	reg->user_data = build_user_data(tag, 0xfe, 1, 1);
+	reg->flags |= IOSQE_CQE_SKIP_SUCCESS;
+	reg->flags |= IOSQE_IO_LINK;
+
+	io_uring_prep_rw(op, rw, 1 /*fds[1]*/, 0,

tools/testing/selftests/ublk/kublk.c static int backing_file_tgt_init(struct ublk_dev *dev) · line 1470

Quoted 10 lines
tools/testing/selftests/ublk/kublk.c +10 -0
+		iod->nr_sectors << 9,
+		iod->start_sector << 9);
+	rw->buf_index = tag;
+	rw->flags |= IOSQE_FIXED_FILE;
+	rw->flags |= IOSQE_IO_LINK;
+	rw->user_data = build_user_data(tag, op, UBLK_IO_TGT_ZC_OP, 1);
+	q->io_inflight++;
+
+	io_uring_prep_buf_unregister(ureg, 0, tag, q->q_id, tag);
+	ureg->user_data = build_user_data(tag, 0xff, UBLK_IO_TGT_ZC_BUF, 1);

You don't have anything handling the unregister command's completion so I think you want the IOSQE_CQE_SKIP_SUCCESS flag on it otherwise you get an unexpected CQE for it.

author
#5 lore raw

Re: [PATCH V2 3/3] selftests: ublk: add ublk zero copy test

Quoted 35 lines · Keith Busch
Quoted 34 lines · Ming Lei
tools/testing/selftests/ublk/kublk.c +34 -0
+	struct io_uring_sqe *reg;
+	struct io_uring_sqe *rw;
+	struct io_uring_sqe *ureg;
+
+	if (!zc) {
+		rw = ublk_queue_alloc_sqe(q);
+		if (!rw)
+			return -ENOMEM;
+
+		io_uring_prep_rw(op, rw, 1 /*fds[1]*/,
+				(void *)iod->addr,
+				iod->nr_sectors << 9,
+				iod->start_sector << 9);
+		io_uring_sqe_set_flags(rw, IOSQE_FIXED_FILE);
+		q->io_inflight++;
+		/* bit63 marks us as tgt io */
+		rw->user_data = build_user_data(tag, op, UBLK_IO_TGT_NORMAL, 1);
+		return 0;
+	}
+
+	ublk_queue_alloc_sqe3(q, &reg, &rw, &ureg);
+
+	io_uring_prep_buf_register(reg, 0, tag, q->q_id, tag);
+	reg->user_data = build_user_data(tag, 0xfe, 1, 1);
+	reg->flags |= IOSQE_CQE_SKIP_SUCCESS;
+	reg->flags |= IOSQE_IO_LINK;
+
+	io_uring_prep_rw(op, rw, 1 /*fds[1]*/, 0,
+		iod->nr_sectors << 9,
+		iod->start_sector << 9);
+	rw->buf_index = tag;
+	rw->flags |= IOSQE_FIXED_FILE;
+	rw->flags |= IOSQE_IO_LINK;
+	rw->user_data = build_user_data(tag, op, UBLK_IO_TGT_ZC_OP, 1);
Quoted 8 lines

tools/testing/selftests/ublk/kublk.c static int backing_file_tgt_init(struct ublk_dev *dev) · line 1470

Quoted 4 lines
tools/testing/selftests/ublk/kublk.c +4 -0
+	q->io_inflight++;
+
+	io_uring_prep_buf_unregister(ureg, 0, tag, q->q_id, tag);
+	ureg->user_data = build_user_data(tag, 0xff, UBLK_IO_TGT_ZC_BUF, 1);

You don't have anything handling the unregister command's completion so I think you want the IOSQE_CQE_SKIP_SUCCESS flag on it otherwise you get an unexpected CQE for it.

Please see ublk_loop_io_done() which does handle unregister command by the flag of UBLK_IO_TGT_ZC_BUF. And it is reasonable to complete the io command after the whole link is done.

BTW, with this way, one ublk bug is actually triggered, and Caleb's patch fixes it.

Thanks,
Ming