linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/2] events/core: introduce perf_pmu_disable_all() to turn off all PMU
@ 2020-11-23  5:37 Pingfan Liu
  2020-11-23  5:37 ` [PATCH 2/2] x86/machine_kexec: disable PMU before jumping to new kernel Pingfan Liu
  2020-11-23 14:05 ` [PATCH 1/2] events/core: introduce perf_pmu_disable_all() to turn off all PMU Peter Zijlstra
  0 siblings, 2 replies; 5+ messages in thread
From: Pingfan Liu @ 2020-11-23  5:37 UTC (permalink / raw)
  To: linux-kernel
  Cc: Pingfan Liu, Peter Zijlstra, Ingo Molnar,
	Arnaldo Carvalho de Melo, Mark Rutland, Alexander Shishkin,
	Jiri Olsa, Namhyung Kim, Thomas Gleixner, Borislav Petkov,
	H. Peter Anvin, Omar Sandoval, Andrew Morton, Mike Rapoport, x86

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.

Signed-off-by: Pingfan Liu <kernelfans@gmail.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Arnaldo Carvalho de Melo <acme@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Borislav Petkov <bp@alien8.de>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: Omar Sandoval <osandov@fb.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Mike Rapoport <rppt@kernel.org>
Cc: x86@kernel.org
To: linux-kernel@vger.kernel.org
---
 include/linux/perf_event.h |  1 +
 kernel/events/core.c       | 10 ++++++++++
 2 files changed, 11 insertions(+)

diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h
index 96450f6..f4baa87 100644
--- a/include/linux/perf_event.h
+++ b/include/linux/perf_event.h
@@ -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);
diff --git a/kernel/events/core.c b/kernel/events/core.c
index dc568ca..c8e04a5 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -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);
 
 /*
-- 
2.7.5


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 2/2] x86/machine_kexec: disable PMU before jumping to new kernel
  2020-11-23  5:37 [PATCH 1/2] events/core: introduce perf_pmu_disable_all() to turn off all PMU Pingfan Liu
@ 2020-11-23  5:37 ` Pingfan Liu
  2020-11-26  8:05   ` Pingfan Liu
  2020-11-23 14:05 ` [PATCH 1/2] events/core: introduce perf_pmu_disable_all() to turn off all PMU Peter Zijlstra
  1 sibling, 1 reply; 5+ messages in thread
From: Pingfan Liu @ 2020-11-23  5:37 UTC (permalink / raw)
  To: x86
  Cc: Pingfan Liu, Peter Zijlstra, Ingo Molnar,
	Arnaldo Carvalho de Melo, Mark Rutland, Alexander Shishkin,
	Jiri Olsa, Namhyung Kim, Thomas Gleixner, Borislav Petkov,
	H. Peter Anvin, Omar Sandoval, Andrew Morton, Mike Rapoport,
	linux-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.

Signed-off-by: Pingfan Liu <kernelfans@gmail.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Arnaldo Carvalho de Melo <acme@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Borislav Petkov <bp@alien8.de>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: Omar Sandoval <osandov@fb.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Mike Rapoport <rppt@kernel.org>
To: x86@kernel.org
Cc: linux-kernel@vger.kernel.org
---
 arch/x86/kernel/machine_kexec_32.c | 1 +
 arch/x86/kernel/machine_kexec_64.c | 3 +++
 2 files changed, 4 insertions(+)

diff --git a/arch/x86/kernel/machine_kexec_32.c b/arch/x86/kernel/machine_kexec_32.c
index 64b00b0..72c6100 100644
--- a/arch/x86/kernel/machine_kexec_32.c
+++ b/arch/x86/kernel/machine_kexec_32.c
@@ -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
diff --git a/arch/x86/kernel/machine_kexec_64.c b/arch/x86/kernel/machine_kexec_64.c
index a29a44a..238893e 100644
--- a/arch/x86/kernel/machine_kexec_64.c
+++ b/arch/x86/kernel/machine_kexec_64.c
@@ -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


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/2] events/core: introduce perf_pmu_disable_all() to turn off all PMU
  2020-11-23  5:37 [PATCH 1/2] events/core: introduce perf_pmu_disable_all() to turn off all PMU Pingfan Liu
  2020-11-23  5:37 ` [PATCH 2/2] x86/machine_kexec: disable PMU before jumping to new kernel Pingfan Liu
@ 2020-11-23 14:05 ` Peter Zijlstra
  2020-11-24  8:50   ` Pingfan Liu
  1 sibling, 1 reply; 5+ messages in thread
From: Peter Zijlstra @ 2020-11-23 14:05 UTC (permalink / raw)
  To: Pingfan Liu
  Cc: linux-kernel, Ingo Molnar, Arnaldo Carvalho de Melo,
	Mark Rutland, Alexander Shishkin, Jiri Olsa, Namhyung Kim,
	Thomas Gleixner, Borislav Petkov, H. Peter Anvin, Omar Sandoval,
	Andrew Morton, Mike Rapoport, x86

On Mon, Nov 23, 2020 at 01:37:25PM +0800, Pingfan Liu wrote:

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

-ENOPARSE, -ETOOLONG

> +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.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/2] events/core: introduce perf_pmu_disable_all() to turn off all PMU
  2020-11-23 14:05 ` [PATCH 1/2] events/core: introduce perf_pmu_disable_all() to turn off all PMU Peter Zijlstra
@ 2020-11-24  8:50   ` Pingfan Liu
  0 siblings, 0 replies; 5+ messages in thread
From: Pingfan Liu @ 2020-11-24  8:50 UTC (permalink / raw)
  To: Peter Zijlstra
  Cc: LKML, Ingo Molnar, Arnaldo Carvalho de Melo, Mark Rutland,
	Alexander Shishkin, Jiri Olsa, Namhyung Kim, Thomas Gleixner,
	Borislav Petkov, H. Peter Anvin, Omar Sandoval, Andrew Morton,
	Mike Rapoport, maintainer:X86 ARCHITECTURE (32-BIT AND 64-BIT)

On Mon, Nov 23, 2020 at 10:05 PM Peter Zijlstra <peterz@infradead.org> wrote:
>
> On Mon, Nov 23, 2020 at 01:37:25PM +0800, Pingfan Liu wrote:
>
> > +/* When crashed, other cpus hang in idle loop, so here do an emergency job under no lock */
>
> -ENOPARSE, -ETOOLONG
>
> > +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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 2/2] x86/machine_kexec: disable PMU before jumping to new kernel
  2020-11-23  5:37 ` [PATCH 2/2] x86/machine_kexec: disable PMU before jumping to new kernel Pingfan Liu
@ 2020-11-26  8:05   ` Pingfan Liu
  0 siblings, 0 replies; 5+ messages in thread
