Skip to content

Commit 83b8450

Browse files
author
Ingo Molnar
committed
Merge branch 'tip/perf/core' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt/linux-trace into perf/core
2 parents 4ff16c2 + 5500fa5 commit 83b8450

13 files changed

Lines changed: 667 additions & 117 deletions

‎include/linux/ftrace.h‎

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,33 @@ ftrace_enable_sysctl(struct ctl_table *table, int write,
3131

3232
typedef void (*ftrace_func_t)(unsigned long ip, unsigned long parent_ip);
3333

34+
/*
35+
* FTRACE_OPS_FL_* bits denote the state of ftrace_ops struct and are
36+
* set in the flags member.
37+
*
38+
* ENABLED - set/unset when ftrace_ops is registered/unregistered
39+
* GLOBAL - set manualy by ftrace_ops user to denote the ftrace_ops
40+
* is part of the global tracers sharing the same filter
41+
* via set_ftrace_* debugfs files.
42+
* DYNAMIC - set when ftrace_ops is registered to denote dynamically
43+
* allocated ftrace_ops which need special care
44+
* CONTROL - set manualy by ftrace_ops user to denote the ftrace_ops
45+
* could be controled by following calls:
46+
* ftrace_function_local_enable
47+
* ftrace_function_local_disable
48+
*/
3449
enum {
3550
FTRACE_OPS_FL_ENABLED = 1 << 0,
3651
FTRACE_OPS_FL_GLOBAL = 1 << 1,
3752
FTRACE_OPS_FL_DYNAMIC = 1 << 2,
53+
FTRACE_OPS_FL_CONTROL = 1 << 3,
3854
};
3955

4056
struct ftrace_ops {
4157
ftrace_func_t func;
4258
struct ftrace_ops *next;
4359
unsigned long flags;
60+
int __percpu *disabled;
4461
#ifdef CONFIG_DYNAMIC_FTRACE
4562
struct ftrace_hash *notrace_hash;
4663
struct ftrace_hash *filter_hash;
@@ -97,6 +114,55 @@ int register_ftrace_function(struct ftrace_ops *ops);
97114
int unregister_ftrace_function(struct ftrace_ops *ops);
98115
void clear_ftrace_function(void);
99116

117+
/**
118+
* ftrace_function_local_enable - enable controlled ftrace_ops on current cpu
119+
*
120+
* This function enables tracing on current cpu by decreasing
121+
* the per cpu control variable.
122+
* It must be called with preemption disabled and only on ftrace_ops
123+
* registered with FTRACE_OPS_FL_CONTROL. If called without preemption
124+
* disabled, this_cpu_ptr will complain when CONFIG_DEBUG_PREEMPT is enabled.
125+
*/
126+
static inline void ftrace_function_local_enable(struct ftrace_ops *ops)
127+
{
128+
if (WARN_ON_ONCE(!(ops->flags & FTRACE_OPS_FL_CONTROL)))
129+
return;
130+
131+
(*this_cpu_ptr(ops->disabled))--;
132+
}
133+
134+
/**
135+
* ftrace_function_local_disable - enable controlled ftrace_ops on current cpu
136+
*
137+
* This function enables tracing on current cpu by decreasing
138+
* the per cpu control variable.
139+
* It must be called with preemption disabled and only on ftrace_ops
140+
* registered with FTRACE_OPS_FL_CONTROL. If called without preemption
141+
* disabled, this_cpu_ptr will complain when CONFIG_DEBUG_PREEMPT is enabled.
142+
*/
143+
static inline void ftrace_function_local_disable(struct ftrace_ops *ops)
144+
{
145+
if (WARN_ON_ONCE(!(ops->flags & FTRACE_OPS_FL_CONTROL)))
146+
return;
147+
148+
(*this_cpu_ptr(ops->disabled))++;
149+
}
150+
151+
/**
152+
* ftrace_function_local_disabled - returns ftrace_ops disabled value
153+
* on current cpu
154+
*
155+
* This function returns value of ftrace_ops::disabled on current cpu.
156+
* It must be called with preemption disabled and only on ftrace_ops
157+
* registered with FTRACE_OPS_FL_CONTROL. If called without preemption
158+
* disabled, this_cpu_ptr will complain when CONFIG_DEBUG_PREEMPT is enabled.
159+
*/
160+
static inline int ftrace_function_local_disabled(struct ftrace_ops *ops)
161+
{
162+
WARN_ON_ONCE(!(ops->flags & FTRACE_OPS_FL_CONTROL));
163+
return *this_cpu_ptr(ops->disabled);
164+
}
165+
100166
extern void ftrace_stub(unsigned long a0, unsigned long a1);
101167

102168
#else /* !CONFIG_FUNCTION_TRACER */
@@ -184,6 +250,7 @@ int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
184250
int len, int reset);
185251
void ftrace_set_global_filter(unsigned char *buf, int len, int reset);
186252
void ftrace_set_global_notrace(unsigned char *buf, int len, int reset);
253+
void ftrace_free_filter(struct ftrace_ops *ops);
187254

188255
int register_ftrace_command(struct ftrace_func_command *cmd);
189256
int unregister_ftrace_command(struct ftrace_func_command *cmd);
@@ -314,9 +381,6 @@ extern void ftrace_enable_daemon(void);
314381
#else
315382
static inline int skip_trace(unsigned long ip) { return 0; }
316383
static inline int ftrace_force_update(void) { return 0; }
317-
static inline void ftrace_set_filter(unsigned char *buf, int len, int reset)
318-
{
319-
}
320384
static inline void ftrace_disable_daemon(void) { }
321385
static inline void ftrace_enable_daemon(void) { }
322386
static inline void ftrace_release_mod(struct module *mod) {}
@@ -340,6 +404,9 @@ static inline int ftrace_text_reserved(void *start, void *end)
340404
*/
341405
#define ftrace_regex_open(ops, flag, inod, file) ({ -ENODEV; })
342406
#define ftrace_set_early_filter(ops, buf, enable) do { } while (0)
407+
#define ftrace_set_filter(ops, buf, len, reset) ({ -ENODEV; })
408+
#define ftrace_set_notrace(ops, buf, len, reset) ({ -ENODEV; })
409+
#define ftrace_free_filter(ops) do { } while (0)
343410

344411
static inline ssize_t ftrace_filter_write(struct file *file, const char __user *ubuf,
345412
size_t cnt, loff_t *ppos) { return -ENODEV; }

‎include/linux/ftrace_event.h‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ enum trace_reg {
146146
TRACE_REG_UNREGISTER,
147147
TRACE_REG_PERF_REGISTER,
148148
TRACE_REG_PERF_UNREGISTER,
149+
TRACE_REG_PERF_OPEN,
150+
TRACE_REG_PERF_CLOSE,
151+
TRACE_REG_PERF_ADD,
152+
TRACE_REG_PERF_DEL,
149153
};
150154

151155
struct ftrace_event_call;
@@ -157,15 +161,15 @@ struct ftrace_event_class {
157161
void *perf_probe;
158162
#endif
159163
int (*reg)(struct ftrace_event_call *event,
160-
enum trace_reg type);
164+
enum trace_reg type, void *data);
161165
int (*define_fields)(struct ftrace_event_call *);
162166
struct list_head *(*get_fields)(struct ftrace_event_call *);
163167
struct list_head fields;
164168
int (*raw_init)(struct ftrace_event_call *);
165169
};
166170

