Skip to contentSkip to conversation

powercap: intel_rapl: Sign-extend the PMU delta on counter wraparound

PATCH Applied Li Yifan 4 participants 4 messages Aug 14 – Aug 20 Power management core

lore
Share & tools
lore

Li Yifan

author
#0 lore raw

PATCH 1/1 powercap: intel_rapl: Sign-extend the PMU delta on counter wraparound

From: "Li, Yifan" <[email protected]>

The RAPL PMU misreports energy when the hardware energy counter overflows and wraps back to zero. perf event counts are defined to increase monotonically, but a single wraparound makes the PMU event count jump backwards by nearly the full counter range, and consumers that take the difference of two reads in unsigned arithmetic then underflow and report an absurd value.

On a Panther Lake system (energy unit 61.035 uJ, counter range 262144 J) the package counter wraps every ~2.9 hours at 25 W, and turbostat prints one bogus sample per wraparound, per domain:

  PkgTmp  PkgWatt         CorWatt GFXWatt RAMWatt SysWatt
  44      24.97           16.30   3.90    1.87    2145386370.35
  43      2145240612.10   16.13   4.02    1.91    40.46

The RAPL energy counters are 32-bit wide on every register interface:
MSR, MMIO and TPMI all describe ENERGY_COUNTER with a GENMASK(31, 0) mask. rapl_read_data_raw() applies that mask, so event_read_counter() returns the counter zero-extended in a u64.

rapl_event_update() then computes

	delta = new_raw_count - prev_raw_count;

without reducing the result modulo 2^32. While the counter does not wrap this is correct, but once the hardware counter wraps, new_raw_count < prev_raw_count and delta becomes (true_delta - 2^32), a large negative value. Declaring delta as s64 only makes that value representable; it does not correct it. That bogus delta is scaled and added to event->count, which is where the backwards jump comes from.

Fix it the way arch/x86/events/rapl.c has done since the RAPL PMU was first introduced: shift both values up so that the 64-bit subtraction reduces modulo 2^32, then shift the difference back down with an arithmetic shift to sign-extend it.

This is correct as long as at most one wraparound happens between two updates, which the existing overflow hrtimer already guarantees: its period is half of the counter range at the 200 W reference used in rapl_package_add_pmu_locked().

