]> git.karo-electronics.de Git - mv-sheeva.git/blob - kernel/trace/ftrace.c
ftrace: Fix dynamic selftest failure on some archs
[mv-sheeva.git] / kernel / trace / ftrace.c
1 /*
2  * Infrastructure for profiling code inserted by 'gcc -pg'.
3  *
4  * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5  * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
6  *
7  * Originally ported from the -rt patch by:
8  *   Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
9  *
10  * Based on code in the latency_tracer, that is:
11  *
12  *  Copyright (C) 2004-2006 Ingo Molnar
13  *  Copyright (C) 2004 William Lee Irwin III
14  */
15
16 #include <linux/stop_machine.h>
17 #include <linux/clocksource.h>
18 #include <linux/kallsyms.h>
19 #include <linux/seq_file.h>
20 #include <linux/suspend.h>
21 #include <linux/debugfs.h>
22 #include <linux/hardirq.h>
23 #include <linux/kthread.h>
24 #include <linux/uaccess.h>
25 #include <linux/ftrace.h>
26 #include <linux/sysctl.h>
27 #include <linux/slab.h>
28 #include <linux/ctype.h>
29 #include <linux/list.h>
30 #include <linux/hash.h>
31 #include <linux/rcupdate.h>
32
33 #include <trace/events/sched.h>
34
35 #include <asm/setup.h>
36
37 #include "trace_output.h"
38 #include "trace_stat.h"
39
40 #define FTRACE_WARN_ON(cond)                    \
41         ({                                      \
42                 int ___r = cond;                \
43                 if (WARN_ON(___r))              \
44                         ftrace_kill();          \
45                 ___r;                           \
46         })
47
48 #define FTRACE_WARN_ON_ONCE(cond)               \
49         ({                                      \
50                 int ___r = cond;                \
51                 if (WARN_ON_ONCE(___r))         \
52                         ftrace_kill();          \
53                 ___r;                           \
54         })
55
56 /* hash bits for specific function selection */
57 #define FTRACE_HASH_BITS 7
58 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
59 #define FTRACE_HASH_DEFAULT_BITS 10
60 #define FTRACE_HASH_MAX_BITS 12
61
62 /* ftrace_enabled is a method to turn ftrace on or off */
63 int ftrace_enabled __read_mostly;
64 static int last_ftrace_enabled;
65
66 /* Quick disabling of function tracer. */
67 int function_trace_stop;
68
69 /* List for set_ftrace_pid's pids. */
70 LIST_HEAD(ftrace_pids);
71 struct ftrace_pid {
72         struct list_head list;
73         struct pid *pid;
74 };
75
76 /*
77  * ftrace_disabled is set when an anomaly is discovered.
78  * ftrace_disabled is much stronger than ftrace_enabled.
79  */
80 static int ftrace_disabled __read_mostly;
81
82 static DEFINE_MUTEX(ftrace_lock);
83
84 static struct ftrace_ops ftrace_list_end __read_mostly = {
85         .func           = ftrace_stub,
86 };
87
88 static struct ftrace_ops *ftrace_global_list __read_mostly = &ftrace_list_end;
89 static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
90 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
91 static ftrace_func_t __ftrace_trace_function_delay __read_mostly = ftrace_stub;
92 ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
93 ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
94 static struct ftrace_ops global_ops;
95
96 static void
97 ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip);
98
99 /*
100  * Traverse the ftrace_global_list, invoking all entries.  The reason that we
101  * can use rcu_dereference_raw() is that elements removed from this list
102  * are simply leaked, so there is no need to interact with a grace-period
103  * mechanism.  The rcu_dereference_raw() calls are needed to handle
104  * concurrent insertions into the ftrace_global_list.
105  *
106  * Silly Alpha and silly pointer-speculation compiler optimizations!
107  */
108 static void ftrace_global_list_func(unsigned long ip,
109                                     unsigned long parent_ip)
110 {
111         struct ftrace_ops *op;
112
113         if (unlikely(trace_recursion_test(TRACE_GLOBAL_BIT)))
114                 return;
115
116         trace_recursion_set(TRACE_GLOBAL_BIT);
117         op = rcu_dereference_raw(ftrace_global_list); /*see above*/
118         while (op != &ftrace_list_end) {
119                 op->func(ip, parent_ip);
120                 op = rcu_dereference_raw(op->next); /*see above*/
121         };
122         trace_recursion_clear(TRACE_GLOBAL_BIT);
123 }
124
125 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
126 {
127         if (!test_tsk_trace_trace(current))
128                 return;
129
130         ftrace_pid_function(ip, parent_ip);
131 }
132
133 static void set_ftrace_pid_function(ftrace_func_t func)
134 {
135         /* do not set ftrace_pid_function to itself! */
136         if (func != ftrace_pid_func)
137                 ftrace_pid_function = func;
138 }
139
140 /**
141  * clear_ftrace_function - reset the ftrace function
142  *
143  * This NULLs the ftrace function and in essence stops
144  * tracing.  There may be lag
145  */
146 void clear_ftrace_function(void)
147 {
148         ftrace_trace_function = ftrace_stub;
149         __ftrace_trace_function = ftrace_stub;
150         __ftrace_trace_function_delay = ftrace_stub;
151         ftrace_pid_function = ftrace_stub;
152 }
153
154 #undef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
155 #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
156 /*
157  * For those archs that do not test ftrace_trace_stop in their
158  * mcount call site, we need to do it from C.
159  */
160 static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
161 {
162         if (function_trace_stop)
163                 return;
164
165         __ftrace_trace_function(ip, parent_ip);
166 }
167 #endif
168
169 static void update_global_ops(void)
170 {
171         ftrace_func_t func;
172
173         /*
174          * If there's only one function registered, then call that
175          * function directly. Otherwise, we need to iterate over the
176          * registered callers.
177          */
178         if (ftrace_global_list == &ftrace_list_end ||
179             ftrace_global_list->next == &ftrace_list_end)
180                 func = ftrace_global_list->func;
181         else
182                 func = ftrace_global_list_func;
183
184         /* If we filter on pids, update to use the pid function */
185         if (!list_empty(&ftrace_pids)) {
186                 set_ftrace_pid_function(func);
187                 func = ftrace_pid_func;
188         }
189
190         global_ops.func = func;
191 }
192
193 static void update_ftrace_function(void)
194 {
195         ftrace_func_t func;
196
197         update_global_ops();
198
199         /*
200          * If we are at the end of the list and this ops is
201          * not dynamic, then have the mcount trampoline call
202          * the function directly
203          */
204         if (ftrace_ops_list == &ftrace_list_end ||
205             (ftrace_ops_list->next == &ftrace_list_end &&
206              !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC)))
207                 func = ftrace_ops_list->func;
208         else
209                 func = ftrace_ops_list_func;
210
211 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
212         ftrace_trace_function = func;
213 #else
214 #ifdef CONFIG_DYNAMIC_FTRACE
215         /* do not update till all functions have been modified */
216         __ftrace_trace_function_delay = func;
217 #else
218         __ftrace_trace_function = func;
219 #endif
220         ftrace_trace_function = ftrace_test_stop_func;
221 #endif
222 }
223
224 static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
225 {
226         ops->next = *list;
227         /*
228          * We are entering ops into the list but another
229          * CPU might be walking that list. We need to make sure
230          * the ops->next pointer is valid before another CPU sees
231          * the ops pointer included into the list.
232          */
233         rcu_assign_pointer(*list, ops);
234 }
235
236 static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
237 {
238         struct ftrace_ops **p;
239
240         /*
241          * If we are removing the last function, then simply point
242          * to the ftrace_stub.
243          */
244         if (*list == ops && ops->next == &ftrace_list_end) {
245                 *list = &ftrace_list_end;
246                 return 0;
247         }
248
249         for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
250                 if (*p == ops)
251                         break;
252
253         if (*p != ops)
254                 return -1;
255
256         *p = (*p)->next;
257         return 0;
258 }
259
260 static int __register_ftrace_function(struct ftrace_ops *ops)
261 {
262         if (ftrace_disabled)
263                 return -ENODEV;
264
265         if (FTRACE_WARN_ON(ops == &global_ops))
266                 return -EINVAL;
267
268         if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
269                 return -EBUSY;
270
271         if (!core_kernel_data((unsigned long)ops))
272                 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
273
274         if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
275                 int first = ftrace_global_list == &ftrace_list_end;
276                 add_ftrace_ops(&ftrace_global_list, ops);
277                 ops->flags |= FTRACE_OPS_FL_ENABLED;
278                 if (first)
279                         add_ftrace_ops(&ftrace_ops_list, &global_ops);
280         } else
281                 add_ftrace_ops(&ftrace_ops_list, ops);
282
283         if (ftrace_enabled)
284                 update_ftrace_function();
285
286         return 0;
287 }
288
289 static int __unregister_ftrace_function(struct ftrace_ops *ops)
290 {
291         int ret;
292
293         if (ftrace_disabled)
294                 return -ENODEV;
295
296         if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
297                 return -EBUSY;
298
299         if (FTRACE_WARN_ON(ops == &global_ops))
300                 return -EINVAL;
301
302         if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
303                 ret = remove_ftrace_ops(&ftrace_global_list, ops);
304                 if (!ret && ftrace_global_list == &ftrace_list_end)
305                         ret = remove_ftrace_ops(&ftrace_ops_list, &global_ops);
306                 if (!ret)
307                         ops->flags &= ~FTRACE_OPS_FL_ENABLED;
308         } else
309                 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
310
311         if (ret < 0)
312                 return ret;
313
314         if (ftrace_enabled)
315                 update_ftrace_function();
316
317         /*
318          * Dynamic ops may be freed, we must make sure that all
319          * callers are done before leaving this function.
320          */
321         if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
322                 synchronize_sched();
323
324         return 0;
325 }
326
327 static void ftrace_update_pid_func(void)
328 {
329         /* Only do something if we are tracing something */
330         if (ftrace_trace_function == ftrace_stub)
331                 return;
332
333         update_ftrace_function();
334 }
335
336 #ifdef CONFIG_FUNCTION_PROFILER
337 struct ftrace_profile {
338         struct hlist_node               node;
339         unsigned long                   ip;
340         unsigned long                   counter;
341 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
342         unsigned long long              time;
343         unsigned long long              time_squared;
344 #endif
345 };
346
347 struct ftrace_profile_page {
348         struct ftrace_profile_page      *next;
349         unsigned long                   index;
350         struct ftrace_profile           records[];
351 };
352
353 struct ftrace_profile_stat {
354         atomic_t                        disabled;
355         struct hlist_head               *hash;
356         struct ftrace_profile_page      *pages;
357         struct ftrace_profile_page      *start;
358         struct tracer_stat              stat;
359 };
360
361 #define PROFILE_RECORDS_SIZE                                            \
362         (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
363
364 #define PROFILES_PER_PAGE                                       \
365         (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
366
367 static int ftrace_profile_bits __read_mostly;
368 static int ftrace_profile_enabled __read_mostly;
369
370 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
371 static DEFINE_MUTEX(ftrace_profile_lock);
372
373 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
374
375 #define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
376
377 static void *
378 function_stat_next(void *v, int idx)
379 {
380         struct ftrace_profile *rec = v;
381         struct ftrace_profile_page *pg;
382
383         pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
384
385  again:
386         if (idx != 0)
387                 rec++;
388
389         if ((void *)rec >= (void *)&pg->records[pg->index]) {
390                 pg = pg->next;
391                 if (!pg)
392                         return NULL;
393                 rec = &pg->records[0];
394                 if (!rec->counter)
395                         goto again;
396         }
397
398         return rec;
399 }
400
401 static void *function_stat_start(struct tracer_stat *trace)
402 {
403         struct ftrace_profile_stat *stat =
404                 container_of(trace, struct ftrace_profile_stat, stat);
405
406         if (!stat || !stat->start)
407                 return NULL;
408
409         return function_stat_next(&stat->start->records[0], 0);
410 }
411
412 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
413 /* function graph compares on total time */
414 static int function_stat_cmp(void *p1, void *p2)
415 {
416         struct ftrace_profile *a = p1;
417         struct ftrace_profile *b = p2;
418
419         if (a->time < b->time)
420                 return -1;
421         if (a->time > b->time)
422                 return 1;
423         else
424                 return 0;
425 }
426 #else
427 /* not function graph compares against hits */
428 static int function_stat_cmp(void *p1, void *p2)
429 {
430         struct ftrace_profile *a = p1;
431         struct ftrace_profile *b = p2;
432
433         if (a->counter < b->counter)
434                 return -1;
435         if (a->counter > b->counter)
436                 return 1;
437         else
438                 return 0;
439 }
440 #endif
441
442 static int function_stat_headers(struct seq_file *m)
443 {
444 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
445         seq_printf(m, "  Function                               "
446                    "Hit    Time            Avg             s^2\n"
447                       "  --------                               "
448                    "---    ----            ---             ---\n");
449 #else
450         seq_printf(m, "  Function                               Hit\n"
451                       "  --------                               ---\n");
452 #endif
453         return 0;
454 }
455
456 static int function_stat_show(struct seq_file *m, void *v)
457 {
458         struct ftrace_profile *rec = v;
459         char str[KSYM_SYMBOL_LEN];
460         int ret = 0;
461 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
462         static struct trace_seq s;
463         unsigned long long avg;
464         unsigned long long stddev;
465 #endif
466         mutex_lock(&ftrace_profile_lock);
467
468         /* we raced with function_profile_reset() */
469         if (unlikely(rec->counter == 0)) {
470                 ret = -EBUSY;
471                 goto out;
472         }
473
474         kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
475         seq_printf(m, "  %-30.30s  %10lu", str, rec->counter);
476
477 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
478         seq_printf(m, "    ");
479         avg = rec->time;
480         do_div(avg, rec->counter);
481
482         /* Sample standard deviation (s^2) */
483         if (rec->counter <= 1)
484                 stddev = 0;
485         else {
486                 stddev = rec->time_squared - rec->counter * avg * avg;
487                 /*
488                  * Divide only 1000 for ns^2 -> us^2 conversion.
489                  * trace_print_graph_duration will divide 1000 again.
490                  */
491                 do_div(stddev, (rec->counter - 1) * 1000);
492         }
493
494         trace_seq_init(&s);
495         trace_print_graph_duration(rec->time, &s);
496         trace_seq_puts(&s, "    ");
497         trace_print_graph_duration(avg, &s);
498         trace_seq_puts(&s, "    ");
499         trace_print_graph_duration(stddev, &s);
500         trace_print_seq(m, &s);
501 #endif
502         seq_putc(m, '\n');
503 out:
504         mutex_unlock(&ftrace_profile_lock);
505
506         return ret;
507 }
508
509 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
510 {
511         struct ftrace_profile_page *pg;
512
513         pg = stat->pages = stat->start;
514
515         while (pg) {
516                 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
517                 pg->index = 0;
518                 pg = pg->next;
519         }
520
521         memset(stat->hash, 0,
522                FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
523 }
524
525 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
526 {
527         struct ftrace_profile_page *pg;
528         int functions;
529         int pages;
530         int i;
531
532         /* If we already allocated, do nothing */
533         if (stat->pages)
534                 return 0;
535
536         stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
537         if (!stat->pages)
538                 return -ENOMEM;
539
540 #ifdef CONFIG_DYNAMIC_FTRACE
541         functions = ftrace_update_tot_cnt;
542 #else
543         /*
544          * We do not know the number of functions that exist because
545          * dynamic tracing is what counts them. With past experience
546          * we have around 20K functions. That should be more than enough.
547          * It is highly unlikely we will execute every function in
548          * the kernel.
549          */
550         functions = 20000;
551 #endif
552
553         pg = stat->start = stat->pages;
554
555         pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
556
557         for (i = 0; i < pages; i++) {
558                 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
559                 if (!pg->next)
560                         goto out_free;
561                 pg = pg->next;
562         }
563
564         return 0;
565
566  out_free:
567         pg = stat->start;
568         while (pg) {
569                 unsigned long tmp = (unsigned long)pg;
570
571                 pg = pg->next;
572                 free_page(tmp);
573         }
574
575         free_page((unsigned long)stat->pages);
576         stat->pages = NULL;
577         stat->start = NULL;
578
579         return -ENOMEM;
580 }
581
582 static int ftrace_profile_init_cpu(int cpu)
583 {
584         struct ftrace_profile_stat *stat;
585         int size;
586
587         stat = &per_cpu(ftrace_profile_stats, cpu);
588
589         if (stat->hash) {
590                 /* If the profile is already created, simply reset it */
591                 ftrace_profile_reset(stat);
592                 return 0;
593         }
594
595         /*
596          * We are profiling all functions, but usually only a few thousand
597          * functions are hit. We'll make a hash of 1024 items.
598          */
599         size = FTRACE_PROFILE_HASH_SIZE;
600
601         stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
602
603         if (!stat->hash)
604                 return -ENOMEM;
605
606         if (!ftrace_profile_bits) {
607                 size--;
608
609                 for (; size; size >>= 1)
610                         ftrace_profile_bits++;
611         }
612
613         /* Preallocate the function profiling pages */
614         if (ftrace_profile_pages_init(stat) < 0) {
615                 kfree(stat->hash);
616                 stat->hash = NULL;
617                 return -ENOMEM;
618         }
619
620         return 0;
621 }
622
623 static int ftrace_profile_init(void)
624 {
625         int cpu;
626         int ret = 0;
627
628         for_each_online_cpu(cpu) {
629                 ret = ftrace_profile_init_cpu(cpu);
630                 if (ret)
631                         break;
632         }
633
634         return ret;
635 }
636
637 /* interrupts must be disabled */
638 static struct ftrace_profile *
639 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
640 {
641         struct ftrace_profile *rec;
642         struct hlist_head *hhd;
643         struct hlist_node *n;
644         unsigned long key;
645
646         key = hash_long(ip, ftrace_profile_bits);
647         hhd = &stat->hash[key];
648
649         if (hlist_empty(hhd))
650                 return NULL;
651
652         hlist_for_each_entry_rcu(rec, n, hhd, node) {
653                 if (rec->ip == ip)
654                         return rec;
655         }
656
657         return NULL;
658 }
659
660 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
661                                struct ftrace_profile *rec)
662 {
663         unsigned long key;
664
665         key = hash_long(rec->ip, ftrace_profile_bits);
666         hlist_add_head_rcu(&rec->node, &stat->hash[key]);
667 }
668
669 /*
670  * The memory is already allocated, this simply finds a new record to use.
671  */
672 static struct ftrace_profile *
673 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
674 {
675         struct ftrace_profile *rec = NULL;
676
677         /* prevent recursion (from NMIs) */
678         if (atomic_inc_return(&stat->disabled) != 1)
679                 goto out;
680
681         /*
682          * Try to find the function again since an NMI
683          * could have added it
684          */
685         rec = ftrace_find_profiled_func(stat, ip);
686         if (rec)
687                 goto out;
688
689         if (stat->pages->index == PROFILES_PER_PAGE) {
690                 if (!stat->pages->next)
691                         goto out;
692                 stat->pages = stat->pages->next;
693         }
694
695         rec = &stat->pages->records[stat->pages->index++];
696         rec->ip = ip;
697         ftrace_add_profile(stat, rec);
698
699  out:
700         atomic_dec(&stat->disabled);
701
702         return rec;
703 }
704
705 static void
706 function_profile_call(unsigned long ip, unsigned long parent_ip)
707 {
708         struct ftrace_profile_stat *stat;
709         struct ftrace_profile *rec;
710         unsigned long flags;
711
712         if (!ftrace_profile_enabled)
713                 return;
714
715         local_irq_save(flags);
716
717         stat = &__get_cpu_var(ftrace_profile_stats);
718         if (!stat->hash || !ftrace_profile_enabled)
719                 goto out;
720
721         rec = ftrace_find_profiled_func(stat, ip);
722         if (!rec) {
723                 rec = ftrace_profile_alloc(stat, ip);
724                 if (!rec)
725                         goto out;
726         }
727
728         rec->counter++;
729  out:
730         local_irq_restore(flags);
731 }
732
733 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
734 static int profile_graph_entry(struct ftrace_graph_ent *trace)
735 {
736         function_profile_call(trace->func, 0);
737         return 1;
738 }
739
740 static void profile_graph_return(struct ftrace_graph_ret *trace)
741 {
742         struct ftrace_profile_stat *stat;
743         unsigned long long calltime;
744         struct ftrace_profile *rec;
745         unsigned long flags;
746
747         local_irq_save(flags);
748         stat = &__get_cpu_var(ftrace_profile_stats);
749         if (!stat->hash || !ftrace_profile_enabled)
750                 goto out;
751
752         /* If the calltime was zero'd ignore it */
753         if (!trace->calltime)
754                 goto out;
755
756         calltime = trace->rettime - trace->calltime;
757
758         if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
759                 int index;
760
761                 index = trace->depth;
762
763                 /* Append this call time to the parent time to subtract */
764                 if (index)
765                         current->ret_stack[index - 1].subtime += calltime;
766
767                 if (current->ret_stack[index].subtime < calltime)
768                         calltime -= current->ret_stack[index].subtime;
769                 else
770                         calltime = 0;
771         }
772
773         rec = ftrace_find_profiled_func(stat, trace->func);
774         if (rec) {
775                 rec->time += calltime;
776                 rec->time_squared += calltime * calltime;
777         }
778
779  out:
780         local_irq_restore(flags);
781 }
782
783 static int register_ftrace_profiler(void)
784 {
785         return register_ftrace_graph(&profile_graph_return,
786                                      &profile_graph_entry);
787 }
788
789 static void unregister_ftrace_profiler(void)
790 {
791         unregister_ftrace_graph();
792 }
793 #else
794 static struct ftrace_ops ftrace_profile_ops __read_mostly = {
795         .func           = function_profile_call,
796 };
797
798 static int register_ftrace_profiler(void)
799 {
800         return register_ftrace_function(&ftrace_profile_ops);
801 }
802
803 static void unregister_ftrace_profiler(void)
804 {
805         unregister_ftrace_function(&ftrace_profile_ops);
806 }
807 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
808
809 static ssize_t
810 ftrace_profile_write(struct file *filp, const char __user *ubuf,
811                      size_t cnt, loff_t *ppos)
812 {
813         unsigned long val;
814         int ret;
815
816         ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
817         if (ret)
818                 return ret;
819
820         val = !!val;
821
822         mutex_lock(&ftrace_profile_lock);
823         if (ftrace_profile_enabled ^ val) {
824                 if (val) {
825                         ret = ftrace_profile_init();
826                         if (ret < 0) {
827                                 cnt = ret;
828                                 goto out;
829                         }
830
831                         ret = register_ftrace_profiler();
832                         if (ret < 0) {
833                                 cnt = ret;
834                                 goto out;
835                         }
836                         ftrace_profile_enabled = 1;
837                 } else {
838                         ftrace_profile_enabled = 0;
839                         /*
840                          * unregister_ftrace_profiler calls stop_machine
841                          * so this acts like an synchronize_sched.
842                          */
843                         unregister_ftrace_profiler();
844                 }
845         }
846  out:
847         mutex_unlock(&ftrace_profile_lock);
848
849         *ppos += cnt;
850
851         return cnt;
852 }
853
854 static ssize_t
855 ftrace_profile_read(struct file *filp, char __user *ubuf,
856                      size_t cnt, loff_t *ppos)
857 {
858         char buf[64];           /* big enough to hold a number */
859         int r;
860
861         r = sprintf(buf, "%u\n", ftrace_profile_enabled);
862         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
863 }
864
865 static const struct file_operations ftrace_profile_fops = {
866         .open           = tracing_open_generic,
867         .read           = ftrace_profile_read,
868         .write          = ftrace_profile_write,
869         .llseek         = default_llseek,
870 };
871
872 /* used to initialize the real stat files */
873 static struct tracer_stat function_stats __initdata = {
874         .name           = "functions",
875         .stat_start     = function_stat_start,
876         .stat_next      = function_stat_next,
877         .stat_cmp       = function_stat_cmp,
878         .stat_headers   = function_stat_headers,
879         .stat_show      = function_stat_show
880 };
881
882 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
883 {
884         struct ftrace_profile_stat *stat;
885         struct dentry *entry;
886         char *name;
887         int ret;
888         int cpu;
889
890         for_each_possible_cpu(cpu) {
891                 stat = &per_cpu(ftrace_profile_stats, cpu);
892
893                 /* allocate enough for function name + cpu number */
894                 name = kmalloc(32, GFP_KERNEL);
895                 if (!name) {
896                         /*
897                          * The files created are permanent, if something happens
898                          * we still do not free memory.
899                          */
900                         WARN(1,
901                              "Could not allocate stat file for cpu %d\n",
902                              cpu);
903                         return;
904                 }
905                 stat->stat = function_stats;
906                 snprintf(name, 32, "function%d", cpu);
907                 stat->stat.name = name;
908                 ret = register_stat_tracer(&stat->stat);
909                 if (ret) {
910                         WARN(1,
911                              "Could not register function stat for cpu %d\n",
912                              cpu);
913                         kfree(name);
914                         return;
915                 }
916         }
917
918         entry = debugfs_create_file("function_profile_enabled", 0644,
919                                     d_tracer, NULL, &ftrace_profile_fops);
920         if (!entry)
921                 pr_warning("Could not create debugfs "
922                            "'function_profile_enabled' entry\n");
923 }
924
925 #else /* CONFIG_FUNCTION_PROFILER */
926 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
927 {
928 }
929 #endif /* CONFIG_FUNCTION_PROFILER */
930
931 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
932
933 #ifdef CONFIG_DYNAMIC_FTRACE
934
935 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
936 # error Dynamic ftrace depends on MCOUNT_RECORD
937 #endif
938
939 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
940
941 struct ftrace_func_probe {
942         struct hlist_node       node;
943         struct ftrace_probe_ops *ops;
944         unsigned long           flags;
945         unsigned long           ip;
946         void                    *data;
947         struct rcu_head         rcu;
948 };
949
950 enum {
951         FTRACE_ENABLE_CALLS             = (1 << 0),
952         FTRACE_DISABLE_CALLS            = (1 << 1),
953         FTRACE_UPDATE_TRACE_FUNC        = (1 << 2),
954         FTRACE_START_FUNC_RET           = (1 << 3),
955         FTRACE_STOP_FUNC_RET            = (1 << 4),
956 };
957 struct ftrace_func_entry {
958         struct hlist_node hlist;
959         unsigned long ip;
960 };
961
962 struct ftrace_hash {
963         unsigned long           size_bits;
964         struct hlist_head       *buckets;
965         unsigned long           count;
966         struct rcu_head         rcu;
967 };
968
969 /*
970  * We make these constant because no one should touch them,
971  * but they are used as the default "empty hash", to avoid allocating
972  * it all the time. These are in a read only section such that if
973  * anyone does try to modify it, it will cause an exception.
974  */
975 static const struct hlist_head empty_buckets[1];
976 static const struct ftrace_hash empty_hash = {
977         .buckets = (struct hlist_head *)empty_buckets,
978 };
979 #define EMPTY_HASH      ((struct ftrace_hash *)&empty_hash)
980
981 static struct ftrace_ops global_ops = {
982         .func                   = ftrace_stub,
983         .notrace_hash           = EMPTY_HASH,
984         .filter_hash            = EMPTY_HASH,
985 };
986
987 static struct dyn_ftrace *ftrace_new_addrs;
988
989 static DEFINE_MUTEX(ftrace_regex_lock);
990
991 struct ftrace_page {
992         struct ftrace_page      *next;
993         int                     index;
994         struct dyn_ftrace       records[];
995 };
996
997 #define ENTRIES_PER_PAGE \
998   ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
999
1000 /* estimate from running different kernels */
1001 #define NR_TO_INIT              10000
1002
1003 static struct ftrace_page       *ftrace_pages_start;
1004 static struct ftrace_page       *ftrace_pages;
1005
1006 static struct dyn_ftrace *ftrace_free_records;
1007
1008 static struct ftrace_func_entry *
1009 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1010 {
1011         unsigned long key;
1012         struct ftrace_func_entry *entry;
1013         struct hlist_head *hhd;
1014         struct hlist_node *n;
1015
1016         if (!hash->count)
1017                 return NULL;
1018
1019         if (hash->size_bits > 0)
1020                 key = hash_long(ip, hash->size_bits);
1021         else
1022                 key = 0;
1023
1024         hhd = &hash->buckets[key];
1025
1026         hlist_for_each_entry_rcu(entry, n, hhd, hlist) {
1027                 if (entry->ip == ip)
1028                         return entry;
1029         }
1030         return NULL;
1031 }
1032
1033 static void __add_hash_entry(struct ftrace_hash *hash,
1034                              struct ftrace_func_entry *entry)
1035 {
1036         struct hlist_head *hhd;
1037         unsigned long key;
1038
1039         if (hash->size_bits)
1040                 key = hash_long(entry->ip, hash->size_bits);
1041         else
1042                 key = 0;
1043
1044         hhd = &hash->buckets[key];
1045         hlist_add_head(&entry->hlist, hhd);
1046         hash->count++;
1047 }
1048
1049 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1050 {
1051         struct ftrace_func_entry *entry;
1052
1053         entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1054         if (!entry)
1055                 return -ENOMEM;
1056
1057         entry->ip = ip;
1058         __add_hash_entry(hash, entry);
1059
1060         return 0;
1061 }
1062
1063 static void
1064 free_hash_entry(struct ftrace_hash *hash,
1065                   struct ftrace_func_entry *entry)
1066 {
1067         hlist_del(&entry->hlist);
1068         kfree(entry);
1069         hash->count--;
1070 }
1071
1072 static void
1073 remove_hash_entry(struct ftrace_hash *hash,
1074                   struct ftrace_func_entry *entry)
1075 {
1076         hlist_del(&entry->hlist);
1077         hash->count--;
1078 }
1079
1080 static void ftrace_hash_clear(struct ftrace_hash *hash)
1081 {
1082         struct hlist_head *hhd;
1083         struct hlist_node *tp, *tn;
1084         struct ftrace_func_entry *entry;
1085         int size = 1 << hash->size_bits;
1086         int i;
1087
1088         if (!hash->count)
1089                 return;
1090
1091         for (i = 0; i < size; i++) {
1092                 hhd = &hash->buckets[i];
1093                 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist)
1094                         free_hash_entry(hash, entry);
1095         }
1096         FTRACE_WARN_ON(hash->count);
1097 }
1098
1099 static void free_ftrace_hash(struct ftrace_hash *hash)
1100 {
1101         if (!hash || hash == EMPTY_HASH)
1102                 return;
1103         ftrace_hash_clear(hash);
1104         kfree(hash->buckets);
1105         kfree(hash);
1106 }
1107
1108 static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1109 {
1110         struct ftrace_hash *hash;
1111
1112         hash = container_of(rcu, struct ftrace_hash, rcu);
1113         free_ftrace_hash(hash);
1114 }
1115
1116 static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1117 {
1118         if (!hash || hash == EMPTY_HASH)
1119                 return;
1120         call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1121 }
1122
1123 static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1124 {
1125         struct ftrace_hash *hash;
1126         int size;
1127
1128         hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1129         if (!hash)
1130                 return NULL;
1131
1132         size = 1 << size_bits;
1133         hash->buckets = kzalloc(sizeof(*hash->buckets) * size, GFP_KERNEL);
1134
1135         if (!hash->buckets) {
1136                 kfree(hash);
1137                 return NULL;
1138         }
1139
1140         hash->size_bits = size_bits;
1141
1142         return hash;
1143 }
1144
1145 static struct ftrace_hash *
1146 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1147 {
1148         struct ftrace_func_entry *entry;
1149         struct ftrace_hash *new_hash;
1150         struct hlist_node *tp;
1151         int size;
1152         int ret;
1153         int i;
1154
1155         new_hash = alloc_ftrace_hash(size_bits);
1156         if (!new_hash)
1157                 return NULL;
1158
1159         /* Empty hash? */
1160         if (!hash || !hash->count)
1161                 return new_hash;
1162
1163         size = 1 << hash->size_bits;
1164         for (i = 0; i < size; i++) {
1165                 hlist_for_each_entry(entry, tp, &hash->buckets[i], hlist) {
1166                         ret = add_hash_entry(new_hash, entry->ip);
1167                         if (ret < 0)
1168                                 goto free_hash;
1169                 }
1170         }
1171
1172         FTRACE_WARN_ON(new_hash->count != hash->count);
1173
1174         return new_hash;
1175
1176  free_hash:
1177         free_ftrace_hash(new_hash);
1178         return NULL;
1179 }
1180
1181 static void
1182 ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1183 static void
1184 ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1185
1186 static int
1187 ftrace_hash_move(struct ftrace_ops *ops, int enable,
1188                  struct ftrace_hash **dst, struct ftrace_hash *src)
1189 {
1190         struct ftrace_func_entry *entry;
1191         struct hlist_node *tp, *tn;
1192         struct hlist_head *hhd;
1193         struct ftrace_hash *old_hash;
1194         struct ftrace_hash *new_hash;
1195         unsigned long key;
1196         int size = src->count;
1197         int bits = 0;
1198         int ret;
1199         int i;
1200
1201         /*
1202          * Remove the current set, update the hash and add
1203          * them back.
1204          */
1205         ftrace_hash_rec_disable(ops, enable);
1206
1207         /*
1208          * If the new source is empty, just free dst and assign it
1209          * the empty_hash.
1210          */
1211         if (!src->count) {
1212                 free_ftrace_hash_rcu(*dst);
1213                 rcu_assign_pointer(*dst, EMPTY_HASH);
1214                 return 0;
1215         }
1216
1217         /*
1218          * Make the hash size about 1/2 the # found
1219          */
1220         for (size /= 2; size; size >>= 1)
1221                 bits++;
1222
1223         /* Don't allocate too much */
1224         if (bits > FTRACE_HASH_MAX_BITS)
1225                 bits = FTRACE_HASH_MAX_BITS;
1226
1227         ret = -ENOMEM;
1228         new_hash = alloc_ftrace_hash(bits);
1229         if (!new_hash)
1230                 goto out;
1231
1232         size = 1 << src->size_bits;
1233         for (i = 0; i < size; i++) {
1234                 hhd = &src->buckets[i];
1235                 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist) {
1236                         if (bits > 0)
1237                                 key = hash_long(entry->ip, bits);
1238                         else
1239                                 key = 0;
1240                         remove_hash_entry(src, entry);
1241                         __add_hash_entry(new_hash, entry);
1242                 }
1243         }
1244
1245         old_hash = *dst;
1246         rcu_assign_pointer(*dst, new_hash);
1247         free_ftrace_hash_rcu(old_hash);
1248
1249         ret = 0;
1250  out:
1251         /*
1252          * Enable regardless of ret:
1253          *  On success, we enable the new hash.
1254          *  On failure, we re-enable the original hash.
1255          */
1256         ftrace_hash_rec_enable(ops, enable);
1257
1258         return ret;
1259 }
1260
1261 /*
1262  * Test the hashes for this ops to see if we want to call
1263  * the ops->func or not.
1264  *
1265  * It's a match if the ip is in the ops->filter_hash or
1266  * the filter_hash does not exist or is empty,
1267  *  AND
1268  * the ip is not in the ops->notrace_hash.
1269  *
1270  * This needs to be called with preemption disabled as
1271  * the hashes are freed with call_rcu_sched().
1272  */
1273 static int
1274 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
1275 {
1276         struct ftrace_hash *filter_hash;
1277         struct ftrace_hash *notrace_hash;
1278         int ret;
1279
1280         filter_hash = rcu_dereference_raw(ops->filter_hash);
1281         notrace_hash = rcu_dereference_raw(ops->notrace_hash);
1282
1283         if ((!filter_hash || !filter_hash->count ||
1284              ftrace_lookup_ip(filter_hash, ip)) &&
1285             (!notrace_hash || !notrace_hash->count ||
1286              !ftrace_lookup_ip(notrace_hash, ip)))
1287                 ret = 1;
1288         else
1289                 ret = 0;
1290
1291         return ret;
1292 }
1293
1294 /*
1295  * This is a double for. Do not use 'break' to break out of the loop,
1296  * you must use a goto.
1297  */
1298 #define do_for_each_ftrace_rec(pg, rec)                                 \
1299         for (pg = ftrace_pages_start; pg; pg = pg->next) {              \
1300                 int _____i;                                             \
1301                 for (_____i = 0; _____i < pg->index; _____i++) {        \
1302                         rec = &pg->records[_____i];
1303
1304 #define while_for_each_ftrace_rec()             \
1305                 }                               \
1306         }
1307
1308 static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1309                                      int filter_hash,
1310                                      bool inc)
1311 {
1312         struct ftrace_hash *hash;
1313         struct ftrace_hash *other_hash;
1314         struct ftrace_page *pg;
1315         struct dyn_ftrace *rec;
1316         int count = 0;
1317         int all = 0;
1318
1319         /* Only update if the ops has been registered */
1320         if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1321                 return;
1322
1323         /*
1324          * In the filter_hash case:
1325          *   If the count is zero, we update all records.
1326          *   Otherwise we just update the items in the hash.
1327          *
1328          * In the notrace_hash case:
1329          *   We enable the update in the hash.
1330          *   As disabling notrace means enabling the tracing,
1331          *   and enabling notrace means disabling, the inc variable
1332          *   gets inversed.
1333          */
1334         if (filter_hash) {
1335                 hash = ops->filter_hash;
1336                 other_hash = ops->notrace_hash;
1337                 if (!hash || !hash->count)
1338                         all = 1;
1339         } else {
1340                 inc = !inc;
1341                 hash = ops->notrace_hash;
1342                 other_hash = ops->filter_hash;
1343                 /*
1344                  * If the notrace hash has no items,
1345                  * then there's nothing to do.
1346                  */
1347                 if (hash && !hash->count)
1348                         return;
1349         }
1350
1351         do_for_each_ftrace_rec(pg, rec) {
1352                 int in_other_hash = 0;
1353                 int in_hash = 0;
1354                 int match = 0;
1355
1356                 if (all) {
1357                         /*
1358                          * Only the filter_hash affects all records.
1359                          * Update if the record is not in the notrace hash.
1360                          */
1361                         if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1362                                 match = 1;
1363                 } else {
1364                         in_hash = hash && !!ftrace_lookup_ip(hash, rec->ip);
1365                         in_other_hash = other_hash && !!ftrace_lookup_ip(other_hash, rec->ip);
1366
1367                         /*
1368                          *
1369                          */
1370                         if (filter_hash && in_hash && !in_other_hash)
1371                                 match = 1;
1372                         else if (!filter_hash && in_hash &&
1373                                  (in_other_hash || !other_hash->count))
1374                                 match = 1;
1375                 }
1376                 if (!match)
1377                         continue;
1378
1379                 if (inc) {
1380                         rec->flags++;
1381                         if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1382                                 return;
1383                 } else {
1384                         if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1385                                 return;
1386                         rec->flags--;
1387                 }
1388                 count++;
1389                 /* Shortcut, if we handled all records, we are done. */
1390                 if (!all && count == hash->count)
1391                         return;
1392         } while_for_each_ftrace_rec();
1393 }
1394
1395 static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1396                                     int filter_hash)
1397 {
1398         __ftrace_hash_rec_update(ops, filter_hash, 0);
1399 }
1400
1401 static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1402                                    int filter_hash)
1403 {
1404         __ftrace_hash_rec_update(ops, filter_hash, 1);
1405 }
1406
1407 static void ftrace_free_rec(struct dyn_ftrace *rec)
1408 {
1409         rec->freelist = ftrace_free_records;
1410         ftrace_free_records = rec;
1411         rec->flags |= FTRACE_FL_FREE;
1412 }
1413
1414 static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
1415 {
1416         struct dyn_ftrace *rec;
1417
1418         /* First check for freed records */
1419         if (ftrace_free_records) {
1420                 rec = ftrace_free_records;
1421
1422                 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
1423                         FTRACE_WARN_ON_ONCE(1);
1424                         ftrace_free_records = NULL;
1425                         return NULL;
1426                 }
1427
1428                 ftrace_free_records = rec->freelist;
1429                 memset(rec, 0, sizeof(*rec));
1430                 return rec;
1431         }
1432
1433         if (ftrace_pages->index == ENTRIES_PER_PAGE) {
1434                 if (!ftrace_pages->next) {
1435                         /* allocate another page */
1436                         ftrace_pages->next =
1437                                 (void *)get_zeroed_page(GFP_KERNEL);
1438                         if (!ftrace_pages->next)
1439                                 return NULL;
1440                 }
1441                 ftrace_pages = ftrace_pages->next;
1442         }
1443
1444         return &ftrace_pages->records[ftrace_pages->index++];
1445 }
1446
1447 static struct dyn_ftrace *
1448 ftrace_record_ip(unsigned long ip)
1449 {
1450         struct dyn_ftrace *rec;
1451
1452         if (ftrace_disabled)
1453                 return NULL;
1454
1455         rec = ftrace_alloc_dyn_node(ip);
1456         if (!rec)
1457                 return NULL;
1458
1459         rec->ip = ip;
1460         rec->newlist = ftrace_new_addrs;
1461         ftrace_new_addrs = rec;
1462
1463         return rec;
1464 }
1465
1466 static void print_ip_ins(const char *fmt, unsigned char *p)
1467 {
1468         int i;
1469
1470         printk(KERN_CONT "%s", fmt);
1471
1472         for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1473                 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1474 }
1475
1476 static void ftrace_bug(int failed, unsigned long ip)
1477 {
1478         switch (failed) {
1479         case -EFAULT:
1480                 FTRACE_WARN_ON_ONCE(1);
1481                 pr_info("ftrace faulted on modifying ");
1482                 print_ip_sym(ip);
1483                 break;
1484         case -EINVAL:
1485                 FTRACE_WARN_ON_ONCE(1);
1486                 pr_info("ftrace failed to modify ");
1487                 print_ip_sym(ip);
1488                 print_ip_ins(" actual: ", (unsigned char *)ip);
1489                 printk(KERN_CONT "\n");
1490                 break;
1491         case -EPERM:
1492                 FTRACE_WARN_ON_ONCE(1);
1493                 pr_info("ftrace faulted on writing ");
1494                 print_ip_sym(ip);
1495                 break;
1496         default:
1497                 FTRACE_WARN_ON_ONCE(1);
1498                 pr_info("ftrace faulted on unknown error ");
1499                 print_ip_sym(ip);
1500         }
1501 }
1502
1503
1504 /* Return 1 if the address range is reserved for ftrace */
1505 int ftrace_text_reserved(void *start, void *end)
1506 {
1507         struct dyn_ftrace *rec;
1508         struct ftrace_page *pg;
1509
1510         do_for_each_ftrace_rec(pg, rec) {
1511                 if (rec->ip <= (unsigned long)end &&
1512                     rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
1513                         return 1;
1514         } while_for_each_ftrace_rec();
1515         return 0;
1516 }
1517
1518
1519 static int
1520 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1521 {
1522         unsigned long ftrace_addr;
1523         unsigned long flag = 0UL;
1524
1525         ftrace_addr = (unsigned long)FTRACE_ADDR;
1526
1527         /*
1528          * If we are enabling tracing:
1529          *
1530          *   If the record has a ref count, then we need to enable it
1531          *   because someone is using it.
1532          *
1533          *   Otherwise we make sure its disabled.
1534          *
1535          * If we are disabling tracing, then disable all records that
1536          * are enabled.
1537          */
1538         if (enable && (rec->flags & ~FTRACE_FL_MASK))
1539                 flag = FTRACE_FL_ENABLED;
1540
1541         /* If the state of this record hasn't changed, then do nothing */
1542         if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1543                 return 0;
1544
1545         if (flag) {
1546                 rec->flags |= FTRACE_FL_ENABLED;
1547                 return ftrace_make_call(rec, ftrace_addr);
1548         }
1549
1550         rec->flags &= ~FTRACE_FL_ENABLED;
1551         return ftrace_make_nop(NULL, rec, ftrace_addr);
1552 }
1553
1554 static void ftrace_replace_code(int enable)
1555 {
1556         struct dyn_ftrace *rec;
1557         struct ftrace_page *pg;
1558         int failed;
1559
1560         if (unlikely(ftrace_disabled))
1561                 return;
1562
1563         do_for_each_ftrace_rec(pg, rec) {
1564                 /* Skip over free records */
1565                 if (rec->flags & FTRACE_FL_FREE)
1566                         continue;
1567
1568                 failed = __ftrace_replace_code(rec, enable);
1569                 if (failed) {
1570                         ftrace_bug(failed, rec->ip);
1571                         /* Stop processing */
1572                         return;
1573                 }
1574         } while_for_each_ftrace_rec();
1575 }
1576
1577 static int
1578 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
1579 {
1580         unsigned long ip;
1581         int ret;
1582
1583         ip = rec->ip;
1584
1585         if (unlikely(ftrace_disabled))
1586                 return 0;
1587
1588         ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
1589         if (ret) {
1590                 ftrace_bug(ret, ip);
1591                 return 0;
1592         }
1593         return 1;
1594 }
1595
1596 /*
1597  * archs can override this function if they must do something
1598  * before the modifying code is performed.
1599  */
1600 int __weak ftrace_arch_code_modify_prepare(void)
1601 {
1602         return 0;
1603 }
1604
1605 /*
1606  * archs can override this function if they must do something
1607  * after the modifying code is performed.
1608  */
1609 int __weak ftrace_arch_code_modify_post_process(void)
1610 {
1611         return 0;
1612 }
1613
1614 static int __ftrace_modify_code(void *data)
1615 {
1616         int *command = data;
1617
1618         /*
1619          * Do not call function tracer while we update the code.
1620          * We are in stop machine, no worrying about races.
1621          */
1622         function_trace_stop++;
1623
1624         if (*command & FTRACE_ENABLE_CALLS)
1625                 ftrace_replace_code(1);
1626         else if (*command & FTRACE_DISABLE_CALLS)
1627                 ftrace_replace_code(0);
1628
1629         if (*command & FTRACE_UPDATE_TRACE_FUNC)
1630                 ftrace_update_ftrace_func(ftrace_trace_function);
1631
1632         if (*command & FTRACE_START_FUNC_RET)
1633                 ftrace_enable_ftrace_graph_caller();
1634         else if (*command & FTRACE_STOP_FUNC_RET)
1635                 ftrace_disable_ftrace_graph_caller();
1636
1637 #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
1638         /*
1639          * For archs that call ftrace_test_stop_func(), we must
1640          * wait till after we update all the function callers
1641          * before we update the callback. This keeps different
1642          * ops that record different functions from corrupting
1643          * each other.
1644          */
1645         __ftrace_trace_function = __ftrace_trace_function_delay;
1646 #endif
1647         function_trace_stop--;
1648
1649         return 0;
1650 }
1651
1652 static void ftrace_run_update_code(int command)
1653 {
1654         int ret;
1655
1656         ret = ftrace_arch_code_modify_prepare();
1657         FTRACE_WARN_ON(ret);
1658         if (ret)
1659                 return;
1660
1661         stop_machine(__ftrace_modify_code, &command, NULL);
1662
1663         ret = ftrace_arch_code_modify_post_process();
1664         FTRACE_WARN_ON(ret);
1665 }
1666
1667 static ftrace_func_t saved_ftrace_func;
1668 static int ftrace_start_up;
1669 static int global_start_up;
1670
1671 static void ftrace_startup_enable(int command)
1672 {
1673         if (saved_ftrace_func != ftrace_trace_function) {
1674                 saved_ftrace_func = ftrace_trace_function;
1675                 command |= FTRACE_UPDATE_TRACE_FUNC;
1676         }
1677
1678         if (!command || !ftrace_enabled)
1679                 return;
1680
1681         ftrace_run_update_code(command);
1682 }
1683
1684 static int ftrace_startup(struct ftrace_ops *ops, int command)
1685 {
1686         bool hash_enable = true;
1687
1688         if (unlikely(ftrace_disabled))
1689                 return -ENODEV;
1690
1691         ftrace_start_up++;
1692         command |= FTRACE_ENABLE_CALLS;
1693
1694         /* ops marked global share the filter hashes */
1695         if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
1696                 ops = &global_ops;
1697                 /* Don't update hash if global is already set */
1698                 if (global_start_up)
1699                         hash_enable = false;
1700                 global_start_up++;
1701         }
1702
1703         ops->flags |= FTRACE_OPS_FL_ENABLED;
1704         if (hash_enable)
1705                 ftrace_hash_rec_enable(ops, 1);
1706
1707         ftrace_startup_enable(command);
1708
1709         return 0;
1710 }
1711
1712 static void ftrace_shutdown(struct ftrace_ops *ops, int command)
1713 {
1714         bool hash_disable = true;
1715
1716         if (unlikely(ftrace_disabled))
1717                 return;
1718
1719         ftrace_start_up--;
1720         /*
1721          * Just warn in case of unbalance, no need to kill ftrace, it's not
1722          * critical but the ftrace_call callers may be never nopped again after
1723          * further ftrace uses.
1724          */
1725         WARN_ON_ONCE(ftrace_start_up < 0);
1726
1727         if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
1728                 ops = &global_ops;
1729                 global_start_up--;
1730                 WARN_ON_ONCE(global_start_up < 0);
1731                 /* Don't update hash if global still has users */
1732                 if (global_start_up) {
1733                         WARN_ON_ONCE(!ftrace_start_up);
1734                         hash_disable = false;
1735                 }
1736         }
1737
1738         if (hash_disable)
1739                 ftrace_hash_rec_disable(ops, 1);
1740
1741         if (ops != &global_ops || !global_start_up)
1742                 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
1743
1744         if (!ftrace_start_up)
1745                 command |= FTRACE_DISABLE_CALLS;
1746
1747         if (saved_ftrace_func != ftrace_trace_function) {
1748                 saved_ftrace_func = ftrace_trace_function;
1749                 command |= FTRACE_UPDATE_TRACE_FUNC;
1750         }
1751
1752         if (!command || !ftrace_enabled)
1753                 return;
1754
1755         ftrace_run_update_code(command);
1756 }
1757
1758 static void ftrace_startup_sysctl(void)
1759 {
1760         if (unlikely(ftrace_disabled))
1761                 return;
1762
1763         /* Force update next time */
1764         saved_ftrace_func = NULL;
1765         /* ftrace_start_up is true if we want ftrace running */
1766         if (ftrace_start_up)
1767                 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
1768 }
1769
1770 static void ftrace_shutdown_sysctl(void)
1771 {
1772         if (unlikely(ftrace_disabled))
1773                 return;
1774
1775         /* ftrace_start_up is true if ftrace is running */
1776         if (ftrace_start_up)
1777                 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
1778 }
1779
1780 static cycle_t          ftrace_update_time;
1781 static unsigned long    ftrace_update_cnt;
1782 unsigned long           ftrace_update_tot_cnt;
1783
1784 static int ftrace_update_code(struct module *mod)
1785 {
1786         struct dyn_ftrace *p;
1787         cycle_t start, stop;
1788
1789         start = ftrace_now(raw_smp_processor_id());
1790         ftrace_update_cnt = 0;
1791
1792         while (ftrace_new_addrs) {
1793
1794                 /* If something went wrong, bail without enabling anything */
1795                 if (unlikely(ftrace_disabled))
1796                         return -1;
1797
1798                 p = ftrace_new_addrs;
1799                 ftrace_new_addrs = p->newlist;
1800                 p->flags = 0L;
1801
1802                 /*
1803                  * Do the initial record conversion from mcount jump
1804                  * to the NOP instructions.
1805                  */
1806                 if (!ftrace_code_disable(mod, p)) {
1807                         ftrace_free_rec(p);
1808                         /* Game over */
1809                         break;
1810                 }
1811
1812                 ftrace_update_cnt++;
1813
1814                 /*
1815                  * If the tracing is enabled, go ahead and enable the record.
1816                  *
1817                  * The reason not to enable the record immediatelly is the
1818                  * inherent check of ftrace_make_nop/ftrace_make_call for
1819                  * correct previous instructions.  Making first the NOP
1820                  * conversion puts the module to the correct state, thus
1821                  * passing the ftrace_make_call check.
1822                  */
1823                 if (ftrace_start_up) {
1824                         int failed = __ftrace_replace_code(p, 1);
1825                         if (failed) {
1826                                 ftrace_bug(failed, p->ip);
1827                                 ftrace_free_rec(p);
1828                         }
1829                 }
1830         }
1831
1832         stop = ftrace_now(raw_smp_processor_id());
1833         ftrace_update_time = stop - start;
1834         ftrace_update_tot_cnt += ftrace_update_cnt;
1835
1836         return 0;
1837 }
1838
1839 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
1840 {
1841         struct ftrace_page *pg;
1842         int cnt;
1843         int i;
1844
1845         /* allocate a few pages */
1846         ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1847         if (!ftrace_pages_start)
1848                 return -1;
1849
1850         /*
1851          * Allocate a few more pages.
1852          *
1853          * TODO: have some parser search vmlinux before
1854          *   final linking to find all calls to ftrace.
1855          *   Then we can:
1856          *    a) know how many pages to allocate.
1857          *     and/or
1858          *    b) set up the table then.
1859          *
1860          *  The dynamic code is still necessary for
1861          *  modules.
1862          */
1863
1864         pg = ftrace_pages = ftrace_pages_start;
1865
1866         cnt = num_to_init / ENTRIES_PER_PAGE;
1867         pr_info("ftrace: allocating %ld entries in %d pages\n",
1868                 num_to_init, cnt + 1);
1869
1870         for (i = 0; i < cnt; i++) {
1871                 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1872
1873                 /* If we fail, we'll try later anyway */
1874                 if (!pg->next)
1875                         break;
1876
1877                 pg = pg->next;
1878         }
1879
1880         return 0;
1881 }
1882
1883 enum {
1884         FTRACE_ITER_FILTER      = (1 << 0),
1885         FTRACE_ITER_NOTRACE     = (1 << 1),
1886         FTRACE_ITER_PRINTALL    = (1 << 2),
1887         FTRACE_ITER_HASH        = (1 << 3),
1888         FTRACE_ITER_ENABLED     = (1 << 4),
1889 };
1890
1891 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1892
1893 struct ftrace_iterator {
1894         loff_t                          pos;
1895         loff_t                          func_pos;
1896         struct ftrace_page              *pg;
1897         struct dyn_ftrace               *func;
1898         struct ftrace_func_probe        *probe;
1899         struct trace_parser             parser;
1900         struct ftrace_hash              *hash;
1901         struct ftrace_ops               *ops;
1902         int                             hidx;
1903         int                             idx;
1904         unsigned                        flags;
1905 };
1906
1907 static void *
1908 t_hash_next(struct seq_file *m, loff_t *pos)
1909 {
1910         struct ftrace_iterator *iter = m->private;
1911         struct hlist_node *hnd = NULL;
1912         struct hlist_head *hhd;
1913
1914         (*pos)++;
1915         iter->pos = *pos;
1916
1917         if (iter->probe)
1918                 hnd = &iter->probe->node;
1919  retry:
1920         if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1921                 return NULL;
1922
1923         hhd = &ftrace_func_hash[iter->hidx];
1924
1925         if (hlist_empty(hhd)) {
1926                 iter->hidx++;
1927                 hnd = NULL;
1928                 goto retry;
1929         }
1930
1931         if (!hnd)
1932                 hnd = hhd->first;
1933         else {
1934                 hnd = hnd->next;
1935                 if (!hnd) {
1936                         iter->hidx++;
1937                         goto retry;
1938                 }
1939         }
1940
1941         if (WARN_ON_ONCE(!hnd))
1942                 return NULL;
1943
1944         iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
1945
1946         return iter;
1947 }
1948
1949 static void *t_hash_start(struct seq_file *m, loff_t *pos)
1950 {
1951         struct ftrace_iterator *iter = m->private;
1952         void *p = NULL;
1953         loff_t l;
1954
1955         if (iter->func_pos > *pos)
1956                 return NULL;
1957
1958         iter->hidx = 0;
1959         for (l = 0; l <= (*pos - iter->func_pos); ) {
1960                 p = t_hash_next(m, &l);
1961                 if (!p)
1962                         break;
1963         }
1964         if (!p)
1965                 return NULL;
1966
1967         /* Only set this if we have an item */
1968         iter->flags |= FTRACE_ITER_HASH;
1969
1970         return iter;
1971 }
1972
1973 static int
1974 t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
1975 {
1976         struct ftrace_func_probe *rec;
1977
1978         rec = iter->probe;
1979         if (WARN_ON_ONCE(!rec))
1980                 return -EIO;
1981
1982         if (rec->ops->print)
1983                 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1984
1985         seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
1986
1987         if (rec->data)
1988                 seq_printf(m, ":%p", rec->data);
1989         seq_putc(m, '\n');
1990
1991         return 0;
1992 }
1993
1994 static void *
1995 t_next(struct seq_file *m, void *v, loff_t *pos)
1996 {
1997         struct ftrace_iterator *iter = m->private;
1998         struct ftrace_ops *ops = &global_ops;
1999         struct dyn_ftrace *rec = NULL;
2000
2001         if (unlikely(ftrace_disabled))
2002                 return NULL;
2003
2004         if (iter->flags & FTRACE_ITER_HASH)
2005                 return t_hash_next(m, pos);
2006
2007         (*pos)++;
2008         iter->pos = iter->func_pos = *pos;
2009
2010         if (iter->flags & FTRACE_ITER_PRINTALL)
2011                 return t_hash_start(m, pos);
2012
2013  retry:
2014         if (iter->idx >= iter->pg->index) {
2015                 if (iter->pg->next) {
2016                         iter->pg = iter->pg->next;
2017                         iter->idx = 0;
2018                         goto retry;
2019                 }
2020         } else {
2021                 rec = &iter->pg->records[iter->idx++];
2022                 if ((rec->flags & FTRACE_FL_FREE) ||
2023
2024                     ((iter->flags & FTRACE_ITER_FILTER) &&
2025                      !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
2026
2027                     ((iter->flags & FTRACE_ITER_NOTRACE) &&
2028                      !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2029
2030                     ((iter->flags & FTRACE_ITER_ENABLED) &&
2031                      !(rec->flags & ~FTRACE_FL_MASK))) {
2032
2033                         rec = NULL;
2034                         goto retry;
2035                 }
2036         }
2037
2038         if (!rec)
2039                 return t_hash_start(m, pos);
2040
2041         iter->func = rec;
2042
2043         return iter;
2044 }
2045
2046 static void reset_iter_read(struct ftrace_iterator *iter)
2047 {
2048         iter->pos = 0;
2049         iter->func_pos = 0;
2050         iter->flags &= ~(FTRACE_ITER_PRINTALL & FTRACE_ITER_HASH);
2051 }
2052
2053 static void *t_start(struct seq_file *m, loff_t *pos)
2054 {
2055         struct ftrace_iterator *iter = m->private;
2056         struct ftrace_ops *ops = &global_ops;
2057         void *p = NULL;
2058         loff_t l;
2059
2060         mutex_lock(&ftrace_lock);
2061
2062         if (unlikely(ftrace_disabled))
2063                 return NULL;
2064
2065         /*
2066          * If an lseek was done, then reset and start from beginning.
2067          */
2068         if (*pos < iter->pos)
2069                 reset_iter_read(iter);
2070
2071         /*
2072          * For set_ftrace_filter reading, if we have the filter
2073          * off, we can short cut and just print out that all
2074          * functions are enabled.
2075          */
2076         if (iter->flags & FTRACE_ITER_FILTER && !ops->filter_hash->count) {
2077                 if (*pos > 0)
2078                         return t_hash_start(m, pos);
2079                 iter->flags |= FTRACE_ITER_PRINTALL;
2080                 /* reset in case of seek/pread */
2081                 iter->flags &= ~FTRACE_ITER_HASH;
2082                 return iter;
2083         }
2084
2085         if (iter->flags & FTRACE_ITER_HASH)
2086                 return t_hash_start(m, pos);
2087
2088         /*
2089          * Unfortunately, we need to restart at ftrace_pages_start
2090          * every time we let go of the ftrace_mutex. This is because
2091          * those pointers can change without the lock.
2092          */
2093         iter->pg = ftrace_pages_start;
2094         iter->idx = 0;
2095         for (l = 0; l <= *pos; ) {
2096                 p = t_next(m, p, &l);
2097                 if (!p)
2098                         break;
2099         }
2100
2101         if (!p) {
2102                 if (iter->flags & FTRACE_ITER_FILTER)
2103                         return t_hash_start(m, pos);
2104
2105                 return NULL;
2106         }
2107
2108         return iter;
2109 }
2110
2111 static void t_stop(struct seq_file *m, void *p)
2112 {
2113         mutex_unlock(&ftrace_lock);
2114 }
2115
2116 static int t_show(struct seq_file *m, void *v)
2117 {
2118         struct ftrace_iterator *iter = m->private;
2119         struct dyn_ftrace *rec;
2120
2121         if (iter->flags & FTRACE_ITER_HASH)
2122                 return t_hash_show(m, iter);
2123
2124         if (iter->flags & FTRACE_ITER_PRINTALL) {
2125                 seq_printf(m, "#### all functions enabled ####\n");
2126                 return 0;
2127         }
2128
2129         rec = iter->func;
2130
2131         if (!rec)
2132                 return 0;
2133
2134         seq_printf(m, "%ps", (void *)rec->ip);
2135         if (iter->flags & FTRACE_ITER_ENABLED)
2136                 seq_printf(m, " (%ld)",
2137                            rec->flags & ~FTRACE_FL_MASK);
2138         seq_printf(m, "\n");
2139
2140         return 0;
2141 }
2142
2143 static const struct seq_operations show_ftrace_seq_ops = {
2144         .start = t_start,
2145         .next = t_next,
2146         .stop = t_stop,
2147         .show = t_show,
2148 };
2149
2150 static int
2151 ftrace_avail_open(struct inode *inode, struct file *file)
2152 {
2153         struct ftrace_iterator *iter;
2154         int ret;
2155
2156         if (unlikely(ftrace_disabled))
2157                 return -ENODEV;
2158
2159         iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2160         if (!iter)
2161                 return -ENOMEM;
2162
2163         iter->pg = ftrace_pages_start;
2164
2165         ret = seq_open(file, &show_ftrace_seq_ops);
2166         if (!ret) {
2167                 struct seq_file *m = file->private_data;
2168
2169                 m->private = iter;
2170         } else {
2171                 kfree(iter);
2172         }
2173
2174         return ret;
2175 }
2176
2177 static int
2178 ftrace_enabled_open(struct inode *inode, struct file *file)
2179 {
2180         struct ftrace_iterator *iter;
2181         int ret;
2182
2183         if (unlikely(ftrace_disabled))
2184                 return -ENODEV;
2185
2186         iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2187         if (!iter)
2188                 return -ENOMEM;
2189
2190         iter->pg = ftrace_pages_start;
2191         iter->flags = FTRACE_ITER_ENABLED;
2192
2193         ret = seq_open(file, &show_ftrace_seq_ops);
2194         if (!ret) {
2195                 struct seq_file *m = file->private_data;
2196
2197                 m->private = iter;
2198         } else {
2199                 kfree(iter);
2200         }
2201
2202         return ret;
2203 }
2204
2205 static void ftrace_filter_reset(struct ftrace_hash *hash)
2206 {
2207         mutex_lock(&ftrace_lock);
2208         ftrace_hash_clear(hash);
2209         mutex_unlock(&ftrace_lock);
2210 }
2211
2212 static int
2213 ftrace_regex_open(struct ftrace_ops *ops, int flag,
2214                   struct inode *inode, struct file *file)
2215 {
2216         struct ftrace_iterator *iter;
2217         struct ftrace_hash *hash;
2218         int ret = 0;
2219
2220         if (unlikely(ftrace_disabled))
2221                 return -ENODEV;
2222
2223         iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2224         if (!iter)
2225                 return -ENOMEM;
2226
2227         if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2228                 kfree(iter);
2229                 return -ENOMEM;
2230         }
2231
2232         if (flag & FTRACE_ITER_NOTRACE)
2233                 hash = ops->notrace_hash;
2234         else
2235                 hash = ops->filter_hash;
2236
2237         iter->ops = ops;
2238         iter->flags = flag;
2239
2240         if (file->f_mode & FMODE_WRITE) {
2241                 mutex_lock(&ftrace_lock);
2242                 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2243                 mutex_unlock(&ftrace_lock);
2244
2245                 if (!iter->hash) {
2246                         trace_parser_put(&iter->parser);
2247                         kfree(iter);
2248                         return -ENOMEM;
2249                 }
2250         }
2251
2252         mutex_lock(&ftrace_regex_lock);
2253
2254         if ((file->f_mode & FMODE_WRITE) &&
2255             (file->f_flags & O_TRUNC))
2256                 ftrace_filter_reset(iter->hash);
2257
2258         if (file->f_mode & FMODE_READ) {
2259                 iter->pg = ftrace_pages_start;
2260
2261                 ret = seq_open(file, &show_ftrace_seq_ops);
2262                 if (!ret) {
2263                         struct seq_file *m = file->private_data;
2264                         m->private = iter;
2265                 } else {
2266                         /* Failed */
2267                         free_ftrace_hash(iter->hash);
2268                         trace_parser_put(&iter->parser);
2269                         kfree(iter);
2270                 }
2271         } else
2272                 file->private_data = iter;
2273         mutex_unlock(&ftrace_regex_lock);
2274
2275         return ret;
2276 }
2277
2278 static int
2279 ftrace_filter_open(struct inode *inode, struct file *file)
2280 {
2281         return ftrace_regex_open(&global_ops, FTRACE_ITER_FILTER,
2282                                  inode, file);
2283 }
2284
2285 static int
2286 ftrace_notrace_open(struct inode *inode, struct file *file)
2287 {
2288         return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
2289                                  inode, file);
2290 }
2291
2292 static loff_t
2293 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
2294 {
2295         loff_t ret;
2296
2297         if (file->f_mode & FMODE_READ)
2298                 ret = seq_lseek(file, offset, origin);
2299         else
2300                 file->f_pos = ret = 1;
2301
2302         return ret;
2303 }
2304
2305 static int ftrace_match(char *str, char *regex, int len, int type)
2306 {
2307         int matched = 0;
2308         int slen;
2309
2310         switch (type) {
2311         case MATCH_FULL:
2312                 if (strcmp(str, regex) == 0)
2313                         matched = 1;
2314                 break;
2315         case MATCH_FRONT_ONLY:
2316                 if (strncmp(str, regex, len) == 0)
2317                         matched = 1;
2318                 break;
2319         case MATCH_MIDDLE_ONLY:
2320                 if (strstr(str, regex))
2321                         matched = 1;
2322                 break;
2323         case MATCH_END_ONLY:
2324                 slen = strlen(str);
2325                 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
2326                         matched = 1;
2327                 break;
2328         }
2329
2330         return matched;
2331 }
2332
2333 static int
2334 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
2335 {
2336         struct ftrace_func_entry *entry;
2337         int ret = 0;
2338
2339         entry = ftrace_lookup_ip(hash, rec->ip);
2340         if (not) {
2341                 /* Do nothing if it doesn't exist */
2342                 if (!entry)
2343                         return 0;
2344
2345                 free_hash_entry(hash, entry);
2346         } else {
2347                 /* Do nothing if it exists */
2348                 if (entry)
2349                         return 0;
2350
2351                 ret = add_hash_entry(hash, rec->ip);
2352         }
2353         return ret;
2354 }
2355
2356 static int
2357 ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2358                     char *regex, int len, int type)
2359 {
2360         char str[KSYM_SYMBOL_LEN];
2361         char *modname;
2362
2363         kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2364
2365         if (mod) {
2366                 /* module lookup requires matching the module */
2367                 if (!modname || strcmp(modname, mod))
2368                         return 0;
2369
2370                 /* blank search means to match all funcs in the mod */
2371                 if (!len)
2372                         return 1;
2373         }
2374
2375         return ftrace_match(str, regex, len, type);
2376 }
2377
2378 static int
2379 match_records(struct ftrace_hash *hash, char *buff,
2380               int len, char *mod, int not)
2381 {
2382         unsigned search_len = 0;
2383         struct ftrace_page *pg;
2384         struct dyn_ftrace *rec;
2385         int type = MATCH_FULL;
2386         char *search = buff;
2387         int found = 0;
2388         int ret;
2389
2390         if (len) {
2391                 type = filter_parse_regex(buff, len, &search, &not);
2392                 search_len = strlen(search);
2393         }
2394
2395         mutex_lock(&ftrace_lock);
2396
2397         if (unlikely(ftrace_disabled))
2398                 goto out_unlock;
2399
2400         do_for_each_ftrace_rec(pg, rec) {
2401
2402                 if (ftrace_match_record(rec, mod, search, search_len, type)) {
2403                         ret = enter_record(hash, rec, not);
2404                         if (ret < 0) {
2405                                 found = ret;
2406                                 goto out_unlock;
2407                         }
2408                         found = 1;
2409                 }
2410         } while_for_each_ftrace_rec();
2411  out_unlock:
2412         mutex_unlock(&ftrace_lock);
2413
2414         return found;
2415 }
2416
2417 static int
2418 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
2419 {
2420         return match_records(hash, buff, len, NULL, 0);
2421 }
2422
2423 static int
2424 ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
2425 {
2426         int not = 0;
2427
2428         /* blank or '*' mean the same */
2429         if (strcmp(buff, "*") == 0)
2430                 buff[0] = 0;
2431
2432         /* handle the case of 'dont filter this module' */
2433         if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2434                 buff[0] = 0;
2435                 not = 1;
2436         }
2437
2438         return match_records(hash, buff, strlen(buff), mod, not);
2439 }
2440
2441 /*
2442  * We register the module command as a template to show others how
2443  * to register the a command as well.
2444  */
2445
2446 static int
2447 ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
2448 {
2449         struct ftrace_ops *ops = &global_ops;
2450         struct ftrace_hash *hash;
2451         char *mod;
2452         int ret = -EINVAL;
2453
2454         /*
2455          * cmd == 'mod' because we only registered this func
2456          * for the 'mod' ftrace_func_command.
2457          * But if you register one func with multiple commands,
2458          * you can tell which command was used by the cmd
2459          * parameter.
2460          */
2461
2462         /* we must have a module name */
2463         if (!param)
2464                 return ret;
2465
2466         mod = strsep(&param, ":");
2467         if (!strlen(mod))
2468                 return ret;
2469
2470         if (enable)
2471                 hash = ops->filter_hash;
2472         else
2473                 hash = ops->notrace_hash;
2474
2475         ret = ftrace_match_module_records(hash, func, mod);
2476         if (!ret)
2477                 ret = -EINVAL;
2478         if (ret < 0)
2479                 return ret;
2480
2481         return 0;
2482 }
2483
2484 static struct ftrace_func_command ftrace_mod_cmd = {
2485         .name                   = "mod",
2486         .func                   = ftrace_mod_callback,
2487 };
2488
2489 static int __init ftrace_mod_cmd_init(void)
2490 {
2491         return register_ftrace_command(&ftrace_mod_cmd);
2492 }
2493 device_initcall(ftrace_mod_cmd_init);
2494
2495 static void
2496 function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
2497 {
2498         struct ftrace_func_probe *entry;
2499         struct hlist_head *hhd;
2500         struct hlist_node *n;
2501         unsigned long key;
2502
2503         key = hash_long(ip, FTRACE_HASH_BITS);
2504
2505         hhd = &ftrace_func_hash[key];
2506
2507         if (hlist_empty(hhd))
2508                 return;
2509
2510         /*
2511          * Disable preemption for these calls to prevent a RCU grace
2512          * period. This syncs the hash iteration and freeing of items
2513          * on the hash. rcu_read_lock is too dangerous here.
2514          */
2515         preempt_disable_notrace();
2516         hlist_for_each_entry_rcu(entry, n, hhd, node) {
2517                 if (entry->ip == ip)
2518                         entry->ops->func(ip, parent_ip, &entry->data);
2519         }
2520         preempt_enable_notrace();
2521 }
2522
2523 static struct ftrace_ops trace_probe_ops __read_mostly =
2524 {
2525         .func           = function_trace_probe_call,
2526 };
2527
2528 static int ftrace_probe_registered;
2529
2530 static void __enable_ftrace_function_probe(void)
2531 {
2532         int ret;
2533         int i;
2534
2535         if (ftrace_probe_registered)
2536                 return;
2537
2538         for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2539                 struct hlist_head *hhd = &ftrace_func_hash[i];
2540                 if (hhd->first)
2541                         break;
2542         }
2543         /* Nothing registered? */
2544         if (i == FTRACE_FUNC_HASHSIZE)
2545                 return;
2546
2547         ret = __register_ftrace_function(&trace_probe_ops);
2548         if (!ret)
2549                 ret = ftrace_startup(&trace_probe_ops, 0);
2550
2551         ftrace_probe_registered = 1;
2552 }
2553
2554 static void __disable_ftrace_function_probe(void)
2555 {
2556         int ret;
2557         int i;
2558
2559         if (!ftrace_probe_registered)
2560                 return;
2561
2562         for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2563                 struct hlist_head *hhd = &ftrace_func_hash[i];
2564                 if (hhd->first)
2565                         return;
2566         }
2567
2568         /* no more funcs left */
2569         ret = __unregister_ftrace_function(&trace_probe_ops);
2570         if (!ret)
2571                 ftrace_shutdown(&trace_probe_ops, 0);
2572
2573         ftrace_probe_registered = 0;
2574 }
2575
2576
2577 static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2578 {
2579         struct ftrace_func_probe *entry =
2580                 container_of(rhp, struct ftrace_func_probe, rcu);
2581
2582         if (entry->ops->free)
2583                 entry->ops->free(&entry->data);
2584         kfree(entry);
2585 }
2586
2587
2588 int
2589 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2590                               void *data)
2591 {
2592         struct ftrace_func_probe *entry;
2593         struct ftrace_page *pg;
2594         struct dyn_ftrace *rec;
2595         int type, len, not;
2596         unsigned long key;
2597         int count = 0;
2598         char *search;
2599
2600         type = filter_parse_regex(glob, strlen(glob), &search, &not);
2601         len = strlen(search);
2602
2603         /* we do not support '!' for function probes */
2604         if (WARN_ON(not))
2605                 return -EINVAL;
2606
2607         mutex_lock(&ftrace_lock);
2608
2609         if (unlikely(ftrace_disabled))
2610                 goto out_unlock;
2611
2612         do_for_each_ftrace_rec(pg, rec) {
2613
2614                 if (!ftrace_match_record(rec, NULL, search, len, type))
2615                         continue;
2616
2617                 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2618                 if (!entry) {
2619                         /* If we did not process any, then return error */
2620                         if (!count)
2621                                 count = -ENOMEM;
2622                         goto out_unlock;
2623                 }
2624
2625                 count++;
2626
2627                 entry->data = data;
2628
2629                 /*
2630                  * The caller might want to do something special
2631                  * for each function we find. We call the callback
2632                  * to give the caller an opportunity to do so.
2633                  */
2634                 if (ops->callback) {
2635                         if (ops->callback(rec->ip, &entry->data) < 0) {
2636                                 /* caller does not like this func */
2637                                 kfree(entry);
2638                                 continue;
2639                         }
2640                 }
2641
2642                 entry->ops = ops;
2643                 entry->ip = rec->ip;
2644
2645                 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2646                 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2647
2648         } while_for_each_ftrace_rec();
2649         __enable_ftrace_function_probe();
2650
2651  out_unlock:
2652         mutex_unlock(&ftrace_lock);
2653
2654         return count;
2655 }
2656
2657 enum {
2658         PROBE_TEST_FUNC         = 1,
2659         PROBE_TEST_DATA         = 2
2660 };
2661
2662 static void
2663 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2664                                   void *data, int flags)
2665 {
2666         struct ftrace_func_probe *entry;
2667         struct hlist_node *n, *tmp;
2668         char str[KSYM_SYMBOL_LEN];
2669         int type = MATCH_FULL;
2670         int i, len = 0;
2671         char *search;
2672
2673         if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
2674                 glob = NULL;
2675         else if (glob) {
2676                 int not;
2677
2678                 type = filter_parse_regex(glob, strlen(glob), &search, &not);
2679                 len = strlen(search);
2680
2681                 /* we do not support '!' for function probes */
2682                 if (WARN_ON(not))
2683                         return;
2684         }
2685
2686         mutex_lock(&ftrace_lock);
2687         for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2688                 struct hlist_head *hhd = &ftrace_func_hash[i];
2689
2690                 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2691
2692                         /* break up if statements for readability */
2693                         if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
2694                                 continue;
2695
2696                         if ((flags & PROBE_TEST_DATA) && entry->data != data)
2697                                 continue;
2698
2699                         /* do this last, since it is the most expensive */
2700                         if (glob) {
2701                                 kallsyms_lookup(entry->ip, NULL, NULL,
2702                                                 NULL, str);
2703                                 if (!ftrace_match(str, glob, len, type))
2704                                         continue;
2705                         }
2706
2707                         hlist_del(&entry->node);
2708                         call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2709                 }
2710         }
2711         __disable_ftrace_function_probe();
2712         mutex_unlock(&ftrace_lock);
2713 }
2714
2715 void
2716 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2717                                 void *data)
2718 {
2719         __unregister_ftrace_function_probe(glob, ops, data,
2720                                           PROBE_TEST_FUNC | PROBE_TEST_DATA);
2721 }
2722
2723 void
2724 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
2725 {
2726         __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
2727 }
2728
2729 void unregister_ftrace_function_probe_all(char *glob)
2730 {
2731         __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
2732 }
2733
2734 static LIST_HEAD(ftrace_commands);
2735 static DEFINE_MUTEX(ftrace_cmd_mutex);
2736
2737 int register_ftrace_command(struct ftrace_func_command *cmd)
2738 {
2739         struct ftrace_func_command *p;
2740         int ret = 0;
2741
2742         mutex_lock(&ftrace_cmd_mutex);
2743         list_for_each_entry(p, &ftrace_commands, list) {
2744                 if (strcmp(cmd->name, p->name) == 0) {
2745                         ret = -EBUSY;
2746                         goto out_unlock;
2747                 }
2748         }
2749         list_add(&cmd->list, &ftrace_commands);
2750  out_unlock:
2751         mutex_unlock(&ftrace_cmd_mutex);
2752
2753         return ret;
2754 }
2755
2756 int unregister_ftrace_command(struct ftrace_func_command *cmd)
2757 {
2758         struct ftrace_func_command *p, *n;
2759         int ret = -ENODEV;
2760
2761         mutex_lock(&ftrace_cmd_mutex);
2762         list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2763                 if (strcmp(cmd->name, p->name) == 0) {
2764                         ret = 0;
2765                         list_del_init(&p->list);
2766                         goto out_unlock;
2767                 }
2768         }
2769  out_unlock:
2770         mutex_unlock(&ftrace_cmd_mutex);
2771
2772         return ret;
2773 }
2774
2775 static int ftrace_process_regex(struct ftrace_hash *hash,
2776                                 char *buff, int len, int enable)
2777 {
2778         char *func, *command, *next = buff;
2779         struct ftrace_func_command *p;
2780         int ret = -EINVAL;
2781
2782         func = strsep(&next, ":");
2783
2784         if (!next) {
2785                 ret = ftrace_match_records(hash, func, len);
2786                 if (!ret)
2787                         ret = -EINVAL;
2788                 if (ret < 0)
2789                         return ret;
2790                 return 0;
2791         }
2792
2793         /* command found */
2794
2795         command = strsep(&next, ":");
2796
2797         mutex_lock(&ftrace_cmd_mutex);
2798         list_for_each_entry(p, &ftrace_commands, list) {
2799                 if (strcmp(p->name, command) == 0) {
2800                         ret = p->func(func, command, next, enable);
2801                         goto out_unlock;
2802                 }
2803         }
2804  out_unlock:
2805         mutex_unlock(&ftrace_cmd_mutex);
2806
2807         return ret;
2808 }
2809
2810 static ssize_t
2811 ftrace_regex_write(struct file *file, const char __user *ubuf,
2812                    size_t cnt, loff_t *ppos, int enable)
2813 {
2814         struct ftrace_iterator *iter;
2815         struct trace_parser *parser;
2816         ssize_t ret, read;
2817
2818         if (!cnt)
2819                 return 0;
2820
2821         mutex_lock(&ftrace_regex_lock);
2822
2823         ret = -ENODEV;
2824         if (unlikely(ftrace_disabled))
2825                 goto out_unlock;
2826
2827         if (file->f_mode & FMODE_READ) {
2828                 struct seq_file *m = file->private_data;
2829                 iter = m->private;
2830         } else
2831                 iter = file->private_data;
2832
2833         parser = &iter->parser;
2834         read = trace_get_user(parser, ubuf, cnt, ppos);
2835
2836         if (read >= 0 && trace_parser_loaded(parser) &&
2837             !trace_parser_cont(parser)) {
2838                 ret = ftrace_process_regex(iter->hash, parser->buffer,
2839                                            parser->idx, enable);
2840                 trace_parser_clear(parser);
2841                 if (ret)
2842                         goto out_unlock;
2843         }
2844
2845         ret = read;
2846 out_unlock:
2847         mutex_unlock(&ftrace_regex_lock);
2848
2849         return ret;
2850 }
2851
2852 static ssize_t
2853 ftrace_filter_write(struct file *file, const char __user *ubuf,
2854                     size_t cnt, loff_t *ppos)
2855 {
2856         return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2857 }
2858
2859 static ssize_t
2860 ftrace_notrace_write(struct file *file, const char __user *ubuf,
2861                      size_t cnt, loff_t *ppos)
2862 {
2863         return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2864 }
2865
2866 static int
2867 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
2868                  int reset, int enable)
2869 {
2870         struct ftrace_hash **orig_hash;
2871         struct ftrace_hash *hash;
2872         int ret;
2873
2874         /* All global ops uses the global ops filters */
2875         if (ops->flags & FTRACE_OPS_FL_GLOBAL)
2876                 ops = &global_ops;
2877
2878         if (unlikely(ftrace_disabled))
2879                 return -ENODEV;
2880
2881         if (enable)
2882                 orig_hash = &ops->filter_hash;
2883         else
2884                 orig_hash = &ops->notrace_hash;
2885
2886         hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
2887         if (!hash)
2888                 return -ENOMEM;
2889
2890         mutex_lock(&ftrace_regex_lock);
2891         if (reset)
2892                 ftrace_filter_reset(hash);
2893         if (buf)
2894                 ftrace_match_records(hash, buf, len);
2895
2896         mutex_lock(&ftrace_lock);
2897         ret = ftrace_hash_move(ops, enable, orig_hash, hash);
2898         if (!ret && ops->flags & FTRACE_OPS_FL_ENABLED
2899             && ftrace_enabled)
2900                 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
2901
2902         mutex_unlock(&ftrace_lock);
2903
2904         mutex_unlock(&ftrace_regex_lock);
2905
2906         free_ftrace_hash(hash);
2907         return ret;
2908 }
2909
2910 /**
2911  * ftrace_set_filter - set a function to filter on in ftrace
2912  * @ops - the ops to set the filter with
2913  * @buf - the string that holds the function filter text.
2914  * @len - the length of the string.
2915  * @reset - non zero to reset all filters before applying this filter.
2916  *
2917  * Filters denote which functions should be enabled when tracing is enabled.
2918  * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2919  */
2920 void ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
2921                        int len, int reset)
2922 {
2923         ftrace_set_regex(ops, buf, len, reset, 1);
2924 }
2925 EXPORT_SYMBOL_GPL(ftrace_set_filter);
2926
2927 /**
2928  * ftrace_set_notrace - set a function to not trace in ftrace
2929  * @ops - the ops to set the notrace filter with
2930  * @buf - the string that holds the function notrace text.
2931  * @len - the length of the string.
2932  * @reset - non zero to reset all filters before applying this filter.
2933  *
2934  * Notrace Filters denote which functions should not be enabled when tracing
2935  * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2936  * for tracing.
2937  */
2938 void ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
2939                         int len, int reset)
2940 {
2941         ftrace_set_regex(ops, buf, len, reset, 0);
2942 }
2943 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
2944 /**
2945  * ftrace_set_filter - set a function to filter on in ftrace
2946  * @ops - the ops to set the filter with
2947  * @buf - the string that holds the function filter text.
2948  * @len - the length of the string.
2949  * @reset - non zero to reset all filters before applying this filter.
2950  *
2951  * Filters denote which functions should be enabled when tracing is enabled.
2952  * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2953  */
2954 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
2955 {
2956         ftrace_set_regex(&global_ops, buf, len, reset, 1);
2957 }
2958 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
2959
2960 /**
2961  * ftrace_set_notrace - set a function to not trace in ftrace
2962  * @ops - the ops to set the notrace filter with
2963  * @buf - the string that holds the function notrace text.
2964  * @len - the length of the string.
2965  * @reset - non zero to reset all filters before applying this filter.
2966  *
2967  * Notrace Filters denote which functions should not be enabled when tracing
2968  * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2969  * for tracing.
2970  */
2971 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
2972 {
2973         ftrace_set_regex(&global_ops, buf, len, reset, 0);
2974 }
2975 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
2976
2977 /*
2978  * command line interface to allow users to set filters on boot up.
2979  */
2980 #define FTRACE_FILTER_SIZE              COMMAND_LINE_SIZE
2981 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
2982 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
2983
2984 static int __init set_ftrace_notrace(char *str)
2985 {
2986         strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
2987         return 1;
2988 }
2989 __setup("ftrace_notrace=", set_ftrace_notrace);
2990
2991 static int __init set_ftrace_filter(char *str)
2992 {
2993         strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
2994         return 1;
2995 }
2996 __setup("ftrace_filter=", set_ftrace_filter);
2997
2998 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2999 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
3000 static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
3001
3002 static int __init set_graph_function(char *str)
3003 {
3004         strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
3005         return 1;
3006 }
3007 __setup("ftrace_graph_filter=", set_graph_function);
3008
3009 static void __init set_ftrace_early_graph(char *buf)
3010 {
3011         int ret;
3012         char *func;
3013
3014         while (buf) {
3015                 func = strsep(&buf, ",");
3016                 /* we allow only one expression at a time */
3017                 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3018                                       func);
3019                 if (ret)
3020                         printk(KERN_DEBUG "ftrace: function %s not "
3021                                           "traceable\n", func);
3022         }
3023 }
3024 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3025
3026 static void __init
3027 set_ftrace_early_filter(struct ftrace_ops *ops, char *buf, int enable)
3028 {
3029         char *func;
3030
3031         while (buf) {
3032                 func = strsep(&buf, ",");
3033                 ftrace_set_regex(ops, func, strlen(func), 0, enable);
3034         }
3035 }
3036
3037 static void __init set_ftrace_early_filters(void)
3038 {
3039         if (ftrace_filter_buf[0])
3040                 set_ftrace_early_filter(&global_ops, ftrace_filter_buf, 1);
3041         if (ftrace_notrace_buf[0])
3042                 set_ftrace_early_filter(&global_ops, ftrace_notrace_buf, 0);
3043 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3044         if (ftrace_graph_buf[0])
3045                 set_ftrace_early_graph(ftrace_graph_buf);
3046 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3047 }
3048
3049 static int
3050 ftrace_regex_release(struct inode *inode, struct file *file)
3051 {
3052         struct seq_file *m = (struct seq_file *)file->private_data;
3053         struct ftrace_iterator *iter;
3054         struct ftrace_hash **orig_hash;
3055         struct trace_parser *parser;
3056         int filter_hash;
3057         int ret;
3058
3059         mutex_lock(&ftrace_regex_lock);
3060         if (file->f_mode & FMODE_READ) {
3061                 iter = m->private;
3062
3063                 seq_release(inode, file);
3064         } else
3065                 iter = file->private_data;
3066
3067         parser = &iter->parser;
3068         if (trace_parser_loaded(parser)) {
3069                 parser->buffer[parser->idx] = 0;
3070                 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
3071         }
3072
3073         trace_parser_put(parser);
3074
3075         if (file->f_mode & FMODE_WRITE) {
3076                 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3077
3078                 if (filter_hash)
3079                         orig_hash = &iter->ops->filter_hash;
3080                 else
3081                         orig_hash = &iter->ops->notrace_hash;
3082
3083                 mutex_lock(&ftrace_lock);
3084                 ret = ftrace_hash_move(iter->ops, filter_hash,
3085                                        orig_hash, iter->hash);
3086                 if (!ret && (iter->ops->flags & FTRACE_OPS_FL_ENABLED)
3087                     && ftrace_enabled)
3088                         ftrace_run_update_code(FTRACE_ENABLE_CALLS);
3089
3090                 mutex_unlock(&ftrace_lock);
3091         }
3092         free_ftrace_hash(iter->hash);
3093         kfree(iter);
3094
3095         mutex_unlock(&ftrace_regex_lock);
3096         return 0;
3097 }
3098
3099 static const struct file_operations ftrace_avail_fops = {
3100         .open = ftrace_avail_open,
3101         .read = seq_read,
3102         .llseek = seq_lseek,
3103         .release = seq_release_private,
3104 };
3105
3106 static const struct file_operations ftrace_enabled_fops = {
3107         .open = ftrace_enabled_open,
3108         .read = seq_read,
3109         .llseek = seq_lseek,
3110         .release = seq_release_private,
3111 };
3112
3113 static const struct file_operations ftrace_filter_fops = {
3114         .open = ftrace_filter_open,
3115         .read = seq_read,
3116         .write = ftrace_filter_write,
3117         .llseek = ftrace_regex_lseek,
3118         .release = ftrace_regex_release,
3119 };
3120
3121 static const struct file_operations ftrace_notrace_fops = {
3122         .open = ftrace_notrace_open,
3123         .read = seq_read,
3124         .write = ftrace_notrace_write,
3125         .llseek = ftrace_regex_lseek,
3126         .release = ftrace_regex_release,
3127 };
3128
3129 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3130
3131 static DEFINE_MUTEX(graph_lock);
3132
3133 int ftrace_graph_count;
3134 int ftrace_graph_filter_enabled;
3135 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3136
3137 static void *
3138 __g_next(struct seq_file *m, loff_t *pos)
3139 {
3140         if (*pos >= ftrace_graph_count)
3141                 return NULL;
3142         return &ftrace_graph_funcs[*pos];
3143 }
3144
3145 static void *
3146 g_next(struct seq_file *m, void *v, loff_t *pos)
3147 {
3148         (*pos)++;
3149         return __g_next(m, pos);
3150 }
3151
3152 static void *g_start(struct seq_file *m, loff_t *pos)
3153 {
3154         mutex_lock(&graph_lock);
3155
3156         /* Nothing, tell g_show to print all functions are enabled */
3157         if (!ftrace_graph_filter_enabled && !*pos)
3158                 return (void *)1;
3159
3160         return __g_next(m, pos);
3161 }
3162
3163 static void g_stop(struct seq_file *m, void *p)
3164 {
3165         mutex_unlock(&graph_lock);
3166 }
3167
3168 static int g_show(struct seq_file *m, void *v)
3169 {
3170         unsigned long *ptr = v;
3171
3172         if (!ptr)
3173                 return 0;
3174
3175         if (ptr == (unsigned long *)1) {
3176                 seq_printf(m, "#### all functions enabled ####\n");
3177                 return 0;
3178         }
3179
3180         seq_printf(m, "%ps\n", (void *)*ptr);
3181
3182         return 0;
3183 }
3184
3185 static const struct seq_operations ftrace_graph_seq_ops = {
3186         .start = g_start,
3187         .next = g_next,
3188         .stop = g_stop,
3189         .show = g_show,
3190 };
3191
3192 static int
3193 ftrace_graph_open(struct inode *inode, struct file *file)
3194 {
3195         int ret = 0;
3196
3197         if (unlikely(ftrace_disabled))
3198                 return -ENODEV;
3199
3200         mutex_lock(&graph_lock);
3201         if ((file->f_mode & FMODE_WRITE) &&
3202             (file->f_flags & O_TRUNC)) {
3203                 ftrace_graph_filter_enabled = 0;
3204                 ftrace_graph_count = 0;
3205                 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3206         }
3207         mutex_unlock(&graph_lock);
3208
3209         if (file->f_mode & FMODE_READ)
3210                 ret = seq_open(file, &ftrace_graph_seq_ops);
3211
3212         return ret;
3213 }
3214
3215 static int
3216 ftrace_graph_release(struct inode *inode, struct file *file)
3217 {
3218         if (file->f_mode & FMODE_READ)
3219                 seq_release(inode, file);
3220         return 0;
3221 }
3222
3223 static int
3224 ftrace_set_func(unsigned long *array, int *idx, char *buffer)
3225 {
3226         struct dyn_ftrace *rec;
3227         struct ftrace_page *pg;
3228         int search_len;
3229         int fail = 1;
3230         int type, not;
3231         char *search;
3232         bool exists;
3233         int i;
3234
3235         /* decode regex */
3236         type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
3237         if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3238                 return -EBUSY;
3239
3240         search_len = strlen(search);
3241
3242         mutex_lock(&ftrace_lock);
3243
3244         if (unlikely(ftrace_disabled)) {
3245                 mutex_unlock(&ftrace_lock);
3246                 return -ENODEV;
3247         }
3248
3249         do_for_each_ftrace_rec(pg, rec) {
3250
3251                 if (rec->flags & FTRACE_FL_FREE)
3252                         continue;
3253
3254                 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
3255                         /* if it is in the array */
3256                         exists = false;
3257                         for (i = 0; i < *idx; i++) {
3258                                 if (array[i] == rec->ip) {
3259                                         exists = true;
3260                                         break;
3261                                 }
3262                         }
3263
3264                         if (!not) {
3265                                 fail = 0;
3266                                 if (!exists) {
3267                                         array[(*idx)++] = rec->ip;
3268                                         if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3269                                                 goto out;
3270                                 }
3271                         } else {
3272                                 if (exists) {
3273                                         array[i] = array[--(*idx)];
3274                                         array[*idx] = 0;
3275                                         fail = 0;
3276                                 }
3277                         }
3278                 }
3279         } while_for_each_ftrace_rec();
3280 out:
3281         mutex_unlock(&ftrace_lock);
3282
3283         if (fail)
3284                 return -EINVAL;
3285
3286         ftrace_graph_filter_enabled = 1;
3287         return 0;
3288 }
3289
3290 static ssize_t
3291 ftrace_graph_write(struct file *file, const char __user *ubuf,
3292                    size_t cnt, loff_t *ppos)
3293 {
3294         struct trace_parser parser;
3295         ssize_t read, ret;
3296
3297         if (!cnt)
3298                 return 0;
3299
3300         mutex_lock(&graph_lock);
3301
3302         if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3303                 ret = -ENOMEM;
3304                 goto out_unlock;
3305         }
3306
3307         read = trace_get_user(&parser, ubuf, cnt, ppos);
3308
3309         if (read >= 0 && trace_parser_loaded((&parser))) {
3310                 parser.buffer[parser.idx] = 0;
3311
3312                 /* we allow only one expression at a time */
3313                 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3314                                         parser.buffer);
3315                 if (ret)
3316                         goto out_free;
3317         }
3318
3319         ret = read;
3320
3321 out_free:
3322         trace_parser_put(&parser);
3323 out_unlock:
3324         mutex_unlock(&graph_lock);
3325
3326         return ret;
3327 }
3328
3329 static const struct file_operations ftrace_graph_fops = {
3330         .open           = ftrace_graph_open,
3331         .read           = seq_read,
3332         .write          = ftrace_graph_write,
3333         .release        = ftrace_graph_release,
3334         .llseek         = seq_lseek,
3335 };
3336 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3337
3338 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
3339 {
3340
3341         trace_create_file("available_filter_functions", 0444,
3342                         d_tracer, NULL, &ftrace_avail_fops);
3343
3344         trace_create_file("enabled_functions", 0444,
3345                         d_tracer, NULL, &ftrace_enabled_fops);
3346
3347         trace_create_file("set_ftrace_filter", 0644, d_tracer,
3348                         NULL, &ftrace_filter_fops);
3349
3350         trace_create_file("set_ftrace_notrace", 0644, d_tracer,
3351                                     NULL, &ftrace_notrace_fops);
3352
3353 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3354         trace_create_file("set_graph_function", 0444, d_tracer,
3355                                     NULL,
3356                                     &ftrace_graph_fops);
3357 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3358
3359         return 0;
3360 }
3361
3362 static int ftrace_process_locs(struct module *mod,
3363                                unsigned long *start,
3364                                unsigned long *end)
3365 {
3366         unsigned long *p;
3367         unsigned long addr;
3368         unsigned long flags = 0; /* Shut up gcc */
3369
3370         mutex_lock(&ftrace_lock);
3371         p = start;
3372         while (p < end) {
3373                 addr = ftrace_call_adjust(*p++);
3374                 /*
3375                  * Some architecture linkers will pad between
3376                  * the different mcount_loc sections of different
3377                  * object files to satisfy alignments.
3378                  * Skip any NULL pointers.
3379                  */
3380                 if (!addr)
3381                         continue;
3382                 ftrace_record_ip(addr);
3383         }
3384
3385         /*
3386          * We only need to disable interrupts on start up
3387          * because we are modifying code that an interrupt
3388          * may execute, and the modification is not atomic.
3389          * But for modules, nothing runs the code we modify
3390          * until we are finished with it, and there's no
3391          * reason to cause large interrupt latencies while we do it.
3392          */
3393         if (!mod)
3394                 local_irq_save(flags);
3395         ftrace_update_code(mod);
3396         if (!mod)
3397                 local_irq_restore(flags);
3398         mutex_unlock(&ftrace_lock);
3399
3400         return 0;
3401 }
3402
3403 #ifdef CONFIG_MODULES
3404 void ftrace_release_mod(struct module *mod)
3405 {
3406         struct dyn_ftrace *rec;
3407         struct ftrace_page *pg;
3408
3409         mutex_lock(&ftrace_lock);
3410
3411         if (ftrace_disabled)
3412                 goto out_unlock;
3413
3414         do_for_each_ftrace_rec(pg, rec) {
3415                 if (within_module_core(rec->ip, mod)) {
3416                         /*
3417                          * rec->ip is changed in ftrace_free_rec()
3418                          * It should not between s and e if record was freed.
3419                          */
3420                         FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
3421                         ftrace_free_rec(rec);
3422                 }
3423         } while_for_each_ftrace_rec();
3424  out_unlock:
3425         mutex_unlock(&ftrace_lock);
3426 }
3427
3428 static void ftrace_init_module(struct module *mod,
3429                                unsigned long *start, unsigned long *end)
3430 {
3431         if (ftrace_disabled || start == end)
3432                 return;
3433         ftrace_process_locs(mod, start, end);
3434 }
3435
3436 static int ftrace_module_notify(struct notifier_block *self,
3437                                 unsigned long val, void *data)
3438 {
3439         struct module *mod = data;
3440
3441         switch (val) {
3442         case MODULE_STATE_COMING:
3443                 ftrace_init_module(mod, mod->ftrace_callsites,
3444                                    mod->ftrace_callsites +
3445                                    mod->num_ftrace_callsites);
3446                 break;
3447         case MODULE_STATE_GOING:
3448                 ftrace_release_mod(mod);
3449                 break;
3450         }
3451
3452         return 0;
3453 }
3454 #else
3455 static int ftrace_module_notify(struct notifier_block *self,
3456                                 unsigned long val, void *data)
3457 {
3458         return 0;
3459 }
3460 #endif /* CONFIG_MODULES */
3461
3462 struct notifier_block ftrace_module_nb = {
3463         .notifier_call = ftrace_module_notify,
3464         .priority = 0,
3465 };
3466
3467 extern unsigned long __start_mcount_loc[];
3468 extern unsigned long __stop_mcount_loc[];
3469
3470 void __init ftrace_init(void)
3471 {
3472         unsigned long count, addr, flags;
3473         int ret;
3474
3475         /* Keep the ftrace pointer to the stub */
3476         addr = (unsigned long)ftrace_stub;
3477
3478         local_irq_save(flags);
3479         ftrace_dyn_arch_init(&addr);
3480         local_irq_restore(flags);
3481
3482         /* ftrace_dyn_arch_init places the return code in addr */
3483         if (addr)
3484                 goto failed;
3485
3486         count = __stop_mcount_loc - __start_mcount_loc;
3487
3488         ret = ftrace_dyn_table_alloc(count);
3489         if (ret)
3490                 goto failed;
3491
3492         last_ftrace_enabled = ftrace_enabled = 1;
3493
3494         ret = ftrace_process_locs(NULL,
3495                                   __start_mcount_loc,
3496                                   __stop_mcount_loc);
3497
3498         ret = register_module_notifier(&ftrace_module_nb);
3499         if (ret)
3500                 pr_warning("Failed to register trace ftrace module notifier\n");
3501
3502         set_ftrace_early_filters();
3503
3504         return;
3505  failed:
3506         ftrace_disabled = 1;
3507 }
3508
3509 #else
3510
3511 static struct ftrace_ops global_ops = {
3512         .func                   = ftrace_stub,
3513 };
3514
3515 static int __init ftrace_nodyn_init(void)
3516 {
3517         ftrace_enabled = 1;
3518         return 0;
3519 }
3520 device_initcall(ftrace_nodyn_init);
3521
3522 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
3523 static inline void ftrace_startup_enable(int command) { }
3524 /* Keep as macros so we do not need to define the commands */
3525 # define ftrace_startup(ops, command)                   \
3526         ({                                              \
3527                 (ops)->flags |= FTRACE_OPS_FL_ENABLED;  \
3528                 0;                                      \
3529         })
3530 # define ftrace_shutdown(ops, command)  do { } while (0)
3531 # define ftrace_startup_sysctl()        do { } while (0)
3532 # define ftrace_shutdown_sysctl()       do { } while (0)
3533
3534 static inline int
3535 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
3536 {
3537         return 1;
3538 }
3539
3540 #endif /* CONFIG_DYNAMIC_FTRACE */
3541
3542 static void
3543 ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip)
3544 {
3545         struct ftrace_ops *op;
3546
3547         if (unlikely(trace_recursion_test(TRACE_INTERNAL_BIT)))
3548                 return;
3549
3550         trace_recursion_set(TRACE_INTERNAL_BIT);
3551         /*
3552          * Some of the ops may be dynamically allocated,
3553          * they must be freed after a synchronize_sched().
3554          */
3555         preempt_disable_notrace();
3556         op = rcu_dereference_raw(ftrace_ops_list);
3557         while (op != &ftrace_list_end) {
3558                 if (ftrace_ops_test(op, ip))
3559                         op->func(ip, parent_ip);
3560                 op = rcu_dereference_raw(op->next);
3561         };
3562         preempt_enable_notrace();
3563         trace_recursion_clear(TRACE_INTERNAL_BIT);
3564 }
3565
3566 static void clear_ftrace_swapper(void)
3567 {
3568         struct task_struct *p;
3569         int cpu;
3570
3571         get_online_cpus();
3572         for_each_online_cpu(cpu) {
3573                 p = idle_task(cpu);
3574                 clear_tsk_trace_trace(p);
3575         }
3576         put_online_cpus();
3577 }
3578
3579 static void set_ftrace_swapper(void)
3580 {
3581         struct task_struct *p;
3582         int cpu;
3583
3584         get_online_cpus();
3585         for_each_online_cpu(cpu) {
3586                 p = idle_task(cpu);
3587                 set_tsk_trace_trace(p);
3588         }
3589         put_online_cpus();
3590 }
3591
3592 static void clear_ftrace_pid(struct pid *pid)
3593 {
3594         struct task_struct *p;
3595
3596         rcu_read_lock();
3597         do_each_pid_task(pid, PIDTYPE_PID, p) {
3598                 clear_tsk_trace_trace(p);
3599         } while_each_pid_task(pid, PIDTYPE_PID, p);
3600         rcu_read_unlock();
3601
3602         put_pid(pid);
3603 }
3604
3605 static void set_ftrace_pid(struct pid *pid)
3606 {
3607         struct task_struct *p;
3608
3609         rcu_read_lock();
3610         do_each_pid_task(pid, PIDTYPE_PID, p) {
3611                 set_tsk_trace_trace(p);
3612         } while_each_pid_task(pid, PIDTYPE_PID, p);
3613         rcu_read_unlock();
3614 }
3615
3616 static void clear_ftrace_pid_task(struct pid *pid)
3617 {
3618         if (pid == ftrace_swapper_pid)
3619                 clear_ftrace_swapper();
3620         else
3621                 clear_ftrace_pid(pid);
3622 }
3623
3624 static void set_ftrace_pid_task(struct pid *pid)
3625 {
3626         if (pid == ftrace_swapper_pid)
3627                 set_ftrace_swapper();
3628         else
3629                 set_ftrace_pid(pid);
3630 }
3631
3632 static int ftrace_pid_add(int p)
3633 {
3634         struct pid *pid;
3635         struct ftrace_pid *fpid;
3636         int ret = -EINVAL;
3637
3638         mutex_lock(&ftrace_lock);
3639
3640         if (!p)
3641                 pid = ftrace_swapper_pid;
3642         else
3643                 pid = find_get_pid(p);
3644
3645         if (!pid)
3646                 goto out;
3647
3648         ret = 0;
3649
3650         list_for_each_entry(fpid, &ftrace_pids, list)
3651                 if (fpid->pid == pid)
3652                         goto out_put;
3653
3654         ret = -ENOMEM;
3655
3656         fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
3657         if (!fpid)
3658                 goto out_put;
3659
3660         list_add(&fpid->list, &ftrace_pids);
3661         fpid->pid = pid;
3662
3663         set_ftrace_pid_task(pid);
3664
3665         ftrace_update_pid_func();
3666         ftrace_startup_enable(0);
3667
3668         mutex_unlock(&ftrace_lock);
3669         return 0;
3670
3671 out_put:
3672         if (pid != ftrace_swapper_pid)
3673                 put_pid(pid);
3674
3675 out:
3676         mutex_unlock(&ftrace_lock);
3677         return ret;
3678 }
3679
3680 static void ftrace_pid_reset(void)
3681 {
3682         struct ftrace_pid *fpid, *safe;
3683
3684         mutex_lock(&ftrace_lock);
3685         list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
3686                 struct pid *pid = fpid->pid;
3687
3688                 clear_ftrace_pid_task(pid);
3689
3690                 list_del(&fpid->list);
3691                 kfree(fpid);
3692         }
3693
3694         ftrace_update_pid_func();
3695         ftrace_startup_enable(0);
3696
3697         mutex_unlock(&ftrace_lock);
3698 }
3699
3700 static void *fpid_start(struct seq_file *m, loff_t *pos)
3701 {
3702         mutex_lock(&ftrace_lock);
3703
3704         if (list_empty(&ftrace_pids) && (!*pos))
3705                 return (void *) 1;
3706
3707         return seq_list_start(&ftrace_pids, *pos);
3708 }
3709
3710 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
3711 {
3712         if (v == (void *)1)
3713                 return NULL;
3714
3715         return seq_list_next(v, &ftrace_pids, pos);
3716 }
3717
3718 static void fpid_stop(struct seq_file *m, void *p)
3719 {
3720         mutex_unlock(&ftrace_lock);
3721 }
3722
3723 static int fpid_show(struct seq_file *m, void *v)
3724 {
3725         const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
3726
3727         if (v == (void *)1) {
3728                 seq_printf(m, "no pid\n");
3729                 return 0;
3730         }
3731
3732         if (fpid->pid == ftrace_swapper_pid)
3733                 seq_printf(m, "swapper tasks\n");
3734         else
3735                 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
3736
3737         return 0;
3738 }
3739
3740 static const struct seq_operations ftrace_pid_sops = {
3741         .start = fpid_start,
3742         .next = fpid_next,
3743         .stop = fpid_stop,
3744         .show = fpid_show,
3745 };
3746
3747 static int
3748 ftrace_pid_open(struct inode *inode, struct file *file)
3749 {
3750         int ret = 0;
3751
3752         if ((file->f_mode & FMODE_WRITE) &&
3753             (file->f_flags & O_TRUNC))
3754                 ftrace_pid_reset();
3755
3756         if (file->f_mode & FMODE_READ)
3757                 ret = seq_open(file, &ftrace_pid_sops);
3758
3759         return ret;
3760 }
3761
3762 static ssize_t
3763 ftrace_pid_write(struct file *filp, const char __user *ubuf,
3764                    size_t cnt, loff_t *ppos)
3765 {
3766         char buf[64], *tmp;
3767         long val;
3768         int ret;
3769
3770         if (cnt >= sizeof(buf))
3771                 return -EINVAL;
3772
3773         if (copy_from_user(&buf, ubuf, cnt))
3774                 return -EFAULT;
3775
3776         buf[cnt] = 0;
3777
3778         /*
3779          * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
3780          * to clean the filter quietly.
3781          */
3782         tmp = strstrip(buf);
3783         if (strlen(tmp) == 0)
3784                 return 1;
3785
3786         ret = strict_strtol(tmp, 10, &val);
3787         if (ret < 0)
3788                 return ret;
3789
3790         ret = ftrace_pid_add(val);
3791
3792         return ret ? ret : cnt;
3793 }
3794
3795 static int
3796 ftrace_pid_release(struct inode *inode, struct file *file)
3797 {
3798         if (file->f_mode & FMODE_READ)
3799                 seq_release(inode, file);
3800
3801         return 0;
3802 }
3803
3804 static const struct file_operations ftrace_pid_fops = {
3805         .open           = ftrace_pid_open,
3806         .write          = ftrace_pid_write,
3807         .read           = seq_read,
3808         .llseek         = seq_lseek,
3809         .release        = ftrace_pid_release,
3810 };
3811
3812 static __init int ftrace_init_debugfs(void)
3813 {
3814         struct dentry *d_tracer;
3815
3816         d_tracer = tracing_init_dentry();
3817         if (!d_tracer)
3818                 return 0;
3819
3820         ftrace_init_dyn_debugfs(d_tracer);
3821
3822         trace_create_file("set_ftrace_pid", 0644, d_tracer,
3823                             NULL, &ftrace_pid_fops);
3824
3825         ftrace_profile_debugfs(d_tracer);
3826
3827         return 0;
3828 }
3829 fs_initcall(ftrace_init_debugfs);
3830
3831 /**
3832  * ftrace_kill - kill ftrace
3833  *
3834  * This function should be used by panic code. It stops ftrace
3835  * but in a not so nice way. If you need to simply kill ftrace
3836  * from a non-atomic section, use ftrace_kill.
3837  */
3838 void ftrace_kill(void)
3839 {
3840         ftrace_disabled = 1;
3841         ftrace_enabled = 0;
3842         clear_ftrace_function();
3843 }
3844
3845 /**
3846  * register_ftrace_function - register a function for profiling
3847  * @ops - ops structure that holds the function for profiling.
3848  *
3849  * Register a function to be called by all functions in the
3850  * kernel.
3851  *
3852  * Note: @ops->func and all the functions it calls must be labeled
3853  *       with "notrace", otherwise it will go into a
3854  *       recursive loop.
3855  */
3856 int register_ftrace_function(struct ftrace_ops *ops)
3857 {
3858         int ret = -1;
3859
3860         mutex_lock(&ftrace_lock);
3861
3862         if (unlikely(ftrace_disabled))
3863                 goto out_unlock;
3864
3865         ret = __register_ftrace_function(ops);
3866         if (!ret)
3867                 ret = ftrace_startup(ops, 0);
3868
3869
3870  out_unlock:
3871         mutex_unlock(&ftrace_lock);
3872         return ret;
3873 }
3874 EXPORT_SYMBOL_GPL(register_ftrace_function);
3875
3876 /**
3877  * unregister_ftrace_function - unregister a function for profiling.
3878  * @ops - ops structure that holds the function to unregister
3879  *
3880  * Unregister a function that was added to be called by ftrace profiling.
3881  */
3882 int unregister_ftrace_function(struct ftrace_ops *ops)
3883 {
3884         int ret;
3885
3886         mutex_lock(&ftrace_lock);
3887         ret = __unregister_ftrace_function(ops);
3888         if (!ret)
3889                 ftrace_shutdown(ops, 0);
3890         mutex_unlock(&ftrace_lock);
3891
3892         return ret;
3893 }
3894 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
3895
3896 int
3897 ftrace_enable_sysctl(struct ctl_table *table, int write,
3898                      void __user *buffer, size_t *lenp,
3899                      loff_t *ppos)
3900 {
3901         int ret = -ENODEV;
3902
3903         mutex_lock(&ftrace_lock);
3904
3905         if (unlikely(ftrace_disabled))
3906                 goto out;
3907
3908         ret = proc_dointvec(table, write, buffer, lenp, ppos);
3909
3910         if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
3911                 goto out;
3912
3913         last_ftrace_enabled = !!ftrace_enabled;
3914
3915         if (ftrace_enabled) {
3916
3917                 ftrace_startup_sysctl();
3918
3919                 /* we are starting ftrace again */
3920                 if (ftrace_ops_list != &ftrace_list_end) {
3921                         if (ftrace_ops_list->next == &ftrace_list_end)
3922                                 ftrace_trace_function = ftrace_ops_list->func;
3923                         else
3924                                 ftrace_trace_function = ftrace_ops_list_func;
3925                 }
3926
3927         } else {
3928                 /* stopping ftrace calls (just send to ftrace_stub) */
3929                 ftrace_trace_function = ftrace_stub;
3930
3931                 ftrace_shutdown_sysctl();
3932         }
3933
3934  out:
3935         mutex_unlock(&ftrace_lock);
3936         return ret;
3937 }
3938
3939 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3940
3941 static int ftrace_graph_active;
3942 static struct notifier_block ftrace_suspend_notifier;
3943
3944 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3945 {
3946         return 0;
3947 }
3948
3949 /* The callbacks that hook a function */
3950 trace_func_graph_ret_t ftrace_graph_return =
3951                         (trace_func_graph_ret_t)ftrace_stub;
3952 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
3953
3954 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3955 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3956 {
3957         int i;
3958         int ret = 0;
3959         unsigned long flags;
3960         int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3961         struct task_struct *g, *t;
3962
3963         for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3964                 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3965                                         * sizeof(struct ftrace_ret_stack),
3966                                         GFP_KERNEL);
3967                 if (!ret_stack_list[i]) {
3968                         start = 0;
3969                         end = i;
3970                         ret = -ENOMEM;
3971                         goto free;
3972                 }
3973         }
3974
3975         read_lock_irqsave(&tasklist_lock, flags);
3976         do_each_thread(g, t) {
3977                 if (start == end) {
3978                         ret = -EAGAIN;
3979                         goto unlock;
3980                 }
3981
3982                 if (t->ret_stack == NULL) {
3983                         atomic_set(&t->tracing_graph_pause, 0);
3984                         atomic_set(&t->trace_overrun, 0);
3985                         t->curr_ret_stack = -1;
3986                         /* Make sure the tasks see the -1 first: */
3987                         smp_wmb();
3988                         t->ret_stack = ret_stack_list[start++];
3989                 }
3990         } while_each_thread(g, t);
3991
3992 unlock:
3993         read_unlock_irqrestore(&tasklist_lock, flags);
3994 free:
3995         for (i = start; i < end; i++)
3996                 kfree(ret_stack_list[i]);
3997         return ret;
3998 }
3999
4000 static void
4001 ftrace_graph_probe_sched_switch(void *ignore,
4002                         struct task_struct *prev, struct task_struct *next)
4003 {
4004         unsigned long long timestamp;
4005         int index;
4006
4007         /*
4008          * Does the user want to count the time a function was asleep.
4009          * If so, do not update the time stamps.
4010          */
4011         if (trace_flags & TRACE_ITER_SLEEP_TIME)
4012                 return;
4013
4014         timestamp = trace_clock_local();
4015
4016         prev->ftrace_timestamp = timestamp;
4017
4018         /* only process tasks that we timestamped */
4019         if (!next->ftrace_timestamp)
4020                 return;
4021
4022         /*
4023          * Update all the counters in next to make up for the
4024          * time next was sleeping.
4025          */
4026         timestamp -= next->ftrace_timestamp;
4027
4028         for (index = next->curr_ret_stack; index >= 0; index--)
4029                 next->ret_stack[index].calltime += timestamp;
4030 }
4031
4032 /* Allocate a return stack for each task */
4033 static int start_graph_tracing(void)
4034 {
4035         struct ftrace_ret_stack **ret_stack_list;
4036         int ret, cpu;
4037
4038         ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4039                                 sizeof(struct ftrace_ret_stack *),
4040                                 GFP_KERNEL);
4041
4042         if (!ret_stack_list)
4043                 return -ENOMEM;
4044
4045         /* The cpu_boot init_task->ret_stack will never be freed */
4046         for_each_online_cpu(cpu) {
4047                 if (!idle_task(cpu)->ret_stack)
4048                         ftrace_graph_init_idle_task(idle_task(cpu), cpu);
4049         }
4050
4051         do {
4052                 ret = alloc_retstack_tasklist(ret_stack_list);
4053         } while (ret == -EAGAIN);
4054
4055         if (!ret) {
4056                 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
4057                 if (ret)
4058                         pr_info("ftrace_graph: Couldn't activate tracepoint"
4059                                 " probe to kernel_sched_switch\n");
4060         }
4061
4062         kfree(ret_stack_list);
4063         return ret;
4064 }
4065
4066 /*
4067  * Hibernation protection.
4068  * The state of the current task is too much unstable during
4069  * suspend/restore to disk. We want to protect against that.
4070  */
4071 static int
4072 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4073                                                         void *unused)
4074 {
4075         switch (state) {
4076         case PM_HIBERNATION_PREPARE:
4077                 pause_graph_tracing();
4078                 break;
4079
4080         case PM_POST_HIBERNATION:
4081                 unpause_graph_tracing();
4082                 break;
4083         }
4084         return NOTIFY_DONE;
4085 }
4086
4087 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4088                         trace_func_graph_ent_t entryfunc)
4089 {
4090         int ret = 0;
4091
4092         mutex_lock(&ftrace_lock);
4093
4094         /* we currently allow only one tracer registered at a time */
4095         if (ftrace_graph_active) {
4096                 ret = -EBUSY;
4097                 goto out;
4098         }
4099
4100         ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
4101         register_pm_notifier(&ftrace_suspend_notifier);
4102
4103         ftrace_graph_active++;
4104         ret = start_graph_tracing();
4105         if (ret) {
4106                 ftrace_graph_active--;
4107                 goto out;
4108         }
4109
4110         ftrace_graph_return = retfunc;
4111         ftrace_graph_entry = entryfunc;
4112
4113         ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
4114
4115 out:
4116         mutex_unlock(&ftrace_lock);
4117         return ret;
4118 }
4119
4120 void unregister_ftrace_graph(void)
4121 {
4122         mutex_lock(&ftrace_lock);
4123
4124         if (unlikely(!ftrace_graph_active))
4125                 goto out;
4126
4127         ftrace_graph_active--;
4128         ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
4129         ftrace_graph_entry = ftrace_graph_entry_stub;
4130         ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
4131         unregister_pm_notifier(&ftrace_suspend_notifier);
4132         unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
4133
4134  out:
4135         mutex_unlock(&ftrace_lock);
4136 }
4137
4138 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
4139
4140 static void
4141 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
4142 {
4143         atomic_set(&t->tracing_graph_pause, 0);
4144         atomic_set(&t->trace_overrun, 0);
4145         t->ftrace_timestamp = 0;
4146         /* make curr_ret_stack visible before we add the ret_stack */
4147         smp_wmb();
4148         t->ret_stack = ret_stack;
4149 }
4150
4151 /*
4152  * Allocate a return stack for the idle task. May be the first
4153  * time through, or it may be done by CPU hotplug online.
4154  */
4155 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
4156 {
4157         t->curr_ret_stack = -1;
4158         /*
4159          * The idle task has no parent, it either has its own
4160          * stack or no stack at all.
4161          */
4162         if (t->ret_stack)
4163                 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
4164
4165         if (ftrace_graph_active) {
4166                 struct ftrace_ret_stack *ret_stack;
4167
4168                 ret_stack = per_cpu(idle_ret_stack, cpu);
4169                 if (!ret_stack) {
4170                         ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4171                                             * sizeof(struct ftrace_ret_stack),
4172                                             GFP_KERNEL);
4173                         if (!ret_stack)
4174                                 return;
4175                         per_cpu(idle_ret_stack, cpu) = ret_stack;
4176                 }
4177                 graph_init_task(t, ret_stack);
4178         }
4179 }
4180
4181 /* Allocate a return stack for newly created task */
4182 void ftrace_graph_init_task(struct task_struct *t)
4183 {
4184         /* Make sure we do not use the parent ret_stack */
4185         t->ret_stack = NULL;
4186         t->curr_ret_stack = -1;
4187
4188         if (ftrace_graph_active) {
4189                 struct ftrace_ret_stack *ret_stack;
4190
4191                 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4192                                 * sizeof(struct ftrace_ret_stack),
4193                                 GFP_KERNEL);
4194                 if (!ret_stack)
4195                         return;
4196                 graph_init_task(t, ret_stack);
4197         }
4198 }
4199
4200 void ftrace_graph_exit_task(struct task_struct *t)
4201 {
4202         struct ftrace_ret_stack *ret_stack = t->ret_stack;
4203
4204         t->ret_stack = NULL;
4205         /* NULL must become visible to IRQs before we free it: */
4206         barrier();
4207
4208         kfree(ret_stack);
4209 }
4210
4211 void ftrace_graph_stop(void)
4212 {
4213         ftrace_stop();
4214 }
4215 #endif