]>
Commit | Line | Data |
---|---|---|
ac199db0 | 1 | /* |
97d5a220 | 2 | * trace event based perf event profiling/tracing |
ac199db0 | 3 | * |
90eec103 | 4 | * Copyright (C) 2009 Red Hat Inc, Peter Zijlstra |
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 | ||
6016ee13 | 12 | static char __percpu *perf_trace_buf[PERF_NR_CONTEXTS]; |
20ab4425 | 13 | |
eb1e7961 FW |
14 | /* |
15 | * Force it to be aligned to unsigned long to avoid misaligned accesses | |
16 | * suprises | |
17 | */ | |
18 | typedef typeof(unsigned long [PERF_MAX_TRACE_SIZE / sizeof(unsigned long)]) | |
19 | perf_trace_t; | |
ce71b9df | 20 | |
20ab4425 | 21 | /* Count the events in use (per event id, not per instance) */ |
97d5a220 | 22 | static int total_ref_count; |
20ab4425 | 23 | |
2425bcb9 | 24 | static int perf_trace_event_perm(struct trace_event_call *tp_event, |
61c32659 FW |
25 | struct perf_event *p_event) |
26 | { | |
d5b5f391 PZ |
27 | if (tp_event->perf_perm) { |
28 | int ret = tp_event->perf_perm(tp_event, p_event); | |
29 | if (ret) | |
30 | return ret; | |
31 | } | |
32 | ||
f4be073d JO |
33 | /* |
34 | * We checked and allowed to create parent, | |
35 | * allow children without checking. | |
36 | */ | |
37 | if (p_event->parent) | |
38 | return 0; | |
39 | ||
40 | /* | |
41 | * It's ok to check current process (owner) permissions in here, | |
42 | * because code below is called only via perf_event_open syscall. | |
43 | */ | |
44 | ||
ced39002 | 45 | /* The ftrace function trace is allowed only for root. */ |
cfa77bc4 JO |
46 | if (ftrace_event_is_function(tp_event)) { |
47 | if (perf_paranoid_tracepoint_raw() && !capable(CAP_SYS_ADMIN)) | |
48 | return -EPERM; | |
49 | ||
0a74c5b3 JO |
50 | if (!is_sampling_event(p_event)) |
51 | return 0; | |
52 | ||
cfa77bc4 JO |
53 | /* |
54 | * We don't allow user space callchains for function trace | |
55 | * event, due to issues with page faults while tracing page | |
56 | * fault handler and its overall trickiness nature. | |
57 | */ | |
58 | if (!p_event->attr.exclude_callchain_user) | |
59 | return -EINVAL; | |
63c45f4b JO |
60 | |
61 | /* | |
62 | * Same reason to disable user stack dump as for user space | |
63 | * callchains above. | |
64 | */ | |
65 | if (p_event->attr.sample_type & PERF_SAMPLE_STACK_USER) | |
66 | return -EINVAL; | |
cfa77bc4 | 67 | } |
ced39002 | 68 | |
61c32659 FW |
69 | /* No tracing, just counting, so no obvious leak */ |
70 | if (!(p_event->attr.sample_type & PERF_SAMPLE_RAW)) | |
71 | return 0; | |
72 | ||
73 | /* Some events are ok to be traced by non-root users... */ | |
74 | if (p_event->attach_state == PERF_ATTACH_TASK) { | |
75 | if (tp_event->flags & TRACE_EVENT_FL_CAP_ANY) | |
76 | return 0; | |
77 | } | |
78 | ||
79 | /* | |
80 | * ...otherwise raw tracepoint data can be a severe data leak, | |
81 | * only allow root to have these. | |
82 | */ | |
83 | if (perf_paranoid_tracepoint_raw() && !capable(CAP_SYS_ADMIN)) | |
84 | return -EPERM; | |
85 | ||
86 | return 0; | |
87 | } | |
88 | ||
2425bcb9 | 89 | static int perf_trace_event_reg(struct trace_event_call *tp_event, |
ceec0b6f | 90 | struct perf_event *p_event) |
e5e25cf4 | 91 | { |
6016ee13 | 92 | struct hlist_head __percpu *list; |
ceec0b6f | 93 | int ret = -ENOMEM; |
1c024eca | 94 | int cpu; |
20ab4425 | 95 | |
1c024eca PZ |
96 | p_event->tp_event = tp_event; |
97 | if (tp_event->perf_refcount++ > 0) | |
e5e25cf4 FW |
98 | return 0; |
99 | ||
1c024eca PZ |
100 | list = alloc_percpu(struct hlist_head); |
101 | if (!list) | |
102 | goto fail; | |
103 | ||
104 | for_each_possible_cpu(cpu) | |
105 | INIT_HLIST_HEAD(per_cpu_ptr(list, cpu)); | |
20ab4425 | 106 | |
1c024eca | 107 | tp_event->perf_events = list; |
e5e25cf4 | 108 | |
97d5a220 | 109 | if (!total_ref_count) { |
6016ee13 | 110 | char __percpu *buf; |
b7e2ecef | 111 | int i; |
20ab4425 | 112 | |
7ae07ea3 | 113 | for (i = 0; i < PERF_NR_CONTEXTS; i++) { |
6016ee13 | 114 | buf = (char __percpu *)alloc_percpu(perf_trace_t); |
b7e2ecef | 115 | if (!buf) |
1c024eca | 116 | goto fail; |
20ab4425 | 117 | |
1c024eca | 118 | perf_trace_buf[i] = buf; |
b7e2ecef | 119 | } |
20ab4425 FW |
120 | } |
121 | ||
ceec0b6f | 122 | ret = tp_event->class->reg(tp_event, TRACE_REG_PERF_REGISTER, NULL); |
1c024eca PZ |
123 | if (ret) |
124 | goto fail; | |
20ab4425 | 125 | |
1c024eca PZ |
126 | total_ref_count++; |
127 | return 0; | |
128 | ||
129 | fail: | |
97d5a220 | 130 | if (!total_ref_count) { |
b7e2ecef PZ |
131 | int i; |
132 | ||
7ae07ea3 | 133 | for (i = 0; i < PERF_NR_CONTEXTS; i++) { |
b7e2ecef PZ |
134 | free_percpu(perf_trace_buf[i]); |
135 | perf_trace_buf[i] = NULL; | |
136 | } | |
fe8e5b5a | 137 | } |
1c024eca PZ |
138 | |
139 | if (!--tp_event->perf_refcount) { | |
140 | free_percpu(tp_event->perf_events); | |
141 | tp_event->perf_events = NULL; | |
fe8e5b5a | 142 | } |
20ab4425 FW |
143 | |
144 | return ret; | |
e5e25cf4 FW |
145 | } |
146 | ||
ceec0b6f JO |
147 | static void perf_trace_event_unreg(struct perf_event *p_event) |
148 | { | |
2425bcb9 | 149 | struct trace_event_call *tp_event = p_event->tp_event; |
ceec0b6f JO |
150 | int i; |
151 | ||
152 | if (--tp_event->perf_refcount > 0) | |
153 | goto out; | |
154 | ||
155 | tp_event->class->reg(tp_event, TRACE_REG_PERF_UNREGISTER, NULL); | |
156 | ||
157 | /* | |
158 | * Ensure our callback won't be called anymore. The buffers | |
159 | * will be freed after that. | |
160 | */ | |
161 | tracepoint_synchronize_unregister(); | |
162 | ||
163 | free_percpu(tp_event->perf_events); | |
164 | tp_event->perf_events = NULL; | |
165 | ||
166 | if (!--total_ref_count) { | |
167 | for (i = 0; i < PERF_NR_CONTEXTS; i++) { | |
168 | free_percpu(perf_trace_buf[i]); | |
169 | perf_trace_buf[i] = NULL; | |
170 | } | |
171 | } | |
172 | out: | |
173 | module_put(tp_event->mod); | |
174 | } | |
175 | ||
176 | static int perf_trace_event_open(struct perf_event *p_event) | |
177 | { | |
2425bcb9 | 178 | struct trace_event_call *tp_event = p_event->tp_event; |
ceec0b6f JO |
179 | return tp_event->class->reg(tp_event, TRACE_REG_PERF_OPEN, p_event); |
180 | } | |
181 | ||
182 | static void perf_trace_event_close(struct perf_event *p_event) | |
183 | { | |
2425bcb9 | 184 | struct trace_event_call *tp_event = p_event->tp_event; |
ceec0b6f JO |
185 | tp_event->class->reg(tp_event, TRACE_REG_PERF_CLOSE, p_event); |
186 | } | |
187 | ||
2425bcb9 | 188 | static int perf_trace_event_init(struct trace_event_call *tp_event, |
ceec0b6f JO |
189 | struct perf_event *p_event) |
190 | { | |
191 | int ret; | |
192 | ||
193 | ret = perf_trace_event_perm(tp_event, p_event); | |
194 | if (ret) | |
195 | return ret; | |
196 | ||
197 | ret = perf_trace_event_reg(tp_event, p_event); | |
198 | if (ret) | |
199 | return ret; | |
200 | ||
201 | ret = perf_trace_event_open(p_event); | |
202 | if (ret) { | |
203 | perf_trace_event_unreg(p_event); | |
204 | return ret; | |
205 | } | |
206 | ||
207 | return 0; | |
208 | } | |
209 | ||
1c024eca | 210 | int perf_trace_init(struct perf_event *p_event) |
ac199db0 | 211 | { |
2425bcb9 | 212 | struct trace_event_call *tp_event; |
0022cedd | 213 | u64 event_id = p_event->attr.config; |
20c8928a | 214 | int ret = -EINVAL; |
ac199db0 | 215 | |
20c8928a | 216 | mutex_lock(&event_mutex); |
1c024eca | 217 | list_for_each_entry(tp_event, &ftrace_events, list) { |
ff5f149b | 218 | if (tp_event->event.type == event_id && |
a1d0ce82 | 219 | tp_event->class && tp_event->class->reg && |
1c024eca PZ |
220 | try_module_get(tp_event->mod)) { |
221 | ret = perf_trace_event_init(tp_event, p_event); | |
9cb627d5 LZ |
222 | if (ret) |
223 | module_put(tp_event->mod); | |
20c8928a LZ |
224 | break; |
225 | } | |
ac199db0 | 226 | } |
20c8928a | 227 | mutex_unlock(&event_mutex); |
ac199db0 | 228 | |
20c8928a | 229 | return ret; |
ac199db0 PZ |
230 | } |
231 | ||
ceec0b6f JO |
232 | void perf_trace_destroy(struct perf_event *p_event) |
233 | { | |
234 | mutex_lock(&event_mutex); | |
235 | perf_trace_event_close(p_event); | |
236 | perf_trace_event_unreg(p_event); | |
237 | mutex_unlock(&event_mutex); | |
238 | } | |
239 | ||
a4eaf7f1 | 240 | int perf_trace_add(struct perf_event *p_event, int flags) |
e5e25cf4 | 241 | { |
2425bcb9 | 242 | struct trace_event_call *tp_event = p_event->tp_event; |
20ab4425 | 243 | |
a4eaf7f1 PZ |
244 | if (!(flags & PERF_EF_START)) |
245 | p_event->hw.state = PERF_HES_STOPPED; | |
246 | ||
466c81c4 PZ |
247 | /* |
248 | * If TRACE_REG_PERF_ADD returns false; no custom action was performed | |
249 | * and we need to take the default action of enqueueing our event on | |
250 | * the right per-cpu hlist. | |
251 | */ | |
252 | if (!tp_event->class->reg(tp_event, TRACE_REG_PERF_ADD, p_event)) { | |
253 | struct hlist_head __percpu *pcpu_list; | |
254 | struct hlist_head *list; | |
255 | ||
256 | pcpu_list = tp_event->perf_events; | |
257 | if (WARN_ON_ONCE(!pcpu_list)) | |
258 | return -EINVAL; | |
259 | ||
260 | list = this_cpu_ptr(pcpu_list); | |
261 | hlist_add_head_rcu(&p_event->hlist_entry, list); | |
262 | } | |
20ab4425 | 263 | |
466c81c4 | 264 | return 0; |
1c024eca | 265 | } |
20ab4425 | 266 | |
a4eaf7f1 | 267 | void perf_trace_del(struct perf_event *p_event, int flags) |
1c024eca | 268 | { |
2425bcb9 | 269 | struct trace_event_call *tp_event = p_event->tp_event; |
466c81c4 PZ |
270 | |
271 | /* | |
272 | * If TRACE_REG_PERF_DEL returns false; no custom action was performed | |
273 | * and we need to take the default action of dequeueing our event from | |
274 | * the right per-cpu hlist. | |
275 | */ | |
276 | if (!tp_event->class->reg(tp_event, TRACE_REG_PERF_DEL, p_event)) | |
277 | hlist_del_rcu(&p_event->hlist_entry); | |
e5e25cf4 FW |
278 | } |
279 | ||
1e1dcd93 | 280 | void *perf_trace_buf_alloc(int size, struct pt_regs **regs, int *rctxp) |
430ad5a6 | 281 | { |
1c024eca | 282 | char *raw_data; |
1e1dcd93 | 283 | int rctx; |
430ad5a6 | 284 | |
eb1e7961 FW |
285 | BUILD_BUG_ON(PERF_MAX_TRACE_SIZE % sizeof(unsigned long)); |
286 | ||
cd92bf61 | 287 | if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE, |
1e1dcd93 | 288 | "perf buffer not large enough")) |
cd92bf61 ON |
289 | return NULL; |
290 | ||
1e1dcd93 AS |
291 | *rctxp = rctx = perf_swevent_get_recursion_context(); |
292 | if (rctx < 0) | |
1c024eca | 293 | return NULL; |
430ad5a6 | 294 | |
86038c5e | 295 | if (regs) |
1e1dcd93 AS |
296 | *regs = this_cpu_ptr(&__perf_regs[rctx]); |
297 | raw_data = this_cpu_ptr(perf_trace_buf[rctx]); | |
430ad5a6 XG |
298 | |
299 | /* zero the dead bytes from align to not leak stack to user */ | |
eb1e7961 | 300 | memset(&raw_data[size - sizeof(u64)], 0, sizeof(u64)); |
1e1dcd93 AS |
301 | return raw_data; |
302 | } | |
303 | EXPORT_SYMBOL_GPL(perf_trace_buf_alloc); | |
304 | NOKPROBE_SYMBOL(perf_trace_buf_alloc); | |
305 | ||
306 | void perf_trace_buf_update(void *record, u16 type) | |
307 | { | |
308 | struct trace_entry *entry = record; | |
309 | int pc = preempt_count(); | |
310 | unsigned long flags; | |
430ad5a6 | 311 | |
87f44bbc PZ |
312 | local_save_flags(flags); |
313 | tracing_generic_entry_update(entry, flags, pc); | |
430ad5a6 | 314 | entry->type = type; |
430ad5a6 | 315 | } |
1e1dcd93 | 316 | NOKPROBE_SYMBOL(perf_trace_buf_update); |
ced39002 JO |
317 | |
318 | #ifdef CONFIG_FUNCTION_TRACER | |
319 | static void | |
2f5f6ad9 | 320 | perf_ftrace_function_call(unsigned long ip, unsigned long parent_ip, |
a1e2e31d | 321 | struct ftrace_ops *ops, struct pt_regs *pt_regs) |
ced39002 JO |
322 | { |
323 | struct ftrace_entry *entry; | |
466c81c4 PZ |
324 | struct perf_event *event; |
325 | struct hlist_head head; | |
ced39002 JO |
326 | struct pt_regs regs; |
327 | int rctx; | |
328 | ||
466c81c4 | 329 | if ((unsigned long)ops->private != smp_processor_id()) |
b8ebfd3f ON |
330 | return; |
331 | ||
466c81c4 PZ |
332 | event = container_of(ops, struct perf_event, ftrace_ops); |
333 | ||
334 | /* | |
335 | * @event->hlist entry is NULL (per INIT_HLIST_NODE), and all | |
336 | * the perf code does is hlist_for_each_entry_rcu(), so we can | |
337 | * get away with simply setting the @head.first pointer in order | |
338 | * to create a singular list. | |
339 | */ | |
340 | head.first = &event->hlist_entry; | |
341 | ||
ced39002 JO |
342 | #define ENTRY_SIZE (ALIGN(sizeof(struct ftrace_entry) + sizeof(u32), \ |
343 | sizeof(u64)) - sizeof(u32)) | |
344 | ||
345 | BUILD_BUG_ON(ENTRY_SIZE > PERF_MAX_TRACE_SIZE); | |
346 | ||
ec5e099d | 347 | memset(®s, 0, sizeof(regs)); |
ced39002 JO |
348 | perf_fetch_caller_regs(®s); |
349 | ||
1e1dcd93 | 350 | entry = perf_trace_buf_alloc(ENTRY_SIZE, NULL, &rctx); |
ced39002 JO |
351 | if (!entry) |
352 | return; | |
353 | ||
354 | entry->ip = ip; | |
355 | entry->parent_ip = parent_ip; | |
1e1dcd93 | 356 | perf_trace_buf_submit(entry, ENTRY_SIZE, rctx, TRACE_FN, |
466c81c4 | 357 | 1, ®s, &head, NULL); |
ced39002 JO |
358 | |
359 | #undef ENTRY_SIZE | |
360 | } | |
361 | ||
362 | static int perf_ftrace_function_register(struct perf_event *event) | |
363 | { | |
364 | struct ftrace_ops *ops = &event->ftrace_ops; | |
365 | ||
1dd311e6 | 366 | ops->flags = FTRACE_OPS_FL_RCU; |
466c81c4 PZ |
367 | ops->func = perf_ftrace_function_call; |
368 | ops->private = (void *)(unsigned long)nr_cpu_ids; | |
369 | ||
ced39002 JO |
370 | return register_ftrace_function(ops); |
371 | } | |
372 | ||
373 | static int perf_ftrace_function_unregister(struct perf_event *event) | |
374 | { | |
375 | struct ftrace_ops *ops = &event->ftrace_ops; | |
5500fa51 JO |
376 | int ret = unregister_ftrace_function(ops); |
377 | ftrace_free_filter(ops); | |
378 | return ret; | |
ced39002 JO |
379 | } |
380 | ||
2425bcb9 | 381 | int perf_ftrace_event_register(struct trace_event_call *call, |
ced39002 JO |
382 | enum trace_reg type, void *data) |
383 | { | |
466c81c4 PZ |
384 | struct perf_event *event = data; |
385 | ||
ced39002 JO |
386 | switch (type) { |
387 | case TRACE_REG_REGISTER: | |
388 | case TRACE_REG_UNREGISTER: | |
389 | break; | |
390 | case TRACE_REG_PERF_REGISTER: | |
391 | case TRACE_REG_PERF_UNREGISTER: | |
392 | return 0; | |
393 | case TRACE_REG_PERF_OPEN: | |
394 | return perf_ftrace_function_register(data); | |
395 | case TRACE_REG_PERF_CLOSE: | |
396 | return perf_ftrace_function_unregister(data); | |
397 | case TRACE_REG_PERF_ADD: | |
466c81c4 PZ |
398 | event->ftrace_ops.private = (void *)(unsigned long)smp_processor_id(); |
399 | return 1; | |
ced39002 | 400 | case TRACE_REG_PERF_DEL: |
466c81c4 PZ |
401 | event->ftrace_ops.private = (void *)(unsigned long)nr_cpu_ids; |
402 | return 1; | |
ced39002 JO |
403 | } |
404 | ||
405 | return -EINVAL; | |
406 | } | |
407 | #endif /* CONFIG_FUNCTION_TRACER */ |