linux-watchdog.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] watchdog: tangox_wdt: test clock rate to avoid division by 0
@ 2016-03-03  8:24 Wolfram Sang
  2016-03-03 11:44 ` Guenter Roeck
  2016-03-06  9:09 ` Wim Van Sebroeck
  0 siblings, 2 replies; 3+ messages in thread
From: Wolfram Sang @ 2016-03-03  8:24 UTC (permalink / raw)
  To: linux-kernel
  Cc: Wolfram Sang, Wim Van Sebroeck, Guenter Roeck, linux-watchdog

From: Wolfram Sang <wsa+renesas@sang-engineering.com>

The clk API may return 0 on clk_get_rate, so we should check the result before
using it as a divisor. For this, refactor the code to use a central
error path.

Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com>
---
 drivers/watchdog/tangox_wdt.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/drivers/watchdog/tangox_wdt.c b/drivers/watchdog/tangox_wdt.c
index 709c1ed6fd79b9..cfbed7e051b619 100644
--- a/drivers/watchdog/tangox_wdt.c
+++ b/drivers/watchdog/tangox_wdt.c
@@ -139,6 +139,10 @@ static int tangox_wdt_probe(struct platform_device *pdev)
 		return err;
 
 	dev->clk_rate = clk_get_rate(dev->clk);
+	if (!dev->clk_rate) {
+		err = -EINVAL;
+		goto err;
+	}
 
 	dev->wdt.parent = &pdev->dev;
 	dev->wdt.info = &tangox_wdt_info;
@@ -171,10 +175,8 @@ static int tangox_wdt_probe(struct platform_device *pdev)
 	}
 
 	err = watchdog_register_device(&dev->wdt);
-	if (err) {
-		clk_disable_unprepare(dev->clk);
-		return err;
-	}
+	if (err)
+		goto err;
 
 	platform_set_drvdata(pdev, dev);
 
@@ -187,6 +189,10 @@ static int tangox_wdt_probe(struct platform_device *pdev)
 	dev_info(&pdev->dev, "SMP86xx/SMP87xx watchdog registered\n");
 
 	return 0;
+
+ err:
+	clk_disable_unprepare(dev->clk);
+	return err;
 }
 
 static int tangox_wdt_remove(struct platform_device *pdev)
-- 
2.7.0

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

* Re: [PATCH v2] watchdog: tangox_wdt: test clock rate to avoid division by 0
  2016-03-03  8:24 [PATCH v2] watchdog: tangox_wdt: test clock rate to avoid division by 0 Wolfram Sang
@ 2016-03-03 11:44 ` Guenter Roeck
  2016-03-06  9:09 ` Wim Van Sebroeck
  1 sibling, 0 replies; 3+ messages in thread
From: Guenter Roeck @ 2016-03-03 11:44 UTC (permalink / raw)
  To: Wolfram Sang, linux-kernel; +Cc: Wim Van Sebroeck, linux-watchdog

On 03/03/2016 12:24 AM, Wolfram Sang wrote:
> From: Wolfram Sang <wsa+renesas@sang-engineering.com>
>
> The clk API may return 0 on clk_get_rate, so we should check the result before
> using it as a divisor. For this, refactor the code to use a central
> error path.
>
> Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com>

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