From: Pingfan Liu @ 2020-11-26  8:05 UTC (permalink / raw)
  To: maintainer:X86 ARCHITECTURE (32-BIT AND 64-BIT)
  Cc: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Mark Rutland, Alexander Shishkin, Jiri Olsa, Namhyung Kim,
	Thomas Gleixner, Borislav Petkov, H. Peter Anvin, Omar Sandoval,
	Andrew Morton, Mike Rapoport, LKML

Sorry that I had made a misunderstanding of the code.

Nacked it

On Mon, Nov 23, 2020 at 1:37 PM Pingfan Liu <kernelfans@gmail.com> wrote:
>
> 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.
>
> Signed-off-by: Pingfan Liu <kernelfans@gmail.com>
> Cc: Peter Zijlstra <peterz@infradead.org>
> Cc: Ingo Molnar <mingo@redhat.com>
> Cc: Arnaldo Carvalho de Melo <acme@kernel.org>
> Cc: Mark Rutland <mark.rutland@arm.com>
> Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
> Cc: Jiri Olsa <jolsa@redhat.com>
> Cc: Namhyung Kim <namhyung@kernel.org>
> Cc: Thomas Gleixner <tglx@linutronix.de>
> Cc: Borislav Petkov <bp@alien8.de>
> Cc: "H. Peter Anvin" <hpa@zytor.com>
> Cc: Omar Sandoval <osandov@fb.com>
> Cc: Andrew Morton <akpm@linux-foundation.org>
> Cc: Mike Rapoport <rppt@kernel.org>
> To: x86@kernel.org
> Cc: linux-kernel@vger.kernel.org
> ---
>  arch/x86/kernel/machine_kexec_32.c | 1 +
>  arch/x86/kernel/machine_kexec_64.c | 3 +++
>  2 files changed, 4 insertions(+)
>
> diff --git a/arch/x86/kernel/machine_kexec_32.c b/arch/x86/kernel/machine_kexec_32.c
> index 64b00b0..72c6100 100644
> --- a/arch/x86/kernel/machine_kexec_32.c
> +++ b/arch/x86/kernel/machine_kexec_32.c
> @@ -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
> diff --git a/arch/x86/kernel/machine_kexec_64.c b/arch/x86/kernel/machine_kexec_64.c
> index a29a44a..238893e 100644
> --- a/arch/x86/kernel/machine_kexec_64.c
> +++ b/arch/x86/kernel/machine_kexec_64.c
> @@ -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
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2020-11-26  8:05 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-23  5:37 [PATCH 1/2] events/core: introduce perf_pmu_disable_all() to turn off all PMU Pingfan Liu
2020-11-23  5:37 ` [PATCH 2/2] x86/machine_kexec: disable PMU before jumping to new kernel Pingfan Liu
2020-11-26  8:05   ` Pingfan Liu
2020-11-23 14:05 ` [PATCH 1/2] events/core: introduce perf_pmu_disable_all() to turn off all PMU Peter Zijlstra
2020-11-24  8:50   ` Pingfan Liu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).