Linux io-uring development
 help / color / mirror / Atom feed
* [PATCH RFC 00/18] scsi: target: Add target_core_uring, an io_uring based user backstore
@ 2026-11-16  0:17 Qiliang Yuan
  2026-11-16  0:17 ` [PATCH RFC 01/18] scsi: target: uring: Introduce " Qiliang Yuan
                   ` (17 more replies)
  0 siblings, 18 replies; 19+ messages in thread
From: Qiliang Yuan @ 2026-11-16  0:17 UTC (permalink / raw)
  To: Martin K. Petersen, Shuah Khan, Jonathan Corbet, Shuah Khan,
	Randy Dunlap
  Cc: linux-kernel, linux-scsi, target-devel, linux-kselftest,
	linux-doc, Jens Axboe, io-uring, Mike Christie, Bodo Stroesser,
	Ming Lei, Qiliang Yuan

TCM/USER is how a userspace process serves a LIO device, for instance
to export distributed storage over iSCSI or vhost-scsi. All commands of
a device go through one command ring under one mutex, the data of each
command is copied into and out of a shared data area, and the server
has to parse every SCSI CDB itself.

This series adds target_core_uring, a second user backstore next to
TCM/USER, built the way ublk is:

- The kernel emulates the SCSI commands. The server only sees reads,
  writes, flushes, UNMAP and WRITE SAME as an operation, an offset and a
  length.
- A device has several queues. Each queue is served through io_uring
  commands on its own ring, with no lock shared between queues.
- Data is copied once, with pread()/pwrite() on the character device, or
  not at all when the server registers the pages of a command as an
  io_uring fixed buffer.

Patches 1-4 create the device, its queues and the character device.
Patches 5-8 pass reads and writes to the server. Patches 9-11 handle a
server that exits or hangs, and aborts. Patches 12-14 add flush, UNMAP
and WRITE SAME, patch 15 adds fixed buffers. Patches 16-18 add a small
server, a selftest and the documentation.

Not covered yet: protection information, and a way for existing
tcmu-runner handlers to move over without changes, as they implement
SCSI rather than block commands.

Exporting a 2 GiB device through tcm_loop to fio (io_uring, direct I/O,
iodepth 32 for 4k and 8 for 1M, 6 seconds per job) on a 32 CPU VM, with
TCM/USER and a single-threaded handler, then with this series, 32 queues
and one server thread per queue. Both servers keep the data in memory.
A single job is slower; the gain comes with concurrency and with large
I/O. Three alternating runs (median, min-max in parentheses):

  workload               TCM/USER (MB/s)   uring (MB/s)          change
  4k randread, 1 job      426 (409-463)      369 (366-384)         -13%
  4k randread, 4 jobs    1165 (1072-1253)   1524 (1413-1541)       +31%
  4k randread, 16 jobs    920 (916-1162)    6193 (5845-6222)      +574%
  4k randwrite, 16 jobs   793 (711-886)     5701 (5257-5706)      +619%
  1M read, 4 jobs        6019 (5857-6047)  49460 (46706-57997)    +722%
  1M write, 4 jobs       2791 (2649-2907)  26692 (25958-27560)    +856%

Signed-off-by: Qiliang Yuan <qiliang.yuan@linux.dev>
---
Qiliang Yuan (18):
      scsi: target: uring: Introduce an io_uring based user backstore
      scsi: target: uring: Allocate per-queue command descriptors
      scsi: target: uring: Create a character device per backstore device
      scsi: target: uring: Map the command descriptors into the server
      scsi: target: uring: Queue commands until a server fetches them
      scsi: target: uring: Hand commands out through TCMUR_IO_FETCH
      scsi: target: uring: Complete commands with TCMUR_IO_COMMIT_AND_FETCH
      scsi: target: uring: Copy command data with pread() and pwrite()
      scsi: target: uring: Fail in-flight commands when the server exits
      scsi: target: uring: Time out commands the server does not complete
      scsi: target: uring: Drop aborted commands that are still queued
      scsi: target: uring: Support SYNCHRONIZE CACHE
      scsi: target: uring: Implement UNMAP
      scsi: target: uring: Handle WRITE SAME
      scsi: target: uring: Register command data as io_uring fixed buffers
      selftests: target_core_uring: Add a minimal server
      selftests: target_core_uring: Test a device exported through tcm_loop
      Documentation: target: Describe the uring backstore

 Documentation/target/index.rst                     |    1 +
 Documentation/target/tcm-uring.rst                 |   84 ++
 MAINTAINERS                                        |    2 +
 drivers/target/Kconfig                             |   10 +
 drivers/target/Makefile                            |    1 +
 drivers/target/target_core_uring.c                 | 1126 ++++++++++++++++++++
 include/uapi/linux/target_core_uring.h             |  126 +++
 tools/testing/selftests/Makefile                   |    1 +
 .../testing/selftests/target_core_uring/.gitignore |    1 +
 tools/testing/selftests/target_core_uring/Makefile |    8 +
 tools/testing/selftests/target_core_uring/config   |    5 +
 .../selftests/target_core_uring/tcmur_server.c     |  310 ++++++
 .../selftests/target_core_uring/tcmur_test.sh      |  137 +++
 13 files changed, 1812 insertions(+)
---
base-commit: 23c62304dde29abe92013d85910d03a77dada35d
change-id: 20261115-feat-target-core-uring-d1351dfe5fe1

Best regards,
-- 
Qiliang Yuan <qiliang.yuan@linux.dev>


^ permalink raw reply	[flat|nested] 19+ messages in thread

* [PATCH RFC 01/18] scsi: target: uring: Introduce an io_uring based user backstore
  2026-11-16  0:17 [PATCH RFC 00/18] scsi: target: Add target_core_uring, an io_uring based user backstore Qiliang Yuan
@ 2026-11-16  0:17 ` Qiliang Yuan
  2026-11-16  0:17 ` [PATCH RFC 02/18] scsi: target: uring: Allocate per-queue command descriptors Qiliang Yuan
                   ` (16 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Qiliang Yuan @ 2026-11-16  0:17 UTC (permalink / raw)
  To: Martin K. Petersen, Shuah Khan, Jonathan Corbet, Shuah Khan,
	Randy Dunlap
  Cc: linux-kernel, linux-scsi, target-devel, linux-kselftest,
	linux-doc, Jens Axboe, io-uring, Mike Christie, Bodo Stroesser,
	Ming Lei, Qiliang Yuan

TCM/USER lets a userspace process serve the commands of a LIO device,
which is how distributed storage is exported over iSCSI or vhost-scsi.

All commands of a TCM/USER device go through one command ring under one
mutex, so throughput does not scale with the number of submitters. The
data of each command is copied into and out of a shared data area, and
the server has to parse every SCSI CDB itself.

Start a new backstore, target_core_uring, that will hand block commands
to the server through io_uring commands on several queues. Register the
backstore and let configfs create devices with a size and a block size.
Let the core emulate the SCSI commands through sbc_parse_cdb(), and fail
reads and writes until a server can pick them up.

Signed-off-by: Qiliang Yuan <qiliang.yuan@linux.dev>
---
 drivers/target/Kconfig             |  10 ++
 drivers/target/Makefile            |   1 +
 drivers/target/target_core_uring.c | 214 +++++++++++++++++++++++++++++++++++++
 3 files changed, 225 insertions(+)

diff --git a/drivers/target/Kconfig b/drivers/target/Kconfig
index 92641d39126a3..545a1d4cf37e5 100644
--- a/drivers/target/Kconfig
+++ b/drivers/target/Kconfig
@@ -43,6 +43,16 @@ config TCM_USER2
 	process to handle requests. This is version 2 of the ABI; version 1
 	is obsolete.
 
+config TCM_URING
+	tristate "TCM/URING Subsystem Plugin for Linux"
+	depends on IO_URING
+	help
+	  Say Y here to enable the TCM/URING subsystem plugin, which lets a
+	  userspace server handle the block commands of a device through
+	  io_uring commands. Unlike TCM/USER, the kernel emulates the SCSI
+	  commands and the server only reads, writes, flushes and unmaps
+	  blocks, on several queues and without copying data twice.
+
 source "drivers/target/loopback/Kconfig"
 source "drivers/target/tcm_fc/Kconfig"
 source "drivers/target/iscsi/Kconfig"
diff --git a/drivers/target/Makefile b/drivers/target/Makefile
index 431b84abfb94e..f9d81430f4b5b 100644
--- a/drivers/target/Makefile
+++ b/drivers/target/Makefile
@@ -24,6 +24,7 @@ obj-$(CONFIG_TCM_IBLOCK)	+= target_core_iblock.o
 obj-$(CONFIG_TCM_FILEIO)	+= target_core_file.o
 obj-$(CONFIG_TCM_PSCSI)		+= target_core_pscsi.o
 obj-$(CONFIG_TCM_USER2)		+= target_core_user.o
+obj-$(CONFIG_TCM_URING)		+= target_core_uring.o
 
 # Fabric modules
 obj-$(CONFIG_LOOPBACK_TARGET)	+= loopback/
diff --git a/drivers/target/target_core_uring.c b/drivers/target/target_core_uring.c
new file mode 100644
index 0000000000000..13981e608a85c
--- /dev/null
+++ b/drivers/target/target_core_uring.c
@@ -0,0 +1,214 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * LIO backstore that hands block commands to a userspace server through
+ * io_uring commands.
+ */
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include <linux/module.h>
+#include <linux/parser.h>
+#include <linux/slab.h>
+#include <target/target_core_base.h>
+#include <target/target_core_backend.h>
+
+#define TCMUR_VERSION		"1.0"
+#define TCMUR_MAX_IO_SIZE	(4U << 20)
+#define TCMUR_DEF_BLOCK_SIZE	512
+
+struct tcmur_dev {
+	struct se_device	se_dev;
+
+	u64			dev_size;
+	u32			block_size;
+};
+
+static struct tcmur_dev *TCMUR_DEV(struct se_device *dev)
+{
+	return container_of(dev, struct tcmur_dev, se_dev);
+}
+
+static sense_reason_t tcmur_execute_rw(struct se_cmd *se_cmd,
+				       struct scatterlist *sgl, u32 sgl_nents,
+				       enum dma_data_direction dir)
+{
+	/* Fail the commands until a server can pick them up */
+	return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
+}
+
+static void tcmur_dev_call_rcu(struct rcu_head *p)
+{
+	struct se_device *dev = container_of(p, struct se_device, rcu_head);
+
+	kfree(TCMUR_DEV(dev));
+}
+
+static int tcmur_attach_hba(struct se_hba *hba, u32 host_id)
+{
+	return 0;
+}
+
+static void tcmur_detach_hba(struct se_hba *hba)
+{
+}
+
+static struct se_device *tcmur_alloc_device(struct se_hba *hba,
+					    const char *name)
+{
+	struct tcmur_dev *udev;
+
+	udev = kzalloc_obj(*udev);
+	if (!udev)
+		return NULL;
+
+	udev->block_size = TCMUR_DEF_BLOCK_SIZE;
+
+	return &udev->se_dev;
+}
+
+static int tcmur_configure_device(struct se_device *dev)
+{
+	struct tcmur_dev *udev = TCMUR_DEV(dev);
+
+	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;
+	}
+
+	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.is_nonrot = 1;
+
+	return 0;
+}
+
+static void tcmur_destroy_device(struct se_device *dev)
+{
+}
+
+static void tcmur_free_device(struct se_device *dev)
+{
+	call_rcu(&dev->rcu_head, tcmur_dev_call_rcu);
+}
+
+enum {
+	Opt_dev_size, Opt_block_size, Opt_err,
+};
+
+static const match_table_t tcmur_tokens = {
+	{ Opt_dev_size,		"dev_size=%s" },
+	{ Opt_block_size,	"block_size=%u" },
+	{ Opt_err,		NULL }
+};
+
+static ssize_t tcmur_set_configfs_dev_params(struct se_device *dev,
+					     const char *page, ssize_t count)
+{
+	struct tcmur_dev *udev = TCMUR_DEV(dev);
+	substring_t args[MAX_OPT_ARGS];
+	char *orig, *ptr, *opts;
+	int ret = 0, token, val;
+	u64 size;
+
+	if (target_dev_configured(dev))
+		return -EBUSY;
+
+	opts = kstrdup(page, GFP_KERNEL);
+	if (!opts)
+		return -ENOMEM;
+	orig = opts;
+
+	while (!ret && (ptr = strsep(&opts, ",\n")) != NULL) {
+		if (!*ptr)
+			continue;
+
+		token = match_token(ptr, tcmur_tokens, args);
+		if (token == Opt_dev_size) {
+			ret = match_u64(&args[0], &size);
+			if (!ret)
+				udev->dev_size = size;
+			continue;
+		}
+		if (token == Opt_err || match_int(&args[0], &val) || val < 0) {
+			ret = -EINVAL;
+			break;
+		}
+
+		switch (token) {
+		case Opt_block_size:
+			if (val < 512 || val > PAGE_SIZE || !is_power_of_2(val))
+				ret = -EINVAL;
+			else
+				udev->block_size = val;
+			break;
+		}
+	}
+
+	kfree(orig);
+	return ret ? ret : count;
+}
+
+static ssize_t tcmur_show_configfs_dev_params(struct se_device *dev, char *b)
+{
+	struct tcmur_dev *udev = TCMUR_DEV(dev);
+	ssize_t bl = 0;
+
+	bl += sprintf(b + bl, "Size: %llu Block size: %u", udev->dev_size,
+		      udev->block_size);
+	bl += sprintf(b + bl, "\n");
+	return bl;
+}
+
+static sector_t tcmur_get_blocks(struct se_device *dev)
+{
+	struct tcmur_dev *udev = TCMUR_DEV(dev);
+
+	return div_u64(udev->dev_size, dev->dev_attrib.block_size) - 1;
+}
+
+static struct exec_cmd_ops tcmur_exec_cmd_ops = {
+	.execute_rw		= tcmur_execute_rw,
+};
+
+static sense_reason_t tcmur_parse_cdb(struct se_cmd *se_cmd)
+{
+	return sbc_parse_cdb(se_cmd, &tcmur_exec_cmd_ops);
+}
+
+static const struct target_backend_ops tcmur_ops = {
+	.name			= "uring",
+	.inquiry_prod		= "URING",
+	.inquiry_rev		= TCMUR_VERSION,
+	.owner			= THIS_MODULE,
+	.attach_hba		= tcmur_attach_hba,
+	.detach_hba		= tcmur_detach_hba,
+	.alloc_device		= tcmur_alloc_device,
+	.configure_device	= tcmur_configure_device,
+	.destroy_device		= tcmur_destroy_device,
+	.free_device		= tcmur_free_device,
+	.parse_cdb		= tcmur_parse_cdb,
+	.set_configfs_dev_params = tcmur_set_configfs_dev_params,
+	.show_configfs_dev_params = tcmur_show_configfs_dev_params,
+	.get_device_type	= sbc_get_device_type,
+	.get_blocks		= tcmur_get_blocks,
+	.tb_dev_attrib_attrs	= sbc_attrib_attrs,
+};
+
+static int __init tcmur_module_init(void)
+{
+	return transport_backend_register(&tcmur_ops);
+}
+
+static void __exit tcmur_module_exit(void)
+{
+	target_backend_unregister(&tcmur_ops);
+	/* Wait for the devices that tcmur_free_device() frees with RCU */
+	rcu_barrier();
+}
+
+module_init(tcmur_module_init);
+module_exit(tcmur_module_exit);
+
+MODULE_DESCRIPTION("TCM io_uring subsystem plugin");
+MODULE_AUTHOR("Qiliang Yuan <qiliang.yuan@linux.dev>");
+MODULE_LICENSE("GPL");

-- 
2.43.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH RFC 02/18] scsi: target: uring: Allocate per-queue command descriptors
  2026-11-16  0:17 [PATCH RFC 00/18] scsi: target: Add target_core_uring, an io_uring based user backstore Qiliang Yuan
  2026-11-16  0:17 ` [PATCH RFC 01/18] scsi: target: uring: Introduce " Qiliang Yuan
