2013-03-16 00:58:10

by Shuah Khan

[permalink] [raw]
Subject: [PATCH 3.0] perf,x86: fix kernel crash with PEBS/BTS after suspend/resume

This patch fixes a kernel crash when using precise sampling (PEBS)
after a suspend/resume. Turns out the CPU notifier code is not invoked
on CPU0 (BP). Therefore, the DS_AREA (used by PEBS) is not restored properly
by the kernel and keeps it power-on/resume value of 0 causing any PEBS
measurement to crash when running on CPU0.

The workaround is to add a hook in the actual resume code to restore
the DS Area MSR value. It is invoked for all CPUS. So for all but CPU0,
the DS_AREA will be restored twice but this is harmless.

Upstream commit id: 1d9d8639c063caf6efc2447f5f26aa637f844ff6

Reported-by: Linus Torvalds <[email protected]>
Signed-off-by: Stephane Eranian <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
Signed-off-by: Shuah Khan <[email protected]>
CC: [email protected] 3.0
---
arch/x86/kernel/cpu/perf_event_intel_ds.c | 8 ++++++++
arch/x86/power/cpu.c | 2 ++
include/linux/perf_event.h | 2 ++
3 files changed, 12 insertions(+)

diff --git a/arch/x86/kernel/cpu/perf_event_intel_ds.c b/arch/x86/kernel/cpu/perf_event_intel_ds.c
index d812fe2..cf82ee5 100644
--- a/arch/x86/kernel/cpu/perf_event_intel_ds.c
+++ b/arch/x86/kernel/cpu/perf_event_intel_ds.c
@@ -754,6 +754,14 @@ static void intel_ds_init(void)
}
}

+void perf_restore_debug_store(void)
+{
+ if (!x86_pmu.bts && !x86_pmu.pebs)
+ return;
+
+ init_debug_store_on_cpu(smp_processor_id());
+}
+
#else /* CONFIG_CPU_SUP_INTEL */

static void reserve_ds_buffers(void)
diff --git a/arch/x86/power/cpu.c b/arch/x86/power/cpu.c
index 87bb35e..0ea8bd2 100644
--- a/arch/x86/power/cpu.c
+++ b/arch/x86/power/cpu.c
@@ -10,6 +10,7 @@

#include <linux/suspend.h>
#include <linux/smp.h>
+#include <linux/perf_event.h>

#include <asm/pgtable.h>
#include <asm/proto.h>
@@ -224,6 +225,7 @@ static void __restore_processor_state(struct saved_context *ctxt)

do_fpu_end();
mtrr_bp_restore();
+ perf_restore_debug_store();
}

/* Needed by apm.c */
diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h
index effadd6..038ad4a 100644
--- a/include/linux/perf_event.h
+++ b/include/linux/perf_event.h
@@ -1153,6 +1153,7 @@ extern void perf_swevent_put_recursion_context(int rctx);
extern void perf_event_enable(struct perf_event *event);
extern void perf_event_disable(struct perf_event *event);
extern void perf_event_task_tick(void);
+extern void perf_restore_debug_store(void);
#else
static inline void
perf_event_task_sched_in(struct task_struct *task) { }
@@ -1187,6 +1188,7 @@ static inline void perf_swevent_put_recursion_context(int rctx) { }
static inline void perf_event_enable(struct perf_event *event) { }
static inline void perf_event_disable(struct perf_event *event) { }
static inline void perf_event_task_tick(void) { }
+static inline void perf_restore_debug_store(void) { }
#endif

#define perf_output_put(handle, x) perf_output_copy((handle), &(x), sizeof(x))
--
1.7.9.5



2013-03-21 02:42:30

by Ben Hutchings

[permalink] [raw]
Subject: Re: [PATCH 3.0] perf,x86: fix kernel crash with PEBS/BTS after suspend/resume

On Fri, 2013-03-15 at 18:57 -0600, Shuah Khan wrote:
> This patch fixes a kernel crash when using precise sampling (PEBS)
> after a suspend/resume. Turns out the CPU notifier code is not invoked
> on CPU0 (BP). Therefore, the DS_AREA (used by PEBS) is not restored properly
> by the kernel and keeps it power-on/resume value of 0 causing any PEBS
> measurement to crash when running on CPU0.
>
> The workaround is to add a hook in the actual resume code to restore
> the DS Area MSR value. It is invoked for all CPUS. So for all but CPU0,
> the DS_AREA will be restored twice but this is harmless.
>
> Upstream commit id: 1d9d8639c063caf6efc2447f5f26aa637f844ff6
>
> Reported-by: Linus Torvalds <[email protected]>
> Signed-off-by: Stephane Eranian <[email protected]>
> Signed-off-by: Linus Torvalds <[email protected]>
> Signed-off-by: Shuah Khan <[email protected]>
> CC: [email protected] 3.0
[...]

I've queued this up for 3.2 (though context was different again), along
with the following fix-ups:

commit 2a6e06b2aed6995af401dcd4feb5e79a0c7ea554
Author: Linus Torvalds <[email protected]>
Date: Sun Mar 17 15:44:43 2013 -0700

perf,x86: fix wrmsr_on_cpu() warning on suspend/resume

commit 6c4d3bc99b3341067775efd4d9d13cc8e655fd7c
Author: David Rientjes <[email protected]>
Date: Sun Mar 17 15:49:10 2013 -0700

perf,x86: fix link failure for non-Intel configs

Ben.

--
Ben Hutchings
It is easier to write an incorrect program than to understand a correct one.


Attachments:
signature.asc (828.00 B)
This is a digitally signed message part