Linux-Watchdog Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH] watchdog: renesas_wdt: don't sleep in atomic context
@ 2020-12-19 17:34 Wolfram Sang
  2020-12-21 16:27 ` Guenter Roeck
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Wolfram Sang @ 2020-12-19 17:34 UTC (permalink / raw)
  To: linux-watchdog; +Cc: linux-renesas-soc, Wolfram Sang

In the restart handler, we hit multiple OOPSes. One because of
usleep_range() and one because of RPM. So, instead of re-using
rwdt_start(), we implement an atomic version of it in the restart
handler. As a little bonus, reboot will occur sooner because we can now
use the smallest divider in the custom restart routine.

Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com>
---

It is problematic to add a Fixes: tag because we would need three of
them. So, I'd think we add a stable tag and as long as the patch
applies, all is fine. Other opinions?

 drivers/watchdog/renesas_wdt.c | 30 ++++++++++++++++++++++++------
 1 file changed, 24 insertions(+), 6 deletions(-)

diff --git a/drivers/watchdog/renesas_wdt.c b/drivers/watchdog/renesas_wdt.c
index 47fce4de0110..d2b5074bca65 100644
--- a/drivers/watchdog/renesas_wdt.c
+++ b/drivers/watchdog/renesas_wdt.c
@@ -9,6 +9,7 @@
 #include <linux/clk.h>
 #include <linux/delay.h>
 #include <linux/io.h>
+#include <linux/iopoll.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/of.h>
@@ -50,6 +51,7 @@ struct rwdt_priv {
 	struct watchdog_device wdev;
 	unsigned long clk_rate;
 	u8 cks;
+	struct clk *clk;
 };
 
 static void rwdt_write(struct rwdt_priv *priv, u32 val, unsigned int reg)
@@ -125,13 +127,30 @@ static unsigned int rwdt_get_timeleft(struct watchdog_device *wdev)
 	return DIV_BY_CLKS_PER_SEC(priv, 65536 - val);
 }
 
+/* needs to be atomic - no RPM, no usleep_range, no scheduling! */
 static int rwdt_restart(struct watchdog_device *wdev, unsigned long action,
 			void *data)
 {
 	struct rwdt_priv *priv = watchdog_get_drvdata(wdev);
+	u8 val;
+
+	clk_prepare_enable(priv->clk);
+
+	/* Stop the timer before we modify any register */
+	val = readb_relaxed(priv->base + RWTCSRA) & ~RWTCSRA_TME;
+	rwdt_write(priv, val, RWTCSRA);
+	/* Delay 2 cycles before setting watchdog counter */
+	udelay(DIV_ROUND_UP(2 * 1000000, priv->clk_rate));
 
-	rwdt_start(wdev);
 	rwdt_write(priv, 0xffff, RWTCNT);
+	/* smallest divider to reboot soon */
+	rwdt_write(priv, 0, RWTCSRA);
+
+	readb_poll_timeout_atomic(priv->base + RWTCSRA, val,
+				  !(val & RWTCSRA_WRFLG), 1, 100);
+
+	rwdt_write(priv, RWTCSRA_TME, RWTCSRA);
+
 	return 0;
 }
 
@@ -191,7 +210,6 @@ static int rwdt_probe(struct platform_device *pdev)
 {
 	struct device *dev = &pdev->dev;
 	struct rwdt_priv *priv;
-	struct clk *clk;
 	unsigned long clks_per_sec;
 	int ret, i;
 	u8 csra;
@@ -207,13 +225,13 @@ static int rwdt_probe(struct platform_device *pdev)
 	if (IS_ERR(priv->base))
 		return PTR_ERR(priv->base);
 
-	clk = devm_clk_get(dev, NULL);
-	if (IS_ERR(clk))
-		return PTR_ERR(clk);
+	priv->clk = devm_clk_get(dev, NULL);
+	if (IS_ERR(priv->clk))
+		return PTR_ERR(priv->clk);
 
 	pm_runtime_enable(dev);
 	pm_runtime_get_sync(dev);
-	priv->clk_rate = clk_get_rate(clk);
+	priv->clk_rate = clk_get_rate(priv->clk);
 	csra = readb_relaxed(priv->base + RWTCSRA);
 	priv->wdev.bootstatus = csra & RWTCSRA_WOVF ? WDIOF_CARDRESET : 0;
 	pm_runtime_put(dev);
-- 
2.29.2


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] watchdog: renesas_wdt: don't sleep in atomic context
  2020-12-19 17:34 [PATCH] watchdog: renesas_wdt: don't sleep in atomic context Wolfram Sang
@ 2020-12-21 16:27 ` Guenter Roeck
  2020-12-22  8:26 ` Geert Uytterhoeven
  2020-12-27 14:11 ` Niklas Söderlund
  2 siblings, 0 replies; 4+ messages in thread
From: Guenter Roeck @ 2020-12-21 16:27 UTC (permalink / raw)
  To: Wolfram Sang, linux-watchdog; +Cc: linux-renesas-soc

On 12/19/20 9:34 AM, Wolfram Sang wrote:
> In the restart handler, we hit multiple OOPSes. One because of
> usleep_range() and one because of RPM. So, instead of re-using
> rwdt_start(), we implement an atomic version of it in the restart
> handler. As a little bonus, reboot will occur sooner because we can now
> use the smallest divider in the custom restart routine.
> 
> Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com>

Reviewed-by: Guenter Roeck <linux@roeck-us.net>

> ---
> 
> It is problematic to add a Fixes: tag because we would need three of
> them. So, I'd think we add a stable tag and as long as the patch
> applies, all is fine. Other opinions?
> 
>  drivers/watchdog/renesas_wdt.c | 30 ++++++++++++++++++++++++------
>  1 file changed, 24 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/watchdog/renesas_wdt.c b/drivers/watchdog/renesas_wdt.c
> index 47fce4de0110..d2b5074bca65 100644
> --- a/drivers/watchdog/renesas_wdt.c
> +++ b/drivers/watchdog/renesas_wdt.c
> @@ -9,6 +9,7 @@
>  #include <linux/clk.h>
>  #include <linux/delay.h>
>  #include <linux/io.h>
> +#include <linux/iopoll.h>
>  #include <linux/kernel.h>
>  #include <linux/module.h>
>  #include <linux/of.h>
> @@ -50,6 +51,7 @@ struct rwdt_priv {
>  	struct watchdog_device wdev;
>  	unsigned long clk_rate;
>  	u8 cks;
> +	struct clk *clk;
>  };
>  
>  static void rwdt_write(struct rwdt_priv *priv, u32 val, unsigned int reg)
> @@ -125,13 +127,30 @@ static unsigned int rwdt_get_timeleft(struct watchdog_device *wdev)
>  	return DIV_BY_CLKS_PER_SEC(priv, 65536 - val);
>  }
>  
> +/* needs to be atomic - no RPM, no usleep_range, no scheduling! */
>  static int rwdt_restart(struct watchdog_device *wdev, unsigned long action,
>  			void *data)
>  {
>  	struct rwdt_priv *priv = watchdog_get_drvdata(wdev);
> +	u8 val;
> +
> +	clk_prepare_enable(priv->clk);
> +
> +	/* Stop the timer before we modify any register */
> +	val = readb_relaxed(priv->base + RWTCSRA) & ~RWTCSRA_TME;
> +	rwdt_write(priv, val, RWTCSRA);
> +	/* Delay 2 cycles before setting watchdog counter */
> +	udelay(DIV_ROUND_UP(2 * 1000000, priv->clk_rate));
>  
> -	rwdt_start(wdev);
>  	rwdt_write(priv, 0xffff, RWTCNT);
> +	/* smallest divider to reboot soon */
> +	rwdt_write(priv, 0, RWTCSRA);
> +
> +	readb_poll_timeout_atomic(priv->base + RWTCSRA, val,
> +				  !(val & RWTCSRA_WRFLG), 1, 100);
> +
> +	rwdt_write(priv, RWTCSRA_TME, RWTCSRA);
> +
>  	return 0;
>  }
>  
> @@ -191,7 +210,6 @@ static int rwdt_probe(struct platform_device *pdev)
>  {
>  	struct device *dev = &pdev->dev;
>  	struct rwdt_priv *priv;
> -	struct clk *clk;
>  	unsigned long clks_per_sec;
>  	int ret, i;
>  	u8 csra;
> @@ -207,13 +225,13 @@ static int rwdt_probe(struct platform_device *pdev)
>  	if (IS_ERR(priv->base))
>  		return PTR_ERR(priv->base);
>  
> -	clk = devm_clk_get(dev, NULL);
> -	if (IS_ERR(clk))
> -		return PTR_ERR(clk);
> +	priv->clk = devm_clk_get(dev, NULL);
> +	if (IS_ERR(priv->clk))
> +		return PTR_ERR(priv->clk);
>  
>  	pm_runtime_enable(dev);
>  	pm_runtime_get_sync(dev);
> -	priv->clk_rate = clk_get_rate(clk);
> +	priv->clk_rate = clk_get_rate(priv->clk);
>  	csra = readb_relaxed(priv->base + RWTCSRA);
>  	priv->wdev.bootstatus = csra & RWTCSRA_WOVF ? WDIOF_CARDRESET : 0;
>  	pm_runtime_put(dev);
> 


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] watchdog: renesas_wdt: don't sleep in atomic context
  2020-12-19 17:34 [PATCH] watchdog: renesas_wdt: don't sleep in atomic context Wolfram Sang
  2020-12-21 16:27 ` Guenter Roeck
@ 2020-12-22  8:26 ` Geert Uytterhoeven
  2020-12-27 14:11 ` Niklas Söderlund
  2 siblings, 0 replies; 4+ messages in thread
From: Geert Uytterhoeven @ 2020-12-22  8:26 UTC (permalink / raw)
  To: Wolfram Sang; +Cc: Linux Watchdog Mailing List, Linux-Renesas

On Sat, Dec 19, 2020 at 6:36 PM Wolfram Sang
<wsa+renesas@sang-engineering.com> wrote:
> In the restart handler, we hit multiple OOPSes. One because of
> usleep_range() and one because of RPM. So, instead of re-using
> rwdt_start(), we implement an atomic version of it in the restart
> handler. As a little bonus, reboot will occur sooner because we can now
> use the smallest divider in the custom restart routine.
>
> Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com>

Reviewed-by: Geert Uytterhoeven <geert+renesas@glider.be>

Gr{oetje,eeting}s,

                        Geert

-- 
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] watchdog: renesas_wdt: don't sleep in atomic context
  2020-12-19 17:34 [PATCH] watchdog: renesas_wdt: don't sleep in atomic context Wolfram Sang
  2020-12-21 16:27 ` Guenter Roeck
  2020-12-22  8:26 ` Geert Uytterhoeven
@ 2020-12-27 14:11 ` Niklas Söderlund
  2 siblings, 0 replies; 4+ messages in thread
From: Niklas Söderlund @ 2020-12-27 14:11 UTC (permalink / raw)
  To: Wolfram Sang; +Cc: linux-watchdog, linux-renesas-soc

Hi Wolfram,

Thanks for your work.

On 2020-12-19 18:34:15 +0100, Wolfram Sang wrote:
> In the restart handler, we hit multiple OOPSes. One because of
> usleep_range() and one because of RPM. So, instead of re-using
> rwdt_start(), we implement an atomic version of it in the restart
> handler. As a little bonus, reboot will occur sooner because we can now
> use the smallest divider in the custom restart routine.
> 
> Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com>

Reviewed-by: Niklas Söderlund <niklas.soderlund+renesas@ragnatech.se>

> ---
> 
> It is problematic to add a Fixes: tag because we would need three of
> them. So, I'd think we add a stable tag and as long as the patch
> applies, all is fine. Other opinions?
> 
>  drivers/watchdog/renesas_wdt.c | 30 ++++++++++++++++++++++++------
>  1 file changed, 24 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/watchdog/renesas_wdt.c b/drivers/watchdog/renesas_wdt.c
> index 47fce4de0110..d2b5074bca65 100644
> --- a/drivers/watchdog/renesas_wdt.c
> +++ b/drivers/watchdog/renesas_wdt.c
> @@ -9,6 +9,7 @@
>  #include <linux/clk.h>
>  #include <linux/delay.h>
>  #include <linux/io.h>
> +#include <linux/iopoll.h>
>  #include <linux/kernel.h>
>  #include <linux/module.h>
>  #include <linux/of.h>
> @@ -50,6 +51,7 @@ struct rwdt_priv {
>  	struct watchdog_device wdev;
>  	unsigned long clk_rate;
>  	u8 cks;
> +	struct clk *clk;
>  };
>  
>  static void rwdt_write(struct rwdt_priv *priv, u32 val, unsigned int reg)
> @@ -125,13 +127,30 @@ static unsigned int rwdt_get_timeleft(struct watchdog_device *wdev)
>  	return DIV_BY_CLKS_PER_SEC(priv, 65536 - val);
>  }
>  
> +/* needs to be atomic - no RPM, no usleep_range, no scheduling! */
>  static int rwdt_restart(struct watchdog_device *wdev, unsigned long action,
>  			void *data)
>  {
>  	struct rwdt_priv *priv = watchdog_get_drvdata(wdev);
> +	u8 val;
> +
> +	clk_prepare_enable(priv->clk);
> +
> +	/* Stop the timer before we modify any register */
> +	val = readb_relaxed(priv->base + RWTCSRA) & ~RWTCSRA_TME;
> +	rwdt_write(priv, val, RWTCSRA);
> +	/* Delay 2 cycles before setting watchdog counter */
> +	udelay(DIV_ROUND_UP(2 * 1000000, priv->clk_rate));
>  
> -	rwdt_start(wdev);
>  	rwdt_write(priv, 0xffff, RWTCNT);
> +	/* smallest divider to reboot soon */
> +	rwdt_write(priv, 0, RWTCSRA);
> +
> +	readb_poll_timeout_atomic(priv->base + RWTCSRA, val,
> +				  !(val & RWTCSRA_WRFLG), 1, 100);
> +
> +	rwdt_write(priv, RWTCSRA_TME, RWTCSRA);
> +
>  	return 0;
>  }
>  
> @@ -191,7 +210,6 @@ static int rwdt_probe(struct platform_device *pdev)
>  {
>  	struct device *dev = &pdev->dev;
>  	struct rwdt_priv *priv;
> -	struct clk *clk;
>  	unsigned long clks_per_sec;
>  	int ret, i;
>  	u8 csra;
> @@ -207,13 +225,13 @@ static int rwdt_probe(struct platform_device *pdev)
>  	if (IS_ERR(priv->base))
>  		return PTR_ERR(priv->base);
>  
> -	clk = devm_clk_get(dev, NULL);
> -	if (IS_ERR(clk))
> -		return PTR_ERR(clk);
> +	priv->clk = devm_clk_get(dev, NULL);
> +	if (IS_ERR(priv->clk))
> +		return PTR_ERR(priv->clk);
>  
>  	pm_runtime_enable(dev);
>  	pm_runtime_get_sync(dev);
> -	priv->clk_rate = clk_get_rate(clk);
> +	priv->clk_rate = clk_get_rate(priv->clk);
>  	csra = readb_relaxed(priv->base + RWTCSRA);
>  	priv->wdev.bootstatus = csra & RWTCSRA_WOVF ? WDIOF_CARDRESET : 0;
>  	pm_runtime_put(dev);
> -- 
> 2.29.2
> 

-- 
Regards,
Niklas Söderlund

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, back to index

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-19 17:34 [PATCH] watchdog: renesas_wdt: don't sleep in atomic context Wolfram Sang
2020-12-21 16:27 ` Guenter Roeck
2020-12-22  8:26 ` Geert Uytterhoeven
2020-12-27 14:11 ` Niklas Söderlund

Linux-Watchdog Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-watchdog/0 linux-watchdog/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-watchdog linux-watchdog/ https://lore.kernel.org/linux-watchdog \
		linux-watchdog@vger.kernel.org
	public-inbox-index linux-watchdog

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-watchdog


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git