Skip to contentSkip to conversation

ublk: add io_uring based userspace block driver

PATCH Applied Ming Lei 5 participants 16 messages Jul 13, 2022 – Jul 19, 2022

Subsystems (3)
lore
Share & tools
lore

Ming Lei

author
#0 lore raw

v5 cover ublk: add io_uring based userspace block driver

Hello Guys,

ublk driver is one kernel driver for implementing generic userspace block device/driver, which delivers io request from ublk block device(/dev/ublkbN) into ublk server[1] which is the userspace part of ublk for communicating with ublk driver and handling specific io logic by its target module.

Another thing ublk driver handles is to copy data between user space buffer and request/bio's pages, or take zero copy if mm is ready for support it in future. ublk driver doesn't handle any IO logic of the specific driver, so it is small/simple, and all io logics are done by the target code in ublkserver.

The above two are main jobs done by ublk driver.

ublk driver can help to move IO logic into userspace, in which the development work is easier/more effective than doing in kernel, such as, ublk-loop takes < 200 lines of loop specific code to get basically same function with kernel loop block driver, meantime the performance is is even better than kernel loop with same setting. ublksrv[1] provide built-in test for comparing both by running "make test T=loop", for example, see the test result running on VM which is over my lattop(root disk is nvme/device mapper/xfs):

	[root@ktest-36 ubdsrv]#make -s -C /root/git/ubdsrv/tests run T=loop/001 R=10
	running loop/001
		fio (ublk/loop(/root/git/ubdsrv/tests/tmp/ublk_loop_VqbMA), libaio, bs 4k, dio, hw queues:1)...
		randwrite: jobs 1, iops 32572
		randread: jobs 1, iops 143052
		rw: jobs 1, iops read 29919 write 29964
	
	[root@ktest-36 ubdsrv]# make test T=loop/003
	make -s -C /root/git/ubdsrv/tests run T=loop/003 R=10
	running loop/003
		fio (kernel_loop/kloop(/root/git/ubdsrv/tests/tmp/ublk_loop_ZIVnG), libaio, bs 4k, dio, hw queues:1)...
		randwrite: jobs 1, iops 27436
		randread: jobs 1, iops 95273
		rw: jobs 1, iops read 22542 write 22543 

Another example is high performance qcow2 support[2], which could be built with ublk framework more easily than doing it inside kernel.

Also there are more people who express interests on userspace block driver[3], Gabriel Krisman Bertazi proposes this topic in lsf/mm/ebpf 2022 and mentioned requirement from Google. Ziyang Zhang from Alibaba said they "plan to replace TCMU by UBD as a new choice" because UBD can get better throughput than TCMU even with single queue[4], meantime UBD is simple. Also there is userspace storage service for providing storage to containers.

It is io_uring based: io request is delivered to userspace via new added io_uring command which has been proved as very efficient for making nvme passthrough IO to get better IOPS than io_uring(READ/WRITE). Meantime one shared/mmap buffer is used for sharing io descriptor to userspace, the buffer is readonly for userspace, each IO just takes 24bytes so far. It is suggested to use io_uring in userspace(target part of ublk server) to handle IO request too. And it is still easy for ublkserver to support io handling by non-io_uring, and this work isn't done yet, but can be supported easily with help o eventfd.

This way is efficient since no extra io command copy is required, no sleep is needed in transferring io command to userspace. Meantime the communication protocol is simple and efficient, one single command of UBD_IO_COMMIT_AND_FETCH_REQ can handle both fetching io request desc and commit command result in one trip. IO handling is often batched after single io_uring_enter() returns, both IO requests from ublk server target and IO commands could be handled as a whole batch.

And the patch by patch change can be found in the following tree:

https://github.com/ming1/linux/tree/my_for-5.20-ubd-devel_v4

ublk server repo(master branch):

https://github.com/ming1/ubdsrv

Any comments are welcome!

Since V4:
- drop patch of "ublk_drv: add UBLK_IO_REFETCH_REQ for supporting to build as module", instead of using io_uring_cmd_complete_in_task for building driver as module

- simplify aborting code

Since V3:
- address Gabriel Krisman Bertazi's comments on V3: add userspace data validation before handling command, remove warning, ...
- remove UBLK_IO_COMMIT_REQ command as suggested by Zixiang and Gabriel Krisman Bertazi
- fix one request double free when running abort
- rewrite/cleanup ublk_copy_pages(), then this handling becomes very clean
- add one command of UBLK_IO_REFETCH_REQ for allowing ublk_drv to build as module

Since V2:
- fix one big performance problem:

	https://github.com/ming1/linux/commit/3c9fd476951759858cc548dee4cedc074194d0b0

- rename as ublk, as suggested by Gabriel Krisman Bertazi
- lots of cleanup & code improvement & bugfix, see details in git hisotry

Since V1:

Remove RFC now because ublk driver codes gets lots of cleanup, enhancement and bug fixes since V1:

- cleanup uapi: remove ublk specific error code, switch to linux error code, remove one command op, remove one field from cmd_desc

- add monitor mechanism to handle ubq_daemon being killed, ublksrv[1] includes builtin tests for covering heavy IO with deleting ublk / killing ubq_daemon at the same time, and V2 pass all the two tests(make test T=generic), and the abort/stop mechanism is simple

- fix MQ command buffer mmap bug, and now 'xfstetests -g auto' works well on MQ ublk-loop devices(test/scratch)

- improve batching submission as suggested by Jens

- improve handling for starting device, replace random wait/poll with completion

- all kinds of cleanup, bug fix,..

Ming Lei (2):
ublk_drv: add io_uring based userspace block driver
ublk_drv: support to complete io command via task_work_add

 drivers/block/Kconfig         |    6 +
 drivers/block/Makefile        |    2 +
 drivers/block/ublk_drv.c      | 1589 +++++++++++++++++++++++++++++++++
 include/uapi/linux/ublk_cmd.h |  162 ++++
 4 files changed, 1759 insertions(+)
 create mode 100644 drivers/block/ublk_drv.c
 create mode 100644 include/uapi/linux/ublk_cmd.h
Signature · 1 line
2.31.1
author
#1 lore raw

PATCH v5 1/2 ublk_drv: add io_uring based userspace block driver

This is the driver part of userspace block driver(ublk driver), the other part is userspace daemon part(ublksrv)[1].

The two parts communicate by io_uring's IORING_OP_URING_CMD with one shared cmd buffer for storing io command, and the buffer is read only for ublksrv, each io command is indexed by io request tag directly, and is written by ublk driver.

For example, when one READ io request is submitted to ublk block driver, ublk driver stores the io command into cmd buffer first, then completes one IORING_OP_URING_CMD for notifying ublksrv, and the URING_CMD is issued to ublk driver beforehand by ublksrv for getting notification of any new io request, and each URING_CMD is associated with one io request by tag.

After ublksrv gets the io command, it translates and handles the ublk io request, such as, for the ublk-loop target, ublksrv translates the request into same request on another file or disk, like the kernel loop block driver. In ublksrv's implementation, the io is still handled by io_uring, and share same ring with IORING_OP_URING_CMD command. When the target io request is done, the same IORING_OP_URING_CMD is issued to ublk driver for both committing io request result and getting future notification of new io request.

Another thing done by ublk driver is to copy data between kernel io request and ublksrv's io buffer:

