Skip to content

Commit 2f5f6ad

Browse files
Steven Rostedtrostedt
authored andcommitted
ftrace: Pass ftrace_ops as third parameter to function trace callback
Currently the function trace callback receives only the ip and parent_ip of the function that it traced. It would be more powerful to also return the ops that registered the function as well. This allows the same function to act differently depending on what ftrace_ops registered it. Link: http://lkml.kernel.org/r/20120612225424.267254552@goodmis.org Reviewed-by: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com> Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
1 parent 6e0f17b commit 2f5f6ad

11 files changed

Lines changed: 113 additions & 46 deletions

File tree

‎arch/x86/include/asm/ftrace.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
#define MCOUNT_ADDR ((long)(mcount))
3333
#define MCOUNT_INSN_SIZE 5 /* sizeof mcount call */
3434

35+
#if defined(CONFIG_DYNAMIC_FTRACE) && defined(CONFIG_X86_64)
36+
#define ARCH_SUPPORTS_FTRACE_OPS 1
37+
#endif
38+
3539
#ifndef __ASSEMBLY__
3640
extern void mcount(void);
3741
extern atomic_t modifying_ftrace_code;

‎arch/x86/kernel/entry_64.S‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ ENTRY(ftrace_caller)
7979

8080
MCOUNT_SAVE_FRAME
8181

82+
leaq function_trace_op, %rdx
8283
movq 0x38(%rsp), %rdi
8384
movq 8(%rbp), %rsi
8485
subq $MCOUNT_INSN_SIZE, %rdi

‎include/linux/ftrace.h‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@
1818

1919
#include <asm/ftrace.h>
2020

21+
/*
22+
* If the arch supports passing the variable contents of
23+
* function_trace_op as the third parameter back from the
24+
* mcount call, then the arch should define this as 1.
25+
*/
26+
#ifndef ARCH_SUPPORTS_FTRACE_OPS
27+
#define ARCH_SUPPORTS_FTRACE_OPS 0
28+
#endif
29+
2130
struct module;
2231
struct ftrace_hash;
2332

@@ -29,7 +38,10 @@ ftrace_enable_sysctl(struct ctl_table *table, int write,
2938
void __user *buffer, size_t *lenp,
3039
loff_t *ppos);
3140

32-
typedef void (*ftrace_func_t)(unsigned long ip, unsigned long parent_ip);
41+
struct ftrace_ops;
42+
43+
typedef void (*ftrace_func_t)(unsigned long ip, unsigned long parent_ip,
44+
struct ftrace_ops *op);
3345

3446
/*
3547
* FTRACE_OPS_FL_* bits denote the state of ftrace_ops struct and are
@@ -163,7 +175,7 @@ static inline int ftrace_function_local_disabled(struct ftrace_ops *ops)
163175
return *this_cpu_ptr(ops->disabled);
164176
}
165177

166-
extern void ftrace_stub(unsigned long a0, unsigned long a1);
178+
extern void ftrace_stub(unsigned long a0, unsigned long a1, struct ftrace_ops *op);
167179

168180
#else /* !CONFIG_FUNCTION_TRACER */
169181
/*

‎kernel/trace/ftrace.c‎

Lines changed: 70 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,19 @@
6464

6565
#define FL_GLOBAL_CONTROL_MASK (FTRACE_OPS_FL_GLOBAL | FTRACE_OPS_FL_CONTROL)
6666

67+
static struct ftrace_ops ftrace_list_end __read_mostly = {
68+
.func = ftrace_stub,
69+
};
70+
6771
/* ftrace_enabled is a method to turn ftrace on or off */
6872
int ftrace_enabled __read_mostly;
6973
static int last_ftrace_enabled;
7074

7175
/* Quick disabling of function tracer. */
72-
int function_trace_stop;
76+
int function_trace_stop __read_mostly;
77+
78+
/* Current function tracing op */
79+
struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
7380

7481
/* List for set_ftrace_pid's pids. */
7582
LIST_HEAD(ftrace_pids);
@@ -86,10 +93,6 @@ static int ftrace_disabled __read_mostly;
8693

8794
static DEFINE_MUTEX(ftrace_lock);
8895

89-
static struct ftrace_ops ftrace_list_end __read_mostly = {
90-
.func = ftrace_stub,
91-
};
92-
9396
static struct ftrace_ops *ftrace_global_list __read_mostly = &ftrace_list_end;
9497
static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end;
9598
static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
@@ -100,8 +103,14 @@ ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
100103
static struct ftrace_ops global_ops;
101104
static struct ftrace_ops control_ops;
102105

103-
static void
104-
ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip);
106+
#if ARCH_SUPPORTS_FTRACE_OPS
107+
static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
108+
struct ftrace_ops *op);
109+
#else
110+
/* See comment below, where ftrace_ops_list_func is defined */
111+
static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
112+
#define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
113+
#endif
105114

106115
/*
107116
* Traverse the ftrace_global_list, invoking all entries. The reason that we
@@ -112,29 +121,29 @@ ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip);
112121
*
113122
* Silly Alpha and silly pointer-speculation compiler optimizations!
114123
*/
115-
static void ftrace_global_list_func(unsigned long ip,
116-
unsigned long parent_ip)
124+
static void
125+
ftrace_global_list_func(unsigned long ip, unsigned long parent_ip,
126+
struct ftrace_ops *op)
117127
{
118-
struct ftrace_ops *op;
119-
120128
if (unlikely(trace_recursion_test(TRACE_GLOBAL_BIT)))
121129
return;
122130

123131
trace_recursion_set(TRACE_GLOBAL_BIT);
124132
op = rcu_dereference_raw(ftrace_global_list); /*see above*/
125133
while (op != &ftrace_list_end) {
126-
op->func(ip, parent_ip);
134+
op->func(ip, parent_ip, op);
127135
op = rcu_dereference_raw(op->next); /*see above*/
128136
};
129137
trace_recursion_clear(TRACE_GLOBAL_BIT);
130138
}
131139

132-
static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
140+
static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
141+
struct ftrace_ops *op)
133142
{
134143
if (!test_tsk_trace_trace(current))
135144
return;
136145

137-
ftrace_pid_function(ip, parent_ip);
146+
ftrace_pid_function(ip, parent_ip, op);
138147
}
139148

140149
static void set_ftrace_pid_function(ftrace_func_t func)
@@ -163,12 +172,13 @@ void clear_ftrace_function(void)
163172
* For those archs that do not test ftrace_trace_stop in their
164173
* mcount call site, we need to do it from C.
165174
*/
166-
static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
175+
static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip,
176+
struct ftrace_ops *op)
167177
{
168178
if (function_trace_stop)
169179
return;
170180

171-
__ftrace_trace_function(ip, parent_ip);
181+
__ftrace_trace_function(ip, parent_ip, op);
172182
}
173183
#endif
174184

@@ -230,15 +240,24 @@ static void update_ftrace_function(void)
230240

231241
/*
232242
* If we are at the end of the list and this ops is
233-
* not dynamic, then have the mcount trampoline call
234-
* the function directly
243+
* not dynamic and the arch supports passing ops, then have the
244+
* mcount trampoline call the function directly.
235245
*/
236246
if (ftrace_ops_list == &ftrace_list_end ||
237247
(ftrace_ops_list->next == &ftrace_list_end &&
238-
!(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC)))
248+
!(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC) &&
249+
ARCH_SUPPORTS_FTRACE_OPS)) {
250+
/* Set the ftrace_ops that the arch callback uses */
251+
if (ftrace_ops_list == &global_ops)
252+
function_trace_op = ftrace_global_list;
253+
else
254+
function_trace_op = ftrace_ops_list;
239255
func = ftrace_ops_list->func;
240-
else
256+
} else {
257+
/* Just use the default ftrace_ops */
258+
function_trace_op = &ftrace_list_end;
241259
func = ftrace_ops_list_func;
260+
}
242261

243262
#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
244263
ftrace_trace_function = func;
@@ -773,7 +792,8 @@ ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
773792
}
774793

