Linux SCSI subsystem development
 help / color / mirror / Atom feed
* [PATCH v5 0/2] scsi: bsg: Release io_uring requests from end-I/O
@ 2026-11-21  8:31 Yingjie Wang
  2026-11-21  8:31 ` [PATCH v5 1/2] block: bsg: add a per-device pool of uring_cmd completion buffers Yingjie Wang
  2026-11-21  8:31 ` [PATCH v5 2/2] scsi: bsg: Release io_uring requests from end-I/O Yingjie Wang
  0 siblings, 2 replies; 4+ messages in thread
From: Yingjie Wang @ 2026-11-21  8:31 UTC (permalink / raw)
  To: Martin K. Petersen, Jens Axboe
  Cc: James E.J. Bottomley, linux-scsi@vger.kernel.org, Bart Van Assche,
	Yang Xiuwei, James Houghton, linux-kernel@vger.kernel.org,
	FUJITA Tomonori, FUJITA Tomonori, linux-block@vger.kernel.org

The BSG io_uring path retains completed requests until task work copies
sense data and unmaps the data buffer. With DEFER_TASKRUN, the submitter
may fault on memory backed by the same queue before running that work.
If completed requests hold all the tags, neither the fault's I/O nor
the task work can progress.

These two patches save completion state in end-I/O and release the
request there. Task work uses the saved state, unmaps the independent
BIO chain and completes the command. They apply on top of the four
synchronous request-lifetime patches posted as v5:

https://lore.kernel.org/r/scsi-sync-v5.1795249365940711260.0.x-professor@outlook.com

Patch 1 provides a BSG-owned pool of 64 sense buffers per device,
allocated when its queue is registered and freed with the BSG device.
The bitmap and 96-byte buffers use one allocation. Opening the device
allocates no pool; transport BSG and CONFIG_IO_URING=n allocate none.
Patch 2 takes a slot before mapping user data and returns it in end-I/O
when there is no sense data to deliver, or from task work otherwise.

When the pool is empty, the command fails with -EAGAIN instead of waiting.
io_uring may retry a nonblocking attempt in io-wq. An io-wq attempt
that finds the pool empty completes with -EAGAIN, requiring userspace
to resubmit. Slots are shared across openers: one opener can retain
all 64 with unreaped CHECK CONDITION completions, causing other openers
to receive -EAGAIN. BSG nodes are normally accessible only to privileged
users, and the series accepts this shared limit to avoid allocating a
pool on every open. SG_SET_COMMAND_Q does not resize the pool.

Changes in v5, following Yang Xiuwei's review and example patches:
- Replace the per-file pool with a per-device pool and a bitmap.
- Return buffers with no sense data in end-I/O; use irqsave locking.
- Embed the bitmap and buffers in one allocation, remove the redundant
  buffer-size field, and trim comments.
- Return quietly on invalid get/put arguments while retaining range
  checks, double-put and outstanding-buffer diagnostics.
- Credit Yang's pool implementation with Co-developed-by and retain his
  Signed-off-by from the posted patch.
- Drop the unrelated multishot rejection and timeout snapshot.
- Rebase onto synchronous v5.

Tested in QEMU on 9f9e0b1e5399 plus synchronous v5, with KASAN and
lockdep. The 960 regression checks and release-order tracing passed:
completed io_uring requests no longer remained allocated before deferred
task work ran. Cancellation, linked commands, 256-command pool exhaustion,
64-slot recovery and concurrent use through two opens passed. A rejected
bounced READ through a second opener left its user buffer unchanged.
128 successful commands returned all slots before running task work.
No KASAN or lockdep reports were observed. These tests check ordering
and behavior; they do not reproduce a same-device page-fault deadlock.

Dropping the multishot rejection restores the base kernel's flag
handling; the tested multishot-flagged commands completed once on both
kernels with normal, DEFER_TASKRUN and IOSQE_ASYNC submissions.

Each patch passed affected-object W=1 builds. Final configuration checks
also covered i386, CONFIG_IO_URING=n and PREEMPT_RT with SCSI=m. Kernel
selftests were not run.

