Skip to contentSkip to conversation

ublk: add UBLK_F_BATCH_IO

PATCH Superseded Ming Lei 3 participants 49 messages Nov 12, 2025 – Nov 19, 2025

Subsystems (6)

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Ming Lei

author
#0 lore raw

v3 cover ublk: add UBLK_F_BATCH_IO

Hello,

This patchset adds UBLK_F_BATCH_IO feature for communicating between kernel and ublk server in batching way:

- Per-queue vs Per-I/O: Commands operate on queues rather than individual I/Os

- Batch processing: Multiple I/Os are handled in single operation

- Multishot commands: Use io_uring multishot for reducing submission overhead

- Flexible task assignment: Any task can handle any I/O (no per-I/O daemons)

- Better load balancing: Tasks can adjust their workload dynamically

- help for future optimizations:
- blk-mq batch tags free
- support io-poll
- per-task batch for avoiding per-io lock
- fetch command priority

- simplify command cancel process with per-queue lock

selftest are provided.

Performance test result(IOPS):

- page copy

tools/testing/selftests/ublk//kublk add -t null -q 16 [-b]

- zero copy(--auto_zc) tools/testing/selftests/ublk//kublk add -t null -q 16 --auto_zc [-b]

- IO test
taskset -c 0-31 fio/t/io_uring -p0 -n $JOBS -r 30 /dev/ublkb0

1) 16 jobs IO
- page copy: 37.77M vs. 42.40M(BATCH_IO), +12%
- zero copy(--auto_zc): 42.83M vs. 44.43M(BATCH_IO), +3.7%

2) single job IO
- page copy: 2.54M vs. 2.6M(BATCH_IO), +2.3%
- zero copy(--auto_zc): 3.13M vs. 3.35M(BATCH_IO), +7%

V3:
- rebase on for-6.19/block
- use blk_mq_end_request_batch() to free requests in batch, only for page copy
- fix one IO hang issue because of memory barrier order, comments on the memory barrier pairing
- add NUMA ware kfifo_alloc_node()
- fix one build warning reported by 0-DAY CI
- selftests improvement & fix

V2:
- ublk_config_io_buf() vs. __ublk_fetch() order
- code style clean
- use READ_ONCE() to cache sqe data because sqe copy becomes conditional recently
- don't use sqe->len for UBLK_U_IO_PREP_IO_CMDS & UBLK_U_IO_COMMIT_IO_CMDS
- fix one build warning
- fix build_user_data()
- run performance analysis, and find one bug in io_uring_cmd_buffer_select(), fix is posted already

Ming Lei (27):
kfifo: add kfifo_alloc_node() helper for NUMA awareness
ublk: add parameter struct io_uring_cmd * to
ublk_prep_auto_buf_reg()
ublk: add union ublk_io_buf with improved naming
ublk: refactor auto buffer register in ublk_dispatch_req()
ublk: pass const pointer to ublk_queue_is_zoned()
ublk: add helper of __ublk_fetch()
ublk: define ublk_ch_batch_io_fops for the coming feature F_BATCH_IO
ublk: prepare for not tracking task context for command batch
ublk: add new batch command UBLK_U_IO_PREP_IO_CMDS &
UBLK_U_IO_COMMIT_IO_CMDS
ublk: handle UBLK_U_IO_PREP_IO_CMDS
ublk: handle UBLK_U_IO_COMMIT_IO_CMDS
ublk: add io events fifo structure
ublk: add batch I/O dispatch infrastructure
ublk: add UBLK_U_IO_FETCH_IO_CMDS for batch I/O processing
ublk: abort requests filled in event kfifo
ublk: add new feature UBLK_F_BATCH_IO
ublk: document feature UBLK_F_BATCH_IO
ublk: implement batch request completion via
blk_mq_end_request_batch()
selftests: ublk: fix user_data truncation for tgt_data >= 256
selftests: ublk: replace assert() with ublk_assert()
selftests: ublk: add ublk_io_buf_idx() for returning io buffer index
selftests: ublk: add batch buffer management infrastructure
selftests: ublk: handle UBLK_U_IO_PREP_IO_CMDS
selftests: ublk: handle UBLK_U_IO_COMMIT_IO_CMDS
selftests: ublk: handle UBLK_U_IO_FETCH_IO_CMDS
selftests: ublk: add --batch/-b for enabling F_BATCH_IO
selftests: ublk: support arbitrary threads/queues combination

 Documentation/block/ublk.rst                  |   60 +-
 drivers/block/ublk_drv.c                      | 1288 +++++++++++++++--
 include/linux/kfifo.h                         |   27 +
 include/uapi/linux/ublk_cmd.h                 |   91 ++
 lib/kfifo.c                                   |   13 +-
 tools/testing/selftests/ublk/Makefile         |    7 +-
 tools/testing/selftests/ublk/batch.c          |  609 ++++++++
 tools/testing/selftests/ublk/common.c         |    2 +-
 tools/testing/selftests/ublk/file_backed.c    |   11 +-
 tools/testing/selftests/ublk/kublk.c          |  143 +-
 tools/testing/selftests/ublk/kublk.h          |  195 ++-
 tools/testing/selftests/ublk/null.c           |   18 +-
 tools/testing/selftests/ublk/stripe.c         |   17 +-
 .../testing/selftests/ublk/test_generic_14.sh |   32 +
 .../testing/selftests/ublk/test_generic_15.sh |   30 +
 .../testing/selftests/ublk/test_generic_16.sh |   30 +
 .../testing/selftests/ublk/test_stress_06.sh  |   45 +
 .../testing/selftests/ublk/test_stress_07.sh  |   44 +
 tools/testing/selftests/ublk/utils.h          |   64 +
 19 files changed, 2551 insertions(+), 175 deletions(-)
 create mode 100644 tools/testing/selftests/ublk/batch.c
 create mode 100755 tools/testing/selftests/ublk/test_generic_14.sh
 create mode 100755 tools/testing/selftests/ublk/test_generic_15.sh
 create mode 100755 tools/testing/selftests/ublk/test_generic_16.sh
 create mode 100755 tools/testing/selftests/ublk/test_stress_06.sh
 create mode 100755 tools/testing/selftests/ublk/test_stress_07.sh
Signature · 1 line
2.47.0
author
#1 lore raw

PATCH v3 1/27 kfifo: add kfifo_alloc_node() helper for NUMA awareness

Add __kfifo_alloc_node() by refactoring and reusing __kfifo_alloc(), and define kfifo_alloc_node() macro to support NUMA-aware memory allocation.

The new __kfifo_alloc_node() function accepts a NUMA node parameter and uses kmalloc_array_node() instead of kmalloc_array() for node-specific allocation. The existing __kfifo_alloc() now calls __kfifo_alloc_node() with NUMA_NO_NODE to maintain backward compatibility.

This enables users to allocate kfifo buffers on specific NUMA nodes, which is important for performance in NUMA systems where the kfifo will be primarily accessed by threads running on specific nodes.

Cc Stefani Seibold <[email protected]>
Cc Andrew Morton <[email protected]>
---
 include/linux/kfifo.h | 27 +++++++++++++++++++++++++++
 lib/kfifo.c           | 13 ++++++++++---
 2 files changed, 37 insertions(+), 3 deletions(-)
include/linux/kfifo.h +27 -0
index fd743d4c4b4b..61d1fe014a6c 100644
@@ -369,6 +369,30 @@ __kfifo_int_must_check_helper( \
 }) \
 )
 
+/**
+ * kfifo_alloc_node - dynamically allocates a new fifo buffer on a NUMA node
+ * @fifo: pointer to the fifo
+ * @size: the number of elements in the fifo, this must be a power of 2
+ * @gfp_mask: get_free_pages mask, passed to kmalloc()
+ * @node: NUMA node to allocate memory on
+ *
+ * This macro dynamically allocates a new fifo buffer with NUMA node awareness.
+ *
+ * The number of elements will be rounded-up to a power of 2.
+ * The fifo will be release with kfifo_free().
+ * Return 0 if no error, otherwise an error code.
+ */
+#define kfifo_alloc_node(fifo, size, gfp_mask, node) \
+__kfifo_int_must_check_helper( \
+({ \
+	typeof((fifo) + 1) __tmp = (fifo); \
+	struct __kfifo *__kfifo = &__tmp->kfifo; \
+	__is_kfifo_ptr(__tmp) ? \
+	__kfifo_alloc_node(__kfifo, size, sizeof(*__tmp->type), gfp_mask, node) : \
+	-EINVAL; \
+}) \
+)
+
 /**
  * kfifo_free - frees the fifo
  * @fifo: the fifo to be freed
@@ -902,6 +926,9 @@ __kfifo_uint_must_check_helper( \
 extern int __kfifo_alloc(struct __kfifo *fifo, unsigned int size,
 	size_t esize, gfp_t gfp_mask);
 
+extern int __kfifo_alloc_node(struct __kfifo *fifo, unsigned int size,
+	size_t esize, gfp_t gfp_mask, int node);
+
 extern void __kfifo_free(struct __kfifo *fifo);
 
 extern int __kfifo_init(struct __kfifo *fifo, void *buffer,
lib/kfifo.c +10 -3
index a8b2eed90599..195cf0feecc2 100644
@@ -22,8 +22,8 @@ static inline unsigned int kfifo_unused(struct __kfifo *fifo)
 	return (fifo->mask + 1) - (fifo->in - fifo->out);
 }
 
-int __kfifo_alloc(struct __kfifo *fifo, unsigned int size,
-		size_t esize, gfp_t gfp_mask)
+int __kfifo_alloc_node(struct __kfifo *fifo, unsigned int size,
+		size_t esize, gfp_t gfp_mask, int node)
 {
 	/*
 	 * round up to the next power of 2, since our 'let the indices
@@ -41,7 +41,7 @@ int __kfifo_alloc(struct __kfifo *fifo, unsigned int size,
 		return -EINVAL;
 	}
 
-	fifo->data = kmalloc_array(esize, size, gfp_mask);
+	fifo->data = kmalloc_array_node(esize, size, gfp_mask, node);
 
 	if (!fifo->data) {
 		fifo->mask = 0;
@@ -51,6 +51,13 @@ int __kfifo_alloc(struct __kfifo *fifo, unsigned int size,
 
 	return 0;
 }
+EXPORT_SYMBOL(__kfifo_alloc_node);
+
+int __kfifo_alloc(struct __kfifo *fifo, unsigned int size,
+		size_t esize, gfp_t gfp_mask)
+{
+	return __kfifo_alloc_node(fifo, size, esize, gfp_mask, NUMA_NO_NODE);
+}
 EXPORT_SYMBOL(__kfifo_alloc);
 
 void __kfifo_free(struct __kfifo *fifo)
Signature · 1 line
2.47.0
maintainer
#2 lore raw

Re: [PATCH V3 01/27] kfifo: add kfifo_alloc_node() helper for NUMA awareness

Quoted 10 lines · On Wed, 12 Nov 2025 17:37:39 +0800 Ming Lei <[email protected]> wrote:

Add __kfifo_alloc_node() by refactoring and reusing __kfifo_alloc(), and define kfifo_alloc_node() macro to support NUMA-aware memory allocation.

The new __kfifo_alloc_node() function accepts a NUMA node parameter and uses kmalloc_array_node() instead of kmalloc_array() for node-specific allocation. The existing __kfifo_alloc() now calls __kfifo_alloc_node() with NUMA_NO_NODE to maintain backward compatibility.

Quoted 3 lines

This enables users to allocate kfifo buffers on specific NUMA nodes, which is important for performance in NUMA systems where the kfifo will be primarily accessed by threads running on specific nodes.

I was about to ask "please don't add infrastructure without users", but I see a "01/27" there. I wander over to lkml but I can't find 02-27 there either. Maybe something went wrong.

I prefer to be cc'ed on the entire series, please.

Quoted 19 lines
include/linux/kfifo.h +13 -0
@@ -369,6 +369,30 @@ __kfifo_int_must_check_helper( \
 }) \
 )
 
+/**
+ * kfifo_alloc_node - dynamically allocates a new fifo buffer on a NUMA node
+ * @fifo: pointer to the fifo
+ * @size: the number of elements in the fifo, this must be a power of 2
+ * @gfp_mask: get_free_pages mask, passed to kmalloc()
+ * @node: NUMA node to allocate memory on
+ *
+ * This macro dynamically allocates a new fifo buffer with NUMA node awareness.
+ *
+ * The number of elements will be rounded-up to a power of 2.
+ * The fifo will be release with kfifo_free().
+ * Return 0 if no error, otherwise an error code.
+ */

include/linux/kfifo.h __kfifo_int_must_check_helper( \ · line 394

Quoted 10 lines
include/linux/kfifo.h +10 -0
+#define kfifo_alloc_node(fifo, size, gfp_mask, node) \
+__kfifo_int_must_check_helper( \
+({ \
+	typeof((fifo) + 1) __tmp = (fifo); \
+	struct __kfifo *__kfifo = &__tmp->kfifo; \
+	__is_kfifo_ptr(__tmp) ? \
+	__kfifo_alloc_node(__kfifo, size, sizeof(*__tmp->type), gfp_mask, node) : \
+	-EINVAL; \
+}) \
+)

Well this is an eyesore. Do we really need it? It seems to be here so we can check for a programming bug? Well, don't add programming bugs!

I'm actually not enjoying the existence of __is_kfifo_ptr() at all. What is it all doing? It's a FIFO for heck's sake, why is this so hard.

include/linux/kfifo.h __kfifo_uint_must_check_helper( \ · line 931

Quoted 7 lines
include/linux/kfifo.h +3 -0
@@ -902,6 +926,9 @@ __kfifo_uint_must_check_helper( \
 extern int __kfifo_alloc(struct __kfifo *fifo, unsigned int size,
 	size_t esize, gfp_t gfp_mask);
 
+extern int __kfifo_alloc_node(struct __kfifo *fifo, unsigned int size,
+	size_t esize, gfp_t gfp_mask, int node);
+

Nit: please align things like this:

extern int __kfifo_alloc_node(struct __kfifo *fifo, unsigned int size,

			      size_t esize, gfp_t gfp_mask, int node);

(several places)

author
#3 lore raw

Re: [PATCH V3 01/27] kfifo: add kfifo_alloc_node() helper for NUMA awareness

Quoted 12 lines · Andrew Morton
Quoted 10 lines · On Wed, 12 Nov 2025 17:37:39 +0800 Ming Lei <[email protected]> wrote:

Add __kfifo_alloc_node() by refactoring and reusing __kfifo_alloc(), and define kfifo_alloc_node() macro to support NUMA-aware memory allocation.

The new __kfifo_alloc_node() function accepts a NUMA node parameter and uses kmalloc_array_node() instead of kmalloc_array() for node-specific allocation. The existing __kfifo_alloc() now calls __kfifo_alloc_node() with NUMA_NO_NODE to maintain backward compatibility.

Quoted 7 lines
Quoted 3 lines

This enables users to allocate kfifo buffers on specific NUMA nodes, which is important for performance in NUMA systems where the kfifo will be primarily accessed by threads running on specific nodes.

I was about to ask "please don't add infrastructure without users", but I see a "01/27" there. I wander over to lkml but I can't find 02-27 there either. Maybe something went wrong.

It can be found in lore:

https://lore.kernel.org/all/[email protected]/

Quoted 2 lines

I prefer to be cc'ed on the entire series, please.

OK.

Quoted 34 lines
Quoted 20 lines
include/linux/kfifo.h +13 -0
@@ -369,6 +369,30 @@ __kfifo_int_must_check_helper( \
 }) \
 )
 
+/**
+ * kfifo_alloc_node - dynamically allocates a new fifo buffer on a NUMA node
+ * @fifo: pointer to the fifo
+ * @size: the number of elements in the fifo, this must be a power of 2
+ * @gfp_mask: get_free_pages mask, passed to kmalloc()
+ * @node: NUMA node to allocate memory on
+ *
+ * This macro dynamically allocates a new fifo buffer with NUMA node awareness.
+ *
+ * The number of elements will be rounded-up to a power of 2.
+ * The fifo will be release with kfifo_free().
+ * Return 0 if no error, otherwise an error code.
+ */

include/linux/kfifo.h __kfifo_int_must_check_helper( \ · line 394

Quoted 10 lines
include/linux/kfifo.h +10 -0
+#define kfifo_alloc_node(fifo, size, gfp_mask, node) \
+__kfifo_int_must_check_helper( \
+({ \
+	typeof((fifo) + 1) __tmp = (fifo); \
+	struct __kfifo *__kfifo = &__tmp->kfifo; \
+	__is_kfifo_ptr(__tmp) ? \
+	__kfifo_alloc_node(__kfifo, size, sizeof(*__tmp->type), gfp_mask, node) : \
+	-EINVAL; \
+}) \
+)

Well this is an eyesore. Do we really need it? It seems to be here so we can check for a programming bug? Well, don't add programming bugs!

Quoted 2 lines

I'm actually not enjoying the existence of __is_kfifo_ptr() at all. What is it all doing? It's a FIFO for heck's sake, why is this so hard.

It is basically a clone of existing kfifo_alloc().

Do we need to clean kfifo_alloc() first? Otherwise I'd keep the same pattern with existing definitions.

Quoted 5 lines
Quoted 5 lines
include/linux/kfifo.h +0 -0
@@ -902,6 +926,9 @@ __kfifo_uint_must_check_helper( \
 extern int __kfifo_alloc(struct __kfifo *fifo, unsigned int size,
 	size_t esize, gfp_t gfp_mask);
 
Quoted 8 lines

include/linux/kfifo.h __kfifo_uint_must_check_helper( \ · line 931

Quoted 3 lines
include/linux/kfifo.h +3 -0
+extern int __kfifo_alloc_node(struct __kfifo *fifo, unsigned int size,
+	size_t esize, gfp_t gfp_mask, int node);
+

Nit: please align things like this:

extern int __kfifo_alloc_node(struct __kfifo *fifo, unsigned int size,

			      size_t esize, gfp_t gfp_mask, int node);

OK.

Thanks,
Ming

maintainer
#4 lore raw

Re: [PATCH V3 01/27] kfifo: add kfifo_alloc_node() helper for NUMA awareness

Quoted 14 lines · On Thu, 13 Nov 2025 09:21:41 +0800 Ming Lei <[email protected]> wrote:
Quoted 14 lines

include/linux/kfifo.h __kfifo_int_must_check_helper( \ · line 394

Quoted 10 lines
include/linux/kfifo.h +10 -0
+#define kfifo_alloc_node(fifo, size, gfp_mask, node) \
+__kfifo_int_must_check_helper( \
+({ \
+	typeof((fifo) + 1) __tmp = (fifo); \
+	struct __kfifo *__kfifo = &__tmp->kfifo; \
+	__is_kfifo_ptr(__tmp) ? \
+	__kfifo_alloc_node(__kfifo, size, sizeof(*__tmp->type), gfp_mask, node) : \
+	-EINVAL; \
+}) \
+)

Well this is an eyesore. Do we really need it? It seems to be here so we can check for a programming bug? Well, don't add programming bugs!

Quoted 7 lines
Quoted 2 lines

I'm actually not enjoying the existence of __is_kfifo_ptr() at all. What is it all doing? It's a FIFO for heck's sake, why is this so hard.

It is basically a clone of existing kfifo_alloc().

Do we need to clean kfifo_alloc() first? Otherwise I'd keep the same pattern with existing definitions.

Is OK, I was just whining. Possibly de-over-engineering kifo is a separate project.

#5 lore raw

Re: [PATCH V3 01/27] kfifo: add kfifo_alloc_node() helper for NUMA awareness

In reply to Ming Lei
Quoted 95 lines · Ming Lei

Add __kfifo_alloc_node() by refactoring and reusing __kfifo_alloc(), and define kfifo_alloc_node() macro to support NUMA-aware memory allocation.

The new __kfifo_alloc_node() function accepts a NUMA node parameter and uses kmalloc_array_node() instead of kmalloc_array() for node-specific allocation. The existing __kfifo_alloc() now calls __kfifo_alloc_node() with NUMA_NO_NODE to maintain backward compatibility.

This enables users to allocate kfifo buffers on specific NUMA nodes, which is important for performance in NUMA systems where the kfifo will be primarily accessed by threads running on specific nodes.

Cc Stefani Seibold <[email protected]>
Cc Andrew Morton <[email protected]>
---
 include/linux/kfifo.h | 27 +++++++++++++++++++++++++++
 lib/kfifo.c           | 13 ++++++++++---
 2 files changed, 37 insertions(+), 3 deletions(-)
include/linux/kfifo.h +27 -0
index fd743d4c4b4b..61d1fe014a6c 100644
@@ -369,6 +369,30 @@ __kfifo_int_must_check_helper( \
 }) \
 )
 
+/**
+ * kfifo_alloc_node - dynamically allocates a new fifo buffer on a NUMA node
+ * @fifo: pointer to the fifo
+ * @size: the number of elements in the fifo, this must be a power of 2
+ * @gfp_mask: get_free_pages mask, passed to kmalloc()
+ * @node: NUMA node to allocate memory on
+ *
+ * This macro dynamically allocates a new fifo buffer with NUMA node awareness.
+ *
+ * The number of elements will be rounded-up to a power of 2.
+ * The fifo will be release with kfifo_free().
+ * Return 0 if no error, otherwise an error code.
+ */
+#define kfifo_alloc_node(fifo, size, gfp_mask, node) \
+__kfifo_int_must_check_helper( \
+({ \
+       typeof((fifo) + 1) __tmp = (fifo); \
+       struct __kfifo *__kfifo = &__tmp->kfifo; \
+       __is_kfifo_ptr(__tmp) ? \
+       __kfifo_alloc_node(__kfifo, size, sizeof(*__tmp->type), gfp_mask, node) : \
+       -EINVAL; \
+}) \
+)
+
 /**
  * kfifo_free - frees the fifo
  * @fifo: the fifo to be freed
@@ -902,6 +926,9 @@ __kfifo_uint_must_check_helper( \
 extern int __kfifo_alloc(struct __kfifo *fifo, unsigned int size,
        size_t esize, gfp_t gfp_mask);
 
+extern int __kfifo_alloc_node(struct __kfifo *fifo, unsigned int size,
+       size_t esize, gfp_t gfp_mask, int node);
+
 extern void __kfifo_free(struct __kfifo *fifo);
 
 extern int __kfifo_init(struct __kfifo *fifo, void *buffer,
lib/kfifo.c +3 -3
index a8b2eed90599..195cf0feecc2 100644
@@ -22,8 +22,8 @@ static inline unsigned int kfifo_unused(struct __kfifo *fifo)
        return (fifo->mask + 1) - (fifo->in - fifo->out);
 }
 
-int __kfifo_alloc(struct __kfifo *fifo, unsigned int size,
-               size_t esize, gfp_t gfp_mask)
+int __kfifo_alloc_node(struct __kfifo *fifo, unsigned int size,
+               size_t esize, gfp_t gfp_mask, int node)
 {
        /*
         * round up to the next power of 2, since our 'let the indices
@@ -41,7 +41,7 @@ int __kfifo_alloc(struct __kfifo *fifo, unsigned int size,
                return -EINVAL;
        }
 
-       fifo->data = kmalloc_array(esize, size, gfp_mask);
+       fifo->data = kmalloc_array_node(esize, size, gfp_mask, node);
 
        if (!fifo->data) {
                fifo->mask = 0;
@@ -51,6 +51,13 @@ int __kfifo_alloc(struct __kfifo *fifo, unsigned int size,
 

lib/kfifo.c int __kfifo_alloc(struct __kfifo *fifo, unsigned int size, · line 61

Quoted 10 lines
lib/kfifo.c +7 -0
        return 0;
 }
+EXPORT_SYMBOL(__kfifo_alloc_node);
+
+int __kfifo_alloc(struct __kfifo *fifo, unsigned int size,
+               size_t esize, gfp_t gfp_mask)
+{
+       return __kfifo_alloc_node(fifo, size, esize, gfp_mask, NUMA_NO_NODE);
+}
 EXPORT_SYMBOL(__kfifo_alloc);

Is it worth keeping __kfifo_alloc() as an extern function? Seems like it would make the executable smaller to turn __kfifo_alloc() into a static inline function that just defers to __kfifo_alloc_node().

Best,
Caleb

Quoted 5 lines
lib/kfifo.c +0 -1
 void __kfifo_free(struct __kfifo *fifo)
--

2.47.0

author
#6 lore raw

Re: [PATCH V3 01/27] kfifo: add kfifo_alloc_node() helper for NUMA awareness

Quoted 96 lines · Caleb Sander Mateos
Quoted 95 lines · Ming Lei

Add __kfifo_alloc_node() by refactoring and reusing __kfifo_alloc(), and define kfifo_alloc_node() macro to support NUMA-aware memory allocation.

The new __kfifo_alloc_node() function accepts a NUMA node parameter and uses kmalloc_array_node() instead of kmalloc_array() for node-specific allocation. The existing __kfifo_alloc() now calls __kfifo_alloc_node() with NUMA_NO_NODE to maintain backward compatibility.

This enables users to allocate kfifo buffers on specific NUMA nodes, which is important for performance in NUMA systems where the kfifo will be primarily accessed by threads running on specific nodes.

Cc Stefani Seibold <[email protected]>
Cc Andrew Morton <[email protected]>
---
 include/linux/kfifo.h | 27 +++++++++++++++++++++++++++
 lib/kfifo.c           | 13 ++++++++++---
 2 files changed, 37 insertions(+), 3 deletions(-)
include/linux/kfifo.h +27 -0
index fd743d4c4b4b..61d1fe014a6c 100644
@@ -369,6 +369,30 @@ __kfifo_int_must_check_helper( \
 }) \
 )
 
+/**
+ * kfifo_alloc_node - dynamically allocates a new fifo buffer on a NUMA node
+ * @fifo: pointer to the fifo
+ * @size: the number of elements in the fifo, this must be a power of 2
+ * @gfp_mask: get_free_pages mask, passed to kmalloc()
+ * @node: NUMA node to allocate memory on
+ *
+ * This macro dynamically allocates a new fifo buffer with NUMA node awareness.
+ *
+ * The number of elements will be rounded-up to a power of 2.
+ * The fifo will be release with kfifo_free().
+ * Return 0 if no error, otherwise an error code.
+ */
+#define kfifo_alloc_node(fifo, size, gfp_mask, node) \
+__kfifo_int_must_check_helper( \
+({ \
+       typeof((fifo) + 1) __tmp = (fifo); \
+       struct __kfifo *__kfifo = &__tmp->kfifo; \
+       __is_kfifo_ptr(__tmp) ? \
+       __kfifo_alloc_node(__kfifo, size, sizeof(*__tmp->type), gfp_mask, node) : \
+       -EINVAL; \
+}) \
+)
+
 /**
  * kfifo_free - frees the fifo
  * @fifo: the fifo to be freed
@@ -902,6 +926,9 @@ __kfifo_uint_must_check_helper( \
 extern int __kfifo_alloc(struct __kfifo *fifo, unsigned int size,
        size_t esize, gfp_t gfp_mask);
 
+extern int __kfifo_alloc_node(struct __kfifo *fifo, unsigned int size,
+       size_t esize, gfp_t gfp_mask, int node);
+
 extern void __kfifo_free(struct __kfifo *fifo);
 
 extern int __kfifo_init(struct __kfifo *fifo, void *buffer,
lib/kfifo.c +3 -3
index a8b2eed90599..195cf0feecc2 100644
@@ -22,8 +22,8 @@ static inline unsigned int kfifo_unused(struct __kfifo *fifo)
        return (fifo->mask + 1) - (fifo->in - fifo->out);
 }
 
-int __kfifo_alloc(struct __kfifo *fifo, unsigned int size,
-               size_t esize, gfp_t gfp_mask)
+int __kfifo_alloc_node(struct __kfifo *fifo, unsigned int size,
+               size_t esize, gfp_t gfp_mask, int node)
 {
        /*
         * round up to the next power of 2, since our 'let the indices
@@ -41,7 +41,7 @@ int __kfifo_alloc(struct __kfifo *fifo, unsigned int size,
                return -EINVAL;
        }
 
-       fifo->data = kmalloc_array(esize, size, gfp_mask);
+       fifo->data = kmalloc_array_node(esize, size, gfp_mask, node);
 
        if (!fifo->data) {
                fifo->mask = 0;
@@ -51,6 +51,13 @@ int __kfifo_alloc(struct __kfifo *fifo, unsigned int size,
 
Quoted 14 lines

lib/kfifo.c int __kfifo_alloc(struct __kfifo *fifo, unsigned int size, · line 61

Quoted 10 lines
lib/kfifo.c +7 -0
        return 0;
 }
+EXPORT_SYMBOL(__kfifo_alloc_node);
+
+int __kfifo_alloc(struct __kfifo *fifo, unsigned int size,
+               size_t esize, gfp_t gfp_mask)
+{
+       return __kfifo_alloc_node(fifo, size, esize, gfp_mask, NUMA_NO_NODE);
+}
 EXPORT_SYMBOL(__kfifo_alloc);

Is it worth keeping __kfifo_alloc() as an extern function? Seems like it would make the executable smaller to turn __kfifo_alloc() into a static inline function that just defers to __kfifo_alloc_node().

OK, will convert to inline __kfifo_alloc() in next version.

Thanks,
Ming

author
#7 lore raw

PATCH v3 2/27 ublk: add parameter `struct io_uring_cmd *` to ublk_prep_auto_buf_reg()

In reply to Ming Lei

Add parameter struct io_uring_cmd * to ublk_prep_auto_buf_reg() and prepare for reusing this helper for the coming UBLK_BATCH_IO feature, which can fetch & commit one batch of io commands via single uring_cmd.

---
 drivers/block/ublk_drv.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
drivers/block/ublk_drv.c +7 -5
index 75f210523e52..2884e0687e31 100644
@@ -1176,11 +1176,12 @@ ublk_auto_buf_reg_fallback(const struct ublk_queue *ubq, struct ublk_io *io)
 }
 
 static bool ublk_auto_buf_reg(const struct ublk_queue *ubq, struct request *req,
-			      struct ublk_io *io, unsigned int issue_flags)
+			      struct ublk_io *io, struct io_uring_cmd *cmd,
+			      unsigned int issue_flags)
 {
 	int ret;
 
-	ret = io_buffer_register_bvec(io->cmd, req, ublk_io_release,
+	ret = io_buffer_register_bvec(cmd, req, ublk_io_release,
 				      io->buf.index, issue_flags);
 	if (ret) {
 		if (io->buf.flags & UBLK_AUTO_BUF_REG_FALLBACK) {
@@ -1192,18 +1193,19 @@ static bool ublk_auto_buf_reg(const struct ublk_queue *ubq, struct request *req,
 	}
 
 	io->task_registered_buffers = 1;
-	io->buf_ctx_handle = io_uring_cmd_ctx_handle(io->cmd);
+	io->buf_ctx_handle = io_uring_cmd_ctx_handle(cmd);
 	io->flags |= UBLK_IO_FLAG_AUTO_BUF_REG;
 	return true;
 }
 
 static bool ublk_prep_auto_buf_reg(struct ublk_queue *ubq,
 				   struct request *req, struct ublk_io *io,
+				   struct io_uring_cmd *cmd,
 				   unsigned int issue_flags)
 {
 	ublk_init_req_ref(ubq, io);
 	if (ublk_support_auto_buf_reg(ubq) && ublk_rq_has_data(req))
-		return ublk_auto_buf_reg(ubq, req, io, issue_flags);
+		return ublk_auto_buf_reg(ubq, req, io, cmd, issue_flags);
 
 	return true;
 }
@@ -1278,7 +1280,7 @@ static void ublk_dispatch_req(struct ublk_queue *ubq,
 	if (!ublk_start_io(ubq, req, io))
 		return;
 
-	if (ublk_prep_auto_buf_reg(ubq, req, io, issue_flags))
+	if (ublk_prep_auto_buf_reg(ubq, req, io, io->cmd, issue_flags))
 		ublk_complete_io_cmd(io, req, UBLK_IO_RES_OK, issue_flags);
 }
 
Signature · 1 line
2.47.0
author
#8 lore raw

PATCH v3 3/27 ublk: add `union ublk_io_buf` with improved naming

In reply to Ming Lei

Add union ublk_io_buf for naming the anonymous union of struct ublk_io's addr and buf fields, meantime apply it to struct ublk_io for storing either ublk auto buffer register data or ublk server io buffer address.

The union uses clear field names:
- addr: for regular ublk server io buffer addresses
- auto_reg: for ublk auto buffer registration data

This eliminates confusing access patterns and improves code readability.

---
 drivers/block/ublk_drv.c | 40 ++++++++++++++++++++++------------------
 1 file changed, 22 insertions(+), 18 deletions(-)
drivers/block/ublk_drv.c +22 -18
index 2884e0687e31..f1fa5ceacdf6 100644
@@ -155,12 +155,13 @@ struct ublk_uring_cmd_pdu {
  */
 #define UBLK_REFCOUNT_INIT (REFCOUNT_MAX / 2)
 
+union ublk_io_buf {
+	__u64	addr;
+	struct ublk_auto_buf_reg auto_reg;
+};
+
 struct ublk_io {
-	/* userspace buffer address from io cmd */
-	union {
-		__u64	addr;
-		struct ublk_auto_buf_reg buf;
-	};
+	union ublk_io_buf buf;
 	unsigned int flags;
 	int res;
 
@@ -498,7 +499,7 @@ static blk_status_t ublk_setup_iod_zoned(struct ublk_queue *ubq,
 	iod->op_flags = ublk_op | ublk_req_build_flags(req);
 	iod->nr_sectors = blk_rq_sectors(req);
 	iod->start_sector = blk_rq_pos(req);
-	iod->addr = io->addr;
+	iod->addr = io->buf.addr;
 
 	return BLK_STS_OK;
 }
@@ -981,7 +982,7 @@ static unsigned int ublk_map_io(const struct ublk_queue *ubq,
 		struct iov_iter iter;
 		const int dir = ITER_DEST;
 
-		import_ubuf(dir, u64_to_user_ptr(io->addr), rq_bytes, &iter);
+		import_ubuf(dir, u64_to_user_ptr(io->buf.addr), rq_bytes, &iter);
 		return ublk_copy_user_pages(req, 0, &iter, dir);
 	}
 	return rq_bytes;
@@ -1002,7 +1003,7 @@ static unsigned int ublk_unmap_io(bool need_map,
 
 		WARN_ON_ONCE(io->res > rq_bytes);
 
-		import_ubuf(dir, u64_to_user_ptr(io->addr), io->res, &iter);
+		import_ubuf(dir, u64_to_user_ptr(io->buf.addr), io->res, &iter);
 		return ublk_copy_user_pages(req, 0, &iter, dir);
 	}
 	return rq_bytes;
@@ -1068,7 +1069,7 @@ static blk_status_t ublk_setup_iod(struct ublk_queue *ubq, struct request *req)
 	iod->op_flags = ublk_op | ublk_req_build_flags(req);
 	iod->nr_sectors = blk_rq_sectors(req);
 	iod->start_sector = blk_rq_pos(req);
-	iod->addr = io->addr;
+	iod->addr = io->buf.addr;
 
 	return BLK_STS_OK;
 }
@@ -1182,9 +1183,9 @@ static bool ublk_auto_buf_reg(const struct ublk_queue *ubq, struct request *req,
 	int ret;
 
 	ret = io_buffer_register_bvec(cmd, req, ublk_io_release,
-				      io->buf.index, issue_flags);
+				      io->buf.auto_reg.index, issue_flags);
 	if (ret) {
-		if (io->buf.flags & UBLK_AUTO_BUF_REG_FALLBACK) {
+		if (io->buf.auto_reg.flags & UBLK_AUTO_BUF_REG_FALLBACK) {
 			ublk_auto_buf_reg_fallback(ubq, io);
 			return true;
 		}
@@ -1473,7 +1474,7 @@ static void ublk_queue_reinit(struct ublk_device *ub, struct ublk_queue *ubq)
 		 */
 		io->flags &= UBLK_IO_FLAG_CANCELED;
 		io->cmd = NULL;
-		io->addr = 0;
+		io->buf.addr = 0;
 
 		/*
 		 * old task is PF_EXITING, put it now
@@ -2034,13 +2035,16 @@ static inline int ublk_check_cmd_op(u32 cmd_op)
 
 static inline int ublk_set_auto_buf_reg(struct ublk_io *io, struct io_uring_cmd *cmd)
 {
-	io->buf = ublk_sqe_addr_to_auto_buf_reg(READ_ONCE(cmd->sqe->addr));
+	struct ublk_auto_buf_reg buf;
+
+	buf = ublk_sqe_addr_to_auto_buf_reg(READ_ONCE(cmd->sqe->addr));
 
-	if (io->buf.reserved0 || io->buf.reserved1)
+	if (buf.reserved0 || buf.reserved1)
 		return -EINVAL;
 
-	if (io->buf.flags & ~UBLK_AUTO_BUF_REG_F_MASK)
+	if (buf.flags & ~UBLK_AUTO_BUF_REG_F_MASK)
 		return -EINVAL;
+	io->buf.auto_reg = buf;
 	return 0;
 }
 
@@ -2062,7 +2066,7 @@ static int ublk_handle_auto_buf_reg(struct ublk_io *io,
 		 * this ublk request gets stuck.
 		 */
 		if (io->buf_ctx_handle == io_uring_cmd_ctx_handle(cmd))
-			*buf_idx = io->buf.index;
+			*buf_idx = io->buf.auto_reg.index;
 	}
 
 	return ublk_set_auto_buf_reg(io, cmd);
@@ -2090,7 +2094,7 @@ ublk_config_io_buf(const struct ublk_device *ub, struct ublk_io *io,
 	if (ublk_dev_support_auto_buf_reg(ub))
 		return ublk_handle_auto_buf_reg(io, cmd, buf_idx);
 
-	io->addr = buf_addr;
+	io->buf.addr = buf_addr;
 	return 0;
 }
 
@@ -2287,7 +2291,7 @@ static bool ublk_get_data(const struct ublk_queue *ubq, struct ublk_io *io,
 	 */
 	io->flags &= ~UBLK_IO_FLAG_NEED_GET_DATA;
 	/* update iod->addr because ublksrv may have passed a new io buffer */
-	ublk_get_iod(ubq, req->tag)->addr = io->addr;
+	ublk_get_iod(ubq, req->tag)->addr = io->buf.addr;
 	pr_devel("%s: update iod->addr: qid %d tag %d io_flags %x addr %llx\n",
 			__func__, ubq->q_id, req->tag, io->flags,
 			ublk_get_iod(ubq, req->tag)->addr);
Signature · 1 line
2.47.0
author
#9 lore raw

PATCH v3 4/27 ublk: refactor auto buffer register in ublk_dispatch_req()

In reply to Ming Lei

Refactor auto buffer register code and prepare for supporting batch IO feature, and the main motivation is to put 'ublk_io' operation code together, so that per-io lock can be applied for the code block.

The key changes are:
- Rename ublk_auto_buf_reg() as ublk_do_auto_buf_reg()
- Introduce an enum auto_buf_reg_res to represent the result of the buffer registration attempt (FAIL, FALLBACK, OK).
- Split the existing ublk_do_auto_buf_reg function into two:
- __ublk_do_auto_buf_reg: Performs the actual buffer registration and returns the auto_buf_reg_res status.
- ublk_do_auto_buf_reg: A wrapper that calls the internal function and handles the I/O preparation based on the result.
- Introduce ublk_prep_auto_buf_reg_io to encapsulate the logic for preparing the I/O for completion after buffer registration.
- Pass the tag directly to ublk_auto_buf_reg_fallback to avoid recalculating it.

This refactoring makes the control flow clearer and isolates the different stages of the auto buffer registration process.

---
 drivers/block/ublk_drv.c | 64 +++++++++++++++++++++++++++-------------
 1 file changed, 43 insertions(+), 21 deletions(-)
drivers/block/ublk_drv.c +43 -21
index f1fa5ceacdf6..b36cd55eceb0 100644
@@ -1168,17 +1168,37 @@ static inline void __ublk_abort_rq(struct ublk_queue *ubq,
 }
 
 static void
-ublk_auto_buf_reg_fallback(const struct ublk_queue *ubq, struct ublk_io *io)
+ublk_auto_buf_reg_fallback(const struct ublk_queue *ubq, unsigned tag)
 {
-	unsigned tag = io - ubq->ios;
 	struct ublksrv_io_desc *iod = ublk_get_iod(ubq, tag);
 
 	iod->op_flags |= UBLK_IO_F_NEED_REG_BUF;
 }
 
-static bool ublk_auto_buf_reg(const struct ublk_queue *ubq, struct request *req,
-			      struct ublk_io *io, struct io_uring_cmd *cmd,
-			      unsigned int issue_flags)
+enum auto_buf_reg_res {
+	AUTO_BUF_REG_FAIL,
+	AUTO_BUF_REG_FALLBACK,
+	AUTO_BUF_REG_OK,
+};
+
+static void ublk_prep_auto_buf_reg_io(const struct ublk_queue *ubq,
+				      struct request *req, struct ublk_io *io,
+				      struct io_uring_cmd *cmd,
+				      enum auto_buf_reg_res res)
+{
+	if (res == AUTO_BUF_REG_OK) {
+		io->task_registered_buffers = 1;
+		io->buf_ctx_handle = io_uring_cmd_ctx_handle(cmd);
+		io->flags |= UBLK_IO_FLAG_AUTO_BUF_REG;
+	}
+	ublk_init_req_ref(ubq, io);
+	__ublk_prep_compl_io_cmd(io, req);
+}
+
+static enum auto_buf_reg_res
+__ublk_do_auto_buf_reg(const struct ublk_queue *ubq, struct request *req,
+		       struct ublk_io *io, struct io_uring_cmd *cmd,
+		       unsigned int issue_flags)
 {
 	int ret;
 
@@ -1186,29 +1206,27 @@ static bool ublk_auto_buf_reg(const struct ublk_queue *ubq, struct request *req,
 				      io->buf.auto_reg.index, issue_flags);
 	if (ret) {
 		if (io->buf.auto_reg.flags & UBLK_AUTO_BUF_REG_FALLBACK) {
-			ublk_auto_buf_reg_fallback(ubq, io);
-			return true;
+			ublk_auto_buf_reg_fallback(ubq, req->tag);
+			return AUTO_BUF_REG_FALLBACK;
 		}
 		blk_mq_end_request(req, BLK_STS_IOERR);
-		return false;
+		return AUTO_BUF_REG_FAIL;
 	}
 
-	io->task_registered_buffers = 1;
-	io->buf_ctx_handle = io_uring_cmd_ctx_handle(cmd);
-	io->flags |= UBLK_IO_FLAG_AUTO_BUF_REG;
-	return true;
+	return AUTO_BUF_REG_OK;
 }
 
-static bool ublk_prep_auto_buf_reg(struct ublk_queue *ubq,
-				   struct request *req, struct ublk_io *io,
-				   struct io_uring_cmd *cmd,
-				   unsigned int issue_flags)
+static void ublk_do_auto_buf_reg(const struct ublk_queue *ubq, struct request *req,
+				 struct ublk_io *io, struct io_uring_cmd *cmd,
+				 unsigned int issue_flags)
 {
-	ublk_init_req_ref(ubq, io);
-	if (ublk_support_auto_buf_reg(ubq) && ublk_rq_has_data(req))
-		return ublk_auto_buf_reg(ubq, req, io, cmd, issue_flags);
+	enum auto_buf_reg_res res = __ublk_do_auto_buf_reg(ubq, req, io, cmd,
+			issue_flags);
 
-	return true;
+	if (res != AUTO_BUF_REG_FAIL) {
+		ublk_prep_auto_buf_reg_io(ubq, req, io, cmd, res);
+		io_uring_cmd_done(cmd, UBLK_IO_RES_OK, issue_flags);
+	}
 }
 
 static bool ublk_start_io(const struct ublk_queue *ubq, struct request *req,
@@ -1281,8 +1299,12 @@ static void ublk_dispatch_req(struct ublk_queue *ubq,
 	if (!ublk_start_io(ubq, req, io))
 		return;
 
-	if (ublk_prep_auto_buf_reg(ubq, req, io, io->cmd, issue_flags))
+	if (ublk_support_auto_buf_reg(ubq) && ublk_rq_has_data(req)) {
+		ublk_do_auto_buf_reg(ubq, req, io, io->cmd, issue_flags);
+	} else {
+		ublk_init_req_ref(ubq, io);
 		ublk_complete_io_cmd(io, req, UBLK_IO_RES_OK, issue_flags);
+	}
 }
 
 static void ublk_cmd_tw_cb(struct io_uring_cmd *cmd,
Signature · 1 line
2.47.0
Reviewed-by on v3 4/27
#10 lore raw

Re: [PATCH V3 04/27] ublk: refactor auto buffer register in ublk_dispatch_req()

Quoted 19 lines · Ming Lei

Refactor auto buffer register code and prepare for supporting batch IO feature, and the main motivation is to put 'ublk_io' operation code together, so that per-io lock can be applied for the code block.

The key changes are:
- Rename ublk_auto_buf_reg() as ublk_do_auto_buf_reg()
- Introduce an enum auto_buf_reg_res to represent the result of the buffer registration attempt (FAIL, FALLBACK, OK).
- Split the existing ublk_do_auto_buf_reg function into two:
- __ublk_do_auto_buf_reg: Performs the actual buffer registration and returns the auto_buf_reg_res status.
- ublk_do_auto_buf_reg: A wrapper that calls the internal function and handles the I/O preparation based on the result.
- Introduce ublk_prep_auto_buf_reg_io to encapsulate the logic for preparing the I/O for completion after buffer registration.
- Pass the tag directly to ublk_auto_buf_reg_fallback to avoid recalculating it.

Quoted 4 lines

This refactoring makes the control flow clearer and isolates the different stages of the auto buffer registration process.

Quoted 111 lines
---
 drivers/block/ublk_drv.c | 64 +++++++++++++++++++++++++++-------------
 1 file changed, 43 insertions(+), 21 deletions(-)
drivers/block/ublk_drv.c +43 -22
index f1fa5ceacdf6..b36cd55eceb0 100644
@@ -1168,17 +1168,37 @@ static inline void __ublk_abort_rq(struct ublk_queue *ubq,
 }
 
 static void
-ublk_auto_buf_reg_fallback(const struct ublk_queue *ubq, struct ublk_io *io)
+ublk_auto_buf_reg_fallback(const struct ublk_queue *ubq, unsigned tag)
 {
-       unsigned tag = io - ubq->ios;
        struct ublksrv_io_desc *iod = ublk_get_iod(ubq, tag);
 
        iod->op_flags |= UBLK_IO_F_NEED_REG_BUF;
 }
 
-static bool ublk_auto_buf_reg(const struct ublk_queue *ubq, struct request *req,
-                             struct ublk_io *io, struct io_uring_cmd *cmd,
-                             unsigned int issue_flags)
+enum auto_buf_reg_res {
+       AUTO_BUF_REG_FAIL,
+       AUTO_BUF_REG_FALLBACK,
+       AUTO_BUF_REG_OK,
+};
+
+static void ublk_prep_auto_buf_reg_io(const struct ublk_queue *ubq,
+                                     struct request *req, struct ublk_io *io,
+                                     struct io_uring_cmd *cmd,
+                                     enum auto_buf_reg_res res)
+{
+       if (res == AUTO_BUF_REG_OK) {
+               io->task_registered_buffers = 1;
+               io->buf_ctx_handle = io_uring_cmd_ctx_handle(cmd);
+               io->flags |= UBLK_IO_FLAG_AUTO_BUF_REG;
+       }
+       ublk_init_req_ref(ubq, io);
+       __ublk_prep_compl_io_cmd(io, req);
+}
+
+static enum auto_buf_reg_res
+__ublk_do_auto_buf_reg(const struct ublk_queue *ubq, struct request *req,
+                      struct ublk_io *io, struct io_uring_cmd *cmd,
+                      unsigned int issue_flags)
 {
        int ret;
 
@@ -1186,29 +1206,27 @@ static bool ublk_auto_buf_reg(const struct ublk_queue *ubq, struct request *req,
                                      io->buf.auto_reg.index, issue_flags);
        if (ret) {
                if (io->buf.auto_reg.flags & UBLK_AUTO_BUF_REG_FALLBACK) {
-                       ublk_auto_buf_reg_fallback(ubq, io);
-                       return true;
+                       ublk_auto_buf_reg_fallback(ubq, req->tag);
+                       return AUTO_BUF_REG_FALLBACK;
                }
                blk_mq_end_request(req, BLK_STS_IOERR);
-               return false;
+               return AUTO_BUF_REG_FAIL;
        }
 
-       io->task_registered_buffers = 1;
-       io->buf_ctx_handle = io_uring_cmd_ctx_handle(cmd);
-       io->flags |= UBLK_IO_FLAG_AUTO_BUF_REG;
-       return true;
+       return AUTO_BUF_REG_OK;
 }
 
-static bool ublk_prep_auto_buf_reg(struct ublk_queue *ubq,
-                                  struct request *req, struct ublk_io *io,
-                                  struct io_uring_cmd *cmd,
-                                  unsigned int issue_flags)
+static void ublk_do_auto_buf_reg(const struct ublk_queue *ubq, struct request *req,
+                                struct ublk_io *io, struct io_uring_cmd *cmd,
+                                unsigned int issue_flags)
 {
-       ublk_init_req_ref(ubq, io);
-       if (ublk_support_auto_buf_reg(ubq) && ublk_rq_has_data(req))
-               return ublk_auto_buf_reg(ubq, req, io, cmd, issue_flags);
+       enum auto_buf_reg_res res = __ublk_do_auto_buf_reg(ubq, req, io, cmd,
+                       issue_flags);
 
-       return true;
+       if (res != AUTO_BUF_REG_FAIL) {
+               ublk_prep_auto_buf_reg_io(ubq, req, io, cmd, res);
+               io_uring_cmd_done(cmd, UBLK_IO_RES_OK, issue_flags);
+       }
 }
 
 static bool ublk_start_io(const struct ublk_queue *ubq, struct request *req,
@@ -1281,8 +1299,12 @@ static void ublk_dispatch_req(struct ublk_queue *ubq,
        if (!ublk_start_io(ubq, req, io))
                return;
 
-       if (ublk_prep_auto_buf_reg(ubq, req, io, io->cmd, issue_flags))
+       if (ublk_support_auto_buf_reg(ubq) && ublk_rq_has_data(req)) {
+               ublk_do_auto_buf_reg(ubq, req, io, io->cmd, issue_flags);
+       } else {
+               ublk_init_req_ref(ubq, io);
                ublk_complete_io_cmd(io, req, UBLK_IO_RES_OK, issue_flags);
+       }
 }
 
 static void ublk_cmd_tw_cb(struct io_uring_cmd *cmd,
--

2.47.0

author
#11 lore raw

PATCH v3 5/27 ublk: pass const pointer to ublk_queue_is_zoned()

In reply to Ming Lei

Pass const pointer to ublk_queue_is_zoned() because it is readonly.

---
 drivers/block/ublk_drv.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/block/ublk_drv.c +1 -1
index b36cd55eceb0..5e83c1b2a69e 100644
@@ -265,7 +265,7 @@ static inline bool ublk_dev_is_zoned(const struct ublk_device *ub)
 	return ub->dev_info.flags & UBLK_F_ZONED;
 }
 
-static inline bool ublk_queue_is_zoned(struct ublk_queue *ubq)
+static inline bool ublk_queue_is_zoned(const struct ublk_queue *ubq)
 {
 	return ubq->flags & UBLK_F_ZONED;
 }
Signature · 1 line
2.47.0
Reviewed-by on v3 5/27
#12 lore raw

Re: [PATCH V3 05/27] ublk: pass const pointer to ublk_queue_is_zoned()

Quoted 4 lines · Ming Lei

Pass const pointer to ublk_queue_is_zoned() because it is readonly.

Quoted 20 lines
---
 drivers/block/ublk_drv.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/block/ublk_drv.c +1 -2
index b36cd55eceb0..5e83c1b2a69e 100644
@@ -265,7 +265,7 @@ static inline bool ublk_dev_is_zoned(const struct ublk_device *ub)
        return ub->dev_info.flags & UBLK_F_ZONED;
 }
 
-static inline bool ublk_queue_is_zoned(struct ublk_queue *ubq)
+static inline bool ublk_queue_is_zoned(const struct ublk_queue *ubq)
 {
        return ubq->flags & UBLK_F_ZONED;
 }
--

2.47.0

author
#13 lore raw

PATCH v3 6/27 ublk: add helper of __ublk_fetch()

In reply to Ming Lei

Add helper __ublk_fetch() for refactoring ublk_fetch().

Meantime move ublk_config_io_buf() out of __ublk_fetch() to make the code structure cleaner.

---
 drivers/block/ublk_drv.c | 46 +++++++++++++++++++++-------------------
 1 file changed, 24 insertions(+), 22 deletions(-)
drivers/block/ublk_drv.c +24 -22
index 5e83c1b2a69e..dd9c35758a46 100644
@@ -2234,39 +2234,41 @@ static int ublk_check_fetch_buf(const struct ublk_device *ub, __u64 buf_addr)
 	return 0;
 }
 
-static int ublk_fetch(struct io_uring_cmd *cmd, struct ublk_device *ub,
-		      struct ublk_io *io, __u64 buf_addr)
+static int __ublk_fetch(struct io_uring_cmd *cmd, struct ublk_device *ub,
+			struct ublk_io *io)
 {
-	int ret = 0;
-
-	/*
-	 * When handling FETCH command for setting up ublk uring queue,
-	 * ub->mutex is the innermost lock, and we won't block for handling
-	 * FETCH, so it is fine even for IO_URING_F_NONBLOCK.
-	 */
-	mutex_lock(&ub->mutex);
 	/* UBLK_IO_FETCH_REQ is only allowed before dev is setup */
-	if (ublk_dev_ready(ub)) {
-		ret = -EBUSY;
-		goto out;
-	}
+	if (ublk_dev_ready(ub))
+		return -EBUSY;
 
 	/* allow each command to be FETCHed at most once */
-	if (io->flags & UBLK_IO_FLAG_ACTIVE) {
-		ret = -EINVAL;
-		goto out;
-	}
+	if (io->flags & UBLK_IO_FLAG_ACTIVE)
+		return -EINVAL;
 
 	WARN_ON_ONCE(io->flags & UBLK_IO_FLAG_OWNED_BY_SRV);
 
 	ublk_fill_io_cmd(io, cmd);
-	ret = ublk_config_io_buf(ub, io, cmd, buf_addr, NULL);
-	if (ret)
-		goto out;
 
 	WRITE_ONCE(io->task, get_task_struct(current));
 	ublk_mark_io_ready(ub);
-out:
+
+	return 0;
+}
+
+static int ublk_fetch(struct io_uring_cmd *cmd, struct ublk_device *ub,
+		      struct ublk_io *io, __u64 buf_addr)
+{
+	int ret;
+
+	/*
+	 * When handling FETCH command for setting up ublk uring queue,
+	 * ub->mutex is the innermost lock, and we won't block for handling
+	 * FETCH, so it is fine even for IO_URING_F_NONBLOCK.
+	 */
+	mutex_lock(&ub->mutex);
+	ret = __ublk_fetch(cmd, ub, io);
+	if (!ret)
+		ret = ublk_config_io_buf(ub, io, cmd, buf_addr, NULL);
 	mutex_unlock(&ub->mutex);
 	return ret;
 }
Signature · 1 line
2.47.0
#14 lore raw

Re: [PATCH V3 06/27] ublk: add helper of __ublk_fetch()

Quoted 66 lines · Ming Lei

Add helper __ublk_fetch() for refactoring ublk_fetch().

Meantime move ublk_config_io_buf() out of __ublk_fetch() to make the code structure cleaner.

---
 drivers/block/ublk_drv.c | 46 +++++++++++++++++++++-------------------
 1 file changed, 24 insertions(+), 22 deletions(-)
drivers/block/ublk_drv.c +14 -22
index 5e83c1b2a69e..dd9c35758a46 100644
@@ -2234,39 +2234,41 @@ static int ublk_check_fetch_buf(const struct ublk_device *ub, __u64 buf_addr)
        return 0;
 }
 
-static int ublk_fetch(struct io_uring_cmd *cmd, struct ublk_device *ub,
-                     struct ublk_io *io, __u64 buf_addr)
+static int __ublk_fetch(struct io_uring_cmd *cmd, struct ublk_device *ub,
+                       struct ublk_io *io)
 {
-       int ret = 0;
-
-       /*
-        * When handling FETCH command for setting up ublk uring queue,
-        * ub->mutex is the innermost lock, and we won't block for handling
-        * FETCH, so it is fine even for IO_URING_F_NONBLOCK.
-        */
-       mutex_lock(&ub->mutex);
        /* UBLK_IO_FETCH_REQ is only allowed before dev is setup */
-       if (ublk_dev_ready(ub)) {
-               ret = -EBUSY;
-               goto out;
-       }
+       if (ublk_dev_ready(ub))
+               return -EBUSY;
 
        /* allow each command to be FETCHed at most once */
-       if (io->flags & UBLK_IO_FLAG_ACTIVE) {
-               ret = -EINVAL;
-               goto out;
-       }
+       if (io->flags & UBLK_IO_FLAG_ACTIVE)
+               return -EINVAL;
 
        WARN_ON_ONCE(io->flags & UBLK_IO_FLAG_OWNED_BY_SRV);
 
        ublk_fill_io_cmd(io, cmd);
-       ret = ublk_config_io_buf(ub, io, cmd, buf_addr, NULL);
-       if (ret)
-               goto out;
 
        WRITE_ONCE(io->task, get_task_struct(current));
        ublk_mark_io_ready(ub);
-out:
+
+       return 0;
+}
+
+static int ublk_fetch(struct io_uring_cmd *cmd, struct ublk_device *ub,
+                     struct ublk_io *io, __u64 buf_addr)
+{
+       int ret;

drivers/block/ublk_drv.c static int ublk_check_fetch_buf(const struct ublk_device *ub, __u64 buf_addr) · line 2271

Quoted 10 lines
drivers/block/ublk_drv.c +10 -0
+
+       /*
+        * When handling FETCH command for setting up ublk uring queue,
+        * ub->mutex is the innermost lock, and we won't block for handling
+        * FETCH, so it is fine even for IO_URING_F_NONBLOCK.
+        */
+       mutex_lock(&ub->mutex);
+       ret = __ublk_fetch(cmd, ub, io);
+       if (!ret)
+               ret = ublk_config_io_buf(ub, io, cmd, buf_addr, NULL);

This changes ublk_config_io_buf() to be called *after* ublk_mark_io_ready(). Is that safe? It seems like io->addr could be read in ublk_setup_iod() as soon as the ublk device is marked as ready for I/O.

Best,
Caleb

Quoted 6 lines
drivers/block/ublk_drv.c +0 -1
        mutex_unlock(&ub->mutex);
        return ret;
 }
--

2.47.0

author
#15 lore raw

Re: [PATCH V3 06/27] ublk: add helper of __ublk_fetch()

Quoted 74 lines · Caleb Sander Mateos
Quoted 73 lines · Ming Lei

Add helper __ublk_fetch() for refactoring ublk_fetch().

Meantime move ublk_config_io_buf() out of __ublk_fetch() to make the code structure cleaner.

---
 drivers/block/ublk_drv.c | 46 +++++++++++++++++++++-------------------
 1 file changed, 24 insertions(+), 22 deletions(-)
drivers/block/ublk_drv.c +21 -22
index 5e83c1b2a69e..dd9c35758a46 100644
@@ -2234,39 +2234,41 @@ static int ublk_check_fetch_buf(const struct ublk_device *ub, __u64 buf_addr)
        return 0;
 }
 
-static int ublk_fetch(struct io_uring_cmd *cmd, struct ublk_device *ub,
-                     struct ublk_io *io, __u64 buf_addr)
+static int __ublk_fetch(struct io_uring_cmd *cmd, struct ublk_device *ub,
+                       struct ublk_io *io)
 {
-       int ret = 0;
-
-       /*
-        * When handling FETCH command for setting up ublk uring queue,
-        * ub->mutex is the innermost lock, and we won't block for handling
-        * FETCH, so it is fine even for IO_URING_F_NONBLOCK.
-        */
-       mutex_lock(&ub->mutex);
        /* UBLK_IO_FETCH_REQ is only allowed before dev is setup */
-       if (ublk_dev_ready(ub)) {
-               ret = -EBUSY;
-               goto out;
-       }
+       if (ublk_dev_ready(ub))
+               return -EBUSY;
 
        /* allow each command to be FETCHed at most once */
-       if (io->flags & UBLK_IO_FLAG_ACTIVE) {
-               ret = -EINVAL;
-               goto out;
-       }
+       if (io->flags & UBLK_IO_FLAG_ACTIVE)
+               return -EINVAL;
 
        WARN_ON_ONCE(io->flags & UBLK_IO_FLAG_OWNED_BY_SRV);
 
        ublk_fill_io_cmd(io, cmd);
-       ret = ublk_config_io_buf(ub, io, cmd, buf_addr, NULL);
-       if (ret)
-               goto out;
 
        WRITE_ONCE(io->task, get_task_struct(current));
        ublk_mark_io_ready(ub);
-out:
+
+       return 0;
+}
+
+static int ublk_fetch(struct io_uring_cmd *cmd, struct ublk_device *ub,
+                     struct ublk_io *io, __u64 buf_addr)
+{
+       int ret;
+
+       /*
+        * When handling FETCH command for setting up ublk uring queue,
+        * ub->mutex is the innermost lock, and we won't block for handling
+        * FETCH, so it is fine even for IO_URING_F_NONBLOCK.
+        */
+       mutex_lock(&ub->mutex);
Quoted 8 lines

drivers/block/ublk_drv.c static int ublk_check_fetch_buf(const struct ublk_device *ub, __u64 buf_addr) · line 2271

Quoted 3 lines
drivers/block/ublk_drv.c +3 -0
+       ret = __ublk_fetch(cmd, ub, io);
+       if (!ret)
+               ret = ublk_config_io_buf(ub, io, cmd, buf_addr, NULL);

This changes ublk_config_io_buf() to be called *after* ublk_mark_io_ready(). Is that safe? It seems like io->addr could be read in ublk_setup_iod() as soon as the ublk device is marked as ready for I/O.

disk can't be added unless acquiring ub->mutex, so it is safe.

Thanks,
Ming

#16 lore raw

Re: [PATCH V3 06/27] ublk: add helper of __ublk_fetch()

Quoted 85 lines · Ming Lei
Quoted 74 lines · Caleb Sander Mateos
Quoted 73 lines · Ming Lei

Add helper __ublk_fetch() for refactoring ublk_fetch().

Meantime move ublk_config_io_buf() out of __ublk_fetch() to make the code structure cleaner.

---
 drivers/block/ublk_drv.c | 46 +++++++++++++++++++++-------------------
 1 file changed, 24 insertions(+), 22 deletions(-)
drivers/block/ublk_drv.c +21 -22
index 5e83c1b2a69e..dd9c35758a46 100644
@@ -2234,39 +2234,41 @@ static int ublk_check_fetch_buf(const struct ublk_device *ub, __u64 buf_addr)
        return 0;
 }
 
-static int ublk_fetch(struct io_uring_cmd *cmd, struct ublk_device *ub,
-                     struct ublk_io *io, __u64 buf_addr)
+static int __ublk_fetch(struct io_uring_cmd *cmd, struct ublk_device *ub,
+                       struct ublk_io *io)
 {
-       int ret = 0;
-
-       /*
-        * When handling FETCH command for setting up ublk uring queue,
-        * ub->mutex is the innermost lock, and we won't block for handling
-        * FETCH, so it is fine even for IO_URING_F_NONBLOCK.
-        */
-       mutex_lock(&ub->mutex);
        /* UBLK_IO_FETCH_REQ is only allowed before dev is setup */
-       if (ublk_dev_ready(ub)) {
-               ret = -EBUSY;
-               goto out;
-       }
+       if (ublk_dev_ready(ub))
+               return -EBUSY;
 
        /* allow each command to be FETCHed at most once */
-       if (io->flags & UBLK_IO_FLAG_ACTIVE) {
-               ret = -EINVAL;
-               goto out;
-       }
+       if (io->flags & UBLK_IO_FLAG_ACTIVE)
+               return -EINVAL;
 
        WARN_ON_ONCE(io->flags & UBLK_IO_FLAG_OWNED_BY_SRV);
 
        ublk_fill_io_cmd(io, cmd);
-       ret = ublk_config_io_buf(ub, io, cmd, buf_addr, NULL);
-       if (ret)
-               goto out;
 
        WRITE_ONCE(io->task, get_task_struct(current));
        ublk_mark_io_ready(ub);
-out:
+
+       return 0;
+}
+
+static int ublk_fetch(struct io_uring_cmd *cmd, struct ublk_device *ub,
+                     struct ublk_io *io, __u64 buf_addr)
+{
+       int ret;
+
+       /*
+        * When handling FETCH command for setting up ublk uring queue,
+        * ub->mutex is the innermost lock, and we won't block for handling
+        * FETCH, so it is fine even for IO_URING_F_NONBLOCK.
+        */
+       mutex_lock(&ub->mutex);
Quoted 8 lines

drivers/block/ublk_drv.c static int ublk_check_fetch_buf(const struct ublk_device *ub, __u64 buf_addr) · line 2271

Quoted 3 lines
drivers/block/ublk_drv.c +3 -0
+       ret = __ublk_fetch(cmd, ub, io);
+       if (!ret)
+               ret = ublk_config_io_buf(ub, io, cmd, buf_addr, NULL);

This changes ublk_config_io_buf() to be called *after* ublk_mark_io_ready(). Is that safe? It seems like io->addr could be read in ublk_setup_iod() as soon as the ublk device is marked as ready for I/O.

Quoted 1 line

disk can't be added unless acquiring ub->mutex, so it is safe.

Okay, I see what you mean. ub->mutex is acquired after wait_for_completion_interruptible() in ublk_ctrl_start_dev() and ublk_ctrl_end_recovery() before allowing I/O to the ublk device. Then,

author
#17 lore raw

PATCH v3 7/27 ublk: define ublk_ch_batch_io_fops for the coming feature F_BATCH_IO

In reply to Ming Lei

Introduces the basic structure for a batched I/O feature in the ublk driver. It adds placeholder functions and a new file operations structure, ublk_ch_batch_io_fops, which will be used for fetching and committing I/O commands in batches. Currently, the feature is disabled.

---
 drivers/block/ublk_drv.c | 26 +++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)
drivers/block/ublk_drv.c +25 -1
index dd9c35758a46..1fcca52591c3 100644
@@ -254,6 +254,11 @@ static inline struct request *__ublk_check_and_get_req(struct ublk_device *ub,
 		u16 q_id, u16 tag, struct ublk_io *io, size_t offset);
 static inline unsigned int ublk_req_build_flags(struct request *req);
 
+static inline bool ublk_dev_support_batch_io(const struct ublk_device *ub)
+{
+	return false;
+}
+
 static inline struct ublksrv_io_desc *
 ublk_get_iod(const struct ublk_queue *ubq, unsigned tag)
 {
@@ -2512,6 +2517,12 @@ static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags)
 	return ublk_ch_uring_cmd_local(cmd, issue_flags);
 }
 
+static int ublk_ch_batch_io_uring_cmd(struct io_uring_cmd *cmd,
+				       unsigned int issue_flags)
+{
+	return -EOPNOTSUPP;
+}
+
 static inline bool ublk_check_ubuf_dir(const struct request *req,
 		int ubuf_dir)
 {
@@ -2618,6 +2629,16 @@ static const struct file_operations ublk_ch_fops = {
 	.mmap = ublk_ch_mmap,
 };
 
+static const struct file_operations ublk_ch_batch_io_fops = {
+	.owner = THIS_MODULE,
+	.open = ublk_ch_open,
+	.release = ublk_ch_release,
+	.read_iter = ublk_ch_read_iter,
+	.write_iter = ublk_ch_write_iter,
+	.uring_cmd = ublk_ch_batch_io_uring_cmd,
+	.mmap = ublk_ch_mmap,
+};
+
 static void ublk_deinit_queue(struct ublk_device *ub, int q_id)
 {
 	struct ublk_queue *ubq = ub->queues[q_id];
@@ -2778,7 +2799,10 @@ static int ublk_add_chdev(struct ublk_device *ub)
 	if (ret)
 		goto fail;
 
-	cdev_init(&ub->cdev, &ublk_ch_fops);
+	if (ublk_dev_support_batch_io(ub))
+		cdev_init(&ub->cdev, &ublk_ch_batch_io_fops);
+	else
+		cdev_init(&ub->cdev, &ublk_ch_fops);
 	ret = cdev_device_add(&ub->cdev, dev);
 	if (ret)
 		goto fail;
Signature · 1 line
2.47.0
author
#18 lore raw

PATCH v3 8/27 ublk: prepare for not tracking task context for command batch

In reply to Ming Lei

batch io is designed to be independent of task context, and we will not track task context for batch io feature.

So warn on non-batch-io code paths.

---
 drivers/block/ublk_drv.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/block/ublk_drv.c +4 -1
index 1fcca52591c3..c62b2f2057fe 100644
@@ -2254,7 +2254,10 @@ static int __ublk_fetch(struct io_uring_cmd *cmd, struct ublk_device *ub,
 
 	ublk_fill_io_cmd(io, cmd);
 
-	WRITE_ONCE(io->task, get_task_struct(current));
+	if (ublk_dev_support_batch_io(ub))
+		WRITE_ONCE(io->task, NULL);
+	else
+		WRITE_ONCE(io->task, get_task_struct(current));
 	ublk_mark_io_ready(ub);
 
 	return 0;
Signature · 1 line
2.47.0
Reviewed-by on v3 8/27
#19 lore raw

Re: [PATCH V3 08/27] ublk: prepare for not tracking task context for command batch

Quoted 5 lines · Ming Lei

batch io is designed to be independent of task context, and we will not track task context for batch io feature.

So warn on non-batch-io code paths.

I don't see any warning? Remove this sentence from the commit description?

Other than that,

Quoted 25 lines
---
 drivers/block/ublk_drv.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/block/ublk_drv.c +4 -2
index 1fcca52591c3..c62b2f2057fe 100644
@@ -2254,7 +2254,10 @@ static int __ublk_fetch(struct io_uring_cmd *cmd, struct ublk_device *ub,
 
        ublk_fill_io_cmd(io, cmd);
 
-       WRITE_ONCE(io->task, get_task_struct(current));
+       if (ublk_dev_support_batch_io(ub))
+               WRITE_ONCE(io->task, NULL);
+       else
+               WRITE_ONCE(io->task, get_task_struct(current));
        ublk_mark_io_ready(ub);
 
        return 0;
--

2.47.0

author
#20 lore raw

Re: [PATCH V3 08/27] ublk: prepare for not tracking task context for command batch

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

batch io is designed to be independent of task context, and we will not track task context for batch io feature.

So warn on non-batch-io code paths.

I don't see any warning? Remove this sentence from the commit description?

Yeah, will remove it in next version.

Quoted 2 lines

Other than that,

Thanks,
Ming

author
#21 lore raw

PATCH v3 9/27 ublk: add new batch command UBLK_U_IO_PREP_IO_CMDS & UBLK_U_IO_COMMIT_IO_CMDS

In reply to Ming Lei

Add new command UBLK_U_IO_PREP_IO_CMDS, which is the batch version of UBLK_IO_FETCH_REQ.

Add new command UBLK_U_IO_COMMIT_IO_CMDS, which is for committing io command result only, still the batch version.

The new command header type is struct ublk_batch_io, and fixed buffer is required for these two uring_cmd.

This patch doesn't actually implement these commands yet, just validates the SQE fields.

---
 drivers/block/ublk_drv.c      | 107 +++++++++++++++++++++++++++++++++-
 include/uapi/linux/ublk_cmd.h |  49 ++++++++++++++++
 2 files changed, 155 insertions(+), 1 deletion(-)
drivers/block/ublk_drv.c +106 -1
index c62b2f2057fe..5f9d7ec9daa4 100644
@@ -85,6 +85,11 @@
 	 UBLK_PARAM_TYPE_DEVT | UBLK_PARAM_TYPE_ZONED |    \
 	 UBLK_PARAM_TYPE_DMA_ALIGN | UBLK_PARAM_TYPE_SEGMENT)
 
+#define UBLK_BATCH_F_ALL  \
+	(UBLK_BATCH_F_HAS_ZONE_LBA | \
+	 UBLK_BATCH_F_HAS_BUF_ADDR | \
+	 UBLK_BATCH_F_AUTO_BUF_REG_FALLBACK)
+
 struct ublk_uring_cmd_pdu {
 	/*
 	 * Store requests in same batch temporarily for queuing them to
@@ -108,6 +113,12 @@ struct ublk_uring_cmd_pdu {
 	u16 tag;
 };
 
+struct ublk_batch_io_data {
+	struct ublk_device *ub;
+	struct io_uring_cmd *cmd;
+	struct ublk_batch_io header;
+};
+
 /*
  * io command is active: sqe cmd is received, and its cqe isn't done
  *
@@ -2520,10 +2531,104 @@ static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags)
 	return ublk_ch_uring_cmd_local(cmd, issue_flags);
 }
 
+static int ublk_check_batch_cmd_flags(const struct ublk_batch_io *uc)
+{
+	const u16 mask = UBLK_BATCH_F_HAS_BUF_ADDR | UBLK_BATCH_F_HAS_ZONE_LBA;
+	const unsigned header_len = sizeof(struct ublk_elem_header);
+
+	if (uc->flags & ~UBLK_BATCH_F_ALL)
+		return -EINVAL;
+
+	/* UBLK_BATCH_F_AUTO_BUF_REG_FALLBACK requires buffer index */
+	if ((uc->flags & UBLK_BATCH_F_AUTO_BUF_REG_FALLBACK) &&
+			(uc->flags & UBLK_BATCH_F_HAS_BUF_ADDR))
+		return -EINVAL;
+
+	switch (uc->flags & mask) {
+	case 0:
+		if (uc->elem_bytes != header_len)
+			return -EINVAL;
+		break;
+	case UBLK_BATCH_F_HAS_ZONE_LBA:
+	case UBLK_BATCH_F_HAS_BUF_ADDR:
+		if (uc->elem_bytes != header_len + sizeof(u64))
+			return -EINVAL;
+		break;
+	case UBLK_BATCH_F_HAS_ZONE_LBA | UBLK_BATCH_F_HAS_BUF_ADDR:
+		if (uc->elem_bytes != header_len + sizeof(u64) + sizeof(u64))
+			return -EINVAL;
+		break;
+	}
+
+	return 0;
+}
+
+static int ublk_check_batch_cmd(const struct ublk_batch_io_data *data)
+{
+
+	const struct ublk_batch_io *uc = &data->header;
+
+	if (!(data->cmd->flags & IORING_URING_CMD_FIXED))
+		return -EINVAL;
+
+	if (uc->nr_elem * uc->elem_bytes > data->cmd->sqe->len)
+		return -E2BIG;
+
+	if (uc->nr_elem > data->ub->dev_info.queue_depth)
+		return -E2BIG;
+
+	if ((uc->flags & UBLK_BATCH_F_HAS_ZONE_LBA) &&
+			!ublk_dev_is_zoned(data->ub))
+		return -EINVAL;
+
+	if ((uc->flags & UBLK_BATCH_F_HAS_BUF_ADDR) &&
+			!ublk_dev_need_map_io(data->ub))
+		return -EINVAL;
+
+	if ((uc->flags & UBLK_BATCH_F_AUTO_BUF_REG_FALLBACK) &&
+			!ublk_dev_support_auto_buf_reg(data->ub))
+		return -EINVAL;
+
+	if (uc->reserved || uc->reserved2)
+		return -EINVAL;
+
+	return ublk_check_batch_cmd_flags(uc);
+}
+
 static int ublk_ch_batch_io_uring_cmd(struct io_uring_cmd *cmd,
 				       unsigned int issue_flags)
 {
-	return -EOPNOTSUPP;
+	const struct ublk_batch_io *uc = io_uring_sqe_cmd(cmd->sqe);
+	struct ublk_device *ub = cmd->file->private_data;
+	struct ublk_batch_io_data data = {
+		.ub  = ub,
+		.cmd = cmd,
+		.header = (struct ublk_batch_io) {
+			.q_id = READ_ONCE(uc->q_id),
+			.flags = READ_ONCE(uc->flags),
+			.nr_elem = READ_ONCE(uc->nr_elem),
+			.elem_bytes = READ_ONCE(uc->elem_bytes),
+		},
+	};
+	u32 cmd_op = cmd->cmd_op;
+	int ret = -EINVAL;
+
+	if (data.header.q_id >= ub->dev_info.nr_hw_queues)
+		goto out;
+
+	switch (cmd_op) {
+	case UBLK_U_IO_PREP_IO_CMDS:
+	case UBLK_U_IO_COMMIT_IO_CMDS:
+		ret = ublk_check_batch_cmd(&data);
+		if (ret)
+			goto out;
+		ret = -EOPNOTSUPP;
+		break;
+	default:
+		ret = -EOPNOTSUPP;
+	}
+out:
+	return ret;
 }
 
 static inline bool ublk_check_ubuf_dir(const struct request *req,
include/uapi/linux/ublk_cmd.h +49 -0

61 lines not on this page · raw message

Signature · 1 line
2.47.0
#22 lore raw

Re: [PATCH V3 09/27] ublk: add new batch command UBLK_U_IO_PREP_IO_CMDS & UBLK_U_IO_COMMIT_IO_CMDS

Quoted 36 lines · Ming Lei

Add new command UBLK_U_IO_PREP_IO_CMDS, which is the batch version of UBLK_IO_FETCH_REQ.

Add new command UBLK_U_IO_COMMIT_IO_CMDS, which is for committing io command result only, still the batch version.

The new command header type is struct ublk_batch_io, and fixed buffer is required for these two uring_cmd.

This patch doesn't actually implement these commands yet, just validates the SQE fields.

---
 drivers/block/ublk_drv.c      | 107 +++++++++++++++++++++++++++++++++-
 include/uapi/linux/ublk_cmd.h |  49 ++++++++++++++++
 2 files changed, 155 insertions(+), 1 deletion(-)
drivers/block/ublk_drv.c +5 -0
index c62b2f2057fe..5f9d7ec9daa4 100644
@@ -85,6 +85,11 @@
         UBLK_PARAM_TYPE_DEVT | UBLK_PARAM_TYPE_ZONED |    \
         UBLK_PARAM_TYPE_DMA_ALIGN | UBLK_PARAM_TYPE_SEGMENT)
 
+#define UBLK_BATCH_F_ALL  \
+       (UBLK_BATCH_F_HAS_ZONE_LBA | \
+        UBLK_BATCH_F_HAS_BUF_ADDR | \
+        UBLK_BATCH_F_AUTO_BUF_REG_FALLBACK)
+
 struct ublk_uring_cmd_pdu {
        /*
         * Store requests in same batch temporarily for queuing them to

drivers/block/ublk_drv.c struct ublk_uring_cmd_pdu { · line 117

Quoted 5 lines
drivers/block/ublk_drv.c +2 -0
@@ -108,6 +113,12 @@ struct ublk_uring_cmd_pdu {
        u16 tag;
 };
 
+struct ublk_batch_io_data {
+       struct ublk_device *ub;

Is it possible for this to be a const pointer?

Quoted 29 lines
drivers/block/ublk_drv.c +22 -0
+       struct io_uring_cmd *cmd;
+       struct ublk_batch_io header;
+};
+
 /*
  * io command is active: sqe cmd is received, and its cqe isn't done
  *
@@ -2520,10 +2531,104 @@ static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags)
        return ublk_ch_uring_cmd_local(cmd, issue_flags);
 }
 
+static int ublk_check_batch_cmd_flags(const struct ublk_batch_io *uc)
+{
+       const u16 mask = UBLK_BATCH_F_HAS_BUF_ADDR | UBLK_BATCH_F_HAS_ZONE_LBA;
+       const unsigned header_len = sizeof(struct ublk_elem_header);
+
+       if (uc->flags & ~UBLK_BATCH_F_ALL)
+               return -EINVAL;
+
+       /* UBLK_BATCH_F_AUTO_BUF_REG_FALLBACK requires buffer index */
+       if ((uc->flags & UBLK_BATCH_F_AUTO_BUF_REG_FALLBACK) &&
+                       (uc->flags & UBLK_BATCH_F_HAS_BUF_ADDR))
+               return -EINVAL;
+
+       switch (uc->flags & mask) {
+       case 0:
+               if (uc->elem_bytes != header_len)
+                       return -EINVAL;
+               break;

drivers/block/ublk_drv.c static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags) · line 2561

Quoted 10 lines
drivers/block/ublk_drv.c +10 -0
+       case UBLK_BATCH_F_HAS_ZONE_LBA:
+       case UBLK_BATCH_F_HAS_BUF_ADDR:
+               if (uc->elem_bytes != header_len + sizeof(u64))
+                       return -EINVAL;
+               break;
+       case UBLK_BATCH_F_HAS_ZONE_LBA | UBLK_BATCH_F_HAS_BUF_ADDR:
+               if (uc->elem_bytes != header_len + sizeof(u64) + sizeof(u64))
+                       return -EINVAL;
+               break;
+       }

This could probably be implemented in a less branchy way using conditional moves:
unsigned elem_bytes = sizeof(struct ublk_elem_header) +

        (uc->flags & UBLK_BATCH_F_HAS_ZONE_LBA ? sizeof(u64) : 0) +
        (uc->flags & UBLK_BATCH_F_HAS_BUF_ADDR ? sizeof(u64) : 0);
if (uc->elem_bytes != elem_bytes)
        return -EINVAL;
Quoted 22 lines
drivers/block/ublk_drv.c +22 -0
+
+       return 0;
+}
+
+static int ublk_check_batch_cmd(const struct ublk_batch_io_data *data)
+{
+
+       const struct ublk_batch_io *uc = &data->header;
+
+       if (!(data->cmd->flags & IORING_URING_CMD_FIXED))
+               return -EINVAL;
+
+       if (uc->nr_elem * uc->elem_bytes > data->cmd->sqe->len)
+               return -E2BIG;
+
+       if (uc->nr_elem > data->ub->dev_info.queue_depth)
+               return -E2BIG;
+
+       if ((uc->flags & UBLK_BATCH_F_HAS_ZONE_LBA) &&
+                       !ublk_dev_is_zoned(data->ub))
+               return -EINVAL;
+

drivers/block/ublk_drv.c static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags) · line 2593

Quoted 10 lines
drivers/block/ublk_drv.c +10 -0
+       if ((uc->flags & UBLK_BATCH_F_HAS_BUF_ADDR) &&
+                       !ublk_dev_need_map_io(data->ub))
+               return -EINVAL;
+
+       if ((uc->flags & UBLK_BATCH_F_AUTO_BUF_REG_FALLBACK) &&
+                       !ublk_dev_support_auto_buf_reg(data->ub))
+               return -EINVAL;
+
+       if (uc->reserved || uc->reserved2)
+               return -EINVAL;

These fields aren't actually copied from the uring_cmd, so this check is a no-op.

Quoted 28 lines
drivers/block/ublk_drv.c +24 -1
+
+       return ublk_check_batch_cmd_flags(uc);
+}
+
 static int ublk_ch_batch_io_uring_cmd(struct io_uring_cmd *cmd,
                                       unsigned int issue_flags)
 {
-       return -EOPNOTSUPP;
+       const struct ublk_batch_io *uc = io_uring_sqe_cmd(cmd->sqe);
+       struct ublk_device *ub = cmd->file->private_data;
+       struct ublk_batch_io_data data = {
+               .ub  = ub,
+               .cmd = cmd,
+               .header = (struct ublk_batch_io) {
+                       .q_id = READ_ONCE(uc->q_id),
+                       .flags = READ_ONCE(uc->flags),
+                       .nr_elem = READ_ONCE(uc->nr_elem),
+                       .elem_bytes = READ_ONCE(uc->elem_bytes),
+               },
+       };
+       u32 cmd_op = cmd->cmd_op;
+       int ret = -EINVAL;
+
+       if (data.header.q_id >= ub->dev_info.nr_hw_queues)
+               goto out;
+
+       switch (cmd_op) {
+       case UBLK_U_IO_PREP_IO_CMDS:

drivers/block/ublk_drv.c static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags) · line 2630

Quoted 10 lines
drivers/block/ublk_drv.c +10 -0
+       case UBLK_U_IO_COMMIT_IO_CMDS:
+               ret = ublk_check_batch_cmd(&data);
+               if (ret)
+                       goto out;
+               ret = -EOPNOTSUPP;
+               break;
+       default:
+               ret = -EOPNOTSUPP;
+       }
+out:

Is the out label really necessary if there's no cleanup involved? Can we just return the result directly?

Best,
Caleb

Quoted 74 lines
drivers/block/ublk_drv.c +1 -0
+       return ret;
 }
 
 static inline bool ublk_check_ubuf_dir(const struct request *req,
include/uapi/linux/ublk_cmd.h +49 -1
index ec77dabba45b..2ce5a496b622 100644
@@ -102,6 +102,10 @@
        _IOWR('u', 0x23, struct ublksrv_io_cmd)
 #define        UBLK_U_IO_UNREGISTER_IO_BUF     \
        _IOWR('u', 0x24, struct ublksrv_io_cmd)
+#define        UBLK_U_IO_PREP_IO_CMDS  \
+       _IOWR('u', 0x25, struct ublk_batch_io)
+#define        UBLK_U_IO_COMMIT_IO_CMDS        \
+       _IOWR('u', 0x26, struct ublk_batch_io)
 
 /* only ABORT means that no re-fetch */
 #define UBLK_IO_RES_OK                 0
@@ -525,6 +529,51 @@ struct ublksrv_io_cmd {
        };
 };
 
+struct ublk_elem_header {
+       __u16 tag;      /* IO tag */
+
+       /*
+        * Buffer index for incoming io command, only valid iff
+        * UBLK_F_AUTO_BUF_REG is set
+        */
+       __u16 buf_index;
+       __s32 result;   /* I/O completion result (commit only) */
+};
+
+/*
+ * uring_cmd buffer structure for batch commands
+ *
+ * buffer includes multiple elements, which number is specified by
+ * `nr_elem`. Each element buffer is organized in the following order:
+ *
+ * struct ublk_elem_buffer {
+ *     // Mandatory fields (8 bytes)
+ *     struct ublk_elem_header header;
+ *
+ *     // Optional fields (8 bytes each, included based on flags)
+ *
+ *     // Buffer address (if UBLK_BATCH_F_HAS_BUF_ADDR) for copying data
+ *     // between ublk request and ublk server buffer
+ *     __u64 buf_addr;
+ *
+ *     // returned Zone append LBA (if UBLK_BATCH_F_HAS_ZONE_LBA)
+ *     __u64 zone_lba;
+ * }
+ *
+ * Used for `UBLK_U_IO_PREP_IO_CMDS` and `UBLK_U_IO_COMMIT_IO_CMDS`
+ */
+struct ublk_batch_io {
+       __u16  q_id;
+#define UBLK_BATCH_F_HAS_ZONE_LBA      (1 << 0)
+#define UBLK_BATCH_F_HAS_BUF_ADDR      (1 << 1)
+#define UBLK_BATCH_F_AUTO_BUF_REG_FALLBACK     (1 << 2)
+       __u16   flags;
+       __u16   nr_elem;
+       __u8    elem_bytes;
+       __u8    reserved;
+       __u64   reserved2;
+};
+
 struct ublk_param_basic {
 #define UBLK_ATTR_READ_ONLY            (1 << 0)
 #define UBLK_ATTR_ROTATIONAL           (1 << 1)
--

2.47.0

author
#23 lore raw

Re: [PATCH V3 09/27] ublk: add new batch command UBLK_U_IO_PREP_IO_CMDS & UBLK_U_IO_COMMIT_IO_CMDS

Quoted 40 lines · Caleb Sander Mateos
Quoted 39 lines · Ming Lei

Add new command UBLK_U_IO_PREP_IO_CMDS, which is the batch version of UBLK_IO_FETCH_REQ.

Add new command UBLK_U_IO_COMMIT_IO_CMDS, which is for committing io command result only, still the batch version.

The new command header type is struct ublk_batch_io, and fixed buffer is required for these two uring_cmd.

This patch doesn't actually implement these commands yet, just validates the SQE fields.

---
 drivers/block/ublk_drv.c      | 107 +++++++++++++++++++++++++++++++++-
 include/uapi/linux/ublk_cmd.h |  49 ++++++++++++++++
 2 files changed, 155 insertions(+), 1 deletion(-)
drivers/block/ublk_drv.c +5 -0
index c62b2f2057fe..5f9d7ec9daa4 100644
@@ -85,6 +85,11 @@
         UBLK_PARAM_TYPE_DEVT | UBLK_PARAM_TYPE_ZONED |    \
         UBLK_PARAM_TYPE_DMA_ALIGN | UBLK_PARAM_TYPE_SEGMENT)
 
+#define UBLK_BATCH_F_ALL  \
+       (UBLK_BATCH_F_HAS_ZONE_LBA | \
+        UBLK_BATCH_F_HAS_BUF_ADDR | \
+        UBLK_BATCH_F_AUTO_BUF_REG_FALLBACK)
+
 struct ublk_uring_cmd_pdu {
        /*
         * Store requests in same batch temporarily for queuing them to
@@ -108,6 +113,12 @@ struct ublk_uring_cmd_pdu {
        u16 tag;
 };
 
Quoted 4 lines

drivers/block/ublk_drv.c struct ublk_uring_cmd_pdu { · line 117

Quoted 2 lines
drivers/block/ublk_drv.c +2 -0
+struct ublk_batch_io_data {
+       struct ublk_device *ub;

Is it possible for this to be a const pointer?

It isn't, for example, mutex_lock(&data->ub->mutex).

Quoted 44 lines
Quoted 30 lines
drivers/block/ublk_drv.c +22 -0
+       struct io_uring_cmd *cmd;
+       struct ublk_batch_io header;
+};
+
 /*
  * io command is active: sqe cmd is received, and its cqe isn't done
  *
@@ -2520,10 +2531,104 @@ static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags)
        return ublk_ch_uring_cmd_local(cmd, issue_flags);
 }
 
+static int ublk_check_batch_cmd_flags(const struct ublk_batch_io *uc)
+{
+       const u16 mask = UBLK_BATCH_F_HAS_BUF_ADDR | UBLK_BATCH_F_HAS_ZONE_LBA;
+       const unsigned header_len = sizeof(struct ublk_elem_header);
+
+       if (uc->flags & ~UBLK_BATCH_F_ALL)
+               return -EINVAL;
+
+       /* UBLK_BATCH_F_AUTO_BUF_REG_FALLBACK requires buffer index */
+       if ((uc->flags & UBLK_BATCH_F_AUTO_BUF_REG_FALLBACK) &&
+                       (uc->flags & UBLK_BATCH_F_HAS_BUF_ADDR))
+               return -EINVAL;
+
+       switch (uc->flags & mask) {
+       case 0:
+               if (uc->elem_bytes != header_len)
+                       return -EINVAL;
+               break;

drivers/block/ublk_drv.c static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags) · line 2561

Quoted 10 lines
drivers/block/ublk_drv.c +10 -0
+       case UBLK_BATCH_F_HAS_ZONE_LBA:
+       case UBLK_BATCH_F_HAS_BUF_ADDR:
+               if (uc->elem_bytes != header_len + sizeof(u64))
+                       return -EINVAL;
+               break;
+       case UBLK_BATCH_F_HAS_ZONE_LBA | UBLK_BATCH_F_HAS_BUF_ADDR:
+               if (uc->elem_bytes != header_len + sizeof(u64) + sizeof(u64))
+                       return -EINVAL;
+               break;
+       }

This could probably be implemented in a less branchy way using conditional moves:
unsigned elem_bytes = sizeof(struct ublk_elem_header) +

Quoted 4 lines
        (uc->flags & UBLK_BATCH_F_HAS_ZONE_LBA ? sizeof(u64) : 0) +
        (uc->flags & UBLK_BATCH_F_HAS_BUF_ADDR ? sizeof(u64) : 0);
if (uc->elem_bytes != elem_bytes)
        return -EINVAL;

I'd start with current more readable way, but the less branchy optimization can be left in future.

Quoted 28 lines
Quoted 28 lines
drivers/block/ublk_drv.c +27 -0
+
+       return 0;
+}
+
+static int ublk_check_batch_cmd(const struct ublk_batch_io_data *data)
+{
+
+       const struct ublk_batch_io *uc = &data->header;
+
+       if (!(data->cmd->flags & IORING_URING_CMD_FIXED))
+               return -EINVAL;
+
+       if (uc->nr_elem * uc->elem_bytes > data->cmd->sqe->len)
+               return -E2BIG;
+
+       if (uc->nr_elem > data->ub->dev_info.queue_depth)
+               return -E2BIG;
+
+       if ((uc->flags & UBLK_BATCH_F_HAS_ZONE_LBA) &&
+                       !ublk_dev_is_zoned(data->ub))
+               return -EINVAL;
+
+       if ((uc->flags & UBLK_BATCH_F_HAS_BUF_ADDR) &&
+                       !ublk_dev_need_map_io(data->ub))
+               return -EINVAL;
+
+       if ((uc->flags & UBLK_BATCH_F_AUTO_BUF_REG_FALLBACK) &&
Quoted 8 lines

drivers/block/ublk_drv.c static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags) · line 2593

Quoted 5 lines
drivers/block/ublk_drv.c +5 -0
+                       !ublk_dev_support_auto_buf_reg(data->ub))
+               return -EINVAL;
+
+       if (uc->reserved || uc->reserved2)
+               return -EINVAL;

These fields aren't actually copied from the uring_cmd, so this check is a no-op.

Good catch, will kill it in next version.

Quoted 34 lines
Quoted 34 lines
drivers/block/ublk_drv.c +29 -1
+
+       return ublk_check_batch_cmd_flags(uc);
+}
+
 static int ublk_ch_batch_io_uring_cmd(struct io_uring_cmd *cmd,
                                       unsigned int issue_flags)
 {
-       return -EOPNOTSUPP;
+       const struct ublk_batch_io *uc = io_uring_sqe_cmd(cmd->sqe);
+       struct ublk_device *ub = cmd->file->private_data;
+       struct ublk_batch_io_data data = {
+               .ub  = ub,
+               .cmd = cmd,
+               .header = (struct ublk_batch_io) {
+                       .q_id = READ_ONCE(uc->q_id),
+                       .flags = READ_ONCE(uc->flags),
+                       .nr_elem = READ_ONCE(uc->nr_elem),
+                       .elem_bytes = READ_ONCE(uc->elem_bytes),
+               },
+       };
+       u32 cmd_op = cmd->cmd_op;
+       int ret = -EINVAL;
+
+       if (data.header.q_id >= ub->dev_info.nr_hw_queues)
+               goto out;
+
+       switch (cmd_op) {
+       case UBLK_U_IO_PREP_IO_CMDS:
+       case UBLK_U_IO_COMMIT_IO_CMDS:
+               ret = ublk_check_batch_cmd(&data);
+               if (ret)
+                       goto out;
+               ret = -EOPNOTSUPP;
Quoted 8 lines

drivers/block/ublk_drv.c static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags) · line 2630

Quoted 5 lines
drivers/block/ublk_drv.c +5 -0
+               break;
+       default:
+               ret = -EOPNOTSUPP;
+       }
+out:

Is the out label really necessary if there's no cleanup involved? Can we just return the result directly?

Yeah, it isn't necessary, but we have benefit of single exit with goto, also will avoid extra return since command handler is added in following patches.

Thanks,
Ming

#24 lore raw

Re: [PATCH V3 09/27] ublk: add new batch command UBLK_U_IO_PREP_IO_CMDS & UBLK_U_IO_COMMIT_IO_CMDS

Quoted 98 lines · Ming Lei
Quoted 40 lines · Caleb Sander Mateos
Quoted 39 lines · Ming Lei

Add new command UBLK_U_IO_PREP_IO_CMDS, which is the batch version of UBLK_IO_FETCH_REQ.

Add new command UBLK_U_IO_COMMIT_IO_CMDS, which is for committing io command result only, still the batch version.

The new command header type is struct ublk_batch_io, and fixed buffer is required for these two uring_cmd.

This patch doesn't actually implement these commands yet, just validates the SQE fields.

---
 drivers/block/ublk_drv.c      | 107 +++++++++++++++++++++++++++++++++-
 include/uapi/linux/ublk_cmd.h |  49 ++++++++++++++++
 2 files changed, 155 insertions(+), 1 deletion(-)
drivers/block/ublk_drv.c +5 -0
index c62b2f2057fe..5f9d7ec9daa4 100644
@@ -85,6 +85,11 @@
         UBLK_PARAM_TYPE_DEVT | UBLK_PARAM_TYPE_ZONED |    \
         UBLK_PARAM_TYPE_DMA_ALIGN | UBLK_PARAM_TYPE_SEGMENT)
 
