Commit | Line | Data |
---|---|---|
16444a8a ACM |
1 | /* |
2 | * Infrastructure for profiling code inserted by 'gcc -pg'. | |
3 | * | |
4 | * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com> | |
5 | * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com> | |
6 | * | |
7 | * Originally ported from the -rt patch by: | |
8 | * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com> | |
9 | * | |
10 | * Based on code in the latency_tracer, that is: | |
11 | * | |
12 | * Copyright (C) 2004-2006 Ingo Molnar | |
6d49e352 | 13 | * Copyright (C) 2004 Nadia Yvette Chambers |
16444a8a ACM |
14 | */ |
15 | ||
3d083395 SR |
16 | #include <linux/stop_machine.h> |
17 | #include <linux/clocksource.h> | |
18 | #include <linux/kallsyms.h> | |
5072c59f | 19 | #include <linux/seq_file.h> |
4a2b8dda | 20 | #include <linux/suspend.h> |
5072c59f | 21 | #include <linux/debugfs.h> |
3d083395 | 22 | #include <linux/hardirq.h> |
2d8b820b | 23 | #include <linux/kthread.h> |
5072c59f | 24 | #include <linux/uaccess.h> |
5855fead | 25 | #include <linux/bsearch.h> |
56d82e00 | 26 | #include <linux/module.h> |
2d8b820b | 27 | #include <linux/ftrace.h> |
b0fc494f | 28 | #include <linux/sysctl.h> |
5a0e3ad6 | 29 | #include <linux/slab.h> |
5072c59f | 30 | #include <linux/ctype.h> |
68950619 | 31 | #include <linux/sort.h> |
3d083395 | 32 | #include <linux/list.h> |
59df055f | 33 | #include <linux/hash.h> |
3f379b03 | 34 | #include <linux/rcupdate.h> |
3d083395 | 35 | |
ad8d75ff | 36 | #include <trace/events/sched.h> |
8aef2d28 | 37 | |
2af15d6a | 38 | #include <asm/setup.h> |
395a59d0 | 39 | |
0706f1c4 | 40 | #include "trace_output.h" |
bac429f0 | 41 | #include "trace_stat.h" |
16444a8a | 42 | |
6912896e | 43 | #define FTRACE_WARN_ON(cond) \ |
0778d9ad SR |
44 | ({ \ |
45 | int ___r = cond; \ | |
46 | if (WARN_ON(___r)) \ | |
6912896e | 47 | ftrace_kill(); \ |
0778d9ad SR |
48 | ___r; \ |
49 | }) | |
6912896e SR |
50 | |
51 | #define FTRACE_WARN_ON_ONCE(cond) \ | |
0778d9ad SR |
52 | ({ \ |
53 | int ___r = cond; \ | |
54 | if (WARN_ON_ONCE(___r)) \ | |
6912896e | 55 | ftrace_kill(); \ |
0778d9ad SR |
56 | ___r; \ |
57 | }) | |
6912896e | 58 | |
8fc0c701 SR |
59 | /* hash bits for specific function selection */ |
60 | #define FTRACE_HASH_BITS 7 | |
61 | #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS) | |
33dc9b12 SR |
62 | #define FTRACE_HASH_DEFAULT_BITS 10 |
63 | #define FTRACE_HASH_MAX_BITS 12 | |
8fc0c701 | 64 | |
e248491a JO |
65 | #define FL_GLOBAL_CONTROL_MASK (FTRACE_OPS_FL_GLOBAL | FTRACE_OPS_FL_CONTROL) |
66 | ||
f04f24fb MH |
67 | #ifdef CONFIG_DYNAMIC_FTRACE |
68 | #define INIT_REGEX_LOCK(opsname) \ | |
69 | .regex_lock = __MUTEX_INITIALIZER(opsname.regex_lock), | |
70 | #else | |
71 | #define INIT_REGEX_LOCK(opsname) | |
72 | #endif | |
73 | ||
2f5f6ad9 SR |
74 | static struct ftrace_ops ftrace_list_end __read_mostly = { |
75 | .func = ftrace_stub, | |
395b97a3 | 76 | .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB, |
2f5f6ad9 SR |
77 | }; |
78 | ||
4eebcc81 SR |
79 | /* ftrace_enabled is a method to turn ftrace on or off */ |
80 | int ftrace_enabled __read_mostly; | |
d61f82d0 | 81 | static int last_ftrace_enabled; |
b0fc494f | 82 | |
60a7ecf4 | 83 | /* Quick disabling of function tracer. */ |
2f5f6ad9 SR |
84 | int function_trace_stop __read_mostly; |
85 | ||
86 | /* Current function tracing op */ | |
87 | struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end; | |
405e1d83 SRRH |
88 | /* What to set function_trace_op to */ |
89 | static struct ftrace_ops *set_function_trace_op; | |
60a7ecf4 | 90 | |
756d17ee | 91 | /* List for set_ftrace_pid's pids. */ |
92 | LIST_HEAD(ftrace_pids); | |
93 | struct ftrace_pid { | |
94 | struct list_head list; | |
95 | struct pid *pid; | |
96 | }; | |
97 | ||
4eebcc81 SR |
98 | /* |
99 | * ftrace_disabled is set when an anomaly is discovered. | |
100 | * ftrace_disabled is much stronger than ftrace_enabled. | |
101 | */ | |
102 | static int ftrace_disabled __read_mostly; | |
103 | ||
52baf119 | 104 | static DEFINE_MUTEX(ftrace_lock); |
b0fc494f | 105 | |
b848914c | 106 | static struct ftrace_ops *ftrace_global_list __read_mostly = &ftrace_list_end; |
e248491a | 107 | static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end; |
b848914c | 108 | static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end; |
16444a8a | 109 | ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub; |
df4fc315 | 110 | ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub; |
2b499381 | 111 | static struct ftrace_ops global_ops; |
e248491a | 112 | static struct ftrace_ops control_ops; |
16444a8a | 113 | |
2f5f6ad9 SR |
114 | #if ARCH_SUPPORTS_FTRACE_OPS |
115 | static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip, | |
a1e2e31d | 116 | struct ftrace_ops *op, struct pt_regs *regs); |
2f5f6ad9 SR |
117 | #else |
118 | /* See comment below, where ftrace_ops_list_func is defined */ | |
119 | static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip); | |
120 | #define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops) | |
121 | #endif | |
b848914c | 122 | |
0a016409 SR |
123 | /* |
124 | * Traverse the ftrace_global_list, invoking all entries. The reason that we | |
1bb539ca | 125 | * can use rcu_dereference_raw_notrace() is that elements removed from this list |
0a016409 | 126 | * are simply leaked, so there is no need to interact with a grace-period |
1bb539ca | 127 | * mechanism. The rcu_dereference_raw_notrace() calls are needed to handle |
0a016409 SR |
128 | * concurrent insertions into the ftrace_global_list. |
129 | * | |
130 | * Silly Alpha and silly pointer-speculation compiler optimizations! | |
131 | */ | |
132 | #define do_for_each_ftrace_op(op, list) \ | |
1bb539ca | 133 | op = rcu_dereference_raw_notrace(list); \ |
0a016409 SR |
134 | do |
135 | ||
136 | /* | |
137 | * Optimized for just a single item in the list (as that is the normal case). | |
138 | */ | |
139 | #define while_for_each_ftrace_op(op) \ | |
1bb539ca | 140 | while (likely(op = rcu_dereference_raw_notrace((op)->next)) && \ |
0a016409 SR |
141 | unlikely((op) != &ftrace_list_end)) |
142 | ||
f04f24fb MH |
143 | static inline void ftrace_ops_init(struct ftrace_ops *ops) |
144 | { | |
145 | #ifdef CONFIG_DYNAMIC_FTRACE | |
146 | if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) { | |
147 | mutex_init(&ops->regex_lock); | |
148 | ops->flags |= FTRACE_OPS_FL_INITIALIZED; | |
149 | } | |
150 | #endif | |
151 | } | |
152 | ||
ea701f11 SR |
153 | /** |
154 | * ftrace_nr_registered_ops - return number of ops registered | |
155 | * | |
156 | * Returns the number of ftrace_ops registered and tracing functions | |
157 | */ | |
158 | int ftrace_nr_registered_ops(void) | |
159 | { | |
160 | struct ftrace_ops *ops; | |
161 | int cnt = 0; | |
162 | ||
163 | mutex_lock(&ftrace_lock); | |
164 | ||
165 | for (ops = ftrace_ops_list; | |
166 | ops != &ftrace_list_end; ops = ops->next) | |
167 | cnt++; | |
168 | ||
169 | mutex_unlock(&ftrace_lock); | |
170 | ||
171 | return cnt; | |
172 | } | |
173 | ||
2f5f6ad9 SR |
174 | static void |
175 | ftrace_global_list_func(unsigned long ip, unsigned long parent_ip, | |
a1e2e31d | 176 | struct ftrace_ops *op, struct pt_regs *regs) |
16444a8a | 177 | { |
c29f122c SR |
178 | int bit; |
179 | ||
edc15caf SR |
180 | bit = trace_test_and_set_recursion(TRACE_GLOBAL_START, TRACE_GLOBAL_MAX); |
181 | if (bit < 0) | |
b1cff0ad | 182 | return; |
16444a8a | 183 | |
0a016409 | 184 | do_for_each_ftrace_op(op, ftrace_global_list) { |
a1e2e31d | 185 | op->func(ip, parent_ip, op, regs); |
0a016409 | 186 | } while_for_each_ftrace_op(op); |
edc15caf SR |
187 | |
188 | trace_clear_recursion(bit); | |
16444a8a ACM |
189 | } |
190 | ||
2f5f6ad9 | 191 | static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip, |
a1e2e31d | 192 | struct ftrace_ops *op, struct pt_regs *regs) |
df4fc315 | 193 | { |
0ef8cde5 | 194 | if (!test_tsk_trace_trace(current)) |
df4fc315 SR |
195 | return; |
196 | ||
a1e2e31d | 197 | ftrace_pid_function(ip, parent_ip, op, regs); |
df4fc315 SR |
198 | } |
199 | ||
200 | static void set_ftrace_pid_function(ftrace_func_t func) | |
201 | { | |
202 | /* do not set ftrace_pid_function to itself! */ | |
203 | if (func != ftrace_pid_func) | |
204 | ftrace_pid_function = func; | |
205 | } | |
206 | ||
16444a8a | 207 | /** |
3d083395 | 208 | * clear_ftrace_function - reset the ftrace function |
16444a8a | 209 | * |
3d083395 SR |
210 | * This NULLs the ftrace function and in essence stops |
211 | * tracing. There may be lag | |
16444a8a | 212 | */ |
3d083395 | 213 | void clear_ftrace_function(void) |
16444a8a | 214 | { |
3d083395 | 215 | ftrace_trace_function = ftrace_stub; |
df4fc315 | 216 | ftrace_pid_function = ftrace_stub; |
3d083395 SR |
217 | } |
218 | ||
e248491a JO |
219 | static void control_ops_disable_all(struct ftrace_ops *ops) |
220 | { | |
221 | int cpu; | |
222 | ||
223 | for_each_possible_cpu(cpu) | |
224 | *per_cpu_ptr(ops->disabled, cpu) = 1; | |
225 | } | |
226 | ||
227 | static int control_ops_alloc(struct ftrace_ops *ops) | |
228 | { | |
229 | int __percpu *disabled; | |
230 | ||
231 | disabled = alloc_percpu(int); | |
232 | if (!disabled) | |
233 | return -ENOMEM; | |
234 | ||
235 | ops->disabled = disabled; | |
236 | control_ops_disable_all(ops); | |
237 | return 0; | |
238 | } | |
239 | ||
240 | static void control_ops_free(struct ftrace_ops *ops) | |
241 | { | |
242 | free_percpu(ops->disabled); | |
243 | } | |
244 | ||
2b499381 | 245 | static void update_global_ops(void) |
491d0dcf | 246 | { |
e6435e96 SRRH |
247 | ftrace_func_t func = ftrace_global_list_func; |
248 | void *private = NULL; | |
249 | ||
250 | /* The list has its own recursion protection. */ | |
251 | global_ops.flags |= FTRACE_OPS_FL_RECURSION_SAFE; | |
491d0dcf SR |
252 | |
253 | /* | |
254 | * If there's only one function registered, then call that | |
255 | * function directly. Otherwise, we need to iterate over the | |
256 | * registered callers. | |
257 | */ | |
b848914c | 258 | if (ftrace_global_list == &ftrace_list_end || |
63503794 | 259 | ftrace_global_list->next == &ftrace_list_end) { |
b848914c | 260 | func = ftrace_global_list->func; |
e6435e96 | 261 | private = ftrace_global_list->private; |
63503794 SR |
262 | /* |
263 | * As we are calling the function directly. | |
264 | * If it does not have recursion protection, | |
265 | * the function_trace_op needs to be updated | |
266 | * accordingly. | |
267 | */ | |
e6435e96 | 268 | if (!(ftrace_global_list->flags & FTRACE_OPS_FL_RECURSION_SAFE)) |
63503794 | 269 | global_ops.flags &= ~FTRACE_OPS_FL_RECURSION_SAFE; |
63503794 SR |
270 | } |
271 | ||
491d0dcf SR |
272 | /* If we filter on pids, update to use the pid function */ |
273 | if (!list_empty(&ftrace_pids)) { | |
274 | set_ftrace_pid_function(func); | |
275 | func = ftrace_pid_func; | |
276 | } | |
2b499381 SR |
277 | |
278 | global_ops.func = func; | |
e6435e96 | 279 | global_ops.private = private; |
2b499381 SR |
280 | } |
281 | ||
405e1d83 SRRH |
282 | static void ftrace_sync(struct work_struct *work) |
283 | { | |
284 | /* | |
285 | * This function is just a stub to implement a hard force | |
286 | * of synchronize_sched(). This requires synchronizing | |
287 | * tasks even in userspace and idle. | |
288 | * | |
289 | * Yes, function tracing is rude. | |
290 | */ | |
291 | } | |
292 | ||
293 | static void ftrace_sync_ipi(void *data) | |
294 | { | |
295 | /* Probably not needed, but do it anyway */ | |
296 | smp_rmb(); | |
297 | } | |
298 | ||
23a8e844 SRRH |
299 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
300 | static void update_function_graph_func(void); | |
301 | #else | |
302 | static inline void update_function_graph_func(void) { } | |
303 | #endif | |
304 | ||
2b499381 SR |
305 | static void update_ftrace_function(void) |
306 | { | |
307 | ftrace_func_t func; | |
308 | ||
309 | update_global_ops(); | |
310 | ||
cdbe61bf SR |
311 | /* |
312 | * If we are at the end of the list and this ops is | |
4740974a SR |
313 | * recursion safe and not dynamic and the arch supports passing ops, |
314 | * then have the mcount trampoline call the function directly. | |
cdbe61bf | 315 | */ |
b848914c | 316 | if (ftrace_ops_list == &ftrace_list_end || |
cdbe61bf | 317 | (ftrace_ops_list->next == &ftrace_list_end && |
2f5f6ad9 | 318 | !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC) && |
4740974a | 319 | (ftrace_ops_list->flags & FTRACE_OPS_FL_RECURSION_SAFE) && |
ccf3672d | 320 | !FTRACE_FORCE_LIST_FUNC)) { |
2f5f6ad9 SR |
321 | /* Set the ftrace_ops that the arch callback uses */ |
322 | if (ftrace_ops_list == &global_ops) | |
405e1d83 | 323 | set_function_trace_op = ftrace_global_list; |
2f5f6ad9 | 324 | else |
405e1d83 | 325 | set_function_trace_op = ftrace_ops_list; |
b848914c | 326 | func = ftrace_ops_list->func; |
2f5f6ad9 SR |
327 | } else { |
328 | /* Just use the default ftrace_ops */ | |
405e1d83 | 329 | set_function_trace_op = &ftrace_list_end; |
b848914c | 330 | func = ftrace_ops_list_func; |
2f5f6ad9 | 331 | } |
2b499381 | 332 | |
405e1d83 SRRH |
333 | /* If there's no change, then do nothing more here */ |
334 | if (ftrace_trace_function == func) | |
335 | return; | |
336 | ||
23a8e844 SRRH |
337 | update_function_graph_func(); |
338 | ||
405e1d83 SRRH |
339 | /* |
340 | * If we are using the list function, it doesn't care | |
341 | * about the function_trace_ops. | |
342 | */ | |
343 | if (func == ftrace_ops_list_func) { | |
344 | ftrace_trace_function = func; | |
345 | /* | |
346 | * Don't even bother setting function_trace_ops, | |
347 | * it would be racy to do so anyway. | |
348 | */ | |
349 | return; | |
350 | } | |
351 | ||
352 | #ifndef CONFIG_DYNAMIC_FTRACE | |
353 | /* | |
354 | * For static tracing, we need to be a bit more careful. | |
355 | * The function change takes affect immediately. Thus, | |
356 | * we need to coorditate the setting of the function_trace_ops | |
357 | * with the setting of the ftrace_trace_function. | |
358 | * | |
359 | * Set the function to the list ops, which will call the | |
360 | * function we want, albeit indirectly, but it handles the | |
361 | * ftrace_ops and doesn't depend on function_trace_op. | |
362 | */ | |
363 | ftrace_trace_function = ftrace_ops_list_func; | |
364 | /* | |
365 | * Make sure all CPUs see this. Yes this is slow, but static | |
366 | * tracing is slow and nasty to have enabled. | |
367 | */ | |
368 | schedule_on_each_cpu(ftrace_sync); | |
369 | /* Now all cpus are using the list ops. */ | |
370 | function_trace_op = set_function_trace_op; | |
371 | /* Make sure the function_trace_op is visible on all CPUs */ | |
372 | smp_wmb(); | |
373 | /* Nasty way to force a rmb on all cpus */ | |
374 | smp_call_function(ftrace_sync_ipi, NULL, 1); | |
375 | /* OK, we are all set to update the ftrace_trace_function now! */ | |
376 | #endif /* !CONFIG_DYNAMIC_FTRACE */ | |
377 | ||
491d0dcf | 378 | ftrace_trace_function = func; |
491d0dcf SR |
379 | } |
380 | ||
2b499381 | 381 | static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops) |
3d083395 | 382 | { |
2b499381 | 383 | ops->next = *list; |
16444a8a | 384 | /* |
b848914c | 385 | * We are entering ops into the list but another |
16444a8a ACM |
386 | * CPU might be walking that list. We need to make sure |
387 | * the ops->next pointer is valid before another CPU sees | |
b848914c | 388 | * the ops pointer included into the list. |
16444a8a | 389 | */ |
2b499381 | 390 | rcu_assign_pointer(*list, ops); |
16444a8a ACM |
391 | } |
392 | ||
2b499381 | 393 | static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops) |
16444a8a | 394 | { |
16444a8a | 395 | struct ftrace_ops **p; |
16444a8a ACM |
396 | |
397 | /* | |
3d083395 SR |
398 | * If we are removing the last function, then simply point |
399 | * to the ftrace_stub. | |
16444a8a | 400 | */ |
2b499381 SR |
401 | if (*list == ops && ops->next == &ftrace_list_end) { |
402 | *list = &ftrace_list_end; | |
e6ea44e9 | 403 | return 0; |
16444a8a ACM |
404 | } |
405 | ||
2b499381 | 406 | for (p = list; *p != &ftrace_list_end; p = &(*p)->next) |
16444a8a ACM |
407 | if (*p == ops) |
408 | break; | |
409 | ||
e6ea44e9 SR |
410 | if (*p != ops) |
411 | return -1; | |
16444a8a ACM |
412 | |
413 | *p = (*p)->next; | |
2b499381 SR |
414 | return 0; |
415 | } | |
16444a8a | 416 | |
e248491a JO |
417 | static void add_ftrace_list_ops(struct ftrace_ops **list, |
418 | struct ftrace_ops *main_ops, | |
419 | struct ftrace_ops *ops) | |
420 | { | |
421 | int first = *list == &ftrace_list_end; | |
422 | add_ftrace_ops(list, ops); | |
423 | if (first) | |
424 | add_ftrace_ops(&ftrace_ops_list, main_ops); | |
425 | } | |
426 | ||
427 | static int remove_ftrace_list_ops(struct ftrace_ops **list, | |
428 | struct ftrace_ops *main_ops, | |
429 | struct ftrace_ops *ops) | |
430 | { | |
431 | int ret = remove_ftrace_ops(list, ops); | |
432 | if (!ret && *list == &ftrace_list_end) | |
433 | ret = remove_ftrace_ops(&ftrace_ops_list, main_ops); | |
434 | return ret; | |
435 | } | |
436 | ||
2b499381 SR |
437 | static int __register_ftrace_function(struct ftrace_ops *ops) |
438 | { | |
2b499381 SR |
439 | if (FTRACE_WARN_ON(ops == &global_ops)) |
440 | return -EINVAL; | |
441 | ||
b848914c SR |
442 | if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED)) |
443 | return -EBUSY; | |
444 | ||
e248491a JO |
445 | /* We don't support both control and global flags set. */ |
446 | if ((ops->flags & FL_GLOBAL_CONTROL_MASK) == FL_GLOBAL_CONTROL_MASK) | |
447 | return -EINVAL; | |
448 | ||
06aeaaea | 449 | #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS |
08f6fba5 SR |
450 | /* |
451 | * If the ftrace_ops specifies SAVE_REGS, then it only can be used | |
452 | * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set. | |
453 | * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant. | |
454 | */ | |
455 | if (ops->flags & FTRACE_OPS_FL_SAVE_REGS && | |
456 | !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)) | |
457 | return -EINVAL; | |
458 | ||
459 | if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED) | |
460 | ops->flags |= FTRACE_OPS_FL_SAVE_REGS; | |
461 | #endif | |
462 | ||
cdbe61bf SR |
463 | if (!core_kernel_data((unsigned long)ops)) |
464 | ops->flags |= FTRACE_OPS_FL_DYNAMIC; | |
465 | ||
b848914c | 466 | if (ops->flags & FTRACE_OPS_FL_GLOBAL) { |
e248491a | 467 | add_ftrace_list_ops(&ftrace_global_list, &global_ops, ops); |
b848914c | 468 | ops->flags |= FTRACE_OPS_FL_ENABLED; |
e248491a JO |
469 | } else if (ops->flags & FTRACE_OPS_FL_CONTROL) { |
470 | if (control_ops_alloc(ops)) | |
471 | return -ENOMEM; | |
472 | add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops); | |
b848914c SR |
473 | } else |
474 | add_ftrace_ops(&ftrace_ops_list, ops); | |
475 | ||
2b499381 SR |
476 | if (ftrace_enabled) |
477 | update_ftrace_function(); | |
478 | ||
479 | return 0; | |
480 | } | |
481 | ||
482 | static int __unregister_ftrace_function(struct ftrace_ops *ops) | |
483 | { | |
484 | int ret; | |
485 | ||
b848914c SR |
486 | if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED))) |
487 | return -EBUSY; | |
488 | ||
2b499381 SR |
489 | if (FTRACE_WARN_ON(ops == &global_ops)) |
490 | return -EINVAL; | |
491 | ||
b848914c | 492 | if (ops->flags & FTRACE_OPS_FL_GLOBAL) { |
e248491a JO |
493 | ret = remove_ftrace_list_ops(&ftrace_global_list, |
494 | &global_ops, ops); | |
b848914c SR |
495 | if (!ret) |
496 | ops->flags &= ~FTRACE_OPS_FL_ENABLED; | |
e248491a JO |
497 | } else if (ops->flags & FTRACE_OPS_FL_CONTROL) { |
498 | ret = remove_ftrace_list_ops(&ftrace_control_list, | |
499 | &control_ops, ops); | |
b848914c SR |
500 | } else |
501 | ret = remove_ftrace_ops(&ftrace_ops_list, ops); | |
502 | ||
2b499381 SR |
503 | if (ret < 0) |
504 | return ret; | |
b848914c | 505 | |
491d0dcf SR |
506 | if (ftrace_enabled) |
507 | update_ftrace_function(); | |
16444a8a | 508 | |
e6ea44e9 | 509 | return 0; |
3d083395 SR |
510 | } |
511 | ||
df4fc315 SR |
512 | static void ftrace_update_pid_func(void) |
513 | { | |
491d0dcf | 514 | /* Only do something if we are tracing something */ |
df4fc315 | 515 | if (ftrace_trace_function == ftrace_stub) |
10dd3ebe | 516 | return; |
df4fc315 | 517 | |
491d0dcf | 518 | update_ftrace_function(); |
df4fc315 SR |
519 | } |
520 | ||
493762fc SR |
521 | #ifdef CONFIG_FUNCTION_PROFILER |
522 | struct ftrace_profile { | |
523 | struct hlist_node node; | |
524 | unsigned long ip; | |
525 | unsigned long counter; | |
0706f1c4 SR |
526 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
527 | unsigned long long time; | |
e330b3bc | 528 | unsigned long long time_squared; |
0706f1c4 | 529 | #endif |
8fc0c701 SR |
530 | }; |
531 | ||
493762fc SR |
532 | struct ftrace_profile_page { |
533 | struct ftrace_profile_page *next; | |
534 | unsigned long index; | |
535 | struct ftrace_profile records[]; | |
d61f82d0 SR |
536 | }; |
537 | ||
cafb168a SR |
538 | struct ftrace_profile_stat { |
539 | atomic_t disabled; | |
540 | struct hlist_head *hash; | |
541 | struct ftrace_profile_page *pages; | |
542 | struct ftrace_profile_page *start; | |
543 | struct tracer_stat stat; | |
544 | }; | |
545 | ||
493762fc SR |
546 | #define PROFILE_RECORDS_SIZE \ |
547 | (PAGE_SIZE - offsetof(struct ftrace_profile_page, records)) | |
5072c59f | 548 | |
493762fc SR |
549 | #define PROFILES_PER_PAGE \ |
550 | (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile)) | |
3d083395 | 551 | |
fb9fb015 SR |
552 | static int ftrace_profile_enabled __read_mostly; |
553 | ||
554 | /* ftrace_profile_lock - synchronize the enable and disable of the profiler */ | |
bac429f0 SR |
555 | static DEFINE_MUTEX(ftrace_profile_lock); |
556 | ||
cafb168a | 557 | static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats); |
493762fc | 558 | |
20079ebe NK |
559 | #define FTRACE_PROFILE_HASH_BITS 10 |
560 | #define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS) | |
493762fc | 561 | |
bac429f0 SR |
562 | static void * |
563 | function_stat_next(void *v, int idx) | |
564 | { | |
493762fc SR |
565 | struct ftrace_profile *rec = v; |
566 | struct ftrace_profile_page *pg; | |
bac429f0 | 567 | |
493762fc | 568 | pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK); |
bac429f0 SR |
569 | |
570 | again: | |
0296e425 LZ |
571 | if (idx != 0) |
572 | rec++; | |
573 | ||
bac429f0 SR |
574 | if ((void *)rec >= (void *)&pg->records[pg->index]) { |
575 | pg = pg->next; | |
576 | if (!pg) | |
577 | return NULL; | |
578 | rec = &pg->records[0]; | |
493762fc SR |
579 | if (!rec->counter) |
580 | goto again; | |
bac429f0 SR |
581 | } |
582 | ||
bac429f0 SR |
583 | return rec; |
584 | } | |
585 | ||
586 | static void *function_stat_start(struct tracer_stat *trace) | |
587 | { | |
cafb168a SR |
588 | struct ftrace_profile_stat *stat = |
589 | container_of(trace, struct ftrace_profile_stat, stat); | |
590 | ||
591 | if (!stat || !stat->start) | |
592 | return NULL; | |
593 | ||
594 | return function_stat_next(&stat->start->records[0], 0); | |
bac429f0 SR |
595 | } |
596 | ||
0706f1c4 SR |
597 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
598 | /* function graph compares on total time */ | |
599 | static int function_stat_cmp(void *p1, void *p2) | |
600 | { | |
601 | struct ftrace_profile *a = p1; | |
602 | struct ftrace_profile *b = p2; | |
603 | ||
604 | if (a->time < b->time) | |
605 | return -1; | |
606 | if (a->time > b->time) | |
607 | return 1; | |
608 | else | |
609 | return 0; | |
610 | } | |
611 | #else | |
612 | /* not function graph compares against hits */ | |
bac429f0 SR |
613 | static int function_stat_cmp(void *p1, void *p2) |
614 | { | |
493762fc SR |
615 | struct ftrace_profile *a = p1; |
616 | struct ftrace_profile *b = p2; | |
bac429f0 SR |
617 | |
618 | if (a->counter < b->counter) | |
619 | return -1; | |
620 | if (a->counter > b->counter) | |
621 | return 1; | |
622 | else | |
623 | return 0; | |
624 | } | |
0706f1c4 | 625 | #endif |
bac429f0 SR |
626 | |
627 | static int function_stat_headers(struct seq_file *m) | |
628 | { | |
0706f1c4 | 629 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
34886c8b | 630 | seq_printf(m, " Function " |
e330b3bc | 631 | "Hit Time Avg s^2\n" |
34886c8b | 632 | " -------- " |
e330b3bc | 633 | "--- ---- --- ---\n"); |
0706f1c4 | 634 | #else |
bac429f0 SR |
635 | seq_printf(m, " Function Hit\n" |
636 | " -------- ---\n"); | |
0706f1c4 | 637 | #endif |
bac429f0 SR |
638 | return 0; |
639 | } | |
640 | ||
641 | static int function_stat_show(struct seq_file *m, void *v) | |
642 | { | |
493762fc | 643 | struct ftrace_profile *rec = v; |
bac429f0 | 644 | char str[KSYM_SYMBOL_LEN]; |
3aaba20f | 645 | int ret = 0; |
0706f1c4 | 646 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
34886c8b SR |
647 | static struct trace_seq s; |
648 | unsigned long long avg; | |
e330b3bc | 649 | unsigned long long stddev; |
0706f1c4 | 650 | #endif |
3aaba20f LZ |
651 | mutex_lock(&ftrace_profile_lock); |
652 | ||
653 | /* we raced with function_profile_reset() */ | |
654 | if (unlikely(rec->counter == 0)) { | |
655 | ret = -EBUSY; | |
656 | goto out; | |
657 | } | |
bac429f0 SR |
658 | |
659 | kallsyms_lookup(rec->ip, NULL, NULL, NULL, str); | |
0706f1c4 SR |
660 | seq_printf(m, " %-30.30s %10lu", str, rec->counter); |
661 | ||
662 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER | |
663 | seq_printf(m, " "); | |
34886c8b SR |
664 | avg = rec->time; |
665 | do_div(avg, rec->counter); | |
666 | ||
e330b3bc CD |
667 | /* Sample standard deviation (s^2) */ |
668 | if (rec->counter <= 1) | |
669 | stddev = 0; | |
670 | else { | |
52d85d76 JL |
671 | /* |
672 | * Apply Welford's method: | |
673 | * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2) | |
674 | */ | |
675 | stddev = rec->counter * rec->time_squared - | |
676 | rec->time * rec->time; | |
677 | ||
e330b3bc CD |
678 | /* |
679 | * Divide only 1000 for ns^2 -> us^2 conversion. | |
680 | * trace_print_graph_duration will divide 1000 again. | |
681 | */ | |
52d85d76 | 682 | do_div(stddev, rec->counter * (rec->counter - 1) * 1000); |
e330b3bc CD |
683 | } |
684 | ||
34886c8b SR |
685 | trace_seq_init(&s); |
686 | trace_print_graph_duration(rec->time, &s); | |
687 | trace_seq_puts(&s, " "); | |
688 | trace_print_graph_duration(avg, &s); | |
e330b3bc CD |
689 | trace_seq_puts(&s, " "); |
690 | trace_print_graph_duration(stddev, &s); | |
0706f1c4 | 691 | trace_print_seq(m, &s); |
0706f1c4 SR |
692 | #endif |
693 | seq_putc(m, '\n'); | |
3aaba20f LZ |
694 | out: |
695 | mutex_unlock(&ftrace_profile_lock); | |
bac429f0 | 696 | |
3aaba20f | 697 | return ret; |
bac429f0 SR |
698 | } |
699 | ||
cafb168a | 700 | static void ftrace_profile_reset(struct ftrace_profile_stat *stat) |
bac429f0 | 701 | { |
493762fc | 702 | struct ftrace_profile_page *pg; |
bac429f0 | 703 | |
cafb168a | 704 | pg = stat->pages = stat->start; |
bac429f0 | 705 | |
493762fc SR |
706 | while (pg) { |
707 | memset(pg->records, 0, PROFILE_RECORDS_SIZE); | |
708 | pg->index = 0; | |
709 | pg = pg->next; | |
bac429f0 SR |
710 | } |
711 | ||
cafb168a | 712 | memset(stat->hash, 0, |
493762fc SR |
713 | FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head)); |
714 | } | |
bac429f0 | 715 | |
cafb168a | 716 | int ftrace_profile_pages_init(struct ftrace_profile_stat *stat) |
493762fc SR |
717 | { |
718 | struct ftrace_profile_page *pg; | |
318e0a73 SR |
719 | int functions; |
720 | int pages; | |
493762fc | 721 | int i; |
bac429f0 | 722 | |
493762fc | 723 | /* If we already allocated, do nothing */ |
cafb168a | 724 | if (stat->pages) |
493762fc | 725 | return 0; |
bac429f0 | 726 | |
cafb168a SR |
727 | stat->pages = (void *)get_zeroed_page(GFP_KERNEL); |
728 | if (!stat->pages) | |
493762fc | 729 | return -ENOMEM; |
bac429f0 | 730 | |
318e0a73 SR |
731 | #ifdef CONFIG_DYNAMIC_FTRACE |
732 | functions = ftrace_update_tot_cnt; | |
733 | #else | |
734 | /* | |
735 | * We do not know the number of functions that exist because | |
736 | * dynamic tracing is what counts them. With past experience | |
737 | * we have around 20K functions. That should be more than enough. | |
738 | * It is highly unlikely we will execute every function in | |
739 | * the kernel. | |
740 | */ | |
741 | functions = 20000; | |
742 | #endif | |
743 | ||
cafb168a | 744 | pg = stat->start = stat->pages; |
bac429f0 | 745 | |
318e0a73 SR |
746 | pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE); |
747 | ||
39e30cd1 | 748 | for (i = 1; i < pages; i++) { |
493762fc | 749 | pg->next = (void *)get_zeroed_page(GFP_KERNEL); |
493762fc | 750 | if (!pg->next) |
318e0a73 | 751 | goto out_free; |
493762fc SR |
752 | pg = pg->next; |
753 | } | |
754 | ||
755 | return 0; | |
318e0a73 SR |
756 | |
757 | out_free: | |
758 | pg = stat->start; | |
759 | while (pg) { | |
760 | unsigned long tmp = (unsigned long)pg; | |
761 | ||
762 | pg = pg->next; | |
763 | free_page(tmp); | |
764 | } | |
765 | ||
318e0a73 SR |
766 | stat->pages = NULL; |
767 | stat->start = NULL; | |
768 | ||
769 | return -ENOMEM; | |
bac429f0 SR |
770 | } |
771 | ||
cafb168a | 772 | static int ftrace_profile_init_cpu(int cpu) |
bac429f0 | 773 | { |
cafb168a | 774 | struct ftrace_profile_stat *stat; |
493762fc | 775 | int size; |
bac429f0 | 776 | |
cafb168a SR |
777 | stat = &per_cpu(ftrace_profile_stats, cpu); |
778 | ||
779 | if (stat->hash) { | |
493762fc | 780 | /* If the profile is already created, simply reset it */ |
cafb168a | 781 | ftrace_profile_reset(stat); |
493762fc SR |
782 | return 0; |
783 | } | |
bac429f0 | 784 | |
493762fc SR |
785 | /* |
786 | * We are profiling all functions, but usually only a few thousand | |
787 | * functions are hit. We'll make a hash of 1024 items. | |
788 | */ | |
789 | size = FTRACE_PROFILE_HASH_SIZE; | |
bac429f0 | 790 | |
cafb168a | 791 | stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL); |
493762fc | 792 | |
cafb168a | 793 | if (!stat->hash) |
493762fc SR |
794 | return -ENOMEM; |
795 | ||
318e0a73 | 796 | /* Preallocate the function profiling pages */ |
cafb168a SR |
797 | if (ftrace_profile_pages_init(stat) < 0) { |
798 | kfree(stat->hash); | |
799 | stat->hash = NULL; | |
493762fc SR |
800 | return -ENOMEM; |
801 | } | |
802 | ||
803 | return 0; | |
bac429f0 SR |
804 | } |
805 | ||
cafb168a SR |
806 | static int ftrace_profile_init(void) |
807 | { | |
808 | int cpu; | |
809 | int ret = 0; | |
810 | ||
c4602c1c | 811 | for_each_possible_cpu(cpu) { |
cafb168a SR |
812 | ret = ftrace_profile_init_cpu(cpu); |
813 | if (ret) | |
814 | break; | |
815 | } | |
816 | ||
817 | return ret; | |
818 | } | |
819 | ||
493762fc | 820 | /* interrupts must be disabled */ |
cafb168a SR |
821 | static struct ftrace_profile * |
822 | ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip) | |
bac429f0 | 823 | { |
493762fc | 824 | struct ftrace_profile *rec; |
bac429f0 | 825 | struct hlist_head *hhd; |
bac429f0 SR |
826 | unsigned long key; |
827 | ||
20079ebe | 828 | key = hash_long(ip, FTRACE_PROFILE_HASH_BITS); |
cafb168a | 829 | hhd = &stat->hash[key]; |
bac429f0 SR |
830 | |
831 | if (hlist_empty(hhd)) | |
832 | return NULL; | |
833 | ||
1bb539ca | 834 | hlist_for_each_entry_rcu_notrace(rec, hhd, node) { |
bac429f0 | 835 | if (rec->ip == ip) |
493762fc SR |
836 | return rec; |
837 | } | |
838 | ||
839 | return NULL; | |
840 | } | |
841 | ||
cafb168a SR |
842 | static void ftrace_add_profile(struct ftrace_profile_stat *stat, |
843 | struct ftrace_profile *rec) | |
493762fc SR |
844 | { |
845 | unsigned long key; | |
846 | ||
20079ebe | 847 | key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS); |
cafb168a | 848 | hlist_add_head_rcu(&rec->node, &stat->hash[key]); |
493762fc SR |
849 | } |
850 | ||
318e0a73 SR |
851 | /* |
852 | * The memory is already allocated, this simply finds a new record to use. | |
853 | */ | |
493762fc | 854 | static struct ftrace_profile * |
318e0a73 | 855 | ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip) |
493762fc SR |
856 | { |
857 | struct ftrace_profile *rec = NULL; | |
858 | ||
318e0a73 | 859 | /* prevent recursion (from NMIs) */ |
cafb168a | 860 | if (atomic_inc_return(&stat->disabled) != 1) |
493762fc SR |
861 | goto out; |
862 | ||
493762fc | 863 | /* |
318e0a73 SR |
864 | * Try to find the function again since an NMI |
865 | * could have added it | |
493762fc | 866 | */ |
cafb168a | 867 | rec = ftrace_find_profiled_func(stat, ip); |
493762fc | 868 | if (rec) |
cafb168a | 869 | goto out; |
493762fc | 870 | |
cafb168a SR |
871 | if (stat->pages->index == PROFILES_PER_PAGE) { |
872 | if (!stat->pages->next) | |
873 | goto out; | |
874 | stat->pages = stat->pages->next; | |
bac429f0 | 875 | } |
493762fc | 876 | |
cafb168a | 877 | rec = &stat->pages->records[stat->pages->index++]; |
493762fc | 878 | rec->ip = ip; |
cafb168a | 879 | ftrace_add_profile(stat, rec); |
493762fc | 880 | |
bac429f0 | 881 | out: |
cafb168a | 882 | atomic_dec(&stat->disabled); |
bac429f0 SR |
883 | |
884 | return rec; | |
885 | } | |
886 | ||
887 | static void | |
2f5f6ad9 | 888 | function_profile_call(unsigned long ip, unsigned long parent_ip, |
a1e2e31d | 889 | struct ftrace_ops *ops, struct pt_regs *regs) |
bac429f0 | 890 | { |
cafb168a | 891 | struct ftrace_profile_stat *stat; |
493762fc | 892 | struct ftrace_profile *rec; |
bac429f0 SR |
893 | unsigned long flags; |
894 | ||
895 | if (!ftrace_profile_enabled) | |
896 | return; | |
897 | ||
898 | local_irq_save(flags); | |
cafb168a SR |
899 | |
900 | stat = &__get_cpu_var(ftrace_profile_stats); | |
0f6ce3de | 901 | if (!stat->hash || !ftrace_profile_enabled) |
cafb168a SR |
902 | goto out; |
903 | ||
904 | rec = ftrace_find_profiled_func(stat, ip); | |
493762fc | 905 | if (!rec) { |
318e0a73 | 906 | rec = ftrace_profile_alloc(stat, ip); |
493762fc SR |
907 | if (!rec) |
908 | goto out; | |
909 | } | |
bac429f0 SR |
910 | |
911 | rec->counter++; | |
912 | out: | |
913 | local_irq_restore(flags); | |
914 | } | |
915 | ||
0706f1c4 SR |
916 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
917 | static int profile_graph_entry(struct ftrace_graph_ent *trace) | |
918 | { | |
a1e2e31d | 919 | function_profile_call(trace->func, 0, NULL, NULL); |
0706f1c4 SR |
920 | return 1; |
921 | } | |
922 | ||
923 | static void profile_graph_return(struct ftrace_graph_ret *trace) | |
924 | { | |
cafb168a | 925 | struct ftrace_profile_stat *stat; |
a2a16d6a | 926 | unsigned long long calltime; |
0706f1c4 | 927 | struct ftrace_profile *rec; |
cafb168a | 928 | unsigned long flags; |
0706f1c4 SR |
929 | |
930 | local_irq_save(flags); | |
cafb168a | 931 | stat = &__get_cpu_var(ftrace_profile_stats); |
0f6ce3de | 932 | if (!stat->hash || !ftrace_profile_enabled) |
cafb168a SR |
933 | goto out; |
934 | ||
37e44bc5 SR |
935 | /* If the calltime was zero'd ignore it */ |
936 | if (!trace->calltime) | |
937 | goto out; | |
938 | ||
a2a16d6a SR |
939 | calltime = trace->rettime - trace->calltime; |
940 | ||
941 | if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) { | |
942 | int index; | |
943 | ||
944 | index = trace->depth; | |
945 | ||
946 | /* Append this call time to the parent time to subtract */ | |
947 | if (index) | |
948 | current->ret_stack[index - 1].subtime += calltime; | |
949 | ||
950 | if (current->ret_stack[index].subtime < calltime) | |
951 | calltime -= current->ret_stack[index].subtime; | |
952 | else | |
953 | calltime = 0; | |
954 | } | |
955 | ||
cafb168a | 956 | rec = ftrace_find_profiled_func(stat, trace->func); |
e330b3bc | 957 | if (rec) { |
a2a16d6a | 958 | rec->time += calltime; |
e330b3bc CD |
959 | rec->time_squared += calltime * calltime; |
960 | } | |
a2a16d6a | 961 | |
cafb168a | 962 | out: |
0706f1c4 SR |
963 | local_irq_restore(flags); |
964 | } | |
965 | ||
966 | static int register_ftrace_profiler(void) | |
967 | { | |
968 | return register_ftrace_graph(&profile_graph_return, | |
969 | &profile_graph_entry); | |
970 | } | |
971 | ||
972 | static void unregister_ftrace_profiler(void) | |
973 | { | |
974 | unregister_ftrace_graph(); | |
975 | } | |
976 | #else | |
bd38c0e6 | 977 | static struct ftrace_ops ftrace_profile_ops __read_mostly = { |
fb9fb015 | 978 | .func = function_profile_call, |
f04f24fb MH |
979 | .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED, |
980 | INIT_REGEX_LOCK(ftrace_profile_ops) | |
bac429f0 SR |
981 | }; |
982 | ||
0706f1c4 SR |
983 | static int register_ftrace_profiler(void) |
984 | { | |
985 | return register_ftrace_function(&ftrace_profile_ops); | |
986 | } | |
987 | ||
988 | static void unregister_ftrace_profiler(void) | |
989 | { | |
990 | unregister_ftrace_function(&ftrace_profile_ops); | |
991 | } | |
992 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ | |
993 | ||
bac429f0 SR |
994 | static ssize_t |
995 | ftrace_profile_write(struct file *filp, const char __user *ubuf, | |
996 | size_t cnt, loff_t *ppos) | |
997 | { | |
998 | unsigned long val; | |
bac429f0 SR |
999 | int ret; |
1000 | ||
22fe9b54 PH |
1001 | ret = kstrtoul_from_user(ubuf, cnt, 10, &val); |
1002 | if (ret) | |
bac429f0 SR |
1003 | return ret; |
1004 | ||
1005 | val = !!val; | |
1006 | ||
1007 | mutex_lock(&ftrace_profile_lock); | |
1008 | if (ftrace_profile_enabled ^ val) { | |
1009 | if (val) { | |
493762fc SR |
1010 | ret = ftrace_profile_init(); |
1011 | if (ret < 0) { | |
1012 | cnt = ret; | |
1013 | goto out; | |
1014 | } | |
1015 | ||
0706f1c4 SR |
1016 | ret = register_ftrace_profiler(); |
1017 | if (ret < 0) { | |
1018 | cnt = ret; | |
1019 | goto out; | |
1020 | } | |
bac429f0 SR |
1021 | ftrace_profile_enabled = 1; |
1022 | } else { | |
1023 | ftrace_profile_enabled = 0; | |
0f6ce3de SR |
1024 | /* |
1025 | * unregister_ftrace_profiler calls stop_machine | |
1026 | * so this acts like an synchronize_sched. | |
1027 | */ | |
0706f1c4 | 1028 | unregister_ftrace_profiler(); |
bac429f0 SR |
1029 | } |
1030 | } | |
493762fc | 1031 | out: |
bac429f0 SR |
1032 | mutex_unlock(&ftrace_profile_lock); |
1033 | ||
cf8517cf | 1034 | *ppos += cnt; |
bac429f0 SR |
1035 | |
1036 | return cnt; | |
1037 | } | |
1038 | ||
493762fc SR |
1039 | static ssize_t |
1040 | ftrace_profile_read(struct file *filp, char __user *ubuf, | |
1041 | size_t cnt, loff_t *ppos) | |
1042 | { | |
fb9fb015 | 1043 | char buf[64]; /* big enough to hold a number */ |
493762fc SR |
1044 | int r; |
1045 | ||
1046 | r = sprintf(buf, "%u\n", ftrace_profile_enabled); | |
1047 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); | |
1048 | } | |
1049 | ||
bac429f0 SR |
1050 | static const struct file_operations ftrace_profile_fops = { |
1051 | .open = tracing_open_generic, | |
1052 | .read = ftrace_profile_read, | |
1053 | .write = ftrace_profile_write, | |
6038f373 | 1054 | .llseek = default_llseek, |
bac429f0 SR |
1055 | }; |
1056 | ||
cafb168a SR |
1057 | /* used to initialize the real stat files */ |
1058 | static struct tracer_stat function_stats __initdata = { | |
fb9fb015 SR |
1059 | .name = "functions", |
1060 | .stat_start = function_stat_start, | |
1061 | .stat_next = function_stat_next, | |
1062 | .stat_cmp = function_stat_cmp, | |
1063 | .stat_headers = function_stat_headers, | |
1064 | .stat_show = function_stat_show | |
cafb168a SR |
1065 | }; |
1066 | ||
6ab5d668 | 1067 | static __init void ftrace_profile_debugfs(struct dentry *d_tracer) |
bac429f0 | 1068 | { |
cafb168a | 1069 | struct ftrace_profile_stat *stat; |
bac429f0 | 1070 | struct dentry *entry; |
cafb168a | 1071 | char *name; |
bac429f0 | 1072 | int ret; |
cafb168a SR |
1073 | int cpu; |
1074 | ||
1075 | for_each_possible_cpu(cpu) { | |
1076 | stat = &per_cpu(ftrace_profile_stats, cpu); | |
1077 | ||
1078 | /* allocate enough for function name + cpu number */ | |
1079 | name = kmalloc(32, GFP_KERNEL); | |
1080 | if (!name) { | |
1081 | /* | |
1082 | * The files created are permanent, if something happens | |
1083 | * we still do not free memory. | |
1084 | */ | |
cafb168a SR |
1085 | WARN(1, |
1086 | "Could not allocate stat file for cpu %d\n", | |
1087 | cpu); | |
1088 | return; | |
1089 | } | |
1090 | stat->stat = function_stats; | |
1091 | snprintf(name, 32, "function%d", cpu); | |
1092 | stat->stat.name = name; | |
1093 | ret = register_stat_tracer(&stat->stat); | |
1094 | if (ret) { | |
1095 | WARN(1, | |
1096 | "Could not register function stat for cpu %d\n", | |
1097 | cpu); | |
1098 | kfree(name); | |
1099 | return; | |
1100 | } | |
bac429f0 SR |
1101 | } |
1102 | ||
1103 | entry = debugfs_create_file("function_profile_enabled", 0644, | |
1104 | d_tracer, NULL, &ftrace_profile_fops); | |
1105 | if (!entry) | |
1106 | pr_warning("Could not create debugfs " | |
1107 | "'function_profile_enabled' entry\n"); | |
1108 | } | |
1109 | ||
bac429f0 | 1110 | #else /* CONFIG_FUNCTION_PROFILER */ |
6ab5d668 | 1111 | static __init void ftrace_profile_debugfs(struct dentry *d_tracer) |
bac429f0 SR |
1112 | { |
1113 | } | |
bac429f0 SR |
1114 | #endif /* CONFIG_FUNCTION_PROFILER */ |
1115 | ||
493762fc SR |
1116 | static struct pid * const ftrace_swapper_pid = &init_struct_pid; |
1117 | ||
1118 | #ifdef CONFIG_DYNAMIC_FTRACE | |
1119 | ||
1120 | #ifndef CONFIG_FTRACE_MCOUNT_RECORD | |
1121 | # error Dynamic ftrace depends on MCOUNT_RECORD | |
1122 | #endif | |
1123 | ||
1124 | static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly; | |
1125 | ||
1126 | struct ftrace_func_probe { | |
1127 | struct hlist_node node; | |
1128 | struct ftrace_probe_ops *ops; | |
1129 | unsigned long flags; | |
1130 | unsigned long ip; | |
1131 | void *data; | |
7818b388 | 1132 | struct list_head free_list; |
493762fc SR |
1133 | }; |
1134 | ||
b448c4e3 SR |
1135 | struct ftrace_func_entry { |
1136 | struct hlist_node hlist; | |
1137 | unsigned long ip; | |
1138 | }; | |
1139 | ||
1140 | struct ftrace_hash { | |
1141 | unsigned long size_bits; | |
1142 | struct hlist_head *buckets; | |
1143 | unsigned long count; | |
07fd5515 | 1144 | struct rcu_head rcu; |
b448c4e3 SR |
1145 | }; |
1146 | ||
33dc9b12 SR |
1147 | /* |
1148 | * We make these constant because no one should touch them, | |
1149 | * but they are used as the default "empty hash", to avoid allocating | |
1150 | * it all the time. These are in a read only section such that if | |
1151 | * anyone does try to modify it, it will cause an exception. | |
1152 | */ | |
1153 | static const struct hlist_head empty_buckets[1]; | |
1154 | static const struct ftrace_hash empty_hash = { | |
1155 | .buckets = (struct hlist_head *)empty_buckets, | |
1cf41dd7 | 1156 | }; |
33dc9b12 | 1157 | #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash) |
493762fc | 1158 | |
2b499381 | 1159 | static struct ftrace_ops global_ops = { |
f45948e8 | 1160 | .func = ftrace_stub, |
33dc9b12 SR |
1161 | .notrace_hash = EMPTY_HASH, |
1162 | .filter_hash = EMPTY_HASH, | |
f04f24fb MH |
1163 | .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED, |
1164 | INIT_REGEX_LOCK(global_ops) | |
f45948e8 SR |
1165 | }; |
1166 | ||
493762fc SR |
1167 | struct ftrace_page { |
1168 | struct ftrace_page *next; | |
a7900875 | 1169 | struct dyn_ftrace *records; |
493762fc | 1170 | int index; |
a7900875 | 1171 | int size; |
493762fc SR |
1172 | }; |
1173 | ||
85ae32ae SR |
1174 | static struct ftrace_page *ftrace_new_pgs; |
1175 | ||
a7900875 SR |
1176 | #define ENTRY_SIZE sizeof(struct dyn_ftrace) |
1177 | #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE) | |
493762fc SR |
1178 | |
1179 | /* estimate from running different kernels */ | |
1180 | #define NR_TO_INIT 10000 | |
1181 | ||
1182 | static struct ftrace_page *ftrace_pages_start; | |
1183 | static struct ftrace_page *ftrace_pages; | |
1184 | ||
06a51d93 SR |
1185 | static bool ftrace_hash_empty(struct ftrace_hash *hash) |
1186 | { | |
1187 | return !hash || !hash->count; | |
1188 | } | |
1189 | ||
b448c4e3 SR |
1190 | static struct ftrace_func_entry * |
1191 | ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip) | |
1192 | { | |
1193 | unsigned long key; | |
1194 | struct ftrace_func_entry *entry; | |
1195 | struct hlist_head *hhd; | |
b448c4e3 | 1196 | |
06a51d93 | 1197 | if (ftrace_hash_empty(hash)) |
b448c4e3 SR |
1198 | return NULL; |
1199 | ||
1200 | if (hash->size_bits > 0) | |
1201 | key = hash_long(ip, hash->size_bits); | |
1202 | else | |
1203 | key = 0; | |
1204 | ||
1205 | hhd = &hash->buckets[key]; | |
1206 | ||
1bb539ca | 1207 | hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) { |
b448c4e3 SR |
1208 | if (entry->ip == ip) |
1209 | return entry; | |
1210 | } | |
1211 | return NULL; | |
1212 | } | |
1213 | ||
33dc9b12 SR |
1214 | static void __add_hash_entry(struct ftrace_hash *hash, |
1215 | struct ftrace_func_entry *entry) | |
b448c4e3 | 1216 | { |
b448c4e3 SR |
1217 | struct hlist_head *hhd; |
1218 | unsigned long key; | |
1219 | ||
b448c4e3 | 1220 | if (hash->size_bits) |
33dc9b12 | 1221 | key = hash_long(entry->ip, hash->size_bits); |
b448c4e3 SR |
1222 | else |
1223 | key = 0; | |
1224 | ||
b448c4e3 SR |
1225 | hhd = &hash->buckets[key]; |
1226 | hlist_add_head(&entry->hlist, hhd); | |
1227 | hash->count++; | |
33dc9b12 SR |
1228 | } |
1229 | ||
1230 | static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip) | |
1231 | { | |
1232 | struct ftrace_func_entry *entry; | |
1233 | ||
1234 | entry = kmalloc(sizeof(*entry), GFP_KERNEL); | |
1235 | if (!entry) | |
1236 | return -ENOMEM; | |
1237 | ||
1238 | entry->ip = ip; | |
1239 | __add_hash_entry(hash, entry); | |
b448c4e3 SR |
1240 | |
1241 | return 0; | |
1242 | } | |
1243 | ||
1244 | static void | |
33dc9b12 | 1245 | free_hash_entry(struct ftrace_hash *hash, |
b448c4e3 SR |
1246 | struct ftrace_func_entry *entry) |
1247 | { | |
1248 | hlist_del(&entry->hlist); | |
1249 | kfree(entry); | |
1250 | hash->count--; | |
1251 | } | |
1252 | ||
33dc9b12 SR |
1253 | static void |
1254 | remove_hash_entry(struct ftrace_hash *hash, | |
1255 | struct ftrace_func_entry *entry) | |
1256 | { | |
1257 | hlist_del(&entry->hlist); | |
1258 | hash->count--; | |
1259 | } | |
1260 | ||
b448c4e3 SR |
1261 | static void ftrace_hash_clear(struct ftrace_hash *hash) |
1262 | { | |
1263 | struct hlist_head *hhd; | |
b67bfe0d | 1264 | struct hlist_node *tn; |
b448c4e3 SR |
1265 | struct ftrace_func_entry *entry; |
1266 | int size = 1 << hash->size_bits; | |
1267 | int i; | |
1268 | ||
33dc9b12 SR |
1269 | if (!hash->count) |
1270 | return; | |
1271 | ||
b448c4e3 SR |
1272 | for (i = 0; i < size; i++) { |
1273 | hhd = &hash->buckets[i]; | |
b67bfe0d | 1274 | hlist_for_each_entry_safe(entry, tn, hhd, hlist) |
33dc9b12 | 1275 | free_hash_entry(hash, entry); |
b448c4e3 SR |
1276 | } |
1277 | FTRACE_WARN_ON(hash->count); | |
1278 | } | |
1279 | ||
33dc9b12 SR |
1280 | static void free_ftrace_hash(struct ftrace_hash *hash) |
1281 | { | |
1282 | if (!hash || hash == EMPTY_HASH) | |
1283 | return; | |
1284 | ftrace_hash_clear(hash); | |
1285 | kfree(hash->buckets); | |
1286 | kfree(hash); | |
1287 | } | |
1288 | ||
07fd5515 SR |
1289 | static void __free_ftrace_hash_rcu(struct rcu_head *rcu) |
1290 | { | |
1291 | struct ftrace_hash *hash; | |
1292 | ||
1293 | hash = container_of(rcu, struct ftrace_hash, rcu); | |
1294 | free_ftrace_hash(hash); | |
1295 | } | |
1296 | ||
1297 | static void free_ftrace_hash_rcu(struct ftrace_hash *hash) | |
1298 | { | |
1299 | if (!hash || hash == EMPTY_HASH) | |
1300 | return; | |
1301 | call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu); | |
1302 | } | |
1303 | ||
5500fa51 JO |
1304 | void ftrace_free_filter(struct ftrace_ops *ops) |
1305 | { | |
f04f24fb | 1306 | ftrace_ops_init(ops); |
5500fa51 JO |
1307 | free_ftrace_hash(ops->filter_hash); |
1308 | free_ftrace_hash(ops->notrace_hash); | |
1309 | } | |
1310 | ||
33dc9b12 SR |
1311 | static struct ftrace_hash *alloc_ftrace_hash(int size_bits) |
1312 | { | |
1313 | struct ftrace_hash *hash; | |
1314 | int size; | |
1315 | ||
1316 | hash = kzalloc(sizeof(*hash), GFP_KERNEL); | |
1317 | if (!hash) | |
1318 | return NULL; | |
1319 | ||
1320 | size = 1 << size_bits; | |
47b0edcb | 1321 | hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL); |
33dc9b12 SR |
1322 | |
1323 | if (!hash->buckets) { | |
1324 | kfree(hash); | |
1325 | return NULL; | |
1326 | } | |
1327 | ||
1328 | hash->size_bits = size_bits; | |
1329 | ||
1330 | return hash; | |
1331 | } | |
1332 | ||
1333 | static struct ftrace_hash * | |
1334 | alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash) | |
1335 | { | |
1336 | struct ftrace_func_entry *entry; | |
1337 | struct ftrace_hash *new_hash; | |
33dc9b12 SR |
1338 | int size; |
1339 | int ret; | |
1340 | int i; | |
1341 | ||
1342 | new_hash = alloc_ftrace_hash(size_bits); | |
1343 | if (!new_hash) | |
1344 | return NULL; | |
1345 | ||
1346 | /* Empty hash? */ | |
06a51d93 | 1347 | if (ftrace_hash_empty(hash)) |
33dc9b12 SR |
1348 | return new_hash; |
1349 | ||
1350 | size = 1 << hash->size_bits; | |
1351 | for (i = 0; i < size; i++) { | |
b67bfe0d | 1352 | hlist_for_each_entry(entry, &hash->buckets[i], hlist) { |
33dc9b12 SR |
1353 | ret = add_hash_entry(new_hash, entry->ip); |
1354 | if (ret < 0) | |
1355 | goto free_hash; | |
1356 | } | |
1357 | } | |
1358 | ||
1359 | FTRACE_WARN_ON(new_hash->count != hash->count); | |
1360 | ||
1361 | return new_hash; | |
1362 | ||
1363 | free_hash: | |
1364 | free_ftrace_hash(new_hash); | |
1365 | return NULL; | |
1366 | } | |
1367 | ||
41fb61c2 SR |
1368 | static void |
1369 | ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash); | |
1370 | static void | |
1371 | ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash); | |
1372 | ||
33dc9b12 | 1373 | static int |
41fb61c2 SR |
1374 | ftrace_hash_move(struct ftrace_ops *ops, int enable, |
1375 | struct ftrace_hash **dst, struct ftrace_hash *src) | |
33dc9b12 SR |
1376 | { |
1377 | struct ftrace_func_entry *entry; | |
b67bfe0d | 1378 | struct hlist_node *tn; |
33dc9b12 | 1379 | struct hlist_head *hhd; |
07fd5515 SR |
1380 | struct ftrace_hash *old_hash; |
1381 | struct ftrace_hash *new_hash; | |
33dc9b12 SR |
1382 | int size = src->count; |
1383 | int bits = 0; | |
41fb61c2 | 1384 | int ret; |
33dc9b12 SR |
1385 | int i; |
1386 | ||
41fb61c2 SR |
1387 | /* |
1388 | * Remove the current set, update the hash and add | |
1389 | * them back. | |
1390 | */ | |
1391 | ftrace_hash_rec_disable(ops, enable); | |
1392 | ||
33dc9b12 SR |
1393 | /* |
1394 | * If the new source is empty, just free dst and assign it | |
1395 | * the empty_hash. | |
1396 | */ | |
1397 | if (!src->count) { | |
07fd5515 SR |
1398 | free_ftrace_hash_rcu(*dst); |
1399 | rcu_assign_pointer(*dst, EMPTY_HASH); | |
d4d34b98 SR |
1400 | /* still need to update the function records */ |
1401 | ret = 0; | |
1402 | goto out; | |
33dc9b12 SR |
1403 | } |
1404 | ||
33dc9b12 SR |
1405 | /* |
1406 | * Make the hash size about 1/2 the # found | |
1407 | */ | |
1408 | for (size /= 2; size; size >>= 1) | |
1409 | bits++; | |
1410 | ||
1411 | /* Don't allocate too much */ | |
1412 | if (bits > FTRACE_HASH_MAX_BITS) | |
1413 | bits = FTRACE_HASH_MAX_BITS; | |
1414 | ||
41fb61c2 | 1415 | ret = -ENOMEM; |
07fd5515 SR |
1416 | new_hash = alloc_ftrace_hash(bits); |
1417 | if (!new_hash) | |
41fb61c2 | 1418 | goto out; |
33dc9b12 SR |
1419 | |
1420 | size = 1 << src->size_bits; | |
1421 | for (i = 0; i < size; i++) { | |
1422 | hhd = &src->buckets[i]; | |
b67bfe0d | 1423 | hlist_for_each_entry_safe(entry, tn, hhd, hlist) { |
33dc9b12 | 1424 | remove_hash_entry(src, entry); |
07fd5515 | 1425 | __add_hash_entry(new_hash, entry); |
33dc9b12 SR |
1426 | } |
1427 | } | |
1428 | ||
07fd5515 SR |
1429 | old_hash = *dst; |
1430 | rcu_assign_pointer(*dst, new_hash); | |
1431 | free_ftrace_hash_rcu(old_hash); | |
1432 | ||
41fb61c2 SR |
1433 | ret = 0; |
1434 | out: | |
1435 | /* | |
1436 | * Enable regardless of ret: | |
1437 | * On success, we enable the new hash. | |
1438 | * On failure, we re-enable the original hash. | |
1439 | */ | |
1440 | ftrace_hash_rec_enable(ops, enable); | |
1441 | ||
1442 | return ret; | |
33dc9b12 SR |
1443 | } |
1444 | ||
b848914c SR |
1445 | /* |
1446 | * Test the hashes for this ops to see if we want to call | |
1447 | * the ops->func or not. | |
1448 | * | |
1449 | * It's a match if the ip is in the ops->filter_hash or | |
1450 | * the filter_hash does not exist or is empty, | |
1451 | * AND | |
1452 | * the ip is not in the ops->notrace_hash. | |
cdbe61bf SR |
1453 | * |
1454 | * This needs to be called with preemption disabled as | |
1455 | * the hashes are freed with call_rcu_sched(). | |
b848914c SR |
1456 | */ |
1457 | static int | |
195a8afc | 1458 | ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs) |
b848914c SR |
1459 | { |
1460 | struct ftrace_hash *filter_hash; | |
1461 | struct ftrace_hash *notrace_hash; | |
1462 | int ret; | |
1463 | ||
195a8afc SRRH |
1464 | #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS |
1465 | /* | |
1466 | * There's a small race when adding ops that the ftrace handler | |
1467 | * that wants regs, may be called without them. We can not | |
1468 | * allow that handler to be called if regs is NULL. | |
1469 | */ | |
1470 | if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS)) | |
1471 | return 0; | |
1472 | #endif | |
1473 | ||
1bb539ca SR |
1474 | filter_hash = rcu_dereference_raw_notrace(ops->filter_hash); |
1475 | notrace_hash = rcu_dereference_raw_notrace(ops->notrace_hash); | |
b848914c | 1476 | |
06a51d93 | 1477 | if ((ftrace_hash_empty(filter_hash) || |
b848914c | 1478 | ftrace_lookup_ip(filter_hash, ip)) && |
06a51d93 | 1479 | (ftrace_hash_empty(notrace_hash) || |
b848914c SR |
1480 | !ftrace_lookup_ip(notrace_hash, ip))) |
1481 | ret = 1; | |
1482 | else | |
1483 | ret = 0; | |
b848914c SR |
1484 | |
1485 | return ret; | |
1486 | } | |
1487 | ||
493762fc SR |
1488 | /* |
1489 | * This is a double for. Do not use 'break' to break out of the loop, | |
1490 | * you must use a goto. | |
1491 | */ | |
1492 | #define do_for_each_ftrace_rec(pg, rec) \ | |
1493 | for (pg = ftrace_pages_start; pg; pg = pg->next) { \ | |
1494 | int _____i; \ | |
1495 | for (_____i = 0; _____i < pg->index; _____i++) { \ | |
1496 | rec = &pg->records[_____i]; | |
1497 | ||
1498 | #define while_for_each_ftrace_rec() \ | |
1499 | } \ | |
1500 | } | |
1501 | ||
5855fead SR |
1502 | |
1503 | static int ftrace_cmp_recs(const void *a, const void *b) | |
1504 | { | |
a650e02a SR |
1505 | const struct dyn_ftrace *key = a; |
1506 | const struct dyn_ftrace *rec = b; | |
5855fead | 1507 | |
a650e02a | 1508 | if (key->flags < rec->ip) |
5855fead | 1509 | return -1; |
a650e02a SR |
1510 | if (key->ip >= rec->ip + MCOUNT_INSN_SIZE) |
1511 | return 1; | |
5855fead SR |
1512 | return 0; |
1513 | } | |
1514 | ||
f0cf973a | 1515 | static unsigned long ftrace_location_range(unsigned long start, unsigned long end) |
c88fd863 SR |
1516 | { |
1517 | struct ftrace_page *pg; | |
1518 | struct dyn_ftrace *rec; | |
5855fead | 1519 | struct dyn_ftrace key; |
c88fd863 | 1520 | |
a650e02a SR |
1521 | key.ip = start; |
1522 | key.flags = end; /* overload flags, as it is unsigned long */ | |
5855fead SR |
1523 | |
1524 | for (pg = ftrace_pages_start; pg; pg = pg->next) { | |
a650e02a SR |
1525 | if (end < pg->records[0].ip || |
1526 | start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE)) | |
9644302e | 1527 | continue; |
5855fead SR |
1528 | rec = bsearch(&key, pg->records, pg->index, |
1529 | sizeof(struct dyn_ftrace), | |
1530 | ftrace_cmp_recs); | |
1531 | if (rec) | |
f0cf973a | 1532 | return rec->ip; |
5855fead | 1533 | } |
c88fd863 SR |
1534 | |
1535 | return 0; | |
1536 | } | |
1537 | ||
a650e02a SR |
1538 | /** |
1539 | * ftrace_location - return true if the ip giving is a traced location | |
1540 | * @ip: the instruction pointer to check | |
1541 | * | |
f0cf973a | 1542 | * Returns rec->ip if @ip given is a pointer to a ftrace location. |
a650e02a SR |
1543 | * That is, the instruction that is either a NOP or call to |
1544 | * the function tracer. It checks the ftrace internal tables to | |
1545 | * determine if the address belongs or not. | |
1546 | */ | |
f0cf973a | 1547 | unsigned long ftrace_location(unsigned long ip) |
a650e02a SR |
1548 | { |
1549 | return ftrace_location_range(ip, ip); | |
1550 | } | |
1551 | ||
1552 | /** | |
1553 | * ftrace_text_reserved - return true if range contains an ftrace location | |
1554 | * @start: start of range to search | |
1555 | * @end: end of range to search (inclusive). @end points to the last byte to check. | |
1556 | * | |
1557 | * Returns 1 if @start and @end contains a ftrace location. | |
1558 | * That is, the instruction that is either a NOP or call to | |
1559 | * the function tracer. It checks the ftrace internal tables to | |
1560 | * determine if the address belongs or not. | |
1561 | */ | |
1562 | int ftrace_text_reserved(void *start, void *end) | |
1563 | { | |
f0cf973a SR |
1564 | unsigned long ret; |
1565 | ||
1566 | ret = ftrace_location_range((unsigned long)start, | |
1567 | (unsigned long)end); | |
1568 | ||
1569 | return (int)!!ret; | |
a650e02a SR |
1570 | } |
1571 | ||
ed926f9b SR |
1572 | static void __ftrace_hash_rec_update(struct ftrace_ops *ops, |
1573 | int filter_hash, | |
1574 | bool inc) | |
1575 | { | |
1576 | struct ftrace_hash *hash; | |
1577 | struct ftrace_hash *other_hash; | |
1578 | struct ftrace_page *pg; | |
1579 | struct dyn_ftrace *rec; | |
1580 | int count = 0; | |
1581 | int all = 0; | |
1582 | ||
1583 | /* Only update if the ops has been registered */ | |
1584 | if (!(ops->flags & FTRACE_OPS_FL_ENABLED)) | |
1585 | return; | |
1586 | ||
1587 | /* | |
1588 | * In the filter_hash case: | |
1589 | * If the count is zero, we update all records. | |
1590 | * Otherwise we just update the items in the hash. | |
1591 | * | |
1592 | * In the notrace_hash case: | |
1593 | * We enable the update in the hash. | |
1594 | * As disabling notrace means enabling the tracing, | |
1595 | * and enabling notrace means disabling, the inc variable | |
1596 | * gets inversed. | |
1597 | */ | |
1598 | if (filter_hash) { | |
1599 | hash = ops->filter_hash; | |
1600 | other_hash = ops->notrace_hash; | |
06a51d93 | 1601 | if (ftrace_hash_empty(hash)) |
ed926f9b SR |
1602 | all = 1; |
1603 | } else { | |
1604 | inc = !inc; | |
1605 | hash = ops->notrace_hash; | |
1606 | other_hash = ops->filter_hash; | |
1607 | /* | |
1608 | * If the notrace hash has no items, | |
1609 | * then there's nothing to do. | |
1610 | */ | |
06a51d93 | 1611 | if (ftrace_hash_empty(hash)) |
ed926f9b SR |
1612 | return; |
1613 | } | |
1614 | ||
1615 | do_for_each_ftrace_rec(pg, rec) { | |
1616 | int in_other_hash = 0; | |
1617 | int in_hash = 0; | |
1618 | int match = 0; | |
1619 | ||
1620 | if (all) { | |
1621 | /* | |
1622 | * Only the filter_hash affects all records. | |
1623 | * Update if the record is not in the notrace hash. | |
1624 | */ | |
b848914c | 1625 | if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip)) |
ed926f9b SR |
1626 | match = 1; |
1627 | } else { | |
06a51d93 SR |
1628 | in_hash = !!ftrace_lookup_ip(hash, rec->ip); |
1629 | in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip); | |
ed926f9b SR |
1630 | |
1631 | /* | |
1632 | * | |
1633 | */ | |
1634 | if (filter_hash && in_hash && !in_other_hash) | |
1635 | match = 1; | |
1636 | else if (!filter_hash && in_hash && | |
06a51d93 | 1637 | (in_other_hash || ftrace_hash_empty(other_hash))) |
ed926f9b SR |
1638 | match = 1; |
1639 | } | |
1640 | if (!match) | |
1641 | continue; | |
1642 | ||
1643 | if (inc) { | |
1644 | rec->flags++; | |
1645 | if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX)) | |
1646 | return; | |
08f6fba5 SR |
1647 | /* |
1648 | * If any ops wants regs saved for this function | |
1649 | * then all ops will get saved regs. | |
1650 | */ | |
1651 | if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) | |
1652 | rec->flags |= FTRACE_FL_REGS; | |
ed926f9b SR |
1653 | } else { |
1654 | if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0)) | |
1655 | return; | |
1656 | rec->flags--; | |
1657 | } | |
1658 | count++; | |
1659 | /* Shortcut, if we handled all records, we are done. */ | |
1660 | if (!all && count == hash->count) | |
1661 | return; | |
1662 | } while_for_each_ftrace_rec(); | |
1663 | } | |
1664 | ||
1665 | static void ftrace_hash_rec_disable(struct ftrace_ops *ops, | |
1666 | int filter_hash) | |
1667 | { | |
1668 | __ftrace_hash_rec_update(ops, filter_hash, 0); | |
1669 | } | |
1670 | ||
1671 | static void ftrace_hash_rec_enable(struct ftrace_ops *ops, | |
1672 | int filter_hash) | |
1673 | { | |
1674 | __ftrace_hash_rec_update(ops, filter_hash, 1); | |
1675 | } | |
1676 | ||
b17e8a37 SR |
1677 | static void print_ip_ins(const char *fmt, unsigned char *p) |
1678 | { | |
1679 | int i; | |
1680 | ||
1681 | printk(KERN_CONT "%s", fmt); | |
1682 | ||
1683 | for (i = 0; i < MCOUNT_INSN_SIZE; i++) | |
1684 | printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]); | |
1685 | } | |
1686 | ||
c88fd863 SR |
1687 | /** |
1688 | * ftrace_bug - report and shutdown function tracer | |
1689 | * @failed: The failed type (EFAULT, EINVAL, EPERM) | |
1690 | * @ip: The address that failed | |
1691 | * | |
1692 | * The arch code that enables or disables the function tracing | |
1693 | * can call ftrace_bug() when it has detected a problem in | |
1694 | * modifying the code. @failed should be one of either: | |
1695 | * EFAULT - if the problem happens on reading the @ip address | |
1696 | * EINVAL - if what is read at @ip is not what was expected | |
1697 | * EPERM - if the problem happens on writting to the @ip address | |
1698 | */ | |
1699 | void ftrace_bug(int failed, unsigned long ip) | |
b17e8a37 SR |
1700 | { |
1701 | switch (failed) { | |
1702 | case -EFAULT: | |
1703 | FTRACE_WARN_ON_ONCE(1); | |
1704 | pr_info("ftrace faulted on modifying "); | |
1705 | print_ip_sym(ip); | |
1706 | break; | |
1707 | case -EINVAL: | |
1708 | FTRACE_WARN_ON_ONCE(1); | |
1709 | pr_info("ftrace failed to modify "); | |
1710 | print_ip_sym(ip); | |
b17e8a37 | 1711 | print_ip_ins(" actual: ", (unsigned char *)ip); |
b17e8a37 SR |
1712 | printk(KERN_CONT "\n"); |
1713 | break; | |
1714 | case -EPERM: | |
1715 | FTRACE_WARN_ON_ONCE(1); | |
1716 | pr_info("ftrace faulted on writing "); | |
1717 | print_ip_sym(ip); | |
1718 | break; | |
1719 | default: | |
1720 | FTRACE_WARN_ON_ONCE(1); | |
1721 | pr_info("ftrace faulted on unknown error "); | |
1722 | print_ip_sym(ip); | |
1723 | } | |
1724 | } | |
1725 | ||
c88fd863 | 1726 | static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update) |
5072c59f | 1727 | { |
64fbcd16 | 1728 | unsigned long flag = 0UL; |
e7d3737e | 1729 | |
982c350b | 1730 | /* |
30fb6aa7 | 1731 | * If we are updating calls: |
982c350b | 1732 | * |
ed926f9b SR |
1733 | * If the record has a ref count, then we need to enable it |
1734 | * because someone is using it. | |
982c350b | 1735 | * |
ed926f9b SR |
1736 | * Otherwise we make sure its disabled. |
1737 | * | |
30fb6aa7 | 1738 | * If we are disabling calls, then disable all records that |
ed926f9b | 1739 | * are enabled. |
982c350b | 1740 | */ |
c88fd863 | 1741 | if (enable && (rec->flags & ~FTRACE_FL_MASK)) |
ed926f9b | 1742 | flag = FTRACE_FL_ENABLED; |
982c350b | 1743 | |
08f6fba5 SR |
1744 | /* |
1745 | * If enabling and the REGS flag does not match the REGS_EN, then | |
1746 | * do not ignore this record. Set flags to fail the compare against | |
1747 | * ENABLED. | |
1748 | */ | |
1749 | if (flag && | |
1750 | (!(rec->flags & FTRACE_FL_REGS) != !(rec->flags & FTRACE_FL_REGS_EN))) | |
1751 | flag |= FTRACE_FL_REGS; | |
1752 | ||
64fbcd16 XG |
1753 | /* If the state of this record hasn't changed, then do nothing */ |
1754 | if ((rec->flags & FTRACE_FL_ENABLED) == flag) | |
c88fd863 | 1755 | return FTRACE_UPDATE_IGNORE; |
982c350b | 1756 | |
64fbcd16 | 1757 | if (flag) { |
08f6fba5 SR |
1758 | /* Save off if rec is being enabled (for return value) */ |
1759 | flag ^= rec->flags & FTRACE_FL_ENABLED; | |
1760 | ||
1761 | if (update) { | |
c88fd863 | 1762 | rec->flags |= FTRACE_FL_ENABLED; |
08f6fba5 SR |
1763 | if (flag & FTRACE_FL_REGS) { |
1764 | if (rec->flags & FTRACE_FL_REGS) | |
1765 | rec->flags |= FTRACE_FL_REGS_EN; | |
1766 | else | |
1767 | rec->flags &= ~FTRACE_FL_REGS_EN; | |
1768 | } | |
1769 | } | |
1770 | ||
1771 | /* | |
1772 | * If this record is being updated from a nop, then | |
1773 | * return UPDATE_MAKE_CALL. | |
1774 | * Otherwise, if the EN flag is set, then return | |
1775 | * UPDATE_MODIFY_CALL_REGS to tell the caller to convert | |
1776 | * from the non-save regs, to a save regs function. | |
1777 | * Otherwise, | |
1778 | * return UPDATE_MODIFY_CALL to tell the caller to convert | |
1779 | * from the save regs, to a non-save regs function. | |
1780 | */ | |
1781 | if (flag & FTRACE_FL_ENABLED) | |
1782 | return FTRACE_UPDATE_MAKE_CALL; | |
1783 | else if (rec->flags & FTRACE_FL_REGS_EN) | |
1784 | return FTRACE_UPDATE_MODIFY_CALL_REGS; | |
1785 | else | |
1786 | return FTRACE_UPDATE_MODIFY_CALL; | |
c88fd863 SR |
1787 | } |
1788 | ||
08f6fba5 SR |
1789 | if (update) { |
1790 | /* If there's no more users, clear all flags */ | |
1791 | if (!(rec->flags & ~FTRACE_FL_MASK)) | |
1792 | rec->flags = 0; | |
1793 | else | |
1794 | /* Just disable the record (keep REGS state) */ | |
1795 | rec->flags &= ~FTRACE_FL_ENABLED; | |
1796 | } | |
c88fd863 SR |
1797 | |
1798 | return FTRACE_UPDATE_MAKE_NOP; | |
1799 | } | |
1800 | ||
1801 | /** | |
1802 | * ftrace_update_record, set a record that now is tracing or not | |
1803 | * @rec: the record to update | |
1804 | * @enable: set to 1 if the record is tracing, zero to force disable | |
1805 | * | |
1806 | * The records that represent all functions that can be traced need | |
1807 | * to be updated when tracing has been enabled. | |
1808 | */ | |
1809 | int ftrace_update_record(struct dyn_ftrace *rec, int enable) | |
1810 | { | |
1811 | return ftrace_check_record(rec, enable, 1); | |
1812 | } | |
1813 | ||
1814 | /** | |
1815 | * ftrace_test_record, check if the record has been enabled or not | |
1816 | * @rec: the record to test | |
1817 | * @enable: set to 1 to check if enabled, 0 if it is disabled | |
1818 | * | |
1819 | * The arch code may need to test if a record is already set to | |
1820 | * tracing to determine how to modify the function code that it | |
1821 | * represents. | |
1822 | */ | |
1823 | int ftrace_test_record(struct dyn_ftrace *rec, int enable) | |
1824 | { | |
1825 | return ftrace_check_record(rec, enable, 0); | |
1826 | } | |
1827 | ||
1828 | static int | |
1829 | __ftrace_replace_code(struct dyn_ftrace *rec, int enable) | |
1830 | { | |
08f6fba5 | 1831 | unsigned long ftrace_old_addr; |
c88fd863 SR |
1832 | unsigned long ftrace_addr; |
1833 | int ret; | |
1834 | ||
c88fd863 SR |
1835 | ret = ftrace_update_record(rec, enable); |
1836 | ||
08f6fba5 SR |
1837 | if (rec->flags & FTRACE_FL_REGS) |
1838 | ftrace_addr = (unsigned long)FTRACE_REGS_ADDR; | |
1839 | else | |
1840 | ftrace_addr = (unsigned long)FTRACE_ADDR; | |
1841 | ||
c88fd863 SR |
1842 | switch (ret) { |
1843 | case FTRACE_UPDATE_IGNORE: | |
1844 | return 0; | |
1845 | ||
1846 | case FTRACE_UPDATE_MAKE_CALL: | |
64fbcd16 | 1847 | return ftrace_make_call(rec, ftrace_addr); |
c88fd863 SR |
1848 | |
1849 | case FTRACE_UPDATE_MAKE_NOP: | |
1850 | return ftrace_make_nop(NULL, rec, ftrace_addr); | |
08f6fba5 SR |
1851 | |
1852 | case FTRACE_UPDATE_MODIFY_CALL_REGS: | |
1853 | case FTRACE_UPDATE_MODIFY_CALL: | |
1854 | if (rec->flags & FTRACE_FL_REGS) | |
1855 | ftrace_old_addr = (unsigned long)FTRACE_ADDR; | |
1856 | else | |
1857 | ftrace_old_addr = (unsigned long)FTRACE_REGS_ADDR; | |
1858 | ||
1859 | return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr); | |
5072c59f SR |
1860 | } |
1861 | ||
c88fd863 | 1862 | return -1; /* unknow ftrace bug */ |
5072c59f SR |
1863 | } |
1864 | ||
e4f5d544 | 1865 | void __weak ftrace_replace_code(int enable) |
3c1720f0 | 1866 | { |
3c1720f0 SR |
1867 | struct dyn_ftrace *rec; |
1868 | struct ftrace_page *pg; | |
6a24a244 | 1869 | int failed; |
3c1720f0 | 1870 | |
45a4a237 SR |
1871 | if (unlikely(ftrace_disabled)) |
1872 | return; | |
1873 | ||
265c831c | 1874 | do_for_each_ftrace_rec(pg, rec) { |
e4f5d544 | 1875 | failed = __ftrace_replace_code(rec, enable); |
fa9d13cf | 1876 | if (failed) { |
3279ba37 SR |
1877 | ftrace_bug(failed, rec->ip); |
1878 | /* Stop processing */ | |
1879 | return; | |
3c1720f0 | 1880 | } |
265c831c | 1881 | } while_for_each_ftrace_rec(); |
3c1720f0 SR |
1882 | } |
1883 | ||
c88fd863 SR |
1884 | struct ftrace_rec_iter { |
1885 | struct ftrace_page *pg; | |
1886 | int index; | |
1887 | }; | |
1888 | ||
1889 | /** | |
1890 | * ftrace_rec_iter_start, start up iterating over traced functions | |
1891 | * | |
1892 | * Returns an iterator handle that is used to iterate over all | |
1893 | * the records that represent address locations where functions | |
1894 | * are traced. | |
1895 | * | |
1896 | * May return NULL if no records are available. | |
1897 | */ | |
1898 | struct ftrace_rec_iter *ftrace_rec_iter_start(void) | |
1899 | { | |
1900 | /* | |
1901 | * We only use a single iterator. | |
1902 | * Protected by the ftrace_lock mutex. | |
1903 | */ | |
1904 | static struct ftrace_rec_iter ftrace_rec_iter; | |
1905 | struct ftrace_rec_iter *iter = &ftrace_rec_iter; | |
1906 | ||
1907 | iter->pg = ftrace_pages_start; | |
1908 | iter->index = 0; | |
1909 | ||
1910 | /* Could have empty pages */ | |
1911 | while (iter->pg && !iter->pg->index) | |
1912 | iter->pg = iter->pg->next; | |
1913 | ||
1914 | if (!iter->pg) | |
1915 | return NULL; | |
1916 | ||
1917 | return iter; | |
1918 | } | |
1919 | ||
1920 | /** | |
1921 | * ftrace_rec_iter_next, get the next record to process. | |
1922 | * @iter: The handle to the iterator. | |
1923 | * | |
1924 | * Returns the next iterator after the given iterator @iter. | |
1925 | */ | |
1926 | struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter) | |
1927 | { | |
1928 | iter->index++; | |
1929 | ||
1930 | if (iter->index >= iter->pg->index) { | |
1931 | iter->pg = iter->pg->next; | |
1932 | iter->index = 0; | |
1933 | ||
1934 | /* Could have empty pages */ | |
1935 | while (iter->pg && !iter->pg->index) | |
1936 | iter->pg = iter->pg->next; | |
1937 | } | |
1938 | ||
1939 | if (!iter->pg) | |
1940 | return NULL; | |
1941 | ||
1942 | return iter; | |
1943 | } | |
1944 | ||
1945 | /** | |
1946 | * ftrace_rec_iter_record, get the record at the iterator location | |
1947 | * @iter: The current iterator location | |
1948 | * | |
1949 | * Returns the record that the current @iter is at. | |
1950 | */ | |
1951 | struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter) | |
1952 | { | |
1953 | return &iter->pg->records[iter->index]; | |
1954 | } | |
1955 | ||
492a7ea5 | 1956 | static int |
31e88909 | 1957 | ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec) |
3c1720f0 SR |
1958 | { |
1959 | unsigned long ip; | |
593eb8a2 | 1960 | int ret; |
3c1720f0 SR |
1961 | |
1962 | ip = rec->ip; | |
1963 | ||
45a4a237 SR |
1964 | if (unlikely(ftrace_disabled)) |
1965 | return 0; | |
1966 | ||
25aac9dc | 1967 | ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR); |
593eb8a2 | 1968 | if (ret) { |
31e88909 | 1969 | ftrace_bug(ret, ip); |
492a7ea5 | 1970 | return 0; |
37ad5084 | 1971 | } |
492a7ea5 | 1972 | return 1; |
3c1720f0 SR |
1973 | } |
1974 | ||
000ab691 SR |
1975 | /* |
1976 | * archs can override this function if they must do something | |
1977 | * before the modifying code is performed. | |
1978 | */ | |
1979 | int __weak ftrace_arch_code_modify_prepare(void) | |
1980 | { | |
1981 | return 0; | |
1982 | } | |
1983 | ||
1984 | /* | |
1985 | * archs can override this function if they must do something | |
1986 | * after the modifying code is performed. | |
1987 | */ | |
1988 | int __weak ftrace_arch_code_modify_post_process(void) | |
1989 | { | |
1990 | return 0; | |
1991 | } | |
1992 | ||
8ed3e2cf | 1993 | void ftrace_modify_all_code(int command) |
3d083395 | 1994 | { |
59338f75 SRRH |
1995 | int update = command & FTRACE_UPDATE_TRACE_FUNC; |
1996 | ||
1997 | /* | |
1998 | * If the ftrace_caller calls a ftrace_ops func directly, | |
1999 | * we need to make sure that it only traces functions it | |
2000 | * expects to trace. When doing the switch of functions, | |
2001 | * we need to update to the ftrace_ops_list_func first | |
2002 | * before the transition between old and new calls are set, | |
2003 | * as the ftrace_ops_list_func will check the ops hashes | |
2004 | * to make sure the ops are having the right functions | |
2005 | * traced. | |
2006 | */ | |
2007 | if (update) | |
2008 | ftrace_update_ftrace_func(ftrace_ops_list_func); | |
2009 | ||
8ed3e2cf | 2010 | if (command & FTRACE_UPDATE_CALLS) |
d61f82d0 | 2011 | ftrace_replace_code(1); |
8ed3e2cf | 2012 | else if (command & FTRACE_DISABLE_CALLS) |
d61f82d0 SR |
2013 | ftrace_replace_code(0); |
2014 | ||
405e1d83 SRRH |
2015 | if (update && ftrace_trace_function != ftrace_ops_list_func) { |
2016 | function_trace_op = set_function_trace_op; | |
2017 | smp_wmb(); | |
2018 | /* If irqs are disabled, we are in stop machine */ | |
2019 | if (!irqs_disabled()) | |
2020 | smp_call_function(ftrace_sync_ipi, NULL, 1); | |
d61f82d0 | 2021 | ftrace_update_ftrace_func(ftrace_trace_function); |
405e1d83 | 2022 | } |
d61f82d0 | 2023 | |
8ed3e2cf | 2024 | if (command & FTRACE_START_FUNC_RET) |
5a45cfe1 | 2025 | ftrace_enable_ftrace_graph_caller(); |
8ed3e2cf | 2026 | else if (command & FTRACE_STOP_FUNC_RET) |
5a45cfe1 | 2027 | ftrace_disable_ftrace_graph_caller(); |
8ed3e2cf SR |
2028 | } |
2029 | ||
2030 | static int __ftrace_modify_code(void *data) | |
2031 | { | |
2032 | int *command = data; | |
2033 | ||
2034 | ftrace_modify_all_code(*command); | |
5a45cfe1 | 2035 | |
d61f82d0 | 2036 | return 0; |
3d083395 SR |
2037 | } |
2038 | ||
c88fd863 SR |
2039 | /** |
2040 | * ftrace_run_stop_machine, go back to the stop machine method | |
2041 | * @command: The command to tell ftrace what to do | |
2042 | * | |
2043 | * If an arch needs to fall back to the stop machine method, the | |
2044 | * it can call this function. | |
2045 | */ | |
2046 | void ftrace_run_stop_machine(int command) | |
2047 | { | |
2048 | stop_machine(__ftrace_modify_code, &command, NULL); | |
2049 | } | |
2050 | ||
2051 | /** | |
2052 | * arch_ftrace_update_code, modify the code to trace or not trace | |
2053 | * @command: The command that needs to be done | |
2054 | * | |
2055 | * Archs can override this function if it does not need to | |
2056 | * run stop_machine() to modify code. | |
2057 | */ | |
2058 | void __weak arch_ftrace_update_code(int command) | |
2059 | { | |
2060 | ftrace_run_stop_machine(command); | |
2061 | } | |
2062 | ||
e309b41d | 2063 | static void ftrace_run_update_code(int command) |
3d083395 | 2064 | { |
000ab691 SR |
2065 | int ret; |
2066 | ||
2067 | ret = ftrace_arch_code_modify_prepare(); | |
2068 | FTRACE_WARN_ON(ret); | |
2069 | if (ret) | |
2070 | return; | |
c88fd863 SR |
2071 | /* |
2072 | * Do not call function tracer while we update the code. | |
2073 | * We are in stop machine. | |
2074 | */ | |
2075 | function_trace_stop++; | |
000ab691 | 2076 | |
c88fd863 SR |
2077 | /* |
2078 | * By default we use stop_machine() to modify the code. | |
2079 | * But archs can do what ever they want as long as it | |
2080 | * is safe. The stop_machine() is the safest, but also | |
2081 | * produces the most overhead. | |
2082 | */ | |
2083 | arch_ftrace_update_code(command); | |
2084 | ||
c88fd863 | 2085 | function_trace_stop--; |
000ab691 SR |
2086 | |
2087 | ret = ftrace_arch_code_modify_post_process(); | |
2088 | FTRACE_WARN_ON(ret); | |
3d083395 SR |
2089 | } |
2090 | ||
d61f82d0 | 2091 | static ftrace_func_t saved_ftrace_func; |
60a7ecf4 | 2092 | static int ftrace_start_up; |
b848914c | 2093 | static int global_start_up; |
df4fc315 SR |
2094 | |
2095 | static void ftrace_startup_enable(int command) | |
2096 | { | |
2097 | if (saved_ftrace_func != ftrace_trace_function) { | |
2098 | saved_ftrace_func = ftrace_trace_function; | |
2099 | command |= FTRACE_UPDATE_TRACE_FUNC; | |
2100 | } | |
2101 | ||
2102 | if (!command || !ftrace_enabled) | |
2103 | return; | |
2104 | ||
2105 | ftrace_run_update_code(command); | |
2106 | } | |
d61f82d0 | 2107 | |
a1cd6173 | 2108 | static int ftrace_startup(struct ftrace_ops *ops, int command) |
3d083395 | 2109 | { |
b848914c | 2110 | bool hash_enable = true; |
8a56d776 | 2111 | int ret; |
b848914c | 2112 | |
4eebcc81 | 2113 | if (unlikely(ftrace_disabled)) |
a1cd6173 | 2114 | return -ENODEV; |
4eebcc81 | 2115 | |
8a56d776 SRRH |
2116 | ret = __register_ftrace_function(ops); |
2117 | if (ret) | |
2118 | return ret; | |
2119 | ||
60a7ecf4 | 2120 | ftrace_start_up++; |
30fb6aa7 | 2121 | command |= FTRACE_UPDATE_CALLS; |
d61f82d0 | 2122 | |
b848914c SR |
2123 | /* ops marked global share the filter hashes */ |
2124 | if (ops->flags & FTRACE_OPS_FL_GLOBAL) { | |
2125 | ops = &global_ops; | |
2126 | /* Don't update hash if global is already set */ | |
2127 | if (global_start_up) | |
2128 | hash_enable = false; | |
2129 | global_start_up++; | |
2130 | } | |
2131 | ||
ed926f9b | 2132 | ops->flags |= FTRACE_OPS_FL_ENABLED; |
b848914c | 2133 | if (hash_enable) |
ed926f9b SR |
2134 | ftrace_hash_rec_enable(ops, 1); |
2135 | ||
df4fc315 | 2136 | ftrace_startup_enable(command); |
a1cd6173 SR |
2137 | |
2138 | return 0; | |
3d083395 SR |
2139 | } |
2140 | ||
8a56d776 | 2141 | static int ftrace_shutdown(struct ftrace_ops *ops, int command) |
3d083395 | 2142 | { |
b848914c | 2143 | bool hash_disable = true; |
8a56d776 | 2144 | int ret; |
b848914c | 2145 | |
4eebcc81 | 2146 | if (unlikely(ftrace_disabled)) |
8a56d776 SRRH |
2147 | return -ENODEV; |
2148 | ||
2149 | ret = __unregister_ftrace_function(ops); | |
2150 | if (ret) | |
2151 | return ret; | |
4eebcc81 | 2152 | |
60a7ecf4 | 2153 | ftrace_start_up--; |
9ea1a153 FW |
2154 | /* |
2155 | * Just warn in case of unbalance, no need to kill ftrace, it's not | |
2156 | * critical but the ftrace_call callers may be never nopped again after | |
2157 | * further ftrace uses. | |
2158 | */ | |
2159 | WARN_ON_ONCE(ftrace_start_up < 0); | |
2160 | ||
b848914c SR |
2161 | if (ops->flags & FTRACE_OPS_FL_GLOBAL) { |
2162 | ops = &global_ops; | |
2163 | global_start_up--; | |
2164 | WARN_ON_ONCE(global_start_up < 0); | |
2165 | /* Don't update hash if global still has users */ | |
2166 | if (global_start_up) { | |
2167 | WARN_ON_ONCE(!ftrace_start_up); | |
2168 | hash_disable = false; | |
2169 | } | |
2170 | } | |
2171 | ||
2172 | if (hash_disable) | |
ed926f9b SR |
2173 | ftrace_hash_rec_disable(ops, 1); |
2174 | ||
b848914c | 2175 | if (ops != &global_ops || !global_start_up) |
ed926f9b | 2176 | ops->flags &= ~FTRACE_OPS_FL_ENABLED; |
b848914c | 2177 | |
30fb6aa7 | 2178 | command |= FTRACE_UPDATE_CALLS; |
3d083395 | 2179 | |
d61f82d0 SR |
2180 | if (saved_ftrace_func != ftrace_trace_function) { |
2181 | saved_ftrace_func = ftrace_trace_function; | |
2182 | command |= FTRACE_UPDATE_TRACE_FUNC; | |
2183 | } | |
3d083395 | 2184 | |
a4c35ed2 SRRH |
2185 | if (!command || !ftrace_enabled) { |
2186 | /* | |
2187 | * If these are control ops, they still need their | |
2188 | * per_cpu field freed. Since, function tracing is | |
2189 | * not currently active, we can just free them | |
2190 | * without synchronizing all CPUs. | |
2191 | */ | |
2192 | if (ops->flags & FTRACE_OPS_FL_CONTROL) | |
2193 | control_ops_free(ops); | |
8a56d776 | 2194 | return 0; |
a4c35ed2 | 2195 | } |
d61f82d0 SR |
2196 | |
2197 | ftrace_run_update_code(command); | |
a4c35ed2 SRRH |
2198 | |
2199 | /* | |
2200 | * Dynamic ops may be freed, we must make sure that all | |
2201 | * callers are done before leaving this function. | |
2202 | * The same goes for freeing the per_cpu data of the control | |
2203 | * ops. | |
2204 | * | |
2205 | * Again, normal synchronize_sched() is not good enough. | |
2206 | * We need to do a hard force of sched synchronization. | |
2207 | * This is because we use preempt_disable() to do RCU, but | |
2208 | * the function tracers can be called where RCU is not watching | |
2209 | * (like before user_exit()). We can not rely on the RCU | |
2210 | * infrastructure to do the synchronization, thus we must do it | |
2211 | * ourselves. | |
2212 | */ | |
2213 | if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_CONTROL)) { | |
2214 | schedule_on_each_cpu(ftrace_sync); | |
2215 | ||
2216 | if (ops->flags & FTRACE_OPS_FL_CONTROL) | |
2217 | control_ops_free(ops); | |
2218 | } | |
2219 | ||
8a56d776 | 2220 | return 0; |
3d083395 SR |
2221 | } |
2222 | ||
e309b41d | 2223 | static void ftrace_startup_sysctl(void) |
b0fc494f | 2224 | { |
4eebcc81 SR |
2225 | if (unlikely(ftrace_disabled)) |
2226 | return; | |
2227 | ||
d61f82d0 SR |
2228 | /* Force update next time */ |
2229 | saved_ftrace_func = NULL; | |
60a7ecf4 SR |
2230 | /* ftrace_start_up is true if we want ftrace running */ |
2231 | if (ftrace_start_up) | |
30fb6aa7 | 2232 | ftrace_run_update_code(FTRACE_UPDATE_CALLS); |
b0fc494f SR |
2233 | } |
2234 | ||
e309b41d | 2235 | static void ftrace_shutdown_sysctl(void) |
b0fc494f | 2236 | { |
4eebcc81 SR |
2237 | if (unlikely(ftrace_disabled)) |
2238 | return; | |
2239 | ||
60a7ecf4 SR |
2240 | /* ftrace_start_up is true if ftrace is running */ |
2241 | if (ftrace_start_up) | |
79e406d7 | 2242 | ftrace_run_update_code(FTRACE_DISABLE_CALLS); |
b0fc494f SR |
2243 | } |
2244 | ||
3d083395 SR |
2245 | static cycle_t ftrace_update_time; |
2246 | static unsigned long ftrace_update_cnt; | |
2247 | unsigned long ftrace_update_tot_cnt; | |
2248 | ||
8c4f3c3f | 2249 | static inline int ops_traces_mod(struct ftrace_ops *ops) |
f7bc8b61 | 2250 | { |
8c4f3c3f SRRH |
2251 | /* |
2252 | * Filter_hash being empty will default to trace module. | |
2253 | * But notrace hash requires a test of individual module functions. | |
2254 | */ | |
2255 | return ftrace_hash_empty(ops->filter_hash) && | |
2256 | ftrace_hash_empty(ops->notrace_hash); | |
2257 | } | |
2258 | ||
2259 | /* | |
2260 | * Check if the current ops references the record. | |
2261 | * | |
2262 | * If the ops traces all functions, then it was already accounted for. | |
2263 | * If the ops does not trace the current record function, skip it. | |
2264 | * If the ops ignores the function via notrace filter, skip it. | |
2265 | */ | |
2266 | static inline bool | |
2267 | ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec) | |
2268 | { | |
2269 | /* If ops isn't enabled, ignore it */ | |
2270 | if (!(ops->flags & FTRACE_OPS_FL_ENABLED)) | |
2271 | return 0; | |
2272 | ||
2273 | /* If ops traces all mods, we already accounted for it */ | |
2274 | if (ops_traces_mod(ops)) | |
2275 | return 0; | |
2276 | ||
2277 | /* The function must be in the filter */ | |
2278 | if (!ftrace_hash_empty(ops->filter_hash) && | |
2279 | !ftrace_lookup_ip(ops->filter_hash, rec->ip)) | |
2280 | return 0; | |
f7bc8b61 | 2281 | |
8c4f3c3f SRRH |
2282 | /* If in notrace hash, we ignore it too */ |
2283 | if (ftrace_lookup_ip(ops->notrace_hash, rec->ip)) | |
2284 | return 0; | |
2285 | ||
2286 | return 1; | |
2287 | } | |
2288 | ||
2289 | static int referenced_filters(struct dyn_ftrace *rec) | |
2290 | { | |
2291 | struct ftrace_ops *ops; | |
2292 | int cnt = 0; | |
2293 | ||
2294 | for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) { | |
2295 | if (ops_references_rec(ops, rec)) | |
2296 | cnt++; | |
2297 | } | |
2298 | ||
2299 | return cnt; | |
f7bc8b61 SR |
2300 | } |
2301 | ||
31e88909 | 2302 | static int ftrace_update_code(struct module *mod) |
3d083395 | 2303 | { |
85ae32ae | 2304 | struct ftrace_page *pg; |
e94142a6 | 2305 | struct dyn_ftrace *p; |
f22f9a89 | 2306 | cycle_t start, stop; |
f7bc8b61 | 2307 | unsigned long ref = 0; |
8c4f3c3f | 2308 | bool test = false; |
85ae32ae | 2309 | int i; |
f7bc8b61 SR |
2310 | |
2311 | /* | |
2312 | * When adding a module, we need to check if tracers are | |
2313 | * currently enabled and if they are set to trace all functions. | |
2314 | * If they are, we need to enable the module functions as well | |
2315 | * as update the reference counts for those function records. | |
2316 | */ | |
2317 | if (mod) { | |
2318 | struct ftrace_ops *ops; | |
2319 | ||
2320 | for (ops = ftrace_ops_list; | |
2321 | ops != &ftrace_list_end; ops = ops->next) { | |
8c4f3c3f SRRH |
2322 | if (ops->flags & FTRACE_OPS_FL_ENABLED) { |
2323 | if (ops_traces_mod(ops)) | |
2324 | ref++; | |
2325 | else | |
2326 | test = true; | |
2327 | } | |
f7bc8b61 SR |
2328 | } |
2329 | } | |
3d083395 | 2330 | |
750ed1a4 | 2331 | start = ftrace_now(raw_smp_processor_id()); |
3d083395 SR |
2332 | ftrace_update_cnt = 0; |
2333 | ||
85ae32ae | 2334 | for (pg = ftrace_new_pgs; pg; pg = pg->next) { |
3d083395 | 2335 | |
85ae32ae | 2336 | for (i = 0; i < pg->index; i++) { |
8c4f3c3f SRRH |
2337 | int cnt = ref; |
2338 | ||
85ae32ae SR |
2339 | /* If something went wrong, bail without enabling anything */ |
2340 | if (unlikely(ftrace_disabled)) | |
2341 | return -1; | |
f22f9a89 | 2342 | |
85ae32ae | 2343 | p = &pg->records[i]; |
8c4f3c3f SRRH |
2344 | if (test) |
2345 | cnt += referenced_filters(p); | |
2346 | p->flags = cnt; | |
f22f9a89 | 2347 | |
85ae32ae SR |
2348 | /* |
2349 | * Do the initial record conversion from mcount jump | |
2350 | * to the NOP instructions. | |
2351 | */ | |
2352 | if (!ftrace_code_disable(mod, p)) | |
2353 | break; | |
5cb084bb | 2354 | |
85ae32ae | 2355 | ftrace_update_cnt++; |
5cb084bb | 2356 | |
85ae32ae SR |
2357 | /* |
2358 | * If the tracing is enabled, go ahead and enable the record. | |
2359 | * | |
2360 | * The reason not to enable the record immediatelly is the | |
2361 | * inherent check of ftrace_make_nop/ftrace_make_call for | |
2362 | * correct previous instructions. Making first the NOP | |
2363 | * conversion puts the module to the correct state, thus | |
2364 | * passing the ftrace_make_call check. | |
2365 | */ | |
8c4f3c3f | 2366 | if (ftrace_start_up && cnt) { |
85ae32ae SR |
2367 | int failed = __ftrace_replace_code(p, 1); |
2368 | if (failed) | |
2369 | ftrace_bug(failed, p->ip); | |
2370 | } | |
5cb084bb | 2371 | } |
3d083395 SR |
2372 | } |
2373 | ||
85ae32ae SR |
2374 | ftrace_new_pgs = NULL; |
2375 | ||
750ed1a4 | 2376 | stop = ftrace_now(raw_smp_processor_id()); |
3d083395 SR |
2377 | ftrace_update_time = stop - start; |
2378 | ftrace_update_tot_cnt += ftrace_update_cnt; | |
2379 | ||
16444a8a ACM |
2380 | return 0; |
2381 | } | |
2382 | ||
a7900875 | 2383 | static int ftrace_allocate_records(struct ftrace_page *pg, int count) |
3c1720f0 | 2384 | { |
a7900875 | 2385 | int order; |
3c1720f0 | 2386 | int cnt; |
3c1720f0 | 2387 | |
a7900875 SR |
2388 | if (WARN_ON(!count)) |
2389 | return -EINVAL; | |
2390 | ||
2391 | order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE)); | |
3c1720f0 SR |
2392 | |
2393 | /* | |
a7900875 SR |
2394 | * We want to fill as much as possible. No more than a page |
2395 | * may be empty. | |
3c1720f0 | 2396 | */ |
a7900875 SR |
2397 | while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE) |
2398 | order--; | |
3c1720f0 | 2399 | |
a7900875 SR |
2400 | again: |
2401 | pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order); | |
3c1720f0 | 2402 | |
a7900875 SR |
2403 | if (!pg->records) { |
2404 | /* if we can't allocate this size, try something smaller */ | |
2405 | if (!order) | |
2406 | return -ENOMEM; | |
2407 | order >>= 1; | |
2408 | goto again; | |
2409 | } | |
3c1720f0 | 2410 | |
a7900875 SR |
2411 | cnt = (PAGE_SIZE << order) / ENTRY_SIZE; |
2412 | pg->size = cnt; | |
3c1720f0 | 2413 | |
a7900875 SR |
2414 | if (cnt > count) |
2415 | cnt = count; | |
2416 | ||
2417 | return cnt; | |
2418 | } | |
2419 | ||
2420 | static struct ftrace_page * | |
2421 | ftrace_allocate_pages(unsigned long num_to_init) | |
2422 | { | |
2423 | struct ftrace_page *start_pg; | |
2424 | struct ftrace_page *pg; | |
2425 | int order; | |
2426 | int cnt; | |
2427 | ||
2428 | if (!num_to_init) | |
2429 | return 0; | |
2430 | ||
2431 | start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL); | |
2432 | if (!pg) | |
2433 | return NULL; | |
2434 | ||
2435 | /* | |
2436 | * Try to allocate as much as possible in one continues | |
2437 | * location that fills in all of the space. We want to | |
2438 | * waste as little space as possible. | |
2439 | */ | |
2440 | for (;;) { | |
2441 | cnt = ftrace_allocate_records(pg, num_to_init); | |
2442 | if (cnt < 0) | |
2443 | goto free_pages; | |
2444 | ||
2445 | num_to_init -= cnt; | |
2446 | if (!num_to_init) | |
3c1720f0 SR |
2447 | break; |
2448 | ||
a7900875 SR |
2449 | pg->next = kzalloc(sizeof(*pg), GFP_KERNEL); |
2450 | if (!pg->next) | |
2451 | goto free_pages; | |
2452 | ||
3c1720f0 SR |
2453 | pg = pg->next; |
2454 | } | |
2455 | ||
a7900875 SR |
2456 | return start_pg; |
2457 | ||
2458 | free_pages: | |
2459 | while (start_pg) { | |
2460 | order = get_count_order(pg->size / ENTRIES_PER_PAGE); | |
2461 | free_pages((unsigned long)pg->records, order); | |
2462 | start_pg = pg->next; | |
2463 | kfree(pg); | |
2464 | pg = start_pg; | |
2465 | } | |
2466 | pr_info("ftrace: FAILED to allocate memory for functions\n"); | |
2467 | return NULL; | |
2468 | } | |
2469 | ||
2470 | static int __init ftrace_dyn_table_alloc(unsigned long num_to_init) | |
2471 | { | |
2472 | int cnt; | |
2473 | ||
2474 | if (!num_to_init) { | |
2475 | pr_info("ftrace: No functions to be traced?\n"); | |
2476 | return -1; | |
2477 | } | |
2478 | ||
2479 | cnt = num_to_init / ENTRIES_PER_PAGE; | |
2480 | pr_info("ftrace: allocating %ld entries in %d pages\n", | |
2481 | num_to_init, cnt + 1); | |
2482 | ||
3c1720f0 SR |
2483 | return 0; |
2484 | } | |
2485 | ||
5072c59f SR |
2486 | #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */ |
2487 | ||
2488 | struct ftrace_iterator { | |
98c4fd04 | 2489 | loff_t pos; |
4aeb6967 SR |
2490 | loff_t func_pos; |
2491 | struct ftrace_page *pg; | |
2492 | struct dyn_ftrace *func; | |
2493 | struct ftrace_func_probe *probe; | |
2494 | struct trace_parser parser; | |
1cf41dd7 | 2495 | struct ftrace_hash *hash; |
33dc9b12 | 2496 | struct ftrace_ops *ops; |
4aeb6967 SR |
2497 | int hidx; |
2498 | int idx; | |
2499 | unsigned flags; | |
5072c59f SR |
2500 | }; |
2501 | ||
8fc0c701 | 2502 | static void * |
4aeb6967 | 2503 | t_hash_next(struct seq_file *m, loff_t *pos) |
8fc0c701 SR |
2504 | { |
2505 | struct ftrace_iterator *iter = m->private; | |
4aeb6967 | 2506 | struct hlist_node *hnd = NULL; |
8fc0c701 SR |
2507 | struct hlist_head *hhd; |
2508 | ||
8fc0c701 | 2509 | (*pos)++; |
98c4fd04 | 2510 | iter->pos = *pos; |
8fc0c701 | 2511 | |
4aeb6967 SR |
2512 | if (iter->probe) |
2513 | hnd = &iter->probe->node; | |
8fc0c701 SR |
2514 | retry: |
2515 | if (iter->hidx >= FTRACE_FUNC_HASHSIZE) | |
2516 | return NULL; | |
2517 | ||
2518 | hhd = &ftrace_func_hash[iter->hidx]; | |
2519 | ||
2520 | if (hlist_empty(hhd)) { | |
2521 | iter->hidx++; | |
2522 | hnd = NULL; | |
2523 | goto retry; | |
2524 | } | |
2525 | ||
2526 | if (!hnd) | |
2527 | hnd = hhd->first; | |
2528 | else { | |
2529 | hnd = hnd->next; | |
2530 | if (!hnd) { | |
2531 | iter->hidx++; | |
2532 | goto retry; | |
2533 | } | |
2534 | } | |
2535 | ||
4aeb6967 SR |
2536 | if (WARN_ON_ONCE(!hnd)) |
2537 | return NULL; | |
2538 | ||
2539 | iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node); | |
2540 | ||
2541 | return iter; | |
8fc0c701 SR |
2542 | } |
2543 | ||
2544 | static void *t_hash_start(struct seq_file *m, loff_t *pos) | |
2545 | { | |
2546 | struct ftrace_iterator *iter = m->private; | |
2547 | void *p = NULL; | |
d82d6244 LZ |
2548 | loff_t l; |
2549 | ||
69a3083c SR |
2550 | if (!(iter->flags & FTRACE_ITER_DO_HASH)) |
2551 | return NULL; | |
2552 | ||
2bccfffd SR |
2553 | if (iter->func_pos > *pos) |
2554 | return NULL; | |
8fc0c701 | 2555 | |
d82d6244 | 2556 | iter->hidx = 0; |
2bccfffd | 2557 | for (l = 0; l <= (*pos - iter->func_pos); ) { |
4aeb6967 | 2558 | p = t_hash_next(m, &l); |
d82d6244 LZ |
2559 | if (!p) |
2560 | break; | |
2561 | } | |
4aeb6967 SR |
2562 | if (!p) |
2563 | return NULL; | |
2564 | ||
98c4fd04 SR |
2565 | /* Only set this if we have an item */ |
2566 | iter->flags |= FTRACE_ITER_HASH; | |
2567 | ||
4aeb6967 | 2568 | return iter; |
8fc0c701 SR |
2569 | } |
2570 | ||
4aeb6967 SR |
2571 | static int |
2572 | t_hash_show(struct seq_file *m, struct ftrace_iterator *iter) | |
8fc0c701 | 2573 | { |
b6887d79 | 2574 | struct ftrace_func_probe *rec; |
8fc0c701 | 2575 | |
4aeb6967 SR |
2576 | rec = iter->probe; |
2577 | if (WARN_ON_ONCE(!rec)) | |
2578 | return -EIO; | |
8fc0c701 | 2579 | |
809dcf29 SR |
2580 | if (rec->ops->print) |
2581 | return rec->ops->print(m, rec->ip, rec->ops, rec->data); | |
2582 | ||
b375a11a | 2583 | seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func); |
8fc0c701 SR |
2584 | |
2585 | if (rec->data) | |
2586 | seq_printf(m, ":%p", rec->data); | |
2587 | seq_putc(m, '\n'); | |
2588 | ||
2589 | return 0; | |
2590 | } | |
2591 | ||
e309b41d | 2592 | static void * |
5072c59f SR |
2593 | t_next(struct seq_file *m, void *v, loff_t *pos) |
2594 | { | |
2595 | struct ftrace_iterator *iter = m->private; | |
fc13cb0c | 2596 | struct ftrace_ops *ops = iter->ops; |
5072c59f SR |
2597 | struct dyn_ftrace *rec = NULL; |
2598 | ||
45a4a237 SR |
2599 | if (unlikely(ftrace_disabled)) |
2600 | return NULL; | |
2601 | ||
8fc0c701 | 2602 | if (iter->flags & FTRACE_ITER_HASH) |
4aeb6967 | 2603 | return t_hash_next(m, pos); |
8fc0c701 | 2604 | |
5072c59f | 2605 | (*pos)++; |
1106b699 | 2606 | iter->pos = iter->func_pos = *pos; |
5072c59f | 2607 | |
0c75a3ed | 2608 | if (iter->flags & FTRACE_ITER_PRINTALL) |
57c072c7 | 2609 | return t_hash_start(m, pos); |
0c75a3ed | 2610 | |
5072c59f SR |
2611 | retry: |
2612 | if (iter->idx >= iter->pg->index) { | |
2613 | if (iter->pg->next) { | |
2614 | iter->pg = iter->pg->next; | |
2615 | iter->idx = 0; | |
2616 | goto retry; | |
2617 | } | |
2618 | } else { | |
2619 | rec = &iter->pg->records[iter->idx++]; | |
32082309 | 2620 | if (((iter->flags & FTRACE_ITER_FILTER) && |
f45948e8 | 2621 | !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) || |
0183fb1c | 2622 | |
41c52c0d | 2623 | ((iter->flags & FTRACE_ITER_NOTRACE) && |
647bcd03 SR |
2624 | !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) || |
2625 | ||
2626 | ((iter->flags & FTRACE_ITER_ENABLED) && | |
23ea9c4d | 2627 | !(rec->flags & FTRACE_FL_ENABLED))) { |
647bcd03 | 2628 | |
5072c59f SR |
2629 | rec = NULL; |
2630 | goto retry; | |
2631 | } | |
2632 | } | |
2633 | ||
4aeb6967 | 2634 | if (!rec) |
57c072c7 | 2635 | return t_hash_start(m, pos); |
4aeb6967 SR |
2636 | |
2637 | iter->func = rec; | |
2638 | ||
2639 | return iter; | |
5072c59f SR |
2640 | } |
2641 | ||
98c4fd04 SR |
2642 | static void reset_iter_read(struct ftrace_iterator *iter) |
2643 | { | |
2644 | iter->pos = 0; | |
2645 | iter->func_pos = 0; | |
70f77b3f | 2646 | iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH); |
5072c59f SR |
2647 | } |
2648 | ||
2649 | static void *t_start(struct seq_file *m, loff_t *pos) | |
2650 | { | |
2651 | struct ftrace_iterator *iter = m->private; | |
fc13cb0c | 2652 | struct ftrace_ops *ops = iter->ops; |
5072c59f | 2653 | void *p = NULL; |
694ce0a5 | 2654 | loff_t l; |
5072c59f | 2655 | |
8fc0c701 | 2656 | mutex_lock(&ftrace_lock); |
45a4a237 SR |
2657 | |
2658 | if (unlikely(ftrace_disabled)) | |
2659 | return NULL; | |
2660 | ||
98c4fd04 SR |
2661 | /* |
2662 | * If an lseek was done, then reset and start from beginning. | |
2663 | */ | |
2664 | if (*pos < iter->pos) | |
2665 | reset_iter_read(iter); | |
2666 | ||
0c75a3ed SR |
2667 | /* |
2668 | * For set_ftrace_filter reading, if we have the filter | |
2669 | * off, we can short cut and just print out that all | |
2670 | * functions are enabled. | |
2671 | */ | |
06a51d93 SR |
2672 | if (iter->flags & FTRACE_ITER_FILTER && |
2673 | ftrace_hash_empty(ops->filter_hash)) { | |
0c75a3ed | 2674 | if (*pos > 0) |
8fc0c701 | 2675 | return t_hash_start(m, pos); |
0c75a3ed | 2676 | iter->flags |= FTRACE_ITER_PRINTALL; |
df091625 CW |
2677 | /* reset in case of seek/pread */ |
2678 | iter->flags &= ~FTRACE_ITER_HASH; | |
0c75a3ed SR |
2679 | return iter; |
2680 | } | |
2681 | ||
8fc0c701 SR |
2682 | if (iter->flags & FTRACE_ITER_HASH) |
2683 | return t_hash_start(m, pos); | |
2684 | ||
98c4fd04 SR |
2685 | /* |
2686 | * Unfortunately, we need to restart at ftrace_pages_start | |
2687 | * every time we let go of the ftrace_mutex. This is because | |
2688 | * those pointers can change without the lock. | |
2689 | */ | |
694ce0a5 LZ |
2690 | iter->pg = ftrace_pages_start; |
2691 | iter->idx = 0; | |
2692 | for (l = 0; l <= *pos; ) { | |
2693 | p = t_next(m, p, &l); | |
2694 | if (!p) | |
2695 | break; | |
50cdaf08 | 2696 | } |
5821e1b7 | 2697 | |
69a3083c SR |
2698 | if (!p) |
2699 | return t_hash_start(m, pos); | |
4aeb6967 SR |
2700 | |
2701 | return iter; | |
5072c59f SR |
2702 | } |
2703 | ||
2704 | static void t_stop(struct seq_file *m, void *p) | |
2705 | { | |
8fc0c701 | 2706 | mutex_unlock(&ftrace_lock); |
5072c59f SR |
2707 | } |
2708 | ||
2709 | static int t_show(struct seq_file *m, void *v) | |
2710 | { | |
0c75a3ed | 2711 | struct ftrace_iterator *iter = m->private; |
4aeb6967 | 2712 | struct dyn_ftrace *rec; |
5072c59f | 2713 | |
8fc0c701 | 2714 | if (iter->flags & FTRACE_ITER_HASH) |
4aeb6967 | 2715 | return t_hash_show(m, iter); |
8fc0c701 | 2716 | |
0c75a3ed SR |
2717 | if (iter->flags & FTRACE_ITER_PRINTALL) { |
2718 | seq_printf(m, "#### all functions enabled ####\n"); | |
2719 | return 0; | |
2720 | } | |
2721 | ||
4aeb6967 SR |
2722 | rec = iter->func; |
2723 | ||
5072c59f SR |
2724 | if (!rec) |
2725 | return 0; | |
2726 | ||
647bcd03 SR |
2727 | seq_printf(m, "%ps", (void *)rec->ip); |
2728 | if (iter->flags & FTRACE_ITER_ENABLED) | |
08f6fba5 SR |
2729 | seq_printf(m, " (%ld)%s", |
2730 | rec->flags & ~FTRACE_FL_MASK, | |
2731 | rec->flags & FTRACE_FL_REGS ? " R" : ""); | |
647bcd03 | 2732 | seq_printf(m, "\n"); |
5072c59f SR |
2733 | |
2734 | return 0; | |
2735 | } | |
2736 | ||
88e9d34c | 2737 | static const struct seq_operations show_ftrace_seq_ops = { |
5072c59f SR |
2738 | .start = t_start, |
2739 | .next = t_next, | |
2740 | .stop = t_stop, | |
2741 | .show = t_show, | |
2742 | }; | |
2743 | ||
e309b41d | 2744 | static int |
5072c59f SR |
2745 | ftrace_avail_open(struct inode *inode, struct file *file) |
2746 | { | |
2747 | struct ftrace_iterator *iter; | |
5072c59f | 2748 | |
4eebcc81 SR |
2749 | if (unlikely(ftrace_disabled)) |
2750 | return -ENODEV; | |
2751 | ||
50e18b94 JO |
2752 | iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter)); |
2753 | if (iter) { | |
2754 | iter->pg = ftrace_pages_start; | |
2755 | iter->ops = &global_ops; | |
4bf39a94 | 2756 | } |
5072c59f | 2757 | |
50e18b94 | 2758 | return iter ? 0 : -ENOMEM; |
5072c59f SR |
2759 | } |
2760 | ||
647bcd03 SR |
2761 | static int |
2762 | ftrace_enabled_open(struct inode *inode, struct file *file) | |
2763 | { | |
2764 | struct ftrace_iterator *iter; | |
647bcd03 SR |
2765 | |
2766 | if (unlikely(ftrace_disabled)) | |
2767 | return -ENODEV; | |
2768 | ||
50e18b94 JO |
2769 | iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter)); |
2770 | if (iter) { | |
2771 | iter->pg = ftrace_pages_start; | |
2772 | iter->flags = FTRACE_ITER_ENABLED; | |
2773 | iter->ops = &global_ops; | |
647bcd03 SR |
2774 | } |
2775 | ||
50e18b94 | 2776 | return iter ? 0 : -ENOMEM; |
647bcd03 SR |
2777 | } |
2778 | ||
1cf41dd7 | 2779 | static void ftrace_filter_reset(struct ftrace_hash *hash) |
5072c59f | 2780 | { |
52baf119 | 2781 | mutex_lock(&ftrace_lock); |
1cf41dd7 | 2782 | ftrace_hash_clear(hash); |
52baf119 | 2783 | mutex_unlock(&ftrace_lock); |
5072c59f SR |
2784 | } |
2785 | ||
fc13cb0c SR |
2786 | /** |
2787 | * ftrace_regex_open - initialize function tracer filter files | |
2788 | * @ops: The ftrace_ops that hold the hash filters | |
2789 | * @flag: The type of filter to process | |
2790 | * @inode: The inode, usually passed in to your open routine | |
2791 | * @file: The file, usually passed in to your open routine | |
2792 | * | |
2793 | * ftrace_regex_open() initializes the filter files for the | |
2794 | * @ops. Depending on @flag it may process the filter hash or | |
2795 | * the notrace hash of @ops. With this called from the open | |
2796 | * routine, you can use ftrace_filter_write() for the write | |
2797 | * routine if @flag has FTRACE_ITER_FILTER set, or | |
2798 | * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set. | |
098c879e | 2799 | * tracing_lseek() should be used as the lseek routine, and |
fc13cb0c SR |
2800 | * release must call ftrace_regex_release(). |
2801 | */ | |
2802 | int | |
f45948e8 | 2803 | ftrace_regex_open(struct ftrace_ops *ops, int flag, |
1cf41dd7 | 2804 | struct inode *inode, struct file *file) |
5072c59f SR |
2805 | { |
2806 | struct ftrace_iterator *iter; | |
f45948e8 | 2807 | struct ftrace_hash *hash; |
5072c59f SR |
2808 | int ret = 0; |
2809 | ||
f04f24fb MH |
2810 | ftrace_ops_init(ops); |
2811 | ||
4eebcc81 SR |
2812 | if (unlikely(ftrace_disabled)) |
2813 | return -ENODEV; | |
2814 | ||
5072c59f SR |
2815 | iter = kzalloc(sizeof(*iter), GFP_KERNEL); |
2816 | if (!iter) | |
2817 | return -ENOMEM; | |
2818 | ||
689fd8b6 | 2819 | if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) { |
2820 | kfree(iter); | |
2821 | return -ENOMEM; | |
2822 | } | |
2823 | ||
3f2367ba MH |
2824 | iter->ops = ops; |
2825 | iter->flags = flag; | |
2826 | ||
2827 | mutex_lock(&ops->regex_lock); | |
2828 | ||
f45948e8 SR |
2829 | if (flag & FTRACE_ITER_NOTRACE) |
2830 | hash = ops->notrace_hash; | |
2831 | else | |
2832 | hash = ops->filter_hash; | |
2833 | ||
33dc9b12 | 2834 | if (file->f_mode & FMODE_WRITE) { |
33dc9b12 | 2835 | iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash); |
33dc9b12 SR |
2836 | if (!iter->hash) { |
2837 | trace_parser_put(&iter->parser); | |
2838 | kfree(iter); | |
3f2367ba MH |
2839 | ret = -ENOMEM; |
2840 | goto out_unlock; | |
33dc9b12 SR |
2841 | } |
2842 | } | |
1cf41dd7 | 2843 | |
5072c59f | 2844 | if ((file->f_mode & FMODE_WRITE) && |
8650ae32 | 2845 | (file->f_flags & O_TRUNC)) |
33dc9b12 | 2846 | ftrace_filter_reset(iter->hash); |
5072c59f SR |
2847 | |
2848 | if (file->f_mode & FMODE_READ) { | |
2849 | iter->pg = ftrace_pages_start; | |
5072c59f SR |
2850 | |
2851 | ret = seq_open(file, &show_ftrace_seq_ops); | |
2852 | if (!ret) { | |
2853 | struct seq_file *m = file->private_data; | |
2854 | m->private = iter; | |
79fe249c | 2855 | } else { |
33dc9b12 SR |
2856 | /* Failed */ |
2857 | free_ftrace_hash(iter->hash); | |
79fe249c | 2858 | trace_parser_put(&iter->parser); |
5072c59f | 2859 | kfree(iter); |
79fe249c | 2860 | } |
5072c59f SR |
2861 | } else |
2862 | file->private_data = iter; | |
3f2367ba MH |
2863 | |
2864 | out_unlock: | |
f04f24fb | 2865 | mutex_unlock(&ops->regex_lock); |
5072c59f SR |
2866 | |
2867 | return ret; | |
2868 | } | |
2869 | ||
41c52c0d SR |
2870 | static int |
2871 | ftrace_filter_open(struct inode *inode, struct file *file) | |
2872 | { | |
e3b3e2e8 SRRH |
2873 | struct ftrace_ops *ops = inode->i_private; |
2874 | ||
2875 | return ftrace_regex_open(ops, | |
69a3083c SR |
2876 | FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH, |
2877 | inode, file); | |
41c52c0d SR |
2878 | } |
2879 | ||
2880 | static int | |
2881 | ftrace_notrace_open(struct inode *inode, struct file *file) | |
2882 | { | |
e3b3e2e8 SRRH |
2883 | struct ftrace_ops *ops = inode->i_private; |
2884 | ||
2885 | return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE, | |
1cf41dd7 | 2886 | inode, file); |
41c52c0d SR |
2887 | } |
2888 | ||
64e7c440 | 2889 | static int ftrace_match(char *str, char *regex, int len, int type) |
9f4801e3 | 2890 | { |
9f4801e3 | 2891 | int matched = 0; |
751e9983 | 2892 | int slen; |
9f4801e3 | 2893 | |
9f4801e3 SR |
2894 | switch (type) { |
2895 | case MATCH_FULL: | |
2896 | if (strcmp(str, regex) == 0) | |
2897 | matched = 1; | |
2898 | break; | |
2899 | case MATCH_FRONT_ONLY: | |
2900 | if (strncmp(str, regex, len) == 0) | |
2901 | matched = 1; | |
2902 | break; | |
2903 | case MATCH_MIDDLE_ONLY: | |
2904 | if (strstr(str, regex)) | |
2905 | matched = 1; | |
2906 | break; | |
2907 | case MATCH_END_ONLY: | |
751e9983 LZ |
2908 | slen = strlen(str); |
2909 | if (slen >= len && memcmp(str + slen - len, regex, len) == 0) | |
9f4801e3 SR |
2910 | matched = 1; |
2911 | break; | |
2912 | } | |
2913 | ||
2914 | return matched; | |
2915 | } | |
2916 | ||
b448c4e3 | 2917 | static int |
1cf41dd7 | 2918 | enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not) |
996e87be | 2919 | { |
b448c4e3 | 2920 | struct ftrace_func_entry *entry; |
b448c4e3 SR |
2921 | int ret = 0; |
2922 | ||
1cf41dd7 SR |
2923 | entry = ftrace_lookup_ip(hash, rec->ip); |
2924 | if (not) { | |
2925 | /* Do nothing if it doesn't exist */ | |
2926 | if (!entry) | |
2927 | return 0; | |
b448c4e3 | 2928 | |
33dc9b12 | 2929 | free_hash_entry(hash, entry); |
1cf41dd7 SR |
2930 | } else { |
2931 | /* Do nothing if it exists */ | |
2932 | if (entry) | |
2933 | return 0; | |
b448c4e3 | 2934 | |
1cf41dd7 | 2935 | ret = add_hash_entry(hash, rec->ip); |
b448c4e3 SR |
2936 | } |
2937 | return ret; | |
996e87be SR |
2938 | } |
2939 | ||
64e7c440 | 2940 | static int |
b9df92d2 SR |
2941 | ftrace_match_record(struct dyn_ftrace *rec, char *mod, |
2942 | char *regex, int len, int type) | |
64e7c440 SR |
2943 | { |
2944 | char str[KSYM_SYMBOL_LEN]; | |
b9df92d2 SR |
2945 | char *modname; |
2946 | ||
2947 | kallsyms_lookup(rec->ip, NULL, NULL, &modname, str); | |
2948 | ||
2949 | if (mod) { | |
2950 | /* module lookup requires matching the module */ | |
2951 | if (!modname || strcmp(modname, mod)) | |
2952 | return 0; | |
2953 | ||
2954 | /* blank search means to match all funcs in the mod */ | |
2955 | if (!len) | |
2956 | return 1; | |
2957 | } | |
64e7c440 | 2958 | |
64e7c440 SR |
2959 | return ftrace_match(str, regex, len, type); |
2960 | } | |
2961 | ||
1cf41dd7 SR |
2962 | static int |
2963 | match_records(struct ftrace_hash *hash, char *buff, | |
2964 | int len, char *mod, int not) | |
9f4801e3 | 2965 | { |
b9df92d2 | 2966 | unsigned search_len = 0; |
9f4801e3 SR |
2967 | struct ftrace_page *pg; |
2968 | struct dyn_ftrace *rec; | |
b9df92d2 SR |
2969 | int type = MATCH_FULL; |
2970 | char *search = buff; | |
311d16da | 2971 | int found = 0; |
b448c4e3 | 2972 | int ret; |
9f4801e3 | 2973 | |
b9df92d2 SR |
2974 | if (len) { |
2975 | type = filter_parse_regex(buff, len, &search, ¬); | |
2976 | search_len = strlen(search); | |
2977 | } | |
9f4801e3 | 2978 | |
52baf119 | 2979 | mutex_lock(&ftrace_lock); |
265c831c | 2980 | |
b9df92d2 SR |
2981 | if (unlikely(ftrace_disabled)) |
2982 | goto out_unlock; | |
9f4801e3 | 2983 | |
265c831c | 2984 | do_for_each_ftrace_rec(pg, rec) { |
b9df92d2 | 2985 | if (ftrace_match_record(rec, mod, search, search_len, type)) { |
1cf41dd7 | 2986 | ret = enter_record(hash, rec, not); |
b448c4e3 SR |
2987 | if (ret < 0) { |
2988 | found = ret; | |
2989 | goto out_unlock; | |
2990 | } | |
311d16da | 2991 | found = 1; |
265c831c SR |
2992 | } |
2993 | } while_for_each_ftrace_rec(); | |
b9df92d2 | 2994 | out_unlock: |
52baf119 | 2995 | mutex_unlock(&ftrace_lock); |
311d16da LZ |
2996 | |
2997 | return found; | |
5072c59f SR |
2998 | } |
2999 | ||
64e7c440 | 3000 | static int |
1cf41dd7 | 3001 | ftrace_match_records(struct ftrace_hash *hash, char *buff, int len) |
64e7c440 | 3002 | { |
1cf41dd7 | 3003 | return match_records(hash, buff, len, NULL, 0); |
64e7c440 SR |
3004 | } |
3005 | ||
1cf41dd7 SR |
3006 | static int |
3007 | ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod) | |
64e7c440 | 3008 | { |
64e7c440 | 3009 | int not = 0; |
6a24a244 | 3010 | |
64e7c440 SR |
3011 | /* blank or '*' mean the same */ |
3012 | if (strcmp(buff, "*") == 0) | |
3013 | buff[0] = 0; | |
3014 | ||
3015 | /* handle the case of 'dont filter this module' */ | |
3016 | if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) { | |
3017 | buff[0] = 0; | |
3018 | not = 1; | |
3019 | } | |
3020 | ||
1cf41dd7 | 3021 | return match_records(hash, buff, strlen(buff), mod, not); |
64e7c440 SR |
3022 | } |
3023 | ||
f6180773 SR |
3024 | /* |
3025 | * We register the module command as a template to show others how | |
3026 | * to register the a command as well. | |
3027 | */ | |
3028 | ||
3029 | static int | |
43dd61c9 SR |
3030 | ftrace_mod_callback(struct ftrace_hash *hash, |
3031 | char *func, char *cmd, char *param, int enable) | |
f6180773 SR |
3032 | { |
3033 | char *mod; | |
b448c4e3 | 3034 | int ret = -EINVAL; |
f6180773 SR |
3035 | |
3036 | /* | |
3037 | * cmd == 'mod' because we only registered this func | |
3038 | * for the 'mod' ftrace_func_command. | |
3039 | * But if you register one func with multiple commands, | |
3040 | * you can tell which command was used by the cmd | |
3041 | * parameter. | |
3042 | */ | |
3043 | ||
3044 | /* we must have a module name */ | |
3045 | if (!param) | |
b448c4e3 | 3046 | return ret; |
f6180773 SR |
3047 | |
3048 | mod = strsep(¶m, ":"); | |
3049 | if (!strlen(mod)) | |
b448c4e3 | 3050 | return ret; |
f6180773 | 3051 | |
1cf41dd7 | 3052 | ret = ftrace_match_module_records(hash, func, mod); |
b448c4e3 SR |
3053 | if (!ret) |
3054 | ret = -EINVAL; | |
3055 | if (ret < 0) | |
3056 | return ret; | |
3057 | ||
3058 | return 0; | |
f6180773 SR |
3059 | } |
3060 | ||
3061 | static struct ftrace_func_command ftrace_mod_cmd = { | |
3062 | .name = "mod", | |
3063 | .func = ftrace_mod_callback, | |
3064 | }; | |
3065 | ||
3066 | static int __init ftrace_mod_cmd_init(void) | |
3067 | { | |
3068 | return register_ftrace_command(&ftrace_mod_cmd); | |
3069 | } | |
6f415672 | 3070 | core_initcall(ftrace_mod_cmd_init); |
f6180773 | 3071 | |
2f5f6ad9 | 3072 | static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip, |
a1e2e31d | 3073 | struct ftrace_ops *op, struct pt_regs *pt_regs) |
59df055f | 3074 | { |
b6887d79 | 3075 | struct ftrace_func_probe *entry; |
59df055f | 3076 | struct hlist_head *hhd; |
59df055f | 3077 | unsigned long key; |
59df055f SR |
3078 | |
3079 | key = hash_long(ip, FTRACE_HASH_BITS); | |
3080 | ||
3081 | hhd = &ftrace_func_hash[key]; | |
3082 | ||
3083 | if (hlist_empty(hhd)) | |
3084 | return; | |
3085 | ||
3086 | /* | |
3087 | * Disable preemption for these calls to prevent a RCU grace | |
3088 | * period. This syncs the hash iteration and freeing of items | |
3089 | * on the hash. rcu_read_lock is too dangerous here. | |
3090 | */ | |
5168ae50 | 3091 | preempt_disable_notrace(); |
1bb539ca | 3092 | hlist_for_each_entry_rcu_notrace(entry, hhd, node) { |
59df055f SR |
3093 | if (entry->ip == ip) |
3094 | entry->ops->func(ip, parent_ip, &entry->data); | |
3095 | } | |
5168ae50 | 3096 | preempt_enable_notrace(); |
59df055f SR |
3097 | } |
3098 | ||
b6887d79 | 3099 | static struct ftrace_ops trace_probe_ops __read_mostly = |
59df055f | 3100 | { |
fb9fb015 | 3101 | .func = function_trace_probe_call, |
f04f24fb MH |
3102 | .flags = FTRACE_OPS_FL_INITIALIZED, |
3103 | INIT_REGEX_LOCK(trace_probe_ops) | |
59df055f SR |
3104 | }; |
3105 | ||
b6887d79 | 3106 | static int ftrace_probe_registered; |
59df055f | 3107 | |
b6887d79 | 3108 | static void __enable_ftrace_function_probe(void) |
59df055f | 3109 | { |
b848914c | 3110 | int ret; |
59df055f SR |
3111 | int i; |
3112 | ||
19dd603e SRRH |
3113 | if (ftrace_probe_registered) { |
3114 | /* still need to update the function call sites */ | |
3115 | if (ftrace_enabled) | |
3116 | ftrace_run_update_code(FTRACE_UPDATE_CALLS); | |
59df055f | 3117 | return; |
19dd603e | 3118 | } |
59df055f SR |
3119 | |
3120 | for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) { | |
3121 | struct hlist_head *hhd = &ftrace_func_hash[i]; | |
3122 | if (hhd->first) | |
3123 | break; | |
3124 | } | |
3125 | /* Nothing registered? */ | |
3126 | if (i == FTRACE_FUNC_HASHSIZE) | |
3127 | return; | |
3128 | ||
8a56d776 | 3129 | ret = ftrace_startup(&trace_probe_ops, 0); |
b848914c | 3130 | |
b6887d79 | 3131 | ftrace_probe_registered = 1; |
59df055f SR |
3132 | } |
3133 | ||
b6887d79 | 3134 | static void __disable_ftrace_function_probe(void) |
59df055f SR |
3135 | { |
3136 | int i; | |
3137 | ||
b6887d79 | 3138 | if (!ftrace_probe_registered) |
59df055f SR |
3139 | return; |
3140 | ||
3141 | for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) { | |
3142 | struct hlist_head *hhd = &ftrace_func_hash[i]; | |
3143 | if (hhd->first) | |
3144 | return; | |
3145 | } | |
3146 | ||
3147 | /* no more funcs left */ | |
8a56d776 | 3148 | ftrace_shutdown(&trace_probe_ops, 0); |
b848914c | 3149 | |
b6887d79 | 3150 | ftrace_probe_registered = 0; |
59df055f SR |
3151 | } |
3152 | ||
3153 | ||
7818b388 | 3154 | static void ftrace_free_entry(struct ftrace_func_probe *entry) |
59df055f | 3155 | { |
59df055f | 3156 | if (entry->ops->free) |
e67efb93 | 3157 | entry->ops->free(entry->ops, entry->ip, &entry->data); |
59df055f SR |
3158 | kfree(entry); |
3159 | } | |
3160 | ||
59df055f | 3161 | int |
b6887d79 | 3162 | register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops, |
59df055f SR |
3163 | void *data) |
3164 | { | |
b6887d79 | 3165 | struct ftrace_func_probe *entry; |
e1df4cb6 SRRH |
3166 | struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash; |
3167 | struct ftrace_hash *hash; | |
59df055f SR |
3168 | struct ftrace_page *pg; |
3169 | struct dyn_ftrace *rec; | |
59df055f | 3170 | int type, len, not; |
6a24a244 | 3171 | unsigned long key; |
59df055f SR |
3172 | int count = 0; |
3173 | char *search; | |
e1df4cb6 | 3174 | int ret; |
59df055f | 3175 | |
3f6fe06d | 3176 | type = filter_parse_regex(glob, strlen(glob), &search, ¬); |
59df055f SR |
3177 | len = strlen(search); |
3178 | ||
b6887d79 | 3179 | /* we do not support '!' for function probes */ |
59df055f SR |
3180 | if (WARN_ON(not)) |
3181 | return -EINVAL; | |
3182 | ||
3f2367ba | 3183 | mutex_lock(&trace_probe_ops.regex_lock); |
59df055f | 3184 | |
e1df4cb6 SRRH |
3185 | hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash); |
3186 | if (!hash) { | |
3187 | count = -ENOMEM; | |
5ae0bf59 | 3188 | goto out; |
e1df4cb6 SRRH |
3189 | } |
3190 | ||
3191 | if (unlikely(ftrace_disabled)) { | |
3192 | count = -ENODEV; | |
5ae0bf59 | 3193 | goto out; |
e1df4cb6 | 3194 | } |
59df055f | 3195 | |
5ae0bf59 SRRH |
3196 | mutex_lock(&ftrace_lock); |
3197 | ||
45a4a237 | 3198 | do_for_each_ftrace_rec(pg, rec) { |
59df055f | 3199 | |
b9df92d2 | 3200 | if (!ftrace_match_record(rec, NULL, search, len, type)) |
59df055f SR |
3201 | continue; |
3202 | ||
3203 | entry = kmalloc(sizeof(*entry), GFP_KERNEL); | |
3204 | if (!entry) { | |
b6887d79 | 3205 | /* If we did not process any, then return error */ |
59df055f SR |
3206 | if (!count) |
3207 | count = -ENOMEM; | |
3208 | goto out_unlock; | |
3209 | } | |
3210 | ||
3211 | count++; | |
3212 | ||
3213 | entry->data = data; | |
3214 | ||
3215 | /* | |
3216 | * The caller might want to do something special | |
3217 | * for each function we find. We call the callback | |
3218 | * to give the caller an opportunity to do so. | |
3219 | */ | |
e67efb93 SRRH |
3220 | if (ops->init) { |
3221 | if (ops->init(ops, rec->ip, &entry->data) < 0) { | |
59df055f SR |
3222 | /* caller does not like this func */ |
3223 | kfree(entry); | |
3224 | continue; | |
3225 | } | |
3226 | } | |
3227 | ||
e1df4cb6 SRRH |
3228 | ret = enter_record(hash, rec, 0); |
3229 | if (ret < 0) { | |
3230 | kfree(entry); | |
3231 | count = ret; | |
3232 | goto out_unlock; | |
3233 | } | |
3234 | ||
59df055f SR |
3235 | entry->ops = ops; |
3236 | entry->ip = rec->ip; | |
3237 | ||
3238 | key = hash_long(entry->ip, FTRACE_HASH_BITS); | |
3239 | hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]); | |
3240 | ||
3241 | } while_for_each_ftrace_rec(); | |
e1df4cb6 SRRH |
3242 | |
3243 | ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash); | |
3244 | if (ret < 0) | |
3245 | count = ret; | |
3246 | ||
b6887d79 | 3247 | __enable_ftrace_function_probe(); |
59df055f SR |
3248 | |
3249 | out_unlock: | |
5ae0bf59 SRRH |
3250 | mutex_unlock(&ftrace_lock); |
3251 | out: | |
3f2367ba | 3252 | mutex_unlock(&trace_probe_ops.regex_lock); |
e1df4cb6 | 3253 | free_ftrace_hash(hash); |
59df055f SR |
3254 | |
3255 | return count; | |
3256 | } | |
3257 | ||
3258 | enum { | |
b6887d79 SR |
3259 | PROBE_TEST_FUNC = 1, |
3260 | PROBE_TEST_DATA = 2 | |
59df055f SR |
3261 | }; |
3262 | ||
3263 | static void | |
b6887d79 | 3264 | __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops, |
59df055f SR |
3265 | void *data, int flags) |
3266 | { | |
e1df4cb6 | 3267 | struct ftrace_func_entry *rec_entry; |
b6887d79 | 3268 | struct ftrace_func_probe *entry; |
7818b388 | 3269 | struct ftrace_func_probe *p; |
e1df4cb6 | 3270 | struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash; |
7818b388 | 3271 | struct list_head free_list; |
e1df4cb6 | 3272 | struct ftrace_hash *hash; |
b67bfe0d | 3273 | struct hlist_node *tmp; |
59df055f SR |
3274 | char str[KSYM_SYMBOL_LEN]; |
3275 | int type = MATCH_FULL; | |
3276 | int i, len = 0; | |
3277 | char *search; | |
3278 | ||
b36461da | 3279 | if (glob && (strcmp(glob, "*") == 0 || !strlen(glob))) |
59df055f | 3280 | glob = NULL; |
b36461da | 3281 | else if (glob) { |
59df055f SR |
3282 | int not; |
3283 | ||
3f6fe06d | 3284 | type = filter_parse_regex(glob, strlen(glob), &search, ¬); |
59df055f SR |
3285 | len = strlen(search); |
3286 | ||
b6887d79 | 3287 | /* we do not support '!' for function probes */ |
59df055f SR |
3288 | if (WARN_ON(not)) |
3289 | return; | |
3290 | } | |
3291 | ||
3f2367ba | 3292 | mutex_lock(&trace_probe_ops.regex_lock); |
e1df4cb6 SRRH |
3293 | |
3294 | hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash); | |
3295 | if (!hash) | |
3296 | /* Hmm, should report this somehow */ | |
3297 | goto out_unlock; | |
3298 | ||
7818b388 SRRH |
3299 | INIT_LIST_HEAD(&free_list); |
3300 | ||
59df055f SR |
3301 | for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) { |
3302 | struct hlist_head *hhd = &ftrace_func_hash[i]; | |
3303 | ||
b67bfe0d | 3304 | hlist_for_each_entry_safe(entry, tmp, hhd, node) { |
59df055f SR |
3305 | |
3306 | /* break up if statements for readability */ | |
b6887d79 | 3307 | if ((flags & PROBE_TEST_FUNC) && entry->ops != ops) |
59df055f SR |
3308 | continue; |
3309 | ||
b6887d79 | 3310 | if ((flags & PROBE_TEST_DATA) && entry->data != data) |
59df055f SR |
3311 | continue; |
3312 | ||
3313 | /* do this last, since it is the most expensive */ | |
3314 | if (glob) { | |
3315 | kallsyms_lookup(entry->ip, NULL, NULL, | |
3316 | NULL, str); | |
3317 | if (!ftrace_match(str, glob, len, type)) | |
3318 | continue; | |
3319 | } | |
3320 | ||
e1df4cb6 SRRH |
3321 | rec_entry = ftrace_lookup_ip(hash, entry->ip); |
3322 | /* It is possible more than one entry had this ip */ | |
3323 | if (rec_entry) | |
3324 | free_hash_entry(hash, rec_entry); | |
3325 | ||
740466bc | 3326 | hlist_del_rcu(&entry->node); |
7818b388 | 3327 | list_add(&entry->free_list, &free_list); |
59df055f SR |
3328 | } |
3329 | } | |
3f2367ba | 3330 | mutex_lock(&ftrace_lock); |
b6887d79 | 3331 | __disable_ftrace_function_probe(); |
e1df4cb6 SRRH |
3332 | /* |
3333 | * Remove after the disable is called. Otherwise, if the last | |
3334 | * probe is removed, a null hash means *all enabled*. | |
3335 | */ | |
3336 | ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash); | |
7818b388 SRRH |
3337 | synchronize_sched(); |
3338 | list_for_each_entry_safe(entry, p, &free_list, free_list) { | |
3339 | list_del(&entry->free_list); | |
3340 | ftrace_free_entry(entry); | |
3341 | } | |
3f2367ba | 3342 | mutex_unlock(&ftrace_lock); |
7818b388 | 3343 | |
e1df4cb6 | 3344 | out_unlock: |
3f2367ba | 3345 | mutex_unlock(&trace_probe_ops.regex_lock); |
e1df4cb6 | 3346 | free_ftrace_hash(hash); |
59df055f SR |
3347 | } |
3348 | ||
3349 | void | |
b6887d79 | 3350 | unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops, |
59df055f SR |
3351 | void *data) |
3352 | { | |
b6887d79 SR |
3353 | __unregister_ftrace_function_probe(glob, ops, data, |
3354 | PROBE_TEST_FUNC | PROBE_TEST_DATA); | |
59df055f SR |
3355 | } |
3356 | ||
3357 | void | |
b6887d79 | 3358 | unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops) |
59df055f | 3359 | { |
b6887d79 | 3360 | __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC); |
59df055f SR |
3361 | } |
3362 | ||
b6887d79 | 3363 | void unregister_ftrace_function_probe_all(char *glob) |
59df055f | 3364 | { |
b6887d79 | 3365 | __unregister_ftrace_function_probe(glob, NULL, NULL, 0); |
59df055f SR |
3366 | } |
3367 | ||
f6180773 SR |
3368 | static LIST_HEAD(ftrace_commands); |
3369 | static DEFINE_MUTEX(ftrace_cmd_mutex); | |
3370 | ||
38de93ab TZ |
3371 | /* |
3372 | * Currently we only register ftrace commands from __init, so mark this | |
3373 | * __init too. | |
3374 | */ | |
3375 | __init int register_ftrace_command(struct ftrace_func_command *cmd) | |
f6180773 SR |
3376 | { |
3377 | struct ftrace_func_command *p; | |
3378 | int ret = 0; | |
3379 | ||
3380 | mutex_lock(&ftrace_cmd_mutex); | |
3381 | list_for_each_entry(p, &ftrace_commands, list) { | |
3382 | if (strcmp(cmd->name, p->name) == 0) { | |
3383 | ret = -EBUSY; | |
3384 | goto out_unlock; | |
3385 | } | |
3386 | } | |
3387 | list_add(&cmd->list, &ftrace_commands); | |
3388 | out_unlock: | |
3389 | mutex_unlock(&ftrace_cmd_mutex); | |
3390 | ||
3391 | return ret; | |
3392 | } | |
3393 | ||
38de93ab TZ |
3394 | /* |
3395 | * Currently we only unregister ftrace commands from __init, so mark | |
3396 | * this __init too. | |
3397 | */ | |
3398 | __init int unregister_ftrace_command(struct ftrace_func_command *cmd) | |
f6180773 SR |
3399 | { |
3400 | struct ftrace_func_command *p, *n; | |
3401 | int ret = -ENODEV; | |
3402 | ||
3403 | mutex_lock(&ftrace_cmd_mutex); | |
3404 | list_for_each_entry_safe(p, n, &ftrace_commands, list) { | |
3405 | if (strcmp(cmd->name, p->name) == 0) { | |
3406 | ret = 0; | |
3407 | list_del_init(&p->list); | |
3408 | goto out_unlock; | |
3409 | } | |
3410 | } | |
3411 | out_unlock: | |
3412 | mutex_unlock(&ftrace_cmd_mutex); | |
3413 | ||
3414 | return ret; | |
3415 | } | |
3416 | ||
33dc9b12 SR |
3417 | static int ftrace_process_regex(struct ftrace_hash *hash, |
3418 | char *buff, int len, int enable) | |
64e7c440 | 3419 | { |
f6180773 | 3420 | char *func, *command, *next = buff; |
6a24a244 | 3421 | struct ftrace_func_command *p; |
0aff1c0c | 3422 | int ret = -EINVAL; |
64e7c440 SR |
3423 | |
3424 | func = strsep(&next, ":"); | |
3425 | ||
3426 | if (!next) { | |
1cf41dd7 | 3427 | ret = ftrace_match_records(hash, func, len); |
b448c4e3 SR |
3428 | if (!ret) |
3429 | ret = -EINVAL; | |
3430 | if (ret < 0) | |
3431 | return ret; | |
3432 | return 0; | |
64e7c440 SR |
3433 | } |
3434 | ||
f6180773 | 3435 | /* command found */ |
64e7c440 SR |
3436 | |
3437 | command = strsep(&next, ":"); | |
3438 | ||
f6180773 SR |
3439 | mutex_lock(&ftrace_cmd_mutex); |
3440 | list_for_each_entry(p, &ftrace_commands, list) { | |
3441 | if (strcmp(p->name, command) == 0) { | |
43dd61c9 | 3442 | ret = p->func(hash, func, command, next, enable); |
f6180773 SR |
3443 | goto out_unlock; |
3444 | } | |
64e7c440 | 3445 | } |
f6180773 SR |
3446 | out_unlock: |
3447 | mutex_unlock(&ftrace_cmd_mutex); | |
64e7c440 | 3448 | |
f6180773 | 3449 | return ret; |
64e7c440 SR |
3450 | } |
3451 | ||
e309b41d | 3452 | static ssize_t |
41c52c0d SR |
3453 | ftrace_regex_write(struct file *file, const char __user *ubuf, |
3454 | size_t cnt, loff_t *ppos, int enable) | |
5072c59f SR |
3455 | { |
3456 | struct ftrace_iterator *iter; | |
689fd8b6 | 3457 | struct trace_parser *parser; |
3458 | ssize_t ret, read; | |
5072c59f | 3459 | |
4ba7978e | 3460 | if (!cnt) |
5072c59f SR |
3461 | return 0; |
3462 | ||
5072c59f SR |
3463 | if (file->f_mode & FMODE_READ) { |
3464 | struct seq_file *m = file->private_data; | |
3465 | iter = m->private; | |
3466 | } else | |
3467 | iter = file->private_data; | |
3468 | ||
f04f24fb | 3469 | if (unlikely(ftrace_disabled)) |
3f2367ba MH |
3470 | return -ENODEV; |
3471 | ||
3472 | /* iter->hash is a local copy, so we don't need regex_lock */ | |
f04f24fb | 3473 | |
689fd8b6 | 3474 | parser = &iter->parser; |
3475 | read = trace_get_user(parser, ubuf, cnt, ppos); | |
5072c59f | 3476 | |
4ba7978e | 3477 | if (read >= 0 && trace_parser_loaded(parser) && |
689fd8b6 | 3478 | !trace_parser_cont(parser)) { |
33dc9b12 | 3479 | ret = ftrace_process_regex(iter->hash, parser->buffer, |
689fd8b6 | 3480 | parser->idx, enable); |
313254a9 | 3481 | trace_parser_clear(parser); |
7c088b51 | 3482 | if (ret < 0) |
3f2367ba | 3483 | goto out; |
eda1e328 | 3484 | } |
5072c59f | 3485 | |
5072c59f | 3486 | ret = read; |
3f2367ba | 3487 | out: |
5072c59f SR |
3488 | return ret; |
3489 | } | |
3490 | ||
fc13cb0c | 3491 | ssize_t |
41c52c0d SR |
3492 | ftrace_filter_write(struct file *file, const char __user *ubuf, |
3493 | size_t cnt, loff_t *ppos) | |
3494 | { | |
3495 | return ftrace_regex_write(file, ubuf, cnt, ppos, 1); | |
3496 | } | |
3497 | ||
fc13cb0c | 3498 | ssize_t |
41c52c0d SR |
3499 | ftrace_notrace_write(struct file *file, const char __user *ubuf, |
3500 | size_t cnt, loff_t *ppos) | |
3501 | { | |
3502 | return ftrace_regex_write(file, ubuf, cnt, ppos, 0); | |
3503 | } | |
3504 | ||
33dc9b12 | 3505 | static int |
647664ea MH |
3506 | ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove) |
3507 | { | |
3508 | struct ftrace_func_entry *entry; | |
3509 | ||
3510 | if (!ftrace_location(ip)) | |
3511 | return -EINVAL; | |
3512 | ||
3513 | if (remove) { | |
3514 | entry = ftrace_lookup_ip(hash, ip); | |
3515 | if (!entry) | |
3516 | return -ENOENT; | |
3517 | free_hash_entry(hash, entry); | |
3518 | return 0; | |
3519 | } | |
3520 | ||
3521 | return add_hash_entry(hash, ip); | |
3522 | } | |
3523 | ||
1c80c432 SRRH |
3524 | static void ftrace_ops_update_code(struct ftrace_ops *ops) |
3525 | { | |
3526 | if (ops->flags & FTRACE_OPS_FL_ENABLED && ftrace_enabled) | |
3527 | ftrace_run_update_code(FTRACE_UPDATE_CALLS); | |
3528 | } | |
3529 | ||
647664ea MH |
3530 | static int |
3531 | ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len, | |
3532 | unsigned long ip, int remove, int reset, int enable) | |
41c52c0d | 3533 | { |
33dc9b12 | 3534 | struct ftrace_hash **orig_hash; |
f45948e8 | 3535 | struct ftrace_hash *hash; |
33dc9b12 | 3536 | int ret; |
f45948e8 | 3537 | |
936e074b SR |
3538 | /* All global ops uses the global ops filters */ |
3539 | if (ops->flags & FTRACE_OPS_FL_GLOBAL) | |
3540 | ops = &global_ops; | |
3541 | ||
41c52c0d | 3542 | if (unlikely(ftrace_disabled)) |
33dc9b12 | 3543 | return -ENODEV; |
41c52c0d | 3544 | |
3f2367ba MH |
3545 | mutex_lock(&ops->regex_lock); |
3546 | ||
f45948e8 | 3547 | if (enable) |
33dc9b12 | 3548 | orig_hash = &ops->filter_hash; |
f45948e8 | 3549 | else |
33dc9b12 SR |
3550 | orig_hash = &ops->notrace_hash; |
3551 | ||
3552 | hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash); | |
3f2367ba MH |
3553 | if (!hash) { |
3554 | ret = -ENOMEM; | |
3555 | goto out_regex_unlock; | |
3556 | } | |
f45948e8 | 3557 | |
41c52c0d | 3558 | if (reset) |
1cf41dd7 | 3559 | ftrace_filter_reset(hash); |
ac483c44 JO |
3560 | if (buf && !ftrace_match_records(hash, buf, len)) { |
3561 | ret = -EINVAL; | |
3562 | goto out_regex_unlock; | |
3563 | } | |
647664ea MH |
3564 | if (ip) { |
3565 | ret = ftrace_match_addr(hash, ip, remove); | |
3566 | if (ret < 0) | |
3567 | goto out_regex_unlock; | |
3568 | } | |
33dc9b12 SR |
3569 | |
3570 | mutex_lock(&ftrace_lock); | |
41fb61c2 | 3571 | ret = ftrace_hash_move(ops, enable, orig_hash, hash); |
1c80c432 SRRH |
3572 | if (!ret) |
3573 | ftrace_ops_update_code(ops); | |
072126f4 | 3574 | |
33dc9b12 SR |
3575 | mutex_unlock(&ftrace_lock); |
3576 | ||
ac483c44 | 3577 | out_regex_unlock: |
f04f24fb | 3578 | mutex_unlock(&ops->regex_lock); |
33dc9b12 SR |
3579 | |
3580 | free_ftrace_hash(hash); | |
3581 | return ret; | |
41c52c0d SR |
3582 | } |
3583 | ||
647664ea MH |
3584 | static int |
3585 | ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove, | |
3586 | int reset, int enable) | |
3587 | { | |
3588 | return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable); | |
3589 | } | |
3590 | ||
3591 | /** | |
3592 | * ftrace_set_filter_ip - set a function to filter on in ftrace by address | |
3593 | * @ops - the ops to set the filter with | |
3594 | * @ip - the address to add to or remove from the filter. | |
3595 | * @remove - non zero to remove the ip from the filter | |
3596 | * @reset - non zero to reset all filters before applying this filter. | |
3597 | * | |
3598 | * Filters denote which functions should be enabled when tracing is enabled | |
3599 | * If @ip is NULL, it failes to update filter. | |
3600 | */ | |
3601 | int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip, | |
3602 | int remove, int reset) | |
3603 | { | |
f04f24fb | 3604 | ftrace_ops_init(ops); |
647664ea MH |
3605 | return ftrace_set_addr(ops, ip, remove, reset, 1); |
3606 | } | |
3607 | EXPORT_SYMBOL_GPL(ftrace_set_filter_ip); | |
3608 | ||
3609 | static int | |
3610 | ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len, | |
3611 | int reset, int enable) | |
3612 | { | |
3613 | return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable); | |
3614 | } | |
3615 | ||
77a2b37d SR |
3616 | /** |
3617 | * ftrace_set_filter - set a function to filter on in ftrace | |
936e074b SR |
3618 | * @ops - the ops to set the filter with |
3619 | * @buf - the string that holds the function filter text. | |
3620 | * @len - the length of the string. | |
3621 | * @reset - non zero to reset all filters before applying this filter. | |
3622 | * | |
3623 | * Filters denote which functions should be enabled when tracing is enabled. | |
3624 | * If @buf is NULL and reset is set, all functions will be enabled for tracing. | |
3625 | */ | |
ac483c44 | 3626 | int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf, |
936e074b SR |
3627 | int len, int reset) |
3628 | { | |
f04f24fb | 3629 | ftrace_ops_init(ops); |
ac483c44 | 3630 | return ftrace_set_regex(ops, buf, len, reset, 1); |
936e074b SR |
3631 | } |
3632 | EXPORT_SYMBOL_GPL(ftrace_set_filter); | |
3633 | ||
3634 | /** | |
3635 | * ftrace_set_notrace - set a function to not trace in ftrace | |
3636 | * @ops - the ops to set the notrace filter with | |
3637 | * @buf - the string that holds the function notrace text. | |
3638 | * @len - the length of the string. | |
3639 | * @reset - non zero to reset all filters before applying this filter. | |
3640 | * | |
3641 | * Notrace Filters denote which functions should not be enabled when tracing | |
3642 | * is enabled. If @buf is NULL and reset is set, all functions will be enabled | |
3643 | * for tracing. | |
3644 | */ | |
ac483c44 | 3645 | int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf, |
936e074b SR |
3646 | int len, int reset) |
3647 | { | |
f04f24fb | 3648 | ftrace_ops_init(ops); |
ac483c44 | 3649 | return ftrace_set_regex(ops, buf, len, reset, 0); |
936e074b SR |
3650 | } |
3651 | EXPORT_SYMBOL_GPL(ftrace_set_notrace); | |
3652 | /** | |
3653 | * ftrace_set_filter - set a function to filter on in ftrace | |
3654 | * @ops - the ops to set the filter with | |
77a2b37d SR |
3655 | * @buf - the string that holds the function filter text. |
3656 | * @len - the length of the string. | |
3657 | * @reset - non zero to reset all filters before applying this filter. | |
3658 | * | |
3659 | * Filters denote which functions should be enabled when tracing is enabled. | |
3660 | * If @buf is NULL and reset is set, all functions will be enabled for tracing. | |
3661 | */ | |
936e074b | 3662 | void ftrace_set_global_filter(unsigned char *buf, int len, int reset) |
77a2b37d | 3663 | { |
f45948e8 | 3664 | ftrace_set_regex(&global_ops, buf, len, reset, 1); |
41c52c0d | 3665 | } |
936e074b | 3666 | EXPORT_SYMBOL_GPL(ftrace_set_global_filter); |
4eebcc81 | 3667 | |
41c52c0d SR |
3668 | /** |
3669 | * ftrace_set_notrace - set a function to not trace in ftrace | |
936e074b | 3670 | * @ops - the ops to set the notrace filter with |
41c52c0d SR |
3671 | * @buf - the string that holds the function notrace text. |
3672 | * @len - the length of the string. | |
3673 | * @reset - non zero to reset all filters before applying this filter. | |
3674 | * | |
3675 | * Notrace Filters denote which functions should not be enabled when tracing | |
3676 | * is enabled. If @buf is NULL and reset is set, all functions will be enabled | |
3677 | * for tracing. | |
3678 | */ | |
936e074b | 3679 | void ftrace_set_global_notrace(unsigned char *buf, int len, int reset) |
41c52c0d | 3680 | { |
f45948e8 | 3681 | ftrace_set_regex(&global_ops, buf, len, reset, 0); |
77a2b37d | 3682 | } |
936e074b | 3683 | EXPORT_SYMBOL_GPL(ftrace_set_global_notrace); |
77a2b37d | 3684 | |
2af15d6a SR |
3685 | /* |
3686 | * command line interface to allow users to set filters on boot up. | |
3687 | */ | |
3688 | #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE | |
3689 | static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata; | |
3690 | static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata; | |
3691 | ||
f1ed7c74 SRRH |
3692 | /* Used by function selftest to not test if filter is set */ |
3693 | bool ftrace_filter_param __initdata; | |
3694 | ||
2af15d6a SR |
3695 | static int __init set_ftrace_notrace(char *str) |
3696 | { | |
f1ed7c74 | 3697 | ftrace_filter_param = true; |
75761cc1 | 3698 | strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE); |
2af15d6a SR |
3699 | return 1; |
3700 | } | |
3701 | __setup("ftrace_notrace=", set_ftrace_notrace); | |
3702 | ||
3703 | static int __init set_ftrace_filter(char *str) | |
3704 | { | |
f1ed7c74 | 3705 | ftrace_filter_param = true; |
75761cc1 | 3706 | strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE); |
2af15d6a SR |
3707 | return 1; |
3708 | } | |
3709 | __setup("ftrace_filter=", set_ftrace_filter); | |
3710 | ||
369bc18f | 3711 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
f6060f46 | 3712 | static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata; |
faf982a6 | 3713 | static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer); |
801c29fd | 3714 | |
369bc18f SA |
3715 | static int __init set_graph_function(char *str) |
3716 | { | |
06f43d66 | 3717 | strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE); |
369bc18f SA |
3718 | return 1; |
3719 | } | |
3720 | __setup("ftrace_graph_filter=", set_graph_function); | |
3721 | ||
3722 | static void __init set_ftrace_early_graph(char *buf) | |
3723 | { | |
3724 | int ret; | |
3725 | char *func; | |
3726 | ||
3727 | while (buf) { | |
3728 | func = strsep(&buf, ","); | |
3729 | /* we allow only one expression at a time */ | |
3730 | ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count, | |
faf982a6 | 3731 | FTRACE_GRAPH_MAX_FUNCS, func); |
369bc18f SA |
3732 | if (ret) |
3733 | printk(KERN_DEBUG "ftrace: function %s not " | |
3734 | "traceable\n", func); | |
3735 | } | |
3736 | } | |
3737 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ | |
3738 | ||
2a85a37f SR |
3739 | void __init |
3740 | ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable) | |
2af15d6a SR |
3741 | { |
3742 | char *func; | |
3743 | ||
f04f24fb MH |
3744 | ftrace_ops_init(ops); |
3745 | ||
2af15d6a SR |
3746 | while (buf) { |
3747 | func = strsep(&buf, ","); | |
f45948e8 | 3748 | ftrace_set_regex(ops, func, strlen(func), 0, enable); |
2af15d6a SR |
3749 | } |
3750 | } | |
3751 | ||
3752 | static void __init set_ftrace_early_filters(void) | |
3753 | { | |
3754 | if (ftrace_filter_buf[0]) | |
2a85a37f | 3755 | ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1); |
2af15d6a | 3756 | if (ftrace_notrace_buf[0]) |
2a85a37f | 3757 | ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0); |
369bc18f SA |
3758 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
3759 | if (ftrace_graph_buf[0]) | |
3760 | set_ftrace_early_graph(ftrace_graph_buf); | |
3761 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ | |
2af15d6a SR |
3762 | } |
3763 | ||
fc13cb0c | 3764 | int ftrace_regex_release(struct inode *inode, struct file *file) |
5072c59f SR |
3765 | { |
3766 | struct seq_file *m = (struct seq_file *)file->private_data; | |
3767 | struct ftrace_iterator *iter; | |
33dc9b12 | 3768 | struct ftrace_hash **orig_hash; |
689fd8b6 | 3769 | struct trace_parser *parser; |
ed926f9b | 3770 | int filter_hash; |
33dc9b12 | 3771 | int ret; |
5072c59f | 3772 | |
5072c59f SR |
3773 | if (file->f_mode & FMODE_READ) { |
3774 | iter = m->private; | |
5072c59f SR |
3775 | seq_release(inode, file); |
3776 | } else | |
3777 | iter = file->private_data; | |
3778 | ||
689fd8b6 | 3779 | parser = &iter->parser; |
3780 | if (trace_parser_loaded(parser)) { | |
3781 | parser->buffer[parser->idx] = 0; | |
1cf41dd7 | 3782 | ftrace_match_records(iter->hash, parser->buffer, parser->idx); |
5072c59f SR |
3783 | } |
3784 | ||
689fd8b6 | 3785 | trace_parser_put(parser); |
689fd8b6 | 3786 | |
3f2367ba MH |
3787 | mutex_lock(&iter->ops->regex_lock); |
3788 | ||
058e297d | 3789 | if (file->f_mode & FMODE_WRITE) { |
ed926f9b SR |
3790 | filter_hash = !!(iter->flags & FTRACE_ITER_FILTER); |
3791 | ||
3792 | if (filter_hash) | |
33dc9b12 | 3793 | orig_hash = &iter->ops->filter_hash; |
ed926f9b SR |
3794 | else |
3795 | orig_hash = &iter->ops->notrace_hash; | |
33dc9b12 | 3796 | |
058e297d | 3797 | mutex_lock(&ftrace_lock); |
41fb61c2 SR |
3798 | ret = ftrace_hash_move(iter->ops, filter_hash, |
3799 | orig_hash, iter->hash); | |
1c80c432 SRRH |
3800 | if (!ret) |
3801 | ftrace_ops_update_code(iter->ops); | |
41fb61c2 | 3802 | |
058e297d SR |
3803 | mutex_unlock(&ftrace_lock); |
3804 | } | |
3f2367ba MH |
3805 | |
3806 | mutex_unlock(&iter->ops->regex_lock); | |
33dc9b12 SR |
3807 | free_ftrace_hash(iter->hash); |
3808 | kfree(iter); | |
058e297d | 3809 | |
5072c59f SR |
3810 | return 0; |
3811 | } | |
3812 | ||
5e2336a0 | 3813 | static const struct file_operations ftrace_avail_fops = { |
5072c59f SR |
3814 | .open = ftrace_avail_open, |
3815 | .read = seq_read, | |
3816 | .llseek = seq_lseek, | |
3be04b47 | 3817 | .release = seq_release_private, |
5072c59f SR |
3818 | }; |
3819 | ||
647bcd03 SR |
3820 | static const struct file_operations ftrace_enabled_fops = { |
3821 | .open = ftrace_enabled_open, | |
3822 | .read = seq_read, | |
3823 | .llseek = seq_lseek, | |
3824 | .release = seq_release_private, | |
3825 | }; | |
3826 | ||
5e2336a0 | 3827 | static const struct file_operations ftrace_filter_fops = { |
5072c59f | 3828 | .open = ftrace_filter_open, |
850a80cf | 3829 | .read = seq_read, |
5072c59f | 3830 | .write = ftrace_filter_write, |
098c879e | 3831 | .llseek = tracing_lseek, |
1cf41dd7 | 3832 | .release = ftrace_regex_release, |
5072c59f SR |
3833 | }; |
3834 | ||
5e2336a0 | 3835 | static const struct file_operations ftrace_notrace_fops = { |
41c52c0d | 3836 | .open = ftrace_notrace_open, |
850a80cf | 3837 | .read = seq_read, |
41c52c0d | 3838 | .write = ftrace_notrace_write, |
098c879e | 3839 | .llseek = tracing_lseek, |
1cf41dd7 | 3840 | .release = ftrace_regex_release, |
41c52c0d SR |
3841 | }; |
3842 | ||
ea4e2bc4 SR |
3843 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
3844 | ||
3845 | static DEFINE_MUTEX(graph_lock); | |
3846 | ||
3847 | int ftrace_graph_count; | |
29ad23b0 | 3848 | int ftrace_graph_notrace_count; |
ea4e2bc4 | 3849 | unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly; |
29ad23b0 | 3850 | unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly; |
ea4e2bc4 | 3851 | |
faf982a6 NK |
3852 | struct ftrace_graph_data { |
3853 | unsigned long *table; | |
3854 | size_t size; | |
3855 | int *count; | |
3856 | const struct seq_operations *seq_ops; | |
3857 | }; | |
3858 | ||
ea4e2bc4 | 3859 | static void * |
85951842 | 3860 | __g_next(struct seq_file *m, loff_t *pos) |
ea4e2bc4 | 3861 | { |
faf982a6 NK |
3862 | struct ftrace_graph_data *fgd = m->private; |
3863 | ||
3864 | if (*pos >= *fgd->count) | |
ea4e2bc4 | 3865 | return NULL; |
faf982a6 | 3866 | return &fgd->table[*pos]; |
85951842 | 3867 | } |
ea4e2bc4 | 3868 | |
85951842 LZ |
3869 | static void * |
3870 | g_next(struct seq_file *m, void *v, loff_t *pos) | |
3871 | { | |
3872 | (*pos)++; | |
3873 | return __g_next(m, pos); | |
ea4e2bc4 SR |
3874 | } |
3875 | ||
3876 | static void *g_start(struct seq_file *m, loff_t *pos) | |
3877 | { | |
faf982a6 NK |
3878 | struct ftrace_graph_data *fgd = m->private; |
3879 | ||
ea4e2bc4 SR |
3880 | mutex_lock(&graph_lock); |
3881 | ||
f9349a8f | 3882 | /* Nothing, tell g_show to print all functions are enabled */ |
faf982a6 | 3883 | if (!*fgd->count && !*pos) |
f9349a8f FW |
3884 | return (void *)1; |
3885 | ||
85951842 | 3886 | return __g_next(m, pos); |
ea4e2bc4 SR |
3887 | } |
3888 | ||
3889 | static void g_stop(struct seq_file *m, void *p) | |
3890 | { | |
3891 | mutex_unlock(&graph_lock); | |
3892 | } | |
3893 | ||
3894 | static int g_show(struct seq_file *m, void *v) | |
3895 | { | |
3896 | unsigned long *ptr = v; | |
ea4e2bc4 SR |
3897 | |
3898 | if (!ptr) | |
3899 | return 0; | |
3900 | ||
f9349a8f FW |
3901 | if (ptr == (unsigned long *)1) { |
3902 | seq_printf(m, "#### all functions enabled ####\n"); | |
3903 | return 0; | |
3904 | } | |
3905 | ||
b375a11a | 3906 | seq_printf(m, "%ps\n", (void *)*ptr); |
ea4e2bc4 SR |
3907 | |
3908 | return 0; | |
3909 | } | |
3910 | ||
88e9d34c | 3911 | static const struct seq_operations ftrace_graph_seq_ops = { |
ea4e2bc4 SR |
3912 | .start = g_start, |
3913 | .next = g_next, | |
3914 | .stop = g_stop, | |
3915 | .show = g_show, | |
3916 | }; | |
3917 | ||
3918 | static int | |
faf982a6 NK |
3919 | __ftrace_graph_open(struct inode *inode, struct file *file, |
3920 | struct ftrace_graph_data *fgd) | |
ea4e2bc4 SR |
3921 | { |
3922 | int ret = 0; | |
3923 | ||
ea4e2bc4 SR |
3924 | mutex_lock(&graph_lock); |
3925 | if ((file->f_mode & FMODE_WRITE) && | |
8650ae32 | 3926 | (file->f_flags & O_TRUNC)) { |
faf982a6 NK |
3927 | *fgd->count = 0; |
3928 | memset(fgd->table, 0, fgd->size * sizeof(*fgd->table)); | |
ea4e2bc4 | 3929 | } |
a4ec5e0c | 3930 | mutex_unlock(&graph_lock); |
ea4e2bc4 | 3931 | |
faf982a6 NK |
3932 | if (file->f_mode & FMODE_READ) { |
3933 | ret = seq_open(file, fgd->seq_ops); | |
3934 | if (!ret) { | |
3935 | struct seq_file *m = file->private_data; | |
3936 | m->private = fgd; | |
3937 | } | |
3938 | } else | |
3939 | file->private_data = fgd; | |
ea4e2bc4 SR |
3940 | |
3941 | return ret; | |
3942 | } | |
3943 | ||
faf982a6 NK |
3944 | static int |
3945 | ftrace_graph_open(struct inode *inode, struct file *file) | |
3946 | { | |
3947 | struct ftrace_graph_data *fgd; | |
3948 | ||
3949 | if (unlikely(ftrace_disabled)) | |
3950 | return -ENODEV; | |
3951 | ||
3952 | fgd = kmalloc(sizeof(*fgd), GFP_KERNEL); | |
3953 | if (fgd == NULL) | |
3954 | return -ENOMEM; | |
3955 | ||
3956 | fgd->table = ftrace_graph_funcs; | |
3957 | fgd->size = FTRACE_GRAPH_MAX_FUNCS; | |
3958 | fgd->count = &ftrace_graph_count; | |
3959 | fgd->seq_ops = &ftrace_graph_seq_ops; | |
3960 | ||
3961 | return __ftrace_graph_open(inode, file, fgd); | |
3962 | } | |
3963 | ||
29ad23b0 NK |
3964 | static int |
3965 | ftrace_graph_notrace_open(struct inode *inode, struct file *file) | |
3966 | { | |
3967 | struct ftrace_graph_data *fgd; | |
3968 | ||
3969 | if (unlikely(ftrace_disabled)) | |
3970 | return -ENODEV; | |
3971 | ||
3972 | fgd = kmalloc(sizeof(*fgd), GFP_KERNEL); | |
3973 | if (fgd == NULL) | |
3974 | return -ENOMEM; | |
3975 | ||
3976 | fgd->table = ftrace_graph_notrace_funcs; | |
3977 | fgd->size = FTRACE_GRAPH_MAX_FUNCS; | |
3978 | fgd->count = &ftrace_graph_notrace_count; | |
3979 | fgd->seq_ops = &ftrace_graph_seq_ops; | |
3980 | ||
3981 | return __ftrace_graph_open(inode, file, fgd); | |
3982 | } | |
3983 | ||
87827111 LZ |
3984 | static int |
3985 | ftrace_graph_release(struct inode *inode, struct file *file) | |
3986 | { | |
faf982a6 NK |
3987 | if (file->f_mode & FMODE_READ) { |
3988 | struct seq_file *m = file->private_data; | |
3989 | ||
3990 | kfree(m->private); | |
87827111 | 3991 | seq_release(inode, file); |
faf982a6 NK |
3992 | } else { |
3993 | kfree(file->private_data); | |
3994 | } | |
3995 | ||
87827111 LZ |
3996 | return 0; |
3997 | } | |
3998 | ||
ea4e2bc4 | 3999 | static int |
faf982a6 | 4000 | ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer) |
ea4e2bc4 | 4001 | { |
ea4e2bc4 SR |
4002 | struct dyn_ftrace *rec; |
4003 | struct ftrace_page *pg; | |
f9349a8f | 4004 | int search_len; |
c7c6b1fe | 4005 | int fail = 1; |
f9349a8f FW |
4006 | int type, not; |
4007 | char *search; | |
4008 | bool exists; | |
4009 | int i; | |
ea4e2bc4 | 4010 | |
f9349a8f | 4011 | /* decode regex */ |
3f6fe06d | 4012 | type = filter_parse_regex(buffer, strlen(buffer), &search, ¬); |
faf982a6 | 4013 | if (!not && *idx >= size) |
c7c6b1fe | 4014 | return -EBUSY; |
f9349a8f FW |
4015 | |
4016 | search_len = strlen(search); | |
4017 | ||
52baf119 | 4018 | mutex_lock(&ftrace_lock); |
45a4a237 SR |
4019 | |
4020 | if (unlikely(ftrace_disabled)) { | |
4021 | mutex_unlock(&ftrace_lock); | |
4022 | return -ENODEV; | |
4023 | } | |
4024 | ||
265c831c SR |
4025 | do_for_each_ftrace_rec(pg, rec) { |
4026 | ||
b9df92d2 | 4027 | if (ftrace_match_record(rec, NULL, search, search_len, type)) { |
c7c6b1fe | 4028 | /* if it is in the array */ |
f9349a8f | 4029 | exists = false; |
c7c6b1fe | 4030 | for (i = 0; i < *idx; i++) { |
f9349a8f FW |
4031 | if (array[i] == rec->ip) { |
4032 | exists = true; | |
265c831c SR |
4033 | break; |
4034 | } | |
c7c6b1fe LZ |
4035 | } |
4036 | ||
4037 | if (!not) { | |
4038 | fail = 0; | |
4039 | if (!exists) { | |
4040 | array[(*idx)++] = rec->ip; | |
faf982a6 | 4041 | if (*idx >= size) |
c7c6b1fe LZ |
4042 | goto out; |
4043 | } | |
4044 | } else { | |
4045 | if (exists) { | |
4046 | array[i] = array[--(*idx)]; | |
4047 | array[*idx] = 0; | |
4048 | fail = 0; | |
4049 | } | |
4050 | } | |
ea4e2bc4 | 4051 | } |
265c831c | 4052 | } while_for_each_ftrace_rec(); |
c7c6b1fe | 4053 | out: |
52baf119 | 4054 | mutex_unlock(&ftrace_lock); |
ea4e2bc4 | 4055 | |
c7c6b1fe LZ |
4056 | if (fail) |
4057 | return -EINVAL; | |
4058 | ||
c7c6b1fe | 4059 | return 0; |
ea4e2bc4 SR |
4060 | } |
4061 | ||
4062 | static ssize_t | |
4063 | ftrace_graph_write(struct file *file, const char __user *ubuf, | |
4064 | size_t cnt, loff_t *ppos) | |
4065 | { | |
689fd8b6 | 4066 | struct trace_parser parser; |
6a10108b | 4067 | ssize_t read, ret = 0; |
faf982a6 | 4068 | struct ftrace_graph_data *fgd = file->private_data; |
ea4e2bc4 | 4069 | |
c7c6b1fe | 4070 | if (!cnt) |
ea4e2bc4 SR |
4071 | return 0; |
4072 | ||
6a10108b NK |
4073 | if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) |
4074 | return -ENOMEM; | |
ea4e2bc4 | 4075 | |
689fd8b6 | 4076 | read = trace_get_user(&parser, ubuf, cnt, ppos); |
ea4e2bc4 | 4077 | |
4ba7978e | 4078 | if (read >= 0 && trace_parser_loaded((&parser))) { |
689fd8b6 | 4079 | parser.buffer[parser.idx] = 0; |
4080 | ||
6a10108b NK |
4081 | mutex_lock(&graph_lock); |
4082 | ||
689fd8b6 | 4083 | /* we allow only one expression at a time */ |
faf982a6 NK |
4084 | ret = ftrace_set_func(fgd->table, fgd->count, fgd->size, |
4085 | parser.buffer); | |
6a10108b NK |
4086 | |
4087 | mutex_unlock(&graph_lock); | |
ea4e2bc4 | 4088 | } |
ea4e2bc4 | 4089 | |
6a10108b NK |
4090 | if (!ret) |
4091 | ret = read; | |
1eb90f13 | 4092 | |
689fd8b6 | 4093 | trace_parser_put(&parser); |
ea4e2bc4 SR |
4094 | |
4095 | return ret; | |
4096 | } | |
4097 | ||
4098 | static const struct file_operations ftrace_graph_fops = { | |
87827111 LZ |
4099 | .open = ftrace_graph_open, |
4100 | .read = seq_read, | |
4101 | .write = ftrace_graph_write, | |
098c879e | 4102 | .llseek = tracing_lseek, |
87827111 | 4103 | .release = ftrace_graph_release, |
ea4e2bc4 | 4104 | }; |
29ad23b0 NK |
4105 | |
4106 | static const struct file_operations ftrace_graph_notrace_fops = { | |
4107 | .open = ftrace_graph_notrace_open, | |
4108 | .read = seq_read, | |
4109 | .write = ftrace_graph_write, | |
098c879e | 4110 | .llseek = tracing_lseek, |
29ad23b0 NK |
4111 | .release = ftrace_graph_release, |
4112 | }; | |
ea4e2bc4 SR |
4113 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |
4114 | ||
df4fc315 | 4115 | static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer) |
5072c59f | 4116 | { |
5072c59f | 4117 | |
5452af66 FW |
4118 | trace_create_file("available_filter_functions", 0444, |
4119 | d_tracer, NULL, &ftrace_avail_fops); | |
5072c59f | 4120 | |
647bcd03 SR |
4121 | trace_create_file("enabled_functions", 0444, |
4122 | d_tracer, NULL, &ftrace_enabled_fops); | |
4123 | ||
5452af66 | 4124 | trace_create_file("set_ftrace_filter", 0644, d_tracer, |
e3b3e2e8 | 4125 | &global_ops, &ftrace_filter_fops); |
41c52c0d | 4126 | |
5452af66 | 4127 | trace_create_file("set_ftrace_notrace", 0644, d_tracer, |
e3b3e2e8 | 4128 | &global_ops, &ftrace_notrace_fops); |
ad90c0e3 | 4129 | |
ea4e2bc4 | 4130 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
5452af66 | 4131 | trace_create_file("set_graph_function", 0444, d_tracer, |
ea4e2bc4 SR |
4132 | NULL, |
4133 | &ftrace_graph_fops); | |
29ad23b0 NK |
4134 | trace_create_file("set_graph_notrace", 0444, d_tracer, |
4135 | NULL, | |
4136 | &ftrace_graph_notrace_fops); | |
ea4e2bc4 SR |
4137 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |
4138 | ||
5072c59f SR |
4139 | return 0; |
4140 | } | |
4141 | ||
9fd49328 | 4142 | static int ftrace_cmp_ips(const void *a, const void *b) |
68950619 | 4143 | { |
9fd49328 SR |
4144 | const unsigned long *ipa = a; |
4145 | const unsigned long *ipb = b; | |
68950619 | 4146 | |
9fd49328 SR |
4147 | if (*ipa > *ipb) |
4148 | return 1; | |
4149 | if (*ipa < *ipb) | |
4150 | return -1; | |
4151 | return 0; | |
4152 | } | |
4153 | ||
4154 | static void ftrace_swap_ips(void *a, void *b, int size) | |
4155 | { | |
4156 | unsigned long *ipa = a; | |
4157 | unsigned long *ipb = b; | |
4158 | unsigned long t; | |
4159 | ||
4160 | t = *ipa; | |
4161 | *ipa = *ipb; | |
4162 | *ipb = t; | |
68950619 SR |
4163 | } |
4164 | ||
5cb084bb | 4165 | static int ftrace_process_locs(struct module *mod, |
31e88909 | 4166 | unsigned long *start, |
68bf21aa SR |
4167 | unsigned long *end) |
4168 | { | |
706c81f8 | 4169 | struct ftrace_page *start_pg; |
a7900875 | 4170 | struct ftrace_page *pg; |
706c81f8 | 4171 | struct dyn_ftrace *rec; |
a7900875 | 4172 | unsigned long count; |
68bf21aa SR |
4173 | unsigned long *p; |
4174 | unsigned long addr; | |
4376cac6 | 4175 | unsigned long flags = 0; /* Shut up gcc */ |
a7900875 SR |
4176 | int ret = -ENOMEM; |
4177 | ||
4178 | count = end - start; | |
4179 | ||
4180 | if (!count) | |
4181 | return 0; | |
4182 | ||
9fd49328 SR |
4183 | sort(start, count, sizeof(*start), |
4184 | ftrace_cmp_ips, ftrace_swap_ips); | |
4185 | ||
706c81f8 SR |
4186 | start_pg = ftrace_allocate_pages(count); |
4187 | if (!start_pg) | |
a7900875 | 4188 | return -ENOMEM; |
68bf21aa | 4189 | |
e6ea44e9 | 4190 | mutex_lock(&ftrace_lock); |
a7900875 | 4191 | |
32082309 SR |
4192 | /* |
4193 | * Core and each module needs their own pages, as | |
4194 | * modules will free them when they are removed. | |
4195 | * Force a new page to be allocated for modules. | |
4196 | */ | |
a7900875 SR |
4197 | if (!mod) { |
4198 | WARN_ON(ftrace_pages || ftrace_pages_start); | |
4199 | /* First initialization */ | |
706c81f8 | 4200 | ftrace_pages = ftrace_pages_start = start_pg; |
a7900875 | 4201 | } else { |
32082309 | 4202 | if (!ftrace_pages) |
a7900875 | 4203 | goto out; |
32082309 | 4204 | |
a7900875 SR |
4205 | if (WARN_ON(ftrace_pages->next)) { |
4206 | /* Hmm, we have free pages? */ | |
4207 | while (ftrace_pages->next) | |
4208 | ftrace_pages = ftrace_pages->next; | |
32082309 | 4209 | } |
a7900875 | 4210 | |
706c81f8 | 4211 | ftrace_pages->next = start_pg; |
32082309 SR |
4212 | } |
4213 | ||
68bf21aa | 4214 | p = start; |
706c81f8 | 4215 | pg = start_pg; |
68bf21aa SR |
4216 | while (p < end) { |
4217 | addr = ftrace_call_adjust(*p++); | |
20e5227e SR |
4218 | /* |
4219 | * Some architecture linkers will pad between | |
4220 | * the different mcount_loc sections of different | |
4221 | * object files to satisfy alignments. | |
4222 | * Skip any NULL pointers. | |
4223 | */ | |
4224 | if (!addr) | |
4225 | continue; | |
706c81f8 SR |
4226 | |
4227 | if (pg->index == pg->size) { | |
4228 | /* We should have allocated enough */ | |
4229 | if (WARN_ON(!pg->next)) | |
4230 | break; | |
4231 | pg = pg->next; | |
4232 | } | |
4233 | ||
4234 | rec = &pg->records[pg->index++]; | |
4235 | rec->ip = addr; | |
68bf21aa SR |
4236 | } |
4237 | ||
706c81f8 SR |
4238 | /* We should have used all pages */ |
4239 | WARN_ON(pg->next); | |
4240 | ||
4241 | /* Assign the last page to ftrace_pages */ | |
4242 | ftrace_pages = pg; | |
4243 | ||
85ae32ae | 4244 | /* These new locations need to be initialized */ |
706c81f8 | 4245 | ftrace_new_pgs = start_pg; |
85ae32ae | 4246 | |
a4f18ed1 | 4247 | /* |
4376cac6 SR |
4248 | * We only need to disable interrupts on start up |
4249 | * because we are modifying code that an interrupt | |
4250 | * may execute, and the modification is not atomic. | |
4251 | * But for modules, nothing runs the code we modify | |
4252 | * until we are finished with it, and there's no | |
4253 | * reason to cause large interrupt latencies while we do it. | |
a4f18ed1 | 4254 | */ |
4376cac6 SR |
4255 | if (!mod) |
4256 | local_irq_save(flags); | |
31e88909 | 4257 | ftrace_update_code(mod); |
4376cac6 SR |
4258 | if (!mod) |
4259 | local_irq_restore(flags); | |
a7900875 SR |
4260 | ret = 0; |
4261 | out: | |
e6ea44e9 | 4262 | mutex_unlock(&ftrace_lock); |
68bf21aa | 4263 | |
a7900875 | 4264 | return ret; |
68bf21aa SR |
4265 | } |
4266 | ||
93eb677d | 4267 | #ifdef CONFIG_MODULES |
32082309 SR |
4268 | |
4269 | #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next) | |
4270 | ||
e7247a15 | 4271 | void ftrace_release_mod(struct module *mod) |
93eb677d SR |
4272 | { |
4273 | struct dyn_ftrace *rec; | |
32082309 | 4274 | struct ftrace_page **last_pg; |
93eb677d | 4275 | struct ftrace_page *pg; |
a7900875 | 4276 | int order; |
93eb677d | 4277 | |
45a4a237 SR |
4278 | mutex_lock(&ftrace_lock); |
4279 | ||
e7247a15 | 4280 | if (ftrace_disabled) |
45a4a237 | 4281 | goto out_unlock; |
93eb677d | 4282 | |
32082309 SR |
4283 | /* |
4284 | * Each module has its own ftrace_pages, remove | |
4285 | * them from the list. | |
4286 | */ | |
4287 | last_pg = &ftrace_pages_start; | |
4288 | for (pg = ftrace_pages_start; pg; pg = *last_pg) { | |
4289 | rec = &pg->records[0]; | |
e7247a15 | 4290 | if (within_module_core(rec->ip, mod)) { |
93eb677d | 4291 | /* |
32082309 SR |
4292 | * As core pages are first, the first |
4293 | * page should never be a module page. | |
93eb677d | 4294 | */ |
32082309 SR |
4295 | if (WARN_ON(pg == ftrace_pages_start)) |
4296 | goto out_unlock; | |
4297 | ||
4298 | /* Check if we are deleting the last page */ | |
4299 | if (pg == ftrace_pages) | |
4300 | ftrace_pages = next_to_ftrace_page(last_pg); | |
4301 | ||
4302 | *last_pg = pg->next; | |
a7900875 SR |
4303 | order = get_count_order(pg->size / ENTRIES_PER_PAGE); |
4304 | free_pages((unsigned long)pg->records, order); | |
4305 | kfree(pg); | |
32082309 SR |
4306 | } else |
4307 | last_pg = &pg->next; | |
4308 | } | |
45a4a237 | 4309 | out_unlock: |
93eb677d SR |
4310 | mutex_unlock(&ftrace_lock); |
4311 | } | |
4312 | ||
4313 | static void ftrace_init_module(struct module *mod, | |
4314 | unsigned long *start, unsigned long *end) | |
90d595fe | 4315 | { |
00fd61ae | 4316 | if (ftrace_disabled || start == end) |
fed1939c | 4317 | return; |
5cb084bb | 4318 | ftrace_process_locs(mod, start, end); |
90d595fe SR |
4319 | } |
4320 | ||
8c189ea6 SRRH |
4321 | static int ftrace_module_notify_enter(struct notifier_block *self, |
4322 | unsigned long val, void *data) | |
93eb677d SR |
4323 | { |
4324 | struct module *mod = data; | |
4325 | ||
8c189ea6 | 4326 | if (val == MODULE_STATE_COMING) |
93eb677d SR |
4327 | ftrace_init_module(mod, mod->ftrace_callsites, |
4328 | mod->ftrace_callsites + | |
4329 | mod->num_ftrace_callsites); | |
8c189ea6 SRRH |
4330 | return 0; |
4331 | } | |
4332 | ||
4333 | static int ftrace_module_notify_exit(struct notifier_block *self, | |
4334 | unsigned long val, void *data) | |
4335 | { | |
4336 | struct module *mod = data; | |
4337 | ||
4338 | if (val == MODULE_STATE_GOING) | |
e7247a15 | 4339 | ftrace_release_mod(mod); |
93eb677d SR |
4340 | |
4341 | return 0; | |
4342 | } | |
4343 | #else | |
8c189ea6 SRRH |
4344 | static int ftrace_module_notify_enter(struct notifier_block *self, |
4345 | unsigned long val, void *data) | |
4346 | { | |
4347 | return 0; | |
4348 | } | |
4349 | static int ftrace_module_notify_exit(struct notifier_block *self, | |
4350 | unsigned long val, void *data) | |
93eb677d SR |
4351 | { |
4352 | return 0; | |
4353 | } | |
4354 | #endif /* CONFIG_MODULES */ | |
4355 | ||
8c189ea6 SRRH |
4356 | struct notifier_block ftrace_module_enter_nb = { |
4357 | .notifier_call = ftrace_module_notify_enter, | |
c1bf08ac | 4358 | .priority = INT_MAX, /* Run before anything that can use kprobes */ |
93eb677d SR |
4359 | }; |
4360 | ||
8c189ea6 SRRH |
4361 | struct notifier_block ftrace_module_exit_nb = { |
4362 | .notifier_call = ftrace_module_notify_exit, | |
4363 | .priority = INT_MIN, /* Run after anything that can remove kprobes */ | |
4364 | }; | |
4365 | ||
68bf21aa SR |
4366 | extern unsigned long __start_mcount_loc[]; |
4367 | extern unsigned long __stop_mcount_loc[]; | |
4368 | ||
4369 | void __init ftrace_init(void) | |
4370 | { | |
4371 | unsigned long count, addr, flags; | |
4372 | int ret; | |
4373 | ||
4374 | /* Keep the ftrace pointer to the stub */ | |
4375 | addr = (unsigned long)ftrace_stub; | |
4376 | ||
4377 | local_irq_save(flags); | |
4378 | ftrace_dyn_arch_init(&addr); | |
4379 | local_irq_restore(flags); | |
4380 | ||
4381 | /* ftrace_dyn_arch_init places the return code in addr */ | |
4382 | if (addr) | |
4383 | goto failed; | |
4384 | ||
4385 | count = __stop_mcount_loc - __start_mcount_loc; | |
4386 | ||
4387 | ret = ftrace_dyn_table_alloc(count); | |
4388 | if (ret) | |
4389 | goto failed; | |
4390 | ||
4391 | last_ftrace_enabled = ftrace_enabled = 1; | |
4392 | ||
5cb084bb | 4393 | ret = ftrace_process_locs(NULL, |
31e88909 | 4394 | __start_mcount_loc, |
68bf21aa SR |
4395 | __stop_mcount_loc); |
4396 | ||
8c189ea6 SRRH |
4397 | ret = register_module_notifier(&ftrace_module_enter_nb); |
4398 | if (ret) | |
4399 | pr_warning("Failed to register trace ftrace module enter notifier\n"); | |
4400 | ||
4401 | ret = register_module_notifier(&ftrace_module_exit_nb); | |
24ed0c4b | 4402 | if (ret) |
8c189ea6 | 4403 | pr_warning("Failed to register trace ftrace module exit notifier\n"); |
93eb677d | 4404 | |
2af15d6a SR |
4405 | set_ftrace_early_filters(); |
4406 | ||
68bf21aa SR |
4407 | return; |
4408 | failed: | |
4409 | ftrace_disabled = 1; | |
4410 | } | |
68bf21aa | 4411 | |
3d083395 | 4412 | #else |
0b6e4d56 | 4413 | |
2b499381 | 4414 | static struct ftrace_ops global_ops = { |
bd69c30b | 4415 | .func = ftrace_stub, |
f04f24fb MH |
4416 | .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED, |
4417 | INIT_REGEX_LOCK(global_ops) | |
bd69c30b SR |
4418 | }; |
4419 | ||
0b6e4d56 FW |
4420 | static int __init ftrace_nodyn_init(void) |
4421 | { | |
4422 | ftrace_enabled = 1; | |
4423 | return 0; | |
4424 | } | |
6f415672 | 4425 | core_initcall(ftrace_nodyn_init); |
0b6e4d56 | 4426 | |
df4fc315 SR |
4427 | static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; } |
4428 | static inline void ftrace_startup_enable(int command) { } | |
5a45cfe1 | 4429 | /* Keep as macros so we do not need to define the commands */ |
8a56d776 SRRH |
4430 | # define ftrace_startup(ops, command) \ |
4431 | ({ \ | |
4432 | int ___ret = __register_ftrace_function(ops); \ | |
4433 | if (!___ret) \ | |
4434 | (ops)->flags |= FTRACE_OPS_FL_ENABLED; \ | |
4435 | ___ret; \ | |
3b6cfdb1 | 4436 | }) |
8a56d776 SRRH |
4437 | # define ftrace_shutdown(ops, command) __unregister_ftrace_function(ops) |
4438 | ||
c7aafc54 IM |
4439 | # define ftrace_startup_sysctl() do { } while (0) |
4440 | # define ftrace_shutdown_sysctl() do { } while (0) | |
b848914c SR |
4441 | |
4442 | static inline int | |
195a8afc | 4443 | ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs) |
b848914c SR |
4444 | { |
4445 | return 1; | |
4446 | } | |
4447 | ||
3d083395 SR |
4448 | #endif /* CONFIG_DYNAMIC_FTRACE */ |
4449 | ||
e248491a | 4450 | static void |
2f5f6ad9 | 4451 | ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip, |
a1e2e31d | 4452 | struct ftrace_ops *op, struct pt_regs *regs) |
e248491a | 4453 | { |
e248491a JO |
4454 | if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT))) |
4455 | return; | |
4456 | ||
4457 | /* | |
4458 | * Some of the ops may be dynamically allocated, | |
4459 | * they must be freed after a synchronize_sched(). | |
4460 | */ | |
4461 | preempt_disable_notrace(); | |
4462 | trace_recursion_set(TRACE_CONTROL_BIT); | |
b5aa3a47 SRRH |
4463 | |
4464 | /* | |
4465 | * Control funcs (perf) uses RCU. Only trace if | |
4466 | * RCU is currently active. | |
4467 | */ | |
4468 | if (!rcu_is_watching()) | |
4469 | goto out; | |
4470 | ||
0a016409 | 4471 | do_for_each_ftrace_op(op, ftrace_control_list) { |
395b97a3 SRRH |
4472 | if (!(op->flags & FTRACE_OPS_FL_STUB) && |
4473 | !ftrace_function_local_disabled(op) && | |
195a8afc | 4474 | ftrace_ops_test(op, ip, regs)) |
a1e2e31d | 4475 | op->func(ip, parent_ip, op, regs); |
0a016409 | 4476 | } while_for_each_ftrace_op(op); |
b5aa3a47 | 4477 | out: |
e248491a JO |
4478 | trace_recursion_clear(TRACE_CONTROL_BIT); |
4479 | preempt_enable_notrace(); | |
4480 | } | |
4481 | ||
4482 | static struct ftrace_ops control_ops = { | |
f04f24fb MH |
4483 | .func = ftrace_ops_control_func, |
4484 | .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED, | |
4485 | INIT_REGEX_LOCK(control_ops) | |
e248491a JO |
4486 | }; |
4487 | ||
2f5f6ad9 SR |
4488 | static inline void |
4489 | __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip, | |
a1e2e31d | 4490 | struct ftrace_ops *ignored, struct pt_regs *regs) |
b848914c | 4491 | { |
cdbe61bf | 4492 | struct ftrace_ops *op; |
edc15caf | 4493 | int bit; |
b848914c | 4494 | |
ccf3672d SR |
4495 | if (function_trace_stop) |
4496 | return; | |
4497 | ||
edc15caf SR |
4498 | bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX); |
4499 | if (bit < 0) | |
4500 | return; | |
b1cff0ad | 4501 | |
cdbe61bf SR |
4502 | /* |
4503 | * Some of the ops may be dynamically allocated, | |
4504 | * they must be freed after a synchronize_sched(). | |
4505 | */ | |
4506 | preempt_disable_notrace(); | |
0a016409 | 4507 | do_for_each_ftrace_op(op, ftrace_ops_list) { |
195a8afc | 4508 | if (ftrace_ops_test(op, ip, regs)) |
a1e2e31d | 4509 | op->func(ip, parent_ip, op, regs); |
0a016409 | 4510 | } while_for_each_ftrace_op(op); |
cdbe61bf | 4511 | preempt_enable_notrace(); |
edc15caf | 4512 | trace_clear_recursion(bit); |
b848914c SR |
4513 | } |
4514 | ||
2f5f6ad9 SR |
4515 | /* |
4516 | * Some archs only support passing ip and parent_ip. Even though | |
4517 | * the list function ignores the op parameter, we do not want any | |
4518 | * C side effects, where a function is called without the caller | |
4519 | * sending a third parameter. | |
a1e2e31d SR |
4520 | * Archs are to support both the regs and ftrace_ops at the same time. |
4521 | * If they support ftrace_ops, it is assumed they support regs. | |
4522 | * If call backs want to use regs, they must either check for regs | |
06aeaaea MH |
4523 | * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS. |
4524 | * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved. | |
a1e2e31d SR |
4525 | * An architecture can pass partial regs with ftrace_ops and still |
4526 | * set the ARCH_SUPPORT_FTARCE_OPS. | |
2f5f6ad9 SR |
4527 | */ |
4528 | #if ARCH_SUPPORTS_FTRACE_OPS | |
4529 | static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip, | |
a1e2e31d | 4530 | struct ftrace_ops *op, struct pt_regs *regs) |
2f5f6ad9 | 4531 | { |
a1e2e31d | 4532 | __ftrace_ops_list_func(ip, parent_ip, NULL, regs); |
2f5f6ad9 SR |
4533 | } |
4534 | #else | |
4535 | static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip) | |
4536 | { | |
a1e2e31d | 4537 | __ftrace_ops_list_func(ip, parent_ip, NULL, NULL); |
2f5f6ad9 SR |
4538 | } |
4539 | #endif | |
4540 | ||
e32d8956 | 4541 | static void clear_ftrace_swapper(void) |
978f3a45 SR |
4542 | { |
4543 | struct task_struct *p; | |
e32d8956 | 4544 | int cpu; |
978f3a45 | 4545 | |
e32d8956 SR |
4546 | get_online_cpus(); |
4547 | for_each_online_cpu(cpu) { | |
4548 | p = idle_task(cpu); | |
978f3a45 | 4549 | clear_tsk_trace_trace(p); |
e32d8956 SR |
4550 | } |
4551 | put_online_cpus(); | |
4552 | } | |
978f3a45 | 4553 | |
e32d8956 SR |
4554 | static void set_ftrace_swapper(void) |
4555 | { | |
4556 | struct task_struct *p; | |
4557 | int cpu; | |
4558 | ||
4559 | get_online_cpus(); | |
4560 | for_each_online_cpu(cpu) { | |
4561 | p = idle_task(cpu); | |
4562 | set_tsk_trace_trace(p); | |
4563 | } | |
4564 | put_online_cpus(); | |
978f3a45 SR |
4565 | } |
4566 | ||
e32d8956 SR |
4567 | static void clear_ftrace_pid(struct pid *pid) |
4568 | { | |
4569 | struct task_struct *p; | |
4570 | ||
229c4ef8 | 4571 | rcu_read_lock(); |
e32d8956 SR |
4572 | do_each_pid_task(pid, PIDTYPE_PID, p) { |
4573 | clear_tsk_trace_trace(p); | |
4574 | } while_each_pid_task(pid, PIDTYPE_PID, p); | |
229c4ef8 ON |
4575 | rcu_read_unlock(); |
4576 | ||
e32d8956 SR |
4577 | put_pid(pid); |
4578 | } | |
4579 | ||
4580 | static void set_ftrace_pid(struct pid *pid) | |
978f3a45 SR |
4581 | { |
4582 | struct task_struct *p; | |
4583 | ||
229c4ef8 | 4584 | rcu_read_lock(); |
978f3a45 SR |
4585 | do_each_pid_task(pid, PIDTYPE_PID, p) { |
4586 | set_tsk_trace_trace(p); | |
4587 | } while_each_pid_task(pid, PIDTYPE_PID, p); | |
229c4ef8 | 4588 | rcu_read_unlock(); |
978f3a45 SR |
4589 | } |
4590 | ||
756d17ee | 4591 | static void clear_ftrace_pid_task(struct pid *pid) |
e32d8956 | 4592 | { |
756d17ee | 4593 | if (pid == ftrace_swapper_pid) |
e32d8956 SR |
4594 | clear_ftrace_swapper(); |
4595 | else | |
756d17ee | 4596 | clear_ftrace_pid(pid); |
e32d8956 SR |
4597 | } |
4598 | ||
4599 | static void set_ftrace_pid_task(struct pid *pid) | |
4600 | { | |
4601 | if (pid == ftrace_swapper_pid) | |
4602 | set_ftrace_swapper(); | |
4603 | else | |
4604 | set_ftrace_pid(pid); | |
4605 | } | |
4606 | ||
756d17ee | 4607 | static int ftrace_pid_add(int p) |
df4fc315 | 4608 | { |
978f3a45 | 4609 | struct pid *pid; |
756d17ee | 4610 | struct ftrace_pid *fpid; |
4611 | int ret = -EINVAL; | |
df4fc315 | 4612 | |
756d17ee | 4613 | mutex_lock(&ftrace_lock); |
df4fc315 | 4614 | |
756d17ee | 4615 | if (!p) |
4616 | pid = ftrace_swapper_pid; | |
4617 | else | |
4618 | pid = find_get_pid(p); | |
df4fc315 | 4619 | |
756d17ee | 4620 | if (!pid) |
4621 | goto out; | |
df4fc315 | 4622 | |
756d17ee | 4623 | ret = 0; |
df4fc315 | 4624 | |
756d17ee | 4625 | list_for_each_entry(fpid, &ftrace_pids, list) |
4626 | if (fpid->pid == pid) | |
4627 | goto out_put; | |
978f3a45 | 4628 | |
756d17ee | 4629 | ret = -ENOMEM; |
df4fc315 | 4630 | |
756d17ee | 4631 | fpid = kmalloc(sizeof(*fpid), GFP_KERNEL); |
4632 | if (!fpid) | |
4633 | goto out_put; | |
df4fc315 | 4634 | |
756d17ee | 4635 | list_add(&fpid->list, &ftrace_pids); |
4636 | fpid->pid = pid; | |
0ef8cde5 | 4637 | |
756d17ee | 4638 | set_ftrace_pid_task(pid); |
978f3a45 | 4639 | |
756d17ee | 4640 | ftrace_update_pid_func(); |
4641 | ftrace_startup_enable(0); | |
4642 | ||
4643 | mutex_unlock(&ftrace_lock); | |
4644 | return 0; | |
4645 | ||
4646 | out_put: | |
4647 | if (pid != ftrace_swapper_pid) | |
4648 | put_pid(pid); | |
978f3a45 | 4649 | |
756d17ee | 4650 | out: |
4651 | mutex_unlock(&ftrace_lock); | |
4652 | return ret; | |
4653 | } | |
4654 | ||
4655 | static void ftrace_pid_reset(void) | |
4656 | { | |
4657 | struct ftrace_pid *fpid, *safe; | |
978f3a45 | 4658 | |
756d17ee | 4659 | mutex_lock(&ftrace_lock); |
4660 | list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) { | |
4661 | struct pid *pid = fpid->pid; | |
4662 | ||
4663 | clear_ftrace_pid_task(pid); | |
4664 | ||
4665 | list_del(&fpid->list); | |
4666 | kfree(fpid); | |
df4fc315 SR |
4667 | } |
4668 | ||
df4fc315 SR |
4669 | ftrace_update_pid_func(); |
4670 | ftrace_startup_enable(0); | |
4671 | ||
e6ea44e9 | 4672 | mutex_unlock(&ftrace_lock); |
756d17ee | 4673 | } |
df4fc315 | 4674 | |
756d17ee | 4675 | static void *fpid_start(struct seq_file *m, loff_t *pos) |
4676 | { | |
4677 | mutex_lock(&ftrace_lock); | |
4678 | ||
4679 | if (list_empty(&ftrace_pids) && (!*pos)) | |
4680 | return (void *) 1; | |
4681 | ||
4682 | return seq_list_start(&ftrace_pids, *pos); | |
4683 | } | |
4684 | ||
4685 | static void *fpid_next(struct seq_file *m, void *v, loff_t *pos) | |
4686 | { | |
4687 | if (v == (void *)1) | |
4688 | return NULL; | |
4689 | ||
4690 | return seq_list_next(v, &ftrace_pids, pos); | |
4691 | } | |
4692 | ||
4693 | static void fpid_stop(struct seq_file *m, void *p) | |
4694 | { | |
4695 | mutex_unlock(&ftrace_lock); | |
4696 | } | |
4697 | ||
4698 | static int fpid_show(struct seq_file *m, void *v) | |
4699 | { | |
4700 | const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list); | |
4701 | ||
4702 | if (v == (void *)1) { | |
4703 | seq_printf(m, "no pid\n"); | |
4704 | return 0; | |
4705 | } | |
4706 | ||
4707 | if (fpid->pid == ftrace_swapper_pid) | |
4708 | seq_printf(m, "swapper tasks\n"); | |
4709 | else | |
4710 | seq_printf(m, "%u\n", pid_vnr(fpid->pid)); | |
4711 | ||
4712 | return 0; | |
4713 | } | |
4714 | ||
4715 | static const struct seq_operations ftrace_pid_sops = { | |
4716 | .start = fpid_start, | |
4717 | .next = fpid_next, | |
4718 | .stop = fpid_stop, | |
4719 | .show = fpid_show, | |
4720 | }; | |
4721 | ||
4722 | static int | |
4723 | ftrace_pid_open(struct inode *inode, struct file *file) | |
4724 | { | |
4725 | int ret = 0; | |
4726 | ||
4727 | if ((file->f_mode & FMODE_WRITE) && | |
4728 | (file->f_flags & O_TRUNC)) | |
4729 | ftrace_pid_reset(); | |
4730 | ||
4731 | if (file->f_mode & FMODE_READ) | |
4732 | ret = seq_open(file, &ftrace_pid_sops); | |
4733 | ||
4734 | return ret; | |
4735 | } | |
4736 | ||
df4fc315 SR |
4737 | static ssize_t |
4738 | ftrace_pid_write(struct file *filp, const char __user *ubuf, | |
4739 | size_t cnt, loff_t *ppos) | |
4740 | { | |
457dc928 | 4741 | char buf[64], *tmp; |
df4fc315 SR |
4742 | long val; |
4743 | int ret; | |
4744 | ||
4745 | if (cnt >= sizeof(buf)) | |
4746 | return -EINVAL; | |
4747 | ||
4748 | if (copy_from_user(&buf, ubuf, cnt)) | |
4749 | return -EFAULT; | |
4750 | ||
4751 | buf[cnt] = 0; | |
4752 | ||
756d17ee | 4753 | /* |
4754 | * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid" | |
4755 | * to clean the filter quietly. | |
4756 | */ | |
457dc928 IM |
4757 | tmp = strstrip(buf); |
4758 | if (strlen(tmp) == 0) | |
756d17ee | 4759 | return 1; |
4760 | ||
bcd83ea6 | 4761 | ret = kstrtol(tmp, 10, &val); |
df4fc315 SR |
4762 | if (ret < 0) |
4763 | return ret; | |
4764 | ||
756d17ee | 4765 | ret = ftrace_pid_add(val); |
df4fc315 | 4766 | |
756d17ee | 4767 | return ret ? ret : cnt; |
4768 | } | |
df4fc315 | 4769 | |
756d17ee | 4770 | static int |
4771 | ftrace_pid_release(struct inode *inode, struct file *file) | |
4772 | { | |
4773 | if (file->f_mode & FMODE_READ) | |
4774 | seq_release(inode, file); | |
df4fc315 | 4775 | |
756d17ee | 4776 | return 0; |
df4fc315 SR |
4777 | } |
4778 | ||
5e2336a0 | 4779 | static const struct file_operations ftrace_pid_fops = { |
756d17ee | 4780 | .open = ftrace_pid_open, |
4781 | .write = ftrace_pid_write, | |
4782 | .read = seq_read, | |
098c879e | 4783 | .llseek = tracing_lseek, |
756d17ee | 4784 | .release = ftrace_pid_release, |
df4fc315 SR |
4785 | }; |
4786 | ||
4787 | static __init int ftrace_init_debugfs(void) | |
4788 | { | |
4789 | struct dentry *d_tracer; | |
df4fc315 SR |
4790 | |
4791 | d_tracer = tracing_init_dentry(); | |
4792 | if (!d_tracer) | |
4793 | return 0; | |
4794 | ||
4795 | ftrace_init_dyn_debugfs(d_tracer); | |
4796 | ||
5452af66 FW |
4797 | trace_create_file("set_ftrace_pid", 0644, d_tracer, |
4798 | NULL, &ftrace_pid_fops); | |
493762fc SR |
4799 | |
4800 | ftrace_profile_debugfs(d_tracer); | |
4801 | ||
df4fc315 SR |
4802 | return 0; |
4803 | } | |
df4fc315 SR |
4804 | fs_initcall(ftrace_init_debugfs); |
4805 | ||
a2bb6a3d | 4806 | /** |
81adbdc0 | 4807 | * ftrace_kill - kill ftrace |
a2bb6a3d SR |
4808 | * |
4809 | * This function should be used by panic code. It stops ftrace | |
4810 | * but in a not so nice way. If you need to simply kill ftrace | |
4811 | * from a non-atomic section, use ftrace_kill. | |
4812 | */ | |
81adbdc0 | 4813 | void ftrace_kill(void) |
a2bb6a3d SR |
4814 | { |
4815 | ftrace_disabled = 1; | |
4816 | ftrace_enabled = 0; | |
a2bb6a3d SR |
4817 | clear_ftrace_function(); |
4818 | } | |
4819 | ||
e0a413f6 SR |
4820 | /** |
4821 | * Test if ftrace is dead or not. | |
4822 | */ | |
4823 | int ftrace_is_dead(void) | |
4824 | { | |
4825 | return ftrace_disabled; | |
4826 | } | |
4827 | ||
16444a8a | 4828 | /** |
3d083395 SR |
4829 | * register_ftrace_function - register a function for profiling |
4830 | * @ops - ops structure that holds the function for profiling. | |
16444a8a | 4831 | * |
3d083395 SR |
4832 | * Register a function to be called by all functions in the |
4833 | * kernel. | |
4834 | * | |
4835 | * Note: @ops->func and all the functions it calls must be labeled | |
4836 | * with "notrace", otherwise it will go into a | |
4837 | * recursive loop. | |
16444a8a | 4838 | */ |
3d083395 | 4839 | int register_ftrace_function(struct ftrace_ops *ops) |
16444a8a | 4840 | { |
45a4a237 | 4841 | int ret = -1; |
4eebcc81 | 4842 | |
f04f24fb MH |
4843 | ftrace_ops_init(ops); |
4844 | ||
e6ea44e9 | 4845 | mutex_lock(&ftrace_lock); |
e7d3737e | 4846 | |
8a56d776 | 4847 | ret = ftrace_startup(ops, 0); |
b848914c | 4848 | |
e6ea44e9 | 4849 | mutex_unlock(&ftrace_lock); |
8d240dd8 | 4850 | |
b0fc494f | 4851 | return ret; |
3d083395 | 4852 | } |
cdbe61bf | 4853 | EXPORT_SYMBOL_GPL(register_ftrace_function); |
3d083395 SR |
4854 | |
4855 | /** | |
32632920 | 4856 | * unregister_ftrace_function - unregister a function for profiling. |
3d083395 SR |
4857 | * @ops - ops structure that holds the function to unregister |
4858 | * | |
4859 | * Unregister a function that was added to be called by ftrace profiling. | |
4860 | */ | |
4861 | int unregister_ftrace_function(struct ftrace_ops *ops) | |
4862 | { | |
4863 | int ret; | |
4864 | ||
e6ea44e9 | 4865 | mutex_lock(&ftrace_lock); |
8a56d776 | 4866 | ret = ftrace_shutdown(ops, 0); |
e6ea44e9 | 4867 | mutex_unlock(&ftrace_lock); |
b0fc494f SR |
4868 | |
4869 | return ret; | |
4870 | } | |
cdbe61bf | 4871 | EXPORT_SYMBOL_GPL(unregister_ftrace_function); |
b0fc494f | 4872 | |
e309b41d | 4873 | int |
b0fc494f | 4874 | ftrace_enable_sysctl(struct ctl_table *table, int write, |
8d65af78 | 4875 | void __user *buffer, size_t *lenp, |
b0fc494f SR |
4876 | loff_t *ppos) |
4877 | { | |
45a4a237 | 4878 | int ret = -ENODEV; |
4eebcc81 | 4879 | |
e6ea44e9 | 4880 | mutex_lock(&ftrace_lock); |
b0fc494f | 4881 | |
45a4a237 SR |
4882 | if (unlikely(ftrace_disabled)) |
4883 | goto out; | |
4884 | ||
4885 | ret = proc_dointvec(table, write, buffer, lenp, ppos); | |
b0fc494f | 4886 | |
a32c7765 | 4887 | if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled)) |
b0fc494f SR |
4888 | goto out; |
4889 | ||
a32c7765 | 4890 | last_ftrace_enabled = !!ftrace_enabled; |
b0fc494f SR |
4891 | |
4892 | if (ftrace_enabled) { | |
4893 | ||
4894 | ftrace_startup_sysctl(); | |
4895 | ||
4896 | /* we are starting ftrace again */ | |
5000c418 JK |
4897 | if (ftrace_ops_list != &ftrace_list_end) |
4898 | update_ftrace_function(); | |
b0fc494f SR |
4899 | |
4900 | } else { | |
4901 | /* stopping ftrace calls (just send to ftrace_stub) */ | |
4902 | ftrace_trace_function = ftrace_stub; | |
4903 | ||
4904 | ftrace_shutdown_sysctl(); | |
4905 | } | |
4906 | ||
4907 | out: | |
e6ea44e9 | 4908 | mutex_unlock(&ftrace_lock); |
3d083395 | 4909 | return ret; |
16444a8a | 4910 | } |
f17845e5 | 4911 | |
fb52607a | 4912 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
e7d3737e | 4913 | |
597af815 | 4914 | static int ftrace_graph_active; |
4a2b8dda | 4915 | static struct notifier_block ftrace_suspend_notifier; |
e7d3737e | 4916 | |
e49dc19c SR |
4917 | int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace) |
4918 | { | |
4919 | return 0; | |
4920 | } | |
4921 | ||
287b6e68 FW |
4922 | /* The callbacks that hook a function */ |
4923 | trace_func_graph_ret_t ftrace_graph_return = | |
4924 | (trace_func_graph_ret_t)ftrace_stub; | |
e49dc19c | 4925 | trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub; |
23a8e844 | 4926 | static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub; |
f201ae23 FW |
4927 | |
4928 | /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */ | |
4929 | static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list) | |
4930 | { | |
4931 | int i; | |
4932 | int ret = 0; | |
4933 | unsigned long flags; | |
4934 | int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE; | |
4935 | struct task_struct *g, *t; | |
4936 | ||
4937 | for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) { | |
4938 | ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH | |
4939 | * sizeof(struct ftrace_ret_stack), | |
4940 | GFP_KERNEL); | |
4941 | if (!ret_stack_list[i]) { | |
4942 | start = 0; | |
4943 | end = i; | |
4944 | ret = -ENOMEM; | |
4945 | goto free; | |
4946 | } | |
4947 | } | |
4948 | ||
4949 | read_lock_irqsave(&tasklist_lock, flags); | |
4950 | do_each_thread(g, t) { | |
4951 | if (start == end) { | |
4952 | ret = -EAGAIN; | |
4953 | goto unlock; | |
4954 | } | |
4955 | ||
4956 | if (t->ret_stack == NULL) { | |
380c4b14 | 4957 | atomic_set(&t->tracing_graph_pause, 0); |
f201ae23 | 4958 | atomic_set(&t->trace_overrun, 0); |
26c01624 SR |
4959 | t->curr_ret_stack = -1; |
4960 | /* Make sure the tasks see the -1 first: */ | |
4961 | smp_wmb(); | |
4962 | t->ret_stack = ret_stack_list[start++]; | |
f201ae23 FW |
4963 | } |
4964 | } while_each_thread(g, t); | |
4965 | ||
4966 | unlock: | |
4967 | read_unlock_irqrestore(&tasklist_lock, flags); | |
4968 | free: | |
4969 | for (i = start; i < end; i++) | |
4970 | kfree(ret_stack_list[i]); | |
4971 | return ret; | |
4972 | } | |
4973 | ||
8aef2d28 | 4974 | static void |
38516ab5 SR |
4975 | ftrace_graph_probe_sched_switch(void *ignore, |
4976 | struct task_struct *prev, struct task_struct *next) | |
8aef2d28 SR |
4977 | { |
4978 | unsigned long long timestamp; | |
4979 | int index; | |
4980 | ||
be6f164a SR |
4981 | /* |
4982 | * Does the user want to count the time a function was asleep. | |
4983 | * If so, do not update the time stamps. | |
4984 | */ | |
4985 | if (trace_flags & TRACE_ITER_SLEEP_TIME) | |
4986 | return; | |
4987 | ||
8aef2d28 SR |
4988 | timestamp = trace_clock_local(); |
4989 | ||
4990 | prev->ftrace_timestamp = timestamp; | |
4991 | ||
4992 | /* only process tasks that we timestamped */ | |
4993 | if (!next->ftrace_timestamp) | |
4994 | return; | |
4995 | ||
4996 | /* | |
4997 | * Update all the counters in next to make up for the | |
4998 | * time next was sleeping. | |
4999 | */ | |
5000 | timestamp -= next->ftrace_timestamp; | |
5001 | ||
5002 | for (index = next->curr_ret_stack; index >= 0; index--) | |
5003 | next->ret_stack[index].calltime += timestamp; | |
5004 | } | |
5005 | ||
f201ae23 | 5006 | /* Allocate a return stack for each task */ |
fb52607a | 5007 | static int start_graph_tracing(void) |
f201ae23 FW |
5008 | { |
5009 | struct ftrace_ret_stack **ret_stack_list; | |
5b058bcd | 5010 | int ret, cpu; |
f201ae23 FW |
5011 | |
5012 | ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE * | |
5013 | sizeof(struct ftrace_ret_stack *), | |
5014 | GFP_KERNEL); | |
5015 | ||
5016 | if (!ret_stack_list) | |
5017 | return -ENOMEM; | |
5018 | ||
5b058bcd | 5019 | /* The cpu_boot init_task->ret_stack will never be freed */ |
179c498a SR |
5020 | for_each_online_cpu(cpu) { |
5021 | if (!idle_task(cpu)->ret_stack) | |
868baf07 | 5022 | ftrace_graph_init_idle_task(idle_task(cpu), cpu); |
179c498a | 5023 | } |
5b058bcd | 5024 | |
f201ae23 FW |
5025 | do { |
5026 | ret = alloc_retstack_tasklist(ret_stack_list); | |
5027 | } while (ret == -EAGAIN); | |
5028 | ||
8aef2d28 | 5029 | if (!ret) { |
38516ab5 | 5030 | ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL); |
8aef2d28 SR |
5031 | if (ret) |
5032 | pr_info("ftrace_graph: Couldn't activate tracepoint" | |
5033 | " probe to kernel_sched_switch\n"); | |
5034 | } | |
5035 | ||
f201ae23 FW |
5036 | kfree(ret_stack_list); |
5037 | return ret; | |
5038 | } | |
5039 | ||
4a2b8dda FW |
5040 | /* |
5041 | * Hibernation protection. | |
5042 | * The state of the current task is too much unstable during | |
5043 | * suspend/restore to disk. We want to protect against that. | |
5044 | */ | |
5045 | static int | |
5046 | ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state, | |
5047 | void *unused) | |
5048 | { | |
5049 | switch (state) { | |
5050 | case PM_HIBERNATION_PREPARE: | |
5051 | pause_graph_tracing(); | |
5052 | break; | |
5053 | ||
5054 | case PM_POST_HIBERNATION: | |
5055 | unpause_graph_tracing(); | |
5056 | break; | |
5057 | } | |
5058 | return NOTIFY_DONE; | |
5059 | } | |
5060 | ||
8a56d776 SRRH |
5061 | /* Just a place holder for function graph */ |
5062 | static struct ftrace_ops fgraph_ops __read_mostly = { | |
5063 | .func = ftrace_stub, | |
5064 | .flags = FTRACE_OPS_FL_STUB | FTRACE_OPS_FL_GLOBAL | | |
5065 | FTRACE_OPS_FL_RECURSION_SAFE, | |
5066 | }; | |
5067 | ||
23a8e844 SRRH |
5068 | static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace) |
5069 | { | |
5070 | if (!ftrace_ops_test(&global_ops, trace->func, NULL)) | |
5071 | return 0; | |
5072 | return __ftrace_graph_entry(trace); | |
5073 | } | |
5074 | ||
5075 | /* | |
5076 | * The function graph tracer should only trace the functions defined | |
5077 | * by set_ftrace_filter and set_ftrace_notrace. If another function | |
5078 | * tracer ops is registered, the graph tracer requires testing the | |
5079 | * function against the global ops, and not just trace any function | |
5080 | * that any ftrace_ops registered. | |
5081 | */ | |
5082 | static void update_function_graph_func(void) | |
5083 | { | |
5084 | if (ftrace_ops_list == &ftrace_list_end || | |
5085 | (ftrace_ops_list == &global_ops && | |
5086 | global_ops.next == &ftrace_list_end)) | |
5087 | ftrace_graph_entry = __ftrace_graph_entry; | |
5088 | else | |
5089 | ftrace_graph_entry = ftrace_graph_entry_test; | |
5090 | } | |
5091 | ||
287b6e68 FW |
5092 | int register_ftrace_graph(trace_func_graph_ret_t retfunc, |
5093 | trace_func_graph_ent_t entryfunc) | |
15e6cb36 | 5094 | { |
e7d3737e FW |
5095 | int ret = 0; |
5096 | ||
e6ea44e9 | 5097 | mutex_lock(&ftrace_lock); |
e7d3737e | 5098 | |
05ce5818 | 5099 | /* we currently allow only one tracer registered at a time */ |
597af815 | 5100 | if (ftrace_graph_active) { |
05ce5818 SR |
5101 | ret = -EBUSY; |
5102 | goto out; | |
5103 | } | |
5104 | ||
4a2b8dda FW |
5105 | ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call; |
5106 | register_pm_notifier(&ftrace_suspend_notifier); | |
5107 | ||
597af815 | 5108 | ftrace_graph_active++; |
fb52607a | 5109 | ret = start_graph_tracing(); |
f201ae23 | 5110 | if (ret) { |
597af815 | 5111 | ftrace_graph_active--; |
f201ae23 FW |
5112 | goto out; |
5113 | } | |
e53a6319 | 5114 | |
287b6e68 | 5115 | ftrace_graph_return = retfunc; |
23a8e844 SRRH |
5116 | |
5117 | /* | |
5118 | * Update the indirect function to the entryfunc, and the | |
5119 | * function that gets called to the entry_test first. Then | |
5120 | * call the update fgraph entry function to determine if | |
5121 | * the entryfunc should be called directly or not. | |
5122 | */ | |
5123 | __ftrace_graph_entry = entryfunc; | |
5124 | ftrace_graph_entry = ftrace_graph_entry_test; | |
5125 | update_function_graph_func(); | |
e53a6319 | 5126 | |
8a56d776 | 5127 | ret = ftrace_startup(&fgraph_ops, FTRACE_START_FUNC_RET); |
e7d3737e FW |
5128 | |
5129 | out: | |
e6ea44e9 | 5130 | mutex_unlock(&ftrace_lock); |
e7d3737e | 5131 | return ret; |
15e6cb36 FW |
5132 | } |
5133 | ||
fb52607a | 5134 | void unregister_ftrace_graph(void) |
15e6cb36 | 5135 | { |
e6ea44e9 | 5136 | mutex_lock(&ftrace_lock); |
e7d3737e | 5137 | |
597af815 | 5138 | if (unlikely(!ftrace_graph_active)) |
2aad1b76 SR |
5139 | goto out; |
5140 | ||
597af815 | 5141 | ftrace_graph_active--; |
287b6e68 | 5142 | ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub; |
e49dc19c | 5143 | ftrace_graph_entry = ftrace_graph_entry_stub; |
23a8e844 | 5144 | __ftrace_graph_entry = ftrace_graph_entry_stub; |
8a56d776 | 5145 | ftrace_shutdown(&fgraph_ops, FTRACE_STOP_FUNC_RET); |
4a2b8dda | 5146 | unregister_pm_notifier(&ftrace_suspend_notifier); |
38516ab5 | 5147 | unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL); |
e7d3737e | 5148 | |
2aad1b76 | 5149 | out: |
e6ea44e9 | 5150 | mutex_unlock(&ftrace_lock); |
15e6cb36 | 5151 | } |
f201ae23 | 5152 | |
868baf07 SR |
5153 | static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack); |
5154 | ||
5155 | static void | |
5156 | graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack) | |
5157 | { | |
5158 | atomic_set(&t->tracing_graph_pause, 0); | |
5159 | atomic_set(&t->trace_overrun, 0); | |
5160 | t->ftrace_timestamp = 0; | |
25985edc | 5161 | /* make curr_ret_stack visible before we add the ret_stack */ |
868baf07 SR |
5162 | smp_wmb(); |
5163 | t->ret_stack = ret_stack; | |
5164 | } | |
5165 | ||
5166 | /* | |
5167 | * Allocate a return stack for the idle task. May be the first | |
5168 | * time through, or it may be done by CPU hotplug online. | |
5169 | */ | |
5170 | void ftrace_graph_init_idle_task(struct task_struct *t, int cpu) | |
5171 | { | |
5172 | t->curr_ret_stack = -1; | |
5173 | /* | |
5174 | * The idle task has no parent, it either has its own | |
5175 | * stack or no stack at all. | |
5176 | */ | |
5177 | if (t->ret_stack) | |
5178 | WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu)); | |
5179 | ||
5180 | if (ftrace_graph_active) { | |
5181 | struct ftrace_ret_stack *ret_stack; | |
5182 | ||
5183 | ret_stack = per_cpu(idle_ret_stack, cpu); | |
5184 | if (!ret_stack) { | |
5185 | ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH | |
5186 | * sizeof(struct ftrace_ret_stack), | |
5187 | GFP_KERNEL); | |
5188 | if (!ret_stack) | |
5189 | return; | |
5190 | per_cpu(idle_ret_stack, cpu) = ret_stack; | |
5191 | } | |
5192 | graph_init_task(t, ret_stack); | |
5193 | } | |
5194 | } | |
5195 | ||
f201ae23 | 5196 | /* Allocate a return stack for newly created task */ |
fb52607a | 5197 | void ftrace_graph_init_task(struct task_struct *t) |
f201ae23 | 5198 | { |
84047e36 SR |
5199 | /* Make sure we do not use the parent ret_stack */ |
5200 | t->ret_stack = NULL; | |
ea14eb71 | 5201 | t->curr_ret_stack = -1; |
84047e36 | 5202 | |
597af815 | 5203 | if (ftrace_graph_active) { |
82310a32 SR |
5204 | struct ftrace_ret_stack *ret_stack; |
5205 | ||
5206 | ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH | |
f201ae23 FW |
5207 | * sizeof(struct ftrace_ret_stack), |
5208 | GFP_KERNEL); | |
82310a32 | 5209 | if (!ret_stack) |
f201ae23 | 5210 | return; |
868baf07 | 5211 | graph_init_task(t, ret_stack); |
84047e36 | 5212 | } |
f201ae23 FW |
5213 | } |
5214 | ||
fb52607a | 5215 | void ftrace_graph_exit_task(struct task_struct *t) |
f201ae23 | 5216 | { |
eae849ca FW |
5217 | struct ftrace_ret_stack *ret_stack = t->ret_stack; |
5218 | ||
f201ae23 | 5219 | t->ret_stack = NULL; |
eae849ca FW |
5220 | /* NULL must become visible to IRQs before we free it: */ |
5221 | barrier(); | |
5222 | ||
5223 | kfree(ret_stack); | |
f201ae23 | 5224 | } |
14a866c5 SR |
5225 | |
5226 | void ftrace_graph_stop(void) | |
5227 | { | |
5228 | ftrace_stop(); | |
5229 | } | |
15e6cb36 | 5230 | #endif |