Skip to contentSkip to conversation

iio: use div64_u64() instead of do_div()

PATCH In review Qing Wang 5 participants 6 messages Feb 9, 2022 – Feb 14, 2022

Subsystems (2)
lore
Share & tools
lore

Qing Wang

author
#0 lore raw

PATCH 1/1 iio: use div64_u64() instead of do_div()

From: Wang Qing <[email protected]>

do_div() does a 64-by-32 division.
When the divisor is u64, do_div() truncates it to 32 bits, this means it can test non-zero and be truncated to zero for division.

fix do_div.cocci warning:
do_div() does a 64-by-32 division, please consider using div64_u64 instead.

---
 drivers/iio/common/scmi_sensors/scmi_iio.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/iio/common/scmi_sensors/scmi_iio.c +5 -5
index d538bf3..d6df5da
@@ -160,7 +160,7 @@ static int scmi_iio_set_odr_val(struct iio_dev *iio_dev, int val, int val2)
 	mult = scnprintf(buf, sizeof(buf), "%llu", sf) - 1;
 
 	sec = int_pow(10, mult) * UHZ_PER_HZ;
-	do_div(sec, uHz);
+	div64_u64(sec, uHz);
 	if (sec == 0) {
 		dev_err(&iio_dev->dev,
 			"Trying to set invalid sensor update value for sensor %s",
@@ -237,10 +237,10 @@ static void convert_ns_to_freq(u64 interval_ns, u64 *hz, u64 *uhz)
 	u64 rem, freq;
 
 	freq = NSEC_PER_SEC;
-	rem = do_div(freq, interval_ns);
+	rem = div64_u64(freq, interval_ns);
 	*hz = freq;
 	*uhz = rem * 1000000UL;
-	do_div(*uhz, interval_ns);
+	div64_u64(*uhz, interval_ns);
 }
 
 static int scmi_iio_get_odr_val(struct iio_dev *iio_dev, int *val, int *val2)
@@ -266,7 +266,7 @@ static int scmi_iio_get_odr_val(struct iio_dev *iio_dev, int *val, int *val2)
 	mult = SCMI_SENS_CFG_GET_UPDATE_EXP(sensor_config);
 	if (mult < 0) {
 		sensor_interval_mult = int_pow(10, abs(mult));
-		do_div(sensor_update_interval, sensor_interval_mult);
+		div64_u64(sensor_update_interval, sensor_interval_mult);
 	} else {
 		sensor_interval_mult = int_pow(10, mult);
 		sensor_update_interval =
@@ -500,7 +500,7 @@ static u64 scmi_iio_convert_interval_to_ns(u32 val)
 	mult = SCMI_SENS_INTVL_GET_EXP(val);
 	if (mult < 0) {
 		sensor_interval_mult = int_pow(10, abs(mult));
-		do_div(sensor_update_interval, sensor_interval_mult);
+		div64_u64(sensor_update_interval, sensor_interval_mult);
 	} else {
 		sensor_interval_mult = int_pow(10, mult);
 		sensor_update_interval =
Signature · 1 line
2.7.4
maintainer
#1 lore raw
Quoted 10 lines · On Wed, 9 Feb 2022 00:37:53 -0800 Qing Wang <[email protected]> wrote:

From: Wang Qing <[email protected]>

do_div() does a 64-by-32 division.
When the divisor is u64, do_div() truncates it to 32 bits, this means it can test non-zero and be truncated to zero for division.

fix do_div.cocci warning:
do_div() does a 64-by-32 division, please consider using div64_u64 instead.

These look correct to me. Jyoti, please could give these a sanity check?

Thanks,

Jonathan

Quoted 48 lines
---
 drivers/iio/common/scmi_sensors/scmi_iio.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/iio/common/scmi_sensors/scmi_iio.c +5 -5
index d538bf3..d6df5da
@@ -160,7 +160,7 @@ static int scmi_iio_set_odr_val(struct iio_dev *iio_dev, int val, int val2)
 	mult = scnprintf(buf, sizeof(buf), "%llu", sf) - 1;
 
 	sec = int_pow(10, mult) * UHZ_PER_HZ;
-	do_div(sec, uHz);
+	div64_u64(sec, uHz);
 	if (sec == 0) {
 		dev_err(&iio_dev->dev,
 			"Trying to set invalid sensor update value for sensor %s",
@@ -237,10 +237,10 @@ static void convert_ns_to_freq(u64 interval_ns, u64 *hz, u64 *uhz)
 	u64 rem, freq;
 
 	freq = NSEC_PER_SEC;
-	rem = do_div(freq, interval_ns);
+	rem = div64_u64(freq, interval_ns);
 	*hz = freq;
 	*uhz = rem * 1000000UL;
-	do_div(*uhz, interval_ns);
+	div64_u64(*uhz, interval_ns);
 }
 
 static int scmi_iio_get_odr_val(struct iio_dev *iio_dev, int *val, int *val2)
@@ -266,7 +266,7 @@ static int scmi_iio_get_odr_val(struct iio_dev *iio_dev, int *val, int *val2)
 	mult = SCMI_SENS_CFG_GET_UPDATE_EXP(sensor_config);
 	if (mult < 0) {
 		sensor_interval_mult = int_pow(10, abs(mult));
-		do_div(sensor_update_interval, sensor_interval_mult);
+		div64_u64(sensor_update_interval, sensor_interval_mult);
 	} else {
 		sensor_interval_mult = int_pow(10, mult);
 		sensor_update_interval =
@@ -500,7 +500,7 @@ static u64 scmi_iio_convert_interval_to_ns(u32 val)
 	mult = SCMI_SENS_INTVL_GET_EXP(val);
 	if (mult < 0) {
 		sensor_interval_mult = int_pow(10, abs(mult));
-		do_div(sensor_update_interval, sensor_interval_mult);
+		div64_u64(sensor_update_interval, sensor_interval_mult);
 	} else {
 		sensor_interval_mult = int_pow(10, mult);
 		sensor_update_interval =
#2 lore raw

Le 13/02/2022 à 18:59, Jonathan Cameron a écrit :

Quoted 9 lines
Quoted 6 lines · On Wed, 9 Feb 2022 00:37:53 -0800 Qing Wang <[email protected]> wrote:

From: Wang Qing <[email protected]>

do_div() does a 64-by-32 division.
When the divisor is u64, do_div() truncates it to 32 bits, this means it can test non-zero and be truncated to zero for division.

Quoted 6 lines
Quoted 4 lines

fix do_div.cocci warning:
do_div() does a 64-by-32 division, please consider using div64_u64 instead.

These look correct to me. Jyoti, please could give these a sanity check?

This is wrong.

See [1].

CJ

[1]: https://lore.kernel.org/linux-kernel/[email protected]/

Quoted 54 lines

Thanks,

Jonathan

Quoted 48 lines
---
  drivers/iio/common/scmi_sensors/scmi_iio.c | 10 +++++-----
  1 file changed, 5 insertions(+), 5 deletions(-)
drivers/iio/common/scmi_sensors/scmi_iio.c +5 -5
index d538bf3..d6df5da
@@ -160,7 +160,7 @@ static int scmi_iio_set_odr_val(struct iio_dev *iio_dev, int val, int val2)
  	mult = scnprintf(buf, sizeof(buf), "%llu", sf) - 1;
 
  	sec = int_pow(10, mult) * UHZ_PER_HZ;
-	do_div(sec, uHz);
+	div64_u64(sec, uHz);
  	if (sec == 0) {
  		dev_err(&iio_dev->dev,
  			"Trying to set invalid sensor update value for sensor %s",
@@ -237,10 +237,10 @@ static void convert_ns_to_freq(u64 interval_ns, u64 *hz, u64 *uhz)
  	u64 rem, freq;
 
  	freq = NSEC_PER_SEC;
-	rem = do_div(freq, interval_ns);
+	rem = div64_u64(freq, interval_ns);
  	*hz = freq;
  	*uhz = rem * 1000000UL;
-	do_div(*uhz, interval_ns);
+	div64_u64(*uhz, interval_ns);
  }
 
  static int scmi_iio_get_odr_val(struct iio_dev *iio_dev, int *val, int *val2)
@@ -266,7 +266,7 @@ static int scmi_iio_get_odr_val(struct iio_dev *iio_dev, int *val, int *val2)
  	mult = SCMI_SENS_CFG_GET_UPDATE_EXP(sensor_config);
  	if (mult < 0) {
  		sensor_interval_mult = int_pow(10, abs(mult));
-		do_div(sensor_update_interval, sensor_interval_mult);
+		div64_u64(sensor_update_interval, sensor_interval_mult);
  	} else {
  		sensor_interval_mult = int_pow(10, mult);
  		sensor_update_interval =
@@ -500,7 +500,7 @@ static u64 scmi_iio_convert_interval_to_ns(u32 val)
  	mult = SCMI_SENS_INTVL_GET_EXP(val);
  	if (mult < 0) {
  		sensor_interval_mult = int_pow(10, abs(mult));
-		do_div(sensor_update_interval, sensor_interval_mult);
+		div64_u64(sensor_update_interval, sensor_interval_mult);
  	} else {
  		sensor_interval_mult = int_pow(10, mult);
  		sensor_update_interval =
#3 lore raw

Le 13/02/2022 à 19:50, Christophe JAILLET a écrit :

Quoted 17 lines

Le 13/02/2022 à 18:59, Jonathan Cameron a écrit :

Quoted 9 lines
Quoted 6 lines · On Wed,  9 Feb 2022 00:37:53 -0800 Qing Wang…

From: Wang Qing <[email protected]>

do_div() does a 64-by-32 division.
When the divisor is u64, do_div() truncates it to 32 bits, this means it can test non-zero and be truncated to zero for division.

Quoted 6 lines
Quoted 4 lines

fix do_div.cocci warning:
do_div() does a 64-by-32 division, please consider using div64_u64 instead.

These look correct to me.  Jyoti, please could give these a sanity check?

Quoted 7 lines

This is wrong.

See [1].

CJ

[1]: https://lore.kernel.org/linux-kernel/20211117112559.jix3hmx7uwqmuryg-bIcnvbaLZ9MEGnE8C9+IrQ@public.gmane.org/

Broken link, sorry:

[1] https://lore.kernel.org/linux-kernel/[email protected]/

Quoted 58 lines
Quoted 56 lines

Thanks,

Jonathan

Quoted 48 lines

---
  drivers/iio/common/scmi_sensors/scmi_iio.c | 10 +++++-----

  1 file changed, 5 insertions(+), 5 deletions(-)
drivers/iio/common/scmi_sensors/scmi_iio.c +4 -4
index d538bf3..d6df5da
@@ -160,7 +160,7 @@ static int scmi_iio_set_odr_val(struct iio_dev 
 *iio_dev, int val, int val2)
       mult = scnprintf(buf, sizeof(buf), "%llu", sf) - 1;
       sec = int_pow(10, mult) * UHZ_PER_HZ;
-    do_div(sec, uHz);
+    div64_u64(sec, uHz);
       if (sec == 0) {
           dev_err(&iio_dev->dev,
               "Trying to set invalid sensor update value for sensor %s",
@@ -237,10 +237,10 @@ static void convert_ns_to_freq(u64 interval_ns, 
 u64 *hz, u64 *uhz)
       u64 rem, freq;
       freq = NSEC_PER_SEC;
-    rem = do_div(freq, interval_ns);
+    rem = div64_u64(freq, interval_ns);
       *hz = freq;
       *uhz = rem * 1000000UL;
-    do_div(*uhz, interval_ns);
+    div64_u64(*uhz, interval_ns);
   }
   static int scmi_iio_get_odr_val(struct iio_dev *iio_dev, int *val, int *val2)
@@ -266,7 +266,7 @@ static int scmi_iio_get_odr_val(struct iio_dev 
 *iio_dev, int *val, int *val2)
       mult = SCMI_SENS_CFG_GET_UPDATE_EXP(sensor_config);
       if (mult < 0) {
           sensor_interval_mult = int_pow(10, abs(mult));
-        do_div(sensor_update_interval, sensor_interval_mult);
+        div64_u64(sensor_update_interval, sensor_interval_mult);
       } else {
           sensor_interval_mult = int_pow(10, mult);

          sensor_update_interval =

drivers/iio/common/scmi_sensors/scmi_iio.c +1 -1
@@ -500,7 +500,7 @@ static u64 scmi_iio_convert_interval_to_ns(u32 val)
       mult = SCMI_SENS_INTVL_GET_EXP(val);
       if (mult < 0) {
           sensor_interval_mult = int_pow(10, abs(mult));
-        do_div(sensor_update_interval, sensor_interval_mult);
+        div64_u64(sensor_update_interval, sensor_interval_mult);
       } else {
           sensor_interval_mult = int_pow(10, mult);

          sensor_update_interval =

#4 lore raw
Quoted 25 lines · On Sun, 13 Feb 2022 19:54:01 +0100 Christophe JAILLET…

Le 13/02/2022 à 19:50, Christophe JAILLET a écrit :

Quoted 24 lines

Le 13/02/2022 à 18:59, Jonathan Cameron a écrit :

Quoted 9 lines
Quoted 6 lines · On Wed,  9 Feb 2022 00:37:53 -0800 Qing Wang…

From: Wang Qing <[email protected]>

do_div() does a 64-by-32 division.
When the divisor is u64, do_div() truncates it to 32 bits, this means it can test non-zero and be truncated to zero for division.

Quoted 7 lines
Quoted 5 lines

fix do_div.cocci warning:
do_div() does a 64-by-32 division, please consider using div64_u64 instead.

These look correct to me.  Jyoti, please could give these a sanity check?

This is wrong.

See [1].

CJ

Quoted 8 lines
Quoted 2 lines

[1]:
https://lore.kernel.org/linux-kernel/20211117112559.jix3hmx7uwqmuryg-bIcnvbaLZ9MEGnE8C9+IrQ@public.gmane.org/

Broken link, sorry:

[1]
https://lore.kernel.org/linux-kernel/[email protected]/

oops. Thanks for the heads up. I'd forgotten the slightly odd convention around do_div

Jonathan

Quoted 61 lines
Quoted 60 lines
Quoted 58 lines

Thanks,

Jonathan

Quoted 50 lines

---
  drivers/iio/common/scmi_sensors/scmi_iio.c | 10 +++++-----

  1 file changed, 5 insertions(+), 5 deletions(-)
drivers/iio/common/scmi_sensors/scmi_iio.c +4 -4
index d538bf3..d6df5da
@@ -160,7 +160,7 @@ static int scmi_iio_set_odr_val(struct iio_dev 
 *iio_dev, int val, int val2)
       mult = scnprintf(buf, sizeof(buf), "%llu", sf) - 1;
       sec = int_pow(10, mult) * UHZ_PER_HZ;
-    do_div(sec, uHz);
+    div64_u64(sec, uHz);
       if (sec == 0) {
           dev_err(&iio_dev->dev,
               "Trying to set invalid sensor update value for sensor %s",
@@ -237,10 +237,10 @@ static void convert_ns_to_freq(u64 interval_ns, 
 u64 *hz, u64 *uhz)
       u64 rem, freq;
       freq = NSEC_PER_SEC;
-    rem = do_div(freq, interval_ns);
+    rem = div64_u64(freq, interval_ns);
       *hz = freq;
       *uhz = rem * 1000000UL;
-    do_div(*uhz, interval_ns);
+    div64_u64(*uhz, interval_ns);
   }
   static int scmi_iio_get_odr_val(struct iio_dev *iio_dev, int *val, 
 int *val2)
@@ -266,7 +266,7 @@ static int scmi_iio_get_odr_val(struct iio_dev 
 *iio_dev, int *val, int *val2)
       mult = SCMI_SENS_CFG_GET_UPDATE_EXP(sensor_config);
       if (mult < 0) {
           sensor_interval_mult = int_pow(10, abs(mult));
-        do_div(sensor_update_interval, sensor_interval_mult);
+        div64_u64(sensor_update_interval, sensor_interval_mult);
       } else {
           sensor_interval_mult = int_pow(10, mult);

          sensor_update_interval =

drivers/iio/common/scmi_sensors/scmi_iio.c +1 -1
@@ -500,7 +500,7 @@ static u64 scmi_iio_convert_interval_to_ns(u32 val)
       mult = SCMI_SENS_INTVL_GET_EXP(val);
       if (mult < 0) {
           sensor_interval_mult = int_pow(10, abs(mult));
-        do_div(sensor_update_interval, sensor_interval_mult);
+        div64_u64(sensor_update_interval, sensor_interval_mult);
       } else {
           sensor_interval_mult = int_pow(10, mult);

          sensor_update_interval =

maintainer
#5 lore raw

yes, this is wrong. Also, the logic would be broken as the two apis do_div() and div64_u64 return values are completely different. Thanks,
Jyoti

Quoted 105 lines · Jonathan Cameron
Quoted 25 lines · On Sun, 13 Feb 2022 19:54:01 +0100 Christophe JAILLET…
Quoted 24 lines · Christophe JAILLET
Quoted 9 lines · Jonathan Cameron
Quoted 6 lines · On Wed, 9 Feb 2022 00:37:53 -0800 Qing Wang…

From: Wang Qing <[email protected]>

do_div() does a 64-by-32 division.
When the divisor is u64, do_div() truncates it to 32 bits, this means it can test non-zero and be truncated to zero for division.

Quoted 7 lines
Quoted 5 lines

fix do_div.cocci warning:
do_div() does a 64-by-32 division, please consider using div64_u64 instead.

These look correct to me. Jyoti, please could give these a sanity check?

This is wrong.

See [1].

CJ

Quoted 8 lines
Quoted 2 lines

[1]:
https://lore.kernel.org/linux-kernel/20211117112559.jix3hmx7uwqmuryg-bIcnvbaLZ9MEGnE8C9+IrQ@public.gmane.org/

Broken link, sorry:

[1]
https://lore.kernel.org/linux-kernel/[email protected]/

oops. Thanks for the heads up. I'd forgotten the slightly odd convention around do_div

Jonathan

Quoted 61 lines
Quoted 60 lines
Quoted 58 lines

Thanks,

Jonathan

Quoted 50 lines
---
  drivers/iio/common/scmi_sensors/scmi_iio.c | 10 +++++-----
  1 file changed, 5 insertions(+), 5 deletions(-)
drivers/iio/common/scmi_sensors/scmi_iio.c +4 -4
index d538bf3..d6df5da
@@ -160,7 +160,7 @@ static int scmi_iio_set_odr_val(struct iio_dev
 *iio_dev, int val, int val2)
      mult = scnprintf(buf, sizeof(buf), "%llu", sf) - 1;
      sec = int_pow(10, mult) * UHZ_PER_HZ;
-    do_div(sec, uHz);
+    div64_u64(sec, uHz);
      if (sec == 0) {
          dev_err(&iio_dev->dev,
              "Trying to set invalid sensor update value for sensor %s",
@@ -237,10 +237,10 @@ static void convert_ns_to_freq(u64 interval_ns,
 u64 *hz, u64 *uhz)
      u64 rem, freq;
      freq = NSEC_PER_SEC;
-    rem = do_div(freq, interval_ns);
+    rem = div64_u64(freq, interval_ns);
      *hz = freq;
      *uhz = rem * 1000000UL;
-    do_div(*uhz, interval_ns);
+    div64_u64(*uhz, interval_ns);
  }
  static int scmi_iio_get_odr_val(struct iio_dev *iio_dev, int *val,
 int *val2)
@@ -266,7 +266,7 @@ static int scmi_iio_get_odr_val(struct iio_dev
 *iio_dev, int *val, int *val2)
      mult = SCMI_SENS_CFG_GET_UPDATE_EXP(sensor_config);
      if (mult < 0) {
          sensor_interval_mult = int_pow(10, abs(mult));
-        do_div(sensor_update_interval, sensor_interval_mult);
+        div64_u64(sensor_update_interval, sensor_interval_mult);
      } else {
          sensor_interval_mult = int_pow(10, mult);

sensor_update_interval =

drivers/iio/common/scmi_sensors/scmi_iio.c +1 -1
@@ -500,7 +500,7 @@ static u64 scmi_iio_convert_interval_to_ns(u32 val)
      mult = SCMI_SENS_INTVL_GET_EXP(val);
      if (mult < 0) {
          sensor_interval_mult = int_pow(10, abs(mult));
-        do_div(sensor_update_interval, sensor_interval_mult);
+        div64_u64(sensor_update_interval, sensor_interval_mult);
      } else {
          sensor_interval_mult = int_pow(10, mult);
          sensor_update_interval =