rbd: allocate name separate from obj_request
[linux-2.6/btrfs-unstable.git] / kernel / trace / ftrace.c
blobb3fde6d7b7fc47683244a5a432ab35a543551499
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 Nadia Yvette Chambers
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/bsearch.h>
26 #include <linux/module.h>
27 #include <linux/ftrace.h>
28 #include <linux/sysctl.h>
29 #include <linux/slab.h>
30 #include <linux/ctype.h>
31 #include <linux/sort.h>
32 #include <linux/list.h>
33 #include <linux/hash.h>
34 #include <linux/rcupdate.h>
36 #include <trace/events/sched.h>
38 #include <asm/setup.h>
40 #include "trace_output.h"
41 #include "trace_stat.h"
43 #define FTRACE_WARN_ON(cond) \
44 ({ \
45 int ___r = cond; \
46 if (WARN_ON(___r)) \
47 ftrace_kill(); \
48 ___r; \
51 #define FTRACE_WARN_ON_ONCE(cond) \
52 ({ \
53 int ___r = cond; \
54 if (WARN_ON_ONCE(___r)) \
55 ftrace_kill(); \
56 ___r; \
59 /* hash bits for specific function selection */
60 #define FTRACE_HASH_BITS 7
61 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
62 #define FTRACE_HASH_DEFAULT_BITS 10
63 #define FTRACE_HASH_MAX_BITS 12
65 #define FL_GLOBAL_CONTROL_MASK (FTRACE_OPS_FL_GLOBAL | FTRACE_OPS_FL_CONTROL)
67 static struct ftrace_ops ftrace_list_end __read_mostly = {
68 .func = ftrace_stub,
69 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
72 /* ftrace_enabled is a method to turn ftrace on or off */
73 int ftrace_enabled __read_mostly;
74 static int last_ftrace_enabled;
76 /* Quick disabling of function tracer. */
77 int function_trace_stop __read_mostly;
79 /* Current function tracing op */
80 struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
82 /* List for set_ftrace_pid's pids. */
83 LIST_HEAD(ftrace_pids);
84 struct ftrace_pid {
85 struct list_head list;
86 struct pid *pid;
90 * ftrace_disabled is set when an anomaly is discovered.
91 * ftrace_disabled is much stronger than ftrace_enabled.
93 static int ftrace_disabled __read_mostly;
95 static DEFINE_MUTEX(ftrace_lock);
97 static struct ftrace_ops *ftrace_global_list __read_mostly = &ftrace_list_end;
98 static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end;
99 static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
100 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
101 ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
102 static struct ftrace_ops global_ops;
103 static struct ftrace_ops control_ops;
105 #if ARCH_SUPPORTS_FTRACE_OPS
106 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
107 struct ftrace_ops *op, struct pt_regs *regs);
108 #else
109 /* See comment below, where ftrace_ops_list_func is defined */
110 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
111 #define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
112 #endif
115 * Traverse the ftrace_global_list, invoking all entries. The reason that we
116 * can use rcu_dereference_raw() is that elements removed from this list
117 * are simply leaked, so there is no need to interact with a grace-period
118 * mechanism. The rcu_dereference_raw() calls are needed to handle
119 * concurrent insertions into the ftrace_global_list.
121 * Silly Alpha and silly pointer-speculation compiler optimizations!
123 #define do_for_each_ftrace_op(op, list) \
124 op = rcu_dereference_raw(list); \
128 * Optimized for just a single item in the list (as that is the normal case).
130 #define while_for_each_ftrace_op(op) \
131 while (likely(op = rcu_dereference_raw((op)->next)) && \
132 unlikely((op) != &ftrace_list_end))
135 * ftrace_nr_registered_ops - return number of ops registered
137 * Returns the number of ftrace_ops registered and tracing functions
139 int ftrace_nr_registered_ops(void)
141 struct ftrace_ops *ops;
142 int cnt = 0;
144 mutex_lock(&ftrace_lock);
146 for (ops = ftrace_ops_list;
147 ops != &ftrace_list_end; ops = ops->next)
148 cnt++;
150 mutex_unlock(&ftrace_lock);
152 return cnt;
155 static void
156 ftrace_global_list_func(unsigned long ip, unsigned long parent_ip,
157 struct ftrace_ops *op, struct pt_regs *regs)
159 int bit;
161 bit = trace_test_and_set_recursion(TRACE_GLOBAL_START, TRACE_GLOBAL_MAX);
162 if (bit < 0)
163 return;
165 do_for_each_ftrace_op(op, ftrace_global_list) {
166 op->func(ip, parent_ip, op, regs);
167 } while_for_each_ftrace_op(op);
169 trace_clear_recursion(bit);
172 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
173 struct ftrace_ops *op, struct pt_regs *regs)
175 if (!test_tsk_trace_trace(current))
176 return;
178 ftrace_pid_function(ip, parent_ip, op, regs);
181 static void set_ftrace_pid_function(ftrace_func_t func)
183 /* do not set ftrace_pid_function to itself! */
184 if (func != ftrace_pid_func)
185 ftrace_pid_function = func;
189 * clear_ftrace_function - reset the ftrace function
191 * This NULLs the ftrace function and in essence stops
192 * tracing. There may be lag
194 void clear_ftrace_function(void)
196 ftrace_trace_function = ftrace_stub;
197 ftrace_pid_function = ftrace_stub;
200 static void control_ops_disable_all(struct ftrace_ops *ops)
202 int cpu;
204 for_each_possible_cpu(cpu)
205 *per_cpu_ptr(ops->disabled, cpu) = 1;
208 static int control_ops_alloc(struct ftrace_ops *ops)
210 int __percpu *disabled;
212 disabled = alloc_percpu(int);
213 if (!disabled)
214 return -ENOMEM;
216 ops->disabled = disabled;
217 control_ops_disable_all(ops);
218 return 0;
221 static void control_ops_free(struct ftrace_ops *ops)
223 free_percpu(ops->disabled);
226 static void update_global_ops(void)
228 ftrace_func_t func;
231 * If there's only one function registered, then call that
232 * function directly. Otherwise, we need to iterate over the
233 * registered callers.
235 if (ftrace_global_list == &ftrace_list_end ||
236 ftrace_global_list->next == &ftrace_list_end) {
237 func = ftrace_global_list->func;
239 * As we are calling the function directly.
240 * If it does not have recursion protection,
241 * the function_trace_op needs to be updated
242 * accordingly.
244 if (ftrace_global_list->flags & FTRACE_OPS_FL_RECURSION_SAFE)
245 global_ops.flags |= FTRACE_OPS_FL_RECURSION_SAFE;
246 else
247 global_ops.flags &= ~FTRACE_OPS_FL_RECURSION_SAFE;
248 } else {
249 func = ftrace_global_list_func;
250 /* The list has its own recursion protection. */
251 global_ops.flags |= FTRACE_OPS_FL_RECURSION_SAFE;
255 /* If we filter on pids, update to use the pid function */
256 if (!list_empty(&ftrace_pids)) {
257 set_ftrace_pid_function(func);
258 func = ftrace_pid_func;
261 global_ops.func = func;
264 static void update_ftrace_function(void)
266 ftrace_func_t func;
268 update_global_ops();
271 * If we are at the end of the list and this ops is
272 * recursion safe and not dynamic and the arch supports passing ops,
273 * then have the mcount trampoline call the function directly.
275 if (ftrace_ops_list == &ftrace_list_end ||
276 (ftrace_ops_list->next == &ftrace_list_end &&
277 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC) &&
278 (ftrace_ops_list->flags & FTRACE_OPS_FL_RECURSION_SAFE) &&
279 !FTRACE_FORCE_LIST_FUNC)) {
280 /* Set the ftrace_ops that the arch callback uses */
281 if (ftrace_ops_list == &global_ops)
282 function_trace_op = ftrace_global_list;
283 else
284 function_trace_op = ftrace_ops_list;
285 func = ftrace_ops_list->func;
286 } else {
287 /* Just use the default ftrace_ops */
288 function_trace_op = &ftrace_list_end;
289 func = ftrace_ops_list_func;
292 ftrace_trace_function = func;
295 static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
297 ops->next = *list;
299 * We are entering ops into the list but another
300 * CPU might be walking that list. We need to make sure
301 * the ops->next pointer is valid before another CPU sees
302 * the ops pointer included into the list.
304 rcu_assign_pointer(*list, ops);
307 static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
309 struct ftrace_ops **p;
312 * If we are removing the last function, then simply point
313 * to the ftrace_stub.
315 if (*list == ops && ops->next == &ftrace_list_end) {
316 *list = &ftrace_list_end;
317 return 0;
320 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
321 if (*p == ops)
322 break;
324 if (*p != ops)
325 return -1;
327 *p = (*p)->next;
328 return 0;
331 static void add_ftrace_list_ops(struct ftrace_ops **list,
332 struct ftrace_ops *main_ops,
333 struct ftrace_ops *ops)
335 int first = *list == &ftrace_list_end;
336 add_ftrace_ops(list, ops);
337 if (first)
338 add_ftrace_ops(&ftrace_ops_list, main_ops);
341 static int remove_ftrace_list_ops(struct ftrace_ops **list,
342 struct ftrace_ops *main_ops,
343 struct ftrace_ops *ops)
345 int ret = remove_ftrace_ops(list, ops);
346 if (!ret && *list == &ftrace_list_end)
347 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
348 return ret;
351 static int __register_ftrace_function(struct ftrace_ops *ops)
353 if (unlikely(ftrace_disabled))
354 return -ENODEV;
356 if (FTRACE_WARN_ON(ops == &global_ops))
357 return -EINVAL;
359 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
360 return -EBUSY;
362 /* We don't support both control and global flags set. */
363 if ((ops->flags & FL_GLOBAL_CONTROL_MASK) == FL_GLOBAL_CONTROL_MASK)
364 return -EINVAL;
366 #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
368 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
369 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
370 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
372 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
373 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
374 return -EINVAL;
376 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
377 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
378 #endif
380 if (!core_kernel_data((unsigned long)ops))
381 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
383 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
384 add_ftrace_list_ops(&ftrace_global_list, &global_ops, ops);
385 ops->flags |= FTRACE_OPS_FL_ENABLED;
386 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
387 if (control_ops_alloc(ops))
388 return -ENOMEM;
389 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
390 } else
391 add_ftrace_ops(&ftrace_ops_list, ops);
393 if (ftrace_enabled)
394 update_ftrace_function();
396 return 0;
399 static int __unregister_ftrace_function(struct ftrace_ops *ops)
401 int ret;
403 if (ftrace_disabled)
404 return -ENODEV;
406 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
407 return -EBUSY;
409 if (FTRACE_WARN_ON(ops == &global_ops))
410 return -EINVAL;
412 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
413 ret = remove_ftrace_list_ops(&ftrace_global_list,
414 &global_ops, ops);
415 if (!ret)
416 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
417 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
418 ret = remove_ftrace_list_ops(&ftrace_control_list,
419 &control_ops, ops);
420 if (!ret) {
422 * The ftrace_ops is now removed from the list,
423 * so there'll be no new users. We must ensure
424 * all current users are done before we free
425 * the control data.
427 synchronize_sched();
428 control_ops_free(ops);
430 } else
431 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
433 if (ret < 0)
434 return ret;
436 if (ftrace_enabled)
437 update_ftrace_function();
440 * Dynamic ops may be freed, we must make sure that all
441 * callers are done before leaving this function.
443 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
444 synchronize_sched();
446 return 0;
449 static void ftrace_update_pid_func(void)
451 /* Only do something if we are tracing something */
452 if (ftrace_trace_function == ftrace_stub)
453 return;
455 update_ftrace_function();
458 #ifdef CONFIG_FUNCTION_PROFILER
459 struct ftrace_profile {
460 struct hlist_node node;
461 unsigned long ip;
462 unsigned long counter;
463 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
464 unsigned long long time;
465 unsigned long long time_squared;
466 #endif
469 struct ftrace_profile_page {
470 struct ftrace_profile_page *next;
471 unsigned long index;
472 struct ftrace_profile records[];
475 struct ftrace_profile_stat {
476 atomic_t disabled;
477 struct hlist_head *hash;
478 struct ftrace_profile_page *pages;
479 struct ftrace_profile_page *start;
480 struct tracer_stat stat;
483 #define PROFILE_RECORDS_SIZE \
484 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
486 #define PROFILES_PER_PAGE \
487 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
489 static int ftrace_profile_bits __read_mostly;
490 static int ftrace_profile_enabled __read_mostly;
492 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
493 static DEFINE_MUTEX(ftrace_profile_lock);
495 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
497 #define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
499 static void *
500 function_stat_next(void *v, int idx)
502 struct ftrace_profile *rec = v;
503 struct ftrace_profile_page *pg;
505 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
507 again:
508 if (idx != 0)
509 rec++;
511 if ((void *)rec >= (void *)&pg->records[pg->index]) {
512 pg = pg->next;
513 if (!pg)
514 return NULL;
515 rec = &pg->records[0];
516 if (!rec->counter)
517 goto again;
520 return rec;
523 static void *function_stat_start(struct tracer_stat *trace)
525 struct ftrace_profile_stat *stat =
526 container_of(trace, struct ftrace_profile_stat, stat);
528 if (!stat || !stat->start)
529 return NULL;
531 return function_stat_next(&stat->start->records[0], 0);
534 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
535 /* function graph compares on total time */
536 static int function_stat_cmp(void *p1, void *p2)
538 struct ftrace_profile *a = p1;
539 struct ftrace_profile *b = p2;
541 if (a->time < b->time)
542 return -1;
543 if (a->time > b->time)
544 return 1;
545 else
546 return 0;
548 #else
549 /* not function graph compares against hits */
550 static int function_stat_cmp(void *p1, void *p2)
552 struct ftrace_profile *a = p1;
553 struct ftrace_profile *b = p2;
555 if (a->counter < b->counter)
556 return -1;
557 if (a->counter > b->counter)
558 return 1;
559 else
560 return 0;
562 #endif
564 static int function_stat_headers(struct seq_file *m)
566 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
567 seq_printf(m, " Function "
568 "Hit Time Avg s^2\n"
569 " -------- "
570 "--- ---- --- ---\n");
571 #else
572 seq_printf(m, " Function Hit\n"
573 " -------- ---\n");
574 #endif
575 return 0;
578 static int function_stat_show(struct seq_file *m, void *v)
580 struct ftrace_profile *rec = v;
581 char str[KSYM_SYMBOL_LEN];
582 int ret = 0;
583 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
584 static struct trace_seq s;
585 unsigned long long avg;
586 unsigned long long stddev;
587 #endif
588 mutex_lock(&ftrace_profile_lock);
590 /* we raced with function_profile_reset() */
591 if (unlikely(rec->counter == 0)) {
592 ret = -EBUSY;
593 goto out;
596 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
597 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
599 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
600 seq_printf(m, " ");
601 avg = rec->time;
602 do_div(avg, rec->counter);
604 /* Sample standard deviation (s^2) */
605 if (rec->counter <= 1)
606 stddev = 0;
607 else {
608 stddev = rec->time_squared - rec->counter * avg * avg;
610 * Divide only 1000 for ns^2 -> us^2 conversion.
611 * trace_print_graph_duration will divide 1000 again.
613 do_div(stddev, (rec->counter - 1) * 1000);
616 trace_seq_init(&s);
617 trace_print_graph_duration(rec->time, &s);
618 trace_seq_puts(&s, " ");
619 trace_print_graph_duration(avg, &s);
620 trace_seq_puts(&s, " ");
621 trace_print_graph_duration(stddev, &s);
622 trace_print_seq(m, &s);
623 #endif
624 seq_putc(m, '\n');
625 out:
626 mutex_unlock(&ftrace_profile_lock);
628 return ret;
631 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
633 struct ftrace_profile_page *pg;
635 pg = stat->pages = stat->start;
637 while (pg) {
638 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
639 pg->index = 0;
640 pg = pg->next;
643 memset(stat->hash, 0,
644 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
647 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
649 struct ftrace_profile_page *pg;
650 int functions;
651 int pages;
652 int i;
654 /* If we already allocated, do nothing */
655 if (stat->pages)
656 return 0;
658 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
659 if (!stat->pages)
660 return -ENOMEM;
662 #ifdef CONFIG_DYNAMIC_FTRACE
663 functions = ftrace_update_tot_cnt;
664 #else
666 * We do not know the number of functions that exist because
667 * dynamic tracing is what counts them. With past experience
668 * we have around 20K functions. That should be more than enough.
669 * It is highly unlikely we will execute every function in
670 * the kernel.
672 functions = 20000;
673 #endif
675 pg = stat->start = stat->pages;
677 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
679 for (i = 0; i < pages; i++) {
680 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
681 if (!pg->next)
682 goto out_free;
683 pg = pg->next;
686 return 0;
688 out_free:
689 pg = stat->start;
690 while (pg) {
691 unsigned long tmp = (unsigned long)pg;
693 pg = pg->next;
694 free_page(tmp);
697 stat->pages = NULL;
698 stat->start = NULL;
700 return -ENOMEM;
703 static int ftrace_profile_init_cpu(int cpu)
705 struct ftrace_profile_stat *stat;
706 int size;
708 stat = &per_cpu(ftrace_profile_stats, cpu);
710 if (stat->hash) {
711 /* If the profile is already created, simply reset it */
712 ftrace_profile_reset(stat);
713 return 0;
717 * We are profiling all functions, but usually only a few thousand
718 * functions are hit. We'll make a hash of 1024 items.
720 size = FTRACE_PROFILE_HASH_SIZE;
722 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
724 if (!stat->hash)
725 return -ENOMEM;
727 if (!ftrace_profile_bits) {
728 size--;
730 for (; size; size >>= 1)
731 ftrace_profile_bits++;
734 /* Preallocate the function profiling pages */
735 if (ftrace_profile_pages_init(stat) < 0) {
736 kfree(stat->hash);
737 stat->hash = NULL;
738 return -ENOMEM;
741 return 0;
744 static int ftrace_profile_init(void)
746 int cpu;
747 int ret = 0;
749 for_each_online_cpu(cpu) {
750 ret = ftrace_profile_init_cpu(cpu);
751 if (ret)
752 break;
755 return ret;
758 /* interrupts must be disabled */
759 static struct ftrace_profile *
760 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
762 struct ftrace_profile *rec;
763 struct hlist_head *hhd;
764 unsigned long key;
766 key = hash_long(ip, ftrace_profile_bits);
767 hhd = &stat->hash[key];
769 if (hlist_empty(hhd))
770 return NULL;
772 hlist_for_each_entry_rcu(rec, hhd, node) {
773 if (rec->ip == ip)
774 return rec;
777 return NULL;
780 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
781 struct ftrace_profile *rec)
783 unsigned long key;
785 key = hash_long(rec->ip, ftrace_profile_bits);
786 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
790 * The memory is already allocated, this simply finds a new record to use.
792 static struct ftrace_profile *
793 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
795 struct ftrace_profile *rec = NULL;
797 /* prevent recursion (from NMIs) */
798 if (atomic_inc_return(&stat->disabled) != 1)
799 goto out;
802 * Try to find the function again since an NMI
803 * could have added it
805 rec = ftrace_find_profiled_func(stat, ip);
806 if (rec)
807 goto out;
809 if (stat->pages->index == PROFILES_PER_PAGE) {
810 if (!stat->pages->next)
811 goto out;
812 stat->pages = stat->pages->next;
815 rec = &stat->pages->records[stat->pages->index++];
816 rec->ip = ip;
817 ftrace_add_profile(stat, rec);
819 out:
820 atomic_dec(&stat->disabled);
822 return rec;
825 static void
826 function_profile_call(unsigned long ip, unsigned long parent_ip,
827 struct ftrace_ops *ops, struct pt_regs *regs)
829 struct ftrace_profile_stat *stat;
830 struct ftrace_profile *rec;
831 unsigned long flags;
833 if (!ftrace_profile_enabled)
834 return;
836 local_irq_save(flags);
838 stat = &__get_cpu_var(ftrace_profile_stats);
839 if (!stat->hash || !ftrace_profile_enabled)
840 goto out;
842 rec = ftrace_find_profiled_func(stat, ip);
843 if (!rec) {
844 rec = ftrace_profile_alloc(stat, ip);
845 if (!rec)
846 goto out;
849 rec->counter++;
850 out:
851 local_irq_restore(flags);
854 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
855 static int profile_graph_entry(struct ftrace_graph_ent *trace)
857 function_profile_call(trace->func, 0, NULL, NULL);
858 return 1;
861 static void profile_graph_return(struct ftrace_graph_ret *trace)
863 struct ftrace_profile_stat *stat;
864 unsigned long long calltime;
865 struct ftrace_profile *rec;
866 unsigned long flags;
868 local_irq_save(flags);
869 stat = &__get_cpu_var(ftrace_profile_stats);
870 if (!stat->hash || !ftrace_profile_enabled)
871 goto out;
873 /* If the calltime was zero'd ignore it */
874 if (!trace->calltime)
875 goto out;
877 calltime = trace->rettime - trace->calltime;
879 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
880 int index;
882 index = trace->depth;
884 /* Append this call time to the parent time to subtract */
885 if (index)
886 current->ret_stack[index - 1].subtime += calltime;
888 if (current->ret_stack[index].subtime < calltime)
889 calltime -= current->ret_stack[index].subtime;
890 else
891 calltime = 0;
894 rec = ftrace_find_profiled_func(stat, trace->func);
895 if (rec) {
896 rec->time += calltime;
897 rec->time_squared += calltime * calltime;
900 out:
901 local_irq_restore(flags);
904 static int register_ftrace_profiler(void)
906 return register_ftrace_graph(&profile_graph_return,
907 &profile_graph_entry);
910 static void unregister_ftrace_profiler(void)
912 unregister_ftrace_graph();
914 #else
915 static struct ftrace_ops ftrace_profile_ops __read_mostly = {
916 .func = function_profile_call,
917 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
920 static int register_ftrace_profiler(void)
922 return register_ftrace_function(&ftrace_profile_ops);
925 static void unregister_ftrace_profiler(void)
927 unregister_ftrace_function(&ftrace_profile_ops);
929 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
931 static ssize_t
932 ftrace_profile_write(struct file *filp, const char __user *ubuf,
933 size_t cnt, loff_t *ppos)
935 unsigned long val;
936 int ret;
938 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
939 if (ret)
940 return ret;
942 val = !!val;
944 mutex_lock(&ftrace_profile_lock);
945 if (ftrace_profile_enabled ^ val) {
946 if (val) {
947 ret = ftrace_profile_init();
948 if (ret < 0) {
949 cnt = ret;
950 goto out;
953 ret = register_ftrace_profiler();
954 if (ret < 0) {
955 cnt = ret;
956 goto out;
958 ftrace_profile_enabled = 1;
959 } else {
960 ftrace_profile_enabled = 0;
962 * unregister_ftrace_profiler calls stop_machine
963 * so this acts like an synchronize_sched.
965 unregister_ftrace_profiler();
968 out:
969 mutex_unlock(&ftrace_profile_lock);
971 *ppos += cnt;
973 return cnt;
976 static ssize_t
977 ftrace_profile_read(struct file *filp, char __user *ubuf,
978 size_t cnt, loff_t *ppos)
980 char buf[64]; /* big enough to hold a number */
981 int r;
983 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
984 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
987 static const struct file_operations ftrace_profile_fops = {
988 .open = tracing_open_generic,
989 .read = ftrace_profile_read,
990 .write = ftrace_profile_write,
991 .llseek = default_llseek,
994 /* used to initialize the real stat files */
995 static struct tracer_stat function_stats __initdata = {
996 .name = "functions",
997 .stat_start = function_stat_start,
998 .stat_next = function_stat_next,
999 .stat_cmp = function_stat_cmp,
1000 .stat_headers = function_stat_headers,
1001 .stat_show = function_stat_show
1004 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
1006 struct ftrace_profile_stat *stat;
1007 struct dentry *entry;
1008 char *name;
1009 int ret;
1010 int cpu;
1012 for_each_possible_cpu(cpu) {
1013 stat = &per_cpu(ftrace_profile_stats, cpu);
1015 /* allocate enough for function name + cpu number */
1016 name = kmalloc(32, GFP_KERNEL);
1017 if (!name) {
1019 * The files created are permanent, if something happens
1020 * we still do not free memory.
1022 WARN(1,
1023 "Could not allocate stat file for cpu %d\n",
1024 cpu);
1025 return;
1027 stat->stat = function_stats;
1028 snprintf(name, 32, "function%d", cpu);
1029 stat->stat.name = name;
1030 ret = register_stat_tracer(&stat->stat);
1031 if (ret) {
1032 WARN(1,
1033 "Could not register function stat for cpu %d\n",
1034 cpu);
1035 kfree(name);
1036 return;
1040 entry = debugfs_create_file("function_profile_enabled", 0644,
1041 d_tracer, NULL, &ftrace_profile_fops);
1042 if (!entry)
1043 pr_warning("Could not create debugfs "
1044 "'function_profile_enabled' entry\n");
1047 #else /* CONFIG_FUNCTION_PROFILER */
1048 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
1051 #endif /* CONFIG_FUNCTION_PROFILER */
1053 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1055 loff_t
1056 ftrace_filter_lseek(struct file *file, loff_t offset, int whence)
1058 loff_t ret;
1060 if (file->f_mode & FMODE_READ)
1061 ret = seq_lseek(file, offset, whence);
1062 else
1063 file->f_pos = ret = 1;
1065 return ret;
1068 #ifdef CONFIG_DYNAMIC_FTRACE
1070 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1071 # error Dynamic ftrace depends on MCOUNT_RECORD
1072 #endif
1074 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1076 struct ftrace_func_probe {
1077 struct hlist_node node;
1078 struct ftrace_probe_ops *ops;
1079 unsigned long flags;
1080 unsigned long ip;
1081 void *data;
1082 struct rcu_head rcu;
1085 struct ftrace_func_entry {
1086 struct hlist_node hlist;
1087 unsigned long ip;
1090 struct ftrace_hash {
1091 unsigned long size_bits;
1092 struct hlist_head *buckets;
1093 unsigned long count;
1094 struct rcu_head rcu;
1098 * We make these constant because no one should touch them,
1099 * but they are used as the default "empty hash", to avoid allocating
1100 * it all the time. These are in a read only section such that if
1101 * anyone does try to modify it, it will cause an exception.
1103 static const struct hlist_head empty_buckets[1];
1104 static const struct ftrace_hash empty_hash = {
1105 .buckets = (struct hlist_head *)empty_buckets,
1107 #define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
1109 static struct ftrace_ops global_ops = {
1110 .func = ftrace_stub,
1111 .notrace_hash = EMPTY_HASH,
1112 .filter_hash = EMPTY_HASH,
1113 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
1116 static DEFINE_MUTEX(ftrace_regex_lock);
1118 struct ftrace_page {
1119 struct ftrace_page *next;
1120 struct dyn_ftrace *records;
1121 int index;
1122 int size;
1125 static struct ftrace_page *ftrace_new_pgs;
1127 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1128 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1130 /* estimate from running different kernels */
1131 #define NR_TO_INIT 10000
1133 static struct ftrace_page *ftrace_pages_start;
1134 static struct ftrace_page *ftrace_pages;
1136 static bool ftrace_hash_empty(struct ftrace_hash *hash)
1138 return !hash || !hash->count;
1141 static struct ftrace_func_entry *
1142 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1144 unsigned long key;
1145 struct ftrace_func_entry *entry;
1146 struct hlist_head *hhd;
1148 if (ftrace_hash_empty(hash))
1149 return NULL;
1151 if (hash->size_bits > 0)
1152 key = hash_long(ip, hash->size_bits);
1153 else
1154 key = 0;
1156 hhd = &hash->buckets[key];
1158 hlist_for_each_entry_rcu(entry, hhd, hlist) {
1159 if (entry->ip == ip)
1160 return entry;
1162 return NULL;
1165 static void __add_hash_entry(struct ftrace_hash *hash,
1166 struct ftrace_func_entry *entry)
1168 struct hlist_head *hhd;
1169 unsigned long key;
1171 if (hash->size_bits)
1172 key = hash_long(entry->ip, hash->size_bits);
1173 else
1174 key = 0;
1176 hhd = &hash->buckets[key];
1177 hlist_add_head(&entry->hlist, hhd);
1178 hash->count++;
1181 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1183 struct ftrace_func_entry *entry;
1185 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1186 if (!entry)
1187 return -ENOMEM;
1189 entry->ip = ip;
1190 __add_hash_entry(hash, entry);
1192 return 0;
1195 static void
1196 free_hash_entry(struct ftrace_hash *hash,
1197 struct ftrace_func_entry *entry)
1199 hlist_del(&entry->hlist);
1200 kfree(entry);
1201 hash->count--;
1204 static void
1205 remove_hash_entry(struct ftrace_hash *hash,
1206 struct ftrace_func_entry *entry)
1208 hlist_del(&entry->hlist);
1209 hash->count--;
1212 static void ftrace_hash_clear(struct ftrace_hash *hash)
1214 struct hlist_head *hhd;
1215 struct hlist_node *tn;
1216 struct ftrace_func_entry *entry;
1217 int size = 1 << hash->size_bits;
1218 int i;
1220 if (!hash->count)
1221 return;
1223 for (i = 0; i < size; i++) {
1224 hhd = &hash->buckets[i];
1225 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
1226 free_hash_entry(hash, entry);
1228 FTRACE_WARN_ON(hash->count);
1231 static void free_ftrace_hash(struct ftrace_hash *hash)
1233 if (!hash || hash == EMPTY_HASH)
1234 return;
1235 ftrace_hash_clear(hash);
1236 kfree(hash->buckets);
1237 kfree(hash);
1240 static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1242 struct ftrace_hash *hash;
1244 hash = container_of(rcu, struct ftrace_hash, rcu);
1245 free_ftrace_hash(hash);
1248 static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1250 if (!hash || hash == EMPTY_HASH)
1251 return;
1252 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1255 void ftrace_free_filter(struct ftrace_ops *ops)
1257 free_ftrace_hash(ops->filter_hash);
1258 free_ftrace_hash(ops->notrace_hash);
1261 static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1263 struct ftrace_hash *hash;
1264 int size;
1266 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1267 if (!hash)
1268 return NULL;
1270 size = 1 << size_bits;
1271 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
1273 if (!hash->buckets) {
1274 kfree(hash);
1275 return NULL;
1278 hash->size_bits = size_bits;
1280 return hash;
1283 static struct ftrace_hash *
1284 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1286 struct ftrace_func_entry *entry;
1287 struct ftrace_hash *new_hash;
1288 int size;
1289 int ret;
1290 int i;
1292 new_hash = alloc_ftrace_hash(size_bits);
1293 if (!new_hash)
1294 return NULL;
1296 /* Empty hash? */
1297 if (ftrace_hash_empty(hash))
1298 return new_hash;
1300 size = 1 << hash->size_bits;
1301 for (i = 0; i < size; i++) {
1302 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
1303 ret = add_hash_entry(new_hash, entry->ip);
1304 if (ret < 0)
1305 goto free_hash;
1309 FTRACE_WARN_ON(new_hash->count != hash->count);
1311 return new_hash;
1313 free_hash:
1314 free_ftrace_hash(new_hash);
1315 return NULL;
1318 static void
1319 ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1320 static void
1321 ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1323 static int
1324 ftrace_hash_move(struct ftrace_ops *ops, int enable,
1325 struct ftrace_hash **dst, struct ftrace_hash *src)
1327 struct ftrace_func_entry *entry;
1328 struct hlist_node *tn;
1329 struct hlist_head *hhd;
1330 struct ftrace_hash *old_hash;
1331 struct ftrace_hash *new_hash;
1332 unsigned long key;
1333 int size = src->count;
1334 int bits = 0;
1335 int ret;
1336 int i;
1339 * Remove the current set, update the hash and add
1340 * them back.
1342 ftrace_hash_rec_disable(ops, enable);
1345 * If the new source is empty, just free dst and assign it
1346 * the empty_hash.
1348 if (!src->count) {
1349 free_ftrace_hash_rcu(*dst);
1350 rcu_assign_pointer(*dst, EMPTY_HASH);
1351 /* still need to update the function records */
1352 ret = 0;
1353 goto out;
1357 * Make the hash size about 1/2 the # found
1359 for (size /= 2; size; size >>= 1)
1360 bits++;
1362 /* Don't allocate too much */
1363 if (bits > FTRACE_HASH_MAX_BITS)
1364 bits = FTRACE_HASH_MAX_BITS;
1366 ret = -ENOMEM;
1367 new_hash = alloc_ftrace_hash(bits);
1368 if (!new_hash)
1369 goto out;
1371 size = 1 << src->size_bits;
1372 for (i = 0; i < size; i++) {
1373 hhd = &src->buckets[i];
1374 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
1375 if (bits > 0)
1376 key = hash_long(entry->ip, bits);
1377 else
1378 key = 0;
1379 remove_hash_entry(src, entry);
1380 __add_hash_entry(new_hash, entry);
1384 old_hash = *dst;
1385 rcu_assign_pointer(*dst, new_hash);
1386 free_ftrace_hash_rcu(old_hash);
1388 ret = 0;
1389 out:
1391 * Enable regardless of ret:
1392 * On success, we enable the new hash.
1393 * On failure, we re-enable the original hash.
1395 ftrace_hash_rec_enable(ops, enable);
1397 return ret;
1401 * Test the hashes for this ops to see if we want to call
1402 * the ops->func or not.
1404 * It's a match if the ip is in the ops->filter_hash or
1405 * the filter_hash does not exist or is empty,
1406 * AND
1407 * the ip is not in the ops->notrace_hash.
1409 * This needs to be called with preemption disabled as
1410 * the hashes are freed with call_rcu_sched().
1412 static int
1413 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
1415 struct ftrace_hash *filter_hash;
1416 struct ftrace_hash *notrace_hash;
1417 int ret;
1419 filter_hash = rcu_dereference_raw(ops->filter_hash);
1420 notrace_hash = rcu_dereference_raw(ops->notrace_hash);
1422 if ((ftrace_hash_empty(filter_hash) ||
1423 ftrace_lookup_ip(filter_hash, ip)) &&
1424 (ftrace_hash_empty(notrace_hash) ||
1425 !ftrace_lookup_ip(notrace_hash, ip)))
1426 ret = 1;
1427 else
1428 ret = 0;
1430 return ret;
1434 * This is a double for. Do not use 'break' to break out of the loop,
1435 * you must use a goto.
1437 #define do_for_each_ftrace_rec(pg, rec) \
1438 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1439 int _____i; \
1440 for (_____i = 0; _____i < pg->index; _____i++) { \
1441 rec = &pg->records[_____i];
1443 #define while_for_each_ftrace_rec() \
1448 static int ftrace_cmp_recs(const void *a, const void *b)
1450 const struct dyn_ftrace *key = a;
1451 const struct dyn_ftrace *rec = b;
1453 if (key->flags < rec->ip)
1454 return -1;
1455 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1456 return 1;
1457 return 0;
1460 static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
1462 struct ftrace_page *pg;
1463 struct dyn_ftrace *rec;
1464 struct dyn_ftrace key;
1466 key.ip = start;
1467 key.flags = end; /* overload flags, as it is unsigned long */
1469 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1470 if (end < pg->records[0].ip ||
1471 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1472 continue;
1473 rec = bsearch(&key, pg->records, pg->index,
1474 sizeof(struct dyn_ftrace),
1475 ftrace_cmp_recs);
1476 if (rec)
1477 return rec->ip;
1480 return 0;
1484 * ftrace_location - return true if the ip giving is a traced location
1485 * @ip: the instruction pointer to check
1487 * Returns rec->ip if @ip given is a pointer to a ftrace location.
1488 * That is, the instruction that is either a NOP or call to
1489 * the function tracer. It checks the ftrace internal tables to
1490 * determine if the address belongs or not.
1492 unsigned long ftrace_location(unsigned long ip)
1494 return ftrace_location_range(ip, ip);
1498 * ftrace_text_reserved - return true if range contains an ftrace location
1499 * @start: start of range to search
1500 * @end: end of range to search (inclusive). @end points to the last byte to check.
1502 * Returns 1 if @start and @end contains a ftrace location.
1503 * That is, the instruction that is either a NOP or call to
1504 * the function tracer. It checks the ftrace internal tables to
1505 * determine if the address belongs or not.
1507 int ftrace_text_reserved(void *start, void *end)
1509 unsigned long ret;
1511 ret = ftrace_location_range((unsigned long)start,
1512 (unsigned long)end);
1514 return (int)!!ret;
1517 static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1518 int filter_hash,
1519 bool inc)
1521 struct ftrace_hash *hash;
1522 struct ftrace_hash *other_hash;
1523 struct ftrace_page *pg;
1524 struct dyn_ftrace *rec;
1525 int count = 0;
1526 int all = 0;
1528 /* Only update if the ops has been registered */
1529 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1530 return;
1533 * In the filter_hash case:
1534 * If the count is zero, we update all records.
1535 * Otherwise we just update the items in the hash.
1537 * In the notrace_hash case:
1538 * We enable the update in the hash.
1539 * As disabling notrace means enabling the tracing,
1540 * and enabling notrace means disabling, the inc variable
1541 * gets inversed.
1543 if (filter_hash) {
1544 hash = ops->filter_hash;
1545 other_hash = ops->notrace_hash;
1546 if (ftrace_hash_empty(hash))
1547 all = 1;
1548 } else {
1549 inc = !inc;
1550 hash = ops->notrace_hash;
1551 other_hash = ops->filter_hash;
1553 * If the notrace hash has no items,
1554 * then there's nothing to do.
1556 if (ftrace_hash_empty(hash))
1557 return;
1560 do_for_each_ftrace_rec(pg, rec) {
1561 int in_other_hash = 0;
1562 int in_hash = 0;
1563 int match = 0;
1565 if (all) {
1567 * Only the filter_hash affects all records.
1568 * Update if the record is not in the notrace hash.
1570 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1571 match = 1;
1572 } else {
1573 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1574 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1579 if (filter_hash && in_hash && !in_other_hash)
1580 match = 1;
1581 else if (!filter_hash && in_hash &&
1582 (in_other_hash || ftrace_hash_empty(other_hash)))
1583 match = 1;
1585 if (!match)
1586 continue;
1588 if (inc) {
1589 rec->flags++;
1590 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1591 return;
1593 * If any ops wants regs saved for this function
1594 * then all ops will get saved regs.
1596 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1597 rec->flags |= FTRACE_FL_REGS;
1598 } else {
1599 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1600 return;
1601 rec->flags--;
1603 count++;
1604 /* Shortcut, if we handled all records, we are done. */
1605 if (!all && count == hash->count)
1606 return;
1607 } while_for_each_ftrace_rec();
1610 static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1611 int filter_hash)
1613 __ftrace_hash_rec_update(ops, filter_hash, 0);
1616 static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1617 int filter_hash)
1619 __ftrace_hash_rec_update(ops, filter_hash, 1);
1622 static void print_ip_ins(const char *fmt, unsigned char *p)
1624 int i;
1626 printk(KERN_CONT "%s", fmt);
1628 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1629 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1633 * ftrace_bug - report and shutdown function tracer
1634 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1635 * @ip: The address that failed
1637 * The arch code that enables or disables the function tracing
1638 * can call ftrace_bug() when it has detected a problem in
1639 * modifying the code. @failed should be one of either:
1640 * EFAULT - if the problem happens on reading the @ip address
1641 * EINVAL - if what is read at @ip is not what was expected
1642 * EPERM - if the problem happens on writting to the @ip address
1644 void ftrace_bug(int failed, unsigned long ip)
1646 switch (failed) {
1647 case -EFAULT:
1648 FTRACE_WARN_ON_ONCE(1);
1649 pr_info("ftrace faulted on modifying ");
1650 print_ip_sym(ip);
1651 break;
1652 case -EINVAL:
1653 FTRACE_WARN_ON_ONCE(1);
1654 pr_info("ftrace failed to modify ");
1655 print_ip_sym(ip);
1656 print_ip_ins(" actual: ", (unsigned char *)ip);
1657 printk(KERN_CONT "\n");
1658 break;
1659 case -EPERM:
1660 FTRACE_WARN_ON_ONCE(1);
1661 pr_info("ftrace faulted on writing ");
1662 print_ip_sym(ip);
1663 break;
1664 default:
1665 FTRACE_WARN_ON_ONCE(1);
1666 pr_info("ftrace faulted on unknown error ");
1667 print_ip_sym(ip);
1671 static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
1673 unsigned long flag = 0UL;
1676 * If we are updating calls:
1678 * If the record has a ref count, then we need to enable it
1679 * because someone is using it.
1681 * Otherwise we make sure its disabled.
1683 * If we are disabling calls, then disable all records that
1684 * are enabled.
1686 if (enable && (rec->flags & ~FTRACE_FL_MASK))
1687 flag = FTRACE_FL_ENABLED;
1690 * If enabling and the REGS flag does not match the REGS_EN, then
1691 * do not ignore this record. Set flags to fail the compare against
1692 * ENABLED.
1694 if (flag &&
1695 (!(rec->flags & FTRACE_FL_REGS) != !(rec->flags & FTRACE_FL_REGS_EN)))
1696 flag |= FTRACE_FL_REGS;
1698 /* If the state of this record hasn't changed, then do nothing */
1699 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1700 return FTRACE_UPDATE_IGNORE;
1702 if (flag) {
1703 /* Save off if rec is being enabled (for return value) */
1704 flag ^= rec->flags & FTRACE_FL_ENABLED;
1706 if (update) {
1707 rec->flags |= FTRACE_FL_ENABLED;
1708 if (flag & FTRACE_FL_REGS) {
1709 if (rec->flags & FTRACE_FL_REGS)
1710 rec->flags |= FTRACE_FL_REGS_EN;
1711 else
1712 rec->flags &= ~FTRACE_FL_REGS_EN;
1717 * If this record is being updated from a nop, then
1718 * return UPDATE_MAKE_CALL.
1719 * Otherwise, if the EN flag is set, then return
1720 * UPDATE_MODIFY_CALL_REGS to tell the caller to convert
1721 * from the non-save regs, to a save regs function.
1722 * Otherwise,
1723 * return UPDATE_MODIFY_CALL to tell the caller to convert
1724 * from the save regs, to a non-save regs function.
1726 if (flag & FTRACE_FL_ENABLED)
1727 return FTRACE_UPDATE_MAKE_CALL;
1728 else if (rec->flags & FTRACE_FL_REGS_EN)
1729 return FTRACE_UPDATE_MODIFY_CALL_REGS;
1730 else
1731 return FTRACE_UPDATE_MODIFY_CALL;
1734 if (update) {
1735 /* If there's no more users, clear all flags */
1736 if (!(rec->flags & ~FTRACE_FL_MASK))
1737 rec->flags = 0;
1738 else
1739 /* Just disable the record (keep REGS state) */
1740 rec->flags &= ~FTRACE_FL_ENABLED;
1743 return FTRACE_UPDATE_MAKE_NOP;
1747 * ftrace_update_record, set a record that now is tracing or not
1748 * @rec: the record to update
1749 * @enable: set to 1 if the record is tracing, zero to force disable
1751 * The records that represent all functions that can be traced need
1752 * to be updated when tracing has been enabled.
1754 int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1756 return ftrace_check_record(rec, enable, 1);
1760 * ftrace_test_record, check if the record has been enabled or not
1761 * @rec: the record to test
1762 * @enable: set to 1 to check if enabled, 0 if it is disabled
1764 * The arch code may need to test if a record is already set to
1765 * tracing to determine how to modify the function code that it
1766 * represents.
1768 int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1770 return ftrace_check_record(rec, enable, 0);
1773 static int
1774 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1776 unsigned long ftrace_old_addr;
1777 unsigned long ftrace_addr;
1778 int ret;
1780 ret = ftrace_update_record(rec, enable);
1782 if (rec->flags & FTRACE_FL_REGS)
1783 ftrace_addr = (unsigned long)FTRACE_REGS_ADDR;
1784 else
1785 ftrace_addr = (unsigned long)FTRACE_ADDR;
1787 switch (ret) {
1788 case FTRACE_UPDATE_IGNORE:
1789 return 0;
1791 case FTRACE_UPDATE_MAKE_CALL:
1792 return ftrace_make_call(rec, ftrace_addr);
1794 case FTRACE_UPDATE_MAKE_NOP:
1795 return ftrace_make_nop(NULL, rec, ftrace_addr);
1797 case FTRACE_UPDATE_MODIFY_CALL_REGS:
1798 case FTRACE_UPDATE_MODIFY_CALL:
1799 if (rec->flags & FTRACE_FL_REGS)
1800 ftrace_old_addr = (unsigned long)FTRACE_ADDR;
1801 else
1802 ftrace_old_addr = (unsigned long)FTRACE_REGS_ADDR;
1804 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
1807 return -1; /* unknow ftrace bug */
1810 void __weak ftrace_replace_code(int enable)
1812 struct dyn_ftrace *rec;
1813 struct ftrace_page *pg;
1814 int failed;
1816 if (unlikely(ftrace_disabled))
1817 return;
1819 do_for_each_ftrace_rec(pg, rec) {
1820 failed = __ftrace_replace_code(rec, enable);
1821 if (failed) {
1822 ftrace_bug(failed, rec->ip);
1823 /* Stop processing */
1824 return;
1826 } while_for_each_ftrace_rec();
1829 struct ftrace_rec_iter {
1830 struct ftrace_page *pg;
1831 int index;
1835 * ftrace_rec_iter_start, start up iterating over traced functions
1837 * Returns an iterator handle that is used to iterate over all
1838 * the records that represent address locations where functions
1839 * are traced.
1841 * May return NULL if no records are available.
1843 struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1846 * We only use a single iterator.
1847 * Protected by the ftrace_lock mutex.
1849 static struct ftrace_rec_iter ftrace_rec_iter;
1850 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1852 iter->pg = ftrace_pages_start;
1853 iter->index = 0;
1855 /* Could have empty pages */
1856 while (iter->pg && !iter->pg->index)
1857 iter->pg = iter->pg->next;
1859 if (!iter->pg)
1860 return NULL;
1862 return iter;
1866 * ftrace_rec_iter_next, get the next record to process.
1867 * @iter: The handle to the iterator.
1869 * Returns the next iterator after the given iterator @iter.
1871 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1873 iter->index++;
1875 if (iter->index >= iter->pg->index) {
1876 iter->pg = iter->pg->next;
1877 iter->index = 0;
1879 /* Could have empty pages */
1880 while (iter->pg && !iter->pg->index)
1881 iter->pg = iter->pg->next;
1884 if (!iter->pg)
1885 return NULL;
1887 return iter;
1891 * ftrace_rec_iter_record, get the record at the iterator location
1892 * @iter: The current iterator location
1894 * Returns the record that the current @iter is at.
1896 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1898 return &iter->pg->records[iter->index];
1901 static int
1902 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
1904 unsigned long ip;
1905 int ret;
1907 ip = rec->ip;
1909 if (unlikely(ftrace_disabled))
1910 return 0;
1912 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
1913 if (ret) {
1914 ftrace_bug(ret, ip);
1915 return 0;
1917 return 1;
1921 * archs can override this function if they must do something
1922 * before the modifying code is performed.
1924 int __weak ftrace_arch_code_modify_prepare(void)
1926 return 0;
1930 * archs can override this function if they must do something
1931 * after the modifying code is performed.
1933 int __weak ftrace_arch_code_modify_post_process(void)
1935 return 0;
1938 void ftrace_modify_all_code(int command)
1940 if (command & FTRACE_UPDATE_CALLS)
1941 ftrace_replace_code(1);
1942 else if (command & FTRACE_DISABLE_CALLS)
1943 ftrace_replace_code(0);
1945 if (command & FTRACE_UPDATE_TRACE_FUNC)
1946 ftrace_update_ftrace_func(ftrace_trace_function);
1948 if (command & FTRACE_START_FUNC_RET)
1949 ftrace_enable_ftrace_graph_caller();
1950 else if (command & FTRACE_STOP_FUNC_RET)
1951 ftrace_disable_ftrace_graph_caller();
1954 static int __ftrace_modify_code(void *data)
1956 int *command = data;
1958 ftrace_modify_all_code(*command);
1960 return 0;
1964 * ftrace_run_stop_machine, go back to the stop machine method
1965 * @command: The command to tell ftrace what to do
1967 * If an arch needs to fall back to the stop machine method, the
1968 * it can call this function.
1970 void ftrace_run_stop_machine(int command)
1972 stop_machine(__ftrace_modify_code, &command, NULL);
1976 * arch_ftrace_update_code, modify the code to trace or not trace
1977 * @command: The command that needs to be done
1979 * Archs can override this function if it does not need to
1980 * run stop_machine() to modify code.
1982 void __weak arch_ftrace_update_code(int command)
1984 ftrace_run_stop_machine(command);
1987 static void ftrace_run_update_code(int command)
1989 int ret;
1991 ret = ftrace_arch_code_modify_prepare();
1992 FTRACE_WARN_ON(ret);
1993 if (ret)
1994 return;
1996 * Do not call function tracer while we update the code.
1997 * We are in stop machine.
1999 function_trace_stop++;
2002 * By default we use stop_machine() to modify the code.
2003 * But archs can do what ever they want as long as it
2004 * is safe. The stop_machine() is the safest, but also
2005 * produces the most overhead.
2007 arch_ftrace_update_code(command);
2009 function_trace_stop--;
2011 ret = ftrace_arch_code_modify_post_process();
2012 FTRACE_WARN_ON(ret);
2015 static ftrace_func_t saved_ftrace_func;
2016 static int ftrace_start_up;
2017 static int global_start_up;
2019 static void ftrace_startup_enable(int command)
2021 if (saved_ftrace_func != ftrace_trace_function) {
2022 saved_ftrace_func = ftrace_trace_function;
2023 command |= FTRACE_UPDATE_TRACE_FUNC;
2026 if (!command || !ftrace_enabled)
2027 return;
2029 ftrace_run_update_code(command);
2032 static int ftrace_startup(struct ftrace_ops *ops, int command)
2034 bool hash_enable = true;
2036 if (unlikely(ftrace_disabled))
2037 return -ENODEV;
2039 ftrace_start_up++;
2040 command |= FTRACE_UPDATE_CALLS;
2042 /* ops marked global share the filter hashes */
2043 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2044 ops = &global_ops;
2045 /* Don't update hash if global is already set */
2046 if (global_start_up)
2047 hash_enable = false;
2048 global_start_up++;
2051 ops->flags |= FTRACE_OPS_FL_ENABLED;
2052 if (hash_enable)
2053 ftrace_hash_rec_enable(ops, 1);
2055 ftrace_startup_enable(command);
2057 return 0;
2060 static void ftrace_shutdown(struct ftrace_ops *ops, int command)
2062 bool hash_disable = true;
2064 if (unlikely(ftrace_disabled))
2065 return;
2067 ftrace_start_up--;
2069 * Just warn in case of unbalance, no need to kill ftrace, it's not
2070 * critical but the ftrace_call callers may be never nopped again after
2071 * further ftrace uses.
2073 WARN_ON_ONCE(ftrace_start_up < 0);
2075 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2076 ops = &global_ops;
2077 global_start_up--;
2078 WARN_ON_ONCE(global_start_up < 0);
2079 /* Don't update hash if global still has users */
2080 if (global_start_up) {
2081 WARN_ON_ONCE(!ftrace_start_up);
2082 hash_disable = false;
2086 if (hash_disable)
2087 ftrace_hash_rec_disable(ops, 1);
2089 if (ops != &global_ops || !global_start_up)
2090 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2092 command |= FTRACE_UPDATE_CALLS;
2094 if (saved_ftrace_func != ftrace_trace_function) {
2095 saved_ftrace_func = ftrace_trace_function;
2096 command |= FTRACE_UPDATE_TRACE_FUNC;
2099 if (!command || !ftrace_enabled)
2100 return;
2102 ftrace_run_update_code(command);
2105 static void ftrace_startup_sysctl(void)
2107 if (unlikely(ftrace_disabled))
2108 return;
2110 /* Force update next time */
2111 saved_ftrace_func = NULL;
2112 /* ftrace_start_up is true if we want ftrace running */
2113 if (ftrace_start_up)
2114 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
2117 static void ftrace_shutdown_sysctl(void)
2119 if (unlikely(ftrace_disabled))
2120 return;
2122 /* ftrace_start_up is true if ftrace is running */
2123 if (ftrace_start_up)
2124 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
2127 static cycle_t ftrace_update_time;
2128 static unsigned long ftrace_update_cnt;
2129 unsigned long ftrace_update_tot_cnt;
2131 static int ops_traces_mod(struct ftrace_ops *ops)
2133 struct ftrace_hash *hash;
2135 hash = ops->filter_hash;
2136 return ftrace_hash_empty(hash);
2139 static int ftrace_update_code(struct module *mod)
2141 struct ftrace_page *pg;
2142 struct dyn_ftrace *p;
2143 cycle_t start, stop;
2144 unsigned long ref = 0;
2145 int i;
2148 * When adding a module, we need to check if tracers are
2149 * currently enabled and if they are set to trace all functions.
2150 * If they are, we need to enable the module functions as well
2151 * as update the reference counts for those function records.
2153 if (mod) {
2154 struct ftrace_ops *ops;
2156 for (ops = ftrace_ops_list;
2157 ops != &ftrace_list_end; ops = ops->next) {
2158 if (ops->flags & FTRACE_OPS_FL_ENABLED &&
2159 ops_traces_mod(ops))
2160 ref++;
2164 start = ftrace_now(raw_smp_processor_id());
2165 ftrace_update_cnt = 0;
2167 for (pg = ftrace_new_pgs; pg; pg = pg->next) {
2169 for (i = 0; i < pg->index; i++) {
2170 /* If something went wrong, bail without enabling anything */
2171 if (unlikely(ftrace_disabled))
2172 return -1;
2174 p = &pg->records[i];
2175 p->flags = ref;
2178 * Do the initial record conversion from mcount jump
2179 * to the NOP instructions.
2181 if (!ftrace_code_disable(mod, p))
2182 break;
2184 ftrace_update_cnt++;
2187 * If the tracing is enabled, go ahead and enable the record.
2189 * The reason not to enable the record immediatelly is the
2190 * inherent check of ftrace_make_nop/ftrace_make_call for
2191 * correct previous instructions. Making first the NOP
2192 * conversion puts the module to the correct state, thus
2193 * passing the ftrace_make_call check.
2195 if (ftrace_start_up && ref) {
2196 int failed = __ftrace_replace_code(p, 1);
2197 if (failed)
2198 ftrace_bug(failed, p->ip);
2203 ftrace_new_pgs = NULL;
2205 stop = ftrace_now(raw_smp_processor_id());
2206 ftrace_update_time = stop - start;
2207 ftrace_update_tot_cnt += ftrace_update_cnt;
2209 return 0;
2212 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
2214 int order;
2215 int cnt;
2217 if (WARN_ON(!count))
2218 return -EINVAL;
2220 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
2223 * We want to fill as much as possible. No more than a page
2224 * may be empty.
2226 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2227 order--;
2229 again:
2230 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2232 if (!pg->records) {
2233 /* if we can't allocate this size, try something smaller */
2234 if (!order)
2235 return -ENOMEM;
2236 order >>= 1;
2237 goto again;
2240 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2241 pg->size = cnt;
2243 if (cnt > count)
2244 cnt = count;
2246 return cnt;
2249 static struct ftrace_page *
2250 ftrace_allocate_pages(unsigned long num_to_init)
2252 struct ftrace_page *start_pg;
2253 struct ftrace_page *pg;
2254 int order;
2255 int cnt;
2257 if (!num_to_init)
2258 return 0;
2260 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2261 if (!pg)
2262 return NULL;
2265 * Try to allocate as much as possible in one continues
2266 * location that fills in all of the space. We want to
2267 * waste as little space as possible.
2269 for (;;) {
2270 cnt = ftrace_allocate_records(pg, num_to_init);
2271 if (cnt < 0)
2272 goto free_pages;
2274 num_to_init -= cnt;
2275 if (!num_to_init)
2276 break;
2278 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2279 if (!pg->next)
2280 goto free_pages;
2282 pg = pg->next;
2285 return start_pg;
2287 free_pages:
2288 while (start_pg) {
2289 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2290 free_pages((unsigned long)pg->records, order);
2291 start_pg = pg->next;
2292 kfree(pg);
2293 pg = start_pg;
2295 pr_info("ftrace: FAILED to allocate memory for functions\n");
2296 return NULL;
2299 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2301 int cnt;
2303 if (!num_to_init) {
2304 pr_info("ftrace: No functions to be traced?\n");
2305 return -1;
2308 cnt = num_to_init / ENTRIES_PER_PAGE;
2309 pr_info("ftrace: allocating %ld entries in %d pages\n",
2310 num_to_init, cnt + 1);
2312 return 0;
2315 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2317 struct ftrace_iterator {
2318 loff_t pos;
2319 loff_t func_pos;
2320 struct ftrace_page *pg;
2321 struct dyn_ftrace *func;
2322 struct ftrace_func_probe *probe;
2323 struct trace_parser parser;
2324 struct ftrace_hash *hash;
2325 struct ftrace_ops *ops;
2326 int hidx;
2327 int idx;
2328 unsigned flags;
2331 static void *
2332 t_hash_next(struct seq_file *m, loff_t *pos)
2334 struct ftrace_iterator *iter = m->private;
2335 struct hlist_node *hnd = NULL;
2336 struct hlist_head *hhd;
2338 (*pos)++;
2339 iter->pos = *pos;
2341 if (iter->probe)
2342 hnd = &iter->probe->node;
2343 retry:
2344 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2345 return NULL;
2347 hhd = &ftrace_func_hash[iter->hidx];
2349 if (hlist_empty(hhd)) {
2350 iter->hidx++;
2351 hnd = NULL;
2352 goto retry;
2355 if (!hnd)
2356 hnd = hhd->first;
2357 else {
2358 hnd = hnd->next;
2359 if (!hnd) {
2360 iter->hidx++;
2361 goto retry;
2365 if (WARN_ON_ONCE(!hnd))
2366 return NULL;
2368 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2370 return iter;
2373 static void *t_hash_start(struct seq_file *m, loff_t *pos)
2375 struct ftrace_iterator *iter = m->private;
2376 void *p = NULL;
2377 loff_t l;
2379 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2380 return NULL;
2382 if (iter->func_pos > *pos)
2383 return NULL;
2385 iter->hidx = 0;
2386 for (l = 0; l <= (*pos - iter->func_pos); ) {
2387 p = t_hash_next(m, &l);
2388 if (!p)
2389 break;
2391 if (!p)
2392 return NULL;
2394 /* Only set this if we have an item */
2395 iter->flags |= FTRACE_ITER_HASH;
2397 return iter;
2400 static int
2401 t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
2403 struct ftrace_func_probe *rec;
2405 rec = iter->probe;
2406 if (WARN_ON_ONCE(!rec))
2407 return -EIO;
2409 if (rec->ops->print)
2410 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2412 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
2414 if (rec->data)
2415 seq_printf(m, ":%p", rec->data);
2416 seq_putc(m, '\n');
2418 return 0;
2421 static void *
2422 t_next(struct seq_file *m, void *v, loff_t *pos)
2424 struct ftrace_iterator *iter = m->private;
2425 struct ftrace_ops *ops = iter->ops;
2426 struct dyn_ftrace *rec = NULL;
2428 if (unlikely(ftrace_disabled))
2429 return NULL;
2431 if (iter->flags & FTRACE_ITER_HASH)
2432 return t_hash_next(m, pos);
2434 (*pos)++;
2435 iter->pos = iter->func_pos = *pos;
2437 if (iter->flags & FTRACE_ITER_PRINTALL)
2438 return t_hash_start(m, pos);
2440 retry:
2441 if (iter->idx >= iter->pg->index) {
2442 if (iter->pg->next) {
2443 iter->pg = iter->pg->next;
2444 iter->idx = 0;
2445 goto retry;
2447 } else {
2448 rec = &iter->pg->records[iter->idx++];
2449 if (((iter->flags & FTRACE_ITER_FILTER) &&
2450 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
2452 ((iter->flags & FTRACE_ITER_NOTRACE) &&
2453 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2455 ((iter->flags & FTRACE_ITER_ENABLED) &&
2456 !(rec->flags & ~FTRACE_FL_MASK))) {
2458 rec = NULL;
2459 goto retry;
2463 if (!rec)
2464 return t_hash_start(m, pos);
2466 iter->func = rec;
2468 return iter;
2471 static void reset_iter_read(struct ftrace_iterator *iter)
2473 iter->pos = 0;
2474 iter->func_pos = 0;
2475 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
2478 static void *t_start(struct seq_file *m, loff_t *pos)
2480 struct ftrace_iterator *iter = m->private;
2481 struct ftrace_ops *ops = iter->ops;
2482 void *p = NULL;
2483 loff_t l;
2485 mutex_lock(&ftrace_lock);
2487 if (unlikely(ftrace_disabled))
2488 return NULL;
2491 * If an lseek was done, then reset and start from beginning.
2493 if (*pos < iter->pos)
2494 reset_iter_read(iter);
2497 * For set_ftrace_filter reading, if we have the filter
2498 * off, we can short cut and just print out that all
2499 * functions are enabled.
2501 if (iter->flags & FTRACE_ITER_FILTER &&
2502 ftrace_hash_empty(ops->filter_hash)) {
2503 if (*pos > 0)
2504 return t_hash_start(m, pos);
2505 iter->flags |= FTRACE_ITER_PRINTALL;
2506 /* reset in case of seek/pread */
2507 iter->flags &= ~FTRACE_ITER_HASH;
2508 return iter;
2511 if (iter->flags & FTRACE_ITER_HASH)
2512 return t_hash_start(m, pos);
2515 * Unfortunately, we need to restart at ftrace_pages_start
2516 * every time we let go of the ftrace_mutex. This is because
2517 * those pointers can change without the lock.
2519 iter->pg = ftrace_pages_start;
2520 iter->idx = 0;
2521 for (l = 0; l <= *pos; ) {
2522 p = t_next(m, p, &l);
2523 if (!p)
2524 break;
2527 if (!p)
2528 return t_hash_start(m, pos);
2530 return iter;
2533 static void t_stop(struct seq_file *m, void *p)
2535 mutex_unlock(&ftrace_lock);
2538 static int t_show(struct seq_file *m, void *v)
2540 struct ftrace_iterator *iter = m->private;
2541 struct dyn_ftrace *rec;
2543 if (iter->flags & FTRACE_ITER_HASH)
2544 return t_hash_show(m, iter);
2546 if (iter->flags & FTRACE_ITER_PRINTALL) {
2547 seq_printf(m, "#### all functions enabled ####\n");
2548 return 0;
2551 rec = iter->func;
2553 if (!rec)
2554 return 0;
2556 seq_printf(m, "%ps", (void *)rec->ip);
2557 if (iter->flags & FTRACE_ITER_ENABLED)
2558 seq_printf(m, " (%ld)%s",
2559 rec->flags & ~FTRACE_FL_MASK,
2560 rec->flags & FTRACE_FL_REGS ? " R" : "");
2561 seq_printf(m, "\n");
2563 return 0;
2566 static const struct seq_operations show_ftrace_seq_ops = {
2567 .start = t_start,
2568 .next = t_next,
2569 .stop = t_stop,
2570 .show = t_show,
2573 static int
2574 ftrace_avail_open(struct inode *inode, struct file *file)
2576 struct ftrace_iterator *iter;
2578 if (unlikely(ftrace_disabled))
2579 return -ENODEV;
2581 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2582 if (iter) {
2583 iter->pg = ftrace_pages_start;
2584 iter->ops = &global_ops;
2587 return iter ? 0 : -ENOMEM;
2590 static int
2591 ftrace_enabled_open(struct inode *inode, struct file *file)
2593 struct ftrace_iterator *iter;
2595 if (unlikely(ftrace_disabled))
2596 return -ENODEV;
2598 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2599 if (iter) {
2600 iter->pg = ftrace_pages_start;
2601 iter->flags = FTRACE_ITER_ENABLED;
2602 iter->ops = &global_ops;
2605 return iter ? 0 : -ENOMEM;
2608 static void ftrace_filter_reset(struct ftrace_hash *hash)
2610 mutex_lock(&ftrace_lock);
2611 ftrace_hash_clear(hash);
2612 mutex_unlock(&ftrace_lock);
2616 * ftrace_regex_open - initialize function tracer filter files
2617 * @ops: The ftrace_ops that hold the hash filters
2618 * @flag: The type of filter to process
2619 * @inode: The inode, usually passed in to your open routine
2620 * @file: The file, usually passed in to your open routine
2622 * ftrace_regex_open() initializes the filter files for the
2623 * @ops. Depending on @flag it may process the filter hash or
2624 * the notrace hash of @ops. With this called from the open
2625 * routine, you can use ftrace_filter_write() for the write
2626 * routine if @flag has FTRACE_ITER_FILTER set, or
2627 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
2628 * ftrace_filter_lseek() should be used as the lseek routine, and
2629 * release must call ftrace_regex_release().
2632 ftrace_regex_open(struct ftrace_ops *ops, int flag,
2633 struct inode *inode, struct file *file)
2635 struct ftrace_iterator *iter;
2636 struct ftrace_hash *hash;
2637 int ret = 0;
2639 if (unlikely(ftrace_disabled))
2640 return -ENODEV;
2642 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2643 if (!iter)
2644 return -ENOMEM;
2646 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2647 kfree(iter);
2648 return -ENOMEM;
2651 if (flag & FTRACE_ITER_NOTRACE)
2652 hash = ops->notrace_hash;
2653 else
2654 hash = ops->filter_hash;
2656 iter->ops = ops;
2657 iter->flags = flag;
2659 if (file->f_mode & FMODE_WRITE) {
2660 mutex_lock(&ftrace_lock);
2661 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2662 mutex_unlock(&ftrace_lock);
2664 if (!iter->hash) {
2665 trace_parser_put(&iter->parser);
2666 kfree(iter);
2667 return -ENOMEM;
2671 mutex_lock(&ftrace_regex_lock);
2673 if ((file->f_mode & FMODE_WRITE) &&
2674 (file->f_flags & O_TRUNC))
2675 ftrace_filter_reset(iter->hash);
2677 if (file->f_mode & FMODE_READ) {
2678 iter->pg = ftrace_pages_start;
2680 ret = seq_open(file, &show_ftrace_seq_ops);
2681 if (!ret) {
2682 struct seq_file *m = file->private_data;
2683 m->private = iter;
2684 } else {
2685 /* Failed */
2686 free_ftrace_hash(iter->hash);
2687 trace_parser_put(&iter->parser);
2688 kfree(iter);
2690 } else
2691 file->private_data = iter;
2692 mutex_unlock(&ftrace_regex_lock);
2694 return ret;
2697 static int
2698 ftrace_filter_open(struct inode *inode, struct file *file)
2700 return ftrace_regex_open(&global_ops,
2701 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2702 inode, file);
2705 static int
2706 ftrace_notrace_open(struct inode *inode, struct file *file)
2708 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
2709 inode, file);
2712 static int ftrace_match(char *str, char *regex, int len, int type)
2714 int matched = 0;
2715 int slen;
2717 switch (type) {
2718 case MATCH_FULL:
2719 if (strcmp(str, regex) == 0)
2720 matched = 1;
2721 break;
2722 case MATCH_FRONT_ONLY:
2723 if (strncmp(str, regex, len) == 0)
2724 matched = 1;
2725 break;
2726 case MATCH_MIDDLE_ONLY:
2727 if (strstr(str, regex))
2728 matched = 1;
2729 break;
2730 case MATCH_END_ONLY:
2731 slen = strlen(str);
2732 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
2733 matched = 1;
2734 break;
2737 return matched;
2740 static int
2741 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
2743 struct ftrace_func_entry *entry;
2744 int ret = 0;
2746 entry = ftrace_lookup_ip(hash, rec->ip);
2747 if (not) {
2748 /* Do nothing if it doesn't exist */
2749 if (!entry)
2750 return 0;
2752 free_hash_entry(hash, entry);
2753 } else {
2754 /* Do nothing if it exists */
2755 if (entry)
2756 return 0;
2758 ret = add_hash_entry(hash, rec->ip);
2760 return ret;
2763 static int
2764 ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2765 char *regex, int len, int type)
2767 char str[KSYM_SYMBOL_LEN];
2768 char *modname;
2770 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2772 if (mod) {
2773 /* module lookup requires matching the module */
2774 if (!modname || strcmp(modname, mod))
2775 return 0;
2777 /* blank search means to match all funcs in the mod */
2778 if (!len)
2779 return 1;
2782 return ftrace_match(str, regex, len, type);
2785 static int
2786 match_records(struct ftrace_hash *hash, char *buff,
2787 int len, char *mod, int not)
2789 unsigned search_len = 0;
2790 struct ftrace_page *pg;
2791 struct dyn_ftrace *rec;
2792 int type = MATCH_FULL;
2793 char *search = buff;
2794 int found = 0;
2795 int ret;
2797 if (len) {
2798 type = filter_parse_regex(buff, len, &search, &not);
2799 search_len = strlen(search);
2802 mutex_lock(&ftrace_lock);
2804 if (unlikely(ftrace_disabled))
2805 goto out_unlock;
2807 do_for_each_ftrace_rec(pg, rec) {
2808 if (ftrace_match_record(rec, mod, search, search_len, type)) {
2809 ret = enter_record(hash, rec, not);
2810 if (ret < 0) {
2811 found = ret;
2812 goto out_unlock;
2814 found = 1;
2816 } while_for_each_ftrace_rec();
2817 out_unlock:
2818 mutex_unlock(&ftrace_lock);
2820 return found;
2823 static int
2824 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
2826 return match_records(hash, buff, len, NULL, 0);
2829 static int
2830 ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
2832 int not = 0;
2834 /* blank or '*' mean the same */
2835 if (strcmp(buff, "*") == 0)
2836 buff[0] = 0;
2838 /* handle the case of 'dont filter this module' */
2839 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2840 buff[0] = 0;
2841 not = 1;
2844 return match_records(hash, buff, strlen(buff), mod, not);
2848 * We register the module command as a template to show others how
2849 * to register the a command as well.
2852 static int
2853 ftrace_mod_callback(struct ftrace_hash *hash,
2854 char *func, char *cmd, char *param, int enable)
2856 char *mod;
2857 int ret = -EINVAL;
2860 * cmd == 'mod' because we only registered this func
2861 * for the 'mod' ftrace_func_command.
2862 * But if you register one func with multiple commands,
2863 * you can tell which command was used by the cmd
2864 * parameter.
2867 /* we must have a module name */
2868 if (!param)
2869 return ret;
2871 mod = strsep(&param, ":");
2872 if (!strlen(mod))
2873 return ret;
2875 ret = ftrace_match_module_records(hash, func, mod);
2876 if (!ret)
2877 ret = -EINVAL;
2878 if (ret < 0)
2879 return ret;
2881 return 0;
2884 static struct ftrace_func_command ftrace_mod_cmd = {
2885 .name = "mod",
2886 .func = ftrace_mod_callback,
2889 static int __init ftrace_mod_cmd_init(void)
2891 return register_ftrace_command(&ftrace_mod_cmd);
2893 core_initcall(ftrace_mod_cmd_init);
2895 static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
2896 struct ftrace_ops *op, struct pt_regs *pt_regs)
2898 struct ftrace_func_probe *entry;
2899 struct hlist_head *hhd;
2900 unsigned long key;
2902 key = hash_long(ip, FTRACE_HASH_BITS);
2904 hhd = &ftrace_func_hash[key];
2906 if (hlist_empty(hhd))
2907 return;
2910 * Disable preemption for these calls to prevent a RCU grace
2911 * period. This syncs the hash iteration and freeing of items
2912 * on the hash. rcu_read_lock is too dangerous here.
2914 preempt_disable_notrace();
2915 hlist_for_each_entry_rcu(entry, hhd, node) {
2916 if (entry->ip == ip)
2917 entry->ops->func(ip, parent_ip, &entry->data);
2919 preempt_enable_notrace();
2922 static struct ftrace_ops trace_probe_ops __read_mostly =
2924 .func = function_trace_probe_call,
2927 static int ftrace_probe_registered;
2929 static void __enable_ftrace_function_probe(void)
2931 int ret;
2932 int i;
2934 if (ftrace_probe_registered)
2935 return;
2937 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2938 struct hlist_head *hhd = &ftrace_func_hash[i];
2939 if (hhd->first)
2940 break;
2942 /* Nothing registered? */
2943 if (i == FTRACE_FUNC_HASHSIZE)
2944 return;
2946 ret = __register_ftrace_function(&trace_probe_ops);
2947 if (!ret)
2948 ret = ftrace_startup(&trace_probe_ops, 0);
2950 ftrace_probe_registered = 1;
2953 static void __disable_ftrace_function_probe(void)
2955 int ret;
2956 int i;
2958 if (!ftrace_probe_registered)
2959 return;
2961 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2962 struct hlist_head *hhd = &ftrace_func_hash[i];
2963 if (hhd->first)
2964 return;
2967 /* no more funcs left */
2968 ret = __unregister_ftrace_function(&trace_probe_ops);
2969 if (!ret)
2970 ftrace_shutdown(&trace_probe_ops, 0);
2972 ftrace_probe_registered = 0;
2976 static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2978 struct ftrace_func_probe *entry =
2979 container_of(rhp, struct ftrace_func_probe, rcu);
2981 if (entry->ops->free)
2982 entry->ops->free(&entry->data);
2983 kfree(entry);
2988 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2989 void *data)
2991 struct ftrace_func_probe *entry;
2992 struct ftrace_page *pg;
2993 struct dyn_ftrace *rec;
2994 int type, len, not;
2995 unsigned long key;
2996 int count = 0;
2997 char *search;
2999 type = filter_parse_regex(glob, strlen(glob), &search, &not);
3000 len = strlen(search);
3002 /* we do not support '!' for function probes */
3003 if (WARN_ON(not))
3004 return -EINVAL;
3006 mutex_lock(&ftrace_lock);
3008 if (unlikely(ftrace_disabled))
3009 goto out_unlock;
3011 do_for_each_ftrace_rec(pg, rec) {
3013 if (!ftrace_match_record(rec, NULL, search, len, type))
3014 continue;
3016 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3017 if (!entry) {
3018 /* If we did not process any, then return error */
3019 if (!count)
3020 count = -ENOMEM;
3021 goto out_unlock;
3024 count++;
3026 entry->data = data;
3029 * The caller might want to do something special
3030 * for each function we find. We call the callback
3031 * to give the caller an opportunity to do so.
3033 if (ops->callback) {
3034 if (ops->callback(rec->ip, &entry->data) < 0) {
3035 /* caller does not like this func */
3036 kfree(entry);
3037 continue;
3041 entry->ops = ops;
3042 entry->ip = rec->ip;
3044 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3045 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3047 } while_for_each_ftrace_rec();
3048 __enable_ftrace_function_probe();
3050 out_unlock:
3051 mutex_unlock(&ftrace_lock);
3053 return count;
3056 enum {
3057 PROBE_TEST_FUNC = 1,
3058 PROBE_TEST_DATA = 2
3061 static void
3062 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3063 void *data, int flags)
3065 struct ftrace_func_probe *entry;
3066 struct hlist_node *tmp;
3067 char str[KSYM_SYMBOL_LEN];
3068 int type = MATCH_FULL;
3069 int i, len = 0;
3070 char *search;
3072 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
3073 glob = NULL;
3074 else if (glob) {
3075 int not;
3077 type = filter_parse_regex(glob, strlen(glob), &search, &not);
3078 len = strlen(search);
3080 /* we do not support '!' for function probes */
3081 if (WARN_ON(not))
3082 return;
3085 mutex_lock(&ftrace_lock);
3086 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3087 struct hlist_head *hhd = &ftrace_func_hash[i];
3089 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
3091 /* break up if statements for readability */
3092 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
3093 continue;
3095 if ((flags & PROBE_TEST_DATA) && entry->data != data)
3096 continue;
3098 /* do this last, since it is the most expensive */
3099 if (glob) {
3100 kallsyms_lookup(entry->ip, NULL, NULL,
3101 NULL, str);
3102 if (!ftrace_match(str, glob, len, type))
3103 continue;
3106 hlist_del_rcu(&entry->node);
3107 call_rcu_sched(&entry->rcu, ftrace_free_entry_rcu);
3110 __disable_ftrace_function_probe();
3111 mutex_unlock(&ftrace_lock);
3114 void
3115 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3116 void *data)
3118 __unregister_ftrace_function_probe(glob, ops, data,
3119 PROBE_TEST_FUNC | PROBE_TEST_DATA);
3122 void
3123 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
3125 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
3128 void unregister_ftrace_function_probe_all(char *glob)
3130 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
3133 static LIST_HEAD(ftrace_commands);
3134 static DEFINE_MUTEX(ftrace_cmd_mutex);
3136 int register_ftrace_command(struct ftrace_func_command *cmd)
3138 struct ftrace_func_command *p;
3139 int ret = 0;
3141 mutex_lock(&ftrace_cmd_mutex);
3142 list_for_each_entry(p, &ftrace_commands, list) {
3143 if (strcmp(cmd->name, p->name) == 0) {
3144 ret = -EBUSY;
3145 goto out_unlock;
3148 list_add(&cmd->list, &ftrace_commands);
3149 out_unlock:
3150 mutex_unlock(&ftrace_cmd_mutex);
3152 return ret;
3155 int unregister_ftrace_command(struct ftrace_func_command *cmd)
3157 struct ftrace_func_command *p, *n;
3158 int ret = -ENODEV;
3160 mutex_lock(&ftrace_cmd_mutex);
3161 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3162 if (strcmp(cmd->name, p->name) == 0) {
3163 ret = 0;
3164 list_del_init(&p->list);
3165 goto out_unlock;
3168 out_unlock:
3169 mutex_unlock(&ftrace_cmd_mutex);
3171 return ret;
3174 static int ftrace_process_regex(struct ftrace_hash *hash,
3175 char *buff, int len, int enable)
3177 char *func, *command, *next = buff;
3178 struct ftrace_func_command *p;
3179 int ret = -EINVAL;
3181 func = strsep(&next, ":");
3183 if (!next) {
3184 ret = ftrace_match_records(hash, func, len);
3185 if (!ret)
3186 ret = -EINVAL;
3187 if (ret < 0)
3188 return ret;
3189 return 0;
3192 /* command found */
3194 command = strsep(&next, ":");
3196 mutex_lock(&ftrace_cmd_mutex);
3197 list_for_each_entry(p, &ftrace_commands, list) {
3198 if (strcmp(p->name, command) == 0) {
3199 ret = p->func(hash, func, command, next, enable);
3200 goto out_unlock;
3203 out_unlock:
3204 mutex_unlock(&ftrace_cmd_mutex);
3206 return ret;
3209 static ssize_t
3210 ftrace_regex_write(struct file *file, const char __user *ubuf,
3211 size_t cnt, loff_t *ppos, int enable)
3213 struct ftrace_iterator *iter;
3214 struct trace_parser *parser;
3215 ssize_t ret, read;
3217 if (!cnt)
3218 return 0;
3220 mutex_lock(&ftrace_regex_lock);
3222 ret = -ENODEV;
3223 if (unlikely(ftrace_disabled))
3224 goto out_unlock;
3226 if (file->f_mode & FMODE_READ) {
3227 struct seq_file *m = file->private_data;
3228 iter = m->private;
3229 } else
3230 iter = file->private_data;
3232 parser = &iter->parser;
3233 read = trace_get_user(parser, ubuf, cnt, ppos);
3235 if (read >= 0 && trace_parser_loaded(parser) &&
3236 !trace_parser_cont(parser)) {
3237 ret = ftrace_process_regex(iter->hash, parser->buffer,
3238 parser->idx, enable);
3239 trace_parser_clear(parser);
3240 if (ret)
3241 goto out_unlock;
3244 ret = read;
3245 out_unlock:
3246 mutex_unlock(&ftrace_regex_lock);
3248 return ret;
3251 ssize_t
3252 ftrace_filter_write(struct file *file, const char __user *ubuf,
3253 size_t cnt, loff_t *ppos)
3255 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3258 ssize_t
3259 ftrace_notrace_write(struct file *file, const char __user *ubuf,
3260 size_t cnt, loff_t *ppos)
3262 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3265 static int
3266 ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3268 struct ftrace_func_entry *entry;
3270 if (!ftrace_location(ip))
3271 return -EINVAL;
3273 if (remove) {
3274 entry = ftrace_lookup_ip(hash, ip);
3275 if (!entry)
3276 return -ENOENT;
3277 free_hash_entry(hash, entry);
3278 return 0;
3281 return add_hash_entry(hash, ip);
3284 static int
3285 ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3286 unsigned long ip, int remove, int reset, int enable)
3288 struct ftrace_hash **orig_hash;
3289 struct ftrace_hash *hash;
3290 int ret;
3292 /* All global ops uses the global ops filters */
3293 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3294 ops = &global_ops;
3296 if (unlikely(ftrace_disabled))
3297 return -ENODEV;
3299 if (enable)
3300 orig_hash = &ops->filter_hash;
3301 else
3302 orig_hash = &ops->notrace_hash;
3304 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3305 if (!hash)
3306 return -ENOMEM;
3308 mutex_lock(&ftrace_regex_lock);
3309 if (reset)
3310 ftrace_filter_reset(hash);
3311 if (buf && !ftrace_match_records(hash, buf, len)) {
3312 ret = -EINVAL;
3313 goto out_regex_unlock;
3315 if (ip) {
3316 ret = ftrace_match_addr(hash, ip, remove);
3317 if (ret < 0)
3318 goto out_regex_unlock;
3321 mutex_lock(&ftrace_lock);
3322 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
3323 if (!ret && ops->flags & FTRACE_OPS_FL_ENABLED
3324 && ftrace_enabled)
3325 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3327 mutex_unlock(&ftrace_lock);
3329 out_regex_unlock:
3330 mutex_unlock(&ftrace_regex_lock);
3332 free_ftrace_hash(hash);
3333 return ret;
3336 static int
3337 ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3338 int reset, int enable)
3340 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3344 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3345 * @ops - the ops to set the filter with
3346 * @ip - the address to add to or remove from the filter.
3347 * @remove - non zero to remove the ip from the filter
3348 * @reset - non zero to reset all filters before applying this filter.
3350 * Filters denote which functions should be enabled when tracing is enabled
3351 * If @ip is NULL, it failes to update filter.
3353 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3354 int remove, int reset)
3356 return ftrace_set_addr(ops, ip, remove, reset, 1);
3358 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3360 static int
3361 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3362 int reset, int enable)
3364 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3368 * ftrace_set_filter - set a function to filter on in ftrace
3369 * @ops - the ops to set the filter with
3370 * @buf - the string that holds the function filter text.
3371 * @len - the length of the string.
3372 * @reset - non zero to reset all filters before applying this filter.
3374 * Filters denote which functions should be enabled when tracing is enabled.
3375 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3377 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
3378 int len, int reset)
3380 return ftrace_set_regex(ops, buf, len, reset, 1);
3382 EXPORT_SYMBOL_GPL(ftrace_set_filter);
3385 * ftrace_set_notrace - set a function to not trace in ftrace
3386 * @ops - the ops to set the notrace filter with
3387 * @buf - the string that holds the function notrace text.
3388 * @len - the length of the string.
3389 * @reset - non zero to reset all filters before applying this filter.
3391 * Notrace Filters denote which functions should not be enabled when tracing
3392 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3393 * for tracing.
3395 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
3396 int len, int reset)
3398 return ftrace_set_regex(ops, buf, len, reset, 0);
3400 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3402 * ftrace_set_filter - set a function to filter on in ftrace
3403 * @ops - the ops to set the filter with
3404 * @buf - the string that holds the function filter text.
3405 * @len - the length of the string.
3406 * @reset - non zero to reset all filters before applying this filter.
3408 * Filters denote which functions should be enabled when tracing is enabled.
3409 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3411 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3413 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3415 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3418 * ftrace_set_notrace - set a function to not trace in ftrace
3419 * @ops - the ops to set the notrace filter with
3420 * @buf - the string that holds the function notrace text.
3421 * @len - the length of the string.
3422 * @reset - non zero to reset all filters before applying this filter.
3424 * Notrace Filters denote which functions should not be enabled when tracing
3425 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3426 * for tracing.
3428 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
3430 ftrace_set_regex(&global_ops, buf, len, reset, 0);
3432 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
3435 * command line interface to allow users to set filters on boot up.
3437 #define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3438 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3439 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3441 static int __init set_ftrace_notrace(char *str)
3443 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
3444 return 1;
3446 __setup("ftrace_notrace=", set_ftrace_notrace);
3448 static int __init set_ftrace_filter(char *str)
3450 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
3451 return 1;
3453 __setup("ftrace_filter=", set_ftrace_filter);
3455 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3456 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
3457 static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
3459 static int __init set_graph_function(char *str)
3461 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
3462 return 1;
3464 __setup("ftrace_graph_filter=", set_graph_function);
3466 static void __init set_ftrace_early_graph(char *buf)
3468 int ret;
3469 char *func;
3471 while (buf) {
3472 func = strsep(&buf, ",");
3473 /* we allow only one expression at a time */
3474 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3475 func);
3476 if (ret)
3477 printk(KERN_DEBUG "ftrace: function %s not "
3478 "traceable\n", func);
3481 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3483 void __init
3484 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
3486 char *func;
3488 while (buf) {
3489 func = strsep(&buf, ",");
3490 ftrace_set_regex(ops, func, strlen(func), 0, enable);
3494 static void __init set_ftrace_early_filters(void)
3496 if (ftrace_filter_buf[0])
3497 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
3498 if (ftrace_notrace_buf[0])
3499 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
3500 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3501 if (ftrace_graph_buf[0])
3502 set_ftrace_early_graph(ftrace_graph_buf);
3503 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3506 int ftrace_regex_release(struct inode *inode, struct file *file)
3508 struct seq_file *m = (struct seq_file *)file->private_data;
3509 struct ftrace_iterator *iter;
3510 struct ftrace_hash **orig_hash;
3511 struct trace_parser *parser;
3512 int filter_hash;
3513 int ret;
3515 mutex_lock(&ftrace_regex_lock);
3516 if (file->f_mode & FMODE_READ) {
3517 iter = m->private;
3519 seq_release(inode, file);
3520 } else
3521 iter = file->private_data;
3523 parser = &iter->parser;
3524 if (trace_parser_loaded(parser)) {
3525 parser->buffer[parser->idx] = 0;
3526 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
3529 trace_parser_put(parser);
3531 if (file->f_mode & FMODE_WRITE) {
3532 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3534 if (filter_hash)
3535 orig_hash = &iter->ops->filter_hash;
3536 else
3537 orig_hash = &iter->ops->notrace_hash;
3539 mutex_lock(&ftrace_lock);
3540 ret = ftrace_hash_move(iter->ops, filter_hash,
3541 orig_hash, iter->hash);
3542 if (!ret && (iter->ops->flags & FTRACE_OPS_FL_ENABLED)
3543 && ftrace_enabled)
3544 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3546 mutex_unlock(&ftrace_lock);
3548 free_ftrace_hash(iter->hash);
3549 kfree(iter);
3551 mutex_unlock(&ftrace_regex_lock);
3552 return 0;
3555 static const struct file_operations ftrace_avail_fops = {
3556 .open = ftrace_avail_open,
3557 .read = seq_read,
3558 .llseek = seq_lseek,
3559 .release = seq_release_private,
3562 static const struct file_operations ftrace_enabled_fops = {
3563 .open = ftrace_enabled_open,
3564 .read = seq_read,
3565 .llseek = seq_lseek,
3566 .release = seq_release_private,
3569 static const struct file_operations ftrace_filter_fops = {
3570 .open = ftrace_filter_open,
3571 .read = seq_read,
3572 .write = ftrace_filter_write,
3573 .llseek = ftrace_filter_lseek,
3574 .release = ftrace_regex_release,
3577 static const struct file_operations ftrace_notrace_fops = {
3578 .open = ftrace_notrace_open,
3579 .read = seq_read,
3580 .write = ftrace_notrace_write,
3581 .llseek = ftrace_filter_lseek,
3582 .release = ftrace_regex_release,
3585 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3587 static DEFINE_MUTEX(graph_lock);
3589 int ftrace_graph_count;
3590 int ftrace_graph_filter_enabled;
3591 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3593 static void *
3594 __g_next(struct seq_file *m, loff_t *pos)
3596 if (*pos >= ftrace_graph_count)
3597 return NULL;
3598 return &ftrace_graph_funcs[*pos];
3601 static void *
3602 g_next(struct seq_file *m, void *v, loff_t *pos)
3604 (*pos)++;
3605 return __g_next(m, pos);
3608 static void *g_start(struct seq_file *m, loff_t *pos)
3610 mutex_lock(&graph_lock);
3612 /* Nothing, tell g_show to print all functions are enabled */
3613 if (!ftrace_graph_filter_enabled && !*pos)
3614 return (void *)1;
3616 return __g_next(m, pos);
3619 static void g_stop(struct seq_file *m, void *p)
3621 mutex_unlock(&graph_lock);
3624 static int g_show(struct seq_file *m, void *v)
3626 unsigned long *ptr = v;
3628 if (!ptr)
3629 return 0;
3631 if (ptr == (unsigned long *)1) {
3632 seq_printf(m, "#### all functions enabled ####\n");
3633 return 0;
3636 seq_printf(m, "%ps\n", (void *)*ptr);
3638 return 0;
3641 static const struct seq_operations ftrace_graph_seq_ops = {
3642 .start = g_start,
3643 .next = g_next,
3644 .stop = g_stop,
3645 .show = g_show,
3648 static int
3649 ftrace_graph_open(struct inode *inode, struct file *file)
3651 int ret = 0;
3653 if (unlikely(ftrace_disabled))
3654 return -ENODEV;
3656 mutex_lock(&graph_lock);
3657 if ((file->f_mode & FMODE_WRITE) &&
3658 (file->f_flags & O_TRUNC)) {
3659 ftrace_graph_filter_enabled = 0;
3660 ftrace_graph_count = 0;
3661 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3663 mutex_unlock(&graph_lock);
3665 if (file->f_mode & FMODE_READ)
3666 ret = seq_open(file, &ftrace_graph_seq_ops);
3668 return ret;
3671 static int
3672 ftrace_graph_release(struct inode *inode, struct file *file)
3674 if (file->f_mode & FMODE_READ)
3675 seq_release(inode, file);
3676 return 0;
3679 static int
3680 ftrace_set_func(unsigned long *array, int *idx, char *buffer)
3682 struct dyn_ftrace *rec;
3683 struct ftrace_page *pg;
3684 int search_len;
3685 int fail = 1;
3686 int type, not;
3687 char *search;
3688 bool exists;
3689 int i;
3691 /* decode regex */
3692 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
3693 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3694 return -EBUSY;
3696 search_len = strlen(search);
3698 mutex_lock(&ftrace_lock);
3700 if (unlikely(ftrace_disabled)) {
3701 mutex_unlock(&ftrace_lock);
3702 return -ENODEV;
3705 do_for_each_ftrace_rec(pg, rec) {
3707 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
3708 /* if it is in the array */
3709 exists = false;
3710 for (i = 0; i < *idx; i++) {
3711 if (array[i] == rec->ip) {
3712 exists = true;
3713 break;
3717 if (!not) {
3718 fail = 0;
3719 if (!exists) {
3720 array[(*idx)++] = rec->ip;
3721 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3722 goto out;
3724 } else {
3725 if (exists) {
3726 array[i] = array[--(*idx)];
3727 array[*idx] = 0;
3728 fail = 0;
3732 } while_for_each_ftrace_rec();
3733 out:
3734 mutex_unlock(&ftrace_lock);
3736 if (fail)
3737 return -EINVAL;
3739 ftrace_graph_filter_enabled = 1;
3740 return 0;
3743 static ssize_t
3744 ftrace_graph_write(struct file *file, const char __user *ubuf,
3745 size_t cnt, loff_t *ppos)
3747 struct trace_parser parser;
3748 ssize_t read, ret;
3750 if (!cnt)
3751 return 0;
3753 mutex_lock(&graph_lock);
3755 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3756 ret = -ENOMEM;
3757 goto out_unlock;
3760 read = trace_get_user(&parser, ubuf, cnt, ppos);
3762 if (read >= 0 && trace_parser_loaded((&parser))) {
3763 parser.buffer[parser.idx] = 0;
3765 /* we allow only one expression at a time */
3766 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3767 parser.buffer);
3768 if (ret)
3769 goto out_free;
3772 ret = read;
3774 out_free:
3775 trace_parser_put(&parser);
3776 out_unlock:
3777 mutex_unlock(&graph_lock);
3779 return ret;
3782 static const struct file_operations ftrace_graph_fops = {
3783 .open = ftrace_graph_open,
3784 .read = seq_read,
3785 .write = ftrace_graph_write,
3786 .llseek = ftrace_filter_lseek,
3787 .release = ftrace_graph_release,
3789 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3791 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
3794 trace_create_file("available_filter_functions", 0444,
3795 d_tracer, NULL, &ftrace_avail_fops);
3797 trace_create_file("enabled_functions", 0444,
3798 d_tracer, NULL, &ftrace_enabled_fops);
3800 trace_create_file("set_ftrace_filter", 0644, d_tracer,
3801 NULL, &ftrace_filter_fops);
3803 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
3804 NULL, &ftrace_notrace_fops);
3806 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3807 trace_create_file("set_graph_function", 0444, d_tracer,
3808 NULL,
3809 &ftrace_graph_fops);
3810 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3812 return 0;
3815 static int ftrace_cmp_ips(const void *a, const void *b)
3817 const unsigned long *ipa = a;
3818 const unsigned long *ipb = b;
3820 if (*ipa > *ipb)
3821 return 1;
3822 if (*ipa < *ipb)
3823 return -1;
3824 return 0;
3827 static void ftrace_swap_ips(void *a, void *b, int size)
3829 unsigned long *ipa = a;
3830 unsigned long *ipb = b;
3831 unsigned long t;
3833 t = *ipa;
3834 *ipa = *ipb;
3835 *ipb = t;
3838 static int ftrace_process_locs(struct module *mod,
3839 unsigned long *start,
3840 unsigned long *end)
3842 struct ftrace_page *start_pg;
3843 struct ftrace_page *pg;
3844 struct dyn_ftrace *rec;
3845 unsigned long count;
3846 unsigned long *p;
3847 unsigned long addr;
3848 unsigned long flags = 0; /* Shut up gcc */
3849 int ret = -ENOMEM;
3851 count = end - start;
3853 if (!count)
3854 return 0;
3856 sort(start, count, sizeof(*start),
3857 ftrace_cmp_ips, ftrace_swap_ips);
3859 start_pg = ftrace_allocate_pages(count);
3860 if (!start_pg)
3861 return -ENOMEM;
3863 mutex_lock(&ftrace_lock);
3866 * Core and each module needs their own pages, as
3867 * modules will free them when they are removed.
3868 * Force a new page to be allocated for modules.
3870 if (!mod) {
3871 WARN_ON(ftrace_pages || ftrace_pages_start);
3872 /* First initialization */
3873 ftrace_pages = ftrace_pages_start = start_pg;
3874 } else {
3875 if (!ftrace_pages)
3876 goto out;
3878 if (WARN_ON(ftrace_pages->next)) {
3879 /* Hmm, we have free pages? */
3880 while (ftrace_pages->next)
3881 ftrace_pages = ftrace_pages->next;
3884 ftrace_pages->next = start_pg;
3887 p = start;
3888 pg = start_pg;
3889 while (p < end) {
3890 addr = ftrace_call_adjust(*p++);
3892 * Some architecture linkers will pad between
3893 * the different mcount_loc sections of different
3894 * object files to satisfy alignments.
3895 * Skip any NULL pointers.
3897 if (!addr)
3898 continue;
3900 if (pg->index == pg->size) {
3901 /* We should have allocated enough */
3902 if (WARN_ON(!pg->next))
3903 break;
3904 pg = pg->next;
3907 rec = &pg->records[pg->index++];
3908 rec->ip = addr;
3911 /* We should have used all pages */
3912 WARN_ON(pg->next);
3914 /* Assign the last page to ftrace_pages */
3915 ftrace_pages = pg;
3917 /* These new locations need to be initialized */
3918 ftrace_new_pgs = start_pg;
3921 * We only need to disable interrupts on start up
3922 * because we are modifying code that an interrupt
3923 * may execute, and the modification is not atomic.
3924 * But for modules, nothing runs the code we modify
3925 * until we are finished with it, and there's no
3926 * reason to cause large interrupt latencies while we do it.
3928 if (!mod)
3929 local_irq_save(flags);
3930 ftrace_update_code(mod);
3931 if (!mod)
3932 local_irq_restore(flags);
3933 ret = 0;
3934 out:
3935 mutex_unlock(&ftrace_lock);
3937 return ret;
3940 #ifdef CONFIG_MODULES
3942 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
3944 void ftrace_release_mod(struct module *mod)
3946 struct dyn_ftrace *rec;
3947 struct ftrace_page **last_pg;
3948 struct ftrace_page *pg;
3949 int order;
3951 mutex_lock(&ftrace_lock);
3953 if (ftrace_disabled)
3954 goto out_unlock;
3957 * Each module has its own ftrace_pages, remove
3958 * them from the list.
3960 last_pg = &ftrace_pages_start;
3961 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
3962 rec = &pg->records[0];
3963 if (within_module_core(rec->ip, mod)) {
3965 * As core pages are first, the first
3966 * page should never be a module page.
3968 if (WARN_ON(pg == ftrace_pages_start))
3969 goto out_unlock;
3971 /* Check if we are deleting the last page */
3972 if (pg == ftrace_pages)
3973 ftrace_pages = next_to_ftrace_page(last_pg);
3975 *last_pg = pg->next;
3976 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3977 free_pages((unsigned long)pg->records, order);
3978 kfree(pg);
3979 } else
3980 last_pg = &pg->next;
3982 out_unlock:
3983 mutex_unlock(&ftrace_lock);
3986 static void ftrace_init_module(struct module *mod,
3987 unsigned long *start, unsigned long *end)
3989 if (ftrace_disabled || start == end)
3990 return;
3991 ftrace_process_locs(mod, start, end);
3994 static int ftrace_module_notify_enter(struct notifier_block *self,
3995 unsigned long val, void *data)
3997 struct module *mod = data;
3999 if (val == MODULE_STATE_COMING)
4000 ftrace_init_module(mod, mod->ftrace_callsites,
4001 mod->ftrace_callsites +
4002 mod->num_ftrace_callsites);
4003 return 0;
4006 static int ftrace_module_notify_exit(struct notifier_block *self,
4007 unsigned long val, void *data)
4009 struct module *mod = data;
4011 if (val == MODULE_STATE_GOING)
4012 ftrace_release_mod(mod);
4014 return 0;
4016 #else
4017 static int ftrace_module_notify_enter(struct notifier_block *self,
4018 unsigned long val, void *data)
4020 return 0;
4022 static int ftrace_module_notify_exit(struct notifier_block *self,
4023 unsigned long val, void *data)
4025 return 0;
4027 #endif /* CONFIG_MODULES */
4029 struct notifier_block ftrace_module_enter_nb = {
4030 .notifier_call = ftrace_module_notify_enter,
4031 .priority = INT_MAX, /* Run before anything that can use kprobes */
4034 struct notifier_block ftrace_module_exit_nb = {
4035 .notifier_call = ftrace_module_notify_exit,
4036 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4039 extern unsigned long __start_mcount_loc[];
4040 extern unsigned long __stop_mcount_loc[];
4042 void __init ftrace_init(void)
4044 unsigned long count, addr, flags;
4045 int ret;
4047 /* Keep the ftrace pointer to the stub */
4048 addr = (unsigned long)ftrace_stub;
4050 local_irq_save(flags);
4051 ftrace_dyn_arch_init(&addr);
4052 local_irq_restore(flags);
4054 /* ftrace_dyn_arch_init places the return code in addr */
4055 if (addr)
4056 goto failed;
4058 count = __stop_mcount_loc - __start_mcount_loc;
4060 ret = ftrace_dyn_table_alloc(count);
4061 if (ret)
4062 goto failed;
4064 last_ftrace_enabled = ftrace_enabled = 1;
4066 ret = ftrace_process_locs(NULL,
4067 __start_mcount_loc,
4068 __stop_mcount_loc);
4070 ret = register_module_notifier(&ftrace_module_enter_nb);
4071 if (ret)
4072 pr_warning("Failed to register trace ftrace module enter notifier\n");
4074 ret = register_module_notifier(&ftrace_module_exit_nb);
4075 if (ret)
4076 pr_warning("Failed to register trace ftrace module exit notifier\n");
4078 set_ftrace_early_filters();
4080 return;
4081 failed:
4082 ftrace_disabled = 1;
4085 #else
4087 static struct ftrace_ops global_ops = {
4088 .func = ftrace_stub,
4089 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
4092 static int __init ftrace_nodyn_init(void)
4094 ftrace_enabled = 1;
4095 return 0;
4097 core_initcall(ftrace_nodyn_init);
4099 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4100 static inline void ftrace_startup_enable(int command) { }
4101 /* Keep as macros so we do not need to define the commands */
4102 # define ftrace_startup(ops, command) \
4103 ({ \
4104 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4105 0; \
4107 # define ftrace_shutdown(ops, command) do { } while (0)
4108 # define ftrace_startup_sysctl() do { } while (0)
4109 # define ftrace_shutdown_sysctl() do { } while (0)
4111 static inline int
4112 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
4114 return 1;
4117 #endif /* CONFIG_DYNAMIC_FTRACE */
4119 static void
4120 ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
4121 struct ftrace_ops *op, struct pt_regs *regs)
4123 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4124 return;
4127 * Some of the ops may be dynamically allocated,
4128 * they must be freed after a synchronize_sched().
4130 preempt_disable_notrace();
4131 trace_recursion_set(TRACE_CONTROL_BIT);
4132 do_for_each_ftrace_op(op, ftrace_control_list) {
4133 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
4134 !ftrace_function_local_disabled(op) &&
4135 ftrace_ops_test(op, ip))
4136 op->func(ip, parent_ip, op, regs);
4137 } while_for_each_ftrace_op(op);
4138 trace_recursion_clear(TRACE_CONTROL_BIT);
4139 preempt_enable_notrace();
4142 static struct ftrace_ops control_ops = {
4143 .func = ftrace_ops_control_func,
4144 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
4147 static inline void
4148 __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
4149 struct ftrace_ops *ignored, struct pt_regs *regs)
4151 struct ftrace_ops *op;
4152 int bit;
4154 if (function_trace_stop)
4155 return;
4157 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4158 if (bit < 0)
4159 return;
4162 * Some of the ops may be dynamically allocated,
4163 * they must be freed after a synchronize_sched().
4165 preempt_disable_notrace();
4166 do_for_each_ftrace_op(op, ftrace_ops_list) {
4167 if (ftrace_ops_test(op, ip))
4168 op->func(ip, parent_ip, op, regs);
4169 } while_for_each_ftrace_op(op);
4170 preempt_enable_notrace();
4171 trace_clear_recursion(bit);
4175 * Some archs only support passing ip and parent_ip. Even though
4176 * the list function ignores the op parameter, we do not want any
4177 * C side effects, where a function is called without the caller
4178 * sending a third parameter.
4179 * Archs are to support both the regs and ftrace_ops at the same time.
4180 * If they support ftrace_ops, it is assumed they support regs.
4181 * If call backs want to use regs, they must either check for regs
4182 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4183 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
4184 * An architecture can pass partial regs with ftrace_ops and still
4185 * set the ARCH_SUPPORT_FTARCE_OPS.
4187 #if ARCH_SUPPORTS_FTRACE_OPS
4188 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
4189 struct ftrace_ops *op, struct pt_regs *regs)
4191 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
4193 #else
4194 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4196 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
4198 #endif
4200 static void clear_ftrace_swapper(void)
4202 struct task_struct *p;
4203 int cpu;
4205 get_online_cpus();
4206 for_each_online_cpu(cpu) {
4207 p = idle_task(cpu);
4208 clear_tsk_trace_trace(p);
4210 put_online_cpus();
4213 static void set_ftrace_swapper(void)
4215 struct task_struct *p;
4216 int cpu;
4218 get_online_cpus();
4219 for_each_online_cpu(cpu) {
4220 p = idle_task(cpu);
4221 set_tsk_trace_trace(p);
4223 put_online_cpus();
4226 static void clear_ftrace_pid(struct pid *pid)
4228 struct task_struct *p;
4230 rcu_read_lock();
4231 do_each_pid_task(pid, PIDTYPE_PID, p) {
4232 clear_tsk_trace_trace(p);
4233 } while_each_pid_task(pid, PIDTYPE_PID, p);
4234 rcu_read_unlock();
4236 put_pid(pid);
4239 static void set_ftrace_pid(struct pid *pid)
4241 struct task_struct *p;
4243 rcu_read_lock();
4244 do_each_pid_task(pid, PIDTYPE_PID, p) {
4245 set_tsk_trace_trace(p);
4246 } while_each_pid_task(pid, PIDTYPE_PID, p);
4247 rcu_read_unlock();
4250 static void clear_ftrace_pid_task(struct pid *pid)
4252 if (pid == ftrace_swapper_pid)
4253 clear_ftrace_swapper();
4254 else
4255 clear_ftrace_pid(pid);
4258 static void set_ftrace_pid_task(struct pid *pid)
4260 if (pid == ftrace_swapper_pid)
4261 set_ftrace_swapper();
4262 else
4263 set_ftrace_pid(pid);
4266 static int ftrace_pid_add(int p)
4268 struct pid *pid;
4269 struct ftrace_pid *fpid;
4270 int ret = -EINVAL;
4272 mutex_lock(&ftrace_lock);
4274 if (!p)
4275 pid = ftrace_swapper_pid;
4276 else
4277 pid = find_get_pid(p);
4279 if (!pid)
4280 goto out;
4282 ret = 0;
4284 list_for_each_entry(fpid, &ftrace_pids, list)
4285 if (fpid->pid == pid)
4286 goto out_put;
4288 ret = -ENOMEM;
4290 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4291 if (!fpid)
4292 goto out_put;
4294 list_add(&fpid->list, &ftrace_pids);
4295 fpid->pid = pid;
4297 set_ftrace_pid_task(pid);
4299 ftrace_update_pid_func();
4300 ftrace_startup_enable(0);
4302 mutex_unlock(&ftrace_lock);
4303 return 0;
4305 out_put:
4306 if (pid != ftrace_swapper_pid)
4307 put_pid(pid);
4309 out:
4310 mutex_unlock(&ftrace_lock);
4311 return ret;
4314 static void ftrace_pid_reset(void)
4316 struct ftrace_pid *fpid, *safe;
4318 mutex_lock(&ftrace_lock);
4319 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4320 struct pid *pid = fpid->pid;
4322 clear_ftrace_pid_task(pid);
4324 list_del(&fpid->list);
4325 kfree(fpid);
4328 ftrace_update_pid_func();
4329 ftrace_startup_enable(0);
4331 mutex_unlock(&ftrace_lock);
4334 static void *fpid_start(struct seq_file *m, loff_t *pos)
4336 mutex_lock(&ftrace_lock);
4338 if (list_empty(&ftrace_pids) && (!*pos))
4339 return (void *) 1;
4341 return seq_list_start(&ftrace_pids, *pos);
4344 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4346 if (v == (void *)1)
4347 return NULL;
4349 return seq_list_next(v, &ftrace_pids, pos);
4352 static void fpid_stop(struct seq_file *m, void *p)
4354 mutex_unlock(&ftrace_lock);
4357 static int fpid_show(struct seq_file *m, void *v)
4359 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4361 if (v == (void *)1) {
4362 seq_printf(m, "no pid\n");
4363 return 0;
4366 if (fpid->pid == ftrace_swapper_pid)
4367 seq_printf(m, "swapper tasks\n");
4368 else
4369 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4371 return 0;
4374 static const struct seq_operations ftrace_pid_sops = {
4375 .start = fpid_start,
4376 .next = fpid_next,
4377 .stop = fpid_stop,
4378 .show = fpid_show,
4381 static int
4382 ftrace_pid_open(struct inode *inode, struct file *file)
4384 int ret = 0;
4386 if ((file->f_mode & FMODE_WRITE) &&
4387 (file->f_flags & O_TRUNC))
4388 ftrace_pid_reset();
4390 if (file->f_mode & FMODE_READ)
4391 ret = seq_open(file, &ftrace_pid_sops);
4393 return ret;
4396 static ssize_t
4397 ftrace_pid_write(struct file *filp, const char __user *ubuf,
4398 size_t cnt, loff_t *ppos)
4400 char buf[64], *tmp;
4401 long val;
4402 int ret;
4404 if (cnt >= sizeof(buf))
4405 return -EINVAL;
4407 if (copy_from_user(&buf, ubuf, cnt))
4408 return -EFAULT;
4410 buf[cnt] = 0;
4413 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4414 * to clean the filter quietly.
4416 tmp = strstrip(buf);
4417 if (strlen(tmp) == 0)
4418 return 1;
4420 ret = kstrtol(tmp, 10, &val);
4421 if (ret < 0)
4422 return ret;
4424 ret = ftrace_pid_add(val);
4426 return ret ? ret : cnt;
4429 static int
4430 ftrace_pid_release(struct inode *inode, struct file *file)
4432 if (file->f_mode & FMODE_READ)
4433 seq_release(inode, file);
4435 return 0;
4438 static const struct file_operations ftrace_pid_fops = {
4439 .open = ftrace_pid_open,
4440 .write = ftrace_pid_write,
4441 .read = seq_read,
4442 .llseek = ftrace_filter_lseek,
4443 .release = ftrace_pid_release,
4446 static __init int ftrace_init_debugfs(void)
4448 struct dentry *d_tracer;
4450 d_tracer = tracing_init_dentry();
4451 if (!d_tracer)
4452 return 0;
4454 ftrace_init_dyn_debugfs(d_tracer);
4456 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4457 NULL, &ftrace_pid_fops);
4459 ftrace_profile_debugfs(d_tracer);
4461 return 0;
4463 fs_initcall(ftrace_init_debugfs);
4466 * ftrace_kill - kill ftrace
4468 * This function should be used by panic code. It stops ftrace
4469 * but in a not so nice way. If you need to simply kill ftrace
4470 * from a non-atomic section, use ftrace_kill.
4472 void ftrace_kill(void)
4474 ftrace_disabled = 1;
4475 ftrace_enabled = 0;
4476 clear_ftrace_function();
4480 * Test if ftrace is dead or not.
4482 int ftrace_is_dead(void)
4484 return ftrace_disabled;
4488 * register_ftrace_function - register a function for profiling
4489 * @ops - ops structure that holds the function for profiling.
4491 * Register a function to be called by all functions in the
4492 * kernel.
4494 * Note: @ops->func and all the functions it calls must be labeled
4495 * with "notrace", otherwise it will go into a
4496 * recursive loop.
4498 int register_ftrace_function(struct ftrace_ops *ops)
4500 int ret = -1;
4502 mutex_lock(&ftrace_lock);
4504 ret = __register_ftrace_function(ops);
4505 if (!ret)
4506 ret = ftrace_startup(ops, 0);
4508 mutex_unlock(&ftrace_lock);
4510 return ret;
4512 EXPORT_SYMBOL_GPL(register_ftrace_function);
4515 * unregister_ftrace_function - unregister a function for profiling.
4516 * @ops - ops structure that holds the function to unregister
4518 * Unregister a function that was added to be called by ftrace profiling.
4520 int unregister_ftrace_function(struct ftrace_ops *ops)
4522 int ret;
4524 mutex_lock(&ftrace_lock);
4525 ret = __unregister_ftrace_function(ops);
4526 if (!ret)
4527 ftrace_shutdown(ops, 0);
4528 mutex_unlock(&ftrace_lock);
4530 return ret;
4532 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
4535 ftrace_enable_sysctl(struct ctl_table *table, int write,
4536 void __user *buffer, size_t *lenp,
4537 loff_t *ppos)
4539 int ret = -ENODEV;
4541 mutex_lock(&ftrace_lock);
4543 if (unlikely(ftrace_disabled))
4544 goto out;
4546 ret = proc_dointvec(table, write, buffer, lenp, ppos);
4548 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
4549 goto out;
4551 last_ftrace_enabled = !!ftrace_enabled;
4553 if (ftrace_enabled) {
4555 ftrace_startup_sysctl();
4557 /* we are starting ftrace again */
4558 if (ftrace_ops_list != &ftrace_list_end)
4559 update_ftrace_function();
4561 } else {
4562 /* stopping ftrace calls (just send to ftrace_stub) */
4563 ftrace_trace_function = ftrace_stub;
4565 ftrace_shutdown_sysctl();
4568 out:
4569 mutex_unlock(&ftrace_lock);
4570 return ret;
4573 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4575 static int ftrace_graph_active;
4576 static struct notifier_block ftrace_suspend_notifier;
4578 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4580 return 0;
4583 /* The callbacks that hook a function */
4584 trace_func_graph_ret_t ftrace_graph_return =
4585 (trace_func_graph_ret_t)ftrace_stub;
4586 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
4588 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4589 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4591 int i;
4592 int ret = 0;
4593 unsigned long flags;
4594 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4595 struct task_struct *g, *t;
4597 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4598 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4599 * sizeof(struct ftrace_ret_stack),
4600 GFP_KERNEL);
4601 if (!ret_stack_list[i]) {
4602 start = 0;
4603 end = i;
4604 ret = -ENOMEM;
4605 goto free;
4609 read_lock_irqsave(&tasklist_lock, flags);
4610 do_each_thread(g, t) {
4611 if (start == end) {
4612 ret = -EAGAIN;
4613 goto unlock;
4616 if (t->ret_stack == NULL) {
4617 atomic_set(&t->tracing_graph_pause, 0);
4618 atomic_set(&t->trace_overrun, 0);
4619 t->curr_ret_stack = -1;
4620 /* Make sure the tasks see the -1 first: */
4621 smp_wmb();
4622 t->ret_stack = ret_stack_list[start++];
4624 } while_each_thread(g, t);
4626 unlock:
4627 read_unlock_irqrestore(&tasklist_lock, flags);
4628 free:
4629 for (i = start; i < end; i++)
4630 kfree(ret_stack_list[i]);
4631 return ret;
4634 static void
4635 ftrace_graph_probe_sched_switch(void *ignore,
4636 struct task_struct *prev, struct task_struct *next)
4638 unsigned long long timestamp;
4639 int index;
4642 * Does the user want to count the time a function was asleep.
4643 * If so, do not update the time stamps.
4645 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4646 return;
4648 timestamp = trace_clock_local();
4650 prev->ftrace_timestamp = timestamp;
4652 /* only process tasks that we timestamped */
4653 if (!next->ftrace_timestamp)
4654 return;
4657 * Update all the counters in next to make up for the
4658 * time next was sleeping.
4660 timestamp -= next->ftrace_timestamp;
4662 for (index = next->curr_ret_stack; index >= 0; index--)
4663 next->ret_stack[index].calltime += timestamp;
4666 /* Allocate a return stack for each task */
4667 static int start_graph_tracing(void)
4669 struct ftrace_ret_stack **ret_stack_list;
4670 int ret, cpu;
4672 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4673 sizeof(struct ftrace_ret_stack *),
4674 GFP_KERNEL);
4676 if (!ret_stack_list)
4677 return -ENOMEM;
4679 /* The cpu_boot init_task->ret_stack will never be freed */
4680 for_each_online_cpu(cpu) {
4681 if (!idle_task(cpu)->ret_stack)
4682 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
4685 do {
4686 ret = alloc_retstack_tasklist(ret_stack_list);
4687 } while (ret == -EAGAIN);
4689 if (!ret) {
4690 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
4691 if (ret)
4692 pr_info("ftrace_graph: Couldn't activate tracepoint"
4693 " probe to kernel_sched_switch\n");
4696 kfree(ret_stack_list);
4697 return ret;
4701 * Hibernation protection.
4702 * The state of the current task is too much unstable during
4703 * suspend/restore to disk. We want to protect against that.
4705 static int
4706 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4707 void *unused)
4709 switch (state) {
4710 case PM_HIBERNATION_PREPARE:
4711 pause_graph_tracing();
4712 break;
4714 case PM_POST_HIBERNATION:
4715 unpause_graph_tracing();
4716 break;
4718 return NOTIFY_DONE;
4721 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4722 trace_func_graph_ent_t entryfunc)
4724 int ret = 0;
4726 mutex_lock(&ftrace_lock);
4728 /* we currently allow only one tracer registered at a time */
4729 if (ftrace_graph_active) {
4730 ret = -EBUSY;
4731 goto out;
4734 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
4735 register_pm_notifier(&ftrace_suspend_notifier);
4737 ftrace_graph_active++;
4738 ret = start_graph_tracing();
4739 if (ret) {
4740 ftrace_graph_active--;
4741 goto out;
4744 ftrace_graph_return = retfunc;
4745 ftrace_graph_entry = entryfunc;
4747 ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
4749 out:
4750 mutex_unlock(&ftrace_lock);
4751 return ret;
4754 void unregister_ftrace_graph(void)
4756 mutex_lock(&ftrace_lock);
4758 if (unlikely(!ftrace_graph_active))
4759 goto out;
4761 ftrace_graph_active--;
4762 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
4763 ftrace_graph_entry = ftrace_graph_entry_stub;
4764 ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
4765 unregister_pm_notifier(&ftrace_suspend_notifier);
4766 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
4768 out:
4769 mutex_unlock(&ftrace_lock);
4772 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
4774 static void
4775 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
4777 atomic_set(&t->tracing_graph_pause, 0);
4778 atomic_set(&t->trace_overrun, 0);
4779 t->ftrace_timestamp = 0;
4780 /* make curr_ret_stack visible before we add the ret_stack */
4781 smp_wmb();
4782 t->ret_stack = ret_stack;
4786 * Allocate a return stack for the idle task. May be the first
4787 * time through, or it may be done by CPU hotplug online.
4789 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
4791 t->curr_ret_stack = -1;
4793 * The idle task has no parent, it either has its own
4794 * stack or no stack at all.
4796 if (t->ret_stack)
4797 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
4799 if (ftrace_graph_active) {
4800 struct ftrace_ret_stack *ret_stack;
4802 ret_stack = per_cpu(idle_ret_stack, cpu);
4803 if (!ret_stack) {
4804 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4805 * sizeof(struct ftrace_ret_stack),
4806 GFP_KERNEL);
4807 if (!ret_stack)
4808 return;
4809 per_cpu(idle_ret_stack, cpu) = ret_stack;
4811 graph_init_task(t, ret_stack);
4815 /* Allocate a return stack for newly created task */
4816 void ftrace_graph_init_task(struct task_struct *t)
4818 /* Make sure we do not use the parent ret_stack */
4819 t->ret_stack = NULL;
4820 t->curr_ret_stack = -1;
4822 if (ftrace_graph_active) {
4823 struct ftrace_ret_stack *ret_stack;
4825 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4826 * sizeof(struct ftrace_ret_stack),
4827 GFP_KERNEL);
4828 if (!ret_stack)
4829 return;
4830 graph_init_task(t, ret_stack);
4834 void ftrace_graph_exit_task(struct task_struct *t)
4836 struct ftrace_ret_stack *ret_stack = t->ret_stack;
4838 t->ret_stack = NULL;
4839 /* NULL must become visible to IRQs before we free it: */
4840 barrier();
4842 kfree(ret_stack);
4845 void ftrace_graph_stop(void)
4847 ftrace_stop();
4849 #endif