Re: [PATCH] iio: frequency: ad983x: replace do_div() with div64_ul().

From: David Laight

Date: Thu Apr 09 2026 - 17:46:26 EST


On Thu, 9 Apr 2026 21:58:20 +0200
Joshua Crofts <joshua.crofts1@xxxxxxxxx> wrote:

> On Thu, 9 Apr 2026 at 19:27, David Laight <david.laight.linux@xxxxxxxxx> wrote:
> > Except here you have clock frequencies.
> > Either they fit in 32bits or they don't.
> > So the value shouldn't be 'unsigned long', but either u32 or u64.
>
> The clk struct in linux/clk.h explicitly uses an unsigned long to represent
> the clock value, which is used in this driver. Using an unsigned long
> ensures platform independent usage without type mismatching.

Not really.
I've NFI why the the clock API using 'unsigned long'.
It almost certainly predates 64bit and any real idea that a clock might
exceed 4.2GHz.
It might even come from someone who wrote 16bit windows code and wanted
to ensure it was actually 32bit.

For 'platform portability' clocks are limited to 32bits so the 32bit
divide is fine.
If the clock could exceed 4.2G then you'd have to use u64 throughout.

>
> > You need to check the domain of the values, coccinelle is just looking
> > at the types.
> > Even with mclk being 'unsigned long' the code is fine provided the
> > frequency is below 4.2GHz.
>
> I agree that we're not dealing with potential truncation problems, however
> changing to div64_ul aligns the types with the clk API and scrubs a perfectly
> valid cocci error.

cocci only does type checking, it doesn't know the domain of the values.
So in this case it is almost certainly a false positive.

David