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); |
639fe4b1 | 13 | EXPORT_PER_CPU_SYMBOL_GPL(perf_trace_regs); |
20ab4425 | 14 | |
dcd5c166 | 15 | EXPORT_SYMBOL_GPL(perf_arch_fetch_caller_regs); |
20ab4425 | 16 | |
430ad5a6 XG |
17 | static char *perf_trace_buf; |
18 | static char *perf_trace_buf_nmi; | |
20ab4425 | 19 | |
eb1e7961 FW |
20 | /* |
21 | * Force it to be aligned to unsigned long to avoid misaligned accesses | |
22 | * suprises | |
23 | */ | |
24 | typedef typeof(unsigned long [PERF_MAX_TRACE_SIZE / sizeof(unsigned long)]) | |
25 | perf_trace_t; | |
ce71b9df | 26 | |
20ab4425 | 27 | /* Count the events in use (per event id, not per instance) */ |
97d5a220 | 28 | static int total_ref_count; |
20ab4425 | 29 | |
97d5a220 | 30 | static int perf_trace_event_enable(struct ftrace_event_call *event) |
e5e25cf4 | 31 | { |
ce71b9df | 32 | char *buf; |
20ab4425 FW |
33 | int ret = -ENOMEM; |
34 | ||
97d5a220 | 35 | if (event->perf_refcount++ > 0) |
e5e25cf4 FW |
36 | return 0; |
37 | ||
97d5a220 | 38 | if (!total_ref_count) { |
ce71b9df | 39 | buf = (char *)alloc_percpu(perf_trace_t); |
20ab4425 FW |
40 | if (!buf) |
41 | goto fail_buf; | |
42 | ||
444a2a3b | 43 | rcu_assign_pointer(perf_trace_buf, buf); |
20ab4425 | 44 | |
ce71b9df | 45 | buf = (char *)alloc_percpu(perf_trace_t); |
20ab4425 FW |
46 | if (!buf) |
47 | goto fail_buf_nmi; | |
48 | ||
444a2a3b | 49 | rcu_assign_pointer(perf_trace_buf_nmi, buf); |
20ab4425 FW |
50 | } |
51 | ||
97d5a220 | 52 | ret = event->perf_event_enable(event); |
fe8e5b5a | 53 | if (!ret) { |
97d5a220 | 54 | total_ref_count++; |
20ab4425 | 55 | return 0; |
fe8e5b5a | 56 | } |
20ab4425 | 57 | |
20ab4425 | 58 | fail_buf_nmi: |
97d5a220 | 59 | if (!total_ref_count) { |
444a2a3b FW |
60 | free_percpu(perf_trace_buf_nmi); |
61 | free_percpu(perf_trace_buf); | |
62 | perf_trace_buf_nmi = NULL; | |
63 | perf_trace_buf = NULL; | |
fe8e5b5a | 64 | } |
20ab4425 | 65 | fail_buf: |
97d5a220 | 66 | event->perf_refcount--; |
20ab4425 FW |
67 | |
68 | return ret; | |
e5e25cf4 FW |
69 | } |
70 | ||
97d5a220 | 71 | int perf_trace_enable(int event_id) |
ac199db0 PZ |
72 | { |
73 | struct ftrace_event_call *event; | |
20c8928a | 74 | int ret = -EINVAL; |
ac199db0 | 75 | |
20c8928a | 76 | mutex_lock(&event_mutex); |
a59fd602 | 77 | list_for_each_entry(event, &ftrace_events, list) { |
97d5a220 | 78 | if (event->id == event_id && event->perf_event_enable && |
558e6547 | 79 | try_module_get(event->mod)) { |
97d5a220 | 80 | ret = perf_trace_event_enable(event); |
20c8928a LZ |
81 | break; |
82 | } | |
ac199db0 | 83 | } |
20c8928a | 84 | mutex_unlock(&event_mutex); |
ac199db0 | 85 | |
20c8928a | 86 | return ret; |
ac199db0 PZ |
87 | } |
88 | ||
97d5a220 | 89 | static void perf_trace_event_disable(struct ftrace_event_call *event) |
e5e25cf4 | 90 | { |
ce71b9df | 91 | char *buf, *nmi_buf; |
20ab4425 | 92 | |
97d5a220 | 93 | if (--event->perf_refcount > 0) |
e5e25cf4 FW |
94 | return; |
95 | ||
97d5a220 | 96 | event->perf_event_disable(event); |
20ab4425 | 97 | |
97d5a220 | 98 | if (!--total_ref_count) { |
444a2a3b FW |
99 | buf = perf_trace_buf; |
100 | rcu_assign_pointer(perf_trace_buf, NULL); | |
20ab4425 | 101 | |
444a2a3b FW |
102 | nmi_buf = perf_trace_buf_nmi; |
103 | rcu_assign_pointer(perf_trace_buf_nmi, NULL); | |
20ab4425 FW |
104 | |
105 | /* | |
106 | * Ensure every events in profiling have finished before | |
107 | * releasing the buffers | |
108 | */ | |
109 | synchronize_sched(); | |
110 | ||
111 | free_percpu(buf); | |
112 | free_percpu(nmi_buf); | |
113 | } | |
e5e25cf4 FW |
114 | } |
115 | ||
97d5a220 | 116 | void perf_trace_disable(int event_id) |
ac199db0 PZ |
117 | { |
118 | struct ftrace_event_call *event; | |
119 | ||
20c8928a | 120 | mutex_lock(&event_mutex); |
a59fd602 | 121 | list_for_each_entry(event, &ftrace_events, list) { |
20c8928a | 122 | if (event->id == event_id) { |
97d5a220 | 123 | perf_trace_event_disable(event); |
558e6547 | 124 | module_put(event->mod); |
20c8928a LZ |
125 | break; |
126 | } | |
ac199db0 | 127 | } |
20c8928a | 128 | mutex_unlock(&event_mutex); |
ac199db0 | 129 | } |
430ad5a6 | 130 | |
97d5a220 FW |
131 | __kprobes void *perf_trace_buf_prepare(int size, unsigned short type, |
132 | int *rctxp, unsigned long *irq_flags) | |
430ad5a6 XG |
133 | { |
134 | struct trace_entry *entry; | |
135 | char *trace_buf, *raw_data; | |
136 | int pc, cpu; | |
137 | ||
eb1e7961 FW |
138 | BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(unsigned long)); |
139 | ||
430ad5a6 XG |
140 | pc = preempt_count(); |
141 | ||
142 | /* Protect the per cpu buffer, begin the rcu read side */ | |
143 | local_irq_save(*irq_flags); | |
144 | ||
145 | *rctxp = perf_swevent_get_recursion_context(); | |
146 | if (*rctxp < 0) | |
147 | goto err_recursion; | |
148 | ||
149 | cpu = smp_processor_id(); | |
150 | ||
151 | if (in_nmi()) | |
8d53dd54 | 152 | trace_buf = rcu_dereference_sched(perf_trace_buf_nmi); |
430ad5a6 | 153 | else |
8d53dd54 | 154 | trace_buf = rcu_dereference_sched(perf_trace_buf); |
430ad5a6 XG |
155 | |
156 | if (!trace_buf) | |
157 | goto err; | |
158 | ||
159 | raw_data = per_cpu_ptr(trace_buf, cpu); | |
160 | ||
161 | /* zero the dead bytes from align to not leak stack to user */ | |
eb1e7961 | 162 | memset(&raw_data[size - sizeof(u64)], 0, sizeof(u64)); |
430ad5a6 XG |
163 | |
164 | entry = (struct trace_entry *)raw_data; | |
165 | tracing_generic_entry_update(entry, *irq_flags, pc); | |
166 | entry->type = type; | |
167 | ||
168 | return raw_data; | |
169 | err: | |
170 | perf_swevent_put_recursion_context(*rctxp); | |
171 | err_recursion: | |
172 | local_irq_restore(*irq_flags); | |
173 | return NULL; | |
174 | } | |
97d5a220 | 175 | EXPORT_SYMBOL_GPL(perf_trace_buf_prepare); |