Skip to contentSkip to conversation

x86/pmu: Verify all available GP counters in check_counters_many()

PATCH Applied Dapeng Mi 3 participants 3 messages Jun 11, 2025 – Jun 25, 2025 KVM

lore
Share & tools
lore

Dapeng Mi

author
#0 lore raw

PATCH 1/1 x86/pmu: Verify all available GP counters in check_counters_many()

The intent of check_counters_many() is to verify all available counters can count correctly at the same time. So an alternative event should be picked to verify the avaialbe GP counter instead of skiping the counter if the initial event is not available.

---
Please notice this patch is based on Sean's "x86: Add CPUID properties,
clean up related code" v2 patchset (https://lore.kernel.org/all/[email protected]/).

 x86/pmu.c | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)
x86/pmu.c +19 -3
index 3987311c..a6b0cfcc 100644
@@ -457,18 +457,34 @@ static void check_fixed_counters(void)
 	}
 }
 
+static struct pmu_event *get_one_event(int idx)
+{
+	int i;
+
+	if (pmu_arch_event_is_available(idx))
+		return &gp_events[idx % gp_events_size];
+
+	for (i = 0; i < gp_events_size; i++) {
+		if (pmu_arch_event_is_available(i))
+			return &gp_events[i];
+	}
+
+	return NULL;
+}
+
 static void check_counters_many(void)
 {
+	struct pmu_event *evt;
 	pmu_counter_t cnt[48];
 	int i, n;
 
 	for (i = 0, n = 0; n < pmu.nr_gp_counters; i++) {
-		if (!pmu_arch_event_is_available(i))
+		evt = get_one_event(i);
+		if (!evt)
 			continue;
 
 		cnt[n].ctr = MSR_GP_COUNTERx(n);
-		cnt[n].config = EVNTSEL_OS | EVNTSEL_USR |
-			gp_events[i % gp_events_size].unit_sel;
+		cnt[n].config = EVNTSEL_OS | EVNTSEL_USR | evt->unit_sel;
 		n++;
 	}
 	for (i = 0; i < fixed_counters_num; i++) {
Signature · 1 line
2.43.0
Tested-by on v1 1/1
#1 lore raw
Quoted 5 lines · Dapeng Mi

The intent of check_counters_many() is to verify all available counters can count correctly at the same time. So an alternative event should be picked to verify the avaialbe GP counter instead of skiping the counter if the initial event is not available.

Quoted 8 lines

---
Please notice this patch is based on Sean's "x86: Add CPUID properties,
clean up related code" v2 patchset (https://lore.kernel.org/all/[email protected]/).

 x86/pmu.c | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)

For AMD processors

Quoted 42 lines
x86/pmu.c +19 -3
index 3987311c..a6b0cfcc 100644
@@ -457,18 +457,34 @@ static void check_fixed_counters(void)
 	}
 }
 
+static struct pmu_event *get_one_event(int idx)
+{
+	int i;
+
+	if (pmu_arch_event_is_available(idx))
+		return &gp_events[idx % gp_events_size];
+
+	for (i = 0; i < gp_events_size; i++) {
+		if (pmu_arch_event_is_available(i))
+			return &gp_events[i];
+	}
+
+	return NULL;
+}
+
 static void check_counters_many(void)
 {
+	struct pmu_event *evt;
 	pmu_counter_t cnt[48];
 	int i, n;
 
 	for (i = 0, n = 0; n < pmu.nr_gp_counters; i++) {
-		if (!pmu_arch_event_is_available(i))
+		evt = get_one_event(i);
+		if (!evt)
 			continue;
 
 		cnt[n].ctr = MSR_GP_COUNTERx(n);
-		cnt[n].config = EVNTSEL_OS | EVNTSEL_USR |
-			gp_events[i % gp_events_size].unit_sel;
+		cnt[n].config = EVNTSEL_OS | EVNTSEL_USR | evt->unit_sel;
 		n++;
 	}
 	for (i = 0; i < fixed_counters_num; i++) {
reviewer Applied
#2 lore raw
In reply to Dapeng Mi
Quoted 4 lines · Dapeng Mi

The intent of check_counters_many() is to verify all available counters can count correctly at the same time. So an alternative event should be picked to verify the avaialbe GP counter instead of skiping the counter if the initial event is not available.

Applied to kvm-x86 next, thanks!

[1/1] x86/pmu: Verify all available GP counters in check_counters_many()

      https://github.com/kvm-x86/kvm-unit-tests/commit/525bdb5d65d5

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