mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Thinh Nguyen <Thinh.Nguyen@synopsys.com>
To: Wesley Cheng <quic_wcheng@quicinc.com>
Cc: Thinh Nguyen <Thinh.Nguyen@synopsys.com>,
	"gregkh@linuxfoundation.org" <gregkh@linuxfoundation.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"linux-usb@vger.kernel.org" <linux-usb@vger.kernel.org>
Subject: Re: [PATCH 1/2] Revert "usb: dwc3: gadget: Fix incorrect UDC state after manual deconfiguration"
Date: Mon, 3 Mar 2025 23:24:17 +0000	[thread overview]
Message-ID: <20250303232411.2sq47ohcygmwd2bo@synopsys.com> (raw)
In-Reply-To: <109547e4-fc2a-42b6-9f16-6e0979b9a49b@quicinc.com>

On Fri, Feb 28, 2025, Wesley Cheng wrote:
> Hi Thinh,
> 
> On 2/28/2025 6:01 PM, Thinh Nguyen wrote:
> > Hi,
> > 
> > On Fri, Feb 28, 2025, Wesley Cheng wrote:
> >> This reverts commit 1ff24d40b3c3c673d833c546f898133b80dffc39.
> >>
> >> The following change creates a potential use after freed scenario within
> >> the UDC core level.  When transitioning from peripheral to host mode, the
> >> DWC3 DRD will remove the USB UDC using usb_del_gadget().  As part of the
> >> UDC removal routine, the UDC core will issue a pullup disable call back to
> >> the DWC3 gadget driver, which will now call usb_gadget_set_state(), and
> >> queues gadget->work.  If gadget->work is unable to complete before the USB
> >> gadget is freed, then we will run into the use after freed situation.
> >>
> >> Below is the callstack in question:
> >>
> >> dwc3_gadget_exit()
> >>  --> usb_del_gadget()
> >>  --> gadget_unbind_driver()
> >>   --> device_del()
> >>    --> gadget_unbind_driver()
> >>     --> usb_gadget_disconnect_locked()
> >>      --> pullup(0)
> >>       --> dwc3_gadget_soft_disconnect()
> >>        --> usb_gadget_set_state(UNATTACHED)
> >>         --> queue_work(gadget->work) --> async
> >>   --> device_unregister()
> >>
> >> Unfortunately, the flush_work() call within usb_del_gadget() doesn't work
> >> in this situation, as it happens before the device_del() and soft
> >> disconnect sequence.
> >>
> >> Signed-off-by: Wesley Cheng <quic_wcheng@quicinc.com>
> >> ---
> >>  drivers/usb/dwc3/gadget.c | 2 --
> >>  1 file changed, 2 deletions(-)
> >>
> >> diff --git a/drivers/usb/dwc3/gadget.c b/drivers/usb/dwc3/gadget.c
> >> index 5156fafbda6c..921f72995dbf 100644
> >> --- a/drivers/usb/dwc3/gadget.c
> >> +++ b/drivers/usb/dwc3/gadget.c
> >> @@ -2781,8 +2781,6 @@ static int dwc3_gadget_soft_disconnect(struct dwc3 *dwc)
> >>  	__dwc3_gadget_stop(dwc);
> >>  	spin_unlock_irqrestore(&dwc->lock, flags);
> >>  
> >> -	usb_gadget_set_state(dwc->gadget, USB_STATE_NOTATTACHED);
> >> -
> >>  	return ret;
> >>  }
> >>  
> > 
> > Have you tried with this fix?
> > https://urldefense.com/v3/__https://web.git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=399a45e5237ca14037120b1b895bd38a3b4492ea__;!!A4F2R9G_pg!c0-XJOQST8G_jMPRaU0NxsP4zRPkygId8mQEsUGwxzQEsz2XuX4JsnesMTuaGuyVgN7hNmYJ3gg-cUeowVZaG_m7DP4$ 
> > 
> 
> Thanks for pointing that out.  Looks like its working fine for me.  Bit odd
> that we're still potentially utilizing the usb gadget after calling
> device_del(), but I guess that should be fine.
> 

Yeah, but device_del() isn't usb_del_gadget(). We just need to make sure
the usb gadget isn't used after usb_del_gadget().

BR,
Thinh

  reply	other threads:[~2025-03-03 23:24 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-01  0:34 [PATCH 0/2] Fix use after freed situation in UDC core Wesley Cheng
2025-03-01  0:34 ` [PATCH 1/2] Revert "usb: dwc3: gadget: Fix incorrect UDC state after manual deconfiguration" Wesley Cheng
2025-03-01  2:01   ` Thinh Nguyen
2025-03-01  2:33     ` Wesley Cheng
2025-03-03 23:24       ` Thinh Nguyen [this message]
2025-03-01  0:34 ` [PATCH 2/2] usb: gadget: udc: Update USB gadget state during soft disconnect Wesley Cheng

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=20250303232411.2sq47ohcygmwd2bo@synopsys.com \
    --to=thinh.nguyen@synopsys.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=quic_wcheng@quicinc.com \
    /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®