775794
static void
776-
function_profile_call(unsigned long ip, unsigned long parent_ip)
795+
function_profile_call(unsigned long ip, unsigned long parent_ip,
796+
struct ftrace_ops *ops)
777797
{
778798
struct ftrace_profile_stat *stat;
779799
struct ftrace_profile *rec;
@@ -803,7 +823,7 @@ function_profile_call(unsigned long ip, unsigned long parent_ip)
803823
#ifdef CONFIG_FUNCTION_GRAPH_TRACER
804824
static int profile_graph_entry(struct ftrace_graph_ent *trace)
805825
{
806-
function_profile_call(trace->func, 0);
826+
function_profile_call(trace->func, 0, NULL);
807827
return 1;
808828
}
809829

@@ -2790,8 +2810,8 @@ static int __init ftrace_mod_cmd_init(void)
27902810
}
27912811
device_initcall(ftrace_mod_cmd_init);
27922812

2793-
static void
2794-
function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
2813+
static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
2814+
struct ftrace_ops *op)
27952815
{
27962816
struct ftrace_func_probe *entry;
27972817
struct hlist_head *hhd;
@@ -3942,10 +3962,9 @@ ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
39423962
#endif /* CONFIG_DYNAMIC_FTRACE */
39433963

39443964
static void
3945-
ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip)
3965+
ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
3966+
struct ftrace_ops *op)
39463967
{
3947-
struct ftrace_ops *op;
3948-
39493968
if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
39503969
return;
39513970

@@ -3959,7 +3978,7 @@ ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip)
39593978
while (op != &ftrace_list_end) {
39603979
if (!ftrace_function_local_disabled(op) &&
39613980
ftrace_ops_test(op, ip))
3962-
op->func(ip, parent_ip);
3981+
op->func(ip, parent_ip, op);
39633982

39643983
op = rcu_dereference_raw(op->next);
39653984
};
@@ -3971,8 +3990,9 @@ static struct ftrace_ops control_ops = {
39713990
.func = ftrace_ops_control_func,
39723991
};
39733992

3974-
static void
3975-
ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip)
3993+
static inline void
3994+
__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
3995+
struct ftrace_ops *ignored)
39763996
{
39773997
struct ftrace_ops *op;
39783998

@@ -3988,13 +4008,32 @@ ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip)
39884008
op = rcu_dereference_raw(ftrace_ops_list);
39894009
while (op != &ftrace_list_end) {
39904010
if (ftrace_ops_test(op, ip))
3991-
op->func(ip, parent_ip);
4011+
op->func(ip, parent_ip, op);
39924012
op = rcu_dereference_raw(op->next);
39934013
};
39944014
preempt_enable_notrace();
39954015
trace_recursion_clear(TRACE_INTERNAL_BIT);
39964016
}
39974017

