Skip to contentSkip to conversation

iio: frequency: ad983x: replace do_div() with div64_ul().

PATCH Superseded Joshua Crofts 5 participants 13 messages Apr 9 – Apr 10

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: frequency: ad983x: replace do_div() with div64_ul().

Coccinelle reported the following in both ad983x drivers:
do_div() does a 64-by-32 division, please consider using div64_ul instead.

Fix this by replacing do_div() with div64_ul(), which safely handles 64-bit dividends and unsigned long divisors across all architectures.

---
 drivers/staging/iio/frequency/ad9832.c | 3 ++-
 drivers/staging/iio/frequency/ad9834.c | 3 ++-
 2 files changed, 4 insertions(+), 2 deletions(-)
drivers/staging/iio/frequency/ad9832.c +2 -1
index b87ea1781b..61ee93263a 100644
@@ -13,6 +13,7 @@
 #include <linux/device.h>
 #include <linux/err.h>
 #include <linux/kernel.h>
+#include <linux/math64.h>
 #include <linux/module.h>
 #include <linux/regulator/consumer.h>
 #include <linux/slab.h>
@@ -115,7 +116,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);
+	freqreg = div64_ul(freqreg, mclk);
 	return freqreg;
 }
 
drivers/staging/iio/frequency/ad9834.c +2 -1
index d339d5e8e0..83d0e1360c 100644
@@ -10,6 +10,7 @@
 #include <linux/workqueue.h>
 #include <linux/device.h>
 #include <linux/kernel.h>
+#include <linux/math64.h>
 #include <linux/slab.h>
 #include <linux/sysfs.h>
 #include <linux/list.h>
@@ -101,7 +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);
+	freqreg = div64_ul(freqreg, mclk);
 	return freqreg;
 }
 
Signature · 1 line
2.47.3
#1 lore raw
Quoted 4 lines · Joshua Crofts

Coccinelle reported the following in both ad983x drivers:
do_div() does a 64-by-32 division, please consider using div64_ul instead.

Apologies, I typoed Greg's and Nuno's emails in the original CC list.

Signature · 3 lines
Kind regards

CJD
maintainer
#2 lore raw
Quoted 7 lines · Joshua Crofts
Quoted 4 lines · Joshua Crofts

Coccinelle reported the following in both ad983x drivers:
do_div() does a 64-by-32 division, please consider using div64_ul instead.

Apologies, I typoed Greg's and Nuno's emails in the original CC list.

Please send a v2 with that, as we didn't get this message at all :(

thanks,

greg k-h

#3 lore raw

PATCH v2 1/1 iio: frequency: ad983x: replace do_div() with div64_ul().

Coccinelle reported the following in both ad983x drivers:
do_div() does a 64-by-32 division, please consider using div64_ul instead.

Fix this by replacing do_div() with div64_ul(), which safely handles 64-bit dividends and unsigned long divisors across all architectures.

---
v2:
- resent email with corrected addresses

 drivers/staging/iio/frequency/ad9832.c | 3 ++-
 drivers/staging/iio/frequency/ad9834.c | 3 ++-
 2 files changed, 4 insertions(+), 2 deletions(-)
drivers/staging/iio/frequency/ad9832.c +2 -1
index b87ea1781b..61ee93263a 100644
@@ -13,6 +13,7 @@
 #include <linux/device.h>
 #include <linux/err.h>
 #include <linux/kernel.h>
+#include <linux/math64.h>
 #include <linux/module.h>
 #include <linux/regulator/consumer.h>
 #include <linux/slab.h>
@@ -115,7 +116,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);
+	freqreg = div64_ul(freqreg, mclk);
 	return freqreg;
 }
 
drivers/staging/iio/frequency/ad9834.c +2 -1
index d339d5e8e0..83d0e1360c 100644
@@ -10,6 +10,7 @@
 #include <linux/workqueue.h>
 #include <linux/device.h>
 #include <linux/kernel.h>
+#include <linux/math64.h>
 #include <linux/slab.h>
 #include <linux/sysfs.h>
 #include <linux/list.h>
@@ -101,7 +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);
+	freqreg = div64_ul(freqreg, mclk);
 	return freqreg;
 }
 
Signature · 1 line
2.47.3
reviewer
#4 lore raw
Quoted 11 lines · Joshua Crofts

Coccinelle reported the following in both ad983x drivers:
do_div() does a 64-by-32 division, please consider using div64_ul instead.

Fix this by replacing do_div() with div64_ul(), which safely handles 64-bit dividends and unsigned long divisors across all architectures.

---
v2:

Please don't send v2 or later revisions in reply to v1. It breaks some workflows. Start a new thread instead.

Quoted 1 line

- resent email with corrected addresses

Instead of a v2, you can just add a RESEND prefix to the subject line in cases like this. But do include an explanation like this when you do that.

Quoted 18 lines
 drivers/staging/iio/frequency/ad9832.c | 3 ++-
 drivers/staging/iio/frequency/ad9834.c | 3 ++-
 2 files changed, 4 insertions(+), 2 deletions(-)
drivers/staging/iio/frequency/ad9832.c +1 -0
index b87ea1781b..61ee93263a 100644
@@ -13,6 +13,7 @@
 #include <linux/device.h>
 #include <linux/err.h>
 #include <linux/kernel.h>
+#include <linux/math64.h>
 #include <linux/module.h>
 #include <linux/regulator/consumer.h>
 #include <linux/slab.h>

drivers/staging/iio/frequency/ad9832.c static unsigned long ad9832_calc_freqreg(unsigned long mclk, unsigned long fout) · line 118

Quoted 3 lines
drivers/staging/iio/frequency/ad9832.c +0 -0
@@ -115,7 +116,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);

I would expect checkpatch to complain that there is no blank line between variable declarations and the rest of the code here.

drivers/staging/iio/frequency/ad9832.c static unsigned long ad9832_calc_freqreg(unsigned long mclk, unsigned long fout) · line 120

Quoted 3 lines
drivers/staging/iio/frequency/ad9832.c +1 -1
-	do_div(freqreg, mclk);
+	freqreg = div64_ul(freqreg, mclk);
 	return freqreg;

We can just return directly now.

	return div64_ul(freqreg, mclk);
Quoted 15 lines
drivers/staging/iio/frequency/ad9832.c +0 -0
 }
 
drivers/staging/iio/frequency/ad9834.c +1 -0
index d339d5e8e0..83d0e1360c 100644
@@ -10,6 +10,7 @@
 #include <linux/workqueue.h>
 #include <linux/device.h>
 #include <linux/kernel.h>
+#include <linux/math64.h>
 #include <linux/slab.h>
 #include <linux/sysfs.h>
 #include <linux/list.h>

drivers/staging/iio/frequency/ad9834.c static unsigned int ad9834_calc_freqreg(unsigned long mclk, unsigned long fout) · line 106

Quoted 6 lines
drivers/staging/iio/frequency/ad9834.c +1 -1
@@ -101,7 +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);
+	freqreg = div64_ul(freqreg, mclk);
 	return freqreg;

ditto.

Quoted 2 lines
drivers/staging/iio/frequency/ad9834.c +0 -0
 }
 
#5 lore raw
Quoted 13 lines · David Lechner
Quoted 11 lines · Joshua Crofts

Coccinelle reported the following in both ad983x drivers:
do_div() does a 64-by-32 division, please consider using div64_ul instead.

Fix this by replacing do_div() with div64_ul(), which safely handles 64-bit dividends and unsigned long divisors across all architectures.

---
v2:

Quoted 8 lines

Please don't send v2 or later revisions in reply to v1. It breaks some workflows. Start a new thread instead.

Quoted 1 line

- resent email with corrected addresses

Instead of a v2, you can just add a RESEND prefix to the subject line in cases like this. But do include an explanation like this when you do that.

Nevertheless my comments still applicable to v2. The also Documented recommendation to avoid sending new versions earlier than 24h.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#6 lore raw
In reply to Joshua Crofts
Quoted 3 lines · Joshua Crofts

Coccinelle reported the following in both ad983x drivers:
do_div() does a 64-by-32 division, please consider using div64_ul instead.

div64_ul()

Quoted 3 lines

Fix this by replacing do_div() with div64_ul(), which safely handles 64-bit dividends and unsigned long divisors across all architectures.

You should really dive into this code, it has more to work on...

...

Quoted 4 lines

#include <linux/device.h>
#include <linux/err.h>

#include <linux/kernel.h>

Consider replacing "proxy" headers at some point. (Not related to this patch, though.)

Quoted 4 lines

+#include <linux/math64.h>
#include <linux/module.h>
#include <linux/regulator/consumer.h>
#include <linux/slab.h>

...

drivers/staging/iio/frequency/ad9832.c static unsigned long ad9832_calc_freqreg(unsigned long mclk, unsigned long fout) · line 118

Quoted 4 lines

