Re: [PATCH v2 01/13] clk: scmi: Fix clock rate rounding

From: Geert Uytterhoeven

Date: Wed Mar 11 2026 - 07:31:00 EST


Hi Cristian,

On Tue, 10 Mar 2026 at 19:40, Cristian Marussi <cristian.marussi@xxxxxxx> wrote:
> While the do_div() helper used for rounding expects its divisor argument
> to be a 32bits quantity, the currently provided divisor parameter is a
> 64bit value that, as a consequence, is silently truncated and a possible
> source of bugs.
>
> Fix by using the proper div64_ul helper.
>
> Cc: Michael Turquette <mturquette@xxxxxxxxxxxx>
> Cc: Stephen Boyd <sboyd@xxxxxxxxxx>
> Cc: linux-clk@xxxxxxxxxxxxxxx
> Fixes: 7a8655e19bdb ("clk: scmi: Fix the rounding of clock rate")
> Signed-off-by: Cristian Marussi <cristian.marussi@xxxxxxx>

Thanks for your patch!

> --- a/drivers/clk/clk-scmi.c
> +++ b/drivers/clk/clk-scmi.c

> @@ -83,7 +83,7 @@ static int scmi_clk_determine_rate(struct clk_hw *hw,
>
> ftmp = req->rate - fmin;
> ftmp += clk->info->range.step_size - 1; /* to round up */
> - do_div(ftmp, clk->info->range.step_size);
> + ftmp = div64_ul(ftmp, clk->info->range.step_size);

include/linux/math64.h has:

#if BITS_PER_LONG == 64
#define div64_ul(x, y) div64_u64((x), (y))
#elif BITS_PER_LONG == 32
#define div64_ul(x, y) div_u64((x), (y))
#endif

I.e. div64_ul() is meant for the case where the divisor is unsigned
long. Hence div64_ul() may still truncate step_size on 32-bit
platforms, and thus should use div64_u64() unconditionally.

I am aware clock rates are "unsigned long" on 32-bit platforms, and
thus cannot support rates that do not fit in a 32-bit value.
If that is the reason you are using div64_ul(), it should be documented
properly. And probably the SCMI core code should reject any rate values
(incl. min, max, step) that do not fit in unsigned long, as such clocks
cannot be used on 32-bit platforms.

>
> req->rate = ftmp * clk->info->range.step_size + fmin;
>

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@xxxxxxxxxxxxxx

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds