Skip to contentSkip to conversation

x86: events: Do not return bogus capabilities if PMU is broken

PATCH In review Paolo Bonzini 2 participants 3 messages Jun 1, 2022

Subsystems (3)
lore
Share & tools
lore

Paolo Bonzini

author maintainer
#0 lore raw

PATCH 1/1 x86: events: Do not return bogus capabilities if PMU is broken

From: Like Xu <[email protected]>

If the PMU is broken due to firmware issues, check_hw_exists() will return false but perf_get_x86_pmu_capability() will still return data from x86_pmu. Likewise if some of the hotplug callbacks cannot be installed the contents of x86_pmu will not be reverted.

Handle the failure in both cases by clearing x86_pmu if init_hw_perf_events() or reverts to software events only.

---
 arch/x86/events/core.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
arch/x86/events/core.c +10 -2
index 99cf67d63cf3..d23f3821daf5 100644
@@ -2095,14 +2095,15 @@ static int __init init_hw_perf_events(void)
 	}
 	if (err != 0) {
 		pr_cont("no PMU driver, software events only.\n");
-		return 0;
+		err = 0;
+		goto out_bad_pmu;
 	}
 
 	pmu_check_apic();
 
 	/* sanity check that the hardware exists or is emulated */
 	if (!check_hw_exists(&pmu, x86_pmu.num_counters, x86_pmu.num_counters_fixed))
-		return 0;
+		goto out_bad_pmu;
 
 	pr_cont("%s PMU driver.\n", x86_pmu.name);
 
@@ -2211,6 +2212,8 @@ static int __init init_hw_perf_events(void)
 	cpuhp_remove_state(CPUHP_AP_PERF_X86_STARTING);
 out:
 	cpuhp_remove_state(CPUHP_PERF_X86_PREPARE);
+out_bad_pmu:
+	memset(&x86_pmu, 0, sizeof(x86_pmu));
 	return err;
 }
 early_initcall(init_hw_perf_events);
@@ -2982,6 +2985,11 @@ unsigned long perf_misc_flags(struct pt_regs *regs)
 
 void perf_get_x86_pmu_capability(struct x86_pmu_capability *cap)
 {
+	if (!x86_pmu.name) {
+		memset(cap, 0, sizeof(*cap));
+		return;
+	}
+
 	cap->version		= x86_pmu.version;
 	/*
 	 * KVM doesn't support the hybrid PMU yet.
Signature · 1 line
2.31.1
maintainer
#1 lore raw
Quoted 11 lines · Paolo Bonzini

From: Like Xu <[email protected]>

If the PMU is broken due to firmware issues, check_hw_exists() will return false but perf_get_x86_pmu_capability() will still return data from x86_pmu. Likewise if some of the hotplug callbacks cannot be installed the contents of x86_pmu will not be reverted.

Handle the failure in both cases by clearing x86_pmu if init_hw_perf_events() or reverts to software events only.

No SoB from Like,

Quoted 36 lines
---
 arch/x86/events/core.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
arch/x86/events/core.c +5 -2
index 99cf67d63cf3..d23f3821daf5 100644
@@ -2095,14 +2095,15 @@ static int __init init_hw_perf_events(void)
 	}
 	if (err != 0) {
 		pr_cont("no PMU driver, software events only.\n");
-		return 0;
+		err = 0;
+		goto out_bad_pmu;
 	}
 
 	pmu_check_apic();
 
 	/* sanity check that the hardware exists or is emulated */
 	if (!check_hw_exists(&pmu, x86_pmu.num_counters, x86_pmu.num_counters_fixed))
-		return 0;
+		goto out_bad_pmu;
 
 	pr_cont("%s PMU driver.\n", x86_pmu.name);
 
@@ -2211,6 +2212,8 @@ static int __init init_hw_perf_events(void)
 	cpuhp_remove_state(CPUHP_AP_PERF_X86_STARTING);
 out:
 	cpuhp_remove_state(CPUHP_PERF_X86_PREPARE);
+out_bad_pmu:
+	memset(&x86_pmu, 0, sizeof(x86_pmu));
 	return err;
 }
 early_initcall(init_hw_perf_events);

arch/x86/events/core.c unsigned long perf_misc_flags(struct pt_regs *regs) · line 2988

Quoted 4 lines
arch/x86/events/core.c +1 -0
@@ -2982,6 +2985,11 @@ unsigned long perf_misc_flags(struct pt_regs *regs)
 
 void perf_get_x86_pmu_capability(struct x86_pmu_capability *cap)
 {
+	if (!x86_pmu.name) {

We have x86_pmu_initialized(), the implementation is a bit daft, but might as well use it here too, no?

arch/x86/events/core.c unsigned long perf_misc_flags(struct pt_regs *regs) · line 2995

Quoted 7 lines
arch/x86/events/core.c +4 -0
+		memset(cap, 0, sizeof(*cap));
+		return;
+	}
+
 	cap->version		= x86_pmu.version;
 	/*
 	 * KVM doesn't support the hybrid PMU yet.

Otherwise seems fine I suppose.

author maintainer
#2 lore raw
Quoted 8 lines · Peter Zijlstra
Quoted 7 lines · Paolo Bonzini

From: Like Xu <[email protected]>

If the PMU is broken due to firmware issues, check_hw_exists() will return false but perf_get_x86_pmu_capability() will still return data from x86_pmu. Likewise if some of the hotplug callbacks cannot be installed the contents of x86_pmu will not be reverted.

Quoted 6 lines
Quoted 4 lines

Handle the failure in both cases by clearing x86_pmu if init_hw_perf_events() or reverts to software events only.

No SoB from Like,

And hardly any code from him either except for a single memset, :) but I'll add

(I have hooks that notice this when pushing, but they don't help with git-send-email...).

Quoted 8 lines

arch/x86/events/core.c unsigned long perf_misc_flags(struct pt_regs *regs) · line 2988

Quoted 5 lines
arch/x86/events/core.c +1 -0
@@ -2982,6 +2985,11 @@ unsigned long perf_misc_flags(struct pt_regs *regs)
 
  void perf_get_x86_pmu_capability(struct x86_pmu_capability *cap)
  {
+	if (!x86_pmu.name) {

We have x86_pmu_initialized(), the implementation is a bit daft, but might as well use it here too, no?

Yes, thanks.

Paolo