Skip to content

Commit ced3900

Browse files
Jiri Olsarostedt
authored andcommitted
ftrace, perf: Add support to use function tracepoint in perf
Adding perf registration support for the ftrace function event, so it is now possible to register it via perf interface. The perf_event struct statically contains ftrace_ops as a handle for function tracer. The function tracer is registered/unregistered in open/close actions. To be efficient, we enable/disable ftrace_ops each time the traced process is scheduled in/out (via TRACE_REG_PERF_(ADD|DELL) handlers). This way tracing is enabled only when the process is running. Intentionally using this way instead of the event's hw state PERF_HES_STOPPED, which would not disable the ftrace_ops. It is now possible to use function trace within perf commands like: perf record -e ftrace:function ls perf stat -e ftrace:function ls Allowed only for root. Link: http://lkml.kernel.org/r/1329317514-8131-6-git-send-email-jolsa@redhat.com Acked-by: Frederic Weisbecker <fweisbec@gmail.com> Signed-off-by: Jiri Olsa <jolsa@redhat.com> Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
1 parent e59a0bf commit ced3900

5 files changed

Lines changed: 109 additions & 2 deletions

File tree

‎include/linux/perf_event.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,9 @@ struct perf_event {
859859
#ifdef CONFIG_EVENT_TRACING
860860
struct ftrace_event_call *tp_event;
861861
struct event_filter *filter;
862+
#ifdef CONFIG_FUNCTION_TRACER
863+
struct ftrace_ops ftrace_ops;
864+
#endif
862865
#endif
863866

864867
#ifdef CONFIG_CGROUP_PERF

‎kernel/trace/trace.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,8 @@ static inline int ftrace_trace_task(struct task_struct *task)
595595
static inline int ftrace_is_dead(void) { return 0; }
596596
#endif
597597

598+
int ftrace_event_is_function(struct ftrace_event_call *call);
599+
598600
/*
599601
* struct trace_parser - servers for reading the user input separated by spaces
600602
* @cont: set if the input is not complete - no final space char was found
@@ -832,4 +834,13 @@ extern const char *__stop___trace_bprintk_fmt[];
832834
FTRACE_ENTRY(call, struct_name, id, PARAMS(tstruct), PARAMS(print))
833835
#include "trace_entries.h"
834836

837+
#ifdef CONFIG_PERF_EVENTS
838+
#ifdef CONFIG_FUNCTION_TRACER
839+
int perf_ftrace_event_register(struct ftrace_event_call *call,
840+
enum trace_reg type, void *data);
841+
#else
842+
#define perf_ftrace_event_register NULL
843+
#endif /* CONFIG_FUNCTION_TRACER */
844+
#endif /* CONFIG_PERF_EVENTS */
845+
835846
#endif /* _LINUX_KERNEL_TRACE_H */

‎kernel/trace/trace_entries.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
/*
5656
* Function trace entry - function address and parent function address:
5757
*/
58-
FTRACE_ENTRY(function, ftrace_entry,
58+
FTRACE_ENTRY_REG(function, ftrace_entry,
5959

6060
TRACE_FN,
6161

@@ -64,7 +64,9 @@ FTRACE_ENTRY(function, ftrace_entry,
6464
__field( unsigned long, parent_ip )
6565
),
6666

67-
F_printk(" %lx <-- %lx", __entry->ip, __entry->parent_ip)
67+
F_printk(" %lx <-- %lx", __entry->ip, __entry->parent_ip),
68+
69+
perf_ftrace_event_register
6870
);
6971

7072
/* Function call entry */

‎kernel/trace/trace_event_perf.c‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ static int total_ref_count;
2424
static int perf_trace_event_perm(struct ftrace_event_call *tp_event,
2525
struct perf_event *p_event)
2626
{
27+
/* The ftrace function trace is allowed only for root. */
28+
if (ftrace_event_is_function(tp_event) &&
29+
perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
30+
return -EPERM;
31+
2732
/* No tracing, just counting, so no obvious leak */
2833
if (!(p_event->attr.sample_type & PERF_SAMPLE_RAW))
2934
return 0;
@@ -250,3 +255,84 @@ __kprobes void *perf_trace_buf_prepare(int size, unsigned short type,
250255
return raw_data;
251256
}
252257
EXPORT_SYMBOL_GPL(perf_trace_buf_prepare);
258+
259+
#ifdef CONFIG_FUNCTION_TRACER
260+
static void
261+
perf_ftrace_function_call(unsigned long ip, unsigned long parent_ip)
262+
{
263+
struct ftrace_entry *entry;
264+
struct hlist_head *head;
265+
struct pt_regs regs;
266+
int rctx;
267+
268+
#define ENTRY_SIZE (ALIGN(sizeof(struct ftrace_entry) + sizeof(u32), \
269+
sizeof(u64)) - sizeof(u32))
270+
271+
BUILD_BUG_ON(ENTRY_SIZE > PERF_MAX_TRACE_SIZE);
272+
273+
perf_fetch_caller_regs(&regs);
274+
275+
entry = perf_trace_buf_prepare(ENTRY_SIZE, TRACE_FN, NULL, &rctx);
276+
if (!entry)
277+
return;
278+
279+
entry->ip = ip;
280+
entry->parent_ip = parent_ip;
281+
282+
head = this_cpu_ptr(event_function.perf_events);
283+
perf_trace_buf_submit(entry, ENTRY_SIZE, rctx, 0,
284+
1, &regs, head);
285+
286+
#undef ENTRY_SIZE
287+
}
288+
289+
static int perf_ftrace_function_register(struct perf_event *event)
290+
{
291+
struct ftrace_ops *ops = &event->ftrace_ops;
292+
293+
ops->flags |= FTRACE_OPS_FL_CONTROL;
294+
ops->func = perf_ftrace_function_call;
295+
return register_ftrace_function(ops);
296+
}
297+
298+
static int perf_ftrace_function_unregister(struct perf_event *event)
299+
{
300+
struct ftrace_ops *ops = &event->ftrace_ops;
301+
return unregister_ftrace_function(ops);
302+
}
303+
304+
static void perf_ftrace_function_enable(struct perf_event *event)
305+
{
306+
ftrace_function_local_enable(&event->ftrace_ops);
307+
}
308+
309+
static void perf_ftrace_function_disable(struct perf_event *event)
310+
{
311+
ftrace_function_local_disable(&event->ftrace_ops);
312+
}
313+
314+
int perf_ftrace_event_register(struct ftrace_event_call *call,
315+
enum trace_reg type, void *data)
316+
{
317+
switch (type) {
318+
case TRACE_REG_REGISTER:
319+
case TRACE_REG_UNREGISTER:
320+
break;
321+
case TRACE_REG_PERF_REGISTER:
322+
case TRACE_REG_PERF_UNREGISTER:
323+
return 0;
324+
case TRACE_REG_PERF_OPEN:
325+
return perf_ftrace_function_register(data);
326+
case TRACE_REG_PERF_CLOSE:
327+
return perf_ftrace_function_unregister(data);
328+
case TRACE_REG_PERF_ADD:
329+
perf_ftrace_function_enable(data);
330+
return 0;
331+
case TRACE_REG_PERF_DEL:
332+
perf_ftrace_function_disable(data);
333+
return 0;
334+
}
335+
336+
return -EINVAL;
337+
}
338+
#endif /* CONFIG_FUNCTION_TRACER */

‎kernel/trace/trace_export.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,4 +184,9 @@ __attribute__((section("_ftrace_events"))) *__event_##call = &event_##call;
184184
FTRACE_ENTRY_REG(call, struct_name, etype, \
185185
PARAMS(tstruct), PARAMS(print), NULL)
186186

187+
int ftrace_event_is_function(struct ftrace_event_call *call)
188+
{
189+
return call == &event_function;
190+
}
191+
187192
#include "trace_entries.h"

0 commit comments

Comments
 (0)