1) before ubsrv handles WRITE request, copy the request's data into ublksrv's userspace io buffer, so that ublksrv can handle the write request

2) after ubsrv handles READ request, copy ublksrv's userspace io buffer into this READ request, then ublk driver can complete the READ request

Zero copy may be switched if mm is ready to support it.

ublk driver doesn't handle any logic of the specific user space driver, so it is small/simple enough.

[1] ublksrv

https://github.com/ming1/ubdsrv

---
 drivers/block/Kconfig         |    6 +
 drivers/block/Makefile        |    2 +
 drivers/block/ublk_drv.c      | 1530 +++++++++++++++++++++++++++++++++
 include/uapi/linux/ublk_cmd.h |  156 ++++
 4 files changed, 1694 insertions(+)
 create mode 100644 drivers/block/ublk_drv.c
 create mode 100644 include/uapi/linux/ublk_cmd.h
drivers/block/Kconfig +6 -0
index fdb81f2794cd..2ba77fd960c2 100644
@@ -408,6 +408,12 @@ config BLK_DEV_RBD
 
 	  If unsure, say N.
 
+config BLK_DEV_UBLK
+	tristate "Userspace block driver"
+	select IO_URING
+	help
+          io uring based userspace block driver.
+
 source "drivers/block/rnbd/Kconfig"
 
 endif # BLK_DEV
drivers/block/Makefile +2 -0
index 934a9c7c3a7c..be631352567e 100644
@@ -39,4 +39,6 @@ obj-$(CONFIG_BLK_DEV_RNBD)	+= rnbd/
 
 obj-$(CONFIG_BLK_DEV_NULL_BLK)	+= null_blk/
 
+obj-$(CONFIG_BLK_DEV_UBLK)			+= ublk_drv.o
+
 swim_mod-y	:= swim.o swim_asm.o
drivers/block/ublk_drv.c new file +1530 -0

1,530 lines not on this page · raw message

include/uapi/linux/ublk_cmd.h new file +156 -0

156 lines not on this page · raw message

Signature · 1 line
2.31.1
#2 lore raw

Re: [PATCH V5 1/2] ublk_drv: add io_uring based userspace block driver

Quoted 38 lines · Ming Lei

This is the driver part of userspace block driver(ublk driver), the other part is userspace daemon part(ublksrv)[1].

The two parts communicate by io_uring's IORING_OP_URING_CMD with one shared cmd buffer for storing io command, and the buffer is read only for ublksrv, each io command is indexed by io request tag directly, and is written by ublk driver.

For example, when one READ io request is submitted to ublk block driver, ublk driver stores the io command into cmd buffer first, then completes one IORING_OP_URING_CMD for notifying ublksrv, and the URING_CMD is issued to ublk driver beforehand by ublksrv for getting notification of any new io request, and each URING_CMD is associated with one io request by tag.

After ublksrv gets the io command, it translates and handles the ublk io request, such as, for the ublk-loop target, ublksrv translates the request into same request on another file or disk, like the kernel loop block driver. In ublksrv's implementation, the io is still handled by io_uring, and share same ring with IORING_OP_URING_CMD command. When the target io request is done, the same IORING_OP_URING_CMD is issued to ublk driver for both committing io request result and getting future notification of new io request.

Another thing done by ublk driver is to copy data between kernel io request and ublksrv's io buffer:

1) before ubsrv handles WRITE request, copy the request's data into ublksrv's userspace io buffer, so that ublksrv can handle the write request

2) after ubsrv handles READ request, copy ublksrv's userspace io buffer into this READ request, then ublk driver can complete the READ request

Zero copy may be switched if mm is ready to support it.

ublk driver doesn't handle any logic of the specific user space driver, so it is small/simple enough.

Quoted 6 lines

[1] ublksrv

https://github.com/ming1/ubdsrv

---

Hi, Ming

I find that a big change from v4 to v5 is the simplification of locks.

In v5 you remove ubq->abort_lock, and I want to ask why it is OK to remove it?

If you have time, could you explain how ublk deals with potential race on:
1)queue_rq 2)ublk_abort_queue 3) ublk_ctrl_stop_dev 4) ublk_rq_task_work. (Lock in ublk really confuses me...)

[...]

Quoted 18 lines
drivers/block/ublk_drv.c new file +18 -0
+
+/*
+ * __ublk_fail_req() may be called from abort context or ->ubq_daemon
+ * context during exiting, so lock is required.
+ *
+ * Also aborting may not be started yet, keep in mind that one failed
+ * request may be issued by block layer again.
+ */
+static void __ublk_fail_req(struct ublk_io *io, struct request *req)
+{
+	WARN_ON_ONCE(io->flags & UBLK_IO_FLAG_ACTIVE);
+
+	if (!(io->flags & UBLK_IO_FLAG_ABORTED)) {
+		io->flags |= UBLK_IO_FLAG_ABORTED;
+		blk_mq_end_request(req, BLK_STS_IOERR);
+	}
+}
+

[...]

Quoted 21 lines
drivers/block/ublk_drv.c new file +21 -0
+
+/*
+ * When ->ubq_daemon is exiting, either new request is ended immediately,
+ * or any queued io command is drained, so it is safe to abort queue
+ * lockless
+ */
+static void ublk_abort_queue(struct ublk_device *ub, struct ublk_queue *ubq)
+{
+	int i;
+
+	if (!ublk_get_device(ub))
+		return;
+
+	for (i = 0; i < ubq->q_depth; i++) {
+		struct ublk_io *io = &ubq->ios[i];
+
+		if (!(io->flags & UBLK_IO_FLAG_ACTIVE)) {
+			struct request *rq;
+
+			/*
+			 * Either we fail the request or ublk_rq_task_work_fn

drivers/block/ublk_drv.c line 712

Quoted 10 lines
drivers/block/ublk_drv.c new file +10 -0
+			 * will do it
+			 */
+			rq = blk_mq_tag_to_rq(ub->tag_set.tags[ubq->q_id], i);
+			if (rq)
+				__ublk_fail_req(io, rq);
+		}
+	}
+	ublk_put_device(ub);
+}
+

Another problem:

1) comment of __ublk_fail_req(): "so lock is required"

2) comment of ublk_abort_queue(): "so it is safe to abort queue lockless"

3) ublk_abort_queue() calls _ublk_fail_req() on all ubqs.

Perhaps you need to update the comments?

Regards,
Zhang

author
#3 lore raw

Re: [PATCH V5 1/2] ublk_drv: add io_uring based userspace block driver

Quoted 47 lines · Ziyang Zhang
Quoted 38 lines · Ming Lei

This is the driver part of userspace block driver(ublk driver), the other part is userspace daemon part(ublksrv)[1].

The two parts communicate by io_uring's IORING_OP_URING_CMD with one shared cmd buffer for storing io command, and the buffer is read only for ublksrv, each io command is indexed by io request tag directly, and is written by ublk driver.

For example, when one READ io request is submitted to ublk block driver, ublk driver stores the io command into cmd buffer first, then completes one IORING_OP_URING_CMD for notifying ublksrv, and the URING_CMD is issued to ublk driver beforehand by ublksrv for getting notification of any new io request, and each URING_CMD is associated with one io request by tag.

After ublksrv gets the io command, it translates and handles the ublk io request, such as, for the ublk-loop target, ublksrv translates the request into same request on another file or disk, like the kernel loop block driver. In ublksrv's implementation, the io is still handled by io_uring, and share same ring with IORING_OP_URING_CMD command. When the target io request is done, the same IORING_OP_URING_CMD is issued to ublk driver for both committing io request result and getting future notification of new io request.

Another thing done by ublk driver is to copy data between kernel io request and ublksrv's io buffer:

1) before ubsrv handles WRITE request, copy the request's data into ublksrv's userspace io buffer, so that ublksrv can handle the write request

2) after ubsrv handles READ request, copy ublksrv's userspace io buffer into this READ request, then ublk driver can complete the READ request

Zero copy may be switched if mm is ready to support it.

ublk driver doesn't handle any logic of the specific user space driver, so it is small/simple enough.

Quoted 6 lines

[1] ublksrv

https://github.com/ming1/ubdsrv

---

Quoted 5 lines

Hi, Ming

I find that a big change from v4 to v5 is the simplification of locks.

In v5 you remove ubq->abort_lock, and I want to ask why it is OK to remove it?

Actually V4 and previous version dealt with the issue too complicated.

Quoted 4 lines

If you have time, could you explain how ublk deals with potential race on:
1)queue_rq 2)ublk_abort_queue 3) ublk_ctrl_stop_dev 4) ublk_rq_task_work. (Lock in ublk really confuses me...)

One big change is the following code:

__ublk_rq_task_work():

	bool task_exiting = current != ubq->ubq_daemon ||
                (current->flags & PF_EXITING);
	...
	if (unlikely(task_exiting)) {
                blk_mq_end_request(req, BLK_STS_IOERR);
                mod_delayed_work(system_wq, &ub->monitor_work, 0);
                return;
    }

Abort is always started after PF_EXITING is set, but if PF_EXITING is set, __ublk_rq_task_work fails the request immediately, then io->flags won't be touched, then no race with abort. Also PF_EXITING is per-task flag, can only be set before calling __ublk_rq_task_work(), and setting it actually serialized with calling task work func.

In ublk_queue_rq(), we don't touch io->flags, so there isn't race with abort.

Wrt. ublk_ctrl_stop_dev(), it isn't related with abort directly, and if del_gendisk() waits for inflight IO, abort work will be started for making forward progress. After del_gendisk() returns, there can't be any inflight io, so it is safe to cancel other pending io command.

Quoted 60 lines

[...]

Quoted 18 lines
drivers/block/ublk_drv.c new file +18 -0
+
+/*
+ * __ublk_fail_req() may be called from abort context or ->ubq_daemon
+ * context during exiting, so lock is required.
+ *
+ * Also aborting may not be started yet, keep in mind that one failed
+ * request may be issued by block layer again.
+ */
+static void __ublk_fail_req(struct ublk_io *io, struct request *req)
+{
+	WARN_ON_ONCE(io->flags & UBLK_IO_FLAG_ACTIVE);
+
+	if (!(io->flags & UBLK_IO_FLAG_ABORTED)) {
+		io->flags |= UBLK_IO_FLAG_ABORTED;
+		blk_mq_end_request(req, BLK_STS_IOERR);
+	}
+}
+

[...]

Quoted 21 lines
drivers/block/ublk_drv.c new file +21 -0
+
+/*
+ * When ->ubq_daemon is exiting, either new request is ended immediately,
+ * or any queued io command is drained, so it is safe to abort queue
+ * lockless
+ */
+static void ublk_abort_queue(struct ublk_device *ub, struct ublk_queue *ubq)
+{
+	int i;
+
+	if (!ublk_get_device(ub))
+		return;
+
+	for (i = 0; i < ubq->q_depth; i++) {
+		struct ublk_io *io = &ubq->ios[i];
+
+		if (!(io->flags & UBLK_IO_FLAG_ACTIVE)) {
+			struct request *rq;
+
+			/*
+			 * Either we fail the request or ublk_rq_task_work_fn

drivers/block/ublk_drv.c line 712

Quoted 10 lines
drivers/block/ublk_drv.c new file +10 -0
+			 * will do it
+			 */
+			rq = blk_mq_tag_to_rq(ub->tag_set.tags[ubq->q_id], i);
+			if (rq)
+				__ublk_fail_req(io, rq);
+		}
+	}
+	ublk_put_device(ub);
+}
+

Another problem:

Quoted 1 line

1) comment of __ublk_fail_req(): "so lock is required"

Yeah, now __ublk_fail_req is only called in abort context, and no race with task work any more, so lock isn't needed.

Quoted 2 lines

2) comment of ublk_abort_queue(): "so it is safe to abort queue lockless"

This comment is updated in v5, and it is correct.

Quoted 2 lines

3) ublk_abort_queue() calls _ublk_fail_req() on all ubqs.

No, ublk_abort_queue() only aborts the passed ubq, so if one ubq daemon is aborted, other ubqs can still handle IO during deleting disk.

Thanks,
Ming

#4 lore raw

Re: [PATCH V5 1/2] ublk_drv: add io_uring based userspace block driver

Quoted 79 lines · Ming Lei
Quoted 47 lines · Ziyang Zhang
Quoted 38 lines · Ming Lei

This is the driver part of userspace block driver(ublk driver), the other part is userspace daemon part(ublksrv)[1].

The two parts communicate by io_uring's IORING_OP_URING_CMD with one shared cmd buffer for storing io command, and the buffer is read only for ublksrv, each io command is indexed by io request tag directly, and is written by ublk driver.

For example, when one READ io request is submitted to ublk block driver, ublk driver stores the io command into cmd buffer first, then completes one IORING_OP_URING_CMD for notifying ublksrv, and the URING_CMD is issued to ublk driver beforehand by ublksrv for getting notification of any new io request, and each URING_CMD is associated with one io request by tag.

After ublksrv gets the io command, it translates and handles the ublk io request, such as, for the ublk-loop target, ublksrv translates the request into same request on another file or disk, like the kernel loop block driver. In ublksrv's implementation, the io is still handled by io_uring, and share same ring with IORING_OP_URING_CMD command. When the target io request is done, the same IORING_OP_URING_CMD is issued to ublk driver for both committing io request result and getting future notification of new io request.

Another thing done by ublk driver is to copy data between kernel io request and ublksrv's io buffer:

1) before ubsrv handles WRITE request, copy the request's data into ublksrv's userspace io buffer, so that ublksrv can handle the write request

2) after ubsrv handles READ request, copy ublksrv's userspace io buffer into this READ request, then ublk driver can complete the READ request

Zero copy may be switched if mm is ready to support it.

ublk driver doesn't handle any logic of the specific user space driver, so it is small/simple enough.

Quoted 6 lines

[1] ublksrv

https://github.com/ming1/ubdsrv

---

Quoted 5 lines

Hi, Ming

I find that a big change from v4 to v5 is the simplification of locks.

In v5 you remove ubq->abort_lock, and I want to ask why it is OK to remove it?

Actually V4 and previous version dealt with the issue too complicated.

Quoted 4 lines

If you have time, could you explain how ublk deals with potential race on:
1)queue_rq 2)ublk_abort_queue 3) ublk_ctrl_stop_dev 4) ublk_rq_task_work. (Lock in ublk really confuses me...)

One big change is the following code:

