Commit c530665c authored by Frederic Weisbecker's avatar Frederic Weisbecker

perf: Take a hot regs snapshot for trace events

We are taking a wrong regs snapshot when a trace event triggers.
Either we use get_irq_regs(), which gives us the interrupted
registers if we are in an interrupt, or we use task_pt_regs()
which gives us the state before we entered the kernel, assuming
we are lucky enough to be no kernel thread, in which case
task_pt_regs() returns the initial set of regs when the kernel
thread was started.

What we want is different. We need a hot snapshot of the regs,
so that we can get the instruction pointer to record in the
sample, the frame pointer for the callchain, and some other
things.

Let's use the new perf_fetch_caller_regs() for that.

Comparison with perf record -e lock: -R -a -f -g
Before:

        perf  [kernel]                   [k] __do_softirq
               |
               --- __do_softirq
                  |
                  |--55.16%-- __open
                  |
                   --44.84%-- __write_nocancel

After:

            perf  [kernel]           [k] perf_tp_event
               |
               --- perf_tp_event
                  |
                  |--41.07%-- lock_acquire
                  |          |
                  |          |--39.36%-- _raw_spin_lock
                  |          |          |
                  |          |          |--7.81%-- hrtimer_interrupt
                  |          |          |          smp_apic_timer_interrupt
                  |          |          |          apic_timer_interrupt

The old case was producing unreliable callchains. Now having
right frame and instruction pointers, we have the trace we
want.

Also syscalls and kprobe events already have the right regs,
let's use them instead of wasting a retrieval.

