Skip to contentSkip to conversation

ublk: two fixes and support UBLK_F_AUTO_ZERO_COPY

PATCH In review Ming Lei 3 participants 19 messages Apr 26, 2025 – Apr 27, 2025

Subsystems (3)
lore
Share & tools
lore

Ming Lei

author
#0 lore raw

cover ublk: two fixes and support UBLK_F_AUTO_ZERO_COPY

Hi Jens,

The 1st patch fixes UBLK_F_NEED_GET_DATA implementation in selftest ublk utility, meantime add test to cover this feature.

The 2nd patch enhances check for register/unregister IO buffer uring command.

The 3rd patch adds UBLK_F_AUTO_ZERO_COPY by registering zc buffer before delivering io command to ublk server and un-registering zc buffer when completing io command. This way not only improves zero copy perf, but also makes it much easier to use:

- ublk/null: iops is improved by 50%(--auto_zc vs. -z, -q 2)

- ublk server needn't to register/unregister io buffer uring_cmd any more, which is done automatically by ublk driver

- ublk server zc buffer consumer OPs need to depend on register/unregister io buffer uring_cmd any more. Without this feature, buffer consumer OP has to respect the dependency by IOSQE_IO_LINK.

The last patch adds function & stress tests for UBLK_F_AUTO_ZERO_COPY.

Thanks,
Ming

Ming Lei (4):
selftests: ublk: fix UBLK_F_NEED_GET_DATA
ublk: enhance check for register/unregister io buffer command
ublk: add feature UBLK_F_AUTO_ZERO_COPY
selftests: ublk: support UBLK_F_AUTO_ZERO_COPY

 drivers/block/ublk_drv.c                      | 110 +++++++++++++++---
 include/uapi/linux/ublk_cmd.h                 |  20 ++++
 tools/testing/selftests/ublk/Makefile         |   3 +
 tools/testing/selftests/ublk/file_backed.c    |   9 +-
 tools/testing/selftests/ublk/kublk.c          |  24 +++-
 tools/testing/selftests/ublk/kublk.h          |   7 ++
 tools/testing/selftests/ublk/null.c           |  43 +++++--
 tools/testing/selftests/ublk/stripe.c         |  14 +--
 .../testing/selftests/ublk/test_generic_07.sh |  25 ++++
 .../testing/selftests/ublk/test_generic_08.sh |  28 +++++
 .../testing/selftests/ublk/test_stress_03.sh  |   6 +
 .../testing/selftests/ublk/test_stress_04.sh  |   6 +
 .../testing/selftests/ublk/test_stress_05.sh  |   8 ++
 13 files changed, 257 insertions(+), 46 deletions(-)
 create mode 100755 tools/testing/selftests/ublk/test_generic_07.sh
 create mode 100755 tools/testing/selftests/ublk/test_generic_08.sh
Signature · 1 line
2.47.0
author
#1 lore raw

PATCH 1/4 selftests: ublk: fix UBLK_F_NEED_GET_DATA

Commit 57e13a2e8cd2 ("selftests: ublk: support user recovery") starts to support UBLK_F_NEED_GET_DATA for covering recovery feature, however the ublk utility implementation isn't done correctly.

Fix it by supporting UBLK_F_NEED_GET_DATA correctly.

Also add test generic_07 for covering UBLK_F_NEED_GET_DATA.

Fixes 57e13a2e8cd2 ("selftests: ublk: support user recovery")
---
 tools/testing/selftests/ublk/Makefile         |  1 +
 tools/testing/selftests/ublk/kublk.c          | 11 +++++---
 tools/testing/selftests/ublk/kublk.h          |  1 +
 .../testing/selftests/ublk/test_generic_07.sh | 25 +++++++++++++++++++
 4 files changed, 35 insertions(+), 3 deletions(-)
 create mode 100755 tools/testing/selftests/ublk/test_generic_07.sh
tools/testing/selftests/ublk/Makefile +1 -0
index ec4624a283bc..f34ac0bac696 100644
@@ -9,6 +9,7 @@ TEST_PROGS += test_generic_03.sh
 TEST_PROGS += test_generic_04.sh
 TEST_PROGS += test_generic_05.sh
 TEST_PROGS += test_generic_06.sh
+TEST_PROGS += test_generic_07.sh
 
 TEST_PROGS += test_null_01.sh
 TEST_PROGS += test_null_02.sh
tools/testing/selftests/ublk/kublk.c +8 -3
index e57a1486bb48..701b47f98902 100644
@@ -538,10 +538,12 @@ int ublk_queue_io_cmd(struct ublk_queue *q, struct ublk_io *io, unsigned tag)
 
 	/* we issue because we need either fetching or committing */
 	if (!(io->flags &
-		(UBLKSRV_NEED_FETCH_RQ | UBLKSRV_NEED_COMMIT_RQ_COMP)))
+		(UBLKSRV_NEED_FETCH_RQ | UBLKSRV_NEED_COMMIT_RQ_COMP | UBLKSRV_NEED_GET_DATA)))
 		return 0;
 
-	if (io->flags & UBLKSRV_NEED_COMMIT_RQ_COMP)
+	if (io->flags & UBLKSRV_NEED_GET_DATA)
+		cmd_op = UBLK_U_IO_NEED_GET_DATA;
+	else if (io->flags & UBLKSRV_NEED_COMMIT_RQ_COMP)
 		cmd_op = UBLK_U_IO_COMMIT_AND_FETCH_REQ;
 	else if (io->flags & UBLKSRV_NEED_FETCH_RQ)
 		cmd_op = UBLK_U_IO_FETCH_REQ;
@@ -658,6 +660,9 @@ static void ublk_handle_cqe(struct io_uring *r,
 		assert(tag < q->q_depth);
 		if (q->tgt_ops->queue_io)
 			q->tgt_ops->queue_io(q, tag);
+	} else if (cqe->res == UBLK_IO_RES_NEED_GET_DATA) {
+		io->flags |= UBLKSRV_NEED_GET_DATA | UBLKSRV_IO_FREE;
+		ublk_queue_io_cmd(q, io, tag);
 	} else {
 		/*
 		 * COMMIT_REQ will be completed immediately since no fetching
@@ -1313,7 +1318,7 @@ int main(int argc, char *argv[])
 
 	opterr = 0;
 	optind = 2;
-	while ((opt = getopt_long(argc, argv, "t:n:d:q:r:e:i:az",
+	while ((opt = getopt_long(argc, argv, "t:n:d:q:r:e:i:g:az",
 				  longopts, &option_idx)) != -1) {
 		switch (opt) {
 		case 'a':
tools/testing/selftests/ublk/kublk.h +1 -0
index 918db5cd633f..44ee1e4ac55b 100644
@@ -115,6 +115,7 @@ struct ublk_io {
 #define UBLKSRV_NEED_FETCH_RQ		(1UL << 0)
 #define UBLKSRV_NEED_COMMIT_RQ_COMP	(1UL << 1)
 #define UBLKSRV_IO_FREE			(1UL << 2)
+#define UBLKSRV_NEED_GET_DATA           (1UL << 3)
 	unsigned short flags;
 	unsigned short refs;		/* used by target code only */
 
tools/testing/selftests/ublk/test_generic_07.sh new file +25 -0
new file mode 100755
index 000000000000..5d82b5955006
@@ -0,0 +1,25 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+
+. "$(cd "$(dirname "$0")" && pwd)"/test_common.sh
+
+TID="generic_07"
+ERR_CODE=0
+
+_prep_test "generic" "test UBLK_F_NEED_GET_DATA"
+
+_create_backfile 0 256M
+dev_id=$(_add_ublk_dev -t loop -q 2 -g 1 "${UBLK_BACKFILES[0]}")
+_check_add_dev $TID $?
+
+# run fio over the ublk disk
+if ! _run_fio_verify_io --filename=/dev/ublkb"${dev_id}" --size=256M; then
+	_cleanup_test "generic"
+	_show_result $TID 255
+fi
+
+_mkfs_mount_test /dev/ublkb"${dev_id}"
+ERR_CODE=$?
+
+_cleanup_test "generic"
+_show_result $TID $ERR_CODE
Signature · 1 line
2.47.0
Reviewed-by on v1 1/4
#2 lore raw

Re: [PATCH 1/4] selftests: ublk: fix UBLK_F_NEED_GET_DATA

Quoted 8 lines · Ming Lei

Commit 57e13a2e8cd2 ("selftests: ublk: support user recovery") starts to support UBLK_F_NEED_GET_DATA for covering recovery feature, however the ublk utility implementation isn't done correctly.

Fix it by supporting UBLK_F_NEED_GET_DATA correctly.

Also add test generic_07 for covering UBLK_F_NEED_GET_DATA.

Looks good to me, just a few minor comments.

Quoted 28 lines
Fixes 57e13a2e8cd2 ("selftests: ublk: support user recovery")
---
 tools/testing/selftests/ublk/Makefile         |  1 +
 tools/testing/selftests/ublk/kublk.c          | 11 +++++---
 tools/testing/selftests/ublk/kublk.h          |  1 +
 .../testing/selftests/ublk/test_generic_07.sh | 25 +++++++++++++++++++
 4 files changed, 35 insertions(+), 3 deletions(-)
 create mode 100755 tools/testing/selftests/ublk/test_generic_07.sh
tools/testing/selftests/ublk/Makefile +1 -0
index ec4624a283bc..f34ac0bac696 100644
@@ -9,6 +9,7 @@ TEST_PROGS += test_generic_03.sh
 TEST_PROGS += test_generic_04.sh
 TEST_PROGS += test_generic_05.sh
 TEST_PROGS += test_generic_06.sh
+TEST_PROGS += test_generic_07.sh
 
 TEST_PROGS += test_null_01.sh
 TEST_PROGS += test_null_02.sh
tools/testing/selftests/ublk/kublk.c +0 -0
index e57a1486bb48..701b47f98902 100644

tools/testing/selftests/ublk/kublk.c int ublk_queue_io_cmd(struct ublk_queue *q, struct ublk_io *io, unsigned tag) · line 541

Quoted 5 lines
tools/testing/selftests/ublk/kublk.c +1 -1
@@ -538,10 +538,12 @@ int ublk_queue_io_cmd(struct ublk_queue *q, struct ublk_io *io, unsigned tag)
 
        /* we issue because we need either fetching or committing */
        if (!(io->flags &
-               (UBLKSRV_NEED_FETCH_RQ | UBLKSRV_NEED_COMMIT_RQ_COMP)))
+               (UBLKSRV_NEED_FETCH_RQ | UBLKSRV_NEED_COMMIT_RQ_COMP | UBLKSRV_NEED_GET_DATA)))

Comment could use an update

Quoted 20 lines
tools/testing/selftests/ublk/kublk.c +6 -1
                return 0;
 
-       if (io->flags & UBLKSRV_NEED_COMMIT_RQ_COMP)
+       if (io->flags & UBLKSRV_NEED_GET_DATA)
+               cmd_op = UBLK_U_IO_NEED_GET_DATA;
+       else if (io->flags & UBLKSRV_NEED_COMMIT_RQ_COMP)
                cmd_op = UBLK_U_IO_COMMIT_AND_FETCH_REQ;
        else if (io->flags & UBLKSRV_NEED_FETCH_RQ)
                cmd_op = UBLK_U_IO_FETCH_REQ;
@@ -658,6 +660,9 @@ static void ublk_handle_cqe(struct io_uring *r,
                assert(tag < q->q_depth);
                if (q->tgt_ops->queue_io)
                        q->tgt_ops->queue_io(q, tag);
+       } else if (cqe->res == UBLK_IO_RES_NEED_GET_DATA) {
+               io->flags |= UBLKSRV_NEED_GET_DATA | UBLKSRV_IO_FREE;
+               ublk_queue_io_cmd(q, io, tag);
        } else {
                /*
                 * COMMIT_REQ will be completed immediately since no fetching

tools/testing/selftests/ublk/kublk.c int main(int argc, char *argv[]) · line 1322

Quoted 6 lines
tools/testing/selftests/ublk/kublk.c +1 -1
@@ -1313,7 +1318,7 @@ int main(int argc, char *argv[])
 
        opterr = 0;
        optind = 2;
-       while ((opt = getopt_long(argc, argv, "t:n:d:q:r:e:i:az",
+       while ((opt = getopt_long(argc, argv, "t:n:d:q:r:e:i:g:az",
                                  longopts, &option_idx)) != -1) {

It's a little strange for -g to take an argument since it's basically a boolean flag. But it looks like several other flags behave the same way.

Quoted 28 lines
tools/testing/selftests/ublk/kublk.c +0 -0
                switch (opt) {
                case 'a':
tools/testing/selftests/ublk/kublk.h +1 -0
index 918db5cd633f..44ee1e4ac55b 100644
@@ -115,6 +115,7 @@ struct ublk_io {
 #define UBLKSRV_NEED_FETCH_RQ          (1UL << 0)
 #define UBLKSRV_NEED_COMMIT_RQ_COMP    (1UL << 1)
 #define UBLKSRV_IO_FREE                        (1UL << 2)
+#define UBLKSRV_NEED_GET_DATA           (1UL << 3)
        unsigned short flags;
        unsigned short refs;            /* used by target code only */
 
tools/testing/selftests/ublk/test_generic_07.sh new file +8 -0
new file mode 100755
index 000000000000..5d82b5955006
@@ -0,0 +1,25 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+
+. "$(cd "$(dirname "$0")" && pwd)"/test_common.sh
+
+TID="generic_07"
+ERR_CODE=0
+

tools/testing/selftests/ublk/test_generic_07.sh line 18

Quoted 10 lines
tools/testing/selftests/ublk/test_generic_07.sh new file +10 -0
+_prep_test "generic" "test UBLK_F_NEED_GET_DATA"
+
+_create_backfile 0 256M
+dev_id=$(_add_ublk_dev -t loop -q 2 -g 1 "${UBLK_BACKFILES[0]}")
+_check_add_dev $TID $?
+
+# run fio over the ublk disk
+if ! _run_fio_verify_io --filename=/dev/ublkb"${dev_id}" --size=256M; then
+       _cleanup_test "generic"
+       _show_result $TID 255

Propagate the return code from fio?

tools/testing/selftests/ublk/test_generic_07.sh line 19

Quoted 1 line
tools/testing/selftests/ublk/test_generic_07.sh new file +1 -0
+fi

All the other tests that use _run_fio_verify_io appear to check _have_program fio first. Is that necessary here too?

Quoted 9 lines
tools/testing/selftests/ublk/test_generic_07.sh new file +6 -1
+
+_mkfs_mount_test /dev/ublkb"${dev_id}"
+ERR_CODE=$?
+
+_cleanup_test "generic"
+_show_result $TID $ERR_CODE
--

2.47.0

author
#3 lore raw

Re: [PATCH 1/4] selftests: ublk: fix UBLK_F_NEED_GET_DATA

Quoted 71 lines · Caleb Sander Mateos
Quoted 8 lines · Ming Lei

Commit 57e13a2e8cd2 ("selftests: ublk: support user recovery") starts to support UBLK_F_NEED_GET_DATA for covering recovery feature, however the ublk utility implementation isn't done correctly.

Fix it by supporting UBLK_F_NEED_GET_DATA correctly.

Also add test generic_07 for covering UBLK_F_NEED_GET_DATA.

Looks good to me, just a few minor comments.

Quoted 28 lines
Fixes 57e13a2e8cd2 ("selftests: ublk: support user recovery")
---
 tools/testing/selftests/ublk/Makefile         |  1 +
 tools/testing/selftests/ublk/kublk.c          | 11 +++++---
 tools/testing/selftests/ublk/kublk.h          |  1 +
 .../testing/selftests/ublk/test_generic_07.sh | 25 +++++++++++++++++++
 4 files changed, 35 insertions(+), 3 deletions(-)
 create mode 100755 tools/testing/selftests/ublk/test_generic_07.sh
tools/testing/selftests/ublk/Makefile +1 -0
index ec4624a283bc..f34ac0bac696 100644
@@ -9,6 +9,7 @@ TEST_PROGS += test_generic_03.sh
 TEST_PROGS += test_generic_04.sh
 TEST_PROGS += test_generic_05.sh
 TEST_PROGS += test_generic_06.sh
+TEST_PROGS += test_generic_07.sh
 
 TEST_PROGS += test_null_01.sh
 TEST_PROGS += test_null_02.sh
tools/testing/selftests/ublk/kublk.c +0 -0
index e57a1486bb48..701b47f98902 100644

tools/testing/selftests/ublk/kublk.c int ublk_queue_io_cmd(struct ublk_queue *q, struct ublk_io *io, unsigned tag) · line 541

Quoted 5 lines
tools/testing/selftests/ublk/kublk.c +1 -1
@@ -538,10 +538,12 @@ int ublk_queue_io_cmd(struct ublk_queue *q, struct ublk_io *io, unsigned tag)
 
        /* we issue because we need either fetching or committing */
        if (!(io->flags &
-               (UBLKSRV_NEED_FETCH_RQ | UBLKSRV_NEED_COMMIT_RQ_COMP)))
+               (UBLKSRV_NEED_FETCH_RQ | UBLKSRV_NEED_COMMIT_RQ_COMP | UBLKSRV_NEED_GET_DATA)))

