clk: sunxi-ng: nkm: use div64_ul() for the rate divisions
Subsystems (2)
PATCH 1/1 clk: sunxi-ng: nkm: use div64_ul() for the rate divisions
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.
---
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(-)
drivers/clk/sunxi-ng/ccu_nkm.c +2 -4
@@ -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
Signature · 1 line
2.43.0
Hi,
Gentle ping on this follow-up to e9d9dae0d248 ("clk: sunxi-ng: nkm:
avoid 32-bit overflow in rate calculations"), now in sunxi clk-for-7.5. It still applies as is on sunxi/for-next.
Thanks,
Romain
Quoted 6 lines · On Wed, 18 Nov 2026 17:22:37 +0100 Romain TISSERAND…
ccu_nkm_calc_rate()andccu_nkm_calc_parent_rate()divide their 64-bit products by an unsigned long (m, n * k) withdo_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 usediv64_ul(), which takes an unsigned long divisor, as coccinelle suggests. On 32-bit it is the samedo_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
Quoted 51 lines
Fixes e9d9dae0d248 ("clk: sunxi-ng: nkm: avoid 32-bit overflow in rate calculations")Reported-by kernel test robot <[email protected]>Signed-off-by Romain TISSERAND <[email protected]>---
Notes:
- Follow-up toe9d9dae0d248in 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, sodo_div()never saw a truncated divisor.
- Testing: built with W=1 for arm (multi_v7_defconfigwith 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(-)drivers/clk/sunxi-ng/ccu_nkm.c +2
-4@@ -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
Hi David,
Quoted 2 lines · David Laight
Exactly when does the truncation happen?
That is what actual values cause a truncation?
None. Every NKM clock in sunxi-ng has N <= 32, K <= 4 and M <= 16, so n * k <= 128 and m <= 16: the divisor always fits in 32 bits and do_div() gives the exact result today.
Quoted 1 line
And if there is one, why does it only happen on 64bit.
Only in the sense that the divisor is an unsigned long: on 64-bit it is 64 bits wide and do_div() takes a u32, so the conversion could in theory drop high bits; on 32-bit unsigned long is already 32 bits, so there is nothing to drop. With the real factor ranges it never happens on either.
So "on 64-bit it would be truncated" overstates it, sorry for the misleading wording and for the Fixes: tag: there is no bug to fix. The patch only quiets the coccinelle do_div() check the robot reported, with no functional change. If the maintainers want it, I will send a v2 whose commit message says just that, without the Fixes: tag; otherwise it can simply be dropped.
Thanks,
Romain
Quoted 18 lines · On Thu, 3 Dec 2026 08:36:45 +0100 Romain TISSERAND…
Hi David,
Quoted 2 lines · David Laight
Exactly when does the truncation happen?
That is what actual values cause a truncation?None. Every NKM clock in sunxi-ng has N <= 32, K <= 4 and M <= 16, so n * k <= 128 and m <= 16: the divisor always fits in 32 bits and
do_div()gives the exact result today.Quoted 1 line
And if there is one, why does it only happen on 64bit.
Only in the sense that the divisor is an unsigned long: on 64-bit it is 64 bits wide and
do_div()takes a u32, so the conversion could in theory drop high bits; on 32-bit unsigned long is already 32 bits, so there is nothing to drop. With the real factor ranges it never happens on either.
Quoted 6 lines
So "on 64-bit it would be truncated" overstates it, sorry for the misleading wording and for the Fixes: tag: there is no bug to fix. The patch only quiets the coccinelle
do_div()check the robot reported, with no functional change. If the maintainers want it, I will send a v2 whose commit message says just that, without the Fixes: tag; otherwise it can simply be dropped.
Coccinelle just needs ignoring (again).
Type based bound checking ends up requiring something that supports indefinite length numbers.
David
Quoted 3 lines
Thanks,
Romain