__ublk_rq_task_work():

	bool task_exiting = current != ubq->ubq_daemon ||
                (current->flags & PF_EXITING);
	...
	if (unlikely(task_exiting)) {
                blk_mq_end_request(req, BLK_STS_IOERR);
                mod_delayed_work(system_wq, &ub->monitor_work, 0);
                return;
    }

Abort is always started after PF_EXITING is set, but if PF_EXITING is set, __ublk_rq_task_work fails the request immediately, then io->flags won't be touched, then no race with abort. Also PF_EXITING is per-task flag, can only be set before calling __ublk_rq_task_work(), and setting it actually serialized with calling task work func.

Quoted 8 lines

In ublk_queue_rq(), we don't touch io->flags, so there isn't race with abort.

Wrt. ublk_ctrl_stop_dev(), it isn't related with abort directly, and if del_gendisk() waits for inflight IO, abort work will be started for making forward progress. After del_gendisk() returns, there can't be any inflight io, so it is safe to cancel other pending io command.

Thanks, Ming. I understand the aborting code now. And it looks good to me.

Previously I think maybe monitor_work and task_work may be scheduled at the same time while task is exiting and blk_mq_end_request() on the same tag could be called twice.

But I find there is a check on ublk_io's flag(UBLK_IO_FLAG_ACTIVE) in ublk_daemon_monitor_work() and ublk_io is aborted in task_work immediately(with UBLK_IO_FLAG_ACTIVE set, not cleared yet)

So there is no chance to call a send blk_mq_end_request() on the same tag.

Besides, for ublk_ios with UBLK_IO_FLAG_ACTIVE unset,
stop_work scheduled in monitor work will call ublk_cancel_queue() by sending cqes with UBLK_IO_RES_ABORT.

Put it together:

When daemon is PF_EXITING:

1) current ublk_io: aborted immediately in task_work

2) UBLK_IO_FLAG_ACTIVE set: aborted in ublk_daemon_monitor_work

3) UBLK_IO_FLAG_ACTIVE unset: send cqe with UBLK_IO_RES_ABORT

Hope I'm correct this time. :)

Quoted 60 lines
Quoted 60 lines

[...]

Quoted 18 lines
drivers/block/ublk_drv.c new file +18 -0
+
+/*
+ * __ublk_fail_req() may be called from abort context or ->ubq_daemon
+ * context during exiting, so lock is required.
+ *
+ * Also aborting may not be started yet, keep in mind that one failed
+ * request may be issued by block layer again.
+ */
+static void __ublk_fail_req(struct ublk_io *io, struct request *req)
+{
+	WARN_ON_ONCE(io->flags & UBLK_IO_FLAG_ACTIVE);
+
+	if (!(io->flags & UBLK_IO_FLAG_ABORTED)) {
+		io->flags |= UBLK_IO_FLAG_ABORTED;
+		blk_mq_end_request(req, BLK_STS_IOERR);
+	}
+}
+

[...]

Quoted 21 lines
drivers/block/ublk_drv.c new file +21 -0
+
+/*
+ * When ->ubq_daemon is exiting, either new request is ended immediately,
+ * or any queued io command is drained, so it is safe to abort queue
+ * lockless
+ */
+static void ublk_abort_queue(struct ublk_device *ub, struct ublk_queue *ubq)
+{
+	int i;
+
+	if (!ublk_get_device(ub))
+		return;
+
+	for (i = 0; i < ubq->q_depth; i++) {
+		struct ublk_io *io = &ubq->ios[i];
+
+		if (!(io->flags & UBLK_IO_FLAG_ACTIVE)) {
+			struct request *rq;
+
+			/*
+			 * Either we fail the request or ublk_rq_task_work_fn

drivers/block/ublk_drv.c line 712

Quoted 10 lines
drivers/block/ublk_drv.c new file +10 -0
+			 * will do it
+			 */
+			rq = blk_mq_tag_to_rq(ub->tag_set.tags[ubq->q_id], i);
+			if (rq)
+				__ublk_fail_req(io, rq);
+		}
+	}
+	ublk_put_device(ub);
+}
+

Another problem:

Quoted 4 lines
Quoted 1 line

1) comment of __ublk_fail_req(): "so lock is required"

Yeah, now __ublk_fail_req is only called in abort context, and no race with task work any more, so lock isn't needed.

Ok, I see.

Quoted 11 lines
Quoted 3 lines

2) comment of ublk_abort_queue(): "so it is safe to abort queue lockless"

This comment is updated in v5, and it is correct.

Quoted 2 lines

3) ublk_abort_queue() calls _ublk_fail_req() on all ubqs.

No, ublk_abort_queue() only aborts the passed ubq, so if one ubq daemon is aborted, other ubqs can still handle IO during deleting disk.

Ok, I see.

I think if one ubq daemon is killed(and blk-mq requests related to it are aborted), stop work should call del_gendisk() and other ubq daemons can still complete blk-mq requests but no new blk-mq requests will be issued.
After that, these unkilled ubq daemons will get UBLK_IO_RES_ABORT cqes
and exit by themselves.

Quoted 4 lines

Thanks,
Ming

author
#5 lore raw

Re: [PATCH V5 1/2] ublk_drv: add io_uring based userspace block driver

Quoted 105 lines · Ziyang Zhang
Quoted 79 lines · Ming Lei
Quoted 47 lines · Ziyang Zhang
Quoted 38 lines · Ming Lei

This is the driver part of userspace block driver(ublk driver), the other part is userspace daemon part(ublksrv)[1].

The two parts communicate by io_uring's IORING_OP_URING_CMD with one shared cmd buffer for storing io command, and the buffer is read only for ublksrv, each io command is indexed by io request tag directly, and is written by ublk driver.

For example, when one READ io request is submitted to ublk block driver, ublk driver stores the io command into cmd buffer first, then completes one IORING_OP_URING_CMD for notifying ublksrv, and the URING_CMD is issued to ublk driver beforehand by ublksrv for getting notification of any new io request, and each URING_CMD is associated with one io request by tag.

After ublksrv gets the io command, it translates and handles the ublk io request, such as, for the ublk-loop target, ublksrv translates the request into same request on another file or disk, like the kernel loop block driver. In ublksrv's implementation, the io is still handled by io_uring, and share same ring with IORING_OP_URING_CMD command. When the target io request is done, the same IORING_OP_URING_CMD is issued to ublk driver for both committing io request result and getting future notification of new io request.

Another thing done by ublk driver is to copy data between kernel io request and ublksrv's io buffer:

1) before ubsrv handles WRITE request, copy the request's data into ublksrv's userspace io buffer, so that ublksrv can handle the write request

2) after ubsrv handles READ request, copy ublksrv's userspace io buffer into this READ request, then ublk driver can complete the READ request

Zero copy may be switched if mm is ready to support it.

ublk driver doesn't handle any logic of the specific user space driver, so it is small/simple enough.

Quoted 6 lines

[1] ublksrv

https://github.com/ming1/ubdsrv

---

Quoted 5 lines

Hi, Ming

I find that a big change from v4 to v5 is the simplification of locks.

In v5 you remove ubq->abort_lock, and I want to ask why it is OK to remove it?

Actually V4 and previous version dealt with the issue too complicated.

Quoted 4 lines

If you have time, could you explain how ublk deals with potential race on:
1)queue_rq 2)ublk_abort_queue 3) ublk_ctrl_stop_dev 4) ublk_rq_task_work. (Lock in ublk really confuses me...)