Comment could use an update

Quoted 21 lines
tools/testing/selftests/ublk/kublk.c +6 -1
                return 0;
 
-       if (io->flags & UBLKSRV_NEED_COMMIT_RQ_COMP)
+       if (io->flags & UBLKSRV_NEED_GET_DATA)
+               cmd_op = UBLK_U_IO_NEED_GET_DATA;
+       else if (io->flags & UBLKSRV_NEED_COMMIT_RQ_COMP)
                cmd_op = UBLK_U_IO_COMMIT_AND_FETCH_REQ;
        else if (io->flags & UBLKSRV_NEED_FETCH_RQ)
                cmd_op = UBLK_U_IO_FETCH_REQ;
@@ -658,6 +660,9 @@ static void ublk_handle_cqe(struct io_uring *r,
                assert(tag < q->q_depth);
                if (q->tgt_ops->queue_io)
                        q->tgt_ops->queue_io(q, tag);
+       } else if (cqe->res == UBLK_IO_RES_NEED_GET_DATA) {
+               io->flags |= UBLKSRV_NEED_GET_DATA | UBLKSRV_IO_FREE;
+               ublk_queue_io_cmd(q, io, tag);
        } else {
                /*
                 * COMMIT_REQ will be completed immediately since no fetching
@@ -1313,7 +1318,7 @@ int main(int argc, char *argv[])
 
Quoted 9 lines

tools/testing/selftests/ublk/kublk.c int main(int argc, char *argv[]) · line 1322

Quoted 5 lines
tools/testing/selftests/ublk/kublk.c +1 -1
        opterr = 0;
        optind = 2;
-       while ((opt = getopt_long(argc, argv, "t:n:d:q:r:e:i:az",
+       while ((opt = getopt_long(argc, argv, "t:n:d:q:r:e:i:g:az",
                                  longopts, &option_idx)) != -1) {

It's a little strange for -g to take an argument since it's basically a boolean flag. But it looks like several other flags behave the same way.

Yeah, we can make it one bool flag.

Quoted 33 lines
Quoted 33 lines
tools/testing/selftests/ublk/kublk.c +0 -0
                switch (opt) {
                case 'a':
tools/testing/selftests/ublk/kublk.h +1 -0
index 918db5cd633f..44ee1e4ac55b 100644
@@ -115,6 +115,7 @@ struct ublk_io {
 #define UBLKSRV_NEED_FETCH_RQ          (1UL << 0)
 #define UBLKSRV_NEED_COMMIT_RQ_COMP    (1UL << 1)
 #define UBLKSRV_IO_FREE                        (1UL << 2)
+#define UBLKSRV_NEED_GET_DATA           (1UL << 3)
        unsigned short flags;
        unsigned short refs;            /* used by target code only */
 
tools/testing/selftests/ublk/test_generic_07.sh new file +12 -0
new file mode 100755
index 000000000000..5d82b5955006
@@ -0,0 +1,25 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+
+. "$(cd "$(dirname "$0")" && pwd)"/test_common.sh
+
+TID="generic_07"
+ERR_CODE=0
+
+_prep_test "generic" "test UBLK_F_NEED_GET_DATA"
+
+_create_backfile 0 256M
+dev_id=$(_add_ublk_dev -t loop -q 2 -g 1 "${UBLK_BACKFILES[0]}")
Quoted 8 lines

tools/testing/selftests/ublk/test_generic_07.sh line 18

Quoted 6 lines
tools/testing/selftests/ublk/test_generic_07.sh new file +6 -0
+_check_add_dev $TID $?
+
+# run fio over the ublk disk
+if ! _run_fio_verify_io --filename=/dev/ublkb"${dev_id}" --size=256M; then
+       _cleanup_test "generic"
+       _show_result $TID 255

Propagate the return code from fio?

OK.

Quoted 5 lines

tools/testing/selftests/ublk/test_generic_07.sh line 19

Quoted 2 lines
tools/testing/selftests/ublk/test_generic_07.sh new file +1 -0
+fi

All the other tests that use _run_fio_verify_io appear to check _have_program fio first. Is that necessary here too?

Will add the check.

Thanks,
Ming

author
#4 lore raw

PATCH 2/4 ublk: enhance check for register/unregister io buffer command

In reply to Ming Lei

The simple check of UBLK_IO_FLAG_OWNED_BY_SRV can avoid incorrect register/unregister io buffer easily, so check it before calling starting to register/un-register io buffer.

Also only allow io buffer register/unregister uring_cmd in case of UBLK_F_SUPPORT_ZERO_COPY.

Fixes 1f6540e2aabb ("ublk: zc register/unregister bvec")
---
 drivers/block/ublk_drv.c | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)
drivers/block/ublk_drv.c +22 -1
index 40f971a66d3e..347790b3a633 100644
@@ -609,6 +609,11 @@ static void ublk_apply_params(struct ublk_device *ub)
 		ublk_dev_param_zoned_apply(ub);
 }
 
