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 | |
13 | * Copyright (C) 2004 William Lee Irwin III | |
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> |
2d8b820b | 25 | #include <linux/ftrace.h> |
b0fc494f | 26 | #include <linux/sysctl.h> |
5072c59f | 27 | #include <linux/ctype.h> |
3d083395 | 28 | #include <linux/list.h> |
59df055f | 29 | #include <linux/hash.h> |
3d083395 | 30 | |
ad8d75ff | 31 | #include <trace/events/sched.h> |
8aef2d28 | 32 | |
395a59d0 | 33 | #include <asm/ftrace.h> |
2af15d6a | 34 | #include <asm/setup.h> |
395a59d0 | 35 | |
0706f1c4 | 36 | #include "trace_output.h" |
bac429f0 | 37 | #include "trace_stat.h" |
16444a8a | 38 | |
6912896e SR |
39 | #define FTRACE_WARN_ON(cond) \ |
40 | do { \ | |
41 | if (WARN_ON(cond)) \ | |
42 | ftrace_kill(); \ | |
43 | } while (0) | |
44 | ||
45 | #define FTRACE_WARN_ON_ONCE(cond) \ | |
46 | do { \ | |
47 | if (WARN_ON_ONCE(cond)) \ | |
48 | ftrace_kill(); \ | |
49 | } while (0) | |
50 | ||
8fc0c701 SR |
51 | /* hash bits for specific function selection */ |
52 | #define FTRACE_HASH_BITS 7 | |
53 | #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS) | |
54 | ||
4eebcc81 SR |
55 | /* ftrace_enabled is a method to turn ftrace on or off */ |
56 | int ftrace_enabled __read_mostly; | |
d61f82d0 | 57 | static int last_ftrace_enabled; |
b0fc494f | 58 | |
60a7ecf4 SR |
59 | /* Quick disabling of function tracer. */ |
60 | int function_trace_stop; | |
61 | ||
756d17ee | 62 | /* List for set_ftrace_pid's pids. */ |
63 | LIST_HEAD(ftrace_pids); | |
64 | struct ftrace_pid { | |
65 | struct list_head list; | |
66 | struct pid *pid; | |
67 | }; | |
68 | ||
4eebcc81 SR |
69 | /* |
70 | * ftrace_disabled is set when an anomaly is discovered. | |
71 | * ftrace_disabled is much stronger than ftrace_enabled. | |
72 | */ | |
73 | static int ftrace_disabled __read_mostly; | |
74 | ||
52baf119 | 75 | static DEFINE_MUTEX(ftrace_lock); |
b0fc494f | 76 | |
16444a8a ACM |
77 | static struct ftrace_ops ftrace_list_end __read_mostly = |
78 | { | |
fb9fb015 | 79 | .func = ftrace_stub, |
16444a8a ACM |
80 | }; |
81 | ||
82 | static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end; | |
83 | ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub; | |
60a7ecf4 | 84 | ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub; |
df4fc315 | 85 | ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub; |
16444a8a | 86 | |
f2252935 | 87 | static void ftrace_list_func(unsigned long ip, unsigned long parent_ip) |
16444a8a ACM |
88 | { |
89 | struct ftrace_ops *op = ftrace_list; | |
90 | ||
91 | /* in case someone actually ports this to alpha! */ | |
92 | read_barrier_depends(); | |
93 | ||
94 | while (op != &ftrace_list_end) { | |
95 | /* silly alpha */ | |
96 | read_barrier_depends(); | |
97 | op->func(ip, parent_ip); | |
98 | op = op->next; | |
99 | }; | |
100 | } | |
101 | ||
df4fc315 SR |
102 | static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip) |
103 | { | |
0ef8cde5 | 104 | if (!test_tsk_trace_trace(current)) |
df4fc315 SR |
105 | return; |
106 | ||
107 | ftrace_pid_function(ip, parent_ip); | |
108 | } | |
109 | ||
110 | static void set_ftrace_pid_function(ftrace_func_t func) | |
111 | { | |
112 | /* do not set ftrace_pid_function to itself! */ | |
113 | if (func != ftrace_pid_func) | |
114 | ftrace_pid_function = func; | |
115 | } | |
116 | ||
16444a8a | 117 | /** |
3d083395 | 118 | * clear_ftrace_function - reset the ftrace function |
16444a8a | 119 | * |
3d083395 SR |
120 | * This NULLs the ftrace function and in essence stops |
121 | * tracing. There may be lag | |
16444a8a | 122 | */ |
3d083395 | 123 | void clear_ftrace_function(void) |
16444a8a | 124 | { |
3d083395 | 125 | ftrace_trace_function = ftrace_stub; |
60a7ecf4 | 126 | __ftrace_trace_function = ftrace_stub; |
df4fc315 | 127 | ftrace_pid_function = ftrace_stub; |
3d083395 SR |
128 | } |
129 | ||
60a7ecf4 SR |
130 | #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST |
131 | /* | |
132 | * For those archs that do not test ftrace_trace_stop in their | |
133 | * mcount call site, we need to do it from C. | |
134 | */ | |
135 | static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip) | |
136 | { | |
137 | if (function_trace_stop) | |
138 | return; | |
139 | ||
140 | __ftrace_trace_function(ip, parent_ip); | |
141 | } | |
142 | #endif | |
143 | ||
e309b41d | 144 | static int __register_ftrace_function(struct ftrace_ops *ops) |
3d083395 | 145 | { |
16444a8a ACM |
146 | ops->next = ftrace_list; |
147 | /* | |
148 | * We are entering ops into the ftrace_list but another | |
149 | * CPU might be walking that list. We need to make sure | |
150 | * the ops->next pointer is valid before another CPU sees | |
151 | * the ops pointer included into the ftrace_list. | |
152 | */ | |
153 | smp_wmb(); | |
154 | ftrace_list = ops; | |
3d083395 | 155 | |
b0fc494f | 156 | if (ftrace_enabled) { |
df4fc315 SR |
157 | ftrace_func_t func; |
158 | ||
159 | if (ops->next == &ftrace_list_end) | |
160 | func = ops->func; | |
161 | else | |
162 | func = ftrace_list_func; | |
163 | ||
756d17ee | 164 | if (!list_empty(&ftrace_pids)) { |
df4fc315 SR |
165 | set_ftrace_pid_function(func); |
166 | func = ftrace_pid_func; | |
167 | } | |
168 | ||
b0fc494f SR |
169 | /* |
170 | * For one func, simply call it directly. | |
171 | * For more than one func, call the chain. | |
172 | */ | |
60a7ecf4 | 173 | #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST |
df4fc315 | 174 | ftrace_trace_function = func; |
60a7ecf4 | 175 | #else |
df4fc315 | 176 | __ftrace_trace_function = func; |
60a7ecf4 SR |
177 | ftrace_trace_function = ftrace_test_stop_func; |
178 | #endif | |
b0fc494f | 179 | } |
3d083395 | 180 | |
16444a8a ACM |
181 | return 0; |
182 | } | |
183 | ||
e309b41d | 184 | static int __unregister_ftrace_function(struct ftrace_ops *ops) |
16444a8a | 185 | { |
16444a8a | 186 | struct ftrace_ops **p; |
16444a8a ACM |
187 | |
188 | /* | |
3d083395 SR |
189 | * If we are removing the last function, then simply point |
190 | * to the ftrace_stub. | |
16444a8a ACM |
191 | */ |
192 | if (ftrace_list == ops && ops->next == &ftrace_list_end) { | |
193 | ftrace_trace_function = ftrace_stub; | |
194 | ftrace_list = &ftrace_list_end; | |
e6ea44e9 | 195 | return 0; |
16444a8a ACM |
196 | } |
197 | ||
198 | for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next) | |
199 | if (*p == ops) | |
200 | break; | |
201 | ||
e6ea44e9 SR |
202 | if (*p != ops) |
203 | return -1; | |
16444a8a ACM |
204 | |
205 | *p = (*p)->next; | |
206 | ||
b0fc494f SR |
207 | if (ftrace_enabled) { |
208 | /* If we only have one func left, then call that directly */ | |
df4fc315 SR |
209 | if (ftrace_list->next == &ftrace_list_end) { |
210 | ftrace_func_t func = ftrace_list->func; | |
211 | ||
756d17ee | 212 | if (!list_empty(&ftrace_pids)) { |
df4fc315 SR |
213 | set_ftrace_pid_function(func); |
214 | func = ftrace_pid_func; | |
215 | } | |
216 | #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST | |
217 | ftrace_trace_function = func; | |
218 | #else | |
219 | __ftrace_trace_function = func; | |
220 | #endif | |
221 | } | |
b0fc494f | 222 | } |
16444a8a | 223 | |
e6ea44e9 | 224 | return 0; |
3d083395 SR |
225 | } |
226 | ||
df4fc315 SR |
227 | static void ftrace_update_pid_func(void) |
228 | { | |
229 | ftrace_func_t func; | |
230 | ||
df4fc315 | 231 | if (ftrace_trace_function == ftrace_stub) |
10dd3ebe | 232 | return; |
df4fc315 | 233 | |
33974093 | 234 | #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST |
df4fc315 | 235 | func = ftrace_trace_function; |
33974093 MF |
236 | #else |
237 | func = __ftrace_trace_function; | |
238 | #endif | |
df4fc315 | 239 | |
756d17ee | 240 | if (!list_empty(&ftrace_pids)) { |
df4fc315 SR |
241 | set_ftrace_pid_function(func); |
242 | func = ftrace_pid_func; | |
243 | } else { | |
66eafebc LW |
244 | if (func == ftrace_pid_func) |
245 | func = ftrace_pid_function; | |
df4fc315 SR |
246 | } |
247 | ||
248 | #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST | |
249 | ftrace_trace_function = func; | |
250 | #else | |
251 | __ftrace_trace_function = func; | |
252 | #endif | |
df4fc315 SR |
253 | } |
254 | ||
493762fc SR |
255 | #ifdef CONFIG_FUNCTION_PROFILER |
256 | struct ftrace_profile { | |
257 | struct hlist_node node; | |
258 | unsigned long ip; | |
259 | unsigned long counter; | |
0706f1c4 SR |
260 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
261 | unsigned long long time; | |
262 | #endif | |
8fc0c701 SR |
263 | }; |
264 | ||
493762fc SR |
265 | struct ftrace_profile_page { |
266 | struct ftrace_profile_page *next; | |
267 | unsigned long index; | |
268 | struct ftrace_profile records[]; | |
d61f82d0 SR |
269 | }; |
270 | ||
cafb168a SR |
271 | struct ftrace_profile_stat { |
272 | atomic_t disabled; | |
273 | struct hlist_head *hash; | |
274 | struct ftrace_profile_page *pages; | |
275 | struct ftrace_profile_page *start; | |
276 | struct tracer_stat stat; | |
277 | }; | |
278 | ||
493762fc SR |
279 | #define PROFILE_RECORDS_SIZE \ |
280 | (PAGE_SIZE - offsetof(struct ftrace_profile_page, records)) | |
5072c59f | 281 | |
493762fc SR |
282 | #define PROFILES_PER_PAGE \ |
283 | (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile)) | |
3d083395 | 284 | |
fb9fb015 SR |
285 | static int ftrace_profile_bits __read_mostly; |
286 | static int ftrace_profile_enabled __read_mostly; | |
287 | ||
288 | /* ftrace_profile_lock - synchronize the enable and disable of the profiler */ | |
bac429f0 SR |
289 | static DEFINE_MUTEX(ftrace_profile_lock); |
290 | ||
cafb168a | 291 | static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats); |
493762fc SR |
292 | |
293 | #define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */ | |
294 | ||
bac429f0 SR |
295 | static void * |
296 | function_stat_next(void *v, int idx) | |
297 | { | |
493762fc SR |
298 | struct ftrace_profile *rec = v; |
299 | struct ftrace_profile_page *pg; | |
bac429f0 | 300 | |
493762fc | 301 | pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK); |
bac429f0 SR |
302 | |
303 | again: | |
0296e425 LZ |
304 | if (idx != 0) |
305 | rec++; | |
306 | ||
bac429f0 SR |
307 | if ((void *)rec >= (void *)&pg->records[pg->index]) { |
308 | pg = pg->next; | |
309 | if (!pg) | |
310 | return NULL; | |
311 | rec = &pg->records[0]; | |
493762fc SR |
312 | if (!rec->counter) |
313 | goto again; | |
bac429f0 SR |
314 | } |
315 | ||
bac429f0 SR |
316 | return rec; |
317 | } | |
318 | ||
319 | static void *function_stat_start(struct tracer_stat *trace) | |
320 | { | |
cafb168a SR |
321 | struct ftrace_profile_stat *stat = |
322 | container_of(trace, struct ftrace_profile_stat, stat); | |
323 | ||
324 | if (!stat || !stat->start) | |
325 | return NULL; | |
326 | ||
327 | return function_stat_next(&stat->start->records[0], 0); | |
bac429f0 SR |
328 | } |
329 | ||
0706f1c4 SR |
330 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
331 | /* function graph compares on total time */ | |
332 | static int function_stat_cmp(void *p1, void *p2) | |
333 | { | |
334 | struct ftrace_profile *a = p1; | |
335 | struct ftrace_profile *b = p2; | |
336 | ||
337 | if (a->time < b->time) | |
338 | return -1; | |
339 | if (a->time > b->time) | |
340 | return 1; | |
341 | else | |
342 | return 0; | |
343 | } | |
344 | #else | |
345 | /* not function graph compares against hits */ | |
bac429f0 SR |
346 | static int function_stat_cmp(void *p1, void *p2) |
347 | { | |
493762fc SR |
348 | struct ftrace_profile *a = p1; |
349 | struct ftrace_profile *b = p2; | |
bac429f0 SR |
350 | |
351 | if (a->counter < b->counter) | |
352 | return -1; | |
353 | if (a->counter > b->counter) | |
354 | return 1; | |
355 | else | |
356 | return 0; | |
357 | } | |
0706f1c4 | 358 | #endif |
bac429f0 SR |
359 | |
360 | static int function_stat_headers(struct seq_file *m) | |
361 | { | |
0706f1c4 | 362 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
34886c8b SR |
363 | seq_printf(m, " Function " |
364 | "Hit Time Avg\n" | |
365 | " -------- " | |
366 | "--- ---- ---\n"); | |
0706f1c4 | 367 | #else |
bac429f0 SR |
368 | seq_printf(m, " Function Hit\n" |
369 | " -------- ---\n"); | |
0706f1c4 | 370 | #endif |
bac429f0 SR |
371 | return 0; |
372 | } | |
373 | ||
374 | static int function_stat_show(struct seq_file *m, void *v) | |
375 | { | |
493762fc | 376 | struct ftrace_profile *rec = v; |
bac429f0 | 377 | char str[KSYM_SYMBOL_LEN]; |
0706f1c4 | 378 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
0706f1c4 | 379 | static DEFINE_MUTEX(mutex); |
34886c8b SR |
380 | static struct trace_seq s; |
381 | unsigned long long avg; | |
0706f1c4 | 382 | #endif |
bac429f0 SR |
383 | |
384 | kallsyms_lookup(rec->ip, NULL, NULL, NULL, str); | |
0706f1c4 SR |
385 | seq_printf(m, " %-30.30s %10lu", str, rec->counter); |
386 | ||
387 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER | |
388 | seq_printf(m, " "); | |
34886c8b SR |
389 | avg = rec->time; |
390 | do_div(avg, rec->counter); | |
391 | ||
392 | mutex_lock(&mutex); | |
393 | trace_seq_init(&s); | |
394 | trace_print_graph_duration(rec->time, &s); | |
395 | trace_seq_puts(&s, " "); | |
396 | trace_print_graph_duration(avg, &s); | |
0706f1c4 SR |
397 | trace_print_seq(m, &s); |
398 | mutex_unlock(&mutex); | |
399 | #endif | |
400 | seq_putc(m, '\n'); | |
bac429f0 | 401 | |
bac429f0 SR |
402 | return 0; |
403 | } | |
404 | ||
cafb168a | 405 | static void ftrace_profile_reset(struct ftrace_profile_stat *stat) |
bac429f0 | 406 | { |
493762fc | 407 | struct ftrace_profile_page *pg; |
bac429f0 | 408 | |
cafb168a | 409 | pg = stat->pages = stat->start; |
bac429f0 | 410 | |
493762fc SR |
411 | while (pg) { |
412 | memset(pg->records, 0, PROFILE_RECORDS_SIZE); | |
413 | pg->index = 0; | |
414 | pg = pg->next; | |
bac429f0 SR |
415 | } |
416 | ||
cafb168a | 417 | memset(stat->hash, 0, |
493762fc SR |
418 | FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head)); |
419 | } | |
bac429f0 | 420 | |
cafb168a | 421 | int ftrace_profile_pages_init(struct ftrace_profile_stat *stat) |
493762fc SR |
422 | { |
423 | struct ftrace_profile_page *pg; | |
318e0a73 SR |
424 | int functions; |
425 | int pages; | |
493762fc | 426 | int i; |
bac429f0 | 427 | |
493762fc | 428 | /* If we already allocated, do nothing */ |
cafb168a | 429 | if (stat->pages) |
493762fc | 430 | return 0; |
bac429f0 | 431 | |
cafb168a SR |
432 | stat->pages = (void *)get_zeroed_page(GFP_KERNEL); |
433 | if (!stat->pages) | |
493762fc | 434 | return -ENOMEM; |
bac429f0 | 435 | |
318e0a73 SR |
436 | #ifdef CONFIG_DYNAMIC_FTRACE |
437 | functions = ftrace_update_tot_cnt; | |
438 | #else | |
439 | /* | |
440 | * We do not know the number of functions that exist because | |
441 | * dynamic tracing is what counts them. With past experience | |
442 | * we have around 20K functions. That should be more than enough. | |
443 | * It is highly unlikely we will execute every function in | |
444 | * the kernel. | |
445 | */ | |
446 | functions = 20000; | |
447 | #endif | |
448 | ||
cafb168a | 449 | pg = stat->start = stat->pages; |
bac429f0 | 450 | |
318e0a73 SR |
451 | pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE); |
452 | ||
453 | for (i = 0; i < pages; i++) { | |
493762fc | 454 | pg->next = (void *)get_zeroed_page(GFP_KERNEL); |
493762fc | 455 | if (!pg->next) |
318e0a73 | 456 | goto out_free; |
493762fc SR |
457 | pg = pg->next; |
458 | } | |
459 | ||
460 | return 0; | |
318e0a73 SR |
461 | |
462 | out_free: | |
463 | pg = stat->start; | |
464 | while (pg) { | |
465 | unsigned long tmp = (unsigned long)pg; | |
466 | ||
467 | pg = pg->next; | |
468 | free_page(tmp); | |
469 | } | |
470 | ||
471 | free_page((unsigned long)stat->pages); | |
472 | stat->pages = NULL; | |
473 | stat->start = NULL; | |
474 | ||
475 | return -ENOMEM; | |
bac429f0 SR |
476 | } |
477 | ||
cafb168a | 478 | static int ftrace_profile_init_cpu(int cpu) |
bac429f0 | 479 | { |
cafb168a | 480 | struct ftrace_profile_stat *stat; |
493762fc | 481 | int size; |
bac429f0 | 482 | |
cafb168a SR |
483 | stat = &per_cpu(ftrace_profile_stats, cpu); |
484 | ||
485 | if (stat->hash) { | |
493762fc | 486 | /* If the profile is already created, simply reset it */ |
cafb168a | 487 | ftrace_profile_reset(stat); |
493762fc SR |
488 | return 0; |
489 | } | |
bac429f0 | 490 | |
493762fc SR |
491 | /* |
492 | * We are profiling all functions, but usually only a few thousand | |
493 | * functions are hit. We'll make a hash of 1024 items. | |
494 | */ | |
495 | size = FTRACE_PROFILE_HASH_SIZE; | |
bac429f0 | 496 | |
cafb168a | 497 | stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL); |
493762fc | 498 | |
cafb168a | 499 | if (!stat->hash) |
493762fc SR |
500 | return -ENOMEM; |
501 | ||
cafb168a SR |
502 | if (!ftrace_profile_bits) { |
503 | size--; | |
493762fc | 504 | |
cafb168a SR |
505 | for (; size; size >>= 1) |
506 | ftrace_profile_bits++; | |
507 | } | |
493762fc | 508 | |
318e0a73 | 509 | /* Preallocate the function profiling pages */ |
cafb168a SR |
510 | if (ftrace_profile_pages_init(stat) < 0) { |
511 | kfree(stat->hash); | |
512 | stat->hash = NULL; | |
493762fc SR |
513 | return -ENOMEM; |
514 | } | |
515 | ||
516 | return 0; | |
bac429f0 SR |
517 | } |
518 | ||
cafb168a SR |
519 | static int ftrace_profile_init(void) |
520 | { | |
521 | int cpu; | |
522 | int ret = 0; | |
523 | ||
524 | for_each_online_cpu(cpu) { | |
525 | ret = ftrace_profile_init_cpu(cpu); | |
526 | if (ret) | |
527 | break; | |
528 | } | |
529 | ||
530 | return ret; | |
531 | } | |
532 | ||
493762fc | 533 | /* interrupts must be disabled */ |
cafb168a SR |
534 | static struct ftrace_profile * |
535 | ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip) | |
bac429f0 | 536 | { |
493762fc | 537 | struct ftrace_profile *rec; |
bac429f0 SR |
538 | struct hlist_head *hhd; |
539 | struct hlist_node *n; | |
bac429f0 SR |
540 | unsigned long key; |
541 | ||
bac429f0 | 542 | key = hash_long(ip, ftrace_profile_bits); |
cafb168a | 543 | hhd = &stat->hash[key]; |
bac429f0 SR |
544 | |
545 | if (hlist_empty(hhd)) | |
546 | return NULL; | |
547 | ||
bac429f0 SR |
548 | hlist_for_each_entry_rcu(rec, n, hhd, node) { |
549 | if (rec->ip == ip) | |
493762fc SR |
550 | return rec; |
551 | } | |
552 | ||
553 | return NULL; | |
554 | } | |
555 | ||
cafb168a SR |
556 | static void ftrace_add_profile(struct ftrace_profile_stat *stat, |
557 | struct ftrace_profile *rec) | |
493762fc SR |
558 | { |
559 | unsigned long key; | |
560 | ||
561 | key = hash_long(rec->ip, ftrace_profile_bits); | |
cafb168a | 562 | hlist_add_head_rcu(&rec->node, &stat->hash[key]); |
493762fc SR |
563 | } |
564 | ||
318e0a73 SR |
565 | /* |
566 | * The memory is already allocated, this simply finds a new record to use. | |
567 | */ | |
493762fc | 568 | static struct ftrace_profile * |
318e0a73 | 569 | ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip) |
493762fc SR |
570 | { |
571 | struct ftrace_profile *rec = NULL; | |
572 | ||
318e0a73 | 573 | /* prevent recursion (from NMIs) */ |
cafb168a | 574 | if (atomic_inc_return(&stat->disabled) != 1) |
493762fc SR |
575 | goto out; |
576 | ||
493762fc | 577 | /* |
318e0a73 SR |
578 | * Try to find the function again since an NMI |
579 | * could have added it | |
493762fc | 580 | */ |
cafb168a | 581 | rec = ftrace_find_profiled_func(stat, ip); |
493762fc | 582 | if (rec) |
cafb168a | 583 | goto out; |
493762fc | 584 | |
cafb168a SR |
585 | if (stat->pages->index == PROFILES_PER_PAGE) { |
586 | if (!stat->pages->next) | |
587 | goto out; | |
588 | stat->pages = stat->pages->next; | |
bac429f0 | 589 | } |
493762fc | 590 | |
cafb168a | 591 | rec = &stat->pages->records[stat->pages->index++]; |
493762fc | 592 | rec->ip = ip; |
cafb168a | 593 | ftrace_add_profile(stat, rec); |
493762fc | 594 | |
bac429f0 | 595 | out: |
cafb168a | 596 | atomic_dec(&stat->disabled); |
bac429f0 SR |
597 | |
598 | return rec; | |
599 | } | |
600 | ||
601 | static void | |
602 | function_profile_call(unsigned long ip, unsigned long parent_ip) | |
603 | { | |
cafb168a | 604 | struct ftrace_profile_stat *stat; |
493762fc | 605 | struct ftrace_profile *rec; |
bac429f0 SR |
606 | unsigned long flags; |
607 | ||
608 | if (!ftrace_profile_enabled) | |
609 | return; | |
610 | ||
611 | local_irq_save(flags); | |
cafb168a SR |
612 | |
613 | stat = &__get_cpu_var(ftrace_profile_stats); | |
0f6ce3de | 614 | if (!stat->hash || !ftrace_profile_enabled) |
cafb168a SR |
615 | goto out; |
616 | ||
617 | rec = ftrace_find_profiled_func(stat, ip); | |
493762fc | 618 | if (!rec) { |
318e0a73 | 619 | rec = ftrace_profile_alloc(stat, ip); |
493762fc SR |
620 | if (!rec) |
621 | goto out; | |
622 | } | |
bac429f0 SR |
623 | |
624 | rec->counter++; | |
625 | out: | |
626 | local_irq_restore(flags); | |
627 | } | |
628 | ||
0706f1c4 SR |
629 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
630 | static int profile_graph_entry(struct ftrace_graph_ent *trace) | |
631 | { | |
632 | function_profile_call(trace->func, 0); | |
633 | return 1; | |
634 | } | |
635 | ||
636 | static void profile_graph_return(struct ftrace_graph_ret *trace) | |
637 | { | |
cafb168a | 638 | struct ftrace_profile_stat *stat; |
a2a16d6a | 639 | unsigned long long calltime; |
0706f1c4 | 640 | struct ftrace_profile *rec; |
cafb168a | 641 | unsigned long flags; |
0706f1c4 SR |
642 | |
643 | local_irq_save(flags); | |
cafb168a | 644 | stat = &__get_cpu_var(ftrace_profile_stats); |
0f6ce3de | 645 | if (!stat->hash || !ftrace_profile_enabled) |
cafb168a SR |
646 | goto out; |
647 | ||
a2a16d6a SR |
648 | calltime = trace->rettime - trace->calltime; |
649 | ||
650 | if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) { | |
651 | int index; | |
652 | ||
653 | index = trace->depth; | |
654 | ||
655 | /* Append this call time to the parent time to subtract */ | |
656 | if (index) | |
657 | current->ret_stack[index - 1].subtime += calltime; | |
658 | ||
659 | if (current->ret_stack[index].subtime < calltime) | |
660 | calltime -= current->ret_stack[index].subtime; | |
661 | else | |
662 | calltime = 0; | |
663 | } | |
664 | ||
cafb168a | 665 | rec = ftrace_find_profiled_func(stat, trace->func); |
0706f1c4 | 666 | if (rec) |
a2a16d6a SR |
667 | rec->time += calltime; |
668 | ||
cafb168a | 669 | out: |
0706f1c4 SR |
670 | local_irq_restore(flags); |
671 | } | |
672 | ||
673 | static int register_ftrace_profiler(void) | |
674 | { | |
675 | return register_ftrace_graph(&profile_graph_return, | |
676 | &profile_graph_entry); | |
677 | } | |
678 | ||
679 | static void unregister_ftrace_profiler(void) | |
680 | { | |
681 | unregister_ftrace_graph(); | |
682 | } | |
683 | #else | |
bac429f0 SR |
684 | static struct ftrace_ops ftrace_profile_ops __read_mostly = |
685 | { | |
fb9fb015 | 686 | .func = function_profile_call, |
bac429f0 SR |
687 | }; |
688 | ||
0706f1c4 SR |
689 | static int register_ftrace_profiler(void) |
690 | { | |
691 | return register_ftrace_function(&ftrace_profile_ops); | |
692 | } | |
693 | ||
694 | static void unregister_ftrace_profiler(void) | |
695 | { | |
696 | unregister_ftrace_function(&ftrace_profile_ops); | |
697 | } | |
698 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ | |
699 | ||
bac429f0 SR |
700 | static ssize_t |
701 | ftrace_profile_write(struct file *filp, const char __user *ubuf, | |
702 | size_t cnt, loff_t *ppos) | |
703 | { | |
704 | unsigned long val; | |
fb9fb015 | 705 | char buf[64]; /* big enough to hold a number */ |
bac429f0 SR |
706 | int ret; |
707 | ||
bac429f0 SR |
708 | if (cnt >= sizeof(buf)) |
709 | return -EINVAL; | |
710 | ||
711 | if (copy_from_user(&buf, ubuf, cnt)) | |
712 | return -EFAULT; | |
713 | ||
714 | buf[cnt] = 0; | |
715 | ||
716 | ret = strict_strtoul(buf, 10, &val); | |
717 | if (ret < 0) | |
718 | return ret; | |
719 | ||
720 | val = !!val; | |
721 | ||
722 | mutex_lock(&ftrace_profile_lock); | |
723 | if (ftrace_profile_enabled ^ val) { | |
724 | if (val) { | |
493762fc SR |
725 | ret = ftrace_profile_init(); |
726 | if (ret < 0) { | |
727 | cnt = ret; | |
728 | goto out; | |
729 | } | |
730 | ||
0706f1c4 SR |
731 | ret = register_ftrace_profiler(); |
732 | if (ret < 0) { | |
733 | cnt = ret; | |
734 | goto out; | |
735 | } | |
bac429f0 SR |
736 | ftrace_profile_enabled = 1; |
737 | } else { | |
738 | ftrace_profile_enabled = 0; | |
0f6ce3de SR |
739 | /* |
740 | * unregister_ftrace_profiler calls stop_machine | |
741 | * so this acts like an synchronize_sched. | |
742 | */ | |
0706f1c4 | 743 | unregister_ftrace_profiler(); |
bac429f0 SR |
744 | } |
745 | } | |
493762fc | 746 | out: |
bac429f0 SR |
747 | mutex_unlock(&ftrace_profile_lock); |
748 | ||
cf8517cf | 749 | *ppos += cnt; |
bac429f0 SR |
750 | |
751 | return cnt; | |
752 | } | |
753 | ||
493762fc SR |
754 | static ssize_t |
755 | ftrace_profile_read(struct file *filp, char __user *ubuf, | |
756 | size_t cnt, loff_t *ppos) | |
757 | { | |
fb9fb015 | 758 | char buf[64]; /* big enough to hold a number */ |
493762fc SR |
759 | int r; |
760 | ||
761 | r = sprintf(buf, "%u\n", ftrace_profile_enabled); | |
762 | return simple_read_from_buffer(ubuf, cnt, ppos, buf, r); | |
763 | } | |
764 | ||
bac429f0 SR |
765 | static const struct file_operations ftrace_profile_fops = { |
766 | .open = tracing_open_generic, | |
767 | .read = ftrace_profile_read, | |
768 | .write = ftrace_profile_write, | |
769 | }; | |
770 | ||
cafb168a SR |
771 | /* used to initialize the real stat files */ |
772 | static struct tracer_stat function_stats __initdata = { | |
fb9fb015 SR |
773 | .name = "functions", |
774 | .stat_start = function_stat_start, | |
775 | .stat_next = function_stat_next, | |
776 | .stat_cmp = function_stat_cmp, | |
777 | .stat_headers = function_stat_headers, | |
778 | .stat_show = function_stat_show | |
cafb168a SR |
779 | }; |
780 | ||
6ab5d668 | 781 | static __init void ftrace_profile_debugfs(struct dentry *d_tracer) |
bac429f0 | 782 | { |
cafb168a | 783 | struct ftrace_profile_stat *stat; |
bac429f0 | 784 | struct dentry *entry; |
cafb168a | 785 | char *name; |
bac429f0 | 786 | int ret; |
cafb168a SR |
787 | int cpu; |
788 | ||
789 | for_each_possible_cpu(cpu) { | |
790 | stat = &per_cpu(ftrace_profile_stats, cpu); | |
791 | ||
792 | /* allocate enough for function name + cpu number */ | |
793 | name = kmalloc(32, GFP_KERNEL); | |
794 | if (!name) { | |
795 | /* | |
796 | * The files created are permanent, if something happens | |
797 | * we still do not free memory. | |
798 | */ | |
cafb168a SR |
799 | WARN(1, |
800 | "Could not allocate stat file for cpu %d\n", | |
801 | cpu); | |
802 | return; | |
803 | } | |
804 | stat->stat = function_stats; | |
805 | snprintf(name, 32, "function%d", cpu); | |
806 | stat->stat.name = name; | |
807 | ret = register_stat_tracer(&stat->stat); | |
808 | if (ret) { | |
809 | WARN(1, | |
810 | "Could not register function stat for cpu %d\n", | |
811 | cpu); | |
812 | kfree(name); | |
813 | return; | |
814 | } | |
bac429f0 SR |
815 | } |
816 | ||
817 | entry = debugfs_create_file("function_profile_enabled", 0644, | |
818 | d_tracer, NULL, &ftrace_profile_fops); | |
819 | if (!entry) | |
820 | pr_warning("Could not create debugfs " | |
821 | "'function_profile_enabled' entry\n"); | |
822 | } | |
823 | ||
bac429f0 | 824 | #else /* CONFIG_FUNCTION_PROFILER */ |
6ab5d668 | 825 | static __init void ftrace_profile_debugfs(struct dentry *d_tracer) |
bac429f0 SR |
826 | { |
827 | } | |
bac429f0 SR |
828 | #endif /* CONFIG_FUNCTION_PROFILER */ |
829 | ||
493762fc SR |
830 | static struct pid * const ftrace_swapper_pid = &init_struct_pid; |
831 | ||
832 | #ifdef CONFIG_DYNAMIC_FTRACE | |
833 | ||
834 | #ifndef CONFIG_FTRACE_MCOUNT_RECORD | |
835 | # error Dynamic ftrace depends on MCOUNT_RECORD | |
836 | #endif | |
837 | ||
838 | static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly; | |
839 | ||
840 | struct ftrace_func_probe { | |
841 | struct hlist_node node; | |
842 | struct ftrace_probe_ops *ops; | |
843 | unsigned long flags; | |
844 | unsigned long ip; | |
845 | void *data; | |
846 | struct rcu_head rcu; | |
847 | }; | |
848 | ||
849 | enum { | |
850 | FTRACE_ENABLE_CALLS = (1 << 0), | |
851 | FTRACE_DISABLE_CALLS = (1 << 1), | |
852 | FTRACE_UPDATE_TRACE_FUNC = (1 << 2), | |
853 | FTRACE_ENABLE_MCOUNT = (1 << 3), | |
854 | FTRACE_DISABLE_MCOUNT = (1 << 4), | |
855 | FTRACE_START_FUNC_RET = (1 << 5), | |
856 | FTRACE_STOP_FUNC_RET = (1 << 6), | |
857 | }; | |
858 | ||
859 | static int ftrace_filtered; | |
860 | ||
861 | static struct dyn_ftrace *ftrace_new_addrs; | |
862 | ||
863 | static DEFINE_MUTEX(ftrace_regex_lock); | |
864 | ||
865 | struct ftrace_page { | |
866 | struct ftrace_page *next; | |
867 | int index; | |
868 | struct dyn_ftrace records[]; | |
869 | }; | |
870 | ||
871 | #define ENTRIES_PER_PAGE \ | |
872 | ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace)) | |
873 | ||
874 | /* estimate from running different kernels */ | |
875 | #define NR_TO_INIT 10000 | |
876 | ||
877 | static struct ftrace_page *ftrace_pages_start; | |
878 | static struct ftrace_page *ftrace_pages; | |
879 | ||
880 | static struct dyn_ftrace *ftrace_free_records; | |
881 | ||
882 | /* | |
883 | * This is a double for. Do not use 'break' to break out of the loop, | |
884 | * you must use a goto. | |
885 | */ | |
886 | #define do_for_each_ftrace_rec(pg, rec) \ | |
887 | for (pg = ftrace_pages_start; pg; pg = pg->next) { \ | |
888 | int _____i; \ | |
889 | for (_____i = 0; _____i < pg->index; _____i++) { \ | |
890 | rec = &pg->records[_____i]; | |
891 | ||
892 | #define while_for_each_ftrace_rec() \ | |
893 | } \ | |
894 | } | |
895 | ||
e309b41d | 896 | static void ftrace_free_rec(struct dyn_ftrace *rec) |
37ad5084 | 897 | { |
ee000b7f | 898 | rec->freelist = ftrace_free_records; |
37ad5084 SR |
899 | ftrace_free_records = rec; |
900 | rec->flags |= FTRACE_FL_FREE; | |
901 | } | |
902 | ||
e309b41d | 903 | static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip) |
3c1720f0 | 904 | { |
37ad5084 SR |
905 | struct dyn_ftrace *rec; |
906 | ||
907 | /* First check for freed records */ | |
908 | if (ftrace_free_records) { | |
909 | rec = ftrace_free_records; | |
910 | ||
37ad5084 | 911 | if (unlikely(!(rec->flags & FTRACE_FL_FREE))) { |
6912896e | 912 | FTRACE_WARN_ON_ONCE(1); |
37ad5084 SR |
913 | ftrace_free_records = NULL; |
914 | return NULL; | |
915 | } | |
916 | ||
ee000b7f | 917 | ftrace_free_records = rec->freelist; |
37ad5084 SR |
918 | memset(rec, 0, sizeof(*rec)); |
919 | return rec; | |
920 | } | |
921 | ||
3c1720f0 | 922 | if (ftrace_pages->index == ENTRIES_PER_PAGE) { |
08f5ac90 SR |
923 | if (!ftrace_pages->next) { |
924 | /* allocate another page */ | |
925 | ftrace_pages->next = | |
926 | (void *)get_zeroed_page(GFP_KERNEL); | |
927 | if (!ftrace_pages->next) | |
928 | return NULL; | |
929 | } | |
3c1720f0 SR |
930 | ftrace_pages = ftrace_pages->next; |
931 | } | |
932 | ||
933 | return &ftrace_pages->records[ftrace_pages->index++]; | |
934 | } | |
935 | ||
08f5ac90 | 936 | static struct dyn_ftrace * |
d61f82d0 | 937 | ftrace_record_ip(unsigned long ip) |
3d083395 | 938 | { |
08f5ac90 | 939 | struct dyn_ftrace *rec; |
3d083395 | 940 | |
f3c7ac40 | 941 | if (ftrace_disabled) |
08f5ac90 | 942 | return NULL; |
3d083395 | 943 | |
08f5ac90 SR |
944 | rec = ftrace_alloc_dyn_node(ip); |
945 | if (!rec) | |
946 | return NULL; | |
3d083395 | 947 | |
08f5ac90 | 948 | rec->ip = ip; |
ee000b7f | 949 | rec->newlist = ftrace_new_addrs; |
e94142a6 | 950 | ftrace_new_addrs = rec; |
3d083395 | 951 | |
08f5ac90 | 952 | return rec; |
3d083395 SR |
953 | } |
954 | ||
b17e8a37 SR |
955 | static void print_ip_ins(const char *fmt, unsigned char *p) |
956 | { | |
957 | int i; | |
958 | ||
959 | printk(KERN_CONT "%s", fmt); | |
960 | ||
961 | for (i = 0; i < MCOUNT_INSN_SIZE; i++) | |
962 | printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]); | |
963 | } | |
964 | ||
31e88909 | 965 | static void ftrace_bug(int failed, unsigned long ip) |
b17e8a37 SR |
966 | { |
967 | switch (failed) { | |
968 | case -EFAULT: | |
969 | FTRACE_WARN_ON_ONCE(1); | |
970 | pr_info("ftrace faulted on modifying "); | |
971 | print_ip_sym(ip); | |
972 | break; | |
973 | case -EINVAL: | |
974 | FTRACE_WARN_ON_ONCE(1); | |
975 | pr_info("ftrace failed to modify "); | |
976 | print_ip_sym(ip); | |
b17e8a37 | 977 | print_ip_ins(" actual: ", (unsigned char *)ip); |
b17e8a37 SR |
978 | printk(KERN_CONT "\n"); |
979 | break; | |
980 | case -EPERM: | |
981 | FTRACE_WARN_ON_ONCE(1); | |
982 | pr_info("ftrace faulted on writing "); | |
983 | print_ip_sym(ip); | |
984 | break; | |
985 | default: | |
986 | FTRACE_WARN_ON_ONCE(1); | |
987 | pr_info("ftrace faulted on unknown error "); | |
988 | print_ip_sym(ip); | |
989 | } | |
990 | } | |
991 | ||
3c1720f0 | 992 | |
2cfa1978 MH |
993 | /* Return 1 if the address range is reserved for ftrace */ |
994 | int ftrace_text_reserved(void *start, void *end) | |
995 | { | |
996 | struct dyn_ftrace *rec; | |
997 | struct ftrace_page *pg; | |
998 | ||
999 | do_for_each_ftrace_rec(pg, rec) { | |
1000 | if (rec->ip <= (unsigned long)end && | |
1001 | rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start) | |
1002 | return 1; | |
1003 | } while_for_each_ftrace_rec(); | |
1004 | return 0; | |
1005 | } | |
1006 | ||
1007 | ||
0eb96701 | 1008 | static int |
31e88909 | 1009 | __ftrace_replace_code(struct dyn_ftrace *rec, int enable) |
5072c59f | 1010 | { |
e7d3737e | 1011 | unsigned long ftrace_addr; |
64fbcd16 | 1012 | unsigned long flag = 0UL; |
e7d3737e | 1013 | |
f0001207 | 1014 | ftrace_addr = (unsigned long)FTRACE_ADDR; |
5072c59f | 1015 | |
982c350b | 1016 | /* |
64fbcd16 XG |
1017 | * If this record is not to be traced or we want to disable it, |
1018 | * then disable it. | |
982c350b | 1019 | * |
64fbcd16 | 1020 | * If we want to enable it and filtering is off, then enable it. |
982c350b | 1021 | * |
64fbcd16 XG |
1022 | * If we want to enable it and filtering is on, enable it only if |
1023 | * it's filtered | |
982c350b | 1024 | */ |
64fbcd16 XG |
1025 | if (enable && !(rec->flags & FTRACE_FL_NOTRACE)) { |
1026 | if (!ftrace_filtered || (rec->flags & FTRACE_FL_FILTER)) | |
1027 | flag = FTRACE_FL_ENABLED; | |
1028 | } | |
982c350b | 1029 | |
64fbcd16 XG |
1030 | /* If the state of this record hasn't changed, then do nothing */ |
1031 | if ((rec->flags & FTRACE_FL_ENABLED) == flag) | |
1032 | return 0; | |
982c350b | 1033 | |
64fbcd16 XG |
1034 | if (flag) { |
1035 | rec->flags |= FTRACE_FL_ENABLED; | |
1036 | return ftrace_make_call(rec, ftrace_addr); | |
5072c59f SR |
1037 | } |
1038 | ||
64fbcd16 XG |
1039 | rec->flags &= ~FTRACE_FL_ENABLED; |
1040 | return ftrace_make_nop(NULL, rec, ftrace_addr); | |
5072c59f SR |
1041 | } |
1042 | ||
e309b41d | 1043 | static void ftrace_replace_code(int enable) |
3c1720f0 | 1044 | { |
3c1720f0 SR |
1045 | struct dyn_ftrace *rec; |
1046 | struct ftrace_page *pg; | |
6a24a244 | 1047 | int failed; |
3c1720f0 | 1048 | |
265c831c SR |
1049 | do_for_each_ftrace_rec(pg, rec) { |
1050 | /* | |
fa9d13cf Z |
1051 | * Skip over free records, records that have |
1052 | * failed and not converted. | |
265c831c SR |
1053 | */ |
1054 | if (rec->flags & FTRACE_FL_FREE || | |
fa9d13cf | 1055 | rec->flags & FTRACE_FL_FAILED || |
03303549 | 1056 | !(rec->flags & FTRACE_FL_CONVERTED)) |
265c831c SR |
1057 | continue; |
1058 | ||
265c831c | 1059 | failed = __ftrace_replace_code(rec, enable); |
fa9d13cf | 1060 | if (failed) { |
265c831c | 1061 | rec->flags |= FTRACE_FL_FAILED; |
3279ba37 SR |
1062 | ftrace_bug(failed, rec->ip); |
1063 | /* Stop processing */ | |
1064 | return; | |
3c1720f0 | 1065 | } |
265c831c | 1066 | } while_for_each_ftrace_rec(); |
3c1720f0 SR |
1067 | } |
1068 | ||
492a7ea5 | 1069 | static int |
31e88909 | 1070 | ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec) |
3c1720f0 SR |
1071 | { |
1072 | unsigned long ip; | |
593eb8a2 | 1073 | int ret; |
3c1720f0 SR |
1074 | |
1075 | ip = rec->ip; | |
1076 | ||
25aac9dc | 1077 | ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR); |
593eb8a2 | 1078 | if (ret) { |
31e88909 | 1079 | ftrace_bug(ret, ip); |
3c1720f0 | 1080 | rec->flags |= FTRACE_FL_FAILED; |
492a7ea5 | 1081 | return 0; |
37ad5084 | 1082 | } |
492a7ea5 | 1083 | return 1; |
3c1720f0 SR |
1084 | } |
1085 | ||
000ab691 SR |
1086 | /* |
1087 | * archs can override this function if they must do something | |
1088 | * before the modifying code is performed. | |
1089 | */ | |
1090 | int __weak ftrace_arch_code_modify_prepare(void) | |
1091 | { | |
1092 | return 0; | |
1093 | } | |
1094 | ||
1095 | /* | |
1096 | * archs can override this function if they must do something | |
1097 | * after the modifying code is performed. | |
1098 | */ | |
1099 | int __weak ftrace_arch_code_modify_post_process(void) | |
1100 | { | |
1101 | return 0; | |
1102 | } | |
1103 | ||
e309b41d | 1104 | static int __ftrace_modify_code(void *data) |
3d083395 | 1105 | { |
d61f82d0 SR |
1106 | int *command = data; |
1107 | ||
a3583244 | 1108 | if (*command & FTRACE_ENABLE_CALLS) |
d61f82d0 | 1109 | ftrace_replace_code(1); |
a3583244 | 1110 | else if (*command & FTRACE_DISABLE_CALLS) |
d61f82d0 SR |
1111 | ftrace_replace_code(0); |
1112 | ||
1113 | if (*command & FTRACE_UPDATE_TRACE_FUNC) | |
1114 | ftrace_update_ftrace_func(ftrace_trace_function); | |
1115 | ||
5a45cfe1 SR |
1116 | if (*command & FTRACE_START_FUNC_RET) |
1117 | ftrace_enable_ftrace_graph_caller(); | |
1118 | else if (*command & FTRACE_STOP_FUNC_RET) | |
1119 | ftrace_disable_ftrace_graph_caller(); | |
1120 | ||
d61f82d0 | 1121 | return 0; |
3d083395 SR |
1122 | } |
1123 | ||
e309b41d | 1124 | static void ftrace_run_update_code(int command) |
3d083395 | 1125 | { |
000ab691 SR |
1126 | int ret; |
1127 | ||
1128 | ret = ftrace_arch_code_modify_prepare(); | |
1129 | FTRACE_WARN_ON(ret); | |
1130 | if (ret) | |
1131 | return; | |
1132 | ||
784e2d76 | 1133 | stop_machine(__ftrace_modify_code, &command, NULL); |
000ab691 SR |
1134 | |
1135 | ret = ftrace_arch_code_modify_post_process(); | |
1136 | FTRACE_WARN_ON(ret); | |
3d083395 SR |
1137 | } |
1138 | ||
d61f82d0 | 1139 | static ftrace_func_t saved_ftrace_func; |
60a7ecf4 | 1140 | static int ftrace_start_up; |
df4fc315 SR |
1141 | |
1142 | static void ftrace_startup_enable(int command) | |
1143 | { | |
1144 | if (saved_ftrace_func != ftrace_trace_function) { | |
1145 | saved_ftrace_func = ftrace_trace_function; | |
1146 | command |= FTRACE_UPDATE_TRACE_FUNC; | |
1147 | } | |
1148 | ||
1149 | if (!command || !ftrace_enabled) | |
1150 | return; | |
1151 | ||
1152 | ftrace_run_update_code(command); | |
1153 | } | |
d61f82d0 | 1154 | |
5a45cfe1 | 1155 | static void ftrace_startup(int command) |
3d083395 | 1156 | { |
4eebcc81 SR |
1157 | if (unlikely(ftrace_disabled)) |
1158 | return; | |
1159 | ||
60a7ecf4 | 1160 | ftrace_start_up++; |
982c350b | 1161 | command |= FTRACE_ENABLE_CALLS; |
d61f82d0 | 1162 | |
df4fc315 | 1163 | ftrace_startup_enable(command); |
3d083395 SR |
1164 | } |
1165 | ||
5a45cfe1 | 1166 | static void ftrace_shutdown(int command) |
3d083395 | 1167 | { |
4eebcc81 SR |
1168 | if (unlikely(ftrace_disabled)) |
1169 | return; | |
1170 | ||
60a7ecf4 | 1171 | ftrace_start_up--; |
9ea1a153 FW |
1172 | /* |
1173 | * Just warn in case of unbalance, no need to kill ftrace, it's not | |
1174 | * critical but the ftrace_call callers may be never nopped again after | |
1175 | * further ftrace uses. | |
1176 | */ | |
1177 | WARN_ON_ONCE(ftrace_start_up < 0); | |
1178 | ||
60a7ecf4 | 1179 | if (!ftrace_start_up) |
d61f82d0 | 1180 | command |= FTRACE_DISABLE_CALLS; |
3d083395 | 1181 | |
d61f82d0 SR |
1182 | if (saved_ftrace_func != ftrace_trace_function) { |
1183 | saved_ftrace_func = ftrace_trace_function; | |
1184 | command |= FTRACE_UPDATE_TRACE_FUNC; | |
1185 | } | |
3d083395 | 1186 | |
d61f82d0 | 1187 | if (!command || !ftrace_enabled) |
e6ea44e9 | 1188 | return; |
d61f82d0 SR |
1189 | |
1190 | ftrace_run_update_code(command); | |
3d083395 SR |
1191 | } |
1192 | ||
e309b41d | 1193 | static void ftrace_startup_sysctl(void) |
b0fc494f | 1194 | { |
d61f82d0 SR |
1195 | int command = FTRACE_ENABLE_MCOUNT; |
1196 | ||
4eebcc81 SR |
1197 | if (unlikely(ftrace_disabled)) |
1198 | return; | |
1199 | ||
d61f82d0 SR |
1200 | /* Force update next time */ |
1201 | saved_ftrace_func = NULL; | |
60a7ecf4 SR |
1202 | /* ftrace_start_up is true if we want ftrace running */ |
1203 | if (ftrace_start_up) | |
d61f82d0 SR |
1204 | command |= FTRACE_ENABLE_CALLS; |
1205 | ||
1206 | ftrace_run_update_code(command); | |
b0fc494f SR |
1207 | } |
1208 | ||
e309b41d | 1209 | static void ftrace_shutdown_sysctl(void) |
b0fc494f | 1210 | { |
d61f82d0 SR |
1211 | int command = FTRACE_DISABLE_MCOUNT; |
1212 | ||
4eebcc81 SR |
1213 | if (unlikely(ftrace_disabled)) |
1214 | return; | |
1215 | ||
60a7ecf4 SR |
1216 | /* ftrace_start_up is true if ftrace is running */ |
1217 | if (ftrace_start_up) | |
d61f82d0 SR |
1218 | command |= FTRACE_DISABLE_CALLS; |
1219 | ||
1220 | ftrace_run_update_code(command); | |
b0fc494f SR |
1221 | } |
1222 | ||
3d083395 SR |
1223 | static cycle_t ftrace_update_time; |
1224 | static unsigned long ftrace_update_cnt; | |
1225 | unsigned long ftrace_update_tot_cnt; | |
1226 | ||
31e88909 | 1227 | static int ftrace_update_code(struct module *mod) |
3d083395 | 1228 | { |
e94142a6 | 1229 | struct dyn_ftrace *p; |
f22f9a89 | 1230 | cycle_t start, stop; |
3d083395 | 1231 | |
750ed1a4 | 1232 | start = ftrace_now(raw_smp_processor_id()); |
3d083395 SR |
1233 | ftrace_update_cnt = 0; |
1234 | ||
e94142a6 | 1235 | while (ftrace_new_addrs) { |
3d083395 | 1236 | |
08f5ac90 SR |
1237 | /* If something went wrong, bail without enabling anything */ |
1238 | if (unlikely(ftrace_disabled)) | |
1239 | return -1; | |
f22f9a89 | 1240 | |
e94142a6 | 1241 | p = ftrace_new_addrs; |
ee000b7f | 1242 | ftrace_new_addrs = p->newlist; |
e94142a6 | 1243 | p->flags = 0L; |
f22f9a89 | 1244 | |
5cb084bb JO |
1245 | /* |
1246 | * Do the initial record convertion from mcount jump | |
1247 | * to the NOP instructions. | |
1248 | */ | |
1249 | if (!ftrace_code_disable(mod, p)) { | |
08f5ac90 | 1250 | ftrace_free_rec(p); |
5cb084bb JO |
1251 | continue; |
1252 | } | |
1253 | ||
1254 | p->flags |= FTRACE_FL_CONVERTED; | |
1255 | ftrace_update_cnt++; | |
1256 | ||
1257 | /* | |
1258 | * If the tracing is enabled, go ahead and enable the record. | |
1259 | * | |
1260 | * The reason not to enable the record immediatelly is the | |
1261 | * inherent check of ftrace_make_nop/ftrace_make_call for | |
1262 | * correct previous instructions. Making first the NOP | |
1263 | * conversion puts the module to the correct state, thus | |
1264 | * passing the ftrace_make_call check. | |
1265 | */ | |
1266 | if (ftrace_start_up) { | |
1267 | int failed = __ftrace_replace_code(p, 1); | |
1268 | if (failed) { | |
1269 | ftrace_bug(failed, p->ip); | |
1270 | ftrace_free_rec(p); | |
1271 | } | |
1272 | } | |
3d083395 SR |
1273 | } |
1274 | ||
750ed1a4 | 1275 | stop = ftrace_now(raw_smp_processor_id()); |
3d083395 SR |
1276 | ftrace_update_time = stop - start; |
1277 | ftrace_update_tot_cnt += ftrace_update_cnt; | |
1278 | ||
16444a8a ACM |
1279 | return 0; |
1280 | } | |
1281 | ||
68bf21aa | 1282 | static int __init ftrace_dyn_table_alloc(unsigned long num_to_init) |
3c1720f0 SR |
1283 | { |
1284 | struct ftrace_page *pg; | |
1285 | int cnt; | |
1286 | int i; | |
3c1720f0 SR |
1287 | |
1288 | /* allocate a few pages */ | |
1289 | ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL); | |
1290 | if (!ftrace_pages_start) | |
1291 | return -1; | |
1292 | ||
1293 | /* | |
1294 | * Allocate a few more pages. | |
1295 | * | |
1296 | * TODO: have some parser search vmlinux before | |
1297 | * final linking to find all calls to ftrace. | |
1298 | * Then we can: | |
1299 | * a) know how many pages to allocate. | |
1300 | * and/or | |
1301 | * b) set up the table then. | |
1302 | * | |
1303 | * The dynamic code is still necessary for | |
1304 | * modules. | |
1305 | */ | |
1306 | ||
1307 | pg = ftrace_pages = ftrace_pages_start; | |
1308 | ||
68bf21aa | 1309 | cnt = num_to_init / ENTRIES_PER_PAGE; |
08f5ac90 | 1310 | pr_info("ftrace: allocating %ld entries in %d pages\n", |
5821e1b7 | 1311 | num_to_init, cnt + 1); |
3c1720f0 SR |
1312 | |
1313 | for (i = 0; i < cnt; i++) { | |
1314 | pg->next = (void *)get_zeroed_page(GFP_KERNEL); | |
1315 | ||
1316 | /* If we fail, we'll try later anyway */ | |
1317 | if (!pg->next) | |
1318 | break; | |
1319 | ||
1320 | pg = pg->next; | |
1321 | } | |
1322 | ||
1323 | return 0; | |
1324 | } | |
1325 | ||
5072c59f SR |
1326 | enum { |
1327 | FTRACE_ITER_FILTER = (1 << 0), | |
689fd8b6 | 1328 | FTRACE_ITER_NOTRACE = (1 << 1), |
1329 | FTRACE_ITER_FAILURES = (1 << 2), | |
1330 | FTRACE_ITER_PRINTALL = (1 << 3), | |
1331 | FTRACE_ITER_HASH = (1 << 4), | |
5072c59f SR |
1332 | }; |
1333 | ||
1334 | #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */ | |
1335 | ||
1336 | struct ftrace_iterator { | |
5072c59f | 1337 | struct ftrace_page *pg; |
8fc0c701 | 1338 | int hidx; |
431aa3fb | 1339 | int idx; |
5072c59f | 1340 | unsigned flags; |
689fd8b6 | 1341 | struct trace_parser parser; |
5072c59f SR |
1342 | }; |
1343 | ||
8fc0c701 SR |
1344 | static void * |
1345 | t_hash_next(struct seq_file *m, void *v, loff_t *pos) | |
1346 | { | |
1347 | struct ftrace_iterator *iter = m->private; | |
1348 | struct hlist_node *hnd = v; | |
1349 | struct hlist_head *hhd; | |
1350 | ||
1351 | WARN_ON(!(iter->flags & FTRACE_ITER_HASH)); | |
1352 | ||
1353 | (*pos)++; | |
1354 | ||
1355 | retry: | |
1356 | if (iter->hidx >= FTRACE_FUNC_HASHSIZE) | |
1357 | return NULL; | |
1358 | ||
1359 | hhd = &ftrace_func_hash[iter->hidx]; | |
1360 | ||
1361 | if (hlist_empty(hhd)) { | |
1362 | iter->hidx++; | |
1363 | hnd = NULL; | |
1364 | goto retry; | |
1365 | } | |
1366 | ||
1367 | if (!hnd) | |
1368 | hnd = hhd->first; | |
1369 | else { | |
1370 | hnd = hnd->next; | |
1371 | if (!hnd) { | |
1372 | iter->hidx++; | |
1373 | goto retry; | |
1374 | } | |
1375 | } | |
1376 | ||
1377 | return hnd; | |
1378 | } | |
1379 | ||
1380 | static void *t_hash_start(struct seq_file *m, loff_t *pos) | |
1381 | { | |
1382 | struct ftrace_iterator *iter = m->private; | |
1383 | void *p = NULL; | |
d82d6244 LZ |
1384 | loff_t l; |
1385 | ||
1386 | if (!(iter->flags & FTRACE_ITER_HASH)) | |
1387 | *pos = 0; | |
8fc0c701 SR |
1388 | |
1389 | iter->flags |= FTRACE_ITER_HASH; | |
1390 | ||
d82d6244 LZ |
1391 | iter->hidx = 0; |
1392 | for (l = 0; l <= *pos; ) { | |
1393 | p = t_hash_next(m, p, &l); | |
1394 | if (!p) | |
1395 | break; | |
1396 | } | |
1397 | return p; | |
8fc0c701 SR |
1398 | } |
1399 | ||
1400 | static int t_hash_show(struct seq_file *m, void *v) | |
1401 | { | |
b6887d79 | 1402 | struct ftrace_func_probe *rec; |
8fc0c701 | 1403 | struct hlist_node *hnd = v; |
8fc0c701 | 1404 | |
b6887d79 | 1405 | rec = hlist_entry(hnd, struct ftrace_func_probe, node); |
8fc0c701 | 1406 | |
809dcf29 SR |
1407 | if (rec->ops->print) |
1408 | return rec->ops->print(m, rec->ip, rec->ops, rec->data); | |
1409 | ||
b375a11a | 1410 | seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func); |
8fc0c701 SR |
1411 | |
1412 | if (rec->data) | |
1413 | seq_printf(m, ":%p", rec->data); | |
1414 | seq_putc(m, '\n'); | |
1415 | ||
1416 | return 0; | |
1417 | } | |
1418 | ||
e309b41d | 1419 | static void * |
5072c59f SR |
1420 | t_next(struct seq_file *m, void *v, loff_t *pos) |
1421 | { | |
1422 | struct ftrace_iterator *iter = m->private; | |
1423 | struct dyn_ftrace *rec = NULL; | |
1424 | ||
8fc0c701 SR |
1425 | if (iter->flags & FTRACE_ITER_HASH) |
1426 | return t_hash_next(m, v, pos); | |
1427 | ||
5072c59f SR |
1428 | (*pos)++; |
1429 | ||
0c75a3ed SR |
1430 | if (iter->flags & FTRACE_ITER_PRINTALL) |
1431 | return NULL; | |
1432 | ||
5072c59f SR |
1433 | retry: |
1434 | if (iter->idx >= iter->pg->index) { | |
1435 | if (iter->pg->next) { | |
1436 | iter->pg = iter->pg->next; | |
1437 | iter->idx = 0; | |
1438 | goto retry; | |
1439 | } | |
1440 | } else { | |
1441 | rec = &iter->pg->records[iter->idx++]; | |
a9fdda33 SR |
1442 | if ((rec->flags & FTRACE_FL_FREE) || |
1443 | ||
1444 | (!(iter->flags & FTRACE_ITER_FAILURES) && | |
eb9a7bf0 AS |
1445 | (rec->flags & FTRACE_FL_FAILED)) || |
1446 | ||
1447 | ((iter->flags & FTRACE_ITER_FAILURES) && | |
a9fdda33 | 1448 | !(rec->flags & FTRACE_FL_FAILED)) || |
eb9a7bf0 | 1449 | |
0183fb1c SR |
1450 | ((iter->flags & FTRACE_ITER_FILTER) && |
1451 | !(rec->flags & FTRACE_FL_FILTER)) || | |
1452 | ||
41c52c0d SR |
1453 | ((iter->flags & FTRACE_ITER_NOTRACE) && |
1454 | !(rec->flags & FTRACE_FL_NOTRACE))) { | |
5072c59f SR |
1455 | rec = NULL; |
1456 | goto retry; | |
1457 | } | |
1458 | } | |
1459 | ||
5072c59f SR |
1460 | return rec; |
1461 | } | |
1462 | ||
1463 | static void *t_start(struct seq_file *m, loff_t *pos) | |
1464 | { | |
1465 | struct ftrace_iterator *iter = m->private; | |
1466 | void *p = NULL; | |
694ce0a5 | 1467 | loff_t l; |
5072c59f | 1468 | |
8fc0c701 | 1469 | mutex_lock(&ftrace_lock); |
0c75a3ed SR |
1470 | /* |
1471 | * For set_ftrace_filter reading, if we have the filter | |
1472 | * off, we can short cut and just print out that all | |
1473 | * functions are enabled. | |
1474 | */ | |
1475 | if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) { | |
1476 | if (*pos > 0) | |
8fc0c701 | 1477 | return t_hash_start(m, pos); |
0c75a3ed | 1478 | iter->flags |= FTRACE_ITER_PRINTALL; |
0c75a3ed SR |
1479 | return iter; |
1480 | } | |
1481 | ||
8fc0c701 SR |
1482 | if (iter->flags & FTRACE_ITER_HASH) |
1483 | return t_hash_start(m, pos); | |
1484 | ||
694ce0a5 LZ |
1485 | iter->pg = ftrace_pages_start; |
1486 | iter->idx = 0; | |
1487 | for (l = 0; l <= *pos; ) { | |
1488 | p = t_next(m, p, &l); | |
1489 | if (!p) | |
1490 | break; | |
50cdaf08 | 1491 | } |
5821e1b7 | 1492 | |
694ce0a5 | 1493 | if (!p && iter->flags & FTRACE_ITER_FILTER) |
8fc0c701 SR |
1494 | return t_hash_start(m, pos); |
1495 | ||
5072c59f SR |
1496 | return p; |
1497 | } | |
1498 | ||
1499 | static void t_stop(struct seq_file *m, void *p) | |
1500 | { | |
8fc0c701 | 1501 | mutex_unlock(&ftrace_lock); |
5072c59f SR |
1502 | } |
1503 | ||
1504 | static int t_show(struct seq_file *m, void *v) | |
1505 | { | |
0c75a3ed | 1506 | struct ftrace_iterator *iter = m->private; |
5072c59f | 1507 | struct dyn_ftrace *rec = v; |
5072c59f | 1508 | |
8fc0c701 SR |
1509 | if (iter->flags & FTRACE_ITER_HASH) |
1510 | return t_hash_show(m, v); | |
1511 | ||
0c75a3ed SR |
1512 | if (iter->flags & FTRACE_ITER_PRINTALL) { |
1513 | seq_printf(m, "#### all functions enabled ####\n"); | |
1514 | return 0; | |
1515 | } | |
1516 | ||
5072c59f SR |
1517 | if (!rec) |
1518 | return 0; | |
1519 | ||
b375a11a | 1520 | seq_printf(m, "%ps\n", (void *)rec->ip); |
5072c59f SR |
1521 | |
1522 | return 0; | |
1523 | } | |
1524 | ||
88e9d34c | 1525 | static const struct seq_operations show_ftrace_seq_ops = { |
5072c59f SR |
1526 | .start = t_start, |
1527 | .next = t_next, | |
1528 | .stop = t_stop, | |
1529 | .show = t_show, | |
1530 | }; | |
1531 | ||
e309b41d | 1532 | static int |
5072c59f SR |
1533 | ftrace_avail_open(struct inode *inode, struct file *file) |
1534 | { | |
1535 | struct ftrace_iterator *iter; | |
1536 | int ret; | |
1537 | ||
4eebcc81 SR |
1538 | if (unlikely(ftrace_disabled)) |
1539 | return -ENODEV; | |
1540 | ||
5072c59f SR |
1541 | iter = kzalloc(sizeof(*iter), GFP_KERNEL); |
1542 | if (!iter) | |
1543 | return -ENOMEM; | |
1544 | ||
1545 | iter->pg = ftrace_pages_start; | |
5072c59f SR |
1546 | |
1547 | ret = seq_open(file, &show_ftrace_seq_ops); | |
1548 | if (!ret) { | |
1549 | struct seq_file *m = file->private_data; | |
4bf39a94 | 1550 | |
5072c59f | 1551 | m->private = iter; |
4bf39a94 | 1552 | } else { |
5072c59f | 1553 | kfree(iter); |
4bf39a94 | 1554 | } |
5072c59f SR |
1555 | |
1556 | return ret; | |
1557 | } | |
1558 | ||
eb9a7bf0 AS |
1559 | static int |
1560 | ftrace_failures_open(struct inode *inode, struct file *file) | |
1561 | { | |
1562 | int ret; | |
1563 | struct seq_file *m; | |
1564 | struct ftrace_iterator *iter; | |
1565 | ||
1566 | ret = ftrace_avail_open(inode, file); | |
1567 | if (!ret) { | |
1568 | m = (struct seq_file *)file->private_data; | |
1569 | iter = (struct ftrace_iterator *)m->private; | |
1570 | iter->flags = FTRACE_ITER_FAILURES; | |
1571 | } | |
1572 | ||
1573 | return ret; | |
1574 | } | |
1575 | ||
1576 | ||
41c52c0d | 1577 | static void ftrace_filter_reset(int enable) |
5072c59f SR |
1578 | { |
1579 | struct ftrace_page *pg; | |
1580 | struct dyn_ftrace *rec; | |
41c52c0d | 1581 | unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE; |
5072c59f | 1582 | |
52baf119 | 1583 | mutex_lock(&ftrace_lock); |
41c52c0d SR |
1584 | if (enable) |
1585 | ftrace_filtered = 0; | |
265c831c SR |
1586 | do_for_each_ftrace_rec(pg, rec) { |
1587 | if (rec->flags & FTRACE_FL_FAILED) | |
1588 | continue; | |
1589 | rec->flags &= ~type; | |
1590 | } while_for_each_ftrace_rec(); | |
52baf119 | 1591 | mutex_unlock(&ftrace_lock); |
5072c59f SR |
1592 | } |
1593 | ||
e309b41d | 1594 | static int |
41c52c0d | 1595 | ftrace_regex_open(struct inode *inode, struct file *file, int enable) |
5072c59f SR |
1596 | { |
1597 | struct ftrace_iterator *iter; | |
1598 | int ret = 0; | |
1599 | ||
4eebcc81 SR |
1600 | if (unlikely(ftrace_disabled)) |
1601 | return -ENODEV; | |
1602 | ||
5072c59f SR |
1603 | iter = kzalloc(sizeof(*iter), GFP_KERNEL); |
1604 | if (!iter) | |
1605 | return -ENOMEM; | |
1606 | ||
689fd8b6 | 1607 | if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) { |
1608 | kfree(iter); | |
1609 | return -ENOMEM; | |
1610 | } | |
1611 | ||
41c52c0d | 1612 | mutex_lock(&ftrace_regex_lock); |
5072c59f | 1613 | if ((file->f_mode & FMODE_WRITE) && |
8650ae32 | 1614 | (file->f_flags & O_TRUNC)) |
41c52c0d | 1615 | ftrace_filter_reset(enable); |
5072c59f SR |
1616 | |
1617 | if (file->f_mode & FMODE_READ) { | |
1618 | iter->pg = ftrace_pages_start; | |
41c52c0d SR |
1619 | iter->flags = enable ? FTRACE_ITER_FILTER : |
1620 | FTRACE_ITER_NOTRACE; | |
5072c59f SR |
1621 | |
1622 | ret = seq_open(file, &show_ftrace_seq_ops); | |
1623 | if (!ret) { | |
1624 | struct seq_file *m = file->private_data; | |
1625 | m->private = iter; | |
79fe249c LZ |
1626 | } else { |
1627 | trace_parser_put(&iter->parser); | |
5072c59f | 1628 | kfree(iter); |
79fe249c | 1629 | } |
5072c59f SR |
1630 | } else |
1631 | file->private_data = iter; | |
41c52c0d | 1632 | mutex_unlock(&ftrace_regex_lock); |
5072c59f SR |
1633 | |
1634 | return ret; | |
1635 | } | |
1636 | ||
41c52c0d SR |
1637 | static int |
1638 | ftrace_filter_open(struct inode *inode, struct file *file) | |
1639 | { | |
1640 | return ftrace_regex_open(inode, file, 1); | |
1641 | } | |
1642 | ||
1643 | static int | |
1644 | ftrace_notrace_open(struct inode *inode, struct file *file) | |
1645 | { | |
1646 | return ftrace_regex_open(inode, file, 0); | |
1647 | } | |
1648 | ||
e309b41d | 1649 | static loff_t |
41c52c0d | 1650 | ftrace_regex_lseek(struct file *file, loff_t offset, int origin) |
5072c59f SR |
1651 | { |
1652 | loff_t ret; | |
1653 | ||
1654 | if (file->f_mode & FMODE_READ) | |
1655 | ret = seq_lseek(file, offset, origin); | |
1656 | else | |
1657 | file->f_pos = ret = 1; | |
1658 | ||
1659 | return ret; | |
1660 | } | |
1661 | ||
64e7c440 | 1662 | static int ftrace_match(char *str, char *regex, int len, int type) |
9f4801e3 | 1663 | { |
9f4801e3 | 1664 | int matched = 0; |
751e9983 | 1665 | int slen; |
9f4801e3 | 1666 | |
9f4801e3 SR |
1667 | switch (type) { |
1668 | case MATCH_FULL: | |
1669 | if (strcmp(str, regex) == 0) | |
1670 | matched = 1; | |
1671 | break; | |
1672 | case MATCH_FRONT_ONLY: | |
1673 | if (strncmp(str, regex, len) == 0) | |
1674 | matched = 1; | |
1675 | break; | |
1676 | case MATCH_MIDDLE_ONLY: | |
1677 | if (strstr(str, regex)) | |
1678 | matched = 1; | |
1679 | break; | |
1680 | case MATCH_END_ONLY: | |
751e9983 LZ |
1681 | slen = strlen(str); |
1682 | if (slen >= len && memcmp(str + slen - len, regex, len) == 0) | |
9f4801e3 SR |
1683 | matched = 1; |
1684 | break; | |
1685 | } | |
1686 | ||
1687 | return matched; | |
1688 | } | |
1689 | ||
64e7c440 SR |
1690 | static int |
1691 | ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type) | |
1692 | { | |
1693 | char str[KSYM_SYMBOL_LEN]; | |
1694 | ||
1695 | kallsyms_lookup(rec->ip, NULL, NULL, NULL, str); | |
1696 | return ftrace_match(str, regex, len, type); | |
1697 | } | |
1698 | ||
311d16da | 1699 | static int ftrace_match_records(char *buff, int len, int enable) |
9f4801e3 | 1700 | { |
6a24a244 | 1701 | unsigned int search_len; |
9f4801e3 SR |
1702 | struct ftrace_page *pg; |
1703 | struct dyn_ftrace *rec; | |
6a24a244 SR |
1704 | unsigned long flag; |
1705 | char *search; | |
9f4801e3 | 1706 | int type; |
9f4801e3 | 1707 | int not; |
311d16da | 1708 | int found = 0; |
9f4801e3 | 1709 | |
6a24a244 | 1710 | flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE; |
3f6fe06d | 1711 | type = filter_parse_regex(buff, len, &search, ¬); |
9f4801e3 SR |
1712 | |
1713 | search_len = strlen(search); | |
1714 | ||
52baf119 | 1715 | mutex_lock(&ftrace_lock); |
265c831c | 1716 | do_for_each_ftrace_rec(pg, rec) { |
265c831c SR |
1717 | |
1718 | if (rec->flags & FTRACE_FL_FAILED) | |
1719 | continue; | |
9f4801e3 SR |
1720 | |
1721 | if (ftrace_match_record(rec, search, search_len, type)) { | |
265c831c SR |
1722 | if (not) |
1723 | rec->flags &= ~flag; | |
1724 | else | |
1725 | rec->flags |= flag; | |
311d16da | 1726 | found = 1; |
265c831c | 1727 | } |
e68746a2 SR |
1728 | /* |
1729 | * Only enable filtering if we have a function that | |
1730 | * is filtered on. | |
1731 | */ | |
1732 | if (enable && (rec->flags & FTRACE_FL_FILTER)) | |
1733 | ftrace_filtered = 1; | |
265c831c | 1734 | } while_for_each_ftrace_rec(); |
52baf119 | 1735 | mutex_unlock(&ftrace_lock); |
311d16da LZ |
1736 | |
1737 | return found; | |
5072c59f SR |
1738 | } |
1739 | ||
64e7c440 SR |
1740 | static int |
1741 | ftrace_match_module_record(struct dyn_ftrace *rec, char *mod, | |
1742 | char *regex, int len, int type) | |
1743 | { | |
1744 | char str[KSYM_SYMBOL_LEN]; | |
1745 | char *modname; | |
1746 | ||
1747 | kallsyms_lookup(rec->ip, NULL, NULL, &modname, str); | |
1748 | ||
1749 | if (!modname || strcmp(modname, mod)) | |
1750 | return 0; | |
1751 | ||
1752 | /* blank search means to match all funcs in the mod */ | |
1753 | if (len) | |
1754 | return ftrace_match(str, regex, len, type); | |
1755 | else | |
1756 | return 1; | |
1757 | } | |
1758 | ||
311d16da | 1759 | static int ftrace_match_module_records(char *buff, char *mod, int enable) |
64e7c440 | 1760 | { |
6a24a244 | 1761 | unsigned search_len = 0; |
64e7c440 SR |
1762 | struct ftrace_page *pg; |
1763 | struct dyn_ftrace *rec; | |
1764 | int type = MATCH_FULL; | |
6a24a244 SR |
1765 | char *search = buff; |
1766 | unsigned long flag; | |
64e7c440 | 1767 | int not = 0; |
311d16da | 1768 | int found = 0; |
64e7c440 | 1769 | |
6a24a244 SR |
1770 | flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE; |
1771 | ||
64e7c440 SR |
1772 | /* blank or '*' mean the same */ |
1773 | if (strcmp(buff, "*") == 0) | |
1774 | buff[0] = 0; | |
1775 | ||
1776 | /* handle the case of 'dont filter this module' */ | |
1777 | if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) { | |
1778 | buff[0] = 0; | |
1779 | not = 1; | |
1780 | } | |
1781 | ||
1782 | if (strlen(buff)) { | |
3f6fe06d | 1783 | type = filter_parse_regex(buff, strlen(buff), &search, ¬); |
64e7c440 SR |
1784 | search_len = strlen(search); |
1785 | } | |
1786 | ||
52baf119 | 1787 | mutex_lock(&ftrace_lock); |
64e7c440 SR |
1788 | do_for_each_ftrace_rec(pg, rec) { |
1789 | ||
1790 | if (rec->flags & FTRACE_FL_FAILED) | |
1791 | continue; | |
1792 | ||
1793 | if (ftrace_match_module_record(rec, mod, | |
1794 | search, search_len, type)) { | |
1795 | if (not) | |
1796 | rec->flags &= ~flag; | |
1797 | else | |
1798 | rec->flags |= flag; | |
311d16da | 1799 | found = 1; |
64e7c440 | 1800 | } |
e68746a2 SR |
1801 | if (enable && (rec->flags & FTRACE_FL_FILTER)) |
1802 | ftrace_filtered = 1; | |
64e7c440 SR |
1803 | |
1804 | } while_for_each_ftrace_rec(); | |
52baf119 | 1805 | mutex_unlock(&ftrace_lock); |
311d16da LZ |
1806 | |
1807 | return found; | |
64e7c440 SR |
1808 | } |
1809 | ||
f6180773 SR |
1810 | /* |
1811 | * We register the module command as a template to show others how | |
1812 | * to register the a command as well. | |
1813 | */ | |
1814 | ||
1815 | static int | |
1816 | ftrace_mod_callback(char *func, char *cmd, char *param, int enable) | |
1817 | { | |
1818 | char *mod; | |
1819 | ||
1820 | /* | |
1821 | * cmd == 'mod' because we only registered this func | |
1822 | * for the 'mod' ftrace_func_command. | |
1823 | * But if you register one func with multiple commands, | |
1824 | * you can tell which command was used by the cmd | |
1825 | * parameter. | |
1826 | */ | |
1827 | ||
1828 | /* we must have a module name */ | |
1829 | if (!param) | |
1830 | return -EINVAL; | |
1831 | ||
1832 | mod = strsep(¶m, ":"); | |
1833 | if (!strlen(mod)) | |
1834 | return -EINVAL; | |
1835 | ||
311d16da LZ |
1836 | if (ftrace_match_module_records(func, mod, enable)) |
1837 | return 0; | |
1838 | return -EINVAL; | |
f6180773 SR |
1839 | } |
1840 | ||
1841 | static struct ftrace_func_command ftrace_mod_cmd = { | |
1842 | .name = "mod", | |
1843 | .func = ftrace_mod_callback, | |
1844 | }; | |
1845 | ||
1846 | static int __init ftrace_mod_cmd_init(void) | |
1847 | { | |
1848 | return register_ftrace_command(&ftrace_mod_cmd); | |
1849 | } | |
1850 | device_initcall(ftrace_mod_cmd_init); | |
1851 | ||
59df055f | 1852 | static void |
b6887d79 | 1853 | function_trace_probe_call(unsigned long ip, unsigned long parent_ip) |
59df055f | 1854 | { |
b6887d79 | 1855 | struct ftrace_func_probe *entry; |
59df055f SR |
1856 | struct hlist_head *hhd; |
1857 | struct hlist_node *n; | |
1858 | unsigned long key; | |
1859 | int resched; | |
1860 | ||
1861 | key = hash_long(ip, FTRACE_HASH_BITS); | |
1862 | ||
1863 | hhd = &ftrace_func_hash[key]; | |
1864 | ||
1865 | if (hlist_empty(hhd)) | |
1866 | return; | |
1867 | ||
1868 | /* | |
1869 | * Disable preemption for these calls to prevent a RCU grace | |
1870 | * period. This syncs the hash iteration and freeing of items | |
1871 | * on the hash. rcu_read_lock is too dangerous here. | |
1872 | */ | |
1873 | resched = ftrace_preempt_disable(); | |
1874 | hlist_for_each_entry_rcu(entry, n, hhd, node) { | |
1875 | if (entry->ip == ip) | |
1876 | entry->ops->func(ip, parent_ip, &entry->data); | |
1877 | } | |
1878 | ftrace_preempt_enable(resched); | |
1879 | } | |
1880 | ||
b6887d79 | 1881 | static struct ftrace_ops trace_probe_ops __read_mostly = |
59df055f | 1882 | { |
fb9fb015 | 1883 | .func = function_trace_probe_call, |
59df055f SR |
1884 | }; |
1885 | ||
b6887d79 | 1886 | static int ftrace_probe_registered; |
59df055f | 1887 | |
b6887d79 | 1888 | static void __enable_ftrace_function_probe(void) |
59df055f SR |
1889 | { |
1890 | int i; | |
1891 | ||
b6887d79 | 1892 | if (ftrace_probe_registered) |
59df055f SR |
1893 | return; |
1894 | ||
1895 | for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) { | |
1896 | struct hlist_head *hhd = &ftrace_func_hash[i]; | |
1897 | if (hhd->first) | |
1898 | break; | |
1899 | } | |
1900 | /* Nothing registered? */ | |
1901 | if (i == FTRACE_FUNC_HASHSIZE) | |
1902 | return; | |
1903 | ||
b6887d79 | 1904 | __register_ftrace_function(&trace_probe_ops); |
59df055f | 1905 | ftrace_startup(0); |
b6887d79 | 1906 | ftrace_probe_registered = 1; |
59df055f SR |
1907 | } |
1908 | ||
b6887d79 | 1909 | static void __disable_ftrace_function_probe(void) |
59df055f SR |
1910 | { |
1911 | int i; | |
1912 | ||
b6887d79 | 1913 | if (!ftrace_probe_registered) |
59df055f SR |
1914 | return; |
1915 | ||
1916 | for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) { | |
1917 | struct hlist_head *hhd = &ftrace_func_hash[i]; | |
1918 | if (hhd->first) | |
1919 | return; | |
1920 | } | |
1921 | ||
1922 | /* no more funcs left */ | |
b6887d79 | 1923 | __unregister_ftrace_function(&trace_probe_ops); |
59df055f | 1924 | ftrace_shutdown(0); |
b6887d79 | 1925 | ftrace_probe_registered = 0; |
59df055f SR |
1926 | } |
1927 | ||
1928 | ||
1929 | static void ftrace_free_entry_rcu(struct rcu_head *rhp) | |
1930 | { | |
b6887d79 SR |
1931 | struct ftrace_func_probe *entry = |
1932 | container_of(rhp, struct ftrace_func_probe, rcu); | |
59df055f SR |
1933 | |
1934 | if (entry->ops->free) | |
1935 | entry->ops->free(&entry->data); | |
1936 | kfree(entry); | |
1937 | } | |
1938 | ||
1939 | ||
1940 | int | |
b6887d79 | 1941 | register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops, |
59df055f SR |
1942 | void *data) |
1943 | { | |
b6887d79 | 1944 | struct ftrace_func_probe *entry; |
59df055f SR |
1945 | struct ftrace_page *pg; |
1946 | struct dyn_ftrace *rec; | |
59df055f | 1947 | int type, len, not; |
6a24a244 | 1948 | unsigned long key; |
59df055f SR |
1949 | int count = 0; |
1950 | char *search; | |
1951 | ||
3f6fe06d | 1952 | type = filter_parse_regex(glob, strlen(glob), &search, ¬); |
59df055f SR |
1953 | len = strlen(search); |
1954 | ||
b6887d79 | 1955 | /* we do not support '!' for function probes */ |
59df055f SR |
1956 | if (WARN_ON(not)) |
1957 | return -EINVAL; | |
1958 | ||
1959 | mutex_lock(&ftrace_lock); | |
1960 | do_for_each_ftrace_rec(pg, rec) { | |
1961 | ||
1962 | if (rec->flags & FTRACE_FL_FAILED) | |
1963 | continue; | |
1964 | ||
1965 | if (!ftrace_match_record(rec, search, len, type)) | |
1966 | continue; | |
1967 | ||
1968 | entry = kmalloc(sizeof(*entry), GFP_KERNEL); | |
1969 | if (!entry) { | |
b6887d79 | 1970 | /* If we did not process any, then return error */ |
59df055f SR |
1971 | if (!count) |
1972 | count = -ENOMEM; | |
1973 | goto out_unlock; | |
1974 | } | |
1975 | ||
1976 | count++; | |
1977 | ||
1978 | entry->data = data; | |
1979 | ||
1980 | /* | |
1981 | * The caller might want to do something special | |
1982 | * for each function we find. We call the callback | |
1983 | * to give the caller an opportunity to do so. | |
1984 | */ | |
1985 | if (ops->callback) { | |
1986 | if (ops->callback(rec->ip, &entry->data) < 0) { | |
1987 | /* caller does not like this func */ | |
1988 | kfree(entry); | |
1989 | continue; | |
1990 | } | |
1991 | } | |
1992 | ||
1993 | entry->ops = ops; | |
1994 | entry->ip = rec->ip; | |
1995 | ||
1996 | key = hash_long(entry->ip, FTRACE_HASH_BITS); | |
1997 | hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]); | |
1998 | ||
1999 | } while_for_each_ftrace_rec(); | |
b6887d79 | 2000 | __enable_ftrace_function_probe(); |
59df055f SR |
2001 | |
2002 | out_unlock: | |
2003 | mutex_unlock(&ftrace_lock); | |
2004 | ||
2005 | return count; | |
2006 | } | |
2007 | ||
2008 | enum { | |
b6887d79 SR |
2009 | PROBE_TEST_FUNC = 1, |
2010 | PROBE_TEST_DATA = 2 | |
59df055f SR |
2011 | }; |
2012 | ||
2013 | static void | |
b6887d79 | 2014 | __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops, |
59df055f SR |
2015 | void *data, int flags) |
2016 | { | |
b6887d79 | 2017 | struct ftrace_func_probe *entry; |
59df055f SR |
2018 | struct hlist_node *n, *tmp; |
2019 | char str[KSYM_SYMBOL_LEN]; | |
2020 | int type = MATCH_FULL; | |
2021 | int i, len = 0; | |
2022 | char *search; | |
2023 | ||
b36461da | 2024 | if (glob && (strcmp(glob, "*") == 0 || !strlen(glob))) |
59df055f | 2025 | glob = NULL; |
b36461da | 2026 | else if (glob) { |
59df055f SR |
2027 | int not; |
2028 | ||
3f6fe06d | 2029 | type = filter_parse_regex(glob, strlen(glob), &search, ¬); |
59df055f SR |
2030 | len = strlen(search); |
2031 | ||
b6887d79 | 2032 | /* we do not support '!' for function probes */ |
59df055f SR |
2033 | if (WARN_ON(not)) |
2034 | return; | |
2035 | } | |
2036 | ||
2037 | mutex_lock(&ftrace_lock); | |
2038 | for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) { | |
2039 | struct hlist_head *hhd = &ftrace_func_hash[i]; | |
2040 | ||
2041 | hlist_for_each_entry_safe(entry, n, tmp, hhd, node) { | |
2042 | ||
2043 | /* break up if statements for readability */ | |
b6887d79 | 2044 | if ((flags & PROBE_TEST_FUNC) && entry->ops != ops) |
59df055f SR |
2045 | continue; |
2046 | ||
b6887d79 | 2047 | if ((flags & PROBE_TEST_DATA) && entry->data != data) |
59df055f SR |
2048 | continue; |
2049 | ||
2050 | /* do this last, since it is the most expensive */ | |
2051 | if (glob) { | |
2052 | kallsyms_lookup(entry->ip, NULL, NULL, | |
2053 | NULL, str); | |
2054 | if (!ftrace_match(str, glob, len, type)) | |
2055 | continue; | |
2056 | } | |
2057 | ||
2058 | hlist_del(&entry->node); | |
2059 | call_rcu(&entry->rcu, ftrace_free_entry_rcu); | |
2060 | } | |
2061 | } | |
b6887d79 | 2062 | __disable_ftrace_function_probe(); |
59df055f SR |
2063 | mutex_unlock(&ftrace_lock); |
2064 | } | |
2065 | ||
2066 | void | |
b6887d79 | 2067 | unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops, |
59df055f SR |
2068 | void *data) |
2069 | { | |
b6887d79 SR |
2070 | __unregister_ftrace_function_probe(glob, ops, data, |
2071 | PROBE_TEST_FUNC | PROBE_TEST_DATA); | |
59df055f SR |
2072 | } |
2073 | ||
2074 | void | |
b6887d79 | 2075 | unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops) |
59df055f | 2076 | { |
b6887d79 | 2077 | __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC); |
59df055f SR |
2078 | } |
2079 | ||
b6887d79 | 2080 | void unregister_ftrace_function_probe_all(char *glob) |
59df055f | 2081 | { |
b6887d79 | 2082 | __unregister_ftrace_function_probe(glob, NULL, NULL, 0); |
59df055f SR |
2083 | } |
2084 | ||
f6180773 SR |
2085 | static LIST_HEAD(ftrace_commands); |
2086 | static DEFINE_MUTEX(ftrace_cmd_mutex); | |
2087 | ||
2088 | int register_ftrace_command(struct ftrace_func_command *cmd) | |
2089 | { | |
2090 | struct ftrace_func_command *p; | |
2091 | int ret = 0; | |
2092 | ||
2093 | mutex_lock(&ftrace_cmd_mutex); | |
2094 | list_for_each_entry(p, &ftrace_commands, list) { | |
2095 | if (strcmp(cmd->name, p->name) == 0) { | |
2096 | ret = -EBUSY; | |
2097 | goto out_unlock; | |
2098 | } | |
2099 | } | |
2100 | list_add(&cmd->list, &ftrace_commands); | |
2101 | out_unlock: | |
2102 | mutex_unlock(&ftrace_cmd_mutex); | |
2103 | ||
2104 | return ret; | |
2105 | } | |
2106 | ||
2107 | int unregister_ftrace_command(struct ftrace_func_command *cmd) | |
2108 | { | |
2109 | struct ftrace_func_command *p, *n; | |
2110 | int ret = -ENODEV; | |
2111 | ||
2112 | mutex_lock(&ftrace_cmd_mutex); | |
2113 | list_for_each_entry_safe(p, n, &ftrace_commands, list) { | |
2114 | if (strcmp(cmd->name, p->name) == 0) { | |
2115 | ret = 0; | |
2116 | list_del_init(&p->list); | |
2117 | goto out_unlock; | |
2118 | } | |
2119 | } | |
2120 | out_unlock: | |
2121 | mutex_unlock(&ftrace_cmd_mutex); | |
2122 | ||
2123 | return ret; | |
2124 | } | |
2125 | ||
64e7c440 SR |
2126 | static int ftrace_process_regex(char *buff, int len, int enable) |
2127 | { | |
f6180773 | 2128 | char *func, *command, *next = buff; |
6a24a244 | 2129 | struct ftrace_func_command *p; |
f6180773 | 2130 | int ret = -EINVAL; |
64e7c440 SR |
2131 | |
2132 | func = strsep(&next, ":"); | |
2133 | ||
2134 | if (!next) { | |
311d16da LZ |
2135 | if (ftrace_match_records(func, len, enable)) |
2136 | return 0; | |
2137 | return ret; | |
64e7c440 SR |
2138 | } |
2139 | ||
f6180773 | 2140 | /* command found */ |
64e7c440 SR |
2141 | |
2142 | command = strsep(&next, ":"); | |
2143 | ||
f6180773 SR |
2144 | mutex_lock(&ftrace_cmd_mutex); |
2145 | list_for_each_entry(p, &ftrace_commands, list) { | |
2146 | if (strcmp(p->name, command) == 0) { | |
2147 | ret = p->func(func, command, next, enable); | |
2148 | goto out_unlock; | |
2149 | } | |
64e7c440 | 2150 | } |
f6180773 SR |
2151 | out_unlock: |
2152 | mutex_unlock(&ftrace_cmd_mutex); | |
64e7c440 | 2153 | |
f6180773 | 2154 | return ret; |
64e7c440 SR |
2155 | } |
2156 | ||
e309b41d | 2157 | static ssize_t |
41c52c0d SR |
2158 | ftrace_regex_write(struct file *file, const char __user *ubuf, |
2159 | size_t cnt, loff_t *ppos, int enable) | |
5072c59f SR |
2160 | { |
2161 | struct ftrace_iterator *iter; | |
689fd8b6 | 2162 | struct trace_parser *parser; |
2163 | ssize_t ret, read; | |
5072c59f | 2164 | |
4ba7978e | 2165 | if (!cnt) |
5072c59f SR |
2166 | return 0; |
2167 | ||
41c52c0d | 2168 | mutex_lock(&ftrace_regex_lock); |
5072c59f SR |
2169 | |
2170 | if (file->f_mode & FMODE_READ) { | |
2171 | struct seq_file *m = file->private_data; | |
2172 | iter = m->private; | |
2173 | } else | |
2174 | iter = file->private_data; | |
2175 | ||
689fd8b6 | 2176 | parser = &iter->parser; |
2177 | read = trace_get_user(parser, ubuf, cnt, ppos); | |
5072c59f | 2178 | |
4ba7978e | 2179 | if (read >= 0 && trace_parser_loaded(parser) && |
689fd8b6 | 2180 | !trace_parser_cont(parser)) { |
2181 | ret = ftrace_process_regex(parser->buffer, | |
2182 | parser->idx, enable); | |
313254a9 | 2183 | trace_parser_clear(parser); |
5072c59f | 2184 | if (ret) |
ed146b25 | 2185 | goto out_unlock; |
eda1e328 | 2186 | } |
5072c59f | 2187 | |
5072c59f | 2188 | ret = read; |
ed146b25 | 2189 | out_unlock: |
689fd8b6 | 2190 | mutex_unlock(&ftrace_regex_lock); |
ed146b25 | 2191 | |
5072c59f SR |
2192 | return ret; |
2193 | } | |
2194 | ||
41c52c0d SR |
2195 | static ssize_t |
2196 | ftrace_filter_write(struct file *file, const char __user *ubuf, | |
2197 | size_t cnt, loff_t *ppos) | |
2198 | { | |
2199 | return ftrace_regex_write(file, ubuf, cnt, ppos, 1); | |
2200 | } | |
2201 | ||
2202 | static ssize_t | |
2203 | ftrace_notrace_write(struct file *file, const char __user *ubuf, | |
2204 | size_t cnt, loff_t *ppos) | |
2205 | { | |
2206 | return ftrace_regex_write(file, ubuf, cnt, ppos, 0); | |
2207 | } | |
2208 | ||
2209 | static void | |
2210 | ftrace_set_regex(unsigned char *buf, int len, int reset, int enable) | |
2211 | { | |
2212 | if (unlikely(ftrace_disabled)) | |
2213 | return; | |
2214 | ||
2215 | mutex_lock(&ftrace_regex_lock); | |
2216 | if (reset) | |
2217 | ftrace_filter_reset(enable); | |
2218 | if (buf) | |
7f24b31b | 2219 | ftrace_match_records(buf, len, enable); |
41c52c0d SR |
2220 | mutex_unlock(&ftrace_regex_lock); |
2221 | } | |
2222 | ||
77a2b37d SR |
2223 | /** |
2224 | * ftrace_set_filter - set a function to filter on in ftrace | |
2225 | * @buf - the string that holds the function filter text. | |
2226 | * @len - the length of the string. | |
2227 | * @reset - non zero to reset all filters before applying this filter. | |
2228 | * | |
2229 | * Filters denote which functions should be enabled when tracing is enabled. | |
2230 | * If @buf is NULL and reset is set, all functions will be enabled for tracing. | |
2231 | */ | |
e309b41d | 2232 | void ftrace_set_filter(unsigned char *buf, int len, int reset) |
77a2b37d | 2233 | { |
41c52c0d SR |
2234 | ftrace_set_regex(buf, len, reset, 1); |
2235 | } | |
4eebcc81 | 2236 | |
41c52c0d SR |
2237 | /** |
2238 | * ftrace_set_notrace - set a function to not trace in ftrace | |
2239 | * @buf - the string that holds the function notrace text. | |
2240 | * @len - the length of the string. | |
2241 | * @reset - non zero to reset all filters before applying this filter. | |
2242 | * | |
2243 | * Notrace Filters denote which functions should not be enabled when tracing | |
2244 | * is enabled. If @buf is NULL and reset is set, all functions will be enabled | |
2245 | * for tracing. | |
2246 | */ | |
2247 | void ftrace_set_notrace(unsigned char *buf, int len, int reset) | |
2248 | { | |
2249 | ftrace_set_regex(buf, len, reset, 0); | |
77a2b37d SR |
2250 | } |
2251 | ||
2af15d6a SR |
2252 | /* |
2253 | * command line interface to allow users to set filters on boot up. | |
2254 | */ | |
2255 | #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE | |
2256 | static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata; | |
2257 | static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata; | |
2258 | ||
2259 | static int __init set_ftrace_notrace(char *str) | |
2260 | { | |
2261 | strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE); | |
2262 | return 1; | |
2263 | } | |
2264 | __setup("ftrace_notrace=", set_ftrace_notrace); | |
2265 | ||
2266 | static int __init set_ftrace_filter(char *str) | |
2267 | { | |
2268 | strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE); | |
2269 | return 1; | |
2270 | } | |
2271 | __setup("ftrace_filter=", set_ftrace_filter); | |
2272 | ||
369bc18f | 2273 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
f6060f46 | 2274 | static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata; |
801c29fd SR |
2275 | static int ftrace_set_func(unsigned long *array, int *idx, char *buffer); |
2276 | ||
369bc18f SA |
2277 | static int __init set_graph_function(char *str) |
2278 | { | |
06f43d66 | 2279 | strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE); |
369bc18f SA |
2280 | return 1; |
2281 | } | |
2282 | __setup("ftrace_graph_filter=", set_graph_function); | |
2283 | ||
2284 | static void __init set_ftrace_early_graph(char *buf) | |
2285 | { | |
2286 | int ret; | |
2287 | char *func; | |
2288 | ||
2289 | while (buf) { | |
2290 | func = strsep(&buf, ","); | |
2291 | /* we allow only one expression at a time */ | |
2292 | ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count, | |
2293 | func); | |
2294 | if (ret) | |
2295 | printk(KERN_DEBUG "ftrace: function %s not " | |
2296 | "traceable\n", func); | |
2297 | } | |
2298 | } | |
2299 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ | |
2300 | ||
2af15d6a SR |
2301 | static void __init set_ftrace_early_filter(char *buf, int enable) |
2302 | { | |
2303 | char *func; | |
2304 | ||
2305 | while (buf) { | |
2306 | func = strsep(&buf, ","); | |
2307 | ftrace_set_regex(func, strlen(func), 0, enable); | |
2308 | } | |
2309 | } | |
2310 | ||
2311 | static void __init set_ftrace_early_filters(void) | |
2312 | { | |
2313 | if (ftrace_filter_buf[0]) | |
2314 | set_ftrace_early_filter(ftrace_filter_buf, 1); | |
2315 | if (ftrace_notrace_buf[0]) | |
2316 | set_ftrace_early_filter(ftrace_notrace_buf, 0); | |
369bc18f SA |
2317 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
2318 | if (ftrace_graph_buf[0]) | |
2319 | set_ftrace_early_graph(ftrace_graph_buf); | |
2320 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ | |
2af15d6a SR |
2321 | } |
2322 | ||
e309b41d | 2323 | static int |
41c52c0d | 2324 | ftrace_regex_release(struct inode *inode, struct file *file, int enable) |
5072c59f SR |
2325 | { |
2326 | struct seq_file *m = (struct seq_file *)file->private_data; | |
2327 | struct ftrace_iterator *iter; | |
689fd8b6 | 2328 | struct trace_parser *parser; |
5072c59f | 2329 | |
41c52c0d | 2330 | mutex_lock(&ftrace_regex_lock); |
5072c59f SR |
2331 | if (file->f_mode & FMODE_READ) { |
2332 | iter = m->private; | |
2333 | ||
2334 | seq_release(inode, file); | |
2335 | } else | |
2336 | iter = file->private_data; | |
2337 | ||
689fd8b6 | 2338 | parser = &iter->parser; |
2339 | if (trace_parser_loaded(parser)) { | |
2340 | parser->buffer[parser->idx] = 0; | |
2341 | ftrace_match_records(parser->buffer, parser->idx, enable); | |
5072c59f SR |
2342 | } |
2343 | ||
e6ea44e9 | 2344 | mutex_lock(&ftrace_lock); |
ee02a2e5 | 2345 | if (ftrace_start_up && ftrace_enabled) |
5072c59f | 2346 | ftrace_run_update_code(FTRACE_ENABLE_CALLS); |
e6ea44e9 | 2347 | mutex_unlock(&ftrace_lock); |
5072c59f | 2348 | |
689fd8b6 | 2349 | trace_parser_put(parser); |
5072c59f | 2350 | kfree(iter); |
689fd8b6 | 2351 | |
41c52c0d | 2352 | mutex_unlock(&ftrace_regex_lock); |
5072c59f SR |
2353 | return 0; |
2354 | } | |
2355 | ||
41c52c0d SR |
2356 | static int |
2357 | ftrace_filter_release(struct inode *inode, struct file *file) | |
2358 | { | |
2359 | return ftrace_regex_release(inode, file, 1); | |
2360 | } | |
2361 | ||
2362 | static int | |
2363 | ftrace_notrace_release(struct inode *inode, struct file *file) | |
2364 | { | |
2365 | return ftrace_regex_release(inode, file, 0); | |
2366 | } | |
2367 | ||
5e2336a0 | 2368 | static const struct file_operations ftrace_avail_fops = { |
5072c59f SR |
2369 | .open = ftrace_avail_open, |
2370 | .read = seq_read, | |
2371 | .llseek = seq_lseek, | |
3be04b47 | 2372 | .release = seq_release_private, |
5072c59f SR |
2373 | }; |
2374 | ||
5e2336a0 | 2375 | static const struct file_operations ftrace_failures_fops = { |
eb9a7bf0 AS |
2376 | .open = ftrace_failures_open, |
2377 | .read = seq_read, | |
2378 | .llseek = seq_lseek, | |
3be04b47 | 2379 | .release = seq_release_private, |
eb9a7bf0 AS |
2380 | }; |
2381 | ||
5e2336a0 | 2382 | static const struct file_operations ftrace_filter_fops = { |
5072c59f | 2383 | .open = ftrace_filter_open, |
850a80cf | 2384 | .read = seq_read, |
5072c59f | 2385 | .write = ftrace_filter_write, |
41c52c0d | 2386 | .llseek = ftrace_regex_lseek, |
5072c59f SR |
2387 | .release = ftrace_filter_release, |
2388 | }; | |
2389 | ||
5e2336a0 | 2390 | static const struct file_operations ftrace_notrace_fops = { |
41c52c0d | 2391 | .open = ftrace_notrace_open, |
850a80cf | 2392 | .read = seq_read, |
41c52c0d SR |
2393 | .write = ftrace_notrace_write, |
2394 | .llseek = ftrace_regex_lseek, | |
2395 | .release = ftrace_notrace_release, | |
2396 | }; | |
2397 | ||
ea4e2bc4 SR |
2398 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
2399 | ||
2400 | static DEFINE_MUTEX(graph_lock); | |
2401 | ||
2402 | int ftrace_graph_count; | |
c7c6b1fe | 2403 | int ftrace_graph_filter_enabled; |
ea4e2bc4 SR |
2404 | unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly; |
2405 | ||
2406 | static void * | |
85951842 | 2407 | __g_next(struct seq_file *m, loff_t *pos) |
ea4e2bc4 | 2408 | { |
85951842 | 2409 | if (*pos >= ftrace_graph_count) |
ea4e2bc4 | 2410 | return NULL; |
a4ec5e0c | 2411 | return &ftrace_graph_funcs[*pos]; |
85951842 | 2412 | } |
ea4e2bc4 | 2413 | |
85951842 LZ |
2414 | static void * |
2415 | g_next(struct seq_file *m, void *v, loff_t *pos) | |
2416 | { | |
2417 | (*pos)++; | |
2418 | return __g_next(m, pos); | |
ea4e2bc4 SR |
2419 | } |
2420 | ||
2421 | static void *g_start(struct seq_file *m, loff_t *pos) | |
2422 | { | |
ea4e2bc4 SR |
2423 | mutex_lock(&graph_lock); |
2424 | ||
f9349a8f | 2425 | /* Nothing, tell g_show to print all functions are enabled */ |
c7c6b1fe | 2426 | if (!ftrace_graph_filter_enabled && !*pos) |
f9349a8f FW |
2427 | return (void *)1; |
2428 | ||
85951842 | 2429 | return __g_next(m, pos); |
ea4e2bc4 SR |
2430 | } |
2431 | ||
2432 | static void g_stop(struct seq_file *m, void *p) | |
2433 | { | |
2434 | mutex_unlock(&graph_lock); | |
2435 | } | |
2436 | ||
2437 | static int g_show(struct seq_file *m, void *v) | |
2438 | { | |
2439 | unsigned long *ptr = v; | |
ea4e2bc4 SR |
2440 | |
2441 | if (!ptr) | |
2442 | return 0; | |
2443 | ||
f9349a8f FW |
2444 | if (ptr == (unsigned long *)1) { |
2445 | seq_printf(m, "#### all functions enabled ####\n"); | |
2446 | return 0; | |
2447 | } | |
2448 | ||
b375a11a | 2449 | seq_printf(m, "%ps\n", (void *)*ptr); |
ea4e2bc4 SR |
2450 | |
2451 | return 0; | |
2452 | } | |
2453 | ||
88e9d34c | 2454 | static const struct seq_operations ftrace_graph_seq_ops = { |
ea4e2bc4 SR |
2455 | .start = g_start, |
2456 | .next = g_next, | |
2457 | .stop = g_stop, | |
2458 | .show = g_show, | |
2459 | }; | |
2460 | ||
2461 | static int | |
2462 | ftrace_graph_open(struct inode *inode, struct file *file) | |
2463 | { | |
2464 | int ret = 0; | |
2465 | ||
2466 | if (unlikely(ftrace_disabled)) | |
2467 | return -ENODEV; | |
2468 | ||
2469 | mutex_lock(&graph_lock); | |
2470 | if ((file->f_mode & FMODE_WRITE) && | |
8650ae32 | 2471 | (file->f_flags & O_TRUNC)) { |
c7c6b1fe | 2472 | ftrace_graph_filter_enabled = 0; |
ea4e2bc4 SR |
2473 | ftrace_graph_count = 0; |
2474 | memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs)); | |
2475 | } | |
a4ec5e0c | 2476 | mutex_unlock(&graph_lock); |
ea4e2bc4 | 2477 | |
a4ec5e0c | 2478 | if (file->f_mode & FMODE_READ) |
ea4e2bc4 | 2479 | ret = seq_open(file, &ftrace_graph_seq_ops); |
ea4e2bc4 SR |
2480 | |
2481 | return ret; | |
2482 | } | |
2483 | ||
87827111 LZ |
2484 | static int |
2485 | ftrace_graph_release(struct inode *inode, struct file *file) | |
2486 | { | |
2487 | if (file->f_mode & FMODE_READ) | |
2488 | seq_release(inode, file); | |
2489 | return 0; | |
2490 | } | |
2491 | ||
ea4e2bc4 | 2492 | static int |
f9349a8f | 2493 | ftrace_set_func(unsigned long *array, int *idx, char *buffer) |
ea4e2bc4 | 2494 | { |
ea4e2bc4 SR |
2495 | struct dyn_ftrace *rec; |
2496 | struct ftrace_page *pg; | |
f9349a8f | 2497 | int search_len; |
c7c6b1fe | 2498 | int fail = 1; |
f9349a8f FW |
2499 | int type, not; |
2500 | char *search; | |
2501 | bool exists; | |
2502 | int i; | |
ea4e2bc4 SR |
2503 | |
2504 | if (ftrace_disabled) | |
2505 | return -ENODEV; | |
2506 | ||
f9349a8f | 2507 | /* decode regex */ |
3f6fe06d | 2508 | type = filter_parse_regex(buffer, strlen(buffer), &search, ¬); |
c7c6b1fe LZ |
2509 | if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS) |
2510 | return -EBUSY; | |
f9349a8f FW |
2511 | |
2512 | search_len = strlen(search); | |
2513 | ||
52baf119 | 2514 | mutex_lock(&ftrace_lock); |
265c831c SR |
2515 | do_for_each_ftrace_rec(pg, rec) { |
2516 | ||
2517 | if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE)) | |
2518 | continue; | |
2519 | ||
f9349a8f | 2520 | if (ftrace_match_record(rec, search, search_len, type)) { |
c7c6b1fe | 2521 | /* if it is in the array */ |
f9349a8f | 2522 | exists = false; |
c7c6b1fe | 2523 | for (i = 0; i < *idx; i++) { |
f9349a8f FW |
2524 | if (array[i] == rec->ip) { |
2525 | exists = true; | |
265c831c SR |
2526 | break; |
2527 | } | |
c7c6b1fe LZ |
2528 | } |
2529 | ||
2530 | if (!not) { | |
2531 | fail = 0; | |
2532 | if (!exists) { | |
2533 | array[(*idx)++] = rec->ip; | |
2534 | if (*idx >= FTRACE_GRAPH_MAX_FUNCS) | |
2535 | goto out; | |
2536 | } | |
2537 | } else { | |
2538 | if (exists) { | |
2539 | array[i] = array[--(*idx)]; | |
2540 | array[*idx] = 0; | |
2541 | fail = 0; | |
2542 | } | |
2543 | } | |
ea4e2bc4 | 2544 | } |
265c831c | 2545 | } while_for_each_ftrace_rec(); |
c7c6b1fe | 2546 | out: |
52baf119 | 2547 | mutex_unlock(&ftrace_lock); |
ea4e2bc4 | 2548 | |
c7c6b1fe LZ |
2549 | if (fail) |
2550 | return -EINVAL; | |
2551 | ||
2552 | ftrace_graph_filter_enabled = 1; | |
2553 | return 0; | |
ea4e2bc4 SR |
2554 | } |
2555 | ||
2556 | static ssize_t | |
2557 | ftrace_graph_write(struct file *file, const char __user *ubuf, | |
2558 | size_t cnt, loff_t *ppos) | |
2559 | { | |
689fd8b6 | 2560 | struct trace_parser parser; |
4ba7978e | 2561 | ssize_t read, ret; |
ea4e2bc4 | 2562 | |
c7c6b1fe | 2563 | if (!cnt) |
ea4e2bc4 SR |
2564 | return 0; |
2565 | ||
2566 | mutex_lock(&graph_lock); | |
2567 | ||
689fd8b6 | 2568 | if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) { |
2569 | ret = -ENOMEM; | |
1eb90f13 | 2570 | goto out_unlock; |
ea4e2bc4 SR |
2571 | } |
2572 | ||
689fd8b6 | 2573 | read = trace_get_user(&parser, ubuf, cnt, ppos); |
ea4e2bc4 | 2574 | |
4ba7978e | 2575 | if (read >= 0 && trace_parser_loaded((&parser))) { |
689fd8b6 | 2576 | parser.buffer[parser.idx] = 0; |
2577 | ||
2578 | /* we allow only one expression at a time */ | |
a4ec5e0c | 2579 | ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count, |
689fd8b6 | 2580 | parser.buffer); |
ea4e2bc4 | 2581 | if (ret) |
1eb90f13 | 2582 | goto out_free; |
ea4e2bc4 | 2583 | } |
ea4e2bc4 SR |
2584 | |
2585 | ret = read; | |
1eb90f13 LZ |
2586 | |
2587 | out_free: | |
689fd8b6 | 2588 | trace_parser_put(&parser); |
1eb90f13 | 2589 | out_unlock: |
ea4e2bc4 SR |
2590 | mutex_unlock(&graph_lock); |
2591 | ||
2592 | return ret; | |
2593 | } | |
2594 | ||
2595 | static const struct file_operations ftrace_graph_fops = { | |
87827111 LZ |
2596 | .open = ftrace_graph_open, |
2597 | .read = seq_read, | |
2598 | .write = ftrace_graph_write, | |
2599 | .release = ftrace_graph_release, | |
ea4e2bc4 SR |
2600 | }; |
2601 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ | |
2602 | ||
df4fc315 | 2603 | static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer) |
5072c59f | 2604 | { |
5072c59f | 2605 | |
5452af66 FW |
2606 | trace_create_file("available_filter_functions", 0444, |
2607 | d_tracer, NULL, &ftrace_avail_fops); | |
5072c59f | 2608 | |
5452af66 FW |
2609 | trace_create_file("failures", 0444, |
2610 | d_tracer, NULL, &ftrace_failures_fops); | |
eb9a7bf0 | 2611 | |
5452af66 FW |
2612 | trace_create_file("set_ftrace_filter", 0644, d_tracer, |
2613 | NULL, &ftrace_filter_fops); | |
41c52c0d | 2614 | |
5452af66 | 2615 | trace_create_file("set_ftrace_notrace", 0644, d_tracer, |
41c52c0d | 2616 | NULL, &ftrace_notrace_fops); |
ad90c0e3 | 2617 | |
ea4e2bc4 | 2618 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
5452af66 | 2619 | trace_create_file("set_graph_function", 0444, d_tracer, |
ea4e2bc4 SR |
2620 | NULL, |
2621 | &ftrace_graph_fops); | |
ea4e2bc4 SR |
2622 | #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ |
2623 | ||
5072c59f SR |
2624 | return 0; |
2625 | } | |
2626 | ||
5cb084bb | 2627 | static int ftrace_process_locs(struct module *mod, |
31e88909 | 2628 | unsigned long *start, |
68bf21aa SR |
2629 | unsigned long *end) |
2630 | { | |
2631 | unsigned long *p; | |
2632 | unsigned long addr; | |
2633 | unsigned long flags; | |
2634 | ||
e6ea44e9 | 2635 | mutex_lock(&ftrace_lock); |
68bf21aa SR |
2636 | p = start; |
2637 | while (p < end) { | |
2638 | addr = ftrace_call_adjust(*p++); | |
20e5227e SR |
2639 | /* |
2640 | * Some architecture linkers will pad between | |
2641 | * the different mcount_loc sections of different | |
2642 | * object files to satisfy alignments. | |
2643 | * Skip any NULL pointers. | |
2644 | */ | |
2645 | if (!addr) | |
2646 | continue; | |
68bf21aa | 2647 | ftrace_record_ip(addr); |
68bf21aa SR |
2648 | } |
2649 | ||
08f5ac90 | 2650 | /* disable interrupts to prevent kstop machine */ |
68bf21aa | 2651 | local_irq_save(flags); |
31e88909 | 2652 | ftrace_update_code(mod); |
68bf21aa | 2653 | local_irq_restore(flags); |
e6ea44e9 | 2654 | mutex_unlock(&ftrace_lock); |
68bf21aa SR |
2655 | |
2656 | return 0; | |
2657 | } | |
2658 | ||
93eb677d | 2659 | #ifdef CONFIG_MODULES |
e7247a15 | 2660 | void ftrace_release_mod(struct module *mod) |
93eb677d SR |
2661 | { |
2662 | struct dyn_ftrace *rec; | |
2663 | struct ftrace_page *pg; | |
93eb677d | 2664 | |
e7247a15 | 2665 | if (ftrace_disabled) |
93eb677d SR |
2666 | return; |
2667 | ||
2668 | mutex_lock(&ftrace_lock); | |
2669 | do_for_each_ftrace_rec(pg, rec) { | |
e7247a15 | 2670 | if (within_module_core(rec->ip, mod)) { |
93eb677d SR |
2671 | /* |
2672 | * rec->ip is changed in ftrace_free_rec() | |
2673 | * It should not between s and e if record was freed. | |
2674 | */ | |
2675 | FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE); | |
2676 | ftrace_free_rec(rec); | |
2677 | } | |
2678 | } while_for_each_ftrace_rec(); | |
2679 | mutex_unlock(&ftrace_lock); | |
2680 | } | |
2681 | ||
2682 | static void ftrace_init_module(struct module *mod, | |
2683 | unsigned long *start, unsigned long *end) | |
90d595fe | 2684 | { |
00fd61ae | 2685 | if (ftrace_disabled || start == end) |
fed1939c | 2686 | return; |
5cb084bb | 2687 | ftrace_process_locs(mod, start, end); |
90d595fe SR |
2688 | } |
2689 | ||
93eb677d SR |
2690 | static int ftrace_module_notify(struct notifier_block *self, |
2691 | unsigned long val, void *data) | |
2692 | { | |
2693 | struct module *mod = data; | |
2694 | ||
2695 | switch (val) { | |
2696 | case MODULE_STATE_COMING: | |
2697 | ftrace_init_module(mod, mod->ftrace_callsites, | |
2698 | mod->ftrace_callsites + | |
2699 | mod->num_ftrace_callsites); | |
2700 | break; | |
2701 | case MODULE_STATE_GOING: | |
e7247a15 | 2702 | ftrace_release_mod(mod); |
93eb677d SR |
2703 | break; |
2704 | } | |
2705 | ||
2706 | return 0; | |
2707 | } | |
2708 | #else | |
2709 | static int ftrace_module_notify(struct notifier_block *self, | |
2710 | unsigned long val, void *data) | |
2711 | { | |
2712 | return 0; | |
2713 | } | |
2714 | #endif /* CONFIG_MODULES */ | |
2715 | ||
2716 | struct notifier_block ftrace_module_nb = { | |
2717 | .notifier_call = ftrace_module_notify, | |
2718 | .priority = 0, | |
2719 | }; | |
2720 | ||
68bf21aa SR |
2721 | extern unsigned long __start_mcount_loc[]; |
2722 | extern unsigned long __stop_mcount_loc[]; | |
2723 | ||
2724 | void __init ftrace_init(void) | |
2725 | { | |
2726 | unsigned long count, addr, flags; | |
2727 | int ret; | |
2728 | ||
2729 | /* Keep the ftrace pointer to the stub */ | |
2730 | addr = (unsigned long)ftrace_stub; | |
2731 | ||
2732 | local_irq_save(flags); | |
2733 | ftrace_dyn_arch_init(&addr); | |
2734 | local_irq_restore(flags); | |
2735 | ||
2736 | /* ftrace_dyn_arch_init places the return code in addr */ | |
2737 | if (addr) | |
2738 | goto failed; | |
2739 | ||
2740 | count = __stop_mcount_loc - __start_mcount_loc; | |
2741 | ||
2742 | ret = ftrace_dyn_table_alloc(count); | |
2743 | if (ret) | |
2744 | goto failed; | |
2745 | ||
2746 | last_ftrace_enabled = ftrace_enabled = 1; | |
2747 | ||
5cb084bb | 2748 | ret = ftrace_process_locs(NULL, |
31e88909 | 2749 | __start_mcount_loc, |
68bf21aa SR |
2750 | __stop_mcount_loc); |
2751 | ||
93eb677d | 2752 | ret = register_module_notifier(&ftrace_module_nb); |
24ed0c4b | 2753 | if (ret) |
93eb677d SR |
2754 | pr_warning("Failed to register trace ftrace module notifier\n"); |
2755 | ||
2af15d6a SR |
2756 | set_ftrace_early_filters(); |
2757 | ||
68bf21aa SR |
2758 | return; |
2759 | failed: | |
2760 | ftrace_disabled = 1; | |
2761 | } | |
68bf21aa | 2762 | |
3d083395 | 2763 | #else |
0b6e4d56 FW |
2764 | |
2765 | static int __init ftrace_nodyn_init(void) | |
2766 | { | |
2767 | ftrace_enabled = 1; | |
2768 | return 0; | |
2769 | } | |
2770 | device_initcall(ftrace_nodyn_init); | |
2771 | ||
df4fc315 SR |
2772 | static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; } |
2773 | static inline void ftrace_startup_enable(int command) { } | |
5a45cfe1 SR |
2774 | /* Keep as macros so we do not need to define the commands */ |
2775 | # define ftrace_startup(command) do { } while (0) | |
2776 | # define ftrace_shutdown(command) do { } while (0) | |
c7aafc54 IM |
2777 | # define ftrace_startup_sysctl() do { } while (0) |
2778 | # define ftrace_shutdown_sysctl() do { } while (0) | |
3d083395 SR |
2779 | #endif /* CONFIG_DYNAMIC_FTRACE */ |
2780 | ||
e32d8956 | 2781 | static void clear_ftrace_swapper(void) |
978f3a45 SR |
2782 | { |
2783 | struct task_struct *p; | |
e32d8956 | 2784 | int cpu; |
978f3a45 | 2785 | |
e32d8956 SR |
2786 | get_online_cpus(); |
2787 | for_each_online_cpu(cpu) { | |
2788 | p = idle_task(cpu); | |
978f3a45 | 2789 | clear_tsk_trace_trace(p); |
e32d8956 SR |
2790 | } |
2791 | put_online_cpus(); | |
2792 | } | |
978f3a45 | 2793 | |
e32d8956 SR |
2794 | static void set_ftrace_swapper(void) |
2795 | { | |
2796 | struct task_struct *p; | |
2797 | int cpu; | |
2798 | ||
2799 | get_online_cpus(); | |
2800 | for_each_online_cpu(cpu) { | |
2801 | p = idle_task(cpu); | |
2802 | set_tsk_trace_trace(p); | |
2803 | } | |
2804 | put_online_cpus(); | |
978f3a45 SR |
2805 | } |
2806 | ||
e32d8956 SR |
2807 | static void clear_ftrace_pid(struct pid *pid) |
2808 | { | |
2809 | struct task_struct *p; | |
2810 | ||
229c4ef8 | 2811 | rcu_read_lock(); |
e32d8956 SR |
2812 | do_each_pid_task(pid, PIDTYPE_PID, p) { |
2813 | clear_tsk_trace_trace(p); | |
2814 | } while_each_pid_task(pid, PIDTYPE_PID, p); | |
229c4ef8 ON |
2815 | rcu_read_unlock(); |
2816 | ||
e32d8956 SR |
2817 | put_pid(pid); |
2818 | } | |
2819 | ||
2820 | static void set_ftrace_pid(struct pid *pid) | |
978f3a45 SR |
2821 | { |
2822 | struct task_struct *p; | |
2823 | ||
229c4ef8 | 2824 | rcu_read_lock(); |
978f3a45 SR |
2825 | do_each_pid_task(pid, PIDTYPE_PID, p) { |
2826 | set_tsk_trace_trace(p); | |
2827 | } while_each_pid_task(pid, PIDTYPE_PID, p); | |
229c4ef8 | 2828 | rcu_read_unlock(); |
978f3a45 SR |
2829 | } |
2830 | ||
756d17ee | 2831 | static void clear_ftrace_pid_task(struct pid *pid) |
e32d8956 | 2832 | { |
756d17ee | 2833 | if (pid == ftrace_swapper_pid) |
e32d8956 SR |
2834 | clear_ftrace_swapper(); |
2835 | else | |
756d17ee | 2836 | clear_ftrace_pid(pid); |
e32d8956 SR |
2837 | } |
2838 | ||
2839 | static void set_ftrace_pid_task(struct pid *pid) | |
2840 | { | |
2841 | if (pid == ftrace_swapper_pid) | |
2842 | set_ftrace_swapper(); | |
2843 | else | |
2844 | set_ftrace_pid(pid); | |
2845 | } | |
2846 | ||
756d17ee | 2847 | static int ftrace_pid_add(int p) |
df4fc315 | 2848 | { |
978f3a45 | 2849 | struct pid *pid; |
756d17ee | 2850 | struct ftrace_pid *fpid; |
2851 | int ret = -EINVAL; | |
df4fc315 | 2852 | |
756d17ee | 2853 | mutex_lock(&ftrace_lock); |
df4fc315 | 2854 | |
756d17ee | 2855 | if (!p) |
2856 | pid = ftrace_swapper_pid; | |
2857 | else | |
2858 | pid = find_get_pid(p); | |
df4fc315 | 2859 | |
756d17ee | 2860 | if (!pid) |
2861 | goto out; | |
df4fc315 | 2862 | |
756d17ee | 2863 | ret = 0; |
df4fc315 | 2864 | |
756d17ee | 2865 | list_for_each_entry(fpid, &ftrace_pids, list) |
2866 | if (fpid->pid == pid) | |
2867 | goto out_put; | |
978f3a45 | 2868 | |
756d17ee | 2869 | ret = -ENOMEM; |
df4fc315 | 2870 | |
756d17ee | 2871 | fpid = kmalloc(sizeof(*fpid), GFP_KERNEL); |
2872 | if (!fpid) | |
2873 | goto out_put; | |
df4fc315 | 2874 | |
756d17ee | 2875 | list_add(&fpid->list, &ftrace_pids); |
2876 | fpid->pid = pid; | |
0ef8cde5 | 2877 | |
756d17ee | 2878 | set_ftrace_pid_task(pid); |
978f3a45 | 2879 | |
756d17ee | 2880 | ftrace_update_pid_func(); |
2881 | ftrace_startup_enable(0); | |
2882 | ||
2883 | mutex_unlock(&ftrace_lock); | |
2884 | return 0; | |
2885 | ||
2886 | out_put: | |
2887 | if (pid != ftrace_swapper_pid) | |
2888 | put_pid(pid); | |
978f3a45 | 2889 | |
756d17ee | 2890 | out: |
2891 | mutex_unlock(&ftrace_lock); | |
2892 | return ret; | |
2893 | } | |
2894 | ||
2895 | static void ftrace_pid_reset(void) | |
2896 | { | |
2897 | struct ftrace_pid *fpid, *safe; | |
978f3a45 | 2898 | |
756d17ee | 2899 | mutex_lock(&ftrace_lock); |
2900 | list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) { | |
2901 | struct pid *pid = fpid->pid; | |
2902 | ||
2903 | clear_ftrace_pid_task(pid); | |
2904 | ||
2905 | list_del(&fpid->list); | |
2906 | kfree(fpid); | |
df4fc315 SR |
2907 | } |
2908 | ||
df4fc315 SR |
2909 | ftrace_update_pid_func(); |
2910 | ftrace_startup_enable(0); | |
2911 | ||
e6ea44e9 | 2912 | mutex_unlock(&ftrace_lock); |
756d17ee | 2913 | } |
df4fc315 | 2914 | |
756d17ee | 2915 | static void *fpid_start(struct seq_file *m, loff_t *pos) |
2916 | { | |
2917 | mutex_lock(&ftrace_lock); | |
2918 | ||
2919 | if (list_empty(&ftrace_pids) && (!*pos)) | |
2920 | return (void *) 1; | |
2921 | ||
2922 | return seq_list_start(&ftrace_pids, *pos); | |
2923 | } | |
2924 | ||
2925 | static void *fpid_next(struct seq_file *m, void *v, loff_t *pos) | |
2926 | { | |
2927 | if (v == (void *)1) | |
2928 | return NULL; | |
2929 | ||
2930 | return seq_list_next(v, &ftrace_pids, pos); | |
2931 | } | |
2932 | ||
2933 | static void fpid_stop(struct seq_file *m, void *p) | |
2934 | { | |
2935 | mutex_unlock(&ftrace_lock); | |
2936 | } | |
2937 | ||
2938 | static int fpid_show(struct seq_file *m, void *v) | |
2939 | { | |
2940 | const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list); | |
2941 | ||
2942 | if (v == (void *)1) { | |
2943 | seq_printf(m, "no pid\n"); | |
2944 | return 0; | |
2945 | } | |
2946 | ||
2947 | if (fpid->pid == ftrace_swapper_pid) | |
2948 | seq_printf(m, "swapper tasks\n"); | |
2949 | else | |
2950 | seq_printf(m, "%u\n", pid_vnr(fpid->pid)); | |
2951 | ||
2952 | return 0; | |
2953 | } | |
2954 | ||
2955 | static const struct seq_operations ftrace_pid_sops = { | |
2956 | .start = fpid_start, | |
2957 | .next = fpid_next, | |
2958 | .stop = fpid_stop, | |
2959 | .show = fpid_show, | |
2960 | }; | |
2961 | ||
2962 | static int | |
2963 | ftrace_pid_open(struct inode *inode, struct file *file) | |
2964 | { | |
2965 | int ret = 0; | |
2966 | ||
2967 | if ((file->f_mode & FMODE_WRITE) && | |
2968 | (file->f_flags & O_TRUNC)) | |
2969 | ftrace_pid_reset(); | |
2970 | ||
2971 | if (file->f_mode & FMODE_READ) | |
2972 | ret = seq_open(file, &ftrace_pid_sops); | |
2973 | ||
2974 | return ret; | |
2975 | } | |
2976 | ||
df4fc315 SR |
2977 | static ssize_t |
2978 | ftrace_pid_write(struct file *filp, const char __user *ubuf, | |
2979 | size_t cnt, loff_t *ppos) | |
2980 | { | |
457dc928 | 2981 | char buf[64], *tmp; |
df4fc315 SR |
2982 | long val; |
2983 | int ret; | |
2984 | ||
2985 | if (cnt >= sizeof(buf)) | |
2986 | return -EINVAL; | |
2987 | ||
2988 | if (copy_from_user(&buf, ubuf, cnt)) | |
2989 | return -EFAULT; | |
2990 | ||
2991 | buf[cnt] = 0; | |
2992 | ||
756d17ee | 2993 | /* |
2994 | * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid" | |
2995 | * to clean the filter quietly. | |
2996 | */ | |
457dc928 IM |
2997 | tmp = strstrip(buf); |
2998 | if (strlen(tmp) == 0) | |
756d17ee | 2999 | return 1; |
3000 | ||
457dc928 | 3001 | ret = strict_strtol(tmp, 10, &val); |
df4fc315 SR |
3002 | if (ret < 0) |
3003 | return ret; | |
3004 | ||
756d17ee | 3005 | ret = ftrace_pid_add(val); |
df4fc315 | 3006 | |
756d17ee | 3007 | return ret ? ret : cnt; |
3008 | } | |
df4fc315 | 3009 | |
756d17ee | 3010 | static int |
3011 | ftrace_pid_release(struct inode *inode, struct file *file) | |
3012 | { | |
3013 | if (file->f_mode & FMODE_READ) | |
3014 | seq_release(inode, file); | |
df4fc315 | 3015 | |
756d17ee | 3016 | return 0; |
df4fc315 SR |
3017 | } |
3018 | ||
5e2336a0 | 3019 | static const struct file_operations ftrace_pid_fops = { |
756d17ee | 3020 | .open = ftrace_pid_open, |
3021 | .write = ftrace_pid_write, | |
3022 | .read = seq_read, | |
3023 | .llseek = seq_lseek, | |
3024 | .release = ftrace_pid_release, | |
df4fc315 SR |
3025 | }; |
3026 | ||
3027 | static __init int ftrace_init_debugfs(void) | |
3028 | { | |
3029 | struct dentry *d_tracer; | |
df4fc315 SR |
3030 | |
3031 | d_tracer = tracing_init_dentry(); | |
3032 | if (!d_tracer) | |
3033 | return 0; | |
3034 | ||
3035 | ftrace_init_dyn_debugfs(d_tracer); | |
3036 | ||
5452af66 FW |
3037 | trace_create_file("set_ftrace_pid", 0644, d_tracer, |
3038 | NULL, &ftrace_pid_fops); | |
493762fc SR |
3039 | |
3040 | ftrace_profile_debugfs(d_tracer); | |
3041 | ||
df4fc315 SR |
3042 | return 0; |
3043 | } | |
df4fc315 SR |
3044 | fs_initcall(ftrace_init_debugfs); |
3045 | ||
a2bb6a3d | 3046 | /** |
81adbdc0 | 3047 | * ftrace_kill - kill ftrace |
a2bb6a3d SR |
3048 | * |
3049 | * This function should be used by panic code. It stops ftrace | |
3050 | * but in a not so nice way. If you need to simply kill ftrace | |
3051 | * from a non-atomic section, use ftrace_kill. | |
3052 | */ | |
81adbdc0 | 3053 | void ftrace_kill(void) |
a2bb6a3d SR |
3054 | { |
3055 | ftrace_disabled = 1; | |
3056 | ftrace_enabled = 0; | |
a2bb6a3d SR |
3057 | clear_ftrace_function(); |
3058 | } | |
3059 | ||
16444a8a | 3060 | /** |
3d083395 SR |
3061 | * register_ftrace_function - register a function for profiling |
3062 | * @ops - ops structure that holds the function for profiling. | |
16444a8a | 3063 | * |
3d083395 SR |
3064 | * Register a function to be called by all functions in the |
3065 | * kernel. | |
3066 | * | |
3067 | * Note: @ops->func and all the functions it calls must be labeled | |
3068 | * with "notrace", otherwise it will go into a | |
3069 | * recursive loop. | |
16444a8a | 3070 | */ |
3d083395 | 3071 | int register_ftrace_function(struct ftrace_ops *ops) |
16444a8a | 3072 | { |
b0fc494f SR |
3073 | int ret; |
3074 | ||
4eebcc81 SR |
3075 | if (unlikely(ftrace_disabled)) |
3076 | return -1; | |
3077 | ||
e6ea44e9 | 3078 | mutex_lock(&ftrace_lock); |
e7d3737e | 3079 | |
b0fc494f | 3080 | ret = __register_ftrace_function(ops); |
5a45cfe1 | 3081 | ftrace_startup(0); |
b0fc494f | 3082 | |
e6ea44e9 | 3083 | mutex_unlock(&ftrace_lock); |
b0fc494f | 3084 | return ret; |
3d083395 SR |
3085 | } |
3086 | ||
3087 | /** | |
32632920 | 3088 | * unregister_ftrace_function - unregister a function for profiling. |
3d083395 SR |
3089 | * @ops - ops structure that holds the function to unregister |
3090 | * | |
3091 | * Unregister a function that was added to be called by ftrace profiling. | |
3092 | */ | |
3093 | int unregister_ftrace_function(struct ftrace_ops *ops) | |
3094 | { | |
3095 | int ret; | |
3096 | ||
e6ea44e9 | 3097 | mutex_lock(&ftrace_lock); |
3d083395 | 3098 | ret = __unregister_ftrace_function(ops); |
5a45cfe1 | 3099 | ftrace_shutdown(0); |
e6ea44e9 | 3100 | mutex_unlock(&ftrace_lock); |
b0fc494f SR |
3101 | |
3102 | return ret; | |
3103 | } | |
3104 | ||
e309b41d | 3105 | int |
b0fc494f | 3106 | ftrace_enable_sysctl(struct ctl_table *table, int write, |
8d65af78 | 3107 | void __user *buffer, size_t *lenp, |
b0fc494f SR |
3108 | loff_t *ppos) |
3109 | { | |
3110 | int ret; | |
3111 | ||
4eebcc81 SR |
3112 | if (unlikely(ftrace_disabled)) |
3113 | return -ENODEV; | |
3114 | ||
e6ea44e9 | 3115 | mutex_lock(&ftrace_lock); |
b0fc494f | 3116 | |
8d65af78 | 3117 | ret = proc_dointvec(table, write, buffer, lenp, ppos); |
b0fc494f | 3118 | |
a32c7765 | 3119 | if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled)) |
b0fc494f SR |
3120 | goto out; |
3121 | ||
a32c7765 | 3122 | last_ftrace_enabled = !!ftrace_enabled; |
b0fc494f SR |
3123 | |
3124 | if (ftrace_enabled) { | |
3125 | ||
3126 | ftrace_startup_sysctl(); | |
3127 | ||
3128 | /* we are starting ftrace again */ | |
3129 | if (ftrace_list != &ftrace_list_end) { | |
3130 | if (ftrace_list->next == &ftrace_list_end) | |
3131 | ftrace_trace_function = ftrace_list->func; | |
3132 | else | |
3133 | ftrace_trace_function = ftrace_list_func; | |
3134 | } | |
3135 | ||
3136 | } else { | |
3137 | /* stopping ftrace calls (just send to ftrace_stub) */ | |
3138 | ftrace_trace_function = ftrace_stub; | |
3139 | ||
3140 | ftrace_shutdown_sysctl(); | |
3141 | } | |
3142 | ||
3143 | out: | |
e6ea44e9 | 3144 | mutex_unlock(&ftrace_lock); |
3d083395 | 3145 | return ret; |
16444a8a | 3146 | } |
f17845e5 | 3147 | |
fb52607a | 3148 | #ifdef CONFIG_FUNCTION_GRAPH_TRACER |
e7d3737e | 3149 | |
597af815 | 3150 | static int ftrace_graph_active; |
4a2b8dda | 3151 | static struct notifier_block ftrace_suspend_notifier; |
e7d3737e | 3152 | |
e49dc19c SR |
3153 | int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace) |
3154 | { | |
3155 | return 0; | |
3156 | } | |
3157 | ||
287b6e68 FW |
3158 | /* The callbacks that hook a function */ |
3159 | trace_func_graph_ret_t ftrace_graph_return = | |
3160 | (trace_func_graph_ret_t)ftrace_stub; | |
e49dc19c | 3161 | trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub; |
f201ae23 FW |
3162 | |
3163 | /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */ | |
3164 | static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list) | |
3165 | { | |
3166 | int i; | |
3167 | int ret = 0; | |
3168 | unsigned long flags; | |
3169 | int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE; | |
3170 | struct task_struct *g, *t; | |
3171 | ||
3172 | for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) { | |
3173 | ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH | |
3174 | * sizeof(struct ftrace_ret_stack), | |
3175 | GFP_KERNEL); | |
3176 | if (!ret_stack_list[i]) { | |
3177 | start = 0; | |
3178 | end = i; | |
3179 | ret = -ENOMEM; | |
3180 | goto free; | |
3181 | } | |
3182 | } | |
3183 | ||
3184 | read_lock_irqsave(&tasklist_lock, flags); | |
3185 | do_each_thread(g, t) { | |
3186 | if (start == end) { | |
3187 | ret = -EAGAIN; | |
3188 | goto unlock; | |
3189 | } | |
3190 | ||
3191 | if (t->ret_stack == NULL) { | |
380c4b14 | 3192 | atomic_set(&t->tracing_graph_pause, 0); |
f201ae23 | 3193 | atomic_set(&t->trace_overrun, 0); |
26c01624 SR |
3194 | t->curr_ret_stack = -1; |
3195 | /* Make sure the tasks see the -1 first: */ | |
3196 | smp_wmb(); | |
3197 | t->ret_stack = ret_stack_list[start++]; | |
f201ae23 FW |
3198 | } |
3199 | } while_each_thread(g, t); | |
3200 | ||
3201 | unlock: | |
3202 | read_unlock_irqrestore(&tasklist_lock, flags); | |
3203 | free: | |
3204 | for (i = start; i < end; i++) | |
3205 | kfree(ret_stack_list[i]); | |
3206 | return ret; | |
3207 | } | |
3208 | ||
8aef2d28 SR |
3209 | static void |
3210 | ftrace_graph_probe_sched_switch(struct rq *__rq, struct task_struct *prev, | |
3211 | struct task_struct *next) | |
3212 | { | |
3213 | unsigned long long timestamp; | |
3214 | int index; | |
3215 | ||
be6f164a SR |
3216 | /* |
3217 | * Does the user want to count the time a function was asleep. | |
3218 | * If so, do not update the time stamps. | |
3219 | */ | |
3220 | if (trace_flags & TRACE_ITER_SLEEP_TIME) | |
3221 | return; | |
3222 | ||
8aef2d28 SR |
3223 | timestamp = trace_clock_local(); |
3224 | ||
3225 | prev->ftrace_timestamp = timestamp; | |
3226 | ||
3227 | /* only process tasks that we timestamped */ | |
3228 | if (!next->ftrace_timestamp) | |
3229 | return; | |
3230 | ||
3231 | /* | |
3232 | * Update all the counters in next to make up for the | |
3233 | * time next was sleeping. | |
3234 | */ | |
3235 | timestamp -= next->ftrace_timestamp; | |
3236 | ||
3237 | for (index = next->curr_ret_stack; index >= 0; index--) | |
3238 | next->ret_stack[index].calltime += timestamp; | |
3239 | } | |
3240 | ||
f201ae23 | 3241 | /* Allocate a return stack for each task */ |
fb52607a | 3242 | static int start_graph_tracing(void) |
f201ae23 FW |
3243 | { |
3244 | struct ftrace_ret_stack **ret_stack_list; | |
5b058bcd | 3245 | int ret, cpu; |
f201ae23 FW |
3246 | |
3247 | ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE * | |
3248 | sizeof(struct ftrace_ret_stack *), | |
3249 | GFP_KERNEL); | |
3250 | ||
3251 | if (!ret_stack_list) | |
3252 | return -ENOMEM; | |
3253 | ||
5b058bcd | 3254 | /* The cpu_boot init_task->ret_stack will never be freed */ |
179c498a SR |
3255 | for_each_online_cpu(cpu) { |
3256 | if (!idle_task(cpu)->ret_stack) | |
3257 | ftrace_graph_init_task(idle_task(cpu)); | |
3258 | } | |
5b058bcd | 3259 | |
f201ae23 FW |
3260 | do { |
3261 | ret = alloc_retstack_tasklist(ret_stack_list); | |
3262 | } while (ret == -EAGAIN); | |
3263 | ||
8aef2d28 SR |
3264 | if (!ret) { |
3265 | ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch); | |
3266 | if (ret) | |
3267 | pr_info("ftrace_graph: Couldn't activate tracepoint" | |
3268 | " probe to kernel_sched_switch\n"); | |
3269 | } | |
3270 | ||
f201ae23 FW |
3271 | kfree(ret_stack_list); |
3272 | return ret; | |
3273 | } | |
3274 | ||
4a2b8dda FW |
3275 | /* |
3276 | * Hibernation protection. | |
3277 | * The state of the current task is too much unstable during | |
3278 | * suspend/restore to disk. We want to protect against that. | |
3279 | */ | |
3280 | static int | |
3281 | ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state, | |
3282 | void *unused) | |
3283 | { | |
3284 | switch (state) { | |
3285 | case PM_HIBERNATION_PREPARE: | |
3286 | pause_graph_tracing(); | |
3287 | break; | |
3288 | ||
3289 | case PM_POST_HIBERNATION: | |
3290 | unpause_graph_tracing(); | |
3291 | break; | |
3292 | } | |
3293 | return NOTIFY_DONE; | |
3294 | } | |
3295 | ||
287b6e68 FW |
3296 | int register_ftrace_graph(trace_func_graph_ret_t retfunc, |
3297 | trace_func_graph_ent_t entryfunc) | |
15e6cb36 | 3298 | { |
e7d3737e FW |
3299 | int ret = 0; |
3300 | ||
e6ea44e9 | 3301 | mutex_lock(&ftrace_lock); |
e7d3737e | 3302 | |
05ce5818 | 3303 | /* we currently allow only one tracer registered at a time */ |
597af815 | 3304 | if (ftrace_graph_active) { |
05ce5818 SR |
3305 | ret = -EBUSY; |
3306 | goto out; | |
3307 | } | |
3308 | ||
4a2b8dda FW |
3309 | ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call; |
3310 | register_pm_notifier(&ftrace_suspend_notifier); | |
3311 | ||
597af815 | 3312 | ftrace_graph_active++; |
fb52607a | 3313 | ret = start_graph_tracing(); |
f201ae23 | 3314 | if (ret) { |
597af815 | 3315 | ftrace_graph_active--; |
f201ae23 FW |
3316 | goto out; |
3317 | } | |
e53a6319 | 3318 | |
287b6e68 FW |
3319 | ftrace_graph_return = retfunc; |
3320 | ftrace_graph_entry = entryfunc; | |
e53a6319 | 3321 | |
5a45cfe1 | 3322 | ftrace_startup(FTRACE_START_FUNC_RET); |
e7d3737e FW |
3323 | |
3324 | out: | |
e6ea44e9 | 3325 | mutex_unlock(&ftrace_lock); |
e7d3737e | 3326 | return ret; |
15e6cb36 FW |
3327 | } |
3328 | ||
fb52607a | 3329 | void unregister_ftrace_graph(void) |
15e6cb36 | 3330 | { |
e6ea44e9 | 3331 | mutex_lock(&ftrace_lock); |
e7d3737e | 3332 | |
597af815 | 3333 | if (unlikely(!ftrace_graph_active)) |
2aad1b76 SR |
3334 | goto out; |
3335 | ||
597af815 | 3336 | ftrace_graph_active--; |
8aef2d28 | 3337 | unregister_trace_sched_switch(ftrace_graph_probe_sched_switch); |
287b6e68 | 3338 | ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub; |
e49dc19c | 3339 | ftrace_graph_entry = ftrace_graph_entry_stub; |
5a45cfe1 | 3340 | ftrace_shutdown(FTRACE_STOP_FUNC_RET); |
4a2b8dda | 3341 | unregister_pm_notifier(&ftrace_suspend_notifier); |
e7d3737e | 3342 | |
2aad1b76 | 3343 | out: |
e6ea44e9 | 3344 | mutex_unlock(&ftrace_lock); |
15e6cb36 | 3345 | } |
f201ae23 FW |
3346 | |
3347 | /* Allocate a return stack for newly created task */ | |
fb52607a | 3348 | void ftrace_graph_init_task(struct task_struct *t) |
f201ae23 | 3349 | { |
84047e36 SR |
3350 | /* Make sure we do not use the parent ret_stack */ |
3351 | t->ret_stack = NULL; | |
ea14eb71 | 3352 | t->curr_ret_stack = -1; |
84047e36 | 3353 | |
597af815 | 3354 | if (ftrace_graph_active) { |
82310a32 SR |
3355 | struct ftrace_ret_stack *ret_stack; |
3356 | ||
3357 | ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH | |
f201ae23 FW |
3358 | * sizeof(struct ftrace_ret_stack), |
3359 | GFP_KERNEL); | |
82310a32 | 3360 | if (!ret_stack) |
f201ae23 | 3361 | return; |
380c4b14 | 3362 | atomic_set(&t->tracing_graph_pause, 0); |
f201ae23 | 3363 | atomic_set(&t->trace_overrun, 0); |
8aef2d28 | 3364 | t->ftrace_timestamp = 0; |
82310a32 SR |
3365 | /* make curr_ret_stack visable before we add the ret_stack */ |
3366 | smp_wmb(); | |
3367 | t->ret_stack = ret_stack; | |
84047e36 | 3368 | } |
f201ae23 FW |
3369 | } |
3370 | ||
fb52607a | 3371 | void ftrace_graph_exit_task(struct task_struct *t) |
f201ae23 | 3372 | { |
eae849ca FW |
3373 | struct ftrace_ret_stack *ret_stack = t->ret_stack; |
3374 | ||
f201ae23 | 3375 | t->ret_stack = NULL; |
eae849ca FW |
3376 | /* NULL must become visible to IRQs before we free it: */ |
3377 | barrier(); | |
3378 | ||
3379 | kfree(ret_stack); | |
f201ae23 | 3380 | } |
14a866c5 SR |
3381 | |
3382 | void ftrace_graph_stop(void) | |
3383 | { | |
3384 | ftrace_stop(); | |
3385 | } | |
15e6cb36 | 3386 | #endif |