[PATCH RFC 02/18] scsi: target: uring: Allocate per-queue command descriptors

From: Qiliang Yuan

Date: Sun Nov 15 2026 - 19:18:35 EST


A target_core_uring device only has a size and a block size so far.

The server needs to learn what each command asks for, and commands
submitted on different CPUs should not share state.

Add the nr_queues and queue_depth parameters and allocate that many
queues when the device is configured. Give each queue an array of
struct tcmur_io_desc, one per tag, that describes a command as an
operation, an offset and a length. Define the descriptor in a new uAPI
header and allocate the array with vmalloc_user() so that it can be
mapped later.

Signed-off-by: Qiliang Yuan <qiliang.yuan@xxxxxxxxx>
---
MAINTAINERS | 1 +
drivers/target/target_core_uring.c | 90 +++++++++++++++++++++++++++++++++-
include/uapi/linux/target_core_uring.h | 43 ++++++++++++++++
3 files changed, 133 insertions(+), 1 deletion(-)

diff --git a/MAINTAINERS b/MAINTAINERS
index 3e31de6e2ebfb..07148430d9ea6 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -25330,6 +25330,7 @@ T: git git://git.kernel.org/pub/scm/linux/kernel/git/mkp/scsi.git
F: Documentation/target/
F: drivers/target/
F: include/target/
+F: include/uapi/linux/target_core_uring.h

SCTP PROTOCOL
M: Marcelo Ricardo Leitner <marcelo.leitner@xxxxxxxxx>
diff --git a/drivers/target/target_core_uring.c b/drivers/target/target_core_uring.c
index 13981e608a85c..135435748d367 100644
--- a/drivers/target/target_core_uring.c
+++ b/drivers/target/target_core_uring.c
@@ -7,20 +7,40 @@
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

#include <linux/module.h>
+#include <linux/mm.h>
#include <linux/parser.h>
#include <linux/slab.h>
+#include <linux/vmalloc.h>
#include <target/target_core_base.h>
#include <target/target_core_backend.h>
+#include <uapi/linux/target_core_uring.h>

#define TCMUR_VERSION "1.0"
#define TCMUR_MAX_IO_SIZE (4U << 20)
+#define TCMUR_DEF_QUEUE_DEPTH 128
#define TCMUR_DEF_BLOCK_SIZE 512

+struct tcmur_dev;
+
+/**
+ * struct tcmur_queue - One command queue
+ * @descs: Command descriptors, indexed by tag
+ * @udev: Device the queue belongs to
+ */
+struct tcmur_queue {
+ struct tcmur_io_desc *descs;
+ struct tcmur_dev *udev;
+} ____cacheline_aligned_in_smp;
+
struct tcmur_dev {
struct se_device se_dev;

u64 dev_size;
u32 block_size;
+ u16 nr_queues;
+ u16 queue_depth;
+
+ struct tcmur_queue *queues;
};

static struct tcmur_dev *TCMUR_DEV(struct se_device *dev)
@@ -36,6 +56,47 @@ static sense_reason_t tcmur_execute_rw(struct se_cmd *se_cmd,
return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
}