+static inline bool ublk_support_zero_copy(const struct ublk_queue *ubq)
+{
+	return ubq->flags & UBLK_F_SUPPORT_ZERO_COPY;
+}
+
 static inline bool ublk_support_user_copy(const struct ublk_queue *ubq)
 {
 	return ubq->flags & (UBLK_F_USER_COPY | UBLK_F_SUPPORT_ZERO_COPY);
@@ -1950,9 +1955,16 @@ static int ublk_register_io_buf(struct io_uring_cmd *cmd,
 				unsigned int index, unsigned int issue_flags)
 {
 	struct ublk_device *ub = cmd->file->private_data;
+	struct ublk_io *io = &ubq->ios[tag];
 	struct request *req;
 	int ret;
 
+	if (!ublk_support_zero_copy(ubq))
+		return -EINVAL;
+
+	if (!(io->flags & UBLK_IO_FLAG_OWNED_BY_SRV))
+		return -EINVAL;
+
 	req = __ublk_check_and_get_req(ub, ubq, tag, 0);
 	if (!req)
 		return -EINVAL;
@@ -1968,8 +1980,17 @@ static int ublk_register_io_buf(struct io_uring_cmd *cmd,
 }
 
 static int ublk_unregister_io_buf(struct io_uring_cmd *cmd,
+				  struct ublk_queue *ubq, unsigned int tag,
 				  unsigned int index, unsigned int issue_flags)
 {
+	struct ublk_io *io = &ubq->ios[tag];
+
+	if (!ublk_support_zero_copy(ubq))
+		return -EINVAL;
+
+	if (!(io->flags & UBLK_IO_FLAG_OWNED_BY_SRV))
+		return -EINVAL;
+
 	return io_buffer_unregister_bvec(cmd, index, issue_flags);
 }
 
@@ -2073,7 +2094,7 @@ static int __ublk_ch_uring_cmd(struct io_uring_cmd *cmd,
 	case UBLK_IO_REGISTER_IO_BUF:
 		return ublk_register_io_buf(cmd, ubq, tag, ub_cmd->addr, issue_flags);
 	case UBLK_IO_UNREGISTER_IO_BUF:
-		return ublk_unregister_io_buf(cmd, ub_cmd->addr, issue_flags);
+		return ublk_unregister_io_buf(cmd, ubq, tag, ub_cmd->addr, issue_flags);
 	case UBLK_IO_FETCH_REQ:
 		ret = ublk_fetch(cmd, ubq, io, ub_cmd->addr);
 		if (ret)
Signature · 1 line
2.47.0
#5 lore raw

Re: [PATCH 2/4] ublk: enhance check for register/unregister io buffer command

Quoted 7 lines · Ming Lei

The simple check of UBLK_IO_FLAG_OWNED_BY_SRV can avoid incorrect register/unregister io buffer easily, so check it before calling starting to register/un-register io buffer.

Also only allow io buffer register/unregister uring_cmd in case of UBLK_F_SUPPORT_ZERO_COPY.

Indeed, both these checks make sense. (Hopefully there aren't any applications depending on the ability to use ublk zero-copy without setting the flag.) I too was thinking of adding the UBLK_IO_FLAG_OWNED_BY_SRV check because it could allow the kref_get_unless_zero() to be replaced with the cheaper kref_get(). I think the checks could be split into 2 separate commits, but up to you.

Quoted 24 lines
Fixes 1f6540e2aabb ("ublk: zc register/unregister bvec")
---
 drivers/block/ublk_drv.c | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)
drivers/block/ublk_drv.c +5 -0
index 40f971a66d3e..347790b3a633 100644
@@ -609,6 +609,11 @@ static void ublk_apply_params(struct ublk_device *ub)
                ublk_dev_param_zoned_apply(ub);
 }
 
+static inline bool ublk_support_zero_copy(const struct ublk_queue *ubq)
+{
+       return ubq->flags & UBLK_F_SUPPORT_ZERO_COPY;
+}
+
 static inline bool ublk_support_user_copy(const struct ublk_queue *ubq)
 {
        return ubq->flags & (UBLK_F_USER_COPY | UBLK_F_SUPPORT_ZERO_COPY);

drivers/block/ublk_drv.c static int ublk_register_io_buf(struct io_uring_cmd *cmd, · line 1958

Quoted 4 lines
drivers/block/ublk_drv.c +1 -0
@@ -1950,9 +1955,16 @@ static int ublk_register_io_buf(struct io_uring_cmd *cmd,
                                unsigned int index, unsigned int issue_flags)
 {
        struct ublk_device *ub = cmd->file->private_data;
+       struct ublk_io *io = &ubq->ios[tag];

I thought you had mentioned in
https://lore.kernel.org/linux-block/aAmYJxaV1-yWEMRo@fedora/ wanting to the ability to offload the ublk zero-copy buffer registration to a thread other than ubq_daemon. Are you still planning to do that, or does the "auto-register" feature supplant the need for that? Accessing the ublk_io here only seems safe when on the ubq_daemon thread.

drivers/block/ublk_drv.c static int ublk_register_io_buf(struct io_uring_cmd *cmd, · line 1966

Quoted 8 lines
drivers/block/ublk_drv.c +5 -0
        struct request *req;
        int ret;
 
+       if (!ublk_support_zero_copy(ubq))
+               return -EINVAL;
+
+       if (!(io->flags & UBLK_IO_FLAG_OWNED_BY_SRV))
+               return -EINVAL;

Every opcode except UBLK_IO_FETCH_REQ now checks io->flags & UBLK_IO_FLAG_OWNED_BY_SRV. Maybe it would make sense to lift the check up to __ublk_ch_uring_cmd() to avoid duplicating it?

Best,
Caleb

Quoted 34 lines
drivers/block/ublk_drv.c +11 -2
+
        req = __ublk_check_and_get_req(ub, ubq, tag, 0);
        if (!req)
                return -EINVAL;
@@ -1968,8 +1980,17 @@ static int ublk_register_io_buf(struct io_uring_cmd *cmd,
 }
 
 static int ublk_unregister_io_buf(struct io_uring_cmd *cmd,
+                                 struct ublk_queue *ubq, unsigned int tag,
                                  unsigned int index, unsigned int issue_flags)
 {
+       struct ublk_io *io = &ubq->ios[tag];
+
+       if (!ublk_support_zero_copy(ubq))
+               return -EINVAL;
+
+       if (!(io->flags & UBLK_IO_FLAG_OWNED_BY_SRV))
+               return -EINVAL;
+
        return io_buffer_unregister_bvec(cmd, index, issue_flags);
 }
 
@@ -2073,7 +2094,7 @@ static int __ublk_ch_uring_cmd(struct io_uring_cmd *cmd,
        case UBLK_IO_REGISTER_IO_BUF:
                return ublk_register_io_buf(cmd, ubq, tag, ub_cmd->addr, issue_flags);
        case UBLK_IO_UNREGISTER_IO_BUF:
-               return ublk_unregister_io_buf(cmd, ub_cmd->addr, issue_flags);
+               return ublk_unregister_io_buf(cmd, ubq, tag, ub_cmd->addr, issue_flags);
        case UBLK_IO_FETCH_REQ:
                ret = ublk_fetch(cmd, ubq, io, ub_cmd->addr);
                if (ret)
--

2.47.0

author
#6 lore raw

Re: [PATCH 2/4] ublk: enhance check for register/unregister io buffer command

Quoted 9 lines · Caleb Sander Mateos
Quoted 7 lines · Ming Lei

The simple check of UBLK_IO_FLAG_OWNED_BY_SRV can avoid incorrect register/unregister io buffer easily, so check it before calling starting to register/un-register io buffer.

Also only allow io buffer register/unregister uring_cmd in case of UBLK_F_SUPPORT_ZERO_COPY.

Quoted 7 lines

Indeed, both these checks make sense. (Hopefully there aren't any applications depending on the ability to use ublk zero-copy without setting the flag.) I too was thinking of adding the UBLK_IO_FLAG_OWNED_BY_SRV check because it could allow the kref_get_unless_zero() to be replaced with the cheaper kref_get(). I think the checks could be split into 2 separate commits, but up to you.

Let's do it in single patch for making everyone easier.

Quoted 27 lines
Quoted 27 lines
Fixes 1f6540e2aabb ("ublk: zc register/unregister bvec")
---
 drivers/block/ublk_drv.c | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)
drivers/block/ublk_drv.c +5 -0
index 40f971a66d3e..347790b3a633 100644
@@ -609,6 +609,11 @@ static void ublk_apply_params(struct ublk_device *ub)
                ublk_dev_param_zoned_apply(ub);
 }
 
+static inline bool ublk_support_zero_copy(const struct ublk_queue *ubq)
+{
+       return ubq->flags & UBLK_F_SUPPORT_ZERO_COPY;
+}
+
 static inline bool ublk_support_user_copy(const struct ublk_queue *ubq)
 {
        return ubq->flags & (UBLK_F_USER_COPY | UBLK_F_SUPPORT_ZERO_COPY);
@@ -1950,9 +1955,16 @@ static int ublk_register_io_buf(struct io_uring_cmd *cmd,
                                unsigned int index, unsigned int issue_flags)
 {
Quoted 8 lines

drivers/block/ublk_drv.c static int ublk_register_io_buf(struct io_uring_cmd *cmd, · line 1958

Quoted 2 lines
drivers/block/ublk_drv.c +1 -0
        struct ublk_device *ub = cmd->file->private_data;
+       struct ublk_io *io = &ubq->ios[tag];

I thought you had mentioned in
https://lore.kernel.org/linux-block/aAmYJxaV1-yWEMRo@fedora/ wanting to the ability to offload the ublk zero-copy buffer registration to a thread other than ubq_daemon. Are you still planning to do that, or does the "auto-register" feature supplant the need for that?

The auto-register idea is actually thought of when I was working on ublk selftest offload function.

If this auto-register feature is supported, it becomes less important to relax the ubq_daemon limit for register_io_buffer command, then I jump on this feature & post put the patch.

But I will continue to work on the offload test code and finally relax the limit for register/unregister io buffer command, hope it can be done in next week.

Quoted 2 lines

Accessing
the ublk_io here only seems safe when on the ubq_daemon thread.

Both ublk_register_io_buf()/ublk_unregister_io_buf() just reads ublk_io or the request buffer only, so it is just fine for the two to run from other contexts.

Quoted 10 lines

drivers/block/ublk_drv.c static int ublk_register_io_buf(struct io_uring_cmd *cmd, · line 1966

Quoted 9 lines
drivers/block/ublk_drv.c +5 -0
        struct request *req;
        int ret;
 
+       if (!ublk_support_zero_copy(ubq))
+               return -EINVAL;
+
+       if (!(io->flags & UBLK_IO_FLAG_OWNED_BY_SRV))
+               return -EINVAL;
Quoted 3 lines

Every opcode except UBLK_IO_FETCH_REQ now checks io->flags & UBLK_IO_FLAG_OWNED_BY_SRV. Maybe it would make sense to lift the check up to __ublk_ch_uring_cmd() to avoid duplicating it?

Good point.

Thanks,
Ming

#7 lore raw

Re: [PATCH 2/4] ublk: enhance check for register/unregister io buffer command

Quoted 68 lines · Ming Lei
Quoted 9 lines · Caleb Sander Mateos
Quoted 7 lines · Ming Lei

The simple check of UBLK_IO_FLAG_OWNED_BY_SRV can avoid incorrect register/unregister io buffer easily, so check it before calling starting to register/un-register io buffer.

Also only allow io buffer register/unregister uring_cmd in case of UBLK_F_SUPPORT_ZERO_COPY.

Quoted 7 lines

Indeed, both these checks make sense. (Hopefully there aren't any applications depending on the ability to use ublk zero-copy without setting the flag.) I too was thinking of adding the UBLK_IO_FLAG_OWNED_BY_SRV check because it could allow the kref_get_unless_zero() to be replaced with the cheaper kref_get(). I think the checks could be split into 2 separate commits, but up to you.

Let's do it in single patch for making everyone easier.

Quoted 27 lines
Quoted 27 lines
Fixes 1f6540e2aabb ("ublk: zc register/unregister bvec")
---
 drivers/block/ublk_drv.c | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)
drivers/block/ublk_drv.c +5 -0
index 40f971a66d3e..347790b3a633 100644
@@ -609,6 +609,11 @@ static void ublk_apply_params(struct ublk_device *ub)
                ublk_dev_param_zoned_apply(ub);
 }
 
+static inline bool ublk_support_zero_copy(const struct ublk_queue *ubq)
+{
+       return ubq->flags & UBLK_F_SUPPORT_ZERO_COPY;
+}
+
 static inline bool ublk_support_user_copy(const struct ublk_queue *ubq)
 {
        return ubq->flags & (UBLK_F_USER_COPY | UBLK_F_SUPPORT_ZERO_COPY);
@@ -1950,9 +1955,16 @@ static int ublk_register_io_buf(struct io_uring_cmd *cmd,
                                unsigned int index, unsigned int issue_flags)
 {
Quoted 8 lines

drivers/block/ublk_drv.c static int ublk_register_io_buf(struct io_uring_cmd *cmd, · line 1958

Quoted 2 lines
drivers/block/ublk_drv.c +1 -0
        struct ublk_device *ub = cmd->file->private_data;
+       struct ublk_io *io = &ubq->ios[tag];

I thought you had mentioned in
https://lore.kernel.org/linux-block/aAmYJxaV1-yWEMRo@fedora/ wanting to the ability to offload the ublk zero-copy buffer registration to a thread other than ubq_daemon. Are you still planning to do that, or does the "auto-register" feature supplant the need for that?

The auto-register idea is actually thought of when I was working on ublk selftest offload function.

If this auto-register feature is supported, it becomes less important to relax the ubq_daemon limit for register_io_buffer command, then I jump on this feature & post put the patch.

But I will continue to work on the offload test code and finally relax the limit for register/unregister io buffer command, hope it can be done in next week.

Quoted 6 lines
Quoted 2 lines

Accessing
the ublk_io here only seems safe when on the ubq_daemon thread.

Both ublk_register_io_buf()/ublk_unregister_io_buf() just reads ublk_io or the request buffer only, so it is just fine for the two to run from other contexts.

Isn't it racy to check io->flags when it could be concurrently modified by another thread (the ubq_daemon)?

Best,
Caleb

Quoted 21 lines
Quoted 11 lines

drivers/block/ublk_drv.c static int ublk_register_io_buf(struct io_uring_cmd *cmd, · line 1966

Quoted 10 lines
drivers/block/ublk_drv.c +5 -0
        struct request *req;
        int ret;
 
+       if (!ublk_support_zero_copy(ubq))
+               return -EINVAL;
+
+       if (!(io->flags & UBLK_IO_FLAG_OWNED_BY_SRV))
+               return -EINVAL;
Quoted 3 lines

Every opcode except UBLK_IO_FETCH_REQ now checks io->flags & UBLK_IO_FLAG_OWNED_BY_SRV. Maybe it would make sense to lift the check up to __ublk_ch_uring_cmd() to avoid duplicating it?

Good point.

Thanks,
Ming

author
#8 lore raw

Re: [PATCH 2/4] ublk: enhance check for register/unregister io buffer command

Quoted 72 lines · Caleb Sander Mateos
Quoted 71 lines · Ming Lei
Quoted 9 lines · Caleb Sander Mateos
Quoted 7 lines · Ming Lei

The simple check of UBLK_IO_FLAG_OWNED_BY_SRV can avoid incorrect register/unregister io buffer easily, so check it before calling starting to register/un-register io buffer.

Also only allow io buffer register/unregister uring_cmd in case of UBLK_F_SUPPORT_ZERO_COPY.

Quoted 7 lines

Indeed, both these checks make sense. (Hopefully there aren't any applications depending on the ability to use ublk zero-copy without setting the flag.) I too was thinking of adding the UBLK_IO_FLAG_OWNED_BY_SRV check because it could allow the kref_get_unless_zero() to be replaced with the cheaper kref_get(). I think the checks could be split into 2 separate commits, but up to you.

Let's do it in single patch for making everyone easier.

Quoted 27 lines
Quoted 27 lines
Fixes 1f6540e2aabb ("ublk: zc register/unregister bvec")
---
 drivers/block/ublk_drv.c | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)
drivers/block/ublk_drv.c +5 -0
index 40f971a66d3e..347790b3a633 100644
@@ -609,6 +609,11 @@ static void ublk_apply_params(struct ublk_device *ub)
                ublk_dev_param_zoned_apply(ub);
 }
 
+static inline bool ublk_support_zero_copy(const struct ublk_queue *ubq)
+{
+       return ubq->flags & UBLK_F_SUPPORT_ZERO_COPY;
+}
+
 static inline bool ublk_support_user_copy(const struct ublk_queue *ubq)
 {
        return ubq->flags & (UBLK_F_USER_COPY | UBLK_F_SUPPORT_ZERO_COPY);
@@ -1950,9 +1955,16 @@ static int ublk_register_io_buf(struct io_uring_cmd *cmd,
                                unsigned int index, unsigned int issue_flags)
 {
Quoted 8 lines

drivers/block/ublk_drv.c static int ublk_register_io_buf(struct io_uring_cmd *cmd, · line 1958

Quoted 2 lines
drivers/block/ublk_drv.c +1 -0
        struct ublk_device *ub = cmd->file->private_data;
+       struct ublk_io *io = &ubq->ios[tag];

I thought you had mentioned in
https://lore.kernel.org/linux-block/aAmYJxaV1-yWEMRo@fedora/ wanting to the ability to offload the ublk zero-copy buffer registration to a thread other than ubq_daemon. Are you still planning to do that, or does the "auto-register" feature supplant the need for that?

The auto-register idea is actually thought of when I was working on ublk selftest offload function.

If this auto-register feature is supported, it becomes less important to relax the ubq_daemon limit for register_io_buffer command, then I jump on this feature & post put the patch.

But I will continue to work on the offload test code and finally relax the limit for register/unregister io buffer command, hope it can be done in next week.

Quoted 2 lines

Accessing
the ublk_io here only seems safe when on the ubq_daemon thread.

Quoted 6 lines
Quoted 3 lines

Both ublk_register_io_buf()/ublk_unregister_io_buf() just reads ublk_io or the request buffer only, so it is just fine for the two to run from other contexts.

Isn't it racy to check io->flags when it could be concurrently modified by another thread (the ubq_daemon)?

Good question!

Yeah, it becomes tricky if registering buffer from other pthread, such as:

- one io handler thread is registering buffer for tag 0 from cpu 0

- UBLK_IO_COMMIT_AND_FETCH_REQ comes on tag 0 from one bad ublk daemon

Then the io handler thread may observe UBLK_IO_FLAG_OWNED_BY_SRV, but meantime UBLK_IO_COMMIT_AND_FETCH_REQ clears it and completes the request, and this request may be freed or recycled immediately. Then the io handler pthread sees wrong request data.

The approach I mentioned in the following link may help to support 'offload':

https://lore.kernel.org/linux-block/aAscRPVcTBiBHNe7@fedora/

The nice thing is that one batch of commands can be delivered via single or multiple READ_MULTISHOT, and per-queue spin lock can be used. Same with io command completion side. And it becomes easier to remove the ubq_daemon constraint with the per-queue lock.

Thanks,
Ming

author
#9 lore raw

PATCH 3/4 ublk: add feature UBLK_F_AUTO_ZERO_COPY

In reply to Ming Lei

UBLK_F_SUPPORT_ZERO_COPY requires ublk server to issue explicit buffer register/unregister uring_cmd for each IO, this way is not only inefficient, but also introduce dependency between buffer consumer and buffer register/ unregister uring_cmd, please see tools/testing/selftests/ublk/stripe.c in which backing file IO has to be issued one by one by IOSQE_IO_LINK.

Add feature UBLK_F_AUTO_ZERO_COPY for addressing the existed zero copy limit:

- register request buffer automatically before delivering io command to ublk server

- unregister request buffer automatically when completing the request

- io_uring will unregister the buffer automatically when uring is exiting, so we needn't worry about accident exit

---
 drivers/block/ublk_drv.c      | 87 +++++++++++++++++++++++++++--------
 include/uapi/linux/ublk_cmd.h | 20 ++++++++
 2 files changed, 89 insertions(+), 18 deletions(-)
drivers/block/ublk_drv.c +69 -18
index 347790b3a633..453767f91431 100644
@@ -64,7 +64,8 @@
 		| UBLK_F_CMD_IOCTL_ENCODE \
 		| UBLK_F_USER_COPY \
 		| UBLK_F_ZONED \
-		| UBLK_F_USER_RECOVERY_FAIL_IO)
+		| UBLK_F_USER_RECOVERY_FAIL_IO \
+		| UBLK_F_AUTO_ZERO_COPY)
 
 #define UBLK_F_ALL_RECOVERY_FLAGS (UBLK_F_USER_RECOVERY \
 		| UBLK_F_USER_RECOVERY_REISSUE \
@@ -131,6 +132,14 @@ struct ublk_uring_cmd_pdu {
  */
 #define UBLK_IO_FLAG_NEED_GET_DATA 0x08
 
+/*
+ * request buffer is registered automatically, so we have to unregister it
+ * before completing this request.
+ *
+ * io_uring will unregister buffer automatically for us during exiting.
+ */
+#define UBLK_IO_FLAG_UNREG_BUF 	0x10
+
 /* atomic RW with ubq->cancel_lock */
 #define UBLK_IO_FLAG_CANCELED	0x80000000
 
@@ -207,7 +216,8 @@ static inline struct ublksrv_io_desc *ublk_get_iod(struct ublk_queue *ubq,
 						   int tag);
 static inline bool ublk_dev_is_user_copy(const struct ublk_device *ub)
 {
-	return ub->dev_info.flags & (UBLK_F_USER_COPY | UBLK_F_SUPPORT_ZERO_COPY);
+	return ub->dev_info.flags & (UBLK_F_USER_COPY |
+			UBLK_F_SUPPORT_ZERO_COPY | UBLK_F_AUTO_ZERO_COPY);
 }
 
 static inline bool ublk_dev_is_zoned(const struct ublk_device *ub)
@@ -614,6 +624,11 @@ static inline bool ublk_support_zero_copy(const struct ublk_queue *ubq)
 	return ubq->flags & UBLK_F_SUPPORT_ZERO_COPY;
 }
 
+static inline bool ublk_support_auto_zc(const struct ublk_queue *ubq)
+{
+	return ubq->flags & UBLK_F_AUTO_ZERO_COPY;
+}
+
 static inline bool ublk_support_user_copy(const struct ublk_queue *ubq)
 {
 	return ubq->flags & (UBLK_F_USER_COPY | UBLK_F_SUPPORT_ZERO_COPY);
@@ -621,7 +636,7 @@ static inline bool ublk_support_user_copy(const struct ublk_queue *ubq)
 
 static inline bool ublk_need_map_io(const struct ublk_queue *ubq)
 {
-	return !ublk_support_user_copy(ubq);
+	return !ublk_support_user_copy(ubq) && !ublk_support_auto_zc(ubq);
 }
 
 static inline bool ublk_need_req_ref(const struct ublk_queue *ubq)
@@ -629,17 +644,21 @@ static inline bool ublk_need_req_ref(const struct ublk_queue *ubq)
 	/*
 	 * read()/write() is involved in user copy, so request reference
 	 * has to be grabbed
+	 *
+	 * For auto zc, ublk server still may issue UBLK_IO_COMMIT_AND_FETCH_REQ
+	 * before one registered buffer is used up, so reference is
+	 * required too.
 	 */
-	return ublk_support_user_copy(ubq);
+	return ublk_support_user_copy(ubq) || ublk_support_auto_zc(ubq);
 }
 
 static inline void ublk_init_req_ref(const struct ublk_queue *ubq,
-		struct request *req)
+		struct request *req, int init_ref)
 {
 	if (ublk_need_req_ref(ubq)) {
 		struct ublk_rq_data *data = blk_mq_rq_to_pdu(req);
 
-		kref_init(&data->ref);
+		refcount_set(&data->ref.refcount, init_ref);
 	}
 }
 
@@ -667,6 +686,15 @@ static inline void ublk_put_req_ref(const struct ublk_queue *ubq,
 	}
 }
 
+/* for ublk zero copy */
+static void ublk_io_release(void *priv)
+{
+	struct request *rq = priv;
+	struct ublk_queue *ubq = rq->mq_hctx->driver_data;
+
+	ublk_put_req_ref(ubq, rq);
+}
+
 static inline bool ublk_need_get_data(const struct ublk_queue *ubq)
 {
 	return ubq->flags & UBLK_F_NEED_GET_DATA;
@@ -1231,7 +1259,22 @@ static void ublk_dispatch_req(struct ublk_queue *ubq,
 			mapped_bytes >> 9;
 	}
 
-	ublk_init_req_ref(ubq, req);
+	if (ublk_support_auto_zc(ubq) && ublk_rq_has_data(req)) {
+		int ret;
+
+		/* one extra reference is dropped by ublk_io_release */
+		ublk_init_req_ref(ubq, req, 2);
+		ret = io_buffer_register_bvec(io->cmd, req, ublk_io_release,
+					      tag, issue_flags);
+		if (ret) {
+			blk_mq_end_request(req, BLK_STS_IOERR);
+			return;
+		}
+		io->flags |= UBLK_IO_FLAG_UNREG_BUF;
+	} else {
+		ublk_init_req_ref(ubq, req, 1);
+	}
+
 	ubq_complete_io_cmd(io, UBLK_IO_RES_OK, issue_flags);
 }
 
@@ -1593,7 +1636,8 @@ static int ublk_ch_mmap(struct file *filp, struct vm_area_struct *vma)
 }
 
 static void ublk_commit_completion(struct ublk_device *ub,
-		const struct ublksrv_io_cmd *ub_cmd)
+		const struct ublksrv_io_cmd *ub_cmd,
+		unsigned int issue_flags)
 {
 	u32 qid = ub_cmd->q_id, tag = ub_cmd->tag;
 	struct ublk_queue *ubq = ublk_get_queue(ub, qid);
@@ -1604,6 +1648,15 @@ static void ublk_commit_completion(struct ublk_device *ub,
 	io->flags &= ~UBLK_IO_FLAG_OWNED_BY_SRV;
 	io->res = ub_cmd->result;
 
+	if (io->flags & UBLK_IO_FLAG_UNREG_BUF) {
+		int ret;
+
+		ret = io_buffer_unregister_bvec(io->cmd, tag, issue_flags);
+		if (ret)
+			io->res = ret;
+		io->flags &= ~UBLK_IO_FLAG_UNREG_BUF;
+	}
+
 	/* find the io request and complete */
 	req = blk_mq_tag_to_rq(ub->tag_set.tags[qid], tag);
 	if (WARN_ON_ONCE(unlikely(!req)))
@@ -1942,14 +1995,6 @@ static inline void ublk_prep_cancel(struct io_uring_cmd *cmd,
 	io_uring_cmd_mark_cancelable(cmd, issue_flags);
 }
 
-static void ublk_io_release(void *priv)
-{
-	struct request *rq = priv;
-	struct ublk_queue *ubq = rq->mq_hctx->driver_data;
-
-	ublk_put_req_ref(ubq, rq);
-}
-
 static int ublk_register_io_buf(struct io_uring_cmd *cmd,
 				struct ublk_queue *ubq, unsigned int tag,
 				unsigned int index, unsigned int issue_flags)
@@ -2124,7 +2169,7 @@ static int __ublk_ch_uring_cmd(struct io_uring_cmd *cmd,
 		}
 
 		ublk_fill_io_cmd(io, cmd, ub_cmd->addr);
-		ublk_commit_completion(ub, ub_cmd);
+		ublk_commit_completion(ub, ub_cmd, issue_flags);
 		break;
 	case UBLK_IO_NEED_GET_DATA:
 		if (!(io->flags & UBLK_IO_FLAG_OWNED_BY_SRV))
@@ -2730,6 +2775,11 @@ static int ublk_ctrl_add_dev(const struct ublksrv_ctrl_cmd *header)
 		return -EINVAL;
 	}
 
+	/* F_AUTO_ZERO_COPY and F_SUPPORT_ZERO_COPY can't co-exist */
+	if ((info.flags & UBLK_F_AUTO_ZERO_COPY) &&
+			(info.flags & UBLK_F_SUPPORT_ZERO_COPY))
+		return -EINVAL;
+
 	/*
 	 * unprivileged device can't be trusted, but RECOVERY and
 	 * RECOVERY_REISSUE still may hang error handling, so can't
@@ -2747,7 +2797,8 @@ static int ublk_ctrl_add_dev(const struct ublksrv_ctrl_cmd *header)
 		 * buffer by pwrite() to ublk char device, which can't be
 		 * used for unprivileged device
 		 */
-		if (info.flags & (UBLK_F_USER_COPY | UBLK_F_SUPPORT_ZERO_COPY))
+		if (info.flags & (UBLK_F_USER_COPY | UBLK_F_SUPPORT_ZERO_COPY |
+					UBLK_F_AUTO_ZERO_COPY))
 			return -EINVAL;
 	}
 
include/uapi/linux/ublk_cmd.h +20 -0
index 583b86681c93..d8bb5e55cce7 100644
@@ -211,6 +211,26 @@
  */
 #define UBLK_F_USER_RECOVERY_FAIL_IO (1ULL << 9)
 
+/*
+ * request buffer is registered automatically before delivering this io
+ * command to ublk server, meantime it is un-registered automatically
+ * when completing this io command.
+ *
+ * request tag has to be used as the buffer index, and ublk server has to
+ * pass this IO's tag as buffer index for using the registered zero copy
+ * buffer
+ *
+ * This way avoids extra uring_cmd cost, but also simplifies backend
+ * implementation, such as, the dependency on IO_REGISTER_IO_BUF and
+ * IO_UNREGISTER_IO_BUF becomes not necessary.
+ *
+ * For using this feature, ublk server has to register buffer table
+ * in sparse way, and buffer number has to be >= ublk queue depth.
+ *
+ * This feature is preferred to UBLK_F_SUPPORT_ZERO_COPY.
+ */
+#define UBLK_F_AUTO_ZERO_COPY 	(1ULL << 10)
+
 /* device state */
 #define UBLK_S_DEV_DEAD	0
 #define UBLK_S_DEV_LIVE	1
Signature · 1 line
2.47.0
#10 lore raw

Re: [PATCH 3/4] ublk: add feature UBLK_F_AUTO_ZERO_COPY

Quoted 6 lines · Ming Lei

UBLK_F_SUPPORT_ZERO_COPY requires ublk server to issue explicit buffer register/unregister uring_cmd for each IO, this way is not only inefficient, but also introduce dependency between buffer consumer and buffer register/ unregister uring_cmd, please see tools/testing/selftests/ublk/stripe.c in which backing file IO has to be issued one by one by IOSQE_IO_LINK.

This is a great idea!

Quoted 3 lines

Add feature UBLK_F_AUTO_ZERO_COPY for addressing the existed zero copy limit:

nit: "existed" -> "existing", "limit" -> "limitation"

Quoted 76 lines

- register request buffer automatically before delivering io command to ublk server

- unregister request buffer automatically when completing the request

- io_uring will unregister the buffer automatically when uring is exiting, so we needn't worry about accident exit

---
 drivers/block/ublk_drv.c      | 87 +++++++++++++++++++++++++++--------
 include/uapi/linux/ublk_cmd.h | 20 ++++++++
 2 files changed, 89 insertions(+), 18 deletions(-)
drivers/block/ublk_drv.c +18 -3
index 347790b3a633..453767f91431 100644
@@ -64,7 +64,8 @@
                | UBLK_F_CMD_IOCTL_ENCODE \
                | UBLK_F_USER_COPY \
                | UBLK_F_ZONED \
-               | UBLK_F_USER_RECOVERY_FAIL_IO)
+               | UBLK_F_USER_RECOVERY_FAIL_IO \
+               | UBLK_F_AUTO_ZERO_COPY)
 
 #define UBLK_F_ALL_RECOVERY_FLAGS (UBLK_F_USER_RECOVERY \
                | UBLK_F_USER_RECOVERY_REISSUE \
@@ -131,6 +132,14 @@ struct ublk_uring_cmd_pdu {
  */
 #define UBLK_IO_FLAG_NEED_GET_DATA 0x08
 
+/*
+ * request buffer is registered automatically, so we have to unregister it
+ * before completing this request.
+ *
+ * io_uring will unregister buffer automatically for us during exiting.
+ */
+#define UBLK_IO_FLAG_UNREG_BUF         0x10
+
 /* atomic RW with ubq->cancel_lock */
 #define UBLK_IO_FLAG_CANCELED  0x80000000
 
@@ -207,7 +216,8 @@ static inline struct ublksrv_io_desc *ublk_get_iod(struct ublk_queue *ubq,
                                                   int tag);
 static inline bool ublk_dev_is_user_copy(const struct ublk_device *ub)
 {
-       return ub->dev_info.flags & (UBLK_F_USER_COPY | UBLK_F_SUPPORT_ZERO_COPY);
+       return ub->dev_info.flags & (UBLK_F_USER_COPY |
+                       UBLK_F_SUPPORT_ZERO_COPY | UBLK_F_AUTO_ZERO_COPY);
 }
 
 static inline bool ublk_dev_is_zoned(const struct ublk_device *ub)
@@ -614,6 +624,11 @@ static inline bool ublk_support_zero_copy(const struct ublk_queue *ubq)
        return ubq->flags & UBLK_F_SUPPORT_ZERO_COPY;
 }
 
+static inline bool ublk_support_auto_zc(const struct ublk_queue *ubq)
+{
+       return ubq->flags & UBLK_F_AUTO_ZERO_COPY;
+}
+
 static inline bool ublk_support_user_copy(const struct ublk_queue *ubq)
 {
        return ubq->flags & (UBLK_F_USER_COPY | UBLK_F_SUPPORT_ZERO_COPY);
@@ -621,7 +636,7 @@ static inline bool ublk_support_user_copy(const struct ublk_queue *ubq)
 
 static inline bool ublk_need_map_io(const struct ublk_queue *ubq)
 {
-       return !ublk_support_user_copy(ubq);
+       return !ublk_support_user_copy(ubq) && !ublk_support_auto_zc(ubq);
 }
 
 static inline bool ublk_need_req_ref(const struct ublk_queue *ubq)

drivers/block/ublk_drv.c static inline bool ublk_need_req_ref(const struct ublk_queue *ubq) · line 652

Quoted 10 lines
drivers/block/ublk_drv.c +5 -1
@@ -629,17 +644,21 @@ static inline bool ublk_need_req_ref(const struct ublk_queue *ubq)
        /*
         * read()/write() is involved in user copy, so request reference
         * has to be grabbed
+        *
+        * For auto zc, ublk server still may issue UBLK_IO_COMMIT_AND_FETCH_REQ
+        * before one registered buffer is used up, so reference is
+        * required too.
         */
-       return ublk_support_user_copy(ubq);
+       return ublk_support_user_copy(ubq) || ublk_support_auto_zc(ubq);

Since both places where ublk_support_user_copy() is used are being adjusted to also check ublk_support_auto_zc(), maybe ublk_support_user_copy() should just be changed to check UBLK_F_AUTO_ZERO_COPY too?

drivers/block/ublk_drv.c static inline bool ublk_need_req_ref(const struct ublk_queue *ubq) · line 661

Quoted 11 lines
drivers/block/ublk_drv.c +2 -2
 }
 
 static inline void ublk_init_req_ref(const struct ublk_queue *ubq,
-               struct request *req)
+               struct request *req, int init_ref)
 {
        if (ublk_need_req_ref(ubq)) {
                struct ublk_rq_data *data = blk_mq_rq_to_pdu(req);
 
-               kref_init(&data->ref);
+               refcount_set(&data->ref.refcount, init_ref);

It might be nicer not to mix kref and raw reference count operations. Maybe you could add a prep patch that switches from struct kref to refcount_t?

Quoted 21 lines
drivers/block/ublk_drv.c +9 -0
        }
 }
 
@@ -667,6 +686,15 @@ static inline void ublk_put_req_ref(const struct ublk_queue *ubq,
        }
 }
 
+/* for ublk zero copy */
+static void ublk_io_release(void *priv)
+{
+       struct request *rq = priv;
+       struct ublk_queue *ubq = rq->mq_hctx->driver_data;
+
+       ublk_put_req_ref(ubq, rq);
+}
+
 static inline bool ublk_need_get_data(const struct ublk_queue *ubq)
 {
        return ubq->flags & UBLK_F_NEED_GET_DATA;
@@ -1231,7 +1259,22 @@ static void ublk_dispatch_req(struct ublk_queue *ubq,
                        mapped_bytes >> 9;

drivers/block/ublk_drv.c static void ublk_dispatch_req(struct ublk_queue *ubq, · line 1268

Quoted 10 lines
drivers/block/ublk_drv.c +7 -1
        }
 
-       ublk_init_req_ref(ubq, req);
+       if (ublk_support_auto_zc(ubq) && ublk_rq_has_data(req)) {
+               int ret;
+
+               /* one extra reference is dropped by ublk_io_release */
+               ublk_init_req_ref(ubq, req, 2);
+               ret = io_buffer_register_bvec(io->cmd, req, ublk_io_release,
+                                             tag, issue_flags);

Using the ublk request tag as the registered buffer index may be too limiting. It would cause collisions if there are multiple ublk devices or queues handled on a single io_uring. It also requires offsetting any registered user buffers so their indices come after all the ublk ones, which may be difficult if ublk devices are added dynamically. Perhaps the UBLK_IO_FETCH_REQ operation could specify the registered buffer index to use for the request?

This also requires the io_uring issuing the zero-copy I/Os to be the same as the one submitting the ublk fetch requests. That would also make it difficult for us to adopt for our ublk server, which uses separate io_urings. Not sure if there is a simple way the ublk server could specify what io_uring to register the ublk zero-copy buffers with.

drivers/block/ublk_drv.c static void ublk_dispatch_req(struct ublk_queue *ubq, · line 1271

Quoted 3 lines
drivers/block/ublk_drv.c +3 -0
+               if (ret) {
+                       blk_mq_end_request(req, BLK_STS_IOERR);
+                       return;

Does this leak the ublk fetch request? Seems like it should be completed to the ublk server with an error code.

Quoted 20 lines
drivers/block/ublk_drv.c +8 -1
+               }
+               io->flags |= UBLK_IO_FLAG_UNREG_BUF;
+       } else {
+               ublk_init_req_ref(ubq, req, 1);
+       }
+
        ubq_complete_io_cmd(io, UBLK_IO_RES_OK, issue_flags);
 }
 
@@ -1593,7 +1636,8 @@ static int ublk_ch_mmap(struct file *filp, struct vm_area_struct *vma)
 }
 
 static void ublk_commit_completion(struct ublk_device *ub,
-               const struct ublksrv_io_cmd *ub_cmd)
+               const struct ublksrv_io_cmd *ub_cmd,
+               unsigned int issue_flags)
 {
        u32 qid = ub_cmd->q_id, tag = ub_cmd->tag;
        struct ublk_queue *ubq = ublk_get_queue(ub, qid);

drivers/block/ublk_drv.c static void ublk_commit_completion(struct ublk_device *ub, · line 1656

Quoted 9 lines
drivers/block/ublk_drv.c +6 -0
@@ -1604,6 +1648,15 @@ static void ublk_commit_completion(struct ublk_device *ub,
        io->flags &= ~UBLK_IO_FLAG_OWNED_BY_SRV;
        io->res = ub_cmd->result;
 
+       if (io->flags & UBLK_IO_FLAG_UNREG_BUF) {
+               int ret;
+
+               ret = io_buffer_unregister_bvec(io->cmd, tag, issue_flags);
+               if (ret)
+                       io->res = ret;

I think it would be confusing to report an error to the application submitting the I/O if unregistration fails. The only scenario where it seems possible for this to fail is if userspace issues an IORING_REGISTER_BUFFERS_UPDATE that overwrites the registered buffer slot while the ublk request is being handled by the ublk server. I would either ignore any error from io_buffer_unregister_bvec() or return it to the ublk server.

Quoted 69 lines
drivers/block/ublk_drv.c +11 -10
+               io->flags &= ~UBLK_IO_FLAG_UNREG_BUF;
+       }
+
        /* find the io request and complete */
        req = blk_mq_tag_to_rq(ub->tag_set.tags[qid], tag);
        if (WARN_ON_ONCE(unlikely(!req)))
@@ -1942,14 +1995,6 @@ static inline void ublk_prep_cancel(struct io_uring_cmd *cmd,
        io_uring_cmd_mark_cancelable(cmd, issue_flags);
 }
 
-static void ublk_io_release(void *priv)
-{
-       struct request *rq = priv;
-       struct ublk_queue *ubq = rq->mq_hctx->driver_data;
-
-       ublk_put_req_ref(ubq, rq);
-}
-
 static int ublk_register_io_buf(struct io_uring_cmd *cmd,
                                struct ublk_queue *ubq, unsigned int tag,
                                unsigned int index, unsigned int issue_flags)
@@ -2124,7 +2169,7 @@ static int __ublk_ch_uring_cmd(struct io_uring_cmd *cmd,
                }
 
                ublk_fill_io_cmd(io, cmd, ub_cmd->addr);
-               ublk_commit_completion(ub, ub_cmd);
+               ublk_commit_completion(ub, ub_cmd, issue_flags);
                break;
        case UBLK_IO_NEED_GET_DATA:
                if (!(io->flags & UBLK_IO_FLAG_OWNED_BY_SRV))
@@ -2730,6 +2775,11 @@ static int ublk_ctrl_add_dev(const struct ublksrv_ctrl_cmd *header)
                return -EINVAL;
        }
 
+       /* F_AUTO_ZERO_COPY and F_SUPPORT_ZERO_COPY can't co-exist */
+       if ((info.flags & UBLK_F_AUTO_ZERO_COPY) &&
+                       (info.flags & UBLK_F_SUPPORT_ZERO_COPY))
+               return -EINVAL;
+
        /*
         * unprivileged device can't be trusted, but RECOVERY and
         * RECOVERY_REISSUE still may hang error handling, so can't
@@ -2747,7 +2797,8 @@ static int ublk_ctrl_add_dev(const struct ublksrv_ctrl_cmd *header)
                 * buffer by pwrite() to ublk char device, which can't be
                 * used for unprivileged device
                 */
-               if (info.flags & (UBLK_F_USER_COPY | UBLK_F_SUPPORT_ZERO_COPY))
+               if (info.flags & (UBLK_F_USER_COPY | UBLK_F_SUPPORT_ZERO_COPY |
+                                       UBLK_F_AUTO_ZERO_COPY))
                        return -EINVAL;
        }
 
include/uapi/linux/ublk_cmd.h +9 -0
index 583b86681c93..d8bb5e55cce7 100644
@@ -211,6 +211,26 @@
  */
 #define UBLK_F_USER_RECOVERY_FAIL_IO (1ULL << 9)
 
+/*
+ * request buffer is registered automatically before delivering this io
+ * command to ublk server, meantime it is un-registered automatically
+ * when completing this io command.
+ *
+ * request tag has to be used as the buffer index, and ublk server has to
+ * pass this IO's tag as buffer index for using the registered zero copy
+ * buffer
+ *

include/uapi/linux/ublk_cmd.h line 232

Quoted 10 lines
include/uapi/linux/ublk_cmd.h +10 -0
+ * This way avoids extra uring_cmd cost, but also simplifies backend
+ * implementation, such as, the dependency on IO_REGISTER_IO_BUF and
+ * IO_UNREGISTER_IO_BUF becomes not necessary.
+ *
+ * For using this feature, ublk server has to register buffer table
+ * in sparse way, and buffer number has to be >= ublk queue depth.
+ *
+ * This feature is preferred to UBLK_F_SUPPORT_ZERO_COPY.
+ */
+#define UBLK_F_AUTO_ZERO_COPY  (1ULL << 10)

This flag is already taken by UBLK_F_UPDATE_SIZE in commit "ublk: Add UBLK_U_CMD_UPDATE_SIZE". You may need to rebase on for-6.16/block.

Best,
Caleb

Quoted 7 lines
include/uapi/linux/ublk_cmd.h +1 -1
+
 /* device state */
 #define UBLK_S_DEV_DEAD        0
 #define UBLK_S_DEV_LIVE        1
--

2.47.0

author
#11 lore raw

Re: [PATCH 3/4] ublk: add feature UBLK_F_AUTO_ZERO_COPY

Quoted 99 lines · Caleb Sander Mateos
Quoted 6 lines · Ming Lei

UBLK_F_SUPPORT_ZERO_COPY requires ublk server to issue explicit buffer register/unregister uring_cmd for each IO, this way is not only inefficient, but also introduce dependency between buffer consumer and buffer register/ unregister uring_cmd, please see tools/testing/selftests/ublk/stripe.c in which backing file IO has to be issued one by one by IOSQE_IO_LINK.

This is a great idea!

Quoted 3 lines

Add feature UBLK_F_AUTO_ZERO_COPY for addressing the existed zero copy limit:

nit: "existed" -> "existing", "limit" -> "limitation"

Quoted 83 lines

- register request buffer automatically before delivering io command to ublk server

- unregister request buffer automatically when completing the request

- io_uring will unregister the buffer automatically when uring is exiting, so we needn't worry about accident exit

---
 drivers/block/ublk_drv.c      | 87 +++++++++++++++++++++++++++--------
 include/uapi/linux/ublk_cmd.h | 20 ++++++++
 2 files changed, 89 insertions(+), 18 deletions(-)
drivers/block/ublk_drv.c +22 -3
index 347790b3a633..453767f91431 100644
@@ -64,7 +64,8 @@
                | UBLK_F_CMD_IOCTL_ENCODE \
                | UBLK_F_USER_COPY \
                | UBLK_F_ZONED \
-               | UBLK_F_USER_RECOVERY_FAIL_IO)
+               | UBLK_F_USER_RECOVERY_FAIL_IO \
+               | UBLK_F_AUTO_ZERO_COPY)
 
 #define UBLK_F_ALL_RECOVERY_FLAGS (UBLK_F_USER_RECOVERY \
                | UBLK_F_USER_RECOVERY_REISSUE \
@@ -131,6 +132,14 @@ struct ublk_uring_cmd_pdu {
  */
 #define UBLK_IO_FLAG_NEED_GET_DATA 0x08
 
+/*
+ * request buffer is registered automatically, so we have to unregister it
+ * before completing this request.
+ *
+ * io_uring will unregister buffer automatically for us during exiting.
+ */
+#define UBLK_IO_FLAG_UNREG_BUF         0x10
+
 /* atomic RW with ubq->cancel_lock */
 #define UBLK_IO_FLAG_CANCELED  0x80000000
 
@@ -207,7 +216,8 @@ static inline struct ublksrv_io_desc *ublk_get_iod(struct ublk_queue *ubq,
                                                   int tag);
 static inline bool ublk_dev_is_user_copy(const struct ublk_device *ub)
 {
-       return ub->dev_info.flags & (UBLK_F_USER_COPY | UBLK_F_SUPPORT_ZERO_COPY);
+       return ub->dev_info.flags & (UBLK_F_USER_COPY |
+                       UBLK_F_SUPPORT_ZERO_COPY | UBLK_F_AUTO_ZERO_COPY);
 }
 
 static inline bool ublk_dev_is_zoned(const struct ublk_device *ub)
@@ -614,6 +624,11 @@ static inline bool ublk_support_zero_copy(const struct ublk_queue *ubq)
        return ubq->flags & UBLK_F_SUPPORT_ZERO_COPY;
 }
 
+static inline bool ublk_support_auto_zc(const struct ublk_queue *ubq)
+{
+       return ubq->flags & UBLK_F_AUTO_ZERO_COPY;
+}
+
 static inline bool ublk_support_user_copy(const struct ublk_queue *ubq)
 {
        return ubq->flags & (UBLK_F_USER_COPY | UBLK_F_SUPPORT_ZERO_COPY);
@@ -621,7 +636,7 @@ static inline bool ublk_support_user_copy(const struct ublk_queue *ubq)
 
 static inline bool ublk_need_map_io(const struct ublk_queue *ubq)
 {
-       return !ublk_support_user_copy(ubq);
+       return !ublk_support_user_copy(ubq) && !ublk_support_auto_zc(ubq);
 }
 
 static inline bool ublk_need_req_ref(const struct ublk_queue *ubq)
@@ -629,17 +644,21 @@ static inline bool ublk_need_req_ref(const struct ublk_queue *ubq)
        /*
         * read()/write() is involved in user copy, so request reference
         * has to be grabbed
+        *
+        * For auto zc, ublk server still may issue UBLK_IO_COMMIT_AND_FETCH_REQ
+        * before one registered buffer is used up, so reference is
+        * required too.
Quoted 8 lines

drivers/block/ublk_drv.c static inline bool ublk_need_req_ref(const struct ublk_queue *ubq) · line 652

Quoted 3 lines
drivers/block/ublk_drv.c +1 -1
         */
-       return ublk_support_user_copy(ubq);
+       return ublk_support_user_copy(ubq) || ublk_support_auto_zc(ubq);

Since both places where ublk_support_user_copy() is used are being adjusted to also check ublk_support_auto_zc(), maybe ublk_support_user_copy() should just be changed to check UBLK_F_AUTO_ZERO_COPY too?

I think that isn't good, we should have let each flag to cover its feature only.

That said it was not good to add zero copy check in both ublk_support_user_copy() and ublk_dev_is_user_copy().

If ublk server needs user copy, it should have enabled it explicitly.

Quoted 10 lines
Quoted 10 lines
drivers/block/ublk_drv.c +1 -1
 }
 
 static inline void ublk_init_req_ref(const struct ublk_queue *ubq,
-               struct request *req)
+               struct request *req, int init_ref)
 {
        if (ublk_need_req_ref(ubq)) {
                struct ublk_rq_data *data = blk_mq_rq_to_pdu(req);
 
Quoted 6 lines

drivers/block/ublk_drv.c static inline bool ublk_need_req_ref(const struct ublk_queue *ubq) · line 661

Quoted 2 lines
drivers/block/ublk_drv.c +1 -1
-               kref_init(&data->ref);
+               refcount_set(&data->ref.refcount, init_ref);

It might be nicer not to mix kref and raw reference count operations. Maybe you could add a prep patch that switches from struct kref to refcount_t?

That is fine, or probably kref need to provide one variant of __kref_init(nr).

Quoted 41 lines
Quoted 22 lines
drivers/block/ublk_drv.c +9 -0
        }
 }
 
@@ -667,6 +686,15 @@ static inline void ublk_put_req_ref(const struct ublk_queue *ubq,
        }
 }
 
+/* for ublk zero copy */
+static void ublk_io_release(void *priv)
+{
+       struct request *rq = priv;
+       struct ublk_queue *ubq = rq->mq_hctx->driver_data;
+
+       ublk_put_req_ref(ubq, rq);
+}
+
 static inline bool ublk_need_get_data(const struct ublk_queue *ubq)
 {
        return ubq->flags & UBLK_F_NEED_GET_DATA;
@@ -1231,7 +1259,22 @@ static void ublk_dispatch_req(struct ublk_queue *ubq,
                        mapped_bytes >> 9;

drivers/block/ublk_drv.c static void ublk_dispatch_req(struct ublk_queue *ubq, · line 1268

Quoted 10 lines
drivers/block/ublk_drv.c +7 -1
        }
 
-       ublk_init_req_ref(ubq, req);
+       if (ublk_support_auto_zc(ubq) && ublk_rq_has_data(req)) {
+               int ret;
+
+               /* one extra reference is dropped by ublk_io_release */
+               ublk_init_req_ref(ubq, req, 2);
+               ret = io_buffer_register_bvec(io->cmd, req, ublk_io_release,
+                                             tag, issue_flags);

Using the ublk request tag as the registered buffer index may be too limiting. It would cause collisions if there are multiple ublk devices or queues handled on a single io_uring. It also requires offsetting any registered user buffers so their indices come after all the ublk ones, which may be difficult if ublk devices are added dynamically. Perhaps the UBLK_IO_FETCH_REQ operation could specify the registered buffer index to use for the request?

Quoted 6 lines

This also requires the io_uring issuing the zero-copy I/Os to be the same as the one submitting the ublk fetch requests. That would also make it difficult for us to adopt for our ublk server, which uses separate io_urings. Not sure if there is a simple way the ublk server could specify what io_uring to register the ublk zero-copy buffers with.

I think it can be done by passing io_uring fd and buffer 'index' via uring_cmd_header, there is still one u64 (->addr) left for zero copy, then the buffer's io_uring fd/fixed_fd and 'index' can be provided to io_uring core for registering buffer, this way should be flexible enough for covering any case.

Quoted 6 lines

drivers/block/ublk_drv.c static void ublk_dispatch_req(struct ublk_queue *ubq, · line 1271

Quoted 4 lines
drivers/block/ublk_drv.c +3 -0
+               if (ret) {
+                       blk_mq_end_request(req, BLK_STS_IOERR);
+                       return;

Does this leak the ublk fetch request? Seems like it should be

It won't leak ublk uring_cmd which is just for delivering ublk io command to ublk server.

Quoted 1 line

completed to the ublk server with an error code.

It could be hard for ublk server to deal with, I'd suggest to start with one simple implementation first.

It is usually one bug, and user will get notified from client's failure, then complain and ublk server gets fixed.

Quoted 24 lines
Quoted 24 lines
drivers/block/ublk_drv.c +8 -1
+               }
+               io->flags |= UBLK_IO_FLAG_UNREG_BUF;
+       } else {
+               ublk_init_req_ref(ubq, req, 1);
+       }
+
        ubq_complete_io_cmd(io, UBLK_IO_RES_OK, issue_flags);
 }
 
@@ -1593,7 +1636,8 @@ static int ublk_ch_mmap(struct file *filp, struct vm_area_struct *vma)
 }
 
 static void ublk_commit_completion(struct ublk_device *ub,
-               const struct ublksrv_io_cmd *ub_cmd)
+               const struct ublksrv_io_cmd *ub_cmd,
+               unsigned int issue_flags)
 {
        u32 qid = ub_cmd->q_id, tag = ub_cmd->tag;
        struct ublk_queue *ubq = ublk_get_queue(ub, qid);
@@ -1604,6 +1648,15 @@ static void ublk_commit_completion(struct ublk_device *ub,
        io->flags &= ~UBLK_IO_FLAG_OWNED_BY_SRV;
        io->res = ub_cmd->result;
 
Quoted 14 lines

drivers/block/ublk_drv.c static void ublk_commit_completion(struct ublk_device *ub, · line 1656

Quoted 6 lines
drivers/block/ublk_drv.c +6 -0
+       if (io->flags & UBLK_IO_FLAG_UNREG_BUF) {
+               int ret;
+
+               ret = io_buffer_unregister_bvec(io->cmd, tag, issue_flags);
+               if (ret)
+                       io->res = ret;

I think it would be confusing to report an error to the application submitting the I/O if unregistration fails. The only scenario where it seems possible for this to fail is if userspace issues an IORING_REGISTER_BUFFERS_UPDATE that overwrites the registered buffer slot while the ublk request is being handled by the ublk server. I would either ignore any error from io_buffer_unregister_bvec() or return it to the ublk server.

Fair enough, maybe WARN_ON_ONCE() is enough.

Quoted 75 lines
Quoted 75 lines
drivers/block/ublk_drv.c +11 -10
+               io->flags &= ~UBLK_IO_FLAG_UNREG_BUF;
+       }
+
        /* find the io request and complete */
        req = blk_mq_tag_to_rq(ub->tag_set.tags[qid], tag);
        if (WARN_ON_ONCE(unlikely(!req)))
@@ -1942,14 +1995,6 @@ static inline void ublk_prep_cancel(struct io_uring_cmd *cmd,
        io_uring_cmd_mark_cancelable(cmd, issue_flags);
 }
 
-static void ublk_io_release(void *priv)
-{
-       struct request *rq = priv;
-       struct ublk_queue *ubq = rq->mq_hctx->driver_data;
-
-       ublk_put_req_ref(ubq, rq);
-}
-
 static int ublk_register_io_buf(struct io_uring_cmd *cmd,
                                struct ublk_queue *ubq, unsigned int tag,
                                unsigned int index, unsigned int issue_flags)
@@ -2124,7 +2169,7 @@ static int __ublk_ch_uring_cmd(struct io_uring_cmd *cmd,
                }
 
                ublk_fill_io_cmd(io, cmd, ub_cmd->addr);
-               ublk_commit_completion(ub, ub_cmd);
+               ublk_commit_completion(ub, ub_cmd, issue_flags);
                break;
        case UBLK_IO_NEED_GET_DATA:
                if (!(io->flags & UBLK_IO_FLAG_OWNED_BY_SRV))
@@ -2730,6 +2775,11 @@ static int ublk_ctrl_add_dev(const struct ublksrv_ctrl_cmd *header)
                return -EINVAL;
        }
 
+       /* F_AUTO_ZERO_COPY and F_SUPPORT_ZERO_COPY can't co-exist */
+       if ((info.flags & UBLK_F_AUTO_ZERO_COPY) &&
+                       (info.flags & UBLK_F_SUPPORT_ZERO_COPY))
+               return -EINVAL;
+
        /*
         * unprivileged device can't be trusted, but RECOVERY and
         * RECOVERY_REISSUE still may hang error handling, so can't
@@ -2747,7 +2797,8 @@ static int ublk_ctrl_add_dev(const struct ublksrv_ctrl_cmd *header)
                 * buffer by pwrite() to ublk char device, which can't be
                 * used for unprivileged device
                 */
-               if (info.flags & (UBLK_F_USER_COPY | UBLK_F_SUPPORT_ZERO_COPY))
+               if (info.flags & (UBLK_F_USER_COPY | UBLK_F_SUPPORT_ZERO_COPY |
+                                       UBLK_F_AUTO_ZERO_COPY))
                        return -EINVAL;
        }
 
include/uapi/linux/ublk_cmd.h +14 -0
index 583b86681c93..d8bb5e55cce7 100644
@@ -211,6 +211,26 @@
  */
 #define UBLK_F_USER_RECOVERY_FAIL_IO (1ULL << 9)
 
+/*
+ * request buffer is registered automatically before delivering this io
+ * command to ublk server, meantime it is un-registered automatically
+ * when completing this io command.
+ *
+ * request tag has to be used as the buffer index, and ublk server has to
+ * pass this IO's tag as buffer index for using the registered zero copy
+ * buffer
+ *
+ * This way avoids extra uring_cmd cost, but also simplifies backend
+ * implementation, such as, the dependency on IO_REGISTER_IO_BUF and
+ * IO_UNREGISTER_IO_BUF becomes not necessary.
+ *
+ * For using this feature, ublk server has to register buffer table
Quoted 8 lines

include/uapi/linux/ublk_cmd.h line 232

Quoted 5 lines
include/uapi/linux/ublk_cmd.h +5 -0
+ * in sparse way, and buffer number has to be >= ublk queue depth.
+ *
+ * This feature is preferred to UBLK_F_SUPPORT_ZERO_COPY.
+ */
+#define UBLK_F_AUTO_ZERO_COPY  (1ULL << 10)

This flag is already taken by UBLK_F_UPDATE_SIZE in commit "ublk: Add UBLK_U_CMD_UPDATE_SIZE". You may need to rebase on for-6.16/block.

Good catch, will fix it in V2.

Thanks,
Ming

#12 lore raw

Re: [PATCH 3/4] ublk: add feature UBLK_F_AUTO_ZERO_COPY

Quoted 110 lines · Ming Lei
Quoted 99 lines · Caleb Sander Mateos
Quoted 6 lines · Ming Lei

UBLK_F_SUPPORT_ZERO_COPY requires ublk server to issue explicit buffer register/unregister uring_cmd for each IO, this way is not only inefficient, but also introduce dependency between buffer consumer and buffer register/ unregister uring_cmd, please see tools/testing/selftests/ublk/stripe.c in which backing file IO has to be issued one by one by IOSQE_IO_LINK.

This is a great idea!

Quoted 3 lines

Add feature UBLK_F_AUTO_ZERO_COPY for addressing the existed zero copy limit:

nit: "existed" -> "existing", "limit" -> "limitation"

Quoted 83 lines

- register request buffer automatically before delivering io command to ublk server

- unregister request buffer automatically when completing the request

- io_uring will unregister the buffer automatically when uring is exiting, so we needn't worry about accident exit

---
 drivers/block/ublk_drv.c      | 87 +++++++++++++++++++++++++++--------
 include/uapi/linux/ublk_cmd.h | 20 ++++++++
 2 files changed, 89 insertions(+), 18 deletions(-)
drivers/block/ublk_drv.c +22 -3
index 347790b3a633..453767f91431 100644
@@ -64,7 +64,8 @@
                | UBLK_F_CMD_IOCTL_ENCODE \
                | UBLK_F_USER_COPY \
                | UBLK_F_ZONED \
-               | UBLK_F_USER_RECOVERY_FAIL_IO)
+               | UBLK_F_USER_RECOVERY_FAIL_IO \
+               | UBLK_F_AUTO_ZERO_COPY)
 
 #define UBLK_F_ALL_RECOVERY_FLAGS (UBLK_F_USER_RECOVERY \
                | UBLK_F_USER_RECOVERY_REISSUE \
@@ -131,6 +132,14 @@ struct ublk_uring_cmd_pdu {
  */
 #define UBLK_IO_FLAG_NEED_GET_DATA 0x08
 
+/*
+ * request buffer is registered automatically, so we have to unregister it
+ * before completing this request.
+ *
+ * io_uring will unregister buffer automatically for us during exiting.
+ */
+#define UBLK_IO_FLAG_UNREG_BUF         0x10
+
 /* atomic RW with ubq->cancel_lock */
 #define UBLK_IO_FLAG_CANCELED  0x80000000
 
@@ -207,7 +216,8 @@ static inline struct ublksrv_io_desc *ublk_get_iod(struct ublk_queue *ubq,
                                                   int tag);
 static inline bool ublk_dev_is_user_copy(const struct ublk_device *ub)
 {
-       return ub->dev_info.flags & (UBLK_F_USER_COPY | UBLK_F_SUPPORT_ZERO_COPY);
+       return ub->dev_info.flags & (UBLK_F_USER_COPY |
+                       UBLK_F_SUPPORT_ZERO_COPY | UBLK_F_AUTO_ZERO_COPY);
 }
 
 static inline bool ublk_dev_is_zoned(const struct ublk_device *ub)
@@ -614,6 +624,11 @@ static inline bool ublk_support_zero_copy(const struct ublk_queue *ubq)
        return ubq->flags & UBLK_F_SUPPORT_ZERO_COPY;
 }
 
+static inline bool ublk_support_auto_zc(const struct ublk_queue *ubq)
+{
+       return ubq->flags & UBLK_F_AUTO_ZERO_COPY;
+}
+
 static inline bool ublk_support_user_copy(const struct ublk_queue *ubq)
 {
        return ubq->flags & (UBLK_F_USER_COPY | UBLK_F_SUPPORT_ZERO_COPY);
@@ -621,7 +636,7 @@ static inline bool ublk_support_user_copy(const struct ublk_queue *ubq)
 
 static inline bool ublk_need_map_io(const struct ublk_queue *ubq)
 {
-       return !ublk_support_user_copy(ubq);
+       return !ublk_support_user_copy(ubq) && !ublk_support_auto_zc(ubq);
 }
 
 static inline bool ublk_need_req_ref(const struct ublk_queue *ubq)
@@ -629,17 +644,21 @@ static inline bool ublk_need_req_ref(const struct ublk_queue *ubq)
        /*
         * read()/write() is involved in user copy, so request reference
         * has to be grabbed
+        *
+        * For auto zc, ublk server still may issue UBLK_IO_COMMIT_AND_FETCH_REQ
+        * before one registered buffer is used up, so reference is
+        * required too.
Quoted 8 lines

drivers/block/ublk_drv.c static inline bool ublk_need_req_ref(const struct ublk_queue *ubq) · line 652

Quoted 3 lines
drivers/block/ublk_drv.c +1 -1
         */
-       return ublk_support_user_copy(ubq);
+       return ublk_support_user_copy(ubq) || ublk_support_auto_zc(ubq);

Since both places where ublk_support_user_copy() is used are being adjusted to also check ublk_support_auto_zc(), maybe ublk_support_user_copy() should just be changed to check UBLK_F_AUTO_ZERO_COPY too?

Quoted 4 lines

I think that isn't good, we should have let each flag to cover its feature only.

That said it was not good to add zero copy check in both ublk_support_user_copy() and ublk_dev_is_user_copy().

Fair point.

Quoted 77 lines

If ublk server needs user copy, it should have enabled it explicitly.

Quoted 10 lines
Quoted 10 lines
drivers/block/ublk_drv.c +1 -1
 }
 
 static inline void ublk_init_req_ref(const struct ublk_queue *ubq,
-               struct request *req)
+               struct request *req, int init_ref)
 {
        if (ublk_need_req_ref(ubq)) {
                struct ublk_rq_data *data = blk_mq_rq_to_pdu(req);
 
Quoted 6 lines

drivers/block/ublk_drv.c static inline bool ublk_need_req_ref(const struct ublk_queue *ubq) · line 661

Quoted 2 lines
drivers/block/ublk_drv.c +1 -1
-               kref_init(&data->ref);
+               refcount_set(&data->ref.refcount, init_ref);

It might be nicer not to mix kref and raw reference count operations. Maybe you could add a prep patch that switches from struct kref to refcount_t?

That is fine, or probably kref need to provide one variant of __kref_init(nr).

Quoted 41 lines
Quoted 22 lines
drivers/block/ublk_drv.c +9 -0
        }
 }
 
@@ -667,6 +686,15 @@ static inline void ublk_put_req_ref(const struct ublk_queue *ubq,
        }
 }
 
+/* for ublk zero copy */
+static void ublk_io_release(void *priv)
+{
+       struct request *rq = priv;
+       struct ublk_queue *ubq = rq->mq_hctx->driver_data;
+
+       ublk_put_req_ref(ubq, rq);
+}
+
 static inline bool ublk_need_get_data(const struct ublk_queue *ubq)
 {
        return ubq->flags & UBLK_F_NEED_GET_DATA;
@@ -1231,7 +1259,22 @@ static void ublk_dispatch_req(struct ublk_queue *ubq,
                        mapped_bytes >> 9;

drivers/block/ublk_drv.c static void ublk_dispatch_req(struct ublk_queue *ubq, · line 1268

Quoted 10 lines
drivers/block/ublk_drv.c +7 -1
        }
 
-       ublk_init_req_ref(ubq, req);
+       if (ublk_support_auto_zc(ubq) && ublk_rq_has_data(req)) {
+               int ret;
+
+               /* one extra reference is dropped by ublk_io_release */
+               ublk_init_req_ref(ubq, req, 2);
+               ret = io_buffer_register_bvec(io->cmd, req, ublk_io_release,
+                                             tag, issue_flags);

Using the ublk request tag as the registered buffer index may be too limiting. It would cause collisions if there are multiple ublk devices or queues handled on a single io_uring. It also requires offsetting any registered user buffers so their indices come after all the ublk ones, which may be difficult if ublk devices are added dynamically. Perhaps the UBLK_IO_FETCH_REQ operation could specify the registered buffer index to use for the request?

Quoted 6 lines

This also requires the io_uring issuing the zero-copy I/Os to be the same as the one submitting the ublk fetch requests. That would also make it difficult for us to adopt for our ublk server, which uses separate io_urings. Not sure if there is a simple way the ublk server could specify what io_uring to register the ublk zero-copy buffers with.

I think it can be done by passing io_uring fd and buffer 'index' via uring_cmd_header, there is still one u64 (->addr) left for zero copy, then the buffer's io_uring fd/fixed_fd and 'index' can be provided to io_uring core for registering buffer, this way should be flexible enough for covering any case.

Quoted 1 line
Quoted 8 lines
Quoted 5 lines

drivers/block/ublk_drv.c static void ublk_dispatch_req(struct ublk_queue *ubq, · line 1271

Quoted 3 lines
drivers/block/ublk_drv.c +3 -0
+               if (ret) {
+                       blk_mq_end_request(req, BLK_STS_IOERR);
+                       return;

Does this leak the ublk fetch request? Seems like it should be

It won't leak ublk uring_cmd which is just for delivering ublk io command to ublk server.

But where does the uring_cmd complete? The early return means this will skip the call to ubq_complete_io_cmd(). Won't that leave the uring_cmd stuck until the io_uring exits?

Quoted 48 lines
Quoted 2 lines

completed to the ublk server with an error code.

It could be hard for ublk server to deal with, I'd suggest to start with one simple implementation first.

It is usually one bug, and user will get notified from client's failure, then complain and ublk server gets fixed.

Quoted 24 lines
Quoted 24 lines
drivers/block/ublk_drv.c +8 -1
+               }
+               io->flags |= UBLK_IO_FLAG_UNREG_BUF;
+       } else {
+               ublk_init_req_ref(ubq, req, 1);
+       }
+
        ubq_complete_io_cmd(io, UBLK_IO_RES_OK, issue_flags);
 }
 
@@ -1593,7 +1636,8 @@ static int ublk_ch_mmap(struct file *filp, struct vm_area_struct *vma)
 }
 
 static void ublk_commit_completion(struct ublk_device *ub,
-               const struct ublksrv_io_cmd *ub_cmd)
+               const struct ublksrv_io_cmd *ub_cmd,
+               unsigned int issue_flags)
 {
        u32 qid = ub_cmd->q_id, tag = ub_cmd->tag;
        struct ublk_queue *ubq = ublk_get_queue(ub, qid);
@@ -1604,6 +1648,15 @@ static void ublk_commit_completion(struct ublk_device *ub,
        io->flags &= ~UBLK_IO_FLAG_OWNED_BY_SRV;
        io->res = ub_cmd->result;
 
Quoted 14 lines

drivers/block/ublk_drv.c static void ublk_commit_completion(struct ublk_device *ub, · line 1656

Quoted 6 lines
drivers/block/ublk_drv.c +6 -0
+       if (io->flags & UBLK_IO_FLAG_UNREG_BUF) {
+               int ret;
+
+               ret = io_buffer_unregister_bvec(io->cmd, tag, issue_flags);
+               if (ret)
+                       io->res = ret;

I think it would be confusing to report an error to the application submitting the I/O if unregistration fails. The only scenario where it seems possible for this to fail is if userspace issues an IORING_REGISTER_BUFFERS_UPDATE that overwrites the registered buffer slot while the ublk request is being handled by the ublk server. I would either ignore any error from io_buffer_unregister_bvec() or return it to the ublk server.

Quoted 1 line

Fair enough, maybe WARN_ON_ONCE() is enough.

I think it's technically triggerable from userspace, but certainly very unexpected.

Best,
Caleb

Quoted 91 lines
Quoted 76 lines
Quoted 76 lines
drivers/block/ublk_drv.c +11 -10
+               io->flags &= ~UBLK_IO_FLAG_UNREG_BUF;
+       }
+
        /* find the io request and complete */
        req = blk_mq_tag_to_rq(ub->tag_set.tags[qid], tag);
        if (WARN_ON_ONCE(unlikely(!req)))
@@ -1942,14 +1995,6 @@ static inline void ublk_prep_cancel(struct io_uring_cmd *cmd,
        io_uring_cmd_mark_cancelable(cmd, issue_flags);
 }
 
-static void ublk_io_release(void *priv)
-{
-       struct request *rq = priv;
-       struct ublk_queue *ubq = rq->mq_hctx->driver_data;
-
-       ublk_put_req_ref(ubq, rq);
-}
-
 static int ublk_register_io_buf(struct io_uring_cmd *cmd,
                                struct ublk_queue *ubq, unsigned int tag,
                                unsigned int index, unsigned int issue_flags)
@@ -2124,7 +2169,7 @@ static int __ublk_ch_uring_cmd(struct io_uring_cmd *cmd,
                }
 
                ublk_fill_io_cmd(io, cmd, ub_cmd->addr);
-               ublk_commit_completion(ub, ub_cmd);
+               ublk_commit_completion(ub, ub_cmd, issue_flags);
                break;
        case UBLK_IO_NEED_GET_DATA:
                if (!(io->flags & UBLK_IO_FLAG_OWNED_BY_SRV))
@@ -2730,6 +2775,11 @@ static int ublk_ctrl_add_dev(const struct ublksrv_ctrl_cmd *header)
                return -EINVAL;
        }
 
+       /* F_AUTO_ZERO_COPY and F_SUPPORT_ZERO_COPY can't co-exist */
+       if ((info.flags & UBLK_F_AUTO_ZERO_COPY) &&
+                       (info.flags & UBLK_F_SUPPORT_ZERO_COPY))
+               return -EINVAL;
+
        /*
         * unprivileged device can't be trusted, but RECOVERY and
         * RECOVERY_REISSUE still may hang error handling, so can't
@@ -2747,7 +2797,8 @@ static int ublk_ctrl_add_dev(const struct ublksrv_ctrl_cmd *header)
                 * buffer by pwrite() to ublk char device, which can't be
                 * used for unprivileged device
                 */
-               if (info.flags & (UBLK_F_USER_COPY | UBLK_F_SUPPORT_ZERO_COPY))
+               if (info.flags & (UBLK_F_USER_COPY | UBLK_F_SUPPORT_ZERO_COPY |
+                                       UBLK_F_AUTO_ZERO_COPY))
                        return -EINVAL;
        }
 
include/uapi/linux/ublk_cmd.h +14 -0
index 583b86681c93..d8bb5e55cce7 100644
@@ -211,6 +211,26 @@
  */
 #define UBLK_F_USER_RECOVERY_FAIL_IO (1ULL << 9)
 
+/*
+ * request buffer is registered automatically before delivering this io
+ * command to ublk server, meantime it is un-registered automatically
+ * when completing this io command.
+ *
+ * request tag has to be used as the buffer index, and ublk server has to
+ * pass this IO's tag as buffer index for using the registered zero copy
+ * buffer
+ *
+ * This way avoids extra uring_cmd cost, but also simplifies backend
+ * implementation, such as, the dependency on IO_REGISTER_IO_BUF and
+ * IO_UNREGISTER_IO_BUF becomes not necessary.
+ *
+ * For using this feature, ublk server has to register buffer table
Quoted 8 lines

include/uapi/linux/ublk_cmd.h line 232

Quoted 5 lines
include/uapi/linux/ublk_cmd.h +5 -0
+ * in sparse way, and buffer number has to be >= ublk queue depth.
+ *
+ * This feature is preferred to UBLK_F_SUPPORT_ZERO_COPY.
+ */
+#define UBLK_F_AUTO_ZERO_COPY  (1ULL << 10)

This flag is already taken by UBLK_F_UPDATE_SIZE in commit "ublk: Add UBLK_U_CMD_UPDATE_SIZE". You may need to rebase on for-6.16/block.

Good catch, will fix it in V2.

Thanks,
Ming

author
#13 lore raw

Re: [PATCH 3/4] ublk: add feature UBLK_F_AUTO_ZERO_COPY

Quoted 199 lines · Caleb Sander Mateos
Quoted 110 lines · Ming Lei
Quoted 99 lines · Caleb Sander Mateos
Quoted 6 lines · Ming Lei

UBLK_F_SUPPORT_ZERO_COPY requires ublk server to issue explicit buffer register/unregister uring_cmd for each IO, this way is not only inefficient, but also introduce dependency between buffer consumer and buffer register/ unregister uring_cmd, please see tools/testing/selftests/ublk/stripe.c in which backing file IO has to be issued one by one by IOSQE_IO_LINK.

This is a great idea!

Quoted 3 lines

Add feature UBLK_F_AUTO_ZERO_COPY for addressing the existed zero copy limit:

nit: "existed" -> "existing", "limit" -> "limitation"

Quoted 83 lines

- register request buffer automatically before delivering io command to ublk server

- unregister request buffer automatically when completing the request

- io_uring will unregister the buffer automatically when uring is exiting, so we needn't worry about accident exit

---
 drivers/block/ublk_drv.c      | 87 +++++++++++++++++++++++++++--------
 include/uapi/linux/ublk_cmd.h | 20 ++++++++
 2 files changed, 89 insertions(+), 18 deletions(-)
drivers/block/ublk_drv.c +22 -3
index 347790b3a633..453767f91431 100644
@@ -64,7 +64,8 @@
                | UBLK_F_CMD_IOCTL_ENCODE \
                | UBLK_F_USER_COPY \
                | UBLK_F_ZONED \
-               | UBLK_F_USER_RECOVERY_FAIL_IO)
+               | UBLK_F_USER_RECOVERY_FAIL_IO \
+               | UBLK_F_AUTO_ZERO_COPY)
 
 #define UBLK_F_ALL_RECOVERY_FLAGS (UBLK_F_USER_RECOVERY \
                | UBLK_F_USER_RECOVERY_REISSUE \
