public inbox for linux-usb@vger.kernel.org
 help / color / mirror / Atom feed
From: Johan Hovold <johan@kernel.org>
To: Udipto Goswami <quic_ugoswami@quicinc.com>
Cc: Thinh Nguyen <Thinh.Nguyen@synopsys.com>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Pratham Pratap <quic_ppratap@quicinc.com>,
	Jack Pham <quic_jackp@quicinc.com>,
	Johan Hovold <johan+linaro@kernel.org>,
	Oliver Neukum <oneukum@suse.com>,
	linux-usb@vger.kernel.org
Subject: Re: [PATCH v8] usb: dwc3: debugfs: Prevent any register access when devices is runtime suspended
Date: Thu, 4 May 2023 10:16:42 +0200	[thread overview]
Message-ID: <ZFNp6mzcvUyX-eon@hovoldconsulting.com> (raw)
In-Reply-To: <20230504045052.22347-1-quic_ugoswami@quicinc.com>

Please use a more succinct Subject. The current one is over 80 chars
which generally too long. Please also check the grammar (e.g. "devices
is") and make sure that the subject reflects what your patch does (and
not what it did in v1).

On Thu, May 04, 2023 at 10:20:52AM +0530, Udipto Goswami wrote:
> When the dwc3 device is runtime suspended, various required clocks would
> get disabled and it is not guaranteed that access to any registers would
> work. Depending on the SoC glue, a register read could be as benign as
> returning 0 or be fatal enough to hang the system.
> 
> In order to prevent such scenarios of fatal errors, make sure to resume
> dwc3 then allow the function to proceed.

As this fixes a crash/hang you should also add a Fixes and CC-stable
tag.

> Signed-off-by: Udipto Goswami <quic_ugoswami@quicinc.com>
> ---
> v8: Replace pm_runtime_get_sync with pm_runtime_resume_and get.
> v7: Replaced pm_runtime_put with pm_runtime_put_sync & returned proper values.
> v6: Added changes to handle get_dync failure appropriately.
> v5: Reworked the patch to resume dwc3 while accessing the registers.
> v4: Introduced pm_runtime_get_if_in_use in order to make sure dwc3 isn't
> 	suspended while accessing the registers.
> v3: Replace pr_err to dev_err. 
> v2: Replaced return 0 with -EINVAL & seq_puts with pr_err.
> 
>  drivers/usb/dwc3/debugfs.c | 99 +++++++++++++++++++++++++++++++++++++-
>  1 file changed, 97 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/usb/dwc3/debugfs.c b/drivers/usb/dwc3/debugfs.c
> index e4a2560b9dc0..a996e3580150 100644
> --- a/drivers/usb/dwc3/debugfs.c
> +++ b/drivers/usb/dwc3/debugfs.c
> @@ -332,6 +332,11 @@ static int dwc3_lsp_show(struct seq_file *s, void *unused)
>  	unsigned int		current_mode;
>  	unsigned long		flags;
>  	u32			reg;
> +	int			ret;
> +
> +	ret = pm_runtime_resume_and_get(dwc->dev);
> +	if (ret < 0)
> +		return ret;
>  
>  	spin_lock_irqsave(&dwc->lock, flags);
>  	reg = dwc3_readl(dwc->regs, DWC3_GSTS);
> @@ -349,6 +354,7 @@ static int dwc3_lsp_show(struct seq_file *s, void *unused)
>  		break;
>  	}
>  	spin_unlock_irqrestore(&dwc->lock, flags);

Add a newline here for symmetry.

> +	pm_runtime_put_sync(dwc->dev);
>  
>  	return 0;
>  }
> @@ -395,6 +401,11 @@ static int dwc3_mode_show(struct seq_file *s, void *unused)
>  	struct dwc3		*dwc = s->private;
>  	unsigned long		flags;
>  	u32			reg;
> +	int			ret;
> +
> +	ret = pm_runtime_resume_and_get(dwc->dev);
> +	if (ret < 0)
> +		return ret;
>  
>  	spin_lock_irqsave(&dwc->lock, flags);
>  	reg = dwc3_readl(dwc->regs, DWC3_GCTL);
> @@ -414,6 +425,7 @@ static int dwc3_mode_show(struct seq_file *s, void *unused)
>  		seq_printf(s, "UNKNOWN %08x\n", DWC3_GCTL_PRTCAP(reg));
>  	}
>  
> +	pm_runtime_put_sync(dwc->dev);

For consistency and readability, add a newline here before the return
statement. Same below.

>  	return 0;
>  }
>  
 
> @@ -584,6 +615,7 @@ static ssize_t dwc3_link_state_write(struct file *file,
>  	char			buf[32];
>  	u32			reg;
>  	u8			speed;
> +	int			ret;
>  
>  	if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
>  		return -EFAULT;
> @@ -603,11 +635,16 @@ static ssize_t dwc3_link_state_write(struct file *file,
>  	else
>  		return -EINVAL;
>  
> +	ret = pm_runtime_resume_and_get(dwc->dev);
> +	if (ret < 0)
> +		return ret;
> +
>  	spin_lock_irqsave(&dwc->lock, flags);
>  	reg = dwc3_readl(dwc->regs, DWC3_GSTS);
>  	if (DWC3_GSTS_CURMOD(reg) != DWC3_GSTS_CURMOD_DEVICE) {
>  		spin_unlock_irqrestore(&dwc->lock, flags);
> -		return -EINVAL;
> +		pm_runtime_put_sync(dwc->dev);
> +		return ret;

Why are you changing the return value here? That's simply wrong.

>  	}
>  
>  	reg = dwc3_readl(dwc->regs, DWC3_DSTS);
> @@ -616,12 +653,14 @@ static ssize_t dwc3_link_state_write(struct file *file,
>  	if (speed < DWC3_DSTS_SUPERSPEED &&
>  	    state != DWC3_LINK_STATE_RECOV) {
>  		spin_unlock_irqrestore(&dwc->lock, flags);
> -		return -EINVAL;
> +		pm_runtime_put_sync(dwc->dev);
> +		return ret;

Same here.

>  	}
>  
>  	dwc3_gadget_set_link_state(dwc, state);
>  	spin_unlock_irqrestore(&dwc->lock, flags);
>  
> +	pm_runtime_put_sync(dwc->dev);
>  	return count;
>  }
>  
 
> @@ -667,6 +712,12 @@ static int dwc3_rx_fifo_size_show(struct seq_file *s, void *unused)
>  	unsigned long		flags;
>  	u32			mdwidth;
>  	u32			val;
> +	int			ret;
> +
> +	ret = pm_runtime_resume_and_get(dwc->dev);
> +	if (ret < 0)
> +		pm_runtime_put_sync(dwc->dev);
> +		return ret;

As the build bot reported, you forgot to remove pm_runtime_put_sync()
here which means that you just broke this function which now always
returns some random stack data.

>  
>  	spin_lock_irqsave(&dwc->lock, flags);
>  	val = dwc3_core_fifo_space(dep, DWC3_RXFIFO);
> @@ -679,6 +730,7 @@ static int dwc3_rx_fifo_size_show(struct seq_file *s, void *unused)
>  	seq_printf(s, "%u\n", val);
>  	spin_unlock_irqrestore(&dwc->lock, flags);
>  
> +	pm_runtime_put_sync(dwc->dev);
>  	return 0;
>  }

Johan

  parent reply	other threads:[~2023-05-04  8:23 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-04  4:50 [PATCH v8] usb: dwc3: debugfs: Prevent any register access when devices is runtime suspended Udipto Goswami
2023-05-04  7:28 ` kernel test robot
2023-05-04  8:16 ` Johan Hovold [this message]
2023-05-09 22:04 ` kernel test robot
2023-05-10  7:32 ` Dan Carpenter
2023-05-10  7:50   ` Johan Hovold

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=ZFNp6mzcvUyX-eon@hovoldconsulting.com \
    --to=johan@kernel.org \
    --cc=Thinh.Nguyen@synopsys.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=johan+linaro@kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=oneukum@suse.com \
    --cc=quic_jackp@quicinc.com \
    --cc=quic_ppratap@quicinc.com \
    --cc=quic_ugoswami@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