tracing: Fix failure path in ftrace_graph_write()
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / trace / ftrace.c
blob5c5cb9be8e8c44ca9752dc5acb940e81335ddcf8
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/kprobes.h>
26 #include <linux/ftrace.h>
27 #include <linux/sysctl.h>
28 #include <linux/ctype.h>
29 #include <linux/list.h>
30 #include <linux/hash.h>
32 #include <trace/events/sched.h>
34 #include <asm/ftrace.h>
35 #include <asm/setup.h>
37 #include "trace_output.h"
38 #include "trace_stat.h"
40 #define FTRACE_WARN_ON(cond) \
41 do { \
42 if (WARN_ON(cond)) \
43 ftrace_kill(); \
44 } while (0)
46 #define FTRACE_WARN_ON_ONCE(cond) \
47 do { \
48 if (WARN_ON_ONCE(cond)) \
49 ftrace_kill(); \
50 } while (0)
52 /* hash bits for specific function selection */
53 #define FTRACE_HASH_BITS 7
54 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
56 /* ftrace_enabled is a method to turn ftrace on or off */
57 int ftrace_enabled __read_mostly;
58 static int last_ftrace_enabled;
60 /* Quick disabling of function tracer. */
61 int function_trace_stop;
64 * ftrace_disabled is set when an anomaly is discovered.
65 * ftrace_disabled is much stronger than ftrace_enabled.
67 static int ftrace_disabled __read_mostly;
69 static DEFINE_MUTEX(ftrace_lock);
71 static struct ftrace_ops ftrace_list_end __read_mostly =
73 .func = ftrace_stub,
76 static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
77 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
78 ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
79 ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
81 static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
83 struct ftrace_ops *op = ftrace_list;
85 /* in case someone actually ports this to alpha! */
86 read_barrier_depends();
88 while (op != &ftrace_list_end) {
89 /* silly alpha */
90 read_barrier_depends();
91 op->func(ip, parent_ip);
92 op = op->next;
96 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
98 if (!test_tsk_trace_trace(current))
99 return;
101 ftrace_pid_function(ip, parent_ip);
104 static void set_ftrace_pid_function(ftrace_func_t func)
106 /* do not set ftrace_pid_function to itself! */
107 if (func != ftrace_pid_func)
108 ftrace_pid_function = func;
112 * clear_ftrace_function - reset the ftrace function
114 * This NULLs the ftrace function and in essence stops
115 * tracing. There may be lag
117 void clear_ftrace_function(void)
119 ftrace_trace_function = ftrace_stub;
120 __ftrace_trace_function = ftrace_stub;
121 ftrace_pid_function = ftrace_stub;
124 #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
126 * For those archs that do not test ftrace_trace_stop in their
127 * mcount call site, we need to do it from C.
129 static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
131 if (function_trace_stop)
132 return;
134 __ftrace_trace_function(ip, parent_ip);
136 #endif
138 static int __register_ftrace_function(struct ftrace_ops *ops)
140 ops->next = ftrace_list;
142 * We are entering ops into the ftrace_list but another
143 * CPU might be walking that list. We need to make sure
144 * the ops->next pointer is valid before another CPU sees
145 * the ops pointer included into the ftrace_list.
147 smp_wmb();
148 ftrace_list = ops;
150 if (ftrace_enabled) {
151 ftrace_func_t func;
153 if (ops->next == &ftrace_list_end)
154 func = ops->func;
155 else
156 func = ftrace_list_func;
158 if (ftrace_pid_trace) {
159 set_ftrace_pid_function(func);
160 func = ftrace_pid_func;
164 * For one func, simply call it directly.
165 * For more than one func, call the chain.
167 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
168 ftrace_trace_function = func;
169 #else
170 __ftrace_trace_function = func;
171 ftrace_trace_function = ftrace_test_stop_func;
172 #endif
175 return 0;
178 static int __unregister_ftrace_function(struct ftrace_ops *ops)
180 struct ftrace_ops **p;
183 * If we are removing the last function, then simply point
184 * to the ftrace_stub.
186 if (ftrace_list == ops && ops->next == &ftrace_list_end) {
187 ftrace_trace_function = ftrace_stub;
188 ftrace_list = &ftrace_list_end;
189 return 0;
192 for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
193 if (*p == ops)
194 break;
196 if (*p != ops)
197 return -1;
199 *p = (*p)->next;
201 if (ftrace_enabled) {
202 /* If we only have one func left, then call that directly */
203 if (ftrace_list->next == &ftrace_list_end) {
204 ftrace_func_t func = ftrace_list->func;
206 if (ftrace_pid_trace) {
207 set_ftrace_pid_function(func);
208 func = ftrace_pid_func;
210 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
211 ftrace_trace_function = func;
212 #else
213 __ftrace_trace_function = func;
214 #endif
218 return 0;
221 static void ftrace_update_pid_func(void)
223 ftrace_func_t func;
225 if (ftrace_trace_function == ftrace_stub)
226 return;
228 func = ftrace_trace_function;
230 if (ftrace_pid_trace) {
231 set_ftrace_pid_function(func);
232 func = ftrace_pid_func;
233 } else {
234 if (func == ftrace_pid_func)
235 func = ftrace_pid_function;
238 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
239 ftrace_trace_function = func;
240 #else
241 __ftrace_trace_function = func;
242 #endif
245 #ifdef CONFIG_FUNCTION_PROFILER
246 struct ftrace_profile {
247 struct hlist_node node;
248 unsigned long ip;
249 unsigned long counter;
250 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
251 unsigned long long time;
252 #endif
255 struct ftrace_profile_page {
256 struct ftrace_profile_page *next;
257 unsigned long index;
258 struct ftrace_profile records[];
261 struct ftrace_profile_stat {
262 atomic_t disabled;
263 struct hlist_head *hash;
264 struct ftrace_profile_page *pages;
265 struct ftrace_profile_page *start;
266 struct tracer_stat stat;
269 #define PROFILE_RECORDS_SIZE \
270 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
272 #define PROFILES_PER_PAGE \
273 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
275 static int ftrace_profile_bits __read_mostly;
276 static int ftrace_profile_enabled __read_mostly;
278 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
279 static DEFINE_MUTEX(ftrace_profile_lock);
281 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
283 #define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
285 static void *
286 function_stat_next(void *v, int idx)
288 struct ftrace_profile *rec = v;
289 struct ftrace_profile_page *pg;
291 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
293 again:
294 if (idx != 0)
295 rec++;
297 if ((void *)rec >= (void *)&pg->records[pg->index]) {
298 pg = pg->next;
299 if (!pg)
300 return NULL;
301 rec = &pg->records[0];
302 if (!rec->counter)
303 goto again;
306 return rec;
309 static void *function_stat_start(struct tracer_stat *trace)
311 struct ftrace_profile_stat *stat =
312 container_of(trace, struct ftrace_profile_stat, stat);
314 if (!stat || !stat->start)
315 return NULL;
317 return function_stat_next(&stat->start->records[0], 0);
320 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
321 /* function graph compares on total time */
322 static int function_stat_cmp(void *p1, void *p2)
324 struct ftrace_profile *a = p1;
325 struct ftrace_profile *b = p2;
327 if (a->time < b->time)
328 return -1;
329 if (a->time > b->time)
330 return 1;
331 else
332 return 0;
334 #else
335 /* not function graph compares against hits */
336 static int function_stat_cmp(void *p1, void *p2)
338 struct ftrace_profile *a = p1;
339 struct ftrace_profile *b = p2;
341 if (a->counter < b->counter)
342 return -1;
343 if (a->counter > b->counter)
344 return 1;
345 else
346 return 0;
348 #endif
350 static int function_stat_headers(struct seq_file *m)
352 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
353 seq_printf(m, " Function "
354 "Hit Time Avg\n"
355 " -------- "
356 "--- ---- ---\n");
357 #else
358 seq_printf(m, " Function Hit\n"
359 " -------- ---\n");
360 #endif
361 return 0;
364 static int function_stat_show(struct seq_file *m, void *v)
366 struct ftrace_profile *rec = v;
367 char str[KSYM_SYMBOL_LEN];
368 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
369 static DEFINE_MUTEX(mutex);
370 static struct trace_seq s;
371 unsigned long long avg;
372 #endif
374 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
375 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
377 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
378 seq_printf(m, " ");
379 avg = rec->time;
380 do_div(avg, rec->counter);
382 mutex_lock(&mutex);
383 trace_seq_init(&s);
384 trace_print_graph_duration(rec->time, &s);
385 trace_seq_puts(&s, " ");
386 trace_print_graph_duration(avg, &s);
387 trace_print_seq(m, &s);
388 mutex_unlock(&mutex);
389 #endif
390 seq_putc(m, '\n');
392 return 0;
395 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
397 struct ftrace_profile_page *pg;
399 pg = stat->pages = stat->start;
401 while (pg) {
402 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
403 pg->index = 0;
404 pg = pg->next;
407 memset(stat->hash, 0,
408 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
411 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
413 struct ftrace_profile_page *pg;
414 int functions;
415 int pages;
416 int i;
418 /* If we already allocated, do nothing */
419 if (stat->pages)
420 return 0;
422 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
423 if (!stat->pages)
424 return -ENOMEM;
426 #ifdef CONFIG_DYNAMIC_FTRACE
427 functions = ftrace_update_tot_cnt;
428 #else
430 * We do not know the number of functions that exist because
431 * dynamic tracing is what counts them. With past experience
432 * we have around 20K functions. That should be more than enough.
433 * It is highly unlikely we will execute every function in
434 * the kernel.
436 functions = 20000;
437 #endif
439 pg = stat->start = stat->pages;
441 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
443 for (i = 0; i < pages; i++) {
444 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
445 if (!pg->next)
446 goto out_free;
447 pg = pg->next;
450 return 0;
452 out_free:
453 pg = stat->start;
454 while (pg) {
455 unsigned long tmp = (unsigned long)pg;
457 pg = pg->next;
458 free_page(tmp);
461 free_page((unsigned long)stat->pages);
462 stat->pages = NULL;
463 stat->start = NULL;
465 return -ENOMEM;
468 static int ftrace_profile_init_cpu(int cpu)
470 struct ftrace_profile_stat *stat;
471 int size;
473 stat = &per_cpu(ftrace_profile_stats, cpu);
475 if (stat->hash) {
476 /* If the profile is already created, simply reset it */
477 ftrace_profile_reset(stat);
478 return 0;
482 * We are profiling all functions, but usually only a few thousand
483 * functions are hit. We'll make a hash of 1024 items.
485 size = FTRACE_PROFILE_HASH_SIZE;
487 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
489 if (!stat->hash)
490 return -ENOMEM;
492 if (!ftrace_profile_bits) {
493 size--;
495 for (; size; size >>= 1)
496 ftrace_profile_bits++;
499 /* Preallocate the function profiling pages */
500 if (ftrace_profile_pages_init(stat) < 0) {
501 kfree(stat->hash);
502 stat->hash = NULL;
503 return -ENOMEM;
506 return 0;
509 static int ftrace_profile_init(void)
511 int cpu;
512 int ret = 0;
514 for_each_online_cpu(cpu) {
515 ret = ftrace_profile_init_cpu(cpu);
516 if (ret)
517 break;
520 return ret;
523 /* interrupts must be disabled */
524 static struct ftrace_profile *
525 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
527 struct ftrace_profile *rec;
528 struct hlist_head *hhd;
529 struct hlist_node *n;
530 unsigned long key;
532 key = hash_long(ip, ftrace_profile_bits);
533 hhd = &stat->hash[key];
535 if (hlist_empty(hhd))
536 return NULL;
538 hlist_for_each_entry_rcu(rec, n, hhd, node) {
539 if (rec->ip == ip)
540 return rec;
543 return NULL;
546 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
547 struct ftrace_profile *rec)
549 unsigned long key;
551 key = hash_long(rec->ip, ftrace_profile_bits);
552 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
556 * The memory is already allocated, this simply finds a new record to use.
558 static struct ftrace_profile *
559 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
561 struct ftrace_profile *rec = NULL;
563 /* prevent recursion (from NMIs) */
564 if (atomic_inc_return(&stat->disabled) != 1)
565 goto out;
568 * Try to find the function again since an NMI
569 * could have added it
571 rec = ftrace_find_profiled_func(stat, ip);
572 if (rec)
573 goto out;
575 if (stat->pages->index == PROFILES_PER_PAGE) {
576 if (!stat->pages->next)
577 goto out;
578 stat->pages = stat->pages->next;
581 rec = &stat->pages->records[stat->pages->index++];
582 rec->ip = ip;
583 ftrace_add_profile(stat, rec);
585 out:
586 atomic_dec(&stat->disabled);
588 return rec;
591 static void
592 function_profile_call(unsigned long ip, unsigned long parent_ip)
594 struct ftrace_profile_stat *stat;
595 struct ftrace_profile *rec;
596 unsigned long flags;
598 if (!ftrace_profile_enabled)
599 return;
601 local_irq_save(flags);
603 stat = &__get_cpu_var(ftrace_profile_stats);
604 if (!stat->hash || !ftrace_profile_enabled)
605 goto out;
607 rec = ftrace_find_profiled_func(stat, ip);
608 if (!rec) {
609 rec = ftrace_profile_alloc(stat, ip);
610 if (!rec)
611 goto out;
614 rec->counter++;
615 out:
616 local_irq_restore(flags);
619 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
620 static int profile_graph_entry(struct ftrace_graph_ent *trace)
622 function_profile_call(trace->func, 0);
623 return 1;
626 static void profile_graph_return(struct ftrace_graph_ret *trace)
628 struct ftrace_profile_stat *stat;
629 unsigned long long calltime;
630 struct ftrace_profile *rec;
631 unsigned long flags;
633 local_irq_save(flags);
634 stat = &__get_cpu_var(ftrace_profile_stats);
635 if (!stat->hash || !ftrace_profile_enabled)
636 goto out;
638 calltime = trace->rettime - trace->calltime;
640 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
641 int index;
643 index = trace->depth;
645 /* Append this call time to the parent time to subtract */
646 if (index)
647 current->ret_stack[index - 1].subtime += calltime;
649 if (current->ret_stack[index].subtime < calltime)
650 calltime -= current->ret_stack[index].subtime;
651 else
652 calltime = 0;
655 rec = ftrace_find_profiled_func(stat, trace->func);
656 if (rec)
657 rec->time += calltime;
659 out:
660 local_irq_restore(flags);
663 static int register_ftrace_profiler(void)
665 return register_ftrace_graph(&profile_graph_return,
666 &profile_graph_entry);
669 static void unregister_ftrace_profiler(void)
671 unregister_ftrace_graph();
673 #else
674 static struct ftrace_ops ftrace_profile_ops __read_mostly =
676 .func = function_profile_call,
679 static int register_ftrace_profiler(void)
681 return register_ftrace_function(&ftrace_profile_ops);
684 static void unregister_ftrace_profiler(void)
686 unregister_ftrace_function(&ftrace_profile_ops);
688 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
690 static ssize_t
691 ftrace_profile_write(struct file *filp, const char __user *ubuf,
692 size_t cnt, loff_t *ppos)
694 unsigned long val;
695 char buf[64]; /* big enough to hold a number */
696 int ret;
698 if (cnt >= sizeof(buf))
699 return -EINVAL;
701 if (copy_from_user(&buf, ubuf, cnt))
702 return -EFAULT;
704 buf[cnt] = 0;
706 ret = strict_strtoul(buf, 10, &val);
707 if (ret < 0)
708 return ret;
710 val = !!val;
712 mutex_lock(&ftrace_profile_lock);
713 if (ftrace_profile_enabled ^ val) {
714 if (val) {
715 ret = ftrace_profile_init();
716 if (ret < 0) {
717 cnt = ret;
718 goto out;
721 ret = register_ftrace_profiler();
722 if (ret < 0) {
723 cnt = ret;
724 goto out;
726 ftrace_profile_enabled = 1;
727 } else {
728 ftrace_profile_enabled = 0;
730 * unregister_ftrace_profiler calls stop_machine
731 * so this acts like an synchronize_sched.
733 unregister_ftrace_profiler();
736 out:
737 mutex_unlock(&ftrace_profile_lock);
739 filp->f_pos += cnt;
741 return cnt;
744 static ssize_t
745 ftrace_profile_read(struct file *filp, char __user *ubuf,
746 size_t cnt, loff_t *ppos)
748 char buf[64]; /* big enough to hold a number */
749 int r;
751 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
752 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
755 static const struct file_operations ftrace_profile_fops = {
756 .open = tracing_open_generic,
757 .read = ftrace_profile_read,
758 .write = ftrace_profile_write,
761 /* used to initialize the real stat files */
762 static struct tracer_stat function_stats __initdata = {
763 .name = "functions",
764 .stat_start = function_stat_start,
765 .stat_next = function_stat_next,
766 .stat_cmp = function_stat_cmp,
767 .stat_headers = function_stat_headers,
768 .stat_show = function_stat_show
771 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
773 struct ftrace_profile_stat *stat;
774 struct dentry *entry;
775 char *name;
776 int ret;
777 int cpu;
779 for_each_possible_cpu(cpu) {
780 stat = &per_cpu(ftrace_profile_stats, cpu);
782 /* allocate enough for function name + cpu number */
783 name = kmalloc(32, GFP_KERNEL);
784 if (!name) {
786 * The files created are permanent, if something happens
787 * we still do not free memory.
789 WARN(1,
790 "Could not allocate stat file for cpu %d\n",
791 cpu);
792 return;
794 stat->stat = function_stats;
795 snprintf(name, 32, "function%d", cpu);
796 stat->stat.name = name;
797 ret = register_stat_tracer(&stat->stat);
798 if (ret) {
799 WARN(1,
800 "Could not register function stat for cpu %d\n",
801 cpu);
802 kfree(name);
803 return;
807 entry = debugfs_create_file("function_profile_enabled", 0644,
808 d_tracer, NULL, &ftrace_profile_fops);
809 if (!entry)
810 pr_warning("Could not create debugfs "
811 "'function_profile_enabled' entry\n");
814 #else /* CONFIG_FUNCTION_PROFILER */
815 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
818 #endif /* CONFIG_FUNCTION_PROFILER */
820 /* set when tracing only a pid */
821 struct pid *ftrace_pid_trace;
822 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
824 #ifdef CONFIG_DYNAMIC_FTRACE
826 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
827 # error Dynamic ftrace depends on MCOUNT_RECORD
828 #endif
830 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
832 struct ftrace_func_probe {
833 struct hlist_node node;
834 struct ftrace_probe_ops *ops;
835 unsigned long flags;
836 unsigned long ip;
837 void *data;
838 struct rcu_head rcu;
841 enum {
842 FTRACE_ENABLE_CALLS = (1 << 0),
843 FTRACE_DISABLE_CALLS = (1 << 1),
844 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
845 FTRACE_ENABLE_MCOUNT = (1 << 3),
846 FTRACE_DISABLE_MCOUNT = (1 << 4),
847 FTRACE_START_FUNC_RET = (1 << 5),
848 FTRACE_STOP_FUNC_RET = (1 << 6),
851 static int ftrace_filtered;
853 static struct dyn_ftrace *ftrace_new_addrs;
855 static DEFINE_MUTEX(ftrace_regex_lock);
857 struct ftrace_page {
858 struct ftrace_page *next;
859 int index;
860 struct dyn_ftrace records[];
863 #define ENTRIES_PER_PAGE \
864 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
866 /* estimate from running different kernels */
867 #define NR_TO_INIT 10000
869 static struct ftrace_page *ftrace_pages_start;
870 static struct ftrace_page *ftrace_pages;
872 static struct dyn_ftrace *ftrace_free_records;
875 * This is a double for. Do not use 'break' to break out of the loop,
876 * you must use a goto.
878 #define do_for_each_ftrace_rec(pg, rec) \
879 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
880 int _____i; \
881 for (_____i = 0; _____i < pg->index; _____i++) { \
882 rec = &pg->records[_____i];
884 #define while_for_each_ftrace_rec() \
888 #ifdef CONFIG_KPROBES
890 static int frozen_record_count;
892 static inline void freeze_record(struct dyn_ftrace *rec)
894 if (!(rec->flags & FTRACE_FL_FROZEN)) {
895 rec->flags |= FTRACE_FL_FROZEN;
896 frozen_record_count++;
900 static inline void unfreeze_record(struct dyn_ftrace *rec)
902 if (rec->flags & FTRACE_FL_FROZEN) {
903 rec->flags &= ~FTRACE_FL_FROZEN;
904 frozen_record_count--;
908 static inline int record_frozen(struct dyn_ftrace *rec)
910 return rec->flags & FTRACE_FL_FROZEN;
912 #else
913 # define freeze_record(rec) ({ 0; })
914 # define unfreeze_record(rec) ({ 0; })
915 # define record_frozen(rec) ({ 0; })
916 #endif /* CONFIG_KPROBES */
918 static void ftrace_free_rec(struct dyn_ftrace *rec)
920 rec->freelist = ftrace_free_records;
921 ftrace_free_records = rec;
922 rec->flags |= FTRACE_FL_FREE;
925 static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
927 struct dyn_ftrace *rec;
929 /* First check for freed records */
930 if (ftrace_free_records) {
931 rec = ftrace_free_records;
933 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
934 FTRACE_WARN_ON_ONCE(1);
935 ftrace_free_records = NULL;
936 return NULL;
939 ftrace_free_records = rec->freelist;
940 memset(rec, 0, sizeof(*rec));
941 return rec;
944 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
945 if (!ftrace_pages->next) {
946 /* allocate another page */
947 ftrace_pages->next =
948 (void *)get_zeroed_page(GFP_KERNEL);
949 if (!ftrace_pages->next)
950 return NULL;
952 ftrace_pages = ftrace_pages->next;
955 return &ftrace_pages->records[ftrace_pages->index++];
958 static struct dyn_ftrace *
959 ftrace_record_ip(unsigned long ip)
961 struct dyn_ftrace *rec;
963 if (ftrace_disabled)
964 return NULL;
966 rec = ftrace_alloc_dyn_node(ip);
967 if (!rec)
968 return NULL;
970 rec->ip = ip;
971 rec->newlist = ftrace_new_addrs;
972 ftrace_new_addrs = rec;
974 return rec;
977 static void print_ip_ins(const char *fmt, unsigned char *p)
979 int i;
981 printk(KERN_CONT "%s", fmt);
983 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
984 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
987 static void ftrace_bug(int failed, unsigned long ip)
989 switch (failed) {
990 case -EFAULT:
991 FTRACE_WARN_ON_ONCE(1);
992 pr_info("ftrace faulted on modifying ");
993 print_ip_sym(ip);
994 break;
995 case -EINVAL:
996 FTRACE_WARN_ON_ONCE(1);
997 pr_info("ftrace failed to modify ");
998 print_ip_sym(ip);
999 print_ip_ins(" actual: ", (unsigned char *)ip);
1000 printk(KERN_CONT "\n");
1001 break;
1002 case -EPERM:
1003 FTRACE_WARN_ON_ONCE(1);
1004 pr_info("ftrace faulted on writing ");
1005 print_ip_sym(ip);
1006 break;
1007 default:
1008 FTRACE_WARN_ON_ONCE(1);
1009 pr_info("ftrace faulted on unknown error ");
1010 print_ip_sym(ip);
1015 static int
1016 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1018 unsigned long ftrace_addr;
1019 unsigned long flag = 0UL;
1021 ftrace_addr = (unsigned long)FTRACE_ADDR;
1024 * If this record is not to be traced or we want to disable it,
1025 * then disable it.
1027 * If we want to enable it and filtering is off, then enable it.
1029 * If we want to enable it and filtering is on, enable it only if
1030 * it's filtered
1032 if (enable && !(rec->flags & FTRACE_FL_NOTRACE)) {
1033 if (!ftrace_filtered || (rec->flags & FTRACE_FL_FILTER))
1034 flag = FTRACE_FL_ENABLED;
1037 /* If the state of this record hasn't changed, then do nothing */
1038 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1039 return 0;
1041 if (flag) {
1042 rec->flags |= FTRACE_FL_ENABLED;
1043 return ftrace_make_call(rec, ftrace_addr);
1046 rec->flags &= ~FTRACE_FL_ENABLED;
1047 return ftrace_make_nop(NULL, rec, ftrace_addr);
1050 static void ftrace_replace_code(int enable)
1052 struct dyn_ftrace *rec;
1053 struct ftrace_page *pg;
1054 int failed;
1056 do_for_each_ftrace_rec(pg, rec) {
1058 * Skip over free records, records that have
1059 * failed and not converted.
1061 if (rec->flags & FTRACE_FL_FREE ||
1062 rec->flags & FTRACE_FL_FAILED ||
1063 !(rec->flags & FTRACE_FL_CONVERTED))
1064 continue;
1066 /* ignore updates to this record's mcount site */
1067 if (get_kprobe((void *)rec->ip)) {
1068 freeze_record(rec);
1069 continue;
1070 } else {
1071 unfreeze_record(rec);
1074 failed = __ftrace_replace_code(rec, enable);
1075 if (failed) {
1076 rec->flags |= FTRACE_FL_FAILED;
1077 if ((system_state == SYSTEM_BOOTING) ||
1078 !core_kernel_text(rec->ip)) {
1079 ftrace_free_rec(rec);
1080 } else {
1081 ftrace_bug(failed, rec->ip);
1082 /* Stop processing */
1083 return;
1086 } while_for_each_ftrace_rec();
1089 static int
1090 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
1092 unsigned long ip;
1093 int ret;
1095 ip = rec->ip;
1097 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
1098 if (ret) {
1099 ftrace_bug(ret, ip);
1100 rec->flags |= FTRACE_FL_FAILED;
1101 return 0;
1103 return 1;
1107 * archs can override this function if they must do something
1108 * before the modifying code is performed.
1110 int __weak ftrace_arch_code_modify_prepare(void)
1112 return 0;
1116 * archs can override this function if they must do something
1117 * after the modifying code is performed.
1119 int __weak ftrace_arch_code_modify_post_process(void)
1121 return 0;
1124 static int __ftrace_modify_code(void *data)
1126 int *command = data;
1128 if (*command & FTRACE_ENABLE_CALLS)
1129 ftrace_replace_code(1);
1130 else if (*command & FTRACE_DISABLE_CALLS)
1131 ftrace_replace_code(0);
1133 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1134 ftrace_update_ftrace_func(ftrace_trace_function);
1136 if (*command & FTRACE_START_FUNC_RET)
1137 ftrace_enable_ftrace_graph_caller();
1138 else if (*command & FTRACE_STOP_FUNC_RET)
1139 ftrace_disable_ftrace_graph_caller();
1141 return 0;
1144 static void ftrace_run_update_code(int command)
1146 int ret;
1148 ret = ftrace_arch_code_modify_prepare();
1149 FTRACE_WARN_ON(ret);
1150 if (ret)
1151 return;
1153 stop_machine(__ftrace_modify_code, &command, NULL);
1155 ret = ftrace_arch_code_modify_post_process();
1156 FTRACE_WARN_ON(ret);
1159 static ftrace_func_t saved_ftrace_func;
1160 static int ftrace_start_up;
1162 static void ftrace_startup_enable(int command)
1164 if (saved_ftrace_func != ftrace_trace_function) {
1165 saved_ftrace_func = ftrace_trace_function;
1166 command |= FTRACE_UPDATE_TRACE_FUNC;
1169 if (!command || !ftrace_enabled)
1170 return;
1172 ftrace_run_update_code(command);
1175 static void ftrace_startup(int command)
1177 if (unlikely(ftrace_disabled))
1178 return;
1180 ftrace_start_up++;
1181 command |= FTRACE_ENABLE_CALLS;
1183 ftrace_startup_enable(command);
1186 static void ftrace_shutdown(int command)
1188 if (unlikely(ftrace_disabled))
1189 return;
1191 ftrace_start_up--;
1193 * Just warn in case of unbalance, no need to kill ftrace, it's not
1194 * critical but the ftrace_call callers may be never nopped again after
1195 * further ftrace uses.
1197 WARN_ON_ONCE(ftrace_start_up < 0);
1199 if (!ftrace_start_up)
1200 command |= FTRACE_DISABLE_CALLS;
1202 if (saved_ftrace_func != ftrace_trace_function) {
1203 saved_ftrace_func = ftrace_trace_function;
1204 command |= FTRACE_UPDATE_TRACE_FUNC;
1207 if (!command || !ftrace_enabled)
1208 return;
1210 ftrace_run_update_code(command);
1213 static void ftrace_startup_sysctl(void)
1215 int command = FTRACE_ENABLE_MCOUNT;
1217 if (unlikely(ftrace_disabled))
1218 return;
1220 /* Force update next time */
1221 saved_ftrace_func = NULL;
1222 /* ftrace_start_up is true if we want ftrace running */
1223 if (ftrace_start_up)
1224 command |= FTRACE_ENABLE_CALLS;
1226 ftrace_run_update_code(command);
1229 static void ftrace_shutdown_sysctl(void)
1231 int command = FTRACE_DISABLE_MCOUNT;
1233 if (unlikely(ftrace_disabled))
1234 return;
1236 /* ftrace_start_up is true if ftrace is running */
1237 if (ftrace_start_up)
1238 command |= FTRACE_DISABLE_CALLS;
1240 ftrace_run_update_code(command);
1243 static cycle_t ftrace_update_time;
1244 static unsigned long ftrace_update_cnt;
1245 unsigned long ftrace_update_tot_cnt;
1247 static int ftrace_update_code(struct module *mod)
1249 struct dyn_ftrace *p;
1250 cycle_t start, stop;
1252 start = ftrace_now(raw_smp_processor_id());
1253 ftrace_update_cnt = 0;
1255 while (ftrace_new_addrs) {
1257 /* If something went wrong, bail without enabling anything */
1258 if (unlikely(ftrace_disabled))
1259 return -1;
1261 p = ftrace_new_addrs;
1262 ftrace_new_addrs = p->newlist;
1263 p->flags = 0L;
1265 /* convert record (i.e, patch mcount-call with NOP) */
1266 if (ftrace_code_disable(mod, p)) {
1267 p->flags |= FTRACE_FL_CONVERTED;
1268 ftrace_update_cnt++;
1269 } else
1270 ftrace_free_rec(p);
1273 stop = ftrace_now(raw_smp_processor_id());
1274 ftrace_update_time = stop - start;
1275 ftrace_update_tot_cnt += ftrace_update_cnt;
1277 return 0;
1280 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
1282 struct ftrace_page *pg;
1283 int cnt;
1284 int i;
1286 /* allocate a few pages */
1287 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1288 if (!ftrace_pages_start)
1289 return -1;
1292 * Allocate a few more pages.
1294 * TODO: have some parser search vmlinux before
1295 * final linking to find all calls to ftrace.
1296 * Then we can:
1297 * a) know how many pages to allocate.
1298 * and/or
1299 * b) set up the table then.
1301 * The dynamic code is still necessary for
1302 * modules.
1305 pg = ftrace_pages = ftrace_pages_start;
1307 cnt = num_to_init / ENTRIES_PER_PAGE;
1308 pr_info("ftrace: allocating %ld entries in %d pages\n",
1309 num_to_init, cnt + 1);
1311 for (i = 0; i < cnt; i++) {
1312 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1314 /* If we fail, we'll try later anyway */
1315 if (!pg->next)
1316 break;
1318 pg = pg->next;
1321 return 0;
1324 enum {
1325 FTRACE_ITER_FILTER = (1 << 0),
1326 FTRACE_ITER_NOTRACE = (1 << 1),
1327 FTRACE_ITER_FAILURES = (1 << 2),
1328 FTRACE_ITER_PRINTALL = (1 << 3),
1329 FTRACE_ITER_HASH = (1 << 4),
1332 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1334 struct ftrace_iterator {
1335 struct ftrace_page *pg;
1336 int hidx;
1337 int idx;
1338 unsigned flags;
1339 struct trace_parser parser;
1342 static void *
1343 t_hash_next(struct seq_file *m, void *v, loff_t *pos)
1345 struct ftrace_iterator *iter = m->private;
1346 struct hlist_node *hnd = v;
1347 struct hlist_head *hhd;
1349 WARN_ON(!(iter->flags & FTRACE_ITER_HASH));
1351 (*pos)++;
1353 retry:
1354 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1355 return NULL;
1357 hhd = &ftrace_func_hash[iter->hidx];
1359 if (hlist_empty(hhd)) {
1360 iter->hidx++;
1361 hnd = NULL;
1362 goto retry;
1365 if (!hnd)
1366 hnd = hhd->first;
1367 else {
1368 hnd = hnd->next;
1369 if (!hnd) {
1370 iter->hidx++;
1371 goto retry;
1375 return hnd;
1378 static void *t_hash_start(struct seq_file *m, loff_t *pos)
1380 struct ftrace_iterator *iter = m->private;
1381 void *p = NULL;
1382 loff_t l;
1384 if (!(iter->flags & FTRACE_ITER_HASH))
1385 *pos = 0;
1387 iter->flags |= FTRACE_ITER_HASH;
1389 iter->hidx = 0;
1390 for (l = 0; l <= *pos; ) {
1391 p = t_hash_next(m, p, &l);
1392 if (!p)
1393 break;
1395 return p;
1398 static int t_hash_show(struct seq_file *m, void *v)
1400 struct ftrace_func_probe *rec;
1401 struct hlist_node *hnd = v;
1403 rec = hlist_entry(hnd, struct ftrace_func_probe, node);
1405 if (rec->ops->print)
1406 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1408 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
1410 if (rec->data)
1411 seq_printf(m, ":%p", rec->data);
1412 seq_putc(m, '\n');
1414 return 0;
1417 static void *
1418 t_next(struct seq_file *m, void *v, loff_t *pos)
1420 struct ftrace_iterator *iter = m->private;
1421 struct dyn_ftrace *rec = NULL;
1423 if (iter->flags & FTRACE_ITER_HASH)
1424 return t_hash_next(m, v, pos);
1426 (*pos)++;
1428 if (iter->flags & FTRACE_ITER_PRINTALL)
1429 return NULL;
1431 retry:
1432 if (iter->idx >= iter->pg->index) {
1433 if (iter->pg->next) {
1434 iter->pg = iter->pg->next;
1435 iter->idx = 0;
1436 goto retry;
1438 } else {
1439 rec = &iter->pg->records[iter->idx++];
1440 if ((rec->flags & FTRACE_FL_FREE) ||
1442 (!(iter->flags & FTRACE_ITER_FAILURES) &&
1443 (rec->flags & FTRACE_FL_FAILED)) ||
1445 ((iter->flags & FTRACE_ITER_FAILURES) &&
1446 !(rec->flags & FTRACE_FL_FAILED)) ||
1448 ((iter->flags & FTRACE_ITER_FILTER) &&
1449 !(rec->flags & FTRACE_FL_FILTER)) ||
1451 ((iter->flags & FTRACE_ITER_NOTRACE) &&
1452 !(rec->flags & FTRACE_FL_NOTRACE))) {
1453 rec = NULL;
1454 goto retry;
1458 return rec;
1461 static void *t_start(struct seq_file *m, loff_t *pos)
1463 struct ftrace_iterator *iter = m->private;
1464 void *p = NULL;
1465 loff_t l;
1467 mutex_lock(&ftrace_lock);
1469 * For set_ftrace_filter reading, if we have the filter
1470 * off, we can short cut and just print out that all
1471 * functions are enabled.
1473 if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
1474 if (*pos > 0)
1475 return t_hash_start(m, pos);
1476 iter->flags |= FTRACE_ITER_PRINTALL;
1477 return iter;
1480 if (iter->flags & FTRACE_ITER_HASH)
1481 return t_hash_start(m, pos);
1483 iter->pg = ftrace_pages_start;
1484 iter->idx = 0;
1485 for (l = 0; l <= *pos; ) {
1486 p = t_next(m, p, &l);
1487 if (!p)
1488 break;
1491 if (!p && iter->flags & FTRACE_ITER_FILTER)
1492 return t_hash_start(m, pos);
1494 return p;
1497 static void t_stop(struct seq_file *m, void *p)
1499 mutex_unlock(&ftrace_lock);
1502 static int t_show(struct seq_file *m, void *v)
1504 struct ftrace_iterator *iter = m->private;
1505 struct dyn_ftrace *rec = v;
1507 if (iter->flags & FTRACE_ITER_HASH)
1508 return t_hash_show(m, v);
1510 if (iter->flags & FTRACE_ITER_PRINTALL) {
1511 seq_printf(m, "#### all functions enabled ####\n");
1512 return 0;
1515 if (!rec)
1516 return 0;
1518 seq_printf(m, "%ps\n", (void *)rec->ip);
1520 return 0;
1523 static struct seq_operations show_ftrace_seq_ops = {
1524 .start = t_start,
1525 .next = t_next,
1526 .stop = t_stop,
1527 .show = t_show,
1530 static int
1531 ftrace_avail_open(struct inode *inode, struct file *file)
1533 struct ftrace_iterator *iter;
1534 int ret;
1536 if (unlikely(ftrace_disabled))
1537 return -ENODEV;
1539 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1540 if (!iter)
1541 return -ENOMEM;
1543 iter->pg = ftrace_pages_start;
1545 ret = seq_open(file, &show_ftrace_seq_ops);
1546 if (!ret) {
1547 struct seq_file *m = file->private_data;
1549 m->private = iter;
1550 } else {
1551 kfree(iter);
1554 return ret;
1557 static int
1558 ftrace_failures_open(struct inode *inode, struct file *file)
1560 int ret;
1561 struct seq_file *m;
1562 struct ftrace_iterator *iter;
1564 ret = ftrace_avail_open(inode, file);
1565 if (!ret) {
1566 m = (struct seq_file *)file->private_data;
1567 iter = (struct ftrace_iterator *)m->private;
1568 iter->flags = FTRACE_ITER_FAILURES;
1571 return ret;
1575 static void ftrace_filter_reset(int enable)
1577 struct ftrace_page *pg;
1578 struct dyn_ftrace *rec;
1579 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1581 mutex_lock(&ftrace_lock);
1582 if (enable)
1583 ftrace_filtered = 0;
1584 do_for_each_ftrace_rec(pg, rec) {
1585 if (rec->flags & FTRACE_FL_FAILED)
1586 continue;
1587 rec->flags &= ~type;
1588 } while_for_each_ftrace_rec();
1589 mutex_unlock(&ftrace_lock);
1592 static int
1593 ftrace_regex_open(struct inode *inode, struct file *file, int enable)
1595 struct ftrace_iterator *iter;
1596 int ret = 0;
1598 if (unlikely(ftrace_disabled))
1599 return -ENODEV;
1601 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1602 if (!iter)
1603 return -ENOMEM;
1605 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
1606 kfree(iter);
1607 return -ENOMEM;
1610 mutex_lock(&ftrace_regex_lock);
1611 if ((file->f_mode & FMODE_WRITE) &&
1612 (file->f_flags & O_TRUNC))
1613 ftrace_filter_reset(enable);
1615 if (file->f_mode & FMODE_READ) {
1616 iter->pg = ftrace_pages_start;
1617 iter->flags = enable ? FTRACE_ITER_FILTER :
1618 FTRACE_ITER_NOTRACE;
1620 ret = seq_open(file, &show_ftrace_seq_ops);
1621 if (!ret) {
1622 struct seq_file *m = file->private_data;
1623 m->private = iter;
1624 } else
1625 kfree(iter);
1626 } else
1627 file->private_data = iter;
1628 mutex_unlock(&ftrace_regex_lock);
1630 return ret;
1633 static int
1634 ftrace_filter_open(struct inode *inode, struct file *file)
1636 return ftrace_regex_open(inode, file, 1);
1639 static int
1640 ftrace_notrace_open(struct inode *inode, struct file *file)
1642 return ftrace_regex_open(inode, file, 0);
1645 static loff_t
1646 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
1648 loff_t ret;
1650 if (file->f_mode & FMODE_READ)
1651 ret = seq_lseek(file, offset, origin);
1652 else
1653 file->f_pos = ret = 1;
1655 return ret;
1658 enum {
1659 MATCH_FULL,
1660 MATCH_FRONT_ONLY,
1661 MATCH_MIDDLE_ONLY,
1662 MATCH_END_ONLY,
1666 * (static function - no need for kernel doc)
1668 * Pass in a buffer containing a glob and this function will
1669 * set search to point to the search part of the buffer and
1670 * return the type of search it is (see enum above).
1671 * This does modify buff.
1673 * Returns enum type.
1674 * search returns the pointer to use for comparison.
1675 * not returns 1 if buff started with a '!'
1676 * 0 otherwise.
1678 static int
1679 ftrace_setup_glob(char *buff, int len, char **search, int *not)
1681 int type = MATCH_FULL;
1682 int i;
1684 if (buff[0] == '!') {
1685 *not = 1;
1686 buff++;
1687 len--;
1688 } else
1689 *not = 0;
1691 *search = buff;
1693 for (i = 0; i < len; i++) {
1694 if (buff[i] == '*') {
1695 if (!i) {
1696 *search = buff + 1;
1697 type = MATCH_END_ONLY;
1698 } else {
1699 if (type == MATCH_END_ONLY)
1700 type = MATCH_MIDDLE_ONLY;
1701 else
1702 type = MATCH_FRONT_ONLY;
1703 buff[i] = 0;
1704 break;
1709 return type;
1712 static int ftrace_match(char *str, char *regex, int len, int type)
1714 int matched = 0;
1715 char *ptr;
1717 switch (type) {
1718 case MATCH_FULL:
1719 if (strcmp(str, regex) == 0)
1720 matched = 1;
1721 break;
1722 case MATCH_FRONT_ONLY:
1723 if (strncmp(str, regex, len) == 0)
1724 matched = 1;
1725 break;
1726 case MATCH_MIDDLE_ONLY:
1727 if (strstr(str, regex))
1728 matched = 1;
1729 break;
1730 case MATCH_END_ONLY:
1731 ptr = strstr(str, regex);
1732 if (ptr && (ptr[len] == 0))
1733 matched = 1;
1734 break;
1737 return matched;
1740 static int
1741 ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
1743 char str[KSYM_SYMBOL_LEN];
1745 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1746 return ftrace_match(str, regex, len, type);
1749 static void ftrace_match_records(char *buff, int len, int enable)
1751 unsigned int search_len;
1752 struct ftrace_page *pg;
1753 struct dyn_ftrace *rec;
1754 unsigned long flag;
1755 char *search;
1756 int type;
1757 int not;
1759 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1760 type = ftrace_setup_glob(buff, len, &search, &not);
1762 search_len = strlen(search);
1764 mutex_lock(&ftrace_lock);
1765 do_for_each_ftrace_rec(pg, rec) {
1767 if (rec->flags & FTRACE_FL_FAILED)
1768 continue;
1770 if (ftrace_match_record(rec, search, search_len, type)) {
1771 if (not)
1772 rec->flags &= ~flag;
1773 else
1774 rec->flags |= flag;
1777 * Only enable filtering if we have a function that
1778 * is filtered on.
1780 if (enable && (rec->flags & FTRACE_FL_FILTER))
1781 ftrace_filtered = 1;
1782 } while_for_each_ftrace_rec();
1783 mutex_unlock(&ftrace_lock);
1786 static int
1787 ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
1788 char *regex, int len, int type)
1790 char str[KSYM_SYMBOL_LEN];
1791 char *modname;
1793 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
1795 if (!modname || strcmp(modname, mod))
1796 return 0;
1798 /* blank search means to match all funcs in the mod */
1799 if (len)
1800 return ftrace_match(str, regex, len, type);
1801 else
1802 return 1;
1805 static void ftrace_match_module_records(char *buff, char *mod, int enable)
1807 unsigned search_len = 0;
1808 struct ftrace_page *pg;
1809 struct dyn_ftrace *rec;
1810 int type = MATCH_FULL;
1811 char *search = buff;
1812 unsigned long flag;
1813 int not = 0;
1815 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1817 /* blank or '*' mean the same */
1818 if (strcmp(buff, "*") == 0)
1819 buff[0] = 0;
1821 /* handle the case of 'dont filter this module' */
1822 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
1823 buff[0] = 0;
1824 not = 1;
1827 if (strlen(buff)) {
1828 type = ftrace_setup_glob(buff, strlen(buff), &search, &not);
1829 search_len = strlen(search);
1832 mutex_lock(&ftrace_lock);
1833 do_for_each_ftrace_rec(pg, rec) {
1835 if (rec->flags & FTRACE_FL_FAILED)
1836 continue;
1838 if (ftrace_match_module_record(rec, mod,
1839 search, search_len, type)) {
1840 if (not)
1841 rec->flags &= ~flag;
1842 else
1843 rec->flags |= flag;
1845 if (enable && (rec->flags & FTRACE_FL_FILTER))
1846 ftrace_filtered = 1;
1848 } while_for_each_ftrace_rec();
1849 mutex_unlock(&ftrace_lock);
1853 * We register the module command as a template to show others how
1854 * to register the a command as well.
1857 static int
1858 ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
1860 char *mod;
1863 * cmd == 'mod' because we only registered this func
1864 * for the 'mod' ftrace_func_command.
1865 * But if you register one func with multiple commands,
1866 * you can tell which command was used by the cmd
1867 * parameter.
1870 /* we must have a module name */
1871 if (!param)
1872 return -EINVAL;
1874 mod = strsep(&param, ":");
1875 if (!strlen(mod))
1876 return -EINVAL;
1878 ftrace_match_module_records(func, mod, enable);
1879 return 0;
1882 static struct ftrace_func_command ftrace_mod_cmd = {
1883 .name = "mod",
1884 .func = ftrace_mod_callback,
1887 static int __init ftrace_mod_cmd_init(void)
1889 return register_ftrace_command(&ftrace_mod_cmd);
1891 device_initcall(ftrace_mod_cmd_init);
1893 static void
1894 function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
1896 struct ftrace_func_probe *entry;
1897 struct hlist_head *hhd;
1898 struct hlist_node *n;
1899 unsigned long key;
1900 int resched;
1902 key = hash_long(ip, FTRACE_HASH_BITS);
1904 hhd = &ftrace_func_hash[key];
1906 if (hlist_empty(hhd))
1907 return;
1910 * Disable preemption for these calls to prevent a RCU grace
1911 * period. This syncs the hash iteration and freeing of items
1912 * on the hash. rcu_read_lock is too dangerous here.
1914 resched = ftrace_preempt_disable();
1915 hlist_for_each_entry_rcu(entry, n, hhd, node) {
1916 if (entry->ip == ip)
1917 entry->ops->func(ip, parent_ip, &entry->data);
1919 ftrace_preempt_enable(resched);
1922 static struct ftrace_ops trace_probe_ops __read_mostly =
1924 .func = function_trace_probe_call,
1927 static int ftrace_probe_registered;
1929 static void __enable_ftrace_function_probe(void)
1931 int i;
1933 if (ftrace_probe_registered)
1934 return;
1936 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1937 struct hlist_head *hhd = &ftrace_func_hash[i];
1938 if (hhd->first)
1939 break;
1941 /* Nothing registered? */
1942 if (i == FTRACE_FUNC_HASHSIZE)
1943 return;
1945 __register_ftrace_function(&trace_probe_ops);
1946 ftrace_startup(0);
1947 ftrace_probe_registered = 1;
1950 static void __disable_ftrace_function_probe(void)
1952 int i;
1954 if (!ftrace_probe_registered)
1955 return;
1957 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1958 struct hlist_head *hhd = &ftrace_func_hash[i];
1959 if (hhd->first)
1960 return;
1963 /* no more funcs left */
1964 __unregister_ftrace_function(&trace_probe_ops);
1965 ftrace_shutdown(0);
1966 ftrace_probe_registered = 0;
1970 static void ftrace_free_entry_rcu(struct rcu_head *rhp)
1972 struct ftrace_func_probe *entry =
1973 container_of(rhp, struct ftrace_func_probe, rcu);
1975 if (entry->ops->free)
1976 entry->ops->free(&entry->data);
1977 kfree(entry);
1982 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
1983 void *data)
1985 struct ftrace_func_probe *entry;
1986 struct ftrace_page *pg;
1987 struct dyn_ftrace *rec;
1988 int type, len, not;
1989 unsigned long key;
1990 int count = 0;
1991 char *search;
1993 type = ftrace_setup_glob(glob, strlen(glob), &search, &not);
1994 len = strlen(search);
1996 /* we do not support '!' for function probes */
1997 if (WARN_ON(not))
1998 return -EINVAL;
2000 mutex_lock(&ftrace_lock);
2001 do_for_each_ftrace_rec(pg, rec) {
2003 if (rec->flags & FTRACE_FL_FAILED)
2004 continue;
2006 if (!ftrace_match_record(rec, search, len, type))
2007 continue;
2009 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2010 if (!entry) {
2011 /* If we did not process any, then return error */
2012 if (!count)
2013 count = -ENOMEM;
2014 goto out_unlock;
2017 count++;
2019 entry->data = data;
2022 * The caller might want to do something special
2023 * for each function we find. We call the callback
2024 * to give the caller an opportunity to do so.
2026 if (ops->callback) {
2027 if (ops->callback(rec->ip, &entry->data) < 0) {
2028 /* caller does not like this func */
2029 kfree(entry);
2030 continue;
2034 entry->ops = ops;
2035 entry->ip = rec->ip;
2037 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2038 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2040 } while_for_each_ftrace_rec();
2041 __enable_ftrace_function_probe();
2043 out_unlock:
2044 mutex_unlock(&ftrace_lock);
2046 return count;
2049 enum {
2050 PROBE_TEST_FUNC = 1,
2051 PROBE_TEST_DATA = 2
2054 static void
2055 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2056 void *data, int flags)
2058 struct ftrace_func_probe *entry;
2059 struct hlist_node *n, *tmp;
2060 char str[KSYM_SYMBOL_LEN];
2061 int type = MATCH_FULL;
2062 int i, len = 0;
2063 char *search;
2065 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
2066 glob = NULL;
2067 else if (glob) {
2068 int not;
2070 type = ftrace_setup_glob(glob, strlen(glob), &search, &not);
2071 len = strlen(search);
2073 /* we do not support '!' for function probes */
2074 if (WARN_ON(not))
2075 return;
2078 mutex_lock(&ftrace_lock);
2079 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2080 struct hlist_head *hhd = &ftrace_func_hash[i];
2082 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2084 /* break up if statements for readability */
2085 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
2086 continue;
2088 if ((flags & PROBE_TEST_DATA) && entry->data != data)
2089 continue;
2091 /* do this last, since it is the most expensive */
2092 if (glob) {
2093 kallsyms_lookup(entry->ip, NULL, NULL,
2094 NULL, str);
2095 if (!ftrace_match(str, glob, len, type))
2096 continue;
2099 hlist_del(&entry->node);
2100 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2103 __disable_ftrace_function_probe();
2104 mutex_unlock(&ftrace_lock);
2107 void
2108 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2109 void *data)
2111 __unregister_ftrace_function_probe(glob, ops, data,
2112 PROBE_TEST_FUNC | PROBE_TEST_DATA);
2115 void
2116 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
2118 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
2121 void unregister_ftrace_function_probe_all(char *glob)
2123 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
2126 static LIST_HEAD(ftrace_commands);
2127 static DEFINE_MUTEX(ftrace_cmd_mutex);
2129 int register_ftrace_command(struct ftrace_func_command *cmd)
2131 struct ftrace_func_command *p;
2132 int ret = 0;
2134 mutex_lock(&ftrace_cmd_mutex);
2135 list_for_each_entry(p, &ftrace_commands, list) {
2136 if (strcmp(cmd->name, p->name) == 0) {
2137 ret = -EBUSY;
2138 goto out_unlock;
2141 list_add(&cmd->list, &ftrace_commands);
2142 out_unlock:
2143 mutex_unlock(&ftrace_cmd_mutex);
2145 return ret;
2148 int unregister_ftrace_command(struct ftrace_func_command *cmd)
2150 struct ftrace_func_command *p, *n;
2151 int ret = -ENODEV;
2153 mutex_lock(&ftrace_cmd_mutex);
2154 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2155 if (strcmp(cmd->name, p->name) == 0) {
2156 ret = 0;
2157 list_del_init(&p->list);
2158 goto out_unlock;
2161 out_unlock:
2162 mutex_unlock(&ftrace_cmd_mutex);
2164 return ret;
2167 static int ftrace_process_regex(char *buff, int len, int enable)
2169 char *func, *command, *next = buff;
2170 struct ftrace_func_command *p;
2171 int ret = -EINVAL;
2173 func = strsep(&next, ":");
2175 if (!next) {
2176 ftrace_match_records(func, len, enable);
2177 return 0;
2180 /* command found */
2182 command = strsep(&next, ":");
2184 mutex_lock(&ftrace_cmd_mutex);
2185 list_for_each_entry(p, &ftrace_commands, list) {
2186 if (strcmp(p->name, command) == 0) {
2187 ret = p->func(func, command, next, enable);
2188 goto out_unlock;
2191 out_unlock:
2192 mutex_unlock(&ftrace_cmd_mutex);
2194 return ret;
2197 static ssize_t
2198 ftrace_regex_write(struct file *file, const char __user *ubuf,
2199 size_t cnt, loff_t *ppos, int enable)
2201 struct ftrace_iterator *iter;
2202 struct trace_parser *parser;
2203 ssize_t ret, read;
2205 if (!cnt)
2206 return 0;
2208 mutex_lock(&ftrace_regex_lock);
2210 if (file->f_mode & FMODE_READ) {
2211 struct seq_file *m = file->private_data;
2212 iter = m->private;
2213 } else
2214 iter = file->private_data;
2216 parser = &iter->parser;
2217 read = trace_get_user(parser, ubuf, cnt, ppos);
2219 if (read >= 0 && trace_parser_loaded(parser) &&
2220 !trace_parser_cont(parser)) {
2221 ret = ftrace_process_regex(parser->buffer,
2222 parser->idx, enable);
2223 if (ret)
2224 goto out;
2226 trace_parser_clear(parser);
2229 ret = read;
2231 mutex_unlock(&ftrace_regex_lock);
2232 out:
2233 return ret;
2236 static ssize_t
2237 ftrace_filter_write(struct file *file, const char __user *ubuf,
2238 size_t cnt, loff_t *ppos)
2240 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2243 static ssize_t
2244 ftrace_notrace_write(struct file *file, const char __user *ubuf,
2245 size_t cnt, loff_t *ppos)
2247 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2250 static void
2251 ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
2253 if (unlikely(ftrace_disabled))
2254 return;
2256 mutex_lock(&ftrace_regex_lock);
2257 if (reset)
2258 ftrace_filter_reset(enable);
2259 if (buf)
2260 ftrace_match_records(buf, len, enable);
2261 mutex_unlock(&ftrace_regex_lock);
2265 * ftrace_set_filter - set a function to filter on in ftrace
2266 * @buf - the string that holds the function filter text.
2267 * @len - the length of the string.
2268 * @reset - non zero to reset all filters before applying this filter.
2270 * Filters denote which functions should be enabled when tracing is enabled.
2271 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2273 void ftrace_set_filter(unsigned char *buf, int len, int reset)
2275 ftrace_set_regex(buf, len, reset, 1);
2279 * ftrace_set_notrace - set a function to not trace in ftrace
2280 * @buf - the string that holds the function notrace text.
2281 * @len - the length of the string.
2282 * @reset - non zero to reset all filters before applying this filter.
2284 * Notrace Filters denote which functions should not be enabled when tracing
2285 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2286 * for tracing.
2288 void ftrace_set_notrace(unsigned char *buf, int len, int reset)
2290 ftrace_set_regex(buf, len, reset, 0);
2294 * command line interface to allow users to set filters on boot up.
2296 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2297 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
2298 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
2300 static int __init set_ftrace_notrace(char *str)
2302 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
2303 return 1;
2305 __setup("ftrace_notrace=", set_ftrace_notrace);
2307 static int __init set_ftrace_filter(char *str)
2309 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
2310 return 1;
2312 __setup("ftrace_filter=", set_ftrace_filter);
2314 static void __init set_ftrace_early_filter(char *buf, int enable)
2316 char *func;
2318 while (buf) {
2319 func = strsep(&buf, ",");
2320 ftrace_set_regex(func, strlen(func), 0, enable);
2324 static void __init set_ftrace_early_filters(void)
2326 if (ftrace_filter_buf[0])
2327 set_ftrace_early_filter(ftrace_filter_buf, 1);
2328 if (ftrace_notrace_buf[0])
2329 set_ftrace_early_filter(ftrace_notrace_buf, 0);
2332 static int
2333 ftrace_regex_release(struct inode *inode, struct file *file, int enable)
2335 struct seq_file *m = (struct seq_file *)file->private_data;
2336 struct ftrace_iterator *iter;
2337 struct trace_parser *parser;
2339 mutex_lock(&ftrace_regex_lock);
2340 if (file->f_mode & FMODE_READ) {
2341 iter = m->private;
2343 seq_release(inode, file);
2344 } else
2345 iter = file->private_data;
2347 parser = &iter->parser;
2348 if (trace_parser_loaded(parser)) {
2349 parser->buffer[parser->idx] = 0;
2350 ftrace_match_records(parser->buffer, parser->idx, enable);
2353 mutex_lock(&ftrace_lock);
2354 if (ftrace_start_up && ftrace_enabled)
2355 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
2356 mutex_unlock(&ftrace_lock);
2358 trace_parser_put(parser);
2359 kfree(iter);
2361 mutex_unlock(&ftrace_regex_lock);
2362 return 0;
2365 static int
2366 ftrace_filter_release(struct inode *inode, struct file *file)
2368 return ftrace_regex_release(inode, file, 1);
2371 static int
2372 ftrace_notrace_release(struct inode *inode, struct file *file)
2374 return ftrace_regex_release(inode, file, 0);
2377 static const struct file_operations ftrace_avail_fops = {
2378 .open = ftrace_avail_open,
2379 .read = seq_read,
2380 .llseek = seq_lseek,
2381 .release = seq_release_private,
2384 static const struct file_operations ftrace_failures_fops = {
2385 .open = ftrace_failures_open,
2386 .read = seq_read,
2387 .llseek = seq_lseek,
2388 .release = seq_release_private,
2391 static const struct file_operations ftrace_filter_fops = {
2392 .open = ftrace_filter_open,
2393 .read = seq_read,
2394 .write = ftrace_filter_write,
2395 .llseek = ftrace_regex_lseek,
2396 .release = ftrace_filter_release,
2399 static const struct file_operations ftrace_notrace_fops = {
2400 .open = ftrace_notrace_open,
2401 .read = seq_read,
2402 .write = ftrace_notrace_write,
2403 .llseek = ftrace_regex_lseek,
2404 .release = ftrace_notrace_release,
2407 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2409 static DEFINE_MUTEX(graph_lock);
2411 int ftrace_graph_count;
2412 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
2414 static void *
2415 __g_next(struct seq_file *m, loff_t *pos)
2417 if (*pos >= ftrace_graph_count)
2418 return NULL;
2419 return &ftrace_graph_funcs[*pos];
2422 static void *
2423 g_next(struct seq_file *m, void *v, loff_t *pos)
2425 (*pos)++;
2426 return __g_next(m, pos);
2429 static void *g_start(struct seq_file *m, loff_t *pos)
2431 mutex_lock(&graph_lock);
2433 /* Nothing, tell g_show to print all functions are enabled */
2434 if (!ftrace_graph_count && !*pos)
2435 return (void *)1;
2437 return __g_next(m, pos);
2440 static void g_stop(struct seq_file *m, void *p)
2442 mutex_unlock(&graph_lock);
2445 static int g_show(struct seq_file *m, void *v)
2447 unsigned long *ptr = v;
2449 if (!ptr)
2450 return 0;
2452 if (ptr == (unsigned long *)1) {
2453 seq_printf(m, "#### all functions enabled ####\n");
2454 return 0;
2457 seq_printf(m, "%ps\n", (void *)*ptr);
2459 return 0;
2462 static struct seq_operations ftrace_graph_seq_ops = {
2463 .start = g_start,
2464 .next = g_next,
2465 .stop = g_stop,
2466 .show = g_show,
2469 static int
2470 ftrace_graph_open(struct inode *inode, struct file *file)
2472 int ret = 0;
2474 if (unlikely(ftrace_disabled))
2475 return -ENODEV;
2477 mutex_lock(&graph_lock);
2478 if ((file->f_mode & FMODE_WRITE) &&
2479 (file->f_flags & O_TRUNC)) {
2480 ftrace_graph_count = 0;
2481 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
2483 mutex_unlock(&graph_lock);
2485 if (file->f_mode & FMODE_READ)
2486 ret = seq_open(file, &ftrace_graph_seq_ops);
2488 return ret;
2491 static int
2492 ftrace_graph_release(struct inode *inode, struct file *file)
2494 if (file->f_mode & FMODE_READ)
2495 seq_release(inode, file);
2496 return 0;
2499 static int
2500 ftrace_set_func(unsigned long *array, int *idx, char *buffer)
2502 struct dyn_ftrace *rec;
2503 struct ftrace_page *pg;
2504 int search_len;
2505 int found = 0;
2506 int type, not;
2507 char *search;
2508 bool exists;
2509 int i;
2511 if (ftrace_disabled)
2512 return -ENODEV;
2514 /* decode regex */
2515 type = ftrace_setup_glob(buffer, strlen(buffer), &search, &not);
2516 if (not)
2517 return -EINVAL;
2519 search_len = strlen(search);
2521 mutex_lock(&ftrace_lock);
2522 do_for_each_ftrace_rec(pg, rec) {
2524 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
2525 break;
2527 if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
2528 continue;
2530 if (ftrace_match_record(rec, search, search_len, type)) {
2531 /* ensure it is not already in the array */
2532 exists = false;
2533 for (i = 0; i < *idx; i++)
2534 if (array[i] == rec->ip) {
2535 exists = true;
2536 break;
2538 if (!exists) {
2539 array[(*idx)++] = rec->ip;
2540 found = 1;
2543 } while_for_each_ftrace_rec();
2545 mutex_unlock(&ftrace_lock);
2547 return found ? 0 : -EINVAL;
2550 static ssize_t
2551 ftrace_graph_write(struct file *file, const char __user *ubuf,
2552 size_t cnt, loff_t *ppos)
2554 struct trace_parser parser;
2555 ssize_t read, ret;
2557 if (!cnt || cnt < 0)
2558 return 0;
2560 mutex_lock(&graph_lock);
2562 if (ftrace_graph_count >= FTRACE_GRAPH_MAX_FUNCS) {
2563 ret = -EBUSY;
2564 goto out_unlock;
2567 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
2568 ret = -ENOMEM;
2569 goto out_unlock;
2572 read = trace_get_user(&parser, ubuf, cnt, ppos);
2574 if (read >= 0 && trace_parser_loaded((&parser))) {
2575 parser.buffer[parser.idx] = 0;
2577 /* we allow only one expression at a time */
2578 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
2579 parser.buffer);
2580 if (ret)
2581 goto out_free;
2584 ret = read;
2586 out_free:
2587 trace_parser_put(&parser);
2588 out_unlock:
2589 mutex_unlock(&graph_lock);
2591 return ret;
2594 static const struct file_operations ftrace_graph_fops = {
2595 .open = ftrace_graph_open,
2596 .read = seq_read,
2597 .write = ftrace_graph_write,
2598 .release = ftrace_graph_release,
2600 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2602 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
2605 trace_create_file("available_filter_functions", 0444,
2606 d_tracer, NULL, &ftrace_avail_fops);
2608 trace_create_file("failures", 0444,
2609 d_tracer, NULL, &ftrace_failures_fops);
2611 trace_create_file("set_ftrace_filter", 0644, d_tracer,
2612 NULL, &ftrace_filter_fops);
2614 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
2615 NULL, &ftrace_notrace_fops);
2617 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2618 trace_create_file("set_graph_function", 0444, d_tracer,
2619 NULL,
2620 &ftrace_graph_fops);
2621 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2623 return 0;
2626 static int ftrace_convert_nops(struct module *mod,
2627 unsigned long *start,
2628 unsigned long *end)
2630 unsigned long *p;
2631 unsigned long addr;
2632 unsigned long flags;
2634 mutex_lock(&ftrace_lock);
2635 p = start;
2636 while (p < end) {
2637 addr = ftrace_call_adjust(*p++);
2639 * Some architecture linkers will pad between
2640 * the different mcount_loc sections of different
2641 * object files to satisfy alignments.
2642 * Skip any NULL pointers.
2644 if (!addr)
2645 continue;
2646 ftrace_record_ip(addr);
2649 /* disable interrupts to prevent kstop machine */
2650 local_irq_save(flags);
2651 ftrace_update_code(mod);
2652 local_irq_restore(flags);
2653 mutex_unlock(&ftrace_lock);
2655 return 0;
2658 #ifdef CONFIG_MODULES
2659 void ftrace_release(void *start, void *end)
2661 struct dyn_ftrace *rec;
2662 struct ftrace_page *pg;
2663 unsigned long s = (unsigned long)start;
2664 unsigned long e = (unsigned long)end;
2666 if (ftrace_disabled || !start || start == end)
2667 return;
2669 mutex_lock(&ftrace_lock);
2670 do_for_each_ftrace_rec(pg, rec) {
2671 if ((rec->ip >= s) && (rec->ip < e)) {
2673 * rec->ip is changed in ftrace_free_rec()
2674 * It should not between s and e if record was freed.
2676 FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
2677 ftrace_free_rec(rec);
2679 } while_for_each_ftrace_rec();
2680 mutex_unlock(&ftrace_lock);
2683 static void ftrace_init_module(struct module *mod,
2684 unsigned long *start, unsigned long *end)
2686 if (ftrace_disabled || start == end)
2687 return;
2688 ftrace_convert_nops(mod, start, end);
2691 static int ftrace_module_notify(struct notifier_block *self,
2692 unsigned long val, void *data)
2694 struct module *mod = data;
2696 switch (val) {
2697 case MODULE_STATE_COMING:
2698 ftrace_init_module(mod, mod->ftrace_callsites,
2699 mod->ftrace_callsites +
2700 mod->num_ftrace_callsites);
2701 break;
2702 case MODULE_STATE_GOING:
2703 ftrace_release(mod->ftrace_callsites,
2704 mod->ftrace_callsites +
2705 mod->num_ftrace_callsites);
2706 break;
2709 return 0;
2711 #else
2712 static int ftrace_module_notify(struct notifier_block *self,
2713 unsigned long val, void *data)
2715 return 0;
2717 #endif /* CONFIG_MODULES */
2719 struct notifier_block ftrace_module_nb = {
2720 .notifier_call = ftrace_module_notify,
2721 .priority = 0,
2724 extern unsigned long __start_mcount_loc[];
2725 extern unsigned long __stop_mcount_loc[];
2727 void __init ftrace_init(void)
2729 unsigned long count, addr, flags;
2730 int ret;
2732 /* Keep the ftrace pointer to the stub */
2733 addr = (unsigned long)ftrace_stub;
2735 local_irq_save(flags);
2736 ftrace_dyn_arch_init(&addr);
2737 local_irq_restore(flags);
2739 /* ftrace_dyn_arch_init places the return code in addr */
2740 if (addr)
2741 goto failed;
2743 count = __stop_mcount_loc - __start_mcount_loc;
2745 ret = ftrace_dyn_table_alloc(count);
2746 if (ret)
2747 goto failed;
2749 last_ftrace_enabled = ftrace_enabled = 1;
2751 ret = ftrace_convert_nops(NULL,
2752 __start_mcount_loc,
2753 __stop_mcount_loc);
2755 ret = register_module_notifier(&ftrace_module_nb);
2756 if (ret)
2757 pr_warning("Failed to register trace ftrace module notifier\n");
2759 set_ftrace_early_filters();
2761 return;
2762 failed:
2763 ftrace_disabled = 1;
2766 #else
2768 static int __init ftrace_nodyn_init(void)
2770 ftrace_enabled = 1;
2771 return 0;
2773 device_initcall(ftrace_nodyn_init);
2775 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
2776 static inline void ftrace_startup_enable(int command) { }
2777 /* Keep as macros so we do not need to define the commands */
2778 # define ftrace_startup(command) do { } while (0)
2779 # define ftrace_shutdown(command) do { } while (0)
2780 # define ftrace_startup_sysctl() do { } while (0)
2781 # define ftrace_shutdown_sysctl() do { } while (0)
2782 #endif /* CONFIG_DYNAMIC_FTRACE */
2784 static ssize_t
2785 ftrace_pid_read(struct file *file, char __user *ubuf,
2786 size_t cnt, loff_t *ppos)
2788 char buf[64];
2789 int r;
2791 if (ftrace_pid_trace == ftrace_swapper_pid)
2792 r = sprintf(buf, "swapper tasks\n");
2793 else if (ftrace_pid_trace)
2794 r = sprintf(buf, "%u\n", pid_vnr(ftrace_pid_trace));
2795 else
2796 r = sprintf(buf, "no pid\n");
2798 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2801 static void clear_ftrace_swapper(void)
2803 struct task_struct *p;
2804 int cpu;
2806 get_online_cpus();
2807 for_each_online_cpu(cpu) {
2808 p = idle_task(cpu);
2809 clear_tsk_trace_trace(p);
2811 put_online_cpus();
2814 static void set_ftrace_swapper(void)
2816 struct task_struct *p;
2817 int cpu;
2819 get_online_cpus();
2820 for_each_online_cpu(cpu) {
2821 p = idle_task(cpu);
2822 set_tsk_trace_trace(p);
2824 put_online_cpus();
2827 static void clear_ftrace_pid(struct pid *pid)
2829 struct task_struct *p;
2831 rcu_read_lock();
2832 do_each_pid_task(pid, PIDTYPE_PID, p) {
2833 clear_tsk_trace_trace(p);
2834 } while_each_pid_task(pid, PIDTYPE_PID, p);
2835 rcu_read_unlock();
2837 put_pid(pid);
2840 static void set_ftrace_pid(struct pid *pid)
2842 struct task_struct *p;
2844 rcu_read_lock();
2845 do_each_pid_task(pid, PIDTYPE_PID, p) {
2846 set_tsk_trace_trace(p);
2847 } while_each_pid_task(pid, PIDTYPE_PID, p);
2848 rcu_read_unlock();
2851 static void clear_ftrace_pid_task(struct pid **pid)
2853 if (*pid == ftrace_swapper_pid)
2854 clear_ftrace_swapper();
2855 else
2856 clear_ftrace_pid(*pid);
2858 *pid = NULL;
2861 static void set_ftrace_pid_task(struct pid *pid)
2863 if (pid == ftrace_swapper_pid)
2864 set_ftrace_swapper();
2865 else
2866 set_ftrace_pid(pid);
2869 static ssize_t
2870 ftrace_pid_write(struct file *filp, const char __user *ubuf,
2871 size_t cnt, loff_t *ppos)
2873 struct pid *pid;
2874 char buf[64];
2875 long val;
2876 int ret;
2878 if (cnt >= sizeof(buf))
2879 return -EINVAL;
2881 if (copy_from_user(&buf, ubuf, cnt))
2882 return -EFAULT;
2884 buf[cnt] = 0;
2886 ret = strict_strtol(buf, 10, &val);
2887 if (ret < 0)
2888 return ret;
2890 mutex_lock(&ftrace_lock);
2891 if (val < 0) {
2892 /* disable pid tracing */
2893 if (!ftrace_pid_trace)
2894 goto out;
2896 clear_ftrace_pid_task(&ftrace_pid_trace);
2898 } else {
2899 /* swapper task is special */
2900 if (!val) {
2901 pid = ftrace_swapper_pid;
2902 if (pid == ftrace_pid_trace)
2903 goto out;
2904 } else {
2905 pid = find_get_pid(val);
2907 if (pid == ftrace_pid_trace) {
2908 put_pid(pid);
2909 goto out;
2913 if (ftrace_pid_trace)
2914 clear_ftrace_pid_task(&ftrace_pid_trace);
2916 if (!pid)
2917 goto out;
2919 ftrace_pid_trace = pid;
2921 set_ftrace_pid_task(ftrace_pid_trace);
2924 /* update the function call */
2925 ftrace_update_pid_func();
2926 ftrace_startup_enable(0);
2928 out:
2929 mutex_unlock(&ftrace_lock);
2931 return cnt;
2934 static const struct file_operations ftrace_pid_fops = {
2935 .read = ftrace_pid_read,
2936 .write = ftrace_pid_write,
2939 static __init int ftrace_init_debugfs(void)
2941 struct dentry *d_tracer;
2943 d_tracer = tracing_init_dentry();
2944 if (!d_tracer)
2945 return 0;
2947 ftrace_init_dyn_debugfs(d_tracer);
2949 trace_create_file("set_ftrace_pid", 0644, d_tracer,
2950 NULL, &ftrace_pid_fops);
2952 ftrace_profile_debugfs(d_tracer);
2954 return 0;
2956 fs_initcall(ftrace_init_debugfs);
2959 * ftrace_kill - kill ftrace
2961 * This function should be used by panic code. It stops ftrace
2962 * but in a not so nice way. If you need to simply kill ftrace
2963 * from a non-atomic section, use ftrace_kill.
2965 void ftrace_kill(void)
2967 ftrace_disabled = 1;
2968 ftrace_enabled = 0;
2969 clear_ftrace_function();
2973 * register_ftrace_function - register a function for profiling
2974 * @ops - ops structure that holds the function for profiling.
2976 * Register a function to be called by all functions in the
2977 * kernel.
2979 * Note: @ops->func and all the functions it calls must be labeled
2980 * with "notrace", otherwise it will go into a
2981 * recursive loop.
2983 int register_ftrace_function(struct ftrace_ops *ops)
2985 int ret;
2987 if (unlikely(ftrace_disabled))
2988 return -1;
2990 mutex_lock(&ftrace_lock);
2992 ret = __register_ftrace_function(ops);
2993 ftrace_startup(0);
2995 mutex_unlock(&ftrace_lock);
2996 return ret;
3000 * unregister_ftrace_function - unregister a function for profiling.
3001 * @ops - ops structure that holds the function to unregister
3003 * Unregister a function that was added to be called by ftrace profiling.
3005 int unregister_ftrace_function(struct ftrace_ops *ops)
3007 int ret;
3009 mutex_lock(&ftrace_lock);
3010 ret = __unregister_ftrace_function(ops);
3011 ftrace_shutdown(0);
3012 mutex_unlock(&ftrace_lock);
3014 return ret;
3018 ftrace_enable_sysctl(struct ctl_table *table, int write,
3019 struct file *file, void __user *buffer, size_t *lenp,
3020 loff_t *ppos)
3022 int ret;
3024 if (unlikely(ftrace_disabled))
3025 return -ENODEV;
3027 mutex_lock(&ftrace_lock);
3029 ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
3031 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
3032 goto out;
3034 last_ftrace_enabled = !!ftrace_enabled;
3036 if (ftrace_enabled) {
3038 ftrace_startup_sysctl();
3040 /* we are starting ftrace again */
3041 if (ftrace_list != &ftrace_list_end) {
3042 if (ftrace_list->next == &ftrace_list_end)
3043 ftrace_trace_function = ftrace_list->func;
3044 else
3045 ftrace_trace_function = ftrace_list_func;
3048 } else {
3049 /* stopping ftrace calls (just send to ftrace_stub) */
3050 ftrace_trace_function = ftrace_stub;
3052 ftrace_shutdown_sysctl();
3055 out:
3056 mutex_unlock(&ftrace_lock);
3057 return ret;
3060 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3062 static int ftrace_graph_active;
3063 static struct notifier_block ftrace_suspend_notifier;
3065 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3067 return 0;
3070 /* The callbacks that hook a function */
3071 trace_func_graph_ret_t ftrace_graph_return =
3072 (trace_func_graph_ret_t)ftrace_stub;
3073 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
3075 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3076 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3078 int i;
3079 int ret = 0;
3080 unsigned long flags;
3081 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3082 struct task_struct *g, *t;
3084 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3085 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3086 * sizeof(struct ftrace_ret_stack),
3087 GFP_KERNEL);
3088 if (!ret_stack_list[i]) {
3089 start = 0;
3090 end = i;
3091 ret = -ENOMEM;
3092 goto free;
3096 read_lock_irqsave(&tasklist_lock, flags);
3097 do_each_thread(g, t) {
3098 if (start == end) {
3099 ret = -EAGAIN;
3100 goto unlock;
3103 if (t->ret_stack == NULL) {
3104 atomic_set(&t->tracing_graph_pause, 0);
3105 atomic_set(&t->trace_overrun, 0);
3106 t->curr_ret_stack = -1;
3107 /* Make sure the tasks see the -1 first: */
3108 smp_wmb();
3109 t->ret_stack = ret_stack_list[start++];
3111 } while_each_thread(g, t);
3113 unlock:
3114 read_unlock_irqrestore(&tasklist_lock, flags);
3115 free:
3116 for (i = start; i < end; i++)
3117 kfree(ret_stack_list[i]);
3118 return ret;
3121 static void
3122 ftrace_graph_probe_sched_switch(struct rq *__rq, struct task_struct *prev,
3123 struct task_struct *next)
3125 unsigned long long timestamp;
3126 int index;
3129 * Does the user want to count the time a function was asleep.
3130 * If so, do not update the time stamps.
3132 if (trace_flags & TRACE_ITER_SLEEP_TIME)
3133 return;
3135 timestamp = trace_clock_local();
3137 prev->ftrace_timestamp = timestamp;
3139 /* only process tasks that we timestamped */
3140 if (!next->ftrace_timestamp)
3141 return;
3144 * Update all the counters in next to make up for the
3145 * time next was sleeping.
3147 timestamp -= next->ftrace_timestamp;
3149 for (index = next->curr_ret_stack; index >= 0; index--)
3150 next->ret_stack[index].calltime += timestamp;
3153 /* Allocate a return stack for each task */
3154 static int start_graph_tracing(void)
3156 struct ftrace_ret_stack **ret_stack_list;
3157 int ret, cpu;
3159 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
3160 sizeof(struct ftrace_ret_stack *),
3161 GFP_KERNEL);
3163 if (!ret_stack_list)
3164 return -ENOMEM;
3166 /* The cpu_boot init_task->ret_stack will never be freed */
3167 for_each_online_cpu(cpu) {
3168 if (!idle_task(cpu)->ret_stack)
3169 ftrace_graph_init_task(idle_task(cpu));
3172 do {
3173 ret = alloc_retstack_tasklist(ret_stack_list);
3174 } while (ret == -EAGAIN);
3176 if (!ret) {
3177 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch);
3178 if (ret)
3179 pr_info("ftrace_graph: Couldn't activate tracepoint"
3180 " probe to kernel_sched_switch\n");
3183 kfree(ret_stack_list);
3184 return ret;
3188 * Hibernation protection.
3189 * The state of the current task is too much unstable during
3190 * suspend/restore to disk. We want to protect against that.
3192 static int
3193 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
3194 void *unused)
3196 switch (state) {
3197 case PM_HIBERNATION_PREPARE:
3198 pause_graph_tracing();
3199 break;
3201 case PM_POST_HIBERNATION:
3202 unpause_graph_tracing();
3203 break;
3205 return NOTIFY_DONE;
3208 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
3209 trace_func_graph_ent_t entryfunc)
3211 int ret = 0;
3213 mutex_lock(&ftrace_lock);
3215 /* we currently allow only one tracer registered at a time */
3216 if (ftrace_graph_active) {
3217 ret = -EBUSY;
3218 goto out;
3221 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
3222 register_pm_notifier(&ftrace_suspend_notifier);
3224 ftrace_graph_active++;
3225 ret = start_graph_tracing();
3226 if (ret) {
3227 ftrace_graph_active--;
3228 goto out;
3231 ftrace_graph_return = retfunc;
3232 ftrace_graph_entry = entryfunc;
3234 ftrace_startup(FTRACE_START_FUNC_RET);
3236 out:
3237 mutex_unlock(&ftrace_lock);
3238 return ret;
3241 void unregister_ftrace_graph(void)
3243 mutex_lock(&ftrace_lock);
3245 if (unlikely(!ftrace_graph_active))
3246 goto out;
3248 ftrace_graph_active--;
3249 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch);
3250 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
3251 ftrace_graph_entry = ftrace_graph_entry_stub;
3252 ftrace_shutdown(FTRACE_STOP_FUNC_RET);
3253 unregister_pm_notifier(&ftrace_suspend_notifier);
3255 out:
3256 mutex_unlock(&ftrace_lock);
3259 /* Allocate a return stack for newly created task */
3260 void ftrace_graph_init_task(struct task_struct *t)
3262 /* Make sure we do not use the parent ret_stack */
3263 t->ret_stack = NULL;
3265 if (ftrace_graph_active) {
3266 struct ftrace_ret_stack *ret_stack;
3268 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
3269 * sizeof(struct ftrace_ret_stack),
3270 GFP_KERNEL);
3271 if (!ret_stack)
3272 return;
3273 t->curr_ret_stack = -1;
3274 atomic_set(&t->tracing_graph_pause, 0);
3275 atomic_set(&t->trace_overrun, 0);
3276 t->ftrace_timestamp = 0;
3277 /* make curr_ret_stack visable before we add the ret_stack */
3278 smp_wmb();
3279 t->ret_stack = ret_stack;
3283 void ftrace_graph_exit_task(struct task_struct *t)
3285 struct ftrace_ret_stack *ret_stack = t->ret_stack;
3287 t->ret_stack = NULL;
3288 /* NULL must become visible to IRQs before we free it: */
3289 barrier();
3291 kfree(ret_stack);
3294 void ftrace_graph_stop(void)
3296 ftrace_stop();
3298 #endif