linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] clk:mmp: clk-mix.c fix divide-by-zero
@ 2019-03-29 11:46 nixiaoming
  2019-03-29 12:41 ` Mukesh Ojha
  2019-03-29 22:48 ` Stephen Boyd
  0 siblings, 2 replies; 4+ messages in thread
From: nixiaoming @ 2019-03-29 11:46 UTC (permalink / raw)
  To: mturquette, sboyd, nixiaoming, 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>
---
 drivers/clk/mmp/clk-mix.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/drivers/clk/mmp/clk-mix.c b/drivers/clk/mmp/clk-mix.c
index 90814b2..9d152c2 100644
--- a/drivers/clk/mmp/clk-mix.c
+++ b/drivers/clk/mmp/clk-mix.c
@@ -245,6 +245,8 @@ 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);
+				if (!div) /* avoid divide-by-zero */
+					continue;
 				mix_rate = parent_rate / div;
 				gap = abs(mix_rate - req->rate);
 				if (!parent_best || gap < gap_best) {
@@ -341,6 +343,8 @@ 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));
+	if (!div) /* avoid divide-by-zero */
+		return -EINVAL;
 
 	return parent_rate / div;
 }
-- 
1.8.5.6


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

* Re: [PATCH] clk:mmp: clk-mix.c fix divide-by-zero
  2019-03-29 11:46 [PATCH] clk:mmp: clk-mix.c fix divide-by-zero nixiaoming
@ 2019-03-29 12:41 ` Mukesh Ojha
  2019-03-29 22:48 ` Stephen Boyd
  1 sibling, 0 replies; 4+ messages in thread
From: Mukesh Ojha @ 2019-03-29 12:41 UTC (permalink / raw)
  To: nixiaoming, mturquette, sboyd, chao.xie; +Cc: linux-clk, linux-kernel


On 3/29/2019 5:16 PM, nixiaoming wrote:
> 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>

Fix the below minor comments..
Otherwise things look good..you can take mine
Reviewed-by: Mukesh Ojha <mojha@codeaurora.org>

after fixing it.


Cheers,
-Mukesh


> ---
>   drivers/clk/mmp/clk-mix.c | 4 ++++
>   1 file changed, 4 insertions(+)
>
> diff --git a/drivers/clk/mmp/clk-mix.c b/drivers/clk/mmp/clk-mix.c
> index 90814b2..9d152c2 100644
> --- a/drivers/clk/mmp/clk-mix.c
> +++ b/drivers/clk/mmp/clk-mix.c
> @@ -245,6 +245,8 @@ 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);
> +				if (!div) /* avoid divide-by-zero */

Please move this comment at the top of the check.


> +					continue;
>   				mix_rate = parent_rate / div;
>   				gap = abs(mix_rate - req->rate);
>   				if (!parent_best || gap < gap_best) {
> @@ -341,6 +343,8 @@ 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));
> +	if (!div) /* avoid divide-by-zero */
ditto
> +		return -EINVAL;
>   
>   	return parent_rate / div;
>   }

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

* Re: [PATCH] clk:mmp: clk-mix.c fix divide-by-zero
  2019-03-29 11:46 [PATCH] clk:mmp: clk-mix.c fix divide-by-zero nixiaoming
  2019-03-29 12:41 ` Mukesh Ojha
@ 2019-03-29 22:48 ` Stephen Boyd
  2019-03-30  2:20   ` Nixiaoming
  1 sibling, 1 reply; 4+ messages in thread
From: Stephen Boyd @ 2019-03-29 22:48 UTC (permalink / raw)
  To: chao.xie, mturquette, nixiaoming; +Cc: linux-clk, linux-kernel

Quoting nixiaoming (2019-03-29 04:46:00)
> 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>

Similar questions apply here as they do on the generic divider patch you
sent.

> ---
>  drivers/clk/mmp/clk-mix.c | 4 ++++
>  1 file changed, 4 insertions(+)
> 
> diff --git a/drivers/clk/mmp/clk-mix.c b/drivers/clk/mmp/clk-mix.c
> index 90814b2..9d152c2 100644
> --- a/drivers/clk/mmp/clk-mix.c
> +++ b/drivers/clk/mmp/clk-mix.c
> @@ -245,6 +245,8 @@ 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);
> +                               if (!div) /* avoid divide-by-zero */

Why can't we return 1 for the divider value here?

> +                                       continue;
>                                 mix_rate = parent_rate / div;
>                                 gap = abs(mix_rate - req->rate);
>                                 if (!parent_best || gap < gap_best) {
> @@ -341,6 +343,8 @@ 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));
> +       if (!div) /* avoid divide-by-zero */

Same question.

> +               return -EINVAL;
>  
>         return parent_rate / div;
>  }

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

* RE: [PATCH] clk:mmp: clk-mix.c fix divide-by-zero
  2019-03-29 22:48 ` Stephen Boyd
@ 2019-03-30  2:20   ` Nixiaoming
  0 siblings, 0 replies; 4+ messages in thread
From: Nixiaoming @ 2019-03-30  2:20 UTC (permalink / raw)
  To: Stephen Boyd, chao.xie, mturquette; +Cc: linux-clk, linux-kernel

On 3/30/2019 6:48 AM Stephen Boyd wrote:
>Quoting nixiaoming (2019-03-29 04:46:00)
>> 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>
>
>Similar questions apply here as they do on the generic divider patch you
>sent.
>
_get_div() in both files is a different function, with a divide-by-zero problem
I will organize it into a patch set later.

>> ---
>>  drivers/clk/mmp/clk-mix.c | 4 ++++
>>  1 file changed, 4 insertions(+)
>> 
>> diff --git a/drivers/clk/mmp/clk-mix.c b/drivers/clk/mmp/clk-mix.c
>> index 90814b2..9d152c2 100644
>> --- a/drivers/clk/mmp/clk-mix.c
>> +++ b/drivers/clk/mmp/clk-mix.c
>> @@ -245,6 +245,8 @@ 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);
>> +                               if (!div) /* avoid divide-by-zero */
>
>Why can't we return 1 for the divider value here?

I personally understand that an exception or skip should be thrown after dividing by 0.
Directly modified to other values, I am not sure whether it affects the logic
My logical understanding of this code is not clear enough, I still need your guidance.
>
>> +                                       continue;
>>                                 mix_rate = parent_rate / div;
>>                                 gap = abs(mix_rate - req->rate);
>>                                 if (!parent_best || gap < gap_best) {
>> @@ -341,6 +343,8 @@ 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));
>> +       if (!div) /* avoid divide-by-zero */
>
>Same question.
I personally understand that an exception or skip should be thrown after dividing by 0.
Directly modified to other values, I am not sure whether it affects the logic
My logical understanding of this code is not clear enough, I still need your guidance.
>
>> +               return -EINVAL;
>>  
>>         return parent_rate / div;
>>  }
>

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

end of thread, other threads:[~2019-03-30  2:20 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-03-29 11:46 [PATCH] clk:mmp: clk-mix.c fix divide-by-zero nixiaoming
2019-03-29 12:41 ` Mukesh Ojha
2019-03-29 22:48 ` Stephen Boyd
2019-03-30  2:20   ` Nixiaoming

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