mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Oleksij Rempel <o.rempel@pengutronix.de>,
	Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 04/12] watchdog: fix division-by-zero when clock rate == 0
Date: Wed, 30 Sep 2020 09:19:57 +0200	[thread overview]
Message-ID: <20200930072005.1407-4-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20200930072005.1407-1-a.fatoum@pengutronix.de>

Instead of storing the clk into the driver's device-specific private
data, just store the rate and make sure it's != 0 on probe.

This aligns us with what Linux does for the STM32 IWDG and DW WDT.

Reported-by: clang-analyzer-10
Cc: Oleksij Rempel <o.rempel@pengutronix.de>
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 drivers/watchdog/ar9344_wdt.c | 21 ++++++++++++++-------
 drivers/watchdog/dw_wdt.c     | 19 ++++++++++++-------
 drivers/watchdog/stm32_iwdg.c |  2 ++
 3 files changed, 28 insertions(+), 14 deletions(-)

diff --git a/drivers/watchdog/ar9344_wdt.c b/drivers/watchdog/ar9344_wdt.c
index 4615288631d6..c7cd552dc711 100644
--- a/drivers/watchdog/ar9344_wdt.c
+++ b/drivers/watchdog/ar9344_wdt.c
@@ -34,8 +34,8 @@
 struct ar9344_wd {
 	struct watchdog		wd;
 	void __iomem		*base;
-	struct clk		*clk;
 	struct device_d		*dev;
+	unsigned int		rate;
 };
 
 static int ar9344_watchdog_set_timeout(struct watchdog *wd, unsigned timeout)
