Skip to contentSkip to conversation

vPMU v5 test case

PATCH New Xiong Zhang 1 participant 7 messages Sep 1, 2023 KVM

lore
Share & tools
lore
#0 lore raw

cover vPMU v5 test case

Intel vPMU version has been upgraded from 2 to 5 in [1], this patchset adds test cases for each new feature.

Reference:
[1]:Upgrade vPMU version to 5
https://lore.kernel.org/kvm/[email protected]/T/#m3a7e8c3b98b8793a993f39c1851ef0b89efbd93d

Xiong Zhang (6):
x86: pmu: remove duplicate code
x86: pmu: Add Freeze_LBRS_On_PMI test case
x86: pmu: PERF_GLOBAL_STATUS_SET MSR verification for vPMU v4
x86: pmu: PERF_GLOBAL_INUSE MSR verification for vPMU v4
x86: pmu: Limit vcpu's fixed counter into fixed_events[]
x86: pmu: Support fixed counter enumeration in vPMU v5

 lib/x86/msr.h  |   8 ++++
 lib/x86/pmu.c  |  10 ++---
 lib/x86/pmu.h  |   6 +++
 x86/pmu.c      |  32 +++++++++++++--
 x86/pmu_lbr.c  | 109 ++++++++++++++++++++++++++++++++++++++++++++++++-
 x86/pmu_pebs.c |  12 ++++--
 6 files changed, 165 insertions(+), 12 deletions(-)

base-commit: e8f8554f810821e37f05112a46ae9775a029b5d1

Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/6 x86: pmu: remove duplicate code

---
 lib/x86/pmu.c | 5 -----
 1 file changed, 5 deletions(-)
lib/x86/pmu.c +0 -5
index 0f2afd6..d06e945 100644
@@ -16,11 +16,6 @@ void pmu_init(void)
 			pmu.fixed_counter_width = (cpuid_10.d >> 5) & 0xff;
 		}
 
-		if (pmu.version > 1) {
-			pmu.nr_fixed_counters = cpuid_10.d & 0x1f;
-			pmu.fixed_counter_width = (cpuid_10.d >> 5) & 0xff;
-		}
-
 		pmu.nr_gp_counters = (cpuid_10.a >> 8) & 0xff;
 		pmu.gp_counter_width = (cpuid_10.a >> 16) & 0xff;
 		pmu.gp_counter_mask_length = (cpuid_10.a >> 24) & 0xff;
Signature · 1 line
2.34.1
#2 lore raw

PATCH 2/6 x86: pmu: Add Freeze_LBRS_On_PMI test case

In reply to Xiong Zhang

Once IA32_DEBUGCTL.FREEZE_LBR_ON_PMI is set, LBR stack will be frozen on PMI. This commit add a test case to check whether LBR stack is changed during PMI handler or not.

In PMU v2, legacy Freeze LBRS on PMI is introduced, IA32_DEBUGCTL.FREEZE_LBR_ON_PMI will be cleared by processor when PMI happens, SW should set it again in PMI handler to enable LBR. In PMU v4, streamlined freeze LBRs on PMI is introduced, the new LBR_FRZ[bit 58] bit is added into IA32_PERF_GLOBAL_STATUS MSR, this bit is set by processor when PMI happens, this bit also serves as a control to enable LBR stack. SW should clear this bit in PMI handler to enable LBR.

This commit checks legacy and streamlined FREEZE_LBR_ON_PMI feature, and their SW and HW sequence.

---
 lib/x86/msr.h |   3 ++
 x86/pmu_lbr.c | 109 +++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 111 insertions(+), 1 deletion(-)
lib/x86/msr.h +3 -0
index 0e3fd03..9748436 100644
@@ -430,6 +430,9 @@
 #define MSR_CORE_PERF_GLOBAL_CTRL	0x0000038f
 #define MSR_CORE_PERF_GLOBAL_OVF_CTRL	0x00000390
 
+/* PERF_GLOBAL_OVF_CTRL bits */
+#define MSR_CORE_PERF_GLOBAL_OVF_CTRL_LBR_FREEZE  (1ULL << 58)
+
 /* AMD Performance Counter Global Status and Control MSRs */
 #define MSR_AMD64_PERF_CNTR_GLOBAL_STATUS	0xc0000300
 #define MSR_AMD64_PERF_CNTR_GLOBAL_CTL		0xc0000301
x86/pmu_lbr.c +108 -1

139 lines not on this page · raw message

Signature · 1 line
2.34.1
#3 lore raw

PATCH 3/6 x86: pmu: PERF_GLOBAL_STATUS_SET MSR verification for vPMU v4

In reply to Xiong Zhang

The IA32_PERF_GLOBAL_STATUS_SET MSR is introduced with arch PMU v4. It allows software to set individual bits in IA32_PERF_GLOBAL_STATUS MSR.