v2: Follow the rename perf_save_regs() -> perf_fetch_caller_regs()
Signed-off-by: default avatarFrederic Weisbecker <fweisbec@gmail.com>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: H. Peter Anvin <hpa@zytor.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Masami Hiramatsu <mhiramat@redhat.com>
Cc: Jason Baron <jbaron@redhat.com>
Cc: Archs <linux-arch@vger.kernel.org>
parent 5331d7b8
...@@ -187,6 +187,9 @@ do { \ ...@@ -187,6 +187,9 @@ do { \
#ifdef CONFIG_PERF_EVENTS #ifdef CONFIG_PERF_EVENTS
struct perf_event; struct perf_event;
DECLARE_PER_CPU(struct pt_regs, perf_trace_regs);
extern int ftrace_profile_enable(int event_id); extern int ftrace_profile_enable(int event_id);
extern void ftrace_profile_disable(int event_id); extern void ftrace_profile_disable(int event_id);
extern int ftrace_profile_set_filter(struct perf_event *event, int event_id, extern int ftrace_profile_set_filter(struct perf_event *event, int event_id,
...@@ -198,11 +201,11 @@ ftrace_perf_buf_prepare(int size, unsigned short type, int *rctxp, ...@@ -198,11 +201,11 @@ ftrace_perf_buf_prepare(int size, unsigned short type, int *rctxp,
static inline void static inline void
ftrace_perf_buf_submit(void *raw_data, int size, int rctx, u64 addr, ftrace_perf_buf_submit(void *raw_data, int size, int rctx, u64 addr,
u64 count, unsigned long irq_flags) u64 count, unsigned long irq_flags, struct pt_regs *regs)
{ {
struct trace_entry *entry = raw_data; struct trace_entry *entry = raw_data;
perf_tp_event(entry->type, addr, count, raw_data, size); perf_tp_event(entry->type, addr, count, raw_data, size, regs);
perf_swevent_put_recursion_context(rctx); perf_swevent_put_recursion_context(rctx);
local_irq_restore(irq_flags); local_irq_restore(irq_flags);
} }
......
...@@ -764,6 +764,7 @@ ftrace_profile_templ_##call(struct ftrace_event_call *event_call, \ ...@@ -764,6 +764,7 @@ ftrace_profile_templ_##call(struct ftrace_event_call *event_call, \
struct ftrace_raw_##call *entry; \ struct ftrace_raw_##call *entry; \
u64 __addr = 0, __count = 1; \ u64 __addr = 0, __count = 1; \
unsigned long irq_flags; \ unsigned long irq_flags; \
struct pt_regs *__regs; \
int __entry_size; \ int __entry_size; \
int __data_size; \ int __data_size; \
int rctx; \ int rctx; \
...@@ -784,8 +785,11 @@ ftrace_profile_templ_##call(struct ftrace_event_call *event_call, \ ...@@ -784,8 +785,11 @@ ftrace_profile_templ_##call(struct ftrace_event_call *event_call, \
\ \
{ assign; } \ { assign; } \
\ \
__regs = &__get_cpu_var(perf_trace_regs); \
perf_fetch_caller_regs(__regs, 2); \
\
ftrace_perf_buf_submit(entry, __entry_size, rctx, __addr, \ ftrace_perf_buf_submit(entry, __entry_size, rctx, __addr, \
__count, irq_flags); \ __count, irq_flags, __regs); \
} }
#undef DEFINE_EVENT #undef DEFINE_EVENT
......
...@@ -4318,9 +4318,8 @@ static const struct pmu perf_ops_task_clock = { ...@@ -4318,9 +4318,8 @@ static const struct pmu perf_ops_task_clock = {
#ifdef CONFIG_EVENT_TRACING #ifdef CONFIG_EVENT_TRACING
void perf_tp_event(int event_id, u64 addr, u64 count, void *record, void perf_tp_event(int event_id, u64 addr, u64 count, void *record,
int entry_size) int entry_size, struct pt_regs *regs)
{ {
struct pt_regs *regs = get_irq_regs();
struct perf_sample_data data; struct perf_sample_data data;
struct perf_raw_record raw = { struct perf_raw_record raw = {
.size = entry_size, .size = entry_size,
...@@ -4330,12 +4329,9 @@ void perf_tp_event(int event_id, u64 addr, u64 count, void *record, ...@@ -4330,12 +4329,9 @@ void perf_tp_event(int event_id, u64 addr, u64 count, void *record,
perf_sample_data_init(&data, addr); perf_sample_data_init(&data, addr);
data.raw = &raw; data.raw = &raw;
if (!regs)
regs = task_pt_regs(current);
/* Trace events already protected against recursion */ /* Trace events already protected against recursion */
do_perf_sw_event(PERF_TYPE_TRACEPOINT, event_id, count, 1, do_perf_sw_event(PERF_TYPE_TRACEPOINT, event_id, count, 1,
&data, regs); &data, regs);
} }
EXPORT_SYMBOL_GPL(perf_tp_event); EXPORT_SYMBOL_GPL(perf_tp_event);
......
...@@ -2,13 +2,14 @@ ...@@ -2,13 +2,14 @@
* trace event based perf counter profiling * trace event based perf counter profiling
* *
* Copyright (C) 2009 Red Hat Inc, Peter Zijlstra <pzijlstr@redhat.com> * Copyright (C) 2009 Red Hat Inc, Peter Zijlstra <pzijlstr@redhat.com>
* * Copyright (C) 2009-2010 Frederic Weisbecker <fweisbec@gmail.com>
*/ */
#include <linux/module.h> #include <linux/module.h>
#include <linux/kprobes.h> #include <linux/kprobes.h>
#include "trace.h" #include "trace.h"
DEFINE_PER_CPU(struct pt_regs, perf_trace_regs);
static char *perf_trace_buf; static char *perf_trace_buf;
static char *perf_trace_buf_nmi; static char *perf_trace_buf_nmi;
......
...@@ -1240,7 +1240,7 @@ static __kprobes void kprobe_profile_func(struct kprobe *kp, ...@@ -1240,7 +1240,7 @@ static __kprobes void kprobe_profile_func(struct kprobe *kp,
for (i = 0; i < tp->nr_args; i++) for (i = 0; i < tp->nr_args; i++)
entry->args[i] = call_fetch(&tp->args[i].fetch, regs); entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
ftrace_perf_buf_submit(entry, size, rctx, entry->ip, 1, irq_flags); ftrace_perf_buf_submit(entry, size, rctx, entry->ip, 1, irq_flags, regs);
} }
/* Kretprobe profile handler */ /* Kretprobe profile handler */
...@@ -1271,7 +1271,8 @@ static __kprobes void kretprobe_profile_func(struct kretprobe_instance *ri, ...@@ -1271,7 +1271,8 @@ static __kprobes void kretprobe_profile_func(struct kretprobe_instance *ri,
for (i = 0; i < tp->nr_args; i++) for (i = 0; i < tp->nr_args; i++)
entry->args[i] = call_fetch(&tp->args[i].fetch, regs); entry->args[i] = call_fetch(&tp->args[i].fetch, regs);
ftrace_perf_buf_submit(entry, size, rctx, entry->ret_ip, 1, irq_flags); ftrace_perf_buf_submit(entry, size, rctx, entry->ret_ip, 1,
irq_flags, regs);
} }
static int probe_profile_enable(struct ftrace_event_call *call) static int probe_profile_enable(struct ftrace_event_call *call)
......
...@@ -467,7 +467,7 @@ static void prof_syscall_enter(struct pt_regs *regs, long id) ...@@ -467,7 +467,7 @@ static void prof_syscall_enter(struct pt_regs *regs, long id)
rec->nr = syscall_nr; rec->nr = syscall_nr;
syscall_get_arguments(current, regs, 0, sys_data->nb_args, syscall_get_arguments(current, regs, 0, sys_data->nb_args,
(unsigned long *)&rec->args); (unsigned long *)&rec->args);
ftrace_perf_buf_submit(rec, size, rctx, 0, 1, flags); ftrace_perf_buf_submit(rec, size, rctx, 0, 1, flags, regs);
} }
int prof_sysenter_enable(struct ftrace_event_call *call) int prof_sysenter_enable(struct ftrace_event_call *call)
...@@ -542,7 +542,7 @@ static void prof_syscall_exit(struct pt_regs *regs, long ret) ...@@ -542,7 +542,7 @@ static void prof_syscall_exit(struct pt_regs *regs, long ret)
rec->nr = syscall_nr; rec->nr = syscall_nr;
rec->ret = syscall_get_return_value(current, regs); rec->ret = syscall_get_return_value(current, regs);
ftrace_perf_buf_submit(rec, size, rctx, 0, 1, flags); ftrace_perf_buf_submit(rec, size, rctx, 0, 1, flags, regs);
} }
int prof_sysexit_enable(struct ftrace_event_call *call) int prof_sysexit_enable(struct ftrace_event_call *call)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment