public inbox for linux-clk@vger.kernel.org
 help / color / mirror / Atom feed
From: Devi Priya <quic_devipriy@quicinc.com>
To: <andersson@kernel.org>, <agross@kernel.org>,
	<konrad.dybcio@linaro.org>, <mturquette@baylibre.com>,
	<sboyd@kernel.org>, <mturquette@linaro.org>,
	<linux-arm-msm@vger.kernel.org>, <linux-clk@vger.kernel.org>,
	<linux-kernel@vger.kernel.org>
Cc: <quic_devipriy@quicinc.com>, <quic_saahtoma@quicinc.com>
Subject: [PATCH V2] clk: qcom: clk-rcg2: Fix clock rate overflow for high parent frequencies
Date: Fri, 1 Sep 2023 13:06:40 +0530	[thread overview]
Message-ID: <20230901073640.4973-1-quic_devipriy@quicinc.com> (raw)

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>
---
 Changes in V2:
	- Replaced 64-bit multiplication & a do_div call with mult_frac
	  call as suggested by Marijn Suijten.
	- Updated the subject title
	- Added Fixes tag
	- Did not pick up the R-b tag due to the above changes.

 drivers/clk/qcom/clk-rcg2.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)

diff --git a/drivers/clk/qcom/clk-rcg2.c b/drivers/clk/qcom/clk-rcg2.c
index a42f661550ca..f64d69164547 100644
--- a/drivers/clk/qcom/clk-rcg2.c
+++ b/drivers/clk/qcom/clk-rcg2.c
@@ -159,15 +159,11 @@ static unsigned long
 calc_rate(unsigned long rate, u32 m, u32 n, u32 mode, u32 hid_div)
 {
 	if (hid_div) {
-		rate *= 2;
-		rate /= hid_div + 1;
+		rate = mult_frac(rate, 2, hid_div + 1);
 	}
 
 	if (mode) {
-		u64 tmp = rate;
-		tmp *= m;
-		do_div(tmp, n);
-		rate = tmp;
+		rate = mult_frac(rate, m, n);
 	}
 
 	return rate;
-- 
2.34.1


             reply	other threads:[~2023-09-01  7:37 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-01  7:36 Devi Priya [this message]
2023-09-04 20:36 ` [PATCH V2] clk: qcom: clk-rcg2: Fix clock rate overflow for high parent frequencies Marijn Suijten
2023-09-20 17:14 ` Bjorn Andersson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230901073640.4973-1-quic_devipriy@quicinc.com \
    --to=quic_devipriy@quicinc.com \
    --cc=agross@kernel.org \
    --cc=andersson@kernel.org \
    --cc=konrad.dybcio@linaro.org \
    --cc=linux-arm-msm@vger.kernel.org \
    --cc=linux-clk@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mturquette@baylibre.com \
    --cc=mturquette@linaro.org \
    --cc=quic_saahtoma@quicinc.com \
    --cc=sboyd@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox