From: John Garry <john.garry@linux.dev>
To: Hui Peng <benquike@gmail.com>,
Doug Gilbert <dgilbert@interlog.com>,
"James E . J . Bottomley" <James.Bottomley@HansenPartnership.com>,
"Martin K . Petersen" <mkp@kernel.org>
Cc: linux-scsi@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] scsi: sg: clean up request on error paths in sg_read()
Date: Mon, 21 Sep 2026 15:47:41 +0100 [thread overview]
Message-ID: <69218179-d750-4880-8be0-2ccab7d76a6d@linux.dev> (raw)
In-Reply-To: <20260919204814.2815374-1-benquike@gmail.com>
On 9/19/26 21:48, Hui Peng wrote:
> In sg_read(), once sg_get_rq_mark() locates a completed Sg_request and
> transitions srp->done from 1 to 2, an allocation failure in kzalloc() or
> a user-copy fault (-EFAULT) in copy_to_user() / sg_read_oxfer() returns
> or jumps to free_old_hdr without calling sg_finish_rem_req(srp) and
> sg_remove_request(sfp, srp).
>
> Because srp->done is already 2, subsequent sg_read() calls skip the
> request, permanently leaking srp->bio and keeping sfp->res_in_use = 1
> (blocking SG_SET_RESERVED_SIZE with -EBUSY and wedging non-SG_FLAG_NO_DXFER
> writes with -EDOM) until the file descriptor is closed.
>
> Always call sg_finish_rem_req(srp) and sg_remove_request(sfp, srp) under
> the free_old_hdr cleanup label, matching sg_new_read().
>
> Fixes: cb59e8408381 ("[PATCH] sg.c: update")
> Assisted-by: LLM
> Signed-off-by: Hui Peng <benquike@gmail.com>
How was this tested?
> ---
> diff --git a/drivers/scsi/sg.c b/drivers/scsi/sg.c
> --- a/drivers/scsi/sg.c
> +++ b/drivers/scsi/sg.c
> @@ -480,8 +480,10 @@ sg_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
>
> hp = &srp->header;
> old_hdr = kzalloc(SZ_SG_HEADER, GFP_KERNEL);
> - if (!old_hdr)
> - return -ENOMEM;
> + if (!old_hdr) {
> + retval = -ENOMEM;
> + goto free_old_hdr;
> + }
>
> old_hdr->reply_len = (int) hp->timeout;
> old_hdr->pack_len = old_hdr->reply_len; /* old, strange behaviour */
> @@ -543,10 +545,10 @@ sg_read(struct file *filp, char __user *buf, size_t count, loff_t * ppos)
> }
> } else
> count = (old_hdr->result == 0) ? 0 : -EIO;
> - sg_finish_rem_req(srp);
> - sg_remove_request(sfp, srp);
> retval = count;
> free_old_hdr:
This is no longer a fully appropriate label name
> + sg_finish_rem_req(srp);
> + sg_remove_request(sfp, srp);
> kfree(old_hdr);
> return retval;
> }
next prev parent reply other threads:[~2026-09-21 14:47 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-19 20:48 Hui Peng
2026-09-21 14:47 ` John Garry [this message]
2026-09-24 5:46 ` [PATCH v2] scsi: sg: finish request on error " Hui Peng
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=69218179-d750-4880-8be0-2ccab7d76a6d@linux.dev \
--to=john.garry@linux.dev \
--cc=James.Bottomley@HansenPartnership.com \
--cc=benquike@gmail.com \
--cc=dgilbert@interlog.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-scsi@vger.kernel.org \
--cc=mkp@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®