Skip to contentSkip to conversation

Fix pmu test errors on SRF/CWF

PATCH Superseded Dapeng Mi 2 participants 8 messages Jul 15, 2025 – Jul 16, 2025 KVM

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Dapeng Mi

author
#0 lore raw

cover Fix pmu test errors on SRF/CWF

This patchset fixes the pmu test errors on Atom server like Sierra Forest (SRF) and Clearwater Forest (CWF).

On Intel Atom platforms, the PMU events "Instruction Retired" or "Branch Instruction Retired" may be overcounted for some certain instructions, like FAR CALL/JMP, RETF, IRET, VMENTRY/VMEXIT/VMPTRLD and complex SGX/SMX/CSTATE instructions/flows[1].

In details, for the Atom platforms before Sierra Forest (including Sierra Forest), Both 2 events "Instruction Retired" and "Branch Instruction Retired" would be overcounted on these certain instructions, but for Clearwater Forest only "Instruction Retired" event is overcounted on these instructions.

As the overcount issue, pmu test would fail to validate the precise count for these 2 events on SRF and CWF. Patches 1-3/5 detects if the platform has this overcount issue, if so relax the precise count validation for these 2 events.

Besides it looks more LLC references are needed on SRF/CWF, so adjust the "LLC references" event count range.

Tests:
* pmu test passes on Intel GNR/SRF/CWF platforms.

Ref:
[1]https://edc.intel.com/content/www/us/en/design/products-and-solutions/processors-and-chipsets/sierra-forest/xeon-6700-series-processor-with-e-cores-specification-update/errata-details

dongsheng (5):
x86/pmu: Add helper to detect Intel overcount issues x86/pmu: Relax precise count validation for Intel overcounted
platforms
x86/pmu: Fix incorrect masking of fixed counters
x86/pmu: Handle instruction overcount issue in overflow test x86/pmu: Expand "llc references" upper limit for broader compatibility

 lib/x86/processor.h | 17 +++++++++
 x86/pmu.c           | 93 +++++++++++++++++++++++++++++++++++++++------
 2 files changed, 98 insertions(+), 12 deletions(-)

base-commit: 525bdb5d65d51a367341f471eb1bcd505d73c51f

Signature · 1 line
2.43.0
author
#1 lore raw

PATCH 1/5 x86/pmu: Add helper to detect Intel overcount issues

From: dongsheng <[email protected]>

For Intel Atom CPUs, the PMU events "Instruction Retired" or "Branch Instruction Retired" may be overcounted for some certain instructions, like FAR CALL/JMP, RETF, IRET, VMENTRY/VMEXIT/VMPTRLD and complex SGX/SMX/CSTATE instructions/flows.

The detailed information can be found in the errata (section SRF7):
https://edc.intel.com/content/www/us/en/design/products-and-solutions/processors-and-chipsets/sierra-forest/xeon-6700-series-processor-with-e-cores-specification-update/errata-details/

For the Atom platforms before Sierra Forest (including Sierra Forest), Both 2 events "Instruction Retired" and "Branch Instruction Retired" would be overcounted on these certain instructions, but for Clearwater Forest only "Instruction Retired" event is overcounted on these instructions.

So add a helper detect_inst_overcount_flags() to detect whether the platform has the overcount issue and the later patches would relax the precise count check by leveraging the gotten overcount flags from this helper.

[Rewrite comments and commit message - Dapeng]
---
 lib/x86/processor.h | 17 ++++++++++++++++
 x86/pmu.c           | 47 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 64 insertions(+)
lib/x86/processor.h +17 -0
index 62f3d578..3f475c21 100644
@@ -1188,4 +1188,21 @@ static inline bool is_lam_u57_enabled(void)
 	return !!(read_cr3() & X86_CR3_LAM_U57);
 }
 
+static inline u32 x86_family(u32 eax)
+{
+	u32 x86;
+
+	x86 = (eax >> 8) & 0xf;
+
+	if (x86 == 0xf)
+		x86 += (eax >> 20) & 0xff;
+
+	return x86;
+}
+
+static inline u32 x86_model(u32 eax)
+{
+	return ((eax >> 12) & 0xf0) | ((eax >> 4) & 0x0f);
+}
+
 #endif
x86/pmu.c +47 -0

