Skip to contentSkip to conversation

x86/pmu: Explicitly zero PERF_GLOBAL_CTRL at start of PMU test

PATCH Applied Sean Christopherson 2 participants 3 messages May 29, 2025 – Jun 10, 2025 KVM

lore
Share & tools
lore

Sean Christopherson

author reviewer
#0 lore raw

PATCH 1/1 x86/pmu: Explicitly zero PERF_GLOBAL_CTRL at start of PMU test

Explicitly zero PERF_GLOBAL_CTRL at the start of the PMU test as the architectural RESET value of PERF_GLOBAL_CTRL is to set all enable bits for general purpose counters (for backwards compatibility with software that was written for v1 PMUs). Leaving PERF_GLOBAL_CTRL set can result in false failures due to counters unexpectedly being left active.

---
 x86/pmu.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
x86/pmu.c +3 -3
index 8cf26b12..9bd0c186 100644
@@ -905,9 +905,6 @@ static void set_ref_cycle_expectations(void)
 	if (!pmu.nr_gp_counters || !pmu_gp_counter_is_available(2))
 		return;
 
-	if (this_cpu_has_perf_global_ctrl())
-		wrmsr(pmu.msr_global_ctl, 0);
-
 	t0 = fenced_rdtsc();
 	start_event(&cnt);
 	t1 = fenced_rdtsc();
@@ -956,6 +953,9 @@ int main(int ac, char **av)
 	handle_irq(PMI_VECTOR, cnt_overflow);
 	buf = malloc(N*64);
 
+	if (this_cpu_has_perf_global_ctrl())
+		wrmsr(pmu.msr_global_ctl, 0);
+
 	check_invalid_rdpmc_gp();
 
 	if (pmu.is_intel) {

base-commit: 72d110d8286baf1b355301cc8c8bdb42be2663fb

Signature · 1 line
2.49.0.1204.g71687c7c1d-goog
Reviewed-by on v1 1/1
#1 lore raw
Quoted 36 lines · Sean Christopherson

Explicitly zero PERF_GLOBAL_CTRL at the start of the PMU test as the architectural RESET value of PERF_GLOBAL_CTRL is to set all enable bits for general purpose counters (for backwards compatibility with software that was written for v1 PMUs). Leaving PERF_GLOBAL_CTRL set can result in false failures due to counters unexpectedly being left active.

---
 x86/pmu.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
x86/pmu.c +3 -3
index 8cf26b12..9bd0c186 100644
@@ -905,9 +905,6 @@ static void set_ref_cycle_expectations(void)
 	if (!pmu.nr_gp_counters || !pmu_gp_counter_is_available(2))
 		return;
 
-	if (this_cpu_has_perf_global_ctrl())
-		wrmsr(pmu.msr_global_ctl, 0);
-
 	t0 = fenced_rdtsc();
 	start_event(&cnt);
 	t1 = fenced_rdtsc();
@@ -956,6 +953,9 @@ int main(int ac, char **av)
 	handle_irq(PMI_VECTOR, cnt_overflow);
 	buf = malloc(N*64);
 
+	if (this_cpu_has_perf_global_ctrl())
+		wrmsr(pmu.msr_global_ctl, 0);
+
 	check_invalid_rdpmc_gp();
 
 	if (pmu.is_intel) {
Quoted 1 line

base-commit: 72d110d8286baf1b355301cc8c8bdb42be2663fb

author reviewer Applied
#2 lore raw
In reply to Sean Christopherson
Quoted 8 lines · Sean Christopherson

Explicitly zero PERF_GLOBAL_CTRL at the start of the PMU test as the architectural RESET value of PERF_GLOBAL_CTRL is to set all enable bits for general purpose counters (for backwards compatibility with software that was written for v1 PMUs). Leaving PERF_GLOBAL_CTRL set can result in false failures due to counters unexpectedly being left active.

[...]

Applied to kvm-x86 next, thanks!

[1/1] x86/pmu: Explicitly zero PERF_GLOBAL_CTRL at start of PMU test

      https://github.com/kvm-x86/kvm-unit-tests/commit/0bd5a078d1d1

--
https://github.com/kvm-x86/kvm-unit-tests/tree/next