linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Thinh Nguyen <Thinh.Nguyen@synopsys.com>
To: Sven Peter <sven@svenpeter.dev>
Cc: Thinh Nguyen <Thinh.Nguyen@synopsys.com>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Felipe Balbi <felipe.balbi@linux.intel.com>,
	Janne Grunau <j@jannau.net>,
	"linux-usb@vger.kernel.org" <linux-usb@vger.kernel.org>,
	"stable@vger.kernel.org" <stable@vger.kernel.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"asahi@lists.linux.dev" <asahi@lists.linux.dev>
Subject: Re: [PATCH] usb: dwc3: Fix race between dwc3_set_mode and __dwc3_set_mode
Date: Mon, 28 Nov 2022 19:14:16 +0000	[thread overview]
Message-ID: <20221128191411.b57fmzppso2xdnna@synopsys.com> (raw)
In-Reply-To: <20221128161526.79730-1-sven@svenpeter.dev>

Hi,

On Mon, Nov 28, 2022, Sven Peter wrote:
> dwc->desired_dr_role is changed by dwc3_set_mode inside a spinlock but
> then read by __dwc3_set_mode outside of that lock. This can lead to a
> race condition when very quick successive role switch events happen:
> 
> CPU A
> 	dwc3_set_mode(DWC3_GCTL_PRTCAP_HOST) // first role switch event
> 		spin_lock_irqsave(&dwc->lock, flags);
> 		dwc->desired_dr_role = mode; // DWC3_GCTL_PRTCAP_HOST
> 		spin_unlock_irqrestore(&dwc->lock, flags);
> 		queue_work(system_freezable_wq, &dwc->drd_work);
> 
> CPU B
> 	__dwc3_set_mode
> 		// ....
> 		spin_lock_irqsave(&dwc->lock, flags);
> 		// desired_dr_role is DWC3_GCTL_PRTCAP_HOST
> 		dwc3_set_prtcap(dwc, dwc->desired_dr_role);
> 		spin_unlock_irqrestore(&dwc->lock, flags);
> 
> CPU A
> 	dwc3_set_mode(DWC3_GCTL_PRTCAP_DEVICE) // second event
> 		spin_lock_irqsave(&dwc->lock, flags);
> 		dwc->desired_dr_role = mode; // DWC3_GCTL_PRTCAP_DEVICE
> 		spin_unlock_irqrestore(&dwc->lock, flags);
> 
> CPU B (continues running __dwc3_set_mode)
> 	switch (dwc->desired_dr_role) { // DWC3_GCTL_PRTCAP_DEVICE
> 	// ....
> 	case DWC3_GCTL_PRTCAP_DEVICE:
> 		// ....
> 		ret = dwc3_gadget_init(dwc);
> 
> We then have DWC3_GCTL.DWC3_GCTL_PRTCAPDIR = DWC3_GCTL_PRTCAP_HOST and
> dwc->current_dr_role = DWC3_GCTL_PRTCAP_HOST but initialized the
> controller in device mode. It's also possible to get into a state
> where both host and device are intialized at the same time.
> Fix this race by creating a local copy of desired_dr_role inside
> __dwc3_set_mode while holding dwc->lock.
> 
> Fixes: 41ce1456e1db ("usb: dwc3: core: make dwc3_set_mode() work properly")
> Signed-off-by: Sven Peter <sven@svenpeter.dev>
> ---
>  drivers/usb/dwc3/core.c | 16 ++++++++++------
>  1 file changed, 10 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/usb/dwc3/core.c b/drivers/usb/dwc3/core.c
> index 1f348bc867c2..fc38a8b13efa 100644
> --- a/drivers/usb/dwc3/core.c
> +++ b/drivers/usb/dwc3/core.c
> @@ -122,21 +122,25 @@ static void __dwc3_set_mode(struct work_struct *work)
>  	unsigned long flags;
>  	int ret;
>  	u32 reg;
> +	u32 desired_dr_role;
>  
>  	mutex_lock(&dwc->mutex);
> +	spin_lock_irqsave(&dwc->lock, flags);
> +	desired_dr_role = dwc->desired_dr_role;
> +	spin_unlock_irqrestore(&dwc->lock, flags);
>  
>  	pm_runtime_get_sync(dwc->dev);
>  
>  	if (dwc->current_dr_role == DWC3_GCTL_PRTCAP_OTG)
>  		dwc3_otg_update(dwc, 0);
>  
> -	if (!dwc->desired_dr_role)
> +	if (!desired_dr_role)
>  		goto out;
>  
> -	if (dwc->desired_dr_role == dwc->current_dr_role)
> +	if (desired_dr_role == dwc->current_dr_role)
>  		goto out;
>  
> -	if (dwc->desired_dr_role == DWC3_GCTL_PRTCAP_OTG && dwc->edev)
> +	if (desired_dr_role == DWC3_GCTL_PRTCAP_OTG && dwc->edev)
>  		goto out;
>  
>  	switch (dwc->current_dr_role) {
> @@ -164,7 +168,7 @@ static void __dwc3_set_mode(struct work_struct *work)
>  	 */
>  	if (dwc->current_dr_role && ((DWC3_IP_IS(DWC3) ||
>  			DWC3_VER_IS_PRIOR(DWC31, 190A)) &&
> -			dwc->desired_dr_role != DWC3_GCTL_PRTCAP_OTG)) {
> +			desired_dr_role != DWC3_GCTL_PRTCAP_OTG)) {
>  		reg = dwc3_readl(dwc->regs, DWC3_GCTL);
>  		reg |= DWC3_GCTL_CORESOFTRESET;
>  		dwc3_writel(dwc->regs, DWC3_GCTL, reg);
> @@ -184,11 +188,11 @@ static void __dwc3_set_mode(struct work_struct *work)
>  
>  	spin_lock_irqsave(&dwc->lock, flags);
>  
> -	dwc3_set_prtcap(dwc, dwc->desired_dr_role);
> +	dwc3_set_prtcap(dwc, desired_dr_role);
>  
>  	spin_unlock_irqrestore(&dwc->lock, flags);
>  
> -	switch (dwc->desired_dr_role) {
> +	switch (desired_dr_role) {
>  	case DWC3_GCTL_PRTCAP_HOST:
>  		ret = dwc3_host_init(dwc);
>  		if (ret) {
> -- 
> 2.25.1
> 

This looks good to me. This should also go on stable.

Acked-by: Thinh Nguyen <Thinh.Nguyen@synopsys.com>

Thanks!
Thinh

      reply	other threads:[~2022-11-28 19:15 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-28 16:15 [PATCH] usb: dwc3: Fix race between dwc3_set_mode and __dwc3_set_mode Sven Peter
2022-11-28 19:14 ` Thinh Nguyen [this message]

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=20221128191411.b57fmzppso2xdnna@synopsys.com \
    --to=thinh.nguyen@synopsys.com \
    --cc=asahi@lists.linux.dev \
    --cc=felipe.balbi@linux.intel.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=j@jannau.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=sven@svenpeter.dev \
    /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).