ftrace: Fix unmatched locking in ftrace_regex_write()
[deliverable/linux.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
cf8517cf 743 *ppos += cnt;
bac429f0
SR
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 1080 rec->flags |= FTRACE_FL_FAILED;
3279ba37
SR
1081 ftrace_bug(failed, rec->ip);
1082 /* Stop processing */
1083 return;
3c1720f0 1084 }
265c831c 1085 } while_for_each_ftrace_rec();
3c1720f0
SR
1086}
1087
492a7ea5 1088static int
31e88909 1089ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
3c1720f0
SR
1090{
1091 unsigned long ip;
593eb8a2 1092 int ret;
3c1720f0
SR
1093
1094 ip = rec->ip;
1095
25aac9dc 1096 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
593eb8a2 1097 if (ret) {
31e88909 1098 ftrace_bug(ret, ip);
3c1720f0 1099 rec->flags |= FTRACE_FL_FAILED;
492a7ea5 1100 return 0;
37ad5084 1101 }
492a7ea5 1102 return 1;
3c1720f0
SR
1103}
1104
000ab691
SR
1105/*
1106 * archs can override this function if they must do something
1107 * before the modifying code is performed.
1108 */
1109int __weak ftrace_arch_code_modify_prepare(void)
1110{
1111 return 0;
1112}
1113
1114/*
1115 * archs can override this function if they must do something
1116 * after the modifying code is performed.
1117 */
1118int __weak ftrace_arch_code_modify_post_process(void)
1119{
1120 return 0;
1121}
1122
e309b41d 1123static int __ftrace_modify_code(void *data)
3d083395 1124{
d61f82d0
SR
1125 int *command = data;
1126
a3583244 1127 if (*command & FTRACE_ENABLE_CALLS)
d61f82d0 1128 ftrace_replace_code(1);
a3583244 1129 else if (*command & FTRACE_DISABLE_CALLS)
d61f82d0
SR
1130 ftrace_replace_code(0);
1131
1132 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1133 ftrace_update_ftrace_func(ftrace_trace_function);
1134
5a45cfe1
SR
1135 if (*command & FTRACE_START_FUNC_RET)
1136 ftrace_enable_ftrace_graph_caller();
1137 else if (*command & FTRACE_STOP_FUNC_RET)
1138 ftrace_disable_ftrace_graph_caller();
1139
d61f82d0 1140 return 0;
3d083395
SR
1141}
1142
e309b41d 1143static void ftrace_run_update_code(int command)
3d083395 1144{
000ab691
SR
1145 int ret;
1146
1147 ret = ftrace_arch_code_modify_prepare();
1148 FTRACE_WARN_ON(ret);
1149 if (ret)
1150 return;
1151
784e2d76 1152 stop_machine(__ftrace_modify_code, &command, NULL);
000ab691
SR
1153
1154 ret = ftrace_arch_code_modify_post_process();
1155 FTRACE_WARN_ON(ret);
3d083395
SR
1156}
1157
d61f82d0 1158static ftrace_func_t saved_ftrace_func;
60a7ecf4 1159static int ftrace_start_up;
df4fc315
SR
1160
1161static void ftrace_startup_enable(int command)
1162{
1163 if (saved_ftrace_func != ftrace_trace_function) {
1164 saved_ftrace_func = ftrace_trace_function;
1165 command |= FTRACE_UPDATE_TRACE_FUNC;
1166 }
1167
1168 if (!command || !ftrace_enabled)
1169 return;
1170
1171 ftrace_run_update_code(command);
1172}
d61f82d0 1173
5a45cfe1 1174static void ftrace_startup(int command)
3d083395 1175{
4eebcc81
SR
1176 if (unlikely(ftrace_disabled))
1177 return;
1178
60a7ecf4 1179 ftrace_start_up++;
982c350b 1180 command |= FTRACE_ENABLE_CALLS;
d61f82d0 1181
df4fc315 1182 ftrace_startup_enable(command);
3d083395
SR
1183}
1184
5a45cfe1 1185static void ftrace_shutdown(int command)
3d083395 1186{
4eebcc81
SR
1187 if (unlikely(ftrace_disabled))
1188 return;
1189
60a7ecf4 1190 ftrace_start_up--;
9ea1a153
FW
1191 /*
1192 * Just warn in case of unbalance, no need to kill ftrace, it's not
1193 * critical but the ftrace_call callers may be never nopped again after
1194 * further ftrace uses.
1195 */
1196 WARN_ON_ONCE(ftrace_start_up < 0);
1197
60a7ecf4 1198 if (!ftrace_start_up)
d61f82d0 1199 command |= FTRACE_DISABLE_CALLS;
3d083395 1200
d61f82d0
SR
1201 if (saved_ftrace_func != ftrace_trace_function) {
1202 saved_ftrace_func = ftrace_trace_function;
1203 command |= FTRACE_UPDATE_TRACE_FUNC;
1204 }
3d083395 1205
d61f82d0 1206 if (!command || !ftrace_enabled)
e6ea44e9 1207 return;
d61f82d0
SR
1208
1209 ftrace_run_update_code(command);
3d083395
SR
1210}
1211
e309b41d 1212static void ftrace_startup_sysctl(void)
b0fc494f 1213{
d61f82d0
SR
1214 int command = FTRACE_ENABLE_MCOUNT;
1215
4eebcc81
SR
1216 if (unlikely(ftrace_disabled))
1217 return;
1218
d61f82d0
SR
1219 /* Force update next time */
1220 saved_ftrace_func = NULL;
60a7ecf4
SR
1221 /* ftrace_start_up is true if we want ftrace running */
1222 if (ftrace_start_up)
d61f82d0
SR
1223 command |= FTRACE_ENABLE_CALLS;
1224
1225 ftrace_run_update_code(command);
b0fc494f
SR
1226}
1227
e309b41d 1228static void ftrace_shutdown_sysctl(void)
b0fc494f 1229{
d61f82d0
SR
1230 int command = FTRACE_DISABLE_MCOUNT;
1231
4eebcc81
SR
1232 if (unlikely(ftrace_disabled))
1233 return;
1234
60a7ecf4
SR
1235 /* ftrace_start_up is true if ftrace is running */
1236 if (ftrace_start_up)
d61f82d0
SR
1237 command |= FTRACE_DISABLE_CALLS;
1238
1239 ftrace_run_update_code(command);
b0fc494f
SR
1240}
1241
3d083395
SR
1242static cycle_t ftrace_update_time;
1243static unsigned long ftrace_update_cnt;
1244unsigned long ftrace_update_tot_cnt;
1245
31e88909 1246static int ftrace_update_code(struct module *mod)
3d083395 1247{
e94142a6 1248 struct dyn_ftrace *p;
f22f9a89 1249 cycle_t start, stop;
3d083395 1250
750ed1a4 1251 start = ftrace_now(raw_smp_processor_id());
3d083395
SR
1252 ftrace_update_cnt = 0;
1253
e94142a6 1254 while (ftrace_new_addrs) {
3d083395 1255
08f5ac90
SR
1256 /* If something went wrong, bail without enabling anything */
1257 if (unlikely(ftrace_disabled))
1258 return -1;
f22f9a89 1259
e94142a6 1260 p = ftrace_new_addrs;
ee000b7f 1261 ftrace_new_addrs = p->newlist;
e94142a6 1262 p->flags = 0L;
f22f9a89 1263
08f5ac90 1264 /* convert record (i.e, patch mcount-call with NOP) */
31e88909 1265 if (ftrace_code_disable(mod, p)) {
08f5ac90
SR
1266 p->flags |= FTRACE_FL_CONVERTED;
1267 ftrace_update_cnt++;
1268 } else
1269 ftrace_free_rec(p);
3d083395
SR
1270 }
1271
750ed1a4 1272 stop = ftrace_now(raw_smp_processor_id());
3d083395
SR
1273 ftrace_update_time = stop - start;
1274 ftrace_update_tot_cnt += ftrace_update_cnt;
1275
16444a8a
ACM
1276 return 0;
1277}
1278
68bf21aa 1279static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
3c1720f0
SR
1280{
1281 struct ftrace_page *pg;
1282 int cnt;
1283 int i;
3c1720f0
SR
1284
1285 /* allocate a few pages */
1286 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1287 if (!ftrace_pages_start)
1288 return -1;
1289
1290 /*
1291 * Allocate a few more pages.
1292 *
1293 * TODO: have some parser search vmlinux before
1294 * final linking to find all calls to ftrace.
1295 * Then we can:
1296 * a) know how many pages to allocate.
1297 * and/or
1298 * b) set up the table then.
1299 *
1300 * The dynamic code is still necessary for
1301 * modules.
1302 */
1303
1304 pg = ftrace_pages = ftrace_pages_start;
1305
68bf21aa 1306 cnt = num_to_init / ENTRIES_PER_PAGE;
08f5ac90 1307 pr_info("ftrace: allocating %ld entries in %d pages\n",
5821e1b7 1308 num_to_init, cnt + 1);
3c1720f0
SR
1309
1310 for (i = 0; i < cnt; i++) {
1311 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1312
1313 /* If we fail, we'll try later anyway */
1314 if (!pg->next)
1315 break;
1316
1317 pg = pg->next;
1318 }
1319
1320 return 0;
1321}
1322
5072c59f
SR
1323enum {
1324 FTRACE_ITER_FILTER = (1 << 0),
689fd8b6 1325 FTRACE_ITER_NOTRACE = (1 << 1),
1326 FTRACE_ITER_FAILURES = (1 << 2),
1327 FTRACE_ITER_PRINTALL = (1 << 3),
1328 FTRACE_ITER_HASH = (1 << 4),
5072c59f
SR
1329};
1330
1331#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1332
1333struct ftrace_iterator {
5072c59f 1334 struct ftrace_page *pg;
8fc0c701 1335 int hidx;
431aa3fb 1336 int idx;
5072c59f 1337 unsigned flags;
689fd8b6 1338 struct trace_parser parser;
5072c59f
SR
1339};
1340
8fc0c701
SR
1341static void *
1342t_hash_next(struct seq_file *m, void *v, loff_t *pos)
1343{
1344 struct ftrace_iterator *iter = m->private;
1345 struct hlist_node *hnd = v;
1346 struct hlist_head *hhd;
1347
1348 WARN_ON(!(iter->flags & FTRACE_ITER_HASH));
1349
1350 (*pos)++;
1351
1352 retry:
1353 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1354 return NULL;
1355
1356 hhd = &ftrace_func_hash[iter->hidx];
1357
1358 if (hlist_empty(hhd)) {
1359 iter->hidx++;
1360 hnd = NULL;
1361 goto retry;
1362 }
1363
1364 if (!hnd)
1365 hnd = hhd->first;
1366 else {
1367 hnd = hnd->next;
1368 if (!hnd) {
1369 iter->hidx++;
1370 goto retry;
1371 }
1372 }
1373
1374 return hnd;
1375}
1376
1377static void *t_hash_start(struct seq_file *m, loff_t *pos)
1378{
1379 struct ftrace_iterator *iter = m->private;
1380 void *p = NULL;
d82d6244
LZ
1381 loff_t l;
1382
1383 if (!(iter->flags & FTRACE_ITER_HASH))
1384 *pos = 0;
8fc0c701
SR
1385
1386 iter->flags |= FTRACE_ITER_HASH;
1387
d82d6244
LZ
1388 iter->hidx = 0;
1389 for (l = 0; l <= *pos; ) {
1390 p = t_hash_next(m, p, &l);
1391 if (!p)
1392 break;
1393 }
1394 return p;
8fc0c701
SR
1395}
1396
1397static int t_hash_show(struct seq_file *m, void *v)
1398{
b6887d79 1399 struct ftrace_func_probe *rec;
8fc0c701 1400 struct hlist_node *hnd = v;
8fc0c701 1401
b6887d79 1402 rec = hlist_entry(hnd, struct ftrace_func_probe, node);
8fc0c701 1403
809dcf29
SR
1404 if (rec->ops->print)
1405 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1406
b375a11a 1407 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
8fc0c701
SR
1408
1409 if (rec->data)
1410 seq_printf(m, ":%p", rec->data);
1411 seq_putc(m, '\n');
1412
1413 return 0;
1414}
1415
e309b41d 1416static void *
5072c59f
SR
1417t_next(struct seq_file *m, void *v, loff_t *pos)
1418{
1419 struct ftrace_iterator *iter = m->private;
1420 struct dyn_ftrace *rec = NULL;
1421
8fc0c701
SR
1422 if (iter->flags & FTRACE_ITER_HASH)
1423 return t_hash_next(m, v, pos);
1424
5072c59f
SR
1425 (*pos)++;
1426
0c75a3ed
SR
1427 if (iter->flags & FTRACE_ITER_PRINTALL)
1428 return NULL;
1429
5072c59f
SR
1430 retry:
1431 if (iter->idx >= iter->pg->index) {
1432 if (iter->pg->next) {
1433 iter->pg = iter->pg->next;
1434 iter->idx = 0;
1435 goto retry;
1436 }
1437 } else {
1438 rec = &iter->pg->records[iter->idx++];
a9fdda33
SR
1439 if ((rec->flags & FTRACE_FL_FREE) ||
1440
1441 (!(iter->flags & FTRACE_ITER_FAILURES) &&
eb9a7bf0
AS
1442 (rec->flags & FTRACE_FL_FAILED)) ||
1443
1444 ((iter->flags & FTRACE_ITER_FAILURES) &&
a9fdda33 1445 !(rec->flags & FTRACE_FL_FAILED)) ||
eb9a7bf0 1446
0183fb1c
SR
1447 ((iter->flags & FTRACE_ITER_FILTER) &&
1448 !(rec->flags & FTRACE_FL_FILTER)) ||
1449
41c52c0d
SR
1450 ((iter->flags & FTRACE_ITER_NOTRACE) &&
1451 !(rec->flags & FTRACE_FL_NOTRACE))) {
5072c59f
SR
1452 rec = NULL;
1453 goto retry;
1454 }
1455 }
1456
5072c59f
SR
1457 return rec;
1458}
1459
1460static void *t_start(struct seq_file *m, loff_t *pos)
1461{
1462 struct ftrace_iterator *iter = m->private;
1463 void *p = NULL;
694ce0a5 1464 loff_t l;
5072c59f 1465
8fc0c701 1466 mutex_lock(&ftrace_lock);
0c75a3ed
SR
1467 /*
1468 * For set_ftrace_filter reading, if we have the filter
1469 * off, we can short cut and just print out that all
1470 * functions are enabled.
1471 */
1472 if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
1473 if (*pos > 0)
8fc0c701 1474 return t_hash_start(m, pos);
0c75a3ed 1475 iter->flags |= FTRACE_ITER_PRINTALL;
0c75a3ed
SR
1476 return iter;
1477 }
1478
8fc0c701
SR
1479 if (iter->flags & FTRACE_ITER_HASH)
1480 return t_hash_start(m, pos);
1481
694ce0a5
LZ
1482 iter->pg = ftrace_pages_start;
1483 iter->idx = 0;
1484 for (l = 0; l <= *pos; ) {
1485 p = t_next(m, p, &l);
1486 if (!p)
1487 break;
50cdaf08 1488 }
5821e1b7 1489
694ce0a5 1490 if (!p && iter->flags & FTRACE_ITER_FILTER)
8fc0c701
SR
1491 return t_hash_start(m, pos);
1492
5072c59f
SR
1493 return p;
1494}
1495
1496static void t_stop(struct seq_file *m, void *p)
1497{
8fc0c701 1498 mutex_unlock(&ftrace_lock);
5072c59f
SR
1499}
1500
1501static int t_show(struct seq_file *m, void *v)
1502{
0c75a3ed 1503 struct ftrace_iterator *iter = m->private;
5072c59f 1504 struct dyn_ftrace *rec = v;
5072c59f 1505
8fc0c701
SR
1506 if (iter->flags & FTRACE_ITER_HASH)
1507 return t_hash_show(m, v);
1508
0c75a3ed
SR
1509 if (iter->flags & FTRACE_ITER_PRINTALL) {
1510 seq_printf(m, "#### all functions enabled ####\n");
1511 return 0;
1512 }
1513
5072c59f
SR
1514 if (!rec)
1515 return 0;
1516
b375a11a 1517 seq_printf(m, "%ps\n", (void *)rec->ip);
5072c59f
SR
1518
1519 return 0;
1520}
1521
88e9d34c 1522static const struct seq_operations show_ftrace_seq_ops = {
5072c59f
SR
1523 .start = t_start,
1524 .next = t_next,
1525 .stop = t_stop,
1526 .show = t_show,
1527};
1528
e309b41d 1529static int
5072c59f
SR
1530ftrace_avail_open(struct inode *inode, struct file *file)
1531{
1532 struct ftrace_iterator *iter;
1533 int ret;
1534
4eebcc81
SR
1535 if (unlikely(ftrace_disabled))
1536 return -ENODEV;
1537
5072c59f
SR
1538 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1539 if (!iter)
1540 return -ENOMEM;
1541
1542 iter->pg = ftrace_pages_start;
5072c59f
SR
1543
1544 ret = seq_open(file, &show_ftrace_seq_ops);
1545 if (!ret) {
1546 struct seq_file *m = file->private_data;
4bf39a94 1547
5072c59f 1548 m->private = iter;
4bf39a94 1549 } else {
5072c59f 1550 kfree(iter);
4bf39a94 1551 }
5072c59f
SR
1552
1553 return ret;
1554}
1555
eb9a7bf0
AS
1556static int
1557ftrace_failures_open(struct inode *inode, struct file *file)
1558{
1559 int ret;
1560 struct seq_file *m;
1561 struct ftrace_iterator *iter;
1562
1563 ret = ftrace_avail_open(inode, file);
1564 if (!ret) {
1565 m = (struct seq_file *)file->private_data;
1566 iter = (struct ftrace_iterator *)m->private;
1567 iter->flags = FTRACE_ITER_FAILURES;
1568 }
1569
1570 return ret;
1571}
1572
1573
41c52c0d 1574static void ftrace_filter_reset(int enable)
5072c59f
SR
1575{
1576 struct ftrace_page *pg;
1577 struct dyn_ftrace *rec;
41c52c0d 1578 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
5072c59f 1579
52baf119 1580 mutex_lock(&ftrace_lock);
41c52c0d
SR
1581 if (enable)
1582 ftrace_filtered = 0;
265c831c
SR
1583 do_for_each_ftrace_rec(pg, rec) {
1584 if (rec->flags & FTRACE_FL_FAILED)
1585 continue;
1586 rec->flags &= ~type;
1587 } while_for_each_ftrace_rec();
52baf119 1588 mutex_unlock(&ftrace_lock);
5072c59f
SR
1589}
1590
e309b41d 1591static int
41c52c0d 1592ftrace_regex_open(struct inode *inode, struct file *file, int enable)
5072c59f
SR
1593{
1594 struct ftrace_iterator *iter;
1595 int ret = 0;
1596
4eebcc81
SR
1597 if (unlikely(ftrace_disabled))
1598 return -ENODEV;
1599
5072c59f
SR
1600 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1601 if (!iter)
1602 return -ENOMEM;
1603
689fd8b6 1604 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
1605 kfree(iter);
1606 return -ENOMEM;
1607 }
1608
41c52c0d 1609 mutex_lock(&ftrace_regex_lock);
5072c59f 1610 if ((file->f_mode & FMODE_WRITE) &&
8650ae32 1611 (file->f_flags & O_TRUNC))
41c52c0d 1612 ftrace_filter_reset(enable);
5072c59f
SR
1613
1614 if (file->f_mode & FMODE_READ) {
1615 iter->pg = ftrace_pages_start;
41c52c0d
SR
1616 iter->flags = enable ? FTRACE_ITER_FILTER :
1617 FTRACE_ITER_NOTRACE;
5072c59f
SR
1618
1619 ret = seq_open(file, &show_ftrace_seq_ops);
1620 if (!ret) {
1621 struct seq_file *m = file->private_data;
1622 m->private = iter;
79fe249c
LZ
1623 } else {
1624 trace_parser_put(&iter->parser);
5072c59f 1625 kfree(iter);
79fe249c 1626 }
5072c59f
SR
1627 } else
1628 file->private_data = iter;
41c52c0d 1629 mutex_unlock(&ftrace_regex_lock);
5072c59f
SR
1630
1631 return ret;
1632}
1633
41c52c0d
SR
1634static int
1635ftrace_filter_open(struct inode *inode, struct file *file)
1636{
1637 return ftrace_regex_open(inode, file, 1);
1638}
1639
1640static int
1641ftrace_notrace_open(struct inode *inode, struct file *file)
1642{
1643 return ftrace_regex_open(inode, file, 0);
1644}
1645
e309b41d 1646static loff_t
41c52c0d 1647ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
5072c59f
SR
1648{
1649 loff_t ret;
1650
1651 if (file->f_mode & FMODE_READ)
1652 ret = seq_lseek(file, offset, origin);
1653 else
1654 file->f_pos = ret = 1;
1655
1656 return ret;
1657}
1658
1659enum {
1660 MATCH_FULL,
1661 MATCH_FRONT_ONLY,
1662 MATCH_MIDDLE_ONLY,
1663 MATCH_END_ONLY,
1664};
1665
9f4801e3
SR
1666/*
1667 * (static function - no need for kernel doc)
1668 *
1669 * Pass in a buffer containing a glob and this function will
1670 * set search to point to the search part of the buffer and
1671 * return the type of search it is (see enum above).
1672 * This does modify buff.
1673 *
1674 * Returns enum type.
1675 * search returns the pointer to use for comparison.
1676 * not returns 1 if buff started with a '!'
1677 * 0 otherwise.
1678 */
1679static int
64e7c440 1680ftrace_setup_glob(char *buff, int len, char **search, int *not)
5072c59f 1681{
5072c59f 1682 int type = MATCH_FULL;
9f4801e3 1683 int i;
ea3a6d6d
SR
1684
1685 if (buff[0] == '!') {
9f4801e3 1686 *not = 1;
ea3a6d6d
SR
1687 buff++;
1688 len--;
9f4801e3
SR
1689 } else
1690 *not = 0;
1691
1692 *search = buff;
5072c59f
SR
1693
1694 for (i = 0; i < len; i++) {
1695 if (buff[i] == '*') {
1696 if (!i) {
9f4801e3 1697 *search = buff + 1;
5072c59f 1698 type = MATCH_END_ONLY;
5072c59f 1699 } else {
9f4801e3 1700 if (type == MATCH_END_ONLY)
5072c59f 1701 type = MATCH_MIDDLE_ONLY;
9f4801e3 1702 else
5072c59f 1703 type = MATCH_FRONT_ONLY;
5072c59f
SR
1704 buff[i] = 0;
1705 break;
1706 }
1707 }
1708 }
1709
9f4801e3
SR
1710 return type;
1711}
1712
64e7c440 1713static int ftrace_match(char *str, char *regex, int len, int type)
9f4801e3 1714{
9f4801e3
SR
1715 int matched = 0;
1716 char *ptr;
1717
9f4801e3
SR
1718 switch (type) {
1719 case MATCH_FULL:
1720 if (strcmp(str, regex) == 0)
1721 matched = 1;
1722 break;
1723 case MATCH_FRONT_ONLY:
1724 if (strncmp(str, regex, len) == 0)
1725 matched = 1;
1726 break;
1727 case MATCH_MIDDLE_ONLY:
1728 if (strstr(str, regex))
1729 matched = 1;
1730 break;
1731 case MATCH_END_ONLY:
1732 ptr = strstr(str, regex);
1733 if (ptr && (ptr[len] == 0))
1734 matched = 1;
1735 break;
1736 }
1737
1738 return matched;
1739}
1740
64e7c440
SR
1741static int
1742ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
1743{
1744 char str[KSYM_SYMBOL_LEN];
1745
1746 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1747 return ftrace_match(str, regex, len, type);
1748}
1749
9f4801e3
SR
1750static void ftrace_match_records(char *buff, int len, int enable)
1751{
6a24a244 1752 unsigned int search_len;
9f4801e3
SR
1753 struct ftrace_page *pg;
1754 struct dyn_ftrace *rec;
6a24a244
SR
1755 unsigned long flag;
1756 char *search;
9f4801e3 1757 int type;
9f4801e3
SR
1758 int not;
1759
6a24a244 1760 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
9f4801e3
SR
1761 type = ftrace_setup_glob(buff, len, &search, &not);
1762
1763 search_len = strlen(search);
1764
52baf119 1765 mutex_lock(&ftrace_lock);
265c831c 1766 do_for_each_ftrace_rec(pg, rec) {
265c831c
SR
1767
1768 if (rec->flags & FTRACE_FL_FAILED)
1769 continue;
9f4801e3
SR
1770
1771 if (ftrace_match_record(rec, search, search_len, type)) {
265c831c
SR
1772 if (not)
1773 rec->flags &= ~flag;
1774 else
1775 rec->flags |= flag;
1776 }
e68746a2
SR
1777 /*
1778 * Only enable filtering if we have a function that
1779 * is filtered on.
1780 */
1781 if (enable && (rec->flags & FTRACE_FL_FILTER))
1782 ftrace_filtered = 1;
265c831c 1783 } while_for_each_ftrace_rec();
52baf119 1784 mutex_unlock(&ftrace_lock);
5072c59f
SR
1785}
1786
64e7c440
SR
1787static int
1788ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
1789 char *regex, int len, int type)
1790{
1791 char str[KSYM_SYMBOL_LEN];
1792 char *modname;
1793
1794 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
1795
1796 if (!modname || strcmp(modname, mod))
1797 return 0;
1798
1799 /* blank search means to match all funcs in the mod */
1800 if (len)
1801 return ftrace_match(str, regex, len, type);
1802 else
1803 return 1;
1804}
1805
1806static void ftrace_match_module_records(char *buff, char *mod, int enable)
1807{
6a24a244 1808 unsigned search_len = 0;
64e7c440
SR
1809 struct ftrace_page *pg;
1810 struct dyn_ftrace *rec;
1811 int type = MATCH_FULL;
6a24a244
SR
1812 char *search = buff;
1813 unsigned long flag;
64e7c440
SR
1814 int not = 0;
1815
6a24a244
SR
1816 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1817
64e7c440
SR
1818 /* blank or '*' mean the same */
1819 if (strcmp(buff, "*") == 0)
1820 buff[0] = 0;
1821
1822 /* handle the case of 'dont filter this module' */
1823 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
1824 buff[0] = 0;
1825 not = 1;
1826 }
1827
1828 if (strlen(buff)) {
1829 type = ftrace_setup_glob(buff, strlen(buff), &search, &not);
1830 search_len = strlen(search);
1831 }
1832
52baf119 1833 mutex_lock(&ftrace_lock);
64e7c440
SR
1834 do_for_each_ftrace_rec(pg, rec) {
1835
1836 if (rec->flags & FTRACE_FL_FAILED)
1837 continue;
1838
1839 if (ftrace_match_module_record(rec, mod,
1840 search, search_len, type)) {
1841 if (not)
1842 rec->flags &= ~flag;
1843 else
1844 rec->flags |= flag;
1845 }
e68746a2
SR
1846 if (enable && (rec->flags & FTRACE_FL_FILTER))
1847 ftrace_filtered = 1;
64e7c440
SR
1848
1849 } while_for_each_ftrace_rec();
52baf119 1850 mutex_unlock(&ftrace_lock);
64e7c440
SR
1851}
1852
f6180773
SR
1853/*
1854 * We register the module command as a template to show others how
1855 * to register the a command as well.
1856 */
1857
1858static int
1859ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
1860{
1861 char *mod;
1862
1863 /*
1864 * cmd == 'mod' because we only registered this func
1865 * for the 'mod' ftrace_func_command.
1866 * But if you register one func with multiple commands,
1867 * you can tell which command was used by the cmd
1868 * parameter.
1869 */
1870
1871 /* we must have a module name */
1872 if (!param)
1873 return -EINVAL;
1874
1875 mod = strsep(&param, ":");
1876 if (!strlen(mod))
1877 return -EINVAL;
1878
1879 ftrace_match_module_records(func, mod, enable);
1880 return 0;
1881}
1882
1883static struct ftrace_func_command ftrace_mod_cmd = {
1884 .name = "mod",
1885 .func = ftrace_mod_callback,
1886};
1887
1888static int __init ftrace_mod_cmd_init(void)
1889{
1890 return register_ftrace_command(&ftrace_mod_cmd);
1891}
1892device_initcall(ftrace_mod_cmd_init);
1893
59df055f 1894static void
b6887d79 1895function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
59df055f 1896{
b6887d79 1897 struct ftrace_func_probe *entry;
59df055f
SR
1898 struct hlist_head *hhd;
1899 struct hlist_node *n;
1900 unsigned long key;
1901 int resched;
1902
1903 key = hash_long(ip, FTRACE_HASH_BITS);
1904
1905 hhd = &ftrace_func_hash[key];
1906
1907 if (hlist_empty(hhd))
1908 return;
1909
1910 /*
1911 * Disable preemption for these calls to prevent a RCU grace
1912 * period. This syncs the hash iteration and freeing of items
1913 * on the hash. rcu_read_lock is too dangerous here.
1914 */
1915 resched = ftrace_preempt_disable();
1916 hlist_for_each_entry_rcu(entry, n, hhd, node) {
1917 if (entry->ip == ip)
1918 entry->ops->func(ip, parent_ip, &entry->data);
1919 }
1920 ftrace_preempt_enable(resched);
1921}
1922
b6887d79 1923static struct ftrace_ops trace_probe_ops __read_mostly =
59df055f 1924{
fb9fb015 1925 .func = function_trace_probe_call,
59df055f
SR
1926};
1927
b6887d79 1928static int ftrace_probe_registered;
59df055f 1929
b6887d79 1930static void __enable_ftrace_function_probe(void)
59df055f
SR
1931{
1932 int i;
1933
b6887d79 1934 if (ftrace_probe_registered)
59df055f
SR
1935 return;
1936
1937 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1938 struct hlist_head *hhd = &ftrace_func_hash[i];
1939 if (hhd->first)
1940 break;
1941 }
1942 /* Nothing registered? */
1943 if (i == FTRACE_FUNC_HASHSIZE)
1944 return;
1945
b6887d79 1946 __register_ftrace_function(&trace_probe_ops);
59df055f 1947 ftrace_startup(0);
b6887d79 1948 ftrace_probe_registered = 1;
59df055f
SR
1949}
1950
b6887d79 1951static void __disable_ftrace_function_probe(void)
59df055f
SR
1952{
1953 int i;
1954
b6887d79 1955 if (!ftrace_probe_registered)
59df055f
SR
1956 return;
1957
1958 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1959 struct hlist_head *hhd = &ftrace_func_hash[i];
1960 if (hhd->first)
1961 return;
1962 }
1963
1964 /* no more funcs left */
b6887d79 1965 __unregister_ftrace_function(&trace_probe_ops);
59df055f 1966 ftrace_shutdown(0);
b6887d79 1967 ftrace_probe_registered = 0;
59df055f
SR
1968}
1969
1970
1971static void ftrace_free_entry_rcu(struct rcu_head *rhp)
1972{
b6887d79
SR
1973 struct ftrace_func_probe *entry =
1974 container_of(rhp, struct ftrace_func_probe, rcu);
59df055f
SR
1975
1976 if (entry->ops->free)
1977 entry->ops->free(&entry->data);
1978 kfree(entry);
1979}
1980
1981
1982int
b6887d79 1983register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
59df055f
SR
1984 void *data)
1985{
b6887d79 1986 struct ftrace_func_probe *entry;
59df055f
SR
1987 struct ftrace_page *pg;
1988 struct dyn_ftrace *rec;
59df055f 1989 int type, len, not;
6a24a244 1990 unsigned long key;
59df055f
SR
1991 int count = 0;
1992 char *search;
1993
1994 type = ftrace_setup_glob(glob, strlen(glob), &search, &not);
1995 len = strlen(search);
1996
b6887d79 1997 /* we do not support '!' for function probes */
59df055f
SR
1998 if (WARN_ON(not))
1999 return -EINVAL;
2000
2001 mutex_lock(&ftrace_lock);
2002 do_for_each_ftrace_rec(pg, rec) {
2003
2004 if (rec->flags & FTRACE_FL_FAILED)
2005 continue;
2006
2007 if (!ftrace_match_record(rec, search, len, type))
2008 continue;
2009
2010 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2011 if (!entry) {
b6887d79 2012 /* If we did not process any, then return error */
59df055f
SR
2013 if (!count)
2014 count = -ENOMEM;
2015 goto out_unlock;
2016 }
2017
2018 count++;
2019
2020 entry->data = data;
2021
2022 /*
2023 * The caller might want to do something special
2024 * for each function we find. We call the callback
2025 * to give the caller an opportunity to do so.
2026 */
2027 if (ops->callback) {
2028 if (ops->callback(rec->ip, &entry->data) < 0) {
2029 /* caller does not like this func */
2030 kfree(entry);
2031 continue;
2032 }
2033 }
2034
2035 entry->ops = ops;
2036 entry->ip = rec->ip;
2037
2038 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2039 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2040
2041 } while_for_each_ftrace_rec();
b6887d79 2042 __enable_ftrace_function_probe();
59df055f
SR
2043
2044 out_unlock:
2045 mutex_unlock(&ftrace_lock);
2046
2047 return count;
2048}
2049
2050enum {
b6887d79
SR
2051 PROBE_TEST_FUNC = 1,
2052 PROBE_TEST_DATA = 2
59df055f
SR
2053};
2054
2055static void
b6887d79 2056__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
59df055f
SR
2057 void *data, int flags)
2058{
b6887d79 2059 struct ftrace_func_probe *entry;
59df055f
SR
2060 struct hlist_node *n, *tmp;
2061 char str[KSYM_SYMBOL_LEN];
2062 int type = MATCH_FULL;
2063 int i, len = 0;
2064 char *search;
2065
b36461da 2066 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
59df055f 2067 glob = NULL;
b36461da 2068 else if (glob) {
59df055f
SR
2069 int not;
2070
2071 type = ftrace_setup_glob(glob, strlen(glob), &search, &not);
2072 len = strlen(search);
2073
b6887d79 2074 /* we do not support '!' for function probes */
59df055f
SR
2075 if (WARN_ON(not))
2076 return;
2077 }
2078
2079 mutex_lock(&ftrace_lock);
2080 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2081 struct hlist_head *hhd = &ftrace_func_hash[i];
2082
2083 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2084
2085 /* break up if statements for readability */
b6887d79 2086 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
59df055f
SR
2087 continue;
2088
b6887d79 2089 if ((flags & PROBE_TEST_DATA) && entry->data != data)
59df055f
SR
2090 continue;
2091
2092 /* do this last, since it is the most expensive */
2093 if (glob) {
2094 kallsyms_lookup(entry->ip, NULL, NULL,
2095 NULL, str);
2096 if (!ftrace_match(str, glob, len, type))
2097 continue;
2098 }
2099
2100 hlist_del(&entry->node);
2101 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2102 }
2103 }
b6887d79 2104 __disable_ftrace_function_probe();
59df055f
SR
2105 mutex_unlock(&ftrace_lock);
2106}
2107
2108void
b6887d79 2109unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
59df055f
SR
2110 void *data)
2111{
b6887d79
SR
2112 __unregister_ftrace_function_probe(glob, ops, data,
2113 PROBE_TEST_FUNC | PROBE_TEST_DATA);
59df055f
SR
2114}
2115
2116void
b6887d79 2117unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
59df055f 2118{
b6887d79 2119 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
59df055f
SR
2120}
2121
b6887d79 2122void unregister_ftrace_function_probe_all(char *glob)
59df055f 2123{
b6887d79 2124 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
59df055f
SR
2125}
2126
f6180773
SR
2127static LIST_HEAD(ftrace_commands);
2128static DEFINE_MUTEX(ftrace_cmd_mutex);
2129
2130int register_ftrace_command(struct ftrace_func_command *cmd)
2131{
2132 struct ftrace_func_command *p;
2133 int ret = 0;
2134
2135 mutex_lock(&ftrace_cmd_mutex);
2136 list_for_each_entry(p, &ftrace_commands, list) {
2137 if (strcmp(cmd->name, p->name) == 0) {
2138 ret = -EBUSY;
2139 goto out_unlock;
2140 }
2141 }
2142 list_add(&cmd->list, &ftrace_commands);
2143 out_unlock:
2144 mutex_unlock(&ftrace_cmd_mutex);
2145
2146 return ret;
2147}
2148
2149int unregister_ftrace_command(struct ftrace_func_command *cmd)
2150{
2151 struct ftrace_func_command *p, *n;
2152 int ret = -ENODEV;
2153
2154 mutex_lock(&ftrace_cmd_mutex);
2155 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2156 if (strcmp(cmd->name, p->name) == 0) {
2157 ret = 0;
2158 list_del_init(&p->list);
2159 goto out_unlock;
2160 }
2161 }
2162 out_unlock:
2163 mutex_unlock(&ftrace_cmd_mutex);
2164
2165 return ret;
2166}
2167
64e7c440
SR
2168static int ftrace_process_regex(char *buff, int len, int enable)
2169{
f6180773 2170 char *func, *command, *next = buff;
6a24a244 2171 struct ftrace_func_command *p;
f6180773 2172 int ret = -EINVAL;
64e7c440
SR
2173
2174 func = strsep(&next, ":");
2175
2176 if (!next) {
2177 ftrace_match_records(func, len, enable);
2178 return 0;
2179 }
2180
f6180773 2181 /* command found */
64e7c440
SR
2182
2183 command = strsep(&next, ":");
2184
f6180773
SR
2185 mutex_lock(&ftrace_cmd_mutex);
2186 list_for_each_entry(p, &ftrace_commands, list) {
2187 if (strcmp(p->name, command) == 0) {
2188 ret = p->func(func, command, next, enable);
2189 goto out_unlock;
2190 }
64e7c440 2191 }
f6180773
SR
2192 out_unlock:
2193 mutex_unlock(&ftrace_cmd_mutex);
64e7c440 2194
f6180773 2195 return ret;
64e7c440
SR
2196}
2197
e309b41d 2198static ssize_t
41c52c0d
SR
2199ftrace_regex_write(struct file *file, const char __user *ubuf,
2200 size_t cnt, loff_t *ppos, int enable)
5072c59f
SR
2201{
2202 struct ftrace_iterator *iter;
689fd8b6 2203 struct trace_parser *parser;
2204 ssize_t ret, read;
5072c59f 2205
4ba7978e 2206 if (!cnt)
5072c59f
SR
2207 return 0;
2208
41c52c0d 2209 mutex_lock(&ftrace_regex_lock);
5072c59f
SR
2210
2211 if (file->f_mode & FMODE_READ) {
2212 struct seq_file *m = file->private_data;
2213 iter = m->private;
2214 } else
2215 iter = file->private_data;
2216
689fd8b6 2217 parser = &iter->parser;
2218 read = trace_get_user(parser, ubuf, cnt, ppos);
5072c59f 2219
4ba7978e 2220 if (read >= 0 && trace_parser_loaded(parser) &&
689fd8b6 2221 !trace_parser_cont(parser)) {
2222 ret = ftrace_process_regex(parser->buffer,
2223 parser->idx, enable);
5072c59f 2224 if (ret)
ed146b25 2225 goto out_unlock;
5072c59f 2226
689fd8b6 2227 trace_parser_clear(parser);
eda1e328 2228 }
5072c59f 2229
5072c59f 2230 ret = read;
ed146b25 2231out_unlock:
689fd8b6 2232 mutex_unlock(&ftrace_regex_lock);
ed146b25 2233
5072c59f
SR
2234 return ret;
2235}
2236
41c52c0d
SR
2237static ssize_t
2238ftrace_filter_write(struct file *file, const char __user *ubuf,
2239 size_t cnt, loff_t *ppos)
2240{
2241 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2242}
2243
2244static ssize_t
2245ftrace_notrace_write(struct file *file, const char __user *ubuf,
2246 size_t cnt, loff_t *ppos)
2247{
2248 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2249}
2250
2251static void
2252ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
2253{
2254 if (unlikely(ftrace_disabled))
2255 return;
2256
2257 mutex_lock(&ftrace_regex_lock);
2258 if (reset)
2259 ftrace_filter_reset(enable);
2260 if (buf)
7f24b31b 2261 ftrace_match_records(buf, len, enable);
41c52c0d
SR
2262 mutex_unlock(&ftrace_regex_lock);
2263}
2264
77a2b37d
SR
2265/**
2266 * ftrace_set_filter - set a function to filter on in ftrace
2267 * @buf - the string that holds the function filter text.
2268 * @len - the length of the string.
2269 * @reset - non zero to reset all filters before applying this filter.
2270 *
2271 * Filters denote which functions should be enabled when tracing is enabled.
2272 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2273 */
e309b41d 2274void ftrace_set_filter(unsigned char *buf, int len, int reset)
77a2b37d 2275{
41c52c0d
SR
2276 ftrace_set_regex(buf, len, reset, 1);
2277}
4eebcc81 2278
41c52c0d
SR
2279/**
2280 * ftrace_set_notrace - set a function to not trace in ftrace
2281 * @buf - the string that holds the function notrace text.
2282 * @len - the length of the string.
2283 * @reset - non zero to reset all filters before applying this filter.
2284 *
2285 * Notrace Filters denote which functions should not be enabled when tracing
2286 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2287 * for tracing.
2288 */
2289void ftrace_set_notrace(unsigned char *buf, int len, int reset)
2290{
2291 ftrace_set_regex(buf, len, reset, 0);
77a2b37d
SR
2292}
2293
2af15d6a
SR
2294/*
2295 * command line interface to allow users to set filters on boot up.
2296 */
2297#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2298static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
2299static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
2300
2301static int __init set_ftrace_notrace(char *str)
2302{
2303 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
2304 return 1;
2305}
2306__setup("ftrace_notrace=", set_ftrace_notrace);
2307
2308static int __init set_ftrace_filter(char *str)
2309{
2310 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
2311 return 1;
2312}
2313__setup("ftrace_filter=", set_ftrace_filter);
2314
2315static void __init set_ftrace_early_filter(char *buf, int enable)
2316{
2317 char *func;
2318
2319 while (buf) {
2320 func = strsep(&buf, ",");
2321 ftrace_set_regex(func, strlen(func), 0, enable);
2322 }
2323}
2324
2325static void __init set_ftrace_early_filters(void)
2326{
2327 if (ftrace_filter_buf[0])
2328 set_ftrace_early_filter(ftrace_filter_buf, 1);
2329 if (ftrace_notrace_buf[0])
2330 set_ftrace_early_filter(ftrace_notrace_buf, 0);
2331}
2332
e309b41d 2333static int
41c52c0d 2334ftrace_regex_release(struct inode *inode, struct file *file, int enable)
5072c59f
SR
2335{
2336 struct seq_file *m = (struct seq_file *)file->private_data;
2337 struct ftrace_iterator *iter;
689fd8b6 2338 struct trace_parser *parser;
5072c59f 2339
41c52c0d 2340 mutex_lock(&ftrace_regex_lock);
5072c59f
SR
2341 if (file->f_mode & FMODE_READ) {
2342 iter = m->private;
2343
2344 seq_release(inode, file);
2345 } else
2346 iter = file->private_data;
2347
689fd8b6 2348 parser = &iter->parser;
2349 if (trace_parser_loaded(parser)) {
2350 parser->buffer[parser->idx] = 0;
2351 ftrace_match_records(parser->buffer, parser->idx, enable);
5072c59f
SR
2352 }
2353
e6ea44e9 2354 mutex_lock(&ftrace_lock);
ee02a2e5 2355 if (ftrace_start_up && ftrace_enabled)
5072c59f 2356 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
e6ea44e9 2357 mutex_unlock(&ftrace_lock);
5072c59f 2358
689fd8b6 2359 trace_parser_put(parser);
5072c59f 2360 kfree(iter);
689fd8b6 2361
41c52c0d 2362 mutex_unlock(&ftrace_regex_lock);
5072c59f
SR
2363 return 0;
2364}
2365
41c52c0d
SR
2366static int
2367ftrace_filter_release(struct inode *inode, struct file *file)
2368{
2369 return ftrace_regex_release(inode, file, 1);
2370}
2371
2372static int
2373ftrace_notrace_release(struct inode *inode, struct file *file)
2374{
2375 return ftrace_regex_release(inode, file, 0);
2376}
2377
5e2336a0 2378static const struct file_operations ftrace_avail_fops = {
5072c59f
SR
2379 .open = ftrace_avail_open,
2380 .read = seq_read,
2381 .llseek = seq_lseek,
3be04b47 2382 .release = seq_release_private,
5072c59f
SR
2383};
2384
5e2336a0 2385static const struct file_operations ftrace_failures_fops = {
eb9a7bf0
AS
2386 .open = ftrace_failures_open,
2387 .read = seq_read,
2388 .llseek = seq_lseek,
3be04b47 2389 .release = seq_release_private,
eb9a7bf0
AS
2390};
2391
5e2336a0 2392static const struct file_operations ftrace_filter_fops = {
5072c59f 2393 .open = ftrace_filter_open,
850a80cf 2394 .read = seq_read,
5072c59f 2395 .write = ftrace_filter_write,
41c52c0d 2396 .llseek = ftrace_regex_lseek,
5072c59f
SR
2397 .release = ftrace_filter_release,
2398};
2399
5e2336a0 2400static const struct file_operations ftrace_notrace_fops = {
41c52c0d 2401 .open = ftrace_notrace_open,
850a80cf 2402 .read = seq_read,
41c52c0d
SR
2403 .write = ftrace_notrace_write,
2404 .llseek = ftrace_regex_lseek,
2405 .release = ftrace_notrace_release,
2406};
2407
ea4e2bc4
SR
2408#ifdef CONFIG_FUNCTION_GRAPH_TRACER
2409
2410static DEFINE_MUTEX(graph_lock);
2411
2412int ftrace_graph_count;
2413unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
2414
2415static void *
85951842 2416__g_next(struct seq_file *m, loff_t *pos)
ea4e2bc4 2417{
85951842 2418 if (*pos >= ftrace_graph_count)
ea4e2bc4 2419 return NULL;
a4ec5e0c 2420 return &ftrace_graph_funcs[*pos];
85951842 2421}
ea4e2bc4 2422
85951842
LZ
2423static void *
2424g_next(struct seq_file *m, void *v, loff_t *pos)
2425{
2426 (*pos)++;
2427 return __g_next(m, pos);
ea4e2bc4
SR
2428}
2429
2430static void *g_start(struct seq_file *m, loff_t *pos)
2431{
ea4e2bc4
SR
2432 mutex_lock(&graph_lock);
2433
f9349a8f
FW
2434 /* Nothing, tell g_show to print all functions are enabled */
2435 if (!ftrace_graph_count && !*pos)
2436 return (void *)1;
2437
85951842 2438 return __g_next(m, pos);
ea4e2bc4
SR
2439}
2440
2441static void g_stop(struct seq_file *m, void *p)
2442{
2443 mutex_unlock(&graph_lock);
2444}
2445
2446static int g_show(struct seq_file *m, void *v)
2447{
2448 unsigned long *ptr = v;
ea4e2bc4
SR
2449
2450 if (!ptr)
2451 return 0;
2452
f9349a8f
FW
2453 if (ptr == (unsigned long *)1) {
2454 seq_printf(m, "#### all functions enabled ####\n");
2455 return 0;
2456 }
2457
b375a11a 2458 seq_printf(m, "%ps\n", (void *)*ptr);
ea4e2bc4
SR
2459
2460 return 0;
2461}
2462
88e9d34c 2463static const struct seq_operations ftrace_graph_seq_ops = {
ea4e2bc4
SR
2464 .start = g_start,
2465 .next = g_next,
2466 .stop = g_stop,
2467 .show = g_show,
2468};
2469
2470static int
2471ftrace_graph_open(struct inode *inode, struct file *file)
2472{
2473 int ret = 0;
2474
2475 if (unlikely(ftrace_disabled))
2476 return -ENODEV;
2477
2478 mutex_lock(&graph_lock);
2479 if ((file->f_mode & FMODE_WRITE) &&
8650ae32 2480 (file->f_flags & O_TRUNC)) {
ea4e2bc4
SR
2481 ftrace_graph_count = 0;
2482 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
2483 }
a4ec5e0c 2484 mutex_unlock(&graph_lock);
ea4e2bc4 2485
a4ec5e0c 2486 if (file->f_mode & FMODE_READ)
ea4e2bc4 2487 ret = seq_open(file, &ftrace_graph_seq_ops);
ea4e2bc4
SR
2488
2489 return ret;
2490}
2491
87827111
LZ
2492static int
2493ftrace_graph_release(struct inode *inode, struct file *file)
2494{
2495 if (file->f_mode & FMODE_READ)
2496 seq_release(inode, file);
2497 return 0;
2498}
2499
ea4e2bc4 2500static int
f9349a8f 2501ftrace_set_func(unsigned long *array, int *idx, char *buffer)
ea4e2bc4 2502{
ea4e2bc4
SR
2503 struct dyn_ftrace *rec;
2504 struct ftrace_page *pg;
f9349a8f 2505 int search_len;
ea4e2bc4 2506 int found = 0;
f9349a8f
FW
2507 int type, not;
2508 char *search;
2509 bool exists;
2510 int i;
ea4e2bc4
SR
2511
2512 if (ftrace_disabled)
2513 return -ENODEV;
2514
f9349a8f
FW
2515 /* decode regex */
2516 type = ftrace_setup_glob(buffer, strlen(buffer), &search, &not);
2517 if (not)
2518 return -EINVAL;
2519
2520 search_len = strlen(search);
2521
52baf119 2522 mutex_lock(&ftrace_lock);
265c831c
SR
2523 do_for_each_ftrace_rec(pg, rec) {
2524
f9349a8f
FW
2525 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
2526 break;
2527
265c831c
SR
2528 if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
2529 continue;
2530
f9349a8f
FW
2531 if (ftrace_match_record(rec, search, search_len, type)) {
2532 /* ensure it is not already in the array */
2533 exists = false;
2534 for (i = 0; i < *idx; i++)
2535 if (array[i] == rec->ip) {
2536 exists = true;
265c831c
SR
2537 break;
2538 }
f9349a8f
FW
2539 if (!exists) {
2540 array[(*idx)++] = rec->ip;
2541 found = 1;
2542 }
ea4e2bc4 2543 }
265c831c 2544 } while_for_each_ftrace_rec();
f9349a8f 2545
52baf119 2546 mutex_unlock(&ftrace_lock);
ea4e2bc4
SR
2547
2548 return found ? 0 : -EINVAL;
2549}
2550
2551static ssize_t
2552ftrace_graph_write(struct file *file, const char __user *ubuf,
2553 size_t cnt, loff_t *ppos)
2554{
689fd8b6 2555 struct trace_parser parser;
4ba7978e 2556 ssize_t read, ret;
ea4e2bc4
SR
2557
2558 if (!cnt || cnt < 0)
2559 return 0;
2560
2561 mutex_lock(&graph_lock);
2562
2563 if (ftrace_graph_count >= FTRACE_GRAPH_MAX_FUNCS) {
2564 ret = -EBUSY;
1eb90f13 2565 goto out_unlock;
ea4e2bc4
SR
2566 }
2567
689fd8b6 2568 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
2569 ret = -ENOMEM;
1eb90f13 2570 goto out_unlock;
ea4e2bc4
SR
2571 }
2572
689fd8b6 2573 read = trace_get_user(&parser, ubuf, cnt, ppos);
ea4e2bc4 2574
4ba7978e 2575 if (read >= 0 && trace_parser_loaded((&parser))) {
689fd8b6 2576 parser.buffer[parser.idx] = 0;
2577
2578 /* we allow only one expression at a time */
a4ec5e0c 2579 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
689fd8b6 2580 parser.buffer);
ea4e2bc4 2581 if (ret)
1eb90f13 2582 goto out_free;
ea4e2bc4 2583 }
ea4e2bc4
SR
2584
2585 ret = read;
1eb90f13
LZ
2586
2587out_free:
689fd8b6 2588 trace_parser_put(&parser);
1eb90f13 2589out_unlock:
ea4e2bc4
SR
2590 mutex_unlock(&graph_lock);
2591
2592 return ret;
2593}
2594
2595static const struct file_operations ftrace_graph_fops = {
87827111
LZ
2596 .open = ftrace_graph_open,
2597 .read = seq_read,
2598 .write = ftrace_graph_write,
2599 .release = ftrace_graph_release,
ea4e2bc4
SR
2600};
2601#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2602
df4fc315 2603static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
5072c59f 2604{
5072c59f 2605
5452af66
FW
2606 trace_create_file("available_filter_functions", 0444,
2607 d_tracer, NULL, &ftrace_avail_fops);
5072c59f 2608
5452af66
FW
2609 trace_create_file("failures", 0444,
2610 d_tracer, NULL, &ftrace_failures_fops);
eb9a7bf0 2611
5452af66
FW
2612 trace_create_file("set_ftrace_filter", 0644, d_tracer,
2613 NULL, &ftrace_filter_fops);
41c52c0d 2614
5452af66 2615 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
41c52c0d 2616 NULL, &ftrace_notrace_fops);
ad90c0e3 2617
ea4e2bc4 2618#ifdef CONFIG_FUNCTION_GRAPH_TRACER
5452af66 2619 trace_create_file("set_graph_function", 0444, d_tracer,
ea4e2bc4
SR
2620 NULL,
2621 &ftrace_graph_fops);
ea4e2bc4
SR
2622#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2623
5072c59f
SR
2624 return 0;
2625}
2626
31e88909
SR
2627static int ftrace_convert_nops(struct module *mod,
2628 unsigned long *start,
68bf21aa
SR
2629 unsigned long *end)
2630{
2631 unsigned long *p;
2632 unsigned long addr;
2633 unsigned long flags;
2634
e6ea44e9 2635 mutex_lock(&ftrace_lock);
68bf21aa
SR
2636 p = start;
2637 while (p < end) {
2638 addr = ftrace_call_adjust(*p++);
20e5227e
SR
2639 /*
2640 * Some architecture linkers will pad between
2641 * the different mcount_loc sections of different
2642 * object files to satisfy alignments.
2643 * Skip any NULL pointers.
2644 */
2645 if (!addr)
2646 continue;
68bf21aa 2647 ftrace_record_ip(addr);
68bf21aa
SR
2648 }
2649
08f5ac90 2650 /* disable interrupts to prevent kstop machine */
68bf21aa 2651 local_irq_save(flags);
31e88909 2652 ftrace_update_code(mod);
68bf21aa 2653 local_irq_restore(flags);
e6ea44e9 2654 mutex_unlock(&ftrace_lock);
68bf21aa
SR
2655
2656 return 0;
2657}
2658
93eb677d 2659#ifdef CONFIG_MODULES
e7247a15 2660void ftrace_release_mod(struct module *mod)
93eb677d
SR
2661{
2662 struct dyn_ftrace *rec;
2663 struct ftrace_page *pg;
93eb677d 2664
e7247a15 2665 if (ftrace_disabled)
93eb677d
SR
2666 return;
2667
2668 mutex_lock(&ftrace_lock);
2669 do_for_each_ftrace_rec(pg, rec) {
e7247a15 2670 if (within_module_core(rec->ip, mod)) {
93eb677d
SR
2671 /*
2672 * rec->ip is changed in ftrace_free_rec()
2673 * It should not between s and e if record was freed.
2674 */
2675 FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
2676 ftrace_free_rec(rec);
2677 }
2678 } while_for_each_ftrace_rec();
2679 mutex_unlock(&ftrace_lock);
2680}
2681
2682static void ftrace_init_module(struct module *mod,
2683 unsigned long *start, unsigned long *end)
90d595fe 2684{
00fd61ae 2685 if (ftrace_disabled || start == end)
fed1939c 2686 return;
31e88909 2687 ftrace_convert_nops(mod, start, end);
90d595fe
SR
2688}
2689
93eb677d
SR
2690static int ftrace_module_notify(struct notifier_block *self,
2691 unsigned long val, void *data)
2692{
2693 struct module *mod = data;
2694
2695 switch (val) {
2696 case MODULE_STATE_COMING:
2697 ftrace_init_module(mod, mod->ftrace_callsites,
2698 mod->ftrace_callsites +
2699 mod->num_ftrace_callsites);
2700 break;
2701 case MODULE_STATE_GOING:
e7247a15 2702 ftrace_release_mod(mod);
93eb677d
SR
2703 break;
2704 }
2705
2706 return 0;
2707}
2708#else
2709static int ftrace_module_notify(struct notifier_block *self,
2710 unsigned long val, void *data)
2711{
2712 return 0;
2713}
2714#endif /* CONFIG_MODULES */
2715
2716struct notifier_block ftrace_module_nb = {
2717 .notifier_call = ftrace_module_notify,
2718 .priority = 0,
2719};
2720
68bf21aa
SR
2721extern unsigned long __start_mcount_loc[];
2722extern unsigned long __stop_mcount_loc[];
2723
2724void __init ftrace_init(void)
2725{
2726 unsigned long count, addr, flags;
2727 int ret;
2728
2729 /* Keep the ftrace pointer to the stub */
2730 addr = (unsigned long)ftrace_stub;
2731
2732 local_irq_save(flags);
2733 ftrace_dyn_arch_init(&addr);
2734 local_irq_restore(flags);
2735
2736 /* ftrace_dyn_arch_init places the return code in addr */
2737 if (addr)
2738 goto failed;
2739
2740 count = __stop_mcount_loc - __start_mcount_loc;
2741
2742 ret = ftrace_dyn_table_alloc(count);
2743 if (ret)
2744 goto failed;
2745
2746 last_ftrace_enabled = ftrace_enabled = 1;
2747
31e88909
SR
2748 ret = ftrace_convert_nops(NULL,
2749 __start_mcount_loc,
68bf21aa
SR
2750 __stop_mcount_loc);
2751
93eb677d 2752 ret = register_module_notifier(&ftrace_module_nb);
24ed0c4b 2753 if (ret)
93eb677d
SR
2754 pr_warning("Failed to register trace ftrace module notifier\n");
2755
2af15d6a
SR
2756 set_ftrace_early_filters();
2757
68bf21aa
SR
2758 return;
2759 failed:
2760 ftrace_disabled = 1;
2761}
68bf21aa 2762
3d083395 2763#else
0b6e4d56
FW
2764
2765static int __init ftrace_nodyn_init(void)
2766{
2767 ftrace_enabled = 1;
2768 return 0;
2769}
2770device_initcall(ftrace_nodyn_init);
2771
df4fc315
SR
2772static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
2773static inline void ftrace_startup_enable(int command) { }
5a45cfe1
SR
2774/* Keep as macros so we do not need to define the commands */
2775# define ftrace_startup(command) do { } while (0)
2776# define ftrace_shutdown(command) do { } while (0)
c7aafc54
IM
2777# define ftrace_startup_sysctl() do { } while (0)
2778# define ftrace_shutdown_sysctl() do { } while (0)
3d083395
SR
2779#endif /* CONFIG_DYNAMIC_FTRACE */
2780
df4fc315
SR
2781static ssize_t
2782ftrace_pid_read(struct file *file, char __user *ubuf,
2783 size_t cnt, loff_t *ppos)
2784{
2785 char buf[64];
2786 int r;
2787
e32d8956
SR
2788 if (ftrace_pid_trace == ftrace_swapper_pid)
2789 r = sprintf(buf, "swapper tasks\n");
2790 else if (ftrace_pid_trace)
cc59c9e8 2791 r = sprintf(buf, "%u\n", pid_vnr(ftrace_pid_trace));
df4fc315
SR
2792 else
2793 r = sprintf(buf, "no pid\n");
2794
2795 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2796}
2797
e32d8956 2798static void clear_ftrace_swapper(void)
978f3a45
SR
2799{
2800 struct task_struct *p;
e32d8956 2801 int cpu;
978f3a45 2802
e32d8956
SR
2803 get_online_cpus();
2804 for_each_online_cpu(cpu) {
2805 p = idle_task(cpu);
978f3a45 2806 clear_tsk_trace_trace(p);
e32d8956
SR
2807 }
2808 put_online_cpus();
2809}
978f3a45 2810
e32d8956
SR
2811static void set_ftrace_swapper(void)
2812{
2813 struct task_struct *p;
2814 int cpu;
2815
2816 get_online_cpus();
2817 for_each_online_cpu(cpu) {
2818 p = idle_task(cpu);
2819 set_tsk_trace_trace(p);
2820 }
2821 put_online_cpus();
978f3a45
SR
2822}
2823
e32d8956
SR
2824static void clear_ftrace_pid(struct pid *pid)
2825{
2826 struct task_struct *p;
2827
229c4ef8 2828 rcu_read_lock();
e32d8956
SR
2829 do_each_pid_task(pid, PIDTYPE_PID, p) {
2830 clear_tsk_trace_trace(p);
2831 } while_each_pid_task(pid, PIDTYPE_PID, p);
229c4ef8
ON
2832 rcu_read_unlock();
2833
e32d8956
SR
2834 put_pid(pid);
2835}
2836
2837static void set_ftrace_pid(struct pid *pid)
978f3a45
SR
2838{
2839 struct task_struct *p;
2840
229c4ef8 2841 rcu_read_lock();
978f3a45
SR
2842 do_each_pid_task(pid, PIDTYPE_PID, p) {
2843 set_tsk_trace_trace(p);
2844 } while_each_pid_task(pid, PIDTYPE_PID, p);
229c4ef8 2845 rcu_read_unlock();
978f3a45
SR
2846}
2847
e32d8956
SR
2848static void clear_ftrace_pid_task(struct pid **pid)
2849{
2850 if (*pid == ftrace_swapper_pid)
2851 clear_ftrace_swapper();
2852 else
2853 clear_ftrace_pid(*pid);
2854
2855 *pid = NULL;
2856}
2857
2858static void set_ftrace_pid_task(struct pid *pid)
2859{
2860 if (pid == ftrace_swapper_pid)
2861 set_ftrace_swapper();
2862 else
2863 set_ftrace_pid(pid);
2864}
2865
df4fc315
SR
2866static ssize_t
2867ftrace_pid_write(struct file *filp, const char __user *ubuf,
2868 size_t cnt, loff_t *ppos)
2869{
978f3a45 2870 struct pid *pid;
df4fc315
SR
2871 char buf[64];
2872 long val;
2873 int ret;
2874
2875 if (cnt >= sizeof(buf))
2876 return -EINVAL;
2877
2878 if (copy_from_user(&buf, ubuf, cnt))
2879 return -EFAULT;
2880
2881 buf[cnt] = 0;
2882
2883 ret = strict_strtol(buf, 10, &val);
2884 if (ret < 0)
2885 return ret;
2886
e6ea44e9 2887 mutex_lock(&ftrace_lock);
978f3a45 2888 if (val < 0) {
df4fc315 2889 /* disable pid tracing */
978f3a45 2890 if (!ftrace_pid_trace)
df4fc315 2891 goto out;
978f3a45
SR
2892
2893 clear_ftrace_pid_task(&ftrace_pid_trace);
df4fc315
SR
2894
2895 } else {
e32d8956
SR
2896 /* swapper task is special */
2897 if (!val) {
2898 pid = ftrace_swapper_pid;
2899 if (pid == ftrace_pid_trace)
2900 goto out;
2901 } else {
2902 pid = find_get_pid(val);
df4fc315 2903
e32d8956
SR
2904 if (pid == ftrace_pid_trace) {
2905 put_pid(pid);
2906 goto out;
2907 }
0ef8cde5 2908 }
0ef8cde5 2909
978f3a45
SR
2910 if (ftrace_pid_trace)
2911 clear_ftrace_pid_task(&ftrace_pid_trace);
2912
2913 if (!pid)
2914 goto out;
2915
2916 ftrace_pid_trace = pid;
2917
2918 set_ftrace_pid_task(ftrace_pid_trace);
df4fc315
SR
2919 }
2920
2921 /* update the function call */
2922 ftrace_update_pid_func();
2923 ftrace_startup_enable(0);
2924
2925 out:
e6ea44e9 2926 mutex_unlock(&ftrace_lock);
df4fc315
SR
2927
2928 return cnt;
2929}
2930
5e2336a0 2931static const struct file_operations ftrace_pid_fops = {
df4fc315
SR
2932 .read = ftrace_pid_read,
2933 .write = ftrace_pid_write,
2934};
2935
2936static __init int ftrace_init_debugfs(void)
2937{
2938 struct dentry *d_tracer;
df4fc315
SR
2939
2940 d_tracer = tracing_init_dentry();
2941 if (!d_tracer)
2942 return 0;
2943
2944 ftrace_init_dyn_debugfs(d_tracer);
2945
5452af66
FW
2946 trace_create_file("set_ftrace_pid", 0644, d_tracer,
2947 NULL, &ftrace_pid_fops);
493762fc
SR
2948
2949 ftrace_profile_debugfs(d_tracer);
2950
df4fc315
SR
2951 return 0;
2952}
df4fc315
SR
2953fs_initcall(ftrace_init_debugfs);
2954
a2bb6a3d 2955/**
81adbdc0 2956 * ftrace_kill - kill ftrace
a2bb6a3d
SR
2957 *
2958 * This function should be used by panic code. It stops ftrace
2959 * but in a not so nice way. If you need to simply kill ftrace
2960 * from a non-atomic section, use ftrace_kill.
2961 */
81adbdc0 2962void ftrace_kill(void)
a2bb6a3d
SR
2963{
2964 ftrace_disabled = 1;
2965 ftrace_enabled = 0;
a2bb6a3d
SR
2966 clear_ftrace_function();
2967}
2968
16444a8a 2969/**
3d083395
SR
2970 * register_ftrace_function - register a function for profiling
2971 * @ops - ops structure that holds the function for profiling.
16444a8a 2972 *
3d083395
SR
2973 * Register a function to be called by all functions in the
2974 * kernel.
2975 *
2976 * Note: @ops->func and all the functions it calls must be labeled
2977 * with "notrace", otherwise it will go into a
2978 * recursive loop.
16444a8a 2979 */
3d083395 2980int register_ftrace_function(struct ftrace_ops *ops)
16444a8a 2981{
b0fc494f
SR
2982 int ret;
2983
4eebcc81
SR
2984 if (unlikely(ftrace_disabled))
2985 return -1;
2986
e6ea44e9 2987 mutex_lock(&ftrace_lock);
e7d3737e 2988
b0fc494f 2989 ret = __register_ftrace_function(ops);
5a45cfe1 2990 ftrace_startup(0);
b0fc494f 2991
e6ea44e9 2992 mutex_unlock(&ftrace_lock);
b0fc494f 2993 return ret;
3d083395
SR
2994}
2995
2996/**
32632920 2997 * unregister_ftrace_function - unregister a function for profiling.
3d083395
SR
2998 * @ops - ops structure that holds the function to unregister
2999 *
3000 * Unregister a function that was added to be called by ftrace profiling.
3001 */
3002int unregister_ftrace_function(struct ftrace_ops *ops)
3003{
3004 int ret;
3005
e6ea44e9 3006 mutex_lock(&ftrace_lock);
3d083395 3007 ret = __unregister_ftrace_function(ops);
5a45cfe1 3008 ftrace_shutdown(0);
e6ea44e9 3009 mutex_unlock(&ftrace_lock);
b0fc494f
SR
3010
3011 return ret;
3012}
3013
e309b41d 3014int
b0fc494f 3015ftrace_enable_sysctl(struct ctl_table *table, int write,
8d65af78 3016 void __user *buffer, size_t *lenp,
b0fc494f
SR
3017 loff_t *ppos)
3018{
3019 int ret;
3020
4eebcc81
SR
3021 if (unlikely(ftrace_disabled))
3022 return -ENODEV;
3023
e6ea44e9 3024 mutex_lock(&ftrace_lock);
b0fc494f 3025
8d65af78 3026 ret = proc_dointvec(table, write, buffer, lenp, ppos);
b0fc494f 3027
a32c7765 3028 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
b0fc494f
SR
3029 goto out;
3030
a32c7765 3031 last_ftrace_enabled = !!ftrace_enabled;
b0fc494f
SR
3032
3033 if (ftrace_enabled) {
3034
3035 ftrace_startup_sysctl();
3036
3037 /* we are starting ftrace again */
3038 if (ftrace_list != &ftrace_list_end) {
3039 if (ftrace_list->next == &ftrace_list_end)
3040 ftrace_trace_function = ftrace_list->func;
3041 else
3042 ftrace_trace_function = ftrace_list_func;
3043 }
3044
3045 } else {
3046 /* stopping ftrace calls (just send to ftrace_stub) */
3047 ftrace_trace_function = ftrace_stub;
3048
3049 ftrace_shutdown_sysctl();
3050 }
3051
3052 out:
e6ea44e9 3053 mutex_unlock(&ftrace_lock);
3d083395 3054 return ret;
16444a8a 3055}
f17845e5 3056
fb52607a 3057#ifdef CONFIG_FUNCTION_GRAPH_TRACER
e7d3737e 3058
597af815 3059static int ftrace_graph_active;
4a2b8dda 3060static struct notifier_block ftrace_suspend_notifier;
e7d3737e 3061
e49dc19c
SR
3062int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3063{
3064 return 0;
3065}
3066
287b6e68
FW
3067/* The callbacks that hook a function */
3068trace_func_graph_ret_t ftrace_graph_return =
3069 (trace_func_graph_ret_t)ftrace_stub;
e49dc19c 3070trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
f201ae23
FW
3071
3072/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3073static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3074{
3075 int i;
3076 int ret = 0;
3077 unsigned long flags;
3078 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3079 struct task_struct *g, *t;
3080
3081 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3082 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3083 * sizeof(struct ftrace_ret_stack),
3084 GFP_KERNEL);
3085 if (!ret_stack_list[i]) {
3086 start = 0;
3087 end = i;
3088 ret = -ENOMEM;
3089 goto free;
3090 }
3091 }
3092
3093 read_lock_irqsave(&tasklist_lock, flags);
3094 do_each_thread(g, t) {
3095 if (start == end) {
3096 ret = -EAGAIN;
3097 goto unlock;
3098 }
3099
3100 if (t->ret_stack == NULL) {
380c4b14 3101 atomic_set(&t->tracing_graph_pause, 0);
f201ae23 3102 atomic_set(&t->trace_overrun, 0);
26c01624
SR
3103 t->curr_ret_stack = -1;
3104 /* Make sure the tasks see the -1 first: */
3105 smp_wmb();
3106 t->ret_stack = ret_stack_list[start++];
f201ae23
FW
3107 }
3108 } while_each_thread(g, t);
3109
3110unlock:
3111 read_unlock_irqrestore(&tasklist_lock, flags);
3112free:
3113 for (i = start; i < end; i++)
3114 kfree(ret_stack_list[i]);
3115 return ret;
3116}
3117
8aef2d28
SR
3118static void
3119ftrace_graph_probe_sched_switch(struct rq *__rq, struct task_struct *prev,
3120 struct task_struct *next)
3121{
3122 unsigned long long timestamp;
3123 int index;
3124
be6f164a
SR
3125 /*
3126 * Does the user want to count the time a function was asleep.
3127 * If so, do not update the time stamps.
3128 */
3129 if (trace_flags & TRACE_ITER_SLEEP_TIME)
3130 return;
3131
8aef2d28
SR
3132 timestamp = trace_clock_local();
3133
3134 prev->ftrace_timestamp = timestamp;
3135
3136 /* only process tasks that we timestamped */
3137 if (!next->ftrace_timestamp)
3138 return;
3139
3140 /*
3141 * Update all the counters in next to make up for the
3142 * time next was sleeping.
3143 */
3144 timestamp -= next->ftrace_timestamp;
3145
3146 for (index = next->curr_ret_stack; index >= 0; index--)
3147 next->ret_stack[index].calltime += timestamp;
3148}
3149
f201ae23 3150/* Allocate a return stack for each task */
fb52607a 3151static int start_graph_tracing(void)
f201ae23
FW
3152{
3153 struct ftrace_ret_stack **ret_stack_list;
5b058bcd 3154 int ret, cpu;
f201ae23
FW
3155
3156 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
3157 sizeof(struct ftrace_ret_stack *),
3158 GFP_KERNEL);
3159
3160 if (!ret_stack_list)
3161 return -ENOMEM;
3162
5b058bcd 3163 /* The cpu_boot init_task->ret_stack will never be freed */
179c498a
SR
3164 for_each_online_cpu(cpu) {
3165 if (!idle_task(cpu)->ret_stack)
3166 ftrace_graph_init_task(idle_task(cpu));
3167 }
5b058bcd 3168
f201ae23
FW
3169 do {
3170 ret = alloc_retstack_tasklist(ret_stack_list);
3171 } while (ret == -EAGAIN);
3172
8aef2d28
SR
3173 if (!ret) {
3174 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch);
3175 if (ret)
3176 pr_info("ftrace_graph: Couldn't activate tracepoint"
3177 " probe to kernel_sched_switch\n");
3178 }
3179
f201ae23
FW
3180 kfree(ret_stack_list);
3181 return ret;
3182}
3183
4a2b8dda
FW
3184/*
3185 * Hibernation protection.
3186 * The state of the current task is too much unstable during
3187 * suspend/restore to disk. We want to protect against that.
3188 */
3189static int
3190ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
3191 void *unused)
3192{
3193 switch (state) {
3194 case PM_HIBERNATION_PREPARE:
3195 pause_graph_tracing();
3196 break;
3197
3198 case PM_POST_HIBERNATION:
3199 unpause_graph_tracing();
3200 break;
3201 }
3202 return NOTIFY_DONE;
3203}
3204
287b6e68
FW
3205int register_ftrace_graph(trace_func_graph_ret_t retfunc,
3206 trace_func_graph_ent_t entryfunc)
15e6cb36 3207{
e7d3737e
FW
3208 int ret = 0;
3209
e6ea44e9 3210 mutex_lock(&ftrace_lock);
e7d3737e 3211
05ce5818 3212 /* we currently allow only one tracer registered at a time */
597af815 3213 if (ftrace_graph_active) {
05ce5818
SR
3214 ret = -EBUSY;
3215 goto out;
3216 }
3217
4a2b8dda
FW
3218 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
3219 register_pm_notifier(&ftrace_suspend_notifier);
3220
597af815 3221 ftrace_graph_active++;
fb52607a 3222 ret = start_graph_tracing();
f201ae23 3223 if (ret) {
597af815 3224 ftrace_graph_active--;
f201ae23
FW
3225 goto out;
3226 }
e53a6319 3227
287b6e68
FW
3228 ftrace_graph_return = retfunc;
3229 ftrace_graph_entry = entryfunc;
e53a6319 3230
5a45cfe1 3231 ftrace_startup(FTRACE_START_FUNC_RET);
e7d3737e
FW
3232
3233out:
e6ea44e9 3234 mutex_unlock(&ftrace_lock);
e7d3737e 3235 return ret;
15e6cb36
FW
3236}
3237
fb52607a 3238void unregister_ftrace_graph(void)
15e6cb36 3239{
e6ea44e9 3240 mutex_lock(&ftrace_lock);
e7d3737e 3241
597af815 3242 if (unlikely(!ftrace_graph_active))
2aad1b76
SR
3243 goto out;
3244
597af815 3245 ftrace_graph_active--;
8aef2d28 3246 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch);
287b6e68 3247 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
e49dc19c 3248 ftrace_graph_entry = ftrace_graph_entry_stub;
5a45cfe1 3249 ftrace_shutdown(FTRACE_STOP_FUNC_RET);
4a2b8dda 3250 unregister_pm_notifier(&ftrace_suspend_notifier);
e7d3737e 3251
2aad1b76 3252 out:
e6ea44e9 3253 mutex_unlock(&ftrace_lock);
15e6cb36 3254}
f201ae23
FW
3255
3256/* Allocate a return stack for newly created task */
fb52607a 3257void ftrace_graph_init_task(struct task_struct *t)
f201ae23 3258{
84047e36
SR
3259 /* Make sure we do not use the parent ret_stack */
3260 t->ret_stack = NULL;
3261
597af815 3262 if (ftrace_graph_active) {
82310a32
SR
3263 struct ftrace_ret_stack *ret_stack;
3264
3265 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
f201ae23
FW
3266 * sizeof(struct ftrace_ret_stack),
3267 GFP_KERNEL);
82310a32 3268 if (!ret_stack)
f201ae23
FW
3269 return;
3270 t->curr_ret_stack = -1;
380c4b14 3271 atomic_set(&t->tracing_graph_pause, 0);
f201ae23 3272 atomic_set(&t->trace_overrun, 0);
8aef2d28 3273 t->ftrace_timestamp = 0;
82310a32
SR
3274 /* make curr_ret_stack visable before we add the ret_stack */
3275 smp_wmb();
3276 t->ret_stack = ret_stack;
84047e36 3277 }
f201ae23
FW
3278}
3279
fb52607a 3280void ftrace_graph_exit_task(struct task_struct *t)
f201ae23 3281{
eae849ca
FW
3282 struct ftrace_ret_stack *ret_stack = t->ret_stack;
3283
f201ae23 3284 t->ret_stack = NULL;
eae849ca
FW
3285 /* NULL must become visible to IRQs before we free it: */
3286 barrier();
3287
3288 kfree(ret_stack);
f201ae23 3289}
14a866c5
SR
3290
3291void ftrace_graph_stop(void)
3292{
3293 ftrace_stop();
3294}
15e6cb36
FW
3295#endif
3296
This page took 0.380265 seconds and 5 git commands to generate.