Skip to contentSkip to conversation

events/core: introduce perf_pmu_disable_all() to turn off all PMU

PATCH In review Pingfan Liu 2 participants 5 messages Nov 23, 2020 – Nov 26, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 events/core: introduce perf_pmu_disable_all() to turn off all PMU

In crash context, NMI should be suppressed before jump to a new kernel. Naturally as the source of NMI on some arches, PMU should be turned off at that time.

Introduce perf_pmu_disable_all() to achieve the goal.

Cc Peter Zijlstra <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Mark Rutland <[email protected]>
Cc Alexander Shishkin <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Omar Sandoval <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Mike Rapoport <[email protected]>

To: [email protected]

---
 include/linux/perf_event.h |  1 +
 kernel/events/core.c       | 10 ++++++++++
 2 files changed, 11 insertions(+)
include/linux/perf_event.h +1 -0
index 96450f6..f4baa87 100644
@@ -965,6 +965,7 @@ extern const struct perf_event_attr *perf_event_attrs(struct perf_event *event);
 extern void perf_event_print_debug(void);
 extern void perf_pmu_disable(struct pmu *pmu);
 extern void perf_pmu_enable(struct pmu *pmu);
+extern void perf_pmu_disable_all(void);
 extern void perf_sched_cb_dec(struct pmu *pmu);
 extern void perf_sched_cb_inc(struct pmu *pmu);
 extern int perf_event_task_disable(void);
kernel/events/core.c +10 -0
index dc568ca..c8e04a5 100644
@@ -1205,6 +1205,16 @@ void perf_pmu_enable(struct pmu *pmu)
 		pmu->pmu_enable(pmu);
 }
 
+/* When crashed, other cpus hang in idle loop, so here do an emergency job under no lock */
+void perf_pmu_disable_all(void)
+{
+	struct pmu *pmu;
+
+	list_for_each_entry(pmu, &pmus, entry)
+		if (pmu->pmu_disable)
+			pmu->pmu_disable(pmu);
+}
+
 static DEFINE_PER_CPU(struct list_head, active_ctx_list);
 
 /*
Signature · 1 line
2.7.5
#1 lore raw

PATCH 2/2 x86/machine_kexec: disable PMU before jumping to new kernel

During jumping to the new kernel, on the crashed cpu, the memory mapping switches from an old one to an identity one. It had better disable PMU to suppress NMI, which can be delivered using the old mapping.

Also on x86_64, idt_invalidate() to clear idt as on 32 bits.

Cc Peter Zijlstra <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Mark Rutland <[email protected]>
Cc Alexander Shishkin <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Omar Sandoval <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Mike Rapoport <[email protected]>

To: [email protected]
Cc: [email protected]

---
 arch/x86/kernel/machine_kexec_32.c | 1 +
 arch/x86/kernel/machine_kexec_64.c | 3 +++
 2 files changed, 4 insertions(+)
arch/x86/kernel/machine_kexec_32.c +1 -0
index 64b00b0..72c6100 100644
@@ -191,6 +191,7 @@ void machine_kexec(struct kimage *image)
 	/* Interrupts aren't acceptable while we reboot */
 	local_irq_disable();
 	hw_breakpoint_disable();
+	perf_pmu_disable_all();
 
 	if (image->preserve_context) {
 #ifdef CONFIG_X86_IO_APIC
arch/x86/kernel/machine_kexec_64.c +3 -0
index a29a44a..238893e 100644
@@ -17,6 +17,7 @@
 #include <linux/suspend.h>
 #include <linux/vmalloc.h>
 #include <linux/efi.h>
+#include <linux/perf_event.h>
 
 #include <asm/init.h>
 #include <asm/tlbflush.h>
@@ -338,6 +339,8 @@ void machine_kexec(struct kimage *image)
 	/* Interrupts aren't acceptable while we reboot */
 	local_irq_disable();
 	hw_breakpoint_disable();
+	perf_pmu_disable_all();
+	idt_invalidate(phys_to_virt(0));
 
 	if (image->preserve_context) {
 #ifdef CONFIG_X86_IO_APIC
Signature · 1 line
2.7.5
maintainer
#3 lore raw
In reply to Pingfan Liu
Quoted 1 line · Pingfan Liu

+/* When crashed, other cpus hang in idle loop, so here do an emergency job under no lock */

-ENOPARSE, -ETOOLONG

kernel/events/core.c void perf_pmu_enable(struct pmu *pmu) · line 1216

Quoted 8 lines
kernel/events/core.c +8 -0
+void perf_pmu_disable_all(void)
+{
+	struct pmu *pmu;
+
+	list_for_each_entry(pmu, &pmus, entry)
+		if (pmu->pmu_disable)
+			pmu->pmu_disable(pmu);
+}

This violates both locking rules and coding style.

#4 lore raw
Quoted 7 lines · Peter Zijlstra
Quoted 1 line · Pingfan Liu

+/* When crashed, other cpus hang in idle loop, so here do an emergency job under no lock */

-ENOPARSE, -ETOOLONG

Quoted 10 lines

kernel/events/core.c void perf_pmu_enable(struct pmu *pmu) · line 1216

Quoted 8 lines
kernel/events/core.c +8 -0
+void perf_pmu_disable_all(void)
+{
+     struct pmu *pmu;
+
+     list_for_each_entry(pmu, &pmus, entry)
+             if (pmu->pmu_disable)
+                     pmu->pmu_disable(pmu);
+}

This violates both locking rules and coding style.

Oops. I will re-work on it to see how to run it safely on crashed context.

Thanks,
Pingfan

#2 lore raw

Re: [PATCH 2/2] x86/machine_kexec: disable PMU before jumping to new kernel

In reply to Pingfan Liu

Sorry that I had made a misunderstanding of the code.

Nacked it

Quoted 64 lines · Pingfan Liu

During jumping to the new kernel, on the crashed cpu, the memory mapping switches from an old one to an identity one. It had better disable PMU to suppress NMI, which can be delivered using the old mapping.

Also on x86_64, idt_invalidate() to clear idt as on 32 bits.

Cc Peter Zijlstra <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Mark Rutland <[email protected]>
Cc Alexander Shishkin <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Omar Sandoval <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Mike Rapoport <[email protected]>

To: [email protected]
Cc: [email protected]

---
 arch/x86/kernel/machine_kexec_32.c | 1 +
 arch/x86/kernel/machine_kexec_64.c | 3 +++
 2 files changed, 4 insertions(+)
arch/x86/kernel/machine_kexec_32.c +1 -0
index 64b00b0..72c6100 100644
@@ -191,6 +191,7 @@ void machine_kexec(struct kimage *image)
        /* Interrupts aren't acceptable while we reboot */
        local_irq_disable();
        hw_breakpoint_disable();
+       perf_pmu_disable_all();
 
        if (image->preserve_context) {
 #ifdef CONFIG_X86_IO_APIC
arch/x86/kernel/machine_kexec_64.c +3 -1
index a29a44a..238893e 100644
@@ -17,6 +17,7 @@
 #include <linux/suspend.h>
 #include <linux/vmalloc.h>
 #include <linux/efi.h>
+#include <linux/perf_event.h>
 
 #include <asm/init.h>
 #include <asm/tlbflush.h>
@@ -338,6 +339,8 @@ void machine_kexec(struct kimage *image)
        /* Interrupts aren't acceptable while we reboot */
        local_irq_disable();
        hw_breakpoint_disable();
+       perf_pmu_disable_all();
+       idt_invalidate(phys_to_virt(0));
 
        if (image->preserve_context) {
 #ifdef CONFIG_X86_IO_APIC
--

2.7.5