1 // SPDX-License-Identifier: GPL-2.0
3 * trace event based perf event profiling/tracing
5 * Copyright (C) 2009 Red Hat Inc, Peter Zijlstra
6 * Copyright (C) 2009-2010 Frederic Weisbecker <fweisbec@gmail.com>
9 #include <linux/module.h>
10 #include <linux/kprobes.h>
11 #include <linux/security.h>
13 #include "trace_probe.h"
15 static char __percpu
*perf_trace_buf
[PERF_NR_CONTEXTS
];
18 * Force it to be aligned to unsigned long to avoid misaligned accesses
21 typedef typeof(unsigned long [PERF_MAX_TRACE_SIZE
/ sizeof(unsigned long)])
24 /* Count the events in use (per event id, not per instance) */
25 static int total_ref_count
;
27 static int perf_trace_event_perm(struct trace_event_call
*tp_event
,
28 struct perf_event
*p_event
)
32 if (tp_event
->perf_perm
) {
33 ret
= tp_event
->perf_perm(tp_event
, p_event
);
39 * We checked and allowed to create parent,
40 * allow children without checking.
46 * It's ok to check current process (owner) permissions in here,
47 * because code below is called only via perf_event_open syscall.
50 /* The ftrace function trace is allowed only for root. */
51 if (ftrace_event_is_function(tp_event
)) {
52 ret
= perf_allow_tracepoint(&p_event
->attr
);
56 if (!is_sampling_event(p_event
))
60 * We don't allow user space callchains for function trace
61 * event, due to issues with page faults while tracing page
62 * fault handler and its overall trickiness nature.
64 if (!p_event
->attr
.exclude_callchain_user
)
68 * Same reason to disable user stack dump as for user space
71 if (p_event
->attr
.sample_type
& PERF_SAMPLE_STACK_USER
)
75 /* No tracing, just counting, so no obvious leak */
76 if (!(p_event
->attr
.sample_type
& PERF_SAMPLE_RAW
))
79 /* Some events are ok to be traced by non-root users... */
80 if (p_event
->attach_state
== PERF_ATTACH_TASK
) {
81 if (tp_event
->flags
& TRACE_EVENT_FL_CAP_ANY
)
86 * ...otherwise raw tracepoint data can be a severe data leak,
87 * only allow root to have these.
89 ret
= perf_allow_tracepoint(&p_event
->attr
);
96 static int perf_trace_event_reg(struct trace_event_call
*tp_event
,
97 struct perf_event
*p_event
)
99 struct hlist_head __percpu
*list
;
103 p_event
->tp_event
= tp_event
;
104 if (tp_event
->perf_refcount
++ > 0)
107 list
= alloc_percpu(struct hlist_head
);
111 for_each_possible_cpu(cpu
)
112 INIT_HLIST_HEAD(per_cpu_ptr(list
, cpu
));
114 tp_event
->perf_events
= list
;
116 if (!total_ref_count
) {
120 for (i
= 0; i
< PERF_NR_CONTEXTS
; i
++) {
121 buf
= (char __percpu
*)alloc_percpu(perf_trace_t
);
125 perf_trace_buf
[i
] = buf
;
129 ret
= tp_event
->class->reg(tp_event
, TRACE_REG_PERF_REGISTER
, NULL
);
137 if (!total_ref_count
) {
140 for (i
= 0; i
< PERF_NR_CONTEXTS
; i
++) {
141 free_percpu(perf_trace_buf
[i
]);
142 perf_trace_buf
[i
] = NULL
;
146 if (!--tp_event
->perf_refcount
) {
147 free_percpu(tp_event
->perf_events
);
148 tp_event
->perf_events
= NULL
;
154 static void perf_trace_event_unreg(struct perf_event
*p_event
)
156 struct trace_event_call
*tp_event
= p_event
->tp_event
;
159 if (--tp_event
->perf_refcount
> 0)
162 tp_event
->class->reg(tp_event
, TRACE_REG_PERF_UNREGISTER
, NULL
);
165 * Ensure our callback won't be called anymore. The buffers
166 * will be freed after that.
168 tracepoint_synchronize_unregister();
170 free_percpu(tp_event
->perf_events
);
171 tp_event
->perf_events
= NULL
;
173 if (!--total_ref_count
) {
174 for (i
= 0; i
< PERF_NR_CONTEXTS
; i
++) {
175 free_percpu(perf_trace_buf
[i
]);
176 perf_trace_buf
[i
] = NULL
;
181 static int perf_trace_event_open(struct perf_event
*p_event
)
183 struct trace_event_call
*tp_event
= p_event
->tp_event
;
184 return tp_event
->class->reg(tp_event
, TRACE_REG_PERF_OPEN
, p_event
);
187 static void perf_trace_event_close(struct perf_event
*p_event
)
189 struct trace_event_call
*tp_event
= p_event
->tp_event
;
190 tp_event
->class->reg(tp_event
, TRACE_REG_PERF_CLOSE
, p_event
);
193 static int perf_trace_event_init(struct trace_event_call
*tp_event
,
194 struct perf_event
*p_event
)
198 ret
= perf_trace_event_perm(tp_event
, p_event
);
202 ret
= perf_trace_event_reg(tp_event
, p_event
);
206 ret
= perf_trace_event_open(p_event
);
208 perf_trace_event_unreg(p_event
);
215 int perf_trace_init(struct perf_event
*p_event
)
217 struct trace_event_call
*tp_event
;
218 u64 event_id
= p_event
->attr
.config
;
221 mutex_lock(&event_mutex
);
222 list_for_each_entry(tp_event
, &ftrace_events
, list
) {
223 if (tp_event
->event
.type
== event_id
&&
224 tp_event
->class && tp_event
->class->reg
&&
225 trace_event_try_get_ref(tp_event
)) {
226 ret
= perf_trace_event_init(tp_event
, p_event
);
228 trace_event_put_ref(tp_event
);
232 mutex_unlock(&event_mutex
);
237 void perf_trace_destroy(struct perf_event
*p_event
)
239 mutex_lock(&event_mutex
);
240 perf_trace_event_close(p_event
);
241 perf_trace_event_unreg(p_event
);
242 trace_event_put_ref(p_event
->tp_event
);
243 mutex_unlock(&event_mutex
);
246 #ifdef CONFIG_KPROBE_EVENTS
247 int perf_kprobe_init(struct perf_event
*p_event
, bool is_retprobe
)
251 struct trace_event_call
*tp_event
;
253 if (p_event
->attr
.kprobe_func
) {
254 func
= strndup_user(u64_to_user_ptr(p_event
->attr
.kprobe_func
),
258 return (ret
== -EINVAL
) ? -E2BIG
: ret
;
261 if (func
[0] == '\0') {
267 tp_event
= create_local_trace_kprobe(
268 func
, (void *)(unsigned long)(p_event
->attr
.kprobe_addr
),
269 p_event
->attr
.probe_offset
, is_retprobe
);
270 if (IS_ERR(tp_event
)) {
271 ret
= PTR_ERR(tp_event
);
275 mutex_lock(&event_mutex
);
276 ret
= perf_trace_event_init(tp_event
, p_event
);
278 destroy_local_trace_kprobe(tp_event
);
279 mutex_unlock(&event_mutex
);
285 void perf_kprobe_destroy(struct perf_event
*p_event
)
287 mutex_lock(&event_mutex
);
288 perf_trace_event_close(p_event
);
289 perf_trace_event_unreg(p_event
);
290 trace_event_put_ref(p_event
->tp_event
);
291 mutex_unlock(&event_mutex
);
293 destroy_local_trace_kprobe(p_event
->tp_event
);
295 #endif /* CONFIG_KPROBE_EVENTS */
297 #ifdef CONFIG_UPROBE_EVENTS
298 int perf_uprobe_init(struct perf_event
*p_event
,
299 unsigned long ref_ctr_offset
, bool is_retprobe
)
303 struct trace_event_call
*tp_event
;
305 if (!p_event
->attr
.uprobe_path
)
308 path
= strndup_user(u64_to_user_ptr(p_event
->attr
.uprobe_path
),
312 return (ret
== -EINVAL
) ? -E2BIG
: ret
;
314 if (path
[0] == '\0') {
319 tp_event
= create_local_trace_uprobe(path
, p_event
->attr
.probe_offset
,
320 ref_ctr_offset
, is_retprobe
);
321 if (IS_ERR(tp_event
)) {
322 ret
= PTR_ERR(tp_event
);
327 * local trace_uprobe need to hold event_mutex to call
328 * uprobe_buffer_enable() and uprobe_buffer_disable().
329 * event_mutex is not required for local trace_kprobes.
331 mutex_lock(&event_mutex
);
332 ret
= perf_trace_event_init(tp_event
, p_event
);
334 destroy_local_trace_uprobe(tp_event
);
335 mutex_unlock(&event_mutex
);
341 void perf_uprobe_destroy(struct perf_event
*p_event
)
343 mutex_lock(&event_mutex
);
344 perf_trace_event_close(p_event
);
345 perf_trace_event_unreg(p_event
);
346 trace_event_put_ref(p_event
->tp_event
);
347 mutex_unlock(&event_mutex
);
348 destroy_local_trace_uprobe(p_event
->tp_event
);
350 #endif /* CONFIG_UPROBE_EVENTS */
352 int perf_trace_add(struct perf_event
*p_event
, int flags
)
354 struct trace_event_call
*tp_event
= p_event
->tp_event
;
355 struct hw_perf_event
*hwc
= &p_event
->hw
;
357 if (!(flags
& PERF_EF_START
))
358 p_event
->hw
.state
= PERF_HES_STOPPED
;
360 if (is_sampling_event(p_event
)) {
361 hwc
->last_period
= hwc
->sample_period
;
362 perf_swevent_set_period(p_event
);
366 * If TRACE_REG_PERF_ADD returns false; no custom action was performed
367 * and we need to take the default action of enqueueing our event on
368 * the right per-cpu hlist.
370 if (!tp_event
->class->reg(tp_event
, TRACE_REG_PERF_ADD
, p_event
)) {
371 struct hlist_head __percpu
*pcpu_list
;
372 struct hlist_head
*list
;
374 pcpu_list
= tp_event
->perf_events
;
375 if (WARN_ON_ONCE(!pcpu_list
))
378 list
= this_cpu_ptr(pcpu_list
);
379 hlist_add_head_rcu(&p_event
->hlist_entry
, list
);
385 void perf_trace_del(struct perf_event
*p_event
, int flags
)
387 struct trace_event_call
*tp_event
= p_event
->tp_event
;
390 * If TRACE_REG_PERF_DEL returns false; no custom action was performed
391 * and we need to take the default action of dequeueing our event from
392 * the right per-cpu hlist.
394 if (!tp_event
->class->reg(tp_event
, TRACE_REG_PERF_DEL
, p_event
))
395 hlist_del_rcu(&p_event
->hlist_entry
);
398 void *perf_trace_buf_alloc(int size
, struct pt_regs
**regs
, int *rctxp
)
403 BUILD_BUG_ON(PERF_MAX_TRACE_SIZE
% sizeof(unsigned long));
405 if (WARN_ONCE(size
> PERF_MAX_TRACE_SIZE
,
406 "perf buffer not large enough, wanted %d, have %d",
407 size
, PERF_MAX_TRACE_SIZE
))
410 *rctxp
= rctx
= perf_swevent_get_recursion_context();
415 *regs
= this_cpu_ptr(&__perf_regs
[rctx
]);
416 raw_data
= this_cpu_ptr(perf_trace_buf
[rctx
]);
418 /* zero the dead bytes from align to not leak stack to user */
419 memset(&raw_data
[size
- sizeof(u64
)], 0, sizeof(u64
));
422 EXPORT_SYMBOL_GPL(perf_trace_buf_alloc
);
423 NOKPROBE_SYMBOL(perf_trace_buf_alloc
);
425 void perf_trace_buf_update(void *record
, u16 type
)
427 struct trace_entry
*entry
= record
;
429 tracing_generic_entry_update(entry
, type
, tracing_gen_ctx());
431 NOKPROBE_SYMBOL(perf_trace_buf_update
);
433 #ifdef CONFIG_FUNCTION_TRACER
435 perf_ftrace_function_call(unsigned long ip
, unsigned long parent_ip
,
436 struct ftrace_ops
*ops
, struct ftrace_regs
*fregs
)
438 struct ftrace_entry
*entry
;
439 struct perf_event
*event
;
440 struct hlist_head head
;
445 if (!rcu_is_watching())
448 bit
= ftrace_test_recursion_trylock(ip
, parent_ip
);
452 if ((unsigned long)ops
->private != smp_processor_id())
455 event
= container_of(ops
, struct perf_event
, ftrace_ops
);
458 * @event->hlist entry is NULL (per INIT_HLIST_NODE), and all
459 * the perf code does is hlist_for_each_entry_rcu(), so we can
460 * get away with simply setting the @head.first pointer in order
461 * to create a singular list.
463 head
.first
= &event
->hlist_entry
;
465 #define ENTRY_SIZE (ALIGN(sizeof(struct ftrace_entry) + sizeof(u32), \
466 sizeof(u64)) - sizeof(u32))
468 BUILD_BUG_ON(ENTRY_SIZE
> PERF_MAX_TRACE_SIZE
);
470 memset(®s
, 0, sizeof(regs
));
471 perf_fetch_caller_regs(®s
);
473 entry
= perf_trace_buf_alloc(ENTRY_SIZE
, NULL
, &rctx
);
478 entry
->parent_ip
= parent_ip
;
479 perf_trace_buf_submit(entry
, ENTRY_SIZE
, rctx
, TRACE_FN
,
480 1, ®s
, &head
, NULL
);
483 ftrace_test_recursion_unlock(bit
);
487 static int perf_ftrace_function_register(struct perf_event
*event
)
489 struct ftrace_ops
*ops
= &event
->ftrace_ops
;
491 ops
->func
= perf_ftrace_function_call
;
492 ops
->private = (void *)(unsigned long)nr_cpu_ids
;
494 return register_ftrace_function(ops
);
497 static int perf_ftrace_function_unregister(struct perf_event
*event
)
499 struct ftrace_ops
*ops
= &event
->ftrace_ops
;
500 int ret
= unregister_ftrace_function(ops
);
501 ftrace_free_filter(ops
);
505 int perf_ftrace_event_register(struct trace_event_call
*call
,
506 enum trace_reg type
, void *data
)
508 struct perf_event
*event
= data
;
511 case TRACE_REG_REGISTER
:
512 case TRACE_REG_UNREGISTER
:
514 case TRACE_REG_PERF_REGISTER
:
515 case TRACE_REG_PERF_UNREGISTER
:
517 case TRACE_REG_PERF_OPEN
:
518 return perf_ftrace_function_register(data
);
519 case TRACE_REG_PERF_CLOSE
:
520 return perf_ftrace_function_unregister(data
);
521 case TRACE_REG_PERF_ADD
:
522 event
->ftrace_ops
.private = (void *)(unsigned long)smp_processor_id();
524 case TRACE_REG_PERF_DEL
:
525 event
->ftrace_ops
.private = (void *)(unsigned long)nr_cpu_ids
;
531 #endif /* CONFIG_FUNCTION_TRACER */