@@ -131,6 +132,14 @@ struct ublk_uring_cmd_pdu {
  */
 #define UBLK_IO_FLAG_NEED_GET_DATA 0x08
 
+/*
+ * request buffer is registered automatically, so we have to unregister it
+ * before completing this request.
+ *
+ * io_uring will unregister buffer automatically for us during exiting.
+ */
+#define UBLK_IO_FLAG_UNREG_BUF         0x10
+
 /* atomic RW with ubq->cancel_lock */
 #define UBLK_IO_FLAG_CANCELED  0x80000000
 
@@ -207,7 +216,8 @@ static inline struct ublksrv_io_desc *ublk_get_iod(struct ublk_queue *ubq,
                                                   int tag);
 static inline bool ublk_dev_is_user_copy(const struct ublk_device *ub)
 {
-       return ub->dev_info.flags & (UBLK_F_USER_COPY | UBLK_F_SUPPORT_ZERO_COPY);
+       return ub->dev_info.flags & (UBLK_F_USER_COPY |
+                       UBLK_F_SUPPORT_ZERO_COPY | UBLK_F_AUTO_ZERO_COPY);
 }
 
 static inline bool ublk_dev_is_zoned(const struct ublk_device *ub)
@@ -614,6 +624,11 @@ static inline bool ublk_support_zero_copy(const struct ublk_queue *ubq)
        return ubq->flags & UBLK_F_SUPPORT_ZERO_COPY;
 }
 
