The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH v4 0/2] scsi: bsg: release io_uring requests from end-I/O
@ 2026-11-18 15:37 Yingjie Wang
  2026-11-18 15:37 ` [PATCH v4 1/2] block: bsg: add a per-file pool of uring_cmd completion buffers Yingjie Wang
  2026-11-18 15:37 ` [PATCH v4 2/2] scsi: bsg: release io_uring requests from end-I/O Yingjie Wang
  0 siblings, 2 replies; 3+ messages in thread
From: Yingjie Wang @ 2026-11-18 15:37 UTC (permalink / raw)
  To: Martin K. Petersen
  Cc: James.Bottomley@HansenPartnership.com, FUJITA Tomonori,
	axboe@kernel.dk, linux-scsi@vger.kernel.org,
	linux-block@vger.kernel.org, bvanassche@acm.org,
	jthoughton@google.com, yangxiuwei@kylinos.cn,
	linux-kernel@vger.kernel.org

This is the reworked bsg half of the series "scsi: release completed
passthrough requests before user copyback" (RFC v3, 2026-11-07).  The
four synchronous patches have been posted separately as a non-RFC v4
series.  These two patches apply on top of that series:

https://lore.kernel.org/r/cover.1794990780.git.x-professor@outlook.com

Original RFC v3 series:
https://lore.kernel.org/r/cover.1794045010.git.x-professor@outlook.com

Yang's suggestion:
https://lore.kernel.org/r/20261111003655.3678579-1-yangxiuwei@kylinos.cn

Compared to the RFC v3 version of this half:

- RFC v3 5/6 exposed a new io_uring helper that handed the copied-SQE
  storage of a uring command to the driver.  Yang Xiuwei pointed out
  that storage owned by bsg itself avoids coupling the BSG driver to
  the io_uring layout and lifetime, with a per-file or per-queue pool
  and -EAGAIN on exhaustion instead of sleeping.  That removes the need
  for the io_uring contract ruling that RFC v3 asked for; the io_uring
  helper is dropped, and the reworked series no longer changes io_uring
  code.

- Patch 1 adds a per-file pool of 96-byte completion buffers to bsg,
  allocated at open() and freed at release().  Every open() of a SCSI
  bsg device creates a pool, so it is charged to the opener's memory
  cgroup; transport bsg devices and kernels built without io_uring
  allocate no pool.  Patch 2 checks a buffer out at issue time,
  snapshots the sense data in end-I/O, and returns the buffer in task
  work before posting the CQE.  Only commands that pass a response
  buffer and a nonzero response length can deliver sense bytes, so only
  those consume a pool buffer.  The buffer is taken before the user
  data buffer is mapped, because unmapping a bounce-mapped READ copies
  zero-filled bounce pages back to the caller: a command rejected after
  mapping would overwrite the caller's data.

- When the pool is exhausted, submission returns -EAGAIN without
  waiting for a buffer.  An initial nonblocking attempt may be retried
  through io-wq; if the pool is still empty in the worker, the command
  completes with -EAGAIN and userspace must resubmit it to retry.  This
  introduces a per-file limit of 64 commands that pass both a response
  buffer and a nonzero response length and are in flight or waiting for
  task work.
  64 is BSG_DEFAULT_CMDS, the default bsg reports through
  SG_GET_COMMAND_Q, but the old io_uring path enforced no such limit,
  and SG_SET_COMMAND_Q does not resize the pool.  The depth can be
  raised in the code if deeper queues on one file matter; each further
  64 slots cost about another 8.6 KiB per open file.

I chose a per-file pool over a per-queue one so that one opener cannot
exhaust the buffers available to other openers and make their commands
fail with -EAGAIN, for example with many commands in flight, or with a
DEFER_TASKRUN ring whose task has not entered the kernel to wait for
completions, so its task work has not run.  It also makes the
64-command limit apply per file rather than to the whole device.  The
cost is one pool per open file, about 8.6 KiB of slab on x86-64,
including for openers that only use SG_IO, on kernels built with
io_uring.

