* [PATCH v4 0/4] scsi: release completed passthrough requests before user copyback
@ 2026-11-18 8:33 Yingjie Wang
2026-11-18 8:33 ` [PATCH v4 1/4] scsi: core: release SG_IO request " Yingjie Wang
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: Yingjie Wang @ 2026-11-18 8:33 UTC (permalink / raw)
To: Martin K. Petersen, James E.J. Bottomley
Cc: linux-scsi, Bart Van Assche, Yang Xiuwei, James Houghton, linux-kernel
Four synchronous SCSI passthrough paths copy data to userspace, or unmap
a user buffer that needs copyback, while they still hold the completed
request. If that user access faults and the fault needs I/O from the
same queue, the task can wait for a tag held by the request it would
only release after the fault returns.
Each patch saves what the copyback still needs (the result, bounded
sense data and, where a user buffer is mapped, the BIO chain saved at
mapping time), releases the request, and then performs the user
copyback:
1/4 SG_IO on a SCSI block device (scsi_ioctl.c)
2/4 the legacy SCSI_IOCTL_SEND_COMMAND ioctl (scsi_ioctl.c)
3/4 CDROMREADAUDIO through sr_read_cdda_bpc() (sr.c)
4/4 the synchronous BSG ioctl (scsi_bsg.c)
Changes since RFC v3:
- Dropped the RFC tag and the io_uring half. RFC v3 5/6 and 6/6
changed the asynchronous BSG path; after review they are being
reworked to use a completion-buffer pool owned by bsg and will be
posted as a separate series.
- 1/4 to 4/4 are RFC v3 1/6 to 4/6, unchanged apart from rebasing onto
9f9e0b1e5399 from 7.4/scsi-fixes, which applied without edits.
Testing on this tip and, for comparison, on the unpatched base, both
with KASAN and lockdep, in QEMU with a virtio-scsi disk and CD-ROM:
- 20 iterations of the existing regression pass 960 assertions on both
kernels with no kernel warnings: 220 for the synchronous paths
(SG_IO, SCSI_IOCTL_SEND_COMMAND, BSG and CDDA) and 740 for the
io_uring path, which this series does not change.
- Tracing the 80 SG_IO, BSG and CDDA commands that map a user buffer
shows the request freed before blk_rq_unmap_user() on this tip, and
after it on the base kernel, in every case. The legacy ioctl has no
user mapping to trace, so it is covered by the functional assertions
only. These tests check the release order, not the hang itself.
- The CDDA cases exercise the virtual CD-ROM's expected error path, not
audio extraction from real media.
- Each commit builds with W=1 on x86-64; the tip also builds on i386,
which was not booted.
All four patches carry Fixes: d285203cf647 ("scsi: add support for a
blk-mq based I/O path.") and Cc: stable.
v1: https://lore.kernel.org/r/cover.1792237341.git.x-professor@outlook.com
v2: https://lore.kernel.org/r/cover.1793003363.git.x-professor@outlook.com
RFC v3: https://lore.kernel.org/r/cover.1794045010.git.x-professor@outlook.com
Yingjie Wang (4):
scsi: core: release SG_IO request before user copyback
scsi: core: release legacy ioctl request before user copyback
scsi: sr: release CDDA request before user copyback
scsi: bsg: release synchronous request before user copyback
drivers/scsi/scsi_bsg.c | 22 ++++++++++++++--------
drivers/scsi/scsi_ioctl.c | 34 ++++++++++++++++++++++++----------
drivers/scsi/sr.c | 3 +++
3 files changed, 41 insertions(+), 18 deletions(-)
base-commit: 9f9e0b1e5399045982788a954d3461de894e40c7
--
2.43.0
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v4 1/4] scsi: core: release SG_IO request before user copyback
2026-11-18 8:33 [PATCH v4 0/4] scsi: release completed passthrough requests before user copyback Yingjie Wang
@ 2026-11-18 8:33 ` Yingjie Wang
2026-11-18 8:33 ` [PATCH v4 2/4] scsi: core: release legacy ioctl " Yingjie Wang
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Yingjie Wang @ 2026-11-18 8:33 UTC (permalink / raw)
To: Martin K. Petersen, James E.J. Bottomley
Cc: linux-scsi, Bart Van Assche, Yang Xiuwei, James Houghton, linux-kernel
From: Yingjie Wang <x-professor@outlook.com>
The block-device SG_IO path retains its completed request while copying
sense data and unmapping the user data buffer. Either operation can fault.
If the fault needs I/O from the same queue and completed requests retain
the available tags, the nested I/O cannot make progress.
Fill the kernel-resident output header and save the bounded sense data
before releasing the request. Then perform the user copies and unmap the
original BIO chain, preserving the existing sense-copy error precedence.
The saved mapping owns its BIO references independently of the request.
The fixed request pool was introduced by scsi-mq; the later move of this
ioctl implementation into drivers/scsi did not introduce the dependency.
Fixes: d285203cf647 ("scsi: add support for a blk-mq based I/O path.")
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_ioctl.c | 16 ++++++++++++----
1 file changed, 12 insertions(+), 4 deletions(-)
diff --git a/drivers/scsi/scsi_ioctl.c b/drivers/scsi/scsi_ioctl.c
index 91525103d074..d370170f9270 100644
--- a/drivers/scsi/scsi_ioctl.c
+++ b/drivers/scsi/scsi_ioctl.c
@@ -412,6 +412,8 @@ static int scsi_complete_sghdr_rq(struct request *rq, struct sg_io_hdr *hdr,
struct bio *bio)
{
struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(rq);
+ u8 sense_buffer[SCSI_SENSE_BUFFERSIZE];
+ unsigned int sense_len = 0;
int r, ret = 0;
/*
@@ -431,10 +433,16 @@ static int scsi_complete_sghdr_rq(struct request *rq, struct sg_io_hdr *hdr,
hdr->sb_len_wr = 0;
if (scmd->sense_len && hdr->sbp) {
- int len = min((unsigned int) hdr->mx_sb_len, scmd->sense_len);
+ sense_len = min3(hdr->mx_sb_len, scmd->sense_len,
+ sizeof(sense_buffer));
+ memcpy(sense_buffer, scmd->sense_buffer, sense_len);
+ }
+
+ blk_mq_free_request(rq);
- if (!copy_to_user(hdr->sbp, scmd->sense_buffer, len))
- hdr->sb_len_wr = len;
+ if (sense_len) {
+ if (!copy_to_user(hdr->sbp, sense_buffer, sense_len))
+ hdr->sb_len_wr = sense_len;
else
ret = -EFAULT;
}
@@ -507,7 +515,7 @@ static int sg_io(struct scsi_device *sdev, struct sg_io_hdr *hdr,
hdr->duration = jiffies_to_msecs(jiffies - start_time);
- ret = scsi_complete_sghdr_rq(rq, hdr, bio);
+ return scsi_complete_sghdr_rq(rq, hdr, bio);
out_put_request:
blk_mq_free_request(rq);
--
2.43.0
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v4 2/4] scsi: core: release legacy ioctl request before user copyback
2026-11-18 8:33 [PATCH v4 0/4] scsi: release completed passthrough requests before user copyback Yingjie Wang
2026-11-18 8:33 ` [PATCH v4 1/4] scsi: core: release SG_IO request " Yingjie Wang
@ 2026-11-18 8:33 ` Yingjie Wang
2026-11-18 8:33 ` [PATCH v4 3/4] scsi: sr: release CDDA " Yingjie Wang
2026-11-18 8:33 ` [PATCH v4 4/4] scsi: bsg: release synchronous " Yingjie Wang
3 siblings, 0 replies; 5+ messages in thread
From: Yingjie Wang @ 2026-11-18 8:33 UTC (permalink / raw)
To: Martin K. Petersen, James E.J. Bottomley
Cc: linux-scsi, Bart Van Assche, Yang Xiuwei, James Houghton, linux-kernel
From: Yingjie Wang <x-professor@outlook.com>
SCSI_IOCTL_SEND_COMMAND copies sense or command data to userspace while
the completed request still occupies its queue tag. A fault that needs
the same queue can therefore wait for a resource held by the ioctl.
Save the command status and the compatibility-limited sense data, then
release the request before copying to userspace. The data buffer is a
separate allocation and remains valid until the existing buffer cleanup.
Keep the positive SCSI status and -EFAULT return conventions unchanged.
This complements the SG_IO fix by covering the legacy
SCSI_IOCTL_SEND_COMMAND path.
Fixes: d285203cf647 ("scsi: add support for a blk-mq based I/O path.")
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_ioctl.c | 18 ++++++++++++------
1 file changed, 12 insertions(+), 6 deletions(-)
diff --git a/drivers/scsi/scsi_ioctl.c b/drivers/scsi/scsi_ioctl.c
index d370170f9270..f064d92b8751 100644
--- a/drivers/scsi/scsi_ioctl.c
+++ b/drivers/scsi/scsi_ioctl.c
@@ -552,6 +552,8 @@ static int sg_scsi_ioctl(struct request_queue *q, bool open_for_write,
struct scsi_ioctl_command __user *sic)
{
struct request *rq;
+ u8 sense_buffer[16];
+ unsigned int sense_len = 0;
int err;
unsigned int in_len, out_len, bytes, opcode, cmdlen;
struct scsi_cmnd *scmd;
@@ -640,17 +642,21 @@ static int sg_scsi_ioctl(struct request_queue *q, bool open_for_write,
blk_execute_rq(rq, false);
err = scmd->result & 0xff; /* only 8 bit SCSI status */
+ if (err && scmd->sense_len && scmd->sense_buffer) {
+ /* limit sense len for backward compatibility */
+ sense_len = min(scmd->sense_len, sizeof(sense_buffer));
+ memcpy(sense_buffer, scmd->sense_buffer, sense_len);
+ }
+ blk_mq_free_request(rq);
+
if (err) {
- if (scmd->sense_len && scmd->sense_buffer) {
- /* limit sense len for backward compatibility */
- if (copy_to_user(sic->data, scmd->sense_buffer,
- min(scmd->sense_len, 16U)))
- err = -EFAULT;
- }
+ if (sense_len && copy_to_user(sic->data, sense_buffer, sense_len))
+ err = -EFAULT;
} else {
if (copy_to_user(sic->data, buffer, out_len))
err = -EFAULT;
}
+ goto error_free_buffer;
error:
blk_mq_free_request(rq);
--
2.43.0
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v4 3/4] scsi: sr: release CDDA request before user copyback
2026-11-18 8:33 [PATCH v4 0/4] scsi: release completed passthrough requests before user copyback Yingjie Wang
2026-11-18 8:33 ` [PATCH v4 1/4] scsi: core: release SG_IO request " Yingjie Wang
2026-11-18 8:33 ` [PATCH v4 2/4] scsi: core: release legacy ioctl " Yingjie Wang
@ 2026-11-18 8:33 ` Yingjie Wang
2026-11-18 8:33 ` [PATCH v4 4/4] scsi: bsg: release synchronous " Yingjie Wang
3 siblings, 0 replies; 5+ messages in thread
From: Yingjie Wang @ 2026-11-18 8:33 UTC (permalink / raw)
To: Martin K. Petersen, James E.J. Bottomley
Cc: linux-scsi, Bart Van Assche, Yang Xiuwei, James Houghton, linux-kernel
From: Yingjie Wang <x-professor@outlook.com>
sr_read_cdda_bpc() retains its completed request while unmapping the user
buffer. For a bounced data-in mapping, unmapping can copy to userspace
and fault. A fault that needs I/O from the same queue cannot progress if
the completed request retains the available tag.
Release the request after consuming its command result and sense data,
before unmapping the original BIO chain saved at submission. No request
or SCSI command field is accessed after the release. The mapping's BIO
references remain owned by blk_rq_unmap_user().
The scsi-mq request pool introduced this tag dependency. The later move
of the CDDA implementation from drivers/cdrom into sr was a relocation.
Fixes: d285203cf647 ("scsi: add support for a blk-mq based I/O path.")
Link: https://lore.kernel.org/r/20261022.scsi-cdda-lifetime.8f31a2c97b42@outlook.com
Cc: stable@vger.kernel.org
Signed-off-by: Yingjie Wang <x-professor@outlook.com>
---
drivers/scsi/sr.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/drivers/scsi/sr.c b/drivers/scsi/sr.c
index 4f2e8d1be6da..c64c25e9f404 100644
--- a/drivers/scsi/sr.c
+++ b/drivers/scsi/sr.c
@@ -969,8 +969,11 @@ static int sr_read_cdda_bpc(struct cdrom_device_info *cdi, void __user *ubuf,
ret = -EIO;
}
+ blk_mq_free_request(rq);
if (blk_rq_unmap_user(bio))
ret = -EFAULT;
+ return ret;
+
out_put_request:
blk_mq_free_request(rq);
return ret;
--
2.43.0
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v4 4/4] scsi: bsg: release synchronous request before user copyback
2026-11-18 8:33 [PATCH v4 0/4] scsi: release completed passthrough requests before user copyback Yingjie Wang
` (2 preceding siblings ...)
2026-11-18 8:33 ` [PATCH v4 3/4] scsi: sr: release CDDA " Yingjie Wang
@ 2026-11-18 8:33 ` Yingjie Wang
3 siblings, 0 replies; 5+ messages in thread
From: Yingjie Wang @ 2026-11-18 8:33 UTC (permalink / raw)
To: Martin K. Petersen, James E.J. Bottomley
Cc: linux-scsi, Bart Van Assche, Yang Xiuwei, James Houghton, linux-kernel
From: Yingjie Wang <x-professor@outlook.com>
The synchronous SCSI BSG ioctl copies sense data and unmaps its user
data buffer while retaining the completed request. Copyback can fault
and need I/O from the same queue, which cannot progress when completed
requests retain the available tags.
Fill the kernel-resident output fields and save the bounded sense data
before releasing the request. Perform both user copies afterward, using
the original BIO chain saved at mapping time. Preserve the copyback
error precedence established by the separate error-propagation fix.
The SG_IO v4 implementation predates scsi-mq. The fixed scsi-mq request
pool introduced this dependency; moving the callback into scsi_bsg.c
did not change its ownership ordering.
Fixes: d285203cf647 ("scsi: add support for a blk-mq based I/O path.")
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 | 22 ++++++++++++++--------
1 file changed, 14 insertions(+), 8 deletions(-)
diff --git a/drivers/scsi/scsi_bsg.c b/drivers/scsi/scsi_bsg.c
index 3cf1caad02f7..5ffbd1843861 100644
--- a/drivers/scsi/scsi_bsg.c
+++ b/drivers/scsi/scsi_bsg.c
@@ -207,6 +207,8 @@ static int scsi_bsg_sg_io_fn(struct request_queue *q, struct sg_io_v4 *hdr,
struct scsi_cmnd *scmd;
struct request *rq;
struct bio *bio;
+ u8 sense_buffer[SCSI_SENSE_BUFFERSIZE];
+ unsigned int sense_len = 0;
int ret, unmap_ret;
if (hdr->protocol != BSG_PROTOCOL_SCSI ||
@@ -266,24 +268,28 @@ static int scsi_bsg_sg_io_fn(struct request_queue *q, struct sg_io_v4 *hdr,
hdr->response_len = 0;
if (scmd->sense_len && hdr->response) {
- int len = min_t(unsigned int, hdr->max_response_len,
- scmd->sense_len);
-
- if (copy_to_user(uptr64(hdr->response), scmd->sense_buffer,
- len))
- ret = -EFAULT;
- else
- hdr->response_len = len;
+ sense_len = min3(hdr->max_response_len, scmd->sense_len,
+ sizeof(sense_buffer));
+ memcpy(sense_buffer, scmd->sense_buffer, sense_len);
}
if (rq_data_dir(rq) == READ)
hdr->din_resid = scmd->resid_len;
else
hdr->dout_resid = scmd->resid_len;
+ blk_mq_free_request(rq);
+
+ if (sense_len) {
+ if (copy_to_user(uptr64(hdr->response), sense_buffer, sense_len))
+ ret = -EFAULT;
+ else
+ hdr->response_len = sense_len;
+ }
unmap_ret = blk_rq_unmap_user(bio);
if (!ret)
ret = unmap_ret;
+ return ret;
out_put_request:
blk_mq_free_request(rq);
--
2.43.0
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-11-18 8:33 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-18 8:33 [PATCH v4 0/4] scsi: release completed passthrough requests before user copyback Yingjie Wang
2026-11-18 8:33 ` [PATCH v4 1/4] scsi: core: release SG_IO request " Yingjie Wang
2026-11-18 8:33 ` [PATCH v4 2/4] scsi: core: release legacy ioctl " Yingjie Wang
2026-11-18 8:33 ` [PATCH v4 3/4] scsi: sr: release CDDA " Yingjie Wang
2026-11-18 8:33 ` [PATCH v4 4/4] scsi: bsg: release synchronous " Yingjie Wang
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®