Linux IIO development
 help / color / mirror / Atom feed
* [PATCH] staging: iio: frequency: ad9832/ad9834: use div64_ul instead of do_div
@ 2026-06-30  3:23 Mohamad Raizudeen
  2026-06-30  7:39 ` Joshua Crofts
  0 siblings, 1 reply; 5+ messages in thread
From: Mohamad Raizudeen @ 2026-06-30  3:23 UTC (permalink / raw)
  To: lars, Michael.Hennerich, jic23, dlechner, nuno.sa, andy
  Cc: Mohamad Raizudeen, gregkh, skhan, linux-iio, linux-staging,
	linux-kernel

Coccinelle warns that do_div() should not be used when the divisor is an unsigned long, because do_div() expects a 32-bit divisor.

In this case, the 'mclk' divisor is an 'unsigned long', which can be 64-bit on certain architectures. The correct function to use here is div64_ul().

Because do_div() modifies the dividend in place, while div64_ul() returns the result, the code was updated to directly return the result of div64_ul. This keeps the math exactly the same but fixes the warning and makes the code safer.

Signed-off-by: Mohamad Raizudeen <raizudeen.kerneldev@gmail.com>
---
 drivers/staging/iio/frequency/ad9832.c | 3 +--
 drivers/staging/iio/frequency/ad9834.c | 3 +--
 2 files changed, 2 insertions(+), 4 deletions(-)

diff --git a/drivers/staging/iio/frequency/ad9832.c b/drivers/staging/iio/frequency/ad9832.c
index b87ea1781b27..91df7ee65bfc 100644
--- a/drivers/staging/iio/frequency/ad9832.c
+++ b/drivers/staging/iio/frequency/ad9832.c
@@ -115,8 +115,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,
diff --git a/drivers/staging/iio/frequency/ad9834.c b/drivers/staging/iio/frequency/ad9834.c
index bdb2580e29bf..70b310488237 100644
--- a/drivers/staging/iio/frequency/ad9834.c
+++ b/drivers/staging/iio/frequency/ad9834.c
@@ -101,8 +101,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.53.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] staging: iio: frequency: ad9832/ad9834: use div64_ul instead of do_div
  2026-06-30  3:23 [PATCH] staging: iio: frequency: ad9832/ad9834: use div64_ul instead of do_div Mohamad Raizudeen
@ 2026-06-30  7:39 ` Joshua Crofts
  2026-06-30 12:02   ` Andy Shevchenko
  0 siblings, 1 reply; 5+ messages in thread
From: Joshua Crofts @ 2026-06-30  7:39 UTC (permalink / raw)
  To: Mohamad Raizudeen
  Cc: lars, Michael.Hennerich, jic23, dlechner, nuno.sa, andy, gregkh,
	skhan, linux-iio, linux-staging, linux-kernel

On Tue, 30 Jun 2026 08:53:22 +0530
Mohamad Raizudeen <raizudeen.kerneldev@gmail.com> wrote:

> Coccinelle warns that do_div() should not be used when the divisor is an unsigned long, because do_div() expects a 32-bit divisor.
> 
> In this case, the 'mclk' divisor is an 'unsigned long', which can be 64-bit on certain architectures. The correct function to use here is div64_ul().
> 
> Because do_div() modifies the dividend in place, while div64_ul() returns the result, the code was updated to directly return the result of div64_ul. This keeps the math exactly the same but fixes the warning and makes the code safer.

Please try to wrap your lines to 72 characters.

> 
> Signed-off-by: Mohamad Raizudeen <raizudeen.kerneldev@gmail.com>
> ---
>  drivers/staging/iio/frequency/ad9832.c | 3 +--
>  drivers/staging/iio/frequency/ad9834.c | 3 +--
>  2 files changed, 2 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/staging/iio/frequency/ad9832.c b/drivers/staging/iio/frequency/ad9832.c
> index b87ea1781b27..91df7ee65bfc 100644
> --- a/drivers/staging/iio/frequency/ad9832.c
> +++ b/drivers/staging/iio/frequency/ad9832.c
> @@ -115,8 +115,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);
>  }

I've actually sent a patch for this previously, however the
discussion ended with the fact that there really isn't a point in
doing this, since mclk will always be a value that can fit
into a 32-bit unsigned type, therefore truncation isn't possible.

See here:
https://lore.kernel.org/all/20260409182755.499a419c@pumpkin/

-- 
Kind regards

CJD

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] staging: iio: frequency: ad9832/ad9834: use div64_ul instead of do_div
  2026-06-30  7:39 ` Joshua Crofts
@ 2026-06-30 12:02   ` Andy Shevchenko
  2026-06-30 13:07     ` Joshua Crofts
  0 siblings, 1 reply; 5+ messages in thread
From: Andy Shevchenko @ 2026-06-30 12:02 UTC (permalink / raw)
  To: Joshua Crofts
  Cc: Mohamad Raizudeen, lars, Michael.Hennerich, jic23, dlechner,
	nuno.sa, andy, gregkh, skhan, linux-iio, linux-staging,
	linux-kernel

On Tue, Jun 30, 2026 at 09:39:31AM +0200, Joshua Crofts wrote:
> On Tue, 30 Jun 2026 08:53:22 +0530
> Mohamad Raizudeen <raizudeen.kerneldev@gmail.com> wrote:

...

> >  {
> >  	unsigned long long freqreg = (u64)fout *
> >  				     (u64)((u64)1L << AD9832_FREQ_BITS);
> > -	do_div(freqreg, mclk);
> > -	return freqreg;
> > +	return div64_ul(freqreg, mclk);
> >  }
> 
> I've actually sent a patch for this previously, however the
> discussion ended with the fact that there really isn't a point in
> doing this, since mclk will always be a value that can fit
> into a 32-bit unsigned type, therefore truncation isn't possible.

Since we are getting more "fixes" in the area, perhaps it's a time to add
a comment in the code summarizing the mentioned discussion?

> See here:
> https://lore.kernel.org/all/20260409182755.499a419c@pumpkin/

-- 
With Best Regards,
Andy Shevchenko



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] staging: iio: frequency: ad9832/ad9834: use div64_ul instead of do_div
  2026-06-30 12:02   ` Andy Shevchenko
@ 2026-06-30 13:07     ` Joshua Crofts
  2026-06-30 13:27       ` Dan Carpenter
  0 siblings, 1 reply; 5+ messages in thread
From: Joshua Crofts @ 2026-06-30 13:07 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: Mohamad Raizudeen, lars, Michael.Hennerich, jic23, dlechner,
	nuno.sa, andy, gregkh, skhan, linux-iio, linux-staging,
	linux-kernel

On Tue, 30 Jun 2026 15:02:02 +0300
Andy Shevchenko <andriy.shevchenko@intel.com> wrote:
> Since we are getting more "fixes" in the area, perhaps it's a time to add
> a comment in the code summarizing the mentioned discussion?
> 

Good idea. I'd also mention Coccinelle and its false positive in the comment
since patches trying to fix this always mention it (my attempt included).

-- 
Kind regards

CJD

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] staging: iio: frequency: ad9832/ad9834: use div64_ul instead of do_div
  2026-06-30 13:07     ` Joshua Crofts
@ 2026-06-30 13:27       ` Dan Carpenter
  0 siblings, 0 replies; 5+ messages in thread
From: Dan Carpenter @ 2026-06-30 13:27 UTC (permalink / raw)
  To: Joshua Crofts
  Cc: Andy Shevchenko, Mohamad Raizudeen, lars, Michael.Hennerich,
	jic23, dlechner, nuno.sa, andy, gregkh, skhan, linux-iio,
	linux-staging, linux-kernel

On Tue, Jun 30, 2026 at 03:07:55PM +0200, Joshua Crofts wrote:
> On Tue, 30 Jun 2026 15:02:02 +0300
> Andy Shevchenko <andriy.shevchenko@intel.com> wrote:
> > Since we are getting more "fixes" in the area, perhaps it's a time to add
> > a comment in the code summarizing the mentioned discussion?
> > 
> 
> Good idea. I'd also mention Coccinelle and its false positive in the comment
> since patches trying to fix this always mention it (my attempt included).

Generally, if the code is old then someone has already looked at the
static checker warnings and decided the original code is fine.  You
should look up old static checker warnings on lore before sending a
patch.

regards,
dan carpenter


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-06-30 13:28 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-30  3:23 [PATCH] staging: iio: frequency: ad9832/ad9834: use div64_ul instead of do_div Mohamad Raizudeen
2026-06-30  7:39 ` Joshua Crofts
2026-06-30 12:02   ` Andy Shevchenko
2026-06-30 13:07     ` Joshua Crofts
2026-06-30 13:27       ` Dan Carpenter

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox