2022-10-27 22:48:00

by Deepak R Varma

[permalink] [raw]
Subject: [PATCH] staging: iio: frequency: ad9832: Use div64_ul instead of do_div

do_div() does a 64-by-32 division. Here the divisor is an unsigned long
which on some platforms is 64 bit wide. So use div64_ul instead of do_div
to avoid a possible truncation. Issue identified using the
coccicheck tool.

Signed-off-by: Deepak R Varma <[email protected]>
---
drivers/staging/iio/frequency/ad9832.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/drivers/staging/iio/frequency/ad9832.c b/drivers/staging/iio/frequency/ad9832.c
index 6f9eebd6c7ee..a8409f6b1c4c 100644
--- a/drivers/staging/iio/frequency/ad9832.c
+++ b/drivers/staging/iio/frequency/ad9832.c
@@ -122,8 +122,7 @@ static unsigned long ad9832_calc_freqreg(unsigned long mclk, unsigned long fout)
{
unsigned long long freqreg = (u64)fout *
(u64)((u64)1L << AD9832_FREQ_BITS);
- do_div(freqreg, mclk);
- return freqreg;
+ return div64_ul(freqreg, mclk);
}

static int ad9832_write_frequency(struct ad9832_state *st,
--
2.34.1





2022-10-28 10:29:43

by Nuno Sa

[permalink] [raw]
Subject: RE: [PATCH] staging: iio: frequency: ad9832: Use div64_ul instead of do_div



> -----Original Message-----
> From: Deepak R Varma <[email protected]>
> Sent: Thursday, October 27, 2022 11:54 PM
> To: [email protected]; Lars-Peter Clausen <[email protected]>;
> Hennerich, Michael <[email protected]>; Jonathan Cameron
> <[email protected]>; Greg Kroah-Hartman <[email protected]>;
> [email protected]; [email protected]; linux-
> [email protected]
> Subject: [PATCH] staging: iio: frequency: ad9832: Use div64_ul instead of
> do_div
>
> [External]
>
> do_div() does a 64-by-32 division. Here the divisor is an unsigned long
> which on some platforms is 64 bit wide. So use div64_ul instead of do_div
> to avoid a possible truncation. Issue identified using the
> coccicheck tool.
>
> Signed-off-by: Deepak R Varma <[email protected]>
> ---

Reviewed-by: Nuno S? <[email protected]>

2022-10-29 12:06:24

by Jonathan Cameron

[permalink] [raw]
Subject: Re: [PATCH] staging: iio: frequency: ad9832: Use div64_ul instead of do_div

On Fri, 28 Oct 2022 10:11:56 +0000
"Sa, Nuno" <[email protected]> wrote:

> > -----Original Message-----
> > From: Deepak R Varma <[email protected]>
> > Sent: Thursday, October 27, 2022 11:54 PM
> > To: [email protected]; Lars-Peter Clausen <[email protected]>;
> > Hennerich, Michael <[email protected]>; Jonathan Cameron
> > <[email protected]>; Greg Kroah-Hartman <[email protected]>;
> > [email protected]; [email protected]; linux-
> > [email protected]
> > Subject: [PATCH] staging: iio: frequency: ad9832: Use div64_ul instead of
> > do_div
> >
> > [External]
> >
> > do_div() does a 64-by-32 division. Here the divisor is an unsigned long
> > which on some platforms is 64 bit wide. So use div64_ul instead of do_div
> > to avoid a possible truncation. Issue identified using the
> > coccicheck tool.
> >
> > Signed-off-by: Deepak R Varma <[email protected]>
> > ---
>
> Reviewed-by: Nuno Sá <[email protected]>

As per the email Greg linked to, please take a close look at the surround code
and include analysis of whether the value can actually be greater than 32 bits.
Note that in most cases that would actually mean the code was broken on 32 bit
platforms.