mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v5 0/4] scsi: Release completed passthrough requests before user copyback
@ 2026-11-21  8:27 Yingjie Wang
  2026-11-21  8:27 ` [PATCH v5 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-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

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 access faults and needs I/O from the same queue, the
task can wait for a tag held by the request it would release only after
the fault returns.

Each patch saves the completion state needed for copyback, releases the
request, then performs the user access:

  1/4  SG_IO on a SCSI block device
  2/4  the legacy SCSI_IOCTL_SEND_COMMAND ioctl
  3/4  CDROMREADAUDIO through sr_read_cdda_bpc()
  4/4  the synchronous BSG ioctl

This series addresses the completion side only. As Bart pointed out,
these paths also access user memory after allocating a request and
before submitting it. Without an I/O scheduler, that request already
holds a driver tag, so a fault needing I/O from the same exhausted tag
pool can encounter a similar dependency. The submission-side ordering
is unchanged here.

Moving CDB copies before request allocation would not address faults
in blk_rq_map_user*(), which needs an allocated request. Addressing
that dependency requires a different approach and is outside this
series.

Changes in v5:
- Document that scsi_complete_sghdr_rq() frees its request, as requested
  by Bart Van Assche.
- Set rq to NULL after the legacy ioctl's early free and use conditional
  cleanup, removing the extra success-path goto, as suggested by Bart.
- Clarify the completion-side scope and tag dependency. Patches 3 and 4
  have no functional changes.

Tested on 9f9e0b1e5399 with QEMU, a virtio-scsi disk and CD-ROM,
KASAN and lockdep. The base and v5 each passed 960 regression checks,
including 220 synchronous checks. Tracing covered 100 synchronous
windows: 80 showed v5 releasing the request before unmapping, while
the base retained it; 20 checked legacy ioctl execution and release.
CDDA coverage exercised EIO and release ordering, not successful audio
extraction. The unchanged io_uring path still retained its request
until task work, as expected. No page-fault deadlock was reproduced.

Each patch passed an x86_64 W=1 build of the affected objects. Kernel
selftests were not run.

The BSG io_uring completion fix remains a separate two-patch series,
which applies on top of these four patches.

v4: https://lore.kernel.org/r/cover.1794990780.git.x-professor@outlook.com
RFC v3: https://lore.kernel.org/r/cover.1794045010.git.x-professor@outlook.com
Bart's scope question:
https://lore.kernel.org/r/3dcb1a44-6e99-4d34-9b49-675d98c90da1@acm.org

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   | 23 ++++++++++++++--------
 drivers/scsi/scsi_ioctl.c | 41 ++++++++++++++++++++++++++++-----------
 drivers/scsi/sr.c         |  3 +++
 3 files changed, 48 insertions(+), 19 deletions(-)


base-commit: 9f9e0b1e5399045982788a954d3461de894e40c7
-- 
2.43.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [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

end of thread, other threads:[~2026-11-21  8:27 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [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

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®