One big change is the following code:

__ublk_rq_task_work():

	bool task_exiting = current != ubq->ubq_daemon ||
                (current->flags & PF_EXITING);
	...
	if (unlikely(task_exiting)) {
                blk_mq_end_request(req, BLK_STS_IOERR);
                mod_delayed_work(system_wq, &ub->monitor_work, 0);
                return;
    }

Abort is always started after PF_EXITING is set, but if PF_EXITING is set, __ublk_rq_task_work fails the request immediately, then io->flags won't be touched, then no race with abort. Also PF_EXITING is per-task flag, can only be set before calling __ublk_rq_task_work(), and setting it actually serialized with calling task work func.

Quoted 8 lines

In ublk_queue_rq(), we don't touch io->flags, so there isn't race with abort.

Wrt. ublk_ctrl_stop_dev(), it isn't related with abort directly, and if del_gendisk() waits for inflight IO, abort work will be started for making forward progress. After del_gendisk() returns, there can't be any inflight io, so it is safe to cancel other pending io command.

Thanks, Ming. I understand the aborting code now. And it looks good to me.

Previously I think maybe monitor_work and task_work may be scheduled at the same time while task is exiting and blk_mq_end_request() on the same tag could be called twice.

But I find there is a check on ublk_io's flag(UBLK_IO_FLAG_ACTIVE) in ublk_daemon_monitor_work() and ublk_io is aborted in task_work immediately(with UBLK_IO_FLAG_ACTIVE set, not cleared yet)

So there is no chance to call a send blk_mq_end_request() on the same tag.

Besides, for ublk_ios with UBLK_IO_FLAG_ACTIVE unset,
stop_work scheduled in monitor work will call ublk_cancel_queue() by sending cqes with UBLK_IO_RES_ABORT.

Quoted 5 lines

Put it together:

When daemon is PF_EXITING:

1) current ublk_io: aborted immediately in task_work

Precisely it is just that ublk io request is ended immediately, so io->flags won't be touched.

Quoted 2 lines

2) UBLK_IO_FLAG_ACTIVE set: aborted in ublk_daemon_monitor_work

This part is important for making forward progress, that is why it has to be done in a wq context.

Quoted 2 lines

3) UBLK_IO_FLAG_ACTIVE unset: send cqe with UBLK_IO_RES_ABORT

This is the 2nd stage of aborting after disk is deleted.

In short, it is one two-stage aborting, and the idea is actually straightforward.

Quoted 3 lines

Hope I'm correct this time. :)

Absolutely.

Quoted 80 lines
Quoted 61 lines
Quoted 61 lines

[...]

Quoted 18 lines
drivers/block/ublk_drv.c new file +18 -0
+
+/*
+ * __ublk_fail_req() may be called from abort context or ->ubq_daemon
+ * context during exiting, so lock is required.
+ *
+ * Also aborting may not be started yet, keep in mind that one failed
+ * request may be issued by block layer again.
+ */
+static void __ublk_fail_req(struct ublk_io *io, struct request *req)
+{
+	WARN_ON_ONCE(io->flags & UBLK_IO_FLAG_ACTIVE);
+
+	if (!(io->flags & UBLK_IO_FLAG_ABORTED)) {
+		io->flags |= UBLK_IO_FLAG_ABORTED;
+		blk_mq_end_request(req, BLK_STS_IOERR);
+	}
+}
+

[...]

Quoted 21 lines
drivers/block/ublk_drv.c new file +21 -0
+
+/*
+ * When ->ubq_daemon is exiting, either new request is ended immediately,
+ * or any queued io command is drained, so it is safe to abort queue
+ * lockless
+ */
+static void ublk_abort_queue(struct ublk_device *ub, struct ublk_queue *ubq)
+{
+	int i;
+
+	if (!ublk_get_device(ub))
+		return;
+
+	for (i = 0; i < ubq->q_depth; i++) {
+		struct ublk_io *io = &ubq->ios[i];
+
+		if (!(io->flags & UBLK_IO_FLAG_ACTIVE)) {
+			struct request *rq;
+
+			/*
+			 * Either we fail the request or ublk_rq_task_work_fn

drivers/block/ublk_drv.c line 712

Quoted 10 lines
drivers/block/ublk_drv.c new file +10 -0
+			 * will do it
+			 */
+			rq = blk_mq_tag_to_rq(ub->tag_set.tags[ubq->q_id], i);
+			if (rq)
+				__ublk_fail_req(io, rq);
+		}
+	}
+	ublk_put_device(ub);
+}
+

Another problem:

Quoted 4 lines
Quoted 1 line

1) comment of __ublk_fail_req(): "so lock is required"

Yeah, now __ublk_fail_req is only called in abort context, and no race with task work any more, so lock isn't needed.

Ok, I see.

Quoted 11 lines
Quoted 3 lines

2) comment of ublk_abort_queue(): "so it is safe to abort queue lockless"

This comment is updated in v5, and it is correct.

Quoted 2 lines

3) ublk_abort_queue() calls _ublk_fail_req() on all ubqs.

No, ublk_abort_queue() only aborts the passed ubq, so if one ubq daemon is aborted, other ubqs can still handle IO during deleting disk.

Quoted 7 lines

Ok, I see.

I think if one ubq daemon is killed(and blk-mq requests related to it are aborted), stop work should call del_gendisk() and other ubq daemons can still complete blk-mq requests but no new blk-mq requests will be issued.
After that, these unkilled ubq daemons will get UBLK_IO_RES_ABORT cqes
and exit by themselves.

Yeah, you are right.

Thanks,
Ming

#6 lore raw

Re: [PATCH V5 1/2] ublk_drv: add io_uring based userspace block driver

Quoted 7 lines · Ming Lei
Quoted 6 lines · Ziyang Zhang
Quoted 5 lines · Ming Lei
Quoted 4 lines · Ziyang Zhang
Quoted 3 lines · Ming Lei

This is the driver part of userspace block driver(ublk driver), the other part is userspace daemon part(ublksrv)[1].

...

Quoted 7 lines
Quoted 6 lines

Put it together:

When daemon is PF_EXITING:

1) current ublk_io: aborted immediately in task_work

Quoted 11 lines

Precisely it is just that ublk io request is ended immediately, so io->flags won't be touched.

Quoted 2 lines

2) UBLK_IO_FLAG_ACTIVE set: aborted in ublk_daemon_monitor_work

This part is important for making forward progress, that is why it has to be done in a wq context.

Quoted 2 lines

3) UBLK_IO_FLAG_ACTIVE unset: send cqe with UBLK_IO_RES_ABORT

Oh... sorry for one mistake. case 2) and 3) should be swapped:

ublk_daemon_monitor_work(): abort blk-mq IOs if UBLK_IO_FLAG_ACTIVE is unset

ublk_cancel_queue(): send cqes with UBLK_IO_RES_ABORT if UBLK_IO_FLAG_ACTIVE is set

author
#7 lore raw

PATCH v5 2/2 ublk_drv: support to complete io command via task_work_add

In reply to Ming Lei

Use task_work_add if it is available, since task_work_add can bring up better performance, especially batching signaling ->ubq_daemon can be done.