+static inline bool ublk_support_auto_zc(const struct ublk_queue *ubq)
+{
+       return ubq->flags & UBLK_F_AUTO_ZERO_COPY;
+}
+
 static inline bool ublk_support_user_copy(const struct ublk_queue *ubq)
 {
        return ubq->flags & (UBLK_F_USER_COPY | UBLK_F_SUPPORT_ZERO_COPY);
@@ -621,7 +636,7 @@ static inline bool ublk_support_user_copy(const struct ublk_queue *ubq)
 
 static inline bool ublk_need_map_io(const struct ublk_queue *ubq)
 {
-       return !ublk_support_user_copy(ubq);
+       return !ublk_support_user_copy(ubq) && !ublk_support_auto_zc(ubq);
 }
 
 static inline bool ublk_need_req_ref(const struct ublk_queue *ubq)
@@ -629,17 +644,21 @@ static inline bool ublk_need_req_ref(const struct ublk_queue *ubq)
        /*
         * read()/write() is involved in user copy, so request reference
         * has to be grabbed
+        *
+        * For auto zc, ublk server still may issue UBLK_IO_COMMIT_AND_FETCH_REQ
+        * before one registered buffer is used up, so reference is
+        * required too.
Quoted 8 lines

drivers/block/ublk_drv.c static inline bool ublk_need_req_ref(const struct ublk_queue *ubq) · line 652

Quoted 3 lines
drivers/block/ublk_drv.c +1 -1
         */
-       return ublk_support_user_copy(ubq);
+       return ublk_support_user_copy(ubq) || ublk_support_auto_zc(ubq);

Since both places where ublk_support_user_copy() is used are being adjusted to also check ublk_support_auto_zc(), maybe ublk_support_user_copy() should just be changed to check UBLK_F_AUTO_ZERO_COPY too?

Quoted 4 lines

I think that isn't good, we should have let each flag to cover its feature only.

That said it was not good to add zero copy check in both ublk_support_user_copy() and ublk_dev_is_user_copy().

Fair point.

Quoted 81 lines

If ublk server needs user copy, it should have enabled it explicitly.

Quoted 10 lines
Quoted 10 lines
drivers/block/ublk_drv.c +1 -1
 }
 
 static inline void ublk_init_req_ref(const struct ublk_queue *ubq,
-               struct request *req)
+               struct request *req, int init_ref)
 {
        if (ublk_need_req_ref(ubq)) {
                struct ublk_rq_data *data = blk_mq_rq_to_pdu(req);
 
Quoted 6 lines

drivers/block/ublk_drv.c static inline bool ublk_need_req_ref(const struct ublk_queue *ubq) · line 661

Quoted 2 lines
drivers/block/ublk_drv.c +1 -1
-               kref_init(&data->ref);
+               refcount_set(&data->ref.refcount, init_ref);

It might be nicer not to mix kref and raw reference count operations. Maybe you could add a prep patch that switches from struct kref to refcount_t?

That is fine, or probably kref need to provide one variant of __kref_init(nr).

Quoted 41 lines
Quoted 22 lines
drivers/block/ublk_drv.c +9 -0
        }
 }
 