+static size_t tcmur_desc_area_size(struct tcmur_dev *udev)
+{
+ return PAGE_ALIGN(udev->queue_depth * sizeof(struct tcmur_io_desc));
+}
+
+static void tcmur_free_queues(struct tcmur_dev *udev)
+{
+ unsigned int i;
+
+ if (!udev->queues)
+ return;
+
+ for (i = 0; i < udev->nr_queues; i++)
+ vfree(udev->queues[i].descs);
+ kvfree(udev->queues);
+ udev->queues = NULL;
+}
+
+static int tcmur_alloc_queues(struct tcmur_dev *udev)
+{
+ struct tcmur_queue *q;
+ unsigned int i;
+
+ udev->queues = kvcalloc(udev->nr_queues, sizeof(*udev->queues),
+ GFP_KERNEL);
+ if (!udev->queues)
+ return -ENOMEM;
+
+ for (i = 0; i < udev->nr_queues; i++) {
+ q = &udev->queues[i];
+
+ q->udev = udev;
+ q->descs = vmalloc_user(tcmur_desc_area_size(udev));
+ if (!q->descs) {
+ tcmur_free_queues(udev);
+ return -ENOMEM;
+ }
+ }
+ return 0;
+}
+
static void tcmur_dev_call_rcu(struct rcu_head *p)
{
struct se_device *dev = container_of(p, struct se_device, rcu_head);
@@ -61,6 +122,8 @@ static struct se_device *tcmur_alloc_device(struct se_hba *hba,
if (!udev)
return NULL;

+ udev->nr_queues = 1;
+ udev->queue_depth = TCMUR_DEF_QUEUE_DEPTH;
udev->block_size = TCMUR_DEF_BLOCK_SIZE;

return &udev->se_dev;
@@ -69,14 +132,20 @@ static struct se_device *tcmur_alloc_device(struct se_hba *hba,
static int tcmur_configure_device(struct se_device *dev)
{
struct tcmur_dev *udev = TCMUR_DEV(dev);
+ int ret;

if (!udev->dev_size || (udev->dev_size & (udev->block_size - 1))) {
pr_err("dev_size must be a multiple of the block size\n");
return -EINVAL;
}

+ ret = tcmur_alloc_queues(udev);
+ if (ret)
+ return ret;
+
dev->dev_attrib.hw_block_size = udev->block_size;
dev->dev_attrib.hw_max_sectors = TCMUR_MAX_IO_SIZE / udev->block_size;
+ dev->dev_attrib.hw_queue_depth = udev->nr_queues * udev->queue_depth;
dev->dev_attrib.is_nonrot = 1;

return 0;
@@ -84,6 +153,9 @@ static int tcmur_configure_device(struct se_device *dev)

static void tcmur_destroy_device(struct se_device *dev)
{
+ struct tcmur_dev *udev = TCMUR_DEV(dev);
+
+ tcmur_free_queues(udev);
}

static void tcmur_free_device(struct se_device *dev)
@@ -92,12 +164,14 @@ static void tcmur_free_device(struct se_device *dev)
}

enum {
- Opt_dev_size, Opt_block_size, Opt_err,
+ Opt_dev_size, Opt_block_size, Opt_nr_queues, Opt_queue_depth, Opt_err,
};

static const match_table_t tcmur_tokens = {
{ Opt_dev_size, "dev_size=%s" },
{ Opt_block_size, "block_size=%u" },
+ { Opt_nr_queues, "nr_queues=%u" },
+ { Opt_queue_depth, "queue_depth=%u" },
{ Opt_err, NULL }
};

@@ -141,6 +215,18 @@ static ssize_t tcmur_set_configfs_dev_params(struct se_device *dev,
else
udev->block_size = val;
break;
+ case Opt_nr_queues:
+ if (!val || val > TCMUR_MAX_NR_QUEUES)
+ ret = -EINVAL;
+ else
+ udev->nr_queues = val;
+ break;
+ case Opt_queue_depth:
+ if (!val || val > TCMUR_MAX_QUEUE_DEPTH)
+ ret = -EINVAL;
+ else
+ udev->queue_depth = val;
+ break;
}
}

@@ -155,6 +241,8 @@ static ssize_t tcmur_show_configfs_dev_params(struct se_device *dev, char *b)

bl += sprintf(b + bl, "Size: %llu Block size: %u", udev->dev_size,
udev->block_size);
+ bl += sprintf(b + bl, " Queues: %u Queue depth: %u", udev->nr_queues,
+ udev->queue_depth);
bl += sprintf(b + bl, "\n");
return bl;
}
diff --git a/include/uapi/linux/target_core_uring.h b/include/uapi/linux/target_core_uring.h
new file mode 100644
index 0000000000000..64fa1dfcd011f
--- /dev/null
+++ b/include/uapi/linux/target_core_uring.h
@@ -0,0 +1,43 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+#ifndef _UAPI_LINUX_TARGET_CORE_URING_H
+#define _UAPI_LINUX_TARGET_CORE_URING_H
+
+#include <linux/types.h>
+
+/*
+ * target_core_uring hands the block commands of a LIO device to a userspace
+ * server.
+ */
+
+#define TCMUR_MAX_NR_QUEUES 512
+#define TCMUR_MAX_QUEUE_DEPTH 4096
+
+enum tcmur_io_op {
+ TCMUR_OP_READ,
+ TCMUR_OP_WRITE,
+};
+
+/* Put the write on stable storage before completing it */
+#define TCMUR_IO_F_FUA (1U << 0)
+
+/**
+ * struct tcmur_io_desc - Describe one command
+ * @op: Operation, one of enum tcmur_io_op
+ * @flags: TCMUR_IO_F_*
+ * @offset: Start of the range in bytes
+ * @len: Length of the range in bytes
+ * @data_len: Bytes of data to transfer
+ * @reserved: Zero
+ *
+ * The descriptors of a queue form an array indexed by tag.
+ */
+struct tcmur_io_desc {
+ __u32 op;
+ __u32 flags;
+ __u64 offset;
+ __u64 len;
+ __u32 data_len;
+ __u32 reserved;
+};
+
+#endif /* _UAPI_LINUX_TARGET_CORE_URING_H */

--
2.43.0