It is observed that task_work_add() can boost iops by +4% on random 4k io test. Also except for completing io command, all other code paths are same with completing io command via io_uring_cmd_complete_in_task.

Meantime add one flag of UBLK_F_URING_CMD_COMP_IN_TASK for comparing the mode easily.

---
 drivers/block/ublk_drv.c      | 75 +++++++++++++++++++++++++++++++----
 include/uapi/linux/ublk_cmd.h |  6 +++
 2 files changed, 73 insertions(+), 8 deletions(-)
drivers/block/ublk_drv.c +67 -8
index 922a84c86fc6..35fa06ee70ff 100644
@@ -41,10 +41,15 @@
 #include <linux/delay.h>
 #include <linux/mm.h>
 #include <asm/page.h>
+#include <linux/task_work.h>
 #include <uapi/linux/ublk_cmd.h>
 
 #define UBLK_MINORS		(1U << MINORBITS)
 
+struct ublk_rq_data {
+	struct callback_head work;
+};
+
 struct ublk_uring_cmd_pdu {
 	struct request *req;
 };
@@ -91,6 +96,7 @@ struct ublk_queue {
 	int q_id;
 	int q_depth;
 
+	unsigned long flags;
 	struct task_struct	*ubq_daemon;
 	char *io_cmd_buf;
 
@@ -149,6 +155,14 @@ static DEFINE_MUTEX(ublk_ctl_mutex);
 
 static struct miscdevice ublk_misc;
 
+static inline bool ublk_can_use_task_work(const struct ublk_queue *ubq)
+{
+	if (IS_BUILTIN(CONFIG_BLK_DEV_UBLK) &&
+			!(ubq->flags & UBLK_F_URING_CMD_COMP_IN_TASK))
+		return true;
+	return false;
+}
+
 static struct ublk_device *ublk_get_device(struct ublk_device *ub)
 {
 	if (kobject_get_unless_zero(&ub->cdev_dev.kobj))
@@ -500,12 +514,10 @@ static void __ublk_fail_req(struct ublk_io *io, struct request *req)
 
 #define UBLK_REQUEUE_DELAY_MS	3
 
-static void ublk_rq_task_work_cb(struct io_uring_cmd *cmd)
+static inline void __ublk_rq_task_work(struct request *req)
 {
-	struct ublk_uring_cmd_pdu *pdu = ublk_get_uring_cmd_pdu(cmd);
-	struct ublk_device *ub = cmd->file->private_data;
-	struct request *req = pdu->req;
 	struct ublk_queue *ubq = req->mq_hctx->driver_data;
+	struct ublk_device *ub = ubq->dev;
 	int tag = req->tag;
 	struct ublk_io *io = &ubq->ios[tag];
 	bool task_exiting = current != ubq->ubq_daemon ||
@@ -557,13 +569,27 @@ static void ublk_rq_task_work_cb(struct io_uring_cmd *cmd)
 	io_uring_cmd_done(io->cmd, UBLK_IO_RES_OK, 0);
 }
 
+static void ublk_rq_task_work_cb(struct io_uring_cmd *cmd)
+{
+	struct ublk_uring_cmd_pdu *pdu = ublk_get_uring_cmd_pdu(cmd);
+
+	__ublk_rq_task_work(pdu->req);
+}
+
+static void ublk_rq_task_work_fn(struct callback_head *work)
+{
+	struct ublk_rq_data *data = container_of(work,
+			struct ublk_rq_data, work);
+	struct request *req = blk_mq_rq_from_pdu(data);
+
+	__ublk_rq_task_work(req);
+}
+
 static blk_status_t ublk_queue_rq(struct blk_mq_hw_ctx *hctx,
 		const struct blk_mq_queue_data *bd)
 {
 	struct ublk_queue *ubq = hctx->driver_data;
 	struct request *rq = bd->rq;
-	struct io_uring_cmd *cmd = ubq->ios[rq->tag].cmd;
-	struct ublk_uring_cmd_pdu *pdu = ublk_get_uring_cmd_pdu(cmd);
 	blk_status_t res;
 
 	/* fill iod to slot in io cmd buffer */
@@ -574,16 +600,36 @@ static blk_status_t ublk_queue_rq(struct blk_mq_hw_ctx *hctx,
 	blk_mq_start_request(bd->rq);
 
 	if (unlikely(ubq_daemon_is_dying(ubq))) {
+ fail:
 		mod_delayed_work(system_wq, &ubq->dev->monitor_work, 0);
 		return BLK_STS_IOERR;
 	}
 
-	pdu->req = rq;
-	io_uring_cmd_complete_in_task(cmd, ublk_rq_task_work_cb);
+	if (ublk_can_use_task_work(ubq)) {
+		struct ublk_rq_data *data = blk_mq_rq_to_pdu(rq);
+		enum task_work_notify_mode notify_mode = bd->last ?
+			TWA_SIGNAL_NO_IPI : TWA_NONE;
+
+		if (task_work_add(ubq->ubq_daemon, &data->work, notify_mode))
+			goto fail;
+	} else {
+		struct io_uring_cmd *cmd = ubq->ios[rq->tag].cmd;
+		struct ublk_uring_cmd_pdu *pdu = ublk_get_uring_cmd_pdu(cmd);
+
+		pdu->req = rq;
+		io_uring_cmd_complete_in_task(cmd, ublk_rq_task_work_cb);
+	}
 
 	return BLK_STS_OK;
 }
 
+static void ublk_commit_rqs(struct blk_mq_hw_ctx *hctx)
+{
+	struct ublk_queue *ubq = hctx->driver_data;
+
+	if (ublk_can_use_task_work(ubq))
+		__set_notify_signal(ubq->ubq_daemon);
+}
 
 static int ublk_init_hctx(struct blk_mq_hw_ctx *hctx, void *driver_data,
 		unsigned int hctx_idx)
@@ -595,9 +641,20 @@ static int ublk_init_hctx(struct blk_mq_hw_ctx *hctx, void *driver_data,
 	return 0;
 }
 
+static int ublk_init_rq(struct blk_mq_tag_set *set, struct request *req,
+		unsigned int hctx_idx, unsigned int numa_node)
+{
+	struct ublk_rq_data *data = blk_mq_rq_to_pdu(req);
+
+	init_task_work(&data->work, ublk_rq_task_work_fn);
+	return 0;
+}
+
 static const struct blk_mq_ops ublk_mq_ops = {
 	.queue_rq       = ublk_queue_rq,
+	.commit_rqs     = ublk_commit_rqs,
 	.init_hctx	= ublk_init_hctx,
+	.init_request   = ublk_init_rq,
 };
 
 static int ublk_ch_open(struct inode *inode, struct file *filp)
@@ -912,6 +969,7 @@ static int ublk_init_queue(struct ublk_device *ub, int q_id)
 	void *ptr;
 	int size;
 
+	ubq->flags = ub->dev_info.flags[0];
 	ubq->q_id = q_id;
 	ubq->q_depth = ub->dev_info.queue_depth;
 	size = ublk_queue_cmd_buf_size(ub, q_id);
@@ -1099,6 +1157,7 @@ static int ublk_add_dev(struct ublk_device *ub)
 	ub->tag_set.nr_hw_queues = ub->dev_info.nr_hw_queues;
 	ub->tag_set.queue_depth = ub->dev_info.queue_depth;
 	ub->tag_set.numa_node = NUMA_NO_NODE;
+	ub->tag_set.cmd_size = sizeof(struct ublk_rq_data);
 	ub->tag_set.flags = BLK_MQ_F_SHOULD_MERGE;
 	ub->tag_set.driver_data = ub;
 
include/uapi/linux/ublk_cmd.h +6 -0
index 4f0c16ec875e..a3f5e7c21807 100644
@@ -48,6 +48,12 @@
  */
 #define UBLK_F_SUPPORT_ZERO_COPY	(1UL << 0)
 
+/*
+ * Force to complete io cmd via io_uring_cmd_complete_in_task so that
+ * performance comparison is done easily with using task_work_add
+ */
+#define UBLK_F_URING_CMD_COMP_IN_TASK	(1UL << 1)
+
 /* device state */
 #define UBLK_S_DEV_DEAD	0
 #define UBLK_S_DEV_LIVE	1
Signature · 1 line
2.31.1
maintainer
#8 lore raw
In reply to Ming Lei
Quoted 6 lines · Ming Lei

Hello Guys,

ublk driver is one kernel driver for implementing generic userspace block device/driver, which delivers io request from ublk block device(/dev/ublkbN) into ublk server[1] which is the userspace part of ublk for communicating with ublk driver and handling specific io logic by its target module.

Ming, is this ready to get merged in an experimental state?

Signature · 1 line
Jens Axboe
author
#9 lore raw
Quoted 9 lines · Jens Axboe
Quoted 6 lines · Ming Lei

Hello Guys,

ublk driver is one kernel driver for implementing generic userspace block device/driver, which delivers io request from ublk block device(/dev/ublkbN) into ublk server[1] which is the userspace part of ublk for communicating with ublk driver and handling specific io logic by its target module.

Ming, is this ready to get merged in an experimental state?

Hi Jens,

Yeah, I think so.

IO path can survive in xfstests(-g auto), and control path works well in ublksrv builtin hotplug & 'kill -9' daemon test.

The UAPI data size should be good, but definition may change per future requirement change, so I think it is ready to go as experimental.

If you are fine, please add the following delta change into patch 1, or let me know if resend is needed.

drivers/block/Kconfig +5 -2
index 2ba77fd960c2..e19fcab016ba 100644
@@ -409,10 +409,13 @@ config BLK_DEV_RBD
 	  If unsure, say N.
 
 config BLK_DEV_UBLK
-	tristate "Userspace block driver"
+	tristate "Userspace block driver (Experimental)"
 	select IO_URING
 	help
-          io uring based userspace block driver.
+	  io_uring based userspace block driver. Together with ublk server, ublk
+	  has been working well, but interface with userspace or command data
+	  definition isn't finalized yet, and might change according to future
+	  requirement, so mark is as experimental now.
 
 source "drivers/block/rnbd/Kconfig"
 

Thanks,
Ming

maintainer
#10 lore raw
Quoted 13 lines · Ming Lei
Quoted 9 lines · Jens Axboe
Quoted 6 lines · Ming Lei

Hello Guys,

ublk driver is one kernel driver for implementing generic userspace block device/driver, which delivers io request from ublk block device(/dev/ublkbN) into ublk server[1] which is the userspace part of ublk for communicating with ublk driver and handling specific io logic by its target module.

Ming, is this ready to get merged in an experimental state?

Hi Jens,

Quoted 8 lines

Yeah, I think so.

IO path can survive in xfstests(-g auto), and control path works well in ublksrv builtin hotplug & 'kill -9' daemon test.

The UAPI data size should be good, but definition may change per future requirement change, so I think it is ready to go as experimental.

OK let's give it a go then. I tried it out and it seems to work for me, even if the shutdown-while-busy is something I'd to look into a bit more.

BTW, did notice a typo on the github page:

2) dependency
- liburing with IORING_SETUP_SQE128 support

- linux kernel 5.9(IORING_SETUP_SQE128 support)

that should be 5.19, typo.

Signature · 1 line
Jens Axboe
maintainer
#11 lore raw
Quoted 27 lines · Jens Axboe
Quoted 13 lines · Ming Lei
Quoted 9 lines · Jens Axboe
Quoted 6 lines · Ming Lei

Hello Guys,

ublk driver is one kernel driver for implementing generic userspace block device/driver, which delivers io request from ublk block device(/dev/ublkbN) into ublk server[1] which is the userspace part of ublk for communicating with ublk driver and handling specific io logic by its target module.

Ming, is this ready to get merged in an experimental state?

Hi Jens,

Quoted 8 lines

Yeah, I think so.

IO path can survive in xfstests(-g auto), and control path works well in ublksrv builtin hotplug & 'kill -9' daemon test.

The UAPI data size should be good, but definition may change per future requirement change, so I think it is ready to go as experimental.

OK let's give it a go then. I tried it out and it seems to work for me, even if the shutdown-while-busy is something I'd to look into a bit more.

Quoted 8 lines

BTW, did notice a typo on the github page:

2) dependency
- liburing with IORING_SETUP_SQE128 support

- linux kernel 5.9(IORING_SETUP_SQE128 support)

that should be 5.19, typo.

I tried this:

axboe@m1pro-kvm ~/g/ubdsrv (master)> sudo ./ublk add -t loop /dev/nvme0n1 axboe@m1pro-kvm ~/g/ubdsrv (master) [255]>

and got this dump:

[   34.041647] WARNING: CPU: 3 PID: 60 at block/blk-mq.c:3880 blk_mq_release+0xa4/0xf0
[   34.043858] Modules linked in:
[   34.044911] CPU: 3 PID: 60 Comm: kworker/3:1 Not tainted 5.19.0-rc6-00320-g5c37a506da31 #1608
[   34.047689] Hardware name: linux,dummy-virt (DT)
[   34.049207] Workqueue: events blkg_free_workfn
[   34.050731] pstate: 60400005 (nZCv daif +PAN -UAO -TCO -DIT -SSBS BTYPE=--)
[   34.053026] pc : blk_mq_release+0xa4/0xf0
[   34.054360] lr : blk_mq_release+0x44/0xf0
[   34.055694] sp : ffff80000b16bcb0
[   34.056804] x29: ffff80000b16bcb0 x28: 0000000000000000 x27: 0000000000000000
[   34.059135] x26: 0000000000000000 x25: ffff00001fe9bb05 x24: 0000000000000000
[   34.061454] x23: ffff000005062eb8 x22: ffff000004608998 x21: 0000000000000000
[   34.063775] x20: ffff000004608a50 x19: ffff000004608950 x18: ffff80000b7b3c88
[   34.066085] x17: 0000000000000000 x16: 0000000000000000 x15: 0000000000000000
[   34.068410] x14: 0000000000000002 x13: 0000000000013638 x12: 0000000000000000
[   34.070715] x11: ffff80000945b7e8 x10: 0000000000006f2e x9 : 00000000ffffffff
[   34.073037] x8 : ffff800008fb5000 x7 : ffff80000860cf28 x6 : 0000000000000000
[   34.075334] x5 : 0000000000000000 x4 : 0000000000000028 x3 : ffff80000b16bc14
[   34.077650] x2 : ffff0000086d66a8 x1 : ffff0000086d66a8 x0 : ffff0000086d6400
[   34.079966] Call trace:
[   34.080789]  blk_mq_release+0xa4/0xf0
[   34.081811]  blk_release_queue+0x58/0xa0
[   34.082758]  kobject_put+0x84/0xe0
[   34.083590]  blk_put_queue+0x10/0x18
[   34.084468]  blkg_free_workfn+0x58/0x84
[   34.085511]  process_one_work+0x2ac/0x438
[   34.086449]  worker_thread+0x1cc/0x264
[   34.087322]  kthread+0xd0/0xe0
[   34.088053]  ret_from_fork+0x10/0x20
Signature · 1 line
Jens Axboe
author
#12 lore raw
Quoted 33 lines · Jens Axboe
Quoted 32 lines · Jens Axboe
Quoted 13 lines · Ming Lei
Quoted 9 lines · Jens Axboe
Quoted 6 lines · Ming Lei

Hello Guys,

ublk driver is one kernel driver for implementing generic userspace block device/driver, which delivers io request from ublk block device(/dev/ublkbN) into ublk server[1] which is the userspace part of ublk for communicating with ublk driver and handling specific io logic by its target module.

Ming, is this ready to get merged in an experimental state?

Hi Jens,

Quoted 8 lines

Yeah, I think so.

IO path can survive in xfstests(-g auto), and control path works well in ublksrv builtin hotplug & 'kill -9' daemon test.

The UAPI data size should be good, but definition may change per future requirement change, so I think it is ready to go as experimental.

OK let's give it a go then. I tried it out and it seems to work for me, even if the shutdown-while-busy is something I'd to look into a bit more.

BTW, did notice a typo on the github page:

2) dependency
- liburing with IORING_SETUP_SQE128 support