@@ -667,6 +686,15 @@ static inline void ublk_put_req_ref(const struct ublk_queue *ubq,
        }
 }
 
+/* for ublk zero copy */
+static void ublk_io_release(void *priv)
+{
+       struct request *rq = priv;
+       struct ublk_queue *ubq = rq->mq_hctx->driver_data;
+
+       ublk_put_req_ref(ubq, rq);
+}
+
 static inline bool ublk_need_get_data(const struct ublk_queue *ubq)
 {
        return ubq->flags & UBLK_F_NEED_GET_DATA;
@@ -1231,7 +1259,22 @@ static void ublk_dispatch_req(struct ublk_queue *ubq,
                        mapped_bytes >> 9;

drivers/block/ublk_drv.c static void ublk_dispatch_req(struct ublk_queue *ubq, · line 1268

Quoted 10 lines
drivers/block/ublk_drv.c +7 -1
        }
 
-       ublk_init_req_ref(ubq, req);
+       if (ublk_support_auto_zc(ubq) && ublk_rq_has_data(req)) {
+               int ret;
+
+               /* one extra reference is dropped by ublk_io_release */
+               ublk_init_req_ref(ubq, req, 2);
+               ret = io_buffer_register_bvec(io->cmd, req, ublk_io_release,
+                                             tag, issue_flags);

Using the ublk request tag as the registered buffer index may be too limiting. It would cause collisions if there are multiple ublk devices or queues handled on a single io_uring. It also requires offsetting any registered user buffers so their indices come after all the ublk ones, which may be difficult if ublk devices are added dynamically. Perhaps the UBLK_IO_FETCH_REQ operation could specify the registered buffer index to use for the request?

Quoted 6 lines

This also requires the io_uring issuing the zero-copy I/Os to be the same as the one submitting the ublk fetch requests. That would also make it difficult for us to adopt for our ublk server, which uses separate io_urings. Not sure if there is a simple way the ublk server could specify what io_uring to register the ublk zero-copy buffers with.

I think it can be done by passing io_uring fd and buffer 'index' via uring_cmd_header, there is still one u64 (->addr) left for zero copy, then the buffer's io_uring fd/fixed_fd and 'index' can be provided to io_uring core for registering buffer, this way should be flexible enough for covering any case.

Quoted 5 lines
Quoted 4 lines
drivers/block/ublk_drv.c +3 -0
+               if (ret) {
+                       blk_mq_end_request(req, BLK_STS_IOERR);
+                       return;
Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Does this leak the ublk fetch request? Seems like it should be

It won't leak ublk uring_cmd which is just for delivering ublk io command to ublk server.

But where does the uring_cmd complete? The early return means this will skip the call to ubq_complete_io_cmd(). Won't that leave the uring_cmd stuck until the io_uring exits?

It is like nothing coming from /dev/ublkbN, we always submitted one uring_cmd beforehand, when no one knows there will be request available now or future.

Quoted 49 lines
Quoted 49 lines
Quoted 3 lines

completed to the ublk server with an error code.

It could be hard for ublk server to deal with, I'd suggest to start with one simple implementation first.

It is usually one bug, and user will get notified from client's failure, then complain and ublk server gets fixed.

Quoted 38 lines
Quoted 21 lines
drivers/block/ublk_drv.c +8 -1
+               }
+               io->flags |= UBLK_IO_FLAG_UNREG_BUF;
+       } else {
+               ublk_init_req_ref(ubq, req, 1);
+       }
+
        ubq_complete_io_cmd(io, UBLK_IO_RES_OK, issue_flags);
 }
 
@@ -1593,7 +1636,8 @@ static int ublk_ch_mmap(struct file *filp, struct vm_area_struct *vma)
 }
 
 static void ublk_commit_completion(struct ublk_device *ub,
-               const struct ublksrv_io_cmd *ub_cmd)
+               const struct ublksrv_io_cmd *ub_cmd,
+               unsigned int issue_flags)
 {
        u32 qid = ub_cmd->q_id, tag = ub_cmd->tag;
        struct ublk_queue *ubq = ublk_get_queue(ub, qid);

drivers/block/ublk_drv.c static void ublk_commit_completion(struct ublk_device *ub, · line 1656

Quoted 9 lines
drivers/block/ublk_drv.c +6 -0
@@ -1604,6 +1648,15 @@ static void ublk_commit_completion(struct ublk_device *ub,
        io->flags &= ~UBLK_IO_FLAG_OWNED_BY_SRV;
        io->res = ub_cmd->result;
 
+       if (io->flags & UBLK_IO_FLAG_UNREG_BUF) {
+               int ret;
+
+               ret = io_buffer_unregister_bvec(io->cmd, tag, issue_flags);
+               if (ret)
+                       io->res = ret;

I think it would be confusing to report an error to the application submitting the I/O if unregistration fails. The only scenario where it seems possible for this to fail is if userspace issues an IORING_REGISTER_BUFFERS_UPDATE that overwrites the registered buffer slot while the ublk request is being handled by the ublk server. I would either ignore any error from io_buffer_unregister_bvec() or return it to the ublk server.

Quoted 4 lines
Quoted 1 line

Fair enough, maybe WARN_ON_ONCE() is enough.

I think it's technically triggerable from userspace, but certainly very unexpected.

Yeah, I agree.

Thanks,
Ming

#14 lore raw

Re: [PATCH 3/4] ublk: add feature UBLK_F_AUTO_ZERO_COPY

In reply to Caleb Sander Mateos
Quoted 9 lines · Caleb Sander Mateos
Quoted 6 lines · Ming Lei

UBLK_F_SUPPORT_ZERO_COPY requires ublk server to issue explicit buffer register/unregister uring_cmd for each IO, this way is not only inefficient, but also introduce dependency between buffer consumer and buffer register/ unregister uring_cmd, please see tools/testing/selftests/ublk/stripe.c in which backing file IO has to be issued one by one by IOSQE_IO_LINK.

This is a great idea!

This is very similiar to something I proposed off-list, and the feedback back then was this won't work because the back-end ring that wants to use the zero-copy buffer isn't the same as the ublk server ring recieving notification of a new command; the ublk driver has no idea which uring to register the bvec with. Also, this is using the request "tag" as the io_uring buf index, which wouldn't work when the ublk server ring handles multiple ublk devices due to the tag collisions.

If you're can make those trade-offs, then this is a great simplification to the whole thing.

author
#15 lore raw

Re: [PATCH 3/4] ublk: add feature UBLK_F_AUTO_ZERO_COPY

Quoted 9 lines · Keith Busch
Quoted 8 lines · Caleb Sander Mateos
Quoted 6 lines · Ming Lei

UBLK_F_SUPPORT_ZERO_COPY requires ublk server to issue explicit buffer register/unregister uring_cmd for each IO, this way is not only inefficient, but also introduce dependency between buffer consumer and buffer register/ unregister uring_cmd, please see tools/testing/selftests/ublk/stripe.c in which backing file IO has to be issued one by one by IOSQE_IO_LINK.

Quoted 3 lines
Quoted 1 line

This is a great idea!

This is very similiar to something I proposed off-list, and the feedback

Looks we both think of it, :-)

Quoted 9 lines

back then was this won't work because the back-end ring that wants to use the zero-copy buffer isn't the same as the ublk server ring recieving notification of a new command; the ublk driver has no idea which uring to register the bvec with. Also, this is using the request "tag" as the io_uring buf index, which wouldn't work when the ublk server ring handles multiple ublk devices due to the tag collisions.

If you're can make those trade-offs, then this is a great simplification to the whole thing.

The io_uring fd & buffer index can be provided from 'ublksrv_io_cmd'.

https://lore.kernel.org/linux-block/aA2RNG3-WzuQqEN6@fedora/

If we only support IORING_ENTER_REGISTERED_RING, 32bit is enough for io_uring fd & buffer index, and there is still 64bits available if not taking UBLK_F_ZONED into account.

Thanks,
Ming

#16 lore raw

Re: [PATCH 3/4] ublk: add feature UBLK_F_AUTO_ZERO_COPY

Quoted 5 lines · Ming Lei
Quoted 2 lines · Keith Busch

This is very similiar to something I proposed off-list, and the feedback

Looks we both think of it, :-)

Yeah, for real. I was a bit dismayed when I learned of such use cases. So much simplicity and elegance went away...

Quoted 10 lines
Quoted 9 lines

back then was this won't work because the back-end ring that wants to use the zero-copy buffer isn't the same as the ublk server ring recieving notification of a new command; the ublk driver has no idea which uring to register the bvec with. Also, this is using the request "tag" as the io_uring buf index, which wouldn't work when the ublk server ring handles multiple ublk devices due to the tag collisions.

If you're can make those trade-offs, then this is a great simplification to the whole thing.

Quoted 7 lines

The io_uring fd & buffer index can be provided from 'ublksrv_io_cmd'.

https://lore.kernel.org/linux-block/aA2RNG3-WzuQqEN6@fedora/

If we only support IORING_ENTER_REGISTERED_RING, 32bit is enough for io_uring fd & buffer index, and there is still 64bits available if not taking UBLK_F_ZONED into account.

We still need a registered sparse table for the backend ring. I think maybe a simple ida from the ublk driver to select an index may let the daemon register something reasonably small.

author
#17 lore raw

Re: [PATCH 3/4] ublk: add feature UBLK_F_AUTO_ZERO_COPY

Quoted 9 lines · Keith Busch
Quoted 5 lines · Ming Lei
Quoted 2 lines · Keith Busch

This is very similiar to something I proposed off-list, and the feedback

Looks we both think of it, :-)

Yeah, for real. I was a bit dismayed when I learned of such use cases. So much simplicity and elegance went away...

That is reality, and probably these use cases may be addressed elegantly too in future...

Quoted 15 lines
Quoted 15 lines
Quoted 10 lines

back then was this won't work because the back-end ring that wants to use the zero-copy buffer isn't the same as the ublk server ring recieving notification of a new command; the ublk driver has no idea which uring to register the bvec with. Also, this is using the request "tag" as the io_uring buf index, which wouldn't work when the ublk server ring handles multiple ublk devices due to the tag collisions.

If you're can make those trade-offs, then this is a great simplification to the whole thing.

The io_uring fd & buffer index can be provided from 'ublksrv_io_cmd'.

https://lore.kernel.org/linux-block/aA2RNG3-WzuQqEN6@fedora/

Quoted 7 lines
Quoted 3 lines

If we only support IORING_ENTER_REGISTERED_RING, 32bit is enough for io_uring fd & buffer index, and there is still 64bits available if not taking UBLK_F_ZONED into account.

We still need a registered sparse table for the backend ring. I think maybe a simple ida from the ublk driver to select an index may let the daemon register something reasonably small.

Yeah, I think it is reasonable to let userspace register the sparse table, and we can document it in UAPI.

thanks,
Ming

author
#18 lore raw

PATCH 4/4 selftests: ublk: support UBLK_F_AUTO_ZERO_COPY

In reply to Ming Lei

Enable UBLK_F_AUTO_ZERO_COPY support for ublk utility by argument --auto_zc, meantime support this feature in null, loop and stripe target code.

Add function test generic_08 for covering basic UBLK_F_AUTO_ZERO_COPY feature.

Also cover UBLK_F_AUTO_ZERO_COPY in stress_03, stress_04 and stress_05 test too.

'fio/t/io_uring -p0 /dev/ublkb0' shows that F_AUTO_ZERO_COPY can improve IOPS by 50% compared with F_SUPPORT_ZERO_COPY in my test VM.

---
 tools/testing/selftests/ublk/Makefile         |  2 +
 tools/testing/selftests/ublk/file_backed.c    |  9 ++--
 tools/testing/selftests/ublk/kublk.c          | 13 ++++--
 tools/testing/selftests/ublk/kublk.h          |  6 +++
 tools/testing/selftests/ublk/null.c           | 43 ++++++++++++++-----
 tools/testing/selftests/ublk/stripe.c         | 14 +++---
 .../testing/selftests/ublk/test_generic_08.sh | 28 ++++++++++++
 .../testing/selftests/ublk/test_stress_03.sh  |  6 +++
 .../testing/selftests/ublk/test_stress_04.sh  |  6 +++
 .../testing/selftests/ublk/test_stress_05.sh  |  8 ++++
 10 files changed, 111 insertions(+), 24 deletions(-)
 create mode 100755 tools/testing/selftests/ublk/test_generic_08.sh
tools/testing/selftests/ublk/Makefile +2 -0
index f34ac0bac696..8475716f407b 100644
@@ -11,6 +11,8 @@ TEST_PROGS += test_generic_05.sh
 TEST_PROGS += test_generic_06.sh
 TEST_PROGS += test_generic_07.sh
 
+TEST_PROGS += test_generic_08.sh
+
 TEST_PROGS += test_null_01.sh
 TEST_PROGS += test_null_02.sh
 TEST_PROGS += test_loop_01.sh
tools/testing/selftests/ublk/file_backed.c +6 -3
index 6f34eabfae97..0e1b9f670403 100644
@@ -29,11 +29,12 @@ static int loop_queue_flush_io(struct ublk_queue *q, const struct ublksrv_io_des
 static int loop_queue_tgt_rw_io(struct ublk_queue *q, const struct ublksrv_io_desc *iod, int tag)
 {
 	unsigned ublk_op = ublksrv_get_op(iod);
-	int zc = ublk_queue_use_zc(q);
-	enum io_uring_op op = ublk_to_uring_op(iod, zc);
+	unsigned zc = ublk_queue_use_zc(q);
+	unsigned auto_zc = ublk_queue_use_auto_zc(q);
+	enum io_uring_op op = ublk_to_uring_op(iod, zc | auto_zc);
 	struct io_uring_sqe *sqe[3];
 
-	if (!zc) {
+	if (!zc || auto_zc) {
 		ublk_queue_alloc_sqes(q, sqe, 1);
 		if (!sqe[0])
 			return -ENOMEM;
@@ -42,6 +43,8 @@ static int loop_queue_tgt_rw_io(struct ublk_queue *q, const struct ublksrv_io_de
 				(void *)iod->addr,
 				iod->nr_sectors << 9,
 				iod->start_sector << 9);
+		if (auto_zc)
+			sqe[0]->buf_index = tag;
 		io_uring_sqe_set_flags(sqe[0], IOSQE_FIXED_FILE);
 		/* bit63 marks us as tgt io */
 		sqe[0]->user_data = build_user_data(tag, ublk_op, 0, 1);
tools/testing/selftests/ublk/kublk.c +10 -3
index 701b47f98902..fe475a76e669 100644
@@ -420,9 +420,12 @@ static int ublk_queue_init(struct ublk_queue *q)
 	q->cmd_inflight = 0;
 	q->tid = gettid();
 
-	if (dev->dev_info.flags & UBLK_F_SUPPORT_ZERO_COPY) {
+	if (dev->dev_info.flags & (UBLK_F_SUPPORT_ZERO_COPY | UBLK_F_AUTO_ZERO_COPY)) {
 		q->state |= UBLKSRV_NO_BUF;
-		q->state |= UBLKSRV_ZC;
+		if (dev->dev_info.flags & UBLK_F_SUPPORT_ZERO_COPY)
+			q->state |= UBLKSRV_ZC;
+		if (dev->dev_info.flags & UBLK_F_AUTO_ZERO_COPY)
+			q->state |= UBLKSRV_AUTO_ZC;
 	}
 
 	cmd_buf_size = ublk_queue_cmd_buf_sz(q);
@@ -461,7 +464,7 @@ static int ublk_queue_init(struct ublk_queue *q)
 		goto fail;
 	}
 
-	if (dev->dev_info.flags & UBLK_F_SUPPORT_ZERO_COPY) {
+	if (dev->dev_info.flags & (UBLK_F_SUPPORT_ZERO_COPY | UBLK_F_AUTO_ZERO_COPY)) {
 		ret = io_uring_register_buffers_sparse(&q->ring, q->q_depth);
 		if (ret) {
 			ublk_err("ublk dev %d queue %d register spare buffers failed %d",
@@ -1203,6 +1206,7 @@ static int cmd_dev_get_features(void)
 		[const_ilog2(UBLK_F_USER_COPY)] = "USER_COPY",
 		[const_ilog2(UBLK_F_ZONED)] = "ZONED",
 		[const_ilog2(UBLK_F_USER_RECOVERY_FAIL_IO)] = "RECOVERY_FAIL_IO",
+		[const_ilog2(UBLK_F_AUTO_ZERO_COPY)] = "AUTO_ZC",
 	};
 	struct ublk_dev *dev;
 	__u64 features = 0;
@@ -1297,6 +1301,7 @@ int main(int argc, char *argv[])
 		{ "recovery_fail_io",	1,	NULL, 'e'},
 		{ "recovery_reissue",	1,	NULL, 'i'},
 		{ "get_data",		1,	NULL, 'g'},
+		{ "auto_zc",		0,	NULL,  0},
 		{ 0, 0, 0, 0 }
 	};
 	const struct ublk_tgt_ops *ops = NULL;
@@ -1367,6 +1372,8 @@ int main(int argc, char *argv[])
 				ublk_dbg_mask = 0;
 			if (!strcmp(longopts[option_idx].name, "foreground"))
 				ctx.fg = 1;
+			if (!strcmp(longopts[option_idx].name, "auto_zc"))
+				ctx.flags |= UBLK_F_AUTO_ZERO_COPY;
 			break;
 		case '?':
 			/*
tools/testing/selftests/ublk/kublk.h +6 -0
index 44ee1e4ac55b..4c97ee6aafa2 100644
@@ -169,6 +169,7 @@ struct ublk_queue {
 #define UBLKSRV_QUEUE_IDLE	(1U << 1)
 #define UBLKSRV_NO_BUF		(1U << 2)
 #define UBLKSRV_ZC		(1U << 3)
+#define UBLKSRV_AUTO_ZC		(1U << 4)
 	unsigned state;
 	pid_t tid;
 	pthread_t thread;
@@ -388,6 +389,11 @@ static inline int ublk_queue_use_zc(const struct ublk_queue *q)
 	return q->state & UBLKSRV_ZC;
 }
 
+static inline int ublk_queue_use_auto_zc(const struct ublk_queue *q)
+{
+	return q->state & UBLKSRV_AUTO_ZC;
+}
+
 extern const struct ublk_tgt_ops null_tgt_ops;
 extern const struct ublk_tgt_ops loop_tgt_ops;
 extern const struct ublk_tgt_ops stripe_tgt_ops;
tools/testing/selftests/ublk/null.c +32 -11

75 lines not on this page · raw message

tools/testing/selftests/ublk/stripe.c +7 -7
index 5dbd6392d83d..ba3d7789ac21 100644
@@ -126,8 +126,9 @@ static inline enum io_uring_op stripe_to_uring_op(
 static int stripe_queue_tgt_rw_io(struct ublk_queue *q, const struct ublksrv_io_desc *iod, int tag)
 {
 	const struct stripe_conf *conf = get_chunk_shift(q);
-	int zc = !!(ublk_queue_use_zc(q) != 0);
-	enum io_uring_op op = stripe_to_uring_op(iod, zc);
+	unsigned auto_zc = (ublk_queue_use_auto_zc(q) != 0);
+	unsigned zc = (ublk_queue_use_zc(q) != 0);
+	enum io_uring_op op = stripe_to_uring_op(iod, zc | auto_zc);
 	struct io_uring_sqe *sqe[NR_STRIPE];
 	struct stripe_array *s = alloc_stripe_array(conf, iod);
 	struct ublk_io *io = ublk_get_io(q, tag);
@@ -153,12 +154,11 @@ static int stripe_queue_tgt_rw_io(struct ublk_queue *q, const struct ublksrv_io_
 				(void *)t->vec,
 				t->nr_vec,
 				t->start << 9);
-		if (zc) {
+		io_uring_sqe_set_flags(sqe[i], IOSQE_FIXED_FILE);
+		if (auto_zc || zc) {
 			sqe[i]->buf_index = tag;
-			io_uring_sqe_set_flags(sqe[i],
-					IOSQE_FIXED_FILE | IOSQE_IO_HARDLINK);
-		} else {
-			io_uring_sqe_set_flags(sqe[i], IOSQE_FIXED_FILE);
+			if (zc)
+				sqe[i]->flags |= IOSQE_IO_HARDLINK;
 		}
 		/* bit63 marks us as tgt io */
 		sqe[i]->user_data = build_user_data(tag, ublksrv_get_op(iod), i - zc, 1);
tools/testing/selftests/ublk/test_generic_08.sh new file +28 -0
new file mode 100755
index 000000000000..5c54331ad900
@@ -0,0 +1,28 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+
+. "$(cd "$(dirname "$0")" && pwd)"/test_common.sh
+
+TID="generic_08"
+ERR_CODE=0
+
+_prep_test "generic" "test UBLK_F_AUTO_ZERO_COPY"
+
+_create_backfile 0 256M
+_create_backfile 1 256M
+
+dev_id=$(_add_ublk_dev -t loop -q 2 --auto_zc "${UBLK_BACKFILES[0]}")
+_check_add_dev $TID $?
+
+if ! _mkfs_mount_test /dev/ublkb"${dev_id}"; then
+	_cleanup_test "generic"
+	_show_result $TID 255
+fi
+
+dev_id=$(_add_ublk_dev -t stripe --auto_zc "${UBLK_BACKFILES[0]}" "${UBLK_BACKFILES[1]}")
+_check_add_dev $TID $?
+_mkfs_mount_test /dev/ublkb"${dev_id}"
+ERR_CODE=$?
+
+_cleanup_test "generic"
+_show_result $TID $ERR_CODE
tools/testing/selftests/ublk/test_stress_03.sh +6 -0
index e0854f71d35b..11173f4e9842 100755
@@ -32,6 +32,12 @@ _create_backfile 2 128M
 ublk_io_and_remove 8G -t null -q 4 -z &
 ublk_io_and_remove 256M -t loop -q 4 -z "${UBLK_BACKFILES[0]}" &
 ublk_io_and_remove 256M -t stripe -q 4 -z "${UBLK_BACKFILES[1]}" "${UBLK_BACKFILES[2]}" &
+
+if _have_feature "AUTO_ZC"; then
+	ublk_io_and_remove 8G -t null -q 4 --auto_zc &
+	ublk_io_and_remove 256M -t loop -q 4 --auto_zc "${UBLK_BACKFILES[0]}" &
+	ublk_io_and_remove 256M -t stripe -q 4 --auto_zc "${UBLK_BACKFILES[1]}" "${UBLK_BACKFILES[2]}" &
+fi
 wait
 
 _cleanup_test "stress"
tools/testing/selftests/ublk/test_stress_04.sh +6 -0
index 1798a98387e8..c219753513d1 100755
@@ -31,6 +31,12 @@ _create_backfile 2 128M
 ublk_io_and_kill_daemon 8G -t null -q 4 -z &
 ublk_io_and_kill_daemon 256M -t loop -q 4 -z "${UBLK_BACKFILES[0]}" &
 ublk_io_and_kill_daemon 256M -t stripe -q 4 -z "${UBLK_BACKFILES[1]}" "${UBLK_BACKFILES[2]}" &
+
+if _have_feature "AUTO_ZC"; then
+	ublk_io_and_kill_daemon 8G -t null -q 4 --auto_zc &
+	ublk_io_and_kill_daemon 256M -t loop -q 4 --auto_zc "${UBLK_BACKFILES[0]}" &
+	ublk_io_and_kill_daemon 256M -t stripe -q 4 --auto_zc "${UBLK_BACKFILES[1]}" "${UBLK_BACKFILES[2]}" &
+fi
 wait
 
 _cleanup_test "stress"
tools/testing/selftests/ublk/test_stress_05.sh +8 -0
index a7071b10224d..878db3097f6e 100755
@@ -60,5 +60,13 @@ if _have_feature "ZERO_COPY"; then
 	done
 fi
 
+if _have_feature "AUTO_ZC"; then
+	for reissue in $(seq 0 1); do
+		ublk_io_and_remove 8G -t null -q 4 -g 1 --auto_zc -r 1 -i "$reissue" &
+		ublk_io_and_remove 256M -t loop -q 4 -g 1 --auto_zc -r 1 -i "$reissue" "${UBLK_BACKFILES[1]}" &
+		wait
+	done
+fi
+
 _cleanup_test "stress"
 _show_result $TID $ERR_CODE
Signature · 1 line
2.47.0