Successful INQUIRY batches were measured on non-debug kernels, with
three interleaved boots per variant and five rounds per boot. Both v4
and v5 used the same synchronous-v5 base. The table shows batch time
divided by command count, in microseconds, with a 96-byte response
buffer supplied. Retry time is included; base has no io_uring fix.
These timings were measured before the multishot rejection and timeout
snapshot were dropped, and were not rerun; pool allocation, locking and
early return are unchanged.

  mode    depth   base     v4     v5
  normal     16  1.587  1.593  1.646
  normal     64  1.158  1.165  1.195
  normal     96  1.066  1.231  1.165
  defer      16  1.582  1.589  1.643
  defer      64  1.155  1.166  1.198
  defer      96  1.065  1.228  1.161

Compared with v4, v5 increased time per command by 2.6-3.4% at depths
16 and 64, mainly due to irqsave locking in the control measurements.
At depth 96, early buffer return reduced -EAGAIN completions from 32 per
batch to about 9 and reduced time per command by 5.4-5.5%. These QEMU
measurements do not establish performance on physical SCSI hardware.

Both patches retain Cc: stable. Patch 1 is a prerequisite for patch 2,
which fixes 7b6d3255e7f8 ("scsi: bsg: add io_uring passthrough handler").
A backport needs both patches, in order, on top of synchronous patch 4/4
("scsi: bsg: Release synchronous request before user copyback"), which
changes the same file. Both pool patches exceed the stable rules'
100-line limit, including context; if that rules them out, I am fine
with dropping the tags.

v4: https://lore.kernel.org/r/cover.1795016239.git.x-professor@outlook.com
Yang's pool patch:
https://lore.kernel.org/r/20261119031751.3471968-2-yangxiuwei@kylinos.cn
Yang's follow-up:
https://lore.kernel.org/r/20261120021604.2648220-1-yangxiuwei@kylinos.cn

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

 block/bsg.c             | 109 ++++++++++++++++++++++++++++++++++++++++
 drivers/scsi/scsi_bsg.c | 109 +++++++++++++++++++++++++++++-----------
 include/linux/bsg.h     |   3 ++
 3 files changed, 192 insertions(+), 29 deletions(-)


base-commit: 9f9e0b1e5399045982788a954d3461de894e40c7
prerequisite-patch-id: ef4b390580d84500ee98fcddff0a34e5cd95e40c
prerequisite-patch-id: 1b98fea4540409102179bfccd2905a686c7f1b5a
prerequisite-patch-id: ce66c68fb99ef813e5506ba7f3de512c1fd0c832
prerequisite-patch-id: 73942e82ea57898fb0af8ee1db909b0dfa8b2b77
-- 
2.43.0


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

* [PATCH v5 1/2] block: bsg: add a per-device pool of uring_cmd completion buffers
  2026-11-21  8:31 [PATCH v5 0/2] scsi: bsg: Release io_uring requests from end-I/O Yingjie Wang
@ 2026-11-21  8:31 ` Yingjie Wang
  2026-11-21  8:31 ` [PATCH v5 2/2] scsi: bsg: Release io_uring requests from end-I/O Yingjie Wang
  1 sibling, 0 replies; 4+ messages in thread
From: Yingjie Wang @ 2026-11-21  8:31 UTC (permalink / raw)
  To: Martin K. Petersen, Jens Axboe
  Cc: James E.J. Bottomley, linux-scsi@vger.kernel.org, Bart Van Assche,
	Yang Xiuwei, James Houghton, linux-kernel@vger.kernel.org,
	FUJITA Tomonori, FUJITA Tomonori, linux-block@vger.kernel.org

A bsg uring_cmd driver needs storage for completion data which outlives
its block request when the request is freed in end-I/O. scsi_bsg needs
to preserve sense data until task work can copy it to userspace.

Allocate a pool when a bsg queue with a uring_cmd handler is registered,
and free it when the bsg device is released. Each pool has BSG_DEFAULT_CMDS
slots of SCSI_SENSE_BUFFERSIZE bytes, for the only in-tree uring_cmd
consumer, scsi_bsg. Transport bsg devices and kernels without io_uring
allocate no pool. A bitmap and the buffers share one allocation.

The get helper returns NULL when the pool is empty so the driver can
return -EAGAIN without allocating or waiting for a buffer in the I/O
path. Slots are shared by all openers of a device. One opener can retain
all 64 slots, for example with unreaped CHECK CONDITION completions on
a DEFER_TASKRUN ring, causing other openers to receive -EAGAIN too.
bsg nodes are normally restricted to privileged users, and sharing avoids
allocating a pool on every open. SG_SET_COMMAND_Q does not resize it.

