* [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-19 21:36 ` Bart Van Assche
2026-11-18 8:33 ` [PATCH v4 2/4] scsi: core: release legacy ioctl " Yingjie Wang
` (2 subsequent siblings)
3 siblings, 1 reply; 8+ 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] 8+ messages in thread* Re: [PATCH v4 1/4] scsi: core: release SG_IO request before user copyback
2026-11-18 8:33 ` [PATCH v4 1/4] scsi: core: release SG_IO request " Yingjie Wang
@ 2026-11-19 21:36 ` Bart Van Assche
0 siblings, 0 replies; 8+ messages in thread
From: Bart Van Assche @ 2026-11-19 21:36 UTC (permalink / raw)
To: Yingjie Wang, Martin K. Petersen, James E.J. Bottomley
Cc: linux-scsi, Yang Xiuwei, James Houghton, linux-kernel
On 11/18/26 12:33 AM, Yingjie Wang wrote:
> 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.
Does the same dependency exist on the submission side? All four code
paths access user memory after scsi_alloc_request() has succeeded and
before blk_execute_rq() is called:
- sg_io() calls copy_from_user() from scsi_fill_sghdr_rq() and next
calls blk_rq_map_user_io().
- sg_scsi_ioctl() calls copy_from_user() twice.
- scsi_bsg_sg_io_fn() calls copy_from_user() and blk_rq_map_user().
- sr_read_cdda_bpc() calls blk_rq_map_user().
If no I/O scheduler has been selected, the request allocated by
scsi_alloc_request() already owns a driver tag. If the host has only
one tag and if any of the above user memory accesses triggers a page
fault that requires I/O on the same queue, isn't that the same deadlock
as the one this series fixes? Pinning a data buffer that is not yet
resident seems at least as likely to me as a fault during copyback.
With mq-deadline there are two scheduler tags for a queue depth of one
so a single nested I/O request can make progress in that case.
I have not tried to trigger this. If my analysis is correct, please
either fix the submission side too or mention in the cover letter that
this series only addresses the completion side.
Thanks,
Bart.
^ permalink raw reply [flat|nested] 8+ 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-19 21:36 ` Bart Van Assche
2026-11-19 21:37 ` Bart Van Assche
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, 2 replies; 8+ 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] 8+ messages in thread* Re: [PATCH v4 2/4] scsi: core: release legacy ioctl request before user copyback
2026-11-18 8:33 ` [PATCH v4 2/4] scsi: core: release legacy ioctl " Yingjie Wang
@ 2026-11-19 21:36 ` Bart Van Assche
2026-11-19 21:37 ` Bart Van Assche
1 sibling, 0 replies; 8+ messages in thread
From: Bart Van Assche @ 2026-11-19 21:36 UTC (permalink / raw)
To: Yingjie Wang, Martin K. Petersen, James E.J. Bottomley
Cc: linux-scsi, Yang Xiuwei, James Houghton, linux-kernel
On 11/18/26 12:33 AM, Yingjie Wang wrote:
> + 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;
> }
[ ... ]
> - ret = scsi_complete_sghdr_rq(rq, hdr, bio);
> + return scsi_complete_sghdr_rq(rq, hdr, bio);
>
> out_put_request:
> blk_mq_free_request(rq);
With this patch applied scsi_complete_sghdr_rq() frees the request that
has been allocated by its caller. Nothing in the name of that function
nor in the code of sg_io() makes that clear. Please either add a comment
above scsi_complete_sghdr_rq() that explains that it frees @rq or keep
the blk_mq_free_request() call in sg_io(), e.g. by splitting
scsi_complete_sghdr_rq() into a function that saves the request state
and a function that copies that state to user space.
Thanks,
Bart.
^ permalink raw reply [flat|nested] 8+ messages in thread* Re: [PATCH v4 2/4] scsi: core: release legacy ioctl request before user copyback
2026-11-18 8:33 ` [PATCH v4 2/4] scsi: core: release legacy ioctl " Yingjie Wang
2026-11-19 21:36 ` Bart Van Assche
@ 2026-11-19 21:37 ` Bart Van Assche
1 sibling, 0 replies; 8+ messages in thread
From: Bart Van Assche @ 2026-11-19 21:37 UTC (permalink / raw)
To: Yingjie Wang, Martin K. Petersen, James E.J. Bottomley
Cc: linux-scsi, Yang Xiuwei, James Houghton, linux-kernel
On 11/18/26 12:33 AM, Yingjie Wang wrote:
> } else {
> if (copy_to_user(sic->data, buffer, out_len))
> err = -EFAULT;
> }
> + goto error_free_buffer;
>
> error:
> blk_mq_free_request(rq);
A goto statement that only skips the label and the statement that
follows it makes this function harder to read. Has it been considered to
restructure this code such that this goto statement is not necessary,
e.g. by setting 'rq' to NULL after it has been freed and by changing
the code below the 'error' label into the following?
if (rq)
blk_mq_free_request(rq);
Thanks,
Bart.
^ permalink raw reply [flat|nested] 8+ 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; 8+ 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] 8+ 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; 8+ 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] 8+ messages in thread