+#define UBLK_BATCH_F_ALL  \
+       (UBLK_BATCH_F_HAS_ZONE_LBA | \
+        UBLK_BATCH_F_HAS_BUF_ADDR | \
+        UBLK_BATCH_F_AUTO_BUF_REG_FALLBACK)
+
 struct ublk_uring_cmd_pdu {
        /*
         * Store requests in same batch temporarily for queuing them to
@@ -108,6 +113,12 @@ struct ublk_uring_cmd_pdu {
        u16 tag;
 };
 
Quoted 4 lines

drivers/block/ublk_drv.c struct ublk_uring_cmd_pdu { · line 117

Quoted 2 lines
drivers/block/ublk_drv.c +2 -0
+struct ublk_batch_io_data {
+       struct ublk_device *ub;

Is it possible for this to be a const pointer?

It isn't, for example, mutex_lock(&data->ub->mutex).

Quoted 44 lines
Quoted 30 lines
drivers/block/ublk_drv.c +22 -0
+       struct io_uring_cmd *cmd;
+       struct ublk_batch_io header;
+};
+
 /*
  * io command is active: sqe cmd is received, and its cqe isn't done
  *
@@ -2520,10 +2531,104 @@ static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags)
        return ublk_ch_uring_cmd_local(cmd, issue_flags);
 }
 
+static int ublk_check_batch_cmd_flags(const struct ublk_batch_io *uc)
+{
+       const u16 mask = UBLK_BATCH_F_HAS_BUF_ADDR | UBLK_BATCH_F_HAS_ZONE_LBA;
+       const unsigned header_len = sizeof(struct ublk_elem_header);
+
+       if (uc->flags & ~UBLK_BATCH_F_ALL)
+               return -EINVAL;
+
+       /* UBLK_BATCH_F_AUTO_BUF_REG_FALLBACK requires buffer index */
+       if ((uc->flags & UBLK_BATCH_F_AUTO_BUF_REG_FALLBACK) &&
+                       (uc->flags & UBLK_BATCH_F_HAS_BUF_ADDR))
+               return -EINVAL;
+
+       switch (uc->flags & mask) {
+       case 0:
+               if (uc->elem_bytes != header_len)
+                       return -EINVAL;
+               break;

drivers/block/ublk_drv.c static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags) · line 2561

Quoted 10 lines
drivers/block/ublk_drv.c +10 -0
+       case UBLK_BATCH_F_HAS_ZONE_LBA:
+       case UBLK_BATCH_F_HAS_BUF_ADDR:
+               if (uc->elem_bytes != header_len + sizeof(u64))
+                       return -EINVAL;
+               break;
+       case UBLK_BATCH_F_HAS_ZONE_LBA | UBLK_BATCH_F_HAS_BUF_ADDR:
+               if (uc->elem_bytes != header_len + sizeof(u64) + sizeof(u64))
+                       return -EINVAL;
+               break;
+       }

This could probably be implemented in a less branchy way using conditional moves:
unsigned elem_bytes = sizeof(struct ublk_elem_header) +

Quoted 4 lines
        (uc->flags & UBLK_BATCH_F_HAS_ZONE_LBA ? sizeof(u64) : 0) +
        (uc->flags & UBLK_BATCH_F_HAS_BUF_ADDR ? sizeof(u64) : 0);
if (uc->elem_bytes != elem_bytes)
        return -EINVAL;
Quoted 2 lines

I'd start with current more readable way, but the less branchy optimization can be left in future.

Not sure I'd agree the current way is "more readable" :) but I don't feel strongly about it.

Best,
Caleb

Quoted 91 lines
Quoted 29 lines
Quoted 29 lines
drivers/block/ublk_drv.c +27 -0
+
+       return 0;
+}
+
+static int ublk_check_batch_cmd(const struct ublk_batch_io_data *data)
+{
+
+       const struct ublk_batch_io *uc = &data->header;
+
+       if (!(data->cmd->flags & IORING_URING_CMD_FIXED))
+               return -EINVAL;
+
+       if (uc->nr_elem * uc->elem_bytes > data->cmd->sqe->len)
+               return -E2BIG;
+
+       if (uc->nr_elem > data->ub->dev_info.queue_depth)
+               return -E2BIG;
+
+       if ((uc->flags & UBLK_BATCH_F_HAS_ZONE_LBA) &&
+                       !ublk_dev_is_zoned(data->ub))
+               return -EINVAL;
+
+       if ((uc->flags & UBLK_BATCH_F_HAS_BUF_ADDR) &&
+                       !ublk_dev_need_map_io(data->ub))
+               return -EINVAL;
+
+       if ((uc->flags & UBLK_BATCH_F_AUTO_BUF_REG_FALLBACK) &&
Quoted 8 lines

drivers/block/ublk_drv.c static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags) · line 2593

Quoted 5 lines
drivers/block/ublk_drv.c +5 -0
+                       !ublk_dev_support_auto_buf_reg(data->ub))
+               return -EINVAL;
+
+       if (uc->reserved || uc->reserved2)
+               return -EINVAL;

These fields aren't actually copied from the uring_cmd, so this check is a no-op.

Good catch, will kill it in next version.

Quoted 34 lines
Quoted 34 lines
drivers/block/ublk_drv.c +29 -1
+
+       return ublk_check_batch_cmd_flags(uc);
+}
+
 static int ublk_ch_batch_io_uring_cmd(struct io_uring_cmd *cmd,
                                       unsigned int issue_flags)
 {
-       return -EOPNOTSUPP;
+       const struct ublk_batch_io *uc = io_uring_sqe_cmd(cmd->sqe);
+       struct ublk_device *ub = cmd->file->private_data;
+       struct ublk_batch_io_data data = {
+               .ub  = ub,
+               .cmd = cmd,
+               .header = (struct ublk_batch_io) {
+                       .q_id = READ_ONCE(uc->q_id),
+                       .flags = READ_ONCE(uc->flags),
+                       .nr_elem = READ_ONCE(uc->nr_elem),
+                       .elem_bytes = READ_ONCE(uc->elem_bytes),
+               },
+       };
+       u32 cmd_op = cmd->cmd_op;
+       int ret = -EINVAL;
+
+       if (data.header.q_id >= ub->dev_info.nr_hw_queues)
+               goto out;
+
+       switch (cmd_op) {
+       case UBLK_U_IO_PREP_IO_CMDS:
+       case UBLK_U_IO_COMMIT_IO_CMDS:
+               ret = ublk_check_batch_cmd(&data);
+               if (ret)
+                       goto out;
+               ret = -EOPNOTSUPP;
Quoted 8 lines

drivers/block/ublk_drv.c static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags) · line 2630

Quoted 5 lines
drivers/block/ublk_drv.c +5 -0
+               break;
+       default:
+               ret = -EOPNOTSUPP;
+       }
+out:

Is the out label really necessary if there's no cleanup involved? Can we just return the result directly?

Yeah, it isn't necessary, but we have benefit of single exit with goto, also will avoid extra return since command handler is added in following patches.

Thanks,
Ming

#25 lore raw

Re: [PATCH V3 09/27] ublk: add new batch command UBLK_U_IO_PREP_IO_CMDS & UBLK_U_IO_COMMIT_IO_CMDS

In reply to Ming Lei
Quoted 83 lines · Ming Lei

Add new command UBLK_U_IO_PREP_IO_CMDS, which is the batch version of UBLK_IO_FETCH_REQ.

Add new command UBLK_U_IO_COMMIT_IO_CMDS, which is for committing io command result only, still the batch version.

The new command header type is struct ublk_batch_io, and fixed buffer is required for these two uring_cmd.

This patch doesn't actually implement these commands yet, just validates the SQE fields.

---
 drivers/block/ublk_drv.c      | 107 +++++++++++++++++++++++++++++++++-
 include/uapi/linux/ublk_cmd.h |  49 ++++++++++++++++
 2 files changed, 155 insertions(+), 1 deletion(-)
drivers/block/ublk_drv.c +42 -0
index c62b2f2057fe..5f9d7ec9daa4 100644
@@ -85,6 +85,11 @@
         UBLK_PARAM_TYPE_DEVT | UBLK_PARAM_TYPE_ZONED |    \
         UBLK_PARAM_TYPE_DMA_ALIGN | UBLK_PARAM_TYPE_SEGMENT)
 
+#define UBLK_BATCH_F_ALL  \
+       (UBLK_BATCH_F_HAS_ZONE_LBA | \
+        UBLK_BATCH_F_HAS_BUF_ADDR | \
+        UBLK_BATCH_F_AUTO_BUF_REG_FALLBACK)
+
 struct ublk_uring_cmd_pdu {
        /*
         * Store requests in same batch temporarily for queuing them to
@@ -108,6 +113,12 @@ struct ublk_uring_cmd_pdu {
        u16 tag;
 };
 
+struct ublk_batch_io_data {
+       struct ublk_device *ub;
+       struct io_uring_cmd *cmd;
+       struct ublk_batch_io header;
+};
+
 /*
  * io command is active: sqe cmd is received, and its cqe isn't done
  *
@@ -2520,10 +2531,104 @@ static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags)
        return ublk_ch_uring_cmd_local(cmd, issue_flags);
 }
 
+static int ublk_check_batch_cmd_flags(const struct ublk_batch_io *uc)
+{
+       const u16 mask = UBLK_BATCH_F_HAS_BUF_ADDR | UBLK_BATCH_F_HAS_ZONE_LBA;
+       const unsigned header_len = sizeof(struct ublk_elem_header);
+
+       if (uc->flags & ~UBLK_BATCH_F_ALL)
+               return -EINVAL;
+
+       /* UBLK_BATCH_F_AUTO_BUF_REG_FALLBACK requires buffer index */
+       if ((uc->flags & UBLK_BATCH_F_AUTO_BUF_REG_FALLBACK) &&
+                       (uc->flags & UBLK_BATCH_F_HAS_BUF_ADDR))
+               return -EINVAL;
+
+       switch (uc->flags & mask) {
+       case 0:
+               if (uc->elem_bytes != header_len)
+                       return -EINVAL;
+               break;
+       case UBLK_BATCH_F_HAS_ZONE_LBA:
+       case UBLK_BATCH_F_HAS_BUF_ADDR:
+               if (uc->elem_bytes != header_len + sizeof(u64))
+                       return -EINVAL;
+               break;
+       case UBLK_BATCH_F_HAS_ZONE_LBA | UBLK_BATCH_F_HAS_BUF_ADDR:
+               if (uc->elem_bytes != header_len + sizeof(u64) + sizeof(u64))
+                       return -EINVAL;
+               break;
+       }
+
+       return 0;
+}

drivers/block/ublk_drv.c static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags) · line 2574

Quoted 10 lines
drivers/block/ublk_drv.c +10 -0
+
+static int ublk_check_batch_cmd(const struct ublk_batch_io_data *data)
+{
+
+       const struct ublk_batch_io *uc = &data->header;
+
+       if (!(data->cmd->flags & IORING_URING_CMD_FIXED))
+               return -EINVAL;
+
+       if (uc->nr_elem * uc->elem_bytes > data->cmd->sqe->len)

sqe->len should be accessed with READ_ONCE() since it may point to user-mapped memory.

Best,
Caleb

Quoted 131 lines
drivers/block/ublk_drv.c +54 -1
+               return -E2BIG;
+
+       if (uc->nr_elem > data->ub->dev_info.queue_depth)
+               return -E2BIG;
+
+       if ((uc->flags & UBLK_BATCH_F_HAS_ZONE_LBA) &&
+                       !ublk_dev_is_zoned(data->ub))
+               return -EINVAL;
+
+       if ((uc->flags & UBLK_BATCH_F_HAS_BUF_ADDR) &&
+                       !ublk_dev_need_map_io(data->ub))
+               return -EINVAL;
+
+       if ((uc->flags & UBLK_BATCH_F_AUTO_BUF_REG_FALLBACK) &&
+                       !ublk_dev_support_auto_buf_reg(data->ub))
+               return -EINVAL;
+
+       if (uc->reserved || uc->reserved2)
+               return -EINVAL;
+
+       return ublk_check_batch_cmd_flags(uc);
+}
+
 static int ublk_ch_batch_io_uring_cmd(struct io_uring_cmd *cmd,
                                       unsigned int issue_flags)
 {
-       return -EOPNOTSUPP;
+       const struct ublk_batch_io *uc = io_uring_sqe_cmd(cmd->sqe);
+       struct ublk_device *ub = cmd->file->private_data;
+       struct ublk_batch_io_data data = {
+               .ub  = ub,
+               .cmd = cmd,
+               .header = (struct ublk_batch_io) {
+                       .q_id = READ_ONCE(uc->q_id),
+                       .flags = READ_ONCE(uc->flags),
+                       .nr_elem = READ_ONCE(uc->nr_elem),
+                       .elem_bytes = READ_ONCE(uc->elem_bytes),
+               },
+       };
+       u32 cmd_op = cmd->cmd_op;
+       int ret = -EINVAL;
+
+       if (data.header.q_id >= ub->dev_info.nr_hw_queues)
+               goto out;
+
+       switch (cmd_op) {
+       case UBLK_U_IO_PREP_IO_CMDS:
+       case UBLK_U_IO_COMMIT_IO_CMDS:
+               ret = ublk_check_batch_cmd(&data);
+               if (ret)
+                       goto out;
+               ret = -EOPNOTSUPP;
+               break;
+       default:
+               ret = -EOPNOTSUPP;
+       }
+out:
+       return ret;
 }
 
 static inline bool ublk_check_ubuf_dir(const struct request *req,
include/uapi/linux/ublk_cmd.h +49 -1
index ec77dabba45b..2ce5a496b622 100644
@@ -102,6 +102,10 @@
        _IOWR('u', 0x23, struct ublksrv_io_cmd)
 #define        UBLK_U_IO_UNREGISTER_IO_BUF     \
        _IOWR('u', 0x24, struct ublksrv_io_cmd)
+#define        UBLK_U_IO_PREP_IO_CMDS  \
+       _IOWR('u', 0x25, struct ublk_batch_io)
+#define        UBLK_U_IO_COMMIT_IO_CMDS        \
+       _IOWR('u', 0x26, struct ublk_batch_io)
 
 /* only ABORT means that no re-fetch */
 #define UBLK_IO_RES_OK                 0
@@ -525,6 +529,51 @@ struct ublksrv_io_cmd {
        };
 };
 
