From: "Jernej Škrabec" <jernej.skrabec@gmail.com>
To: "Emilio López" <emilio@elopez.com.ar>,
"David Laight" <david.laight.linux@gmail.com>,
"Anastasia Belova" <abelova@astralinux.ru>
Cc: Anastasia Belova <abelova@astralinux.ru>,
Michael Turquette <mturquette@baylibre.com>,
Stephen Boyd <sboyd@kernel.org>, Chen-Yu Tsai <wens@csie.org>,
Samuel Holland <samuel@sholland.org>,
Maxime Ripard <mripard@kernel.org>,
Hans de Goede <hdegoede@redhat.com>,
linux-clk@vger.kernel.org, linux-arm-kernel@lists.infradead.org,
linux-sunxi@lists.linux.dev, linux-kernel@vger.kernel.org,
lvc-project@linuxtesting.org
Subject: Re: [PATCH] clk: sunxi: clean up rate counting
Date: Thu, 20 Feb 2025 19:12:43 +0100 [thread overview]
Message-ID: <2385242.n0HT0TaD9V@jernej-laptop> (raw)
In-Reply-To: <20250203112930.650813-1-abelova@astralinux.ru>
Dne ponedeljek, 3. februar 2025 ob 12:29:28 Srednjeevropski standardni čas je Anastasia Belova napisal(a):
> If n = 255, the result of multiplication of n and 24000000
> may not fit int type. Swap division and shift with multiplication.
>
> Found by Linux Verification Center (linuxtesting.org) with SVACE.
>
> Fixes: 6424e0aeebc4 ("clk: sunxi: rewrite sun9i_a80_get_pll4_factors()")
> Signed-off-by: Anastasia Belova <abelova@astralinux.ru>
While this should work, should we try to remove (old) sunxi clock drivers
instead? Drivers we converted 8 years ago, except A20 GMAC and A80 PRCM clocks.
Even if we convert them now, we need some transition time for them.
Best regards,
Jernej
> ---
> drivers/clk/sunxi/clk-sun9i-core.c | 14 +++++++++-----
> 1 file changed, 9 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/clk/sunxi/clk-sun9i-core.c b/drivers/clk/sunxi/clk-sun9i-core.c
> index d93c7a53c6c0..639c83ed63b8 100644
> --- a/drivers/clk/sunxi/clk-sun9i-core.c
> +++ b/drivers/clk/sunxi/clk-sun9i-core.c
> @@ -25,12 +25,12 @@
>
> static void sun9i_a80_get_pll4_factors(struct factors_request *req)
> {
> - int n;
> - int m = 1;
> - int p = 1;
> + unsigned int n;
> + unsigned int m = 1;
> + unsigned int p = 1;
>
> /* Normalize value to a 6 MHz multiple (24 MHz / 4) */
> - n = DIV_ROUND_UP(req->rate, 6000000);
> + n = DIV_ROUND_UP(req->rate, 6000000ul);
>
> /* If n is too large switch to steps of 12 MHz */
> if (n > 255) {
> @@ -50,7 +50,11 @@ static void sun9i_a80_get_pll4_factors(struct factors_request *req)
> else if (n < 12)
> n = 12;
>
> - req->rate = ((24000000 * n) >> p) / (m + 1);
> + /* Division and shift should be done before multiplication to
> + * avoid overflow. The result will be correct because '>> p' and
> + * '/ (m + 1)' are both just conditional 'divide by 2'
> + */
> + req->rate = ((24000000ul >> p) / (m + 1)) * n;
> req->n = n;
> req->m = m;
> req->p = p;
>
next prev parent reply other threads:[~2025-02-20 18:12 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-03 11:29 [PATCH] clk: sunxi: clean up rate counting Anastasia Belova
2025-02-20 18:12 ` Jernej Škrabec [this message]
2025-02-21 1:37 ` Andre Przywara
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=2385242.n0HT0TaD9V@jernej-laptop \
--to=jernej.skrabec@gmail.com \
--cc=abelova@astralinux.ru \
--cc=david.laight.linux@gmail.com \
--cc=emilio@elopez.com.ar \
--cc=hdegoede@redhat.com \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-clk@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-sunxi@lists.linux.dev \
--cc=lvc-project@linuxtesting.org \
--cc=mripard@kernel.org \
--cc=mturquette@baylibre.com \
--cc=samuel@sholland.org \
--cc=sboyd@kernel.org \
--cc=wens@csie.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