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