+struct ublk_elem_header {
+       __u16 tag;      /* IO tag */
+
+       /*
+        * Buffer index for incoming io command, only valid iff
+        * UBLK_F_AUTO_BUF_REG is set
+        */
+       __u16 buf_index;
+       __s32 result;   /* I/O completion result (commit only) */
+};
+
+/*
+ * uring_cmd buffer structure for batch commands
+ *
+ * buffer includes multiple elements, which number is specified by
+ * `nr_elem`. Each element buffer is organized in the following order:
+ *
+ * struct ublk_elem_buffer {
+ *     // Mandatory fields (8 bytes)
+ *     struct ublk_elem_header header;
+ *
+ *     // Optional fields (8 bytes each, included based on flags)
+ *
+ *     // Buffer address (if UBLK_BATCH_F_HAS_BUF_ADDR) for copying data
+ *     // between ublk request and ublk server buffer
+ *     __u64 buf_addr;
+ *
+ *     // returned Zone append LBA (if UBLK_BATCH_F_HAS_ZONE_LBA)
+ *     __u64 zone_lba;
+ * }
+ *
+ * Used for `UBLK_U_IO_PREP_IO_CMDS` and `UBLK_U_IO_COMMIT_IO_CMDS`
+ */
+struct ublk_batch_io {
+       __u16  q_id;
+#define UBLK_BATCH_F_HAS_ZONE_LBA      (1 << 0)
+#define UBLK_BATCH_F_HAS_BUF_ADDR      (1 << 1)
+#define UBLK_BATCH_F_AUTO_BUF_REG_FALLBACK     (1 << 2)
+       __u16   flags;
+       __u16   nr_elem;
+       __u8    elem_bytes;
+       __u8    reserved;
+       __u64   reserved2;
+};
+
 struct ublk_param_basic {
 #define UBLK_ATTR_READ_ONLY            (1 << 0)
 #define UBLK_ATTR_ROTATIONAL           (1 << 1)
--

2.47.0

#26 lore raw

Re: [PATCH V3 09/27] ublk: add new batch command UBLK_U_IO_PREP_IO_CMDS & UBLK_U_IO_COMMIT_IO_CMDS

Quoted 90 lines · Caleb Sander Mateos
Quoted 88 lines · Ming Lei

Add new command UBLK_U_IO_PREP_IO_CMDS, which is the batch version of UBLK_IO_FETCH_REQ.

Add new command UBLK_U_IO_COMMIT_IO_CMDS, which is for committing io command result only, still the batch version.

The new command header type is struct ublk_batch_io, and fixed buffer is required for these two uring_cmd.

This patch doesn't actually implement these commands yet, just validates the SQE fields.

---
 drivers/block/ublk_drv.c      | 107 +++++++++++++++++++++++++++++++++-
 include/uapi/linux/ublk_cmd.h |  49 ++++++++++++++++
 2 files changed, 155 insertions(+), 1 deletion(-)
drivers/block/ublk_drv.c +47 -0
index c62b2f2057fe..5f9d7ec9daa4 100644
@@ -85,6 +85,11 @@
         UBLK_PARAM_TYPE_DEVT | UBLK_PARAM_TYPE_ZONED |    \
         UBLK_PARAM_TYPE_DMA_ALIGN | UBLK_PARAM_TYPE_SEGMENT)
 
+#define UBLK_BATCH_F_ALL  \
+       (UBLK_BATCH_F_HAS_ZONE_LBA | \
+        UBLK_BATCH_F_HAS_BUF_ADDR | \
+        UBLK_BATCH_F_AUTO_BUF_REG_FALLBACK)
+
 struct ublk_uring_cmd_pdu {
        /*
         * Store requests in same batch temporarily for queuing them to
@@ -108,6 +113,12 @@ struct ublk_uring_cmd_pdu {
        u16 tag;
 };
 
+struct ublk_batch_io_data {
+       struct ublk_device *ub;
+       struct io_uring_cmd *cmd;
+       struct ublk_batch_io header;
+};
+
 /*
  * io command is active: sqe cmd is received, and its cqe isn't done
  *
@@ -2520,10 +2531,104 @@ static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags)
        return ublk_ch_uring_cmd_local(cmd, issue_flags);
 }
 
+static int ublk_check_batch_cmd_flags(const struct ublk_batch_io *uc)
+{
+       const u16 mask = UBLK_BATCH_F_HAS_BUF_ADDR | UBLK_BATCH_F_HAS_ZONE_LBA;
+       const unsigned header_len = sizeof(struct ublk_elem_header);
+
+       if (uc->flags & ~UBLK_BATCH_F_ALL)
+               return -EINVAL;
+
+       /* UBLK_BATCH_F_AUTO_BUF_REG_FALLBACK requires buffer index */
+       if ((uc->flags & UBLK_BATCH_F_AUTO_BUF_REG_FALLBACK) &&
+                       (uc->flags & UBLK_BATCH_F_HAS_BUF_ADDR))
+               return -EINVAL;
+
+       switch (uc->flags & mask) {
+       case 0:
+               if (uc->elem_bytes != header_len)
+                       return -EINVAL;
+               break;
+       case UBLK_BATCH_F_HAS_ZONE_LBA:
+       case UBLK_BATCH_F_HAS_BUF_ADDR:
+               if (uc->elem_bytes != header_len + sizeof(u64))
+                       return -EINVAL;
+               break;
+       case UBLK_BATCH_F_HAS_ZONE_LBA | UBLK_BATCH_F_HAS_BUF_ADDR:
+               if (uc->elem_bytes != header_len + sizeof(u64) + sizeof(u64))
+                       return -EINVAL;
+               break;
+       }
+
+       return 0;
+}
+
+static int ublk_check_batch_cmd(const struct ublk_batch_io_data *data)
+{
+
+       const struct ublk_batch_io *uc = &data->header;
Quoted 8 lines

drivers/block/ublk_drv.c static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags) · line 2574

Quoted 5 lines
drivers/block/ublk_drv.c +5 -0
+
+       if (!(data->cmd->flags & IORING_URING_CMD_FIXED))
+               return -EINVAL;
+
+       if (uc->nr_elem * uc->elem_bytes > data->cmd->sqe->len)

sqe->len should be accessed with READ_ONCE() since it may point to user-mapped memory.

Actually, is there any point in this check since sqe->len is no longer used anywhere?

author
#27 lore raw

Re: [PATCH V3 09/27] ublk: add new batch command UBLK_U_IO_PREP_IO_CMDS & UBLK_U_IO_COMMIT_IO_CMDS

Quoted 101 lines · Caleb Sander Mateos
Quoted 90 lines · Caleb Sander Mateos
Quoted 88 lines · Ming Lei

Add new command UBLK_U_IO_PREP_IO_CMDS, which is the batch version of UBLK_IO_FETCH_REQ.

Add new command UBLK_U_IO_COMMIT_IO_CMDS, which is for committing io command result only, still the batch version.

The new command header type is struct ublk_batch_io, and fixed buffer is required for these two uring_cmd.

This patch doesn't actually implement these commands yet, just validates the SQE fields.

---
 drivers/block/ublk_drv.c      | 107 +++++++++++++++++++++++++++++++++-
 include/uapi/linux/ublk_cmd.h |  49 ++++++++++++++++
 2 files changed, 155 insertions(+), 1 deletion(-)
drivers/block/ublk_drv.c +47 -0
index c62b2f2057fe..5f9d7ec9daa4 100644
@@ -85,6 +85,11 @@
         UBLK_PARAM_TYPE_DEVT | UBLK_PARAM_TYPE_ZONED |    \
         UBLK_PARAM_TYPE_DMA_ALIGN | UBLK_PARAM_TYPE_SEGMENT)
 
+#define UBLK_BATCH_F_ALL  \
+       (UBLK_BATCH_F_HAS_ZONE_LBA | \
+        UBLK_BATCH_F_HAS_BUF_ADDR | \
+        UBLK_BATCH_F_AUTO_BUF_REG_FALLBACK)
+
 struct ublk_uring_cmd_pdu {
        /*
         * Store requests in same batch temporarily for queuing them to
@@ -108,6 +113,12 @@ struct ublk_uring_cmd_pdu {
        u16 tag;
 };
 
+struct ublk_batch_io_data {
+       struct ublk_device *ub;
+       struct io_uring_cmd *cmd;
+       struct ublk_batch_io header;
+};
+
 /*
  * io command is active: sqe cmd is received, and its cqe isn't done
  *
@@ -2520,10 +2531,104 @@ static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags)
        return ublk_ch_uring_cmd_local(cmd, issue_flags);
 }
 
+static int ublk_check_batch_cmd_flags(const struct ublk_batch_io *uc)
+{
+       const u16 mask = UBLK_BATCH_F_HAS_BUF_ADDR | UBLK_BATCH_F_HAS_ZONE_LBA;
+       const unsigned header_len = sizeof(struct ublk_elem_header);
+
+       if (uc->flags & ~UBLK_BATCH_F_ALL)
+               return -EINVAL;
+
+       /* UBLK_BATCH_F_AUTO_BUF_REG_FALLBACK requires buffer index */
+       if ((uc->flags & UBLK_BATCH_F_AUTO_BUF_REG_FALLBACK) &&
+                       (uc->flags & UBLK_BATCH_F_HAS_BUF_ADDR))
+               return -EINVAL;
+
+       switch (uc->flags & mask) {
+       case 0:
+               if (uc->elem_bytes != header_len)
+                       return -EINVAL;
+               break;
+       case UBLK_BATCH_F_HAS_ZONE_LBA:
+       case UBLK_BATCH_F_HAS_BUF_ADDR:
+               if (uc->elem_bytes != header_len + sizeof(u64))
+                       return -EINVAL;
+               break;
+       case UBLK_BATCH_F_HAS_ZONE_LBA | UBLK_BATCH_F_HAS_BUF_ADDR:
+               if (uc->elem_bytes != header_len + sizeof(u64) + sizeof(u64))
+                       return -EINVAL;
+               break;
+       }
+
+       return 0;
+}
+
+static int ublk_check_batch_cmd(const struct ublk_batch_io_data *data)
+{
+
+       const struct ublk_batch_io *uc = &data->header;
Quoted 8 lines

drivers/block/ublk_drv.c static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags) · line 2574

Quoted 5 lines
drivers/block/ublk_drv.c +5 -0
+
+       if (!(data->cmd->flags & IORING_URING_CMD_FIXED))
+               return -EINVAL;
+
+       if (uc->nr_elem * uc->elem_bytes > data->cmd->sqe->len)

sqe->len should be accessed with READ_ONCE() since it may point to user-mapped memory.

Quoted 2 lines

Actually, is there any point in this check since sqe->len is no longer used anywhere?

Right, will remove it in next version.

Thanks,
Ming

author
#28 lore raw

PATCH v3 10/27 ublk: handle UBLK_U_IO_PREP_IO_CMDS

In reply to Ming Lei

This commit implements the handling of the UBLK_U_IO_PREP_IO_CMDS command, which allows userspace to prepare a batch of I/O requests.

The core of this change is the ublk_walk_cmd_buf function, which iterates over the elements in the uring_cmd fixed buffer. For each element, it parses the I/O details, finds the corresponding ublk_io structure, and prepares it for future dispatch.

Add per-io lock for protecting concurrent delivery and committing.

---
 drivers/block/ublk_drv.c      | 205 +++++++++++++++++++++++++++++++++-
 include/uapi/linux/ublk_cmd.h |   5 +
 2 files changed, 209 insertions(+), 1 deletion(-)
drivers/block/ublk_drv.c +204 -1
index 5f9d7ec9daa4..84d838df18cb 100644
@@ -117,6 +117,7 @@ struct ublk_batch_io_data {
 	struct ublk_device *ub;
 	struct io_uring_cmd *cmd;
 	struct ublk_batch_io header;
+	unsigned int issue_flags;
 };
 
 /*
@@ -201,6 +202,7 @@ struct ublk_io {
 	unsigned task_registered_buffers;
 
 	void *buf_ctx_handle;
+	spinlock_t lock;
 } ____cacheline_aligned_in_smp;
 
 struct ublk_queue {
@@ -270,6 +272,16 @@ static inline bool ublk_dev_support_batch_io(const struct ublk_device *ub)
 	return false;
 }
 
+static inline void ublk_io_lock(struct ublk_io *io)
+{
+	spin_lock(&io->lock);
+}
+
+static inline void ublk_io_unlock(struct ublk_io *io)
+{
+	spin_unlock(&io->lock);
+}
+
 static inline struct ublksrv_io_desc *
 ublk_get_iod(const struct ublk_queue *ubq, unsigned tag)
 {
@@ -2531,6 +2543,183 @@ static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags)
 	return ublk_ch_uring_cmd_local(cmd, issue_flags);
 }
 
+static inline __u64 ublk_batch_buf_addr(const struct ublk_batch_io *uc,
+					const struct ublk_elem_header *elem)
+{
+	const void *buf = elem;
+
+	if (uc->flags & UBLK_BATCH_F_HAS_BUF_ADDR)
+		return *(__u64 *)(buf + sizeof(*elem));
+	return 0;
+}
+
+static struct ublk_auto_buf_reg
+ublk_batch_auto_buf_reg(const struct ublk_batch_io *uc,
+			const struct ublk_elem_header *elem)
+{
+	struct ublk_auto_buf_reg reg = {
+		.index = elem->buf_index,
+		.flags = (uc->flags & UBLK_BATCH_F_AUTO_BUF_REG_FALLBACK) ?
+			UBLK_AUTO_BUF_REG_FALLBACK : 0,
+	};
+
+	return reg;
+}
+
+/*
+ * 48 can hold any type of buffer element(8, 16 and 24 bytes) because
+ * it is the least common multiple(LCM) of 8, 16 and 24
+ */
+#define UBLK_CMD_BATCH_TMP_BUF_SZ  (48 * 10)
+struct ublk_batch_io_iter {
+	/* copy to this buffer from iterator first */
+	unsigned char buf[UBLK_CMD_BATCH_TMP_BUF_SZ];
+	struct iov_iter iter;
+	unsigned done, total;
+	unsigned char elem_bytes;
+};
+
+static inline int
+__ublk_walk_cmd_buf(struct ublk_queue *ubq,
+		    struct ublk_batch_io_iter *iter,
+		    const struct ublk_batch_io_data *data,
+		    unsigned bytes,
+		    int (*cb)(struct ublk_queue *q,
+			    const struct ublk_batch_io_data *data,
+			    const struct ublk_elem_header *elem))
+{
+	int i, ret = 0;
+
+	for (i = 0; i < bytes; i += iter->elem_bytes) {
+		const struct ublk_elem_header *elem =
+			(const struct ublk_elem_header *)&iter->buf[i];
+
+		if (unlikely(elem->tag >= data->ub->dev_info.queue_depth)) {
+			ret = -EINVAL;
+			break;
+		}
+
+		ret = cb(ubq, data, elem);
+		if (unlikely(ret))
+			break;
+	}
+
+	/* revert unhandled bytes in case of failure */
+	if (ret)
+		iov_iter_revert(&iter->iter, bytes - i);
+
+	iter->done += i;
+	return ret;
+}
+
+static int ublk_walk_cmd_buf(struct ublk_batch_io_iter *iter,
+			     const struct ublk_batch_io_data *data,
+			     int (*cb)(struct ublk_queue *q,
+				     const struct ublk_batch_io_data *data,
+				     const struct ublk_elem_header *elem))
+{
+	struct ublk_queue *ubq = ublk_get_queue(data->ub, data->header.q_id);
+	int ret = 0;
+
+	while (iter->done < iter->total) {
+		unsigned int len = min(sizeof(iter->buf), iter->total - iter->done);
+
+		ret = copy_from_iter(iter->buf, len, &iter->iter);
+		if (ret != len) {
+			pr_warn("ublk%d: read batch cmd buffer failed %u/%u\n",
+					data->ub->dev_info.dev_id, ret, len);
+			ret = -EINVAL;
+			break;
+		}
+
+		ret = __ublk_walk_cmd_buf(ubq, iter, data, len, cb);
+		if (ret)
+			break;
+	}
+	return ret;
+}
+
+static int ublk_batch_unprep_io(struct ublk_queue *ubq,
+				const struct ublk_batch_io_data *data,
+				const struct ublk_elem_header *elem)
+{
+	struct ublk_io *io = &ubq->ios[elem->tag];
+
+	data->ub->nr_io_ready--;
+	ublk_io_lock(io);
+	io->flags = 0;
+	ublk_io_unlock(io);
+	return 0;
+}
+
+static void ublk_batch_revert_prep_cmd(struct ublk_batch_io_iter *iter,
+				       const struct ublk_batch_io_data *data)
+{
+	int ret;
+
+	if (!iter->done)
+		return;
+
+	iov_iter_revert(&iter->iter, iter->done);
+	iter->total = iter->done;
+	iter->done = 0;
+
+	ret = ublk_walk_cmd_buf(iter, data, ublk_batch_unprep_io);
+	WARN_ON_ONCE(ret);
+}
+
+static int ublk_batch_prep_io(struct ublk_queue *ubq,
+			      const struct ublk_batch_io_data *data,
+			      const struct ublk_elem_header *elem)
+{
+	struct ublk_io *io = &ubq->ios[elem->tag];
+	const struct ublk_batch_io *uc = &data->header;
+	union ublk_io_buf buf = { 0 };
+	int ret;
+
+	if (ublk_dev_support_auto_buf_reg(data->ub))
+		buf.auto_reg = ublk_batch_auto_buf_reg(uc, elem);
+	else if (ublk_dev_need_map_io(data->ub)) {
+		buf.addr = ublk_batch_buf_addr(uc, elem);
+
+		ret = ublk_check_fetch_buf(data->ub, buf.addr);
+		if (ret)
+			return ret;
+	}
+
+	ublk_io_lock(io);
+	ret = __ublk_fetch(data->cmd, data->ub, io);
+	if (!ret)
+		io->buf = buf;
+	ublk_io_unlock(io);
+
+	return ret;
+}
+
+static int ublk_handle_batch_prep_cmd(const struct ublk_batch_io_data *data)
+{
+	const struct ublk_batch_io *uc = &data->header;
+	struct io_uring_cmd *cmd = data->cmd;
+	struct ublk_batch_io_iter iter = {
+		.total = uc->nr_elem * uc->elem_bytes,
+		.elem_bytes = uc->elem_bytes,
+	};
+	int ret;
+
+	ret = io_uring_cmd_import_fixed(cmd->sqe->addr, iter.total,
+			WRITE, &iter.iter, cmd, data->issue_flags);
+	if (ret)
+		return ret;
+
+	mutex_lock(&data->ub->mutex);
+	ret = ublk_walk_cmd_buf(&iter, data, ublk_batch_prep_io);
+
+	if (ret && iter.done)
+		ublk_batch_revert_prep_cmd(&iter, data);
+	mutex_unlock(&data->ub->mutex);
+	return ret;
+}
+
 static int ublk_check_batch_cmd_flags(const struct ublk_batch_io *uc)
 {
 	const u16 mask = UBLK_BATCH_F_HAS_BUF_ADDR | UBLK_BATCH_F_HAS_ZONE_LBA;
@@ -2609,6 +2798,7 @@ static int ublk_ch_batch_io_uring_cmd(struct io_uring_cmd *cmd,
 			.nr_elem = READ_ONCE(uc->nr_elem),
 			.elem_bytes = READ_ONCE(uc->elem_bytes),
 		},
+		.issue_flags = issue_flags,
 	};
 	u32 cmd_op = cmd->cmd_op;
 	int ret = -EINVAL;
@@ -2618,6 +2808,11 @@ static int ublk_ch_batch_io_uring_cmd(struct io_uring_cmd *cmd,
 
 	switch (cmd_op) {
 	case UBLK_U_IO_PREP_IO_CMDS:
+		ret = ublk_check_batch_cmd(&data);
+		if (ret)
+			goto out;
+		ret = ublk_handle_batch_prep_cmd(&data);
+		break;
 	case UBLK_U_IO_COMMIT_IO_CMDS:
 		ret = ublk_check_batch_cmd(&data);
 		if (ret)
@@ -2792,7 +2987,7 @@ static int ublk_init_queue(struct ublk_device *ub, int q_id)
 	struct ublk_queue *ubq;
 	struct page *page;
 	int numa_node;
-	int size;
+	int size, i;
 
 	/* Determine NUMA node based on queue's CPU affinity */
 	numa_node = ublk_get_queue_numa_node(ub, q_id);
@@ -2817,6 +3012,9 @@ static int ublk_init_queue(struct ublk_device *ub, int q_id)
 	}
 	ubq->io_cmd_buf = page_address(page);
 
+	for (i = 0; i < ubq->q_depth; i++)
+		spin_lock_init(&ubq->ios[i].lock);
+
 	ub->queues[q_id] = ubq;
 	ubq->dev = ub;
 	return 0;
@@ -3043,6 +3241,11 @@ static int ublk_ctrl_start_dev(struct ublk_device *ub,
 		return -EINVAL;
 
 	mutex_lock(&ub->mutex);
+	/* device may become not ready in case of F_BATCH */
+	if (!ublk_dev_ready(ub)) {
+		ret = -EINVAL;
+		goto out_unlock;
+	}
 	if (ub->dev_info.state == UBLK_S_DEV_LIVE ||
 	    test_bit(UB_STATE_USED, &ub->state)) {
 		ret = -EEXIST;
include/uapi/linux/ublk_cmd.h +5 -0
index 2ce5a496b622..c96c299057c3 100644
@@ -102,6 +102,11 @@
 	_IOWR('u', 0x23, struct ublksrv_io_cmd)
 #define	UBLK_U_IO_UNREGISTER_IO_BUF	\
 	_IOWR('u', 0x24, struct ublksrv_io_cmd)
+
+/*
+ * return 0 if the command is run successfully, otherwise failure code
+ * is returned
+ */
 #define	UBLK_U_IO_PREP_IO_CMDS	\
 	_IOWR('u', 0x25, struct ublk_batch_io)
 #define	UBLK_U_IO_COMMIT_IO_CMDS	\
Signature · 1 line
2.47.0
#29 lore raw

Re: [PATCH V3 10/27] ublk: handle UBLK_U_IO_PREP_IO_CMDS

Quoted 94 lines · Ming Lei

This commit implements the handling of the UBLK_U_IO_PREP_IO_CMDS command, which allows userspace to prepare a batch of I/O requests.

The core of this change is the ublk_walk_cmd_buf function, which iterates over the elements in the uring_cmd fixed buffer. For each element, it parses the I/O details, finds the corresponding ublk_io structure, and prepares it for future dispatch.

Add per-io lock for protecting concurrent delivery and committing.

---
 drivers/block/ublk_drv.c      | 205 +++++++++++++++++++++++++++++++++-
 include/uapi/linux/ublk_cmd.h |   5 +
 2 files changed, 209 insertions(+), 1 deletion(-)
drivers/block/ublk_drv.c +48 -0
index 5f9d7ec9daa4..84d838df18cb 100644
@@ -117,6 +117,7 @@ struct ublk_batch_io_data {
        struct ublk_device *ub;
        struct io_uring_cmd *cmd;
        struct ublk_batch_io header;
+       unsigned int issue_flags;
 };
 
 /*
@@ -201,6 +202,7 @@ struct ublk_io {
        unsigned task_registered_buffers;
 
        void *buf_ctx_handle;
+       spinlock_t lock;
 } ____cacheline_aligned_in_smp;
 
 struct ublk_queue {
@@ -270,6 +272,16 @@ static inline bool ublk_dev_support_batch_io(const struct ublk_device *ub)
        return false;
 }
 
+static inline void ublk_io_lock(struct ublk_io *io)
+{
+       spin_lock(&io->lock);
+}
+
+static inline void ublk_io_unlock(struct ublk_io *io)
+{
+       spin_unlock(&io->lock);
+}
+
 static inline struct ublksrv_io_desc *
 ublk_get_iod(const struct ublk_queue *ubq, unsigned tag)
 {
@@ -2531,6 +2543,183 @@ static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags)
        return ublk_ch_uring_cmd_local(cmd, issue_flags);
 }
 
+static inline __u64 ublk_batch_buf_addr(const struct ublk_batch_io *uc,
+                                       const struct ublk_elem_header *elem)
+{
+       const void *buf = elem;
+
+       if (uc->flags & UBLK_BATCH_F_HAS_BUF_ADDR)
+               return *(__u64 *)(buf + sizeof(*elem));
+       return 0;
+}
+
+static struct ublk_auto_buf_reg
+ublk_batch_auto_buf_reg(const struct ublk_batch_io *uc,
+                       const struct ublk_elem_header *elem)
+{
+       struct ublk_auto_buf_reg reg = {
+               .index = elem->buf_index,
+               .flags = (uc->flags & UBLK_BATCH_F_AUTO_BUF_REG_FALLBACK) ?
+                       UBLK_AUTO_BUF_REG_FALLBACK : 0,
+       };
+
+       return reg;
+}
+
+/*
+ * 48 can hold any type of buffer element(8, 16 and 24 bytes) because
+ * it is the least common multiple(LCM) of 8, 16 and 24
+ */
+#define UBLK_CMD_BATCH_TMP_BUF_SZ  (48 * 10)
+struct ublk_batch_io_iter {
+       /* copy to this buffer from iterator first */
+       unsigned char buf[UBLK_CMD_BATCH_TMP_BUF_SZ];
+       struct iov_iter iter;
+       unsigned done, total;
+       unsigned char elem_bytes;
+};
+

drivers/block/ublk_drv.c static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags) · line 2591

Quoted 10 lines
drivers/block/ublk_drv.c +10 -0
+static inline int
+__ublk_walk_cmd_buf(struct ublk_queue *ubq,
+                   struct ublk_batch_io_iter *iter,
+                   const struct ublk_batch_io_data *data,
+                   unsigned bytes,
+                   int (*cb)(struct ublk_queue *q,
+                           const struct ublk_batch_io_data *data,
+                           const struct ublk_elem_header *elem))
+{
+       int i, ret = 0;

unsigned i to avoid comparisons between signed and unsigned values?

Quoted 26 lines
drivers/block/ublk_drv.c +26 -0
+
+       for (i = 0; i < bytes; i += iter->elem_bytes) {
+               const struct ublk_elem_header *elem =
+                       (const struct ublk_elem_header *)&iter->buf[i];
+
+               if (unlikely(elem->tag >= data->ub->dev_info.queue_depth)) {
+                       ret = -EINVAL;
+                       break;
+               }
+
+               ret = cb(ubq, data, elem);
+               if (unlikely(ret))
+                       break;
+       }
+
+       /* revert unhandled bytes in case of failure */
+       if (ret)
+               iov_iter_revert(&iter->iter, bytes - i);
+
+       iter->done += i;
+       return ret;
+}
+
+static int ublk_walk_cmd_buf(struct ublk_batch_io_iter *iter,
+                            const struct ublk_batch_io_data *data,
+                            int (*cb)(struct ublk_queue *q,

drivers/block/ublk_drv.c static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags) · line 2627

Quoted 10 lines
drivers/block/ublk_drv.c +10 -0
+                                    const struct ublk_batch_io_data *data,
+                                    const struct ublk_elem_header *elem))
+{
+       struct ublk_queue *ubq = ublk_get_queue(data->ub, data->header.q_id);
+       int ret = 0;
+
+       while (iter->done < iter->total) {
+               unsigned int len = min(sizeof(iter->buf), iter->total - iter->done);
+
+               ret = copy_from_iter(iter->buf, len, &iter->iter);

Would make more sense to store this as an unsigned value.

drivers/block/ublk_drv.c static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags) · line 2632

Quoted 5 lines
drivers/block/ublk_drv.c +5 -0
+               if (ret != len) {
+                       pr_warn("ublk%d: read batch cmd buffer failed %u/%u\n",
+                                       data->ub->dev_info.dev_id, ret, len);
+                       ret = -EINVAL;
+                       break;

Just return -EINVAL?

drivers/block/ublk_drv.c static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags) · line 2637

Quoted 5 lines
drivers/block/ublk_drv.c +5 -0
+               }
+
+               ret = __ublk_walk_cmd_buf(ubq, iter, data, len, cb);
+               if (ret)
+                       break;

Just return ret?

Quoted 62 lines
drivers/block/ublk_drv.c +62 -0
+       }
+       return ret;
+}
+
+static int ublk_batch_unprep_io(struct ublk_queue *ubq,
+                               const struct ublk_batch_io_data *data,
+                               const struct ublk_elem_header *elem)
+{
+       struct ublk_io *io = &ubq->ios[elem->tag];
+
+       data->ub->nr_io_ready--;
+       ublk_io_lock(io);
+       io->flags = 0;
+       ublk_io_unlock(io);
+       return 0;
+}
+
+static void ublk_batch_revert_prep_cmd(struct ublk_batch_io_iter *iter,
+                                      const struct ublk_batch_io_data *data)
+{
+       int ret;
+
+       if (!iter->done)
+               return;
+
+       iov_iter_revert(&iter->iter, iter->done);
+       iter->total = iter->done;
+       iter->done = 0;
+
+       ret = ublk_walk_cmd_buf(iter, data, ublk_batch_unprep_io);
+       WARN_ON_ONCE(ret);
+}
+
+static int ublk_batch_prep_io(struct ublk_queue *ubq,
+                             const struct ublk_batch_io_data *data,
+                             const struct ublk_elem_header *elem)
+{
+       struct ublk_io *io = &ubq->ios[elem->tag];
+       const struct ublk_batch_io *uc = &data->header;
+       union ublk_io_buf buf = { 0 };
+       int ret;
+
+       if (ublk_dev_support_auto_buf_reg(data->ub))
+               buf.auto_reg = ublk_batch_auto_buf_reg(uc, elem);
+       else if (ublk_dev_need_map_io(data->ub)) {
+               buf.addr = ublk_batch_buf_addr(uc, elem);
+
+               ret = ublk_check_fetch_buf(data->ub, buf.addr);
+               if (ret)
+                       return ret;
+       }
+
+       ublk_io_lock(io);
+       ret = __ublk_fetch(data->cmd, data->ub, io);
+       if (!ret)
+               io->buf = buf;
+       ublk_io_unlock(io);
+
+       return ret;
+}
+
+static int ublk_handle_batch_prep_cmd(const struct ublk_batch_io_data *data)

drivers/block/ublk_drv.c static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags) · line 2709

Quoted 10 lines
drivers/block/ublk_drv.c +10 -0
+{
+       const struct ublk_batch_io *uc = &data->header;
+       struct io_uring_cmd *cmd = data->cmd;
+       struct ublk_batch_io_iter iter = {
+               .total = uc->nr_elem * uc->elem_bytes,
+               .elem_bytes = uc->elem_bytes,
+       };
+       int ret;
+
+       ret = io_uring_cmd_import_fixed(cmd->sqe->addr, iter.total,

sqe-> addr should be accessed with READ_ONCE() since it may point to user-mapped memory.

drivers/block/ublk_drv.c static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags) · line 2718

Quoted 9 lines
drivers/block/ublk_drv.c +9 -0
+                       WRITE, &iter.iter, cmd, data->issue_flags);
+       if (ret)
+               return ret;
+
+       mutex_lock(&data->ub->mutex);
+       ret = ublk_walk_cmd_buf(&iter, data, ublk_batch_prep_io);
+
+       if (ret && iter.done)
+               ublk_batch_revert_prep_cmd(&iter, data);

Mentioned this on V1 as well, but the iter.done check is duplicated in ublk_batch_revert_prep_cmd().

Best,
Caleb

Quoted 77 lines
drivers/block/ublk_drv.c +19 -1
+       mutex_unlock(&data->ub->mutex);
+       return ret;
+}
+
 static int ublk_check_batch_cmd_flags(const struct ublk_batch_io *uc)
 {
        const u16 mask = UBLK_BATCH_F_HAS_BUF_ADDR | UBLK_BATCH_F_HAS_ZONE_LBA;
@@ -2609,6 +2798,7 @@ static int ublk_ch_batch_io_uring_cmd(struct io_uring_cmd *cmd,
                        .nr_elem = READ_ONCE(uc->nr_elem),
                        .elem_bytes = READ_ONCE(uc->elem_bytes),
                },
+               .issue_flags = issue_flags,
        };
        u32 cmd_op = cmd->cmd_op;
        int ret = -EINVAL;
@@ -2618,6 +2808,11 @@ static int ublk_ch_batch_io_uring_cmd(struct io_uring_cmd *cmd,
 
        switch (cmd_op) {
        case UBLK_U_IO_PREP_IO_CMDS:
+               ret = ublk_check_batch_cmd(&data);
+               if (ret)
+                       goto out;
+               ret = ublk_handle_batch_prep_cmd(&data);
+               break;
        case UBLK_U_IO_COMMIT_IO_CMDS:
                ret = ublk_check_batch_cmd(&data);
                if (ret)
@@ -2792,7 +2987,7 @@ static int ublk_init_queue(struct ublk_device *ub, int q_id)
        struct ublk_queue *ubq;
        struct page *page;
        int numa_node;
-       int size;
+       int size, i;
 
        /* Determine NUMA node based on queue's CPU affinity */
        numa_node = ublk_get_queue_numa_node(ub, q_id);
@@ -2817,6 +3012,9 @@ static int ublk_init_queue(struct ublk_device *ub, int q_id)
        }
        ubq->io_cmd_buf = page_address(page);
 
+       for (i = 0; i < ubq->q_depth; i++)
+               spin_lock_init(&ubq->ios[i].lock);
+
        ub->queues[q_id] = ubq;
        ubq->dev = ub;
        return 0;
@@ -3043,6 +3241,11 @@ static int ublk_ctrl_start_dev(struct ublk_device *ub,
                return -EINVAL;
 
        mutex_lock(&ub->mutex);
+       /* device may become not ready in case of F_BATCH */
+       if (!ublk_dev_ready(ub)) {
+               ret = -EINVAL;
+               goto out_unlock;
+       }
        if (ub->dev_info.state == UBLK_S_DEV_LIVE ||
            test_bit(UB_STATE_USED, &ub->state)) {
                ret = -EEXIST;
include/uapi/linux/ublk_cmd.h +5 -1
index 2ce5a496b622..c96c299057c3 100644
@@ -102,6 +102,11 @@
        _IOWR('u', 0x23, struct ublksrv_io_cmd)
 #define        UBLK_U_IO_UNREGISTER_IO_BUF     \
        _IOWR('u', 0x24, struct ublksrv_io_cmd)
+
+/*
+ * return 0 if the command is run successfully, otherwise failure code
+ * is returned
+ */
 #define        UBLK_U_IO_PREP_IO_CMDS  \
        _IOWR('u', 0x25, struct ublk_batch_io)
 #define        UBLK_U_IO_COMMIT_IO_CMDS        \
--

2.47.0

author
#30 lore raw

Re: [PATCH V3 10/27] ublk: handle UBLK_U_IO_PREP_IO_CMDS

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

This commit implements the handling of the UBLK_U_IO_PREP_IO_CMDS command, which allows userspace to prepare a batch of I/O requests.

The core of this change is the ublk_walk_cmd_buf function, which iterates over the elements in the uring_cmd fixed buffer. For each element, it parses the I/O details, finds the corresponding ublk_io structure, and prepares it for future dispatch.

Add per-io lock for protecting concurrent delivery and committing.

---
 drivers/block/ublk_drv.c      | 205 +++++++++++++++++++++++++++++++++-
 include/uapi/linux/ublk_cmd.h |   5 +
 2 files changed, 209 insertions(+), 1 deletion(-)
drivers/block/ublk_drv.c +52 -0
index 5f9d7ec9daa4..84d838df18cb 100644
@@ -117,6 +117,7 @@ struct ublk_batch_io_data {
        struct ublk_device *ub;
        struct io_uring_cmd *cmd;
        struct ublk_batch_io header;
+       unsigned int issue_flags;
 };
 
 /*
@@ -201,6 +202,7 @@ struct ublk_io {
        unsigned task_registered_buffers;
 
        void *buf_ctx_handle;
+       spinlock_t lock;
 } ____cacheline_aligned_in_smp;
 
 struct ublk_queue {
@@ -270,6 +272,16 @@ static inline bool ublk_dev_support_batch_io(const struct ublk_device *ub)
        return false;
 }
 
+static inline void ublk_io_lock(struct ublk_io *io)
+{
+       spin_lock(&io->lock);
+}
+
+static inline void ublk_io_unlock(struct ublk_io *io)
+{
+       spin_unlock(&io->lock);
+}
+
 static inline struct ublksrv_io_desc *
 ublk_get_iod(const struct ublk_queue *ubq, unsigned tag)
 {
@@ -2531,6 +2543,183 @@ static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags)
        return ublk_ch_uring_cmd_local(cmd, issue_flags);
 }
 
+static inline __u64 ublk_batch_buf_addr(const struct ublk_batch_io *uc,
+                                       const struct ublk_elem_header *elem)
+{
+       const void *buf = elem;
+
+       if (uc->flags & UBLK_BATCH_F_HAS_BUF_ADDR)
+               return *(__u64 *)(buf + sizeof(*elem));
+       return 0;
+}
+
+static struct ublk_auto_buf_reg
+ublk_batch_auto_buf_reg(const struct ublk_batch_io *uc,
+                       const struct ublk_elem_header *elem)
+{
+       struct ublk_auto_buf_reg reg = {
+               .index = elem->buf_index,
+               .flags = (uc->flags & UBLK_BATCH_F_AUTO_BUF_REG_FALLBACK) ?
+                       UBLK_AUTO_BUF_REG_FALLBACK : 0,
+       };
+
+       return reg;
+}
+
+/*
+ * 48 can hold any type of buffer element(8, 16 and 24 bytes) because
+ * it is the least common multiple(LCM) of 8, 16 and 24
+ */
+#define UBLK_CMD_BATCH_TMP_BUF_SZ  (48 * 10)
+struct ublk_batch_io_iter {
+       /* copy to this buffer from iterator first */
+       unsigned char buf[UBLK_CMD_BATCH_TMP_BUF_SZ];
+       struct iov_iter iter;
+       unsigned done, total;
+       unsigned char elem_bytes;
+};
+
+static inline int
+__ublk_walk_cmd_buf(struct ublk_queue *ubq,
+                   struct ublk_batch_io_iter *iter,
+                   const struct ublk_batch_io_data *data,
Quoted 8 lines

drivers/block/ublk_drv.c static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags) · line 2591

Quoted 6 lines
drivers/block/ublk_drv.c +6 -0
+                   unsigned bytes,
+                   int (*cb)(struct ublk_queue *q,
+                           const struct ublk_batch_io_data *data,
+                           const struct ublk_elem_header *elem))
+{
+       int i, ret = 0;

unsigned i to avoid comparisons between signed and unsigned values?

OK.

Quoted 31 lines
Quoted 31 lines
drivers/block/ublk_drv.c +30 -0
+
+       for (i = 0; i < bytes; i += iter->elem_bytes) {
+               const struct ublk_elem_header *elem =
+                       (const struct ublk_elem_header *)&iter->buf[i];
+
+               if (unlikely(elem->tag >= data->ub->dev_info.queue_depth)) {
+                       ret = -EINVAL;
+                       break;
+               }
+
+               ret = cb(ubq, data, elem);
+               if (unlikely(ret))
+                       break;
+       }
+
+       /* revert unhandled bytes in case of failure */
+       if (ret)
+               iov_iter_revert(&iter->iter, bytes - i);
+
+       iter->done += i;
+       return ret;
+}
+
+static int ublk_walk_cmd_buf(struct ublk_batch_io_iter *iter,
+                            const struct ublk_batch_io_data *data,
+                            int (*cb)(struct ublk_queue *q,
+                                    const struct ublk_batch_io_data *data,
+                                    const struct ublk_elem_header *elem))
+{
+       struct ublk_queue *ubq = ublk_get_queue(data->ub, data->header.q_id);
Quoted 8 lines

drivers/block/ublk_drv.c static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags) · line 2627

Quoted 6 lines
drivers/block/ublk_drv.c +6 -0
+       int ret = 0;
+
+       while (iter->done < iter->total) {
+               unsigned int len = min(sizeof(iter->buf), iter->total - iter->done);
+
+               ret = copy_from_iter(iter->buf, len, &iter->iter);

Would make more sense to store this as an unsigned value.

OK.

BTW, it has been changed to plain copy_from_user() in my local version by dropping fixed buffer for commit/prep ios command.

There is also one big bug fix in patch 'ublk: add batch I/O dispatch infrastructure',

Do you prefer to continuing to review on V3 or the coming V4?

If you prefer to V4, I can prepare and send it soon.

Quoted 84 lines

drivers/block/ublk_drv.c static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags) · line 2632

Quoted 6 lines
drivers/block/ublk_drv.c +5 -0
+               if (ret != len) {
+                       pr_warn("ublk%d: read batch cmd buffer failed %u/%u\n",
+                                       data->ub->dev_info.dev_id, ret, len);
+                       ret = -EINVAL;
+                       break;

Just return -EINVAL?

drivers/block/ublk_drv.c static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags) · line 2637

Quoted 5 lines
drivers/block/ublk_drv.c +5 -0
+               }
+
+               ret = __ublk_walk_cmd_buf(ubq, iter, data, len, cb);
+               if (ret)
+                       break;

Just return ret?

Quoted 67 lines
drivers/block/ublk_drv.c +67 -0
+       }
+       return ret;
+}
+
+static int ublk_batch_unprep_io(struct ublk_queue *ubq,
+                               const struct ublk_batch_io_data *data,
+                               const struct ublk_elem_header *elem)
+{
+       struct ublk_io *io = &ubq->ios[elem->tag];
+
+       data->ub->nr_io_ready--;
+       ublk_io_lock(io);
+       io->flags = 0;
+       ublk_io_unlock(io);
+       return 0;
+}
+
+static void ublk_batch_revert_prep_cmd(struct ublk_batch_io_iter *iter,
+                                      const struct ublk_batch_io_data *data)
+{
+       int ret;
+
+       if (!iter->done)
+               return;
+
+       iov_iter_revert(&iter->iter, iter->done);
+       iter->total = iter->done;
+       iter->done = 0;
+
+       ret = ublk_walk_cmd_buf(iter, data, ublk_batch_unprep_io);
+       WARN_ON_ONCE(ret);
+}
+
+static int ublk_batch_prep_io(struct ublk_queue *ubq,
+                             const struct ublk_batch_io_data *data,
+                             const struct ublk_elem_header *elem)
+{
+       struct ublk_io *io = &ubq->ios[elem->tag];
+       const struct ublk_batch_io *uc = &data->header;
+       union ublk_io_buf buf = { 0 };
+       int ret;
+
+       if (ublk_dev_support_auto_buf_reg(data->ub))
+               buf.auto_reg = ublk_batch_auto_buf_reg(uc, elem);
+       else if (ublk_dev_need_map_io(data->ub)) {
+               buf.addr = ublk_batch_buf_addr(uc, elem);
+
+               ret = ublk_check_fetch_buf(data->ub, buf.addr);
+               if (ret)
+                       return ret;
+       }
+
+       ublk_io_lock(io);
+       ret = __ublk_fetch(data->cmd, data->ub, io);
+       if (!ret)
+               io->buf = buf;
+       ublk_io_unlock(io);
+
+       return ret;
+}
+
+static int ublk_handle_batch_prep_cmd(const struct ublk_batch_io_data *data)
+{
+       const struct ublk_batch_io *uc = &data->header;
+       struct io_uring_cmd *cmd = data->cmd;
+       struct ublk_batch_io_iter iter = {
+               .total = uc->nr_elem * uc->elem_bytes,
Quoted 8 lines

drivers/block/ublk_drv.c static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags) · line 2709

Quoted 5 lines
drivers/block/ublk_drv.c +5 -0
+               .elem_bytes = uc->elem_bytes,
+       };
+       int ret;
+
+       ret = io_uring_cmd_import_fixed(cmd->sqe->addr, iter.total,

sqe-> addr should be accessed with READ_ONCE() since it may point to user-mapped memory.

OK.

Quoted 13 lines

drivers/block/ublk_drv.c static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags) · line 2718

