Skip to contentSkip to conversation

blktests: add mini ublk source and blktests/033

PATCH In review Ming Lei 3 participants 7 messages Feb 24, 2023 – Mar 2, 2023 Block layer

lore
Share & tools
lore

Ming Lei

author
#0 lore raw

v4 cover blktests: add mini ublk source and blktests/033

Hello,

The 1st patch adds one mini ublk program, which only supports null & loop targets.

The 2nd patch add blktests/033 for covering gendisk leak issue.

v4:
- fix one error handling bug in src/miniublk.c
- patch style improvement on src/miniublk.c
- both are suggested by Chaitanya Kulkarni

v3:
- move minublk.c into src/
- remove '-i' in top Makefile
- fix commit log for 1/2
- add 'udevadm settle' after adding device, so that the following test can be done reliably
- fix _init_ublk()
- redirect runtime log into $FULL
- all are suggested by Shinichiro Kawasaki

v2:
- cleanup & bugfix on miniublk.c
- avoid ignoring build warning just for addressing missed liburing or ublk kernel header
- patch style fix
- make 'make check' happy

Ming Lei (2):
src: add mini ublk source code
block/033: add test to cover gendisk leak

 common/ublk         |   35 ++
 src/.gitignore      |    1 +
 src/Makefile        |   18 +
 src/miniublk.c      | 1367 +++++++++++++++++++++++++++++++++++++++++++
 tests/block/033     |   41 ++
 tests/block/033.out |    2 +
 6 files changed, 1464 insertions(+)
 create mode 100644 common/ublk
 create mode 100644 src/miniublk.c
 create mode 100755 tests/block/033
 create mode 100644 tests/block/033.out
Signature · 1 line
2.31.1
author
#1 lore raw

PATCH v4 1/2 src: add mini ublk source code

Prepare for adding ublk related test:

1) ublk delete is sync removal, this way is convenient to blkg/queue/disk instance leak issue

2) mini ublk has two builtin target(null, loop), and loop IO is handled by io_uring, so we can use ublk to cover part of io_uring workloads

3) not like loop/nbd, ublk won't pre-allocate/add disk, and always add/delete disk dynamically, this way may cover disk plug & unplug tests

4) ublk specific test given people starts to use it, so better to let blktest cover ublk related tests

Add mini ublk source for test purpose only, which is easy to use:

./miniublk add -t {null|loop} [-q nr_queues] [-d depth] [-n dev_id]

	 default: nr_queues=2(max 4), depth=128(max 128), dev_id=-1(auto allocation)
	 -t loop -f backing_file
	 -t null

./miniublk del [-n dev_id] -a
-a delete all devices, -n delete specified device
./miniublk list [-n dev_id] -a
-a list all devices, -n list specified device, default -a

miniublk depends on liburing 2.2, adds HAVE_LIBURING for checking if liburing 2.2 exists; also add HAVE_UBLK_HEADER for checking ublk kernel UAPI header exits. If either of two dependencies can't be met, simply ignore miniublk target.

Also v6.0 is the 1st linux kernel release with ublk.

---
 src/.gitignore |    1 +
 src/Makefile   |   18 +
 src/miniublk.c | 1367 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 1386 insertions(+)
 create mode 100644 src/miniublk.c
src/.gitignore +1 -0
index 355bed3..df7aff5 100644
@@ -8,3 +8,4 @@
 /sg/dxfer-from-dev
 /sg/syzkaller1
 /zbdioctl
+/miniublk
src/Makefile +18 -0
index 3b587f6..81c6541 100644
@@ -2,6 +2,10 @@ HAVE_C_HEADER = $(shell if echo "\#include <$(1)>" |		\
 		$(CC) -E - > /dev/null 2>&1; then echo "$(2)";	\
 		else echo "$(3)"; fi)
 
+HAVE_C_MACRO = $(shell if echo "#include <$(1)>" |	\
+		$(CC) -E - 2>&1 /dev/null | grep $(2) > /dev/null 2>&1; \
+		then echo 1;else echo 0; fi)
+
 C_TARGETS := \
 	loblksize \
 	loop_change_fd \
@@ -13,16 +17,27 @@ C_TARGETS := \
 	sg/syzkaller1 \
 	zbdioctl
 
+C_MINIUBLK := miniublk
+
+HAVE_LIBURING := $(call HAVE_C_MACRO,liburing.h,IORING_OP_URING_CMD)
+HAVE_UBLK_HEADER := $(call HAVE_C_HEADER,linux/ublk_cmd.h,1)
+
 CXX_TARGETS := \
 	discontiguous-io
 
