Commit | Line | Data |
---|---|---|
ac199db0 | 1 | /* |
97d5a220 | 2 | * trace event based perf event profiling/tracing |
ac199db0 PZ |
3 | * |
4 | * Copyright (C) 2009 Red Hat Inc, Peter Zijlstra <pzijlstr@redhat.com> | |
c530665c | 5 | * Copyright (C) 2009-2010 Frederic Weisbecker <fweisbec@gmail.com> |
ac199db0 PZ |
6 | */ |
7 | ||
558e6547 | 8 | #include <linux/module.h> |
430ad5a6 | 9 | #include <linux/kprobes.h> |
ac199db0 PZ |
10 | #include "trace.h" |
11 | ||
c530665c | 12 | DEFINE_PER_CPU(struct pt_regs, perf_trace_regs); |
20ab4425 | 13 | |
430ad5a6 XG |
14 | static char *perf_trace_buf; |
15 | static char *perf_trace_buf_nmi; | |
20ab4425 | 16 | |
97d5a220 | 17 | typedef typeof(char [PERF_MAX_TRACE_SIZE]) perf_trace_t ; |
ce71b9df | 18 | |
20ab4425 | 19 | /* Count the events in use (per event id, not per instance) */ |
97d5a220 | 20 | static int total_ref_count; |
20ab4425 | 21 | |
97d5a220 | 22 | static int perf_trace_event_enable(struct ftrace_event_call *event) |
e5e25cf4 | 23 | { |
ce71b9df | 24 | char *buf; |
20ab4425 FW |
25 | int ret = -ENOMEM; |
26 | ||
97d5a220 | 27 | if (event->perf_refcount++ > 0) |
e5e25cf4 FW |
28 | return 0; |
29 | ||
97d5a220 | 30 | if (!total_ref_count) { |
ce71b9df | 31 | buf = (char *)alloc_percpu(perf_trace_t); |
20ab4425 FW |
32 | if (!buf) |
33 | goto fail_buf; | |
34 | ||
444a2a3b | 35 | rcu_assign_pointer(perf_trace_buf, buf); |
20ab4425 | 36 | |
ce71b9df | 37 | buf = (char *)alloc_percpu(perf_trace_t); |
20ab4425 FW |
38 | if (!buf) |
39 | goto fail_buf_nmi; | |
40 | ||
444a2a3b | 41 | rcu_assign_pointer(perf_trace_buf_nmi, buf); |
20ab4425 FW |
42 | } |
43 | ||
97d5a220 | 44 | ret = event->perf_event_enable(event); |
fe8e5b5a | 45 | if (!ret) { |
97d5a220 | 46 | total_ref_count++; |
20ab4425 | 47 | return 0; |
fe8e5b5a | 48 | } |
20ab4425 | 49 | |
20ab4425 | 50 | fail_buf_nmi: |
97d5a220 | 51 | if (!total_ref_count) { |
444a2a3b FW |
52 | free_percpu(perf_trace_buf_nmi); |
53 | free_percpu(perf_trace_buf); | |
54 | perf_trace_buf_nmi = NULL; | |
55 | perf_trace_buf = NULL; | |
fe8e5b5a | 56 | } |
20ab4425 | 57 | fail_buf: |
97d5a220 | 58 | event->perf_refcount--; |
20ab4425 FW |
59 | |
60 | return ret; | |
e5e25cf4 FW |
61 | } |
62 | ||
97d5a220 | 63 | int perf_trace_enable(int event_id) |
ac199db0 PZ |
64 | { |
65 | struct ftrace_event_call *event; | |
20c8928a | 66 | int ret = -EINVAL; |
ac199db0 | 67 | |
20c8928a | 68 | mutex_lock(&event_mutex); |
a59fd602 | 69 | list_for_each_entry(event, &ftrace_events, list) { |
97d5a220 | 70 | if (event->id == event_id && event->perf_event_enable && |
558e6547 | 71 | try_module_get(event->mod)) { |
97d5a220 | 72 | ret = perf_trace_event_enable(event); |
20c8928a LZ |
73 | break; |
74 | } | |
ac199db0 | 75 | } |
20c8928a | 76 | mutex_unlock(&event_mutex); |
ac199db0 | 77 | |
20c8928a | 78 | return ret; |
ac199db0 PZ |
79 | } |
80 | ||
97d5a220 | 81 | static void perf_trace_event_disable(struct ftrace_event_call *event) |
e5e25cf4 | 82 | { |
ce71b9df | 83 | char *buf, *nmi_buf; |
20ab4425 | 84 | |
97d5a220 | 85 | if (--event->perf_refcount > 0) |
e5e25cf4 FW |
86 | return; |
87 | ||
97d5a220 | 88 | event->perf_event_disable(event); |
20ab4425 | 89 | |
97d5a220 | 90 | if (!--total_ref_count) { |
444a2a3b FW |
91 | buf = perf_trace_buf; |
92 | rcu_assign_pointer(perf_trace_buf, NULL); | |
20ab4425 | 93 | |
444a2a3b FW |
94 | nmi_buf = perf_trace_buf_nmi; |
95 | rcu_assign_pointer(perf_trace_buf_nmi, NULL); | |
20ab4425 FW |
96 | |
97 | /* | |
98 | * Ensure every events in profiling have finished before | |
99 | * releasing the buffers | |
100 | */ | |
101 | synchronize_sched(); | |
102 | ||
103 | free_percpu(buf); | |
104 | free_percpu(nmi_buf); | |
105 | } | |
e5e25cf4 FW |
106 | } |
107 | ||
97d5a220 | 108 | void perf_trace_disable(int event_id) |
ac199db0 PZ |
109 | { |
110 | struct ftrace_event_call *event; | |
111 | ||
20c8928a | 112 | mutex_lock(&event_mutex); |
a59fd602 | 113 | list_for_each_entry(event, &ftrace_events, list) { |
20c8928a | 114 | if (event->id == event_id) { |
97d5a220 | 115 | perf_trace_event_disable(event); |
558e6547 | 116 | module_put(event->mod); |
20c8928a LZ |
117 | break; |
118 | } | |
ac199db0 | 119 | } |
20c8928a | 120 | mutex_unlock(&event_mutex); |
ac199db0 | 121 | } |
430ad5a6 | 122 | |
97d5a220 FW |
123 | __kprobes void *perf_trace_buf_prepare(int size, unsigned short type, |
124 | int *rctxp, unsigned long *irq_flags) | |
430ad5a6 XG |
125 | { |
126 | struct trace_entry *entry; | |
127 | char *trace_buf, *raw_data; | |
128 | int pc, cpu; | |
129 | ||
130 | pc = preempt_count(); | |
131 | ||
132 | /* Protect the per cpu buffer, begin the rcu read side */ | |
133 | local_irq_save(*irq_flags); | |
134 | ||
135 | *rctxp = perf_swevent_get_recursion_context(); | |
136 | if (*rctxp < 0) | |
137 | goto err_recursion; | |
138 | ||
139 | cpu = smp_processor_id(); | |
140 | ||
141 | if (in_nmi()) | |
142 | trace_buf = rcu_dereference(perf_trace_buf_nmi); | |
143 | else | |
144 | trace_buf = rcu_dereference(perf_trace_buf); | |
145 | ||
146 | if (!trace_buf) | |
147 | goto err; | |
148 | ||
149 | raw_data = per_cpu_ptr(trace_buf, cpu); | |
150 | ||
151 | /* zero the dead bytes from align to not leak stack to user */ | |
152 | *(u64 *)(&raw_data[size - sizeof(u64)]) = 0ULL; | |
153 | ||
154 | entry = (struct trace_entry *)raw_data; | |
155 | tracing_generic_entry_update(entry, *irq_flags, pc); | |
156 | entry->type = type; | |
157 | ||
158 | return raw_data; | |
159 | err: | |
160 | perf_swevent_put_recursion_context(*rctxp); | |
161 | err_recursion: | |
162 | local_irq_restore(*irq_flags); | |
163 | return NULL; | |
164 | } | |
97d5a220 | 165 | EXPORT_SYMBOL_GPL(perf_trace_buf_prepare); |