Quoted 8 lines
Quoted 3 lines

- linux kernel 5.9(IORING_SETUP_SQE128 support)

that should be 5.19, typo.

I tried this:

axboe@m1pro-kvm ~/g/ubdsrv (master)> sudo ./ublk add -t loop /dev/nvme0n1 axboe@m1pro-kvm ~/g/ubdsrv (master) [255]>

That looks one issue in ubdsrv, and '-f /dev/nvme0n1' is needed.

Quoted 24 lines

and got this dump:

[   34.041647] WARNING: CPU: 3 PID: 60 at block/blk-mq.c:3880 blk_mq_release+0xa4/0xf0
[   34.043858] Modules linked in:
[   34.044911] CPU: 3 PID: 60 Comm: kworker/3:1 Not tainted 5.19.0-rc6-00320-g5c37a506da31 #1608
[   34.047689] Hardware name: linux,dummy-virt (DT)
[   34.049207] Workqueue: events blkg_free_workfn
[   34.050731] pstate: 60400005 (nZCv daif +PAN -UAO -TCO -DIT -SSBS BTYPE=--)
[   34.053026] pc : blk_mq_release+0xa4/0xf0
[   34.054360] lr : blk_mq_release+0x44/0xf0
[   34.055694] sp : ffff80000b16bcb0
[   34.056804] x29: ffff80000b16bcb0 x28: 0000000000000000 x27: 0000000000000000
[   34.059135] x26: 0000000000000000 x25: ffff00001fe9bb05 x24: 0000000000000000
[   34.061454] x23: ffff000005062eb8 x22: ffff000004608998 x21: 0000000000000000
[   34.063775] x20: ffff000004608a50 x19: ffff000004608950 x18: ffff80000b7b3c88
[   34.066085] x17: 0000000000000000 x16: 0000000000000000 x15: 0000000000000000
[   34.068410] x14: 0000000000000002 x13: 0000000000013638 x12: 0000000000000000
[   34.070715] x11: ffff80000945b7e8 x10: 0000000000006f2e x9 : 00000000ffffffff
[   34.073037] x8 : ffff800008fb5000 x7 : ffff80000860cf28 x6 : 0000000000000000
[   34.075334] x5 : 0000000000000000 x4 : 0000000000000028 x3 : ffff80000b16bc14
[   34.077650] x2 : ffff0000086d66a8 x1 : ffff0000086d66a8 x0 : ffff0000086d6400
[   34.079966] Call trace:
[   34.080789]  blk_mq_release+0xa4/0xf0
Quoted 8 lines
[   34.081811]  blk_release_queue+0x58/0xa0
[   34.082758]  kobject_put+0x84/0xe0
[   34.083590]  blk_put_queue+0x10/0x18
[   34.084468]  blkg_free_workfn+0x58/0x84
[   34.085511]  process_one_work+0x2ac/0x438
[   34.086449]  worker_thread+0x1cc/0x264
[   34.087322]  kthread+0xd0/0xe0
[   34.088053]  ret_from_fork+0x10/0x20

