* [PATCH v5 1/4] scsi: core: Release SG_IO request before user copyback
2026-11-21 8:27 [PATCH v5 0/4] scsi: Release completed passthrough requests before user copyback Yingjie Wang
@ 2026-11-21 8:27 ` Yingjie Wang
2026-11-21 8:27 ` [PATCH v5 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-21 8:27 UTC (permalink / raw)
To: Martin K. Petersen
Cc: James E.J. Bottomley, linux-scsi, Bart Van Assche, Yang Xiuwei,
James Houghton, linux-kernel
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 BIO chain holds its own references, which blk_rq_unmap_user()
releases.
The finite number of request tags introduced by scsi-mq creates this
dependency. The later move of this ioctl implementation into drivers/scsi
did not introduce it.
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>
---
Changes in v5:
- Document that scsi_complete_sghdr_rq() frees its request (Bart).
drivers/scsi/scsi_ioctl.c | 20 ++++++++++++++++----
1 file changed, 16 insertions(+), 4 deletions(-)
diff --git a/drivers/scsi/scsi_ioctl.c b/drivers/scsi/scsi_ioctl.c
index 91525103d074..16a0b6bf573e 100644
--- a/drivers/scsi/scsi_ioctl.c
+++ b/drivers/scsi/scsi_ioctl.c
@@ -408,10 +408,16 @@ static int scsi_fill_sghdr_rq(struct scsi_device *sdev, struct request *rq,
return 0;
}
+/*
+ * Save completion state and free rq before user copies and BIO unmapping.
+ * The caller must not use rq afterwards.
+ */
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 +437,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 +519,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 v5 2/4] scsi: core: Release legacy ioctl request before user copyback
2026-11-21 8:27 [PATCH v5 0/4] scsi: Release completed passthrough requests before user copyback Yingjie Wang
2026-11-21 8:27 ` [PATCH v5 1/4] scsi: core: Release SG_IO request " Yingjie Wang
@ 2026-11-21 8:27 ` Yingjie Wang
2026-11-21 8:27 ` [PATCH v5 3/4] scsi: sr: Release CDDA " Yingjie Wang
2026-11-21 8:27 ` [PATCH v5 4/4] scsi: bsg: Release synchronous " Yingjie Wang
3 siblings, 0 replies; 5+ messages in thread
From: Yingjie Wang @ 2026-11-21 8:27 UTC (permalink / raw)
To: Martin K. Petersen
Cc: James E.J. Bottomley, linux-scsi, Bart Van Assche, Yang Xiuwei,
James Houghton, linux-kernel
SCSI_IOCTL_SEND_COMMAND copies sense or returned 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.
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>
---
Changes in v5:
- Set rq to NULL after the early free and use conditional cleanup (Bart).
drivers/scsi/scsi_ioctl.c | 21 ++++++++++++++-------
1 file changed, 14 insertions(+), 7 deletions(-)
diff --git a/drivers/scsi/scsi_ioctl.c b/drivers/scsi/scsi_ioctl.c
index 16a0b6bf573e..25c905c4943b 100644
--- a/drivers/scsi/scsi_ioctl.c
+++ b/drivers/scsi/scsi_ioctl.c
@@ -556,6 +556,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;
@@ -644,20 +646,25 @@ 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);
+ rq = NULL;
+
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;
}
error:
- blk_mq_free_request(rq);
+ if (rq)
+ blk_mq_free_request(rq);
error_free_buffer:
kfree(buffer);
--
2.43.0
^ permalink raw reply [flat|nested] 5+ messages in thread* [PATCH v5 3/4] scsi: sr: Release CDDA request before user copyback
2026-11-21 8:27 [PATCH v5 0/4] scsi: Release completed passthrough requests before user copyback Yingjie Wang
2026-11-21 8:27 ` [PATCH v5 1/4] scsi: core: Release SG_IO request " Yingjie Wang
2026-11-21 8:27 ` [PATCH v5 2/4] scsi: core: Release legacy ioctl " Yingjie Wang
@ 2026-11-21 8:27 ` Yingjie Wang
2026-11-21 8:27 ` [PATCH v5 4/4] scsi: bsg: Release synchronous " Yingjie Wang
3 siblings, 0 replies; 5+ messages in thread
From: Yingjie Wang @ 2026-11-21 8:27 UTC (permalink / raw)
To: Martin K. Petersen
Cc: James E.J. Bottomley, linux-scsi, Bart Van Assche, Yang Xiuwei,
James Houghton, linux-kernel
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 saved BIO chain
holds its own references, which blk_rq_unmap_user() releases.
The finite number of request tags introduced by scsi-mq creates this
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>
---
No code changes from v4. Clarify BIO ownership and the tag dependency.
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 v5 4/4] scsi: bsg: Release synchronous request before user copyback
2026-11-21 8:27 [PATCH v5 0/4] scsi: Release completed passthrough requests before user copyback Yingjie Wang
` (2 preceding siblings ...)
2026-11-21 8:27 ` [PATCH v5 3/4] scsi: sr: Release CDDA " Yingjie Wang
@ 2026-11-21 8:27 ` Yingjie Wang
3 siblings, 0 replies; 5+ messages in thread
From: Yingjie Wang @ 2026-11-21 8:27 UTC (permalink / raw)
To: Martin K. Petersen
Cc: James E.J. Bottomley, linux-scsi, Bart Van Assche, Yang Xiuwei,
James Houghton, linux-kernel
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 commit a52f2ad45726 ("scsi: bsg: Report
synchronous data copyback failures").
The SG_IO v4 implementation predates scsi-mq. The finite number of request
tags introduced by scsi-mq creates 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>
---
No functional changes from v4. Clarify the tag dependency and name the
existing copyback-error fix.
drivers/scsi/scsi_bsg.c | 23 +++++++++++++++--------
1 file changed, 15 insertions(+), 8 deletions(-)
diff --git a/drivers/scsi/scsi_bsg.c b/drivers/scsi/scsi_bsg.c
index 3cf1caad02f7..c17cad798326 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,14 +268,9 @@ 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)
@@ -281,9 +278,19 @@ static int scsi_bsg_sg_io_fn(struct request_queue *q, struct sg_io_v4 *hdr,
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