isci: add some type safety to the state machine interface
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / trace / ftrace.c
blob1ee417fcbfa5a09c4f337261f6c0867d9143d3de
1 /*
2 * Infrastructure for profiling code inserted by 'gcc -pg'.
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
7 * Originally ported from the -rt patch by:
8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
10 * Based on code in the latency_tracer, that is:
12 * Copyright (C) 2004-2006 Ingo Molnar
13 * Copyright (C) 2004 William Lee Irwin III
16 #include <linux/stop_machine.h>
17 #include <linux/clocksource.h>
18 #include <linux/kallsyms.h>
19 #include <linux/seq_file.h>
20 #include <linux/suspend.h>
21 #include <linux/debugfs.h>
22 #include <linux/hardirq.h>
23 #include <linux/kthread.h>
24 #include <linux/uaccess.h>
25 #include <linux/ftrace.h>
26 #include <linux/sysctl.h>
27 #include <linux/slab.h>
28 #include <linux/ctype.h>
29 #include <linux/list.h>
30 #include <linux/hash.h>
31 #include <linux/rcupdate.h>
33 #include <trace/events/sched.h>
35 #include <asm/ftrace.h>
36 #include <asm/setup.h>
38 #include "trace_output.h"
39 #include "trace_stat.h"
41 #define FTRACE_WARN_ON(cond) \
42 ({ \
43 int ___r = cond; \
44 if (WARN_ON(___r)) \
45 ftrace_kill(); \
46 ___r; \
49 #define FTRACE_WARN_ON_ONCE(cond) \
50 ({ \
51 int ___r = cond; \
52 if (WARN_ON_ONCE(___r)) \
53 ftrace_kill(); \
54 ___r; \
57 /* hash bits for specific function selection */
58 #define FTRACE_HASH_BITS 7
59 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
60 #define FTRACE_HASH_DEFAULT_BITS 10
61 #define FTRACE_HASH_MAX_BITS 12
63 /* ftrace_enabled is a method to turn ftrace on or off */
64 int ftrace_enabled __read_mostly;
65 static int last_ftrace_enabled;
67 /* Quick disabling of function tracer. */
68 int function_trace_stop;
70 /* List for set_ftrace_pid's pids. */
71 LIST_HEAD(ftrace_pids);
72 struct ftrace_pid {
73 struct list_head list;
74 struct pid *pid;
78 * ftrace_disabled is set when an anomaly is discovered.
79 * ftrace_disabled is much stronger than ftrace_enabled.
81 static int ftrace_disabled __read_mostly;
83 static DEFINE_MUTEX(ftrace_lock);
85 static struct ftrace_ops ftrace_list_end __read_mostly =
87 .func = ftrace_stub,
90 static struct ftrace_ops *ftrace_global_list __read_mostly = &ftrace_list_end;
91 static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
92 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
93 ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
94 ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
95 static struct ftrace_ops global_ops;
97 static void
98 ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip);
101 * Traverse the ftrace_global_list, invoking all entries. The reason that we
102 * can use rcu_dereference_raw() is that elements removed from this list
103 * are simply leaked, so there is no need to interact with a grace-period
104 * mechanism. The rcu_dereference_raw() calls are needed to handle
105 * concurrent insertions into the ftrace_global_list.
107 * Silly Alpha and silly pointer-speculation compiler optimizations!
109 static void ftrace_global_list_func(unsigned long ip,
110 unsigned long parent_ip)
112 struct ftrace_ops *op;
114 if (unlikely(trace_recursion_test(TRACE_GLOBAL_BIT)))
115 return;
117 trace_recursion_set(TRACE_GLOBAL_BIT);
118 op = rcu_dereference_raw(ftrace_global_list); /*see above*/
119 while (op != &ftrace_list_end) {
120 op->func(ip, parent_ip);
121 op = rcu_dereference_raw(op->next); /*see above*/
123 trace_recursion_clear(TRACE_GLOBAL_BIT);
126 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
128 if (!test_tsk_trace_trace(current))
129 return;
131 ftrace_pid_function(ip, parent_ip);
134 static void set_ftrace_pid_function(ftrace_func_t func)
136 /* do not set ftrace_pid_function to itself! */
137 if (func != ftrace_pid_func)
138 ftrace_pid_function = func;
142 * clear_ftrace_function - reset the ftrace function
144 * This NULLs the ftrace function and in essence stops
145 * tracing. There may be lag
147 void clear_ftrace_function(void)
149 ftrace_trace_function = ftrace_stub;
150 __ftrace_trace_function = ftrace_stub;
151 ftrace_pid_function = ftrace_stub;
154 #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
156 * For those archs that do not test ftrace_trace_stop in their
157 * mcount call site, we need to do it from C.
159 static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
161 if (function_trace_stop)
162 return;
164 __ftrace_trace_function(ip, parent_ip);
166 #endif
168 static void update_global_ops(void)
170 ftrace_func_t func;
173 * If there's only one function registered, then call that
174 * function directly. Otherwise, we need to iterate over the
175 * registered callers.
177 if (ftrace_global_list == &ftrace_list_end ||
178 ftrace_global_list->next == &ftrace_list_end)
179 func = ftrace_global_list->func;
180 else
181 func = ftrace_global_list_func;
183 /* If we filter on pids, update to use the pid function */
184 if (!list_empty(&ftrace_pids)) {
185 set_ftrace_pid_function(func);
186 func = ftrace_pid_func;
189 global_ops.func = func;
192 static void update_ftrace_function(void)
194 ftrace_func_t func;
196 update_global_ops();
199 * If we are at the end of the list and this ops is
200 * not dynamic, then have the mcount trampoline call
201 * the function directly
203 if (ftrace_ops_list == &ftrace_list_end ||
204 (ftrace_ops_list->next == &ftrace_list_end &&
205 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC)))
206 func = ftrace_ops_list->func;
207 else
208 func = ftrace_ops_list_func;
210 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
211 ftrace_trace_function = func;
212 #else
213 __ftrace_trace_function = func;
214 ftrace_trace_function = ftrace_test_stop_func;
215 #endif
218 static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
220 ops->next = *list;
222 * We are entering ops into the list but another
223 * CPU might be walking that list. We need to make sure
224 * the ops->next pointer is valid before another CPU sees
225 * the ops pointer included into the list.
227 rcu_assign_pointer(*list, ops);
230 static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
232 struct ftrace_ops **p;
235 * If we are removing the last function, then simply point
236 * to the ftrace_stub.
238 if (*list == ops && ops->next == &ftrace_list_end) {
239 *list = &ftrace_list_end;
240 return 0;
243 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
244 if (*p == ops)
245 break;
247 if (*p != ops)
248 return -1;
250 *p = (*p)->next;
251 return 0;
254 static int __register_ftrace_function(struct ftrace_ops *ops)
256 if (ftrace_disabled)
257 return -ENODEV;
259 if (FTRACE_WARN_ON(ops == &global_ops))
260 return -EINVAL;
262 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
263 return -EBUSY;
265 if (!core_kernel_data((unsigned long)ops))
266 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
268 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
269 int first = ftrace_global_list == &ftrace_list_end;
270 add_ftrace_ops(&ftrace_global_list, ops);
271 ops->flags |= FTRACE_OPS_FL_ENABLED;
272 if (first)
273 add_ftrace_ops(&ftrace_ops_list, &global_ops);
274 } else
275 add_ftrace_ops(&ftrace_ops_list, ops);
277 if (ftrace_enabled)
278 update_ftrace_function();
280 return 0;
283 static int __unregister_ftrace_function(struct ftrace_ops *ops)
285 int ret;
287 if (ftrace_disabled)
288 return -ENODEV;
290 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
291 return -EBUSY;
293 if (FTRACE_WARN_ON(ops == &global_ops))
294 return -EINVAL;
296 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
297 ret = remove_ftrace_ops(&ftrace_global_list, ops);
298 if (!ret && ftrace_global_list == &ftrace_list_end)
299 ret = remove_ftrace_ops(&ftrace_ops_list, &global_ops);
300 if (!ret)
301 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
302 } else
303 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
305 if (ret < 0)
306 return ret;
308 if (ftrace_enabled)
309 update_ftrace_function();
312 * Dynamic ops may be freed, we must make sure that all
313 * callers are done before leaving this function.
315 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
316 synchronize_sched();
318 return 0;
321 static void ftrace_update_pid_func(void)
323 /* Only do something if we are tracing something */
324 if (ftrace_trace_function == ftrace_stub)
325 return;
327 update_ftrace_function();
330 #ifdef CONFIG_FUNCTION_PROFILER
331 struct ftrace_profile {
332 struct hlist_node node;
333 unsigned long ip;
334 unsigned long counter;
335 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
336 unsigned long long time;
337 unsigned long long time_squared;
338 #endif
341 struct ftrace_profile_page {
342 struct ftrace_profile_page *next;
343 unsigned long index;
344 struct ftrace_profile records[];
347 struct ftrace_profile_stat {
348 atomic_t disabled;
349 struct hlist_head *hash;
350 struct ftrace_profile_page *pages;
351 struct ftrace_profile_page *start;
352 struct tracer_stat stat;
355 #define PROFILE_RECORDS_SIZE \
356 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
358 #define PROFILES_PER_PAGE \
359 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
361 static int ftrace_profile_bits __read_mostly;
362 static int ftrace_profile_enabled __read_mostly;
364 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
365 static DEFINE_MUTEX(ftrace_profile_lock);
367 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
369 #define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
371 static void *
372 function_stat_next(void *v, int idx)
374 struct ftrace_profile *rec = v;
375 struct ftrace_profile_page *pg;
377 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
379 again:
380 if (idx != 0)
381 rec++;
383 if ((void *)rec >= (void *)&pg->records[pg->index]) {
384 pg = pg->next;
385 if (!pg)
386 return NULL;
387 rec = &pg->records[0];
388 if (!rec->counter)
389 goto again;
392 return rec;
395 static void *function_stat_start(struct tracer_stat *trace)
397 struct ftrace_profile_stat *stat =
398 container_of(trace, struct ftrace_profile_stat, stat);
400 if (!stat || !stat->start)
401 return NULL;
403 return function_stat_next(&stat->start->records[0], 0);
406 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
407 /* function graph compares on total time */
408 static int function_stat_cmp(void *p1, void *p2)
410 struct ftrace_profile *a = p1;
411 struct ftrace_profile *b = p2;
413 if (a->time < b->time)
414 return -1;
415 if (a->time > b->time)
416 return 1;
417 else
418 return 0;
420 #else
421 /* not function graph compares against hits */
422 static int function_stat_cmp(void *p1, void *p2)
424 struct ftrace_profile *a = p1;
425 struct ftrace_profile *b = p2;
427 if (a->counter < b->counter)
428 return -1;
429 if (a->counter > b->counter)
430 return 1;
431 else
432 return 0;
434 #endif
436 static int function_stat_headers(struct seq_file *m)
438 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
439 seq_printf(m, " Function "
440 "Hit Time Avg s^2\n"
441 " -------- "
442 "--- ---- --- ---\n");
443 #else
444 seq_printf(m, " Function Hit\n"
445 " -------- ---\n");
446 #endif
447 return 0;
450 static int function_stat_show(struct seq_file *m, void *v)
452 struct ftrace_profile *rec = v;
453 char str[KSYM_SYMBOL_LEN];
454 int ret = 0;
455 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
456 static struct trace_seq s;
457 unsigned long long avg;
458 unsigned long long stddev;
459 #endif
460 mutex_lock(&ftrace_profile_lock);
462 /* we raced with function_profile_reset() */
463 if (unlikely(rec->counter == 0)) {
464 ret = -EBUSY;
465 goto out;
468 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
469 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
471 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
472 seq_printf(m, " ");
473 avg = rec->time;
474 do_div(avg, rec->counter);
476 /* Sample standard deviation (s^2) */
477 if (rec->counter <= 1)
478 stddev = 0;
479 else {
480 stddev = rec->time_squared - rec->counter * avg * avg;
482 * Divide only 1000 for ns^2 -> us^2 conversion.
483 * trace_print_graph_duration will divide 1000 again.
485 do_div(stddev, (rec->counter - 1) * 1000);
488 trace_seq_init(&s);
489 trace_print_graph_duration(rec->time, &s);
490 trace_seq_puts(&s, " ");
491 trace_print_graph_duration(avg, &s);
492 trace_seq_puts(&s, " ");
493 trace_print_graph_duration(stddev, &s);
494 trace_print_seq(m, &s);
495 #endif
496 seq_putc(m, '\n');
497 out:
498 mutex_unlock(&ftrace_profile_lock);
500 return ret;
503 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
505 struct ftrace_profile_page *pg;
507 pg = stat->pages = stat->start;
509 while (pg) {
510 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
511 pg->index = 0;
512 pg = pg->next;
515 memset(stat->hash, 0,
516 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
519 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
521 struct ftrace_profile_page *pg;
522 int functions;
523 int pages;
524 int i;
526 /* If we already allocated, do nothing */
527 if (stat->pages)
528 return 0;
530 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
531 if (!stat->pages)
532 return -ENOMEM;
534 #ifdef CONFIG_DYNAMIC_FTRACE
535 functions = ftrace_update_tot_cnt;
536 #else
538 * We do not know the number of functions that exist because
539 * dynamic tracing is what counts them. With past experience
540 * we have around 20K functions. That should be more than enough.
541 * It is highly unlikely we will execute every function in
542 * the kernel.
544 functions = 20000;
545 #endif
547 pg = stat->start = stat->pages;
549 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
551 for (i = 0; i < pages; i++) {
552 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
553 if (!pg->next)
554 goto out_free;
555 pg = pg->next;
558 return 0;
560 out_free:
561 pg = stat->start;
562 while (pg) {
563 unsigned long tmp = (unsigned long)pg;
565 pg = pg->next;
566 free_page(tmp);
569 free_page((unsigned long)stat->pages);
570 stat->pages = NULL;
571 stat->start = NULL;
573 return -ENOMEM;
576 static int ftrace_profile_init_cpu(int cpu)
578 struct ftrace_profile_stat *stat;
579 int size;
581 stat = &per_cpu(ftrace_profile_stats, cpu);
583 if (stat->hash) {
584 /* If the profile is already created, simply reset it */
585 ftrace_profile_reset(stat);
586 return 0;
590 * We are profiling all functions, but usually only a few thousand
591 * functions are hit. We'll make a hash of 1024 items.
593 size = FTRACE_PROFILE_HASH_SIZE;
595 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
597 if (!stat->hash)
598 return -ENOMEM;
600 if (!ftrace_profile_bits) {
601 size--;
603 for (; size; size >>= 1)
604 ftrace_profile_bits++;
607 /* Preallocate the function profiling pages */
608 if (ftrace_profile_pages_init(stat) < 0) {
609 kfree(stat->hash);
610 stat->hash = NULL;
611 return -ENOMEM;
614 return 0;
617 static int ftrace_profile_init(void)
619 int cpu;
620 int ret = 0;
622 for_each_online_cpu(cpu) {
623 ret = ftrace_profile_init_cpu(cpu);
624 if (ret)
625 break;
628 return ret;
631 /* interrupts must be disabled */
632 static struct ftrace_profile *
633 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
635 struct ftrace_profile *rec;
636 struct hlist_head *hhd;
637 struct hlist_node *n;
638 unsigned long key;
640 key = hash_long(ip, ftrace_profile_bits);
641 hhd = &stat->hash[key];
643 if (hlist_empty(hhd))
644 return NULL;
646 hlist_for_each_entry_rcu(rec, n, hhd, node) {
647 if (rec->ip == ip)
648 return rec;
651 return NULL;
654 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
655 struct ftrace_profile *rec)
657 unsigned long key;
659 key = hash_long(rec->ip, ftrace_profile_bits);
660 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
664 * The memory is already allocated, this simply finds a new record to use.
666 static struct ftrace_profile *
667 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
669 struct ftrace_profile *rec = NULL;
671 /* prevent recursion (from NMIs) */
672 if (atomic_inc_return(&stat->disabled) != 1)
673 goto out;
676 * Try to find the function again since an NMI
677 * could have added it
679 rec = ftrace_find_profiled_func(stat, ip);
680 if (rec)
681 goto out;
683 if (stat->pages->index == PROFILES_PER_PAGE) {
684 if (!stat->pages->next)
685 goto out;
686 stat->pages = stat->pages->next;
689 rec = &stat->pages->records[stat->pages->index++];
690 rec->ip = ip;
691 ftrace_add_profile(stat, rec);
693 out:
694 atomic_dec(&stat->disabled);
696 return rec;
699 static void
700 function_profile_call(unsigned long ip, unsigned long parent_ip)
702 struct ftrace_profile_stat *stat;
703 struct ftrace_profile *rec;
704 unsigned long flags;
706 if (!ftrace_profile_enabled)
707 return;
709 local_irq_save(flags);
711 stat = &__get_cpu_var(ftrace_profile_stats);
712 if (!stat->hash || !ftrace_profile_enabled)
713 goto out;
715 rec = ftrace_find_profiled_func(stat, ip);
716 if (!rec) {
717 rec = ftrace_profile_alloc(stat, ip);
718 if (!rec)
719 goto out;
722 rec->counter++;
723 out:
724 local_irq_restore(flags);
727 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
728 static int profile_graph_entry(struct ftrace_graph_ent *trace)
730 function_profile_call(trace->func, 0);
731 return 1;
734 static void profile_graph_return(struct ftrace_graph_ret *trace)
736 struct ftrace_profile_stat *stat;
737 unsigned long long calltime;
738 struct ftrace_profile *rec;
739 unsigned long flags;
741 local_irq_save(flags);
742 stat = &__get_cpu_var(ftrace_profile_stats);
743 if (!stat->hash || !ftrace_profile_enabled)
744 goto out;
746 /* If the calltime was zero'd ignore it */
747 if (!trace->calltime)
748 goto out;
750 calltime = trace->rettime - trace->calltime;
752 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
753 int index;
755 index = trace->depth;
757 /* Append this call time to the parent time to subtract */
758 if (index)
759 current->ret_stack[index - 1].subtime += calltime;
761 if (current->ret_stack[index].subtime < calltime)
762 calltime -= current->ret_stack[index].subtime;
763 else
764 calltime = 0;
767 rec = ftrace_find_profiled_func(stat, trace->func);
768 if (rec) {
769 rec->time += calltime;
770 rec->time_squared += calltime * calltime;
773 out:
774 local_irq_restore(flags);
777 static int register_ftrace_profiler(void)
779 return register_ftrace_graph(&profile_graph_return,
780 &profile_graph_entry);
783 static void unregister_ftrace_profiler(void)
785 unregister_ftrace_graph();
787 #else
788 static struct ftrace_ops ftrace_profile_ops __read_mostly =
790 .func = function_profile_call,
793 static int register_ftrace_profiler(void)
795 return register_ftrace_function(&ftrace_profile_ops);
798 static void unregister_ftrace_profiler(void)
800 unregister_ftrace_function(&ftrace_profile_ops);
802 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
804 static ssize_t
805 ftrace_profile_write(struct file *filp, const char __user *ubuf,
806 size_t cnt, loff_t *ppos)
808 unsigned long val;
809 char buf[64]; /* big enough to hold a number */
810 int ret;
812 if (cnt >= sizeof(buf))
813 return -EINVAL;
815 if (copy_from_user(&buf, ubuf, cnt))
816 return -EFAULT;
818 buf[cnt] = 0;
820 ret = strict_strtoul(buf, 10, &val);
821 if (ret < 0)
822 return ret;
824 val = !!val;
826 mutex_lock(&ftrace_profile_lock);
827 if (ftrace_profile_enabled ^ val) {
828 if (val) {
829 ret = ftrace_profile_init();
830 if (ret < 0) {
831 cnt = ret;
832 goto out;
835 ret = register_ftrace_profiler();
836 if (ret < 0) {
837 cnt = ret;
838 goto out;
840 ftrace_profile_enabled = 1;
841 } else {
842 ftrace_profile_enabled = 0;
844 * unregister_ftrace_profiler calls stop_machine
845 * so this acts like an synchronize_sched.
847 unregister_ftrace_profiler();
850 out:
851 mutex_unlock(&ftrace_profile_lock);
853 *ppos += cnt;
855 return cnt;
858 static ssize_t
859 ftrace_profile_read(struct file *filp, char __user *ubuf,
860 size_t cnt, loff_t *ppos)
862 char buf[64]; /* big enough to hold a number */
863 int r;
865 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
866 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
869 static const struct file_operations ftrace_profile_fops = {
870 .open = tracing_open_generic,
871 .read = ftrace_profile_read,
872 .write = ftrace_profile_write,
873 .llseek = default_llseek,
876 /* used to initialize the real stat files */
877 static struct tracer_stat function_stats __initdata = {
878 .name = "functions",
879 .stat_start = function_stat_start,
880 .stat_next = function_stat_next,
881 .stat_cmp = function_stat_cmp,
882 .stat_headers = function_stat_headers,
883 .stat_show = function_stat_show
886 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
888 struct ftrace_profile_stat *stat;
889 struct dentry *entry;
890 char *name;
891 int ret;
892 int cpu;
894 for_each_possible_cpu(cpu) {
895 stat = &per_cpu(ftrace_profile_stats, cpu);
897 /* allocate enough for function name + cpu number */
898 name = kmalloc(32, GFP_KERNEL);
899 if (!name) {
901 * The files created are permanent, if something happens
902 * we still do not free memory.
904 WARN(1,
905 "Could not allocate stat file for cpu %d\n",
906 cpu);
907 return;
909 stat->stat = function_stats;
910 snprintf(name, 32, "function%d", cpu);
911 stat->stat.name = name;
912 ret = register_stat_tracer(&stat->stat);
913 if (ret) {
914 WARN(1,
915 "Could not register function stat for cpu %d\n",
916 cpu);
917 kfree(name);
918 return;
922 entry = debugfs_create_file("function_profile_enabled", 0644,
923 d_tracer, NULL, &ftrace_profile_fops);
924 if (!entry)
925 pr_warning("Could not create debugfs "
926 "'function_profile_enabled' entry\n");
929 #else /* CONFIG_FUNCTION_PROFILER */
930 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
933 #endif /* CONFIG_FUNCTION_PROFILER */
935 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
937 #ifdef CONFIG_DYNAMIC_FTRACE
939 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
940 # error Dynamic ftrace depends on MCOUNT_RECORD
941 #endif
943 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
945 struct ftrace_func_probe {
946 struct hlist_node node;
947 struct ftrace_probe_ops *ops;
948 unsigned long flags;
949 unsigned long ip;
950 void *data;
951 struct rcu_head rcu;
954 enum {
955 FTRACE_ENABLE_CALLS = (1 << 0),
956 FTRACE_DISABLE_CALLS = (1 << 1),
957 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
958 FTRACE_START_FUNC_RET = (1 << 3),
959 FTRACE_STOP_FUNC_RET = (1 << 4),
961 struct ftrace_func_entry {
962 struct hlist_node hlist;
963 unsigned long ip;
966 struct ftrace_hash {
967 unsigned long size_bits;
968 struct hlist_head *buckets;
969 unsigned long count;
970 struct rcu_head rcu;
974 * We make these constant because no one should touch them,
975 * but they are used as the default "empty hash", to avoid allocating
976 * it all the time. These are in a read only section such that if
977 * anyone does try to modify it, it will cause an exception.
979 static const struct hlist_head empty_buckets[1];
980 static const struct ftrace_hash empty_hash = {
981 .buckets = (struct hlist_head *)empty_buckets,
983 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
985 static struct ftrace_ops global_ops = {
986 .func = ftrace_stub,
987 .notrace_hash = EMPTY_HASH,
988 .filter_hash = EMPTY_HASH,
991 static struct dyn_ftrace *ftrace_new_addrs;
993 static DEFINE_MUTEX(ftrace_regex_lock);
995 struct ftrace_page {
996 struct ftrace_page *next;
997 int index;
998 struct dyn_ftrace records[];
1001 #define ENTRIES_PER_PAGE \
1002 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
1004 /* estimate from running different kernels */
1005 #define NR_TO_INIT 10000
1007 static struct ftrace_page *ftrace_pages_start;
1008 static struct ftrace_page *ftrace_pages;
1010 static struct dyn_ftrace *ftrace_free_records;
1012 static struct ftrace_func_entry *
1013 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1015 unsigned long key;
1016 struct ftrace_func_entry *entry;
1017 struct hlist_head *hhd;
1018 struct hlist_node *n;
1020 if (!hash->count)
1021 return NULL;
1023 if (hash->size_bits > 0)
1024 key = hash_long(ip, hash->size_bits);
1025 else
1026 key = 0;
1028 hhd = &hash->buckets[key];
1030 hlist_for_each_entry_rcu(entry, n, hhd, hlist) {
1031 if (entry->ip == ip)
1032 return entry;
1034 return NULL;
1037 static void __add_hash_entry(struct ftrace_hash *hash,
1038 struct ftrace_func_entry *entry)
1040 struct hlist_head *hhd;
1041 unsigned long key;
1043 if (hash->size_bits)
1044 key = hash_long(entry->ip, hash->size_bits);
1045 else
1046 key = 0;
1048 hhd = &hash->buckets[key];
1049 hlist_add_head(&entry->hlist, hhd);
1050 hash->count++;
1053 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1055 struct ftrace_func_entry *entry;
1057 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1058 if (!entry)
1059 return -ENOMEM;
1061 entry->ip = ip;
1062 __add_hash_entry(hash, entry);
1064 return 0;
1067 static void
1068 free_hash_entry(struct ftrace_hash *hash,
1069 struct ftrace_func_entry *entry)
1071 hlist_del(&entry->hlist);
1072 kfree(entry);
1073 hash->count--;
1076 static void
1077 remove_hash_entry(struct ftrace_hash *hash,
1078 struct ftrace_func_entry *entry)
1080 hlist_del(&entry->hlist);
1081 hash->count--;
1084 static void ftrace_hash_clear(struct ftrace_hash *hash)
1086 struct hlist_head *hhd;
1087 struct hlist_node *tp, *tn;
1088 struct ftrace_func_entry *entry;
1089 int size = 1 << hash->size_bits;
1090 int i;
1092 if (!hash->count)
1093 return;
1095 for (i = 0; i < size; i++) {
1096 hhd = &hash->buckets[i];
1097 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist)
1098 free_hash_entry(hash, entry);
1100 FTRACE_WARN_ON(hash->count);
1103 static void free_ftrace_hash(struct ftrace_hash *hash)
1105 if (!hash || hash == EMPTY_HASH)
1106 return;
1107 ftrace_hash_clear(hash);
1108 kfree(hash->buckets);
1109 kfree(hash);
1112 static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1114 struct ftrace_hash *hash;
1116 hash = container_of(rcu, struct ftrace_hash, rcu);
1117 free_ftrace_hash(hash);
1120 static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1122 if (!hash || hash == EMPTY_HASH)
1123 return;
1124 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1127 static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1129 struct ftrace_hash *hash;
1130 int size;
1132 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1133 if (!hash)
1134 return NULL;
1136 size = 1 << size_bits;
1137 hash->buckets = kzalloc(sizeof(*hash->buckets) * size, GFP_KERNEL);
1139 if (!hash->buckets) {
1140 kfree(hash);
1141 return NULL;
1144 hash->size_bits = size_bits;
1146 return hash;
1149 static struct ftrace_hash *
1150 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1152 struct ftrace_func_entry *entry;
1153 struct ftrace_hash *new_hash;
1154 struct hlist_node *tp;
1155 int size;
1156 int ret;
1157 int i;
1159 new_hash = alloc_ftrace_hash(size_bits);
1160 if (!new_hash)
1161 return NULL;
1163 /* Empty hash? */
1164 if (!hash || !hash->count)
1165 return new_hash;
1167 size = 1 << hash->size_bits;
1168 for (i = 0; i < size; i++) {
1169 hlist_for_each_entry(entry, tp, &hash->buckets[i], hlist) {
1170 ret = add_hash_entry(new_hash, entry->ip);
1171 if (ret < 0)
1172 goto free_hash;
1176 FTRACE_WARN_ON(new_hash->count != hash->count);
1178 return new_hash;
1180 free_hash:
1181 free_ftrace_hash(new_hash);
1182 return NULL;
1185 static int
1186 ftrace_hash_move(struct ftrace_hash **dst, struct ftrace_hash *src)
1188 struct ftrace_func_entry *entry;
1189 struct hlist_node *tp, *tn;
1190 struct hlist_head *hhd;
1191 struct ftrace_hash *old_hash;
1192 struct ftrace_hash *new_hash;
1193 unsigned long key;
1194 int size = src->count;
1195 int bits = 0;
1196 int i;
1199 * If the new source is empty, just free dst and assign it
1200 * the empty_hash.
1202 if (!src->count) {
1203 free_ftrace_hash_rcu(*dst);
1204 rcu_assign_pointer(*dst, EMPTY_HASH);
1205 return 0;
1209 * Make the hash size about 1/2 the # found
1211 for (size /= 2; size; size >>= 1)
1212 bits++;
1214 /* Don't allocate too much */
1215 if (bits > FTRACE_HASH_MAX_BITS)
1216 bits = FTRACE_HASH_MAX_BITS;
1218 new_hash = alloc_ftrace_hash(bits);
1219 if (!new_hash)
1220 return -ENOMEM;
1222 size = 1 << src->size_bits;
1223 for (i = 0; i < size; i++) {
1224 hhd = &src->buckets[i];
1225 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist) {
1226 if (bits > 0)
1227 key = hash_long(entry->ip, bits);
1228 else
1229 key = 0;
1230 remove_hash_entry(src, entry);
1231 __add_hash_entry(new_hash, entry);
1235 old_hash = *dst;
1236 rcu_assign_pointer(*dst, new_hash);
1237 free_ftrace_hash_rcu(old_hash);
1239 return 0;
1243 * Test the hashes for this ops to see if we want to call
1244 * the ops->func or not.
1246 * It's a match if the ip is in the ops->filter_hash or
1247 * the filter_hash does not exist or is empty,
1248 * AND
1249 * the ip is not in the ops->notrace_hash.
1251 * This needs to be called with preemption disabled as
1252 * the hashes are freed with call_rcu_sched().
1254 static int
1255 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
1257 struct ftrace_hash *filter_hash;
1258 struct ftrace_hash *notrace_hash;
1259 int ret;
1261 filter_hash = rcu_dereference_raw(ops->filter_hash);
1262 notrace_hash = rcu_dereference_raw(ops->notrace_hash);
1264 if ((!filter_hash || !filter_hash->count ||
1265 ftrace_lookup_ip(filter_hash, ip)) &&
1266 (!notrace_hash || !notrace_hash->count ||
1267 !ftrace_lookup_ip(notrace_hash, ip)))
1268 ret = 1;
1269 else
1270 ret = 0;
1272 return ret;
1276 * This is a double for. Do not use 'break' to break out of the loop,
1277 * you must use a goto.
1279 #define do_for_each_ftrace_rec(pg, rec) \
1280 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1281 int _____i; \
1282 for (_____i = 0; _____i < pg->index; _____i++) { \
1283 rec = &pg->records[_____i];
1285 #define while_for_each_ftrace_rec() \
1289 static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1290 int filter_hash,
1291 bool inc)
1293 struct ftrace_hash *hash;
1294 struct ftrace_hash *other_hash;
1295 struct ftrace_page *pg;
1296 struct dyn_ftrace *rec;
1297 int count = 0;
1298 int all = 0;
1300 /* Only update if the ops has been registered */
1301 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1302 return;
1305 * In the filter_hash case:
1306 * If the count is zero, we update all records.
1307 * Otherwise we just update the items in the hash.
1309 * In the notrace_hash case:
1310 * We enable the update in the hash.
1311 * As disabling notrace means enabling the tracing,
1312 * and enabling notrace means disabling, the inc variable
1313 * gets inversed.
1315 if (filter_hash) {
1316 hash = ops->filter_hash;
1317 other_hash = ops->notrace_hash;
1318 if (!hash || !hash->count)
1319 all = 1;
1320 } else {
1321 inc = !inc;
1322 hash = ops->notrace_hash;
1323 other_hash = ops->filter_hash;
1325 * If the notrace hash has no items,
1326 * then there's nothing to do.
1328 if (hash && !hash->count)
1329 return;
1332 do_for_each_ftrace_rec(pg, rec) {
1333 int in_other_hash = 0;
1334 int in_hash = 0;
1335 int match = 0;
1337 if (all) {
1339 * Only the filter_hash affects all records.
1340 * Update if the record is not in the notrace hash.
1342 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1343 match = 1;
1344 } else {
1345 in_hash = hash && !!ftrace_lookup_ip(hash, rec->ip);
1346 in_other_hash = other_hash && !!ftrace_lookup_ip(other_hash, rec->ip);
1351 if (filter_hash && in_hash && !in_other_hash)
1352 match = 1;
1353 else if (!filter_hash && in_hash &&
1354 (in_other_hash || !other_hash->count))
1355 match = 1;
1357 if (!match)
1358 continue;
1360 if (inc) {
1361 rec->flags++;
1362 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1363 return;
1364 } else {
1365 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1366 return;
1367 rec->flags--;
1369 count++;
1370 /* Shortcut, if we handled all records, we are done. */
1371 if (!all && count == hash->count)
1372 return;
1373 } while_for_each_ftrace_rec();
1376 static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1377 int filter_hash)
1379 __ftrace_hash_rec_update(ops, filter_hash, 0);
1382 static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1383 int filter_hash)
1385 __ftrace_hash_rec_update(ops, filter_hash, 1);
1388 static void ftrace_free_rec(struct dyn_ftrace *rec)
1390 rec->freelist = ftrace_free_records;
1391 ftrace_free_records = rec;
1392 rec->flags |= FTRACE_FL_FREE;
1395 static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
1397 struct dyn_ftrace *rec;
1399 /* First check for freed records */
1400 if (ftrace_free_records) {
1401 rec = ftrace_free_records;
1403 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
1404 FTRACE_WARN_ON_ONCE(1);
1405 ftrace_free_records = NULL;
1406 return NULL;
1409 ftrace_free_records = rec->freelist;
1410 memset(rec, 0, sizeof(*rec));
1411 return rec;
1414 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
1415 if (!ftrace_pages->next) {
1416 /* allocate another page */
1417 ftrace_pages->next =
1418 (void *)get_zeroed_page(GFP_KERNEL);
1419 if (!ftrace_pages->next)
1420 return NULL;
1422 ftrace_pages = ftrace_pages->next;
1425 return &ftrace_pages->records[ftrace_pages->index++];
1428 static struct dyn_ftrace *
1429 ftrace_record_ip(unsigned long ip)
1431 struct dyn_ftrace *rec;
1433 if (ftrace_disabled)
1434 return NULL;
1436 rec = ftrace_alloc_dyn_node(ip);
1437 if (!rec)
1438 return NULL;
1440 rec->ip = ip;
1441 rec->newlist = ftrace_new_addrs;
1442 ftrace_new_addrs = rec;
1444 return rec;
1447 static void print_ip_ins(const char *fmt, unsigned char *p)
1449 int i;
1451 printk(KERN_CONT "%s", fmt);
1453 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1454 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1457 static void ftrace_bug(int failed, unsigned long ip)
1459 switch (failed) {
1460 case -EFAULT:
1461 FTRACE_WARN_ON_ONCE(1);
1462 pr_info("ftrace faulted on modifying ");
1463 print_ip_sym(ip);
1464 break;
1465 case -EINVAL:
1466 FTRACE_WARN_ON_ONCE(1);
1467 pr_info("ftrace failed to modify ");
1468 print_ip_sym(ip);
1469 print_ip_ins(" actual: ", (unsigned char *)ip);
1470 printk(KERN_CONT "\n");
1471 break;
1472 case -EPERM:
1473 FTRACE_WARN_ON_ONCE(1);
1474 pr_info("ftrace faulted on writing ");
1475 print_ip_sym(ip);
1476 break;
1477 default:
1478 FTRACE_WARN_ON_ONCE(1);
1479 pr_info("ftrace faulted on unknown error ");
1480 print_ip_sym(ip);
1485 /* Return 1 if the address range is reserved for ftrace */
1486 int ftrace_text_reserved(void *start, void *end)
1488 struct dyn_ftrace *rec;
1489 struct ftrace_page *pg;
1491 do_for_each_ftrace_rec(pg, rec) {
1492 if (rec->ip <= (unsigned long)end &&
1493 rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
1494 return 1;
1495 } while_for_each_ftrace_rec();
1496 return 0;
1500 static int
1501 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1503 unsigned long ftrace_addr;
1504 unsigned long flag = 0UL;
1506 ftrace_addr = (unsigned long)FTRACE_ADDR;
1509 * If we are enabling tracing:
1511 * If the record has a ref count, then we need to enable it
1512 * because someone is using it.
1514 * Otherwise we make sure its disabled.
1516 * If we are disabling tracing, then disable all records that
1517 * are enabled.
1519 if (enable && (rec->flags & ~FTRACE_FL_MASK))
1520 flag = FTRACE_FL_ENABLED;
1522 /* If the state of this record hasn't changed, then do nothing */
1523 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1524 return 0;
1526 if (flag) {
1527 rec->flags |= FTRACE_FL_ENABLED;
1528 return ftrace_make_call(rec, ftrace_addr);
1531 rec->flags &= ~FTRACE_FL_ENABLED;
1532 return ftrace_make_nop(NULL, rec, ftrace_addr);
1535 static void ftrace_replace_code(int enable)
1537 struct dyn_ftrace *rec;
1538 struct ftrace_page *pg;
1539 int failed;
1541 if (unlikely(ftrace_disabled))
1542 return;
1544 do_for_each_ftrace_rec(pg, rec) {
1545 /* Skip over free records */
1546 if (rec->flags & FTRACE_FL_FREE)
1547 continue;
1549 failed = __ftrace_replace_code(rec, enable);
1550 if (failed) {
1551 ftrace_bug(failed, rec->ip);
1552 /* Stop processing */
1553 return;
1555 } while_for_each_ftrace_rec();
1558 static int
1559 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
1561 unsigned long ip;
1562 int ret;
1564 ip = rec->ip;
1566 if (unlikely(ftrace_disabled))
1567 return 0;
1569 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
1570 if (ret) {
1571 ftrace_bug(ret, ip);
1572 return 0;
1574 return 1;
1578 * archs can override this function if they must do something
1579 * before the modifying code is performed.
1581 int __weak ftrace_arch_code_modify_prepare(void)
1583 return 0;
1587 * archs can override this function if they must do something
1588 * after the modifying code is performed.
1590 int __weak ftrace_arch_code_modify_post_process(void)
1592 return 0;
1595 static int __ftrace_modify_code(void *data)
1597 int *command = data;
1599 if (*command & FTRACE_ENABLE_CALLS)
1600 ftrace_replace_code(1);
1601 else if (*command & FTRACE_DISABLE_CALLS)
1602 ftrace_replace_code(0);
1604 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1605 ftrace_update_ftrace_func(ftrace_trace_function);
1607 if (*command & FTRACE_START_FUNC_RET)
1608 ftrace_enable_ftrace_graph_caller();
1609 else if (*command & FTRACE_STOP_FUNC_RET)
1610 ftrace_disable_ftrace_graph_caller();
1612 return 0;
1615 static void ftrace_run_update_code(int command)
1617 int ret;
1619 ret = ftrace_arch_code_modify_prepare();
1620 FTRACE_WARN_ON(ret);
1621 if (ret)
1622 return;
1624 stop_machine(__ftrace_modify_code, &command, NULL);
1626 ret = ftrace_arch_code_modify_post_process();
1627 FTRACE_WARN_ON(ret);
1630 static ftrace_func_t saved_ftrace_func;
1631 static int ftrace_start_up;
1632 static int global_start_up;
1634 static void ftrace_startup_enable(int command)
1636 if (saved_ftrace_func != ftrace_trace_function) {
1637 saved_ftrace_func = ftrace_trace_function;
1638 command |= FTRACE_UPDATE_TRACE_FUNC;
1641 if (!command || !ftrace_enabled)
1642 return;
1644 ftrace_run_update_code(command);
1647 static int ftrace_startup(struct ftrace_ops *ops, int command)
1649 bool hash_enable = true;
1651 if (unlikely(ftrace_disabled))
1652 return -ENODEV;
1654 ftrace_start_up++;
1655 command |= FTRACE_ENABLE_CALLS;
1657 /* ops marked global share the filter hashes */
1658 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
1659 ops = &global_ops;
1660 /* Don't update hash if global is already set */
1661 if (global_start_up)
1662 hash_enable = false;
1663 global_start_up++;
1666 ops->flags |= FTRACE_OPS_FL_ENABLED;
1667 if (hash_enable)
1668 ftrace_hash_rec_enable(ops, 1);
1670 ftrace_startup_enable(command);
1672 return 0;
1675 static void ftrace_shutdown(struct ftrace_ops *ops, int command)
1677 bool hash_disable = true;
1679 if (unlikely(ftrace_disabled))
1680 return;
1682 ftrace_start_up--;
1684 * Just warn in case of unbalance, no need to kill ftrace, it's not
1685 * critical but the ftrace_call callers may be never nopped again after
1686 * further ftrace uses.
1688 WARN_ON_ONCE(ftrace_start_up < 0);
1690 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
1691 ops = &global_ops;
1692 global_start_up--;
1693 WARN_ON_ONCE(global_start_up < 0);
1694 /* Don't update hash if global still has users */
1695 if (global_start_up) {
1696 WARN_ON_ONCE(!ftrace_start_up);
1697 hash_disable = false;
1701 if (hash_disable)
1702 ftrace_hash_rec_disable(ops, 1);
1704 if (ops != &global_ops || !global_start_up)
1705 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
1707 if (!ftrace_start_up)
1708 command |= FTRACE_DISABLE_CALLS;
1710 if (saved_ftrace_func != ftrace_trace_function) {
1711 saved_ftrace_func = ftrace_trace_function;
1712 command |= FTRACE_UPDATE_TRACE_FUNC;
1715 if (!command || !ftrace_enabled)
1716 return;
1718 ftrace_run_update_code(command);
1721 static void ftrace_startup_sysctl(void)
1723 if (unlikely(ftrace_disabled))
1724 return;
1726 /* Force update next time */
1727 saved_ftrace_func = NULL;
1728 /* ftrace_start_up is true if we want ftrace running */
1729 if (ftrace_start_up)
1730 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
1733 static void ftrace_shutdown_sysctl(void)
1735 if (unlikely(ftrace_disabled))
1736 return;
1738 /* ftrace_start_up is true if ftrace is running */
1739 if (ftrace_start_up)
1740 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
1743 static cycle_t ftrace_update_time;
1744 static unsigned long ftrace_update_cnt;
1745 unsigned long ftrace_update_tot_cnt;
1747 static int ftrace_update_code(struct module *mod)
1749 struct dyn_ftrace *p;
1750 cycle_t start, stop;
1752 start = ftrace_now(raw_smp_processor_id());
1753 ftrace_update_cnt = 0;
1755 while (ftrace_new_addrs) {
1757 /* If something went wrong, bail without enabling anything */
1758 if (unlikely(ftrace_disabled))
1759 return -1;
1761 p = ftrace_new_addrs;
1762 ftrace_new_addrs = p->newlist;
1763 p->flags = 0L;
1766 * Do the initial record conversion from mcount jump
1767 * to the NOP instructions.
1769 if (!ftrace_code_disable(mod, p)) {
1770 ftrace_free_rec(p);
1771 /* Game over */
1772 break;
1775 ftrace_update_cnt++;
1778 * If the tracing is enabled, go ahead and enable the record.
1780 * The reason not to enable the record immediatelly is the
1781 * inherent check of ftrace_make_nop/ftrace_make_call for
1782 * correct previous instructions. Making first the NOP
1783 * conversion puts the module to the correct state, thus
1784 * passing the ftrace_make_call check.
1786 if (ftrace_start_up) {
1787 int failed = __ftrace_replace_code(p, 1);
1788 if (failed) {
1789 ftrace_bug(failed, p->ip);
1790 ftrace_free_rec(p);
1795 stop = ftrace_now(raw_smp_processor_id());
1796 ftrace_update_time = stop - start;
1797 ftrace_update_tot_cnt += ftrace_update_cnt;
1799 return 0;
1802 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
1804 struct ftrace_page *pg;
1805 int cnt;
1806 int i;
1808 /* allocate a few pages */
1809 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1810 if (!ftrace_pages_start)
1811 return -1;
1814 * Allocate a few more pages.
1816 * TODO: have some parser search vmlinux before
1817 * final linking to find all calls to ftrace.
1818 * Then we can:
1819 * a) know how many pages to allocate.
1820 * and/or
1821 * b) set up the table then.
1823 * The dynamic code is still necessary for
1824 * modules.
1827 pg = ftrace_pages = ftrace_pages_start;
1829 cnt = num_to_init / ENTRIES_PER_PAGE;
1830 pr_info("ftrace: allocating %ld entries in %d pages\n",
1831 num_to_init, cnt + 1);
1833 for (i = 0; i < cnt; i++) {
1834 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1836 /* If we fail, we'll try later anyway */
1837 if (!pg->next)
1838 break;
1840 pg = pg->next;
1843 return 0;
1846 enum {
1847 FTRACE_ITER_FILTER = (1 << 0),
1848 FTRACE_ITER_NOTRACE = (1 << 1),
1849 FTRACE_ITER_PRINTALL = (1 << 2),
1850 FTRACE_ITER_HASH = (1 << 3),
1851 FTRACE_ITER_ENABLED = (1 << 4),
1854 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1856 struct ftrace_iterator {
1857 loff_t pos;
1858 loff_t func_pos;
1859 struct ftrace_page *pg;
1860 struct dyn_ftrace *func;
1861 struct ftrace_func_probe *probe;
1862 struct trace_parser parser;
1863 struct ftrace_hash *hash;
1864 struct ftrace_ops *ops;
1865 int hidx;
1866 int idx;
1867 unsigned flags;
1870 static void *
1871 t_hash_next(struct seq_file *m, loff_t *pos)
1873 struct ftrace_iterator *iter = m->private;
1874 struct hlist_node *hnd = NULL;
1875 struct hlist_head *hhd;
1877 (*pos)++;
1878 iter->pos = *pos;
1880 if (iter->probe)
1881 hnd = &iter->probe->node;
1882 retry:
1883 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1884 return NULL;
1886 hhd = &ftrace_func_hash[iter->hidx];
1888 if (hlist_empty(hhd)) {
1889 iter->hidx++;
1890 hnd = NULL;
1891 goto retry;
1894 if (!hnd)
1895 hnd = hhd->first;
1896 else {
1897 hnd = hnd->next;
1898 if (!hnd) {
1899 iter->hidx++;
1900 goto retry;
1904 if (WARN_ON_ONCE(!hnd))
1905 return NULL;
1907 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
1909 return iter;
1912 static void *t_hash_start(struct seq_file *m, loff_t *pos)
1914 struct ftrace_iterator *iter = m->private;
1915 void *p = NULL;
1916 loff_t l;
1918 if (iter->func_pos > *pos)
1919 return NULL;
1921 iter->hidx = 0;
1922 for (l = 0; l <= (*pos - iter->func_pos); ) {
1923 p = t_hash_next(m, &l);
1924 if (!p)
1925 break;
1927 if (!p)
1928 return NULL;
1930 /* Only set this if we have an item */
1931 iter->flags |= FTRACE_ITER_HASH;
1933 return iter;
1936 static int
1937 t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
1939 struct ftrace_func_probe *rec;
1941 rec = iter->probe;
1942 if (WARN_ON_ONCE(!rec))
1943 return -EIO;
1945 if (rec->ops->print)
1946 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1948 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
1950 if (rec->data)
1951 seq_printf(m, ":%p", rec->data);
1952 seq_putc(m, '\n');
1954 return 0;
1957 static void *
1958 t_next(struct seq_file *m, void *v, loff_t *pos)
1960 struct ftrace_iterator *iter = m->private;
1961 struct ftrace_ops *ops = &global_ops;
1962 struct dyn_ftrace *rec = NULL;
1964 if (unlikely(ftrace_disabled))
1965 return NULL;
1967 if (iter->flags & FTRACE_ITER_HASH)
1968 return t_hash_next(m, pos);
1970 (*pos)++;
1971 iter->pos = iter->func_pos = *pos;
1973 if (iter->flags & FTRACE_ITER_PRINTALL)
1974 return t_hash_start(m, pos);
1976 retry:
1977 if (iter->idx >= iter->pg->index) {
1978 if (iter->pg->next) {
1979 iter->pg = iter->pg->next;
1980 iter->idx = 0;
1981 goto retry;
1983 } else {
1984 rec = &iter->pg->records[iter->idx++];
1985 if ((rec->flags & FTRACE_FL_FREE) ||
1987 ((iter->flags & FTRACE_ITER_FILTER) &&
1988 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
1990 ((iter->flags & FTRACE_ITER_NOTRACE) &&
1991 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
1993 ((iter->flags & FTRACE_ITER_ENABLED) &&
1994 !(rec->flags & ~FTRACE_FL_MASK))) {
1996 rec = NULL;
1997 goto retry;
2001 if (!rec)
2002 return t_hash_start(m, pos);
2004 iter->func = rec;
2006 return iter;
2009 static void reset_iter_read(struct ftrace_iterator *iter)
2011 iter->pos = 0;
2012 iter->func_pos = 0;
2013 iter->flags &= ~(FTRACE_ITER_PRINTALL & FTRACE_ITER_HASH);
2016 static void *t_start(struct seq_file *m, loff_t *pos)
2018 struct ftrace_iterator *iter = m->private;
2019 struct ftrace_ops *ops = &global_ops;
2020 void *p = NULL;
2021 loff_t l;
2023 mutex_lock(&ftrace_lock);
2025 if (unlikely(ftrace_disabled))
2026 return NULL;
2029 * If an lseek was done, then reset and start from beginning.
2031 if (*pos < iter->pos)
2032 reset_iter_read(iter);
2035 * For set_ftrace_filter reading, if we have the filter
2036 * off, we can short cut and just print out that all
2037 * functions are enabled.
2039 if (iter->flags & FTRACE_ITER_FILTER && !ops->filter_hash->count) {
2040 if (*pos > 0)
2041 return t_hash_start(m, pos);
2042 iter->flags |= FTRACE_ITER_PRINTALL;
2043 /* reset in case of seek/pread */
2044 iter->flags &= ~FTRACE_ITER_HASH;
2045 return iter;
2048 if (iter->flags & FTRACE_ITER_HASH)
2049 return t_hash_start(m, pos);
2052 * Unfortunately, we need to restart at ftrace_pages_start
2053 * every time we let go of the ftrace_mutex. This is because
2054 * those pointers can change without the lock.
2056 iter->pg = ftrace_pages_start;
2057 iter->idx = 0;
2058 for (l = 0; l <= *pos; ) {
2059 p = t_next(m, p, &l);
2060 if (!p)
2061 break;
2064 if (!p) {
2065 if (iter->flags & FTRACE_ITER_FILTER)
2066 return t_hash_start(m, pos);
2068 return NULL;
2071 return iter;
2074 static void t_stop(struct seq_file *m, void *p)
2076 mutex_unlock(&ftrace_lock);
2079 static int t_show(struct seq_file *m, void *v)
2081 struct ftrace_iterator *iter = m->private;
2082 struct dyn_ftrace *rec;
2084 if (iter->flags & FTRACE_ITER_HASH)
2085 return t_hash_show(m, iter);
2087 if (iter->flags & FTRACE_ITER_PRINTALL) {
2088 seq_printf(m, "#### all functions enabled ####\n");
2089 return 0;
2092 rec = iter->func;
2094 if (!rec)
2095 return 0;
2097 seq_printf(m, "%ps", (void *)rec->ip);
2098 if (iter->flags & FTRACE_ITER_ENABLED)
2099 seq_printf(m, " (%ld)",
2100 rec->flags & ~FTRACE_FL_MASK);
2101 seq_printf(m, "\n");
2103 return 0;
2106 static const struct seq_operations show_ftrace_seq_ops = {
2107 .start = t_start,
2108 .next = t_next,
2109 .stop = t_stop,
2110 .show = t_show,
2113 static int
2114 ftrace_avail_open(struct inode *inode, struct file *file)
2116 struct ftrace_iterator *iter;
2117 int ret;
2119 if (unlikely(ftrace_disabled))
2120 return -ENODEV;
2122 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2123 if (!iter)
2124 return -ENOMEM;
2126 iter->pg = ftrace_pages_start;
2128 ret = seq_open(file, &show_ftrace_seq_ops);
2129 if (!ret) {
2130 struct seq_file *m = file->private_data;
2132 m->private = iter;
2133 } else {
2134 kfree(iter);
2137 return ret;
2140 static int
2141 ftrace_enabled_open(struct inode *inode, struct file *file)
2143 struct ftrace_iterator *iter;
2144 int ret;
2146 if (unlikely(ftrace_disabled))
2147 return -ENODEV;
2149 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2150 if (!iter)
2151 return -ENOMEM;
2153 iter->pg = ftrace_pages_start;
2154 iter->flags = FTRACE_ITER_ENABLED;
2156 ret = seq_open(file, &show_ftrace_seq_ops);
2157 if (!ret) {
2158 struct seq_file *m = file->private_data;
2160 m->private = iter;
2161 } else {
2162 kfree(iter);
2165 return ret;
2168 static void ftrace_filter_reset(struct ftrace_hash *hash)
2170 mutex_lock(&ftrace_lock);
2171 ftrace_hash_clear(hash);
2172 mutex_unlock(&ftrace_lock);
2175 static int
2176 ftrace_regex_open(struct ftrace_ops *ops, int flag,
2177 struct inode *inode, struct file *file)
2179 struct ftrace_iterator *iter;
2180 struct ftrace_hash *hash;
2181 int ret = 0;
2183 if (unlikely(ftrace_disabled))
2184 return -ENODEV;
2186 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2187 if (!iter)
2188 return -ENOMEM;
2190 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2191 kfree(iter);
2192 return -ENOMEM;
2195 if (flag & FTRACE_ITER_NOTRACE)
2196 hash = ops->notrace_hash;
2197 else
2198 hash = ops->filter_hash;
2200 iter->ops = ops;
2201 iter->flags = flag;
2203 if (file->f_mode & FMODE_WRITE) {
2204 mutex_lock(&ftrace_lock);
2205 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2206 mutex_unlock(&ftrace_lock);
2208 if (!iter->hash) {
2209 trace_parser_put(&iter->parser);
2210 kfree(iter);
2211 return -ENOMEM;
2215 mutex_lock(&ftrace_regex_lock);
2217 if ((file->f_mode & FMODE_WRITE) &&
2218 (file->f_flags & O_TRUNC))
2219 ftrace_filter_reset(iter->hash);
2221 if (file->f_mode & FMODE_READ) {
2222 iter->pg = ftrace_pages_start;
2224 ret = seq_open(file, &show_ftrace_seq_ops);
2225 if (!ret) {
2226 struct seq_file *m = file->private_data;
2227 m->private = iter;
2228 } else {
2229 /* Failed */
2230 free_ftrace_hash(iter->hash);
2231 trace_parser_put(&iter->parser);
2232 kfree(iter);
2234 } else
2235 file->private_data = iter;
2236 mutex_unlock(&ftrace_regex_lock);
2238 return ret;
2241 static int
2242 ftrace_filter_open(struct inode *inode, struct file *file)
2244 return ftrace_regex_open(&global_ops, FTRACE_ITER_FILTER,
2245 inode, file);
2248 static int
2249 ftrace_notrace_open(struct inode *inode, struct file *file)
2251 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
2252 inode, file);
2255 static loff_t
2256 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
2258 loff_t ret;
2260 if (file->f_mode & FMODE_READ)
2261 ret = seq_lseek(file, offset, origin);
2262 else
2263 file->f_pos = ret = 1;
2265 return ret;
2268 static int ftrace_match(char *str, char *regex, int len, int type)
2270 int matched = 0;
2271 int slen;
2273 switch (type) {
2274 case MATCH_FULL:
2275 if (strcmp(str, regex) == 0)
2276 matched = 1;
2277 break;
2278 case MATCH_FRONT_ONLY:
2279 if (strncmp(str, regex, len) == 0)
2280 matched = 1;
2281 break;
2282 case MATCH_MIDDLE_ONLY:
2283 if (strstr(str, regex))
2284 matched = 1;
2285 break;
2286 case MATCH_END_ONLY:
2287 slen = strlen(str);
2288 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
2289 matched = 1;
2290 break;
2293 return matched;
2296 static int
2297 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
2299 struct ftrace_func_entry *entry;
2300 int ret = 0;
2302 entry = ftrace_lookup_ip(hash, rec->ip);
2303 if (not) {
2304 /* Do nothing if it doesn't exist */
2305 if (!entry)
2306 return 0;
2308 free_hash_entry(hash, entry);
2309 } else {
2310 /* Do nothing if it exists */
2311 if (entry)
2312 return 0;
2314 ret = add_hash_entry(hash, rec->ip);
2316 return ret;
2319 static int
2320 ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2321 char *regex, int len, int type)
2323 char str[KSYM_SYMBOL_LEN];
2324 char *modname;
2326 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2328 if (mod) {
2329 /* module lookup requires matching the module */
2330 if (!modname || strcmp(modname, mod))
2331 return 0;
2333 /* blank search means to match all funcs in the mod */
2334 if (!len)
2335 return 1;
2338 return ftrace_match(str, regex, len, type);
2341 static int
2342 match_records(struct ftrace_hash *hash, char *buff,
2343 int len, char *mod, int not)
2345 unsigned search_len = 0;
2346 struct ftrace_page *pg;
2347 struct dyn_ftrace *rec;
2348 int type = MATCH_FULL;
2349 char *search = buff;
2350 int found = 0;
2351 int ret;
2353 if (len) {
2354 type = filter_parse_regex(buff, len, &search, &not);
2355 search_len = strlen(search);
2358 mutex_lock(&ftrace_lock);
2360 if (unlikely(ftrace_disabled))
2361 goto out_unlock;
2363 do_for_each_ftrace_rec(pg, rec) {
2365 if (ftrace_match_record(rec, mod, search, search_len, type)) {
2366 ret = enter_record(hash, rec, not);
2367 if (ret < 0) {
2368 found = ret;
2369 goto out_unlock;
2371 found = 1;
2373 } while_for_each_ftrace_rec();
2374 out_unlock:
2375 mutex_unlock(&ftrace_lock);
2377 return found;
2380 static int
2381 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
2383 return match_records(hash, buff, len, NULL, 0);
2386 static int
2387 ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
2389 int not = 0;
2391 /* blank or '*' mean the same */
2392 if (strcmp(buff, "*") == 0)
2393 buff[0] = 0;
2395 /* handle the case of 'dont filter this module' */
2396 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2397 buff[0] = 0;
2398 not = 1;
2401 return match_records(hash, buff, strlen(buff), mod, not);
2405 * We register the module command as a template to show others how
2406 * to register the a command as well.
2409 static int
2410 ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
2412 struct ftrace_ops *ops = &global_ops;
2413 struct ftrace_hash *hash;
2414 char *mod;
2415 int ret = -EINVAL;
2418 * cmd == 'mod' because we only registered this func
2419 * for the 'mod' ftrace_func_command.
2420 * But if you register one func with multiple commands,
2421 * you can tell which command was used by the cmd
2422 * parameter.
2425 /* we must have a module name */
2426 if (!param)
2427 return ret;
2429 mod = strsep(&param, ":");
2430 if (!strlen(mod))
2431 return ret;
2433 if (enable)
2434 hash = ops->filter_hash;
2435 else
2436 hash = ops->notrace_hash;
2438 ret = ftrace_match_module_records(hash, func, mod);
2439 if (!ret)
2440 ret = -EINVAL;
2441 if (ret < 0)
2442 return ret;
2444 return 0;
2447 static struct ftrace_func_command ftrace_mod_cmd = {
2448 .name = "mod",
2449 .func = ftrace_mod_callback,
2452 static int __init ftrace_mod_cmd_init(void)
2454 return register_ftrace_command(&ftrace_mod_cmd);
2456 device_initcall(ftrace_mod_cmd_init);
2458 static void
2459 function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
2461 struct ftrace_func_probe *entry;
2462 struct hlist_head *hhd;
2463 struct hlist_node *n;
2464 unsigned long key;
2466 key = hash_long(ip, FTRACE_HASH_BITS);
2468 hhd = &ftrace_func_hash[key];
2470 if (hlist_empty(hhd))
2471 return;
2474 * Disable preemption for these calls to prevent a RCU grace
2475 * period. This syncs the hash iteration and freeing of items
2476 * on the hash. rcu_read_lock is too dangerous here.
2478 preempt_disable_notrace();
2479 hlist_for_each_entry_rcu(entry, n, hhd, node) {
2480 if (entry->ip == ip)
2481 entry->ops->func(ip, parent_ip, &entry->data);
2483 preempt_enable_notrace();
2486 static struct ftrace_ops trace_probe_ops __read_mostly =
2488 .func = function_trace_probe_call,
2491 static int ftrace_probe_registered;
2493 static void __enable_ftrace_function_probe(void)
2495 int ret;
2496 int i;
2498 if (ftrace_probe_registered)
2499 return;
2501 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2502 struct hlist_head *hhd = &ftrace_func_hash[i];
2503 if (hhd->first)
2504 break;
2506 /* Nothing registered? */
2507 if (i == FTRACE_FUNC_HASHSIZE)
2508 return;
2510 ret = __register_ftrace_function(&trace_probe_ops);
2511 if (!ret)
2512 ret = ftrace_startup(&trace_probe_ops, 0);
2514 ftrace_probe_registered = 1;
2517 static void __disable_ftrace_function_probe(void)
2519 int ret;
2520 int i;
2522 if (!ftrace_probe_registered)
2523 return;
2525 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2526 struct hlist_head *hhd = &ftrace_func_hash[i];
2527 if (hhd->first)
2528 return;
2531 /* no more funcs left */
2532 ret = __unregister_ftrace_function(&trace_probe_ops);
2533 if (!ret)
2534 ftrace_shutdown(&trace_probe_ops, 0);
2536 ftrace_probe_registered = 0;
2540 static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2542 struct ftrace_func_probe *entry =
2543 container_of(rhp, struct ftrace_func_probe, rcu);
2545 if (entry->ops->free)
2546 entry->ops->free(&entry->data);
2547 kfree(entry);
2552 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2553 void *data)
2555 struct ftrace_func_probe *entry;
2556 struct ftrace_page *pg;
2557 struct dyn_ftrace *rec;
2558 int type, len, not;
2559 unsigned long key;
2560 int count = 0;
2561 char *search;
2563 type = filter_parse_regex(glob, strlen(glob), &search, &not);
2564 len = strlen(search);
2566 /* we do not support '!' for function probes */
2567 if (WARN_ON(not))
2568 return -EINVAL;
2570 mutex_lock(&ftrace_lock);
2572 if (unlikely(ftrace_disabled))
2573 goto out_unlock;
2575 do_for_each_ftrace_rec(pg, rec) {
2577 if (!ftrace_match_record(rec, NULL, search, len, type))
2578 continue;
2580 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2581 if (!entry) {
2582 /* If we did not process any, then return error */
2583 if (!count)
2584 count = -ENOMEM;
2585 goto out_unlock;
2588 count++;
2590 entry->data = data;
2593 * The caller might want to do something special
2594 * for each function we find. We call the callback
2595 * to give the caller an opportunity to do so.
2597 if (ops->callback) {
2598 if (ops->callback(rec->ip, &entry->data) < 0) {
2599 /* caller does not like this func */
2600 kfree(entry);
2601 continue;
2605 entry->ops = ops;
2606 entry->ip = rec->ip;
2608 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2609 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2611 } while_for_each_ftrace_rec();
2612 __enable_ftrace_function_probe();
2614 out_unlock:
2615 mutex_unlock(&ftrace_lock);
2617 return count;
2620 enum {
2621 PROBE_TEST_FUNC = 1,
2622 PROBE_TEST_DATA = 2
2625 static void
2626 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2627 void *data, int flags)
2629 struct ftrace_func_probe *entry;
2630 struct hlist_node *n, *tmp;
2631 char str[KSYM_SYMBOL_LEN];
2632 int type = MATCH_FULL;
2633 int i, len = 0;
2634 char *search;
2636 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
2637 glob = NULL;
2638 else if (glob) {
2639 int not;
2641 type = filter_parse_regex(glob, strlen(glob), &search, &not);
2642 len = strlen(search);
2644 /* we do not support '!' for function probes */
2645 if (WARN_ON(not))
2646 return;
2649 mutex_lock(&ftrace_lock);
2650 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2651 struct hlist_head *hhd = &ftrace_func_hash[i];
2653 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2655 /* break up if statements for readability */
2656 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
2657 continue;
2659 if ((flags & PROBE_TEST_DATA) && entry->data != data)
2660 continue;
2662 /* do this last, since it is the most expensive */
2663 if (glob) {
2664 kallsyms_lookup(entry->ip, NULL, NULL,
2665 NULL, str);
2666 if (!ftrace_match(str, glob, len, type))
2667 continue;
2670 hlist_del(&entry->node);
2671 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2674 __disable_ftrace_function_probe();
2675 mutex_unlock(&ftrace_lock);
2678 void
2679 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2680 void *data)
2682 __unregister_ftrace_function_probe(glob, ops, data,
2683 PROBE_TEST_FUNC | PROBE_TEST_DATA);
2686 void
2687 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
2689 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
2692 void unregister_ftrace_function_probe_all(char *glob)
2694 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
2697 static LIST_HEAD(ftrace_commands);
2698 static DEFINE_MUTEX(ftrace_cmd_mutex);
2700 int register_ftrace_command(struct ftrace_func_command *cmd)
2702 struct ftrace_func_command *p;
2703 int ret = 0;
2705 mutex_lock(&ftrace_cmd_mutex);
2706 list_for_each_entry(p, &ftrace_commands, list) {
2707 if (strcmp(cmd->name, p->name) == 0) {
2708 ret = -EBUSY;
2709 goto out_unlock;
2712 list_add(&cmd->list, &ftrace_commands);
2713 out_unlock:
2714 mutex_unlock(&ftrace_cmd_mutex);
2716 return ret;
2719 int unregister_ftrace_command(struct ftrace_func_command *cmd)
2721 struct ftrace_func_command *p, *n;
2722 int ret = -ENODEV;
2724 mutex_lock(&ftrace_cmd_mutex);
2725 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2726 if (strcmp(cmd->name, p->name) == 0) {
2727 ret = 0;
2728 list_del_init(&p->list);
2729 goto out_unlock;
2732 out_unlock:
2733 mutex_unlock(&ftrace_cmd_mutex);
2735 return ret;
2738 static int ftrace_process_regex(struct ftrace_hash *hash,
2739 char *buff, int len, int enable)
2741 char *func, *command, *next = buff;
2742 struct ftrace_func_command *p;
2743 int ret;
2745 func = strsep(&next, ":");
2747 if (!next) {
2748 ret = ftrace_match_records(hash, func, len);
2749 if (!ret)
2750 ret = -EINVAL;
2751 if (ret < 0)
2752 return ret;
2753 return 0;
2756 /* command found */
2758 command = strsep(&next, ":");
2760 mutex_lock(&ftrace_cmd_mutex);
2761 list_for_each_entry(p, &ftrace_commands, list) {
2762 if (strcmp(p->name, command) == 0) {
2763 ret = p->func(func, command, next, enable);
2764 goto out_unlock;
2767 out_unlock:
2768 mutex_unlock(&ftrace_cmd_mutex);
2770 return ret;
2773 static ssize_t
2774 ftrace_regex_write(struct file *file, const char __user *ubuf,
2775 size_t cnt, loff_t *ppos, int enable)
2777 struct ftrace_iterator *iter;
2778 struct trace_parser *parser;
2779 ssize_t ret, read;
2781 if (!cnt)
2782 return 0;
2784 mutex_lock(&ftrace_regex_lock);
2786 ret = -ENODEV;
2787 if (unlikely(ftrace_disabled))
2788 goto out_unlock;
2790 if (file->f_mode & FMODE_READ) {
2791 struct seq_file *m = file->private_data;
2792 iter = m->private;
2793 } else
2794 iter = file->private_data;
2796 parser = &iter->parser;
2797 read = trace_get_user(parser, ubuf, cnt, ppos);
2799 if (read >= 0 && trace_parser_loaded(parser) &&
2800 !trace_parser_cont(parser)) {
2801 ret = ftrace_process_regex(iter->hash, parser->buffer,
2802 parser->idx, enable);
2803 trace_parser_clear(parser);
2804 if (ret)
2805 goto out_unlock;
2808 ret = read;
2809 out_unlock:
2810 mutex_unlock(&ftrace_regex_lock);
2812 return ret;
2815 static ssize_t
2816 ftrace_filter_write(struct file *file, const char __user *ubuf,
2817 size_t cnt, loff_t *ppos)
2819 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2822 static ssize_t
2823 ftrace_notrace_write(struct file *file, const char __user *ubuf,
2824 size_t cnt, loff_t *ppos)
2826 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2829 static int
2830 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
2831 int reset, int enable)
2833 struct ftrace_hash **orig_hash;
2834 struct ftrace_hash *hash;
2835 int ret;
2837 /* All global ops uses the global ops filters */
2838 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
2839 ops = &global_ops;
2841 if (unlikely(ftrace_disabled))
2842 return -ENODEV;
2844 if (enable)
2845 orig_hash = &ops->filter_hash;
2846 else
2847 orig_hash = &ops->notrace_hash;
2849 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
2850 if (!hash)
2851 return -ENOMEM;
2853 mutex_lock(&ftrace_regex_lock);
2854 if (reset)
2855 ftrace_filter_reset(hash);
2856 if (buf)
2857 ftrace_match_records(hash, buf, len);
2859 mutex_lock(&ftrace_lock);
2860 ret = ftrace_hash_move(orig_hash, hash);
2861 mutex_unlock(&ftrace_lock);
2863 mutex_unlock(&ftrace_regex_lock);
2865 free_ftrace_hash(hash);
2866 return ret;
2870 * ftrace_set_filter - set a function to filter on in ftrace
2871 * @ops - the ops to set the filter with
2872 * @buf - the string that holds the function filter text.
2873 * @len - the length of the string.
2874 * @reset - non zero to reset all filters before applying this filter.
2876 * Filters denote which functions should be enabled when tracing is enabled.
2877 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2879 void ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
2880 int len, int reset)
2882 ftrace_set_regex(ops, buf, len, reset, 1);
2884 EXPORT_SYMBOL_GPL(ftrace_set_filter);
2887 * ftrace_set_notrace - set a function to not trace in ftrace
2888 * @ops - the ops to set the notrace filter with
2889 * @buf - the string that holds the function notrace text.
2890 * @len - the length of the string.
2891 * @reset - non zero to reset all filters before applying this filter.
2893 * Notrace Filters denote which functions should not be enabled when tracing
2894 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2895 * for tracing.
2897 void ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
2898 int len, int reset)
2900 ftrace_set_regex(ops, buf, len, reset, 0);
2902 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
2904 * ftrace_set_filter - set a function to filter on in ftrace
2905 * @ops - the ops to set the filter with
2906 * @buf - the string that holds the function filter text.
2907 * @len - the length of the string.
2908 * @reset - non zero to reset all filters before applying this filter.
2910 * Filters denote which functions should be enabled when tracing is enabled.
2911 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2913 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
2915 ftrace_set_regex(&global_ops, buf, len, reset, 1);
2917 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
2920 * ftrace_set_notrace - set a function to not trace in ftrace
2921 * @ops - the ops to set the notrace filter with
2922 * @buf - the string that holds the function notrace text.
2923 * @len - the length of the string.
2924 * @reset - non zero to reset all filters before applying this filter.
2926 * Notrace Filters denote which functions should not be enabled when tracing
2927 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2928 * for tracing.
2930 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
2932 ftrace_set_regex(&global_ops, buf, len, reset, 0);
2934 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
2937 * command line interface to allow users to set filters on boot up.
2939 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2940 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
2941 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
2943 static int __init set_ftrace_notrace(char *str)
2945 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
2946 return 1;
2948 __setup("ftrace_notrace=", set_ftrace_notrace);
2950 static int __init set_ftrace_filter(char *str)
2952 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
2953 return 1;
2955 __setup("ftrace_filter=", set_ftrace_filter);
2957 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2958 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
2959 static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
2961 static int __init set_graph_function(char *str)
2963 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
2964 return 1;
2966 __setup("ftrace_graph_filter=", set_graph_function);
2968 static void __init set_ftrace_early_graph(char *buf)
2970 int ret;
2971 char *func;
2973 while (buf) {
2974 func = strsep(&buf, ",");
2975 /* we allow only one expression at a time */
2976 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
2977 func);
2978 if (ret)
2979 printk(KERN_DEBUG "ftrace: function %s not "
2980 "traceable\n", func);
2983 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2985 static void __init
2986 set_ftrace_early_filter(struct ftrace_ops *ops, char *buf, int enable)
2988 char *func;
2990 while (buf) {
2991 func = strsep(&buf, ",");
2992 ftrace_set_regex(ops, func, strlen(func), 0, enable);
2996 static void __init set_ftrace_early_filters(void)
2998 if (ftrace_filter_buf[0])
2999 set_ftrace_early_filter(&global_ops, ftrace_filter_buf, 1);
3000 if (ftrace_notrace_buf[0])
3001 set_ftrace_early_filter(&global_ops, ftrace_notrace_buf, 0);
3002 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3003 if (ftrace_graph_buf[0])
3004 set_ftrace_early_graph(ftrace_graph_buf);
3005 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3008 static int
3009 ftrace_regex_release(struct inode *inode, struct file *file)
3011 struct seq_file *m = (struct seq_file *)file->private_data;
3012 struct ftrace_iterator *iter;
3013 struct ftrace_hash **orig_hash;
3014 struct trace_parser *parser;
3015 int filter_hash;
3016 int ret;
3018 mutex_lock(&ftrace_regex_lock);
3019 if (file->f_mode & FMODE_READ) {
3020 iter = m->private;
3022 seq_release(inode, file);
3023 } else
3024 iter = file->private_data;
3026 parser = &iter->parser;
3027 if (trace_parser_loaded(parser)) {
3028 parser->buffer[parser->idx] = 0;
3029 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
3032 trace_parser_put(parser);
3034 if (file->f_mode & FMODE_WRITE) {
3035 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3037 if (filter_hash)
3038 orig_hash = &iter->ops->filter_hash;
3039 else
3040 orig_hash = &iter->ops->notrace_hash;
3042 mutex_lock(&ftrace_lock);
3044 * Remove the current set, update the hash and add
3045 * them back.
3047 ftrace_hash_rec_disable(iter->ops, filter_hash);
3048 ret = ftrace_hash_move(orig_hash, iter->hash);
3049 if (!ret) {
3050 ftrace_hash_rec_enable(iter->ops, filter_hash);
3051 if (iter->ops->flags & FTRACE_OPS_FL_ENABLED
3052 && ftrace_enabled)
3053 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
3055 mutex_unlock(&ftrace_lock);
3057 free_ftrace_hash(iter->hash);
3058 kfree(iter);
3060 mutex_unlock(&ftrace_regex_lock);
3061 return 0;
3064 static const struct file_operations ftrace_avail_fops = {
3065 .open = ftrace_avail_open,
3066 .read = seq_read,
3067 .llseek = seq_lseek,
3068 .release = seq_release_private,
3071 static const struct file_operations ftrace_enabled_fops = {
3072 .open = ftrace_enabled_open,
3073 .read = seq_read,
3074 .llseek = seq_lseek,
3075 .release = seq_release_private,
3078 static const struct file_operations ftrace_filter_fops = {
3079 .open = ftrace_filter_open,
3080 .read = seq_read,
3081 .write = ftrace_filter_write,
3082 .llseek = ftrace_regex_lseek,
3083 .release = ftrace_regex_release,
3086 static const struct file_operations ftrace_notrace_fops = {
3087 .open = ftrace_notrace_open,
3088 .read = seq_read,
3089 .write = ftrace_notrace_write,
3090 .llseek = ftrace_regex_lseek,
3091 .release = ftrace_regex_release,
3094 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3096 static DEFINE_MUTEX(graph_lock);
3098 int ftrace_graph_count;
3099 int ftrace_graph_filter_enabled;
3100 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3102 static void *
3103 __g_next(struct seq_file *m, loff_t *pos)
3105 if (*pos >= ftrace_graph_count)
3106 return NULL;
3107 return &ftrace_graph_funcs[*pos];
3110 static void *
3111 g_next(struct seq_file *m, void *v, loff_t *pos)
3113 (*pos)++;
3114 return __g_next(m, pos);
3117 static void *g_start(struct seq_file *m, loff_t *pos)
3119 mutex_lock(&graph_lock);
3121 /* Nothing, tell g_show to print all functions are enabled */
3122 if (!ftrace_graph_filter_enabled && !*pos)
3123 return (void *)1;
3125 return __g_next(m, pos);
3128 static void g_stop(struct seq_file *m, void *p)
3130 mutex_unlock(&graph_lock);
3133 static int g_show(struct seq_file *m, void *v)
3135 unsigned long *ptr = v;
3137 if (!ptr)
3138 return 0;
3140 if (ptr == (unsigned long *)1) {
3141 seq_printf(m, "#### all functions enabled ####\n");
3142 return 0;
3145 seq_printf(m, "%ps\n", (void *)*ptr);
3147 return 0;
3150 static const struct seq_operations ftrace_graph_seq_ops = {
3151 .start = g_start,
3152 .next = g_next,
3153 .stop = g_stop,
3154 .show = g_show,
3157 static int
3158 ftrace_graph_open(struct inode *inode, struct file *file)
3160 int ret = 0;
3162 if (unlikely(ftrace_disabled))
3163 return -ENODEV;
3165 mutex_lock(&graph_lock);
3166 if ((file->f_mode & FMODE_WRITE) &&
3167 (file->f_flags & O_TRUNC)) {
3168 ftrace_graph_filter_enabled = 0;
3169 ftrace_graph_count = 0;
3170 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3172 mutex_unlock(&graph_lock);
3174 if (file->f_mode & FMODE_READ)
3175 ret = seq_open(file, &ftrace_graph_seq_ops);
3177 return ret;
3180 static int
3181 ftrace_graph_release(struct inode *inode, struct file *file)
3183 if (file->f_mode & FMODE_READ)
3184 seq_release(inode, file);
3185 return 0;
3188 static int
3189 ftrace_set_func(unsigned long *array, int *idx, char *buffer)
3191 struct dyn_ftrace *rec;
3192 struct ftrace_page *pg;
3193 int search_len;
3194 int fail = 1;
3195 int type, not;
3196 char *search;
3197 bool exists;
3198 int i;
3200 /* decode regex */
3201 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
3202 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3203 return -EBUSY;
3205 search_len = strlen(search);
3207 mutex_lock(&ftrace_lock);
3209 if (unlikely(ftrace_disabled)) {
3210 mutex_unlock(&ftrace_lock);
3211 return -ENODEV;
3214 do_for_each_ftrace_rec(pg, rec) {
3216 if (rec->flags & FTRACE_FL_FREE)
3217 continue;
3219 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
3220 /* if it is in the array */
3221 exists = false;
3222 for (i = 0; i < *idx; i++) {
3223 if (array[i] == rec->ip) {
3224 exists = true;
3225 break;
3229 if (!not) {
3230 fail = 0;
3231 if (!exists) {
3232 array[(*idx)++] = rec->ip;
3233 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3234 goto out;
3236 } else {
3237 if (exists) {
3238 array[i] = array[--(*idx)];
3239 array[*idx] = 0;
3240 fail = 0;
3244 } while_for_each_ftrace_rec();
3245 out:
3246 mutex_unlock(&ftrace_lock);
3248 if (fail)
3249 return -EINVAL;
3251 ftrace_graph_filter_enabled = 1;
3252 return 0;
3255 static ssize_t
3256 ftrace_graph_write(struct file *file, const char __user *ubuf,
3257 size_t cnt, loff_t *ppos)
3259 struct trace_parser parser;
3260 ssize_t read, ret;
3262 if (!cnt)
3263 return 0;
3265 mutex_lock(&graph_lock);
3267 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3268 ret = -ENOMEM;
3269 goto out_unlock;
3272 read = trace_get_user(&parser, ubuf, cnt, ppos);
3274 if (read >= 0 && trace_parser_loaded((&parser))) {
3275 parser.buffer[parser.idx] = 0;
3277 /* we allow only one expression at a time */
3278 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3279 parser.buffer);
3280 if (ret)
3281 goto out_free;
3284 ret = read;
3286 out_free:
3287 trace_parser_put(&parser);
3288 out_unlock:
3289 mutex_unlock(&graph_lock);
3291 return ret;
3294 static const struct file_operations ftrace_graph_fops = {
3295 .open = ftrace_graph_open,
3296 .read = seq_read,
3297 .write = ftrace_graph_write,
3298 .release = ftrace_graph_release,
3299 .llseek = seq_lseek,
3301 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3303 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
3306 trace_create_file("available_filter_functions", 0444,
3307 d_tracer, NULL, &ftrace_avail_fops);
3309 trace_create_file("enabled_functions", 0444,
3310 d_tracer, NULL, &ftrace_enabled_fops);
3312 trace_create_file("set_ftrace_filter", 0644, d_tracer,
3313 NULL, &ftrace_filter_fops);
3315 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
3316 NULL, &ftrace_notrace_fops);
3318 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3319 trace_create_file("set_graph_function", 0444, d_tracer,
3320 NULL,
3321 &ftrace_graph_fops);
3322 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3324 return 0;
3327 static int ftrace_process_locs(struct module *mod,
3328 unsigned long *start,
3329 unsigned long *end)
3331 unsigned long *p;
3332 unsigned long addr;
3334 mutex_lock(&ftrace_lock);
3335 p = start;
3336 while (p < end) {
3337 addr = ftrace_call_adjust(*p++);
3339 * Some architecture linkers will pad between
3340 * the different mcount_loc sections of different
3341 * object files to satisfy alignments.
3342 * Skip any NULL pointers.
3344 if (!addr)
3345 continue;
3346 ftrace_record_ip(addr);
3349 ftrace_update_code(mod);
3350 mutex_unlock(&ftrace_lock);
3352 return 0;
3355 #ifdef CONFIG_MODULES
3356 void ftrace_release_mod(struct module *mod)
3358 struct dyn_ftrace *rec;
3359 struct ftrace_page *pg;
3361 mutex_lock(&ftrace_lock);
3363 if (ftrace_disabled)
3364 goto out_unlock;
3366 do_for_each_ftrace_rec(pg, rec) {
3367 if (within_module_core(rec->ip, mod)) {
3369 * rec->ip is changed in ftrace_free_rec()
3370 * It should not between s and e if record was freed.
3372 FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
3373 ftrace_free_rec(rec);
3375 } while_for_each_ftrace_rec();
3376 out_unlock:
3377 mutex_unlock(&ftrace_lock);
3380 static void ftrace_init_module(struct module *mod,
3381 unsigned long *start, unsigned long *end)
3383 if (ftrace_disabled || start == end)
3384 return;
3385 ftrace_process_locs(mod, start, end);
3388 static int ftrace_module_notify(struct notifier_block *self,
3389 unsigned long val, void *data)
3391 struct module *mod = data;
3393 switch (val) {
3394 case MODULE_STATE_COMING:
3395 ftrace_init_module(mod, mod->ftrace_callsites,
3396 mod->ftrace_callsites +
3397 mod->num_ftrace_callsites);
3398 break;
3399 case MODULE_STATE_GOING:
3400 ftrace_release_mod(mod);
3401 break;
3404 return 0;
3406 #else
3407 static int ftrace_module_notify(struct notifier_block *self,
3408 unsigned long val, void *data)
3410 return 0;
3412 #endif /* CONFIG_MODULES */
3414 struct notifier_block ftrace_module_nb = {
3415 .notifier_call = ftrace_module_notify,
3416 .priority = 0,
3419 extern unsigned long __start_mcount_loc[];
3420 extern unsigned long __stop_mcount_loc[];
3422 void __init ftrace_init(void)
3424 unsigned long count, addr, flags;
3425 int ret;
3427 /* Keep the ftrace pointer to the stub */
3428 addr = (unsigned long)ftrace_stub;
3430 local_irq_save(flags);
3431 ftrace_dyn_arch_init(&addr);
3432 local_irq_restore(flags);
3434 /* ftrace_dyn_arch_init places the return code in addr */
3435 if (addr)
3436 goto failed;
3438 count = __stop_mcount_loc - __start_mcount_loc;
3440 ret = ftrace_dyn_table_alloc(count);
3441 if (ret)
3442 goto failed;
3444 last_ftrace_enabled = ftrace_enabled = 1;
3446 ret = ftrace_process_locs(NULL,
3447 __start_mcount_loc,
3448 __stop_mcount_loc);
3450 ret = register_module_notifier(&ftrace_module_nb);
3451 if (ret)
3452 pr_warning("Failed to register trace ftrace module notifier\n");
3454 set_ftrace_early_filters();
3456 return;
3457 failed:
3458 ftrace_disabled = 1;
3461 #else
3463 static struct ftrace_ops global_ops = {
3464 .func = ftrace_stub,
3467 static int __init ftrace_nodyn_init(void)
3469 ftrace_enabled = 1;
3470 return 0;
3472 device_initcall(ftrace_nodyn_init);
3474 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
3475 static inline void ftrace_startup_enable(int command) { }
3476 /* Keep as macros so we do not need to define the commands */
3477 # define ftrace_startup(ops, command) \
3478 ({ \
3479 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
3480 0; \
3482 # define ftrace_shutdown(ops, command) do { } while (0)
3483 # define ftrace_startup_sysctl() do { } while (0)
3484 # define ftrace_shutdown_sysctl() do { } while (0)
3486 static inline int
3487 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
3489 return 1;
3492 #endif /* CONFIG_DYNAMIC_FTRACE */
3494 static void
3495 ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip)
3497 struct ftrace_ops *op;
3499 if (unlikely(trace_recursion_test(TRACE_INTERNAL_BIT)))
3500 return;
3502 trace_recursion_set(TRACE_INTERNAL_BIT);
3504 * Some of the ops may be dynamically allocated,
3505 * they must be freed after a synchronize_sched().
3507 preempt_disable_notrace();
3508 op = rcu_dereference_raw(ftrace_ops_list);
3509 while (op != &ftrace_list_end) {
3510 if (ftrace_ops_test(op, ip))
3511 op->func(ip, parent_ip);
3512 op = rcu_dereference_raw(op->next);
3514 preempt_enable_notrace();
3515 trace_recursion_clear(TRACE_INTERNAL_BIT);
3518 static void clear_ftrace_swapper(void)
3520 struct task_struct *p;
3521 int cpu;
3523 get_online_cpus();
3524 for_each_online_cpu(cpu) {
3525 p = idle_task(cpu);
3526 clear_tsk_trace_trace(p);
3528 put_online_cpus();
3531 static void set_ftrace_swapper(void)
3533 struct task_struct *p;
3534 int cpu;
3536 get_online_cpus();
3537 for_each_online_cpu(cpu) {
3538 p = idle_task(cpu);
3539 set_tsk_trace_trace(p);
3541 put_online_cpus();
3544 static void clear_ftrace_pid(struct pid *pid)
3546 struct task_struct *p;
3548 rcu_read_lock();
3549 do_each_pid_task(pid, PIDTYPE_PID, p) {
3550 clear_tsk_trace_trace(p);
3551 } while_each_pid_task(pid, PIDTYPE_PID, p);
3552 rcu_read_unlock();
3554 put_pid(pid);
3557 static void set_ftrace_pid(struct pid *pid)
3559 struct task_struct *p;
3561 rcu_read_lock();
3562 do_each_pid_task(pid, PIDTYPE_PID, p) {
3563 set_tsk_trace_trace(p);
3564 } while_each_pid_task(pid, PIDTYPE_PID, p);
3565 rcu_read_unlock();
3568 static void clear_ftrace_pid_task(struct pid *pid)
3570 if (pid == ftrace_swapper_pid)
3571 clear_ftrace_swapper();
3572 else
3573 clear_ftrace_pid(pid);
3576 static void set_ftrace_pid_task(struct pid *pid)
3578 if (pid == ftrace_swapper_pid)
3579 set_ftrace_swapper();
3580 else
3581 set_ftrace_pid(pid);
3584 static int ftrace_pid_add(int p)
3586 struct pid *pid;
3587 struct ftrace_pid *fpid;
3588 int ret = -EINVAL;
3590 mutex_lock(&ftrace_lock);
3592 if (!p)
3593 pid = ftrace_swapper_pid;
3594 else
3595 pid = find_get_pid(p);
3597 if (!pid)
3598 goto out;
3600 ret = 0;
3602 list_for_each_entry(fpid, &ftrace_pids, list)
3603 if (fpid->pid == pid)
3604 goto out_put;
3606 ret = -ENOMEM;
3608 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
3609 if (!fpid)
3610 goto out_put;
3612 list_add(&fpid->list, &ftrace_pids);
3613 fpid->pid = pid;
3615 set_ftrace_pid_task(pid);
3617 ftrace_update_pid_func();
3618 ftrace_startup_enable(0);
3620 mutex_unlock(&ftrace_lock);
3621 return 0;
3623 out_put:
3624 if (pid != ftrace_swapper_pid)
3625 put_pid(pid);
3627 out:
3628 mutex_unlock(&ftrace_lock);
3629 return ret;
3632 static void ftrace_pid_reset(void)
3634 struct ftrace_pid *fpid, *safe;
3636 mutex_lock(&ftrace_lock);
3637 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
3638 struct pid *pid = fpid->pid;
3640 clear_ftrace_pid_task(pid);
3642 list_del(&fpid->list);
3643 kfree(fpid);
3646 ftrace_update_pid_func();
3647 ftrace_startup_enable(0);
3649 mutex_unlock(&ftrace_lock);
3652 static void *fpid_start(struct seq_file *m, loff_t *pos)
3654 mutex_lock(&ftrace_lock);
3656 if (list_empty(&ftrace_pids) && (!*pos))
3657 return (void *) 1;
3659 return seq_list_start(&ftrace_pids, *pos);
3662 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
3664 if (v == (void *)1)
3665 return NULL;
3667 return seq_list_next(v, &ftrace_pids, pos);
3670 static void fpid_stop(struct seq_file *m, void *p)
3672 mutex_unlock(&ftrace_lock);
3675 static int fpid_show(struct seq_file *m, void *v)
3677 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
3679 if (v == (void *)1) {
3680 seq_printf(m, "no pid\n");
3681 return 0;
3684 if (fpid->pid == ftrace_swapper_pid)
3685 seq_printf(m, "swapper tasks\n");
3686 else
3687 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
3689 return 0;
3692 static const struct seq_operations ftrace_pid_sops = {
3693 .start = fpid_start,
3694 .next = fpid_next,
3695 .stop = fpid_stop,
3696 .show = fpid_show,
3699 static int
3700 ftrace_pid_open(struct inode *inode, struct file *file)
3702 int ret = 0;
3704 if ((file->f_mode & FMODE_WRITE) &&
3705 (file->f_flags & O_TRUNC))
3706 ftrace_pid_reset();
3708 if (file->f_mode & FMODE_READ)
3709 ret = seq_open(file, &ftrace_pid_sops);
3711 return ret;
3714 static ssize_t
3715 ftrace_pid_write(struct file *filp, const char __user *ubuf,
3716 size_t cnt, loff_t *ppos)
3718 char buf[64], *tmp;
3719 long val;
3720 int ret;
3722 if (cnt >= sizeof(buf))
3723 return -EINVAL;
3725 if (copy_from_user(&buf, ubuf, cnt))
3726 return -EFAULT;
3728 buf[cnt] = 0;
3731 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
3732 * to clean the filter quietly.
3734 tmp = strstrip(buf);
3735 if (strlen(tmp) == 0)
3736 return 1;
3738 ret = strict_strtol(tmp, 10, &val);
3739 if (ret < 0)
3740 return ret;
3742 ret = ftrace_pid_add(val);
3744 return ret ? ret : cnt;
3747 static int
3748 ftrace_pid_release(struct inode *inode, struct file *file)
3750 if (file->f_mode & FMODE_READ)
3751 seq_release(inode, file);
3753 return 0;
3756 static const struct file_operations ftrace_pid_fops = {
3757 .open = ftrace_pid_open,
3758 .write = ftrace_pid_write,
3759 .read = seq_read,
3760 .llseek = seq_lseek,
3761 .release = ftrace_pid_release,
3764 static __init int ftrace_init_debugfs(void)
3766 struct dentry *d_tracer;
3768 d_tracer = tracing_init_dentry();
3769 if (!d_tracer)
3770 return 0;
3772 ftrace_init_dyn_debugfs(d_tracer);
3774 trace_create_file("set_ftrace_pid", 0644, d_tracer,
3775 NULL, &ftrace_pid_fops);
3777 ftrace_profile_debugfs(d_tracer);
3779 return 0;
3781 fs_initcall(ftrace_init_debugfs);
3784 * ftrace_kill - kill ftrace
3786 * This function should be used by panic code. It stops ftrace
3787 * but in a not so nice way. If you need to simply kill ftrace
3788 * from a non-atomic section, use ftrace_kill.
3790 void ftrace_kill(void)
3792 ftrace_disabled = 1;
3793 ftrace_enabled = 0;
3794 clear_ftrace_function();
3798 * register_ftrace_function - register a function for profiling
3799 * @ops - ops structure that holds the function for profiling.
3801 * Register a function to be called by all functions in the
3802 * kernel.
3804 * Note: @ops->func and all the functions it calls must be labeled
3805 * with "notrace", otherwise it will go into a
3806 * recursive loop.
3808 int register_ftrace_function(struct ftrace_ops *ops)
3810 int ret = -1;
3812 mutex_lock(&ftrace_lock);
3814 if (unlikely(ftrace_disabled))
3815 goto out_unlock;
3817 ret = __register_ftrace_function(ops);
3818 if (!ret)
3819 ret = ftrace_startup(ops, 0);
3822 out_unlock:
3823 mutex_unlock(&ftrace_lock);
3824 return ret;
3826 EXPORT_SYMBOL_GPL(register_ftrace_function);
3829 * unregister_ftrace_function - unregister a function for profiling.
3830 * @ops - ops structure that holds the function to unregister
3832 * Unregister a function that was added to be called by ftrace profiling.
3834 int unregister_ftrace_function(struct ftrace_ops *ops)
3836 int ret;
3838 mutex_lock(&ftrace_lock);
3839 ret = __unregister_ftrace_function(ops);
3840 if (!ret)
3841 ftrace_shutdown(ops, 0);
3842 mutex_unlock(&ftrace_lock);
3844 return ret;
3846 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
3849 ftrace_enable_sysctl(struct ctl_table *table, int write,
3850 void __user *buffer, size_t *lenp,
3851 loff_t *ppos)
3853 int ret = -ENODEV;
3855 mutex_lock(&ftrace_lock);
3857 if (unlikely(ftrace_disabled))
3858 goto out;
3860 ret = proc_dointvec(table, write, buffer, lenp, ppos);
3862 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
3863 goto out;
3865 last_ftrace_enabled = !!ftrace_enabled;
3867 if (ftrace_enabled) {
3869 ftrace_startup_sysctl();
3871 /* we are starting ftrace again */
3872 if (ftrace_ops_list != &ftrace_list_end) {
3873 if (ftrace_ops_list->next == &ftrace_list_end)
3874 ftrace_trace_function = ftrace_ops_list->func;
3875 else
3876 ftrace_trace_function = ftrace_ops_list_func;
3879 } else {
3880 /* stopping ftrace calls (just send to ftrace_stub) */
3881 ftrace_trace_function = ftrace_stub;
3883 ftrace_shutdown_sysctl();
3886 out:
3887 mutex_unlock(&ftrace_lock);
3888 return ret;
3891 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3893 static int ftrace_graph_active;
3894 static struct notifier_block ftrace_suspend_notifier;
3896 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3898 return 0;
3901 /* The callbacks that hook a function */
3902 trace_func_graph_ret_t ftrace_graph_return =
3903 (trace_func_graph_ret_t)ftrace_stub;
3904 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
3906 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3907 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3909 int i;
3910 int ret = 0;
3911 unsigned long flags;
3912 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3913 struct task_struct *g, *t;
3915 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3916 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3917 * sizeof(struct ftrace_ret_stack),
3918 GFP_KERNEL);
3919 if (!ret_stack_list[i]) {
3920 start = 0;
3921 end = i;
3922 ret = -ENOMEM;
3923 goto free;
3927 read_lock_irqsave(&tasklist_lock, flags);
3928 do_each_thread(g, t) {
3929 if (start == end) {
3930 ret = -EAGAIN;
3931 goto unlock;
3934 if (t->ret_stack == NULL) {
3935 atomic_set(&t->tracing_graph_pause, 0);
3936 atomic_set(&t->trace_overrun, 0);
3937 t->curr_ret_stack = -1;
3938 /* Make sure the tasks see the -1 first: */
3939 smp_wmb();
3940 t->ret_stack = ret_stack_list[start++];
3942 } while_each_thread(g, t);
3944 unlock:
3945 read_unlock_irqrestore(&tasklist_lock, flags);
3946 free:
3947 for (i = start; i < end; i++)
3948 kfree(ret_stack_list[i]);
3949 return ret;
3952 static void
3953 ftrace_graph_probe_sched_switch(void *ignore,
3954 struct task_struct *prev, struct task_struct *next)
3956 unsigned long long timestamp;
3957 int index;
3960 * Does the user want to count the time a function was asleep.
3961 * If so, do not update the time stamps.
3963 if (trace_flags & TRACE_ITER_SLEEP_TIME)
3964 return;
3966 timestamp = trace_clock_local();
3968 prev->ftrace_timestamp = timestamp;
3970 /* only process tasks that we timestamped */
3971 if (!next->ftrace_timestamp)
3972 return;
3975 * Update all the counters in next to make up for the
3976 * time next was sleeping.
3978 timestamp -= next->ftrace_timestamp;
3980 for (index = next->curr_ret_stack; index >= 0; index--)
3981 next->ret_stack[index].calltime += timestamp;
3984 /* Allocate a return stack for each task */
3985 static int start_graph_tracing(void)
3987 struct ftrace_ret_stack **ret_stack_list;
3988 int ret, cpu;
3990 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
3991 sizeof(struct ftrace_ret_stack *),
3992 GFP_KERNEL);
3994 if (!ret_stack_list)
3995 return -ENOMEM;
3997 /* The cpu_boot init_task->ret_stack will never be freed */
3998 for_each_online_cpu(cpu) {
3999 if (!idle_task(cpu)->ret_stack)
4000 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
4003 do {
4004 ret = alloc_retstack_tasklist(ret_stack_list);
4005 } while (ret == -EAGAIN);
4007 if (!ret) {
4008 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
4009 if (ret)
4010 pr_info("ftrace_graph: Couldn't activate tracepoint"
4011 " probe to kernel_sched_switch\n");
4014 kfree(ret_stack_list);
4015 return ret;
4019 * Hibernation protection.
4020 * The state of the current task is too much unstable during
4021 * suspend/restore to disk. We want to protect against that.
4023 static int
4024 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4025 void *unused)
4027 switch (state) {
4028 case PM_HIBERNATION_PREPARE:
4029 pause_graph_tracing();
4030 break;
4032 case PM_POST_HIBERNATION:
4033 unpause_graph_tracing();
4034 break;
4036 return NOTIFY_DONE;
4039 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4040 trace_func_graph_ent_t entryfunc)
4042 int ret = 0;
4044 mutex_lock(&ftrace_lock);
4046 /* we currently allow only one tracer registered at a time */
4047 if (ftrace_graph_active) {
4048 ret = -EBUSY;
4049 goto out;
4052 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
4053 register_pm_notifier(&ftrace_suspend_notifier);
4055 ftrace_graph_active++;
4056 ret = start_graph_tracing();
4057 if (ret) {
4058 ftrace_graph_active--;
4059 goto out;
4062 ftrace_graph_return = retfunc;
4063 ftrace_graph_entry = entryfunc;
4065 ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
4067 out:
4068 mutex_unlock(&ftrace_lock);
4069 return ret;
4072 void unregister_ftrace_graph(void)
4074 mutex_lock(&ftrace_lock);
4076 if (unlikely(!ftrace_graph_active))
4077 goto out;
4079 ftrace_graph_active--;
4080 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
4081 ftrace_graph_entry = ftrace_graph_entry_stub;
4082 ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
4083 unregister_pm_notifier(&ftrace_suspend_notifier);
4084 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
4086 out:
4087 mutex_unlock(&ftrace_lock);
4090 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
4092 static void
4093 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
4095 atomic_set(&t->tracing_graph_pause, 0);
4096 atomic_set(&t->trace_overrun, 0);
4097 t->ftrace_timestamp = 0;
4098 /* make curr_ret_stack visible before we add the ret_stack */
4099 smp_wmb();
4100 t->ret_stack = ret_stack;
4104 * Allocate a return stack for the idle task. May be the first
4105 * time through, or it may be done by CPU hotplug online.
4107 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
4109 t->curr_ret_stack = -1;
4111 * The idle task has no parent, it either has its own
4112 * stack or no stack at all.
4114 if (t->ret_stack)
4115 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
4117 if (ftrace_graph_active) {
4118 struct ftrace_ret_stack *ret_stack;
4120 ret_stack = per_cpu(idle_ret_stack, cpu);
4121 if (!ret_stack) {
4122 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4123 * sizeof(struct ftrace_ret_stack),
4124 GFP_KERNEL);
4125 if (!ret_stack)
4126 return;
4127 per_cpu(idle_ret_stack, cpu) = ret_stack;
4129 graph_init_task(t, ret_stack);
4133 /* Allocate a return stack for newly created task */
4134 void ftrace_graph_init_task(struct task_struct *t)
4136 /* Make sure we do not use the parent ret_stack */
4137 t->ret_stack = NULL;
4138 t->curr_ret_stack = -1;
4140 if (ftrace_graph_active) {
4141 struct ftrace_ret_stack *ret_stack;
4143 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4144 * sizeof(struct ftrace_ret_stack),
4145 GFP_KERNEL);
4146 if (!ret_stack)
4147 return;
4148 graph_init_task(t, ret_stack);
4152 void ftrace_graph_exit_task(struct task_struct *t)
4154 struct ftrace_ret_stack *ret_stack = t->ret_stack;
4156 t->ret_stack = NULL;
4157 /* NULL must become visible to IRQs before we free it: */
4158 barrier();
4160 kfree(ret_stack);
4163 void ftrace_graph_stop(void)
4165 ftrace_stop();
4167 #endif