static unsigned long ad9832_calc_freqreg(unsigned long mclk, unsigned long fout)
{

drivers/staging/iio/frequency/ad9832.c +0 -0
 	unsigned long long freqreg = (u64)fout *
 				     (u64)((u64)1L << AD9832_FREQ_BITS);

(u64)1L is a fancy way of doing 1ULL. The second (u64) is also redundant and
altogether it's just a BIT_ULL(). But, check the value of _FREQ_BITS. If it's less than 32, the whole train of nonsense can be collapsed just to a BIT().

Quoted 4 lines
drivers/staging/iio/frequency/ad9832.c +1 -1
-	do_div(freqreg, mclk);
+	freqreg = div64_ul(freqreg, mclk);
 	return freqreg;
 }

...

drivers/staging/iio/frequency/ad9834.c line 13

Quoted 7 lines
drivers/staging/iio/frequency/ad9834.c +1 -0
@@ -10,6 +10,7 @@
 #include <linux/workqueue.h>
 #include <linux/device.h>
 #include <linux/kernel.h>
+#include <linux/math64.h>

Side note as per above.

drivers/staging/iio/frequency/ad9834.c static unsigned int ad9834_calc_freqreg(unsigned long mclk, unsigned long fout) · line 107

Quoted 11 lines
drivers/staging/iio/frequency/ad9834.c +1 -1
 #include <linux/slab.h>
 #include <linux/sysfs.h>
 #include <linux/list.h>
@@ -101,7 +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);
+	freqreg = div64_ul(freqreg, mclk);
 	return freqreg;
 }

Ditto. Also split changes on per-driver basis.

...

So, if you go suggested way, I would expect to have 4 patches (2 series by 2 patches each) out of this one.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#7 lore raw
In reply to Joshua Crofts
Quoted 7 lines · On Thu, 9 Apr 2026 13:18:23 +0000 Joshua Crofts <[email protected]>…

Coccinelle reported the following in both ad983x drivers:
do_div() does a 64-by-32 division, please consider using div64_ul instead.

Fix this by replacing do_div() with div64_ul(), which safely handles 64-bit dividends and unsigned long divisors across all architectures.

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.

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.

So you are probably fixing nothing.

David

Quoted 49 lines
---
 drivers/staging/iio/frequency/ad9832.c | 3 ++-
 drivers/staging/iio/frequency/ad9834.c | 3 ++-
 2 files changed, 4 insertions(+), 2 deletions(-)
drivers/staging/iio/frequency/ad9832.c +2 -1
index b87ea1781b..61ee93263a 100644
@@ -13,6 +13,7 @@
 #include <linux/device.h>
 #include <linux/err.h>
 #include <linux/kernel.h>
+#include <linux/math64.h>
 #include <linux/module.h>
 #include <linux/regulator/consumer.h>
 #include <linux/slab.h>
@@ -115,7 +116,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);
+	freqreg = div64_ul(freqreg, mclk);
 	return freqreg;
 }
 
drivers/staging/iio/frequency/ad9834.c +2 -1
index d339d5e8e0..83d0e1360c 100644
@@ -10,6 +10,7 @@
 #include <linux/workqueue.h>
 #include <linux/device.h>
 #include <linux/kernel.h>
+#include <linux/math64.h>
 #include <linux/slab.h>
 #include <linux/sysfs.h>
 #include <linux/list.h>
@@ -101,7 +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);
+	freqreg = div64_ul(freqreg, mclk);
 	return freqreg;
 }
 
#8 lore raw
Quoted 3 lines · David Laight

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.

Quoted 4 lines

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.

Signature · 3 lines
Kind regards

CJD
#9 lore raw
Quoted 8 lines · On Thu, 9 Apr 2026 21:58:20 +0200 Joshua Crofts <[email protected]>…
Quoted 3 lines · David Laight

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.

Quoted 9 lines
Quoted 5 lines

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

#10 lore raw
In reply to Joshua Crofts
Quoted 8 lines · Joshua Crofts
Quoted 3 lines · David Laight

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.

...which effectively means the frequency most likely fits 32-bit. Otherwise would it ever work on 32-bit platform? This is a mistake in CLK design.

Quoted 8 lines
Quoted 4 lines

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.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#11 lore raw
Quoted 6 lines · Andy Shevchenko
Quoted 3 lines

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.

...which effectively means the frequency most likely fits 32-bit. Otherwise would it ever work on 32-bit platform? This is a mistake in CLK design.

So we keep the do_div() macro and cast mclk to u32.

Signature · 3 lines
Kind regards

CJD
#12 lore raw
Quoted 11 lines · On Fri, 10 Apr 2026 08:58:10 +0200 Joshua Crofts <[email protected]>…
Quoted 6 lines · Andy Shevchenko
Quoted 3 lines

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.

...which effectively means the frequency most likely fits 32-bit. Otherwise would it ever work on 32-bit platform? This is a mistake in CLK design.

So we keep the do_div() macro and cast mclk to u32.

There is no need for a cast, just ignore coccineal.

David