@@ -45,7 +45,7 @@ static int ar9344_watchdog_set_timeout(struct watchdog *wd, unsigned timeout)
 
 	if (timeout) {
 		ctrl = AR9344_WD_CTRL_ACTION_FCR;
-		val = timeout * clk_get_rate(priv->clk);
+		val = timeout * priv->rate;
 	} else {
 		ctrl = AR9344_WD_CTRL_ACTION_NONE;
 		val = U32_MAX;
@@ -74,6 +74,7 @@ static int ar9344_wdt_probe(struct device_d *dev)
 {
 	struct resource *iores;
 	struct ar9344_wd *priv;
+	struct clk *clk;
 	int ret;
 
 	priv = xzalloc(sizeof(struct ar9344_wd));
@@ -93,16 +94,22 @@ static int ar9344_wdt_probe(struct device_d *dev)
 
 	ar9344_watchdog_detect_reset_source(priv);
 
-	priv->clk = clk_get(dev, NULL);
-	if (IS_ERR(priv->clk)) {
+	clk = clk_get(dev, NULL);
+	if (IS_ERR(clk)) {
 		dev_err(dev, "could not get clk\n");
-		ret = PTR_ERR(priv->clk);
+		ret = PTR_ERR(clk);
 		goto on_error;
 	}
 
-	clk_enable(priv->clk);
+	clk_enable(clk);
 
-	priv->wd.timeout_max = U32_MAX / clk_get_rate(priv->clk);
+	priv->rate = clk_get_rate(clk);
+	if (priv->rate == 0) {
+		ret = -EINVAL;
+		goto on_error;
+	}
+
+	priv->wd.timeout_max = U32_MAX / priv->rate;
 
 	ret = watchdog_register(&priv->wd);
 	if (ret)
diff --git a/drivers/watchdog/dw_wdt.c b/drivers/watchdog/dw_wdt.c
index cb0d17e3610b..17771c712644 100644
--- a/drivers/watchdog/dw_wdt.c
+++ b/drivers/watchdog/dw_wdt.c
@@ -41,10 +41,10 @@
 
 struct dw_wdt {
 	void __iomem		*regs;
-	struct clk		*clk;
 	struct restart_handler	restart;
 	struct watchdog		wdd;
 	struct reset_control	*rst;
+	unsigned int		rate;
 };
 
 #define to_dw_wdt(wdd)	container_of(wdd, struct dw_wdt, wdd)
@@ -55,7 +55,7 @@ static inline int dw_wdt_top_in_seconds(struct dw_wdt *dw_wdt, unsigned top)
 	 * There are 16 possible timeout values in 0..15 where the number of
 	 * cycles is 2 ^ (16 + i) and the watchdog counts down.
 	 */
-	return (1U << (16 + top)) / clk_get_rate(dw_wdt->clk);
+	return (1U << (16 + top)) / dw_wdt->rate;
 }
 
 static int dw_wdt_start(struct watchdog *wdd)
@@ -134,6 +134,7 @@ static int dw_wdt_drv_probe(struct device_d *dev)
 	struct watchdog *wdd;
 	struct dw_wdt *dw_wdt;
 	struct resource *mem;
+	struct clk *clk;
 	int ret;
 
 	dw_wdt = xzalloc(sizeof(*dw_wdt));
@@ -143,11 +144,11 @@ static int dw_wdt_drv_probe(struct device_d *dev)
 	if (IS_ERR(dw_wdt->regs))
 		return PTR_ERR(dw_wdt->regs);
 
-	dw_wdt->clk = clk_get(dev, NULL);
-	if (IS_ERR(dw_wdt->clk))
-		return PTR_ERR(dw_wdt->clk);
+	clk = clk_get(dev, NULL);
+	if (IS_ERR(clk))
+		return PTR_ERR(clk);
 
-	ret = clk_enable(dw_wdt->clk);
+	ret = clk_enable(clk);
 	if (ret)
 		return ret;
 
@@ -160,6 +161,10 @@ static int dw_wdt_drv_probe(struct device_d *dev)
 	wdd->hwdev = dev;
 	wdd->set_timeout = dw_wdt_set_timeout;
 
+	dw_wdt->rate = clk_get_rate(clk);
+	if (dw_wdt->rate == 0)
+		return -EINVAL;
+
 	ret = watchdog_register(wdd);
 	if (ret)
 		goto out_disable_clk;
@@ -179,7 +184,7 @@ static int dw_wdt_drv_probe(struct device_d *dev)
 	return 0;
 
 out_disable_clk:
-	clk_disable(dw_wdt->clk);
+	clk_disable(clk);
 	return ret;
 }
 
diff --git a/drivers/watchdog/stm32_iwdg.c b/drivers/watchdog/stm32_iwdg.c
index 9e38f1a669f2..4d7a263b7e62 100644
--- a/drivers/watchdog/stm32_iwdg.c
+++ b/drivers/watchdog/stm32_iwdg.c
@@ -157,6 +157,8 @@ static int stm32_iwdg_probe(struct device_d *dev)
 		return ret;
 
 	wd->rate = clk_get_rate(clk);
+	if (wd->rate == 0)
+		return -EINVAL;
 
 	if (data->has_pclk) {
 		clk = clk_get(dev, "pclk");
-- 
2.28.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  parent reply	other threads:[~2020-09-30  7:20 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-30  7:19 [PATCH 01/12] hw_random: mxc-rngc: fix read of uninitialized variable Ahmad Fatoum
2020-09-30  7:19 ` [PATCH 02/12] globalvar: fix uninitialized read of variable when no nvvars exist Ahmad Fatoum
2020-09-30  7:19 ` [PATCH 03/12] commands: uimage: fix indeterminate exit code of command Ahmad Fatoum
2020-09-30  7:19 ` Ahmad Fatoum [this message]
2020-09-30  7:19 ` [PATCH 05/12] net: usb: asix: propagate errors from MDIO accessors Ahmad Fatoum
2020-09-30  7:24   ` [PATCH] fixup! " Ahmad Fatoum
2020-10-02  4:04     ` Sascha Hauer
2020-09-30  7:19 ` [PATCH 06/12] digest: sha: remove no-op "erase" of automatic variables Ahmad Fatoum
2020-09-30  7:20 ` [PATCH 07/12] common: memsize: eliminate dead store Ahmad Fatoum
2020-09-30  7:20 ` [PATCH 08/12] USB: musb: remove dead stores Ahmad Fatoum
2020-09-30  7:20 ` [PATCH 09/12] fs: squashfs: remove dead stores for xattr_id Ahmad Fatoum
2020-09-30  7:20 ` [PATCH 10/12] reset: remove dead initialization Ahmad Fatoum
2020-09-30  7:20 ` [PATCH 11/12] sandbox: fix behavior with images >= 4G on 32-bit Ahmad Fatoum
2020-09-30  7:20 ` [PATCH 12/12] blspec: fix dead assignment Ahmad Fatoum
2020-10-02  4:04 ` [PATCH 01/12] hw_random: mxc-rngc: fix read of uninitialized variable Sascha Hauer

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=20200930072005.1407-4-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    --cc=o.rempel@pengutronix.de \
    /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