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