Quoted 10 lines
drivers/block/ublk_drv.c +9 -0
+                       WRITE, &iter.iter, cmd, data->issue_flags);
+       if (ret)
+               return ret;
+
+       mutex_lock(&data->ub->mutex);
+       ret = ublk_walk_cmd_buf(&iter, data, ublk_batch_prep_io);
+
+       if (ret && iter.done)
+               ublk_batch_revert_prep_cmd(&iter, data);

Mentioned this on V1 as well, but the iter.done check is duplicated in ublk_batch_revert_prep_cmd().

OK, will drop the check in ublk_batch_revert_prep_cmd().

Thanks,
Ming

#31 lore raw

Re: [PATCH V3 10/27] ublk: handle UBLK_U_IO_PREP_IO_CMDS

Quoted 154 lines · Ming Lei
Quoted 99 lines · Caleb Sander Mateos
Quoted 98 lines · Ming Lei

This commit implements the handling of the UBLK_U_IO_PREP_IO_CMDS command, which allows userspace to prepare a batch of I/O requests.

The core of this change is the ublk_walk_cmd_buf function, which iterates over the elements in the uring_cmd fixed buffer. For each element, it parses the I/O details, finds the corresponding ublk_io structure, and prepares it for future dispatch.

Add per-io lock for protecting concurrent delivery and committing.

---
 drivers/block/ublk_drv.c      | 205 +++++++++++++++++++++++++++++++++-
 include/uapi/linux/ublk_cmd.h |   5 +
 2 files changed, 209 insertions(+), 1 deletion(-)
drivers/block/ublk_drv.c +52 -0
index 5f9d7ec9daa4..84d838df18cb 100644
@@ -117,6 +117,7 @@ struct ublk_batch_io_data {
        struct ublk_device *ub;
        struct io_uring_cmd *cmd;
        struct ublk_batch_io header;
+       unsigned int issue_flags;
 };
 
 /*
@@ -201,6 +202,7 @@ struct ublk_io {
        unsigned task_registered_buffers;
 
        void *buf_ctx_handle;
+       spinlock_t lock;
 } ____cacheline_aligned_in_smp;
 
 struct ublk_queue {
@@ -270,6 +272,16 @@ static inline bool ublk_dev_support_batch_io(const struct ublk_device *ub)
        return false;
 }
 
+static inline void ublk_io_lock(struct ublk_io *io)
+{
+       spin_lock(&io->lock);
+}
+
+static inline void ublk_io_unlock(struct ublk_io *io)
+{
+       spin_unlock(&io->lock);
+}
+
 static inline struct ublksrv_io_desc *
 ublk_get_iod(const struct ublk_queue *ubq, unsigned tag)
 {
@@ -2531,6 +2543,183 @@ static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags)
        return ublk_ch_uring_cmd_local(cmd, issue_flags);
 }
 
+static inline __u64 ublk_batch_buf_addr(const struct ublk_batch_io *uc,
+                                       const struct ublk_elem_header *elem)
+{
+       const void *buf = elem;
+
+       if (uc->flags & UBLK_BATCH_F_HAS_BUF_ADDR)
+               return *(__u64 *)(buf + sizeof(*elem));
+       return 0;
+}
+
+static struct ublk_auto_buf_reg
+ublk_batch_auto_buf_reg(const struct ublk_batch_io *uc,
+                       const struct ublk_elem_header *elem)
+{
+       struct ublk_auto_buf_reg reg = {
+               .index = elem->buf_index,
+               .flags = (uc->flags & UBLK_BATCH_F_AUTO_BUF_REG_FALLBACK) ?
+                       UBLK_AUTO_BUF_REG_FALLBACK : 0,
+       };
+
+       return reg;
+}
+
+/*
+ * 48 can hold any type of buffer element(8, 16 and 24 bytes) because
+ * it is the least common multiple(LCM) of 8, 16 and 24
+ */
+#define UBLK_CMD_BATCH_TMP_BUF_SZ  (48 * 10)
+struct ublk_batch_io_iter {
+       /* copy to this buffer from iterator first */
+       unsigned char buf[UBLK_CMD_BATCH_TMP_BUF_SZ];
+       struct iov_iter iter;
+       unsigned done, total;
+       unsigned char elem_bytes;
+};
+
+static inline int
+__ublk_walk_cmd_buf(struct ublk_queue *ubq,
+                   struct ublk_batch_io_iter *iter,
+                   const struct ublk_batch_io_data *data,
Quoted 8 lines

drivers/block/ublk_drv.c static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags) · line 2591

Quoted 6 lines
drivers/block/ublk_drv.c +6 -0
+                   unsigned bytes,
+                   int (*cb)(struct ublk_queue *q,
+                           const struct ublk_batch_io_data *data,
+                           const struct ublk_elem_header *elem))
+{
+       int i, ret = 0;

unsigned i to avoid comparisons between signed and unsigned values?

OK.

Quoted 31 lines
Quoted 31 lines
drivers/block/ublk_drv.c +30 -0
+
+       for (i = 0; i < bytes; i += iter->elem_bytes) {
+               const struct ublk_elem_header *elem =
+                       (const struct ublk_elem_header *)&iter->buf[i];
+
+               if (unlikely(elem->tag >= data->ub->dev_info.queue_depth)) {
+                       ret = -EINVAL;
+                       break;
+               }
+
+               ret = cb(ubq, data, elem);
+               if (unlikely(ret))
+                       break;
+       }
+
+       /* revert unhandled bytes in case of failure */
+       if (ret)
+               iov_iter_revert(&iter->iter, bytes - i);
+
+       iter->done += i;
+       return ret;
+}
+
+static int ublk_walk_cmd_buf(struct ublk_batch_io_iter *iter,
+                            const struct ublk_batch_io_data *data,
+                            int (*cb)(struct ublk_queue *q,
+                                    const struct ublk_batch_io_data *data,
+                                    const struct ublk_elem_header *elem))
+{
+       struct ublk_queue *ubq = ublk_get_queue(data->ub, data->header.q_id);
Quoted 8 lines

drivers/block/ublk_drv.c static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags) · line 2627

Quoted 6 lines
drivers/block/ublk_drv.c +6 -0
+       int ret = 0;
+
+       while (iter->done < iter->total) {
+               unsigned int len = min(sizeof(iter->buf), iter->total - iter->done);
+
+               ret = copy_from_iter(iter->buf, len, &iter->iter);

Would make more sense to store this as an unsigned value.

OK.

Quoted 8 lines

BTW, it has been changed to plain copy_from_user() in my local version by dropping fixed buffer for commit/prep ios command.

There is also one big bug fix in patch 'ublk: add batch I/O dispatch infrastructure',

Do you prefer to continuing to review on V3 or the coming V4?

If you prefer to V4, I can prepare and send it soon.

Either is fine. Sorry I have been getting to these patches in my spare time, so not sure I'll have time to review the whole series. But I'll try to take a look at the latest version as time allows.

Best,
Caleb

Quoted 115 lines
Quoted 85 lines

drivers/block/ublk_drv.c static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags) · line 2632

Quoted 7 lines
drivers/block/ublk_drv.c +5 -0
+               if (ret != len) {
+                       pr_warn("ublk%d: read batch cmd buffer failed %u/%u\n",
+                                       data->ub->dev_info.dev_id, ret, len);
+                       ret = -EINVAL;
+                       break;

Just return -EINVAL?

drivers/block/ublk_drv.c static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags) · line 2637

Quoted 5 lines
drivers/block/ublk_drv.c +5 -0
+               }
+
+               ret = __ublk_walk_cmd_buf(ubq, iter, data, len, cb);
+               if (ret)
+                       break;

Just return ret?

Quoted 67 lines
drivers/block/ublk_drv.c +67 -0
+       }
+       return ret;
+}
+
+static int ublk_batch_unprep_io(struct ublk_queue *ubq,
+                               const struct ublk_batch_io_data *data,
+                               const struct ublk_elem_header *elem)
+{
+       struct ublk_io *io = &ubq->ios[elem->tag];
+
+       data->ub->nr_io_ready--;
+       ublk_io_lock(io);
+       io->flags = 0;
+       ublk_io_unlock(io);
+       return 0;
+}
+
+static void ublk_batch_revert_prep_cmd(struct ublk_batch_io_iter *iter,
+                                      const struct ublk_batch_io_data *data)
+{
+       int ret;
+
+       if (!iter->done)
+               return;
+
+       iov_iter_revert(&iter->iter, iter->done);
+       iter->total = iter->done;
+       iter->done = 0;
+
+       ret = ublk_walk_cmd_buf(iter, data, ublk_batch_unprep_io);
+       WARN_ON_ONCE(ret);
+}
+
+static int ublk_batch_prep_io(struct ublk_queue *ubq,
+                             const struct ublk_batch_io_data *data,
+                             const struct ublk_elem_header *elem)
+{
+       struct ublk_io *io = &ubq->ios[elem->tag];
+       const struct ublk_batch_io *uc = &data->header;
+       union ublk_io_buf buf = { 0 };
+       int ret;
+
+       if (ublk_dev_support_auto_buf_reg(data->ub))
+               buf.auto_reg = ublk_batch_auto_buf_reg(uc, elem);
+       else if (ublk_dev_need_map_io(data->ub)) {
+               buf.addr = ublk_batch_buf_addr(uc, elem);
+
+               ret = ublk_check_fetch_buf(data->ub, buf.addr);
+               if (ret)
+                       return ret;
+       }
+
+       ublk_io_lock(io);
+       ret = __ublk_fetch(data->cmd, data->ub, io);
+       if (!ret)
+               io->buf = buf;
+       ublk_io_unlock(io);
+
+       return ret;
+}
+
+static int ublk_handle_batch_prep_cmd(const struct ublk_batch_io_data *data)
+{
+       const struct ublk_batch_io *uc = &data->header;
+       struct io_uring_cmd *cmd = data->cmd;
+       struct ublk_batch_io_iter iter = {
+               .total = uc->nr_elem * uc->elem_bytes,
Quoted 8 lines

drivers/block/ublk_drv.c static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags) · line 2709

Quoted 5 lines
drivers/block/ublk_drv.c +5 -0
+               .elem_bytes = uc->elem_bytes,
+       };
+       int ret;
+
+       ret = io_uring_cmd_import_fixed(cmd->sqe->addr, iter.total,

sqe-> addr should be accessed with READ_ONCE() since it may point to user-mapped memory.

OK.

Quoted 13 lines

drivers/block/ublk_drv.c static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags) · line 2718

Quoted 10 lines
drivers/block/ublk_drv.c +9 -0
+                       WRITE, &iter.iter, cmd, data->issue_flags);
+       if (ret)
+               return ret;
+
+       mutex_lock(&data->ub->mutex);
+       ret = ublk_walk_cmd_buf(&iter, data, ublk_batch_prep_io);
+
+       if (ret && iter.done)
+               ublk_batch_revert_prep_cmd(&iter, data);

Mentioned this on V1 as well, but the iter.done check is duplicated in ublk_batch_revert_prep_cmd().

OK, will drop the check in ublk_batch_revert_prep_cmd().

Thanks,
Ming

author
#32 lore raw

PATCH v3 11/27 ublk: handle UBLK_U_IO_COMMIT_IO_CMDS

In reply to Ming Lei

Handle UBLK_U_IO_COMMIT_IO_CMDS by walking the uring_cmd fixed buffer:

- read each element into one temp buffer in batch style

- parse and apply each element for committing io result

---
 drivers/block/ublk_drv.c      | 121 ++++++++++++++++++++++++++++++++--
 include/uapi/linux/ublk_cmd.h |   8 +++
 2 files changed, 125 insertions(+), 4 deletions(-)
drivers/block/ublk_drv.c +117 -4
index 84d838df18cb..f79efbaba340 100644
@@ -2098,9 +2098,9 @@ static inline int ublk_set_auto_buf_reg(struct ublk_io *io, struct io_uring_cmd
 	return 0;
 }
 
-static int ublk_handle_auto_buf_reg(struct ublk_io *io,
-				    struct io_uring_cmd *cmd,
-				    u16 *buf_idx)
+static void __ublk_handle_auto_buf_reg(struct ublk_io *io,
+				       struct io_uring_cmd *cmd,
+				       u16 *buf_idx)
 {
 	if (io->flags & UBLK_IO_FLAG_AUTO_BUF_REG) {
 		io->flags &= ~UBLK_IO_FLAG_AUTO_BUF_REG;
@@ -2118,7 +2118,13 @@ static int ublk_handle_auto_buf_reg(struct ublk_io *io,
 		if (io->buf_ctx_handle == io_uring_cmd_ctx_handle(cmd))
 			*buf_idx = io->buf.auto_reg.index;
 	}
+}
 
+static int ublk_handle_auto_buf_reg(struct ublk_io *io,
+				    struct io_uring_cmd *cmd,
+				    u16 *buf_idx)
+{
+	__ublk_handle_auto_buf_reg(io, cmd, buf_idx);
 	return ublk_set_auto_buf_reg(io, cmd);
 }
 
@@ -2553,6 +2559,17 @@ static inline __u64 ublk_batch_buf_addr(const struct ublk_batch_io *uc,
 	return 0;
 }
 
+static inline __u64 ublk_batch_zone_lba(const struct ublk_batch_io *uc,
+					const struct ublk_elem_header *elem)
+{
+	const void *buf = (const void *)elem;
+
+	if (uc->flags & UBLK_BATCH_F_HAS_ZONE_LBA)
+		return *(__u64 *)(buf + sizeof(*elem) +
+				8 * !!(uc->flags & UBLK_BATCH_F_HAS_BUF_ADDR));
+	return -1;
+}
+
 static struct ublk_auto_buf_reg
 ublk_batch_auto_buf_reg(const struct ublk_batch_io *uc,
 			const struct ublk_elem_header *elem)
@@ -2720,6 +2737,102 @@ static int ublk_handle_batch_prep_cmd(const struct ublk_batch_io_data *data)
 	return ret;
 }
 
+static int ublk_batch_commit_io_check(const struct ublk_queue *ubq,
+				      struct ublk_io *io,
+				      union ublk_io_buf *buf)
+{
+	struct request *req = io->req;
+
+	if (!req)
+		return -EINVAL;
+
+	if (io->flags & UBLK_IO_FLAG_ACTIVE)
+		return -EBUSY;
+
+	if (!(io->flags & UBLK_IO_FLAG_OWNED_BY_SRV))
+		return -EINVAL;
+
+	if (ublk_need_map_io(ubq)) {
+		/*
+		 * COMMIT_AND_FETCH_REQ has to provide IO buffer if
+		 * NEED GET DATA is not enabled or it is Read IO.
+		 */
+		if (!buf->addr && (!ublk_need_get_data(ubq) ||
+					req_op(req) == REQ_OP_READ))
+			return -EINVAL;
+	}
+	return 0;
+}
+
+static int ublk_batch_commit_io(struct ublk_queue *ubq,
+				const struct ublk_batch_io_data *data,
+				const struct ublk_elem_header *elem)
+{
+	struct ublk_io *io = &ubq->ios[elem->tag];
+	const struct ublk_batch_io *uc = &data->header;
+	u16 buf_idx = UBLK_INVALID_BUF_IDX;
+	union ublk_io_buf buf = { 0 };
+	struct request *req = NULL;
+	bool auto_reg = false;
+	bool compl = false;
+	int ret;
+
+	if (ublk_dev_support_auto_buf_reg(data->ub)) {
+		buf.auto_reg = ublk_batch_auto_buf_reg(uc, elem);
+		auto_reg = true;
+	} else if (ublk_dev_need_map_io(data->ub))
+		buf.addr = ublk_batch_buf_addr(uc, elem);
+
+	ublk_io_lock(io);
+	ret = ublk_batch_commit_io_check(ubq, io, &buf);
+	if (!ret) {
+		io->res = elem->result;
+		io->buf = buf;
+		req = ublk_fill_io_cmd(io, data->cmd);
+
+		if (auto_reg)
+			__ublk_handle_auto_buf_reg(io, data->cmd, &buf_idx);
+		compl = ublk_need_complete_req(data->ub, io);
+	}
+	ublk_io_unlock(io);
+
+	if (unlikely(ret)) {
+		pr_warn("%s: dev %u queue %u io %u: commit failure %d\n",
+			__func__, data->ub->dev_info.dev_id, ubq->q_id,
+			elem->tag, ret);
+		return ret;
+	}
+
+	/* can't touch 'ublk_io' any more */
+	if (buf_idx != UBLK_INVALID_BUF_IDX)
+		io_buffer_unregister_bvec(data->cmd, buf_idx, data->issue_flags);
+	if (req_op(req) == REQ_OP_ZONE_APPEND)
+		req->__sector = ublk_batch_zone_lba(uc, elem);
+	if (compl)
+		__ublk_complete_rq(req, io, ublk_dev_need_map_io(data->ub));
+	return 0;
+}
+
+static int ublk_handle_batch_commit_cmd(const struct ublk_batch_io_data *data)
+{
+	const struct ublk_batch_io *uc = &data->header;
+	struct io_uring_cmd *cmd = data->cmd;
+	struct ublk_batch_io_iter iter = {
+		.total = uc->nr_elem * uc->elem_bytes,
+		.elem_bytes = uc->elem_bytes,
+	};
+	int ret;
+
+	ret = io_uring_cmd_import_fixed(cmd->sqe->addr, iter.total,
+			WRITE, &iter.iter, cmd, data->issue_flags);
+	if (ret)
+		return ret;
+
+	ret = ublk_walk_cmd_buf(&iter, data, ublk_batch_commit_io);
+
+	return iter.done == 0 ? ret : iter.done;
+}
+
 static int ublk_check_batch_cmd_flags(const struct ublk_batch_io *uc)
 {
 	const u16 mask = UBLK_BATCH_F_HAS_BUF_ADDR | UBLK_BATCH_F_HAS_ZONE_LBA;
@@ -2817,7 +2930,7 @@ static int ublk_ch_batch_io_uring_cmd(struct io_uring_cmd *cmd,
 		ret = ublk_check_batch_cmd(&data);
 		if (ret)
 			goto out;
-		ret = -EOPNOTSUPP;
+		ret = ublk_handle_batch_commit_cmd(&data);
 		break;
 	default:
 		ret = -EOPNOTSUPP;
include/uapi/linux/ublk_cmd.h +8 -0
index c96c299057c3..295ec8f34173 100644
@@ -109,6 +109,14 @@
  */
 #define	UBLK_U_IO_PREP_IO_CMDS	\
 	_IOWR('u', 0x25, struct ublk_batch_io)
+/*
+ * If failure code is returned, nothing in the command buffer is handled.
+ * Otherwise, the returned value means how many bytes in command buffer
+ * are handled actually, then number of handled IOs can be calculated with
+ * `elem_bytes` for each IO. IOs in the remained bytes are not committed,
+ * userspace has to check return value for dealing with partial committing
+ * correctly.
+ */
 #define	UBLK_U_IO_COMMIT_IO_CMDS	\
 	_IOWR('u', 0x26, struct ublk_batch_io)
 
Signature · 1 line
2.47.0
author
#33 lore raw

PATCH v3 12/27 ublk: add io events fifo structure

In reply to Ming Lei

Add ublk io events fifo structure and prepare for supporting command batch, which will use io_uring multishot uring_cmd for fetching one batch of io commands each time.

One nice feature of kfifo is to allow multiple producer vs single consumer. We just need lock the producer side, meantime the single consumer can be lockless.

The producer is actually from ublk_queue_rq() or ublk_queue_rqs(), so lock contention can be eased by setting proper blk-mq nr_queues.

---
 drivers/block/ublk_drv.c | 65 ++++++++++++++++++++++++++++++++++++----
 1 file changed, 60 insertions(+), 5 deletions(-)
drivers/block/ublk_drv.c +60 -5
index f79efbaba340..840d6a16bd4b 100644
@@ -44,6 +44,7 @@
 #include <linux/task_work.h>
 #include <linux/namei.h>
 #include <linux/kref.h>
+#include <linux/kfifo.h>
 #include <uapi/linux/ublk_cmd.h>
 
 #define UBLK_MINORS		(1U << MINORBITS)
@@ -217,6 +218,22 @@ struct ublk_queue {
 	bool fail_io; /* copy of dev->state == UBLK_S_DEV_FAIL_IO */
 	spinlock_t		cancel_lock;
 	struct ublk_device *dev;
+
+	/*
+	 * Inflight ublk request tag is saved in this fifo
+	 *
+	 * There are multiple writer from ublk_queue_rq() or ublk_queue_rqs(),
+	 * so lock is required for storing request tag to fifo
+	 *
+	 * Make sure just one reader for fetching request from task work
+	 * function to ublk server, so no need to grab the lock in reader
+	 * side.
+	 */
+	struct {
+		DECLARE_KFIFO_PTR(evts_fifo, unsigned short);
+		spinlock_t evts_lock;
+	}____cacheline_aligned_in_smp;
+
 	struct ublk_io ios[] __counted_by(q_depth);
 };
 
@@ -282,6 +299,32 @@ static inline void ublk_io_unlock(struct ublk_io *io)
 	spin_unlock(&io->lock);
 }
 
+/* Initialize the queue */
+static inline int ublk_io_evts_init(struct ublk_queue *q, unsigned int size,
+				    int numa_node)
+{
+	spin_lock_init(&q->evts_lock);
+	return kfifo_alloc_node(&q->evts_fifo, size, GFP_KERNEL, numa_node);
+}
+
+/* Check if queue is empty */
+static inline bool ublk_io_evts_empty(const struct ublk_queue *q)
+{
+	return kfifo_is_empty(&q->evts_fifo);
+}
+
+/* Check if queue is full */
+static inline bool ublk_io_evts_full(const struct ublk_queue *q)
+{
+	return kfifo_is_full(&q->evts_fifo);
+}
+
+static inline void ublk_io_evts_deinit(struct ublk_queue *q)
+{
+	WARN_ON_ONCE(!kfifo_is_empty(&q->evts_fifo));
+	kfifo_free(&q->evts_fifo);
+}
+
 static inline struct ublksrv_io_desc *
 ublk_get_iod(const struct ublk_queue *ubq, unsigned tag)
 {
@@ -3076,6 +3119,9 @@ static void ublk_deinit_queue(struct ublk_device *ub, int q_id)
 	if (ubq->io_cmd_buf)
 		free_pages((unsigned long)ubq->io_cmd_buf, get_order(size));
 
+	if (ublk_dev_support_batch_io(ub))
+		ublk_io_evts_deinit(ubq);
+
 	kvfree(ubq);
 	ub->queues[q_id] = NULL;
 }
@@ -3100,7 +3146,7 @@ static int ublk_init_queue(struct ublk_device *ub, int q_id)
 	struct ublk_queue *ubq;
 	struct page *page;
 	int numa_node;
-	int size, i;
+	int size, i, ret = -ENOMEM;
 
 	/* Determine NUMA node based on queue's CPU affinity */
 	numa_node = ublk_get_queue_numa_node(ub, q_id);
@@ -3119,18 +3165,27 @@ static int ublk_init_queue(struct ublk_device *ub, int q_id)
 
 	/* Allocate I/O command buffer on local NUMA node */
 	page = alloc_pages_node(numa_node, gfp_flags, get_order(size));
-	if (!page) {
-		kvfree(ubq);
-		return -ENOMEM;
-	}
+	if (!page)
+		goto fail_nomem;
 	ubq->io_cmd_buf = page_address(page);
 
 	for (i = 0; i < ubq->q_depth; i++)
 		spin_lock_init(&ubq->ios[i].lock);
 
+	if (ublk_dev_support_batch_io(ub)) {
+		ret = ublk_io_evts_init(ubq, ubq->q_depth, numa_node);
+		if (ret)
+			goto fail;
+	}
 	ub->queues[q_id] = ubq;
 	ubq->dev = ub;
+
 	return 0;
+fail:
+	ublk_deinit_queue(ub, q_id);
+fail_nomem:
+	kvfree(ubq);
+	return ret;
 }
 
 static void ublk_deinit_queues(struct ublk_device *ub)
Signature · 1 line
2.47.0
author
#34 lore raw

PATCH v3 13/27 ublk: add batch I/O dispatch infrastructure

In reply to Ming Lei

Add infrastructure for delivering I/O commands to ublk server in batches, preparing for the upcoming UBLK_U_IO_FETCH_IO_CMDS feature.

Key components:

- struct ublk_batch_fcmd: Represents a batch fetch uring_cmd that will receive multiple I/O tags in a single operation, using io_uring's multishot command for efficient ublk IO delivery.

- ublk_batch_dispatch(): Batch version of ublk_dispatch_req() that:
* Pulls multiple request tags from the events FIFO (lock-free reader)
* Prepares each I/O for delivery (including auto buffer registration)
* Delivers tags to userspace via single uring_cmd notification
* Handles partial failures by restoring undelivered tags to FIFO

The batch approach significantly reduces notification overhead by aggregating multiple I/O completions into single uring_cmd, while maintaining the same I/O processing semantics as individual operations.

Error handling ensures system consistency: if buffer selection or CQE posting fails, undelivered tags are restored to the FIFO for retry.

This runs in task work context, scheduled via io_uring_cmd_complete_in_task() or called directly from ->uring_cmd(), enabling efficient batch processing without blocking the I/O submission path.

---
 drivers/block/ublk_drv.c      | 126 ++++++++++++++++++++++++++++++++++
 include/uapi/linux/ublk_cmd.h |   6 ++
 2 files changed, 132 insertions(+)
drivers/block/ublk_drv.c +126 -0
index 840d6a16bd4b..c9183ba99792 100644
@@ -91,6 +91,12 @@
 	 UBLK_BATCH_F_HAS_BUF_ADDR | \
 	 UBLK_BATCH_F_AUTO_BUF_REG_FALLBACK)
 