4018+
/*
4019+
* Some archs only support passing ip and parent_ip. Even though
4020+
* the list function ignores the op parameter, we do not want any
4021+
* C side effects, where a function is called without the caller
4022+
* sending a third parameter.
4023+
*/
4024+
#if ARCH_SUPPORTS_FTRACE_OPS
4025+
static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
4026+
struct ftrace_ops *op)
4027+
{
4028+
__ftrace_ops_list_func(ip, parent_ip, NULL);
4029+
}
4030+
#else
4031+
static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4032+
{
4033+
__ftrace_ops_list_func(ip, parent_ip, NULL);
4034+
}
4035+
#endif
4036+
39984037
static void clear_ftrace_swapper(void)
39994038
{
40004039
struct task_struct *p;

‎kernel/trace/trace_event_perf.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,8 @@ EXPORT_SYMBOL_GPL(perf_trace_buf_prepare);
258258

259259
#ifdef CONFIG_FUNCTION_TRACER
260260
static void
261-
perf_ftrace_function_call(unsigned long ip, unsigned long parent_ip)
261+
perf_ftrace_function_call(unsigned long ip, unsigned long parent_ip,
262+
struct ftrace_ops *ops)
262263
{
263264
struct ftrace_entry *entry;
264265
struct hlist_head *head;

‎kernel/trace/trace_events.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1681,7 +1681,8 @@ static __init void event_trace_self_tests(void)
16811681
static DEFINE_PER_CPU(atomic_t, ftrace_test_event_disable);
16821682

16831683
static void
1684-
function_test_events_call(unsigned long ip, unsigned long parent_ip)
1684+
function_test_events_call(unsigned long ip, unsigned long parent_ip,
1685+
struct ftrace_ops *op)
16851686
{
16861687
struct ring_buffer_event *event;
16871688
struct ring_buffer *buffer;

‎kernel/trace/trace_functions.c‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ static void function_trace_start(struct trace_array *tr)
4848
}
4949

5050
static void
51-
function_trace_call_preempt_only(unsigned long ip, unsigned long parent_ip)
51+
function_trace_call_preempt_only(unsigned long ip, unsigned long parent_ip,
52+
struct ftrace_ops *op)
5253
{
5354
struct trace_array *tr = func_trace;
5455
struct trace_array_cpu *data;
@@ -75,7 +76,8 @@ function_trace_call_preempt_only(unsigned long ip, unsigned long parent_ip)
7576
}
7677

7778
static void
78-
function_trace_call(unsigned long ip, unsigned long parent_ip)
79+
function_trace_call(unsigned long ip, unsigned long parent_ip,
80+
struct ftrace_ops *op)
7981
{
8082
struct trace_array *tr = func_trace;
8183
struct trace_array_cpu *data;
@@ -106,7 +108,8 @@ function_trace_call(unsigned long ip, unsigned long parent_ip)
106108
}
107109

108110
static void
109-
function_stack_trace_call(unsigned long ip, unsigned long parent_ip)
111+
function_stack_trace_call(unsigned long ip, unsigned long parent_ip,
112+
struct ftrace_ops *op)
110113
{
111114
struct trace_array *tr = func_trace;
112115
struct trace_array_cpu *data;

‎kernel/trace/trace_irqsoff.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ static int func_prolog_dec(struct trace_array *tr,
136136
* irqsoff uses its own tracer function to keep the overhead down:
137137
*/
138138
static void
139-
irqsoff_tracer_call(unsigned long ip, unsigned long parent_ip)
139+
irqsoff_tracer_call(unsigned long ip, unsigned long parent_ip,
140+
struct ftrace_ops *op)
140141
{
141142
struct trace_array *tr = irqsoff_trace;
142143
struct trace_array_cpu *data;

‎kernel/trace/trace_sched_wakeup.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func_prolog_preempt_disable(struct trace_array *tr,
108108
* wakeup uses its own tracer function to keep the overhead down:
109109
*/
110110
static void
111-
wakeup_tracer_call(unsigned long ip, unsigned long parent_ip)
111+
wakeup_tracer_call(unsigned long ip, unsigned long parent_ip, struct ftrace_ops *op)
112112
{
113113
struct trace_array *tr = wakeup_trace;
114114
struct trace_array_cpu *data;

‎kernel/trace/trace_selftest.c‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,35 +103,40 @@ static inline void warn_failed_init_tracer(struct tracer *trace, int init_ret)
103103

104104
static int trace_selftest_test_probe1_cnt;
105105
static void trace_selftest_test_probe1_func(unsigned long ip,
106-
unsigned long pip)
106+
unsigned long pip,
107+
struct ftrace_ops *op)
107108
{
108109
trace_selftest_test_probe1_cnt++;
109110
}
110111

111112
static int trace_selftest_test_probe2_cnt;
112113
static void trace_selftest_test_probe2_func(unsigned long ip,
113-
unsigned long pip)
114+
unsigned long pip,
115+
struct ftrace_ops *op)
114116
{
115117
trace_selftest_test_probe2_cnt++;
116118
}
117119

118120
static int trace_selftest_test_probe3_cnt;
119121
static void trace_selftest_test_probe3_func(unsigned long ip,
120-
unsigned long pip)
122+
unsigned long pip,
123+
struct ftrace_ops *op)
121124
{
122125
trace_selftest_test_probe3_cnt++;
123126
}
124127

125128
static int trace_selftest_test_global_cnt;
126129
static void trace_selftest_test_global_func(unsigned long ip,
127-
unsigned long pip)
130+
unsigned long pip,
131+
struct ftrace_ops *op)
128132
{
129133
trace_selftest_test_global_cnt++;
130134
}
131135

132136
static int trace_selftest_test_dyn_cnt;
133137
static void trace_selftest_test_dyn_func(unsigned long ip,
134-
unsigned long pip)
138+
unsigned long pip,
139+
struct ftrace_ops *op)
135140
{
136141
trace_selftest_test_dyn_cnt++;
137142
}

0 commit comments

Comments
 (0)