65 lines not on this page · raw message

Signature · 1 line
2.43.0
#2 lore raw

Re: [kvm-unit-tests patch 1/5] x86/pmu: Add helper to detect Intel overcount issues

Quoted 43 lines · Dapeng Mi

From: dongsheng <[email protected]>

For Intel Atom CPUs, the PMU events "Instruction Retired" or "Branch Instruction Retired" may be overcounted for some certain instructions, like FAR CALL/JMP, RETF, IRET, VMENTRY/VMEXIT/VMPTRLD and complex SGX/SMX/CSTATE instructions/flows.

The detailed information can be found in the errata (section SRF7):
https://edc.intel.com/content/www/us/en/design/products-and-solutions/processors-and-chipsets/sierra-forest/xeon-6700-series-processor-with-e-cores-specification-update/errata-details/

For the Atom platforms before Sierra Forest (including Sierra Forest), Both 2 events "Instruction Retired" and "Branch Instruction Retired" would be overcounted on these certain instructions, but for Clearwater Forest only "Instruction Retired" event is overcounted on these instructions.

So add a helper detect_inst_overcount_flags() to detect whether the platform has the overcount issue and the later patches would relax the precise count check by leveraging the gotten overcount flags from this helper.

[Rewrite comments and commit message - Dapeng]
---
  lib/x86/processor.h | 17 ++++++++++++++++
  x86/pmu.c           | 47 +++++++++++++++++++++++++++++++++++++++++++++
  2 files changed, 64 insertions(+)
lib/x86/processor.h +6 -0
index 62f3d578..3f475c21 100644
@@ -1188,4 +1188,21 @@ static inline bool is_lam_u57_enabled(void)
  	return !!(read_cr3() & X86_CR3_LAM_U57);
  }
 
+static inline u32 x86_family(u32 eax)
+{
+	u32 x86;
+
+	x86 = (eax >> 8) & 0xf;
+

lib/x86/processor.h static inline bool is_lam_u57_enabled(void) · line 1206

Quoted 10 lines
lib/x86/processor.h +10 -0
+	if (x86 == 0xf)
+		x86 += (eax >> 20) & 0xff;
+
+	return x86;
+}
+
+static inline u32 x86_model(u32 eax)
+{
+	return ((eax >> 12) & 0xf0) | ((eax >> 4) & 0x0f);
+}

It seems to copy the implementation of kvm selftest.

I need to point it out that it's not correct (because I fixed the similar issue on QEMU recently).

We cannot count Extended Model ID unconditionally. Intel counts Extended Model when (base) Family is 0x6 or 0xF, while AMD counts EXtended Model when (base) Family is 0xF.

You can refer to kernel's x86_model() in arch/x86/lib/cpu.c, while it optimizes the condition to "family >= 0x6", which seems to have the assumption that Intel doesn't have processor with family ID from 7 to 0xe and AMD doesn't have processor with family ID from 6 to 0xe.

author
#3 lore raw

Re: [kvm-unit-tests patch 1/5] x86/pmu: Add helper to detect Intel overcount issues

Quoted 59 lines · Xiaoyao Li
Quoted 43 lines · Dapeng Mi

From: dongsheng <[email protected]>

For Intel Atom CPUs, the PMU events "Instruction Retired" or "Branch Instruction Retired" may be overcounted for some certain instructions, like FAR CALL/JMP, RETF, IRET, VMENTRY/VMEXIT/VMPTRLD and complex SGX/SMX/CSTATE instructions/flows.

The detailed information can be found in the errata (section SRF7):
https://edc.intel.com/content/www/us/en/design/products-and-solutions/processors-and-chipsets/sierra-forest/xeon-6700-series-processor-with-e-cores-specification-update/errata-details/

For the Atom platforms before Sierra Forest (including Sierra Forest), Both 2 events "Instruction Retired" and "Branch Instruction Retired" would be overcounted on these certain instructions, but for Clearwater Forest only "Instruction Retired" event is overcounted on these instructions.

So add a helper detect_inst_overcount_flags() to detect whether the platform has the overcount issue and the later patches would relax the precise count check by leveraging the gotten overcount flags from this helper.

[Rewrite comments and commit message - Dapeng]
---
  lib/x86/processor.h | 17 ++++++++++++++++
  x86/pmu.c           | 47 +++++++++++++++++++++++++++++++++++++++++++++
  2 files changed, 64 insertions(+)
lib/x86/processor.h +6 -0
index 62f3d578..3f475c21 100644
@@ -1188,4 +1188,21 @@ static inline bool is_lam_u57_enabled(void)
  	return !!(read_cr3() & X86_CR3_LAM_U57);
  }
 
+static inline u32 x86_family(u32 eax)
+{
+	u32 x86;
+
+	x86 = (eax >> 8) & 0xf;
+

lib/x86/processor.h static inline bool is_lam_u57_enabled(void) · line 1206

Quoted 10 lines
lib/x86/processor.h +10 -0
+	if (x86 == 0xf)
+		x86 += (eax >> 20) & 0xff;
+
+	return x86;
+}
+
+static inline u32 x86_model(u32 eax)
+{
+	return ((eax >> 12) & 0xf0) | ((eax >> 4) & 0x0f);
+}

It seems to copy the implementation of kvm selftest.

I need to point it out that it's not correct (because I fixed the similar issue on QEMU recently).

Quoted 8 lines

We cannot count Extended Model ID unconditionally. Intel counts Extended Model when (base) Family is 0x6 or 0xF, while AMD counts EXtended Model when (base) Family is 0xF.

You can refer to kernel's x86_model() in arch/x86/lib/cpu.c, while it optimizes the condition to "family >= 0x6", which seems to have the assumption that Intel doesn't have processor with family ID from 7 to 0xe and AMD doesn't have processor with family ID from 6 to 0xe.

Sure. Thanks for reviewing.

author
#4 lore raw

PATCH 2/5 x86/pmu: Relax precise count validation for Intel overcounted platforms

In reply to Dapeng Mi

From: dongsheng <[email protected]>

As the VM-Exit/VM-Entry overcount issue on Intel Atom platforms, there is no way to validate the precise count for "instructions" and "branches" events on these overcounted Atom platforms. Thus relax the precise count validation on these overcounted platforms.

---
 x86/pmu.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
