scsi: release completed passthrough requests before user copyback
v4 cover scsi: release completed passthrough requests before user copyback
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/[email protected]
v2: https://lore.kernel.org/r/[email protected]
RFC v3: https://lore.kernel.org/r/[email protected]
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
Signature · 1 line
2.43.0
PATCH v4 1/4 scsi: core: release SG_IO request before user copyback
From: Yingjie Wang <[email protected]>
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.
--- drivers/scsi/scsi_ioctl.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)
drivers/scsi/scsi_ioctl.c +12 -4
@@ -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);
Signature · 1 line
2.43.0
PATCH v4 2/4 scsi: core: release legacy ioctl request before user copyback
In reply to Yingjie WangFrom: Yingjie Wang <[email protected]>
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.
--- drivers/scsi/scsi_ioctl.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-)
drivers/scsi/scsi_ioctl.c +12 -6
@@ -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 &©_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);
Signature · 1 line
2.43.0
PATCH v4 3/4 scsi: sr: release CDDA request before user copyback
In reply to Yingjie WangFrom: Yingjie Wang <[email protected]>
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.
--- drivers/scsi/sr.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/scsi/sr.c +3 -0
@@ -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;
Signature · 1 line
2.43.0
PATCH v4 4/4 scsi: bsg: release synchronous request before user copyback
In reply to Yingjie WangFrom: Yingjie Wang <[email protected]>
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.
--- drivers/scsi/scsi_bsg.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-)
drivers/scsi/scsi_bsg.c +14 -8
@@ -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);
Signature · 1 line
2.43.0
Re: [PATCH v4 1/4] scsi: core: release SG_IO request before user copyback
In reply to Yingjie WangQuoted 5 lines · Yingjie Wang
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.
Re: [PATCH v4 2/4] scsi: core: release legacy ioctl request before user copyback
In reply to Yingjie WangQuoted 10 lines · Yingjie Wang
(quoted diff) +4
-2+ 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; }
[ ... ]
Review
Quoted 5 lines
(quoted diff) +1
-1- 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.
Re: [PATCH v4 2/4] scsi: core: release legacy ioctl request before user copyback
In reply to Yingjie Wangdrivers/scsi/scsi_ioctl.c static int sg_scsi_ioctl(struct request_queue *q, bool open_for_write, · line 662
Quoted 8 lines · Yingjie Wang
} else { if (copy_to_user(sic->data, buffer, out_len)) err = -EFAULT; }drivers/scsi/scsi_ioctl.c +1
-0+ 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.