+ifeq ($(HAVE_LIBURING)$(HAVE_UBLK_HEADER), 11)
+TARGETS := $(C_TARGETS) $(CXX_TARGETS) $(C_MINIUBLK)
+else
+$(info Skip $(C_MINIUBLK) build due to missing kernel header(v6.0+) or liburing(2.2+))
 TARGETS := $(C_TARGETS) $(CXX_TARGETS)
+endif
 
 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 -lpthread -luring
 
 all: $(TARGETS)
 
@@ -39,4 +54,7 @@ $(C_TARGETS): %: %.c
 $(CXX_TARGETS): %: %.cpp
 	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -o $@ $^
 
+$(C_MINIUBLK): %: miniublk.c
+	$(CC) $(CFLAGS) $(MINIUBLK_FLAGS) -o $@ miniublk.c
+
 .PHONY: all clean install
src/miniublk.c new file +1367 -0

1,367 lines not on this page · raw message

Signature · 1 line
2.31.1
author
#2 lore raw

PATCH v4 2/2 block/033: add test to cover gendisk leak

In reply to Ming Lei

So far only sync ublk removal is supported, and the device's last reference is dropped in gendisk's ->free_disk(), so it can be used to test gendisk leak issue.

---
 common/ublk         | 35 +++++++++++++++++++++++++++++++++++
 tests/block/033     | 41 +++++++++++++++++++++++++++++++++++++++++
 tests/block/033.out |  2 ++
 3 files changed, 78 insertions(+)
 create mode 100644 common/ublk
 create mode 100755 tests/block/033
 create mode 100644 tests/block/033.out
common/ublk new file +35 -0
new file mode 100644
index 0000000..932c534
@@ -0,0 +1,35 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-3.0+
+# Copyright (C) 2023 Ming Lei
+#
+# ublk_drv helper functions.
+
+. common/shellcheck
+
+_have_ublk() {
+	_have_driver ublk_drv
+	_have_src_program miniublk
+}
+
+_remove_ublk_devices() {
+	src/miniublk del -a
+}
+
+_init_ublk() {
+	_remove_ublk_devices
+
+	modprobe -rq ublk_drv
+	if ! modprobe ublk_drv; then
+		SKIP_REASONS+=("requires ublk_drv")
+		return 1
+	fi
+
+	udevadm settle
+	return 0
+}
+
+_exit_ublk() {
+	_remove_ublk_devices
+	udevadm settle
+	modprobe -r -q ublk_drv
+}
tests/block/033 new file +41 -0

41 lines not on this page · raw message

tests/block/033.out new file +2 -0
new file mode 100644
index 0000000..067846a
@@ -0,0 +1,2 @@
+Running block/033
+Test complete
Signature · 1 line
2.31.1
#3 lore raw
In reply to Ming Lei
Quoted 11 lines · Ming Lei

Hello,

The 1st patch adds one mini ublk program, which only supports null & loop targets.

The 2nd patch add blktests/033 for covering gendisk leak issue.

v4:
- fix one error handling bug in src/miniublk.c
- patch style improvement on src/miniublk.c
- both are suggested by Chaitanya Kulkarni

The changes for v4 looks good to me. I also did a trial run together with the follow-up patch [*] and observed good result.

[*] https://lore.kernel.org/linux-block/[email protected]/

Chaitanya, will you review v4 again?

Signature · 1 line
Shin'ichiro Kawasaki
#5 lore raw
Quoted 6 lines · Chaitanya Kulkarni
Quoted 5 lines · Ming Lei

Hello,

The 1st patch adds one mini ublk program, which only supports null & loop targets.

Quoted 6 lines
Quoted 2 lines

The 2nd patch add blktests/033 for covering gendisk leak issue.

Looks good to me, feel free to merge it.

Thanks Chaitanya.

Ming, I've merged the patches. Thanks!

Signature · 1 line
Shin'ichiro Kawasaki
author
#6 lore raw
Quoted 10 lines · Shinichiro Kawasaki
Quoted 9 lines · Chaitanya Kulkarni
Quoted 7 lines · Ming Lei

Hello,

The 1st patch adds one mini ublk program, which only supports null & loop targets.

The 2nd patch add blktests/033 for covering gendisk leak issue.

Quoted 7 lines
Quoted 3 lines

Looks good to me, feel free to merge it.

Thanks Chaitanya.

Ming, I've merged the patches. Thanks!

Great! Thanks all of you!

Thanks,
Ming