ARM: 7409/1: Do not call flush_cache_user_range with mmap_sem held
[linux/fpc-iii.git] / kernel / trace / ftrace.c
blob9f8e2e11020a7f34aa4e93e69daea83121a7963f
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_UPDATE_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 void
1186 ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1187 static void
1188 ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1190 static int
1191 ftrace_hash_move(struct ftrace_ops *ops, int enable,
1192 struct ftrace_hash **dst, struct ftrace_hash *src)
1194 struct ftrace_func_entry *entry;
1195 struct hlist_node *tp, *tn;
1196 struct hlist_head *hhd;
1197 struct ftrace_hash *old_hash;
1198 struct ftrace_hash *new_hash;
1199 unsigned long key;
1200 int size = src->count;
1201 int bits = 0;
1202 int ret;
1203 int i;
1206 * Remove the current set, update the hash and add
1207 * them back.
1209 ftrace_hash_rec_disable(ops, enable);
1212 * If the new source is empty, just free dst and assign it
1213 * the empty_hash.
1215 if (!src->count) {
1216 free_ftrace_hash_rcu(*dst);
1217 rcu_assign_pointer(*dst, EMPTY_HASH);
1218 return 0;
1222 * Make the hash size about 1/2 the # found
1224 for (size /= 2; size; size >>= 1)
1225 bits++;
1227 /* Don't allocate too much */
1228 if (bits > FTRACE_HASH_MAX_BITS)
1229 bits = FTRACE_HASH_MAX_BITS;
1231 ret = -ENOMEM;
1232 new_hash = alloc_ftrace_hash(bits);
1233 if (!new_hash)
1234 goto out;
1236 size = 1 << src->size_bits;
1237 for (i = 0; i < size; i++) {
1238 hhd = &src->buckets[i];
1239 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist) {
1240 if (bits > 0)
1241 key = hash_long(entry->ip, bits);
1242 else
1243 key = 0;
1244 remove_hash_entry(src, entry);
1245 __add_hash_entry(new_hash, entry);
1249 old_hash = *dst;
1250 rcu_assign_pointer(*dst, new_hash);
1251 free_ftrace_hash_rcu(old_hash);
1253 ret = 0;
1254 out:
1256 * Enable regardless of ret:
1257 * On success, we enable the new hash.
1258 * On failure, we re-enable the original hash.
1260 ftrace_hash_rec_enable(ops, enable);
1262 return ret;
1266 * Test the hashes for this ops to see if we want to call
1267 * the ops->func or not.
1269 * It's a match if the ip is in the ops->filter_hash or
1270 * the filter_hash does not exist or is empty,
1271 * AND
1272 * the ip is not in the ops->notrace_hash.
1274 * This needs to be called with preemption disabled as
1275 * the hashes are freed with call_rcu_sched().
1277 static int
1278 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
1280 struct ftrace_hash *filter_hash;
1281 struct ftrace_hash *notrace_hash;
1282 int ret;
1284 filter_hash = rcu_dereference_raw(ops->filter_hash);
1285 notrace_hash = rcu_dereference_raw(ops->notrace_hash);
1287 if ((!filter_hash || !filter_hash->count ||
1288 ftrace_lookup_ip(filter_hash, ip)) &&
1289 (!notrace_hash || !notrace_hash->count ||
1290 !ftrace_lookup_ip(notrace_hash, ip)))
1291 ret = 1;
1292 else
1293 ret = 0;
1295 return ret;
1299 * This is a double for. Do not use 'break' to break out of the loop,
1300 * you must use a goto.
1302 #define do_for_each_ftrace_rec(pg, rec) \
1303 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1304 int _____i; \
1305 for (_____i = 0; _____i < pg->index; _____i++) { \
1306 rec = &pg->records[_____i];
1308 #define while_for_each_ftrace_rec() \
1312 static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1313 int filter_hash,
1314 bool inc)
1316 struct ftrace_hash *hash;
1317 struct ftrace_hash *other_hash;
1318 struct ftrace_page *pg;
1319 struct dyn_ftrace *rec;
1320 int count = 0;
1321 int all = 0;
1323 /* Only update if the ops has been registered */
1324 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1325 return;
1328 * In the filter_hash case:
1329 * If the count is zero, we update all records.
1330 * Otherwise we just update the items in the hash.
1332 * In the notrace_hash case:
1333 * We enable the update in the hash.
1334 * As disabling notrace means enabling the tracing,
1335 * and enabling notrace means disabling, the inc variable
1336 * gets inversed.
1338 if (filter_hash) {
1339 hash = ops->filter_hash;
1340 other_hash = ops->notrace_hash;
1341 if (!hash || !hash->count)
1342 all = 1;
1343 } else {
1344 inc = !inc;
1345 hash = ops->notrace_hash;
1346 other_hash = ops->filter_hash;
1348 * If the notrace hash has no items,
1349 * then there's nothing to do.
1351 if (hash && !hash->count)
1352 return;
1355 do_for_each_ftrace_rec(pg, rec) {
1356 int in_other_hash = 0;
1357 int in_hash = 0;
1358 int match = 0;
1360 if (all) {
1362 * Only the filter_hash affects all records.
1363 * Update if the record is not in the notrace hash.
1365 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1366 match = 1;
1367 } else {
1368 in_hash = hash && !!ftrace_lookup_ip(hash, rec->ip);
1369 in_other_hash = other_hash && !!ftrace_lookup_ip(other_hash, rec->ip);
1374 if (filter_hash && in_hash && !in_other_hash)
1375 match = 1;
1376 else if (!filter_hash && in_hash &&
1377 (in_other_hash || !other_hash->count))
1378 match = 1;
1380 if (!match)
1381 continue;
1383 if (inc) {
1384 rec->flags++;
1385 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1386 return;
1387 } else {
1388 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1389 return;
1390 rec->flags--;
1392 count++;
1393 /* Shortcut, if we handled all records, we are done. */
1394 if (!all && count == hash->count)
1395 return;
1396 } while_for_each_ftrace_rec();
1399 static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1400 int filter_hash)
1402 __ftrace_hash_rec_update(ops, filter_hash, 0);
1405 static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1406 int filter_hash)
1408 __ftrace_hash_rec_update(ops, filter_hash, 1);
1411 static void ftrace_free_rec(struct dyn_ftrace *rec)
1413 rec->freelist = ftrace_free_records;
1414 ftrace_free_records = rec;
1415 rec->flags |= FTRACE_FL_FREE;
1418 static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
1420 struct dyn_ftrace *rec;
1422 /* First check for freed records */
1423 if (ftrace_free_records) {
1424 rec = ftrace_free_records;
1426 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
1427 FTRACE_WARN_ON_ONCE(1);
1428 ftrace_free_records = NULL;
1429 return NULL;
1432 ftrace_free_records = rec->freelist;
1433 memset(rec, 0, sizeof(*rec));
1434 return rec;
1437 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
1438 if (!ftrace_pages->next) {
1439 /* allocate another page */
1440 ftrace_pages->next =
1441 (void *)get_zeroed_page(GFP_KERNEL);
1442 if (!ftrace_pages->next)
1443 return NULL;
1445 ftrace_pages = ftrace_pages->next;
1448 return &ftrace_pages->records[ftrace_pages->index++];
1451 static struct dyn_ftrace *
1452 ftrace_record_ip(unsigned long ip)
1454 struct dyn_ftrace *rec;
1456 if (ftrace_disabled)
1457 return NULL;
1459 rec = ftrace_alloc_dyn_node(ip);
1460 if (!rec)
1461 return NULL;
1463 rec->ip = ip;
1464 rec->newlist = ftrace_new_addrs;
1465 ftrace_new_addrs = rec;
1467 return rec;
1470 static void print_ip_ins(const char *fmt, unsigned char *p)
1472 int i;
1474 printk(KERN_CONT "%s", fmt);
1476 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1477 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1480 static void ftrace_bug(int failed, unsigned long ip)
1482 switch (failed) {
1483 case -EFAULT:
1484 FTRACE_WARN_ON_ONCE(1);
1485 pr_info("ftrace faulted on modifying ");
1486 print_ip_sym(ip);
1487 break;
1488 case -EINVAL:
1489 FTRACE_WARN_ON_ONCE(1);
1490 pr_info("ftrace failed to modify ");
1491 print_ip_sym(ip);
1492 print_ip_ins(" actual: ", (unsigned char *)ip);
1493 printk(KERN_CONT "\n");
1494 break;
1495 case -EPERM:
1496 FTRACE_WARN_ON_ONCE(1);
1497 pr_info("ftrace faulted on writing ");
1498 print_ip_sym(ip);
1499 break;
1500 default:
1501 FTRACE_WARN_ON_ONCE(1);
1502 pr_info("ftrace faulted on unknown error ");
1503 print_ip_sym(ip);
1508 /* Return 1 if the address range is reserved for ftrace */
1509 int ftrace_text_reserved(void *start, void *end)
1511 struct dyn_ftrace *rec;
1512 struct ftrace_page *pg;
1514 do_for_each_ftrace_rec(pg, rec) {
1515 if (rec->ip <= (unsigned long)end &&
1516 rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
1517 return 1;
1518 } while_for_each_ftrace_rec();
1519 return 0;
1523 static int
1524 __ftrace_replace_code(struct dyn_ftrace *rec, int update)
1526 unsigned long ftrace_addr;
1527 unsigned long flag = 0UL;
1529 ftrace_addr = (unsigned long)FTRACE_ADDR;
1532 * If we are updating calls:
1534 * If the record has a ref count, then we need to enable it
1535 * because someone is using it.
1537 * Otherwise we make sure its disabled.
1539 * If we are disabling calls, then disable all records that
1540 * are enabled.
1542 if (update && (rec->flags & ~FTRACE_FL_MASK))
1543 flag = FTRACE_FL_ENABLED;
1545 /* If the state of this record hasn't changed, then do nothing */
1546 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1547 return 0;
1549 if (flag) {
1550 rec->flags |= FTRACE_FL_ENABLED;
1551 return ftrace_make_call(rec, ftrace_addr);
1554 rec->flags &= ~FTRACE_FL_ENABLED;
1555 return ftrace_make_nop(NULL, rec, ftrace_addr);
1558 static void ftrace_replace_code(int update)
1560 struct dyn_ftrace *rec;
1561 struct ftrace_page *pg;
1562 int failed;
1564 if (unlikely(ftrace_disabled))
1565 return;
1567 do_for_each_ftrace_rec(pg, rec) {
1568 /* Skip over free records */
1569 if (rec->flags & FTRACE_FL_FREE)
1570 continue;
1572 failed = __ftrace_replace_code(rec, update);
1573 if (failed) {
1574 ftrace_bug(failed, rec->ip);
1575 /* Stop processing */
1576 return;
1578 } while_for_each_ftrace_rec();
1581 static int
1582 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
1584 unsigned long ip;
1585 int ret;
1587 ip = rec->ip;
1589 if (unlikely(ftrace_disabled))
1590 return 0;
1592 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
1593 if (ret) {
1594 ftrace_bug(ret, ip);
1595 return 0;
1597 return 1;
1601 * archs can override this function if they must do something
1602 * before the modifying code is performed.
1604 int __weak ftrace_arch_code_modify_prepare(void)
1606 return 0;
1610 * archs can override this function if they must do something
1611 * after the modifying code is performed.
1613 int __weak ftrace_arch_code_modify_post_process(void)
1615 return 0;
1618 static int __ftrace_modify_code(void *data)
1620 int *command = data;
1622 if (*command & FTRACE_UPDATE_CALLS)
1623 ftrace_replace_code(1);
1624 else if (*command & FTRACE_DISABLE_CALLS)
1625 ftrace_replace_code(0);
1627 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1628 ftrace_update_ftrace_func(ftrace_trace_function);
1630 if (*command & FTRACE_START_FUNC_RET)
1631 ftrace_enable_ftrace_graph_caller();
1632 else if (*command & FTRACE_STOP_FUNC_RET)
1633 ftrace_disable_ftrace_graph_caller();
1635 return 0;
1638 static void ftrace_run_update_code(int command)
1640 int ret;
1642 ret = ftrace_arch_code_modify_prepare();
1643 FTRACE_WARN_ON(ret);
1644 if (ret)
1645 return;
1647 stop_machine(__ftrace_modify_code, &command, NULL);
1649 ret = ftrace_arch_code_modify_post_process();
1650 FTRACE_WARN_ON(ret);
1653 static ftrace_func_t saved_ftrace_func;
1654 static int ftrace_start_up;
1655 static int global_start_up;
1657 static void ftrace_startup_enable(int command)
1659 if (saved_ftrace_func != ftrace_trace_function) {
1660 saved_ftrace_func = ftrace_trace_function;
1661 command |= FTRACE_UPDATE_TRACE_FUNC;
1664 if (!command || !ftrace_enabled)
1665 return;
1667 ftrace_run_update_code(command);
1670 static int ftrace_startup(struct ftrace_ops *ops, int command)
1672 bool hash_enable = true;
1674 if (unlikely(ftrace_disabled))
1675 return -ENODEV;
1677 ftrace_start_up++;
1678 command |= FTRACE_UPDATE_CALLS;
1680 /* ops marked global share the filter hashes */
1681 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
1682 ops = &global_ops;
1683 /* Don't update hash if global is already set */
1684 if (global_start_up)
1685 hash_enable = false;
1686 global_start_up++;
1689 ops->flags |= FTRACE_OPS_FL_ENABLED;
1690 if (hash_enable)
1691 ftrace_hash_rec_enable(ops, 1);
1693 ftrace_startup_enable(command);
1695 return 0;
1698 static void ftrace_shutdown(struct ftrace_ops *ops, int command)
1700 bool hash_disable = true;
1702 if (unlikely(ftrace_disabled))
1703 return;
1705 ftrace_start_up--;
1707 * Just warn in case of unbalance, no need to kill ftrace, it's not
1708 * critical but the ftrace_call callers may be never nopped again after
1709 * further ftrace uses.
1711 WARN_ON_ONCE(ftrace_start_up < 0);
1713 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
1714 ops = &global_ops;
1715 global_start_up--;
1716 WARN_ON_ONCE(global_start_up < 0);
1717 /* Don't update hash if global still has users */
1718 if (global_start_up) {
1719 WARN_ON_ONCE(!ftrace_start_up);
1720 hash_disable = false;
1724 if (hash_disable)
1725 ftrace_hash_rec_disable(ops, 1);
1727 if (ops != &global_ops || !global_start_up)
1728 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
1730 command |= FTRACE_UPDATE_CALLS;
1732 if (saved_ftrace_func != ftrace_trace_function) {
1733 saved_ftrace_func = ftrace_trace_function;
1734 command |= FTRACE_UPDATE_TRACE_FUNC;
1737 if (!command || !ftrace_enabled)
1738 return;
1740 ftrace_run_update_code(command);
1743 static void ftrace_startup_sysctl(void)
1745 if (unlikely(ftrace_disabled))
1746 return;
1748 /* Force update next time */
1749 saved_ftrace_func = NULL;
1750 /* ftrace_start_up is true if we want ftrace running */
1751 if (ftrace_start_up)
1752 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
1755 static void ftrace_shutdown_sysctl(void)
1757 if (unlikely(ftrace_disabled))
1758 return;
1760 /* ftrace_start_up is true if ftrace is running */
1761 if (ftrace_start_up)
1762 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
1765 static cycle_t ftrace_update_time;
1766 static unsigned long ftrace_update_cnt;
1767 unsigned long ftrace_update_tot_cnt;
1769 static int ops_traces_mod(struct ftrace_ops *ops)
1771 struct ftrace_hash *hash;
1773 hash = ops->filter_hash;
1774 return !!(!hash || !hash->count);
1777 static int ftrace_update_code(struct module *mod)
1779 struct dyn_ftrace *p;
1780 cycle_t start, stop;
1781 unsigned long ref = 0;
1784 * When adding a module, we need to check if tracers are
1785 * currently enabled and if they are set to trace all functions.
1786 * If they are, we need to enable the module functions as well
1787 * as update the reference counts for those function records.
1789 if (mod) {
1790 struct ftrace_ops *ops;
1792 for (ops = ftrace_ops_list;
1793 ops != &ftrace_list_end; ops = ops->next) {
1794 if (ops->flags & FTRACE_OPS_FL_ENABLED &&
1795 ops_traces_mod(ops))
1796 ref++;
1800 start = ftrace_now(raw_smp_processor_id());
1801 ftrace_update_cnt = 0;
1803 while (ftrace_new_addrs) {
1805 /* If something went wrong, bail without enabling anything */
1806 if (unlikely(ftrace_disabled))
1807 return -1;
1809 p = ftrace_new_addrs;
1810 ftrace_new_addrs = p->newlist;
1811 p->flags = ref;
1814 * Do the initial record conversion from mcount jump
1815 * to the NOP instructions.
1817 if (!ftrace_code_disable(mod, p)) {
1818 ftrace_free_rec(p);
1819 /* Game over */
1820 break;
1823 ftrace_update_cnt++;
1826 * If the tracing is enabled, go ahead and enable the record.
1828 * The reason not to enable the record immediatelly is the
1829 * inherent check of ftrace_make_nop/ftrace_make_call for
1830 * correct previous instructions. Making first the NOP
1831 * conversion puts the module to the correct state, thus
1832 * passing the ftrace_make_call check.
1834 if (ftrace_start_up && ref) {
1835 int failed = __ftrace_replace_code(p, 1);
1836 if (failed) {
1837 ftrace_bug(failed, p->ip);
1838 ftrace_free_rec(p);
1843 stop = ftrace_now(raw_smp_processor_id());
1844 ftrace_update_time = stop - start;
1845 ftrace_update_tot_cnt += ftrace_update_cnt;
1847 return 0;
1850 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
1852 struct ftrace_page *pg;
1853 int cnt;
1854 int i;
1856 /* allocate a few pages */
1857 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1858 if (!ftrace_pages_start)
1859 return -1;
1862 * Allocate a few more pages.
1864 * TODO: have some parser search vmlinux before
1865 * final linking to find all calls to ftrace.
1866 * Then we can:
1867 * a) know how many pages to allocate.
1868 * and/or
1869 * b) set up the table then.
1871 * The dynamic code is still necessary for
1872 * modules.
1875 pg = ftrace_pages = ftrace_pages_start;
1877 cnt = num_to_init / ENTRIES_PER_PAGE;
1878 pr_info("ftrace: allocating %ld entries in %d pages\n",
1879 num_to_init, cnt + 1);
1881 for (i = 0; i < cnt; i++) {
1882 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1884 /* If we fail, we'll try later anyway */
1885 if (!pg->next)
1886 break;
1888 pg = pg->next;
1891 return 0;
1894 enum {
1895 FTRACE_ITER_FILTER = (1 << 0),
1896 FTRACE_ITER_NOTRACE = (1 << 1),
1897 FTRACE_ITER_PRINTALL = (1 << 2),
1898 FTRACE_ITER_HASH = (1 << 3),
1899 FTRACE_ITER_ENABLED = (1 << 4),
1902 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1904 struct ftrace_iterator {
1905 loff_t pos;
1906 loff_t func_pos;
1907 struct ftrace_page *pg;
1908 struct dyn_ftrace *func;
1909 struct ftrace_func_probe *probe;
1910 struct trace_parser parser;
1911 struct ftrace_hash *hash;
1912 struct ftrace_ops *ops;
1913 int hidx;
1914 int idx;
1915 unsigned flags;
1918 static void *
1919 t_hash_next(struct seq_file *m, loff_t *pos)
1921 struct ftrace_iterator *iter = m->private;
1922 struct hlist_node *hnd = NULL;
1923 struct hlist_head *hhd;
1925 (*pos)++;
1926 iter->pos = *pos;
1928 if (iter->probe)
1929 hnd = &iter->probe->node;
1930 retry:
1931 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1932 return NULL;
1934 hhd = &ftrace_func_hash[iter->hidx];
1936 if (hlist_empty(hhd)) {
1937 iter->hidx++;
1938 hnd = NULL;
1939 goto retry;
1942 if (!hnd)
1943 hnd = hhd->first;
1944 else {
1945 hnd = hnd->next;
1946 if (!hnd) {
1947 iter->hidx++;
1948 goto retry;
1952 if (WARN_ON_ONCE(!hnd))
1953 return NULL;
1955 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
1957 return iter;
1960 static void *t_hash_start(struct seq_file *m, loff_t *pos)
1962 struct ftrace_iterator *iter = m->private;
1963 void *p = NULL;
1964 loff_t l;
1966 if (iter->func_pos > *pos)
1967 return NULL;
1969 iter->hidx = 0;
1970 for (l = 0; l <= (*pos - iter->func_pos); ) {
1971 p = t_hash_next(m, &l);
1972 if (!p)
1973 break;
1975 if (!p)
1976 return NULL;
1978 /* Only set this if we have an item */
1979 iter->flags |= FTRACE_ITER_HASH;
1981 return iter;
1984 static int
1985 t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
1987 struct ftrace_func_probe *rec;
1989 rec = iter->probe;
1990 if (WARN_ON_ONCE(!rec))
1991 return -EIO;
1993 if (rec->ops->print)
1994 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1996 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
1998 if (rec->data)
1999 seq_printf(m, ":%p", rec->data);
2000 seq_putc(m, '\n');
2002 return 0;
2005 static void *
2006 t_next(struct seq_file *m, void *v, loff_t *pos)
2008 struct ftrace_iterator *iter = m->private;
2009 struct ftrace_ops *ops = &global_ops;
2010 struct dyn_ftrace *rec = NULL;
2012 if (unlikely(ftrace_disabled))
2013 return NULL;
2015 if (iter->flags & FTRACE_ITER_HASH)
2016 return t_hash_next(m, pos);
2018 (*pos)++;
2019 iter->pos = iter->func_pos = *pos;
2021 if (iter->flags & FTRACE_ITER_PRINTALL)
2022 return t_hash_start(m, pos);
2024 retry:
2025 if (iter->idx >= iter->pg->index) {
2026 if (iter->pg->next) {
2027 iter->pg = iter->pg->next;
2028 iter->idx = 0;
2029 goto retry;
2031 } else {
2032 rec = &iter->pg->records[iter->idx++];
2033 if ((rec->flags & FTRACE_FL_FREE) ||
2035 ((iter->flags & FTRACE_ITER_FILTER) &&
2036 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
2038 ((iter->flags & FTRACE_ITER_NOTRACE) &&
2039 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2041 ((iter->flags & FTRACE_ITER_ENABLED) &&
2042 !(rec->flags & ~FTRACE_FL_MASK))) {
2044 rec = NULL;
2045 goto retry;
2049 if (!rec)
2050 return t_hash_start(m, pos);
2052 iter->func = rec;
2054 return iter;
2057 static void reset_iter_read(struct ftrace_iterator *iter)
2059 iter->pos = 0;
2060 iter->func_pos = 0;
2061 iter->flags &= ~(FTRACE_ITER_PRINTALL & FTRACE_ITER_HASH);
2064 static void *t_start(struct seq_file *m, loff_t *pos)
2066 struct ftrace_iterator *iter = m->private;
2067 struct ftrace_ops *ops = &global_ops;
2068 void *p = NULL;
2069 loff_t l;
2071 mutex_lock(&ftrace_lock);
2073 if (unlikely(ftrace_disabled))
2074 return NULL;
2077 * If an lseek was done, then reset and start from beginning.
2079 if (*pos < iter->pos)
2080 reset_iter_read(iter);
2083 * For set_ftrace_filter reading, if we have the filter
2084 * off, we can short cut and just print out that all
2085 * functions are enabled.
2087 if (iter->flags & FTRACE_ITER_FILTER && !ops->filter_hash->count) {
2088 if (*pos > 0)
2089 return t_hash_start(m, pos);
2090 iter->flags |= FTRACE_ITER_PRINTALL;
2091 /* reset in case of seek/pread */
2092 iter->flags &= ~FTRACE_ITER_HASH;
2093 return iter;
2096 if (iter->flags & FTRACE_ITER_HASH)
2097 return t_hash_start(m, pos);
2100 * Unfortunately, we need to restart at ftrace_pages_start
2101 * every time we let go of the ftrace_mutex. This is because
2102 * those pointers can change without the lock.
2104 iter->pg = ftrace_pages_start;
2105 iter->idx = 0;
2106 for (l = 0; l <= *pos; ) {
2107 p = t_next(m, p, &l);
2108 if (!p)
2109 break;
2112 if (!p) {
2113 if (iter->flags & FTRACE_ITER_FILTER)
2114 return t_hash_start(m, pos);
2116 return NULL;
2119 return iter;
2122 static void t_stop(struct seq_file *m, void *p)
2124 mutex_unlock(&ftrace_lock);
2127 static int t_show(struct seq_file *m, void *v)
2129 struct ftrace_iterator *iter = m->private;
2130 struct dyn_ftrace *rec;
2132 if (iter->flags & FTRACE_ITER_HASH)
2133 return t_hash_show(m, iter);
2135 if (iter->flags & FTRACE_ITER_PRINTALL) {
2136 seq_printf(m, "#### all functions enabled ####\n");
2137 return 0;
2140 rec = iter->func;
2142 if (!rec)
2143 return 0;
2145 seq_printf(m, "%ps", (void *)rec->ip);
2146 if (iter->flags & FTRACE_ITER_ENABLED)
2147 seq_printf(m, " (%ld)",
2148 rec->flags & ~FTRACE_FL_MASK);
2149 seq_printf(m, "\n");
2151 return 0;
2154 static const struct seq_operations show_ftrace_seq_ops = {
2155 .start = t_start,
2156 .next = t_next,
2157 .stop = t_stop,
2158 .show = t_show,
2161 static int
2162 ftrace_avail_open(struct inode *inode, struct file *file)
2164 struct ftrace_iterator *iter;
2165 int ret;
2167 if (unlikely(ftrace_disabled))
2168 return -ENODEV;
2170 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2171 if (!iter)
2172 return -ENOMEM;
2174 iter->pg = ftrace_pages_start;
2176 ret = seq_open(file, &show_ftrace_seq_ops);
2177 if (!ret) {
2178 struct seq_file *m = file->private_data;
2180 m->private = iter;
2181 } else {
2182 kfree(iter);
2185 return ret;
2188 static int
2189 ftrace_enabled_open(struct inode *inode, struct file *file)
2191 struct ftrace_iterator *iter;
2192 int ret;
2194 if (unlikely(ftrace_disabled))
2195 return -ENODEV;
2197 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2198 if (!iter)
2199 return -ENOMEM;
2201 iter->pg = ftrace_pages_start;
2202 iter->flags = FTRACE_ITER_ENABLED;
2204 ret = seq_open(file, &show_ftrace_seq_ops);
2205 if (!ret) {
2206 struct seq_file *m = file->private_data;
2208 m->private = iter;
2209 } else {
2210 kfree(iter);
2213 return ret;
2216 static void ftrace_filter_reset(struct ftrace_hash *hash)
2218 mutex_lock(&ftrace_lock);
2219 ftrace_hash_clear(hash);
2220 mutex_unlock(&ftrace_lock);
2223 static int
2224 ftrace_regex_open(struct ftrace_ops *ops, int flag,
2225 struct inode *inode, struct file *file)
2227 struct ftrace_iterator *iter;
2228 struct ftrace_hash *hash;
2229 int ret = 0;
2231 if (unlikely(ftrace_disabled))
2232 return -ENODEV;
2234 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2235 if (!iter)
2236 return -ENOMEM;
2238 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2239 kfree(iter);
2240 return -ENOMEM;
2243 if (flag & FTRACE_ITER_NOTRACE)
2244 hash = ops->notrace_hash;
2245 else
2246 hash = ops->filter_hash;
2248 iter->ops = ops;
2249 iter->flags = flag;
2251 if (file->f_mode & FMODE_WRITE) {
2252 mutex_lock(&ftrace_lock);
2253 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2254 mutex_unlock(&ftrace_lock);
2256 if (!iter->hash) {
2257 trace_parser_put(&iter->parser);
2258 kfree(iter);
2259 return -ENOMEM;
2263 mutex_lock(&ftrace_regex_lock);
2265 if ((file->f_mode & FMODE_WRITE) &&
2266 (file->f_flags & O_TRUNC))
2267 ftrace_filter_reset(iter->hash);
2269 if (file->f_mode & FMODE_READ) {
2270 iter->pg = ftrace_pages_start;
2272 ret = seq_open(file, &show_ftrace_seq_ops);
2273 if (!ret) {
2274 struct seq_file *m = file->private_data;
2275 m->private = iter;
2276 } else {
2277 /* Failed */
2278 free_ftrace_hash(iter->hash);
2279 trace_parser_put(&iter->parser);
2280 kfree(iter);
2282 } else
2283 file->private_data = iter;
2284 mutex_unlock(&ftrace_regex_lock);
2286 return ret;
2289 static int
2290 ftrace_filter_open(struct inode *inode, struct file *file)
2292 return ftrace_regex_open(&global_ops, FTRACE_ITER_FILTER,
2293 inode, file);
2296 static int
2297 ftrace_notrace_open(struct inode *inode, struct file *file)
2299 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
2300 inode, file);
2303 static loff_t
2304 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
2306 loff_t ret;
2308 if (file->f_mode & FMODE_READ)
2309 ret = seq_lseek(file, offset, origin);
2310 else
2311 file->f_pos = ret = 1;
2313 return ret;
2316 static int ftrace_match(char *str, char *regex, int len, int type)
2318 int matched = 0;
2319 int slen;
2321 switch (type) {
2322 case MATCH_FULL:
2323 if (strcmp(str, regex) == 0)
2324 matched = 1;
2325 break;
2326 case MATCH_FRONT_ONLY:
2327 if (strncmp(str, regex, len) == 0)
2328 matched = 1;
2329 break;
2330 case MATCH_MIDDLE_ONLY:
2331 if (strstr(str, regex))
2332 matched = 1;
2333 break;
2334 case MATCH_END_ONLY:
2335 slen = strlen(str);
2336 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
2337 matched = 1;
2338 break;
2341 return matched;
2344 static int
2345 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
2347 struct ftrace_func_entry *entry;
2348 int ret = 0;
2350 entry = ftrace_lookup_ip(hash, rec->ip);
2351 if (not) {
2352 /* Do nothing if it doesn't exist */
2353 if (!entry)
2354 return 0;
2356 free_hash_entry(hash, entry);
2357 } else {
2358 /* Do nothing if it exists */
2359 if (entry)
2360 return 0;
2362 ret = add_hash_entry(hash, rec->ip);
2364 return ret;
2367 static int
2368 ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2369 char *regex, int len, int type)
2371 char str[KSYM_SYMBOL_LEN];
2372 char *modname;
2374 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2376 if (mod) {
2377 /* module lookup requires matching the module */
2378 if (!modname || strcmp(modname, mod))
2379 return 0;
2381 /* blank search means to match all funcs in the mod */
2382 if (!len)
2383 return 1;
2386 return ftrace_match(str, regex, len, type);
2389 static int
2390 match_records(struct ftrace_hash *hash, char *buff,
2391 int len, char *mod, int not)
2393 unsigned search_len = 0;
2394 struct ftrace_page *pg;
2395 struct dyn_ftrace *rec;
2396 int type = MATCH_FULL;
2397 char *search = buff;
2398 int found = 0;
2399 int ret;
2401 if (len) {
2402 type = filter_parse_regex(buff, len, &search, &not);
2403 search_len = strlen(search);
2406 mutex_lock(&ftrace_lock);
2408 if (unlikely(ftrace_disabled))
2409 goto out_unlock;
2411 do_for_each_ftrace_rec(pg, rec) {
2413 if (ftrace_match_record(rec, mod, search, search_len, type)) {
2414 ret = enter_record(hash, rec, not);
2415 if (ret < 0) {
2416 found = ret;
2417 goto out_unlock;
2419 found = 1;
2421 } while_for_each_ftrace_rec();
2422 out_unlock:
2423 mutex_unlock(&ftrace_lock);
2425 return found;
2428 static int
2429 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
2431 return match_records(hash, buff, len, NULL, 0);
2434 static int
2435 ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
2437 int not = 0;
2439 /* blank or '*' mean the same */
2440 if (strcmp(buff, "*") == 0)
2441 buff[0] = 0;
2443 /* handle the case of 'dont filter this module' */
2444 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2445 buff[0] = 0;
2446 not = 1;
2449 return match_records(hash, buff, strlen(buff), mod, not);
2453 * We register the module command as a template to show others how
2454 * to register the a command as well.
2457 static int
2458 ftrace_mod_callback(struct ftrace_hash *hash,
2459 char *func, char *cmd, char *param, int enable)
2461 char *mod;
2462 int ret = -EINVAL;
2465 * cmd == 'mod' because we only registered this func
2466 * for the 'mod' ftrace_func_command.
2467 * But if you register one func with multiple commands,
2468 * you can tell which command was used by the cmd
2469 * parameter.
2472 /* we must have a module name */
2473 if (!param)
2474 return ret;
2476 mod = strsep(&param, ":");
2477 if (!strlen(mod))
2478 return ret;
2480 ret = ftrace_match_module_records(hash, func, mod);
2481 if (!ret)
2482 ret = -EINVAL;
2483 if (ret < 0)
2484 return ret;
2486 return 0;
2489 static struct ftrace_func_command ftrace_mod_cmd = {
2490 .name = "mod",
2491 .func = ftrace_mod_callback,
2494 static int __init ftrace_mod_cmd_init(void)
2496 return register_ftrace_command(&ftrace_mod_cmd);
2498 device_initcall(ftrace_mod_cmd_init);
2500 static void
2501 function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
2503 struct ftrace_func_probe *entry;
2504 struct hlist_head *hhd;
2505 struct hlist_node *n;
2506 unsigned long key;
2508 key = hash_long(ip, FTRACE_HASH_BITS);
2510 hhd = &ftrace_func_hash[key];
2512 if (hlist_empty(hhd))
2513 return;
2516 * Disable preemption for these calls to prevent a RCU grace
2517 * period. This syncs the hash iteration and freeing of items
2518 * on the hash. rcu_read_lock is too dangerous here.
2520 preempt_disable_notrace();
2521 hlist_for_each_entry_rcu(entry, n, hhd, node) {
2522 if (entry->ip == ip)
2523 entry->ops->func(ip, parent_ip, &entry->data);
2525 preempt_enable_notrace();
2528 static struct ftrace_ops trace_probe_ops __read_mostly =
2530 .func = function_trace_probe_call,
2533 static int ftrace_probe_registered;
2535 static void __enable_ftrace_function_probe(void)
2537 int ret;
2538 int i;
2540 if (ftrace_probe_registered)
2541 return;
2543 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2544 struct hlist_head *hhd = &ftrace_func_hash[i];
2545 if (hhd->first)
2546 break;
2548 /* Nothing registered? */
2549 if (i == FTRACE_FUNC_HASHSIZE)
2550 return;
2552 ret = __register_ftrace_function(&trace_probe_ops);
2553 if (!ret)
2554 ret = ftrace_startup(&trace_probe_ops, 0);
2556 ftrace_probe_registered = 1;
2559 static void __disable_ftrace_function_probe(void)
2561 int ret;
2562 int i;
2564 if (!ftrace_probe_registered)
2565 return;
2567 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2568 struct hlist_head *hhd = &ftrace_func_hash[i];
2569 if (hhd->first)
2570 return;
2573 /* no more funcs left */
2574 ret = __unregister_ftrace_function(&trace_probe_ops);
2575 if (!ret)
2576 ftrace_shutdown(&trace_probe_ops, 0);
2578 ftrace_probe_registered = 0;
2582 static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2584 struct ftrace_func_probe *entry =
2585 container_of(rhp, struct ftrace_func_probe, rcu);
2587 if (entry->ops->free)
2588 entry->ops->free(&entry->data);
2589 kfree(entry);
2594 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2595 void *data)
2597 struct ftrace_func_probe *entry;
2598 struct ftrace_page *pg;
2599 struct dyn_ftrace *rec;
2600 int type, len, not;
2601 unsigned long key;
2602 int count = 0;
2603 char *search;
2605 type = filter_parse_regex(glob, strlen(glob), &search, &not);
2606 len = strlen(search);
2608 /* we do not support '!' for function probes */
2609 if (WARN_ON(not))
2610 return -EINVAL;
2612 mutex_lock(&ftrace_lock);
2614 if (unlikely(ftrace_disabled))
2615 goto out_unlock;
2617 do_for_each_ftrace_rec(pg, rec) {
2619 if (!ftrace_match_record(rec, NULL, search, len, type))
2620 continue;
2622 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2623 if (!entry) {
2624 /* If we did not process any, then return error */
2625 if (!count)
2626 count = -ENOMEM;
2627 goto out_unlock;
2630 count++;
2632 entry->data = data;
2635 * The caller might want to do something special
2636 * for each function we find. We call the callback
2637 * to give the caller an opportunity to do so.
2639 if (ops->callback) {
2640 if (ops->callback(rec->ip, &entry->data) < 0) {
2641 /* caller does not like this func */
2642 kfree(entry);
2643 continue;
2647 entry->ops = ops;
2648 entry->ip = rec->ip;
2650 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2651 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2653 } while_for_each_ftrace_rec();
2654 __enable_ftrace_function_probe();
2656 out_unlock:
2657 mutex_unlock(&ftrace_lock);
2659 return count;
2662 enum {
2663 PROBE_TEST_FUNC = 1,
2664 PROBE_TEST_DATA = 2
2667 static void
2668 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2669 void *data, int flags)
2671 struct ftrace_func_probe *entry;
2672 struct hlist_node *n, *tmp;
2673 char str[KSYM_SYMBOL_LEN];
2674 int type = MATCH_FULL;
2675 int i, len = 0;
2676 char *search;
2678 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
2679 glob = NULL;
2680 else if (glob) {
2681 int not;
2683 type = filter_parse_regex(glob, strlen(glob), &search, &not);
2684 len = strlen(search);
2686 /* we do not support '!' for function probes */
2687 if (WARN_ON(not))
2688 return;
2691 mutex_lock(&ftrace_lock);
2692 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2693 struct hlist_head *hhd = &ftrace_func_hash[i];
2695 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2697 /* break up if statements for readability */
2698 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
2699 continue;
2701 if ((flags & PROBE_TEST_DATA) && entry->data != data)
2702 continue;
2704 /* do this last, since it is the most expensive */
2705 if (glob) {
2706 kallsyms_lookup(entry->ip, NULL, NULL,
2707 NULL, str);
2708 if (!ftrace_match(str, glob, len, type))
2709 continue;
2712 hlist_del(&entry->node);
2713 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2716 __disable_ftrace_function_probe();
2717 mutex_unlock(&ftrace_lock);
2720 void
2721 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2722 void *data)
2724 __unregister_ftrace_function_probe(glob, ops, data,
2725 PROBE_TEST_FUNC | PROBE_TEST_DATA);
2728 void
2729 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
2731 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
2734 void unregister_ftrace_function_probe_all(char *glob)
2736 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
2739 static LIST_HEAD(ftrace_commands);
2740 static DEFINE_MUTEX(ftrace_cmd_mutex);
2742 int register_ftrace_command(struct ftrace_func_command *cmd)
2744 struct ftrace_func_command *p;
2745 int ret = 0;
2747 mutex_lock(&ftrace_cmd_mutex);
2748 list_for_each_entry(p, &ftrace_commands, list) {
2749 if (strcmp(cmd->name, p->name) == 0) {
2750 ret = -EBUSY;
2751 goto out_unlock;
2754 list_add(&cmd->list, &ftrace_commands);
2755 out_unlock:
2756 mutex_unlock(&ftrace_cmd_mutex);
2758 return ret;
2761 int unregister_ftrace_command(struct ftrace_func_command *cmd)
2763 struct ftrace_func_command *p, *n;
2764 int ret = -ENODEV;
2766 mutex_lock(&ftrace_cmd_mutex);
2767 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2768 if (strcmp(cmd->name, p->name) == 0) {
2769 ret = 0;
2770 list_del_init(&p->list);
2771 goto out_unlock;
2774 out_unlock:
2775 mutex_unlock(&ftrace_cmd_mutex);
2777 return ret;
2780 static int ftrace_process_regex(struct ftrace_hash *hash,
2781 char *buff, int len, int enable)
2783 char *func, *command, *next = buff;
2784 struct ftrace_func_command *p;
2785 int ret = -EINVAL;
2787 func = strsep(&next, ":");
2789 if (!next) {
2790 ret = ftrace_match_records(hash, func, len);
2791 if (!ret)
2792 ret = -EINVAL;
2793 if (ret < 0)
2794 return ret;
2795 return 0;
2798 /* command found */
2800 command = strsep(&next, ":");
2802 mutex_lock(&ftrace_cmd_mutex);
2803 list_for_each_entry(p, &ftrace_commands, list) {
2804 if (strcmp(p->name, command) == 0) {
2805 ret = p->func(hash, func, command, next, enable);
2806 goto out_unlock;
2809 out_unlock:
2810 mutex_unlock(&ftrace_cmd_mutex);
2812 return ret;
2815 static ssize_t
2816 ftrace_regex_write(struct file *file, const char __user *ubuf,
2817 size_t cnt, loff_t *ppos, int enable)
2819 struct ftrace_iterator *iter;
2820 struct trace_parser *parser;
2821 ssize_t ret, read;
2823 if (!cnt)
2824 return 0;
2826 mutex_lock(&ftrace_regex_lock);
2828 ret = -ENODEV;
2829 if (unlikely(ftrace_disabled))
2830 goto out_unlock;
2832 if (file->f_mode & FMODE_READ) {
2833 struct seq_file *m = file->private_data;
2834 iter = m->private;
2835 } else
2836 iter = file->private_data;
2838 parser = &iter->parser;
2839 read = trace_get_user(parser, ubuf, cnt, ppos);
2841 if (read >= 0 && trace_parser_loaded(parser) &&
2842 !trace_parser_cont(parser)) {
2843 ret = ftrace_process_regex(iter->hash, parser->buffer,
2844 parser->idx, enable);
2845 trace_parser_clear(parser);
2846 if (ret)
2847 goto out_unlock;
2850 ret = read;
2851 out_unlock:
2852 mutex_unlock(&ftrace_regex_lock);
2854 return ret;
2857 static ssize_t
2858 ftrace_filter_write(struct file *file, const char __user *ubuf,
2859 size_t cnt, loff_t *ppos)
2861 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2864 static ssize_t
2865 ftrace_notrace_write(struct file *file, const char __user *ubuf,
2866 size_t cnt, loff_t *ppos)
2868 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2871 static int
2872 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
2873 int reset, int enable)
2875 struct ftrace_hash **orig_hash;
2876 struct ftrace_hash *hash;
2877 int ret;
2879 /* All global ops uses the global ops filters */
2880 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
2881 ops = &global_ops;
2883 if (unlikely(ftrace_disabled))
2884 return -ENODEV;
2886 if (enable)
2887 orig_hash = &ops->filter_hash;
2888 else
2889 orig_hash = &ops->notrace_hash;
2891 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
2892 if (!hash)
2893 return -ENOMEM;
2895 mutex_lock(&ftrace_regex_lock);
2896 if (reset)
2897 ftrace_filter_reset(hash);
2898 if (buf)
2899 ftrace_match_records(hash, buf, len);
2901 mutex_lock(&ftrace_lock);
2902 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
2903 if (!ret && ops->flags & FTRACE_OPS_FL_ENABLED
2904 && ftrace_enabled)
2905 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
2907 mutex_unlock(&ftrace_lock);
2909 mutex_unlock(&ftrace_regex_lock);
2911 free_ftrace_hash(hash);
2912 return ret;
2916 * ftrace_set_filter - set a function to filter on in ftrace
2917 * @ops - the ops to set the filter with
2918 * @buf - the string that holds the function filter text.
2919 * @len - the length of the string.
2920 * @reset - non zero to reset all filters before applying this filter.
2922 * Filters denote which functions should be enabled when tracing is enabled.
2923 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2925 void ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
2926 int len, int reset)
2928 ftrace_set_regex(ops, buf, len, reset, 1);
2930 EXPORT_SYMBOL_GPL(ftrace_set_filter);
2933 * ftrace_set_notrace - set a function to not trace in ftrace
2934 * @ops - the ops to set the notrace filter with
2935 * @buf - the string that holds the function notrace text.
2936 * @len - the length of the string.
2937 * @reset - non zero to reset all filters before applying this filter.
2939 * Notrace Filters denote which functions should not be enabled when tracing
2940 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2941 * for tracing.
2943 void ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
2944 int len, int reset)
2946 ftrace_set_regex(ops, buf, len, reset, 0);
2948 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
2950 * ftrace_set_filter - set a function to filter on in ftrace
2951 * @ops - the ops to set the filter with
2952 * @buf - the string that holds the function filter text.
2953 * @len - the length of the string.
2954 * @reset - non zero to reset all filters before applying this filter.
2956 * Filters denote which functions should be enabled when tracing is enabled.
2957 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2959 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
2961 ftrace_set_regex(&global_ops, buf, len, reset, 1);
2963 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
2966 * ftrace_set_notrace - set a function to not trace in ftrace
2967 * @ops - the ops to set the notrace filter with
2968 * @buf - the string that holds the function notrace text.
2969 * @len - the length of the string.
2970 * @reset - non zero to reset all filters before applying this filter.
2972 * Notrace Filters denote which functions should not be enabled when tracing
2973 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2974 * for tracing.
2976 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
2978 ftrace_set_regex(&global_ops, buf, len, reset, 0);
2980 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
2983 * command line interface to allow users to set filters on boot up.
2985 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2986 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
2987 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
2989 static int __init set_ftrace_notrace(char *str)
2991 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
2992 return 1;
2994 __setup("ftrace_notrace=", set_ftrace_notrace);
2996 static int __init set_ftrace_filter(char *str)
2998 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
2999 return 1;
3001 __setup("ftrace_filter=", set_ftrace_filter);
3003 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3004 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
3005 static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
3007 static int __init set_graph_function(char *str)
3009 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
3010 return 1;
3012 __setup("ftrace_graph_filter=", set_graph_function);
3014 static void __init set_ftrace_early_graph(char *buf)
3016 int ret;
3017 char *func;
3019 while (buf) {
3020 func = strsep(&buf, ",");
3021 /* we allow only one expression at a time */
3022 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3023 func);
3024 if (ret)
3025 printk(KERN_DEBUG "ftrace: function %s not "
3026 "traceable\n", func);
3029 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3031 static void __init
3032 set_ftrace_early_filter(struct ftrace_ops *ops, char *buf, int enable)
3034 char *func;
3036 while (buf) {
3037 func = strsep(&buf, ",");
3038 ftrace_set_regex(ops, func, strlen(func), 0, enable);
3042 static void __init set_ftrace_early_filters(void)
3044 if (ftrace_filter_buf[0])
3045 set_ftrace_early_filter(&global_ops, ftrace_filter_buf, 1);
3046 if (ftrace_notrace_buf[0])
3047 set_ftrace_early_filter(&global_ops, ftrace_notrace_buf, 0);
3048 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3049 if (ftrace_graph_buf[0])
3050 set_ftrace_early_graph(ftrace_graph_buf);
3051 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3054 static int
3055 ftrace_regex_release(struct inode *inode, struct file *file)
3057 struct seq_file *m = (struct seq_file *)file->private_data;
3058 struct ftrace_iterator *iter;
3059 struct ftrace_hash **orig_hash;
3060 struct trace_parser *parser;
3061 int filter_hash;
3062 int ret;
3064 mutex_lock(&ftrace_regex_lock);
3065 if (file->f_mode & FMODE_READ) {
3066 iter = m->private;
3068 seq_release(inode, file);
3069 } else
3070 iter = file->private_data;
3072 parser = &iter->parser;
3073 if (trace_parser_loaded(parser)) {
3074 parser->buffer[parser->idx] = 0;
3075 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
3078 trace_parser_put(parser);
3080 if (file->f_mode & FMODE_WRITE) {
3081 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3083 if (filter_hash)
3084 orig_hash = &iter->ops->filter_hash;
3085 else
3086 orig_hash = &iter->ops->notrace_hash;
3088 mutex_lock(&ftrace_lock);
3089 ret = ftrace_hash_move(iter->ops, filter_hash,
3090 orig_hash, iter->hash);
3091 if (!ret && (iter->ops->flags & FTRACE_OPS_FL_ENABLED)
3092 && ftrace_enabled)
3093 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3095 mutex_unlock(&ftrace_lock);
3097 free_ftrace_hash(iter->hash);
3098 kfree(iter);
3100 mutex_unlock(&ftrace_regex_lock);
3101 return 0;
3104 static const struct file_operations ftrace_avail_fops = {
3105 .open = ftrace_avail_open,
3106 .read = seq_read,
3107 .llseek = seq_lseek,
3108 .release = seq_release_private,
3111 static const struct file_operations ftrace_enabled_fops = {
3112 .open = ftrace_enabled_open,
3113 .read = seq_read,
3114 .llseek = seq_lseek,
3115 .release = seq_release_private,
3118 static const struct file_operations ftrace_filter_fops = {
3119 .open = ftrace_filter_open,
3120 .read = seq_read,
3121 .write = ftrace_filter_write,
3122 .llseek = ftrace_regex_lseek,
3123 .release = ftrace_regex_release,
3126 static const struct file_operations ftrace_notrace_fops = {
3127 .open = ftrace_notrace_open,
3128 .read = seq_read,
3129 .write = ftrace_notrace_write,
3130 .llseek = ftrace_regex_lseek,
3131 .release = ftrace_regex_release,
3134 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3136 static DEFINE_MUTEX(graph_lock);
3138 int ftrace_graph_count;
3139 int ftrace_graph_filter_enabled;
3140 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3142 static void *
3143 __g_next(struct seq_file *m, loff_t *pos)
3145 if (*pos >= ftrace_graph_count)
3146 return NULL;
3147 return &ftrace_graph_funcs[*pos];
3150 static void *
3151 g_next(struct seq_file *m, void *v, loff_t *pos)
3153 (*pos)++;
3154 return __g_next(m, pos);
3157 static void *g_start(struct seq_file *m, loff_t *pos)
3159 mutex_lock(&graph_lock);
3161 /* Nothing, tell g_show to print all functions are enabled */
3162 if (!ftrace_graph_filter_enabled && !*pos)
3163 return (void *)1;
3165 return __g_next(m, pos);
3168 static void g_stop(struct seq_file *m, void *p)
3170 mutex_unlock(&graph_lock);
3173 static int g_show(struct seq_file *m, void *v)
3175 unsigned long *ptr = v;
3177 if (!ptr)
3178 return 0;
3180 if (ptr == (unsigned long *)1) {
3181 seq_printf(m, "#### all functions enabled ####\n");
3182 return 0;
3185 seq_printf(m, "%ps\n", (void *)*ptr);
3187 return 0;
3190 static const struct seq_operations ftrace_graph_seq_ops = {
3191 .start = g_start,
3192 .next = g_next,
3193 .stop = g_stop,
3194 .show = g_show,
3197 static int
3198 ftrace_graph_open(struct inode *inode, struct file *file)
3200 int ret = 0;
3202 if (unlikely(ftrace_disabled))
3203 return -ENODEV;
3205 mutex_lock(&graph_lock);
3206 if ((file->f_mode & FMODE_WRITE) &&
3207 (file->f_flags & O_TRUNC)) {
3208 ftrace_graph_filter_enabled = 0;
3209 ftrace_graph_count = 0;
3210 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3212 mutex_unlock(&graph_lock);
3214 if (file->f_mode & FMODE_READ)
3215 ret = seq_open(file, &ftrace_graph_seq_ops);
3217 return ret;
3220 static int
3221 ftrace_graph_release(struct inode *inode, struct file *file)
3223 if (file->f_mode & FMODE_READ)
3224 seq_release(inode, file);
3225 return 0;
3228 static int
3229 ftrace_set_func(unsigned long *array, int *idx, char *buffer)
3231 struct dyn_ftrace *rec;
3232 struct ftrace_page *pg;
3233 int search_len;
3234 int fail = 1;
3235 int type, not;
3236 char *search;
3237 bool exists;
3238 int i;
3240 /* decode regex */
3241 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
3242 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3243 return -EBUSY;
3245 search_len = strlen(search);
3247 mutex_lock(&ftrace_lock);
3249 if (unlikely(ftrace_disabled)) {
3250 mutex_unlock(&ftrace_lock);
3251 return -ENODEV;
3254 do_for_each_ftrace_rec(pg, rec) {
3256 if (rec->flags & FTRACE_FL_FREE)
3257 continue;
3259 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
3260 /* if it is in the array */
3261 exists = false;
3262 for (i = 0; i < *idx; i++) {
3263 if (array[i] == rec->ip) {
3264 exists = true;
3265 break;
3269 if (!not) {
3270 fail = 0;
3271 if (!exists) {
3272 array[(*idx)++] = rec->ip;
3273 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3274 goto out;
3276 } else {
3277 if (exists) {
3278 array[i] = array[--(*idx)];
3279 array[*idx] = 0;
3280 fail = 0;
3284 } while_for_each_ftrace_rec();
3285 out:
3286 mutex_unlock(&ftrace_lock);
3288 if (fail)
3289 return -EINVAL;
3291 ftrace_graph_filter_enabled = 1;
3292 return 0;
3295 static ssize_t
3296 ftrace_graph_write(struct file *file, const char __user *ubuf,
3297 size_t cnt, loff_t *ppos)
3299 struct trace_parser parser;
3300 ssize_t read, ret;
3302 if (!cnt)
3303 return 0;
3305 mutex_lock(&graph_lock);
3307 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3308 ret = -ENOMEM;
3309 goto out_unlock;
3312 read = trace_get_user(&parser, ubuf, cnt, ppos);
3314 if (read >= 0 && trace_parser_loaded((&parser))) {
3315 parser.buffer[parser.idx] = 0;
3317 /* we allow only one expression at a time */
3318 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3319 parser.buffer);
3320 if (ret)
3321 goto out_free;
3324 ret = read;
3326 out_free:
3327 trace_parser_put(&parser);
3328 out_unlock:
3329 mutex_unlock(&graph_lock);
3331 return ret;
3334 static const struct file_operations ftrace_graph_fops = {
3335 .open = ftrace_graph_open,
3336 .read = seq_read,
3337 .write = ftrace_graph_write,
3338 .release = ftrace_graph_release,
3339 .llseek = seq_lseek,
3341 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3343 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
3346 trace_create_file("available_filter_functions", 0444,
3347 d_tracer, NULL, &ftrace_avail_fops);
3349 trace_create_file("enabled_functions", 0444,
3350 d_tracer, NULL, &ftrace_enabled_fops);
3352 trace_create_file("set_ftrace_filter", 0644, d_tracer,
3353 NULL, &ftrace_filter_fops);
3355 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
3356 NULL, &ftrace_notrace_fops);
3358 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3359 trace_create_file("set_graph_function", 0444, d_tracer,
3360 NULL,
3361 &ftrace_graph_fops);
3362 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3364 return 0;
3367 static int ftrace_process_locs(struct module *mod,
3368 unsigned long *start,
3369 unsigned long *end)
3371 unsigned long *p;
3372 unsigned long addr;
3373 unsigned long flags;
3375 mutex_lock(&ftrace_lock);
3376 p = start;
3377 while (p < end) {
3378 addr = ftrace_call_adjust(*p++);
3380 * Some architecture linkers will pad between
3381 * the different mcount_loc sections of different
3382 * object files to satisfy alignments.
3383 * Skip any NULL pointers.
3385 if (!addr)
3386 continue;
3387 ftrace_record_ip(addr);
3391 * Disable interrupts to prevent interrupts from executing
3392 * code that is being modified.
3394 local_irq_save(flags);
3395 ftrace_update_code(mod);
3396 local_irq_restore(flags);
3397 mutex_unlock(&ftrace_lock);
3399 return 0;
3402 #ifdef CONFIG_MODULES
3403 void ftrace_release_mod(struct module *mod)
3405 struct dyn_ftrace *rec;
3406 struct ftrace_page *pg;
3408 mutex_lock(&ftrace_lock);
3410 if (ftrace_disabled)
3411 goto out_unlock;
3413 do_for_each_ftrace_rec(pg, rec) {
3414 if (within_module_core(rec->ip, mod)) {
3416 * rec->ip is changed in ftrace_free_rec()
3417 * It should not between s and e if record was freed.
3419 FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
3420 ftrace_free_rec(rec);
3422 } while_for_each_ftrace_rec();
3423 out_unlock:
3424 mutex_unlock(&ftrace_lock);
3427 static void ftrace_init_module(struct module *mod,
3428 unsigned long *start, unsigned long *end)
3430 if (ftrace_disabled || start == end)
3431 return;
3432 ftrace_process_locs(mod, start, end);
3435 static int ftrace_module_notify(struct notifier_block *self,
3436 unsigned long val, void *data)
3438 struct module *mod = data;
3440 switch (val) {
3441 case MODULE_STATE_COMING:
3442 ftrace_init_module(mod, mod->ftrace_callsites,
3443 mod->ftrace_callsites +
3444 mod->num_ftrace_callsites);
3445 break;
3446 case MODULE_STATE_GOING:
3447 ftrace_release_mod(mod);
3448 break;
3451 return 0;
3453 #else
3454 static int ftrace_module_notify(struct notifier_block *self,
3455 unsigned long val, void *data)
3457 return 0;
3459 #endif /* CONFIG_MODULES */
3461 struct notifier_block ftrace_module_nb = {
3462 .notifier_call = ftrace_module_notify,
3463 .priority = 0,
3466 extern unsigned long __start_mcount_loc[];
3467 extern unsigned long __stop_mcount_loc[];
3469 void __init ftrace_init(void)
3471 unsigned long count, addr, flags;
3472 int ret;
3474 /* Keep the ftrace pointer to the stub */
3475 addr = (unsigned long)ftrace_stub;
3477 local_irq_save(flags);
3478 ftrace_dyn_arch_init(&addr);
3479 local_irq_restore(flags);
3481 /* ftrace_dyn_arch_init places the return code in addr */
3482 if (addr)
3483 goto failed;
3485 count = __stop_mcount_loc - __start_mcount_loc;
3487 ret = ftrace_dyn_table_alloc(count);
3488 if (ret)
3489 goto failed;
3491 last_ftrace_enabled = ftrace_enabled = 1;
3493 ret = ftrace_process_locs(NULL,
3494 __start_mcount_loc,
3495 __stop_mcount_loc);
3497 ret = register_module_notifier(&ftrace_module_nb);
3498 if (ret)
3499 pr_warning("Failed to register trace ftrace module notifier\n");
3501 set_ftrace_early_filters();
3503 return;
3504 failed:
3505 ftrace_disabled = 1;
3508 #else
3510 static struct ftrace_ops global_ops = {
3511 .func = ftrace_stub,
3514 static int __init ftrace_nodyn_init(void)
3516 ftrace_enabled = 1;
3517 return 0;
3519 device_initcall(ftrace_nodyn_init);
3521 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
3522 static inline void ftrace_startup_enable(int command) { }
3523 /* Keep as macros so we do not need to define the commands */
3524 # define ftrace_startup(ops, command) \
3525 ({ \
3526 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
3527 0; \
3529 # define ftrace_shutdown(ops, command) do { } while (0)
3530 # define ftrace_startup_sysctl() do { } while (0)
3531 # define ftrace_shutdown_sysctl() do { } while (0)
3533 static inline int
3534 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
3536 return 1;
3539 #endif /* CONFIG_DYNAMIC_FTRACE */
3541 static void
3542 ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip)
3544 struct ftrace_ops *op;
3546 if (unlikely(trace_recursion_test(TRACE_INTERNAL_BIT)))
3547 return;
3549 trace_recursion_set(TRACE_INTERNAL_BIT);
3551 * Some of the ops may be dynamically allocated,
3552 * they must be freed after a synchronize_sched().
3554 preempt_disable_notrace();
3555 op = rcu_dereference_raw(ftrace_ops_list);
3556 while (op != &ftrace_list_end) {
3557 if (ftrace_ops_test(op, ip))
3558 op->func(ip, parent_ip);
3559 op = rcu_dereference_raw(op->next);
3561 preempt_enable_notrace();
3562 trace_recursion_clear(TRACE_INTERNAL_BIT);
3565 static void clear_ftrace_swapper(void)
3567 struct task_struct *p;
3568 int cpu;
3570 get_online_cpus();
3571 for_each_online_cpu(cpu) {
3572 p = idle_task(cpu);
3573 clear_tsk_trace_trace(p);
3575 put_online_cpus();
3578 static void set_ftrace_swapper(void)
3580 struct task_struct *p;
3581 int cpu;
3583 get_online_cpus();
3584 for_each_online_cpu(cpu) {
3585 p = idle_task(cpu);
3586 set_tsk_trace_trace(p);
3588 put_online_cpus();
3591 static void clear_ftrace_pid(struct pid *pid)
3593 struct task_struct *p;
3595 rcu_read_lock();
3596 do_each_pid_task(pid, PIDTYPE_PID, p) {
3597 clear_tsk_trace_trace(p);
3598 } while_each_pid_task(pid, PIDTYPE_PID, p);
3599 rcu_read_unlock();
3601 put_pid(pid);
3604 static void set_ftrace_pid(struct pid *pid)
3606 struct task_struct *p;
3608 rcu_read_lock();
3609 do_each_pid_task(pid, PIDTYPE_PID, p) {
3610 set_tsk_trace_trace(p);
3611 } while_each_pid_task(pid, PIDTYPE_PID, p);
3612 rcu_read_unlock();
3615 static void clear_ftrace_pid_task(struct pid *pid)
3617 if (pid == ftrace_swapper_pid)
3618 clear_ftrace_swapper();
3619 else
3620 clear_ftrace_pid(pid);
3623 static void set_ftrace_pid_task(struct pid *pid)
3625 if (pid == ftrace_swapper_pid)
3626 set_ftrace_swapper();
3627 else
3628 set_ftrace_pid(pid);
3631 static int ftrace_pid_add(int p)
3633 struct pid *pid;
3634 struct ftrace_pid *fpid;
3635 int ret = -EINVAL;
3637 mutex_lock(&ftrace_lock);
3639 if (!p)
3640 pid = ftrace_swapper_pid;
3641 else
3642 pid = find_get_pid(p);
3644 if (!pid)
3645 goto out;
3647 ret = 0;
3649 list_for_each_entry(fpid, &ftrace_pids, list)
3650 if (fpid->pid == pid)
3651 goto out_put;
3653 ret = -ENOMEM;
3655 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
3656 if (!fpid)
3657 goto out_put;
3659 list_add(&fpid->list, &ftrace_pids);
3660 fpid->pid = pid;
3662 set_ftrace_pid_task(pid);
3664 ftrace_update_pid_func();
3665 ftrace_startup_enable(0);
3667 mutex_unlock(&ftrace_lock);
3668 return 0;
3670 out_put:
3671 if (pid != ftrace_swapper_pid)
3672 put_pid(pid);
3674 out:
3675 mutex_unlock(&ftrace_lock);
3676 return ret;
3679 static void ftrace_pid_reset(void)
3681 struct ftrace_pid *fpid, *safe;
3683 mutex_lock(&ftrace_lock);
3684 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
3685 struct pid *pid = fpid->pid;
3687 clear_ftrace_pid_task(pid);
3689 list_del(&fpid->list);
3690 kfree(fpid);
3693 ftrace_update_pid_func();
3694 ftrace_startup_enable(0);
3696 mutex_unlock(&ftrace_lock);
3699 static void *fpid_start(struct seq_file *m, loff_t *pos)
3701 mutex_lock(&ftrace_lock);
3703 if (list_empty(&ftrace_pids) && (!*pos))
3704 return (void *) 1;
3706 return seq_list_start(&ftrace_pids, *pos);
3709 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
3711 if (v == (void *)1)
3712 return NULL;
3714 return seq_list_next(v, &ftrace_pids, pos);
3717 static void fpid_stop(struct seq_file *m, void *p)
3719 mutex_unlock(&ftrace_lock);
3722 static int fpid_show(struct seq_file *m, void *v)
3724 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
3726 if (v == (void *)1) {
3727 seq_printf(m, "no pid\n");
3728 return 0;
3731 if (fpid->pid == ftrace_swapper_pid)
3732 seq_printf(m, "swapper tasks\n");
3733 else
3734 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
3736 return 0;
3739 static const struct seq_operations ftrace_pid_sops = {
3740 .start = fpid_start,
3741 .next = fpid_next,
3742 .stop = fpid_stop,
3743 .show = fpid_show,
3746 static int
3747 ftrace_pid_open(struct inode *inode, struct file *file)
3749 int ret = 0;
3751 if ((file->f_mode & FMODE_WRITE) &&
3752 (file->f_flags & O_TRUNC))
3753 ftrace_pid_reset();
3755 if (file->f_mode & FMODE_READ)
3756 ret = seq_open(file, &ftrace_pid_sops);
3758 return ret;
3761 static ssize_t
3762 ftrace_pid_write(struct file *filp, const char __user *ubuf,
3763 size_t cnt, loff_t *ppos)
3765 char buf[64], *tmp;
3766 long val;
3767 int ret;
3769 if (cnt >= sizeof(buf))
3770 return -EINVAL;
3772 if (copy_from_user(&buf, ubuf, cnt))
3773 return -EFAULT;
3775 buf[cnt] = 0;
3778 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
3779 * to clean the filter quietly.
3781 tmp = strstrip(buf);
3782 if (strlen(tmp) == 0)
3783 return 1;
3785 ret = strict_strtol(tmp, 10, &val);
3786 if (ret < 0)
3787 return ret;
3789 ret = ftrace_pid_add(val);
3791 return ret ? ret : cnt;
3794 static int
3795 ftrace_pid_release(struct inode *inode, struct file *file)
3797 if (file->f_mode & FMODE_READ)
3798 seq_release(inode, file);
3800 return 0;
3803 static const struct file_operations ftrace_pid_fops = {
3804 .open = ftrace_pid_open,
3805 .write = ftrace_pid_write,
3806 .read = seq_read,
3807 .llseek = seq_lseek,
3808 .release = ftrace_pid_release,
3811 static __init int ftrace_init_debugfs(void)
3813 struct dentry *d_tracer;
3815 d_tracer = tracing_init_dentry();
3816 if (!d_tracer)
3817 return 0;
3819 ftrace_init_dyn_debugfs(d_tracer);
3821 trace_create_file("set_ftrace_pid", 0644, d_tracer,
3822 NULL, &ftrace_pid_fops);
3824 ftrace_profile_debugfs(d_tracer);
3826 return 0;
3828 fs_initcall(ftrace_init_debugfs);
3831 * ftrace_kill - kill ftrace
3833 * This function should be used by panic code. It stops ftrace
3834 * but in a not so nice way. If you need to simply kill ftrace
3835 * from a non-atomic section, use ftrace_kill.
3837 void ftrace_kill(void)
3839 ftrace_disabled = 1;
3840 ftrace_enabled = 0;
3841 clear_ftrace_function();
3845 * register_ftrace_function - register a function for profiling
3846 * @ops - ops structure that holds the function for profiling.
3848 * Register a function to be called by all functions in the
3849 * kernel.
3851 * Note: @ops->func and all the functions it calls must be labeled
3852 * with "notrace", otherwise it will go into a
3853 * recursive loop.
3855 int register_ftrace_function(struct ftrace_ops *ops)
3857 int ret = -1;
3859 mutex_lock(&ftrace_lock);
3861 if (unlikely(ftrace_disabled))
3862 goto out_unlock;
3864 ret = __register_ftrace_function(ops);
3865 if (!ret)
3866 ret = ftrace_startup(ops, 0);
3869 out_unlock:
3870 mutex_unlock(&ftrace_lock);
3871 return ret;
3873 EXPORT_SYMBOL_GPL(register_ftrace_function);
3876 * unregister_ftrace_function - unregister a function for profiling.
3877 * @ops - ops structure that holds the function to unregister
3879 * Unregister a function that was added to be called by ftrace profiling.
3881 int unregister_ftrace_function(struct ftrace_ops *ops)
3883 int ret;
3885 mutex_lock(&ftrace_lock);
3886 ret = __unregister_ftrace_function(ops);
3887 if (!ret)
3888 ftrace_shutdown(ops, 0);
3889 mutex_unlock(&ftrace_lock);
3891 return ret;
3893 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
3896 ftrace_enable_sysctl(struct ctl_table *table, int write,
3897 void __user *buffer, size_t *lenp,
3898 loff_t *ppos)
3900 int ret = -ENODEV;
3902 mutex_lock(&ftrace_lock);
3904 if (unlikely(ftrace_disabled))
3905 goto out;
3907 ret = proc_dointvec(table, write, buffer, lenp, ppos);
3909 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
3910 goto out;
3912 last_ftrace_enabled = !!ftrace_enabled;
3914 if (ftrace_enabled) {
3916 ftrace_startup_sysctl();
3918 /* we are starting ftrace again */
3919 if (ftrace_ops_list != &ftrace_list_end) {
3920 if (ftrace_ops_list->next == &ftrace_list_end)
3921 ftrace_trace_function = ftrace_ops_list->func;
3922 else
3923 ftrace_trace_function = ftrace_ops_list_func;
3926 } else {
3927 /* stopping ftrace calls (just send to ftrace_stub) */
3928 ftrace_trace_function = ftrace_stub;
3930 ftrace_shutdown_sysctl();
3933 out:
3934 mutex_unlock(&ftrace_lock);
3935 return ret;
3938 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3940 static int ftrace_graph_active;
3941 static struct notifier_block ftrace_suspend_notifier;
3943 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3945 return 0;
3948 /* The callbacks that hook a function */
3949 trace_func_graph_ret_t ftrace_graph_return =
3950 (trace_func_graph_ret_t)ftrace_stub;
3951 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
3953 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3954 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3956 int i;
3957 int ret = 0;
3958 unsigned long flags;
3959 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3960 struct task_struct *g, *t;
3962 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3963 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3964 * sizeof(struct ftrace_ret_stack),
3965 GFP_KERNEL);
3966 if (!ret_stack_list[i]) {
3967 start = 0;
3968 end = i;
3969 ret = -ENOMEM;
3970 goto free;
3974 read_lock_irqsave(&tasklist_lock, flags);
3975 do_each_thread(g, t) {
3976 if (start == end) {
3977 ret = -EAGAIN;
3978 goto unlock;
3981 if (t->ret_stack == NULL) {
3982 atomic_set(&t->tracing_graph_pause, 0);
3983 atomic_set(&t->trace_overrun, 0);
3984 t->curr_ret_stack = -1;
3985 /* Make sure the tasks see the -1 first: */
3986 smp_wmb();
3987 t->ret_stack = ret_stack_list[start++];
3989 } while_each_thread(g, t);
3991 unlock:
3992 read_unlock_irqrestore(&tasklist_lock, flags);
3993 free:
3994 for (i = start; i < end; i++)
3995 kfree(ret_stack_list[i]);
3996 return ret;
3999 static void
4000 ftrace_graph_probe_sched_switch(void *ignore,
4001 struct task_struct *prev, struct task_struct *next)
4003 unsigned long long timestamp;
4004 int index;
4007 * Does the user want to count the time a function was asleep.
4008 * If so, do not update the time stamps.
4010 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4011 return;
4013 timestamp = trace_clock_local();
4015 prev->ftrace_timestamp = timestamp;
4017 /* only process tasks that we timestamped */
4018 if (!next->ftrace_timestamp)
4019 return;
4022 * Update all the counters in next to make up for the
4023 * time next was sleeping.
4025 timestamp -= next->ftrace_timestamp;
4027 for (index = next->curr_ret_stack; index >= 0; index--)
4028 next->ret_stack[index].calltime += timestamp;
4031 /* Allocate a return stack for each task */
4032 static int start_graph_tracing(void)
4034 struct ftrace_ret_stack **ret_stack_list;
4035 int ret, cpu;
4037 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4038 sizeof(struct ftrace_ret_stack *),
4039 GFP_KERNEL);
4041 if (!ret_stack_list)
4042 return -ENOMEM;
4044 /* The cpu_boot init_task->ret_stack will never be freed */
4045 for_each_online_cpu(cpu) {
4046 if (!idle_task(cpu)->ret_stack)
4047 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
4050 do {
4051 ret = alloc_retstack_tasklist(ret_stack_list);
4052 } while (ret == -EAGAIN);
4054 if (!ret) {
4055 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
4056 if (ret)
4057 pr_info("ftrace_graph: Couldn't activate tracepoint"
4058 " probe to kernel_sched_switch\n");
4061 kfree(ret_stack_list);
4062 return ret;
4066 * Hibernation protection.
4067 * The state of the current task is too much unstable during
4068 * suspend/restore to disk. We want to protect against that.
4070 static int
4071 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4072 void *unused)
4074 switch (state) {
4075 case PM_HIBERNATION_PREPARE:
4076 pause_graph_tracing();
4077 break;
4079 case PM_POST_HIBERNATION:
4080 unpause_graph_tracing();
4081 break;
4083 return NOTIFY_DONE;
4086 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4087 trace_func_graph_ent_t entryfunc)
4089 int ret = 0;
4091 mutex_lock(&ftrace_lock);
4093 /* we currently allow only one tracer registered at a time */
4094 if (ftrace_graph_active) {
4095 ret = -EBUSY;
4096 goto out;
4099 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
4100 register_pm_notifier(&ftrace_suspend_notifier);
4102 ftrace_graph_active++;
4103 ret = start_graph_tracing();
4104 if (ret) {
4105 ftrace_graph_active--;
4106 goto out;
4109 ftrace_graph_return = retfunc;
4110 ftrace_graph_entry = entryfunc;
4112 ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
4114 out:
4115 mutex_unlock(&ftrace_lock);
4116 return ret;
4119 void unregister_ftrace_graph(void)
4121 mutex_lock(&ftrace_lock);
4123 if (unlikely(!ftrace_graph_active))
4124 goto out;
4126 ftrace_graph_active--;
4127 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
4128 ftrace_graph_entry = ftrace_graph_entry_stub;
4129 ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
4130 unregister_pm_notifier(&ftrace_suspend_notifier);
4131 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
4133 out:
4134 mutex_unlock(&ftrace_lock);
4137 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
4139 static void
4140 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
4142 atomic_set(&t->tracing_graph_pause, 0);
4143 atomic_set(&t->trace_overrun, 0);
4144 t->ftrace_timestamp = 0;
4145 /* make curr_ret_stack visible before we add the ret_stack */
4146 smp_wmb();
4147 t->ret_stack = ret_stack;
4151 * Allocate a return stack for the idle task. May be the first
4152 * time through, or it may be done by CPU hotplug online.
4154 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
4156 t->curr_ret_stack = -1;
4158 * The idle task has no parent, it either has its own
4159 * stack or no stack at all.
4161 if (t->ret_stack)
4162 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
4164 if (ftrace_graph_active) {
4165 struct ftrace_ret_stack *ret_stack;
4167 ret_stack = per_cpu(idle_ret_stack, cpu);
4168 if (!ret_stack) {
4169 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4170 * sizeof(struct ftrace_ret_stack),
4171 GFP_KERNEL);
4172 if (!ret_stack)
4173 return;
4174 per_cpu(idle_ret_stack, cpu) = ret_stack;
4176 graph_init_task(t, ret_stack);
4180 /* Allocate a return stack for newly created task */
4181 void ftrace_graph_init_task(struct task_struct *t)
4183 /* Make sure we do not use the parent ret_stack */
4184 t->ret_stack = NULL;
4185 t->curr_ret_stack = -1;
4187 if (ftrace_graph_active) {
4188 struct ftrace_ret_stack *ret_stack;
4190 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4191 * sizeof(struct ftrace_ret_stack),
4192 GFP_KERNEL);
4193 if (!ret_stack)
4194 return;
4195 graph_init_task(t, ret_stack);
4199 void ftrace_graph_exit_task(struct task_struct *t)
4201 struct ftrace_ret_stack *ret_stack = t->ret_stack;
4203 t->ret_stack = NULL;
4204 /* NULL must become visible to IRQs before we free it: */
4205 barrier();
4207 kfree(ret_stack);
4210 void ftrace_graph_stop(void)
4212 ftrace_stop();
4214 #endif