This commit adds the test case for this MSR. When global status is cleared at counter overflow, guest write PERF_GLOBAL_STATUS_SET MSR to set the counter overflow bit, then check this counter's overflow bit in IA32_PERF_GLOABAL_STATUS MSR.

---
 lib/x86/msr.h | 1 +
 x86/pmu.c     | 8 ++++++++
 2 files changed, 9 insertions(+)
lib/x86/msr.h +1 -0
index 9748436..63b8539 100644
@@ -429,6 +429,7 @@
 #define MSR_CORE_PERF_GLOBAL_STATUS	0x0000038e
 #define MSR_CORE_PERF_GLOBAL_CTRL	0x0000038f
 #define MSR_CORE_PERF_GLOBAL_OVF_CTRL	0x00000390
+#define MSR_CORE_PERF_GLOBAL_STATUS_SET	0x00000391
 
 /* PERF_GLOBAL_OVF_CTRL bits */
 #define MSR_CORE_PERF_GLOBAL_OVF_CTRL_LBR_FREEZE  (1ULL << 58)
x86/pmu.c +8 -0
index 72c2c9c..a171e9e 100644
@@ -350,6 +350,14 @@ static void check_counter_overflow(void)
 		status = rdmsr(pmu.msr_global_status);
 		report(!(status & (1ull << idx)), "status clear-%d", i);
 		report(check_irq() == (i % 2), "irq-%d", i);
+		if (pmu.version >= 4) {
+			wrmsr(MSR_CORE_PERF_GLOBAL_STATUS_SET, 1ull << idx);
+			status = rdmsr(pmu.msr_global_status);
+			report(status & (1ull << idx), "status set-%d", i);
+			wrmsr(pmu.msr_global_status_clr, 1ull << idx);
+			status = rdmsr(pmu.msr_global_status);
+			report(!(status & (1ull << idx)), "status set clear-%d", i);
+		}
 	}
 
 	report_prefix_pop();
Signature · 1 line
2.34.1
#4 lore raw

PATCH 4/6 x86: pmu: PERF_GLOBAL_INUSE MSR verification for vPMU v4

In reply to Xiong Zhang

Arch PMU v4 introduces a new MSR, IA32_PERF_GLOBAL_INUSE. It provides as "InUse" bit for each GP counter and fixed counter in processor. Additionally PMI InUse[bit 63] indicates if the PMI mechanisam has been configured.

This commit add the test case for this MSR, when a counter is started, its index bit must be set in this MSR, when a counter is stopped through writing 0 into counter's control MSR, its index bit must be cleared in this MSR.

---
 lib/x86/msr.h |  4 ++++
 x86/pmu.c     | 14 ++++++++++++--
 2 files changed, 16 insertions(+), 2 deletions(-)
lib/x86/msr.h +4 -0
index 63b8539..3bffe80 100644
@@ -430,6 +430,10 @@
 #define MSR_CORE_PERF_GLOBAL_CTRL	0x0000038f
 #define MSR_CORE_PERF_GLOBAL_OVF_CTRL	0x00000390
 #define MSR_CORE_PERF_GLOBAL_STATUS_SET	0x00000391
+#define MSR_CORE_PERF_GLOBAL_INUSE      0x00000392
+
+/* PERF_GLOBAL_INUSE bits */
+#define MSR_CORE_PERF_GLOBAL_INUSE_PMI_BIT       63
 
 /* PERF_GLOBAL_OVF_CTRL bits */
 #define MSR_CORE_PERF_GLOBAL_OVF_CTRL_LBR_FREEZE  (1ULL << 58)
x86/pmu.c +12 -2
index a171e9e..0ec0062 100644
@@ -155,6 +155,15 @@ static void __start_event(pmu_counter_t *evt, uint64_t count)
 	    ctrl = (ctrl & ~(0xf << shift)) | (usrospmi << shift);
 	    wrmsr(MSR_CORE_PERF_FIXED_CTR_CTRL, ctrl);
     }
+    if (pmu.version >= 4) {
+	    u64 inuse = rdmsr(MSR_CORE_PERF_GLOBAL_INUSE);
+	    int idx = event_to_global_idx(evt);
+
+	    report(inuse & BIT_ULL(idx), "start counter_idx: %d", idx);
+	    if (evt->config & EVNTSEL_INT)
+		    report(inuse & BIT_ULL(MSR_CORE_PERF_GLOBAL_INUSE_PMI_BIT),
+			   "INT, start counter_idx: %d", idx);
+    }
     global_enable(evt);
     apic_write(APIC_LVTPC, PMI_VECTOR);
 }
@@ -168,14 +177,15 @@ static void stop_event(pmu_counter_t *evt)
 {
 	global_disable(evt);
 	if (is_gp(evt)) {
-		wrmsr(MSR_GP_EVENT_SELECTx(event_to_global_idx(evt)),
-		      evt->config & ~EVNTSEL_EN);
+		wrmsr(MSR_GP_EVENT_SELECTx(event_to_global_idx(evt)), 0);
 	} else {
 		uint32_t ctrl = rdmsr(MSR_CORE_PERF_FIXED_CTR_CTRL);
 		int shift = (evt->ctr - MSR_CORE_PERF_FIXED_CTR0) * 4;
 		wrmsr(MSR_CORE_PERF_FIXED_CTR_CTRL, ctrl & ~(0xf << shift));
 	}
 	evt->count = rdmsr(evt->ctr);
+	if (pmu.version >= 4)
+		report((rdmsr(MSR_CORE_PERF_GLOBAL_INUSE) & BIT_ULL(evt->idx)) == 0, "stop counter idx: %d", evt->idx);
 }
 
 static noinline void measure_many(pmu_counter_t *evt, int count)
Signature · 1 line
2.34.1
#5 lore raw

PATCH 5/6 x86: pmu: Limit vcpu's fixed counter into fixed_events[]

In reply to Xiong Zhang

Arch PMU v5 has Fixed Counter Enumeration feature, user can specify a fixed counter which has index greater than fixed_events[] array through CPUID.0AH.ECX, so limit fixed counter index into fixed_events[] array.

---
 x86/pmu.c | 6 ++++++
 1 file changed, 6 insertions(+)
x86/pmu.c +6 -0
index 0ec0062..416e9d7 100644
@@ -53,6 +53,7 @@ char *buf;
 
 static struct pmu_event *gp_events;
 static unsigned int gp_events_size;
