2022-10-27 22:22:58

by Deepak R Varma

[permalink] [raw]
Subject: [PATCH] staging: iio: frequency: ad9834: 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/ad9834.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/drivers/staging/iio/frequency/ad9834.c b/drivers/staging/iio/frequency/ad9834.c
index 285df0e489a6..3917a76e7976 100644
--- a/drivers/staging/iio/frequency/ad9834.c
+++ b/drivers/staging/iio/frequency/ad9834.c
@@ -102,8 +102,7 @@ static unsigned int ad9834_calc_freqreg(unsigned long mclk, unsigned long fout)
{
unsigned long long freqreg = (u64)fout * (u64)BIT(AD9834_FREQ_BITS);

- do_div(freqreg, mclk);
- return freqreg;
+ return div64_ul(freqreg, mclk);
}

static int ad9834_write_frequency(struct ad9834_state *st,
--
2.34.1





2022-10-28 10:29:41

by Nuno Sa

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



> -----Original Message-----
> From: Deepak R Varma <[email protected]>
> Sent: Thursday, October 27, 2022 11:48 PM
> To: Lars-Peter Clausen <[email protected]>; Hennerich, Michael
> <[email protected]>; Jonathan Cameron <[email protected]>;
> Greg Kroah-Hartman <[email protected]>; linux-
> [email protected]; [email protected]; linux-
> [email protected]
> Subject: [PATCH] staging: iio: frequency: ad9834: 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]>