Tested on a KASAN+lockdep kernel in QEMU with a virtio-scsi disk: the
existing regression from the synchronous series passes (960
assertions, 740 of them on the BSG io_uring path), and its 20 deferred
io_uring trace windows now retain no request, where the synchronous
series alone leaves one in each.  The canceled-task-work regression
passes.  A new pool test submits one 256-deep batch on a DEFER_TASKRUN
ring, which runs task work only when the task waits for completions:
the 192 commands beyond the pool depth complete with -EAGAIN instead of
sleeping, all of them complete on resubmission with the expected
status and sense length, and all 64 buffers are available afterwards.
With all 64 buffers held, an unaligned READ is rejected before its data
buffer is mapped, so the caller's data is untouched.  No kernel
diagnostics.  Both patches build with W=1 on x86-64; the final objects
also build on i386 and with IO_URING=n and SCSI=m.

An earlier memcg test of the same allocation code, before this rebase,
confirmed the accounting on a non-debug kernel: each bsg open was
charged 8832 bytes more than an open of /dev/null, and with memory.max
at 32 MiB, repeated opens triggered the memcg OOM killer after about
3600 opens.

To check the overhead of this series, earlier tests of the same
implementation, before this rebase, timed batches of INQUIRY commands
through a QEMU virtio-scsi disk on a non-debug x86-64 kernel (one fd,
normal and DEFER_TASKRUN rings; batch time divided by the number of
commands, not per-command latency).  Up to 64 commands per batch, the
time per command was within about 2% of the kernel without these two
patches, with or without a response buffer.  At 96 with a response
buffer, the 32 commands beyond the pool depth completed with -EAGAIN
and were resubmitted after the batch, adding about 17% per command.
These numbers come from one VM, not real hardware.

Both patches are tagged for stable.  Patch 2 fixes 7b6d3255e7f8
("scsi: bsg: add io_uring passthrough handler") and does not build
without patch 1, so a backport needs both, in order.  Both are larger
than the 100 lines the stable rules ask for; if that rules them out,
I am fine with dropping the tags.

Yingjie Wang (2):
  block: bsg: add a per-file pool of uring_cmd completion buffers
  scsi: bsg: release io_uring requests from end-I/O

 block/bsg-lib.c         |   3 +-
 block/bsg.c             | 141 +++++++++++++++++++++++++++++++++++++++-
 drivers/scsi/scsi_bsg.c | 118 ++++++++++++++++++++++++---------
 include/linux/bsg.h     |   6 +-
 4 files changed, 232 insertions(+), 36 deletions(-)


base-commit: 9f9e0b1e5399045982788a954d3461de894e40c7
prerequisite-patch-id: a314d6d602c04c1f965a95edc39f159adfabdc2f
prerequisite-patch-id: d2d516da234fb37cd463dfc6292a62fcdcc6673f
prerequisite-patch-id: ce66c68fb99ef813e5506ba7f3de512c1fd0c832
prerequisite-patch-id: 9de82eb99c62cae2c0a4a266744008e25a241fc2
-- 
2.43.0


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

* [PATCH v4 1/2] block: bsg: add a per-file pool of uring_cmd completion buffers
  2026-11-18 15:37 [PATCH v4 0/2] scsi: bsg: release io_uring requests from end-I/O Yingjie Wang
@ 2026-11-18 15:37 ` Yingjie Wang
  2026-11-18 15:37 ` [PATCH v4 2/2] scsi: bsg: release io_uring requests from end-I/O Yingjie Wang
  1 sibling, 0 replies; 3+ messages in thread
From: Yingjie Wang @ 2026-11-18 15:37 UTC (permalink / raw)
  To: Martin K. Petersen
  Cc: James.Bottomley@HansenPartnership.com, FUJITA Tomonori,
	axboe@kernel.dk, linux-scsi@vger.kernel.org,
	linux-block@vger.kernel.org, bvanassche@acm.org,
	jthoughton@google.com, yangxiuwei@kylinos.cn,
	linux-kernel@vger.kernel.org

A bsg uring_cmd driver may need to pass data that must outlive the
block request to its task work: scsi_bsg has to snapshot sense data
when it stops keeping completed requests until task work runs (next
patch).

Let a bsg driver request a pool of fixed-size buffers for each open
file, allocated at open() time and freed at release().  The driver
declares the buffer size it needs when the queue is registered with
bsg_register_queue(), and checks a buffer out with
bsg_uring_cmd_get_completion_buf() at issue time.  When the pool is
exhausted, the caller can reject the command with -EAGAIN instead of
allocating or sleeping in the I/O path.

On a kernel built with io_uring, bsg allocates a pool at open() for
drivers that register a nonzero completion buffer size.  The pool lives
as long as the file, so charge it to the opener's memory cgroup with
GFP_KERNEL_ACCOUNT.  Without io_uring no uring_cmd can be issued, so no
pool is allocated.

The pool depth is BSG_DEFAULT_CMDS (64), the value bsg reports by
default through SG_GET_COMMAND_Q.  For SCSI the buffers take 6 KiB per
open file.  A buffer stays checked out from issue until task work
returns it, so the depth caps the number of commands per file that
hold a buffer.  The depth is deliberately not tied to the device queue
depth: end-I/O releases the request tag, so the number of commands
waiting for task work is not bounded by can_queue.  If the pool were
sized to a device queue depth of 1, its only buffer could still be
held by a completed command waiting for task work after that command's
tag was freed; the next command would then fail with -EAGAIN although
a tag is available.

Each buffer must be returned exactly once, using
bsg_uring_cmd_put_completion_buf() before the command completes or
fails.  The pool warns if a buffer is returned while it is already free.

io_uring holds a reference on the file for the lifetime of a request,
and a driver returns its buffer before completing the command, so
bsg_release() cannot free a pool that still has buffers checked out.

The SCSI registration passes SCSI_SENSE_BUFFERSIZE; transport bsg
registers no uring_cmd handler and passes 0, so it allocates no pool.
No in-tree caller checks buffers out yet; scsi_bsg adopts the pool in
the next patch.

Suggested-by: Yang Xiuwei <yangxiuwei@kylinos.cn>
Cc: stable@vger.kernel.org
Signed-off-by: Yingjie Wang <x-professor@outlook.com>
---
 block/bsg-lib.c         |   3 +-
 block/bsg.c             | 141 +++++++++++++++++++++++++++++++++++++++-
 drivers/scsi/scsi_bsg.c |   2 +-
 include/linux/bsg.h     |   6 +-
 4 files changed, 147 insertions(+), 5 deletions(-)

diff --git a/block/bsg-lib.c b/block/bsg-lib.c
index 536cee4430a7..6f9aedd9c8e9 100644
--- a/block/bsg-lib.c
+++ b/block/bsg-lib.c
@@ -400,7 +400,8 @@ struct request_queue *bsg_setup_queue(struct device *dev, const char *name,
 
 	blk_queue_rq_timeout(q, BLK_DEFAULT_SG_TIMEOUT);
 
-	bset->bd = bsg_register_queue(q, dev, name, bsg_transport_sg_io_fn, NULL);
+	bset->bd = bsg_register_queue(q, dev, name, bsg_transport_sg_io_fn,
+				      NULL, 0);
 	if (IS_ERR(bset->bd)) {
 		ret = PTR_ERR(bset->bd);
 		goto out_cleanup_queue;
diff --git a/block/bsg.c b/block/bsg.c
index b296fa373292..ccd9323c39f8 100644
--- a/block/bsg.c
+++ b/block/bsg.c
@@ -28,6 +28,7 @@ struct bsg_device {
 	int max_queue;
 	unsigned int timeout;
 	unsigned int reserved_size;
+	size_t completion_buf_size;
 	bsg_sg_io_fn *sg_io_fn;
 	bsg_uring_cmd_fn *uring_cmd_fn;
 };
@@ -40,6 +41,30 @@ static inline struct bsg_device *to_bsg_device(struct inode *inode)
 #define BSG_DEFAULT_CMDS	64
 #define BSG_MAX_DEVS		(1 << MINORBITS)
 
+/*
+ * Pool of fixed-size completion buffers, one per open file of a queue
+ * whose driver registered a buffer size.  A uring_cmd driver that returns
+ * data from task work can check a buffer out at issue time and return it
+ * from task work instead of allocating in the I/O path.
+ * bsg_register_queue() declares the buffer size the driver needs.  The
+ * pool is allocated at open() time, so no buffer is allocated in the I/O
+ * path, and is charged to the opener's memory cgroup; when the pool is
+ * exhausted the driver can reject the command with -EAGAIN instead of
+ * sleeping.  Keeping the pool per file makes its lifetime simple: io_uring
+ * holds a reference on the file for the lifetime of a request, and a
+ * driver returns its buffer before completing the command, so
+ * bsg_release() cannot free a pool that still has buffers checked out.
+ */
+struct bsg_completion_pool {
+	spinlock_t lock;		/* protects free_top and stack */
+	unsigned int depth;		/* number of buffers */
+	unsigned int free_top;		/* buffers currently available */
+	size_t buf_size;		/* size of each buffer */
+	u8 *bufs;			/* depth buffers of buf_size bytes */
+	u8 *checked_out;		/* per buffer: 1 while borrowed */
+	u32 stack[];			/* indices of the available buffers */
+};
+
 static DEFINE_IDA(bsg_minor_ida);
 static const struct class bsg_class;
 static int bsg_major;
@@ -73,15 +98,126 @@ static int bsg_sg_io(struct bsg_device *bd, bool open_for_write,
 	return ret;
 }
 
+static struct bsg_completion_pool *
+bsg_completion_pool_alloc(struct bsg_device *bd)
+{
+	struct bsg_completion_pool *pool;
+	unsigned int depth = BSG_DEFAULT_CMDS;
+	unsigned int i;
+
+	pool = kzalloc(struct_size(pool, stack, depth), GFP_KERNEL_ACCOUNT);
+	if (!pool)
+		return NULL;
+	pool->bufs = kcalloc(depth, bd->completion_buf_size,
+			     GFP_KERNEL_ACCOUNT);
+	pool->checked_out = kcalloc(depth, 1, GFP_KERNEL_ACCOUNT);
+	if (!pool->bufs || !pool->checked_out) {
+		kfree(pool->checked_out);
+		kfree(pool->bufs);
+		kfree(pool);
+		return NULL;
+	}
+	pool->depth = depth;
+	pool->free_top = depth;
+	pool->buf_size = bd->completion_buf_size;
+	spin_lock_init(&pool->lock);
+	for (i = 0; i < depth; i++)
+		pool->stack[i] = i;
+	return pool;
+}
+
+static void bsg_completion_pool_free(struct bsg_completion_pool *pool)
+{
+	kfree(pool->checked_out);
+	kfree(pool->bufs);
+	kfree(pool);
+}
+
+/**
+ * bsg_uring_cmd_get_completion_buf - check out a per-file completion buffer
+ * @ioucmd: the uring command being issued
+ * @len: required buffer size
+ *
+ * Check a buffer out of the pool of the opened bsg file.  The caller must
+ * return the buffer with bsg_uring_cmd_put_completion_buf() before it
+ * completes or fails the command, and must not reissue the command while
+ * the buffer is checked out.
+ *
+ * Return: the buffer, or NULL if the file has no pool, @len exceeds the
+ * registered buffer size, or the pool is exhausted.
+ */
+void *bsg_uring_cmd_get_completion_buf(struct io_uring_cmd *ioucmd, size_t len)
+{
+	struct bsg_completion_pool *pool = ioucmd->file->private_data;
+	unsigned int index;
+	void *buf;
+
+	if (!pool || WARN_ON_ONCE(len > pool->buf_size))
+		return NULL;
+
+	spin_lock(&pool->lock);
+	if (pool->free_top == 0) {
+		spin_unlock(&pool->lock);
+		return NULL;
+	}
+	index = pool->stack[--pool->free_top];
+	pool->checked_out[index] = 1;
+	buf = pool->bufs + (size_t)index * pool->buf_size;
+	spin_unlock(&pool->lock);
+	return buf;
+}
+EXPORT_SYMBOL_GPL(bsg_uring_cmd_get_completion_buf);
+
+/**
+ * bsg_uring_cmd_put_completion_buf - return a per-file completion buffer
+ * @ioucmd: the uring command being completed
+ * @buf: buffer previously returned by bsg_uring_cmd_get_completion_buf()
+ *
+ * Return the buffer to the pool of the opened bsg file.  Each buffer
+ * must be returned exactly once.  The pool warns if a buffer is returned
+ * while it is already free.
+ */
+void bsg_uring_cmd_put_completion_buf(struct io_uring_cmd *ioucmd, void *buf)
+{
+	struct bsg_completion_pool *pool = ioucmd->file->private_data;
+	unsigned int index;
+
+	if (WARN_ON_ONCE(!buf))
+		return;
+	index = ((u8 *)buf - pool->bufs) / pool->buf_size;
+	if (WARN_ON_ONCE(index >= pool->depth))
+		return;
+	spin_lock(&pool->lock);
+	if (WARN_ON_ONCE(!pool->checked_out[index])) {
+		spin_unlock(&pool->lock);
+		return;
+	}
+	pool->checked_out[index] = 0;
+	pool->stack[pool->free_top++] = index;
+	spin_unlock(&pool->lock);
+}
+EXPORT_SYMBOL_GPL(bsg_uring_cmd_put_completion_buf);
+
 static int bsg_open(struct inode *inode, struct file *file)
 {
-	if (!blk_get_queue(to_bsg_device(inode)->queue))
+	struct bsg_device *bd = to_bsg_device(inode);
+
+	if (!blk_get_queue(bd->queue))
 		return -ENXIO;
+	if (IS_ENABLED(CONFIG_IO_URING) && bd->completion_buf_size) {
+		file->private_data = bsg_completion_pool_alloc(bd);
+		if (!file->private_data) {
+			blk_put_queue(bd->queue);
+			return -ENOMEM;
+		}
+	}
 	return 0;
 }
 
 static int bsg_release(struct inode *inode, struct file *file)
 {
+	if (file->private_data)
+		bsg_completion_pool_free(file->private_data);
 	blk_put_queue(to_bsg_device(inode)->queue);
 	return 0;
 }
@@ -220,7 +356,7 @@ EXPORT_SYMBOL_GPL(bsg_unregister_queue);
 
 struct bsg_device *bsg_register_queue(struct request_queue *q,
 		struct device *parent, const char *name, bsg_sg_io_fn *sg_io_fn,
-		bsg_uring_cmd_fn *uring_cmd_fn)
+		bsg_uring_cmd_fn *uring_cmd_fn, size_t completion_buf_size)
 {
 	struct bsg_device *bd;
 	int ret;
@@ -231,6 +367,7 @@ struct bsg_device *bsg_register_queue(struct request_queue *q,
 	bd->max_queue = BSG_DEFAULT_CMDS;
 	bd->reserved_size = INT_MAX;
 	bd->queue = q;
+	bd->completion_buf_size = completion_buf_size;
 	bd->sg_io_fn = sg_io_fn;
 	bd->uring_cmd_fn = uring_cmd_fn;
 
diff --git a/drivers/scsi/scsi_bsg.c b/drivers/scsi/scsi_bsg.c
index 5ffbd1843861..1c9cf77d0d6a 100644
--- a/drivers/scsi/scsi_bsg.c
+++ b/drivers/scsi/scsi_bsg.c
@@ -300,5 +300,5 @@ struct bsg_device *scsi_bsg_register_queue(struct scsi_device *sdev)
 {
 	return bsg_register_queue(sdev->request_queue, &sdev->sdev_gendev,
 			dev_name(&sdev->sdev_gendev), scsi_bsg_sg_io_fn,
-			scsi_bsg_uring_cmd);
+			scsi_bsg_uring_cmd, SCSI_SENSE_BUFFERSIZE);
 }
diff --git a/include/linux/bsg.h b/include/linux/bsg.h
index 162730bfc2d8..3f33ead2c3ee 100644
--- a/include/linux/bsg.h
+++ b/include/linux/bsg.h
@@ -17,7 +17,11 @@ typedef int (bsg_uring_cmd_fn)(struct request_queue *q, struct io_uring_cmd *iou
 
 struct bsg_device *bsg_register_queue(struct request_queue *q,
 		struct device *parent, const char *name,
-		bsg_sg_io_fn *sg_io_fn, bsg_uring_cmd_fn *uring_cmd_fn);
+		bsg_sg_io_fn *sg_io_fn, bsg_uring_cmd_fn *uring_cmd_fn,
+		size_t completion_buf_size);
 void bsg_unregister_queue(struct bsg_device *bcd);
 
+void *bsg_uring_cmd_get_completion_buf(struct io_uring_cmd *ioucmd, size_t len);
+void bsg_uring_cmd_put_completion_buf(struct io_uring_cmd *ioucmd, void *buf);
+
 #endif /* _LINUX_BSG_H */
-- 
2.43.0


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

* [PATCH v4 2/2] scsi: bsg: release io_uring requests from end-I/O
  2026-11-18 15:37 [PATCH v4 0/2] scsi: bsg: release io_uring requests from end-I/O Yingjie Wang
  2026-11-18 15:37 ` [PATCH v4 1/2] block: bsg: add a per-file pool of uring_cmd completion buffers Yingjie Wang
@ 2026-11-18 15:37 ` Yingjie Wang
  1 sibling, 0 replies; 3+ messages in thread
From: Yingjie Wang @ 2026-11-18 15:37 UTC (permalink / raw)
  To: Martin K. Petersen
  Cc: James.Bottomley@HansenPartnership.com, FUJITA Tomonori,
	axboe@kernel.dk, linux-scsi@vger.kernel.org,
	linux-block@vger.kernel.org, bvanassche@acm.org,
	jthoughton@google.com, yangxiuwei@kylinos.cn,
	linux-kernel@vger.kernel.org

The BSG io_uring end-I/O callback leaves completed requests for task work
to release. The request keeps its tag until that work runs. If the
submitting task then faults on memory backed by the same queue while
its completed requests hold all the tags, it blocks waiting for a tag
and never runs the task work that would free one.

Snapshot the SCSI result and bounded sense data in end-I/O, queue task
work, and return RQ_END_IO_FREE for every completion. Task work then
unmaps the saved BIO, copies the saved sense data and posts the CQE,
without dereferencing the request or scsi_cmnd.

Store the sense snapshot in a buffer checked out from the per-file bsg
completion pool, and only for commands that pass a response buffer and
a nonzero response length, since only those can deliver sense bytes.
This adds no allocation to the I/O path and does not grow the request.

For a bounce-mapped READ, blk_rq_unmap_user() copies the zero-filled
bounce pages back to userspace even if no I/O was issued. Check out
the completion buffer before mapping user data so that pool exhaustion
leaves the caller's buffer untouched.

When the pool is exhausted, submission returns -EAGAIN without waiting
for a buffer. An initial nonblocking attempt may be retried through
io-wq; if the pool is still empty in the worker, the command completes
with -EAGAIN and userspace must resubmit it to retry.

This introduces a per-file limit of 64 commands that pass both a
response buffer and a nonzero response length and are in flight or
waiting for task work. SG_SET_COMMAND_Q does not resize the pool.

Task work returns the buffer after the sense copy and before
io_uring_cmd_done32(); io_uring keeps a reference on the file until the
request is freed, so the pool cannot be released while a buffer is
checked out. Clear the reported sense length on cancellation or a
failed sense copy.

Fixes: 7b6d3255e7f8 ("scsi: bsg: add io_uring passthrough handler")
Link: https://lore.kernel.org/r/20261022.scsi-copyback-review.31b8d6a70f4c@outlook.com
Cc: stable@vger.kernel.org
Signed-off-by: Yingjie Wang <x-professor@outlook.com>
---
 drivers/scsi/scsi_bsg.c | 116 +++++++++++++++++++++++++++++-----------
 1 file changed, 85 insertions(+), 31 deletions(-)

diff --git a/drivers/scsi/scsi_bsg.c b/drivers/scsi/scsi_bsg.c
index 1c9cf77d0d6a..8f9b2aaa3ccf 100644
--- a/drivers/scsi/scsi_bsg.c
+++ b/drivers/scsi/scsi_bsg.c
@@ -16,9 +16,13 @@
  */
 struct scsi_bsg_uring_cmd_pdu {
 	struct bio *bio;		/* mapped user buffer, unmap in task work */
-	struct request *req;		/* block request, freed in task work */
+	u8 *sense_buffer;		/* sense snapshot from the per-file pool */
 	u64 response_addr;		/* user space response buffer address */
-	u32 max_response_len;		/* user response buffer size */
+	/* max_response_len is replaced by res2 after the end-I/O snapshot. */
+	union {
+		u32 max_response_len;	/* maximum sense snapshot size */
+		u64 res2;		/* saved completion result */
+	};
 };
 static_assert(sizeof(struct scsi_bsg_uring_cmd_pdu) <= sizeof_field(struct io_uring_cmd, pdu));
 
@@ -28,17 +32,22 @@ static inline struct scsi_bsg_uring_cmd_pdu *scsi_bsg_uring_cmd_pdu(
 	return io_uring_cmd_to_pdu(ioucmd, struct scsi_bsg_uring_cmd_pdu);
 }
 
-/* Task work: build res2 (layout in uapi/linux/bsg.h) and copy sense to user. */
+static u64 scsi_bsg_res2_clear_sense_len(u64 res2)
+{
+	return bsg_scsi_res2_build(bsg_scsi_res2_device_status(res2),
+				   bsg_scsi_res2_driver_status(res2),
+				   bsg_scsi_res2_host_status(res2), 0,
+				   bsg_scsi_res2_resid_len(res2));
+}
+
+/* Task work: unmap the saved BIO and copy sense to user space. */
 static void scsi_bsg_uring_task_cb(struct io_tw_req tw_req, io_tw_token_t tw)
 {
 	struct io_uring_cmd *ioucmd = io_uring_cmd_from_tw(tw_req);
 	struct scsi_bsg_uring_cmd_pdu *pdu = scsi_bsg_uring_cmd_pdu(ioucmd);
-	struct request *rq = pdu->req;
-	struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(rq);
-	u64 res2;
+	u64 res2 = pdu->res2;
+	u8 sense_len = bsg_scsi_res2_sense_len(res2);
 	int ret = 0;
-	u8 driver_status = 0;
-	u8 sense_len_wr = 0;
 
 	if (pdu->bio)
 		ret = blk_rq_unmap_user(pdu->bio);
@@ -48,27 +57,22 @@ static void scsi_bsg_uring_task_cb(struct io_tw_req tw_req, io_tw_token_t tw)
 	 * has exited, this may run from a kworker or in a different task, so
 	 * don't copy the sense data to the submitter's address.
 	 */
-	if (unlikely(tw.cancel))
+	if (unlikely(tw.cancel)) {
 		ret = -ECANCELED;
-
-	if (scsi_status_is_check_condition(scmd->result)) {
-		driver_status = DRIVER_SENSE;
-		if (pdu->response_addr && !tw.cancel)
-			sense_len_wr = min_t(unsigned int, pdu->max_response_len,
-					     scmd->sense_len);
-	}
-
-	if (sense_len_wr) {
-		if (copy_to_user(uptr64(pdu->response_addr), scmd->sense_buffer,
-				 sense_len_wr))
+		sense_len = 0;
+	} else if (sense_len) {
+		if (copy_to_user(uptr64(pdu->response_addr), pdu->sense_buffer,
+				 sense_len)) {
 			ret = -EFAULT;
+			sense_len = 0;
+		}
 	}
 
-	res2 = bsg_scsi_res2_build(status_byte(scmd->result), driver_status,
-				  host_byte(scmd->result), sense_len_wr,
-				  scmd->resid_len);
+	if (!sense_len)
+		res2 = scsi_bsg_res2_clear_sense_len(res2);
 
-	blk_mq_free_request(rq);
+	if (pdu->sense_buffer)
+		bsg_uring_cmd_put_completion_buf(ioucmd, pdu->sense_buffer);
 	io_uring_cmd_done32(ioucmd, ret, res2,
 			    IO_URING_CMD_TASK_WORK_ISSUE_FLAGS);
 }
@@ -78,9 +82,26 @@ static enum rq_end_io_ret scsi_bsg_uring_cmd_done(struct request *req,
 						  const struct io_comp_batch *iocb)
 {
 	struct io_uring_cmd *ioucmd = req->end_io_data;
+	struct scsi_bsg_uring_cmd_pdu *pdu = scsi_bsg_uring_cmd_pdu(ioucmd);
+	struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(req);
+	u8 driver_status = 0;
+	u8 sense_len = 0;
 
+	if (scsi_status_is_check_condition(scmd->result)) {
+		driver_status = DRIVER_SENSE;
+		if (pdu->response_addr) {
+			sense_len = min_t(unsigned int, pdu->max_response_len,
+					  scmd->sense_len);
+			if (sense_len)
+				memcpy(pdu->sense_buffer, scmd->sense_buffer,
+				       sense_len);
+		}
+	}
+	pdu->res2 = bsg_scsi_res2_build(status_byte(scmd->result),
+					driver_status, host_byte(scmd->result),
+					sense_len, scmd->resid_len);
 	io_uring_cmd_do_in_task_lazy(ioucmd, scsi_bsg_uring_task_cb);
-	return RQ_END_IO_NONE;
+	return RQ_END_IO_FREE;
 }
 
 static int scsi_bsg_map_user_buffer(struct request *req,
@@ -123,8 +144,15 @@ static int scsi_bsg_uring_cmd(struct request_queue *q, struct io_uring_cmd *iouc
 	u32 dout_xfer_len = READ_ONCE(cmd->dout_xfer_len);
 	u64 din_xferp = READ_ONCE(cmd->din_xferp);
 	u32 din_xfer_len = READ_ONCE(cmd->din_xfer_len);
+	u64 response_addr = READ_ONCE(cmd->response);
+	u32 max_response_len = min_t(u32, READ_ONCE(cmd->max_response_len),
+				     SCSI_SENSE_BUFFERSIZE);
+	u32 timeout_ms = READ_ONCE(cmd->timeout_ms);
 	int ret;
 
+	if (ioucmd->flags & IORING_URING_CMD_MULTISHOT)
+		return -EOPNOTSUPP;
+
 	if (cmd->protocol != BSG_PROTOCOL_SCSI ||
 	    cmd->subprotocol != BSG_SUB_PROTOCOL_SCSI_CMD)
 		return -EINVAL;
@@ -168,8 +196,32 @@ static int scsi_bsg_uring_cmd(struct request_queue *q, struct io_uring_cmd *iouc
 		goto out_free_req;
 	}
 
-	pdu->response_addr = cmd->response;
-	pdu->max_response_len = cmd->max_response_len;
+	pdu->response_addr = response_addr;
+	pdu->max_response_len = max_response_len;
+
+	/*
+	 * Sense bytes can only be delivered when the caller passed a
+	 * response buffer and a nonzero response length; a command without
+	 * both never snapshots sense, so it needs no pool buffer and cannot
+	 * be rejected for pool exhaustion.
+	 *
+	 * Take the buffer before mapping the user data buffer.
+	 * blk_rq_unmap_user() is not pure cleanup: for a bounce-mapped READ
+	 * it copies the zero-filled bounce pages back into the caller's
+	 * buffer, so a command rejected for pool exhaustion after mapping
+	 * would zero the caller's data before returning -EAGAIN.
+	 */
+	if (response_addr && max_response_len) {
+		pdu->sense_buffer =
+			bsg_uring_cmd_get_completion_buf(ioucmd,
+							 SCSI_SENSE_BUFFERSIZE);
+		if (!pdu->sense_buffer) {
+			ret = -EAGAIN;
+			goto out_free_req;
+		}
+	} else {
+		pdu->sense_buffer = NULL;
+	}
 
 	if (dout_xfer_len || din_xfer_len) {
 		bool is_write = dout_xfer_len > 0;
@@ -180,22 +232,24 @@ static int scsi_bsg_uring_cmd(struct request_queue *q, struct io_uring_cmd *iouc
 					       gfp_mask, is_write, buf_addr,
 					       buf_len);
 		if (ret)
-			goto out_free_req;
+			goto out_put_buf;
 		pdu->bio = req->bio;
 	} else {
 		pdu->bio = NULL;
 	}
 
-	req->timeout = cmd->timeout_ms ?
-		msecs_to_jiffies(cmd->timeout_ms) : BLK_DEFAULT_SG_TIMEOUT;
+	req->timeout = timeout_ms ? msecs_to_jiffies(timeout_ms) :
+				   BLK_DEFAULT_SG_TIMEOUT;
 
 	req->end_io = scsi_bsg_uring_cmd_done;
 	req->end_io_data = ioucmd;
-	pdu->req = req;
 
 	blk_execute_rq_nowait(req, false);
 	return -EIOCBQUEUED;
 
+out_put_buf:
+	if (pdu->sense_buffer)
+		bsg_uring_cmd_put_completion_buf(ioucmd, pdu->sense_buffer);
 out_free_req:
 	blk_mq_free_request(req);
 	return ret;
-- 
2.43.0


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

end of thread, other threads:[~2026-11-18 15:37 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-18 15:37 [PATCH v4 0/2] scsi: bsg: release io_uring requests from end-I/O Yingjie Wang
2026-11-18 15:37 ` [PATCH v4 1/2] block: bsg: add a per-file pool of uring_cmd completion buffers Yingjie Wang
2026-11-18 15:37 ` [PATCH v4 2/2] scsi: bsg: release io_uring requests from end-I/O Yingjie Wang

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