Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

clk: qcom: clk-rcg2: Fix clock rate overflow 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.
Hence, replace the common pattern of doing 64-bit multiplication
and then a do_div() call with simpler mult_frac call.

Fixes: bcd61c0f535a ("clk: qcom: Add support for root clock generators (RCGs)")
Signed-off-by: Devi Priya <quic_devipriy@quicinc.com>
Reviewed-by: Marijn Suijten <marijn.suijten@somainline.org>
Link: https://lore.kernel.org/r/20230901073640.4973-1-quic_devipriy@quicinc.com
[bjorn: Also drop unnecessary {} around single statements]
Signed-off-by: Bjorn Andersson <andersson@kernel.org>

authored by

Devi Priya and committed by
Bjorn Andersson
f7b7d301 4afda5f6

+4 -10
+4 -10
drivers/clk/qcom/clk-rcg2.c
··· 158 158 static unsigned long 159 159 calc_rate(unsigned long rate, u32 m, u32 n, u32 mode, u32 hid_div) 160 160 { 161 - if (hid_div) { 162 - rate *= 2; 163 - rate /= hid_div + 1; 164 - } 161 + if (hid_div) 162 + rate = mult_frac(rate, 2, hid_div + 1); 165 163 166 - if (mode) { 167 - u64 tmp = rate; 168 - tmp *= m; 169 - do_div(tmp, n); 170 - rate = tmp; 171 - } 164 + if (mode) 165 + rate = mult_frac(rate, m, n); 172 166 173 167 return rate; 174 168 }