Re: [PATCH] clk: sunxi-ng: nkm: use div64_ul() for the rate divisions
From: David Laight
Date: Wed Dec 02 2026 - 17:40:34 EST
On Wed, 18 Nov 2026 17:22:37 +0100
Romain TISSERAND <romain.tisserand@xxxxxxxxx> wrote:
> ccu_nkm_calc_rate() and ccu_nkm_calc_parent_rate() divide their 64-bit
> products by an unsigned long (m, n * k) with do_div(), whose divisor is
> 32 bits: on 64-bit it would be truncated. The N, K and M factors keep
> it far below 2^32, so the results do not change, but use div64_ul(),
> which takes an unsigned long divisor, as coccinelle suggests. On 32-bit
> it is the same do_div() as before.
Exactly when does the truncation happen?
That is what actual values cause a truncation?
And if there is one, why does it only happen on 64bit.
David
>
> Fixes: e9d9dae0d248 ("clk: sunxi-ng: nkm: avoid 32-bit overflow in rate calculations")
> Reported-by: kernel test robot <lkp@xxxxxxxxx>
> Closes: https://lore.kernel.org/oe-kbuild-all/202611151822.QXTOs54d-lkp@xxxxxxxxx/
> Signed-off-by: Romain TISSERAND <romain.tisserand@xxxxxxxxx>
> ---
> Notes:
> - Follow-up to e9d9dae0d248 in sunxi/clk-for-7.5, for the coccinelle
> report of the kernel test robot (do_div() with an unsigned long
> divisor); squash it into that commit if you prefer.
> - No functional change: m and n * k stay far below 2^32 for every NKM
> clock, so do_div() never saw a truncated divisor.
> - Testing: built with W=1 for arm (multi_v7_defconfig with the A31 and
> A33 CCUs) and arm64 (defconfig), no warnings; on arm the divisions
> still go through __do_div64 (no __aeabi_uldivmod). Not run on
> hardware: the generated code for the rates the hardware uses is the
> same division as before.
> - Tool disclosure: prepared with Claude Opus 5.5 (Anthropic) as a coding
> assistant, from the robot report; I reviewed and understand the change.
>
> drivers/clk/sunxi-ng/ccu_nkm.c | 6 ++----
> 1 file changed, 2 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/clk/sunxi-ng/ccu_nkm.c b/drivers/clk/sunxi-ng/ccu_nkm.c
> index e5a8f22d50f5..a11d51dd0f2a 100644
> --- a/drivers/clk/sunxi-ng/ccu_nkm.c
> +++ b/drivers/clk/sunxi-ng/ccu_nkm.c
> @@ -35,9 +35,8 @@ static unsigned long ccu_nkm_calc_rate(unsigned long parent,
> unsigned long n, unsigned long k,
> unsigned long m)
> {
> - u64 rate = (u64)parent * n * k;
> + u64 rate = div64_ul((u64)parent * n * k, m);
>
> - do_div(rate, m);
> return min_t(u64, rate, ULONG_MAX);
> }
>
> @@ -45,9 +44,8 @@ static unsigned long ccu_nkm_calc_parent_rate(unsigned long rate,
> unsigned long n, unsigned long k,
> unsigned long m)
> {
> - u64 parent = (u64)rate * m;
> + u64 parent = div64_ul((u64)rate * m, n * k);
>
> - do_div(parent, n * k);
> return min_t(u64, parent, ULONG_MAX);
> }
>
>
> base-commit: 3f8438336a3b340ffbda9e89480630436423782d