The problem has been present since the powercap RAPL PMU was added, but only affected TPMI RAPL until commit 748d6ba43afd ("powercap:
intel_rapl: Enable MSR-based RAPL PMU support") routed MSR RAPL through the same PMU, which exposed it on client platforms such as Panther Lake.

Fixes 575024a8aa7c ("powercap: intel_rapl: Introduce APIs for PMU support")
---
 drivers/powercap/intel_rapl_common.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
drivers/powercap/intel_rapl_common.c +10 -1
index 1006d183d508..6b7d11a0abc5 100644
@@ -32,6 +32,9 @@
 
 #define ENERGY_STATUS_MASK		GENMASK(31, 0)
 
+/* Width of the RAPL energy counters, see the *_ENERGY_STATUS_MASK defines */
+#define RAPL_CNTR_WIDTH			32
+
 #define POWER_UNIT_OFFSET		0x00
 #define POWER_UNIT_MASK			GENMASK(3, 0)
 
@@ -1227,6 +1230,7 @@ static u64 rapl_event_update(struct perf_event *event)
 	struct rapl_package_pmu_data *data = event_to_pmu_data(event);
 	u64 prev_raw_count, new_raw_count;
 	s64 delta, sdelta;
+	int shift = 64 - RAPL_CNTR_WIDTH;
 
 	/*
 	 * Follow the generic code to drain hwc->prev_count.
@@ -1243,8 +1247,13 @@ static u64 rapl_event_update(struct perf_event *event)
 	 * Now we have the new raw value and have updated the prev
 	 * timestamp already. We can now calculate the elapsed delta
 	 * (event-)time and add that to the generic event.
+	 *
+	 * Careful, the counter is narrower than u64 and is not
+	 * sign-extended above its physical width.  Shift both values up
+	 * so that the subtraction wraps, then shift the result back down.
 	 */
-	delta = new_raw_count - prev_raw_count;
+	delta = (new_raw_count << shift) - (prev_raw_count << shift);
+	delta >>= shift;
 
 	/*
 	 * Scale delta to smallest unit (2^-32)

base-commit: f5bbbfec59b4e2fb7520a91de3df8a6174325d6a

Signature · 1 line
2.43.0
Acked-by on v1 1/1
#1 lore raw
Quoted 53 lines · Li Yifan

From: "Li, Yifan" <[email protected]>

The RAPL PMU misreports energy when the hardware energy counter overflows and wraps back to zero.  perf event counts are defined to increase monotonically, but a single wraparound makes the PMU event count jump backwards by nearly the full counter range, and consumers that take the difference of two reads in unsigned arithmetic then underflow and report an absurd value.

On a Panther Lake system (energy unit 61.035 uJ, counter range 262144 J) the package counter wraps every ~2.9 hours at 25 W, and turbostat prints one bogus sample per wraparound, per domain:

  PkgTmp  PkgWatt         CorWatt GFXWatt RAMWatt SysWatt 44      24.97           16.30   3.90    1.87    2145386370.35 43      2145240612.10   16.13   4.02    1.91    40.46

The RAPL energy counters are 32-bit wide on every register interface:
MSR, MMIO and TPMI all describe ENERGY_COUNTER with a GENMASK(31, 0) mask.  rapl_read_data_raw() applies that mask, so event_read_counter()
returns the counter zero-extended in a u64.

rapl_event_update() then computes

	delta = new_raw_count - prev_raw_count;

without reducing the result modulo 2^32.  While the counter does not wrap this is correct, but once the hardware counter wraps, new_raw_count < prev_raw_count and delta becomes (true_delta - 2^32), a large negative value.  Declaring delta as s64 only makes that value representable; it does not correct it.  That bogus delta is scaled and
added to event->count, which is where the backwards jump comes from.

Fix it the way arch/x86/events/rapl.c has done since the RAPL PMU was first introduced: shift both values up so that the 64-bit subtraction reduces modulo 2^32, then shift the difference back down with an arithmetic shift to sign-extend it.

This is correct as long as at most one wraparound happens between two updates, which the existing overflow hrtimer already guarantees: its period is half of the counter range at the 200 W reference used in rapl_package_add_pmu_locked().

The problem has been present since the powercap RAPL PMU was added, but
only affected TPMI RAPL until commit 748d6ba43afd ("powercap:
intel_rapl: Enable MSR-based RAPL PMU support") routed MSR RAPL through
the same PMU, which exposed it on client platforms such as Panther Lake.

Quoted 6 lines
Fixes 575024a8aa7c ("powercap: intel_rapl: Introduce APIs for PMU

support")

Quoted 51 lines

---
 drivers/powercap/intel_rapl_common.c | 11 ++++++++++-

 1 file changed, 10 insertions(+), 1 deletion(-)
drivers/powercap/intel_rapl_common.c +0 -0
index 1006d183d508..6b7d11a0abc5 100644
@@ -32,6 +32,9 @@


 #define ENERGY_STATUS_MASK GENMASK(31, 0)

+/* Width of the RAPL energy counters, see the *_ENERGY_STATUS_MASK defines */

drivers/powercap/intel_rapl_common.c +2 -0
+#define RAPL_CNTR_WIDTH			32
+

 #define POWER_UNIT_OFFSET 0x00
 #define POWER_UNIT_MASK GENMASK(3, 0)

drivers/powercap/intel_rapl_common.c +0 -0
@@ -1227,6 +1230,7 @@ static u64 rapl_event_update(struct perf_event
 *event)

  struct rapl_package_pmu_data *data =
event_to_pmu_data(event);
  u64 prev_raw_count, new_raw_count;
  s64 delta, sdelta;

drivers/powercap/intel_rapl_common.c +1 -0
+	int shift = 64 - RAPL_CNTR_WIDTH;


  /*
  * Follow the generic code to drain hwc->prev_count.

drivers/powercap/intel_rapl_common.c +3 -0
@@ -1243,8 +1247,13 @@ static u64 rapl_event_update(struct perf_event
 *event)
  	 * Now we have the new raw value and have updated the prev
  	 * timestamp already. We can now calculate the elapsed delta
  	 * (event-)time and add that to the generic event.
+	 *
+	 * Careful, the counter is narrower than u64 and is not
+	 * sign-extended above its physical width.  Shift both

values up

drivers/powercap/intel_rapl_common.c +1 -0
+	 * so that the subtraction wraps, then shift the result back

down.
  */

drivers/powercap/intel_rapl_common.c +1 -1
-	delta = new_raw_count - prev_raw_count;
+	delta = (new_raw_count << shift) - (prev_raw_count <<

shift);

drivers/powercap/intel_rapl_common.c +1 -0
+	delta >>= shift;


  /*
  * Scale delta to smallest unit (2^-32)

base-commit: f5bbbfec59b4e2fb7520a91de3df8a6174325d6a

Reviewed-by on v1 1/1
#2 lore raw
In reply to Li Yifan
Quoted 48 lines · Li Yifan

From: "Li, Yifan" <[email protected]>

The RAPL PMU misreports energy when the hardware energy counter overflows and wraps back to zero. perf event counts are defined to increase monotonically, but a single wraparound makes the PMU event count jump backwards by nearly the full counter range, and consumers that take the difference of two reads in unsigned arithmetic then underflow and report an absurd value.

On a Panther Lake system (energy unit 61.035 uJ, counter range 262144 J) the package counter wraps every ~2.9 hours at 25 W, and turbostat prints one bogus sample per wraparound, per domain:

  PkgTmp  PkgWatt         CorWatt GFXWatt RAMWatt SysWatt
  44      24.97           16.30   3.90    1.87    2145386370.35
  43      2145240612.10   16.13   4.02    1.91    40.46

The RAPL energy counters are 32-bit wide on every register interface:
MSR, MMIO and TPMI all describe ENERGY_COUNTER with a GENMASK(31, 0) mask. rapl_read_data_raw() applies that mask, so event_read_counter() returns the counter zero-extended in a u64.

rapl_event_update() then computes

	delta = new_raw_count - prev_raw_count;

without reducing the result modulo 2^32. While the counter does not wrap this is correct, but once the hardware counter wraps, new_raw_count < prev_raw_count and delta becomes (true_delta - 2^32), a large negative value. Declaring delta as s64 only makes that value representable; it does not correct it. That bogus delta is scaled and added to event->count, which is where the backwards jump comes from.

Fix it the way arch/x86/events/rapl.c has done since the RAPL PMU was first introduced: shift both values up so that the 64-bit subtraction reduces modulo 2^32, then shift the difference back down with an arithmetic shift to sign-extend it.

This is correct as long as at most one wraparound happens between two updates, which the existing overflow hrtimer already guarantees: its period is half of the counter range at the 200 W reference used in rapl_package_add_pmu_locked().

The problem has been present since the powercap RAPL PMU was added, but only affected TPMI RAPL until commit 748d6ba43afd ("powercap:
intel_rapl: Enable MSR-based RAPL PMU support") routed MSR RAPL through the same PMU, which exposed it on client platforms such as Panther Lake.

Quoted 6 lines
Fixes 575024a8aa7c ("powercap: intel_rapl: Introduce APIs for PMU support")

---

Looks good to me.

Quoted 42 lines
 drivers/powercap/intel_rapl_common.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
drivers/powercap/intel_rapl_common.c +10 -1
index 1006d183d508..6b7d11a0abc5 100644
@@ -32,6 +32,9 @@
 
 #define ENERGY_STATUS_MASK		GENMASK(31, 0)
 
+/* Width of the RAPL energy counters, see the *_ENERGY_STATUS_MASK defines */
+#define RAPL_CNTR_WIDTH			32
+
 #define POWER_UNIT_OFFSET		0x00
 #define POWER_UNIT_MASK			GENMASK(3, 0)
 
@@ -1227,6 +1230,7 @@ static u64 rapl_event_update(struct perf_event *event)
 	struct rapl_package_pmu_data *data = event_to_pmu_data(event);
 	u64 prev_raw_count, new_raw_count;
 	s64 delta, sdelta;
+	int shift = 64 - RAPL_CNTR_WIDTH;
 
 	/*
 	 * Follow the generic code to drain hwc->prev_count.
@@ -1243,8 +1247,13 @@ static u64 rapl_event_update(struct perf_event *event)
 	 * Now we have the new raw value and have updated the prev
 	 * timestamp already. We can now calculate the elapsed delta
 	 * (event-)time and add that to the generic event.
+	 *
+	 * Careful, the counter is narrower than u64 and is not
+	 * sign-extended above its physical width.  Shift both values up
+	 * so that the subtraction wraps, then shift the result back down.
 	 */
-	delta = new_raw_count - prev_raw_count;
+	delta = (new_raw_count << shift) - (prev_raw_count << shift);
+	delta >>= shift;
 
 	/*
 	 * Scale delta to smallest unit (2^-32)

base-commit: f5bbbfec59b4e2fb7520a91de3df8a6174325d6a

Signature · 2 lines
Sathyanarayanan Kuppuswamy
Linux Kernel Developer
maintainer Applied
#3 lore raw
Quoted 59 lines · Kuppuswamy Sathyanarayanan
Quoted 48 lines · Li Yifan

From: "Li, Yifan" <[email protected]>

The RAPL PMU misreports energy when the hardware energy counter overflows and wraps back to zero. perf event counts are defined to increase monotonically, but a single wraparound makes the PMU event count jump backwards by nearly the full counter range, and consumers that take the difference of two reads in unsigned arithmetic then underflow and report an absurd value.

On a Panther Lake system (energy unit 61.035 uJ, counter range 262144 J) the package counter wraps every ~2.9 hours at 25 W, and turbostat prints one bogus sample per wraparound, per domain:

  PkgTmp  PkgWatt         CorWatt GFXWatt RAMWatt SysWatt
  44      24.97           16.30   3.90    1.87    2145386370.35
  43      2145240612.10   16.13   4.02    1.91    40.46

The RAPL energy counters are 32-bit wide on every register interface:
MSR, MMIO and TPMI all describe ENERGY_COUNTER with a GENMASK(31, 0) mask. rapl_read_data_raw() applies that mask, so event_read_counter() returns the counter zero-extended in a u64.

rapl_event_update() then computes

      delta = new_raw_count - prev_raw_count;

without reducing the result modulo 2^32. While the counter does not wrap this is correct, but once the hardware counter wraps, new_raw_count < prev_raw_count and delta becomes (true_delta - 2^32), a large negative value. Declaring delta as s64 only makes that value representable; it does not correct it. That bogus delta is scaled and added to event->count, which is where the backwards jump comes from.

Fix it the way arch/x86/events/rapl.c has done since the RAPL PMU was first introduced: shift both values up so that the 64-bit subtraction reduces modulo 2^32, then shift the difference back down with an arithmetic shift to sign-extend it.

This is correct as long as at most one wraparound happens between two updates, which the existing overflow hrtimer already guarantees: its period is half of the counter range at the 200 W reference used in rapl_package_add_pmu_locked().

The problem has been present since the powercap RAPL PMU was added, but only affected TPMI RAPL until commit 748d6ba43afd ("powercap:
intel_rapl: Enable MSR-based RAPL PMU support") routed MSR RAPL through the same PMU, which exposed it on client platforms such as Panther Lake.

Quoted 6 lines
Fixes 575024a8aa7c ("powercap: intel_rapl: Introduce APIs for PMU support")

---

Quoted 3 lines

Looks good to me.

Applied as 7.3-rc material, thanks!

Quoted 47 lines
Quoted 41 lines
 drivers/powercap/intel_rapl_common.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
drivers/powercap/intel_rapl_common.c +10 -1
index 1006d183d508..6b7d11a0abc5 100644
@@ -32,6 +32,9 @@
 
 #define ENERGY_STATUS_MASK           GENMASK(31, 0)
 
+/* Width of the RAPL energy counters, see the *_ENERGY_STATUS_MASK defines */
+#define RAPL_CNTR_WIDTH                      32
+
 #define POWER_UNIT_OFFSET            0x00
 #define POWER_UNIT_MASK                      GENMASK(3, 0)
 
@@ -1227,6 +1230,7 @@ static u64 rapl_event_update(struct perf_event *event)
      struct rapl_package_pmu_data *data = event_to_pmu_data(event);
      u64 prev_raw_count, new_raw_count;
      s64 delta, sdelta;
+     int shift = 64 - RAPL_CNTR_WIDTH;
 
      /*
       * Follow the generic code to drain hwc->prev_count.
@@ -1243,8 +1247,13 @@ static u64 rapl_event_update(struct perf_event *event)
       * Now we have the new raw value and have updated the prev
       * timestamp already. We can now calculate the elapsed delta
       * (event-)time and add that to the generic event.
+      *
+      * Careful, the counter is narrower than u64 and is not
+      * sign-extended above its physical width.  Shift both values up
+      * so that the subtraction wraps, then shift the result back down.
       */
-     delta = new_raw_count - prev_raw_count;
+     delta = (new_raw_count << shift) - (prev_raw_count << shift);
+     delta >>= shift;
 
      /*
       * Scale delta to smallest unit (2^-32)
Quoted 1 line

base-commit: f5bbbfec59b4e2fb7520a91de3df8a6174325d6a

--
Sathyanarayanan Kuppuswamy
Linux Kernel Developer