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