linux-clk.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3 2/2] clk:mmp: clk-mix.c fix divide-by-zero
@ 2019-03-30 13:55 nixiaoming
  2019-04-23 23:00 ` Stephen Boyd
  0 siblings, 1 reply; 4+ messages in thread
From: nixiaoming @ 2019-03-30 13:55 UTC (permalink / raw)
  To: jbrunet, mturquette, sboyd, soren.brinkmann, nixiaoming, mojha, chao.xie
  Cc: linux-clk, linux-kernel

The _get_div() function has a branch with a return value of 0
Add a check on the return value of _get_div() to avoid divide-by-zero

Signed-off-by: nixiaoming <nixiaoming@huawei.com>
Reviewed-by: Mukesh Ojha <mojha@codeaurora.org>
---
 drivers/clk/mmp/clk-mix.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/drivers/clk/mmp/clk-mix.c b/drivers/clk/mmp/clk-mix.c
index 90814b2..6ed5ad7 100644
--- a/drivers/clk/mmp/clk-mix.c
+++ b/drivers/clk/mmp/clk-mix.c
@@ -245,6 +245,9 @@ static int mmp_clk_mix_determine_rate(struct clk_hw *hw,
 			div_val_max = _get_maxdiv(mix);
 			for (j = 0; j < div_val_max; j++) {
 				div = _get_div(mix, j);
+				/* avoid divide-by-zero */
+				if (!div)
+					continue;
 				mix_rate = parent_rate / div;
 				gap = abs(mix_rate - req->rate);
 				if (!parent_best || gap < gap_best) {
@@ -341,6 +344,9 @@ static unsigned long mmp_clk_mix_recalc_rate(struct clk_hw *hw,
 	shift = mix->reg_info.shift_div;
 
 	div = _get_div(mix, MMP_CLK_BITS_GET_VAL(mux_div, width, shift));
+	/* avoid divide-by-zero */
+	if (!div)
+		return -EINVAL;
 
 	return parent_rate / div;
 }
-- 
1.8.5.6


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

* Re: [PATCH v3 2/2] clk:mmp: clk-mix.c fix divide-by-zero
  2019-03-30 13:55 [PATCH v3 2/2] clk:mmp: clk-mix.c fix divide-by-zero nixiaoming
@ 2019-04-23 23:00 ` Stephen Boyd
  2019-04-24 15:34   ` Nixiaoming
  0 siblings, 1 reply; 4+ messages in thread
From: Stephen Boyd @ 2019-04-23 23:00 UTC (permalink / raw)
  To: chao.xie, jbrunet, mojha, mturquette, nixiaoming, sboyd, soren.brinkmann
  Cc: linux-clk, linux-kernel

Quoting nixiaoming (2019-03-30 06:55:42)
> The _get_div() function has a branch with a return value of 0
> Add a check on the return value of _get_div() to avoid divide-by-zero
> 

Are you seeing this in practice? Or just trying to avoid a div-by-zero
case that you've found from inspection? 

> Signed-off-by: nixiaoming <nixiaoming@huawei.com>
> Reviewed-by: Mukesh Ojha <mojha@codeaurora.org>

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

* RE: [PATCH v3 2/2] clk:mmp: clk-mix.c fix divide-by-zero
  2019-04-23 23:00 ` Stephen Boyd
@ 2019-04-24 15:34   ` Nixiaoming
  2019-04-24 20:55     ` Stephen Boyd
  0 siblings, 1 reply; 4+ messages in thread
From: Nixiaoming @ 2019-04-24 15:34 UTC (permalink / raw)
  To: Stephen Boyd, chao.xie, jbrunet, mojha, mturquette, sboyd,
	soren.brinkmann
  Cc: linux-clk, linux-kernel

On Wed, Apr 24, 2019 at 7:00 AM Stephen Boyd <sboyd@kernel.org> wrote:
>Quoting nixiaoming (2019-03-30 06:55:42)
>> The _get_div() function has a branch with a return value of 0
>> Add a check on the return value of _get_div() to avoid divide-by-zero
>> 
>
>Are you seeing this in practice? Or just trying to avoid a div-by-zero
>case that you've found from inspection? 
>
This potential bug is found by code inspection.
_get_div() is defined as a static function which is only refered twice 
in drivers/clk/mmp/clk-mix.c. In both cases the return value of _get_div()
 is used as divider without any check.
 If _get_div() never returns 0, then the branch returning 0 is dead code,
or the return value should be check to avoid dividing by zero error. 

thanks

>> Signed-off-by: nixiaoming <nixiaoming@huawei.com>
>> Reviewed-by: Mukesh Ojha <mojha@codeaurora.org>
>

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

* RE: [PATCH v3 2/2] clk:mmp: clk-mix.c fix divide-by-zero
  2019-04-24 15:34   ` Nixiaoming
@ 2019-04-24 20:55     ` Stephen Boyd
  0 siblings, 0 replies; 4+ messages in thread
From: Stephen Boyd @ 2019-04-24 20:55 UTC (permalink / raw)
  To: chao.xie, jbrunet, mojha, mturquette, sboyd, soren.brinkmann, Nixiaoming
  Cc: linux-clk, linux-kernel

Quoting Nixiaoming (2019-04-24 08:34:54)
> On Wed, Apr 24, 2019 at 7:00 AM Stephen Boyd <sboyd@kernel.org> wrote:
> >Quoting nixiaoming (2019-03-30 06:55:42)
> >> The _get_div() function has a branch with a return value of 0
> >> Add a check on the return value of _get_div() to avoid divide-by-zero
> >> 
> >
> >Are you seeing this in practice? Or just trying to avoid a div-by-zero
> >case that you've found from inspection? 
> >
> This potential bug is found by code inspection.
> _get_div() is defined as a static function which is only refered twice 
> in drivers/clk/mmp/clk-mix.c. In both cases the return value of _get_div()
>  is used as divider without any check.
>  If _get_div() never returns 0, then the branch returning 0 is dead code,
> or the return value should be check to avoid dividing by zero error. 
> 

Ok. If it's found by code inspection then I'd rather wait until someone
can confirm what the behavior should be when _get_div() returns 0. Is it
an error case that never happens or does the hardware support 0 as a
divider value meaning "bypass"? I'd like to understand that before
patching up a potential div-by-zero case and not knowing what it means.


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

end of thread, other threads:[~2019-04-24 20:55 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-03-30 13:55 [PATCH v3 2/2] clk:mmp: clk-mix.c fix divide-by-zero nixiaoming
2019-04-23 23:00 ` Stephen Boyd
2019-04-24 15:34   ` Nixiaoming
2019-04-24 20:55     ` Stephen Boyd

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