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

From: Yingjie Wang

Date: Sat Nov 21 2026 - 03:32:00 EST


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@xxxxxxxxxxx

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@xxxxxxxxxxx
Yang's pool patch:
https://lore.kernel.org/r/20261119031751.3471968-2-yangxiuwei@xxxxxxxxxx
Yang's follow-up:
https://lore.kernel.org/r/20261120021604.2648220-1-yangxiuwei@xxxxxxxxxx

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