clk: qcom: clk-rcg2: Fix wrong RCG clock rate for high parent frequencies
Subsystems (3)
PATCH 1/1 clk: qcom: clk-rcg2: Fix wrong RCG clock rate for high parent frequencies
If the parent clock rate is greater than unsigned long max/2 then integer overflow happens when calculating the clock rate on 32-bit systems. As RCG2 uses half integer dividers, the clock rate is first being multiplied by 2 which will overflow the unsigned long max value. So, use unsigned long long for rate computations to avoid overflow.
--- drivers/clk/qcom/clk-rcg2.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/clk/qcom/clk-rcg2.c +6 -6
@@ -156,18 +156,18 @@ static int clk_rcg2_set_parent(struct clk_hw *hw, u8 index)* hid_div n */ static unsigned long -calc_rate (unsigned long rate,u32 m,u32 n,u32 mode,u32 hid_div) +calc_rate (unsigned long parent_rate,u32 m,u32 n,u32 mode,u32 hid_div) { +u64 rate = parent_rate; +if (hid_div) { rate *=2 ; - rate /= hid_div +1 ; +do_div (rate, hid_div +1 ); }if (mode) { -u64 tmp = rate; - tmp *= m; -do_div (tmp, n); - rate = tmp; + rate *= m; +do_div (rate, n); }return rate;
Signature · 1 line
2.17.1
Quoted 16 lines · Devi Priya
If the parent clock rate is greater than unsigned long max/2 then integer overflow happens when calculating the clock rate on 32-bit systems. As RCG2 uses half integer dividers, the clock rate is first being multiplied by 2 which will overflow the unsigned long max value. So, use unsigned long long for rate computations to avoid overflow.
Signed-off-by Devi Priya <[email protected]>--- drivers/clk/qcom/clk-rcg2.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)drivers/clk/qcom/clk-rcg2.c +0
-0
drivers/clk/qcom/clk-rcg2.c static int clk_rcg2_set_parent(struct clk_hw *hw, u8 index) · line 161
Quoted 7 lines
drivers/clk/qcom/clk-rcg2.c +2
-1@@ -156,18 +156,18 @@ static int clk_rcg2_set_parent(struct clk_hw *hw, u8 index)* hid_div n */ static unsigned long -calc_rate (unsigned long rate,u32 m,u32 n,u32 mode,u32 hid_div) +calc_rate (unsigned long parent_rate,u32 m,u32 n,u32 mode,u32 hid_div) { +u64 rate = parent_rate;
This should not be necessary.. You're being passed a copy of the original value, which you can operate on.
Otherwise, LGTM
Konrad
Konrad Dybcio
Reviewed-byQuoted 27 lines · Konrad Dybcio
Quoted 16 lines · Devi Priya
If the parent clock rate is greater than unsigned long max/2 then integer overflow happens when calculating the clock rate on 32-bit systems. As RCG2 uses half integer dividers, the clock rate is first being multiplied by 2 which will overflow the unsigned long max value. So, use unsigned long long for rate computations to avoid overflow.
Signed-off-by Devi Priya <[email protected]>--- drivers/clk/qcom/clk-rcg2.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)drivers/clk/qcom/clk-rcg2.c +0
-0drivers/clk/qcom/clk-rcg2.c static int clk_rcg2_set_parent(struct clk_hw *hw, u8 index) · line 161
Quoted 7 lines
drivers/clk/qcom/clk-rcg2.c +2
-1@@ -156,18 +156,18 @@ static int clk_rcg2_set_parent(struct clk_hw *hw, u8 index)* hid_div n */ static unsigned long -calc_rate (unsigned long rate,u32 m,u32 n,u32 mode,u32 hid_div) +calc_rate (unsigned long parent_rate,u32 m,u32 n,u32 mode,u32 hid_div) { +u64 rate = parent_rate;This should not be necessary.. You're being passed a copy of the original value, which you can operate on.
Quoted 1 line
Otherwise, LGTM
Well obviously no, as I hit enter I realized this is of a different type..
Konrad
Stephen Boyd
maintainerQuoting Devi Priya (2023-07-20 01:33:04)
Quoted 8 lines
If the parent clock rate is greater than unsigned long max/2 then integer overflow happens when calculating the clock rate on 32-bit systems. As RCG2 uses half integer dividers, the clock rate is first being multiplied by 2 which will overflow the unsigned long max value. So, use unsigned long long for rate computations to avoid overflow.
Signed-off-by Devi Priya <[email protected]>---
Any Fixes tag?
Can you retitle this to state "overflow" rather than just "wrong"? That's more descriptive.
E.g "Fix clockrate overflow for high parent frequencies"
Quoted 18 lines · Devi Priya
If the parent clock rate is greater than unsigned long max/2 then integer overflow happens when calculating the clock rate on 32-bit systems. As RCG2 uses half integer dividers, the clock rate is first being multiplied by 2 which will overflow the unsigned long max value. So, use unsigned long long for rate computations to avoid overflow.
Signed-off-by Devi Priya <[email protected]>--- drivers/clk/qcom/clk-rcg2.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)drivers/clk/qcom/clk-rcg2.c +0
-0@@ -156,18 +156,18 @@ static int clk_rcg2_set_parent(struct clk_hw *hw, u8 index)* hid_div n */
drivers/clk/qcom/clk-rcg2.c static int clk_rcg2_set_parent(struct clk_hw *hw, u8 index) · line 165
Quoted 10 lines
drivers/clk/qcom/clk-rcg2.c +4
-2static unsigned long -calc_rate (unsigned long rate,u32 m,u32 n,u32 mode,u32 hid_div) +calc_rate (unsigned long parent_rate,u32 m,u32 n,u32 mode,u32 hid_div) { +u64 rate = parent_rate; +if (hid_div) { rate *=2 ; - rate /= hid_div +1 ; +do_div (rate, hid_div +1 );
I'm pretty sure mult_frac() could have solved this as well, without temporarily going to u64?
mult_frac(rate, 2, hid_div + 1)
drivers/clk/qcom/clk-rcg2.c static int clk_rcg2_set_parent(struct clk_hw *hw, u8 index) · line 170
Quoted 9 lines
drivers/clk/qcom/clk-rcg2.c +2
-4}if (mode) { -u64 tmp = rate; - tmp *= m; -do_div (tmp, n); - rate = tmp; + rate *= m; +do_div (rate, n);
mult_frac(rate, m, n)
Or am I totally wrong?
- Marijn
Quoted 6 lines
drivers/clk/qcom/clk-rcg2.c +0
-0}return rate;Signature · 1 line
2.17.1
Quoted 4 lines · Marijn Suijten
Can you retitle this to state "overflow" rather than just "wrong"? That's more descriptive.
E.g "Fix clockrate overflow for high parent frequencies"
Sure okay
Quoted 34 lines · Devi Priya
Quoted 18 lines
If the parent clock rate is greater than unsigned long max/2 then integer overflow happens when calculating the clock rate on 32-bit systems. As RCG2 uses half integer dividers, the clock rate is first being multiplied by 2 which will overflow the unsigned long max value. So, use unsigned long long for rate computations to avoid overflow.
Signed-off-by Devi Priya <[email protected]>--- drivers/clk/qcom/clk-rcg2.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)drivers/clk/qcom/clk-rcg2.c +0
-0@@ -156,18 +156,18 @@ static int clk_rcg2_set_parent(struct clk_hw *hw, u8 index)* hid_div n */ drivers/clk/qcom/clk-rcg2.c static int clk_rcg2_set_parent(struct clk_hw *hw, u8 index) · line 165
Quoted 10 lines
drivers/clk/qcom/clk-rcg2.c +4
-2static unsigned long -calc_rate (unsigned long rate,u32 m,u32 n,u32 mode,u32 hid_div) +calc_rate (unsigned long parent_rate,u32 m,u32 n,u32 mode,u32 hid_div) { +u64 rate = parent_rate; +if (hid_div) { rate *=2 ; - rate /= hid_div +1 ; +do_div (rate, hid_div +1 );I'm pretty sure
mult_frac()could have solved this as well, without temporarily going to u64?
Quoted 1 line
mult_frac(rate, 2, hid_div + 1)
Yes, sure will update
Quoted 12 lines
drivers/clk/qcom/clk-rcg2.c static int clk_rcg2_set_parent(struct clk_hw *hw, u8 index) · line 170
Quoted 10 lines
drivers/clk/qcom/clk-rcg2.c +2
-4}if (mode) { -u64 tmp = rate; - tmp *= m; -do_div (tmp, n); - rate = tmp; + rate *= m; +do_div (rate, n);
mult_frac(rate, m, n)
Will update in V2
Thanks,
Devi Priya
Quoted 11 lines
Or am I totally wrong?
- Marijn
Quoted 6 lines
drivers/clk/qcom/clk-rcg2.c +0
-0}return rate;Signature · 1 line
2.17.1