x86/pmu.c +9 -4
index 87365aff..04946d10 100644
@@ -237,10 +237,15 @@ static void adjust_events_range(struct pmu_event *gp_events,
 	 * occur while running the measured code, e.g. if the host takes IRQs.
 	 */
 	if (pmu.is_intel && this_cpu_has_perf_global_ctrl()) {
-		gp_events[instruction_idx].min = LOOP_INSNS;
-		gp_events[instruction_idx].max = LOOP_INSNS;
-		gp_events[branch_idx].min = LOOP_BRANCHES;
-		gp_events[branch_idx].max = LOOP_BRANCHES;
+		if (!(intel_inst_overcount_flags & INST_RETIRED_OVERCOUNT)) {
+			gp_events[instruction_idx].min = LOOP_INSNS;
+			gp_events[instruction_idx].max = LOOP_INSNS;
+		}
+
+		if (!(intel_inst_overcount_flags & BR_RETIRED_OVERCOUNT)) {
+			gp_events[branch_idx].min = LOOP_BRANCHES;
+			gp_events[branch_idx].max = LOOP_BRANCHES;
+		}
 	}
 
 	/*
Signature · 1 line
2.43.0
author
#5 lore raw

PATCH 3/5 x86/pmu: Fix incorrect masking of fixed counters

In reply to Dapeng Mi

From: dongsheng <[email protected]>

The current implementation mistakenly limits the width of fixed counters to the width of GP counters. Corrects the logic to ensure fixed counters are properly masked according to their own width.

Opportunistically refine the GP counter bitwidth processing code.

---
 x86/pmu.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
x86/pmu.c +3 -5
index 04946d10..44c728a5 100644
@@ -556,18 +556,16 @@ static void check_counter_overflow(void)
 		int idx;
 
 		cnt.count = overflow_preset;
-		if (pmu_use_full_writes())
-			cnt.count &= (1ull << pmu.gp_counter_width) - 1;
-
 		if (i == pmu.nr_gp_counters) {
 			if (!pmu.is_intel)
 				break;
 
 			cnt.ctr = fixed_events[0].unit_sel;
-			cnt.count = measure_for_overflow(&cnt);
-			cnt.count &= (1ull << pmu.gp_counter_width) - 1;
+			cnt.count &= (1ull << pmu.fixed_counter_width) - 1;
 		} else {
 			cnt.ctr = MSR_GP_COUNTERx(i);
+			if (pmu_use_full_writes())
+				cnt.count &= (1ull << pmu.gp_counter_width) - 1;
 		}
 
 		if (i % 2)
Signature · 1 line
2.43.0
author
#6 lore raw

PATCH 4/5 x86/pmu: Handle instruction overcount issue in overflow test

In reply to Dapeng Mi

From: dongsheng <[email protected]>

During the execution of __measure(), VM exits (e.g., due to WRMSR/EXTERNAL_INTERRUPT) may occur. On systems affected by the instruction overcount issue, each VM-Exit/VM-Entry can erroneously increment the instruction count by one, leading to false failures in overflow tests.

To address this, the patch introduces a range-based validation in place of precise instruction count checks. Additionally, overflow_preset is now statically set to 1 - LOOP_INSNS, rather than being dynamically determined via measure_for_overflow().

These changes ensure consistent and predictable behavior aligned with the intended loop instruction count, while avoiding modifications to the subsequent status and status-clear testing logic.

The chosen validation range is empirically derived to maintain test reliability across hardware variations.

---
 x86/pmu.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)
x86/pmu.c +21 -2
index 44c728a5..c54c0988 100644
@@ -518,6 +518,21 @@ static void check_counters_many(void)
 
 static uint64_t measure_for_overflow(pmu_counter_t *cnt)
 {
+	/*
+	 * During the execution of __measure(), VM exits (e.g., due to
+	 * WRMSR/EXTERNAL_INTERRUPT) may occur. On systems affected by the
+	 * instruction overcount issue, each VM-Exit/VM-Entry can erroneously
+	 * increment the instruction count by one, leading to false failures
+	 * in overflow tests.
+	 *
+	 * To mitigate this, if the overcount issue is detected, we hardcode
+	 * the overflow preset to (1 - LOOP_INSNS) instead of calculating it
+	 * dynamically. This ensures that an overflow will reliably occur,
+	 * regardless of any overcounting caused by VM exits.
+	 */
+	if (intel_inst_overcount_flags & INST_RETIRED_OVERCOUNT)
+		return 1 - LOOP_INSNS;
+
 	__measure(cnt, 0);
 	/*
 	 * To generate overflow, i.e. roll over to '0', the initial count just
@@ -574,8 +589,12 @@ static void check_counter_overflow(void)
 			cnt.config &= ~EVNTSEL_INT;
 		idx = event_to_global_idx(&cnt);
 		__measure(&cnt, cnt.count);
-		if (pmu.is_intel)
-			report(cnt.count == 1, "cntr-%d", i);
+		if (pmu.is_intel) {
+			if (intel_inst_overcount_flags & INST_RETIRED_OVERCOUNT)
+				report(cnt.count < 14, "cntr-%d", i);
+			else
+				report(cnt.count == 1, "cntr-%d", i);
+		}
 		else
 			report(cnt.count == 0xffffffffffff || cnt.count < 7, "cntr-%d", i);
 
Signature · 1 line
2.43.0
author
#7 lore raw

PATCH 5/5 x86/pmu: Expand "llc references" upper limit for broader compatibility

In reply to Dapeng Mi

From: dongsheng <[email protected]>

Increase the upper limit of the "llc references" test to accommodate results observed on additional Intel CPU models, including CWF and SRF.
These CPUs exhibited higher reference counts that previously caused the test to fail.

---
 x86/pmu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
x86/pmu.c +1 -1
index c54c0988..445ea6b4 100644
@@ -116,7 +116,7 @@ struct pmu_event {
 	{"core cycles", 0x003c, 1*N, 50*N},
 	{"instructions", 0x00c0, 10*N, 10.2*N},
 	{"ref cycles", 0x013c, 1*N, 30*N},
-	{"llc references", 0x4f2e, 1, 2*N},
+	{"llc references", 0x4f2e, 1, 2.5*N},
 	{"llc misses", 0x412e, 1, 1*N},
 	{"branches", 0x00c4, 1*N, 1.1*N},
 	{"branch misses", 0x00c5, 1, 0.1*N},
Signature · 1 line
2.43.0