Skip to contentSkip to conversation

power: supply: da9150: use div64_u64() instead of do_div()

PATCH In review Qing Wang 3 participants 4 messages Feb 9, 2022 – Feb 11, 2022

Subsystems (2)
lore
Share & tools
lore

Qing Wang

author
#0 lore raw

PATCH 1/1 power: supply: da9150: 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/power/supply/da9150-fg.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/power/supply/da9150-fg.c +1 -1
index 6e36782..896491a
@@ -250,7 +250,7 @@ static int da9150_fg_current_avg(struct da9150_fg *fg,
 	div = (u64) (sd_gain * shunt_val * 65536ULL);
 	do_div(div, 1000000);
 	res = (u64) (iavg * 1000000ULL);
-	do_div(res, div);
+	div64_u64(res, div);
 
 	val->intval = (int) res;
 
Signature · 1 line
2.7.4
#1 lore raw

Hi,

Quoted 11 lines · 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.

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

---

Thanks, queued.

-- Sebastian

Quoted 19 lines
 drivers/power/supply/da9150-fg.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/power/supply/da9150-fg.c +1 -1
index 6e36782..896491a
@@ -250,7 +250,7 @@ static int da9150_fg_current_avg(struct da9150_fg *fg,
 	div = (u64) (sd_gain * shunt_val * 65536ULL);
 	do_div(div, 1000000);
 	res = (u64) (iavg * 1000000ULL);
-	do_div(res, div);
+	div64_u64(res, div);
 
 	val->intval = (int) res;
 
Signature · 1 line
2.7.4
#2 lore raw

Le 11/02/2022 à 21:50, Sebastian Reichel a écrit :

Quoted 9 lines

Hi,

Quoted 6 lines · 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.

---

Thanks, queued.

All these patches are broken and should be NACKed, fixed or reverted if applied.

See [1].

CJ

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

Quoted 22 lines

-- Sebastian

Quoted 19 lines
  drivers/power/supply/da9150-fg.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/power/supply/da9150-fg.c +1 -1
index 6e36782..896491a
@@ -250,7 +250,7 @@ static int da9150_fg_current_avg(struct da9150_fg *fg,
  	div = (u64) (sd_gain * shunt_val * 65536ULL);
  	do_div(div, 1000000);
  	res = (u64) (iavg * 1000000ULL);
-	do_div(res, div);
+	div64_u64(res, div);
 
  	val->intval = (int) res;
 
Signature · 1 line
2.7.4
#3 lore raw

Hi,

Quoted 18 lines · Christophe JAILLET

Le 11/02/2022 à 21:50, Sebastian Reichel a écrit :

Quoted 9 lines

Hi,

Quoted 6 lines · 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.

---

Thanks, queued.

Quoted 8 lines

All these patches are broken and should be NACKed, fixed or reverted if applied.

See [1].

CJ

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

Oops, thanks for the hint. Patch dropped. I actually missed the do_div(div, 1000000) during my review (probably should go sleeping), so div should be within safe range in the second do_div() anyways.

-- Sebastian

Quoted 22 lines
Quoted 21 lines

-- Sebastian

Quoted 17 lines
  drivers/power/supply/da9150-fg.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/power/supply/da9150-fg.c +1 -1
index 6e36782..896491a
@@ -250,7 +250,7 @@ static int da9150_fg_current_avg(struct da9150_fg *fg,
  	div = (u64) (sd_gain * shunt_val * 65536ULL);
  	do_div(div, 1000000);
  	res = (u64) (iavg * 1000000ULL);
-	do_div(res, div);
+	div64_u64(res, div);
  	val->intval = (int) res;
Signature · 1 line
2.7.4