I guess there should be some validation missed in driver side too, will look into it.

Thanks,
Ming

#13 lore raw
In reply to Ming Lei

Hi Ming,

Thanks for your patch!

Quoted 5 lines · Ming Lei
drivers/block/Kconfig +0 -0
@@ -409,10 +409,13 @@ config BLK_DEV_RBD
          If unsure, say N.
 

drivers/block/Kconfig config BLK_DEV_RBD · line 418

Quoted 10 lines
drivers/block/Kconfig +5 -2
 config BLK_DEV_UBLK
-       tristate "Userspace block driver"
+       tristate "Userspace block driver (Experimental)"
        select IO_URING
        help
-          io uring based userspace block driver.
+         io_uring based userspace block driver. Together with ublk server, ublk
+         has been working well, but interface with userspace or command data
+         definition isn't finalized yet, and might change according to future
+         requirement, so mark is as experimental now.

it

drivers/block/Kconfig config BLK_DEV_RBD · line 420

Quoted 2 lines
drivers/block/Kconfig +0 -0
 source "drivers/block/rnbd/Kconfig"

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

maintainer Applied
#14 lore raw
In reply to Ming Lei
Quoted 11 lines · Ming Lei

ublk driver is one kernel driver for implementing generic userspace block device/driver, which delivers io request from ublk block device(/dev/ublkbN) into ublk server[1] which is the userspace part of ublk for communicating with ublk driver and handling specific io logic by its target module.

Another thing ublk driver handles is to copy data between user space buffer and request/bio's pages, or take zero copy if mm is ready for support it in future. ublk driver doesn't handle any IO logic of the specific driver, so it is small/simple, and all io logics are done by the target code in ublkserver.

[...]

Applied, thanks!

[1/2] ublk_drv: add io_uring based userspace block driver

      commit: 3fee8d7599e17fe17ef6c1b96e2237babe8b68ea
[2/2] ublk_drv: support to complete io command via task_work_add
      commit: 664ff52d6f338a9afcabee535e8dedf04659f0d6

Best regards,

Signature · 1 line
Jens Axboe
Reviewed-by on the v5 series
#15 lore raw
In reply to Ming Lei
Quoted 4 lines · Ming Lei

ublk driver is one kernel driver for implementing generic userspace block device/driver, which delivers io request from ublk block device(/dev/ublkbN) into ublk server[1] which is the userspace part of ublk for communicating with ublk driver and handling specific io logic by its target module.

Hey Ming,

I didn't get a chance to look deep into v5 as I was on a last minute leave in the past few days. Either way, I went through them now and the patches look good to me. I'm quite happy they are merged, thank you very much for this work.

Just for ML archive purposes, the entire series is

:)

Signature · 1 line
Gabriel Krisman Bertazi