@ 2026-11-16  0:17 ` Qiliang Yuan
  2026-11-16  0:17 ` [PATCH RFC 03/18] scsi: target: uring: Create a character device per backstore device Qiliang Yuan
                   ` (15 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Qiliang Yuan @ 2026-11-16  0:17 UTC (permalink / raw)
  To: Martin K. Petersen, Shuah Khan, Jonathan Corbet, Shuah Khan,
	Randy Dunlap
  Cc: linux-kernel, linux-scsi, target-devel, linux-kselftest,
	linux-doc, Jens Axboe, io-uring, Mike Christie, Bodo Stroesser,
	Ming Lei, Qiliang Yuan

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@linux.dev>
---
 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@gmail.com>
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


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH RFC 03/18] scsi: target: uring: Create a character device per backstore device
  2026-11-16  0:17 [PATCH RFC 00/18] scsi: target: Add target_core_uring, an io_uring based user backstore Qiliang Yuan
  2026-11-16  0:17 ` [PATCH RFC 01/18] scsi: target: uring: Introduce " Qiliang Yuan
  2026-11-16  0:17 ` [PATCH RFC 02/18] scsi: target: uring: Allocate per-queue command descriptors Qiliang Yuan
@ 2026-11-16  0:17 ` Qiliang Yuan
  2026-11-16  0:17 ` [PATCH RFC 04/18] scsi: target: uring: Map the command descriptors into the server Qiliang Yuan
                   ` (14 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Qiliang Yuan @ 2026-11-16  0:17 UTC (permalink / raw)
  To: Martin K. Petersen, Shuah Khan, Jonathan Corbet, Shuah Khan,
	Randy Dunlap
  Cc: linux-kernel, linux-scsi, target-devel, linux-kselftest,
	linux-doc, Jens Axboe, io-uring, Mike Christie, Bodo Stroesser,
	Ming Lei, Qiliang Yuan

A target_core_uring device has queues and command descriptors, but
userspace has no handle on it.

The server needs a file to issue io_uring commands on, and the state of
the device has to stay around for as long as that file is open, which
can be longer than the configfs item lives.

Create /dev/tcmurN when a device is configured and show its name in the
info file. Allow one opener at a time, with CAP_SYS_ADMIN. Tie the
lifetime of the device to the embedded struct device, so that the queues
are freed when the last reference to the character device goes. Report
the geometry of the device through the TCMUR_GET_DEV_INFO ioctl.

Signed-off-by: Qiliang Yuan <qiliang.yuan@linux.dev>
---
 drivers/target/target_core_uring.c     | 138 ++++++++++++++++++++++++++++++++-
 include/uapi/linux/target_core_uring.h |  27 ++++++-
 2 files changed, 160 insertions(+), 5 deletions(-)

diff --git a/drivers/target/target_core_uring.c b/drivers/target/target_core_uring.c
index 135435748d367..d1ca4f16e7d0b 100644
--- a/drivers/target/target_core_uring.c
+++ b/drivers/target/target_core_uring.c
@@ -7,6 +7,9 @@
 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 
 #include <linux/module.h>
+#include <linux/cdev.h>
+#include <linux/device.h>
+#include <linux/idr.h>
 #include <linux/mm.h>
 #include <linux/parser.h>
 #include <linux/slab.h>
@@ -16,10 +19,14 @@
 #include <uapi/linux/target_core_uring.h>
 
 #define TCMUR_VERSION		"1.0"
+#define TCMUR_MINORS		(1U << MINORBITS)
 #define TCMUR_MAX_IO_SIZE	(4U << 20)
 #define TCMUR_DEF_QUEUE_DEPTH	128
 #define TCMUR_DEF_BLOCK_SIZE	512
 
+/* Set while the file is open, to allow one server at a time */
+#define TCMUR_DEV_OPEN		0
+
 struct tcmur_dev;
 
 /**
@@ -34,6 +41,10 @@ struct tcmur_queue {
 
 struct tcmur_dev {
 	struct se_device	se_dev;
+	struct device		cdev_dev;
+	struct cdev		cdev;
+	int			dev_id;
+	unsigned long		flags;
 
 	u64			dev_size;
 	u32			block_size;
@@ -43,6 +54,13 @@ struct tcmur_dev {
 	struct tcmur_queue	*queues;
 };
 
+static DEFINE_IDA(tcmur_ida);
+static dev_t tcmur_devt;
+
+static const struct class tcmur_class = {
+	.name = "tcm-uring",
+};
+
 static struct tcmur_dev *TCMUR_DEV(struct se_device *dev)
 {
 	return container_of(dev, struct tcmur_dev, se_dev);
@@ -61,6 +79,56 @@ static size_t tcmur_desc_area_size(struct tcmur_dev *udev)
 	return PAGE_ALIGN(udev->queue_depth * sizeof(struct tcmur_io_desc));
 }
 
+static long tcmur_ch_ioctl(struct file *filp, unsigned int cmd,
+			   unsigned long arg)
+{
+	struct tcmur_dev *udev = filp->private_data;
+	struct tcmur_dev_info info = {
+		.dev_size	= udev->dev_size,
+		.block_size	= udev->block_size,
+		.max_io_size	= TCMUR_MAX_IO_SIZE,
+		.nr_queues	= udev->nr_queues,
+		.queue_depth	= udev->queue_depth,
+	};
+
+	if (cmd != TCMUR_GET_DEV_INFO)
+		return -ENOTTY;
+
+	if (copy_to_user((void __user *)arg, &info, sizeof(info)))
+		return -EFAULT;
+	return 0;
+}
+
+static int tcmur_ch_open(struct inode *inode, struct file *filp)
+{
+	struct tcmur_dev *udev = container_of(inode->i_cdev, struct tcmur_dev,
+					      cdev);
+
+	if (!capable(CAP_SYS_ADMIN))
+		return -EPERM;
+	if (test_and_set_bit(TCMUR_DEV_OPEN, &udev->flags))
+		return -EBUSY;
+
+	filp->private_data = udev;
+	return 0;
+}
+
+static int tcmur_ch_release(struct inode *inode, struct file *filp)
+{
+	struct tcmur_dev *udev = filp->private_data;
+
+	clear_bit(TCMUR_DEV_OPEN, &udev->flags);
+	return 0;
+}
+
+static const struct file_operations tcmur_ch_fops = {
+	.owner		= THIS_MODULE,
+	.open		= tcmur_ch_open,
+	.release	= tcmur_ch_release,
+	.unlocked_ioctl	= tcmur_ch_ioctl,
+	.compat_ioctl	= compat_ptr_ioctl,
+};
+
 static void tcmur_free_queues(struct tcmur_dev *udev)
 {
 	unsigned int i;
@@ -104,6 +172,16 @@ static void tcmur_dev_call_rcu(struct rcu_head *p)
 	kfree(TCMUR_DEV(dev));
 }
 
+static void tcmur_cdev_release(struct device *dev)
+{
+	struct tcmur_dev *udev = container_of(dev, struct tcmur_dev, cdev_dev);
+
+	tcmur_free_queues(udev);
+	if (udev->dev_id >= 0)
+		ida_free(&tcmur_ida, udev->dev_id);
+	call_rcu(&udev->se_dev.rcu_head, tcmur_dev_call_rcu);
+}
+
 static int tcmur_attach_hba(struct se_hba *hba, u32 host_id)
 {
 	return 0;
@@ -122,10 +200,15 @@ static struct se_device *tcmur_alloc_device(struct se_hba *hba,
 	if (!udev)
 		return NULL;
 
+	udev->dev_id = -1;
 	udev->nr_queues = 1;
 	udev->queue_depth = TCMUR_DEF_QUEUE_DEPTH;
 	udev->block_size = TCMUR_DEF_BLOCK_SIZE;
 
+	device_initialize(&udev->cdev_dev);
+	udev->cdev_dev.class = &tcmur_class;
+	udev->cdev_dev.release = tcmur_cdev_release;
+
 	return &udev->se_dev;
 }
 
@@ -143,24 +226,47 @@ static int tcmur_configure_device(struct se_device *dev)
 	if (ret)
 		return ret;
 
+	ret = ida_alloc_max(&tcmur_ida, TCMUR_MINORS - 1, GFP_KERNEL);
+	if (ret < 0)
+		goto free_queues;
+	udev->dev_id = 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;
 
+	udev->cdev_dev.devt = MKDEV(MAJOR(tcmur_devt), udev->dev_id);
+	ret = dev_set_name(&udev->cdev_dev, "tcmur%d", udev->dev_id);
+	if (ret)
+		goto free_id;
+
+	cdev_init(&udev->cdev, &tcmur_ch_fops);
+	udev->cdev.owner = THIS_MODULE;
+	ret = cdev_device_add(&udev->cdev, &udev->cdev_dev);
+	if (ret)
+		goto free_id;
+
 	return 0;
+
+free_id:
+	ida_free(&tcmur_ida, udev->dev_id);
+	udev->dev_id = -1;
+free_queues:
+	tcmur_free_queues(udev);
+	return ret;
 }
 
 static void tcmur_destroy_device(struct se_device *dev)
 {
 	struct tcmur_dev *udev = TCMUR_DEV(dev);
 
-	tcmur_free_queues(udev);
+	cdev_device_del(&udev->cdev, &udev->cdev_dev);
 }
 
 static void tcmur_free_device(struct se_device *dev)
 {
-	call_rcu(&dev->rcu_head, tcmur_dev_call_rcu);
+	put_device(&TCMUR_DEV(dev)->cdev_dev);
 }
 
 enum {
@@ -239,6 +345,8 @@ static ssize_t tcmur_show_configfs_dev_params(struct se_device *dev, char *b)
 	struct tcmur_dev *udev = TCMUR_DEV(dev);
 	ssize_t bl = 0;
 
+	if (udev->dev_id >= 0)
+		bl += sprintf(b + bl, "Device: /dev/tcmur%d ", udev->dev_id);
 	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,
@@ -284,13 +392,35 @@ static const struct target_backend_ops tcmur_ops = {
 
 static int __init tcmur_module_init(void)
 {
-	return transport_backend_register(&tcmur_ops);
+	int ret;
+
+	ret = alloc_chrdev_region(&tcmur_devt, 0, TCMUR_MINORS, "tcm-uring");
+	if (ret)
+		return ret;
+
+	ret = class_register(&tcmur_class);
+	if (ret)
+		goto unregister_chrdev;
+
+	ret = transport_backend_register(&tcmur_ops);
+	if (ret)
+		goto unregister_class;
+
+	return 0;
+
+unregister_class:
+	class_unregister(&tcmur_class);
+unregister_chrdev:
+	unregister_chrdev_region(tcmur_devt, TCMUR_MINORS);
+	return ret;
 }
 
 static void __exit tcmur_module_exit(void)
 {
 	target_backend_unregister(&tcmur_ops);
-	/* Wait for the devices that tcmur_free_device() frees with RCU */
+	class_unregister(&tcmur_class);
+	unregister_chrdev_region(tcmur_devt, TCMUR_MINORS);
+	/* Wait for the devices that tcmur_cdev_release() frees with RCU */
 	rcu_barrier();
 }
 
diff --git a/include/uapi/linux/target_core_uring.h b/include/uapi/linux/target_core_uring.h
index 64fa1dfcd011f..c23e01e7638a2 100644
--- a/include/uapi/linux/target_core_uring.h
+++ b/include/uapi/linux/target_core_uring.h
@@ -3,15 +3,40 @@
 #define _UAPI_LINUX_TARGET_CORE_URING_H
 
 #include <linux/types.h>
+#include <linux/ioctl.h>
 
 /*
  * target_core_uring hands the block commands of a LIO device to a userspace
- * server.
+ * server. The server opens /dev/tcmurN to drive the device.
  */
 
 #define TCMUR_MAX_NR_QUEUES	512
 #define TCMUR_MAX_QUEUE_DEPTH	4096
 
+/**
+ * struct tcmur_dev_info - Geometry of a device
+ * @dev_size:		Size of the device in bytes
+ * @block_size:		Logical block size in bytes
+ * @max_io_size:	Largest amount of data in one command, in bytes
+ * @nr_queues:		Number of queues
+ * @queue_depth:	Number of tags per queue
+ * @reserved0:		Zero
+ * @features:		Zero for now
+ * @reserved1:		Zero
+ */
+struct tcmur_dev_info {
+	__u64	dev_size;
+	__u32	block_size;
+	__u32	max_io_size;
+	__u16	nr_queues;
+	__u16	queue_depth;
+	__u32	reserved0;
+	__u64	features;
+	__u64	reserved1[4];
+};
+
+#define TCMUR_GET_DEV_INFO	_IOR(0xBB, 0x00, struct tcmur_dev_info)
+
 enum tcmur_io_op {
 	TCMUR_OP_READ,
 	TCMUR_OP_WRITE,

-- 
2.43.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH RFC 04/18] scsi: target: uring: Map the command descriptors into the server
  2026-11-16  0:17 [PATCH RFC 00/18] scsi: target: Add target_core_uring, an io_uring based user backstore Qiliang Yuan
                   ` (2 preceding siblings ...)
  2026-11-16  0:17 ` [PATCH RFC 03/18] scsi: target: uring: Create a character device per backstore device Qiliang Yuan
@ 2026-11-16  0:17 ` Qiliang Yuan
  2026-11-16  0:17 ` [PATCH RFC 05/18] scsi: target: uring: Queue commands until a server fetches them Qiliang Yuan
                   ` (13 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Qiliang Yuan @ 2026-11-16  0:17 UTC (permalink / raw)
  To: Martin K. Petersen, Shuah Khan, Jonathan Corbet, Shuah Khan,
	Randy Dunlap
  Cc: linux-kernel, linux-scsi, target-devel, linux-kselftest,
	linux-doc, Jens Axboe, io-uring, Mike Christie, Bodo Stroesser,
	Ming Lei, Qiliang Yuan

Each queue of a target_core_uring device has an array of command
descriptors that the kernel fills.

The server cannot read the descriptors yet.

Let the server mmap() the descriptors of queue q_id at offset
q_id * TCMUR_DESC_AREA_SIZE of the character device. Refuse writable
mappings, as only the kernel writes descriptors.

Signed-off-by: Qiliang Yuan <qiliang.yuan@linux.dev>
---
 drivers/target/target_core_uring.c     | 22 +++++++++++++++++++++-
 include/uapi/linux/target_core_uring.h |  6 +++++-
 2 files changed, 26 insertions(+), 2 deletions(-)

diff --git a/drivers/target/target_core_uring.c b/drivers/target/target_core_uring.c
index d1ca4f16e7d0b..904ce2a613618 100644
--- a/drivers/target/target_core_uring.c
+++ b/drivers/target/target_core_uring.c
@@ -31,7 +31,7 @@ struct tcmur_dev;
 
 /**
  * struct tcmur_queue - One command queue
- * @descs:	Command descriptors, indexed by tag
+ * @descs:	Command descriptors, indexed by tag and mapped by the server
  * @udev:	Device the queue belongs to
  */
 struct tcmur_queue {
@@ -79,6 +79,25 @@ static size_t tcmur_desc_area_size(struct tcmur_dev *udev)
 	return PAGE_ALIGN(udev->queue_depth * sizeof(struct tcmur_io_desc));
 }
 
+static int tcmur_ch_mmap(struct file *filp, struct vm_area_struct *vma)
+{
+	struct tcmur_dev *udev = filp->private_data;
+	u64 off = (u64)vma->vm_pgoff << PAGE_SHIFT;
+	u32 rem;
+	u64 q_id = div_u64_rem(off, TCMUR_DESC_AREA_SIZE, &rem);
+
+	if (rem || q_id >= udev->nr_queues ||
+	    vma->vm_end - vma->vm_start != tcmur_desc_area_size(udev))
+		return -EINVAL;
+
+	/* Refuse writable mappings, as only the kernel writes descriptors */
+	if (vma->vm_flags & VM_WRITE)
+		return -EPERM;
+	vm_flags_clear(vma, VM_MAYWRITE);
+
+	return remap_vmalloc_range(vma, udev->queues[q_id].descs, 0);
+}
+
 static long tcmur_ch_ioctl(struct file *filp, unsigned int cmd,
 			   unsigned long arg)
 {
@@ -125,6 +144,7 @@ static const struct file_operations tcmur_ch_fops = {
 	.owner		= THIS_MODULE,
 	.open		= tcmur_ch_open,
 	.release	= tcmur_ch_release,
+	.mmap		= tcmur_ch_mmap,
 	.unlocked_ioctl	= tcmur_ch_ioctl,
 	.compat_ioctl	= compat_ptr_ioctl,
 };
diff --git a/include/uapi/linux/target_core_uring.h b/include/uapi/linux/target_core_uring.h
index c23e01e7638a2..d2b8ffe40bf7a 100644
--- a/include/uapi/linux/target_core_uring.h
+++ b/include/uapi/linux/target_core_uring.h
@@ -54,7 +54,8 @@ enum tcmur_io_op {
  * @data_len:	Bytes of data to transfer
  * @reserved:	Zero
  *
- * The descriptors of a queue form an array indexed by tag.
+ * The descriptors of a queue form an array indexed by tag, which the server
+ * maps read-only at offset q_id * TCMUR_DESC_AREA_SIZE of the file.
  */
 struct tcmur_io_desc {
 	__u32	op;
@@ -65,4 +66,7 @@ struct tcmur_io_desc {
 	__u32	reserved;
 };
 
+#define TCMUR_DESC_AREA_SIZE \
+	(TCMUR_MAX_QUEUE_DEPTH * sizeof(struct tcmur_io_desc))
+
 #endif /* _UAPI_LINUX_TARGET_CORE_URING_H */

-- 
2.43.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH RFC 05/18] scsi: target: uring: Queue commands until a server fetches them
  2026-11-16  0:17 [PATCH RFC 00/18] scsi: target: Add target_core_uring, an io_uring based user backstore Qiliang Yuan
                   ` (3 preceding siblings ...)
  2026-11-16  0:17 ` [PATCH RFC 04/18] scsi: target: uring: Map the command descriptors into the server Qiliang Yuan
@ 2026-11-16  0:17 ` Qiliang Yuan
  2026-11-16  0:17 ` [PATCH RFC 06/18] scsi: target: uring: Hand commands out through TCMUR_IO_FETCH Qiliang Yuan
                   ` (12 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Qiliang Yuan @ 2026-11-16  0:17 UTC (permalink / raw)
  To: Martin K. Petersen, Shuah Khan, Jonathan Corbet, Shuah Khan,
	Randy Dunlap
  Cc: linux-kernel, linux-scsi, target-devel, linux-kselftest,
	linux-doc, Jens Axboe, io-uring, Mike Christie, Bodo Stroesser,
	Ming Lei, Qiliang Yuan

target_core_uring fails every read and write, as nothing can serve them.

A command can arrive before the server is ready for it: at startup,
while all tags of a queue are busy, or while the server restarts.

Allocate a struct tcmur_cmd for each read and write, fill its descriptor
and put it on the pending list of the queue of the submitting CPU. The
commands wait there until a later patch lets a server fetch them.

Signed-off-by: Qiliang Yuan <qiliang.yuan@linux.dev>
---
 drivers/target/target_core_uring.c | 81 ++++++++++++++++++++++++++++++++++++--
 1 file changed, 78 insertions(+), 3 deletions(-)

diff --git a/drivers/target/target_core_uring.c b/drivers/target/target_core_uring.c
index 904ce2a613618..90e443ecb944c 100644
--- a/drivers/target/target_core_uring.c
+++ b/drivers/target/target_core_uring.c
@@ -12,6 +12,7 @@
 #include <linux/idr.h>
 #include <linux/mm.h>
 #include <linux/parser.h>
+#include <linux/scatterlist.h>
 #include <linux/slab.h>
 #include <linux/vmalloc.h>
 #include <target/target_core_base.h>
@@ -29,12 +30,32 @@
 
 struct tcmur_dev;
 
+/**
+ * struct tcmur_cmd - One command for the server
+ * @node:	Entry in tcmur_queue.pending or in a local list
+ * @se_cmd:	Target command this belongs to
+ * @sgl:	Data pages of the command
+ * @sgl_nents:	Number of entries in @sgl
+ * @desc:	What the server is asked to do
+ */
+struct tcmur_cmd {
+	struct list_head	node;
+	struct se_cmd		*se_cmd;
+	struct scatterlist	*sgl;
+	u32			sgl_nents;
+	struct tcmur_io_desc	desc;
+};
+
 /**
  * struct tcmur_queue - One command queue
+ * @lock:	Protect the list
+ * @pending:	Commands waiting for a tag
  * @descs:	Command descriptors, indexed by tag and mapped by the server
  * @udev:	Device the queue belongs to
  */
 struct tcmur_queue {
+	spinlock_t		lock;
+	struct list_head	pending;
 	struct tcmur_io_desc	*descs;
 	struct tcmur_dev	*udev;
 } ____cacheline_aligned_in_smp;
@@ -54,6 +75,7 @@ struct tcmur_dev {
 	struct tcmur_queue	*queues;
 };
 
+static struct kmem_cache *tcmur_cmd_cache;
 static DEFINE_IDA(tcmur_ida);
 static dev_t tcmur_devt;
 
@@ -66,12 +88,56 @@ static struct tcmur_dev *TCMUR_DEV(struct se_device *dev)
 	return container_of(dev, struct tcmur_dev, se_dev);
 }
 