+static unsigned int fixed_events_size;
 
 static inline void loop(void)
 {
@@ -256,6 +257,8 @@ static void check_fixed_counters(void)
 	int i;
 
 	for (i = 0; i < pmu.nr_fixed_counters; i++) {
+		if (i >= fixed_events_size)
+			continue;
 		cnt.ctr = fixed_events[i].unit_sel;
 		measure_one(&cnt);
 		report(verify_event(cnt.count, &fixed_events[i]), "fixed-%d", i);
@@ -277,6 +280,8 @@ static void check_counters_many(void)
 		n++;
 	}
 	for (i = 0; i < pmu.nr_fixed_counters; i++) {
+		if (i >= fixed_events_size)
+			continue;
 		cnt[n].ctr = fixed_events[i].unit_sel;
 		cnt[n].config = EVNTSEL_OS | EVNTSEL_USR;
 		n++;
@@ -700,6 +705,7 @@ int main(int ac, char **av)
 		}
 		gp_events = (struct pmu_event *)intel_gp_events;
 		gp_events_size = sizeof(intel_gp_events)/sizeof(intel_gp_events[0]);
+		fixed_events_size = sizeof(fixed_events)/sizeof(fixed_events[0]);
 		report_prefix_push("Intel");
 		set_ref_cycle_expectations();
 	} else {
Signature · 1 line
2.34.1
#6 lore raw

PATCH 6/6 x86: pmu: Support fixed counter enumeration in vPMU v5

In reply to Xiong Zhang

With Architectural Performance Monitoring Version 5, register CPUID.0AH.ECX indicates Fixed Counter enumeration. It is a bit mask which enumerates the supported Fixed Counters in a processor. If bit 'i' is set, it implies that Fixed Counter 'i' is supported.

So user can specify non-continuous Fixed Counters, i.e. CPUID.0AH.ECX= 0x77, CPUID.0AH.EDX.EDX[4:0] = 3, this means fixed counters 0,1,2, 4,5,6 are supported, while Fixed counter 3 isn't supported.

This commit add the non-continuous fixed counters support. If vPMU version < 5, nr_fixed_counters is continuous fixed counter number starting from 0. If vPMU version >= 5, nr_fixed_counters is highest bit set index in fixed_counter_mask bitmap, finxed counters with index [0, nr_fixed_counters) may be not all supported.

---
 lib/x86/pmu.c  |  5 +++++
 lib/x86/pmu.h  |  6 ++++++
 x86/pmu.c      |  8 +++++---
 x86/pmu_pebs.c | 12 +++++++++---
 4 files changed, 25 insertions(+), 6 deletions(-)
lib/x86/pmu.c +5 -0
index d06e945..e6492ed 100644
@@ -14,6 +14,11 @@ void pmu_init(void)
 		if (pmu.version > 1) {
 			pmu.nr_fixed_counters = cpuid_10.d & 0x1f;
 			pmu.fixed_counter_width = (cpuid_10.d >> 5) & 0xff;
+			pmu.fixed_counter_mask = (1u << pmu.nr_fixed_counters) - 1;
+			if (pmu.version >= 5) {
+				pmu.fixed_counter_mask = cpuid_10.c;
+				pmu.nr_fixed_counters = fls(cpuid_10.c) + 1;
+			}
 		}
 
 		pmu.nr_gp_counters = (cpuid_10.a >> 8) & 0xff;
lib/x86/pmu.h +6 -0
index 8465e3c..038d74e 100644
@@ -57,6 +57,7 @@ struct pmu_caps {
 	u8 version;
 	u8 nr_fixed_counters;
 	u8 fixed_counter_width;
+	u32 fixed_counter_mask;
 	u8 nr_gp_counters;
 	u8 gp_counter_width;
 	u8 gp_counter_mask_length;
@@ -106,6 +107,11 @@ static inline bool this_cpu_has_perf_global_status(void)
 	return pmu.version > 1;
 }
 
+static inline bool pmu_fixed_counter_supported(int i)
+{
+	return pmu.fixed_counter_mask & BIT(i);
+}
+
 static inline bool pmu_gp_counter_is_available(int i)
 {
 	return pmu.gp_counter_available & BIT(i);
x86/pmu.c +5 -3
index 416e9d7..9806f29 100644
@@ -257,7 +257,7 @@ static void check_fixed_counters(void)
 	int i;
 
 	for (i = 0; i < pmu.nr_fixed_counters; i++) {
-		if (i >= fixed_events_size)
+		if (i >= fixed_events_size || !pmu_fixed_counter_supported(i))
 			continue;
 		cnt.ctr = fixed_events[i].unit_sel;
 		measure_one(&cnt);
@@ -280,7 +280,7 @@ static void check_counters_many(void)
 		n++;
 	}
 	for (i = 0; i < pmu.nr_fixed_counters; i++) {
-		if (i >= fixed_events_size)
+		if (i >= fixed_events_size || !pmu_fixed_counter_supported(i))
 			continue;
 		cnt[n].ctr = fixed_events[i].unit_sel;
 		cnt[n].config = EVNTSEL_OS | EVNTSEL_USR;
@@ -437,7 +437,8 @@ static void check_rdpmc(void)
 		else
 			report(cnt.count == (u32)val, "fast-%d", i);
 	}
-	for (i = 0; i < pmu.nr_fixed_counters; i++) {
+	for (i = 0; i < pmu.nr_fixed_counters && pmu_fixed_counter_supported(i);
+	     i++) {
 		uint64_t x = val & ((1ull << pmu.fixed_counter_width) - 1);
 		pmu_counter_t cnt = {
 			.ctr = MSR_CORE_PERF_FIXED_CTR0 + i,
@@ -720,6 +721,7 @@ int main(int ac, char **av)
 	printf("Mask length:         %d\n", pmu.gp_counter_mask_length);
 	printf("Fixed counters:      %d\n", pmu.nr_fixed_counters);
 	printf("Fixed counter width: %d\n", pmu.fixed_counter_width);
+	printf("Supported Fixed counter mask: 0x%x\n", pmu.fixed_counter_mask);
 
 	apic_write(APIC_LVTPC, PMI_VECTOR);
 
x86/pmu_pebs.c +9 -3
index 894ae6c..bc8e64d 100644
@@ -222,7 +222,9 @@ static void pebs_enable(u64 bitmask, u64 pebs_data_cfg)
 	ds->pebs_interrupt_threshold = ds->pebs_buffer_base +
 		get_adaptive_pebs_record_size(pebs_data_cfg);
 
-	for (idx = 0; idx < pmu.nr_fixed_counters; idx++) {
+	for (idx = 0;
+	     idx < pmu.nr_fixed_counters && pmu_fixed_counter_supported(idx);
+	     idx++) {
 		if (!(BIT_ULL(FIXED_CNT_INDEX + idx) & bitmask))
 			continue;
 		if (has_baseline)
@@ -357,13 +359,17 @@ static void check_pebs_counters(u64 pebs_data_cfg)
 	unsigned int idx;
 	u64 bitmask = 0;
 
-	for (idx = 0; idx < pmu.nr_fixed_counters; idx++)
+	for (idx = 0;
+	     idx < pmu.nr_fixed_counters && pmu_fixed_counter_supported(idx);
+	     idx++)
 		check_one_counter(FIXED, idx, pebs_data_cfg);
 
 	for (idx = 0; idx < max_nr_gp_events; idx++)
 		check_one_counter(GP, idx, pebs_data_cfg);
 
-	for (idx = 0; idx < pmu.nr_fixed_counters; idx++)
+	for (idx = 0;
+	     idx < pmu.nr_fixed_counters && pmu_fixed_counter_supported(idx);
+	     idx++)
 		bitmask |= BIT_ULL(FIXED_CNT_INDEX + idx);
 	for (idx = 0; idx < max_nr_gp_events; idx += 2)
 		bitmask |= BIT_ULL(idx);
Signature · 1 line
2.34.1