167171
extern int ftrace_event_reg(struct ftrace_event_call *event,
168-
enum trace_reg type);
172+
enum trace_reg type, void *data);
169173

170174
enum {
171175
TRACE_EVENT_FL_ENABLED_BIT,
@@ -241,6 +245,7 @@ enum {
241245
FILTER_STATIC_STRING,
242246
FILTER_DYN_STRING,
243247
FILTER_PTR_STRING,
248+
FILTER_TRACE_FN,
244249
};
245250

246251
#define EVENT_STORAGE_SIZE 128

‎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/ftrace.c‎

Lines changed: 110 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262
#define FTRACE_HASH_DEFAULT_BITS 10
6363
#define FTRACE_HASH_MAX_BITS 12
6464

65+
#define FL_GLOBAL_CONTROL_MASK (FTRACE_OPS_FL_GLOBAL | FTRACE_OPS_FL_CONTROL)
66+
6567
/* ftrace_enabled is a method to turn ftrace on or off */
6668
int ftrace_enabled __read_mostly;
6769
static int last_ftrace_enabled;
@@ -89,12 +91,14 @@ static struct ftrace_ops ftrace_list_end __read_mostly = {
8991
};
9092

9193
static struct ftrace_ops *ftrace_global_list __read_mostly = &ftrace_list_end;
94+
static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end;
9295
static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
9396
ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
9497
static ftrace_func_t __ftrace_trace_function_delay __read_mostly = ftrace_stub;
9598
ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
9699
ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
97100
static struct ftrace_ops global_ops;
101+
static struct ftrace_ops control_ops;
98102

99103
static void
100104
ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip);
@@ -168,6 +172,32 @@ static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
168172
}
169173
#endif
170174

175+
static void control_ops_disable_all(struct ftrace_ops *ops)
176+
{
177+
int cpu;
178+
179+
for_each_possible_cpu(cpu)
180+
*per_cpu_ptr(ops->disabled, cpu) = 1;
181+
}
182+
183+
static int control_ops_alloc(struct ftrace_ops *ops)
184+
{
185+
int __percpu *disabled;
186+
187+
disabled = alloc_percpu(int);
188+
if (!disabled)
189+
return -ENOMEM;
190+
191+
ops->disabled = disabled;
192+
control_ops_disable_all(ops);
193+
return 0;
194+
}
195+
196+
static void control_ops_free(struct ftrace_ops *ops)
197+
{
198+
free_percpu(ops->disabled);
199+
}
200+
171201
static void update_global_ops(void)
172202
{
173203
ftrace_func_t func;
@@ -259,6 +289,26 @@ static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
259289
return 0;
260290
}
261291

292+
static void add_ftrace_list_ops(struct ftrace_ops **list,
293+
struct ftrace_ops *main_ops,
294+
struct ftrace_ops *ops)
295+
{
296+
int first = *list == &ftrace_list_end;
297+
add_ftrace_ops(list, ops);
298+
if (first)
299+
add_ftrace_ops(&ftrace_ops_list, main_ops);
300+
}
301+
302+
static int remove_ftrace_list_ops(struct ftrace_ops **list,
303+
struct ftrace_ops *main_ops,
304+
struct ftrace_ops *ops)
305+
{
306+
int ret = remove_ftrace_ops(list, ops);
307+
if (!ret && *list == &ftrace_list_end)
308+
ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
309+
return ret;
310+
}
311+
262312
static int __register_ftrace_function(struct ftrace_ops *ops)
263313
{
264314
if (ftrace_disabled)
@@ -270,15 +320,20 @@ static int __register_ftrace_function(struct ftrace_ops *ops)
270320
if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
271321
return -EBUSY;
272322

323+
/* We don't support both control and global flags set. */
324+
if ((ops->flags & FL_GLOBAL_CONTROL_MASK) == FL_GLOBAL_CONTROL_MASK)
325+
return -EINVAL;
326+
273327
if (!core_kernel_data((unsigned long)ops))
274328
ops->flags |= FTRACE_OPS_FL_DYNAMIC;
275329

276330
if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
277-
int first = ftrace_global_list == &ftrace_list_end;
278-
add_ftrace_ops(&ftrace_global_list, ops);
331+
add_ftrace_list_ops(&ftrace_global_list, &global_ops, ops);
279332
ops->flags |= FTRACE_OPS_FL_ENABLED;
280-
if (first)
281-
add_ftrace_ops(&ftrace_ops_list, &global_ops);
333+
} else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
334+
if (control_ops_alloc(ops))
335+
return -ENOMEM;
336+
add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
282337
} else
283338
add_ftrace_ops(&ftrace_ops_list, ops);
284339

@@ -302,11 +357,23 @@ static int __unregister_ftrace_function(struct ftrace_ops *ops)
302357
return -EINVAL;
303358

304359
if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
305-
ret = remove_ftrace_ops(&ftrace_global_list, ops);
306-
if (!ret && ftrace_global_list == &ftrace_list_end)
307-
ret = remove_ftrace_ops(&ftrace_ops_list, &global_ops);
360+
ret = remove_ftrace_list_ops(&ftrace_global_list,
361+
&global_ops, ops);
308362
if (!ret)
309363
ops->flags &= ~FTRACE_OPS_FL_ENABLED;
364+
} else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
365+
ret = remove_ftrace_list_ops(&ftrace_control_list,
366+
&control_ops, ops);
367+
if (!ret) {
368+
/*
369+
* The ftrace_ops is now removed from the list,
370+
* so there'll be no new users. We must ensure
371+
* all current users are done before we free
372+
* the control data.
373+
*/
374+
synchronize_sched();
375+
control_ops_free(ops);
376+
}
310377
} else
311378
ret = remove_ftrace_ops(&ftrace_ops_list, ops);
312379

@@ -1119,6 +1186,12 @@ static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
11191186
call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
11201187
}
11211188

1189+
void ftrace_free_filter(struct ftrace_ops *ops)
1190+
{
1191+
free_ftrace_hash(ops->filter_hash);
1192+
free_ftrace_hash(ops->notrace_hash);
1193+
}
1194+
11221195
static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
11231196
{
11241197
struct ftrace_hash *hash;
@@ -3873,6 +3946,36 @@ ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
38733946

38743947
#endif /* CONFIG_DYNAMIC_FTRACE */
38753948

3949+
static void
3950+
ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip)
3951+
{
3952+
struct ftrace_ops *op;
3953+
3954+
if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
3955+
return;
3956+
3957+
/*
3958+
* Some of the ops may be dynamically allocated,
3959+
* they must be freed after a synchronize_sched().
3960+
*/
3961+
preempt_disable_notrace();
3962+
trace_recursion_set(TRACE_CONTROL_BIT);
3963+
op = rcu_dereference_raw(ftrace_control_list);
3964+
while (op != &ftrace_list_end) {
3965+
if (!ftrace_function_local_disabled(op) &&
3966+
ftrace_ops_test(op, ip))
3967+
op->func(ip, parent_ip);
3968+
3969+
op = rcu_dereference_raw(op->next);
3970+
};
3971+
trace_recursion_clear(TRACE_CONTROL_BIT);
3972+
preempt_enable_notrace();
3973+
}
3974+
3975+
static struct ftrace_ops control_ops = {
3976+
.func = ftrace_ops_control_func,
3977+
};
3978+
38763979
static void
38773980
ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip)
38783981
{

0 commit comments

Comments
 (0)