Skip to contentSkip to conversation

x86/pmu: change the num_counters to reflect the real gp counters

PATCH In review Wei Huang 2 participants 2 messages Sep 8, 2017 – Sep 12, 2017 KVM

lore
Share & tools
lore

Wei Huang

author
#0 lore raw

PATCH 1/1 x86/pmu: change the num_counters to reflect the real gp counters

Currently pmu.c sets num_counters to MIN(num_gp_counters, num_gp_events). This is to prevent the out-of-bound access to gp_events[] array in check_counters_many(). However it also means that check_counters_many() can NOT stress all gp counters if num_gp_counters > num_gp_events. This small patch changes the value of num_counters back to num_gp_counters and prevents the out-of-bound problem by using the mod function in the array access.

---
 x86/pmu.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
x86/pmu.c +2 -3
index a0238dc..b56b61e 100644
@@ -275,7 +275,8 @@ static void check_counters_many(void)
 
 		cnt[n].count = 0;
 		cnt[n].ctr = MSR_IA32_PERFCTR0 + n;
-		cnt[n].config = EVNTSEL_OS | EVNTSEL_USR | gp_events[i].unit_sel;
+		cnt[n].config = EVNTSEL_OS | EVNTSEL_USR |
+			gp_events[i % ARRAY_SIZE(gp_events)].unit_sel;
 		n++;
 	}
 	for (i = 0; i < edx.split.num_counters_fixed; i++) {
@@ -397,8 +398,6 @@ int main(int ac, char **av)
 	printf("Fixed counter width: %d\n", edx.split.bit_width_fixed);
 
 	num_counters = eax.split.num_counters;
-	if (num_counters > ARRAY_SIZE(gp_events))
-		num_counters = ARRAY_SIZE(gp_events);
 
 	apic_write(APIC_LVTPC, PC_VECTOR);
 
Signature · 1 line
2.7.5
maintainer
#1 lore raw
Quoted 29 lines · Wei Huang

Currently pmu.c sets num_counters to MIN(num_gp_counters, num_gp_events). This is to prevent the out-of-bound access to gp_events[] array in check_counters_many(). However it also means that check_counters_many() can NOT stress all gp counters if num_gp_counters > num_gp_events. This small patch changes the value of num_counters back to num_gp_counters and prevents the out-of-bound problem by using the mod function in the array access.

---
 x86/pmu.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
x86/pmu.c +2 -1
index a0238dc..b56b61e 100644
@@ -275,7 +275,8 @@ static void check_counters_many(void)
 
 		cnt[n].count = 0;
 		cnt[n].ctr = MSR_IA32_PERFCTR0 + n;
-		cnt[n].config = EVNTSEL_OS | EVNTSEL_USR | gp_events[i].unit_sel;
+		cnt[n].config = EVNTSEL_OS | EVNTSEL_USR |
+			gp_events[i % ARRAY_SIZE(gp_events)].unit_sel;
 		n++;
 	}
 	for (i = 0; i < edx.split.num_counters_fixed; i++) {

x86/pmu.c int main(int ac, char **av) · line 403

Quoted 8 lines
x86/pmu.c +0 -2
@@ -397,8 +398,6 @@ int main(int ac, char **av)
 	printf("Fixed counter width: %d\n", edx.split.bit_width_fixed);
 
 	num_counters = eax.split.num_counters;
-	if (num_counters > ARRAY_SIZE(gp_events))
-		num_counters = ARRAY_SIZE(gp_events);
 
 	apic_write(APIC_LVTPC, PC_VECTOR);
 

Clever! I applied the patch.

Paolo