Re: [PATCH] clk: baikal-t1: Using div64_ Ul replaces do_ Div() function

From: Geert Uytterhoeven
Date: Mon Jul 24 2023 - 06:13:56 EST


Hi Yonggang,

CC Serge

On Wed, Jun 14, 2023 at 8:07 AM <wuyonggang001@xxxxxxxxxx> wrote:
> Fix the following coccicheck warning:
>
> drivers/clk/baikal-t1/ccu-pll.c:81:1-7: WARNING: do_div() does a
> 64-by-32 division, please consider using div64_ul instead.
>
> Signed-off-by: Yonggang Wu <wuyonggang001@xxxxxxxxxx>

Thanks for your patch, which is now commit b93d1331ea266dea
("clk: baikal-t1: Using div64_ Ul replaces do_ Div() function")
in clk/clk-next.

> b/drivers/clk/baikal-t1/ccu-pll.c
> index 13ef28001439..d41735c6956a 100644
> --- a/drivers/clk/baikal-t1/ccu-pll.c
> +++ b/drivers/clk/baikal-t1/ccu-pll.c
> @@ -66,7 +66,7 @@ static inline unsigned long
> ccu_pll_lock_delay_us(unsigned long ref_clk,
> {
> u64 us = 500ULL * nr * USEC_PER_SEC;
>
> - do_div(us, ref_clk);
> + div64_ul(us, ref_clk);

The above is not equivalent:
- do_div() returned the quotient as an output parameter in us,
- div64_ul() returns the quotient using the return value.

Have you tested your patch?

>
> return us;

So this should become:

return div64_ul(500ULL * nr * USEC_PER_SEC, ref_clk);

> }
> @@ -78,9 +78,9 @@ static inline unsigned long ccu_pll_calc_freq(unsigned
> long ref_clk,
> {
> u64 tmp = ref_clk;
>
> - do_div(tmp, nr);
> + div64_ul(tmp, nr);
> tmp *= nf;
> - do_div(tmp, od);
> + div64_ul(tmp, od);
>
> return tmp;

Likewise.
But as ref_clk is unsigned long, there is no need to use div64_ul()
for the first division, and this can be simplified to:

u64 tmp = (u64)(ref_clk / nr) * nf;
return div64_ul(tmp, od);

To avoid loss of precision, it might be better to reverse the order
of the division and multiplication:

u64 tmp = (u64)ref_clk * nf / nr;

But doing that requires intimate knowledge about the range of nf to
avoid overflow, so I leave that to Serge.

> }

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