From: Alan Stern <stern@rowland.harvard.edu>
To: "Jozo M." <jomajm@gmail.com>
Cc: linux-usb@vger.kernel.org
Subject: Re: gadgetfs inode.c - possible memory corruption ?
Date: Thu, 7 Jul 2022 12:11:56 -0400 [thread overview]
Message-ID: <YscFzAAkhbPojMVL@rowland.harvard.edu> (raw)
In-Reply-To: <CA+BOZ0uBvKHc3idN+Pn_g4Z4e5ObYPZ4WY4f78fzj_B=8c5qJw@mail.gmail.com>
On Thu, Jul 07, 2022 at 11:43:09AM +0200, Jozo M. wrote:
> Hello,
>
> my kernel running on imx6 was crashing on USB gadgetfs because my
> kernel was using wait_event API instead of completion (I was convinced
> it is due to wrong HW setup).
> During research gadgetfs inode.c function ep_io was not clear for me:
>
> we are submiting USB request here
> value = usb_ep_queue (epdata->ep, req, GFP_ATOMIC);
> then we are waiting for completion here:
> value = wait_for_completion_interruptible(&done);
> but if completion is interrupted we end up here:
> spin_unlock_irq (&epdata->dev->lock);
> DBG (epdata->dev, "endpoint gone\n");
> epdata->status = -ENODEV;
You left out part of the code. We end up at this code in the case where
epdata->ep == NULL, and the only way that can happen is if the endpoint
was removed while we were waiting for the completion.
> At this point ep_io is terminated and stack is not valid. Later on
> epio_complete might be called from IRQ and it calls complete ((struct
> completion *)req->context) but stack is no longer valid;
> Shouldn't we put req->context = NULL; before spin_unlock_irq
> (&epdata->dev->lock); ?
> req->context = NULL;
> spin_unlock_irq (&epdata->dev->lock);
> DBG (epdata->dev, "endpoint gone\n");
> epdata->status = -ENODEV;
You're right that this is a bug, but your solution is not correct.
There is a race: epio_complete can run at the same time as this code if
the endpoint is removed concurrently with the interrupt, and your
approach is still subject to this race.
The right way to fix the problem is to call wait_for_completion(&done)
between the DBG and the assignment to epdata->status. That way the
stack will still be valid when epio_complete runs.
Please feel free to submit a patch doing this.
Alan Stern
next prev parent reply other threads:[~2022-07-07 16:12 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-07-07 9:43 gadgetfs inode.c - possible memory corruption ? Jozo M.
2022-07-07 16:11 ` Alan Stern [this message]
2022-07-07 16:47 ` Jozo M.
2022-07-07 17:16 ` Alan Stern
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=YscFzAAkhbPojMVL@rowland.harvard.edu \
--to=stern@rowland.harvard.edu \
--cc=jomajm@gmail.com \
--cc=linux-usb@vger.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