[lvc-project] [PATCH] powercap: intel_rapl: use unsigned arithmetic in time window computation

Ваторопин Андрей a.vatoropin at crpt.ru
Fri Feb 13 09:29:16 MSK 2026


From: Andrey Vatoropin <a.vatoropin at crpt.ru>

In rapl_compute_time_window_core() the time window is calculated using the
variable "y". This variable is limited by the mask 0x1F, however the
result of the corner-case expression (1 << 0x1F) is 0x80000000 of signed
integer type. Actually it's undefined in standard C language but the
kernel is compiled with -fno-strict-overflow (-fwrapv) flag which does the
trick.

Eventually the unexpected sign extension is possible when the result of
type int is expanded to u64, like 0x80000000 -> 0xFFFFFFFF80000000 which
leads to incorrect arithmetic.

Avoid sign extension by casting the left operand of the shift to the
unsigned type before performing the shift.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes: 3c2c08454ce9 ("powercap / RAPL: handle atom and core differences")
Cc: stable at vger.kernel.org
Signed-off-by: Andrey Vatoropin <a.vatoropin at crpt.ru>
---
 drivers/powercap/intel_rapl_common.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/powercap/intel_rapl_common.c b/drivers/powercap/intel_rapl_common.c
index 3ff6da3bf4e6..0f2d3d1a00c5 100644
--- a/drivers/powercap/intel_rapl_common.c
+++ b/drivers/powercap/intel_rapl_common.c
@@ -1107,7 +1107,7 @@ static u64 rapl_compute_time_window_core(struct rapl_domain *rd, u64 value,
 	if (!to_raw) {
 		f = (value & 0x60) >> 5;
 		y = value & 0x1f;
-		value = (1 << y) * (4 + f) * rd->time_unit / 4;
+		value = (1U << y) * (4 + f) * rd->time_unit / 4;
 	} else {
 		if (value < rd->time_unit)
 			return 0;
-- 
2.43.0


More information about the lvc-project mailing list