A checked-out buffer must be returned before completing or failing its
command. io_uring retains the file reference through command completion,
and the open cdev keeps the bsg device alive, so the pool cannot be freed
while a buffer is checked out. Protect the bitmap with irqsave locking
because buffers can be returned from end-I/O as well as process context.

The next patch makes scsi_bsg use the pool to release completed requests
before task work. No caller checks out buffers in this patch alone.

Cc: stable@vger.kernel.org
Co-developed-by: Yang Xiuwei <yangxiuwei@kylinos.cn>
Signed-off-by: Yang Xiuwei <yangxiuwei@kylinos.cn>
Signed-off-by: Yingjie Wang <x-professor@outlook.com>
---
Changes in v5:
- Replace the per-file pool with Yang's per-device pool: inline bitmap,
  one allocation, irqsave locking. Return quietly on invalid get/put
  arguments and trim comments. The subject changed accordingly.
- Add Co-developed-by and retain Yang's Signed-off-by from his posted patch.

This patch is a prerequisite for the following fix; it has no Fixes tag
of its own.

 block/bsg.c         | 109 ++++++++++++++++++++++++++++++++++++++++++++
 include/linux/bsg.h |   3 ++
 2 files changed, 112 insertions(+)

diff --git a/block/bsg.c b/block/bsg.c
index b296fa373292..73ec02ee8c23 100644
--- a/block/bsg.c
+++ b/block/bsg.c
@@ -12,10 +12,12 @@
 #include <linux/idr.h>
 #include <linux/bsg.h>
 #include <linux/slab.h>
+#include <linux/bitmap.h>
 #include <linux/io_uring/cmd.h>
 
 #include <scsi/scsi.h>
 #include <scsi/scsi_ioctl.h>
+#include <scsi/scsi_device.h>
 #include <scsi/sg.h>
 
 #define BSG_DESCRIPTION	"Block layer SCSI generic (bsg) driver"
@@ -30,6 +32,7 @@ struct bsg_device {
 	unsigned int reserved_size;
 	bsg_sg_io_fn *sg_io_fn;
 	bsg_uring_cmd_fn *uring_cmd_fn;
+	struct bsg_completion_pool *completion_pool;
 };
 
 static inline struct bsg_device *to_bsg_device(struct inode *inode)
@@ -40,6 +43,101 @@ static inline struct bsg_device *to_bsg_device(struct inode *inode)
 #define BSG_DEFAULT_CMDS	64
 #define BSG_MAX_DEVS		(1 << MINORBITS)
 
+/*
+ * Per-device completion buffers for uring_cmd. These can retain completion
+ * data after the request is freed. scsi_bsg is the only user, so each
+ * buffer holds SCSI_SENSE_BUFFERSIZE bytes.
+ */
+struct bsg_completion_pool {
+	spinlock_t lock;		/* protects checked_out, including in end-I/O */
+	DECLARE_BITMAP(checked_out, BSG_DEFAULT_CMDS);
+	u8 bufs[];
+};
+
+static struct bsg_completion_pool *bsg_completion_pool_alloc(void)
+{
+	struct bsg_completion_pool *pool;
+
+	pool = kzalloc(struct_size(pool, bufs,
+				   BSG_DEFAULT_CMDS * SCSI_SENSE_BUFFERSIZE),
+		       GFP_KERNEL);
+	if (!pool)
+		return NULL;
+	spin_lock_init(&pool->lock);
+	return pool;
+}
+
+static void bsg_completion_pool_free(struct bsg_completion_pool *pool)
+{
+	if (!pool)
+		return;
+	WARN_ON_ONCE(!bitmap_empty(pool->checked_out, BSG_DEFAULT_CMDS));
+	kfree(pool);
+}
+
+/**
+ * bsg_uring_cmd_get_completion_buf - check out a per-device completion buffer
+ * @ioucmd: the uring command being issued
+ * @len: required buffer size
+ *
+ * The caller must return the buffer before completing or failing the command.
+ *
+ * Return: the buffer, or NULL if there is no pool, @len exceeds the buffer
+ * size, or the pool is exhausted.
+ */
+void *bsg_uring_cmd_get_completion_buf(struct io_uring_cmd *ioucmd, size_t len)
+{
+	struct bsg_device *bd = to_bsg_device(file_inode(ioucmd->file));
+	struct bsg_completion_pool *pool = bd->completion_pool;
+	unsigned long flags;
+	unsigned int index;
+	void *buf = NULL;
+
+	if (!pool || len > SCSI_SENSE_BUFFERSIZE)
+		return NULL;
+
+	spin_lock_irqsave(&pool->lock, flags);
+	index = find_first_zero_bit(pool->checked_out, BSG_DEFAULT_CMDS);
+	if (index < BSG_DEFAULT_CMDS) {
+		__set_bit(index, pool->checked_out);
+		buf = pool->bufs + index * SCSI_SENSE_BUFFERSIZE;
+	}
+	spin_unlock_irqrestore(&pool->lock, flags);
+	return buf;
+}
+EXPORT_SYMBOL_GPL(bsg_uring_cmd_get_completion_buf);
+
+/**
+ * bsg_uring_cmd_put_completion_buf - return a per-device completion buffer
+ * @ioucmd: the uring command that checked out the buffer
+ * @buf: buffer previously returned by bsg_uring_cmd_get_completion_buf()
+ *
+ * Return each buffer exactly once, from submission error handling,
+ * end-I/O or task work. A second return while the buffer is free
+ * triggers a warning.
+ */
+void bsg_uring_cmd_put_completion_buf(struct io_uring_cmd *ioucmd, void *buf)
+{
+	struct bsg_device *bd = to_bsg_device(file_inode(ioucmd->file));
+	struct bsg_completion_pool *pool = bd->completion_pool;
+	unsigned long flags;
+	unsigned long offset;
+	unsigned int index;
+
+	if (!pool || !buf)
+		return;
+	offset = (unsigned long)buf - (unsigned long)pool->bufs;
+	if (offset >= BSG_DEFAULT_CMDS * SCSI_SENSE_BUFFERSIZE ||
+	    offset % SCSI_SENSE_BUFFERSIZE)
+		return;
+	index = offset / SCSI_SENSE_BUFFERSIZE;
+
+	spin_lock_irqsave(&pool->lock, flags);
+	WARN_ON_ONCE(!__test_and_clear_bit(index, pool->checked_out));
+	spin_unlock_irqrestore(&pool->lock, flags);
+}
+EXPORT_SYMBOL_GPL(bsg_uring_cmd_put_completion_buf);
+
 static DEFINE_IDA(bsg_minor_ida);
 static const struct class bsg_class;
 static int bsg_major;
@@ -203,6 +301,7 @@ static void bsg_device_release(struct device *dev)
 {
 	struct bsg_device *bd = container_of(dev, struct bsg_device, device);
 
+	bsg_completion_pool_free(bd->completion_pool);
 	ida_free(&bsg_minor_ida, MINOR(bd->device.devt));
 	kfree(bd);
 }
@@ -234,10 +333,20 @@ struct bsg_device *bsg_register_queue(struct request_queue *q,
 	bd->sg_io_fn = sg_io_fn;
 	bd->uring_cmd_fn = uring_cmd_fn;
 
+	/* Transport bsg devices have no uring_cmd handler and need no pool. */
+	if (IS_ENABLED(CONFIG_IO_URING) && uring_cmd_fn) {
+		bd->completion_pool = bsg_completion_pool_alloc();
+		if (!bd->completion_pool) {
+			kfree(bd);
+			return ERR_PTR(-ENOMEM);
+		}
+	}
+
 	ret = ida_alloc_max(&bsg_minor_ida, BSG_MAX_DEVS - 1, GFP_KERNEL);
 	if (ret < 0) {
 		if (ret == -ENOSPC)
 			dev_err(parent, "bsg: too many bsg devices\n");
+		bsg_completion_pool_free(bd->completion_pool);
 		kfree(bd);
 		return ERR_PTR(ret);
 	}
diff --git a/include/linux/bsg.h b/include/linux/bsg.h
index 162730bfc2d8..f5560be80c20 100644
--- a/include/linux/bsg.h
+++ b/include/linux/bsg.h
@@ -20,4 +20,7 @@ struct bsg_device *bsg_register_queue(struct request_queue *q,
 		bsg_sg_io_fn *sg_io_fn, bsg_uring_cmd_fn *uring_cmd_fn);
 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] 4+ messages in thread

* [PATCH v5 2/2] scsi: bsg: Release io_uring requests from end-I/O
  2026-11-21  8:31 [PATCH v5 0/2] scsi: bsg: Release io_uring requests from end-I/O Yingjie Wang
  2026-11-21  8:31 ` [PATCH v5 1/2] block: bsg: add a per-device pool of uring_cmd completion buffers Yingjie Wang
@ 2026-11-21  8:31 ` Yingjie Wang
  2026-11-21  8:41   ` sashiko-bot
  1 sibling, 1 reply; 4+ messages in thread
From: Yingjie Wang @ 2026-11-21  8:31 UTC (permalink / raw)
  To: Martin K. Petersen, Jens Axboe
  Cc: James E.J. Bottomley, linux-scsi@vger.kernel.org, Bart Van Assche,
	Yang Xiuwei, James Houghton, linux-kernel@vger.kernel.org,
	FUJITA Tomonori, FUJITA Tomonori, linux-block@vger.kernel.org

The bsg io_uring path keeps its completed request until task work copies
sense data and unmaps the data buffer. A DEFER_TASKRUN ring can leave
that work pending while its task faults on memory backed by the same
queue. If completed requests hold all available tags, the fault's I/O
cannot proceed and the task cannot run the work which releases them.

Save the result and bounded sense data in end-I/O, return RQ_END_IO_FREE,
and let task work use the saved state. The saved BIO chain holds its own
references, which blk_rq_unmap_user() releases. Canceled task work still
unmaps it, suppresses sense copyback, and completes with -ECANCELED.

Use the bsg completion pool for sense snapshots. Only commands supplying
a response buffer with a nonzero length take a slot. Take it before
mapping data, because unmapping a bounced READ copies data back even
when the command was never submitted. If no slot is available, return
-EAGAIN; io_uring may retry a nonblocking attempt in io-wq. An io-wq
attempt that finds the pool empty completes with -EAGAIN, requiring
userspace to resubmit.

Return the slot in end-I/O when no sense data needs copying, allowing
successful commands to release it without waiting for task work. Task
work returns any remaining slot before posting the CQE. Clear the saved
sense length on cancellation or copy failure so the CQE does not report
sense bytes that were not delivered.

Fixes: 7b6d3255e7f8 ("scsi: bsg: add io_uring passthrough handler")
Suggested-by: Yang Xiuwei <yangxiuwei@kylinos.cn>
Cc: stable@vger.kernel.org
Signed-off-by: Yingjie Wang <x-professor@outlook.com>
---
Changes in v5:
- Use the shared per-device pool from patch 1.
- Return the sense buffer in end-I/O when no sense data needs copying (Yang).
- Drop the unrelated multishot rejection and timeout snapshot.
- Rebase on synchronous v5.

 drivers/scsi/scsi_bsg.c | 109 +++++++++++++++++++++++++++++-----------
 1 file changed, 80 insertions(+), 29 deletions(-)

diff --git a/drivers/scsi/scsi_bsg.c b/drivers/scsi/scsi_bsg.c
index c17cad798326..d1ad956df9d6 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-device 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,34 @@ 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->sense_buffer) {
+			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);
+
+	/* Return the buffer now if there is no sense data to copy. */
+	if (!sense_len && pdu->sense_buffer) {
+		bsg_uring_cmd_put_completion_buf(ioucmd, pdu->sense_buffer);
+		pdu->sense_buffer = NULL;
+	}
 
 	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,6 +152,9 @@ 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);
 	int ret;
 
 	if (cmd->protocol != BSG_PROTOCOL_SCSI ||
@@ -168,8 +200,25 @@ 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;
+
+	/*
+	 * Take a sense buffer before mapping data: unmapping a bounced READ
+	 * copies back even if no I/O ran, so rejecting it afterwards would
+	 * overwrite the caller's data. No response buffer means no sense copy.
+	 */
+	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,7 +229,7 @@ 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;
@@ -191,11 +240,13 @@ static int scsi_bsg_uring_cmd(struct request_queue *q, struct io_uring_cmd *iouc
 
 	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] 4+ messages in thread

* Re: [PATCH v5 2/2] scsi: bsg: Release io_uring requests from end-I/O
  2026-11-21  8:31 ` [PATCH v5 2/2] scsi: bsg: Release io_uring requests from end-I/O Yingjie Wang
