Skip to contentSkip to conversation

staging: iio: frequency: ad9834: Use div64_ul instead of do_div

PATCH In review Deepak R Varma 3 participants 3 messages Oct 27, 2022 – Oct 29, 2022

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

PATCH v2 1/1 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.

---

Changes in v2:
1. No functional change.
Include outreachy mailing list on the to list

 drivers/staging/iio/frequency/ad9834.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/staging/iio/frequency/ad9834.c +1 -2
index 285df0e489a6..3917a76e7976 100644
@@ -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

maintainer Reviewed-by on v2 1/1
#1 lore raw
Quoted 15 lines

-----Original Message-----
From: Deepak R Varma <[email protected]>
Sent: Friday, October 28, 2022 12:00 AM
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 v2] staging: iio: frequency: ad9834: Use div64_ul instead of do_div

[External]

Quoted 7 lines

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.

---

Clearly, I should have looked better and only reply to this one:

maintainer
#2 lore raw
Quoted 18 lines · Nuno
Quoted 18 lines

-----Original Message-----
From: Deepak R Varma <[email protected]>
Sent: Friday, October 28, 2022 12:00 AM
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 v2] 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.

Quoted 6 lines
Quoted 2 lines

---

Clearly, I should have looked better and only reply to this one:

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.