Skip to contentSkip to conversation

hwmon:fam15h_power Use div64_u64() for 64-bit divisor

PATCH In review SeungJu Cheon 3 participants 3 messages Dec 30, 2025 – Dec 31, 2025

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

PATCH 1/1 hwmon:fam15h_power Use div64_u64() for 64-bit divisor

tdelta is u64, but do_div() truncates the divisor to 32 bits. Use div64_u64() to handle the full 64-bit divisor correctly.

---
 drivers/hwmon/fam15h_power.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/hwmon/fam15h_power.c +1 -1
index 8ecebea53651..5e3692606516 100644
@@ -241,7 +241,7 @@ static ssize_t power1_average_show(struct device *dev,
 		}
 		tdelta = data->cpu_sw_pwr_ptsc[cu] - prev_ptsc[cu];
 		jdelta[cu] *= data->cpu_pwr_sample_ratio * 1000;
-		do_div(jdelta[cu], tdelta);
+		jdelta[cu] = div64_u64(jdelta[cu], tdelta);
 
 		/* the unit is microWatt */
 		avg_acc += jdelta[cu];
Signature · 1 line
2.52.0
#1 lore raw
Quoted 2 lines · On Tue, 30 Dec 2025 21:09:59 +0900 SeungJu Cheon <[email protected]> wrote:

tdelta is u64, but do_div() truncates the divisor to 32 bits. Use div64_u64() to handle the full 64-bit divisor correctly.

Looking at the code I think that tdelta is a time interval and will always fit in 32bits - so the code is probably fine.

Also I can't see anything that requires jdelta[] be an array. Neither can I see the justification for MAX_CUS being 8.

Quoted 11 lines
---
 drivers/hwmon/fam15h_power.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/hwmon/fam15h_power.c +0 -0
index 8ecebea53651..5e3692606516 100644

drivers/hwmon/fam15h_power.c static ssize_t power1_average_show(struct device *dev, · line 247

Quoted 8 lines
drivers/hwmon/fam15h_power.c +1 -1
@@ -241,7 +241,7 @@ static ssize_t power1_average_show(struct device *dev,
 		}
 		tdelta = data->cpu_sw_pwr_ptsc[cu] - prev_ptsc[cu];
 		jdelta[cu] *= data->cpu_pwr_sample_ratio * 1000;
-		do_div(jdelta[cu], tdelta);
+		jdelta[cu] = div64_u64(jdelta[cu], tdelta);
 
 		/* the unit is microWatt */
 		avg_acc += jdelta[cu];

That could be:

		jdelta *= data->cpu_pwr_sample_ratio * 1000;
		avg_acc += div64_u64(jdelta, tdelta);

(or even 1 line...)

David

maintainer
#2 lore raw
Quoted 9 lines · David Laight
Quoted 2 lines · On Tue, 30 Dec 2025 21:09:59 +0900 SeungJu Cheon <[email protected]> wrote:

tdelta is u64, but do_div() truncates the divisor to 32 bits. Use div64_u64() to handle the full 64-bit divisor correctly.

Looking at the code I think that tdelta is a time interval and will always fit in 32bits - so the code is probably fine.

Agreed.

Quoted 3 lines

Also I can't see anything that requires jdelta[] be an array. Neither can I see the justification for MAX_CUS being 8.

Also agree.

Guenter

Quoted 28 lines
Quoted 11 lines
---
 drivers/hwmon/fam15h_power.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/hwmon/fam15h_power.c +0 -0
index 8ecebea53651..5e3692606516 100644

drivers/hwmon/fam15h_power.c static ssize_t power1_average_show(struct device *dev, · line 247

Quoted 8 lines
drivers/hwmon/fam15h_power.c +1 -1
@@ -241,7 +241,7 @@ static ssize_t power1_average_show(struct device *dev,
 		}
 		tdelta = data->cpu_sw_pwr_ptsc[cu] - prev_ptsc[cu];
 		jdelta[cu] *= data->cpu_pwr_sample_ratio * 1000;
-		do_div(jdelta[cu], tdelta);
+		jdelta[cu] = div64_u64(jdelta[cu], tdelta);
 
 		/* the unit is microWatt */
 		avg_acc += jdelta[cu];

That could be:

		jdelta *= data->cpu_pwr_sample_ratio * 1000;
		avg_acc += div64_u64(jdelta, tdelta);

(or even 1 line...)

David