+static struct tcmur_queue *tcmur_cmd_queue(struct tcmur_dev *udev,
+					   struct se_cmd *se_cmd)
+{
+	return &udev->queues[(unsigned int)se_cmd->cpuid % udev->nr_queues];
+}
+
+static void tcmur_queue_cmd(struct tcmur_dev *udev, struct tcmur_cmd *tcmd)
+{
+	struct tcmur_queue *q = tcmur_cmd_queue(udev, tcmd->se_cmd);
+
+	spin_lock(&q->lock);
+	list_add_tail(&tcmd->node, &q->pending);
+	spin_unlock(&q->lock);
+}
+
+static sense_reason_t tcmur_submit(struct se_cmd *se_cmd, u32 op, u64 offset,
+				   u64 len, struct scatterlist *sgl,
+				   u32 sgl_nents)
+{
+	struct tcmur_dev *udev = TCMUR_DEV(se_cmd->se_dev);
+	struct tcmur_cmd *tcmd;
+
+	tcmd = kmem_cache_zalloc(tcmur_cmd_cache, GFP_NOIO);
+	if (!tcmd)
+		return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
+
+	tcmd->se_cmd = se_cmd;
+	tcmd->sgl = sgl;
+	tcmd->sgl_nents = sgl_nents;
+	tcmd->desc.op = op;
+	tcmd->desc.offset = offset;
+	tcmd->desc.len = len;
+	tcmd->desc.data_len = se_cmd->data_length;
+	if (op == TCMUR_OP_WRITE && (se_cmd->se_cmd_flags & SCF_FUA))
+		tcmd->desc.flags |= TCMUR_IO_F_FUA;
+
+	tcmur_queue_cmd(udev, tcmd);
+	return 0;
+}
+
 static sense_reason_t tcmur_execute_rw(struct se_cmd *se_cmd,
 				       struct scatterlist *sgl, u32 sgl_nents,
 				       enum dma_data_direction dir)
 {
-	/* Fail the commands until a server can pick them up */
-	return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
+	u32 block_size = se_cmd->se_dev->dev_attrib.block_size;
+
+	return tcmur_submit(se_cmd, dir == DMA_FROM_DEVICE ? TCMUR_OP_READ :
+							     TCMUR_OP_WRITE,
+			    se_cmd->t_task_lba * block_size,
+			    se_cmd->data_length, sgl, sgl_nents);
 }
 
 static size_t tcmur_desc_area_size(struct tcmur_dev *udev)
@@ -175,6 +241,8 @@ static int tcmur_alloc_queues(struct tcmur_dev *udev)
 	for (i = 0; i < udev->nr_queues; i++) {
 		q = &udev->queues[i];
 
+		spin_lock_init(&q->lock);
+		INIT_LIST_HEAD(&q->pending);
 		q->udev = udev;
 		q->descs = vmalloc_user(tcmur_desc_area_size(udev));
 		if (!q->descs) {
@@ -414,9 +482,13 @@ static int __init tcmur_module_init(void)
 {
 	int ret;
 
+	tcmur_cmd_cache = KMEM_CACHE(tcmur_cmd, 0);
+	if (!tcmur_cmd_cache)
+		return -ENOMEM;
+
 	ret = alloc_chrdev_region(&tcmur_devt, 0, TCMUR_MINORS, "tcm-uring");
 	if (ret)
-		return ret;
+		goto destroy_cache;
 
 	ret = class_register(&tcmur_class);
 	if (ret)
@@ -432,6 +504,8 @@ static int __init tcmur_module_init(void)
 	class_unregister(&tcmur_class);
 unregister_chrdev:
 	unregister_chrdev_region(tcmur_devt, TCMUR_MINORS);
+destroy_cache:
+	kmem_cache_destroy(tcmur_cmd_cache);
 	return ret;
 }
 
@@ -442,6 +516,7 @@ static void __exit tcmur_module_exit(void)
 	unregister_chrdev_region(tcmur_devt, TCMUR_MINORS);
 	/* Wait for the devices that tcmur_cdev_release() frees with RCU */
 	rcu_barrier();
+	kmem_cache_destroy(tcmur_cmd_cache);
 }
 
 module_init(tcmur_module_init);

-- 
2.43.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH RFC 06/18] scsi: target: uring: Hand commands out through TCMUR_IO_FETCH
  2026-11-16  0:17 [PATCH RFC 00/18] scsi: target: Add target_core_uring, an io_uring based user backstore Qiliang Yuan
                   ` (4 preceding siblings ...)
  2026-11-16  0:17 ` [PATCH RFC 05/18] scsi: target: uring: Queue commands until a server fetches them Qiliang Yuan
@ 2026-11-16  0:17 ` Qiliang Yuan
  2026-11-16  0:18 ` [PATCH RFC 07/18] scsi: target: uring: Complete commands with TCMUR_IO_COMMIT_AND_FETCH Qiliang Yuan
                   ` (11 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Qiliang Yuan @ 2026-11-16  0:17 UTC (permalink / raw)
  To: Martin K. Petersen, Shuah Khan, Jonathan Corbet, Shuah Khan,
	Randy Dunlap
  Cc: linux-kernel, linux-scsi, target-devel, linux-kselftest,
	linux-doc, Jens Axboe, io-uring, Mike Christie, Bodo Stroesser,
	Ming Lei, Qiliang Yuan

Reads and writes of a target_core_uring device wait on the pending list
of their queue.

Nothing takes commands off that list.

Add the TCMUR_IO_FETCH io_uring command. The server issues one per tag;
it completes once the descriptor of the tag holds a command. If a
command is pending, hand it out right away. Otherwise park the fetch
command on the idle list of the queue and complete it from task work
when a command arrives. Complete parked fetch commands with -ECANCELED
when io_uring cancels them, and with -ENODEV when the device goes away.

Signed-off-by: Qiliang Yuan <qiliang.yuan@linux.dev>
---
 drivers/target/target_core_uring.c     | 216 ++++++++++++++++++++++++++++++++-
 include/uapi/linux/target_core_uring.h |  24 +++-
 2 files changed, 235 insertions(+), 5 deletions(-)

diff --git a/drivers/target/target_core_uring.c b/drivers/target/target_core_uring.c
index 90e443ecb944c..85c9a1ae84433 100644
--- a/drivers/target/target_core_uring.c
+++ b/drivers/target/target_core_uring.c
@@ -2,6 +2,10 @@
 /*
  * LIO backstore that hands block commands to a userspace server through
  * io_uring commands.
+ *
+ * Each device has a character device and a set of queues. A queue holds
+ * queue_depth tags; the server owns a tag from the moment the kernel
+ * completes its fetch command until it commits the result.
  */
 
 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
@@ -10,6 +14,7 @@
 #include <linux/cdev.h>
 #include <linux/device.h>
 #include <linux/idr.h>
+#include <linux/io_uring/cmd.h>
 #include <linux/mm.h>
 #include <linux/parser.h>
 #include <linux/scatterlist.h>
@@ -46,16 +51,34 @@ struct tcmur_cmd {
 	struct tcmur_io_desc	desc;
 };
 
+/**
+ * struct tcmur_io - State of one tag
+ * @node:	Entry in tcmur_queue.idle while @ucmd waits for a command
+ * @ucmd:	Fetch command of the server
+ * @cmd:	Command the server is working on
+ * @owned:	The server owns the tag and has to commit it
+ */
+struct tcmur_io {
+	struct list_head	node;
+	struct io_uring_cmd	*ucmd;
+	struct tcmur_cmd	*cmd;
+	bool			owned;
+};
+
 /**
  * struct tcmur_queue - One command queue
- * @lock:	Protect the list
+ * @lock:	Protect the lists and the tags
+ * @idle:	Tags with a fetch command waiting for a command
  * @pending:	Commands waiting for a tag
+ * @ios:	Tags of the queue
  * @descs:	Command descriptors, indexed by tag and mapped by the server
  * @udev:	Device the queue belongs to
  */
 struct tcmur_queue {
 	spinlock_t		lock;
+	struct list_head	idle;
 	struct list_head	pending;
+	struct tcmur_io		*ios;
 	struct tcmur_io_desc	*descs;
 	struct tcmur_dev	*udev;
 } ____cacheline_aligned_in_smp;
@@ -66,6 +89,7 @@ struct tcmur_dev {
 	struct cdev		cdev;
 	int			dev_id;
 	unsigned long		flags;
+	bool			dead;
 
 	u64			dev_size;
 	u32			block_size;
@@ -75,6 +99,18 @@ struct tcmur_dev {
 	struct tcmur_queue	*queues;
 };
 
+/**
+ * struct tcmur_uring_pdu - State of a parked fetch command
+ * @q:		Queue of the tag
+ * @io:		Tag the command fetches for
+ * @res:	Result to complete the command with
+ */
+struct tcmur_uring_pdu {
+	struct tcmur_queue	*q;
+	struct tcmur_io		*io;
+	int			res;
+};
+
 static struct kmem_cache *tcmur_cmd_cache;
 static DEFINE_IDA(tcmur_ida);
 static dev_t tcmur_devt;
@@ -94,13 +130,46 @@ static struct tcmur_queue *tcmur_cmd_queue(struct tcmur_dev *udev,
 	return &udev->queues[(unsigned int)se_cmd->cpuid % udev->nr_queues];
 }
 
+/* Hand @tcmd to the server through the tag @io */
+static void tcmur_io_start(struct tcmur_queue *q, struct tcmur_io *io,
+			   struct tcmur_cmd *tcmd)
+{
+	lockdep_assert_held(&q->lock);
+
+	io->cmd = tcmd;
+	io->owned = true;
+	q->descs[io - q->ios] = tcmd->desc;
+}
+
+static void tcmur_fetch_tw_cb(struct io_tw_req tw_req, io_tw_token_t tw)
+{
+	struct io_uring_cmd *ucmd = io_uring_cmd_from_tw(tw_req);
+	struct tcmur_uring_pdu *pdu = io_uring_cmd_to_pdu(ucmd,
+						struct tcmur_uring_pdu);
+
+	io_uring_cmd_done(ucmd, pdu->res, IO_URING_CMD_TASK_WORK_ISSUE_FLAGS);
+}
+
 static void tcmur_queue_cmd(struct tcmur_dev *udev, struct tcmur_cmd *tcmd)
 {
 	struct tcmur_queue *q = tcmur_cmd_queue(udev, tcmd->se_cmd);
+	struct io_uring_cmd *ucmd = NULL;
+	struct tcmur_io *io;
 
 	spin_lock(&q->lock);
-	list_add_tail(&tcmd->node, &q->pending);
+	io = list_first_entry_or_null(&q->idle, struct tcmur_io, node);
+	if (io) {
+		list_del(&io->node);
+		ucmd = io->ucmd;
+		io->ucmd = NULL;
+		tcmur_io_start(q, io, tcmd);
+	} else {
+		list_add_tail(&tcmd->node, &q->pending);
+	}
 	spin_unlock(&q->lock);
+
+	if (ucmd)
+		io_uring_cmd_complete_in_task(ucmd, tcmur_fetch_tw_cb);
 }
 
 static sense_reason_t tcmur_submit(struct se_cmd *se_cmd, u32 op, u64 offset,
@@ -140,6 +209,113 @@ static sense_reason_t tcmur_execute_rw(struct se_cmd *se_cmd,
 			    se_cmd->data_length, sgl, sgl_nents);
 }
 
+static void tcmur_cancel_fetch(struct io_uring_cmd *ucmd,
+			       unsigned int issue_flags)
+{
+	struct tcmur_uring_pdu *pdu = io_uring_cmd_to_pdu(ucmd,
+						struct tcmur_uring_pdu);
+	struct tcmur_queue *q = pdu->q;
+	struct tcmur_io *io = pdu->io;
+	bool found = false;
+
+	spin_lock(&q->lock);
+	if (io->ucmd == ucmd) {
+		list_del(&io->node);
+		io->ucmd = NULL;
+		found = true;
+	}
+	spin_unlock(&q->lock);
+
+	if (found)
+		io_uring_cmd_done(ucmd, -ECANCELED, issue_flags);
+}
+
+static int tcmur_fetch(struct tcmur_queue *q, struct tcmur_io *io,
+		       struct io_uring_cmd *ucmd, unsigned int issue_flags)
+{
+	struct tcmur_uring_pdu *pdu = io_uring_cmd_to_pdu(ucmd,
+						struct tcmur_uring_pdu);
+	struct tcmur_cmd *tcmd;
+	int ret = 0;
+
+	spin_lock(&q->lock);
+	if (q->udev->dead) {
+		ret = -ENODEV;
+	} else if (io->ucmd || io->owned) {
+		ret = -EBUSY;
+	} else if (!list_empty(&q->pending)) {
+		tcmd = list_first_entry(&q->pending, struct tcmur_cmd, node);
+		list_del(&tcmd->node);
+		tcmur_io_start(q, io, tcmd);
+	} else {
+		pdu->q = q;
+		pdu->io = io;
+		pdu->res = 0;
+		io->ucmd = ucmd;
+		list_add(&io->node, &q->idle);
+		ret = -EIOCBQUEUED;
+	}
+	spin_unlock(&q->lock);
+
+	if (ret == -EIOCBQUEUED)
+		io_uring_cmd_mark_cancelable(ucmd, issue_flags);
+	return ret;
+}
+
+static int tcmur_ch_uring_cmd_local(struct io_uring_cmd *ucmd,
+				    unsigned int issue_flags)
+{
+	const struct tcmur_io_cmd *ioc = io_uring_sqe_cmd(ucmd->sqe,
+							  struct tcmur_io_cmd);
+	struct tcmur_dev *udev = ucmd->file->private_data;
+	u16 q_id = READ_ONCE(ioc->q_id);
+	u16 tag = READ_ONCE(ioc->tag);
+	struct tcmur_queue *q;
+	struct tcmur_io *io;
+
+	if (q_id >= udev->nr_queues || tag >= udev->queue_depth)
+		return -EINVAL;
+
+	q = &udev->queues[q_id];
+	io = &q->ios[tag];
+
+	switch (ucmd->cmd_op) {
+	case TCMUR_IO_FETCH:
+		return tcmur_fetch(q, io, ucmd, issue_flags);
+	default:
+		return -EOPNOTSUPP;
+	}
+}
+
+static void tcmur_ch_uring_cmd_tw_cb(struct io_tw_req tw_req, io_tw_token_t tw)
+{
+	unsigned int issue_flags = IO_URING_CMD_TASK_WORK_ISSUE_FLAGS;
+	struct io_uring_cmd *ucmd = io_uring_cmd_from_tw(tw_req);
+	int ret = -ECANCELED;
+
+	if (!tw.cancel)
+		ret = tcmur_ch_uring_cmd_local(ucmd, issue_flags);
+	if (ret != -EIOCBQUEUED)
+		io_uring_cmd_done(ucmd, ret, issue_flags);
+}
+
+static int tcmur_ch_uring_cmd(struct io_uring_cmd *ucmd,
+			      unsigned int issue_flags)
+{
+	if (unlikely(issue_flags & IO_URING_F_CANCEL)) {
+		tcmur_cancel_fetch(ucmd, issue_flags);
+		return 0;
+	}
+
+	/* Run in the submitting task, where fetch commands are completed */
+	if (unlikely(issue_flags & IO_URING_F_UNLOCKED)) {
+		io_uring_cmd_complete_in_task(ucmd, tcmur_ch_uring_cmd_tw_cb);
+		return -EIOCBQUEUED;
+	}
+
+	return tcmur_ch_uring_cmd_local(ucmd, issue_flags);
+}
+
 static size_t tcmur_desc_area_size(struct tcmur_dev *udev)
 {
 	return PAGE_ALIGN(udev->queue_depth * sizeof(struct tcmur_io_desc));
@@ -210,6 +386,7 @@ static const struct file_operations tcmur_ch_fops = {
 	.owner		= THIS_MODULE,
 	.open		= tcmur_ch_open,
 	.release	= tcmur_ch_release,
+	.uring_cmd	= tcmur_ch_uring_cmd,
 	.mmap		= tcmur_ch_mmap,
 	.unlocked_ioctl	= tcmur_ch_ioctl,
 	.compat_ioctl	= compat_ptr_ioctl,
@@ -222,8 +399,10 @@ static void tcmur_free_queues(struct tcmur_dev *udev)
 	if (!udev->queues)
 		return;
 
-	for (i = 0; i < udev->nr_queues; i++)
+	for (i = 0; i < udev->nr_queues; i++) {
 		vfree(udev->queues[i].descs);
+		kvfree(udev->queues[i].ios);
+	}
 	kvfree(udev->queues);
 	udev->queues = NULL;
 }
@@ -242,10 +421,13 @@ static int tcmur_alloc_queues(struct tcmur_dev *udev)
 		q = &udev->queues[i];
 
 		spin_lock_init(&q->lock);
+		INIT_LIST_HEAD(&q->idle);
 		INIT_LIST_HEAD(&q->pending);
 		q->udev = udev;
+		q->ios = kvcalloc(udev->queue_depth, sizeof(*q->ios),
+				  GFP_KERNEL);
 		q->descs = vmalloc_user(tcmur_desc_area_size(udev));
-		if (!q->descs) {
+		if (!q->ios || !q->descs) {
 			tcmur_free_queues(udev);
 			return -ENOMEM;
 		}
@@ -348,8 +530,34 @@ 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);
+	struct tcmur_uring_pdu *pdu;
+	struct io_uring_cmd *ucmd;
+	struct tcmur_queue *q;
+	struct tcmur_io *io;
+	unsigned int i;
 
 	cdev_device_del(&udev->cdev, &udev->cdev_dev);
+
+	/*
+	 * Tell the server that the device is gone. Complete the fetch
+	 * commands from task work, like tcmur_queue_cmd() does.
+	 */
+	for (i = 0; i < udev->nr_queues; i++) {
+		q = &udev->queues[i];
+
+		spin_lock(&q->lock);
+		udev->dead = true;
+		while (!list_empty(&q->idle)) {
+			io = list_first_entry(&q->idle, struct tcmur_io, node);
+			list_del(&io->node);
+			ucmd = io->ucmd;
+			io->ucmd = NULL;
+			pdu = io_uring_cmd_to_pdu(ucmd, struct tcmur_uring_pdu);
+			pdu->res = -ENODEV;
+			io_uring_cmd_complete_in_task(ucmd, tcmur_fetch_tw_cb);
+		}
+		spin_unlock(&q->lock);
+	}
 }
 
 static void tcmur_free_device(struct se_device *dev)
diff --git a/include/uapi/linux/target_core_uring.h b/include/uapi/linux/target_core_uring.h
index d2b8ffe40bf7a..9ce871d2e2087 100644
--- a/include/uapi/linux/target_core_uring.h
+++ b/include/uapi/linux/target_core_uring.h
@@ -7,7 +7,9 @@
 
 /*
  * target_core_uring hands the block commands of a LIO device to a userspace
- * server. The server opens /dev/tcmurN to drive the device.
+ * server. The server opens /dev/tcmurN, maps the command descriptors of
+ * each queue and exchanges commands with the kernel through io_uring
+ * commands (IORING_OP_URING_CMD) issued on that file.
  */
 
 #define TCMUR_MAX_NR_QUEUES	512
@@ -37,6 +39,26 @@ struct tcmur_dev_info {
 
 #define TCMUR_GET_DEV_INFO	_IOR(0xBB, 0x00, struct tcmur_dev_info)
 
+/**
+ * struct tcmur_io_cmd - Payload of the io_uring commands, placed in sqe->cmd
+ * @q_id:	Queue of the tag
+ * @tag:	Tag the command is for
+ * @reserved:	Zero
+ * @reserved1:	Zero
+ */
+struct tcmur_io_cmd {
+	__u16	q_id;
+	__u16	tag;
+	__s32	reserved;
+	__u64	reserved1;
+};
+
+/*
+ * TCMUR_IO_FETCH: wait for a command on (q_id, tag). The CQE result is 0
+ * once the descriptor of the tag holds a command.
+ */
+#define TCMUR_IO_FETCH			_IOWR(0xBB, 0x08, struct tcmur_io_cmd)
+
 enum tcmur_io_op {
 	TCMUR_OP_READ,
 	TCMUR_OP_WRITE,

-- 
2.43.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH RFC 07/18] scsi: target: uring: Complete commands with TCMUR_IO_COMMIT_AND_FETCH
  2026-11-16  0:17 [PATCH RFC 00/18] scsi: target: Add target_core_uring, an io_uring based user backstore Qiliang Yuan
                   ` (5 preceding siblings ...)
  2026-11-16  0:17 ` [PATCH RFC 06/18] scsi: target: uring: Hand commands out through TCMUR_IO_FETCH Qiliang Yuan
@ 2026-11-16  0:18 ` Qiliang Yuan
  2026-11-16  0:18 ` [PATCH RFC 08/18] scsi: target: uring: Copy command data with pread() and pwrite() Qiliang Yuan
                   ` (10 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Qiliang Yuan @ 2026-11-16  0:18 UTC (permalink / raw)
  To: Martin K. Petersen, Shuah Khan, Jonathan Corbet, Shuah Khan,
	Randy Dunlap
  Cc: linux-kernel, linux-scsi, target-devel, linux-kselftest,
	linux-doc, Jens Axboe, io-uring, Mike Christie, Bodo Stroesser,
	Ming Lei, Qiliang Yuan

The server of a target_core_uring device can fetch a command for each
tag.

It cannot report the result, so the command never completes and the tag
is never used again.

Add the TCMUR_IO_COMMIT_AND_FETCH io_uring command, which completes the
command of a tag with GOOD or CHECK CONDITION status and fetches the
next one in the same call. Count references on struct tcmur_cmd and
complete the target command when the last one goes, as later patches
add users that can outlive the commit.

Signed-off-by: Qiliang Yuan <qiliang.yuan@linux.dev>
---
 drivers/target/target_core_uring.c     | 47 ++++++++++++++++++++++++++++++++++
 include/uapi/linux/target_core_uring.h |  9 +++++--
 2 files changed, 54 insertions(+), 2 deletions(-)

diff --git a/drivers/target/target_core_uring.c b/drivers/target/target_core_uring.c
index 85c9a1ae84433..770bb665fbe00 100644
--- a/drivers/target/target_core_uring.c
+++ b/drivers/target/target_core_uring.c
@@ -20,6 +20,7 @@
 #include <linux/scatterlist.h>
 #include <linux/slab.h>
 #include <linux/vmalloc.h>
+#include <scsi/scsi_proto.h>
 #include <target/target_core_base.h>
 #include <target/target_core_backend.h>
 #include <uapi/linux/target_core_uring.h>
@@ -42,6 +43,8 @@ struct tcmur_dev;
  * @sgl:	Data pages of the command
  * @sgl_nents:	Number of entries in @sgl
  * @desc:	What the server is asked to do
+ * @ref:	Held by the tag or the pending list
+ * @status:	SAM status to complete @se_cmd with
  */
 struct tcmur_cmd {
 	struct list_head	node;
@@ -49,6 +52,8 @@ struct tcmur_cmd {
 	struct scatterlist	*sgl;
 	u32			sgl_nents;
 	struct tcmur_io_desc	desc;
+	refcount_t		ref;
+	u8			status;
 };
 
 /**
@@ -130,6 +135,22 @@ static struct tcmur_queue *tcmur_cmd_queue(struct tcmur_dev *udev,
 	return &udev->queues[(unsigned int)se_cmd->cpuid % udev->nr_queues];
 }
 
+static void tcmur_cmd_put(struct tcmur_cmd *tcmd)
+{
+	if (!refcount_dec_and_test(&tcmd->ref))
+		return;
+
+	target_complete_cmd(tcmd->se_cmd, tcmd->status);
+	kmem_cache_free(tcmur_cmd_cache, tcmd);
+}
+
+/* Drop the reference of the tag or of the pending list and set the result */
+static void tcmur_cmd_end(struct tcmur_cmd *tcmd, u8 status)
+{
+	tcmd->status = status;
+	tcmur_cmd_put(tcmd);
+}
+
 /* Hand @tcmd to the server through the tag @io */
 static void tcmur_io_start(struct tcmur_queue *q, struct tcmur_io *io,
 			   struct tcmur_cmd *tcmd)
@@ -156,6 +177,8 @@ static void tcmur_queue_cmd(struct tcmur_dev *udev, struct tcmur_cmd *tcmd)
 	struct io_uring_cmd *ucmd = NULL;
 	struct tcmur_io *io;
 
+	refcount_set(&tcmd->ref, 1);
+
 	spin_lock(&q->lock);
 	io = list_first_entry_or_null(&q->idle, struct tcmur_io, node);
 	if (io) {
@@ -262,6 +285,24 @@ static int tcmur_fetch(struct tcmur_queue *q, struct tcmur_io *io,
 	return ret;
 }
 
+static int tcmur_commit(struct tcmur_queue *q, struct tcmur_io *io, int result)
+{
+	struct tcmur_cmd *tcmd;
+
+	spin_lock(&q->lock);
+	if (!io->owned) {
+		spin_unlock(&q->lock);
+		return -EINVAL;
+	}
+	tcmd = io->cmd;
+	io->cmd = NULL;
+	io->owned = false;
+	spin_unlock(&q->lock);
+
+	tcmur_cmd_end(tcmd, result ? SAM_STAT_CHECK_CONDITION : SAM_STAT_GOOD);
+	return 0;
+}
+
 static int tcmur_ch_uring_cmd_local(struct io_uring_cmd *ucmd,
 				    unsigned int issue_flags)
 {
@@ -272,6 +313,7 @@ static int tcmur_ch_uring_cmd_local(struct io_uring_cmd *ucmd,
 	u16 tag = READ_ONCE(ioc->tag);
 	struct tcmur_queue *q;
 	struct tcmur_io *io;
+	int ret;
 
 	if (q_id >= udev->nr_queues || tag >= udev->queue_depth)
 		return -EINVAL;
@@ -282,6 +324,11 @@ static int tcmur_ch_uring_cmd_local(struct io_uring_cmd *ucmd,
 	switch (ucmd->cmd_op) {
 	case TCMUR_IO_FETCH:
 		return tcmur_fetch(q, io, ucmd, issue_flags);
+	case TCMUR_IO_COMMIT_AND_FETCH:
+		ret = tcmur_commit(q, io, READ_ONCE(ioc->result));
+		if (ret)
+			return ret;
+		return tcmur_fetch(q, io, ucmd, issue_flags);
 	default:
 		return -EOPNOTSUPP;
 	}
diff --git a/include/uapi/linux/target_core_uring.h b/include/uapi/linux/target_core_uring.h
index 9ce871d2e2087..a662a23fca9ab 100644
--- a/include/uapi/linux/target_core_uring.h
+++ b/include/uapi/linux/target_core_uring.h
@@ -43,21 +43,26 @@ struct tcmur_dev_info {
  * struct tcmur_io_cmd - Payload of the io_uring commands, placed in sqe->cmd
  * @q_id:	Queue of the tag
  * @tag:	Tag the command is for
- * @reserved:	Zero
+ * @result:	TCMUR_IO_COMMIT_AND_FETCH: 0, or a negative errno if the
+ *		command failed
  * @reserved1:	Zero
  */
 struct tcmur_io_cmd {
 	__u16	q_id;
 	__u16	tag;
-	__s32	reserved;
+	__s32	result;
 	__u64	reserved1;
 };
 
 /*
  * TCMUR_IO_FETCH: wait for a command on (q_id, tag). The CQE result is 0
  * once the descriptor of the tag holds a command.
+ *
+ * TCMUR_IO_COMMIT_AND_FETCH: complete the command of (q_id, tag) with
+ * @result, then wait for the next one.
  */
 #define TCMUR_IO_FETCH			_IOWR(0xBB, 0x08, struct tcmur_io_cmd)
+#define TCMUR_IO_COMMIT_AND_FETCH	_IOWR(0xBB, 0x09, struct tcmur_io_cmd)
 
 enum tcmur_io_op {
 	TCMUR_OP_READ,

-- 
2.43.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH RFC 08/18] scsi: target: uring: Copy command data with pread() and pwrite()
  2026-11-16  0:17 [PATCH RFC 00/18] scsi: target: Add target_core_uring, an io_uring based user backstore Qiliang Yuan
                   ` (6 preceding siblings ...)
  2026-11-16  0:18 ` [PATCH RFC 07/18] scsi: target: uring: Complete commands with TCMUR_IO_COMMIT_AND_FETCH Qiliang Yuan
@ 2026-11-16  0:18 ` Qiliang Yuan
  2026-11-16  0:18 ` [PATCH RFC 09/18] scsi: target: uring: Fail in-flight commands when the server exits Qiliang Yuan
                   ` (9 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Qiliang Yuan @ 2026-11-16  0:18 UTC (permalink / raw)
  To: Martin K. Petersen, Shuah Khan, Jonathan Corbet, Shuah Khan,
	Randy Dunlap
  Cc: linux-kernel, linux-scsi, target-devel, linux-kselftest,
	linux-doc, Jens Axboe, io-uring, Mike Christie, Bodo Stroesser,
	Ming Lei, Qiliang Yuan

The server of a target_core_uring device fetches commands and commits
their results.

It has no access to the data of reads and writes.

Let the server pwrite() the data of a read command and pread() the data
of a write command on the character device, at a position that encodes
the queue, the tag and the offset in the command. Copy straight between
the pages of the command and the buffer of the server, so that the data
is copied once. Hold a reference on the command during the copy, so
that it does not complete underneath.

Exporting a 2 GiB device through tcm_loop to fio (io_uring, direct I/O,
iodepth 32 for 4k and 8 for 1M, 6 seconds per job) on a 32 CPU VM, with
TCM/USER and a single-threaded handler, then with this backstore, 32
queues and one server thread per queue. Both servers keep the data in
memory. A single job is slower; the gain comes with concurrency and with
large I/O. Three alternating runs (median, min-max in parentheses):

  workload               TCM/USER (MB/s)   uring (MB/s)          change
  4k randread, 1 job      426 (409-463)      354 (348-378)         -17%
  4k randread, 4 jobs    1165 (1072-1253)   1466 (1451-1518)       +26%
  4k randread, 16 jobs    920 (916-1162)    6500 (6173-6769)      +607%
  4k randwrite, 16 jobs   793 (711-886)     6070 (5966-6216)      +665%
  1M read, 4 jobs        6019 (5857-6047)  47687 (32175-49464)    +692%
  1M write, 4 jobs       2791 (2649-2907)  28295 (27152-29050)    +914%

Signed-off-by: Qiliang Yuan <qiliang.yuan@linux.dev>
---
 drivers/target/target_core_uring.c     | 89 +++++++++++++++++++++++++++++++++-
 include/uapi/linux/target_core_uring.h | 15 ++++++
 2 files changed, 103 insertions(+), 1 deletion(-)

diff --git a/drivers/target/target_core_uring.c b/drivers/target/target_core_uring.c
index 770bb665fbe00..3a6f8d25d28e5 100644
--- a/drivers/target/target_core_uring.c
+++ b/drivers/target/target_core_uring.c
@@ -19,6 +19,7 @@
 #include <linux/parser.h>
 #include <linux/scatterlist.h>
 #include <linux/slab.h>
+#include <linux/uio.h>
 #include <linux/vmalloc.h>
 #include <scsi/scsi_proto.h>
 #include <target/target_core_base.h>
@@ -43,7 +44,8 @@ struct tcmur_dev;
  * @sgl:	Data pages of the command
  * @sgl_nents:	Number of entries in @sgl
  * @desc:	What the server is asked to do
- * @ref:	Held by the tag or the pending list
+ * @ref:	Held by the tag or the pending list and by data copies in
+ *		progress
  * @status:	SAM status to complete @se_cmd with
  */
 struct tcmur_cmd {
@@ -232,6 +234,26 @@ static sense_reason_t tcmur_execute_rw(struct se_cmd *se_cmd,
 			    se_cmd->data_length, sgl, sgl_nents);
 }
 
+/* Take a reference on the command the server works on in (@q_id, @tag) */
+static struct tcmur_cmd *tcmur_get_cmd(struct tcmur_dev *udev, u16 q_id,
+				       u16 tag)
+{
+	struct tcmur_cmd *tcmd;
+	struct tcmur_queue *q;
+
+	if (q_id >= udev->nr_queues || tag >= udev->queue_depth)
+		return NULL;
+
+	q = &udev->queues[q_id];
+	spin_lock(&q->lock);
+	tcmd = q->ios[tag].cmd;
+	if (tcmd)
+		refcount_inc(&tcmd->ref);
+	spin_unlock(&q->lock);
+
+	return tcmd;
+}
+
 static void tcmur_cancel_fetch(struct io_uring_cmd *ucmd,
 			       unsigned int issue_flags)
 {
@@ -363,6 +385,69 @@ static int tcmur_ch_uring_cmd(struct io_uring_cmd *ucmd,
 	return tcmur_ch_uring_cmd_local(ucmd, issue_flags);
 }
 
+/* Copy between the data pages of a command and the buffer of the server */
+static ssize_t tcmur_ch_copy(struct kiocb *iocb, struct iov_iter *iter,
+			     bool to_cmd)
+{
+	struct tcmur_dev *udev = iocb->ki_filp->private_data;
+	u32 off = iocb->ki_pos & TCMUR_BUF_OFF_MASK;
+	u16 tag = iocb->ki_pos >> TCMUR_BUF_TAG_SHIFT;
+	u16 q_id = iocb->ki_pos >> TCMUR_BUF_QID_SHIFT;
+	size_t count, n, copied;
+	struct tcmur_cmd *tcmd;
+	struct scatterlist *sg;
+	ssize_t done = 0;
+	int i;
+
+	tcmd = tcmur_get_cmd(udev, q_id, tag);
+	if (!tcmd)
+		return -EINVAL;
+
+	if (to_cmd != (tcmd->desc.op == TCMUR_OP_READ) ||
+	    off > tcmd->desc.data_len) {
+		done = -EINVAL;
+		goto put_cmd;
+	}
+
+	count = min(iov_iter_count(iter), tcmd->desc.data_len - off);
+	for_each_sg(tcmd->sgl, sg, tcmd->sgl_nents, i) {
+		if (!count)
+			break;
+		if (off >= sg->length) {
+			off -= sg->length;
+			continue;
+		}
+
+		n = min(sg->length - off, count);
+		if (to_cmd)
+			copied = copy_page_from_iter(sg_page(sg),
+						     sg->offset + off, n, iter);
+		else
+			copied = copy_page_to_iter(sg_page(sg),
+						   sg->offset + off, n, iter);
+		done += copied;
+		count -= copied;
+		if (copied != n)
+			break;
+		off = 0;
+	}
+	if (!done && count)
+		done = -EFAULT;
+put_cmd:
+	tcmur_cmd_put(tcmd);
+	return done;
+}
+
+static ssize_t tcmur_ch_read_iter(struct kiocb *iocb, struct iov_iter *to)
+{
+	return tcmur_ch_copy(iocb, to, false);
+}
+
+static ssize_t tcmur_ch_write_iter(struct kiocb *iocb, struct iov_iter *from)
+{
+	return tcmur_ch_copy(iocb, from, true);
+}
+
 static size_t tcmur_desc_area_size(struct tcmur_dev *udev)
 {
 	return PAGE_ALIGN(udev->queue_depth * sizeof(struct tcmur_io_desc));
@@ -433,6 +518,8 @@ static const struct file_operations tcmur_ch_fops = {
 	.owner		= THIS_MODULE,
 	.open		= tcmur_ch_open,
 	.release	= tcmur_ch_release,
+	.read_iter	= tcmur_ch_read_iter,
+	.write_iter	= tcmur_ch_write_iter,
 	.uring_cmd	= tcmur_ch_uring_cmd,
 	.mmap		= tcmur_ch_mmap,
 	.unlocked_ioctl	= tcmur_ch_ioctl,
diff --git a/include/uapi/linux/target_core_uring.h b/include/uapi/linux/target_core_uring.h
index a662a23fca9ab..8b95be853753c 100644
--- a/include/uapi/linux/target_core_uring.h
+++ b/include/uapi/linux/target_core_uring.h
@@ -96,4 +96,19 @@ struct tcmur_io_desc {
 #define TCMUR_DESC_AREA_SIZE \
 	(TCMUR_MAX_QUEUE_DEPTH * sizeof(struct tcmur_io_desc))
 
+/*
+ * The data of a command is read (TCMUR_OP_WRITE) or written
+ * (TCMUR_OP_READ) with pread()/pwrite() on the file, at the position
+ * returned by tcmur_buf_pos().
+ */
+#define TCMUR_BUF_TAG_SHIFT	32
+#define TCMUR_BUF_QID_SHIFT	48
+#define TCMUR_BUF_OFF_MASK	((1ULL << TCMUR_BUF_TAG_SHIFT) - 1)
+
+static inline __u64 tcmur_buf_pos(__u16 q_id, __u16 tag, __u32 off)
+{
+	return ((__u64)q_id << TCMUR_BUF_QID_SHIFT) |
+	       ((__u64)tag << TCMUR_BUF_TAG_SHIFT) | off;
+}
+
 #endif /* _UAPI_LINUX_TARGET_CORE_URING_H */

-- 
2.43.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH RFC 09/18] scsi: target: uring: Fail in-flight commands when the server exits
  2026-11-16  0:17 [PATCH RFC 00/18] scsi: target: Add target_core_uring, an io_uring based user backstore Qiliang Yuan
                   ` (7 preceding siblings ...)
  2026-11-16  0:18 ` [PATCH RFC 08/18] scsi: target: uring: Copy command data with pread() and pwrite() Qiliang Yuan
@ 2026-11-16  0:18 ` Qiliang Yuan
  2026-11-16  0:18 ` [PATCH RFC 10/18] scsi: target: uring: Time out commands the server does not complete Qiliang Yuan
                   ` (8 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Qiliang Yuan @ 2026-11-16  0:18 UTC (permalink / raw)
  To: Martin K. Petersen, Shuah Khan, Jonathan Corbet, Shuah Khan,
	Randy Dunlap
  Cc: linux-kernel, linux-scsi, target-devel, linux-kselftest,
	linux-doc, Jens Axboe, io-uring, Mike Christie, Bodo Stroesser,
	Ming Lei, Qiliang Yuan

A tag of a target_core_uring device belongs to the server from the
moment it fetches a command until it commits the result.

If the server exits or crashes in between, the command never completes
and the tag stays owned, so a new server cannot fetch on it.

Fail the commands the server was working on when it closes the
character device, and give the tags back. Commands that were not fetched
yet stay on the pending lists for the next server.

Signed-off-by: Qiliang Yuan <qiliang.yuan@linux.dev>
---
 drivers/target/target_core_uring.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)

diff --git a/drivers/target/target_core_uring.c b/drivers/target/target_core_uring.c
index 3a6f8d25d28e5..2978dd08c99cf 100644
--- a/drivers/target/target_core_uring.c
+++ b/drivers/target/target_core_uring.c
@@ -153,6 +153,16 @@ static void tcmur_cmd_end(struct tcmur_cmd *tcmd, u8 status)
 	tcmur_cmd_put(tcmd);
 }
 
+static void tcmur_end_cmds(struct list_head *cmds, u8 status)
+{
+	struct tcmur_cmd *tcmd, *tmp;
+
+	list_for_each_entry_safe(tcmd, tmp, cmds, node) {
+		list_del(&tcmd->node);
+		tcmur_cmd_end(tcmd, status);
+	}
+}
+
 /* Hand @tcmd to the server through the tag @io */
 static void tcmur_io_start(struct tcmur_queue *q, struct tcmur_io *io,
 			   struct tcmur_cmd *tcmd)
@@ -509,6 +519,27 @@ static int tcmur_ch_open(struct inode *inode, struct file *filp)
 static int tcmur_ch_release(struct inode *inode, struct file *filp)
 {
 	struct tcmur_dev *udev = filp->private_data;
+	struct tcmur_queue *q;
+	struct tcmur_io *io;
+	LIST_HEAD(failed);
+	unsigned int i, j;
+
+	/* Fail the commands that the server did not commit */
+	for (i = 0; i < udev->nr_queues; i++) {
+		q = &udev->queues[i];
+
+		spin_lock(&q->lock);
+		for (j = 0; j < udev->queue_depth; j++) {
+			io = &q->ios[j];
+			io->owned = false;
+			if (!io->cmd)
+				continue;
+			list_add_tail(&io->cmd->node, &failed);
+			io->cmd = NULL;
+		}
+		spin_unlock(&q->lock);
+	}
+	tcmur_end_cmds(&failed, SAM_STAT_CHECK_CONDITION);
 
 	clear_bit(TCMUR_DEV_OPEN, &udev->flags);
 	return 0;

-- 
2.43.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH RFC 10/18] scsi: target: uring: Time out commands the server does not complete
  2026-11-16  0:17 [PATCH RFC 00/18] scsi: target: Add target_core_uring, an io_uring based user backstore Qiliang Yuan
                   ` (8 preceding siblings ...)
  2026-11-16  0:18 ` [PATCH RFC 09/18] scsi: target: uring: Fail in-flight commands when the server exits Qiliang Yuan
@ 2026-11-16  0:18 ` Qiliang Yuan
  2026-11-16  0:18 ` [PATCH RFC 11/18] scsi: target: uring: Drop aborted commands that are still queued Qiliang Yuan
                   ` (7 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Qiliang Yuan @ 2026-11-16  0:18 UTC (permalink / raw)
  To: Martin K. Petersen, Shuah Khan, Jonathan Corbet, Shuah Khan,
	Randy Dunlap
  Cc: linux-kernel, linux-scsi, target-devel, linux-kselftest,
	linux-doc, Jens Axboe, io-uring, Mike Christie, Bodo Stroesser,
	Ming Lei, Qiliang Yuan

Commands of a target_core_uring device wait on a pending list until a
server fetches them, and then for the server to commit them.

Without a server, or with one that hangs, they wait forever, and so
does everything that waits for them, such as removing the LUN.

Add the cmd_time_out parameter, 30 seconds by default, and fail the
commands that are older. A delayed work scans the queues when the oldest
command is due. A timed out command that the server works on is
completed, but the server keeps the tag until it commits it.

Signed-off-by: Qiliang Yuan <qiliang.yuan@linux.dev>
---
 drivers/target/target_core_uring.c | 80 +++++++++++++++++++++++++++++++++++++-
 1 file changed, 78 insertions(+), 2 deletions(-)

diff --git a/drivers/target/target_core_uring.c b/drivers/target/target_core_uring.c
index 2978dd08c99cf..0f83bdc55e06a 100644
--- a/drivers/target/target_core_uring.c
+++ b/drivers/target/target_core_uring.c
@@ -21,6 +21,7 @@
 #include <linux/slab.h>
 #include <linux/uio.h>
 #include <linux/vmalloc.h>
+#include <linux/workqueue.h>
 #include <scsi/scsi_proto.h>
 #include <target/target_core_base.h>
 #include <target/target_core_backend.h>
@@ -31,6 +32,7 @@
 #define TCMUR_MAX_IO_SIZE	(4U << 20)
 #define TCMUR_DEF_QUEUE_DEPTH	128
 #define TCMUR_DEF_BLOCK_SIZE	512
+#define TCMUR_DEF_CMD_TIMEOUT	30
 
 /* Set while the file is open, to allow one server at a time */
 #define TCMUR_DEV_OPEN		0
@@ -46,6 +48,7 @@ struct tcmur_dev;
  * @desc:	What the server is asked to do
  * @ref:	Held by the tag or the pending list and by data copies in
  *		progress
+ * @deadline:	When to give up waiting for the server
  * @status:	SAM status to complete @se_cmd with
  */
 struct tcmur_cmd {
@@ -55,6 +58,7 @@ struct tcmur_cmd {
 	u32			sgl_nents;
 	struct tcmur_io_desc	desc;
 	refcount_t		ref;
+	unsigned long		deadline;
 	u8			status;
 };
 
@@ -102,8 +106,10 @@ struct tcmur_dev {
 	u32			block_size;
 	u16			nr_queues;
 	u16			queue_depth;
+	unsigned int		cmd_timeout;
 
 	struct tcmur_queue	*queues;
+	struct delayed_work	timeout_work;
 };
 
 /**
@@ -190,6 +196,8 @@ static void tcmur_queue_cmd(struct tcmur_dev *udev, struct tcmur_cmd *tcmd)
 	struct tcmur_io *io;
 
 	refcount_set(&tcmd->ref, 1);
+	if (udev->cmd_timeout)
+		tcmd->deadline = jiffies + udev->cmd_timeout * HZ;
 
 	spin_lock(&q->lock);
 	io = list_first_entry_or_null(&q->idle, struct tcmur_io, node);
@@ -205,6 +213,9 @@ static void tcmur_queue_cmd(struct tcmur_dev *udev, struct tcmur_cmd *tcmd)
 
 	if (ucmd)
 		io_uring_cmd_complete_in_task(ucmd, tcmur_fetch_tw_cb);
+	if (udev->cmd_timeout)
+		schedule_delayed_work(&udev->timeout_work,
+				      udev->cmd_timeout * HZ);
 }
 
 static sense_reason_t tcmur_submit(struct se_cmd *se_cmd, u32 op, u64 offset,
@@ -244,6 +255,58 @@ static sense_reason_t tcmur_execute_rw(struct se_cmd *se_cmd,
 			    se_cmd->data_length, sgl, sgl_nents);
 }
 
+static void tcmur_timeout_work(struct work_struct *work)
+{
+	struct tcmur_dev *udev = container_of(work, struct tcmur_dev,
+					      timeout_work.work);
+	unsigned long now = jiffies, next = 0;
+	struct tcmur_cmd *tcmd, *tmp;
+	struct tcmur_queue *q;
+	struct tcmur_io *io;
+	bool rearm = false;
+	LIST_HEAD(expired);
+	unsigned int i, j;
+
+	for (i = 0; i < udev->nr_queues; i++) {
+		q = &udev->queues[i];
+
+		spin_lock(&q->lock);
+		list_for_each_entry_safe(tcmd, tmp, &q->pending, node) {
+			if (time_after(tcmd->deadline, now)) {
+				if (!rearm || time_before(tcmd->deadline, next))
+					next = tcmd->deadline;
+				rearm = true;
+				break;
+			}
+			list_move_tail(&tcmd->node, &expired);
+		}
+		for (j = 0; j < udev->queue_depth; j++) {
+			io = &q->ios[j];
+			tcmd = io->cmd;
+			if (!tcmd)
+				continue;
+			if (time_after(tcmd->deadline, now)) {
+				if (!rearm || time_before(tcmd->deadline, next))
+					next = tcmd->deadline;
+				rearm = true;
+				continue;
+			}
+			/* Leave the tag to the server until it commits it */
+			io->cmd = NULL;
+			list_add_tail(&tcmd->node, &expired);
+		}
+		spin_unlock(&q->lock);
+	}
+
+	if (!list_empty(&expired))
+		pr_warn_ratelimited("tcmur%d: command timed out\n",
+				    udev->dev_id);
+	tcmur_end_cmds(&expired, SAM_STAT_CHECK_CONDITION);
+
+	if (rearm)
+		schedule_delayed_work(&udev->timeout_work, next - now);
+}
+
 /* Take a reference on the command the server works on in (@q_id, @tag) */
 static struct tcmur_cmd *tcmur_get_cmd(struct tcmur_dev *udev, u16 q_id,
 				       u16 tag)
@@ -331,7 +394,10 @@ static int tcmur_commit(struct tcmur_queue *q, struct tcmur_io *io, int result)
 	io->owned = false;
 	spin_unlock(&q->lock);
 
-	tcmur_cmd_end(tcmd, result ? SAM_STAT_CHECK_CONDITION : SAM_STAT_GOOD);
+	/* Skip a command that timed out already */
+	if (tcmd)
+		tcmur_cmd_end(tcmd, result ? SAM_STAT_CHECK_CONDITION :
+					     SAM_STAT_GOOD);
 	return 0;
 }
 
@@ -639,6 +705,8 @@ static struct se_device *tcmur_alloc_device(struct se_hba *hba,
 	udev->nr_queues = 1;
 	udev->queue_depth = TCMUR_DEF_QUEUE_DEPTH;
 	udev->block_size = TCMUR_DEF_BLOCK_SIZE;
+	udev->cmd_timeout = TCMUR_DEF_CMD_TIMEOUT;
+	INIT_DELAYED_WORK(&udev->timeout_work, tcmur_timeout_work);
 
 	device_initialize(&udev->cdev_dev);
 	udev->cdev_dev.class = &tcmur_class;
@@ -723,6 +791,8 @@ static void tcmur_destroy_device(struct se_device *dev)
 		}
 		spin_unlock(&q->lock);
 	}
+
+	cancel_delayed_work_sync(&udev->timeout_work);
 }
 
 static void tcmur_free_device(struct se_device *dev)
@@ -731,7 +801,8 @@ static void tcmur_free_device(struct se_device *dev)
 }
 
 enum {
-	Opt_dev_size, Opt_block_size, Opt_nr_queues, Opt_queue_depth, Opt_err,
+	Opt_dev_size, Opt_block_size, Opt_nr_queues, Opt_queue_depth,
+	Opt_cmd_time_out, Opt_err,
 };
 
 static const match_table_t tcmur_tokens = {
@@ -739,6 +810,7 @@ static const match_table_t tcmur_tokens = {
 	{ Opt_block_size,	"block_size=%u" },
 	{ Opt_nr_queues,	"nr_queues=%u" },
 	{ Opt_queue_depth,	"queue_depth=%u" },
+	{ Opt_cmd_time_out,	"cmd_time_out=%u" },
 	{ Opt_err,		NULL }
 };
 
@@ -794,6 +866,9 @@ static ssize_t tcmur_set_configfs_dev_params(struct se_device *dev,
 			else
 				udev->queue_depth = val;
 			break;
+		case Opt_cmd_time_out:
+			udev->cmd_timeout = val;
+			break;
 		}
 	}
 
@@ -812,6 +887,7 @@ static ssize_t tcmur_show_configfs_dev_params(struct se_device *dev, char *b)
 		      udev->block_size);
 	bl += sprintf(b + bl, " Queues: %u Queue depth: %u", udev->nr_queues,
 		      udev->queue_depth);
+	bl += sprintf(b + bl, " Command timeout: %u", udev->cmd_timeout);
 	bl += sprintf(b + bl, "\n");
 	return bl;
 }

-- 
2.43.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH RFC 11/18] scsi: target: uring: Drop aborted commands that are still queued
  2026-11-16  0:17 [PATCH RFC 00/18] scsi: target: Add target_core_uring, an io_uring based user backstore Qiliang Yuan
                   ` (9 preceding siblings ...)
  2026-11-16  0:18 ` [PATCH RFC 10/18] scsi: target: uring: Time out commands the server does not complete Qiliang Yuan
@ 2026-11-16  0:18 ` Qiliang Yuan
  2026-11-16  0:18 ` [PATCH RFC 12/18] scsi: target: uring: Support SYNCHRONIZE CACHE Qiliang Yuan
                   ` (6 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Qiliang Yuan @ 2026-11-16  0:18 UTC (permalink / raw)
  To: Martin K. Petersen, Shuah Khan, Jonathan Corbet, Shuah Khan,
	Randy Dunlap
  Cc: linux-kernel, linux-scsi, target-devel, linux-kselftest,
	linux-doc, Jens Axboe, io-uring, Mike Christie, Bodo Stroesser,
	Ming Lei, Qiliang Yuan

When a command is aborted, the core waits for the backstore to complete
it.

A target_core_uring command that no server has fetched only completes
when it times out, which delays the abort by up to cmd_time_out.

Implement tmr_notify() and complete the aborted commands that are still
on a pending list with TASK ABORTED status. Track them through
se_cmd->priv, which is set only while a command is pending.

Signed-off-by: Qiliang Yuan <qiliang.yuan@linux.dev>
---
 drivers/target/target_core_uring.c | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

diff --git a/drivers/target/target_core_uring.c b/drivers/target/target_core_uring.c
index 0f83bdc55e06a..629419a4c8f46 100644
--- a/drivers/target/target_core_uring.c
+++ b/drivers/target/target_core_uring.c
@@ -175,6 +175,7 @@ static void tcmur_io_start(struct tcmur_queue *q, struct tcmur_io *io,
 {
 	lockdep_assert_held(&q->lock);
 
+	tcmd->se_cmd->priv = NULL;
 	io->cmd = tcmd;
 	io->owned = true;
 	q->descs[io - q->ios] = tcmd->desc;
@@ -207,6 +208,7 @@ static void tcmur_queue_cmd(struct tcmur_dev *udev, struct tcmur_cmd *tcmd)
 		io->ucmd = NULL;
 		tcmur_io_start(q, io, tcmd);
 	} else {
+		tcmd->se_cmd->priv = tcmd;
 		list_add_tail(&tcmd->node, &q->pending);
 	}
 	spin_unlock(&q->lock);
@@ -255,6 +257,31 @@ static sense_reason_t tcmur_execute_rw(struct se_cmd *se_cmd,
 			    se_cmd->data_length, sgl, sgl_nents);
 }
 
+static void tcmur_tmr_notify(struct se_device *dev, enum tcm_tmreq_table tmf,
+			     struct list_head *aborted_cmds)
+{
+	struct tcmur_dev *udev = TCMUR_DEV(dev);
+	struct tcmur_cmd *tcmd;
+	struct tcmur_queue *q;
+	struct se_cmd *se_cmd;
+
+	/* Drop the aborted commands that no server has picked up yet */
+	list_for_each_entry(se_cmd, aborted_cmds, state_list) {
+		q = tcmur_cmd_queue(udev, se_cmd);
+
+		spin_lock(&q->lock);
+		tcmd = se_cmd->priv;
+		if (tcmd) {
+			se_cmd->priv = NULL;
+			list_del(&tcmd->node);
+		}
+		spin_unlock(&q->lock);
+
+		if (tcmd)
+			tcmur_cmd_end(tcmd, SAM_STAT_TASK_ABORTED);
+	}
+}
+
 static void tcmur_timeout_work(struct work_struct *work)
 {
 	struct tcmur_dev *udev = container_of(work, struct tcmur_dev,
@@ -278,6 +305,7 @@ static void tcmur_timeout_work(struct work_struct *work)
 				rearm = true;
 				break;
 			}
+			tcmd->se_cmd->priv = NULL;
 			list_move_tail(&tcmd->node, &expired);
 		}
 		for (j = 0; j < udev->queue_depth; j++) {
@@ -920,6 +948,7 @@ static const struct target_backend_ops tcmur_ops = {
 	.destroy_device		= tcmur_destroy_device,
 	.free_device		= tcmur_free_device,
 	.parse_cdb		= tcmur_parse_cdb,
+	.tmr_notify		= tcmur_tmr_notify,
 	.set_configfs_dev_params = tcmur_set_configfs_dev_params,
 	.show_configfs_dev_params = tcmur_show_configfs_dev_params,
 	.get_device_type	= sbc_get_device_type,

-- 
2.43.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH RFC 12/18] scsi: target: uring: Support SYNCHRONIZE CACHE
  2026-11-16  0:17 [PATCH RFC 00/18] scsi: target: Add target_core_uring, an io_uring based user backstore Qiliang Yuan
                   ` (10 preceding siblings ...)
  2026-11-16  0:18 ` [PATCH RFC 11/18] scsi: target: uring: Drop aborted commands that are still queued Qiliang Yuan
@ 2026-11-16  0:18 ` Qiliang Yuan
  2026-11-16  0:18 ` [PATCH RFC 13/18] scsi: target: uring: Implement UNMAP Qiliang Yuan
                   ` (5 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Qiliang Yuan @ 2026-11-16  0:18 UTC (permalink / raw)
  To: Martin K. Petersen, Shuah Khan, Jonathan Corbet, Shuah Khan,
	Randy Dunlap
  Cc: linux-kernel, linux-scsi, target-devel, linux-kselftest,
	linux-doc, Jens Axboe, io-uring, Mike Christie, Bodo Stroesser,
	Ming Lei, Qiliang Yuan

target_core_uring only passes reads and writes to the server.

A server that caches writes has no way to learn that the initiator
wants them on stable storage.

Add TCMUR_OP_FLUSH and send it for SYNCHRONIZE CACHE. The descriptor
carries the range to flush; a zero length means up to the end of the
device.

Signed-off-by: Qiliang Yuan <qiliang.yuan@linux.dev>
---
 drivers/target/target_core_uring.c     | 14 +++++++++++++-
 include/uapi/linux/target_core_uring.h |  1 +
 2 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/drivers/target/target_core_uring.c b/drivers/target/target_core_uring.c
index 629419a4c8f46..f39bf7bf36d0f 100644
--- a/drivers/target/target_core_uring.c
+++ b/drivers/target/target_core_uring.c
@@ -237,7 +237,8 @@ static sense_reason_t tcmur_submit(struct se_cmd *se_cmd, u32 op, u64 offset,
 	tcmd->desc.op = op;
 	tcmd->desc.offset = offset;
 	tcmd->desc.len = len;
-	tcmd->desc.data_len = se_cmd->data_length;
+	if (sgl)
+		tcmd->desc.data_len = se_cmd->data_length;
 	if (op == TCMUR_OP_WRITE && (se_cmd->se_cmd_flags & SCF_FUA))
 		tcmd->desc.flags |= TCMUR_IO_F_FUA;
 
@@ -257,6 +258,16 @@ static sense_reason_t tcmur_execute_rw(struct se_cmd *se_cmd,
 			    se_cmd->data_length, sgl, sgl_nents);
 }
 
+static sense_reason_t tcmur_execute_sync_cache(struct se_cmd *se_cmd)
+{
+	u32 block_size = se_cmd->se_dev->dev_attrib.block_size;
+
+	/* Pass a zero length to flush up to the end of the device */
+	return tcmur_submit(se_cmd, TCMUR_OP_FLUSH,
+			    se_cmd->t_task_lba * block_size,
+			    se_cmd->data_length, NULL, 0);
+}
+
 static void tcmur_tmr_notify(struct se_device *dev, enum tcm_tmreq_table tmf,
 			     struct list_head *aborted_cmds)
 {
@@ -929,6 +940,7 @@ static sector_t tcmur_get_blocks(struct se_device *dev)
 
 static struct exec_cmd_ops tcmur_exec_cmd_ops = {
 	.execute_rw		= tcmur_execute_rw,
+	.execute_sync_cache	= tcmur_execute_sync_cache,
 };
 
 static sense_reason_t tcmur_parse_cdb(struct se_cmd *se_cmd)
diff --git a/include/uapi/linux/target_core_uring.h b/include/uapi/linux/target_core_uring.h
index 8b95be853753c..a1d8ae47ebf7d 100644
--- a/include/uapi/linux/target_core_uring.h
+++ b/include/uapi/linux/target_core_uring.h
@@ -67,6 +67,7 @@ struct tcmur_io_cmd {
 enum tcmur_io_op {
 	TCMUR_OP_READ,
 	TCMUR_OP_WRITE,
+	TCMUR_OP_FLUSH,
 };
 
 /* Put the write on stable storage before completing it */

-- 
2.43.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH RFC 13/18] scsi: target: uring: Implement UNMAP
  2026-11-16  0:17 [PATCH RFC 00/18] scsi: target: Add target_core_uring, an io_uring based user backstore Qiliang Yuan
                   ` (11 preceding siblings ...)
  2026-11-16  0:18 ` [PATCH RFC 12/18] scsi: target: uring: Support SYNCHRONIZE CACHE Qiliang Yuan
@ 2026-11-16  0:18 ` Qiliang Yuan
  2026-11-16  0:18 ` [PATCH RFC 14/18] scsi: target: uring: Handle WRITE SAME Qiliang Yuan
                   ` (4 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Qiliang Yuan @ 2026-11-16  0:18 UTC (permalink / raw)
  To: Martin K. Petersen, Shuah Khan, Jonathan Corbet, Shuah Khan,
	Randy Dunlap
  Cc: linux-kernel, linux-scsi, target-devel, linux-kselftest,
	linux-doc, Jens Axboe, io-uring, Mike Christie, Bodo Stroesser,
	Ming Lei, Qiliang Yuan

target_core_uring passes reads, writes and flushes to the server.

Thin provisioned storage also needs to know which blocks the initiator
no longer uses.

Add TCMUR_OP_UNMAP and send one for each range of an UNMAP command or of
a WRITE SAME with the UNMAP bit. The core expects execute_unmap() to
return once the range is unmapped, so queue a struct tcmur_cmd that
lives on the stack and wait for the server to commit it.

Signed-off-by: Qiliang Yuan <qiliang.yuan@linux.dev>
---
 drivers/target/target_core_uring.c     | 39 ++++++++++++++++++++++++++++++++++
 include/uapi/linux/target_core_uring.h |  1 +
 2 files changed, 40 insertions(+)

diff --git a/drivers/target/target_core_uring.c b/drivers/target/target_core_uring.c
index f39bf7bf36d0f..366c5e9820956 100644
--- a/drivers/target/target_core_uring.c
+++ b/drivers/target/target_core_uring.c
@@ -49,6 +49,7 @@ struct tcmur_dev;
  * @ref:	Held by the tag or the pending list and by data copies in
  *		progress
  * @deadline:	When to give up waiting for the server
+ * @done:	Complete this instead of @se_cmd when the last reference goes
  * @status:	SAM status to complete @se_cmd with
  */
 struct tcmur_cmd {
@@ -59,6 +60,7 @@ struct tcmur_cmd {
 	struct tcmur_io_desc	desc;
 	refcount_t		ref;
 	unsigned long		deadline;
+	struct completion	*done;
 	u8			status;
 };
 
@@ -148,6 +150,11 @@ static void tcmur_cmd_put(struct tcmur_cmd *tcmd)
 	if (!refcount_dec_and_test(&tcmd->ref))
 		return;
 
+	if (tcmd->done) {
+		complete(tcmd->done);
+		return;
+	}
+
 	target_complete_cmd(tcmd->se_cmd, tcmd->status);
 	kmem_cache_free(tcmur_cmd_cache, tcmd);
 }
@@ -268,6 +275,36 @@ static sense_reason_t tcmur_execute_sync_cache(struct se_cmd *se_cmd)
 			    se_cmd->data_length, NULL, 0);
 }
 
+static sense_reason_t tcmur_execute_unmap(struct se_cmd *se_cmd, sector_t lba,
+					  sector_t nolb)
+{
+	u32 block_size = se_cmd->se_dev->dev_attrib.block_size;
+	DECLARE_COMPLETION_ONSTACK(done);
+	struct tcmur_cmd tcmd = {
+		.se_cmd		= se_cmd,
+		.done		= &done,
+		.desc.op	= TCMUR_OP_UNMAP,
+		.desc.offset	= lba * block_size,
+		.desc.len	= nolb * block_size,
+	};
+
+	/* Wait here, as the core completes the command after all ranges */
+	tcmur_queue_cmd(TCMUR_DEV(se_cmd->se_dev), &tcmd);
+	wait_for_completion(&done);
+
+	return tcmd.status ? TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE : 0;
+}
+
+static bool tcmur_configure_unmap(struct se_device *dev)
+{
+	dev->dev_attrib.max_unmap_lba_count = UINT_MAX /
+					      dev->dev_attrib.hw_block_size;
+	dev->dev_attrib.max_unmap_block_desc_count = 1;
+	dev->dev_attrib.unmap_granularity = 1;
+	dev->dev_attrib.unmap_granularity_alignment = 0;
+	return true;
+}
+
 static void tcmur_tmr_notify(struct se_device *dev, enum tcm_tmreq_table tmf,
 			     struct list_head *aborted_cmds)
 {
@@ -941,6 +978,7 @@ static sector_t tcmur_get_blocks(struct se_device *dev)
 static struct exec_cmd_ops tcmur_exec_cmd_ops = {
 	.execute_rw		= tcmur_execute_rw,
 	.execute_sync_cache	= tcmur_execute_sync_cache,
+	.execute_unmap		= tcmur_execute_unmap,
 };
 
 static sense_reason_t tcmur_parse_cdb(struct se_cmd *se_cmd)
@@ -959,6 +997,7 @@ static const struct target_backend_ops tcmur_ops = {
 	.configure_device	= tcmur_configure_device,
 	.destroy_device		= tcmur_destroy_device,
 	.free_device		= tcmur_free_device,
+	.configure_unmap	= tcmur_configure_unmap,
 	.parse_cdb		= tcmur_parse_cdb,
 	.tmr_notify		= tcmur_tmr_notify,
 	.set_configfs_dev_params = tcmur_set_configfs_dev_params,
diff --git a/include/uapi/linux/target_core_uring.h b/include/uapi/linux/target_core_uring.h
index a1d8ae47ebf7d..9df10558cfa9e 100644
--- a/include/uapi/linux/target_core_uring.h
+++ b/include/uapi/linux/target_core_uring.h
@@ -68,6 +68,7 @@ enum tcmur_io_op {
 	TCMUR_OP_READ,
 	TCMUR_OP_WRITE,
 	TCMUR_OP_FLUSH,
+	TCMUR_OP_UNMAP,
 };
 
 /* Put the write on stable storage before completing it */

-- 
2.43.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH RFC 14/18] scsi: target: uring: Handle WRITE SAME
  2026-11-16  0:17 [PATCH RFC 00/18] scsi: target: Add target_core_uring, an io_uring based user backstore Qiliang Yuan
                   ` (12 preceding siblings ...)
  2026-11-16  0:18 ` [PATCH RFC 13/18] scsi: target: uring: Implement UNMAP Qiliang Yuan
@ 2026-11-16  0:18 ` Qiliang Yuan
  2026-11-16  0:18 ` [PATCH RFC 15/18] scsi: target: uring: Register command data as io_uring fixed buffers Qiliang Yuan
                   ` (3 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Qiliang Yuan @ 2026-11-16  0:18 UTC (permalink / raw)
  To: Martin K. Petersen, Shuah Khan, Jonathan Corbet, Shuah Khan,
	Randy Dunlap
  Cc: linux-kernel, linux-scsi, target-devel, linux-kselftest,
	linux-doc, Jens Axboe, io-uring, Mike Christie, Bodo Stroesser,
	Ming Lei, Qiliang Yuan

target_core_uring does not set execute_write_same(), so the core rejects
WRITE SAME without the UNMAP bit.

Initiators use it to zero or initialize ranges without sending the data
for every block.

Add TCMUR_OP_WRITE_SAME. The descriptor carries the range to fill, and
the data of the command is the one block to repeat. Accept up to 65536
blocks per command.

Signed-off-by: Qiliang Yuan <qiliang.yuan@linux.dev>
---
 drivers/target/target_core_uring.c     | 14 ++++++++++++++
 include/uapi/linux/target_core_uring.h |  9 +++++----
 2 files changed, 19 insertions(+), 4 deletions(-)

diff --git a/drivers/target/target_core_uring.c b/drivers/target/target_core_uring.c
index 366c5e9820956..42283a1ae1512 100644
--- a/drivers/target/target_core_uring.c
+++ b/drivers/target/target_core_uring.c
@@ -30,6 +30,8 @@
 #define TCMUR_VERSION		"1.0"
 #define TCMUR_MINORS		(1U << MINORBITS)
 #define TCMUR_MAX_IO_SIZE	(4U << 20)
+/* Limit one WRITE SAME to this many blocks */
+#define TCMUR_MAX_WRITE_SAME_LEN	0x10000
 #define TCMUR_DEF_QUEUE_DEPTH	128
 #define TCMUR_DEF_BLOCK_SIZE	512
 #define TCMUR_DEF_CMD_TIMEOUT	30
@@ -295,6 +297,16 @@ static sense_reason_t tcmur_execute_unmap(struct se_cmd *se_cmd, sector_t lba,
 	return tcmd.status ? TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE : 0;
 }
 
+static sense_reason_t tcmur_execute_write_same(struct se_cmd *se_cmd)
+{
+	u32 block_size = se_cmd->se_dev->dev_attrib.block_size;
+
+	return tcmur_submit(se_cmd, TCMUR_OP_WRITE_SAME,
+			    se_cmd->t_task_lba * block_size,
+			    (u64)sbc_get_write_same_sectors(se_cmd) * block_size,
+			    se_cmd->t_data_sg, se_cmd->t_data_nents);
+}
+
 static bool tcmur_configure_unmap(struct se_device *dev)
 {
 	dev->dev_attrib.max_unmap_lba_count = UINT_MAX /
@@ -813,6 +825,7 @@ static int tcmur_configure_device(struct se_device *dev)
 	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.max_write_same_len = TCMUR_MAX_WRITE_SAME_LEN;
 	dev->dev_attrib.is_nonrot = 1;
 
 	udev->cdev_dev.devt = MKDEV(MAJOR(tcmur_devt), udev->dev_id);
@@ -979,6 +992,7 @@ static struct exec_cmd_ops tcmur_exec_cmd_ops = {
 	.execute_rw		= tcmur_execute_rw,
 	.execute_sync_cache	= tcmur_execute_sync_cache,
 	.execute_unmap		= tcmur_execute_unmap,
+	.execute_write_same	= tcmur_execute_write_same,
 };
 
 static sense_reason_t tcmur_parse_cdb(struct se_cmd *se_cmd)
diff --git a/include/uapi/linux/target_core_uring.h b/include/uapi/linux/target_core_uring.h
index 9df10558cfa9e..469340b152f15 100644
--- a/include/uapi/linux/target_core_uring.h
+++ b/include/uapi/linux/target_core_uring.h
@@ -69,6 +69,7 @@ enum tcmur_io_op {
 	TCMUR_OP_WRITE,
 	TCMUR_OP_FLUSH,
 	TCMUR_OP_UNMAP,
+	TCMUR_OP_WRITE_SAME,
 };
 
 /* Put the write on stable storage before completing it */
@@ -80,7 +81,7 @@ 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
+ * @data_len:	Bytes of data to transfer; one block for TCMUR_OP_WRITE_SAME
  * @reserved:	Zero
  *
  * The descriptors of a queue form an array indexed by tag, which the server
@@ -99,9 +100,9 @@ struct tcmur_io_desc {
 	(TCMUR_MAX_QUEUE_DEPTH * sizeof(struct tcmur_io_desc))
 
 /*
- * The data of a command is read (TCMUR_OP_WRITE) or written
- * (TCMUR_OP_READ) with pread()/pwrite() on the file, at the position
- * returned by tcmur_buf_pos().
+ * The data of a command is read (TCMUR_OP_WRITE, TCMUR_OP_WRITE_SAME) or
+ * written (TCMUR_OP_READ) with pread()/pwrite() on the file, at the
+ * position returned by tcmur_buf_pos().
  */
 #define TCMUR_BUF_TAG_SHIFT	32
 #define TCMUR_BUF_QID_SHIFT	48

-- 
2.43.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH RFC 15/18] scsi: target: uring: Register command data as io_uring fixed buffers
  2026-11-16  0:17 [PATCH RFC 00/18] scsi: target: Add target_core_uring, an io_uring based user backstore Qiliang Yuan
                   ` (13 preceding siblings ...)
  2026-11-16  0:18 ` [PATCH RFC 14/18] scsi: target: uring: Handle WRITE SAME Qiliang Yuan
@ 2026-11-16  0:18 ` Qiliang Yuan
  2026-11-16  0:18 ` [PATCH RFC 16/18] selftests: target_core_uring: Add a minimal server Qiliang Yuan
                   ` (2 subsequent siblings)
  17 siblings, 0 replies; 19+ messages in thread
From: Qiliang Yuan @ 2026-11-16  0:18 UTC (permalink / raw)
  To: Martin K. Petersen, Shuah Khan, Jonathan Corbet, Shuah Khan,
	Randy Dunlap
  Cc: linux-kernel, linux-scsi, target-devel, linux-kselftest,
	linux-doc, Jens Axboe, io-uring, Mike Christie, Bodo Stroesser,
	Ming Lei, Qiliang Yuan

The server of a target_core_uring device copies the data of each command
with pread() or pwrite().

For large commands that copy dominates, although the server often only
passes the data on to a file, a block device or a socket.

Add TCMUR_IO_REGISTER_BUF, which registers the pages of a command as a
fixed buffer of the server's io_uring, and TCMUR_IO_UNREGISTER_BUF. The
server then uses the buffer in fixed read and write requests on its
backing store. Let the buffer hold a reference on the command, so that
the command completes only after the buffer is released.

Exporting a 2 GiB device through tcm_loop to fio (io_uring, direct I/O,
iodepth 32 for 4k and 8 for 1M, 6 seconds per job) on a 32 CPU VM, with
32 queues and a server that keeps the data in a tmpfs file, copying
through a bounce buffer or using fixed buffers. Small commands are
slower with fixed buffers, as each one takes two more io_uring requests;
the server can choose per command. Three alternating runs (median,
min-max in parentheses):

  workload               copy (MB/s)         fixed buffer (MB/s)   change
  4k randread, 16 jobs    5808 (5463-5944)    5217 (5090-5463)       -10%
  4k randwrite, 16 jobs   2023 (1831-2101)    1845 (1571-1850)        -9%
  1M read, 4 jobs        21516 (10137-22138) 44973 (31086-45013)    +109%
  1M write, 4 jobs        5997 (5669-7364)    7430 (5970-8002)       +24%

Signed-off-by: Qiliang Yuan <qiliang.yuan@linux.dev>
---
 drivers/target/target_core_uring.c     | 60 ++++++++++++++++++++++++++++++++--
 include/uapi/linux/target_core_uring.h | 13 ++++++--
 2 files changed, 69 insertions(+), 4 deletions(-)

diff --git a/drivers/target/target_core_uring.c b/drivers/target/target_core_uring.c
index 42283a1ae1512..4f5cf18b1ec07 100644
--- a/drivers/target/target_core_uring.c
+++ b/drivers/target/target_core_uring.c
@@ -48,8 +48,8 @@ struct tcmur_dev;
  * @sgl:	Data pages of the command
  * @sgl_nents:	Number of entries in @sgl
  * @desc:	What the server is asked to do
- * @ref:	Held by the tag or the pending list and by data copies in
- *		progress
+ * @ref:	Held by the tag or the pending list, by data copies in
+ *		progress and by registered buffers
  * @deadline:	When to give up waiting for the server
  * @done:	Complete this instead of @se_cmd when the last reference goes
  * @status:	SAM status to complete @se_cmd with
@@ -489,6 +489,56 @@ static int tcmur_commit(struct tcmur_queue *q, struct tcmur_io *io, int result)
 	return 0;
 }
 
+static void tcmur_buf_release(void *priv)
+{
+	tcmur_cmd_put(priv);
+}
+
+static int tcmur_register_buf(struct tcmur_dev *udev, u16 q_id, u16 tag,
+			      struct io_uring_cmd *ucmd, unsigned int index,
+			      unsigned int issue_flags)
+{
+	struct tcmur_cmd *tcmd = tcmur_get_cmd(udev, q_id, tag);
+	unsigned int len, n = 0;
+	struct scatterlist *sg;
+	struct bio_vec *bvs;
+	int ret = -EINVAL;
+	u32 left;
+	int i;
+
+	if (!tcmd)
+		return -EINVAL;
+	if (!tcmd->desc.data_len)
+		goto put_cmd;
+
+	bvs = kmalloc_array(tcmd->sgl_nents, sizeof(*bvs), GFP_KERNEL);
+	if (!bvs) {
+		ret = -ENOMEM;
+		goto put_cmd;
+	}
+
+	left = tcmd->desc.data_len;
+	for_each_sg(tcmd->sgl, sg, tcmd->sgl_nents, i) {
+		len = min(sg->length, left);
+		bvec_set_page(&bvs[n++], sg_page(sg), len, sg->offset);
+		left -= len;
+		if (!left)
+			break;
+	}
+
+	/* Keep the command reference until the buffer is released */
+	ret = io_buffer_register_bvec(ucmd, bvs, n, tcmur_buf_release, tcmd,
+				      tcmd->desc.op == TCMUR_OP_READ ?
+				      IO_BUF_DEST : IO_BUF_SOURCE,
+				      index, issue_flags);
+	kfree(bvs);
+	if (!ret)
+		return 0;
+put_cmd:
+	tcmur_cmd_put(tcmd);
+	return ret;
+}
+
 static int tcmur_ch_uring_cmd_local(struct io_uring_cmd *ucmd,
 				    unsigned int issue_flags)
 {
@@ -515,6 +565,12 @@ static int tcmur_ch_uring_cmd_local(struct io_uring_cmd *ucmd,
 		if (ret)
 			return ret;
 		return tcmur_fetch(q, io, ucmd, issue_flags);
+	case TCMUR_IO_REGISTER_BUF:
+		return tcmur_register_buf(udev, q_id, tag, ucmd,
+					  READ_ONCE(ioc->addr), issue_flags);
+	case TCMUR_IO_UNREGISTER_BUF:
+		return io_buffer_unregister(ucmd, READ_ONCE(ioc->addr),
+					    issue_flags);
 	default:
 		return -EOPNOTSUPP;
 	}
diff --git a/include/uapi/linux/target_core_uring.h b/include/uapi/linux/target_core_uring.h
index 469340b152f15..92d3d2fc9c25e 100644
--- a/include/uapi/linux/target_core_uring.h
+++ b/include/uapi/linux/target_core_uring.h
@@ -45,13 +45,14 @@ struct tcmur_dev_info {
  * @tag:	Tag the command is for
  * @result:	TCMUR_IO_COMMIT_AND_FETCH: 0, or a negative errno if the
  *		command failed
- * @reserved1:	Zero
+ * @addr:	TCMUR_IO_REGISTER_BUF and TCMUR_IO_UNREGISTER_BUF: index in the
+ *		buffer table of the ring
  */
 struct tcmur_io_cmd {
 	__u16	q_id;
 	__u16	tag;
 	__s32	result;
-	__u64	reserved1;
+	__u64	addr;
 };
 
 /*
@@ -64,6 +65,14 @@ struct tcmur_io_cmd {
 #define TCMUR_IO_FETCH			_IOWR(0xBB, 0x08, struct tcmur_io_cmd)
 #define TCMUR_IO_COMMIT_AND_FETCH	_IOWR(0xBB, 0x09, struct tcmur_io_cmd)
 
+/*
+ * TCMUR_IO_REGISTER_BUF: register the data pages of the command of
+ * (q_id, tag) as fixed buffer @addr of the ring, so that the server can
+ * do I/O on them without copying. TCMUR_IO_UNREGISTER_BUF removes it.
+ */
+#define TCMUR_IO_REGISTER_BUF		_IOWR(0xBB, 0x0a, struct tcmur_io_cmd)
+#define TCMUR_IO_UNREGISTER_BUF		_IOWR(0xBB, 0x0b, struct tcmur_io_cmd)
+
 enum tcmur_io_op {
 	TCMUR_OP_READ,
 	TCMUR_OP_WRITE,

-- 
2.43.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH RFC 16/18] selftests: target_core_uring: Add a minimal server
  2026-11-16  0:17 [PATCH RFC 00/18] scsi: target: Add target_core_uring, an io_uring based user backstore Qiliang Yuan
                   ` (14 preceding siblings ...)
  2026-11-16  0:18 ` [PATCH RFC 15/18] scsi: target: uring: Register command data as io_uring fixed buffers Qiliang Yuan
@ 2026-11-16  0:18 ` Qiliang Yuan
  2026-11-16  0:18 ` [PATCH RFC 17/18] selftests: target_core_uring: Test a device exported through tcm_loop Qiliang Yuan
  2026-11-16  0:18 ` [PATCH RFC 18/18] Documentation: target: Describe the uring backstore Qiliang Yuan
  17 siblings, 0 replies; 19+ messages in thread
From: Qiliang Yuan @ 2026-11-16  0:18 UTC (permalink / raw)
  To: Martin K. Petersen, Shuah Khan, Jonathan Corbet, Shuah Khan,
	Randy Dunlap
  Cc: linux-kernel, linux-scsi, target-devel, linux-kselftest,
	linux-doc, Jens Axboe, io-uring, Mike Christie, Bodo Stroesser,
	Ming Lei, Qiliang Yuan

target_core_uring needs a userspace server to do anything.

There is none to test the backstore with, and no example of how to use
its interface.

Add tcmur_server, which serves a device from a backing file, from
memory or as a null device. It runs one thread and one io_uring per
queue. With -z it registers the data pages of reads and writes as fixed
buffers and does the I/O on the backing file without copying.

Signed-off-by: Qiliang Yuan <qiliang.yuan@linux.dev>
---
 MAINTAINERS                                        |   1 +
 .../testing/selftests/target_core_uring/.gitignore |   1 +
 tools/testing/selftests/target_core_uring/Makefile |   7 +
 .../selftests/target_core_uring/tcmur_server.c     | 310 +++++++++++++++++++++
 4 files changed, 319 insertions(+)

diff --git a/MAINTAINERS b/MAINTAINERS
index 07148430d9ea6..b3ac53cd7bf97 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -25331,6 +25331,7 @@ F:	Documentation/target/
 F:	drivers/target/
 F:	include/target/
 F:	include/uapi/linux/target_core_uring.h
+F:	tools/testing/selftests/target_core_uring/
 
 SCTP PROTOCOL
 M:	Marcelo Ricardo Leitner <marcelo.leitner@gmail.com>
diff --git a/tools/testing/selftests/target_core_uring/.gitignore b/tools/testing/selftests/target_core_uring/.gitignore
new file mode 100644
index 0000000000000..6e2f9115e1392
--- /dev/null
+++ b/tools/testing/selftests/target_core_uring/.gitignore
@@ -0,0 +1 @@
+tcmur_server
diff --git a/tools/testing/selftests/target_core_uring/Makefile b/tools/testing/selftests/target_core_uring/Makefile
new file mode 100644
index 0000000000000..438628fd1606b
--- /dev/null
+++ b/tools/testing/selftests/target_core_uring/Makefile
@@ -0,0 +1,7 @@
+# SPDX-License-Identifier: GPL-2.0
+CFLAGS += -O2 -Wall -D_GNU_SOURCE $(KHDR_INCLUDES)
+LDLIBS += -luring -lpthread
+
+TEST_GEN_PROGS_EXTENDED := tcmur_server
+
+include ../lib.mk
diff --git a/tools/testing/selftests/target_core_uring/tcmur_server.c b/tools/testing/selftests/target_core_uring/tcmur_server.c
new file mode 100644
index 0000000000000..128a0021e7796
--- /dev/null
+++ b/tools/testing/selftests/target_core_uring/tcmur_server.c
@@ -0,0 +1,310 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Minimal server for target_core_uring devices.
+ *
+ * usage: tcmur_server [-z] <tcmur device> null|ram|<backing file>
+ *
+ * Run one thread per queue. With -z, register the data pages of each read
+ * and write as a fixed buffer and do the I/O on the backing file without
+ * copying.
+ */
+#define _GNU_SOURCE
+#include <errno.h>
+#include <fcntl.h>
+#include <liburing.h>
+#include <pthread.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <linux/falloc.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <linux/target_core_uring.h>
+
+enum { BACKEND_NULL, BACKEND_RAM, BACKEND_FILE };
+enum { UD_FETCH, UD_REG, UD_RW, UD_UNREG };
+
+static struct tcmur_dev_info info;
+static int dev_fd, backing_fd = -1, backend;
+static bool zero_copy;
+static char *ram;
+
+struct queue {
+	pthread_t thread;
+	struct io_uring ring;
+	struct tcmur_io_desc *descs;
+	char *buf;
+	int q_id;
+};
+
+static struct io_uring_sqe *get_sqe(struct queue *q)
+{
+	struct io_uring_sqe *sqe = io_uring_get_sqe(&q->ring);
+
+	if (!sqe) {
+		io_uring_submit(&q->ring);
+		sqe = io_uring_get_sqe(&q->ring);
+	}
+	return sqe;
+}
+
+static struct io_uring_sqe *prep_cmd(struct queue *q, unsigned int op, int tag,
+				     int result, int kind)
+{
+	struct tcmur_io_cmd cmd = {
+		.q_id	= q->q_id,
+		.tag	= tag,
+		.result	= result,
+		.addr	= tag,
+	};
+	struct io_uring_sqe *sqe = get_sqe(q);
+
+	io_uring_prep_rw(IORING_OP_URING_CMD, sqe, dev_fd, NULL, 0, 0);
+	sqe->cmd_op = op;
+	memcpy(sqe->cmd, &cmd, sizeof(cmd));
+	sqe->user_data = (uint64_t)kind << 16 | tag;
+	return sqe;
+}
+
+static int copy_data(struct queue *q, int tag, void *buf, uint32_t len,
+		     bool to_cmd)
+{
+	uint64_t pos = tcmur_buf_pos(q->q_id, tag, 0);
+	ssize_t ret;
+
+	if (to_cmd)
+		ret = pwrite(dev_fd, buf, len, pos);
+	else
+		ret = pread(dev_fd, buf, len, pos);
+	return ret == len ? 0 : -EIO;
+}
+
+static int write_same(struct queue *q, int tag, struct tcmur_io_desc *d)
+{
+	uint64_t off;
+	int ret;
+
+	ret = copy_data(q, tag, q->buf, d->data_len, false);
+	if (ret)
+		return ret;
+
+	for (off = d->offset; off < d->offset + d->len; off += d->data_len) {
+		if (backend == BACKEND_RAM)
+			memcpy(ram + off, q->buf, d->data_len);
+		else if (pwrite(backing_fd, q->buf, d->data_len, off) !=
+			 d->data_len)
+			return -EIO;
+	}
+	return 0;
+}
+
+/* Start the I/O on the data pages of the command, without copying them */
+static void start_zero_copy(struct queue *q, int tag, struct tcmur_io_desc *d)
+{
+	struct io_uring_sqe *sqe;
+
+	sqe = prep_cmd(q, TCMUR_IO_REGISTER_BUF, tag, 0, UD_REG);
+	sqe->flags |= IOSQE_IO_LINK | IOSQE_CQE_SKIP_SUCCESS;
+
+	sqe = get_sqe(q);
+	if (d->op == TCMUR_OP_READ)
+		io_uring_prep_read_fixed(sqe, backing_fd, NULL, d->data_len,
+					 d->offset, tag);
+	else
+		io_uring_prep_write_fixed(sqe, backing_fd, NULL, d->data_len,
+					  d->offset, tag);
+	sqe->flags |= IOSQE_IO_HARDLINK;
+	sqe->user_data = (uint64_t)UD_RW << 16 | tag;
+
+	sqe = prep_cmd(q, TCMUR_IO_UNREGISTER_BUF, tag, 0, UD_UNREG);
+	sqe->flags |= IOSQE_CQE_SKIP_SUCCESS;
+}
+
+/* Return true if the command completes later */
+static bool handle(struct queue *q, int tag, int *result)
+{
+	struct tcmur_io_desc *d = &q->descs[tag];
+	bool rd = d->op == TCMUR_OP_READ;
+	int ret = 0;
+
+	switch (d->op) {
+	case TCMUR_OP_READ:
+	case TCMUR_OP_WRITE:
+		if (d->offset + d->len > info.dev_size) {
+			ret = -EINVAL;
+		} else if (backend == BACKEND_RAM) {
+			ret = copy_data(q, tag, ram + d->offset, d->data_len,
+					rd);
+		} else if (backend == BACKEND_FILE && zero_copy) {
+			start_zero_copy(q, tag, d);
+			return true;
+		} else if (backend == BACKEND_FILE && rd) {
+			if (pread(backing_fd, q->buf, d->data_len, d->offset) !=
+			    d->data_len)
+				ret = -EIO;
+			else
+				ret = copy_data(q, tag, q->buf, d->data_len,
+						true);
+		} else if (backend == BACKEND_FILE) {
+			ret = copy_data(q, tag, q->buf, d->data_len, false);
+			if (!ret && pwrite(backing_fd, q->buf, d->data_len,
+					   d->offset) != d->data_len)
+				ret = -EIO;
+		}
+		if (!ret && backend == BACKEND_FILE &&
+		    (d->flags & TCMUR_IO_F_FUA) && fdatasync(backing_fd))
+			ret = -errno;
+		break;
+	case TCMUR_OP_FLUSH:
+		if (backend == BACKEND_FILE && fdatasync(backing_fd))
+			ret = -errno;
+		break;
+	case TCMUR_OP_UNMAP:
+		if (backend == BACKEND_RAM)
+			memset(ram + d->offset, 0, d->len);
+		else if (backend == BACKEND_FILE &&
+			 fallocate(backing_fd, FALLOC_FL_PUNCH_HOLE |
+				   FALLOC_FL_KEEP_SIZE, d->offset, d->len))
+			ret = -errno;
+		break;
+	case TCMUR_OP_WRITE_SAME:
+		if (backend != BACKEND_NULL)
+			ret = write_same(q, tag, d);
+		break;
+	default:
+		ret = -EOPNOTSUPP;
+	}
+
+	*result = ret;
+	return false;
+}
+
+static void *queue_fn(void *arg)
+{
+	struct io_uring_params p = {
+		.flags = IORING_SETUP_SINGLE_ISSUER | IORING_SETUP_DEFER_TASKRUN,
+	};
+	struct queue *q = arg;
+	struct io_uring_cqe *cqe;
+	unsigned int head, n;
+	int tag, kind, result;
+
+	/* Create the ring here, as only its creator may submit to it */
+	if (io_uring_queue_init_params(info.queue_depth * 4, &q->ring, &p) ||
+	    (zero_copy &&
+	     io_uring_register_buffers_sparse(&q->ring, info.queue_depth))) {
+		perror("io_uring setup");
+		exit(1);
+	}
+
+	for (tag = 0; tag < info.queue_depth; tag++)
+		prep_cmd(q, TCMUR_IO_FETCH, tag, 0, UD_FETCH);
+
+	for (;;) {
+		if (io_uring_submit_and_wait(&q->ring, 1) < 0) {
+			perror("io_uring_submit_and_wait");
+			exit(1);
+		}
+
+		n = 0;
+		io_uring_for_each_cqe(&q->ring, head, cqe) {
+			n++;
+			tag = cqe->user_data & 0xffff;
+			kind = cqe->user_data >> 16;
+
+			switch (kind) {
+			case UD_FETCH:
+				if (cqe->res < 0)
+					goto out;
+				if (handle(q, tag, &result))
+					continue;
+				break;
+			case UD_RW:
+				result = cqe->res == q->descs[tag].data_len ?
+					 0 : -EIO;
+				break;
+			default:
+				/* Report failures through the I/O that follows */
+				continue;
+			}
+			prep_cmd(q, TCMUR_IO_COMMIT_AND_FETCH, tag, result,
+				 UD_FETCH);
+		}
+		io_uring_cq_advance(&q->ring, n);
+	}
+out:
+	return NULL;
+}
+
+int main(int argc, char **argv)
+{
+	struct queue *queues;
+	size_t desc_size;
+	int i, opt;
+
+	while ((opt = getopt(argc, argv, "z")) != -1) {
+		if (opt == 'z')
+			zero_copy = true;
+		else
+			return 1;
+	}
+	if (argc - optind != 2) {
+		fprintf(stderr, "usage: %s [-z] <tcmur device> null|ram|<backing file>\n",
+			argv[0]);
+		return 1;
+	}
+
+	dev_fd = open(argv[optind], O_RDWR);
+	if (dev_fd < 0 || ioctl(dev_fd, TCMUR_GET_DEV_INFO, &info)) {
+		perror(argv[optind]);
+		return 1;
+	}
+
+	if (!strcmp(argv[optind + 1], "null")) {
+		backend = BACKEND_NULL;
+	} else if (!strcmp(argv[optind + 1], "ram")) {
+		backend = BACKEND_RAM;
+		ram = mmap(NULL, info.dev_size, PROT_READ | PROT_WRITE,
+			   MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE, -1, 0);
+		if (ram == MAP_FAILED) {
+			perror("mmap");
+			return 1;
+		}
+	} else {
+		backend = BACKEND_FILE;
+		backing_fd = open(argv[optind + 1], O_RDWR);
+		if (backing_fd < 0) {
+			perror(argv[optind + 1]);
+			return 1;
+		}
+	}
+	if (zero_copy && backend != BACKEND_FILE) {
+		fprintf(stderr, "-z needs a backing file\n");
+		return 1;
+	}
+
+	desc_size = info.queue_depth * sizeof(struct tcmur_io_desc);
+	desc_size = (desc_size + getpagesize() - 1) & ~(getpagesize() - 1);
+	queues = calloc(info.nr_queues, sizeof(*queues));
+	for (i = 0; i < info.nr_queues; i++) {
+		struct queue *q = &queues[i];
+
+		q->q_id = i;
+		q->buf = malloc(info.max_io_size);
+		q->descs = mmap(NULL, desc_size, PROT_READ, MAP_SHARED, dev_fd,
+				(off_t)i * TCMUR_DESC_AREA_SIZE);
+		if (!q->buf || q->descs == MAP_FAILED) {
+			perror("queue setup");
+			return 1;
+		}
+	}
+
+	for (i = 0; i < info.nr_queues; i++)
+		pthread_create(&queues[i].thread, NULL, queue_fn, &queues[i]);
+	for (i = 0; i < info.nr_queues; i++)
+		pthread_join(queues[i].thread, NULL);
+	return 0;
+}

-- 
2.43.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH RFC 17/18] selftests: target_core_uring: Test a device exported through tcm_loop
  2026-11-16  0:17 [PATCH RFC 00/18] scsi: target: Add target_core_uring, an io_uring based user backstore Qiliang Yuan
                   ` (15 preceding siblings ...)
  2026-11-16  0:18 ` [PATCH RFC 16/18] selftests: target_core_uring: Add a minimal server Qiliang Yuan
@ 2026-11-16  0:18 ` Qiliang Yuan
  2026-11-16  0:18 ` [PATCH RFC 18/18] Documentation: target: Describe the uring backstore Qiliang Yuan
  17 siblings, 0 replies; 19+ messages in thread
From: Qiliang Yuan @ 2026-11-16  0:18 UTC (permalink / raw)
  To: Martin K. Petersen, Shuah Khan, Jonathan Corbet, Shuah Khan,
	Randy Dunlap
  Cc: linux-kernel, linux-scsi, target-devel, linux-kselftest,
	linux-doc, Jens Axboe, io-uring, Mike Christie, Bodo Stroesser,
	Ming Lei, Qiliang Yuan

tcmur_server can serve a target_core_uring device from a backing file.

Nothing checks that the backstore and the server work together.

Add a test that exports a device through tcm_loop and compares the SCSI
disk with the backing file after writes, reads, flushes, UNMAP and
WRITE SAME, with and without zero copy. Also check that a new server
takes over after the first one is killed, and that commands time out
without a server.

Signed-off-by: Qiliang Yuan <qiliang.yuan@linux.dev>
---
 tools/testing/selftests/Makefile                   |   1 +
 tools/testing/selftests/target_core_uring/Makefile |   1 +
 tools/testing/selftests/target_core_uring/config   |   5 +
 .../selftests/target_core_uring/tcmur_test.sh      | 137 +++++++++++++++++++++
 4 files changed, 144 insertions(+)

diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile
index 07c3719f8b04b..8e97d4b374a09 100644
--- a/tools/testing/selftests/Makefile
+++ b/tools/testing/selftests/Makefile
@@ -131,6 +131,7 @@ TARGETS += static_keys
 TARGETS += sync
 TARGETS += syscall_user_dispatch
 TARGETS += sysctl
+TARGETS += target_core_uring
 TARGETS += tc-testing
 TARGETS += tdx
 TARGETS += thermal/intel/power_floor
diff --git a/tools/testing/selftests/target_core_uring/Makefile b/tools/testing/selftests/target_core_uring/Makefile
index 438628fd1606b..202f174a17e08 100644
--- a/tools/testing/selftests/target_core_uring/Makefile
+++ b/tools/testing/selftests/target_core_uring/Makefile
@@ -2,6 +2,7 @@
 CFLAGS += -O2 -Wall -D_GNU_SOURCE $(KHDR_INCLUDES)
 LDLIBS += -luring -lpthread
 
+TEST_PROGS := tcmur_test.sh
 TEST_GEN_PROGS_EXTENDED := tcmur_server
 
 include ../lib.mk
diff --git a/tools/testing/selftests/target_core_uring/config b/tools/testing/selftests/target_core_uring/config
new file mode 100644
index 0000000000000..9115bf367c169
--- /dev/null
+++ b/tools/testing/selftests/target_core_uring/config
@@ -0,0 +1,5 @@
+CONFIG_IO_URING=y
+CONFIG_TARGET_CORE=m
+CONFIG_TCM_URING=m
+CONFIG_LOOPBACK_TARGET=m
+CONFIG_BLK_DEV_SD=m
diff --git a/tools/testing/selftests/target_core_uring/tcmur_test.sh b/tools/testing/selftests/target_core_uring/tcmur_test.sh
new file mode 100755
index 0000000000000..99f68e74c7cd0
--- /dev/null
+++ b/tools/testing/selftests/target_core_uring/tcmur_test.sh
@@ -0,0 +1,137 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+#
+# Export a target_core_uring device through tcm_loop, serve it with
+# tcmur_server and check the resulting SCSI disk against the backing file.
+
+DIR=$(dirname "$(readlink -f "$0")")
+KSFT_SKIP=4
+T=/sys/kernel/config/target
+C=$T/core/uring_70/tcmur_test
+WWN=naa.5001405074636d75
+L=$T/loopback/$WWN/tpgt_1
+SIZE=$((256 << 20))
+IMG=$(mktemp /tmp/tcmur_test.XXXXXX)
+PAT=$(mktemp /tmp/tcmur_test.XXXXXX)
+server=
+dev=
+fail=0
+
+[ "$(id -u)" = 0 ] || { echo "SKIP: need root"; exit $KSFT_SKIP; }
+modprobe -q target_core_uring && modprobe -q tcm_loop ||
+	{ echo "SKIP: need target_core_uring and tcm_loop"; exit $KSFT_SKIP; }
+
+check() {	# <name> <command...>
+	local name=$1; shift
+
+	if "$@"; then
+		echo "ok $name"
+	else
+		echo "not ok $name"
+		fail=1
+	fi
+}
+
+# setup <control parameters> <serve|idle> [server arguments...]
+setup() {
+	local params=$1 serve=$2 before cdev; shift 2
+
+	truncate -s 0 "$IMG"; truncate -s $SIZE "$IMG"
+	mkdir -p $C
+	echo "dev_size=$SIZE,nr_queues=2,queue_depth=32$params" > $C/control
+	echo 1 > $C/enable
+	echo 1 > $C/attrib/emulate_tpu
+	echo 1 > $C/attrib/emulate_tpws
+	echo 1 > $C/attrib/emulate_write_cache
+	cdev=$(grep -o '/dev/tcmur[0-9]*' $C/info)
+	udevadm settle
+
+	if [ "$serve" = serve ]; then
+		"$DIR"/tcmur_server "$@" "$cdev" "$IMG" &
+		server=$!
+		sleep 1
+	fi
+
+	before=$(lsblk -dno NAME | sort)
+	mkdir -p $L
+	echo naa.5001405074636d76 > $L/nexus
+	mkdir $L/lun/lun_0
+	ln -s $C $L/lun/lun_0/tcmur_test
+	for _ in $(seq 40); do
+		dev=$(comm -13 <(echo "$before") <(lsblk -dno NAME | sort) | head -1)
+		[ -n "$dev" ] && break
+		sleep 0.5
+	done
+	udevadm settle
+	dev=/dev/$dev
+}
+
+teardown() {
+	rm $L/lun/lun_0/tcmur_test
+	rmdir $L/lun/lun_0 $L $T/loopback/$WWN
+	rmdir $C "$(dirname $C)"
+	# Wait for the server, which exits when the device goes
+	if [ -n "$server" ]; then
+		wait $server 2>/dev/null
+		server=
+	fi
+}
+
+same_as_image() {	# <offset MiB> <count MiB> <file>
+	cmp -s <(dd if="$IMG" bs=1M skip="$1" count="$2" status=none) "$3"
+}
+
+test_io() {	# <name> [server arguments...]
+	local name=$1 blocks; shift
+
+	setup "" serve "$@"
+	dd if=/dev/urandom of="$PAT" bs=1M count=8 status=none
+
+	dd if="$PAT" of=$dev bs=1M seek=16 oflag=direct status=none
+	check "$name write" same_as_image 16 8 "$PAT"
+	check "$name read" cmp -s \
+		<(dd if=$dev bs=1M skip=16 count=8 iflag=direct status=none) "$PAT"
+	check "$name flush" dd if="$PAT" of=$dev bs=1M seek=32 conv=fsync \
+		status=none
+
+	blocks=$(stat -c %b "$IMG")
+	blkdiscard -o $((16 << 20)) -l $((8 << 20)) $dev
+	check "$name unmap" [ "$(stat -c %b "$IMG")" -lt "$blocks" ]
+
+	if command -v sg_write_same > /dev/null; then
+		dd if=/dev/zero of="$PAT" bs=1M count=1 status=none
+		sg_write_same --in=/dev/zero --lba=$((32 << 11)) --num=2048 \
+			--xferlen=512 $dev > /dev/null 2>&1
+		check "$name write same" same_as_image 32 1 "$PAT"
+	else
+		echo "ok $name write same # SKIP no sg_write_same"
+	fi
+	teardown
+}
+
+test_io copy
+test_io "zero copy" -z
+
+# Check that a new server takes over after the first one is killed
+setup "" serve
+kill -9 $server; wait $server 2>/dev/null
+for _ in $(seq 20); do
+	"$DIR"/tcmur_server "$(grep -o '/dev/tcmur[0-9]*' $C/info)" "$IMG" \
+		2>/dev/null &
+	server=$!
+	sleep 0.5
+	kill -0 $server 2>/dev/null && break
+done
+check "server restart" dd if=$dev of=/dev/null bs=1M count=8 iflag=direct \
+	status=none
+teardown
+
+# Check that commands fail after cmd_time_out seconds without a server
+setup ",cmd_time_out=1" idle
+start=$(date +%s)
+dd if=$dev of=/dev/null bs=4k count=1 iflag=direct status=none 2>/dev/null
+check "command timeout" [ $? -ne 0 -a $(($(date +%s) - start)) -lt 30 ]
+teardown
+
+rm -f "$IMG" "$PAT"
+exit $fail

-- 
2.43.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

* [PATCH RFC 18/18] Documentation: target: Describe the uring backstore
  2026-11-16  0:17 [PATCH RFC 00/18] scsi: target: Add target_core_uring, an io_uring based user backstore Qiliang Yuan
                   ` (16 preceding siblings ...)
  2026-11-16  0:18 ` [PATCH RFC 17/18] selftests: target_core_uring: Test a device exported through tcm_loop Qiliang Yuan
@ 2026-11-16  0:18 ` Qiliang Yuan
  17 siblings, 0 replies; 19+ messages in thread
From: Qiliang Yuan @ 2026-11-16  0:18 UTC (permalink / raw)
  To: Martin K. Petersen, Shuah Khan, Jonathan Corbet, Shuah Khan,
	Randy Dunlap
  Cc: linux-kernel, linux-scsi, target-devel, linux-kselftest,
	linux-doc, Jens Axboe, io-uring, Mike Christie, Bodo Stroesser,
	Ming Lei, Qiliang Yuan

target_core_uring is a new LIO backstore served by a userspace process.

Its configfs parameters and the protocol between the kernel and the
server are only described by the uAPI header and the code.

Add tcm-uring.rst, which explains how the backstore differs from
TCM/USER, how to create a device and what a server has to do.

Signed-off-by: Qiliang Yuan <qiliang.yuan@linux.dev>
---
 Documentation/target/index.rst     |  1 +
 Documentation/target/tcm-uring.rst | 84 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 85 insertions(+)

diff --git a/Documentation/target/index.rst b/Documentation/target/index.rst
index 51fa8ebc652ef..253b5fe422732 100644
--- a/Documentation/target/index.rst
+++ b/Documentation/target/index.rst
@@ -8,5 +8,6 @@ TCM Virtual Device
     :maxdepth: 1
 
     tcmu-design
+    tcm-uring
     tcm_mod_builder
     scripts
diff --git a/Documentation/target/tcm-uring.rst b/Documentation/target/tcm-uring.rst
new file mode 100644
index 0000000000000..2911d80721f75
--- /dev/null
+++ b/Documentation/target/tcm-uring.rst
@@ -0,0 +1,84 @@
+.. SPDX-License-Identifier: GPL-2.0
+
+======================
+TCM io_uring backstore
+======================
+
+target_core_uring is a LIO backstore whose block commands are served by a
+userspace process, like TCM/USER (see tcmu-design.rst). It differs from
+TCM/USER in three ways:
+
+- The kernel emulates the SCSI commands. The server only reads, writes,
+  flushes and unmaps blocks and never parses a CDB.
+- A device has several queues. Each queue is served through its own
+  io_uring, so that queues do not contend with each other.
+- Data is copied once, with pread() and pwrite(), or not at all when the
+  server registers the data pages of a command as a fixed buffer.
+
+Creating a device
+=================
+
+A device is created through configfs like any other backstore::
+
+    mkdir -p /sys/kernel/config/target/core/uring_0/disk0
+    cd /sys/kernel/config/target/core/uring_0/disk0
+    echo "dev_size=1073741824,nr_queues=4,queue_depth=128" > control
+    echo 1 > enable
+
+The ``control`` file takes these parameters:
+
+``dev_size``
+    Size of the device in bytes. It must be a multiple of the block size.
+
+``block_size``
+    Logical block size in bytes, 512 by default.
+
+``nr_queues``
+    Number of queues, 1 by default. A command goes to the queue selected by
+    the CPU it was submitted on.
+
+``queue_depth``
+    Number of commands the server can work on per queue, 128 by default.
+
+``cmd_time_out``
+    Seconds after which a command the server has not completed fails. 30 by
+    default; 0 waits forever.
+
+Once the device is enabled, the ``info`` file shows the character device
+the server has to open, for instance ``/dev/tcmur0``.
+
+Serving a device
+================
+
+The interface is defined in ``include/uapi/linux/target_core_uring.h``.
+Only one process can open the character device at a time. The server:
+
+1. Reads the geometry with the ``TCMUR_GET_DEV_INFO`` ioctl.
+2. Maps the descriptors of each queue read-only, at offset
+   ``q_id * TCMUR_DESC_AREA_SIZE``. The area holds one
+   ``struct tcmur_io_desc`` per tag.
+3. Issues one ``TCMUR_IO_FETCH`` io_uring command (``IORING_OP_URING_CMD``)
+   per tag. A fetch command completes with 0 once the descriptor of its tag
+   holds a command.
+4. Handles the command and issues ``TCMUR_IO_COMMIT_AND_FETCH`` with the
+   result, which also waits for the next command of the tag.
+
+The server owns a tag from the completion of the fetch command until it
+commits the result. Meanwhile it accesses the data of the command in one of
+two ways:
+
+- pread() and pwrite() on the character device at the position returned by
+  ``tcmur_buf_pos()``. Reading returns the data of a write command; writing
+  fills the data of a read command.
+- ``TCMUR_IO_REGISTER_BUF`` registers the data pages as a fixed buffer of
+  the io_uring, which the server then passes to ``IORING_OP_READ_FIXED`` or
+  ``IORING_OP_WRITE_FIXED`` on its backing store. The buffer has to be
+  removed with ``TCMUR_IO_UNREGISTER_BUF``; the command only completes once
+  it is gone.
+
+Commands that arrive while no tag is waiting stay queued in the kernel.
+When the server exits, the commands it was working on fail and the queued
+ones wait for the next server, until they time out.
+
+``tools/testing/selftests/target_core_uring/tcmur_server.c`` is a small
+server that implements all of the above.

-- 
2.43.0


^ permalink raw reply related	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2026-11-16  0:20 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-16  0:17 [PATCH RFC 00/18] scsi: target: Add target_core_uring, an io_uring based user backstore Qiliang Yuan
2026-11-16  0:17 ` [PATCH RFC 01/18] scsi: target: uring: Introduce " Qiliang Yuan
2026-11-16  0:17 ` [PATCH RFC 02/18] scsi: target: uring: Allocate per-queue command descriptors Qiliang Yuan
2026-11-16  0:17 ` [PATCH RFC 03/18] scsi: target: uring: Create a character device per backstore device Qiliang Yuan
2026-11-16  0:17 ` [PATCH RFC 04/18] scsi: target: uring: Map the command descriptors into the server Qiliang Yuan
2026-11-16  0:17 ` [PATCH RFC 05/18] scsi: target: uring: Queue commands until a server fetches them Qiliang Yuan
2026-11-16  0:17 ` [PATCH RFC 06/18] scsi: target: uring: Hand commands out through TCMUR_IO_FETCH Qiliang Yuan
2026-11-16  0:18 ` [PATCH RFC 07/18] scsi: target: uring: Complete commands with TCMUR_IO_COMMIT_AND_FETCH Qiliang Yuan
2026-11-16  0:18 ` [PATCH RFC 08/18] scsi: target: uring: Copy command data with pread() and pwrite() Qiliang Yuan
2026-11-16  0:18 ` [PATCH RFC 09/18] scsi: target: uring: Fail in-flight commands when the server exits Qiliang Yuan
2026-11-16  0:18 ` [PATCH RFC 10/18] scsi: target: uring: Time out commands the server does not complete Qiliang Yuan
2026-11-16  0:18 ` [PATCH RFC 11/18] scsi: target: uring: Drop aborted commands that are still queued Qiliang Yuan
2026-11-16  0:18 ` [PATCH RFC 12/18] scsi: target: uring: Support SYNCHRONIZE CACHE Qiliang Yuan
2026-11-16  0:18 ` [PATCH RFC 13/18] scsi: target: uring: Implement UNMAP Qiliang Yuan
2026-11-16  0:18 ` [PATCH RFC 14/18] scsi: target: uring: Handle WRITE SAME Qiliang Yuan
2026-11-16  0:18 ` [PATCH RFC 15/18] scsi: target: uring: Register command data as io_uring fixed buffers Qiliang Yuan
2026-11-16  0:18 ` [PATCH RFC 16/18] selftests: target_core_uring: Add a minimal server Qiliang Yuan
2026-11-16  0:18 ` [PATCH RFC 17/18] selftests: target_core_uring: Test a device exported through tcm_loop Qiliang Yuan
2026-11-16  0:18 ` [PATCH RFC 18/18] Documentation: target: Describe the uring backstore Qiliang Yuan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox