[PATCH v5 2/2] scsi: bsg: Release io_uring requests from end-I/O

From: Yingjie Wang

Date: Sat Nov 21 2026 - 03:31:57 EST


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@xxxxxxxxxx>
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Yingjie Wang <x-professor@xxxxxxxxxxx>
---
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