> ---
>   drivers/watchdog/tangox_wdt.c | 14 ++++++++++----
>   1 file changed, 10 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/watchdog/tangox_wdt.c b/drivers/watchdog/tangox_wdt.c
> index 709c1ed6fd79b9..cfbed7e051b619 100644
> --- a/drivers/watchdog/tangox_wdt.c
> +++ b/drivers/watchdog/tangox_wdt.c
> @@ -139,6 +139,10 @@ static int tangox_wdt_probe(struct platform_device *pdev)
>   		return err;
>
>   	dev->clk_rate = clk_get_rate(dev->clk);
> +	if (!dev->clk_rate) {
> +		err = -EINVAL;
> +		goto err;
> +	}
>
>   	dev->wdt.parent = &pdev->dev;
>   	dev->wdt.info = &tangox_wdt_info;
> @@ -171,10 +175,8 @@ static int tangox_wdt_probe(struct platform_device *pdev)
>   	}
>
>   	err = watchdog_register_device(&dev->wdt);
> -	if (err) {
> -		clk_disable_unprepare(dev->clk);
> -		return err;
> -	}
> +	if (err)
> +		goto err;
>
>   	platform_set_drvdata(pdev, dev);
>
> @@ -187,6 +189,10 @@ static int tangox_wdt_probe(struct platform_device *pdev)
>   	dev_info(&pdev->dev, "SMP86xx/SMP87xx watchdog registered\n");
>
>   	return 0;
> +
> + err:
> +	clk_disable_unprepare(dev->clk);
> +	return err;
>   }
>
>   static int tangox_wdt_remove(struct platform_device *pdev)
>


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

* Re: [PATCH v2] watchdog: tangox_wdt: test clock rate to avoid division by 0
  2016-03-03  8:24 [PATCH v2] watchdog: tangox_wdt: test clock rate to avoid division by 0 Wolfram Sang
  2016-03-03 11:44 ` Guenter Roeck
@ 2016-03-06  9:09 ` Wim Van Sebroeck
  1 sibling, 0 replies; 3+ messages in thread
From: Wim Van Sebroeck @ 2016-03-06  9:09 UTC (permalink / raw)
  To: Wolfram Sang; +Cc: linux-kernel, Guenter Roeck, linux-watchdog

Hi Wolfram,

> From: Wolfram Sang <wsa+renesas@sang-engineering.com>
> 
> The clk API may return 0 on clk_get_rate, so we should check the result before
> using it as a divisor. For this, refactor the code to use a central
> error path.
> 
> Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com>
> ---
>  drivers/watchdog/tangox_wdt.c | 14 ++++++++++----
>  1 file changed, 10 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/watchdog/tangox_wdt.c b/drivers/watchdog/tangox_wdt.c
> index 709c1ed6fd79b9..cfbed7e051b619 100644
> --- a/drivers/watchdog/tangox_wdt.c
> +++ b/drivers/watchdog/tangox_wdt.c
> @@ -139,6 +139,10 @@ static int tangox_wdt_probe(struct platform_device *pdev)
>  		return err;
>  
>  	dev->clk_rate = clk_get_rate(dev->clk);
> +	if (!dev->clk_rate) {
> +		err = -EINVAL;
> +		goto err;
> +	}
>  
>  	dev->wdt.parent = &pdev->dev;
>  	dev->wdt.info = &tangox_wdt_info;
> @@ -171,10 +175,8 @@ static int tangox_wdt_probe(struct platform_device *pdev)
>  	}
>  
>  	err = watchdog_register_device(&dev->wdt);
> -	if (err) {
> -		clk_disable_unprepare(dev->clk);
> -		return err;
> -	}
> +	if (err)
> +		goto err;
>  
>  	platform_set_drvdata(pdev, dev);
>  
> @@ -187,6 +189,10 @@ static int tangox_wdt_probe(struct platform_device *pdev)
>  	dev_info(&pdev->dev, "SMP86xx/SMP87xx watchdog registered\n");
>  
>  	return 0;
> +
> + err:
> +	clk_disable_unprepare(dev->clk);
> +	return err;
>  }
>  
>  static int tangox_wdt_remove(struct platform_device *pdev)
> -- 
> 2.7.0
> 

Patch has been added to linux-watchdog-next.

Kind regards,
Wim.


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

end of thread, other threads:[~2016-03-06  9:09 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-03-03  8:24 [PATCH v2] watchdog: tangox_wdt: test clock rate to avoid division by 0 Wolfram Sang
2016-03-03 11:44 ` Guenter Roeck
2016-03-06  9:09 ` Wim Van Sebroeck

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).