+/* ublk batch fetch uring_cmd */
+struct ublk_batch_fcmd {
+	struct io_uring_cmd *cmd;
+	unsigned short buf_group;
+};
+
 struct ublk_uring_cmd_pdu {
 	/*
 	 * Store requests in same batch temporarily for queuing them to
@@ -616,6 +622,32 @@ static wait_queue_head_t ublk_idr_wq;	/* wait until one idr is freed */
 static DEFINE_MUTEX(ublk_ctl_mutex);
 
 
+static void ublk_batch_deinit_fetch_buf(const struct ublk_batch_io_data *data,
+					struct ublk_batch_fcmd *fcmd,
+					int res)
+{
+	io_uring_cmd_done(fcmd->cmd, res, data->issue_flags);
+	fcmd->cmd = NULL;
+}
+
+static int ublk_batch_fetch_post_cqe(struct ublk_batch_fcmd *fcmd,
+				     struct io_br_sel *sel,
+				     unsigned int issue_flags)
+{
+	if (io_uring_mshot_cmd_post_cqe(fcmd->cmd, sel, issue_flags))
+		return -ENOBUFS;
+	return 0;
+}
+
+static ssize_t ublk_batch_copy_io_tags(struct ublk_batch_fcmd *fcmd,
+				       void __user *buf, const u16 *tag_buf,
+				       unsigned int len)
+{
+	if (copy_to_user(buf, tag_buf, len))
+		return -EFAULT;
+	return len;
+}
+
 #define UBLK_MAX_UBLKS UBLK_MINORS
 
 /*
@@ -1378,6 +1410,100 @@ static void ublk_dispatch_req(struct ublk_queue *ubq,
 	}
 }
 
+static bool __ublk_batch_prep_dispatch(struct ublk_queue *ubq,
+				       const struct ublk_batch_io_data *data,
+				       unsigned short tag)
+{
+	struct ublk_device *ub = data->ub;
+	struct ublk_io *io = &ubq->ios[tag];
+	struct request *req = blk_mq_tag_to_rq(ub->tag_set.tags[ubq->q_id], tag);
+	enum auto_buf_reg_res res = AUTO_BUF_REG_FALLBACK;
+	struct io_uring_cmd *cmd = data->cmd;
+
+	if (!ublk_start_io(ubq, req, io))
+		return false;
+
+	if (ublk_support_auto_buf_reg(ubq) && ublk_rq_has_data(req))
+		res = __ublk_do_auto_buf_reg(ubq, req, io, cmd,
+				data->issue_flags);
+
+	ublk_io_lock(io);
+	ublk_prep_auto_buf_reg_io(ubq, req, io, cmd, res);
+	ublk_io_unlock(io);
+
+	return res != AUTO_BUF_REG_FAIL;
+}
+
+static void ublk_batch_prep_dispatch(struct ublk_queue *ubq,
+				     const struct ublk_batch_io_data *data,
+				     unsigned short *tag_buf,
+				     unsigned int len)
+{
+	int i;
+
+	for (i = 0; i < len; i += 1) {
+		unsigned short tag = tag_buf[i];
+
+		if (!__ublk_batch_prep_dispatch(ubq, data, tag))
+			tag_buf[i] = UBLK_BATCH_IO_UNUSED_TAG;
+	}
+}
+
+#define MAX_NR_TAG 128
+static int __ublk_batch_dispatch(struct ublk_queue *ubq,
+				 const struct ublk_batch_io_data *data,
+				 struct ublk_batch_fcmd *fcmd)
+{
+	unsigned short tag_buf[MAX_NR_TAG];
+	struct io_br_sel sel;
+	size_t len = 0;
+	int ret;
+
+	sel = io_uring_cmd_buffer_select(fcmd->cmd, fcmd->buf_group, &len,
+					 data->issue_flags);
+	if (sel.val < 0)
+		return sel.val;
+	if (!sel.addr)
+		return -ENOBUFS;
+
+	/* single reader needn't lock and sizeof(kfifo element) is 2 bytes */
+	len = min(len, sizeof(tag_buf)) / 2;
+	len = kfifo_out(&ubq->evts_fifo, tag_buf, len);
+
+	ublk_batch_prep_dispatch(ubq, data, tag_buf, len);
+
+	sel.val = ublk_batch_copy_io_tags(fcmd, sel.addr, tag_buf, len * 2);
+	ret = ublk_batch_fetch_post_cqe(fcmd, &sel, data->issue_flags);
+	if (unlikely(ret < 0)) {
+		int res = kfifo_in_spinlocked_noirqsave(&ubq->evts_fifo,
+			tag_buf, len, &ubq->evts_lock);
+
+		pr_warn("%s: copy tags or post CQE failure, move back "
+				"tags(%d %zu) ret %d\n", __func__, res, len,
+				ret);
+	}
+	return ret;
+}
+
+static __maybe_unused int
+ublk_batch_dispatch(struct ublk_queue *ubq,
+		    const struct ublk_batch_io_data *data,
+		    struct ublk_batch_fcmd *fcmd)
+{
+	int ret = 0;
+
+	while (!ublk_io_evts_empty(ubq)) {
+		ret = __ublk_batch_dispatch(ubq, data, fcmd);
+		if (ret <= 0)
+			break;
+	}
+
+	if (ret < 0)
+		ublk_batch_deinit_fetch_buf(data, fcmd, ret);
+
+	return ret;
+}
+
 static void ublk_cmd_tw_cb(struct io_uring_cmd *cmd,
 			   unsigned int issue_flags)
 {
include/uapi/linux/ublk_cmd.h +6 -0
index 295ec8f34173..a56a50bbb00a 100644
@@ -553,6 +553,12 @@ struct ublk_elem_header {
 	__s32 result;	/* I/O completion result (commit only) */
 };
 
+/*
+ * If this tag value is observed from buffer of `UBLK_U_IO_FETCH_IO_CMDS`
+ * ublk server can simply ignore it
+ */
+#define UBLK_BATCH_IO_UNUSED_TAG 	(__u16)(-1)
+
 /*
  * uring_cmd buffer structure for batch commands
  *
Signature · 1 line
2.47.0
author
#35 lore raw

PATCH v3 14/27 ublk: add UBLK_U_IO_FETCH_IO_CMDS for batch I/O processing

In reply to Ming Lei

Add UBLK_U_IO_FETCH_IO_CMDS command to enable efficient batch processing of I/O requests. This multishot uring_cmd allows the ublk server to fetch multiple I/O commands in a single operation, significantly reducing submission overhead compared to individual FETCH_REQ* commands.

Key Design Features:

1. Multishot Operation: One UBLK_U_IO_FETCH_IO_CMDS can fetch many I/O commands, with the batch size limited by the provided buffer length.

2. Dynamic Load Balancing: Multiple fetch commands can be submitted simultaneously, but only one is active at any time. This enables efficient load distribution across multiple server task contexts.

3. Implicit State Management: The implementation uses three key variables to track state:
- evts_fifo: Queue of request tags awaiting processing
- fcmd_head: List of available fetch commands
- active_fcmd: Currently active fetch command (NULL = none active)

States are derived implicitly:
- IDLE: No fetch commands available
- READY: Fetch commands available, none active
- ACTIVE: One fetch command processing events

4. Lockless Reader Optimization: The active fetch command can read from evts_fifo without locking (single reader guarantee), while writers (ublk_queue_rq/ublk_queue_rqs) use evts_lock protection. The memory barrier pairing plays key role for the single lockless reader optimization.

Implementation Details:

- ublk_queue_rq() and ublk_queue_rqs() save request tags to evts_fifo
- __ublk_pick_active_fcmd() selects an available fetch command when events arrive and no command is currently active
- ublk_batch_dispatch() moves tags from evts_fifo to the fetch command's buffer and posts completion via io_uring_mshot_cmd_post_cqe()
- State transitions are coordinated via evts_lock to maintain consistency

---
 drivers/block/ublk_drv.c      | 412 +++++++++++++++++++++++++++++++---
 include/uapi/linux/ublk_cmd.h |   7 +
 2 files changed, 388 insertions(+), 31 deletions(-)
drivers/block/ublk_drv.c +381 -31
index c9183ba99792..531a1754a0a4 100644
@@ -93,6 +93,7 @@
 
 /* ublk batch fetch uring_cmd */
 struct ublk_batch_fcmd {
+	struct list_head node;
 	struct io_uring_cmd *cmd;
 	unsigned short buf_group;
 };
@@ -117,7 +118,10 @@ struct ublk_uring_cmd_pdu {
 	 */
 	struct ublk_queue *ubq;
 
-	u16 tag;
+	union {
+		u16 tag;
+		struct ublk_batch_fcmd *fcmd; /* batch io only */
+	};
 };
 
 struct ublk_batch_io_data {
@@ -226,18 +230,36 @@ struct ublk_queue {
 	struct ublk_device *dev;
 
 	/*
-	 * Inflight ublk request tag is saved in this fifo
+	 * Batch I/O State Management:
+	 *
+	 * The batch I/O system uses implicit state management based on the
+	 * combination of three key variables below.
+	 *
+	 * - IDLE: list_empty(&fcmd_head) && !active_fcmd
+	 *   No fetch commands available, events queue in evts_fifo
+	 *
+	 * - READY: !list_empty(&fcmd_head) && !active_fcmd
+	 *   Fetch commands available but none processing events
 	 *
-	 * There are multiple writer from ublk_queue_rq() or ublk_queue_rqs(),
-	 * so lock is required for storing request tag to fifo
+	 * - ACTIVE: active_fcmd
+	 *   One fetch command actively processing events from evts_fifo
 	 *
-	 * Make sure just one reader for fetching request from task work
-	 * function to ublk server, so no need to grab the lock in reader
-	 * side.
+	 * Key Invariants:
+	 * - At most one active_fcmd at any time (single reader)
+	 * - active_fcmd is always from fcmd_head list when non-NULL
+	 * - evts_fifo can be read locklessly by the single active reader
+	 * - All state transitions require evts_lock protection
+	 * - Multiple writers to evts_fifo require lock protection
 	 */
 	struct {
 		DECLARE_KFIFO_PTR(evts_fifo, unsigned short);
 		spinlock_t evts_lock;
+
+		/* List of fetch commands available to process events */
+		struct list_head fcmd_head;
+
+		/* Currently active fetch command (NULL = none active) */
+		struct ublk_batch_fcmd  *active_fcmd;
 	}____cacheline_aligned_in_smp;
 
 	struct ublk_io ios[] __counted_by(q_depth);
@@ -289,12 +311,20 @@ static void ublk_abort_queue(struct ublk_device *ub, struct ublk_queue *ubq);
 static inline struct request *__ublk_check_and_get_req(struct ublk_device *ub,
 		u16 q_id, u16 tag, struct ublk_io *io, size_t offset);
 static inline unsigned int ublk_req_build_flags(struct request *req);
+static void ublk_batch_dispatch(struct ublk_queue *ubq,
+				struct ublk_batch_io_data *data,
+				struct ublk_batch_fcmd *fcmd);
 
 static inline bool ublk_dev_support_batch_io(const struct ublk_device *ub)
 {
 	return false;
 }
 
+static inline bool ublk_support_batch_io(const struct ublk_queue *ubq)
+{
+	return false;
+}
+
 static inline void ublk_io_lock(struct ublk_io *io)
 {
 	spin_lock(&io->lock);
@@ -621,13 +651,45 @@ static wait_queue_head_t ublk_idr_wq;	/* wait until one idr is freed */
 
 static DEFINE_MUTEX(ublk_ctl_mutex);
 
+static struct ublk_batch_fcmd *
+ublk_batch_alloc_fcmd(struct io_uring_cmd *cmd)
+{
+	struct ublk_batch_fcmd *fcmd = kzalloc(sizeof(*fcmd), GFP_NOIO);
+
+	if (fcmd) {
+		fcmd->cmd = cmd;
+		fcmd->buf_group = READ_ONCE(cmd->sqe->buf_index);
+	}
+	return fcmd;
+}
+
+static void ublk_batch_free_fcmd(struct ublk_batch_fcmd *fcmd)
+{
+	kfree(fcmd);
+}
+
+static void __ublk_release_fcmd(struct ublk_queue *ubq)
+{
+	WRITE_ONCE(ubq->active_fcmd, NULL);
+}
 
-static void ublk_batch_deinit_fetch_buf(const struct ublk_batch_io_data *data,
+/*
+ * Nothing can move on, so clear ->active_fcmd, and the caller should stop
+ * dispatching
+ */
+static void ublk_batch_deinit_fetch_buf(struct ublk_queue *ubq,
+					const struct ublk_batch_io_data *data,
 					struct ublk_batch_fcmd *fcmd,
 					int res)
 {
+	spin_lock(&ubq->evts_lock);
+	list_del(&fcmd->node);
+	WARN_ON_ONCE(fcmd != ubq->active_fcmd);
+	__ublk_release_fcmd(ubq);
+	spin_unlock(&ubq->evts_lock);
+
 	io_uring_cmd_done(fcmd->cmd, res, data->issue_flags);
-	fcmd->cmd = NULL;
+	ublk_batch_free_fcmd(fcmd);
 }
 
 static int ublk_batch_fetch_post_cqe(struct ublk_batch_fcmd *fcmd,
@@ -1459,6 +1521,8 @@ static int __ublk_batch_dispatch(struct ublk_queue *ubq,
 	size_t len = 0;
 	int ret;
 
+	WARN_ON_ONCE(data->cmd != fcmd->cmd);
+
 	sel = io_uring_cmd_buffer_select(fcmd->cmd, fcmd->buf_group, &len,
 					 data->issue_flags);
 	if (sel.val < 0)
@@ -1485,23 +1549,94 @@ static int __ublk_batch_dispatch(struct ublk_queue *ubq,
 	return ret;
 }
 
-static __maybe_unused int
-ublk_batch_dispatch(struct ublk_queue *ubq,
-		    const struct ublk_batch_io_data *data,
-		    struct ublk_batch_fcmd *fcmd)
+static struct ublk_batch_fcmd *__ublk_acquire_fcmd(
+		struct ublk_queue *ubq)
+{
+	struct ublk_batch_fcmd *fcmd;
+
+	lockdep_assert_held(&ubq->evts_lock);
+
+	/*
+	 * Ordering updating ubq->evts_fifo and checking ubq->active_fcmd.
+	 *
+	 * The pair is the smp_mb() in ublk_batch_dispatch().
+	 *
+	 * If ubq->active_fcmd is observed as non-NULL, the new added tags
+	 * can be visisible in ublk_batch_dispatch() with the barrier pairing.
+	 */
+	smp_mb();
+	if (READ_ONCE(ubq->active_fcmd)) {
+		fcmd = NULL;
+	} else {
+		fcmd = list_first_entry_or_null(&ubq->fcmd_head,
+				struct ublk_batch_fcmd, node);
+		WRITE_ONCE(ubq->active_fcmd, fcmd);
+	}
+	return fcmd;
+}
+
+static void ublk_batch_tw_cb(struct io_uring_cmd *cmd,
+			   unsigned int issue_flags)
+{
+	struct ublk_uring_cmd_pdu *pdu = ublk_get_uring_cmd_pdu(cmd);
+	struct ublk_batch_fcmd *fcmd = pdu->fcmd;
+	struct ublk_batch_io_data data = {
+		.ub = pdu->ubq->dev,
+		.cmd = fcmd->cmd,
+		.issue_flags = issue_flags,
+	};
+
+	WARN_ON_ONCE(pdu->ubq->active_fcmd != fcmd);
+
+	ublk_batch_dispatch(pdu->ubq, &data, fcmd);
+}
+
+static void ublk_batch_dispatch(struct ublk_queue *ubq,
+				struct ublk_batch_io_data *data,
+				struct ublk_batch_fcmd *fcmd)
 {
+	struct ublk_batch_fcmd *new_fcmd;
+	void *handle;
+	bool empty;
 	int ret = 0;
 
+again:
 	while (!ublk_io_evts_empty(ubq)) {
 		ret = __ublk_batch_dispatch(ubq, data, fcmd);
 		if (ret <= 0)
 			break;
 	}
 
-	if (ret < 0)
-		ublk_batch_deinit_fetch_buf(data, fcmd, ret);
+	if (ret < 0) {
+		ublk_batch_deinit_fetch_buf(ubq, data, fcmd, ret);
+		return;
+	}
 
-	return ret;
+	handle = io_uring_cmd_ctx_handle(fcmd->cmd);
+	__ublk_release_fcmd(ubq);
+	/*
+	 * Order clearing ubq->active_fcmd from __ublk_release_fcmd() and
+	 * checking ubq->evts_fifo.
+	 *
+	 * The pair is the smp_mb() in __ublk_acquire_fcmd().
+	 */
+	smp_mb();
+	empty = ublk_io_evts_empty(ubq);
+	if (likely(empty))
+		return;
+
+	spin_lock(&ubq->evts_lock);
+	new_fcmd = __ublk_acquire_fcmd(ubq);
+	spin_unlock(&ubq->evts_lock);
+
+	if (!new_fcmd)
+		return;
+	if (handle == io_uring_cmd_ctx_handle(new_fcmd->cmd)) {
+		data->cmd = new_fcmd->cmd;
+		fcmd = new_fcmd;
+		goto again;
+	}
+	io_uring_cmd_complete_in_task(new_fcmd->cmd, ublk_batch_tw_cb);
 }
 
 static void ublk_cmd_tw_cb(struct io_uring_cmd *cmd,
@@ -1513,13 +1648,27 @@ static void ublk_cmd_tw_cb(struct io_uring_cmd *cmd,
 	ublk_dispatch_req(ubq, pdu->req, issue_flags);
 }
 
-static void ublk_queue_cmd(struct ublk_queue *ubq, struct request *rq)
+static void ublk_queue_cmd(struct ublk_queue *ubq, struct request *rq, bool last)
 {
-	struct io_uring_cmd *cmd = ubq->ios[rq->tag].cmd;
-	struct ublk_uring_cmd_pdu *pdu = ublk_get_uring_cmd_pdu(cmd);
+	if (ublk_support_batch_io(ubq)) {
+		unsigned short tag = rq->tag;
+		struct ublk_batch_fcmd *fcmd = NULL;
 
-	pdu->req = rq;
-	io_uring_cmd_complete_in_task(cmd, ublk_cmd_tw_cb);
+		spin_lock(&ubq->evts_lock);
+		kfifo_put(&ubq->evts_fifo, tag);
+		if (last)
+			fcmd = __ublk_acquire_fcmd(ubq);
+		spin_unlock(&ubq->evts_lock);
+
+		if (fcmd)
+			io_uring_cmd_complete_in_task(fcmd->cmd, ublk_batch_tw_cb);
+	} else {
+		struct io_uring_cmd *cmd = ubq->ios[rq->tag].cmd;
+		struct ublk_uring_cmd_pdu *pdu = ublk_get_uring_cmd_pdu(cmd);
+
+		pdu->req = rq;
+		io_uring_cmd_complete_in_task(cmd, ublk_cmd_tw_cb);
+	}
 }
 
 static void ublk_cmd_list_tw_cb(struct io_uring_cmd *cmd,
@@ -1537,14 +1686,44 @@ static void ublk_cmd_list_tw_cb(struct io_uring_cmd *cmd,
 	} while (rq);
 }
 
-static void ublk_queue_cmd_list(struct ublk_io *io, struct rq_list *l)
+static void ublk_batch_queue_cmd_list(struct ublk_queue *ubq, struct rq_list *l)
 {
-	struct io_uring_cmd *cmd = io->cmd;
-	struct ublk_uring_cmd_pdu *pdu = ublk_get_uring_cmd_pdu(cmd);
+	unsigned short tags[MAX_NR_TAG];
+	struct ublk_batch_fcmd *fcmd;
+	struct request *rq;
+	unsigned cnt = 0;
+
+	spin_lock(&ubq->evts_lock);
+	rq_list_for_each(l, rq) {
+		tags[cnt++] = (unsigned short)rq->tag;
+		if (cnt >= MAX_NR_TAG) {
+			kfifo_in(&ubq->evts_fifo, tags, cnt);
+			cnt = 0;
+		}
+	}
+	if (cnt)
+		kfifo_in(&ubq->evts_fifo, tags, cnt);
+	fcmd = __ublk_acquire_fcmd(ubq);
+	spin_unlock(&ubq->evts_lock);
 
-	pdu->req_list = rq_list_peek(l);
 	rq_list_init(l);
-	io_uring_cmd_complete_in_task(cmd, ublk_cmd_list_tw_cb);
+	if (fcmd)
+		io_uring_cmd_complete_in_task(fcmd->cmd, ublk_batch_tw_cb);
+}
+
+static void ublk_queue_cmd_list(struct ublk_queue *ubq, struct ublk_io *io,
+				struct rq_list *l, bool batch)
+{
+	if (batch) {
+		ublk_batch_queue_cmd_list(ubq, l);
+	} else {
+		struct io_uring_cmd *cmd = io->cmd;
+		struct ublk_uring_cmd_pdu *pdu = ublk_get_uring_cmd_pdu(cmd);
+
+		pdu->req_list = rq_list_peek(l);
+		rq_list_init(l);
+		io_uring_cmd_complete_in_task(cmd, ublk_cmd_list_tw_cb);
+	}
 }
 
 static enum blk_eh_timer_return ublk_timeout(struct request *rq)
@@ -1623,7 +1802,7 @@ static blk_status_t ublk_queue_rq(struct blk_mq_hw_ctx *hctx,
 		return BLK_STS_OK;
 	}
 
-	ublk_queue_cmd(ubq, rq);
+	ublk_queue_cmd(ubq, rq, bd->last);
 	return BLK_STS_OK;
 }
 
@@ -1635,11 +1814,25 @@ static inline bool ublk_belong_to_same_batch(const struct ublk_io *io,
 		(io->task == io2->task);
 }
 
-static void ublk_queue_rqs(struct rq_list *rqlist)
+static void ublk_commit_rqs(struct blk_mq_hw_ctx *hctx)
+{
+	struct ublk_queue *ubq = hctx->driver_data;
+	struct ublk_batch_fcmd *fcmd;
+
+	spin_lock(&ubq->evts_lock);
+	fcmd = __ublk_acquire_fcmd(ubq);
+	spin_unlock(&ubq->evts_lock);
+
+	if (fcmd)
+		io_uring_cmd_complete_in_task(fcmd->cmd, ublk_batch_tw_cb);
+}
+
+static void __ublk_queue_rqs(struct rq_list *rqlist, bool batch)
 {
 	struct rq_list requeue_list = { };
 	struct rq_list submit_list = { };
 	struct ublk_io *io = NULL;
+	struct ublk_queue *ubq = NULL;
 	struct request *req;
 
 	while ((req = rq_list_pop(rqlist))) {
@@ -1653,16 +1846,27 @@ static void ublk_queue_rqs(struct rq_list *rqlist)
 
 		if (io && !ublk_belong_to_same_batch(io, this_io) &&
 				!rq_list_empty(&submit_list))
-			ublk_queue_cmd_list(io, &submit_list);
+			ublk_queue_cmd_list(ubq, io, &submit_list, batch);
 		io = this_io;
+		ubq = this_q;
 		rq_list_add_tail(&submit_list, req);
 	}
 
 	if (!rq_list_empty(&submit_list))
-		ublk_queue_cmd_list(io, &submit_list);
+		ublk_queue_cmd_list(ubq, io, &submit_list, batch);
 	*rqlist = requeue_list;
 }
 
+static void ublk_queue_rqs(struct rq_list *rqlist)
+{
+	__ublk_queue_rqs(rqlist, false);
+}
+
+static void ublk_batch_queue_rqs(struct rq_list *rqlist)
+{
+	__ublk_queue_rqs(rqlist, true);
+}
+
 static int ublk_init_hctx(struct blk_mq_hw_ctx *hctx, void *driver_data,
 		unsigned int hctx_idx)
 {
@@ -1680,6 +1884,14 @@ static const struct blk_mq_ops ublk_mq_ops = {
 	.timeout	= ublk_timeout,
 };
 
+static const struct blk_mq_ops ublk_batch_mq_ops = {
+	.commit_rqs	= ublk_commit_rqs,
+	.queue_rq       = ublk_queue_rq,
+	.queue_rqs      = ublk_batch_queue_rqs,
+	.init_hctx	= ublk_init_hctx,
+	.timeout	= ublk_timeout,
+};
+
 static void ublk_queue_reinit(struct ublk_device *ub, struct ublk_queue *ubq)
 {
 	int i;
@@ -2057,6 +2269,56 @@ static void ublk_cancel_cmd(struct ublk_queue *ubq, unsigned tag,
 		io_uring_cmd_done(io->cmd, UBLK_IO_RES_ABORT, issue_flags);
 }
 
+static void ublk_batch_cancel_cmd(struct ublk_queue *ubq,
+				  struct ublk_batch_fcmd *fcmd,
+				  unsigned int issue_flags)
+{
+	bool done;
+
+	spin_lock(&ubq->evts_lock);
+	done = (ubq->active_fcmd != fcmd);
+	if (done)
+		list_del(&fcmd->node);
+	spin_unlock(&ubq->evts_lock);
+
+	if (done) {
+		io_uring_cmd_done(fcmd->cmd, UBLK_IO_RES_ABORT, issue_flags);
+		ublk_batch_free_fcmd(fcmd);
+	}
+}
+
+static void ublk_batch_cancel_queue(struct ublk_queue *ubq)
+{
+	LIST_HEAD(fcmd_list);
+
+	spin_lock(&ubq->evts_lock);
+	ubq->force_abort = true;
+	list_splice_init(&ubq->fcmd_head, &fcmd_list);
+	if (ubq->active_fcmd)
+		list_move(&ubq->active_fcmd->node, &ubq->fcmd_head);
+	spin_unlock(&ubq->evts_lock);
+
+	while (!list_empty(&fcmd_list)) {
+		struct ublk_batch_fcmd *fcmd = list_first_entry(&fcmd_list,
+				struct ublk_batch_fcmd, node);
+
+		ublk_batch_cancel_cmd(ubq, fcmd, IO_URING_F_UNLOCKED);
+	}
+}
+
+static void ublk_batch_cancel_fn(struct io_uring_cmd *cmd,
+				 unsigned int issue_flags)
+{
+	struct ublk_uring_cmd_pdu *pdu = ublk_get_uring_cmd_pdu(cmd);
+	struct ublk_batch_fcmd *fcmd = pdu->fcmd;
+	struct ublk_queue *ubq = pdu->ubq;
+
+	if (!ubq->canceling)
+		ublk_start_cancel(ubq->dev);
+
+	ublk_batch_cancel_cmd(ubq, fcmd, issue_flags);
+}
+
 /*
  * The ublk char device won't be closed when calling cancel fn, so both
  * ublk device and queue are guaranteed to be live
@@ -2108,6 +2370,11 @@ static void ublk_cancel_queue(struct ublk_queue *ubq)
 {
 	int i;
 
+	if (ublk_support_batch_io(ubq)) {
+		ublk_batch_cancel_queue(ubq);
+		return;
+	}
+
 	for (i = 0; i < ubq->q_depth; i++)
 		ublk_cancel_cmd(ubq, i, IO_URING_F_UNLOCKED);
 }
@@ -3066,6 +3333,74 @@ static int ublk_check_batch_cmd(const struct ublk_batch_io_data *data)
 	return ublk_check_batch_cmd_flags(uc);
 }
 
+static int ublk_batch_attach(struct ublk_queue *ubq,
+			     struct ublk_batch_io_data *data,
+			     struct ublk_batch_fcmd *fcmd)
+{
+	struct ublk_batch_fcmd *new_fcmd = NULL;
+	bool free = false;
+
+	spin_lock(&ubq->evts_lock);
+	if (unlikely(ubq->force_abort || ubq->canceling)) {
+		free = true;
+	} else {
+		list_add_tail(&fcmd->node, &ubq->fcmd_head);
+		new_fcmd = __ublk_acquire_fcmd(ubq);
+	}
+	spin_unlock(&ubq->evts_lock);
+
+	/*
+	 * If the two fetch commands are originated from same io_ring_ctx,
+	 * run batch dispatch directly. Otherwise, schedule task work for
+	 * doing it.
+	 */
+	if (new_fcmd && io_uring_cmd_ctx_handle(new_fcmd->cmd) ==
+			io_uring_cmd_ctx_handle(fcmd->cmd)) {
+		data->cmd = new_fcmd->cmd;
+		ublk_batch_dispatch(ubq, data, new_fcmd);
+	} else if (new_fcmd) {
+		io_uring_cmd_complete_in_task(new_fcmd->cmd,
+				ublk_batch_tw_cb);
+	}
+
+	if (free) {
+		ublk_batch_free_fcmd(fcmd);
+		return -ENODEV;
+	}
+	return -EIOCBQUEUED;
+}
+
+static int ublk_handle_batch_fetch_cmd(struct ublk_batch_io_data *data)
+{
+	struct ublk_queue *ubq = ublk_get_queue(data->ub, data->header.q_id);
+	struct ublk_batch_fcmd *fcmd = ublk_batch_alloc_fcmd(data->cmd);
+	struct ublk_uring_cmd_pdu *pdu = ublk_get_uring_cmd_pdu(data->cmd);
+
+	if (!fcmd)
+		return -ENOMEM;
+
+	pdu->ubq = ubq;
+	pdu->fcmd = fcmd;
+	io_uring_cmd_mark_cancelable(data->cmd, data->issue_flags);
+
+	return ublk_batch_attach(ubq, data, fcmd);
+}
+
+static int ublk_validate_batch_fetch_cmd(struct ublk_batch_io_data *data,
+					 const struct ublk_batch_io *uc)
+{
+	if (!(data->cmd->flags & IORING_URING_CMD_MULTISHOT))
+		return -EINVAL;
+
+	if (uc->elem_bytes != sizeof(__u16))
+		return -EINVAL;
+
+	if (uc->flags != 0)
+		return -E2BIG;
+
+	return 0;
+}
+
 static int ublk_ch_batch_io_uring_cmd(struct io_uring_cmd *cmd,
 				       unsigned int issue_flags)
 {
@@ -3088,6 +3423,11 @@ static int ublk_ch_batch_io_uring_cmd(struct io_uring_cmd *cmd,
 	if (data.header.q_id >= ub->dev_info.nr_hw_queues)
 		goto out;
 
+	if (unlikely(issue_flags & IO_URING_F_CANCEL)) {
+		ublk_batch_cancel_fn(cmd, issue_flags);
+		return 0;
+	}
+
 	switch (cmd_op) {
 	case UBLK_U_IO_PREP_IO_CMDS:
 		ret = ublk_check_batch_cmd(&data);
@@ -3101,6 +3441,12 @@ static int ublk_ch_batch_io_uring_cmd(struct io_uring_cmd *cmd,
 			goto out;
 		ret = ublk_handle_batch_commit_cmd(&data);
 		break;
+	case UBLK_U_IO_FETCH_IO_CMDS:
+		ret = ublk_validate_batch_fetch_cmd(&data, uc);
+		if (ret)
+			goto out;
+		ret = ublk_handle_batch_fetch_cmd(&data);
+		break;
 	default:
 		ret = -EOPNOTSUPP;
 	}
@@ -3302,6 +3648,7 @@ static int ublk_init_queue(struct ublk_device *ub, int q_id)
 		ret = ublk_io_evts_init(ubq, ubq->q_depth, numa_node);
 		if (ret)
 			goto fail;
+		INIT_LIST_HEAD(&ubq->fcmd_head);
 	}
 	ub->queues[q_id] = ubq;
 	ubq->dev = ub;
@@ -3426,7 +3773,10 @@ static void ublk_align_max_io_size(struct ublk_device *ub)
 
 static int ublk_add_tag_set(struct ublk_device *ub)
 {
-	ub->tag_set.ops = &ublk_mq_ops;
+	if (ublk_dev_support_batch_io(ub))
+		ub->tag_set.ops = &ublk_batch_mq_ops;
+	else
+		ub->tag_set.ops = &ublk_mq_ops;
 	ub->tag_set.nr_hw_queues = ub->dev_info.nr_hw_queues;
 	ub->tag_set.queue_depth = ub->dev_info.queue_depth;
 	ub->tag_set.numa_node = NUMA_NO_NODE;
include/uapi/linux/ublk_cmd.h +7 -0
index a56a50bbb00a..8a06eebdedaf 100644
@@ -120,6 +120,13 @@
 #define	UBLK_U_IO_COMMIT_IO_CMDS	\
 	_IOWR('u', 0x26, struct ublk_batch_io)
 
+/*
+ * Fetch io commands to provided buffer in multishot style,
+ * `IORING_URING_CMD_MULTISHOT` is required for this command.
+ */
+#define	UBLK_U_IO_FETCH_IO_CMDS 	\
+	_IOWR('u', 0x27, struct ublk_batch_io)
+
 /* only ABORT means that no re-fetch */
 #define UBLK_IO_RES_OK			0
 #define UBLK_IO_RES_NEED_GET_DATA	1
Signature · 1 line
2.47.0
author
#36 lore raw

PATCH v3 15/27 ublk: abort requests filled in event kfifo

In reply to Ming Lei

In case of BATCH_IO, any request filled in event kfifo, they don't get chance to be dispatched any more when releasing ublk char device, so we have to abort them too.

Add ublk_abort_batch_queue() for aborting this kind of requests.

---
 drivers/block/ublk_drv.c | 26 +++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)
drivers/block/ublk_drv.c +25 -1
index 531a1754a0a4..c21ed4811767 100644
@@ -2178,7 +2178,8 @@ static int ublk_ch_mmap(struct file *filp, struct vm_area_struct *vma)
 static void __ublk_fail_req(struct ublk_device *ub, struct ublk_io *io,
 		struct request *req)
 {
-	WARN_ON_ONCE(io->flags & UBLK_IO_FLAG_ACTIVE);
+	WARN_ON_ONCE(!ublk_dev_support_batch_io(ub) &&
+			io->flags & UBLK_IO_FLAG_ACTIVE);
 
 	if (ublk_nosrv_should_reissue_outstanding(ub))
 		blk_mq_requeue_request(req, false);
@@ -2188,6 +2189,26 @@ static void __ublk_fail_req(struct ublk_device *ub, struct ublk_io *io,
 	}
 }
 
+/*
+ * Request tag may just be filled to event kfifo, not get chance to
+ * dispatch, abort these requests too
+ */
+static void ublk_abort_batch_queue(struct ublk_device *ub,
+				   struct ublk_queue *ubq)
+{
+	while (true) {
+		struct request *req;
+		short tag;
+
+		if (!kfifo_out(&ubq->evts_fifo, &tag, 1))
+			break;
+
+		req = blk_mq_tag_to_rq(ub->tag_set.tags[ubq->q_id], tag);
+		if (req && blk_mq_request_started(req))
+			__ublk_fail_req(ub, &ubq->ios[tag], req);
+	}
+}
+
 /*
  * Called from ublk char device release handler, when any uring_cmd is
  * done, meantime request queue is "quiesced" since all inflight requests
@@ -2206,6 +2227,9 @@ static void ublk_abort_queue(struct ublk_device *ub, struct ublk_queue *ubq)
 		if (io->flags & UBLK_IO_FLAG_OWNED_BY_SRV)
 			__ublk_fail_req(ub, io, io->req);
 	}
+
+	if (ublk_support_batch_io(ubq))
+		ublk_abort_batch_queue(ub, ubq);
 }
 
 static void ublk_start_cancel(struct ublk_device *ub)
Signature · 1 line
2.47.0
author
#37 lore raw

PATCH v3 16/27 ublk: add new feature UBLK_F_BATCH_IO

In reply to Ming Lei

Add new feature UBLK_F_BATCH_IO which replaces the following two per-io commands:

- UBLK_U_IO_FETCH_REQ

- UBLK_U_IO_COMMIT_AND_FETCH_REQ

with three per-queue batch io uring_cmd:

- UBLK_U_IO_PREP_IO_CMDS

- UBLK_U_IO_COMMIT_IO_CMDS

- UBLK_U_IO_FETCH_IO_CMDS

Then ublk can deliver batch io commands to ublk server in single multishort uring_cmd, also allows to prepare & commit multiple commands in batch style via single uring_cmd, communication cost is reduced a lot.

This feature also doesn't limit task context any more for all supported commands, so any allowed uring_cmd can be issued in any task context. ublk server implementation becomes much easier.

Meantime load balance becomes much easier to support with this feature. The command UBLK_U_IO_FETCH_IO_CMDS can be issued from multiple task contexts, so each task can adjust this command's buffer length or number of inflight commands for controlling how much load is handled by current task.

Later, priority parameter will be added to command UBLK_U_IO_FETCH_IO_CMDS for improving load balance support.

UBLK_U_IO_GET_DATA isn't supported in batch io yet, but it may be enabled in future via its batch pair.

---
 drivers/block/ublk_drv.c      | 58 ++++++++++++++++++++++++++++++++---
 include/uapi/linux/ublk_cmd.h | 16 ++++++++++
 2 files changed, 69 insertions(+), 5 deletions(-)
drivers/block/ublk_drv.c +53 -5
index c21ed4811767..a2bf5a10f446 100644
@@ -74,7 +74,8 @@
 		| UBLK_F_AUTO_BUF_REG \
 		| UBLK_F_QUIESCE \
 		| UBLK_F_PER_IO_DAEMON \
-		| UBLK_F_BUF_REG_OFF_DAEMON)
+		| UBLK_F_BUF_REG_OFF_DAEMON \
+		| UBLK_F_BATCH_IO)
 
 #define UBLK_F_ALL_RECOVERY_FLAGS (UBLK_F_USER_RECOVERY \
 		| UBLK_F_USER_RECOVERY_REISSUE \
@@ -317,12 +318,12 @@ static void ublk_batch_dispatch(struct ublk_queue *ubq,
 
 static inline bool ublk_dev_support_batch_io(const struct ublk_device *ub)
 {
-	return false;
+	return ub->dev_info.flags & UBLK_F_BATCH_IO;
 }
 
 static inline bool ublk_support_batch_io(const struct ublk_queue *ubq)
 {
-	return false;
+	return ubq->flags & UBLK_F_BATCH_IO;
 }
 
 static inline void ublk_io_lock(struct ublk_io *io)
@@ -3425,6 +3426,41 @@ static int ublk_validate_batch_fetch_cmd(struct ublk_batch_io_data *data,
 	return 0;
 }
 
+static int ublk_handle_non_batch_cmd(struct io_uring_cmd *cmd,
+				     unsigned int issue_flags)
+{
+	const struct ublksrv_io_cmd *ub_cmd = io_uring_sqe_cmd(cmd->sqe);
+	struct ublk_device *ub = cmd->file->private_data;
+	unsigned tag = READ_ONCE(ub_cmd->tag);
+	unsigned q_id = READ_ONCE(ub_cmd->q_id);
+	unsigned index = READ_ONCE(ub_cmd->addr);
+	struct ublk_queue *ubq;
+	struct ublk_io *io;
+	int ret = -EINVAL;
+
+	if (!ub)
+		return ret;
+
+	if (q_id >= ub->dev_info.nr_hw_queues)
+		return ret;
+
+	ubq = ublk_get_queue(ub, q_id);
+	if (tag >= ubq->q_depth)
+		return ret;
+
+	io = &ubq->ios[tag];
+
+	switch (cmd->cmd_op) {
+	case UBLK_U_IO_REGISTER_IO_BUF:
+		return ublk_register_io_buf(cmd, ub, q_id, tag, io, index,
+				issue_flags);
+	case UBLK_U_IO_UNREGISTER_IO_BUF:
+		return ublk_unregister_io_buf(cmd, ub, index, issue_flags);
+	default:
+		return -EOPNOTSUPP;
+	}
+}
+
 static int ublk_ch_batch_io_uring_cmd(struct io_uring_cmd *cmd,
 				       unsigned int issue_flags)
 {
@@ -3472,7 +3508,8 @@ static int ublk_ch_batch_io_uring_cmd(struct io_uring_cmd *cmd,
 		ret = ublk_handle_batch_fetch_cmd(&data);
 		break;
 	default:
-		ret = -EOPNOTSUPP;
+		ret = ublk_handle_non_batch_cmd(cmd, issue_flags);
+		break;
 	}
 out:
 	return ret;
@@ -4138,9 +4175,13 @@ static int ublk_ctrl_add_dev(const struct ublksrv_ctrl_cmd *header)
 
 	ub->dev_info.flags |= UBLK_F_CMD_IOCTL_ENCODE |
 		UBLK_F_URING_CMD_COMP_IN_TASK |
-		UBLK_F_PER_IO_DAEMON |
+		(ublk_dev_support_batch_io(ub) ? 0 : UBLK_F_PER_IO_DAEMON) |
 		UBLK_F_BUF_REG_OFF_DAEMON;
 
+	/* So far, UBLK_F_PER_IO_DAEMON won't be exposed for BATCH_IO */
+	if (ublk_dev_support_batch_io(ub))
+		ub->dev_info.flags &= ~UBLK_F_PER_IO_DAEMON;
+
 	/* GET_DATA isn't needed any more with USER_COPY or ZERO COPY */
 	if (ub->dev_info.flags & (UBLK_F_USER_COPY | UBLK_F_SUPPORT_ZERO_COPY |
 				UBLK_F_AUTO_BUF_REG))
@@ -4493,6 +4534,13 @@ static int ublk_wait_for_idle_io(struct ublk_device *ub,
 	unsigned int elapsed = 0;
 	int ret;
 
+	/*
+	 * For UBLK_F_BATCH_IO ublk server can get notified with existing
+	 * or new fetch command, so needn't wait any more
+	 */
+	if (ublk_dev_support_batch_io(ub))
+		return 0;
+
 	while (elapsed < timeout_ms && !signal_pending(current)) {
 		unsigned int queues_cancelable = 0;
 		int i;
include/uapi/linux/ublk_cmd.h +16 -0
index 8a06eebdedaf..650886f35927 100644
@@ -335,6 +335,22 @@
  */
 #define UBLK_F_BUF_REG_OFF_DAEMON (1ULL << 14)
 
+
+/*
+ * Support the following commands for delivering & committing io command
+ * in batch.
+ *
+ * 	- UBLK_U_IO_PREP_IO_CMDS
+ * 	- UBLK_U_IO_COMMIT_IO_CMDS
+ * 	- UBLK_U_IO_FETCH_IO_CMDS
+ * 	- UBLK_U_IO_REGISTER_IO_BUF
+ * 	- UBLK_U_IO_UNREGISTER_IO_BUF
+ *
+ * The existing UBLK_U_IO_FETCH_REQ, UBLK_U_IO_COMMIT_AND_FETCH_REQ and
+ * UBLK_U_IO_GET_DATA uring_cmd are not supported for this feature.
+ */
+#define UBLK_F_BATCH_IO		(1ULL << 15)
+
 /* device state */
 #define UBLK_S_DEV_DEAD	0
 #define UBLK_S_DEV_LIVE	1
Signature · 1 line
2.47.0
author
#38 lore raw

PATCH v3 17/27 ublk: document feature UBLK_F_BATCH_IO

In reply to Ming Lei

Document feature UBLK_F_BATCH_IO.

---
 Documentation/block/ublk.rst | 60 +++++++++++++++++++++++++++++++++---
 1 file changed, 56 insertions(+), 4 deletions(-)
Documentation/block/ublk.rst +56 -4
index 8c4030bcabb6..09a5604f8e10 100644
@@ -260,9 +260,12 @@ The following IO commands are communicated via io_uring passthrough command,
 and each command is only for forwarding the IO and committing the result
 with specified IO tag in the command data:
 
-- ``UBLK_IO_FETCH_REQ``
+Traditional Per-I/O Commands
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
-  Sent from the server IO pthread for fetching future incoming IO requests
+- ``UBLK_U_IO_FETCH_REQ``
+
+  Sent from the server I/O pthread for fetching future incoming I/O requests
   destined to ``/dev/ublkb*``. This command is sent only once from the server
   IO pthread for ublk driver to setup IO forward environment.
 
@@ -278,7 +281,7 @@ with specified IO tag in the command data:
   supported by the driver, daemons must be per-queue instead - i.e. all I/Os
   associated to a single qid must be handled by the same task.
 
-- ``UBLK_IO_COMMIT_AND_FETCH_REQ``
+- ``UBLK_U_IO_COMMIT_AND_FETCH_REQ``
 
   When an IO request is destined to ``/dev/ublkb*``, the driver stores
   the IO's ``ublksrv_io_desc`` to the specified mapped area; then the
@@ -293,7 +296,7 @@ with specified IO tag in the command data:
   requests with the same IO tag. That is, ``UBLK_IO_COMMIT_AND_FETCH_REQ``
   is reused for both fetching request and committing back IO result.
 
-- ``UBLK_IO_NEED_GET_DATA``
+- ``UBLK_U_IO_NEED_GET_DATA``
 
   With ``UBLK_F_NEED_GET_DATA`` enabled, the WRITE request will be firstly
   issued to ublk server without data copy. Then, IO backend of ublk server
@@ -322,6 +325,55 @@ with specified IO tag in the command data:
   ``UBLK_IO_COMMIT_AND_FETCH_REQ`` to the server, ublkdrv needs to copy
   the server buffer (pages) read to the IO request pages.
 
+Batch I/O Commands (UBLK_F_BATCH_IO)
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+The ``UBLK_F_BATCH_IO`` feature provides an alternative high-performance
+I/O handling model that replaces the traditional per-I/O commands with
+per-queue batch commands. This significantly reduces communication overhead
+and enables better load balancing across multiple server tasks.
+
+Key differences from traditional mode:
+
+- **Per-queue vs Per-I/O**: Commands operate on queues rather than individual I/Os
+- **Batch processing**: Multiple I/Os are handled in single operations
+- **Multishot commands**: Use io_uring multishot for reduced submission overhead
+- **Flexible task assignment**: Any task can handle any I/O (no per-I/O daemons)
+- **Better load balancing**: Tasks can adjust their workload dynamically
+
+Batch I/O Commands:
+
+- ``UBLK_U_IO_PREP_IO_CMDS``
+
+  Prepares multiple I/O commands in batch. The server provides a buffer
+  containing multiple I/O descriptors that will be processed together.
+  This reduces the number of individual command submissions required.
+
+- ``UBLK_U_IO_COMMIT_IO_CMDS``
+
+  Commits results for multiple I/O operations in batch. The server provides
+  a buffer containing the results of multiple completed I/Os, allowing
+  efficient bulk completion of requests.
+
+- ``UBLK_U_IO_FETCH_IO_CMDS``
+
+  **Multishot command** for fetching I/O commands in batch. This is the key
+  command that enables high-performance batch processing:
+
+  * Uses io_uring multishot capability for reduced submission overhead
+  * Single command can fetch multiple I/O requests over time
+  * Buffer size determines maximum batch size per operation
+  * Multiple fetch commands can be submitted for load balancing
+  * Only one fetch command is active at any time per queue
+  * Supports dynamic load balancing across multiple server tasks
+
+  Each task can submit ``UBLK_U_IO_FETCH_IO_CMDS`` with different buffer
+  sizes to control how much work it handles. This enables sophisticated
+  load balancing strategies in multi-threaded servers.
+
+Migration: Applications using traditional commands (``UBLK_U_IO_FETCH_REQ``,
+``UBLK_U_IO_COMMIT_AND_FETCH_REQ``) cannot use batch mode simultaneously.
+
 Zero copy
 ---------
 
Signature · 1 line
2.47.0
author
#39 lore raw

PATCH v3 18/27 ublk: implement batch request completion via blk_mq_end_request_batch()

In reply to Ming Lei

Reduce overhead when completing multiple requests in batch I/O mode by accumulating them in an io_comp_batch structure and completing them together via blk_mq_end_request_batch(). This minimizes per-request completion overhead and improves performance for high IOPS workloads.

The implementation adds an io_comp_batch pointer to struct ublk_io and initializes it in __ublk_fetch(). For batch I/O, the pointer is set to the batch structure in ublk_batch_commit_io(). The __ublk_complete_rq() function uses io->iob to call blk_mq_add_to_batch() for batch mode. After processing all batch I/Os, the completion callback is invoked in ublk_handle_batch_commit_cmd() to complete all accumulated requests efficiently.

So far just covers direct completion. For deferred completion(zero copy, auto buffer reg), ublk_io_release() is often delayed in freeing buffer consumer io_uring request's code path, so this patch often doesn't work, also it is hard to pass the per-task 'struct io_comp_batch' for deferred completion.

---
 drivers/block/ublk_drv.c | 31 +++++++++++++++++++++++--------
 1 file changed, 23 insertions(+), 8 deletions(-)
drivers/block/ublk_drv.c +23 -8
index a2bf5a10f446..f597cdc13b2a 100644
@@ -130,6 +130,7 @@ struct ublk_batch_io_data {
 	struct io_uring_cmd *cmd;
 	struct ublk_batch_io header;
 	unsigned int issue_flags;
+	struct io_comp_batch *iob;
 };
 
 /*
@@ -639,7 +640,12 @@ static blk_status_t ublk_setup_iod_zoned(struct ublk_queue *ubq,
 #endif
 
 static inline void __ublk_complete_rq(struct request *req, struct ublk_io *io,
-				      bool need_map);
+				      bool need_map, struct io_comp_batch *iob);
+
+static void ublk_complete_batch(struct io_comp_batch *iob)
+{
+	blk_mq_end_request_batch(iob);
+}
 
 static dev_t ublk_chr_devt;
 static const struct class ublk_chr_class = {
@@ -909,7 +915,7 @@ static inline void ublk_put_req_ref(struct ublk_io *io, struct request *req)
 		return;
 
 	/* ublk_need_map_io() and ublk_need_req_ref() are mutually exclusive */
-	__ublk_complete_rq(req, io, false);
+	__ublk_complete_rq(req, io, false, NULL);
 }
 
 static inline bool ublk_sub_req_ref(struct ublk_io *io)
@@ -1248,7 +1254,7 @@ static inline struct ublk_uring_cmd_pdu *ublk_get_uring_cmd_pdu(
 
 /* todo: handle partial completion */
 static inline void __ublk_complete_rq(struct request *req, struct ublk_io *io,
-				      bool need_map)
+				      bool need_map, struct io_comp_batch *iob)
 {
 	unsigned int unmapped_bytes;
 	blk_status_t res = BLK_STS_OK;
@@ -1285,8 +1291,11 @@ static inline void __ublk_complete_rq(struct request *req, struct ublk_io *io,
 
 	if (blk_update_request(req, BLK_STS_OK, io->res))
 		blk_mq_requeue_request(req, true);
-	else if (likely(!blk_should_fake_timeout(req->q)))
+	else if (likely(!blk_should_fake_timeout(req->q))) {
+		if (blk_mq_add_to_batch(req, iob, false, ublk_complete_batch))
+			return;
 		__blk_mq_end_request(req, BLK_STS_OK);
+	}
 
 	return;
 exit:
@@ -2186,7 +2195,7 @@ static void __ublk_fail_req(struct ublk_device *ub, struct ublk_io *io,
 		blk_mq_requeue_request(req, false);
 	else {
 		io->res = -EIO;
-		__ublk_complete_rq(req, io, ublk_dev_need_map_io(ub));
+		__ublk_complete_rq(req, io, ublk_dev_need_map_io(ub), NULL);
 	}
 }
 
@@ -2923,7 +2932,7 @@ static int ublk_ch_uring_cmd_local(struct io_uring_cmd *cmd,
 		if (req_op(req) == REQ_OP_ZONE_APPEND)
 			req->__sector = addr;
 		if (compl)
-			__ublk_complete_rq(req, io, ublk_dev_need_map_io(ub));
+			__ublk_complete_rq(req, io, ublk_dev_need_map_io(ub), NULL);
 
 		if (ret)
 			goto out;
@@ -3270,11 +3279,11 @@ static int ublk_batch_commit_io(struct ublk_queue *ubq,
 	if (req_op(req) == REQ_OP_ZONE_APPEND)
 		req->__sector = ublk_batch_zone_lba(uc, elem);
 	if (compl)
-		__ublk_complete_rq(req, io, ublk_dev_need_map_io(data->ub));
+		__ublk_complete_rq(req, io, ublk_dev_need_map_io(data->ub), data->iob);
 	return 0;
 }
 
-static int ublk_handle_batch_commit_cmd(const struct ublk_batch_io_data *data)
+static int ublk_handle_batch_commit_cmd(struct ublk_batch_io_data *data)
 {
 	const struct ublk_batch_io *uc = &data->header;
 	struct io_uring_cmd *cmd = data->cmd;
@@ -3282,8 +3291,11 @@ static int ublk_handle_batch_commit_cmd(const struct ublk_batch_io_data *data)
 		.total = uc->nr_elem * uc->elem_bytes,
 		.elem_bytes = uc->elem_bytes,
 	};
+	DEFINE_IO_COMP_BATCH(iob);
 	int ret;
 
+	data->iob = &iob;
+
 	ret = io_uring_cmd_import_fixed(cmd->sqe->addr, iter.total,
 			WRITE, &iter.iter, cmd, data->issue_flags);
 	if (ret)
@@ -3291,6 +3303,9 @@ static int ublk_handle_batch_commit_cmd(const struct ublk_batch_io_data *data)
 
 	ret = ublk_walk_cmd_buf(&iter, data, ublk_batch_commit_io);
 
+	if (iob.complete)
+		iob.complete(&iob);
+
 	return iter.done == 0 ? ret : iter.done;
 }
 
Signature · 1 line
2.47.0
author
#40 lore raw

PATCH v3 19/27 selftests: ublk: fix user_data truncation for tgt_data >= 256

In reply to Ming Lei

The build_user_data() function packs multiple fields into a __u64 value using bit shifts. Without explicit __u64 casts before shifting, the shift operations are performed on 32-bit unsigned integers before being promoted to 64-bit, causing data loss.

Specifically, when tgt_data >= 256, the expression (tgt_data << 24) shifts on a 32-bit value, truncating the upper 8 bits before promotion to __u64. Since tgt_data can be up to 16 bits (assertion allows up to 65535), values >= 256 would have their high byte lost.

Add explicit __u64 casts to both op and tgt_data before shifting to ensure the shift operations happen in 64-bit space, preserving all bits of the input values.

user_data_to_tgt_data() is only used by stripe.c, in which the max supported member disks are 4, so won't trigger this issue.

---
 tools/testing/selftests/ublk/kublk.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/ublk/kublk.h +1 -1
index fe42705c6d42..38d80e60e211 100644
@@ -220,7 +220,7 @@ static inline __u64 build_user_data(unsigned tag, unsigned op,
 	_Static_assert(UBLK_MAX_QUEUES_SHIFT <= 7);
 	assert(!(tag >> 16) && !(op >> 8) && !(tgt_data >> 16) && !(q_id >> 7));
 
-	return tag | (op << 16) | (tgt_data << 24) |
+	return tag | ((__u64)op << 16) | ((__u64)tgt_data << 24) |
 		(__u64)q_id << 56 | (__u64)is_target_io << 63;
 }
 
Signature · 1 line
2.47.0
author
#41 lore raw

PATCH v3 20/27 selftests: ublk: replace assert() with ublk_assert()

In reply to Ming Lei

Replace assert() with ublk_assert() since it is often triggered in daemon, and we may get nothing shown in terminal.

Add ublk_assert(), so we can log something to syslog when assert() is triggered.

---
 tools/testing/selftests/ublk/common.c      |  2 +-
 tools/testing/selftests/ublk/file_backed.c |  2 +-
 tools/testing/selftests/ublk/kublk.c       |  2 +-
 tools/testing/selftests/ublk/kublk.h       |  2 +-
 tools/testing/selftests/ublk/stripe.c      | 10 +++++-----
 tools/testing/selftests/ublk/utils.h       | 10 ++++++++++
 6 files changed, 19 insertions(+), 9 deletions(-)
tools/testing/selftests/ublk/common.c +1 -1
index 01580a6f8519..4c07bc37eb6d 100644
@@ -16,7 +16,7 @@ int backing_file_tgt_init(struct ublk_dev *dev)
 {
 	int fd, i;
 
-	assert(dev->nr_fds == 1);
+	ublk_assert(dev->nr_fds == 1);
 
 	for (i = 0; i < dev->tgt.nr_backing_files; i++) {
 		char *file = dev->tgt.backing_file[i];
tools/testing/selftests/ublk/file_backed.c +1 -1
index cd9fe69ecce2..9e7dd3859ea9 100644
@@ -10,7 +10,7 @@ static enum io_uring_op ublk_to_uring_op(const struct ublksrv_io_desc *iod, int
 		return zc ? IORING_OP_READ_FIXED : IORING_OP_READ;
 	else if (ublk_op == UBLK_IO_OP_WRITE)
 		return zc ? IORING_OP_WRITE_FIXED : IORING_OP_WRITE;
-	assert(0);
+	ublk_assert(0);
 }
 
 static int loop_queue_flush_io(struct ublk_thread *t, struct ublk_queue *q,
tools/testing/selftests/ublk/kublk.c +1 -1
index f8fa102a627f..bb8da9ff247d 100644
@@ -750,7 +750,7 @@ static void ublk_handle_uring_cmd(struct ublk_thread *t,
 	}
 
 	if (cqe->res == UBLK_IO_RES_OK) {
-		assert(tag < q->q_depth);
+		ublk_assert(tag < q->q_depth);
 		if (q->tgt_ops->queue_io)
 			q->tgt_ops->queue_io(t, q, tag);
 	} else if (cqe->res == UBLK_IO_RES_NEED_GET_DATA) {
tools/testing/selftests/ublk/kublk.h +1 -1
index 38d80e60e211..f5c0978f30c2 100644
@@ -218,7 +218,7 @@ static inline __u64 build_user_data(unsigned tag, unsigned op,
 {
 	/* we only have 7 bits to encode q_id */
 	_Static_assert(UBLK_MAX_QUEUES_SHIFT <= 7);
-	assert(!(tag >> 16) && !(op >> 8) && !(tgt_data >> 16) && !(q_id >> 7));
+	ublk_assert(!(tag >> 16) && !(op >> 8) && !(tgt_data >> 16) && !(q_id >> 7));
 
 	return tag | ((__u64)op << 16) | ((__u64)tgt_data << 24) |
 		(__u64)q_id << 56 | (__u64)is_target_io << 63;
tools/testing/selftests/ublk/stripe.c +5 -5
index 791fa8dc1651..50874858a829 100644
@@ -96,12 +96,12 @@ static void calculate_stripe_array(const struct stripe_conf *conf,
 			this->seq = seq;
 			s->nr += 1;
 		} else {
-			assert(seq == this->seq);
-			assert(this->start + this->nr_sects == stripe_off);
+			ublk_assert(seq == this->seq);
+			ublk_assert(this->start + this->nr_sects == stripe_off);
 			this->nr_sects += nr_sects;
 		}
 
-		assert(this->nr_vec < this->cap);
+		ublk_assert(this->nr_vec < this->cap);
 		this->vec[this->nr_vec].iov_base = (void *)(base + done);
 		this->vec[this->nr_vec++].iov_len = nr_sects << 9;
 
@@ -120,7 +120,7 @@ static inline enum io_uring_op stripe_to_uring_op(
 		return zc ? IORING_OP_READV_FIXED : IORING_OP_READV;
 	else if (ublk_op == UBLK_IO_OP_WRITE)
 		return zc ? IORING_OP_WRITEV_FIXED : IORING_OP_WRITEV;
-	assert(0);
+	ublk_assert(0);
 }
 
 static int stripe_queue_tgt_rw_io(struct ublk_thread *t, struct ublk_queue *q,
@@ -318,7 +318,7 @@ static int ublk_stripe_tgt_init(const struct dev_ctx *ctx, struct ublk_dev *dev)
 	if (!dev->tgt.nr_backing_files || dev->tgt.nr_backing_files > NR_STRIPE)
 		return -EINVAL;
 
-	assert(dev->nr_fds == dev->tgt.nr_backing_files + 1);
+	ublk_assert(dev->nr_fds == dev->tgt.nr_backing_files + 1);
 
 	for (i = 0; i < dev->tgt.nr_backing_files; i++)
 		dev->tgt.backing_file_size[i] &= ~((1 << chunk_shift) - 1);
tools/testing/selftests/ublk/utils.h +10 -0
index a852e0b7153e..17eefed73690 100644
@@ -43,6 +43,7 @@ static inline void ublk_err(const char *fmt, ...)
 
 	va_start(ap, fmt);
 	vfprintf(stderr, fmt, ap);
+	va_end(ap);
 }
 
 static inline void ublk_log(const char *fmt, ...)
@@ -52,6 +53,7 @@ static inline void ublk_log(const char *fmt, ...)
 
 		va_start(ap, fmt);
 		vfprintf(stdout, fmt, ap);
+		va_end(ap);
 	}
 }
 
@@ -62,7 +64,15 @@ static inline void ublk_dbg(int level, const char *fmt, ...)
 
 		va_start(ap, fmt);
 		vfprintf(stdout, fmt, ap);
+		va_end(ap);
 	}
 }
 
+#define ublk_assert(x)  do { \
+	if (!(x)) {     \
+		ublk_err("%s %d: assert!\n", __func__, __LINE__); \
+		assert(x);      \
+	}       \
+} while (0)
+
 #endif
Signature · 1 line
2.47.0
author
#42 lore raw

PATCH v3 21/27 selftests: ublk: add ublk_io_buf_idx() for returning io buffer index

In reply to Ming Lei

Since UBLK_F_PER_IO_DAEMON is added, io buffer index may depend on current thread because the common way is to use per-pthread io_ring_ctx for issuing ublk uring_cmd.

Add one helper for returning io buffer index, so we can hide the buffer index implementation details for target code.

---
 tools/testing/selftests/ublk/file_backed.c |  9 +++++----
 tools/testing/selftests/ublk/kublk.c       |  9 +++++----
 tools/testing/selftests/ublk/kublk.h       | 10 +++++++++-
 tools/testing/selftests/ublk/null.c        | 18 ++++++++++--------
 tools/testing/selftests/ublk/stripe.c      |  7 ++++---
 5 files changed, 33 insertions(+), 20 deletions(-)
tools/testing/selftests/ublk/file_backed.c +5 -4
index 9e7dd3859ea9..58ac59528b74 100644
@@ -36,6 +36,7 @@ static int loop_queue_tgt_rw_io(struct ublk_thread *t, struct ublk_queue *q,
 	enum io_uring_op op = ublk_to_uring_op(iod, zc | auto_zc);
 	struct io_uring_sqe *sqe[3];
 	void *addr = (zc | auto_zc) ? NULL : (void *)iod->addr;
+	unsigned short buf_idx = ublk_io_buf_idx(t, q, tag);
 
 	if (!zc || auto_zc) {
 		ublk_io_alloc_sqes(t, sqe, 1);
@@ -47,7 +48,7 @@ static int loop_queue_tgt_rw_io(struct ublk_thread *t, struct ublk_queue *q,
 				iod->nr_sectors << 9,
 				iod->start_sector << 9);
 		if (auto_zc)
-			sqe[0]->buf_index = tag;
+			sqe[0]->buf_index = buf_idx;
 		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, q->q_id, 1);
@@ -56,7 +57,7 @@ static int loop_queue_tgt_rw_io(struct ublk_thread *t, struct ublk_queue *q,
 
 	ublk_io_alloc_sqes(t, sqe, 3);
 
-	io_uring_prep_buf_register(sqe[0], q, tag, q->q_id, ublk_get_io(q, tag)->buf_index);
+	io_uring_prep_buf_register(sqe[0], q, tag, q->q_id, buf_idx);
 	sqe[0]->flags |= IOSQE_CQE_SKIP_SUCCESS | IOSQE_IO_HARDLINK;
 	sqe[0]->user_data = build_user_data(tag,
 			ublk_cmd_op_nr(sqe[0]->cmd_op), 0, q->q_id, 1);
@@ -64,11 +65,11 @@ static int loop_queue_tgt_rw_io(struct ublk_thread *t, struct ublk_queue *q,
 	io_uring_prep_rw(op, sqe[1], ublk_get_registered_fd(q, 1) /*fds[1]*/, 0,
 		iod->nr_sectors << 9,
 		iod->start_sector << 9);
-	sqe[1]->buf_index = tag;
+	sqe[1]->buf_index = buf_idx;
 	sqe[1]->flags |= IOSQE_FIXED_FILE | IOSQE_IO_HARDLINK;
 	sqe[1]->user_data = build_user_data(tag, ublk_op, 0, q->q_id, 1);
 
-	io_uring_prep_buf_unregister(sqe[2], q, tag, q->q_id, ublk_get_io(q, tag)->buf_index);
+	io_uring_prep_buf_unregister(sqe[2], q, tag, q->q_id, buf_idx);
 	sqe[2]->user_data = build_user_data(tag, ublk_cmd_op_nr(sqe[2]->cmd_op), 0, q->q_id, 1);
 
 	return 2;
tools/testing/selftests/ublk/kublk.c +5 -4
index bb8da9ff247d..1665a7865af4 100644
@@ -579,16 +579,17 @@ static void ublk_dev_unprep(struct ublk_dev *dev)
 	close(dev->fds[0]);
 }
 
-static void ublk_set_auto_buf_reg(const struct ublk_queue *q,
+static void ublk_set_auto_buf_reg(const struct ublk_thread *t,
+				  const struct ublk_queue *q,
 				  struct io_uring_sqe *sqe,
 				  unsigned short tag)
 {
 	struct ublk_auto_buf_reg buf = {};
 
 	if (q->tgt_ops->buf_index)
-		buf.index = q->tgt_ops->buf_index(q, tag);
+		buf.index = q->tgt_ops->buf_index(t, q, tag);
 	else
-		buf.index = q->ios[tag].buf_index;
+		buf.index = ublk_io_buf_idx(t, q, tag);
 
 	if (ublk_queue_auto_zc_fallback(q))
 		buf.flags = UBLK_AUTO_BUF_REG_FALLBACK;
@@ -655,7 +656,7 @@ int ublk_queue_io_cmd(struct ublk_thread *t, struct ublk_io *io)
 		cmd->addr	= 0;
 
 	if (ublk_queue_use_auto_zc(q))
-		ublk_set_auto_buf_reg(q, sqe[0], io->tag);
+		ublk_set_auto_buf_reg(t, q, sqe[0], io->tag);
 
 	user_data = build_user_data(io->tag, _IOC_NR(cmd_op), 0, q->q_id, 0);
 	io_uring_sqe_set_data64(sqe[0], user_data);
tools/testing/selftests/ublk/kublk.h +9 -1
index f5c0978f30c2..5b951ad9b03d 100644
@@ -143,7 +143,8 @@ struct ublk_tgt_ops {
 	void (*usage)(const struct ublk_tgt_ops *ops);
 
 	/* return buffer index for UBLK_F_AUTO_BUF_REG */
-	unsigned short (*buf_index)(const struct ublk_queue *, int tag);
+	unsigned short (*buf_index)(const struct ublk_thread *t,
+			const struct ublk_queue *, int tag);
 };
 
 struct ublk_tgt {
@@ -351,6 +352,13 @@ static inline void ublk_set_sqe_cmd_op(struct io_uring_sqe *sqe, __u32 cmd_op)
 	addr[1] = 0;
 }
 
+static inline unsigned short ublk_io_buf_idx(const struct ublk_thread *t,
+					     const struct ublk_queue *q,
+					     unsigned tag)
+{
+	return q->ios[tag].buf_index;
+}
+
 static inline struct ublk_io *ublk_get_io(struct ublk_queue *q, unsigned tag)
 {
 	return &q->ios[tag];
tools/testing/selftests/ublk/null.c +10 -8
index 280043f6b689..819f72ac2da9 100644
@@ -43,12 +43,12 @@ static int ublk_null_tgt_init(const struct dev_ctx *ctx, struct ublk_dev *dev)
 }
 
 static void __setup_nop_io(int tag, const struct ublksrv_io_desc *iod,
-		struct io_uring_sqe *sqe, int q_id)
+		struct io_uring_sqe *sqe, int q_id, unsigned buf_idx)
 {
 	unsigned ublk_op = ublksrv_get_op(iod);
 
 	io_uring_prep_nop(sqe);
-	sqe->buf_index = tag;
+	sqe->buf_index = buf_idx;
 	sqe->flags |= IOSQE_FIXED_FILE;
 	sqe->rw_flags = IORING_NOP_FIXED_BUFFER | IORING_NOP_INJECT_RESULT;
 	sqe->len = iod->nr_sectors << 9; 	/* injected result */
@@ -60,18 +60,19 @@ static int null_queue_zc_io(struct ublk_thread *t, struct ublk_queue *q,
 {
 	const struct ublksrv_io_desc *iod = ublk_get_iod(q, tag);
 	struct io_uring_sqe *sqe[3];
+	unsigned short buf_idx = ublk_io_buf_idx(t, q, tag);
 
 	ublk_io_alloc_sqes(t, sqe, 3);
 
-	io_uring_prep_buf_register(sqe[0], q, tag, q->q_id, ublk_get_io(q, tag)->buf_index);
+	io_uring_prep_buf_register(sqe[0], q, tag, q->q_id, buf_idx);
 	sqe[0]->user_data = build_user_data(tag,
 			ublk_cmd_op_nr(sqe[0]->cmd_op), 0, q->q_id, 1);
 	sqe[0]->flags |= IOSQE_CQE_SKIP_SUCCESS | IOSQE_IO_HARDLINK;
 
-	__setup_nop_io(tag, iod, sqe[1], q->q_id);
+	__setup_nop_io(tag, iod, sqe[1], q->q_id, buf_idx);
 	sqe[1]->flags |= IOSQE_IO_HARDLINK;
 
-	io_uring_prep_buf_unregister(sqe[2], q, tag, q->q_id, ublk_get_io(q, tag)->buf_index);
+	io_uring_prep_buf_unregister(sqe[2], q, tag, q->q_id, buf_idx);
 	sqe[2]->user_data = build_user_data(tag, ublk_cmd_op_nr(sqe[2]->cmd_op), 0, q->q_id, 1);
 
 	// buf register is marked as IOSQE_CQE_SKIP_SUCCESS
@@ -85,7 +86,7 @@ static int null_queue_auto_zc_io(struct ublk_thread *t, struct ublk_queue *q,
 	struct io_uring_sqe *sqe[1];
 
 	ublk_io_alloc_sqes(t, sqe, 1);
-	__setup_nop_io(tag, iod, sqe[0], q->q_id);
+	__setup_nop_io(tag, iod, sqe[0], q->q_id, ublk_io_buf_idx(t, q, tag));
 	return 1;
 }
 
@@ -136,11 +137,12 @@ static int ublk_null_queue_io(struct ublk_thread *t, struct ublk_queue *q,
  * return invalid buffer index for triggering auto buffer register failure,
  * then UBLK_IO_RES_NEED_REG_BUF handling is covered
  */
-static unsigned short ublk_null_buf_index(const struct ublk_queue *q, int tag)
+static unsigned short ublk_null_buf_index(const struct ublk_thread *t,
+		const struct ublk_queue *q, int tag)
 {
 	if (ublk_queue_auto_zc_fallback(q))
 		return (unsigned short)-1;
-	return q->ios[tag].buf_index;
+	return ublk_io_buf_idx(t, q, tag);
 }
 
 const struct ublk_tgt_ops null_tgt_ops = {
tools/testing/selftests/ublk/stripe.c +4 -3
index 50874858a829..db281a879877 100644
@@ -135,6 +135,7 @@ static int stripe_queue_tgt_rw_io(struct ublk_thread *t, struct ublk_queue *q,
 	struct ublk_io *io = ublk_get_io(q, tag);
 	int i, extra = zc ? 2 : 0;
 	void *base = (zc | auto_zc) ? NULL : (void *)iod->addr;
+	unsigned short buf_idx = ublk_io_buf_idx(t, q, tag);
 
 	io->private_data = s;
 	calculate_stripe_array(conf, iod, s, base);
@@ -142,7 +143,7 @@ static int stripe_queue_tgt_rw_io(struct ublk_thread *t, struct ublk_queue *q,
 	ublk_io_alloc_sqes(t, sqe, s->nr + extra);
 
 	if (zc) {
-		io_uring_prep_buf_register(sqe[0], q, tag, q->q_id, io->buf_index);
+		io_uring_prep_buf_register(sqe[0], q, tag, q->q_id, buf_idx);
 		sqe[0]->flags |= IOSQE_CQE_SKIP_SUCCESS | IOSQE_IO_HARDLINK;
 		sqe[0]->user_data = build_user_data(tag,
 			ublk_cmd_op_nr(sqe[0]->cmd_op), 0, q->q_id, 1);
@@ -158,7 +159,7 @@ static int stripe_queue_tgt_rw_io(struct ublk_thread *t, struct ublk_queue *q,
 				t->start << 9);
 		io_uring_sqe_set_flags(sqe[i], IOSQE_FIXED_FILE);
 		if (auto_zc || zc) {
-			sqe[i]->buf_index = tag;
+			sqe[i]->buf_index = buf_idx;
 			if (zc)
 				sqe[i]->flags |= IOSQE_IO_HARDLINK;
 		}
@@ -168,7 +169,7 @@ static int stripe_queue_tgt_rw_io(struct ublk_thread *t, struct ublk_queue *q,
 	if (zc) {
 		struct io_uring_sqe *unreg = sqe[s->nr + 1];
 
-		io_uring_prep_buf_unregister(unreg, q, tag, q->q_id, io->buf_index);
+		io_uring_prep_buf_unregister(unreg, q, tag, q->q_id, buf_idx);
 		unreg->user_data = build_user_data(
 			tag, ublk_cmd_op_nr(unreg->cmd_op), 0, q->q_id, 1);
 	}
Signature · 1 line
2.47.0
author
#43 lore raw

PATCH v3 22/27 selftests: ublk: add batch buffer management infrastructure

In reply to Ming Lei

Add the foundational infrastructure for UBLK_F_BATCH_IO buffer management including:

- Allocator utility functions for small sized per-thread allocation
- Batch buffer allocation and deallocation functions
- Buffer index management for commit buffers
- Thread state management for batch I/O mode
- Buffer size calculation based on device features

This prepares the groundwork for handling batch I/O commands by establishing the buffer management layer needed for UBLK_U_IO_PREP_IO_CMDS and UBLK_U_IO_COMMIT_IO_CMDS operations.

The allocator uses CPU sets for efficient per-thread buffer tracking, and commit buffers are pre-allocated with 2 buffers per thread to handle overlapping command operations.

---
 tools/testing/selftests/ublk/Makefile |   2 +-
 tools/testing/selftests/ublk/batch.c  | 153 ++++++++++++++++++++++++++
 tools/testing/selftests/ublk/kublk.c  |  26 ++++-
 tools/testing/selftests/ublk/kublk.h  |  53 +++++++++
 tools/testing/selftests/ublk/utils.h  |  54 +++++++++
 5 files changed, 284 insertions(+), 4 deletions(-)
 create mode 100644 tools/testing/selftests/ublk/batch.c
tools/testing/selftests/ublk/Makefile +1 -1
index 770269efe42a..a724276622d0 100644
@@ -44,7 +44,7 @@ TEST_GEN_PROGS_EXTENDED = kublk
 
 include ../lib.mk
 
-$(TEST_GEN_PROGS_EXTENDED): kublk.c null.c file_backed.c common.c stripe.c \
+$(TEST_GEN_PROGS_EXTENDED): kublk.c batch.c null.c file_backed.c common.c stripe.c \
 	fault_inject.c
 
 check:
tools/testing/selftests/ublk/batch.c new file +153 -0

153 lines not on this page · raw message

tools/testing/selftests/ublk/kublk.c +23 -3
index 1665a7865af4..29594612edc9 100644
@@ -423,6 +423,8 @@ static void ublk_thread_deinit(struct ublk_thread *t)
 {
 	io_uring_unregister_buffers(&t->ring);
 
+	ublk_batch_free_buf(t);
+
 	io_uring_unregister_ring_fd(&t->ring);
 
 	if (t->ring.ring_fd > 0) {
@@ -505,15 +507,33 @@ static int ublk_thread_init(struct ublk_thread *t, unsigned long long extra_flag
 		unsigned nr_ios = dev->dev_info.queue_depth * dev->dev_info.nr_hw_queues;
 		unsigned max_nr_ios_per_thread = nr_ios / dev->nthreads;
 		max_nr_ios_per_thread += !!(nr_ios % dev->nthreads);
-		ret = io_uring_register_buffers_sparse(
-			&t->ring, max_nr_ios_per_thread);
+
+		t->nr_bufs = max_nr_ios_per_thread;
+	} else {
+		t->nr_bufs = 0;
+	}
+
+	if (ublk_dev_batch_io(dev))
+		 ublk_batch_prepare(t);
+
+	if (t->nr_bufs) {
+		ret = io_uring_register_buffers_sparse(&t->ring, t->nr_bufs);
 		if (ret) {
-			ublk_err("ublk dev %d thread %d register spare buffers failed %d",
+			ublk_err("ublk dev %d thread %d register spare buffers failed %d\n",
 					dev->dev_info.dev_id, t->idx, ret);
 			goto fail;
 		}
 	}
 
+	if (ublk_dev_batch_io(dev)) {
+		ret = ublk_batch_alloc_buf(t);
+		if (ret) {
+			ublk_err("ublk dev %d thread %d alloc batch buf failed %d\n",
+				dev->dev_info.dev_id, t->idx, ret);
+			goto fail;
+		}
+	}
+
 	io_uring_register_ring_fd(&t->ring);
 
 	if (flags & UBLKS_Q_NO_UBLK_FIXED_FD) {
tools/testing/selftests/ublk/kublk.h +53 -0

80 lines not on this page · raw message

tools/testing/selftests/ublk/utils.h +54 -0

60 lines not on this page · raw message

Signature · 1 line
2.47.0
author
#44 lore raw

PATCH v3 23/27 selftests: ublk: handle UBLK_U_IO_PREP_IO_CMDS

In reply to Ming Lei

Implement support for UBLK_U_IO_PREP_IO_CMDS in the batch I/O framework:

- Add batch command initialization and setup functions
- Implement prep command queueing with proper buffer management
- Add command completion handling for prep and commit commands
- Integrate batch I/O setup into thread initialization
- Update CQE handling to support batch commands

The implementation uses the previously established buffer management infrastructure to queue UBLK_U_IO_PREP_IO_CMDS commands. Commands are prepared in the first thread context and use commit buffers for efficient command batching.

Key changes:
- ublk_batch_queue_prep_io_cmds() prepares I/O command batches
- ublk_batch_compl_cmd() handles batch command completions
- Modified thread setup to use batch operations when enabled
- Enhanced buffer index calculation for batch mode

---
 tools/testing/selftests/ublk/batch.c | 114 +++++++++++++++++++++++++++
 tools/testing/selftests/ublk/kublk.c |  46 ++++++++---
 tools/testing/selftests/ublk/kublk.h |  22 ++++++
 3 files changed, 172 insertions(+), 10 deletions(-)
tools/testing/selftests/ublk/batch.c +114 -0
index b6bcdd529df9..ca0ecc33e6a1 100644
@@ -151,3 +151,117 @@ void ublk_batch_free_buf(struct ublk_thread *t)
 {
 	free_batch_commit_buf(t);
 }
+
+static void ublk_init_batch_cmd(struct ublk_thread *t, __u16 q_id,
+				struct io_uring_sqe *sqe, unsigned op,
+				unsigned short elem_bytes,
+				unsigned short nr_elem,
+				unsigned short buf_idx)
+{
+	struct ublk_batch_io *cmd;
+	__u64 user_data;
+
+	cmd = (struct ublk_batch_io *)ublk_get_sqe_cmd(sqe);
+
+	ublk_set_sqe_cmd_op(sqe, op);
+
+	sqe->fd	= 0;	/* dev->fds[0] */
+	sqe->opcode	= IORING_OP_URING_CMD;
+	sqe->flags	= IOSQE_FIXED_FILE;
+
+	cmd->q_id	= q_id;
+	cmd->flags	= 0;
+	cmd->reserved 	= 0;
+	cmd->elem_bytes = elem_bytes;
+	cmd->nr_elem	= nr_elem;
+
+	user_data = build_user_data(buf_idx, _IOC_NR(op), 0, q_id, 0);
+	io_uring_sqe_set_data64(sqe, user_data);
+
+	t->cmd_inflight += 1;
+
+	ublk_dbg(UBLK_DBG_IO_CMD, "%s: thread %u qid %d cmd_op %x data %lx "
+			"nr_elem %u elem_bytes %u buf_size %u buf_idx %d "
+			"cmd_inflight %u\n",
+			__func__, t->idx, q_id, op, user_data,
+			cmd->nr_elem, cmd->elem_bytes,
+			nr_elem * elem_bytes, buf_idx, t->cmd_inflight);
+}
+
+static void ublk_setup_commit_sqe(struct ublk_thread *t,
+				  struct io_uring_sqe *sqe,
+				  unsigned short buf_idx)
+{
+	struct ublk_batch_io *cmd;
+
+	cmd = (struct ublk_batch_io *)ublk_get_sqe_cmd(sqe);
+
+	sqe->rw_flags= IORING_URING_CMD_FIXED;
+	sqe->buf_index = buf_idx;
+	cmd->flags |= t->cmd_flags;
+}
+
+int ublk_batch_queue_prep_io_cmds(struct ublk_thread *t, struct ublk_queue *q)
+{
+	unsigned short nr_elem = q->q_depth;
+	unsigned short buf_idx = ublk_alloc_commit_buf(t);
+	struct io_uring_sqe *sqe;
+	void *buf;
+	int i;
+
+	ublk_assert(buf_idx != UBLKS_T_COMMIT_BUF_INV_IDX);
+
+	ublk_io_alloc_sqes(t, &sqe, 1);
+
+	ublk_assert(nr_elem == q->q_depth);
+	buf = ublk_get_commit_buf(t, buf_idx);
+	for (i = 0; i < nr_elem; i++) {
+		struct ublk_batch_elem *elem = (struct ublk_batch_elem *)(
+				buf + i * t->commit_buf_elem_size);
+		struct ublk_io *io = &q->ios[i];
+
+		elem->tag = i;
+		elem->result = 0;
+
+		if (ublk_queue_use_auto_zc(q))
+			elem->buf_index = ublk_batch_io_buf_idx(t, q, i);
+		else if (!ublk_queue_no_buf(q))
+			elem->buf_addr = (__u64)io->buf_addr;
+	}
+
+	sqe->addr = (__u64)buf;
+	sqe->len = t->commit_buf_elem_size * nr_elem;
+
+	ublk_init_batch_cmd(t, q->q_id, sqe, UBLK_U_IO_PREP_IO_CMDS,
+			t->commit_buf_elem_size, nr_elem, buf_idx);
+	ublk_setup_commit_sqe(t, sqe, buf_idx);
+	return 0;
+}
+
+static void ublk_batch_compl_commit_cmd(struct ublk_thread *t,
+					const struct io_uring_cqe *cqe,
+					unsigned op)
+{
+	unsigned short buf_idx = user_data_to_tag(cqe->user_data);
+
+	if (op == _IOC_NR(UBLK_U_IO_PREP_IO_CMDS))
+		ublk_assert(cqe->res == 0);
+	else if (op == _IOC_NR(UBLK_U_IO_COMMIT_IO_CMDS))
+		;//assert(cqe->res == t->commit_buf_size);
+	else
+		ublk_assert(0);
+
+	ublk_free_commit_buf(t, buf_idx);
+}
+
+void ublk_batch_compl_cmd(struct ublk_thread *t,
+			  const struct io_uring_cqe *cqe)
+{
+	unsigned op = user_data_to_op(cqe->user_data);
+
+	if (op == _IOC_NR(UBLK_U_IO_PREP_IO_CMDS) ||
+			op == _IOC_NR(UBLK_U_IO_COMMIT_IO_CMDS)) {
+		ublk_batch_compl_commit_cmd(t, cqe, op);
+		return;
+	}
+}
tools/testing/selftests/ublk/kublk.c +36 -10

78 lines not on this page · raw message

tools/testing/selftests/ublk/kublk.h +22 -0
index e75c28680783..51fad0f4419b 100644
@@ -398,10 +398,16 @@ static inline void ublk_set_sqe_cmd_op(struct io_uring_sqe *sqe, __u32 cmd_op)
 	addr[1] = 0;
 }
 
+static inline unsigned short ublk_batch_io_buf_idx(
+		const struct ublk_thread *t, const struct ublk_queue *q,
+		unsigned tag);
+
 static inline unsigned short ublk_io_buf_idx(const struct ublk_thread *t,
 					     const struct ublk_queue *q,
 					     unsigned tag)
 {
+	if (ublk_queue_batch_io(q))
+		return ublk_batch_io_buf_idx(t, q, tag);
 	return q->ios[tag].buf_index;
 }
 
@@ -464,6 +470,22 @@ static inline int ublk_queue_no_buf(const struct ublk_queue *q)
 	return ublk_queue_use_zc(q) || ublk_queue_use_auto_zc(q);
 }
 
+/*
+ * Each IO's buffer index has to be calculated by this helper for
+ * UBLKS_T_BATCH_IO
+ */
+static inline unsigned short ublk_batch_io_buf_idx(
+		const struct ublk_thread *t, const struct ublk_queue *q,
+		unsigned tag)
+{
+	return tag;
+}
+
+/* Queue UBLK_U_IO_PREP_IO_CMDS for a specific queue with batch elements */
+int ublk_batch_queue_prep_io_cmds(struct ublk_thread *t, struct ublk_queue *q);
+/* Handle completion of batch I/O commands (prep/commit) */
+void ublk_batch_compl_cmd(struct ublk_thread *t,
+			  const struct io_uring_cqe *cqe);
 /* Initialize batch I/O state and calculate buffer parameters */
 void ublk_batch_prepare(struct ublk_thread *t);
 /* Allocate and register commit buffers for batch operations */
Signature · 1 line
2.47.0
author
#45 lore raw

PATCH v3 24/27 selftests: ublk: handle UBLK_U_IO_COMMIT_IO_CMDS

In reply to Ming Lei

Implement UBLK_U_IO_COMMIT_IO_CMDS to enable efficient batched completion of I/O operations in the batch I/O framework.

This completes the batch I/O infrastructure by adding the commit phase that notifies the kernel about completed I/O operations:

Key features:
- Batch multiple I/O completions into single UBLK_U_IO_COMMIT_IO_CMDS
- Dynamic commit buffer allocation and management per thread
- Automatic commit buffer preparation before processing events
- Commit buffer submission after processing completed I/Os
- Integration with existing completion workflows

Implementation details:
- ublk_batch_prep_commit() allocates and initializes commit buffers
- ublk_batch_complete_io() adds completed I/Os to current batch
- ublk_batch_commit_io_cmds() submits batched completions to kernel
- Modified ublk_process_io() to handle batch commit lifecycle
- Enhanced ublk_complete_io() to route to batch or legacy completion

The commit buffer stores completion information (tag, result, buffer details) for multiple I/Os, then submits them all at once, significantly reducing syscall overhead compared to individual I/O completions.

---
 tools/testing/selftests/ublk/batch.c | 74 ++++++++++++++++++++++++++--
 tools/testing/selftests/ublk/kublk.c |  8 ++-
 tools/testing/selftests/ublk/kublk.h | 69 +++++++++++++++++---------
 3 files changed, 122 insertions(+), 29 deletions(-)
tools/testing/selftests/ublk/batch.c +70 -4
index ca0ecc33e6a1..83c9cad71591 100644
@@ -175,7 +175,7 @@ static void ublk_init_batch_cmd(struct ublk_thread *t, __u16 q_id,
 	cmd->elem_bytes = elem_bytes;
 	cmd->nr_elem	= nr_elem;
 
-	user_data = build_user_data(buf_idx, _IOC_NR(op), 0, q_id, 0);
+	user_data = build_user_data(buf_idx, _IOC_NR(op), nr_elem, q_id, 0);
 	io_uring_sqe_set_data64(sqe, user_data);
 
 	t->cmd_inflight += 1;
@@ -246,9 +246,11 @@ static void ublk_batch_compl_commit_cmd(struct ublk_thread *t,
 
 	if (op == _IOC_NR(UBLK_U_IO_PREP_IO_CMDS))
 		ublk_assert(cqe->res == 0);
-	else if (op == _IOC_NR(UBLK_U_IO_COMMIT_IO_CMDS))
-		;//assert(cqe->res == t->commit_buf_size);
-	else
+	else if (op == _IOC_NR(UBLK_U_IO_COMMIT_IO_CMDS)) {
+		int nr_elem = user_data_to_tgt_data(cqe->user_data);
+
+		ublk_assert(cqe->res == t->commit_buf_elem_size * nr_elem);
+	} else
 		ublk_assert(0);
 
 	ublk_free_commit_buf(t, buf_idx);
@@ -265,3 +267,67 @@ void ublk_batch_compl_cmd(struct ublk_thread *t,
 		return;
 	}
 }
+
+void ublk_batch_commit_io_cmds(struct ublk_thread *t)
+{
+	struct io_uring_sqe *sqe;
+	unsigned short buf_idx;
+	unsigned short nr_elem = t->commit.done;
+
+	/* nothing to commit */
+	if (!nr_elem) {
+		ublk_free_commit_buf(t, t->commit.buf_idx);
+		return;
+	}
+
+	ublk_io_alloc_sqes(t, &sqe, 1);
+	buf_idx = t->commit.buf_idx;
+	sqe->addr = (__u64)t->commit.elem;
+	sqe->len = nr_elem * t->commit_buf_elem_size;
+
+	/* commit isn't per-queue command */
+	ublk_init_batch_cmd(t, t->commit.q_id, sqe, UBLK_U_IO_COMMIT_IO_CMDS,
+			t->commit_buf_elem_size, nr_elem, buf_idx);
+	ublk_setup_commit_sqe(t, sqe, buf_idx);
+}
+
+static void ublk_batch_init_commit(struct ublk_thread *t,
+				   unsigned short buf_idx)
+{
+	/* so far only support 1:1 queue/thread mapping */
+	t->commit.q_id = t->idx;
+	t->commit.buf_idx = buf_idx;
+	t->commit.elem = ublk_get_commit_buf(t, buf_idx);
+	t->commit.done = 0;
+	t->commit.count = t->commit_buf_size /
+		t->commit_buf_elem_size;
+}
+
+void ublk_batch_prep_commit(struct ublk_thread *t)
+{
+	unsigned short buf_idx = ublk_alloc_commit_buf(t);
+
+	ublk_assert(buf_idx != UBLKS_T_COMMIT_BUF_INV_IDX);
+	ublk_batch_init_commit(t, buf_idx);
+}
+
+void ublk_batch_complete_io(struct ublk_thread *t, struct ublk_queue *q,
+			    unsigned tag, int res)
+{
+	struct batch_commit_buf *cb = &t->commit;
+	struct ublk_batch_elem *elem = (struct ublk_batch_elem *)(cb->elem +
+			cb->done * t->commit_buf_elem_size);
+	struct ublk_io *io = &q->ios[tag];
+
+	ublk_assert(q->q_id == t->commit.q_id);
+
+	elem->tag = tag;
+	elem->buf_index = ublk_batch_io_buf_idx(t, q, tag);
+	elem->result = res;
+
+	if (!ublk_queue_no_buf(q))
+		elem->buf_addr	= (__u64) (uintptr_t) io->buf_addr;
+
+	cb->done += 1;
+	ublk_assert(cb->done <= cb->count);
+}
tools/testing/selftests/ublk/kublk.c +7 -1
index e981fcf18475..6565e804679c 100644
@@ -852,7 +852,13 @@ static int ublk_process_io(struct ublk_thread *t)
 		return -ENODEV;
 
 	ret = io_uring_submit_and_wait(&t->ring, 1);
-	reapped = ublk_reap_events_uring(t);
+	if (ublk_thread_batch_io(t)) {
+		ublk_batch_prep_commit(t);
+		reapped = ublk_reap_events_uring(t);
+		ublk_batch_commit_io_cmds(t);
+	} else {
+		reapped = ublk_reap_events_uring(t);
+	}
 
 	ublk_dbg(UBLK_DBG_THREAD, "submit result %d, reapped %d stop %d idle %d\n",
 			ret, reapped, (t->state & UBLKS_T_STOPPING),
tools/testing/selftests/ublk/kublk.h +45 -24

93 lines not on this page · raw message

Signature · 1 line
2.47.0
author
#46 lore raw

PATCH v3 25/27 selftests: ublk: handle UBLK_U_IO_FETCH_IO_CMDS

In reply to Ming Lei

Add support for UBLK_U_IO_FETCH_IO_CMDS to enable efficient batch fetching of I/O commands using multishot io_uring operations.

Key improvements:
- Implement multishot UBLK_U_IO_FETCH_IO_CMDS for continuous command fetching
- Add fetch buffer management with page-aligned, mlocked buffers
- Process fetched I/O command tags from kernel-provided buffers
- Integrate fetch operations with existing batch I/O infrastructure
- Significantly reduce uring_cmd issuing overhead through batching

The implementation uses two fetch buffers per thread with automatic requeuing to maintain continuous I/O command flow. Each fetch operation retrieves multiple command tags in a single syscall, dramatically improving performance compared to individual command fetching.

Technical details:
- Fetch buffers are page-aligned and mlocked for optimal performance
- Uses IORING_URING_CMD_MULTISHOT for continuous operation
- Automatic buffer management and requeuing on completion
- Enhanced CQE handling for fetch command completions

---
 tools/testing/selftests/ublk/batch.c | 137 ++++++++++++++++++++++++++-
 tools/testing/selftests/ublk/kublk.c |  14 ++-
 tools/testing/selftests/ublk/kublk.h |  13 +++
 3 files changed, 160 insertions(+), 4 deletions(-)
tools/testing/selftests/ublk/batch.c +136 -1
index 83c9cad71591..69fb417ebf93 100644
@@ -141,15 +141,63 @@ void ublk_batch_prepare(struct ublk_thread *t)
 			t->nr_bufs);
 }
 
+static void free_batch_fetch_buf(struct ublk_thread *t)
+{
+	int i;
+
+	for (i = 0; i < UBLKS_T_NR_FETCH_BUF; i++) {
+		io_uring_free_buf_ring(&t->ring, t->fetch[i].br, 1, i);
+		munlock(t->fetch[i].fetch_buf, t->fetch[i].fetch_buf_size);
+		free(t->fetch[i].fetch_buf);
+	}
+}
+
+static int alloc_batch_fetch_buf(struct ublk_thread *t)
+{
+	/* page aligned fetch buffer, and it is mlocked for speedup delivery */
+	unsigned pg_sz = getpagesize();
+	unsigned buf_size = round_up(t->dev->dev_info.queue_depth * 2, pg_sz);
+	int ret;
+	int i = 0;
+
+	for (i = 0; i < UBLKS_T_NR_FETCH_BUF; i++) {
+		t->fetch[i].fetch_buf_size = buf_size;
+
+		if (posix_memalign((void **)&t->fetch[i].fetch_buf, pg_sz,
+					t->fetch[i].fetch_buf_size))
+			return -ENOMEM;
+
+		/* lock fetch buffer page for fast fetching */
+		if (mlock(t->fetch[i].fetch_buf, t->fetch[i].fetch_buf_size))
+			ublk_err("%s: can't lock fetch buffer %s\n", __func__,
+				strerror(errno));
+		t->fetch[i].br = io_uring_setup_buf_ring(&t->ring, 1,
+			i, IOU_PBUF_RING_INC, &ret);
+		if (!t->fetch[i].br) {
+			ublk_err("Buffer ring register failed %d\n", ret);
+			return ret;
+		}
+	}
+
+	return 0;
+}
+
 int ublk_batch_alloc_buf(struct ublk_thread *t)
 {
+	int ret;
+
 	ublk_assert(t->nr_commit_buf < 16);
-	return alloc_batch_commit_buf(t);
+
+	ret = alloc_batch_commit_buf(t);
+	if (ret)
+		return ret;
+	return alloc_batch_fetch_buf(t);
 }
 
 void ublk_batch_free_buf(struct ublk_thread *t)
 {
 	free_batch_commit_buf(t);
+	free_batch_fetch_buf(t);
 }
 
 static void ublk_init_batch_cmd(struct ublk_thread *t, __u16 q_id,
@@ -201,6 +249,79 @@ static void ublk_setup_commit_sqe(struct ublk_thread *t,
 	cmd->flags |= t->cmd_flags;
 }
 
+static void ublk_batch_queue_fetch(struct ublk_thread *t,
+				   struct ublk_queue *q,
+				   unsigned short buf_idx)
+{
+	unsigned short nr_elem = t->fetch[buf_idx].fetch_buf_size / 2;
+	struct io_uring_sqe *sqe;
+
+	io_uring_buf_ring_add(t->fetch[buf_idx].br, t->fetch[buf_idx].fetch_buf,
+			t->fetch[buf_idx].fetch_buf_size,
+			0, 0, 0);
+	io_uring_buf_ring_advance(t->fetch[buf_idx].br, 1);
+
+	ublk_io_alloc_sqes(t, &sqe, 1);
+
+	ublk_init_batch_cmd(t, q->q_id, sqe, UBLK_U_IO_FETCH_IO_CMDS, 2, nr_elem,
+			buf_idx);
+
+	sqe->rw_flags= IORING_URING_CMD_MULTISHOT;
+	sqe->buf_group = buf_idx;
+	sqe->flags |= IOSQE_BUFFER_SELECT;
+
+	t->fetch[buf_idx].fetch_buf_off = 0;
+}
+
+void ublk_batch_start_fetch(struct ublk_thread *t,
+			    struct ublk_queue *q)
+{
+	int i;
+
+	for (i = 0; i < UBLKS_T_NR_FETCH_BUF; i++)
+		ublk_batch_queue_fetch(t, q, i);
+}
+
+static unsigned short ublk_compl_batch_fetch(struct ublk_thread *t,
+				   struct ublk_queue *q,
+				   const struct io_uring_cqe *cqe)
+{
+	unsigned short buf_idx = user_data_to_tag(cqe->user_data);
+	unsigned start = t->fetch[buf_idx].fetch_buf_off;
+	unsigned end = start + cqe->res;
+	void *buf = t->fetch[buf_idx].fetch_buf;
+	int i;
+
+	if (cqe->res < 0)
+		return buf_idx;
+
+       if ((end - start) / 2 > q->q_depth) {
+               ublk_err("%s: fetch duplicated ios offset %u count %u\n", __func__, start, cqe->res);
+
+               for (i = start; i < end; i += 2) {
+                       unsigned short tag = *(unsigned short *)(buf + i);
+
+                       ublk_err("%u ", tag);
+               }
+               ublk_err("\n");
+       }
+
+	for (i = start; i < end; i += 2) {
+		unsigned short tag = *(unsigned short *)(buf + i);
+
+		if (tag == UBLK_BATCH_IO_UNUSED_TAG)
+			continue;
+
+		if (tag >= q->q_depth)
+			ublk_err("%s: bad tag %u\n", __func__, tag);
+
+		if (q->tgt_ops->queue_io)
+			q->tgt_ops->queue_io(t, q, tag);
+	}
+	t->fetch[buf_idx].fetch_buf_off = end;
+	return buf_idx;
+}
+
 int ublk_batch_queue_prep_io_cmds(struct ublk_thread *t, struct ublk_queue *q)
 {
 	unsigned short nr_elem = q->q_depth;
@@ -260,12 +381,26 @@ void ublk_batch_compl_cmd(struct ublk_thread *t,
 			  const struct io_uring_cqe *cqe)
 {
 	unsigned op = user_data_to_op(cqe->user_data);
+	struct ublk_queue *q;
+	unsigned buf_idx;
+	unsigned q_id;
 
 	if (op == _IOC_NR(UBLK_U_IO_PREP_IO_CMDS) ||
 			op == _IOC_NR(UBLK_U_IO_COMMIT_IO_CMDS)) {
 		ublk_batch_compl_commit_cmd(t, cqe, op);
 		return;
 	}
+
+	/* FETCH command is per queue */
+	q_id = user_data_to_q_id(cqe->user_data);
+	q = &t->dev->q[q_id];
+	buf_idx = ublk_compl_batch_fetch(t, q, cqe);
+
+	if (cqe->res < 0 && cqe->res != -ENOBUFS) {
+		 t->state |= UBLKS_T_STOPPING;
+	} else if (!(cqe->flags & IORING_CQE_F_MORE) || cqe->res == -ENOBUFS) {
+		ublk_batch_queue_fetch(t, q, buf_idx);
+	}
 }
 
 void ublk_batch_commit_io_cmds(struct ublk_thread *t)
tools/testing/selftests/ublk/kublk.c +11 -3
index 6565e804679c..cb329c7aebc4 100644
@@ -493,6 +493,10 @@ static int ublk_thread_init(struct ublk_thread *t, unsigned long long extra_flag
 	int ring_depth = dev->tgt.sq_depth, cq_depth = dev->tgt.cq_depth;
 	int ret;
 
+	/* FETCH_IO_CMDS is multishot, so increase cq depth for BATCH_IO */
+	if (ublk_dev_batch_io(dev))
+		cq_depth += dev->dev_info.queue_depth;
+
 	ret = ublk_setup_ring(&t->ring, ring_depth, cq_depth,
 			IORING_SETUP_COOP_TASKRUN |
 			IORING_SETUP_SINGLE_ISSUER |
@@ -797,7 +801,7 @@ static void ublk_handle_cqe(struct ublk_thread *t,
 	unsigned q_id = user_data_to_q_id(cqe->user_data);
 	unsigned cmd_op = user_data_to_op(cqe->user_data);
 
-	if (cqe->res < 0 && cqe->res != -ENODEV)
+	if (cqe->res < 0 && cqe->res != -ENODEV && cqe->res != -ENOBUFS)
 		ublk_err("%s: res %d userdata %llx thread state %x\n", __func__,
 				cqe->res, cqe->user_data, t->state);
 
@@ -922,9 +926,13 @@ static __attribute__((noinline)) int __ublk_io_handler_fn(struct ublk_thread_inf
 	if (!ublk_thread_batch_io(&t)) {
 		/* submit all io commands to ublk driver */
 		ublk_submit_fetch_commands(&t);
-	} else if (!t.idx) {
+	} else {
+		struct ublk_queue *q = &t.dev->q[t.idx];
+
 		/* prepare all io commands in the 1st thread context */
-		ublk_batch_setup_queues(&t);
+		if (!t.idx)
+			ublk_batch_setup_queues(&t);
+		ublk_batch_start_fetch(&t, q);
 	}
 
 	do {
tools/testing/selftests/ublk/kublk.h +13 -0
index 0a355653d64c..222501048c24 100644
@@ -190,6 +190,13 @@ struct batch_commit_buf {
 	unsigned short count;
 };
 
+struct batch_fetch_buf {
+	struct io_uring_buf_ring *br;
+	void *fetch_buf;
+	unsigned int fetch_buf_size;
+	unsigned int fetch_buf_off;
+};
+
 struct ublk_thread {
 	struct ublk_dev *dev;
 	unsigned idx;
@@ -216,6 +223,9 @@ struct ublk_thread {
 #define UBLKS_T_COMMIT_BUF_INV_IDX  ((unsigned short)-1)
 	struct allocator commit_buf_alloc;
 	struct batch_commit_buf commit;
+	/* FETCH_IO_CMDS buffer */
+#define UBLKS_T_NR_FETCH_BUF 	2
+	struct batch_fetch_buf fetch[UBLKS_T_NR_FETCH_BUF];
 
 	struct io_uring ring;
 };
@@ -468,6 +478,9 @@ static inline unsigned short ublk_batch_io_buf_idx(
 
 /* Queue UBLK_U_IO_PREP_IO_CMDS for a specific queue with batch elements */
 int ublk_batch_queue_prep_io_cmds(struct ublk_thread *t, struct ublk_queue *q);
+/* Start fetching I/O commands using multishot UBLK_U_IO_FETCH_IO_CMDS */
+void ublk_batch_start_fetch(struct ublk_thread *t,
+			    struct ublk_queue *q);
 /* Handle completion of batch I/O commands (prep/commit) */
 void ublk_batch_compl_cmd(struct ublk_thread *t,
 			  const struct io_uring_cqe *cqe);
Signature · 1 line
2.47.0
author
#47 lore raw

PATCH v3 26/27 selftests: ublk: add --batch/-b for enabling F_BATCH_IO

In reply to Ming Lei

Add --batch/-b for enabling F_BATCH_IO.

Add generic_14 for covering its basic function.

Add stress_06 and stress_07 for covering stress test.

---
 tools/testing/selftests/ublk/Makefile         |  3 ++
 tools/testing/selftests/ublk/kublk.c          | 13 +++++-
 .../testing/selftests/ublk/test_generic_14.sh | 32 +++++++++++++
 .../testing/selftests/ublk/test_stress_06.sh  | 45 +++++++++++++++++++
 .../testing/selftests/ublk/test_stress_07.sh  | 44 ++++++++++++++++++
 5 files changed, 136 insertions(+), 1 deletion(-)
 create mode 100755 tools/testing/selftests/ublk/test_generic_14.sh
 create mode 100755 tools/testing/selftests/ublk/test_stress_06.sh
 create mode 100755 tools/testing/selftests/ublk/test_stress_07.sh
tools/testing/selftests/ublk/Makefile +3 -0
index a724276622d0..cbf57113b1a6 100644
@@ -21,6 +21,7 @@ TEST_PROGS += test_generic_10.sh
 TEST_PROGS += test_generic_11.sh
 TEST_PROGS += test_generic_12.sh
 TEST_PROGS += test_generic_13.sh
+TEST_PROGS += test_generic_14.sh
 
 TEST_PROGS += test_null_01.sh
 TEST_PROGS += test_null_02.sh
@@ -39,6 +40,8 @@ TEST_PROGS += test_stress_02.sh
 TEST_PROGS += test_stress_03.sh
 TEST_PROGS += test_stress_04.sh
 TEST_PROGS += test_stress_05.sh
+TEST_PROGS += test_stress_06.sh
+TEST_PROGS += test_stress_07.sh
 
 TEST_GEN_PROGS_EXTENDED = kublk
 
tools/testing/selftests/ublk/kublk.c +12 -1
index cb329c7aebc4..4c45482a847c 100644
@@ -1476,6 +1476,7 @@ static int cmd_dev_get_features(void)
 		FEAT_NAME(UBLK_F_QUIESCE),
 		FEAT_NAME(UBLK_F_PER_IO_DAEMON),
 		FEAT_NAME(UBLK_F_BUF_REG_OFF_DAEMON),
+		FEAT_NAME(UBLK_F_BATCH_IO),
 	};
 	struct ublk_dev *dev;
 	__u64 features = 0;
@@ -1571,6 +1572,7 @@ static void __cmd_create_help(char *exe, bool recovery)
 	printf("\t[--foreground] [--quiet] [-z] [--auto_zc] [--auto_zc_fallback] [--debug_mask mask] [-r 0|1 ] [-g]\n");
 	printf("\t[-e 0|1 ] [-i 0|1] [--no_ublk_fixed_fd]\n");
 	printf("\t[--nthreads threads] [--per_io_tasks]\n");
+	printf("\t[--batch|-b]\n");
 	printf("\t[target options] [backfile1] [backfile2] ...\n");
 	printf("\tdefault: nr_queues=2(max 32), depth=128(max 1024), dev_id=-1(auto allocation)\n");
 	printf("\tdefault: nthreads=nr_queues");
@@ -1633,6 +1635,7 @@ int main(int argc, char *argv[])
 		{ "nthreads",		1,	NULL,  0 },
 		{ "per_io_tasks",	0,	NULL,  0 },
 		{ "no_ublk_fixed_fd",	0,	NULL,  0 },
+		{ "batch",              0,      NULL, 'b'},
 		{ 0, 0, 0, 0 }
 	};
 	const struct ublk_tgt_ops *ops = NULL;
@@ -1654,9 +1657,12 @@ int main(int argc, char *argv[])
 
 	opterr = 0;
 	optind = 2;
-	while ((opt = getopt_long(argc, argv, "t:n:d:q:r:e:i:s:gaz",
+	while ((opt = getopt_long(argc, argv, "t:n:d:q:r:e:i:s:gazb",
 				  longopts, &option_idx)) != -1) {
 		switch (opt) {
+		case 'b':
+			ctx.flags |= UBLK_F_BATCH_IO;
+			break;
 		case 'a':
 			ctx.all = 1;
 			break;
@@ -1737,6 +1743,11 @@ int main(int argc, char *argv[])
 		}
 	}
 
+	if (ctx.per_io_tasks && (ctx.flags & UBLK_F_BATCH_IO)) {
+		ublk_err("per_io_task and F_BATCH_IO conflict\n");
+		return -EINVAL;
+	}
+
 	/* auto_zc_fallback depends on F_AUTO_BUF_REG & F_SUPPORT_ZERO_COPY */
 	if (ctx.auto_zc_fallback &&
 	    !((ctx.flags & UBLK_F_AUTO_BUF_REG) &&
tools/testing/selftests/ublk/test_generic_14.sh new file +32 -0
new file mode 100755
index 000000000000..e197961b07f1
@@ -0,0 +1,32 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+
+. "$(cd "$(dirname "$0")" && pwd)"/test_common.sh
+
+TID="generic_14"
+ERR_CODE=0
+
+if ! _have_feature "BATCH_IO"; then
+	exit "$UBLK_SKIP_CODE"
+fi
+
+_prep_test "generic" "test basic function of UBLK_F_BATCH_IO"
+
+_create_backfile 0 256M
+_create_backfile 1 256M
+
+dev_id=$(_add_ublk_dev -t loop -q 2 -b "${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 -b --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_06.sh new file +45 -0

45 lines not on this page · raw message

tools/testing/selftests/ublk/test_stress_07.sh new file +44 -0

44 lines not on this page · raw message

Signature · 1 line
2.47.0
author
#48 lore raw

PATCH v3 27/27 selftests: ublk: support arbitrary threads/queues combination

In reply to Ming Lei

Enable flexible thread-to-queue mapping in batch I/O mode to support arbitrary combinations of threads and queues, improving resource utilization and scalability.

Key improvements:
- Support N:M thread-to-queue mapping (previously limited to 1:1)
- Dynamic buffer allocation based on actual queue assignment per thread
- Thread-safe queue preparation with spinlock protection
- Intelligent buffer index calculation for multi-queue scenarios
- Enhanced validation for thread/queue combination constraints

Implementation details:
- Add q_thread_map matrix to track queue-to-thread assignments
- Dynamic allocation of commit and fetch buffers per thread
- Round-robin queue assignment algorithm for load balancing
- Per-queue spinlock to prevent race conditions during prep
- Updated buffer index calculation using queue position within thread

This enables efficient configurations like:
- Any other N:M combinations for optimal resource matching

Testing:
- Added test_generic_15.sh: 4 threads vs 1 queue
- Added test_generic_16.sh: 1 thread vs 4 queues
- Validates correctness across different mapping scenarios

---
 tools/testing/selftests/ublk/Makefile         |   2 +
 tools/testing/selftests/ublk/batch.c          | 199 +++++++++++++++---
 tools/testing/selftests/ublk/kublk.c          |  49 ++++-
 tools/testing/selftests/ublk/kublk.h          |  40 +++-
 .../testing/selftests/ublk/test_generic_15.sh |  30 +++
 .../testing/selftests/ublk/test_generic_16.sh |  30 +++
 6 files changed, 302 insertions(+), 48 deletions(-)
 create mode 100755 tools/testing/selftests/ublk/test_generic_15.sh
 create mode 100755 tools/testing/selftests/ublk/test_generic_16.sh
tools/testing/selftests/ublk/Makefile +2 -0
index cbf57113b1a6..3dbd9a857716 100644
@@ -22,6 +22,8 @@ TEST_PROGS += test_generic_11.sh
 TEST_PROGS += test_generic_12.sh
 TEST_PROGS += test_generic_13.sh
 TEST_PROGS += test_generic_14.sh
+TEST_PROGS += test_generic_15.sh
+TEST_PROGS += test_generic_16.sh
 
 TEST_PROGS += test_null_01.sh
 TEST_PROGS += test_null_02.sh
tools/testing/selftests/ublk/batch.c +170 -29

312 lines not on this page · raw message

tools/testing/selftests/ublk/kublk.c +38 -11

126 lines not on this page · raw message

tools/testing/selftests/ublk/kublk.h +32 -8
index 222501048c24..565819cf2dfe 100644
@@ -166,12 +166,16 @@ struct ublk_queue {
 	const struct ublk_tgt_ops *tgt_ops;
 	struct ublksrv_io_desc *io_cmd_buf;
 
-/* borrow one bit of ublk uapi flags, which may never be used */
+/* borrow three bit of ublk uapi flags, which may never be used */
 #define UBLKS_Q_AUTO_BUF_REG_FALLBACK	(1ULL << 63)
 #define UBLKS_Q_NO_UBLK_FIXED_FD	(1ULL << 62)
+#define UBLKS_Q_PREPARED	(1ULL << 61)
 	__u64 flags;
 	int ublk_fd;	/* cached ublk char device fd */
 	struct ublk_io ios[UBLK_QUEUE_DEPTH];
+
+	/* used for prep io commands */
+	pthread_spinlock_t lock;
 };
 
 /* align with `ublk_elem_header` */
@@ -198,8 +202,12 @@ struct batch_fetch_buf {
 };
 
 struct ublk_thread {
+	/* Thread-local copy of queue-to-thread mapping for this thread */
+	unsigned char q_map[UBLK_MAX_QUEUES];
+
 	struct ublk_dev *dev;
-	unsigned idx;
+	unsigned short idx;
+	unsigned short nr_queues;
 
 #define UBLKS_T_STOPPING	(1U << 0)
 #define UBLKS_T_IDLE	(1U << 1)
@@ -222,10 +230,10 @@ struct ublk_thread {
 	void *commit_buf;
 #define UBLKS_T_COMMIT_BUF_INV_IDX  ((unsigned short)-1)
 	struct allocator commit_buf_alloc;
-	struct batch_commit_buf commit;
+	struct batch_commit_buf *commit;
 	/* FETCH_IO_CMDS buffer */
-#define UBLKS_T_NR_FETCH_BUF 	2
-	struct batch_fetch_buf fetch[UBLKS_T_NR_FETCH_BUF];
+	unsigned short nr_fetch_bufs;
+	struct batch_fetch_buf *fetch;
 
 	struct io_uring ring;
 };
@@ -465,6 +473,21 @@ static inline int ublk_queue_no_buf(const struct ublk_queue *q)
 	return ublk_queue_use_zc(q) || ublk_queue_use_auto_zc(q);
 }
 
+static inline int ublk_batch_commit_prepared(struct batch_commit_buf *cb)
+{
+	return cb->buf_idx != UBLKS_T_COMMIT_BUF_INV_IDX;
+}
+
+static inline unsigned ublk_queue_idx_in_thread(const struct ublk_thread *t,
+						const struct ublk_queue *q)
+{
+	unsigned char idx;
+
+	idx = t->q_map[q->q_id];
+	ublk_assert(idx != 0);
+	return idx - 1;
+}
+
 /*
  * Each IO's buffer index has to be calculated by this helper for
  * UBLKS_T_BATCH_IO
@@ -473,14 +496,13 @@ static inline unsigned short ublk_batch_io_buf_idx(
 		const struct ublk_thread *t, const struct ublk_queue *q,
 		unsigned tag)
 {
-	return tag;
+	return ublk_queue_idx_in_thread(t, q) * q->q_depth + tag;
 }
 
 /* Queue UBLK_U_IO_PREP_IO_CMDS for a specific queue with batch elements */
 int ublk_batch_queue_prep_io_cmds(struct ublk_thread *t, struct ublk_queue *q);
 /* Start fetching I/O commands using multishot UBLK_U_IO_FETCH_IO_CMDS */
-void ublk_batch_start_fetch(struct ublk_thread *t,
-			    struct ublk_queue *q);
+void ublk_batch_start_fetch(struct ublk_thread *t);
 /* Handle completion of batch I/O commands (prep/commit) */
 void ublk_batch_compl_cmd(struct ublk_thread *t,
 			  const struct io_uring_cqe *cqe);
@@ -498,6 +520,8 @@ void ublk_batch_commit_io_cmds(struct ublk_thread *t);
 /* Add a completed I/O operation to the current batch commit buffer */
 void ublk_batch_complete_io(struct ublk_thread *t, struct ublk_queue *q,
 			    unsigned tag, int res);
+void ublk_batch_setup_map(unsigned char (*q_thread_map)[UBLK_MAX_QUEUES],
+			   int nthreads, int queues);
 
 static inline int ublk_complete_io(struct ublk_thread *t, struct ublk_queue *q,
 				   unsigned tag, int res)
tools/testing/selftests/ublk/test_generic_15.sh new file +30 -0
new file mode 100755
index 000000000000..0afd037235cf
@@ -0,0 +1,30 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+
+. "$(cd "$(dirname "$0")" && pwd)"/test_common.sh
+
+TID="generic_15"
+ERR_CODE=0
+
+if ! _have_feature "BATCH_IO"; then
+	exit "$UBLK_SKIP_CODE"
+fi
+
+if ! _have_program fio; then
+	exit "$UBLK_SKIP_CODE"
+fi
+
+_prep_test "generic" "test UBLK_F_BATCH_IO with 4_threads vs. 1_queues"
+
+_create_backfile 0 512M
+
+dev_id=$(_add_ublk_dev -t loop -q 1 --nthreads 4 -b "${UBLK_BACKFILES[0]}")
+_check_add_dev $TID $?
+
+# run fio over the ublk disk
+fio --name=job1 --filename=/dev/ublkb"${dev_id}" --ioengine=libaio --rw=readwrite \
+	--iodepth=32 --size=100M --numjobs=4 > /dev/null 2>&1
+ERR_CODE=$?
+
+_cleanup_test "generic"
+_show_result $TID $ERR_CODE
tools/testing/selftests/ublk/test_generic_16.sh new file +30 -0
new file mode 100755
index 000000000000..32bcf4a3d0b4
@@ -0,0 +1,30 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+
+. "$(cd "$(dirname "$0")" && pwd)"/test_common.sh
+
+TID="generic_16"
+ERR_CODE=0
+
+if ! _have_feature "BATCH_IO"; then
+	exit "$UBLK_SKIP_CODE"
+fi
+
+if ! _have_program fio; then
+	exit "$UBLK_SKIP_CODE"
+fi
+
+_prep_test "generic" "test UBLK_F_BATCH_IO with 1_threads vs. 4_queues"
+
+_create_backfile 0 512M
+
+dev_id=$(_add_ublk_dev -t loop -q 4 --nthreads 1 -b "${UBLK_BACKFILES[0]}")
+_check_add_dev $TID $?
+
+# run fio over the ublk disk
+fio --name=job1 --filename=/dev/ublkb"${dev_id}" --ioengine=libaio --rw=readwrite \
+	--iodepth=32 --size=100M --numjobs=4 > /dev/null 2>&1
+ERR_CODE=$?
+
+_cleanup_test "generic"
+_show_result $TID $ERR_CODE
Signature · 1 line
2.47.0