From: Alan Stern <stern@rowland.harvard.edu>
To: linyyuan@codeaurora.org
Cc: Felipe Balbi <balbi@kernel.org>,
Thinh Nguyen <Thinh.Nguyen@synopsys.com>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org,
Jack Pham <jackp@codeaurora.org>
Subject: Re: [PATCH] usb: dwc3: fix race of usb_gadget_driver operation
Date: Sat, 26 Jun 2021 11:03:04 -0400 [thread overview]
Message-ID: <20210626150304.GA601624@rowland.harvard.edu> (raw)
In-Reply-To: <b24825113327c72c742d55e89ec2726e@codeaurora.org>
On Sat, Jun 26, 2021 at 09:16:25AM +0800, linyyuan@codeaurora.org wrote:
> On 2021-06-26 00:37, Alan Stern wrote:
> > On Fri, Jun 25, 2021 at 06:44:15PM +0800, Linyu Yuan wrote:
> > > --- a/drivers/usb/dwc3/ep0.c
> > > +++ b/drivers/usb/dwc3/ep0.c
> > > @@ -597,10 +597,11 @@ static int dwc3_ep0_set_address(struct dwc3
> > > *dwc, struct usb_ctrlrequest *ctrl)
> > >
> > > static int dwc3_ep0_delegate_req(struct dwc3 *dwc, struct
> > > usb_ctrlrequest *ctrl)
> > > {
> > > - int ret;
> > > + int ret = 0;
> > >
> > > spin_unlock(&dwc->lock);
> > > - ret = dwc->gadget_driver->setup(dwc->gadget, ctrl);
> > > + if (dwc->async_callbacks)
> > > + ret = dwc->gadget_driver->setup(dwc->gadget, ctrl);
> > > spin_lock(&dwc->lock);
> >
> > Here and in the other places, you should test dwc->async_callbacks
> > _before_ dropping the spinlock. Otherwise there is a race (the flag
> > could be written at about the same time it is checked).
> thanks for your comments,
>
> if you think there is race here, how to make sure gadget_driver pointer is
> safe,
> this is closest place where we can confirm it is non-NULL by checking
> async_callbacks ?
I explained this twice already: We know that gadget_driver is not
NULL because usb_gadget_remove_driver calls synchronize_irq before
doing usb_gadget_udc_stop.
Look at this timing diagram:
CPU0 CPU1
---- ----
IRQ happens for setup packet
Handler sees async_callbacks
is enabled
Handler unlocks dwc->lock
usb_gadget_remove_driver runs
Disables async callbacks
Calls synchronize_irq
Handler calls dwc-> . waits for IRQ handler to
gadget_driver->setup . return
Handler locks dwc-lock .
... .
Handler returns .
. synchronize_irq returns
Calls usb_gadget_udc_stop
dwc->gadget_driver is
set to NULL
As you can see, dwc->gadget_driver is non-NULL when CPU0 uses it,
even though async_callbacks gets cleared during the time when the
lock is released.
Alan Stern
next prev parent reply other threads:[~2021-06-26 15:03 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-06-25 10:44 [PATCH] usb: dwc3: fix race of usb_gadget_driver operation Linyu Yuan
2021-06-25 16:37 ` Alan Stern
2021-06-26 1:16 ` linyyuan
2021-06-26 15:03 ` Alan Stern [this message]
2021-06-27 2:48 ` linyyuan
2021-06-27 14:09 ` Alan Stern
2021-06-28 9:36 ` linyyuan
2021-06-28 14:10 ` 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=20210626150304.GA601624@rowland.harvard.edu \
--to=stern@rowland.harvard.edu \
--cc=Thinh.Nguyen@synopsys.com \
--cc=balbi@kernel.org \
--cc=gregkh@linuxfoundation.org \
--cc=jackp@codeaurora.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-usb@vger.kernel.org \
--cc=linyyuan@codeaurora.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;
as well as URLs for NNTP newsgroup(s).