Skip to contentSkip to conversation

staging: iio: ad5933: Use div64_ul instead of do_div

PATCH In review Deepak R Varma 4 participants 6 messages Oct 27, 2022 – Nov 4, 2022

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

PATCH 1/1 staging: iio: ad5933: 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 was identified using the coccicheck tool.

---
 drivers/staging/iio/impedance-analyzer/ad5933.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/iio/impedance-analyzer/ad5933.c +1 -1
index f177b20f0f2d..730bb31a20d8 100644
@@ -196,7 +196,7 @@ static int ad5933_set_freq(struct ad5933_state *st,
 	} dat;
 
 	freqreg = (u64)freq * (u64)(1 << 27);
-	do_div(freqreg, st->mclk_hz / 4);
+	freqreg = div64_ul(freqreg, st->mclk_hz / 4);
 
 	switch (reg) {
 	case AD5933_REG_FREQ_START:

--
2.34.1

maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 14 lines

-----Original Message-----
From: Deepak R Varma <[email protected]>
Sent: Thursday, October 27, 2022 11:29 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: ad5933: 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 was identified using the coccicheck tool.

---

#2 lore raw
Quoted 9 lines
Quoted 9 lines

-----Original Message-----
From: Deepak R Varma <[email protected]>

[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 was identified using the coccicheck tool.

These changes should all get nacked unless the domain of the values can be shown to be out of range.

The entire point of do_div() is that because division is expensive using a limited range division is significantly faster.

Even on Intel 64 bit cpu the 64 by 32 divide is significantly faster then a full 64 bit divide for the same input values.

One might also question why the divisor is actually 'unsigned long' at all. The code is almost certainly expected to compile for 32bit so the domain of the value should fit in 32 bits.
So either the type could be unsigned int, or it really doesn't matter that the value is truncated to 32bit because it can never be larger.

David

Quoted 5 lines
Quoted 3 lines

---

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

#3 lore raw
Quoted 5 lines · David Laight
Quoted 5 lines
Quoted 5 lines

-----Original Message-----
From: Deepak R Varma <[email protected]>

[External]

Quoted 7 lines
Quoted 4 lines
Quoted 4 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 was identified using the coccicheck tool.

These changes should all get nacked unless the domain of the values can be shown to be out of range.

Hello David,
I looked through the data sheets for these hardware [ad983*] and believe the divisor max can attain 75MHz which fits well in a 32 bit size. Hence the proposed changes for these drivers to switch to div64_ul may actually slowdown the division. Please correct if I said something incorrect.

Requesting to kindly ignore my proposed changes.

Quoted 4 lines

The entire point of do_div() is that because division is expensive using a limited range division is significantly faster.

Quoted 8 lines

Even on Intel 64 bit cpu the 64 by 32 divide is significantly faster then a full 64 bit divide for the same input values.

One might also question why the divisor is actually 'unsigned long' at all. The code is almost certainly expected to compile for 32bit so the domain of the value should fit in 32 bits.
So either the type could be unsigned int, or it really doesn't matter that the value is truncated to 32bit because it can never be larger.

Thank you for the detailed explanation. This is very helpful.

Thank you for the detailed explanation. This is very helpful.

./drv

Quoted 14 lines

David

Quoted 5 lines
Quoted 3 lines

---

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

#4 lore raw
In reply to Deepak R Varma
Quoted 24 lines · Deepak R Varma

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 was identified using the coccicheck tool.

---
 drivers/staging/iio/impedance-analyzer/ad5933.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/iio/impedance-analyzer/ad5933.c +1 -2
index f177b20f0f2d..730bb31a20d8 100644
@@ -196,7 +196,7 @@ static int ad5933_set_freq(struct ad5933_state *st,
 	} dat;
 
 	freqreg = (u64)freq * (u64)(1 << 27);
-	do_div(freqreg, st->mclk_hz / 4);
+	freqreg = div64_ul(freqreg, st->mclk_hz / 4);
 
 	switch (reg) {
 	case AD5933_REG_FREQ_START:
--
Quoted 1 line

2.34.1

No, this isn't ok, please read the mailing list archives for why these changes are not going to be accepted:

	https://lore.kernel.org/r/[email protected]

Please always at least look at the archives of the past few weeks as to if changes like this are able to be accepted or not.

thanks,

greg k-h

#5 lore raw
Quoted 29 lines · Greg Kroah-Hartman
Quoted 24 lines · Deepak R Varma

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 was identified using the coccicheck tool.

---
 drivers/staging/iio/impedance-analyzer/ad5933.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/iio/impedance-analyzer/ad5933.c +1 -2
index f177b20f0f2d..730bb31a20d8 100644
@@ -196,7 +196,7 @@ static int ad5933_set_freq(struct ad5933_state *st,
 	} dat;
 
 	freqreg = (u64)freq * (u64)(1 << 27);
-	do_div(freqreg, st->mclk_hz / 4);
+	freqreg = div64_ul(freqreg, st->mclk_hz / 4);
 
 	switch (reg) {
 	case AD5933_REG_FREQ_START:
--
Quoted 1 line

2.34.1

No, this isn't ok, please read the mailing list archives for why these changes are not going to be accepted:

Quoted 4 lines
	https://lore.kernel.org/r/[email protected]

Please always at least look at the archives of the past few weeks as to if changes like this are able to be accepted or not.

Hello Greg,
My apologies for not looking at the lore mailing archive. I only looked at the past git commits and found a few similar changes accepted in the past. My bad. I will always look at the mailing archive as well going forward.

There are other review comments from the experts on similar patches. I appreciate everyone's time and comment. I will look at those and revert accordingly.

Thank you,
./drv

Quoted 4 lines

thanks,

greg k-h