Skip to contentSkip to conversation

powercap/intel_rapl: Fix handling for large time window

PATCH Superseded Zhang Rui 2 participants 3 messages Feb 2, 2023 – Feb 11, 2023 Power management core

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Zhang Rui

author
#0 lore raw

PATCH 1/1 powercap/intel_rapl: Fix handling for large time window

When setting the power limit time window, software updates the 'y' bits and 'f' bits in the power limit register, and the value hardware takes follows the formula below

	Time window = 2 ^ y * (1 + f / 4) * Time_Unit

When handling large time window input from userspace, using left shifting breaks in two cases,
1. when ilog2(value) is bigger than 31, in expression "1 << y", left shifting by more than 31 bits has undefined behavior. This breaks 'y'. For example, on an Alderlake platform, "1 << 32" returns 1.
2. when ilog2(value) equals 31, "1 << 31" returns negative value because '1' is recognized as signed int. And this breaks 'f'.

Given that 'y' has 5 bits and hardware can never take a value larger than 31, fix the first problem by clamp the time window to the maximum possible value that the hardware can take.

Fix the second problem by using unsigned bit left shift.

Note that hardware has its own maximum time window limitation, which may be lower than the time window value retrieved from the power limit register. When this happens, hardware clamps the input to its maximum time window limitation. That is why a software clamp is preferred to handle the problem on hand.

---
 drivers/powercap/intel_rapl_common.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/powercap/intel_rapl_common.c +6 -1
index 26d00b1853b4..8b30e5259d3b 100644
@@ -999,7 +999,12 @@ static u64 rapl_compute_time_window_core(struct rapl_package *rp, u64 value,
 
 		do_div(value, rp->time_unit);
 		y = ilog2(value);
-		f = div64_u64(4 * (value - (1 << y)), 1 << y);
+		if (y > 0x1f) {
+			pr_warn("%s: time window too large, clamped\n", rp->name);
+			return 0x7f;
+		}
+
+		f = div64_u64(4 * (value - (1ULL << y)), 1ULL << y);
 		value = (y & 0x1f) | ((f & 0x3) << 5);
 	}
 	return value;
Signature · 1 line
2.25.1
#1 lore raw
Quoted 37 lines · Zhang Rui

When setting the power limit time window, software updates the 'y' bits and 'f' bits in the power limit register, and the value hardware takes follows the formula below

        Time window = 2 ^ y * (1 + f / 4) * Time_Unit

When handling large time window input from userspace, using left shifting breaks in two cases,
1. when ilog2(value) is bigger than 31, in expression "1 << y", left shifting by more than 31 bits has undefined behavior. This breaks 'y'. For example, on an Alderlake platform, "1 << 32" returns 1.
2. when ilog2(value) equals 31, "1 << 31" returns negative value because '1' is recognized as signed int. And this breaks 'f'.

Given that 'y' has 5 bits and hardware can never take a value larger than 31, fix the first problem by clamp the time window to the maximum possible value that the hardware can take.

Fix the second problem by using unsigned bit left shift.

Note that hardware has its own maximum time window limitation, which may be lower than the time window value retrieved from the power limit register. When this happens, hardware clamps the input to its maximum time window limitation. That is why a software clamp is preferred to handle the problem on hand.

---
 drivers/powercap/intel_rapl_common.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/powercap/intel_rapl_common.c +0 -0
index 26d00b1853b4..8b30e5259d3b 100644

drivers/powercap/intel_rapl_common.c static u64 rapl_compute_time_window_core(struct rapl_package *rp, u64 value, · line 1003

Quoted 6 lines
drivers/powercap/intel_rapl_common.c +2 -1
@@ -999,7 +999,12 @@ static u64 rapl_compute_time_window_core(struct rapl_package *rp, u64 value,
 
                do_div(value, rp->time_unit);
                y = ilog2(value);
-               f = div64_u64(4 * (value - (1 << y)), 1 << y);
+               if (y > 0x1f) {
+                       pr_warn("%s: time window too large, clamped\n", rp->name);

IIUC this happens when user space provides a value that is too large. Why do you want to log a kernel warning in that case?

drivers/powercap/intel_rapl_common.c static u64 rapl_compute_time_window_core(struct rapl_package *rp, u64 value, · line 1004

Quoted 1 line
drivers/powercap/intel_rapl_common.c +1 -0
+                       return 0x7f;

Because the target hardware field has 7 bits, the function will return all ones if the exponent is too large. It would be good to put a comment stating this here.

Quoted 7 lines
drivers/powercap/intel_rapl_common.c +3 -1
+               }
+
+               f = div64_u64(4 * (value - (1ULL << y)), 1ULL << y);
                value = (y & 0x1f) | ((f & 0x3) << 5);
        }
        return value;
--
author
#2 lore raw
Quoted 50 lines · Rafael J. Wysocki
Quoted 49 lines · Zhang Rui

When setting the power limit time window, software updates the 'y' bits
and 'f' bits in the power limit register, and the value hardware takes
follows the formula below

        Time window = 2 ^ y * (1 + f / 4) * Time_Unit

When handling large time window input from userspace, using left shifting breaks in two cases,
1. when ilog2(value) is bigger than 31, in expression "1 << y", left
shifting by more than 31 bits has undefined behavior. This
breaks
'y'. For example, on an Alderlake platform, "1 << 32" returns 1.
2. when ilog2(value) equals 31, "1 << 31" returns negative value because '1' is recognized as signed int. And this breaks 'f'.

Given that 'y' has 5 bits and hardware can never take a value larger
than 31, fix the first problem by clamp the time window to the maximum
possible value that the hardware can take.

Fix the second problem by using unsigned bit left shift.

Note that hardware has its own maximum time window limitation, which
may be lower than the time window value retrieved from the power limit
register. When this happens, hardware clamps the input to its maximum
time window limitation. That is why a software clamp is preferred to
handle the problem on hand.

---
 drivers/powercap/intel_rapl_common.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/powercap/intel_rapl_common.c +0 -0
index 26d00b1853b4..8b30e5259d3b 100644
@@ -999,7 +999,12 @@ static u64
 rapl_compute_time_window_core(struct rapl_package *rp, u64 value,
 
Quoted 10 lines

drivers/powercap/intel_rapl_common.c static u64 rapl_compute_time_window_core(struct rapl_package *rp, u64 value,

Quoted 6 lines
drivers/powercap/intel_rapl_common.c +2 -1
                do_div(value, rp->time_unit);
                y = ilog2(value);
-               f = div64_u64(4 * (value - (1 << y)), 1 << y);
+               if (y > 0x1f) {
+                       pr_warn("%s: time window too large,
 clamped\n", rp->name);

IIUC this happens when user space provides a value that is too large. Why do you want to log a kernel warning in that case?

Right.
I don't know any API for this purpose, so just removed the warning in patch v2.

Quoted 7 lines

drivers/powercap/intel_rapl_common.c static u64 rapl_compute_time_window_core(struct rapl_package *rp, u64 value, · line 1004

Quoted 1 line
drivers/powercap/intel_rapl_common.c +1 -0
+                       return 0x7f;

Because the target hardware field has 7 bits, the function will return
all ones if the exponent is too large. It would be good to put a comment stating this here.

sure.

thanks,
rui

Quoted 8 lines
Quoted 8 lines
drivers/powercap/intel_rapl_common.c +3 -0
+               }
+
+               f = div64_u64(4 * (value - (1ULL << y)), 1ULL <<
 y);
                value = (y & 0x1f) | ((f & 0x3) << 5);
        }
        return value;

--