@ 2026-11-21  8:41   ` sashiko-bot
  0 siblings, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-11-21  8:41 UTC (permalink / raw)
  To: Yingjie Wang; +Cc: linux-scsi@vger.kernel.org

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] scsi: bsg: per-device spinlock contention on sense buffer
  pool per I/O
- [Medium] scsi: bsg: in-flight uring_cmd depth clamped to 64 with late
  pool check
--

commit 5e758c01d49b423d6603c6f22a4f49d0ad99b0ff
Author: Yingjie Wang <x-professor@outlook.com>

scsi: bsg: Release io_uring requests from end-I/O

This commit snapshots SCSI completion status and sense data in the request
end-I/O callback using the BSG completion buffer pool so the block request
can be freed before io_uring task work runs.

> diff --git a/drivers/scsi/scsi_bsg.c b/drivers/scsi/scsi_bsg.c
> index c17cad7983260..d1ad956df9d6f 100644
> --- a/drivers/scsi/scsi_bsg.c
> +++ b/drivers/scsi/scsi_bsg.c

[ ... ]

> @@ -78,9 +82,34 @@ 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->sense_buffer) {
> +			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);
> +
> +	/* Return the buffer now if there is no sense data to copy. */
> +	if (!sense_len && pdu->sense_buffer) {
> +		bsg_uring_cmd_put_completion_buf(ioucmd, pdu->sense_buffer);
> +		pdu->sense_buffer = NULL;
> +	}

[Severity: Medium]
Does checking out and returning a sense buffer on every command serialize
submissions and completions on the per-device pool->lock?

Because scsi_bsg_uring_cmd() calls bsg_uring_cmd_get_completion_buf() up
front whenever the caller provides a response buffer, every I/O acquires
spin_lock_irqsave(&pool->lock, flags) and modifies pool->checked_out twice
(once on submission and once here or in task work), even when the command
succeeds with SAM_STAT_GOOD and produces no sense data:

block/bsg.c:bsg_uring_cmd_get_completion_buf() {
    ...
	spin_lock_irqsave(&pool->lock, flags);
	index = find_first_zero_bit(pool->checked_out, BSG_DEFAULT_CMDS);
	if (index < BSG_DEFAULT_CMDS) {
		__set_bit(index, pool->checked_out);
		buf = pool->bufs + index * SCSI_SENSE_BUFFERSIZE;
	}
	spin_unlock_irqrestore(&pool->lock, flags);
    ...
}

Allocating the completion buffer in scsi_bsg_uring_cmd_done() only on
SAM_STAT_CHECK_CONDITION would keep the normal completion path lockless.

[ ... ]

> @@ -168,8 +200,25 @@ 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;
> +
> +	/*
> +	 * Take a sense buffer before mapping data: unmapping a bounced READ
> +	 * copies back even if no I/O ran, so rejecting it afterwards would
> +	 * overwrite the caller's data. No response buffer means no sense copy.
> +	 */
> +	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;
> +	}

[Severity: Medium]
Does reserving a slot from bd->completion_pool at submission time clamp
concurrent in-flight commands with a sense buffer to BSG_DEFAULT_CMDS (64)
per device, regardless of the device's blk-mq queue depth?

In addition, bsg_uring_cmd_get_completion_buf() is called after
scsi_alloc_request() and copy_from_user().  When all 64 slots are in use,
inline submission with IO_URING_F_NONBLOCK allocates and frees a blk-mq
request, returns -EAGAIN, and causes io_uring to punt the SQE to an io-wq
worker, which allocates and frees a request a second time before failing
again with -EAGAIN.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/scsi-pool-v5.1795249365940711260.0.x-professor@outlook.com?part=2

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

end of thread, other threads:[~2026-11-21  8:41 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-21  8:31 [PATCH v5 0/2] scsi: bsg: Release io_uring requests from end-I/O Yingjie Wang
2026-11-21  8:31 ` [PATCH v5 1/2] block: bsg: add a per-device pool of uring_cmd completion buffers Yingjie Wang
2026-11-21  8:31 ` [PATCH v5 2/2] scsi: bsg: Release io_uring requests from end-I/O Yingjie Wang
2026-11-21  8:41   ` sashiko-bot

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