be6888f40d2b146fb2d017b54cafbcdeee5f63c3
[linux-2.6-block.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/module.h>
26 #include <linux/ftrace.h>
27 #include <linux/sysctl.h>
28 #include <linux/slab.h>
29 #include <linux/ctype.h>
30 #include <linux/list.h>
31 #include <linux/hash.h>
32 #include <linux/rcupdate.h>
33
34 #include <trace/events/sched.h>
35
36 #include <asm/setup.h>
37
38 #include "trace_output.h"
39 #include "trace_stat.h"
40
41 #define FTRACE_WARN_ON(cond)                    \
42         ({                                      \
43                 int ___r = cond;                \
44                 if (WARN_ON(___r))              \
45                         ftrace_kill();          \
46                 ___r;                           \
47         })
48
49 #define FTRACE_WARN_ON_ONCE(cond)               \
50         ({                                      \
51                 int ___r = cond;                \
52                 if (WARN_ON_ONCE(___r))         \
53                         ftrace_kill();          \
54                 ___r;                           \
55         })
56
57 /* hash bits for specific function selection */
58 #define FTRACE_HASH_BITS 7
59 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
60 #define FTRACE_HASH_DEFAULT_BITS 10
61 #define FTRACE_HASH_MAX_BITS 12
62
63 /* ftrace_enabled is a method to turn ftrace on or off */
64 int ftrace_enabled __read_mostly;
65 static int last_ftrace_enabled;
66
67 /* Quick disabling of function tracer. */
68 int function_trace_stop;
69
70 /* List for set_ftrace_pid's pids. */
71 LIST_HEAD(ftrace_pids);
72 struct ftrace_pid {
73         struct list_head list;
74         struct pid *pid;
75 };
76
77 /*
78  * ftrace_disabled is set when an anomaly is discovered.
79  * ftrace_disabled is much stronger than ftrace_enabled.
80  */
81 static int ftrace_disabled __read_mostly;
82
83 static DEFINE_MUTEX(ftrace_lock);
84
85 static struct ftrace_ops ftrace_list_end __read_mostly = {
86         .func           = ftrace_stub,
87 };
88
89 static struct ftrace_ops *ftrace_global_list __read_mostly = &ftrace_list_end;
90 static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
91 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
92 static ftrace_func_t __ftrace_trace_function_delay __read_mostly = ftrace_stub;
93 ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
94 ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
95 static struct ftrace_ops global_ops;
96
97 static void
98 ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip);
99
100 /*
101  * Traverse the ftrace_global_list, invoking all entries.  The reason that we
102  * can use rcu_dereference_raw() is that elements removed from this list
103  * are simply leaked, so there is no need to interact with a grace-period
104  * mechanism.  The rcu_dereference_raw() calls are needed to handle
105  * concurrent insertions into the ftrace_global_list.
106  *
107  * Silly Alpha and silly pointer-speculation compiler optimizations!
108  */
109 static void ftrace_global_list_func(unsigned long ip,
110                                     unsigned long parent_ip)
111 {
112         struct ftrace_ops *op;
113
114         if (unlikely(trace_recursion_test(TRACE_GLOBAL_BIT)))
115                 return;
116
117         trace_recursion_set(TRACE_GLOBAL_BIT);
118         op = rcu_dereference_raw(ftrace_global_list); /*see above*/
119         while (op != &ftrace_list_end) {
120                 op->func(ip, parent_ip);
121                 op = rcu_dereference_raw(op->next); /*see above*/
122         };
123         trace_recursion_clear(TRACE_GLOBAL_BIT);
124 }
125
126 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
127 {
128         if (!test_tsk_trace_trace(current))
129                 return;
130
131         ftrace_pid_function(ip, parent_ip);
132 }
133
134 static void set_ftrace_pid_function(ftrace_func_t func)
135 {
136         /* do not set ftrace_pid_function to itself! */
137         if (func != ftrace_pid_func)
138                 ftrace_pid_function = func;
139 }
140
141 /**
142  * clear_ftrace_function - reset the ftrace function
143  *
144  * This NULLs the ftrace function and in essence stops
145  * tracing.  There may be lag
146  */
147 void clear_ftrace_function(void)
148 {
149         ftrace_trace_function = ftrace_stub;
150         __ftrace_trace_function = ftrace_stub;
151         __ftrace_trace_function_delay = ftrace_stub;
152         ftrace_pid_function = ftrace_stub;
153 }
154
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 struct ftrace_func_entry {
951         struct hlist_node hlist;
952         unsigned long ip;
953 };
954
955 struct ftrace_hash {
956         unsigned long           size_bits;
957         struct hlist_head       *buckets;
958         unsigned long           count;
959         struct rcu_head         rcu;
960 };
961
962 /*
963  * We make these constant because no one should touch them,
964  * but they are used as the default "empty hash", to avoid allocating
965  * it all the time. These are in a read only section such that if
966  * anyone does try to modify it, it will cause an exception.
967  */
968 static const struct hlist_head empty_buckets[1];
969 static const struct ftrace_hash empty_hash = {
970         .buckets = (struct hlist_head *)empty_buckets,
971 };
972 #define EMPTY_HASH      ((struct ftrace_hash *)&empty_hash)
973
974 static struct ftrace_ops global_ops = {
975         .func                   = ftrace_stub,
976         .notrace_hash           = EMPTY_HASH,
977         .filter_hash            = EMPTY_HASH,
978 };
979
980 static struct dyn_ftrace *ftrace_new_addrs;
981
982 static DEFINE_MUTEX(ftrace_regex_lock);
983
984 struct ftrace_page {
985         struct ftrace_page      *next;
986         int                     index;
987         struct dyn_ftrace       records[];
988 };
989
990 #define ENTRIES_PER_PAGE \
991   ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
992
993 /* estimate from running different kernels */
994 #define NR_TO_INIT              10000
995
996 static struct ftrace_page       *ftrace_pages_start;
997 static struct ftrace_page       *ftrace_pages;
998
999 static struct ftrace_func_entry *
1000 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1001 {
1002         unsigned long key;
1003         struct ftrace_func_entry *entry;
1004         struct hlist_head *hhd;
1005         struct hlist_node *n;
1006
1007         if (!hash->count)
1008                 return NULL;
1009
1010         if (hash->size_bits > 0)
1011                 key = hash_long(ip, hash->size_bits);
1012         else
1013                 key = 0;
1014
1015         hhd = &hash->buckets[key];
1016
1017         hlist_for_each_entry_rcu(entry, n, hhd, hlist) {
1018                 if (entry->ip == ip)
1019                         return entry;
1020         }
1021         return NULL;
1022 }
1023
1024 static void __add_hash_entry(struct ftrace_hash *hash,
1025                              struct ftrace_func_entry *entry)
1026 {
1027         struct hlist_head *hhd;
1028         unsigned long key;
1029
1030         if (hash->size_bits)
1031                 key = hash_long(entry->ip, hash->size_bits);
1032         else
1033                 key = 0;
1034
1035         hhd = &hash->buckets[key];
1036         hlist_add_head(&entry->hlist, hhd);
1037         hash->count++;
1038 }
1039
1040 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1041 {
1042         struct ftrace_func_entry *entry;
1043
1044         entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1045         if (!entry)
1046                 return -ENOMEM;
1047
1048         entry->ip = ip;
1049         __add_hash_entry(hash, entry);
1050
1051         return 0;
1052 }
1053
1054 static void
1055 free_hash_entry(struct ftrace_hash *hash,
1056                   struct ftrace_func_entry *entry)
1057 {
1058         hlist_del(&entry->hlist);
1059         kfree(entry);
1060         hash->count--;
1061 }
1062
1063 static void
1064 remove_hash_entry(struct ftrace_hash *hash,
1065                   struct ftrace_func_entry *entry)
1066 {
1067         hlist_del(&entry->hlist);
1068         hash->count--;
1069 }
1070
1071 static void ftrace_hash_clear(struct ftrace_hash *hash)
1072 {
1073         struct hlist_head *hhd;
1074         struct hlist_node *tp, *tn;
1075         struct ftrace_func_entry *entry;
1076         int size = 1 << hash->size_bits;
1077         int i;
1078
1079         if (!hash->count)
1080                 return;
1081
1082         for (i = 0; i < size; i++) {
1083                 hhd = &hash->buckets[i];
1084                 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist)
1085                         free_hash_entry(hash, entry);
1086         }
1087         FTRACE_WARN_ON(hash->count);
1088 }
1089
1090 static void free_ftrace_hash(struct ftrace_hash *hash)
1091 {
1092         if (!hash || hash == EMPTY_HASH)
1093                 return;
1094         ftrace_hash_clear(hash);
1095         kfree(hash->buckets);
1096         kfree(hash);
1097 }
1098
1099 static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1100 {
1101         struct ftrace_hash *hash;
1102
1103         hash = container_of(rcu, struct ftrace_hash, rcu);
1104         free_ftrace_hash(hash);
1105 }
1106
1107 static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1108 {
1109         if (!hash || hash == EMPTY_HASH)
1110                 return;
1111         call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1112 }
1113
1114 static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1115 {
1116         struct ftrace_hash *hash;
1117         int size;
1118
1119         hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1120         if (!hash)
1121                 return NULL;
1122
1123         size = 1 << size_bits;
1124         hash->buckets = kzalloc(sizeof(*hash->buckets) * size, GFP_KERNEL);
1125
1126         if (!hash->buckets) {
1127                 kfree(hash);
1128                 return NULL;
1129         }
1130
1131         hash->size_bits = size_bits;
1132
1133         return hash;
1134 }
1135
1136 static struct ftrace_hash *
1137 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1138 {
1139         struct ftrace_func_entry *entry;
1140         struct ftrace_hash *new_hash;
1141         struct hlist_node *tp;
1142         int size;
1143         int ret;
1144         int i;
1145
1146         new_hash = alloc_ftrace_hash(size_bits);
1147         if (!new_hash)
1148                 return NULL;
1149
1150         /* Empty hash? */
1151         if (!hash || !hash->count)
1152                 return new_hash;
1153
1154         size = 1 << hash->size_bits;
1155         for (i = 0; i < size; i++) {
1156                 hlist_for_each_entry(entry, tp, &hash->buckets[i], hlist) {
1157                         ret = add_hash_entry(new_hash, entry->ip);
1158                         if (ret < 0)
1159                                 goto free_hash;
1160                 }
1161         }
1162
1163         FTRACE_WARN_ON(new_hash->count != hash->count);
1164
1165         return new_hash;
1166
1167  free_hash:
1168         free_ftrace_hash(new_hash);
1169         return NULL;
1170 }
1171
1172 static void
1173 ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1174 static void
1175 ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1176
1177 static int
1178 ftrace_hash_move(struct ftrace_ops *ops, int enable,
1179                  struct ftrace_hash **dst, struct ftrace_hash *src)
1180 {
1181         struct ftrace_func_entry *entry;
1182         struct hlist_node *tp, *tn;
1183         struct hlist_head *hhd;
1184         struct ftrace_hash *old_hash;
1185         struct ftrace_hash *new_hash;
1186         unsigned long key;
1187         int size = src->count;
1188         int bits = 0;
1189         int ret;
1190         int i;
1191
1192         /*
1193          * Remove the current set, update the hash and add
1194          * them back.
1195          */
1196         ftrace_hash_rec_disable(ops, enable);
1197
1198         /*
1199          * If the new source is empty, just free dst and assign it
1200          * the empty_hash.
1201          */
1202         if (!src->count) {
1203                 free_ftrace_hash_rcu(*dst);
1204                 rcu_assign_pointer(*dst, EMPTY_HASH);
1205                 /* still need to update the function records */
1206                 ret = 0;
1207                 goto out;
1208         }
1209
1210         /*
1211          * Make the hash size about 1/2 the # found
1212          */
1213         for (size /= 2; size; size >>= 1)
1214                 bits++;
1215
1216         /* Don't allocate too much */
1217         if (bits > FTRACE_HASH_MAX_BITS)
1218                 bits = FTRACE_HASH_MAX_BITS;
1219
1220         ret = -ENOMEM;
1221         new_hash = alloc_ftrace_hash(bits);
1222         if (!new_hash)
1223                 goto out;
1224
1225         size = 1 << src->size_bits;
1226         for (i = 0; i < size; i++) {
1227                 hhd = &src->buckets[i];
1228                 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist) {
1229                         if (bits > 0)
1230                                 key = hash_long(entry->ip, bits);
1231                         else
1232                                 key = 0;
1233                         remove_hash_entry(src, entry);
1234                         __add_hash_entry(new_hash, entry);
1235                 }
1236         }
1237
1238         old_hash = *dst;
1239         rcu_assign_pointer(*dst, new_hash);
1240         free_ftrace_hash_rcu(old_hash);
1241
1242         ret = 0;
1243  out:
1244         /*
1245          * Enable regardless of ret:
1246          *  On success, we enable the new hash.
1247          *  On failure, we re-enable the original hash.
1248          */
1249         ftrace_hash_rec_enable(ops, enable);
1250
1251         return ret;
1252 }
1253
1254 /*
1255  * Test the hashes for this ops to see if we want to call
1256  * the ops->func or not.
1257  *
1258  * It's a match if the ip is in the ops->filter_hash or
1259  * the filter_hash does not exist or is empty,
1260  *  AND
1261  * the ip is not in the ops->notrace_hash.
1262  *
1263  * This needs to be called with preemption disabled as
1264  * the hashes are freed with call_rcu_sched().
1265  */
1266 static int
1267 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
1268 {
1269         struct ftrace_hash *filter_hash;
1270         struct ftrace_hash *notrace_hash;
1271         int ret;
1272
1273         filter_hash = rcu_dereference_raw(ops->filter_hash);
1274         notrace_hash = rcu_dereference_raw(ops->notrace_hash);
1275
1276         if ((!filter_hash || !filter_hash->count ||
1277              ftrace_lookup_ip(filter_hash, ip)) &&
1278             (!notrace_hash || !notrace_hash->count ||
1279              !ftrace_lookup_ip(notrace_hash, ip)))
1280                 ret = 1;
1281         else
1282                 ret = 0;
1283
1284         return ret;
1285 }
1286
1287 /*
1288  * This is a double for. Do not use 'break' to break out of the loop,
1289  * you must use a goto.
1290  */
1291 #define do_for_each_ftrace_rec(pg, rec)                                 \
1292         for (pg = ftrace_pages_start; pg; pg = pg->next) {              \
1293                 int _____i;                                             \
1294                 for (_____i = 0; _____i < pg->index; _____i++) {        \
1295                         rec = &pg->records[_____i];
1296
1297 #define while_for_each_ftrace_rec()             \
1298                 }                               \
1299         }
1300
1301 /**
1302  * ftrace_location - return true if the ip giving is a traced location
1303  * @ip: the instruction pointer to check
1304  *
1305  * Returns 1 if @ip given is a pointer to a ftrace location.
1306  * That is, the instruction that is either a NOP or call to
1307  * the function tracer. It checks the ftrace internal tables to
1308  * determine if the address belongs or not.
1309  */
1310 int ftrace_location(unsigned long ip)
1311 {
1312         struct ftrace_page *pg;
1313         struct dyn_ftrace *rec;
1314
1315         do_for_each_ftrace_rec(pg, rec) {
1316                 if (rec->ip == ip)
1317                         return 1;
1318         } while_for_each_ftrace_rec();
1319
1320         return 0;
1321 }
1322
1323 static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1324                                      int filter_hash,
1325                                      bool inc)
1326 {
1327         struct ftrace_hash *hash;
1328         struct ftrace_hash *other_hash;
1329         struct ftrace_page *pg;
1330         struct dyn_ftrace *rec;
1331         int count = 0;
1332         int all = 0;
1333
1334         /* Only update if the ops has been registered */
1335         if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1336                 return;
1337
1338         /*
1339          * In the filter_hash case:
1340          *   If the count is zero, we update all records.
1341          *   Otherwise we just update the items in the hash.
1342          *
1343          * In the notrace_hash case:
1344          *   We enable the update in the hash.
1345          *   As disabling notrace means enabling the tracing,
1346          *   and enabling notrace means disabling, the inc variable
1347          *   gets inversed.
1348          */
1349         if (filter_hash) {
1350                 hash = ops->filter_hash;
1351                 other_hash = ops->notrace_hash;
1352                 if (!hash || !hash->count)
1353                         all = 1;
1354         } else {
1355                 inc = !inc;
1356                 hash = ops->notrace_hash;
1357                 other_hash = ops->filter_hash;
1358                 /*
1359                  * If the notrace hash has no items,
1360                  * then there's nothing to do.
1361                  */
1362                 if (hash && !hash->count)
1363                         return;
1364         }
1365
1366         do_for_each_ftrace_rec(pg, rec) {
1367                 int in_other_hash = 0;
1368                 int in_hash = 0;
1369                 int match = 0;
1370
1371                 if (all) {
1372                         /*
1373                          * Only the filter_hash affects all records.
1374                          * Update if the record is not in the notrace hash.
1375                          */
1376                         if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1377                                 match = 1;
1378                 } else {
1379                         in_hash = hash && !!ftrace_lookup_ip(hash, rec->ip);
1380                         in_other_hash = other_hash && !!ftrace_lookup_ip(other_hash, rec->ip);
1381
1382                         /*
1383                          *
1384                          */
1385                         if (filter_hash && in_hash && !in_other_hash)
1386                                 match = 1;
1387                         else if (!filter_hash && in_hash &&
1388                                  (in_other_hash || !other_hash->count))
1389                                 match = 1;
1390                 }
1391                 if (!match)
1392                         continue;
1393
1394                 if (inc) {
1395                         rec->flags++;
1396                         if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1397                                 return;
1398                 } else {
1399                         if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1400                                 return;
1401                         rec->flags--;
1402                 }
1403                 count++;
1404                 /* Shortcut, if we handled all records, we are done. */
1405                 if (!all && count == hash->count)
1406                         return;
1407         } while_for_each_ftrace_rec();
1408 }
1409
1410 static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1411                                     int filter_hash)
1412 {
1413         __ftrace_hash_rec_update(ops, filter_hash, 0);
1414 }
1415
1416 static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1417                                    int filter_hash)
1418 {
1419         __ftrace_hash_rec_update(ops, filter_hash, 1);
1420 }
1421
1422 static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
1423 {
1424         if (ftrace_pages->index == ENTRIES_PER_PAGE) {
1425                 if (!ftrace_pages->next) {
1426                         /* allocate another page */
1427                         ftrace_pages->next =
1428                                 (void *)get_zeroed_page(GFP_KERNEL);
1429                         if (!ftrace_pages->next)
1430                                 return NULL;
1431                 }
1432                 ftrace_pages = ftrace_pages->next;
1433         }
1434
1435         return &ftrace_pages->records[ftrace_pages->index++];
1436 }
1437
1438 static struct dyn_ftrace *
1439 ftrace_record_ip(unsigned long ip)
1440 {
1441         struct dyn_ftrace *rec;
1442
1443         if (ftrace_disabled)
1444                 return NULL;
1445
1446         rec = ftrace_alloc_dyn_node(ip);
1447         if (!rec)
1448                 return NULL;
1449
1450         rec->ip = ip;
1451         rec->newlist = ftrace_new_addrs;
1452         ftrace_new_addrs = rec;
1453
1454         return rec;
1455 }
1456
1457 static void print_ip_ins(const char *fmt, unsigned char *p)
1458 {
1459         int i;
1460
1461         printk(KERN_CONT "%s", fmt);
1462
1463         for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1464                 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1465 }
1466
1467 /**
1468  * ftrace_bug - report and shutdown function tracer
1469  * @failed: The failed type (EFAULT, EINVAL, EPERM)
1470  * @ip: The address that failed
1471  *
1472  * The arch code that enables or disables the function tracing
1473  * can call ftrace_bug() when it has detected a problem in
1474  * modifying the code. @failed should be one of either:
1475  * EFAULT - if the problem happens on reading the @ip address
1476  * EINVAL - if what is read at @ip is not what was expected
1477  * EPERM - if the problem happens on writting to the @ip address
1478  */
1479 void ftrace_bug(int failed, unsigned long ip)
1480 {
1481         switch (failed) {
1482         case -EFAULT:
1483                 FTRACE_WARN_ON_ONCE(1);
1484                 pr_info("ftrace faulted on modifying ");
1485                 print_ip_sym(ip);
1486                 break;
1487         case -EINVAL:
1488                 FTRACE_WARN_ON_ONCE(1);
1489                 pr_info("ftrace failed to modify ");
1490                 print_ip_sym(ip);
1491                 print_ip_ins(" actual: ", (unsigned char *)ip);
1492                 printk(KERN_CONT "\n");
1493                 break;
1494         case -EPERM:
1495                 FTRACE_WARN_ON_ONCE(1);
1496                 pr_info("ftrace faulted on writing ");
1497                 print_ip_sym(ip);
1498                 break;
1499         default:
1500                 FTRACE_WARN_ON_ONCE(1);
1501                 pr_info("ftrace faulted on unknown error ");
1502                 print_ip_sym(ip);
1503         }
1504 }
1505
1506
1507 /* Return 1 if the address range is reserved for ftrace */
1508 int ftrace_text_reserved(void *start, void *end)
1509 {
1510         struct dyn_ftrace *rec;
1511         struct ftrace_page *pg;
1512
1513         do_for_each_ftrace_rec(pg, rec) {
1514                 if (rec->ip <= (unsigned long)end &&
1515                     rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
1516                         return 1;
1517         } while_for_each_ftrace_rec();
1518         return 0;
1519 }
1520
1521 static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
1522 {
1523         unsigned long flag = 0UL;
1524
1525         /*
1526          * If we are updating calls:
1527          *
1528          *   If the record has a ref count, then we need to enable it
1529          *   because someone is using it.
1530          *
1531          *   Otherwise we make sure its disabled.
1532          *
1533          * If we are disabling calls, then disable all records that
1534          * are enabled.
1535          */
1536         if (enable && (rec->flags & ~FTRACE_FL_MASK))
1537                 flag = FTRACE_FL_ENABLED;
1538
1539         /* If the state of this record hasn't changed, then do nothing */
1540         if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1541                 return FTRACE_UPDATE_IGNORE;
1542
1543         if (flag) {
1544                 if (update)
1545                         rec->flags |= FTRACE_FL_ENABLED;
1546                 return FTRACE_UPDATE_MAKE_CALL;
1547         }
1548
1549         if (update)
1550                 rec->flags &= ~FTRACE_FL_ENABLED;
1551
1552         return FTRACE_UPDATE_MAKE_NOP;
1553 }
1554
1555 /**
1556  * ftrace_update_record, set a record that now is tracing or not
1557  * @rec: the record to update
1558  * @enable: set to 1 if the record is tracing, zero to force disable
1559  *
1560  * The records that represent all functions that can be traced need
1561  * to be updated when tracing has been enabled.
1562  */
1563 int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1564 {
1565         return ftrace_check_record(rec, enable, 1);
1566 }
1567
1568 /**
1569  * ftrace_test_record, check if the record has been enabled or not
1570  * @rec: the record to test
1571  * @enable: set to 1 to check if enabled, 0 if it is disabled
1572  *
1573  * The arch code may need to test if a record is already set to
1574  * tracing to determine how to modify the function code that it
1575  * represents.
1576  */
1577 int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1578 {
1579         return ftrace_check_record(rec, enable, 0);
1580 }
1581
1582 static int
1583 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1584 {
1585         unsigned long ftrace_addr;
1586         int ret;
1587
1588         ftrace_addr = (unsigned long)FTRACE_ADDR;
1589
1590         ret = ftrace_update_record(rec, enable);
1591
1592         switch (ret) {
1593         case FTRACE_UPDATE_IGNORE:
1594                 return 0;
1595
1596         case FTRACE_UPDATE_MAKE_CALL:
1597                 return ftrace_make_call(rec, ftrace_addr);
1598
1599         case FTRACE_UPDATE_MAKE_NOP:
1600                 return ftrace_make_nop(NULL, rec, ftrace_addr);
1601         }
1602
1603         return -1; /* unknow ftrace bug */
1604 }
1605
1606 static void ftrace_replace_code(int update)
1607 {
1608         struct dyn_ftrace *rec;
1609         struct ftrace_page *pg;
1610         int failed;
1611
1612         if (unlikely(ftrace_disabled))
1613                 return;
1614
1615         do_for_each_ftrace_rec(pg, rec) {
1616                 failed = __ftrace_replace_code(rec, update);
1617                 if (failed) {
1618                         ftrace_bug(failed, rec->ip);
1619                         /* Stop processing */
1620                         return;
1621                 }
1622         } while_for_each_ftrace_rec();
1623 }
1624
1625 struct ftrace_rec_iter {
1626         struct ftrace_page      *pg;
1627         int                     index;
1628 };
1629
1630 /**
1631  * ftrace_rec_iter_start, start up iterating over traced functions
1632  *
1633  * Returns an iterator handle that is used to iterate over all
1634  * the records that represent address locations where functions
1635  * are traced.
1636  *
1637  * May return NULL if no records are available.
1638  */
1639 struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1640 {
1641         /*
1642          * We only use a single iterator.
1643          * Protected by the ftrace_lock mutex.
1644          */
1645         static struct ftrace_rec_iter ftrace_rec_iter;
1646         struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1647
1648         iter->pg = ftrace_pages_start;
1649         iter->index = 0;
1650
1651         /* Could have empty pages */
1652         while (iter->pg && !iter->pg->index)
1653                 iter->pg = iter->pg->next;
1654
1655         if (!iter->pg)
1656                 return NULL;
1657
1658         return iter;
1659 }
1660
1661 /**
1662  * ftrace_rec_iter_next, get the next record to process.
1663  * @iter: The handle to the iterator.
1664  *
1665  * Returns the next iterator after the given iterator @iter.
1666  */
1667 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1668 {
1669         iter->index++;
1670
1671         if (iter->index >= iter->pg->index) {
1672                 iter->pg = iter->pg->next;
1673                 iter->index = 0;
1674
1675                 /* Could have empty pages */
1676                 while (iter->pg && !iter->pg->index)
1677                         iter->pg = iter->pg->next;
1678         }
1679
1680         if (!iter->pg)
1681                 return NULL;
1682
1683         return iter;
1684 }
1685
1686 /**
1687  * ftrace_rec_iter_record, get the record at the iterator location
1688  * @iter: The current iterator location
1689  *
1690  * Returns the record that the current @iter is at.
1691  */
1692 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1693 {
1694         return &iter->pg->records[iter->index];
1695 }
1696
1697 static int
1698 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
1699 {
1700         unsigned long ip;
1701         int ret;
1702
1703         ip = rec->ip;
1704
1705         if (unlikely(ftrace_disabled))
1706                 return 0;
1707
1708         ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
1709         if (ret) {
1710                 ftrace_bug(ret, ip);
1711                 return 0;
1712         }
1713         return 1;
1714 }
1715
1716 /*
1717  * archs can override this function if they must do something
1718  * before the modifying code is performed.
1719  */
1720 int __weak ftrace_arch_code_modify_prepare(void)
1721 {
1722         return 0;
1723 }
1724
1725 /*
1726  * archs can override this function if they must do something
1727  * after the modifying code is performed.
1728  */
1729 int __weak ftrace_arch_code_modify_post_process(void)
1730 {
1731         return 0;
1732 }
1733
1734 static int __ftrace_modify_code(void *data)
1735 {
1736         int *command = data;
1737
1738         if (*command & FTRACE_UPDATE_CALLS)
1739                 ftrace_replace_code(1);
1740         else if (*command & FTRACE_DISABLE_CALLS)
1741                 ftrace_replace_code(0);
1742
1743         if (*command & FTRACE_UPDATE_TRACE_FUNC)
1744                 ftrace_update_ftrace_func(ftrace_trace_function);
1745
1746         if (*command & FTRACE_START_FUNC_RET)
1747                 ftrace_enable_ftrace_graph_caller();
1748         else if (*command & FTRACE_STOP_FUNC_RET)
1749                 ftrace_disable_ftrace_graph_caller();
1750
1751         return 0;
1752 }
1753
1754 /**
1755  * ftrace_run_stop_machine, go back to the stop machine method
1756  * @command: The command to tell ftrace what to do
1757  *
1758  * If an arch needs to fall back to the stop machine method, the
1759  * it can call this function.
1760  */
1761 void ftrace_run_stop_machine(int command)
1762 {
1763         stop_machine(__ftrace_modify_code, &command, NULL);
1764 }
1765
1766 /**
1767  * arch_ftrace_update_code, modify the code to trace or not trace
1768  * @command: The command that needs to be done
1769  *
1770  * Archs can override this function if it does not need to
1771  * run stop_machine() to modify code.
1772  */
1773 void __weak arch_ftrace_update_code(int command)
1774 {
1775         ftrace_run_stop_machine(command);
1776 }
1777
1778 static void ftrace_run_update_code(int command)
1779 {
1780         int ret;
1781
1782         ret = ftrace_arch_code_modify_prepare();
1783         FTRACE_WARN_ON(ret);
1784         if (ret)
1785                 return;
1786         /*
1787          * Do not call function tracer while we update the code.
1788          * We are in stop machine.
1789          */
1790         function_trace_stop++;
1791
1792         /*
1793          * By default we use stop_machine() to modify the code.
1794          * But archs can do what ever they want as long as it
1795          * is safe. The stop_machine() is the safest, but also
1796          * produces the most overhead.
1797          */
1798         arch_ftrace_update_code(command);
1799
1800 #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
1801         /*
1802          * For archs that call ftrace_test_stop_func(), we must
1803          * wait till after we update all the function callers
1804          * before we update the callback. This keeps different
1805          * ops that record different functions from corrupting
1806          * each other.
1807          */
1808         __ftrace_trace_function = __ftrace_trace_function_delay;
1809 #endif
1810         function_trace_stop--;
1811
1812         ret = ftrace_arch_code_modify_post_process();
1813         FTRACE_WARN_ON(ret);
1814 }
1815
1816 static ftrace_func_t saved_ftrace_func;
1817 static int ftrace_start_up;
1818 static int global_start_up;
1819
1820 static void ftrace_startup_enable(int command)
1821 {
1822         if (saved_ftrace_func != ftrace_trace_function) {
1823                 saved_ftrace_func = ftrace_trace_function;
1824                 command |= FTRACE_UPDATE_TRACE_FUNC;
1825         }
1826
1827         if (!command || !ftrace_enabled)
1828                 return;
1829
1830         ftrace_run_update_code(command);
1831 }
1832
1833 static int ftrace_startup(struct ftrace_ops *ops, int command)
1834 {
1835         bool hash_enable = true;
1836
1837         if (unlikely(ftrace_disabled))
1838                 return -ENODEV;
1839
1840         ftrace_start_up++;
1841         command |= FTRACE_UPDATE_CALLS;
1842
1843         /* ops marked global share the filter hashes */
1844         if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
1845                 ops = &global_ops;
1846                 /* Don't update hash if global is already set */
1847                 if (global_start_up)
1848                         hash_enable = false;
1849                 global_start_up++;
1850         }
1851
1852         ops->flags |= FTRACE_OPS_FL_ENABLED;
1853         if (hash_enable)
1854                 ftrace_hash_rec_enable(ops, 1);
1855
1856         ftrace_startup_enable(command);
1857
1858         return 0;
1859 }
1860
1861 static void ftrace_shutdown(struct ftrace_ops *ops, int command)
1862 {
1863         bool hash_disable = true;
1864
1865         if (unlikely(ftrace_disabled))
1866                 return;
1867
1868         ftrace_start_up--;
1869         /*
1870          * Just warn in case of unbalance, no need to kill ftrace, it's not
1871          * critical but the ftrace_call callers may be never nopped again after
1872          * further ftrace uses.
1873          */
1874         WARN_ON_ONCE(ftrace_start_up < 0);
1875
1876         if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
1877                 ops = &global_ops;
1878                 global_start_up--;
1879                 WARN_ON_ONCE(global_start_up < 0);
1880                 /* Don't update hash if global still has users */
1881                 if (global_start_up) {
1882                         WARN_ON_ONCE(!ftrace_start_up);
1883                         hash_disable = false;
1884                 }
1885         }
1886
1887         if (hash_disable)
1888                 ftrace_hash_rec_disable(ops, 1);
1889
1890         if (ops != &global_ops || !global_start_up)
1891                 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
1892
1893         command |= FTRACE_UPDATE_CALLS;
1894
1895         if (saved_ftrace_func != ftrace_trace_function) {
1896                 saved_ftrace_func = ftrace_trace_function;
1897                 command |= FTRACE_UPDATE_TRACE_FUNC;
1898         }
1899
1900         if (!command || !ftrace_enabled)
1901                 return;
1902
1903         ftrace_run_update_code(command);
1904 }
1905
1906 static void ftrace_startup_sysctl(void)
1907 {
1908         if (unlikely(ftrace_disabled))
1909                 return;
1910
1911         /* Force update next time */
1912         saved_ftrace_func = NULL;
1913         /* ftrace_start_up is true if we want ftrace running */
1914         if (ftrace_start_up)
1915                 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
1916 }
1917
1918 static void ftrace_shutdown_sysctl(void)
1919 {
1920         if (unlikely(ftrace_disabled))
1921                 return;
1922
1923         /* ftrace_start_up is true if ftrace is running */
1924         if (ftrace_start_up)
1925                 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
1926 }
1927
1928 static cycle_t          ftrace_update_time;
1929 static unsigned long    ftrace_update_cnt;
1930 unsigned long           ftrace_update_tot_cnt;
1931
1932 static int ops_traces_mod(struct ftrace_ops *ops)
1933 {
1934         struct ftrace_hash *hash;
1935
1936         hash = ops->filter_hash;
1937         return !!(!hash || !hash->count);
1938 }
1939
1940 static int ftrace_update_code(struct module *mod)
1941 {
1942         struct dyn_ftrace *p;
1943         cycle_t start, stop;
1944         unsigned long ref = 0;
1945
1946         /*
1947          * When adding a module, we need to check if tracers are
1948          * currently enabled and if they are set to trace all functions.
1949          * If they are, we need to enable the module functions as well
1950          * as update the reference counts for those function records.
1951          */
1952         if (mod) {
1953                 struct ftrace_ops *ops;
1954
1955                 for (ops = ftrace_ops_list;
1956                      ops != &ftrace_list_end; ops = ops->next) {
1957                         if (ops->flags & FTRACE_OPS_FL_ENABLED &&
1958                             ops_traces_mod(ops))
1959                                 ref++;
1960                 }
1961         }
1962
1963         start = ftrace_now(raw_smp_processor_id());
1964         ftrace_update_cnt = 0;
1965
1966         while (ftrace_new_addrs) {
1967
1968                 /* If something went wrong, bail without enabling anything */
1969                 if (unlikely(ftrace_disabled))
1970                         return -1;
1971
1972                 p = ftrace_new_addrs;
1973                 ftrace_new_addrs = p->newlist;
1974                 p->flags = ref;
1975
1976                 /*
1977                  * Do the initial record conversion from mcount jump
1978                  * to the NOP instructions.
1979                  */
1980                 if (!ftrace_code_disable(mod, p))
1981                         break;
1982
1983                 ftrace_update_cnt++;
1984
1985                 /*
1986                  * If the tracing is enabled, go ahead and enable the record.
1987                  *
1988                  * The reason not to enable the record immediatelly is the
1989                  * inherent check of ftrace_make_nop/ftrace_make_call for
1990                  * correct previous instructions.  Making first the NOP
1991                  * conversion puts the module to the correct state, thus
1992                  * passing the ftrace_make_call check.
1993                  */
1994                 if (ftrace_start_up && ref) {
1995                         int failed = __ftrace_replace_code(p, 1);
1996                         if (failed)
1997                                 ftrace_bug(failed, p->ip);
1998                 }
1999         }
2000
2001         stop = ftrace_now(raw_smp_processor_id());
2002         ftrace_update_time = stop - start;
2003         ftrace_update_tot_cnt += ftrace_update_cnt;
2004
2005         return 0;
2006 }
2007
2008 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2009 {
2010         struct ftrace_page *pg;
2011         int cnt;
2012         int i;
2013
2014         /* allocate a few pages */
2015         ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
2016         if (!ftrace_pages_start)
2017                 return -1;
2018
2019         /*
2020          * Allocate a few more pages.
2021          *
2022          * TODO: have some parser search vmlinux before
2023          *   final linking to find all calls to ftrace.
2024          *   Then we can:
2025          *    a) know how many pages to allocate.
2026          *     and/or
2027          *    b) set up the table then.
2028          *
2029          *  The dynamic code is still necessary for
2030          *  modules.
2031          */
2032
2033         pg = ftrace_pages = ftrace_pages_start;
2034
2035         cnt = num_to_init / ENTRIES_PER_PAGE;
2036         pr_info("ftrace: allocating %ld entries in %d pages\n",
2037                 num_to_init, cnt + 1);
2038
2039         for (i = 0; i < cnt; i++) {
2040                 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
2041
2042                 /* If we fail, we'll try later anyway */
2043                 if (!pg->next)
2044                         break;
2045
2046                 pg = pg->next;
2047         }
2048
2049         return 0;
2050 }
2051
2052 enum {
2053         FTRACE_ITER_FILTER      = (1 << 0),
2054         FTRACE_ITER_NOTRACE     = (1 << 1),
2055         FTRACE_ITER_PRINTALL    = (1 << 2),
2056         FTRACE_ITER_HASH        = (1 << 3),
2057         FTRACE_ITER_ENABLED     = (1 << 4),
2058 };
2059
2060 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2061
2062 struct ftrace_iterator {
2063         loff_t                          pos;
2064         loff_t                          func_pos;
2065         struct ftrace_page              *pg;
2066         struct dyn_ftrace               *func;
2067         struct ftrace_func_probe        *probe;
2068         struct trace_parser             parser;
2069         struct ftrace_hash              *hash;
2070         struct ftrace_ops               *ops;
2071         int                             hidx;
2072         int                             idx;
2073         unsigned                        flags;
2074 };
2075
2076 static void *
2077 t_hash_next(struct seq_file *m, loff_t *pos)
2078 {
2079         struct ftrace_iterator *iter = m->private;
2080         struct hlist_node *hnd = NULL;
2081         struct hlist_head *hhd;
2082
2083         (*pos)++;
2084         iter->pos = *pos;
2085
2086         if (iter->probe)
2087                 hnd = &iter->probe->node;
2088  retry:
2089         if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2090                 return NULL;
2091
2092         hhd = &ftrace_func_hash[iter->hidx];
2093
2094         if (hlist_empty(hhd)) {
2095                 iter->hidx++;
2096                 hnd = NULL;
2097                 goto retry;
2098         }
2099
2100         if (!hnd)
2101                 hnd = hhd->first;
2102         else {
2103                 hnd = hnd->next;
2104                 if (!hnd) {
2105                         iter->hidx++;
2106                         goto retry;
2107                 }
2108         }
2109
2110         if (WARN_ON_ONCE(!hnd))
2111                 return NULL;
2112
2113         iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2114
2115         return iter;
2116 }
2117
2118 static void *t_hash_start(struct seq_file *m, loff_t *pos)
2119 {
2120         struct ftrace_iterator *iter = m->private;
2121         void *p = NULL;
2122         loff_t l;
2123
2124         if (iter->func_pos > *pos)
2125                 return NULL;
2126
2127         iter->hidx = 0;
2128         for (l = 0; l <= (*pos - iter->func_pos); ) {
2129                 p = t_hash_next(m, &l);
2130                 if (!p)
2131                         break;
2132         }
2133         if (!p)
2134                 return NULL;
2135
2136         /* Only set this if we have an item */
2137         iter->flags |= FTRACE_ITER_HASH;
2138
2139         return iter;
2140 }
2141
2142 static int
2143 t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
2144 {
2145         struct ftrace_func_probe *rec;
2146
2147         rec = iter->probe;
2148         if (WARN_ON_ONCE(!rec))
2149                 return -EIO;
2150
2151         if (rec->ops->print)
2152                 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2153
2154         seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
2155
2156         if (rec->data)
2157                 seq_printf(m, ":%p", rec->data);
2158         seq_putc(m, '\n');
2159
2160         return 0;
2161 }
2162
2163 static void *
2164 t_next(struct seq_file *m, void *v, loff_t *pos)
2165 {
2166         struct ftrace_iterator *iter = m->private;
2167         struct ftrace_ops *ops = &global_ops;
2168         struct dyn_ftrace *rec = NULL;
2169
2170         if (unlikely(ftrace_disabled))
2171                 return NULL;
2172
2173         if (iter->flags & FTRACE_ITER_HASH)
2174                 return t_hash_next(m, pos);
2175
2176         (*pos)++;
2177         iter->pos = iter->func_pos = *pos;
2178
2179         if (iter->flags & FTRACE_ITER_PRINTALL)
2180                 return t_hash_start(m, pos);
2181
2182  retry:
2183         if (iter->idx >= iter->pg->index) {
2184                 if (iter->pg->next) {
2185                         iter->pg = iter->pg->next;
2186                         iter->idx = 0;
2187                         goto retry;
2188                 }
2189         } else {
2190                 rec = &iter->pg->records[iter->idx++];
2191                 if (((iter->flags & FTRACE_ITER_FILTER) &&
2192                      !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
2193
2194                     ((iter->flags & FTRACE_ITER_NOTRACE) &&
2195                      !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2196
2197                     ((iter->flags & FTRACE_ITER_ENABLED) &&
2198                      !(rec->flags & ~FTRACE_FL_MASK))) {
2199
2200                         rec = NULL;
2201                         goto retry;
2202                 }
2203         }
2204
2205         if (!rec)
2206                 return t_hash_start(m, pos);
2207
2208         iter->func = rec;
2209
2210         return iter;
2211 }
2212
2213 static void reset_iter_read(struct ftrace_iterator *iter)
2214 {
2215         iter->pos = 0;
2216         iter->func_pos = 0;
2217         iter->flags &= ~(FTRACE_ITER_PRINTALL & FTRACE_ITER_HASH);
2218 }
2219
2220 static void *t_start(struct seq_file *m, loff_t *pos)
2221 {
2222         struct ftrace_iterator *iter = m->private;
2223         struct ftrace_ops *ops = &global_ops;
2224         void *p = NULL;
2225         loff_t l;
2226
2227         mutex_lock(&ftrace_lock);
2228
2229         if (unlikely(ftrace_disabled))
2230                 return NULL;
2231
2232         /*
2233          * If an lseek was done, then reset and start from beginning.
2234          */
2235         if (*pos < iter->pos)
2236                 reset_iter_read(iter);
2237
2238         /*
2239          * For set_ftrace_filter reading, if we have the filter
2240          * off, we can short cut and just print out that all
2241          * functions are enabled.
2242          */
2243         if (iter->flags & FTRACE_ITER_FILTER && !ops->filter_hash->count) {
2244                 if (*pos > 0)
2245                         return t_hash_start(m, pos);
2246                 iter->flags |= FTRACE_ITER_PRINTALL;
2247                 /* reset in case of seek/pread */
2248                 iter->flags &= ~FTRACE_ITER_HASH;
2249                 return iter;
2250         }
2251
2252         if (iter->flags & FTRACE_ITER_HASH)
2253                 return t_hash_start(m, pos);
2254
2255         /*
2256          * Unfortunately, we need to restart at ftrace_pages_start
2257          * every time we let go of the ftrace_mutex. This is because
2258          * those pointers can change without the lock.
2259          */
2260         iter->pg = ftrace_pages_start;
2261         iter->idx = 0;
2262         for (l = 0; l <= *pos; ) {
2263                 p = t_next(m, p, &l);
2264                 if (!p)
2265                         break;
2266         }
2267
2268         if (!p) {
2269                 if (iter->flags & FTRACE_ITER_FILTER)
2270                         return t_hash_start(m, pos);
2271
2272                 return NULL;
2273         }
2274
2275         return iter;
2276 }
2277
2278 static void t_stop(struct seq_file *m, void *p)
2279 {
2280         mutex_unlock(&ftrace_lock);
2281 }
2282
2283 static int t_show(struct seq_file *m, void *v)
2284 {
2285         struct ftrace_iterator *iter = m->private;
2286         struct dyn_ftrace *rec;
2287
2288         if (iter->flags & FTRACE_ITER_HASH)
2289                 return t_hash_show(m, iter);
2290
2291         if (iter->flags & FTRACE_ITER_PRINTALL) {
2292                 seq_printf(m, "#### all functions enabled ####\n");
2293                 return 0;
2294         }
2295
2296         rec = iter->func;
2297
2298         if (!rec)
2299                 return 0;
2300
2301         seq_printf(m, "%ps", (void *)rec->ip);
2302         if (iter->flags & FTRACE_ITER_ENABLED)
2303                 seq_printf(m, " (%ld)",
2304                            rec->flags & ~FTRACE_FL_MASK);
2305         seq_printf(m, "\n");
2306
2307         return 0;
2308 }
2309
2310 static const struct seq_operations show_ftrace_seq_ops = {
2311         .start = t_start,
2312         .next = t_next,
2313         .stop = t_stop,
2314         .show = t_show,
2315 };
2316
2317 static int
2318 ftrace_avail_open(struct inode *inode, struct file *file)
2319 {
2320         struct ftrace_iterator *iter;
2321         int ret;
2322
2323         if (unlikely(ftrace_disabled))
2324                 return -ENODEV;
2325
2326         iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2327         if (!iter)
2328                 return -ENOMEM;
2329
2330         iter->pg = ftrace_pages_start;
2331
2332         ret = seq_open(file, &show_ftrace_seq_ops);
2333         if (!ret) {
2334                 struct seq_file *m = file->private_data;
2335
2336                 m->private = iter;
2337         } else {
2338                 kfree(iter);
2339         }
2340
2341         return ret;
2342 }
2343
2344 static int
2345 ftrace_enabled_open(struct inode *inode, struct file *file)
2346 {
2347         struct ftrace_iterator *iter;
2348         int ret;
2349
2350         if (unlikely(ftrace_disabled))
2351                 return -ENODEV;
2352
2353         iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2354         if (!iter)
2355                 return -ENOMEM;
2356
2357         iter->pg = ftrace_pages_start;
2358         iter->flags = FTRACE_ITER_ENABLED;
2359
2360         ret = seq_open(file, &show_ftrace_seq_ops);
2361         if (!ret) {
2362                 struct seq_file *m = file->private_data;
2363
2364                 m->private = iter;
2365         } else {
2366                 kfree(iter);
2367         }
2368
2369         return ret;
2370 }
2371
2372 static void ftrace_filter_reset(struct ftrace_hash *hash)
2373 {
2374         mutex_lock(&ftrace_lock);
2375         ftrace_hash_clear(hash);
2376         mutex_unlock(&ftrace_lock);
2377 }
2378
2379 static int
2380 ftrace_regex_open(struct ftrace_ops *ops, int flag,
2381                   struct inode *inode, struct file *file)
2382 {
2383         struct ftrace_iterator *iter;
2384         struct ftrace_hash *hash;
2385         int ret = 0;
2386
2387         if (unlikely(ftrace_disabled))
2388                 return -ENODEV;
2389
2390         iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2391         if (!iter)
2392                 return -ENOMEM;
2393
2394         if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2395                 kfree(iter);
2396                 return -ENOMEM;
2397         }
2398
2399         if (flag & FTRACE_ITER_NOTRACE)
2400                 hash = ops->notrace_hash;
2401         else
2402                 hash = ops->filter_hash;
2403
2404         iter->ops = ops;
2405         iter->flags = flag;
2406
2407         if (file->f_mode & FMODE_WRITE) {
2408                 mutex_lock(&ftrace_lock);
2409                 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2410                 mutex_unlock(&ftrace_lock);
2411
2412                 if (!iter->hash) {
2413                         trace_parser_put(&iter->parser);
2414                         kfree(iter);
2415                         return -ENOMEM;
2416                 }
2417         }
2418
2419         mutex_lock(&ftrace_regex_lock);
2420
2421         if ((file->f_mode & FMODE_WRITE) &&
2422             (file->f_flags & O_TRUNC))
2423                 ftrace_filter_reset(iter->hash);
2424
2425         if (file->f_mode & FMODE_READ) {
2426                 iter->pg = ftrace_pages_start;
2427
2428                 ret = seq_open(file, &show_ftrace_seq_ops);
2429                 if (!ret) {
2430                         struct seq_file *m = file->private_data;
2431                         m->private = iter;
2432                 } else {
2433                         /* Failed */
2434                         free_ftrace_hash(iter->hash);
2435                         trace_parser_put(&iter->parser);
2436                         kfree(iter);
2437                 }
2438         } else
2439                 file->private_data = iter;
2440         mutex_unlock(&ftrace_regex_lock);
2441
2442         return ret;
2443 }
2444
2445 static int
2446 ftrace_filter_open(struct inode *inode, struct file *file)
2447 {
2448         return ftrace_regex_open(&global_ops, FTRACE_ITER_FILTER,
2449                                  inode, file);
2450 }
2451
2452 static int
2453 ftrace_notrace_open(struct inode *inode, struct file *file)
2454 {
2455         return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
2456                                  inode, file);
2457 }
2458
2459 static loff_t
2460 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
2461 {
2462         loff_t ret;
2463
2464         if (file->f_mode & FMODE_READ)
2465                 ret = seq_lseek(file, offset, origin);
2466         else
2467                 file->f_pos = ret = 1;
2468
2469         return ret;
2470 }
2471
2472 static int ftrace_match(char *str, char *regex, int len, int type)
2473 {
2474         int matched = 0;
2475         int slen;
2476
2477         switch (type) {
2478         case MATCH_FULL:
2479                 if (strcmp(str, regex) == 0)
2480                         matched = 1;
2481                 break;
2482         case MATCH_FRONT_ONLY:
2483                 if (strncmp(str, regex, len) == 0)
2484                         matched = 1;
2485                 break;
2486         case MATCH_MIDDLE_ONLY:
2487                 if (strstr(str, regex))
2488                         matched = 1;
2489                 break;
2490         case MATCH_END_ONLY:
2491                 slen = strlen(str);
2492                 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
2493                         matched = 1;
2494                 break;
2495         }
2496
2497         return matched;
2498 }
2499
2500 static int
2501 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
2502 {
2503         struct ftrace_func_entry *entry;
2504         int ret = 0;
2505
2506         entry = ftrace_lookup_ip(hash, rec->ip);
2507         if (not) {
2508                 /* Do nothing if it doesn't exist */
2509                 if (!entry)
2510                         return 0;
2511
2512                 free_hash_entry(hash, entry);
2513         } else {
2514                 /* Do nothing if it exists */
2515                 if (entry)
2516                         return 0;
2517
2518                 ret = add_hash_entry(hash, rec->ip);
2519         }
2520         return ret;
2521 }
2522
2523 static int
2524 ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2525                     char *regex, int len, int type)
2526 {
2527         char str[KSYM_SYMBOL_LEN];
2528         char *modname;
2529
2530         kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2531
2532         if (mod) {
2533                 /* module lookup requires matching the module */
2534                 if (!modname || strcmp(modname, mod))
2535                         return 0;
2536
2537                 /* blank search means to match all funcs in the mod */
2538                 if (!len)
2539                         return 1;
2540         }
2541
2542         return ftrace_match(str, regex, len, type);
2543 }
2544
2545 static int
2546 match_records(struct ftrace_hash *hash, char *buff,
2547               int len, char *mod, int not)
2548 {
2549         unsigned search_len = 0;
2550         struct ftrace_page *pg;
2551         struct dyn_ftrace *rec;
2552         int type = MATCH_FULL;
2553         char *search = buff;
2554         int found = 0;
2555         int ret;
2556
2557         if (len) {
2558                 type = filter_parse_regex(buff, len, &search, &not);
2559                 search_len = strlen(search);
2560         }
2561
2562         mutex_lock(&ftrace_lock);
2563
2564         if (unlikely(ftrace_disabled))
2565                 goto out_unlock;
2566
2567         do_for_each_ftrace_rec(pg, rec) {
2568                 if (ftrace_match_record(rec, mod, search, search_len, type)) {
2569                         ret = enter_record(hash, rec, not);
2570                         if (ret < 0) {
2571                                 found = ret;
2572                                 goto out_unlock;
2573                         }
2574                         found = 1;
2575                 }
2576         } while_for_each_ftrace_rec();
2577  out_unlock:
2578         mutex_unlock(&ftrace_lock);
2579
2580         return found;
2581 }
2582
2583 static int
2584 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
2585 {
2586         return match_records(hash, buff, len, NULL, 0);
2587 }
2588
2589 static int
2590 ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
2591 {
2592         int not = 0;
2593
2594         /* blank or '*' mean the same */
2595         if (strcmp(buff, "*") == 0)
2596                 buff[0] = 0;
2597
2598         /* handle the case of 'dont filter this module' */
2599         if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2600                 buff[0] = 0;
2601                 not = 1;
2602         }
2603
2604         return match_records(hash, buff, strlen(buff), mod, not);
2605 }
2606
2607 /*
2608  * We register the module command as a template to show others how
2609  * to register the a command as well.
2610  */
2611
2612 static int
2613 ftrace_mod_callback(struct ftrace_hash *hash,
2614                     char *func, char *cmd, char *param, int enable)
2615 {
2616         char *mod;
2617         int ret = -EINVAL;
2618
2619         /*
2620          * cmd == 'mod' because we only registered this func
2621          * for the 'mod' ftrace_func_command.
2622          * But if you register one func with multiple commands,
2623          * you can tell which command was used by the cmd
2624          * parameter.
2625          */
2626
2627         /* we must have a module name */
2628         if (!param)
2629                 return ret;
2630
2631         mod = strsep(&param, ":");
2632         if (!strlen(mod))
2633                 return ret;
2634
2635         ret = ftrace_match_module_records(hash, func, mod);
2636         if (!ret)
2637                 ret = -EINVAL;
2638         if (ret < 0)
2639                 return ret;
2640
2641         return 0;
2642 }
2643
2644 static struct ftrace_func_command ftrace_mod_cmd = {
2645         .name                   = "mod",
2646         .func                   = ftrace_mod_callback,
2647 };
2648
2649 static int __init ftrace_mod_cmd_init(void)
2650 {
2651         return register_ftrace_command(&ftrace_mod_cmd);
2652 }
2653 device_initcall(ftrace_mod_cmd_init);
2654
2655 static void
2656 function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
2657 {
2658         struct ftrace_func_probe *entry;
2659         struct hlist_head *hhd;
2660         struct hlist_node *n;
2661         unsigned long key;
2662
2663         key = hash_long(ip, FTRACE_HASH_BITS);
2664
2665         hhd = &ftrace_func_hash[key];
2666
2667         if (hlist_empty(hhd))
2668                 return;
2669
2670         /*
2671          * Disable preemption for these calls to prevent a RCU grace
2672          * period. This syncs the hash iteration and freeing of items
2673          * on the hash. rcu_read_lock is too dangerous here.
2674          */
2675         preempt_disable_notrace();
2676         hlist_for_each_entry_rcu(entry, n, hhd, node) {
2677                 if (entry->ip == ip)
2678                         entry->ops->func(ip, parent_ip, &entry->data);
2679         }
2680         preempt_enable_notrace();
2681 }
2682
2683 static struct ftrace_ops trace_probe_ops __read_mostly =
2684 {
2685         .func           = function_trace_probe_call,
2686 };
2687
2688 static int ftrace_probe_registered;
2689
2690 static void __enable_ftrace_function_probe(void)
2691 {
2692         int ret;
2693         int i;
2694
2695         if (ftrace_probe_registered)
2696                 return;
2697
2698         for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2699                 struct hlist_head *hhd = &ftrace_func_hash[i];
2700                 if (hhd->first)
2701                         break;
2702         }
2703         /* Nothing registered? */
2704         if (i == FTRACE_FUNC_HASHSIZE)
2705                 return;
2706
2707         ret = __register_ftrace_function(&trace_probe_ops);
2708         if (!ret)
2709                 ret = ftrace_startup(&trace_probe_ops, 0);
2710
2711         ftrace_probe_registered = 1;
2712 }
2713
2714 static void __disable_ftrace_function_probe(void)
2715 {
2716         int ret;
2717         int i;
2718
2719         if (!ftrace_probe_registered)
2720                 return;
2721
2722         for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2723                 struct hlist_head *hhd = &ftrace_func_hash[i];
2724                 if (hhd->first)
2725                         return;
2726         }
2727
2728         /* no more funcs left */
2729         ret = __unregister_ftrace_function(&trace_probe_ops);
2730         if (!ret)
2731                 ftrace_shutdown(&trace_probe_ops, 0);
2732
2733         ftrace_probe_registered = 0;
2734 }
2735
2736
2737 static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2738 {
2739         struct ftrace_func_probe *entry =
2740                 container_of(rhp, struct ftrace_func_probe, rcu);
2741
2742         if (entry->ops->free)
2743                 entry->ops->free(&entry->data);
2744         kfree(entry);
2745 }
2746
2747
2748 int
2749 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2750                               void *data)
2751 {
2752         struct ftrace_func_probe *entry;
2753         struct ftrace_page *pg;
2754         struct dyn_ftrace *rec;
2755         int type, len, not;
2756         unsigned long key;
2757         int count = 0;
2758         char *search;
2759
2760         type = filter_parse_regex(glob, strlen(glob), &search, &not);
2761         len = strlen(search);
2762
2763         /* we do not support '!' for function probes */
2764         if (WARN_ON(not))
2765                 return -EINVAL;
2766
2767         mutex_lock(&ftrace_lock);
2768
2769         if (unlikely(ftrace_disabled))
2770                 goto out_unlock;
2771
2772         do_for_each_ftrace_rec(pg, rec) {
2773
2774                 if (!ftrace_match_record(rec, NULL, search, len, type))
2775                         continue;
2776
2777                 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2778                 if (!entry) {
2779                         /* If we did not process any, then return error */
2780                         if (!count)
2781                                 count = -ENOMEM;
2782                         goto out_unlock;
2783                 }
2784
2785                 count++;
2786
2787                 entry->data = data;
2788
2789                 /*
2790                  * The caller might want to do something special
2791                  * for each function we find. We call the callback
2792                  * to give the caller an opportunity to do so.
2793                  */
2794                 if (ops->callback) {
2795                         if (ops->callback(rec->ip, &entry->data) < 0) {
2796                                 /* caller does not like this func */
2797                                 kfree(entry);
2798                                 continue;
2799                         }
2800                 }
2801
2802                 entry->ops = ops;
2803                 entry->ip = rec->ip;
2804
2805                 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2806                 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2807
2808         } while_for_each_ftrace_rec();
2809         __enable_ftrace_function_probe();
2810
2811  out_unlock:
2812         mutex_unlock(&ftrace_lock);
2813
2814         return count;
2815 }
2816
2817 enum {
2818         PROBE_TEST_FUNC         = 1,
2819         PROBE_TEST_DATA         = 2
2820 };
2821
2822 static void
2823 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2824                                   void *data, int flags)
2825 {
2826         struct ftrace_func_probe *entry;
2827         struct hlist_node *n, *tmp;
2828         char str[KSYM_SYMBOL_LEN];
2829         int type = MATCH_FULL;
2830         int i, len = 0;
2831         char *search;
2832
2833         if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
2834                 glob = NULL;
2835         else if (glob) {
2836                 int not;
2837
2838                 type = filter_parse_regex(glob, strlen(glob), &search, &not);
2839                 len = strlen(search);
2840
2841                 /* we do not support '!' for function probes */
2842                 if (WARN_ON(not))
2843                         return;
2844         }
2845
2846         mutex_lock(&ftrace_lock);
2847         for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2848                 struct hlist_head *hhd = &ftrace_func_hash[i];
2849
2850                 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2851
2852                         /* break up if statements for readability */
2853                         if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
2854                                 continue;
2855
2856                         if ((flags & PROBE_TEST_DATA) && entry->data != data)
2857                                 continue;
2858
2859                         /* do this last, since it is the most expensive */
2860                         if (glob) {
2861                                 kallsyms_lookup(entry->ip, NULL, NULL,
2862                                                 NULL, str);
2863                                 if (!ftrace_match(str, glob, len, type))
2864                                         continue;
2865                         }
2866
2867                         hlist_del(&entry->node);
2868                         call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2869                 }
2870         }
2871         __disable_ftrace_function_probe();
2872         mutex_unlock(&ftrace_lock);
2873 }
2874
2875 void
2876 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2877                                 void *data)
2878 {
2879         __unregister_ftrace_function_probe(glob, ops, data,
2880                                           PROBE_TEST_FUNC | PROBE_TEST_DATA);
2881 }
2882
2883 void
2884 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
2885 {
2886         __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
2887 }
2888
2889 void unregister_ftrace_function_probe_all(char *glob)
2890 {
2891         __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
2892 }
2893
2894 static LIST_HEAD(ftrace_commands);
2895 static DEFINE_MUTEX(ftrace_cmd_mutex);
2896
2897 int register_ftrace_command(struct ftrace_func_command *cmd)
2898 {
2899         struct ftrace_func_command *p;
2900         int ret = 0;
2901
2902         mutex_lock(&ftrace_cmd_mutex);
2903         list_for_each_entry(p, &ftrace_commands, list) {
2904                 if (strcmp(cmd->name, p->name) == 0) {
2905                         ret = -EBUSY;
2906                         goto out_unlock;
2907                 }
2908         }
2909         list_add(&cmd->list, &ftrace_commands);
2910  out_unlock:
2911         mutex_unlock(&ftrace_cmd_mutex);
2912
2913         return ret;
2914 }
2915
2916 int unregister_ftrace_command(struct ftrace_func_command *cmd)
2917 {
2918         struct ftrace_func_command *p, *n;
2919         int ret = -ENODEV;
2920
2921         mutex_lock(&ftrace_cmd_mutex);
2922         list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2923                 if (strcmp(cmd->name, p->name) == 0) {
2924                         ret = 0;
2925                         list_del_init(&p->list);
2926                         goto out_unlock;
2927                 }
2928         }
2929  out_unlock:
2930         mutex_unlock(&ftrace_cmd_mutex);
2931
2932         return ret;
2933 }
2934
2935 static int ftrace_process_regex(struct ftrace_hash *hash,
2936                                 char *buff, int len, int enable)
2937 {
2938         char *func, *command, *next = buff;
2939         struct ftrace_func_command *p;
2940         int ret = -EINVAL;
2941
2942         func = strsep(&next, ":");
2943
2944         if (!next) {
2945                 ret = ftrace_match_records(hash, func, len);
2946                 if (!ret)
2947                         ret = -EINVAL;
2948                 if (ret < 0)
2949                         return ret;
2950                 return 0;
2951         }
2952
2953         /* command found */
2954
2955         command = strsep(&next, ":");
2956
2957         mutex_lock(&ftrace_cmd_mutex);
2958         list_for_each_entry(p, &ftrace_commands, list) {
2959                 if (strcmp(p->name, command) == 0) {
2960                         ret = p->func(hash, func, command, next, enable);
2961                         goto out_unlock;
2962                 }
2963         }
2964  out_unlock:
2965         mutex_unlock(&ftrace_cmd_mutex);
2966
2967         return ret;
2968 }
2969
2970 static ssize_t
2971 ftrace_regex_write(struct file *file, const char __user *ubuf,
2972                    size_t cnt, loff_t *ppos, int enable)
2973 {
2974         struct ftrace_iterator *iter;
2975         struct trace_parser *parser;
2976         ssize_t ret, read;
2977
2978         if (!cnt)
2979                 return 0;
2980
2981         mutex_lock(&ftrace_regex_lock);
2982
2983         ret = -ENODEV;
2984         if (unlikely(ftrace_disabled))
2985                 goto out_unlock;
2986
2987         if (file->f_mode & FMODE_READ) {
2988                 struct seq_file *m = file->private_data;
2989                 iter = m->private;
2990         } else
2991                 iter = file->private_data;
2992
2993         parser = &iter->parser;
2994         read = trace_get_user(parser, ubuf, cnt, ppos);
2995
2996         if (read >= 0 && trace_parser_loaded(parser) &&
2997             !trace_parser_cont(parser)) {
2998                 ret = ftrace_process_regex(iter->hash, parser->buffer,
2999                                            parser->idx, enable);
3000                 trace_parser_clear(parser);
3001                 if (ret)
3002                         goto out_unlock;
3003         }
3004
3005         ret = read;
3006 out_unlock:
3007         mutex_unlock(&ftrace_regex_lock);
3008
3009         return ret;
3010 }
3011
3012 static ssize_t
3013 ftrace_filter_write(struct file *file, const char __user *ubuf,
3014                     size_t cnt, loff_t *ppos)
3015 {
3016         return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3017 }
3018
3019 static ssize_t
3020 ftrace_notrace_write(struct file *file, const char __user *ubuf,
3021                      size_t cnt, loff_t *ppos)
3022 {
3023         return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3024 }
3025
3026 static int
3027 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3028                  int reset, int enable)
3029 {
3030         struct ftrace_hash **orig_hash;
3031         struct ftrace_hash *hash;
3032         int ret;
3033
3034         /* All global ops uses the global ops filters */
3035         if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3036                 ops = &global_ops;
3037
3038         if (unlikely(ftrace_disabled))
3039                 return -ENODEV;
3040
3041         if (enable)
3042                 orig_hash = &ops->filter_hash;
3043         else
3044                 orig_hash = &ops->notrace_hash;
3045
3046         hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3047         if (!hash)
3048                 return -ENOMEM;
3049
3050         mutex_lock(&ftrace_regex_lock);
3051         if (reset)
3052                 ftrace_filter_reset(hash);
3053         if (buf)
3054                 ftrace_match_records(hash, buf, len);
3055
3056         mutex_lock(&ftrace_lock);
3057         ret = ftrace_hash_move(ops, enable, orig_hash, hash);
3058         if (!ret && ops->flags & FTRACE_OPS_FL_ENABLED
3059             && ftrace_enabled)
3060                 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3061
3062         mutex_unlock(&ftrace_lock);
3063
3064         mutex_unlock(&ftrace_regex_lock);
3065
3066         free_ftrace_hash(hash);
3067         return ret;
3068 }
3069
3070 /**
3071  * ftrace_set_filter - set a function to filter on in ftrace
3072  * @ops - the ops to set the filter with
3073  * @buf - the string that holds the function filter text.
3074  * @len - the length of the string.
3075  * @reset - non zero to reset all filters before applying this filter.
3076  *
3077  * Filters denote which functions should be enabled when tracing is enabled.
3078  * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3079  */
3080 void ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
3081                        int len, int reset)
3082 {
3083         ftrace_set_regex(ops, buf, len, reset, 1);
3084 }
3085 EXPORT_SYMBOL_GPL(ftrace_set_filter);
3086
3087 /**
3088  * ftrace_set_notrace - set a function to not trace in ftrace
3089  * @ops - the ops to set the notrace filter with
3090  * @buf - the string that holds the function notrace text.
3091  * @len - the length of the string.
3092  * @reset - non zero to reset all filters before applying this filter.
3093  *
3094  * Notrace Filters denote which functions should not be enabled when tracing
3095  * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3096  * for tracing.
3097  */
3098 void ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
3099                         int len, int reset)
3100 {
3101         ftrace_set_regex(ops, buf, len, reset, 0);
3102 }
3103 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3104 /**
3105  * ftrace_set_filter - set a function to filter on in ftrace
3106  * @ops - the ops to set the filter with
3107  * @buf - the string that holds the function filter text.
3108  * @len - the length of the string.
3109  * @reset - non zero to reset all filters before applying this filter.
3110  *
3111  * Filters denote which functions should be enabled when tracing is enabled.
3112  * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3113  */
3114 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3115 {
3116         ftrace_set_regex(&global_ops, buf, len, reset, 1);
3117 }
3118 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3119
3120 /**
3121  * ftrace_set_notrace - set a function to not trace in ftrace
3122  * @ops - the ops to set the notrace filter with
3123  * @buf - the string that holds the function notrace text.
3124  * @len - the length of the string.
3125  * @reset - non zero to reset all filters before applying this filter.
3126  *
3127  * Notrace Filters denote which functions should not be enabled when tracing
3128  * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3129  * for tracing.
3130  */
3131 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
3132 {
3133         ftrace_set_regex(&global_ops, buf, len, reset, 0);
3134 }
3135 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
3136
3137 /*
3138  * command line interface to allow users to set filters on boot up.
3139  */
3140 #define FTRACE_FILTER_SIZE              COMMAND_LINE_SIZE
3141 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3142 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3143
3144 static int __init set_ftrace_notrace(char *str)
3145 {
3146         strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
3147         return 1;
3148 }
3149 __setup("ftrace_notrace=", set_ftrace_notrace);
3150
3151 static int __init set_ftrace_filter(char *str)
3152 {
3153         strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
3154         return 1;
3155 }
3156 __setup("ftrace_filter=", set_ftrace_filter);
3157
3158 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3159 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
3160 static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
3161
3162 static int __init set_graph_function(char *str)
3163 {
3164         strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
3165         return 1;
3166 }
3167 __setup("ftrace_graph_filter=", set_graph_function);
3168
3169 static void __init set_ftrace_early_graph(char *buf)
3170 {
3171         int ret;
3172         char *func;
3173
3174         while (buf) {
3175                 func = strsep(&buf, ",");
3176                 /* we allow only one expression at a time */
3177                 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3178                                       func);
3179                 if (ret)
3180                         printk(KERN_DEBUG "ftrace: function %s not "
3181                                           "traceable\n", func);
3182         }
3183 }
3184 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3185
3186 static void __init
3187 set_ftrace_early_filter(struct ftrace_ops *ops, char *buf, int enable)
3188 {
3189         char *func;
3190
3191         while (buf) {
3192                 func = strsep(&buf, ",");
3193                 ftrace_set_regex(ops, func, strlen(func), 0, enable);
3194         }
3195 }
3196
3197 static void __init set_ftrace_early_filters(void)
3198 {
3199         if (ftrace_filter_buf[0])
3200                 set_ftrace_early_filter(&global_ops, ftrace_filter_buf, 1);
3201         if (ftrace_notrace_buf[0])
3202                 set_ftrace_early_filter(&global_ops, ftrace_notrace_buf, 0);
3203 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3204         if (ftrace_graph_buf[0])
3205                 set_ftrace_early_graph(ftrace_graph_buf);
3206 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3207 }
3208
3209 static int
3210 ftrace_regex_release(struct inode *inode, struct file *file)
3211 {
3212         struct seq_file *m = (struct seq_file *)file->private_data;
3213         struct ftrace_iterator *iter;
3214         struct ftrace_hash **orig_hash;
3215         struct trace_parser *parser;
3216         int filter_hash;
3217         int ret;
3218
3219         mutex_lock(&ftrace_regex_lock);
3220         if (file->f_mode & FMODE_READ) {
3221                 iter = m->private;
3222
3223                 seq_release(inode, file);
3224         } else
3225                 iter = file->private_data;
3226
3227         parser = &iter->parser;
3228         if (trace_parser_loaded(parser)) {
3229                 parser->buffer[parser->idx] = 0;
3230                 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
3231         }
3232
3233         trace_parser_put(parser);
3234
3235         if (file->f_mode & FMODE_WRITE) {
3236                 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3237
3238                 if (filter_hash)
3239                         orig_hash = &iter->ops->filter_hash;
3240                 else
3241                         orig_hash = &iter->ops->notrace_hash;
3242
3243                 mutex_lock(&ftrace_lock);
3244                 ret = ftrace_hash_move(iter->ops, filter_hash,
3245                                        orig_hash, iter->hash);
3246                 if (!ret && (iter->ops->flags & FTRACE_OPS_FL_ENABLED)
3247                     && ftrace_enabled)
3248                         ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3249
3250                 mutex_unlock(&ftrace_lock);
3251         }
3252         free_ftrace_hash(iter->hash);
3253         kfree(iter);
3254
3255         mutex_unlock(&ftrace_regex_lock);
3256         return 0;
3257 }
3258
3259 static const struct file_operations ftrace_avail_fops = {
3260         .open = ftrace_avail_open,
3261         .read = seq_read,
3262         .llseek = seq_lseek,
3263         .release = seq_release_private,
3264 };
3265
3266 static const struct file_operations ftrace_enabled_fops = {
3267         .open = ftrace_enabled_open,
3268         .read = seq_read,
3269         .llseek = seq_lseek,
3270         .release = seq_release_private,
3271 };
3272
3273 static const struct file_operations ftrace_filter_fops = {
3274         .open = ftrace_filter_open,
3275         .read = seq_read,
3276         .write = ftrace_filter_write,
3277         .llseek = ftrace_regex_lseek,
3278         .release = ftrace_regex_release,
3279 };
3280
3281 static const struct file_operations ftrace_notrace_fops = {
3282         .open = ftrace_notrace_open,
3283         .read = seq_read,
3284         .write = ftrace_notrace_write,
3285         .llseek = ftrace_regex_lseek,
3286         .release = ftrace_regex_release,
3287 };
3288
3289 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3290
3291 static DEFINE_MUTEX(graph_lock);
3292
3293 int ftrace_graph_count;
3294 int ftrace_graph_filter_enabled;
3295 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3296
3297 static void *
3298 __g_next(struct seq_file *m, loff_t *pos)
3299 {
3300         if (*pos >= ftrace_graph_count)
3301                 return NULL;
3302         return &ftrace_graph_funcs[*pos];
3303 }
3304
3305 static void *
3306 g_next(struct seq_file *m, void *v, loff_t *pos)
3307 {
3308         (*pos)++;
3309         return __g_next(m, pos);
3310 }
3311
3312 static void *g_start(struct seq_file *m, loff_t *pos)
3313 {
3314         mutex_lock(&graph_lock);
3315
3316         /* Nothing, tell g_show to print all functions are enabled */
3317         if (!ftrace_graph_filter_enabled && !*pos)
3318                 return (void *)1;
3319
3320         return __g_next(m, pos);
3321 }
3322
3323 static void g_stop(struct seq_file *m, void *p)
3324 {
3325         mutex_unlock(&graph_lock);
3326 }
3327
3328 static int g_show(struct seq_file *m, void *v)
3329 {
3330         unsigned long *ptr = v;
3331
3332         if (!ptr)
3333                 return 0;
3334
3335         if (ptr == (unsigned long *)1) {
3336                 seq_printf(m, "#### all functions enabled ####\n");
3337                 return 0;
3338         }
3339
3340         seq_printf(m, "%ps\n", (void *)*ptr);
3341
3342         return 0;
3343 }
3344
3345 static const struct seq_operations ftrace_graph_seq_ops = {
3346         .start = g_start,
3347         .next = g_next,
3348         .stop = g_stop,
3349         .show = g_show,
3350 };
3351
3352 static int
3353 ftrace_graph_open(struct inode *inode, struct file *file)
3354 {
3355         int ret = 0;
3356
3357         if (unlikely(ftrace_disabled))
3358                 return -ENODEV;
3359
3360         mutex_lock(&graph_lock);
3361         if ((file->f_mode & FMODE_WRITE) &&
3362             (file->f_flags & O_TRUNC)) {
3363                 ftrace_graph_filter_enabled = 0;
3364                 ftrace_graph_count = 0;
3365                 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3366         }
3367         mutex_unlock(&graph_lock);
3368
3369         if (file->f_mode & FMODE_READ)
3370                 ret = seq_open(file, &ftrace_graph_seq_ops);
3371
3372         return ret;
3373 }
3374
3375 static int
3376 ftrace_graph_release(struct inode *inode, struct file *file)
3377 {
3378         if (file->f_mode & FMODE_READ)
3379                 seq_release(inode, file);
3380         return 0;
3381 }
3382
3383 static int
3384 ftrace_set_func(unsigned long *array, int *idx, char *buffer)
3385 {
3386         struct dyn_ftrace *rec;
3387         struct ftrace_page *pg;
3388         int search_len;
3389         int fail = 1;
3390         int type, not;
3391         char *search;
3392         bool exists;
3393         int i;
3394
3395         /* decode regex */
3396         type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
3397         if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3398                 return -EBUSY;
3399
3400         search_len = strlen(search);
3401
3402         mutex_lock(&ftrace_lock);
3403
3404         if (unlikely(ftrace_disabled)) {
3405                 mutex_unlock(&ftrace_lock);
3406                 return -ENODEV;
3407         }
3408
3409         do_for_each_ftrace_rec(pg, rec) {
3410
3411                 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
3412                         /* if it is in the array */
3413                         exists = false;
3414                         for (i = 0; i < *idx; i++) {
3415                                 if (array[i] == rec->ip) {
3416                                         exists = true;
3417                                         break;
3418                                 }
3419                         }
3420
3421                         if (!not) {
3422                                 fail = 0;
3423                                 if (!exists) {
3424                                         array[(*idx)++] = rec->ip;
3425                                         if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3426                                                 goto out;
3427                                 }
3428                         } else {
3429                                 if (exists) {
3430                                         array[i] = array[--(*idx)];
3431                                         array[*idx] = 0;
3432                                         fail = 0;
3433                                 }
3434                         }
3435                 }
3436         } while_for_each_ftrace_rec();
3437 out:
3438         mutex_unlock(&ftrace_lock);
3439
3440         if (fail)
3441                 return -EINVAL;
3442
3443         ftrace_graph_filter_enabled = 1;
3444         return 0;
3445 }
3446
3447 static ssize_t
3448 ftrace_graph_write(struct file *file, const char __user *ubuf,
3449                    size_t cnt, loff_t *ppos)
3450 {
3451         struct trace_parser parser;
3452         ssize_t read, ret;
3453
3454         if (!cnt)
3455                 return 0;
3456
3457         mutex_lock(&graph_lock);
3458
3459         if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3460                 ret = -ENOMEM;
3461                 goto out_unlock;
3462         }
3463
3464         read = trace_get_user(&parser, ubuf, cnt, ppos);
3465
3466         if (read >= 0 && trace_parser_loaded((&parser))) {
3467                 parser.buffer[parser.idx] = 0;
3468
3469                 /* we allow only one expression at a time */
3470                 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3471                                         parser.buffer);
3472                 if (ret)
3473                         goto out_free;
3474         }
3475
3476         ret = read;
3477
3478 out_free:
3479         trace_parser_put(&parser);
3480 out_unlock:
3481         mutex_unlock(&graph_lock);
3482
3483         return ret;
3484 }
3485
3486 static const struct file_operations ftrace_graph_fops = {
3487         .open           = ftrace_graph_open,
3488         .read           = seq_read,
3489         .write          = ftrace_graph_write,
3490         .release        = ftrace_graph_release,
3491         .llseek         = seq_lseek,
3492 };
3493 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3494
3495 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
3496 {
3497
3498         trace_create_file("available_filter_functions", 0444,
3499                         d_tracer, NULL, &ftrace_avail_fops);
3500
3501         trace_create_file("enabled_functions", 0444,
3502                         d_tracer, NULL, &ftrace_enabled_fops);
3503
3504         trace_create_file("set_ftrace_filter", 0644, d_tracer,
3505                         NULL, &ftrace_filter_fops);
3506
3507         trace_create_file("set_ftrace_notrace", 0644, d_tracer,
3508                                     NULL, &ftrace_notrace_fops);
3509
3510 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3511         trace_create_file("set_graph_function", 0444, d_tracer,
3512                                     NULL,
3513                                     &ftrace_graph_fops);
3514 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3515
3516         return 0;
3517 }
3518
3519 static int ftrace_process_locs(struct module *mod,
3520                                unsigned long *start,
3521                                unsigned long *end)
3522 {
3523         unsigned long *p;
3524         unsigned long addr;
3525         unsigned long flags = 0; /* Shut up gcc */
3526
3527         mutex_lock(&ftrace_lock);
3528         /*
3529          * Core and each module needs their own pages, as
3530          * modules will free them when they are removed.
3531          * Force a new page to be allocated for modules.
3532          */
3533         if (mod) {
3534                 if (!ftrace_pages)
3535                         return -ENOMEM;
3536
3537                 /*
3538                  * If the last page was full, it will be
3539                  * allocated anyway.
3540                  */
3541                 if (ftrace_pages->index != ENTRIES_PER_PAGE) {
3542                         ftrace_pages->next = (void *)get_zeroed_page(GFP_KERNEL);
3543                         if (!ftrace_pages->next)
3544                                 return -ENOMEM;
3545                         ftrace_pages = ftrace_pages->next;
3546                 }
3547         }
3548
3549         p = start;
3550         while (p < end) {
3551                 addr = ftrace_call_adjust(*p++);
3552                 /*
3553                  * Some architecture linkers will pad between
3554                  * the different mcount_loc sections of different
3555                  * object files to satisfy alignments.
3556                  * Skip any NULL pointers.
3557                  */
3558                 if (!addr)
3559                         continue;
3560                 ftrace_record_ip(addr);
3561         }
3562
3563         /*
3564          * We only need to disable interrupts on start up
3565          * because we are modifying code that an interrupt
3566          * may execute, and the modification is not atomic.
3567          * But for modules, nothing runs the code we modify
3568          * until we are finished with it, and there's no
3569          * reason to cause large interrupt latencies while we do it.
3570          */
3571         if (!mod)
3572                 local_irq_save(flags);
3573         ftrace_update_code(mod);
3574         if (!mod)
3575                 local_irq_restore(flags);
3576         mutex_unlock(&ftrace_lock);
3577
3578         return 0;
3579 }
3580
3581 #ifdef CONFIG_MODULES
3582
3583 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
3584
3585 void ftrace_release_mod(struct module *mod)
3586 {
3587         struct dyn_ftrace *rec;
3588         struct ftrace_page **last_pg;
3589         struct ftrace_page *pg;
3590
3591         mutex_lock(&ftrace_lock);
3592
3593         if (ftrace_disabled)
3594                 goto out_unlock;
3595
3596         /*
3597          * Each module has its own ftrace_pages, remove
3598          * them from the list.
3599          */
3600         last_pg = &ftrace_pages_start;
3601         for (pg = ftrace_pages_start; pg; pg = *last_pg) {
3602                 rec = &pg->records[0];
3603                 if (within_module_core(rec->ip, mod)) {
3604                         /*
3605                          * As core pages are first, the first
3606                          * page should never be a module page.
3607                          */
3608                         if (WARN_ON(pg == ftrace_pages_start))
3609                                 goto out_unlock;
3610
3611                         /* Check if we are deleting the last page */
3612                         if (pg == ftrace_pages)
3613                                 ftrace_pages = next_to_ftrace_page(last_pg);
3614
3615                         *last_pg = pg->next;
3616                         free_page((unsigned long)pg);
3617                 } else
3618                         last_pg = &pg->next;
3619         }
3620  out_unlock:
3621         mutex_unlock(&ftrace_lock);
3622 }
3623
3624 static void ftrace_init_module(struct module *mod,
3625                                unsigned long *start, unsigned long *end)
3626 {
3627         if (ftrace_disabled || start == end)
3628                 return;
3629         ftrace_process_locs(mod, start, end);
3630 }
3631
3632 static int ftrace_module_notify(struct notifier_block *self,
3633                                 unsigned long val, void *data)
3634 {
3635         struct module *mod = data;
3636
3637         switch (val) {
3638         case MODULE_STATE_COMING:
3639                 ftrace_init_module(mod, mod->ftrace_callsites,
3640                                    mod->ftrace_callsites +
3641                                    mod->num_ftrace_callsites);
3642                 break;
3643         case MODULE_STATE_GOING:
3644                 ftrace_release_mod(mod);
3645                 break;
3646         }
3647
3648         return 0;
3649 }
3650 #else
3651 static int ftrace_module_notify(struct notifier_block *self,
3652                                 unsigned long val, void *data)
3653 {
3654         return 0;
3655 }
3656 #endif /* CONFIG_MODULES */
3657
3658 struct notifier_block ftrace_module_nb = {
3659         .notifier_call = ftrace_module_notify,
3660         .priority = 0,
3661 };
3662
3663 extern unsigned long __start_mcount_loc[];
3664 extern unsigned long __stop_mcount_loc[];
3665
3666 void __init ftrace_init(void)
3667 {
3668         unsigned long count, addr, flags;
3669         int ret;
3670
3671         /* Keep the ftrace pointer to the stub */
3672         addr = (unsigned long)ftrace_stub;
3673
3674         local_irq_save(flags);
3675         ftrace_dyn_arch_init(&addr);
3676         local_irq_restore(flags);
3677
3678         /* ftrace_dyn_arch_init places the return code in addr */
3679         if (addr)
3680                 goto failed;
3681
3682         count = __stop_mcount_loc - __start_mcount_loc;
3683
3684         ret = ftrace_dyn_table_alloc(count);
3685         if (ret)
3686                 goto failed;
3687
3688         last_ftrace_enabled = ftrace_enabled = 1;
3689
3690         ret = ftrace_process_locs(NULL,
3691                                   __start_mcount_loc,
3692                                   __stop_mcount_loc);
3693
3694         ret = register_module_notifier(&ftrace_module_nb);
3695         if (ret)
3696                 pr_warning("Failed to register trace ftrace module notifier\n");
3697
3698         set_ftrace_early_filters();
3699
3700         return;
3701  failed:
3702         ftrace_disabled = 1;
3703 }
3704
3705 #else
3706
3707 static struct ftrace_ops global_ops = {
3708         .func                   = ftrace_stub,
3709 };
3710
3711 static int __init ftrace_nodyn_init(void)
3712 {
3713         ftrace_enabled = 1;
3714         return 0;
3715 }
3716 device_initcall(ftrace_nodyn_init);
3717
3718 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
3719 static inline void ftrace_startup_enable(int command) { }
3720 /* Keep as macros so we do not need to define the commands */
3721 # define ftrace_startup(ops, command)                   \
3722         ({                                              \
3723                 (ops)->flags |= FTRACE_OPS_FL_ENABLED;  \
3724                 0;                                      \
3725         })
3726 # define ftrace_shutdown(ops, command)  do { } while (0)
3727 # define ftrace_startup_sysctl()        do { } while (0)
3728 # define ftrace_shutdown_sysctl()       do { } while (0)
3729
3730 static inline int
3731 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
3732 {
3733         return 1;
3734 }
3735
3736 #endif /* CONFIG_DYNAMIC_FTRACE */
3737
3738 static void
3739 ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip)
3740 {
3741         struct ftrace_ops *op;
3742
3743         if (unlikely(trace_recursion_test(TRACE_INTERNAL_BIT)))
3744                 return;
3745
3746         trace_recursion_set(TRACE_INTERNAL_BIT);
3747         /*
3748          * Some of the ops may be dynamically allocated,
3749          * they must be freed after a synchronize_sched().
3750          */
3751         preempt_disable_notrace();
3752         op = rcu_dereference_raw(ftrace_ops_list);
3753         while (op != &ftrace_list_end) {
3754                 if (ftrace_ops_test(op, ip))
3755                         op->func(ip, parent_ip);
3756                 op = rcu_dereference_raw(op->next);
3757         };
3758         preempt_enable_notrace();
3759         trace_recursion_clear(TRACE_INTERNAL_BIT);
3760 }
3761
3762 static void clear_ftrace_swapper(void)
3763 {
3764         struct task_struct *p;
3765         int cpu;
3766
3767         get_online_cpus();
3768         for_each_online_cpu(cpu) {
3769                 p = idle_task(cpu);
3770                 clear_tsk_trace_trace(p);
3771         }
3772         put_online_cpus();
3773 }
3774
3775 static void set_ftrace_swapper(void)
3776 {
3777         struct task_struct *p;
3778         int cpu;
3779
3780         get_online_cpus();
3781         for_each_online_cpu(cpu) {
3782                 p = idle_task(cpu);
3783                 set_tsk_trace_trace(p);
3784         }
3785         put_online_cpus();
3786 }
3787
3788 static void clear_ftrace_pid(struct pid *pid)
3789 {
3790         struct task_struct *p;
3791
3792         rcu_read_lock();
3793         do_each_pid_task(pid, PIDTYPE_PID, p) {
3794                 clear_tsk_trace_trace(p);
3795         } while_each_pid_task(pid, PIDTYPE_PID, p);
3796         rcu_read_unlock();
3797
3798         put_pid(pid);
3799 }
3800
3801 static void set_ftrace_pid(struct pid *pid)
3802 {
3803         struct task_struct *p;
3804
3805         rcu_read_lock();
3806         do_each_pid_task(pid, PIDTYPE_PID, p) {
3807                 set_tsk_trace_trace(p);
3808         } while_each_pid_task(pid, PIDTYPE_PID, p);
3809         rcu_read_unlock();
3810 }
3811
3812 static void clear_ftrace_pid_task(struct pid *pid)
3813 {
3814         if (pid == ftrace_swapper_pid)
3815                 clear_ftrace_swapper();
3816         else
3817                 clear_ftrace_pid(pid);
3818 }
3819
3820 static void set_ftrace_pid_task(struct pid *pid)
3821 {
3822         if (pid == ftrace_swapper_pid)
3823                 set_ftrace_swapper();
3824         else
3825                 set_ftrace_pid(pid);
3826 }
3827
3828 static int ftrace_pid_add(int p)
3829 {
3830         struct pid *pid;
3831         struct ftrace_pid *fpid;
3832         int ret = -EINVAL;
3833
3834         mutex_lock(&ftrace_lock);
3835
3836         if (!p)
3837                 pid = ftrace_swapper_pid;
3838         else
3839                 pid = find_get_pid(p);
3840
3841         if (!pid)
3842                 goto out;
3843
3844         ret = 0;
3845
3846         list_for_each_entry(fpid, &ftrace_pids, list)
3847                 if (fpid->pid == pid)
3848                         goto out_put;
3849
3850         ret = -ENOMEM;
3851
3852         fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
3853         if (!fpid)
3854                 goto out_put;
3855
3856         list_add(&fpid->list, &ftrace_pids);
3857         fpid->pid = pid;
3858
3859         set_ftrace_pid_task(pid);
3860
3861         ftrace_update_pid_func();
3862         ftrace_startup_enable(0);
3863
3864         mutex_unlock(&ftrace_lock);
3865         return 0;
3866
3867 out_put:
3868         if (pid != ftrace_swapper_pid)
3869                 put_pid(pid);
3870
3871 out:
3872         mutex_unlock(&ftrace_lock);
3873         return ret;
3874 }
3875
3876 static void ftrace_pid_reset(void)
3877 {
3878         struct ftrace_pid *fpid, *safe;
3879
3880         mutex_lock(&ftrace_lock);
3881         list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
3882                 struct pid *pid = fpid->pid;
3883
3884                 clear_ftrace_pid_task(pid);
3885
3886                 list_del(&fpid->list);
3887                 kfree(fpid);
3888         }
3889
3890         ftrace_update_pid_func();
3891         ftrace_startup_enable(0);
3892
3893         mutex_unlock(&ftrace_lock);
3894 }
3895
3896 static void *fpid_start(struct seq_file *m, loff_t *pos)
3897 {
3898         mutex_lock(&ftrace_lock);
3899
3900         if (list_empty(&ftrace_pids) && (!*pos))
3901                 return (void *) 1;
3902
3903         return seq_list_start(&ftrace_pids, *pos);
3904 }
3905
3906 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
3907 {
3908         if (v == (void *)1)
3909                 return NULL;
3910
3911         return seq_list_next(v, &ftrace_pids, pos);
3912 }
3913
3914 static void fpid_stop(struct seq_file *m, void *p)
3915 {
3916         mutex_unlock(&ftrace_lock);
3917 }
3918
3919 static int fpid_show(struct seq_file *m, void *v)
3920 {
3921         const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
3922
3923         if (v == (void *)1) {
3924                 seq_printf(m, "no pid\n");
3925                 return 0;
3926         }
3927
3928         if (fpid->pid == ftrace_swapper_pid)
3929                 seq_printf(m, "swapper tasks\n");
3930         else
3931                 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
3932
3933         return 0;
3934 }
3935
3936 static const struct seq_operations ftrace_pid_sops = {
3937         .start = fpid_start,
3938         .next = fpid_next,
3939         .stop = fpid_stop,
3940         .show = fpid_show,
3941 };
3942
3943 static int
3944 ftrace_pid_open(struct inode *inode, struct file *file)
3945 {
3946         int ret = 0;
3947
3948         if ((file->f_mode & FMODE_WRITE) &&
3949             (file->f_flags & O_TRUNC))
3950                 ftrace_pid_reset();
3951
3952         if (file->f_mode & FMODE_READ)
3953                 ret = seq_open(file, &ftrace_pid_sops);
3954
3955         return ret;
3956 }
3957
3958 static ssize_t
3959 ftrace_pid_write(struct file *filp, const char __user *ubuf,
3960                    size_t cnt, loff_t *ppos)
3961 {
3962         char buf[64], *tmp;
3963         long val;
3964         int ret;
3965
3966         if (cnt >= sizeof(buf))
3967                 return -EINVAL;
3968
3969         if (copy_from_user(&buf, ubuf, cnt))
3970                 return -EFAULT;
3971
3972         buf[cnt] = 0;
3973
3974         /*
3975          * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
3976          * to clean the filter quietly.
3977          */
3978         tmp = strstrip(buf);
3979         if (strlen(tmp) == 0)
3980                 return 1;
3981
3982         ret = strict_strtol(tmp, 10, &val);
3983         if (ret < 0)
3984                 return ret;
3985
3986         ret = ftrace_pid_add(val);
3987
3988         return ret ? ret : cnt;
3989 }
3990
3991 static int
3992 ftrace_pid_release(struct inode *inode, struct file *file)
3993 {
3994         if (file->f_mode & FMODE_READ)
3995                 seq_release(inode, file);
3996
3997         return 0;
3998 }
3999
4000 static const struct file_operations ftrace_pid_fops = {
4001         .open           = ftrace_pid_open,
4002         .write          = ftrace_pid_write,
4003         .read           = seq_read,
4004         .llseek         = seq_lseek,
4005         .release        = ftrace_pid_release,
4006 };
4007
4008 static __init int ftrace_init_debugfs(void)
4009 {
4010         struct dentry *d_tracer;
4011
4012         d_tracer = tracing_init_dentry();
4013         if (!d_tracer)
4014                 return 0;
4015
4016         ftrace_init_dyn_debugfs(d_tracer);
4017
4018         trace_create_file("set_ftrace_pid", 0644, d_tracer,
4019                             NULL, &ftrace_pid_fops);
4020
4021         ftrace_profile_debugfs(d_tracer);
4022
4023         return 0;
4024 }
4025 fs_initcall(ftrace_init_debugfs);
4026
4027 /**
4028  * ftrace_kill - kill ftrace
4029  *
4030  * This function should be used by panic code. It stops ftrace
4031  * but in a not so nice way. If you need to simply kill ftrace
4032  * from a non-atomic section, use ftrace_kill.
4033  */
4034 void ftrace_kill(void)
4035 {
4036         ftrace_disabled = 1;
4037         ftrace_enabled = 0;
4038         clear_ftrace_function();
4039 }
4040
4041 /**
4042  * Test if ftrace is dead or not.
4043  */
4044 int ftrace_is_dead(void)
4045 {
4046         return ftrace_disabled;
4047 }
4048
4049 /**
4050  * register_ftrace_function - register a function for profiling
4051  * @ops - ops structure that holds the function for profiling.
4052  *
4053  * Register a function to be called by all functions in the
4054  * kernel.
4055  *
4056  * Note: @ops->func and all the functions it calls must be labeled
4057  *       with "notrace", otherwise it will go into a
4058  *       recursive loop.
4059  */
4060 int register_ftrace_function(struct ftrace_ops *ops)
4061 {
4062         int ret = -1;
4063
4064         mutex_lock(&ftrace_lock);
4065
4066         if (unlikely(ftrace_disabled))
4067                 goto out_unlock;
4068
4069         ret = __register_ftrace_function(ops);
4070         if (!ret)
4071                 ret = ftrace_startup(ops, 0);
4072
4073
4074  out_unlock:
4075         mutex_unlock(&ftrace_lock);
4076         return ret;
4077 }
4078 EXPORT_SYMBOL_GPL(register_ftrace_function);
4079
4080 /**
4081  * unregister_ftrace_function - unregister a function for profiling.
4082  * @ops - ops structure that holds the function to unregister
4083  *
4084  * Unregister a function that was added to be called by ftrace profiling.
4085  */
4086 int unregister_ftrace_function(struct ftrace_ops *ops)
4087 {
4088         int ret;
4089
4090         mutex_lock(&ftrace_lock);
4091         ret = __unregister_ftrace_function(ops);
4092         if (!ret)
4093                 ftrace_shutdown(ops, 0);
4094         mutex_unlock(&ftrace_lock);
4095
4096         return ret;
4097 }
4098 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
4099
4100 int
4101 ftrace_enable_sysctl(struct ctl_table *table, int write,
4102                      void __user *buffer, size_t *lenp,
4103                      loff_t *ppos)
4104 {
4105         int ret = -ENODEV;
4106
4107         mutex_lock(&ftrace_lock);
4108
4109         if (unlikely(ftrace_disabled))
4110                 goto out;
4111
4112         ret = proc_dointvec(table, write, buffer, lenp, ppos);
4113
4114         if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
4115                 goto out;
4116
4117         last_ftrace_enabled = !!ftrace_enabled;
4118
4119         if (ftrace_enabled) {
4120
4121                 ftrace_startup_sysctl();
4122
4123                 /* we are starting ftrace again */
4124                 if (ftrace_ops_list != &ftrace_list_end) {
4125                         if (ftrace_ops_list->next == &ftrace_list_end)
4126                                 ftrace_trace_function = ftrace_ops_list->func;
4127                         else
4128                                 ftrace_trace_function = ftrace_ops_list_func;
4129                 }
4130
4131         } else {
4132                 /* stopping ftrace calls (just send to ftrace_stub) */
4133                 ftrace_trace_function = ftrace_stub;
4134
4135                 ftrace_shutdown_sysctl();
4136         }
4137
4138  out:
4139         mutex_unlock(&ftrace_lock);
4140         return ret;
4141 }
4142
4143 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4144
4145 static int ftrace_graph_active;
4146 static struct notifier_block ftrace_suspend_notifier;
4147
4148 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4149 {
4150         return 0;
4151 }
4152
4153 /* The callbacks that hook a function */
4154 trace_func_graph_ret_t ftrace_graph_return =
4155                         (trace_func_graph_ret_t)ftrace_stub;
4156 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
4157
4158 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4159 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4160 {
4161         int i;
4162         int ret = 0;
4163         unsigned long flags;
4164         int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4165         struct task_struct *g, *t;
4166
4167         for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4168                 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4169                                         * sizeof(struct ftrace_ret_stack),
4170                                         GFP_KERNEL);
4171                 if (!ret_stack_list[i]) {
4172                         start = 0;
4173                         end = i;
4174                         ret = -ENOMEM;
4175                         goto free;
4176                 }
4177         }
4178
4179         read_lock_irqsave(&tasklist_lock, flags);
4180         do_each_thread(g, t) {
4181                 if (start == end) {
4182                         ret = -EAGAIN;
4183                         goto unlock;
4184                 }
4185
4186                 if (t->ret_stack == NULL) {
4187                         atomic_set(&t->tracing_graph_pause, 0);
4188                         atomic_set(&t->trace_overrun, 0);
4189                         t->curr_ret_stack = -1;
4190                         /* Make sure the tasks see the -1 first: */
4191                         smp_wmb();
4192                         t->ret_stack = ret_stack_list[start++];
4193                 }
4194         } while_each_thread(g, t);
4195
4196 unlock:
4197         read_unlock_irqrestore(&tasklist_lock, flags);
4198 free:
4199         for (i = start; i < end; i++)
4200                 kfree(ret_stack_list[i]);
4201         return ret;
4202 }
4203
4204 static void
4205 ftrace_graph_probe_sched_switch(void *ignore,
4206                         struct task_struct *prev, struct task_struct *next)
4207 {
4208         unsigned long long timestamp;
4209         int index;
4210
4211         /*
4212          * Does the user want to count the time a function was asleep.
4213          * If so, do not update the time stamps.
4214          */
4215         if (trace_flags & TRACE_ITER_SLEEP_TIME)
4216                 return;
4217
4218         timestamp = trace_clock_local();
4219
4220         prev->ftrace_timestamp = timestamp;
4221
4222         /* only process tasks that we timestamped */
4223         if (!next->ftrace_timestamp)
4224                 return;
4225
4226         /*
4227          * Update all the counters in next to make up for the
4228          * time next was sleeping.
4229          */
4230         timestamp -= next->ftrace_timestamp;
4231
4232         for (index = next->curr_ret_stack; index >= 0; index--)
4233                 next->ret_stack[index].calltime += timestamp;
4234 }
4235
4236 /* Allocate a return stack for each task */
4237 static int start_graph_tracing(void)
4238 {
4239         struct ftrace_ret_stack **ret_stack_list;
4240         int ret, cpu;
4241
4242         ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4243                                 sizeof(struct ftrace_ret_stack *),
4244                                 GFP_KERNEL);
4245
4246         if (!ret_stack_list)
4247                 return -ENOMEM;
4248
4249         /* The cpu_boot init_task->ret_stack will never be freed */
4250         for_each_online_cpu(cpu) {
4251                 if (!idle_task(cpu)->ret_stack)
4252                         ftrace_graph_init_idle_task(idle_task(cpu), cpu);
4253         }
4254
4255         do {
4256                 ret = alloc_retstack_tasklist(ret_stack_list);
4257         } while (ret == -EAGAIN);
4258
4259         if (!ret) {
4260                 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
4261                 if (ret)
4262                         pr_info("ftrace_graph: Couldn't activate tracepoint"
4263                                 " probe to kernel_sched_switch\n");
4264         }
4265
4266         kfree(ret_stack_list);
4267         return ret;
4268 }
4269
4270 /*
4271  * Hibernation protection.
4272  * The state of the current task is too much unstable during
4273  * suspend/restore to disk. We want to protect against that.
4274  */
4275 static int
4276 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4277                                                         void *unused)
4278 {
4279         switch (state) {
4280         case PM_HIBERNATION_PREPARE:
4281                 pause_graph_tracing();
4282                 break;
4283
4284         case PM_POST_HIBERNATION:
4285                 unpause_graph_tracing();
4286                 break;
4287         }
4288         return NOTIFY_DONE;
4289 }
4290
4291 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4292                         trace_func_graph_ent_t entryfunc)
4293 {
4294         int ret = 0;
4295
4296         mutex_lock(&ftrace_lock);
4297
4298         /* we currently allow only one tracer registered at a time */
4299         if (ftrace_graph_active) {
4300                 ret = -EBUSY;
4301                 goto out;
4302         }
4303
4304         ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
4305         register_pm_notifier(&ftrace_suspend_notifier);
4306
4307         ftrace_graph_active++;
4308         ret = start_graph_tracing();
4309         if (ret) {
4310                 ftrace_graph_active--;
4311                 goto out;
4312         }
4313
4314         ftrace_graph_return = retfunc;
4315         ftrace_graph_entry = entryfunc;
4316
4317         ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
4318
4319 out:
4320         mutex_unlock(&ftrace_lock);
4321         return ret;
4322 }
4323
4324 void unregister_ftrace_graph(void)
4325 {
4326         mutex_lock(&ftrace_lock);
4327
4328         if (unlikely(!ftrace_graph_active))
4329                 goto out;
4330
4331         ftrace_graph_active--;
4332         ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
4333         ftrace_graph_entry = ftrace_graph_entry_stub;
4334         ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
4335         unregister_pm_notifier(&ftrace_suspend_notifier);
4336         unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
4337
4338  out:
4339         mutex_unlock(&ftrace_lock);
4340 }
4341
4342 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
4343
4344 static void
4345 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
4346 {
4347         atomic_set(&t->tracing_graph_pause, 0);
4348         atomic_set(&t->trace_overrun, 0);
4349         t->ftrace_timestamp = 0;
4350         /* make curr_ret_stack visible before we add the ret_stack */
4351         smp_wmb();
4352         t->ret_stack = ret_stack;
4353 }
4354
4355 /*
4356  * Allocate a return stack for the idle task. May be the first
4357  * time through, or it may be done by CPU hotplug online.
4358  */
4359 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
4360 {
4361         t->curr_ret_stack = -1;
4362         /*
4363          * The idle task has no parent, it either has its own
4364          * stack or no stack at all.
4365          */
4366         if (t->ret_stack)
4367                 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
4368
4369         if (ftrace_graph_active) {
4370                 struct ftrace_ret_stack *ret_stack;
4371
4372                 ret_stack = per_cpu(idle_ret_stack, cpu);
4373                 if (!ret_stack) {
4374                         ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4375                                             * sizeof(struct ftrace_ret_stack),
4376                                             GFP_KERNEL);
4377                         if (!ret_stack)
4378                                 return;
4379                         per_cpu(idle_ret_stack, cpu) = ret_stack;
4380                 }
4381                 graph_init_task(t, ret_stack);
4382         }
4383 }
4384
4385 /* Allocate a return stack for newly created task */
4386 void ftrace_graph_init_task(struct task_struct *t)
4387 {
4388         /* Make sure we do not use the parent ret_stack */
4389         t->ret_stack = NULL;
4390         t->curr_ret_stack = -1;
4391
4392         if (ftrace_graph_active) {
4393                 struct ftrace_ret_stack *ret_stack;
4394
4395                 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4396                                 * sizeof(struct ftrace_ret_stack),
4397                                 GFP_KERNEL);
4398                 if (!ret_stack)
4399                         return;
4400                 graph_init_task(t, ret_stack);
4401         }
4402 }
4403
4404 void ftrace_graph_exit_task(struct task_struct *t)
4405 {
4406         struct ftrace_ret_stack *ret_stack = t->ret_stack;
4407
4408         t->ret_stack = NULL;
4409         /* NULL must become visible to IRQs before we free it: */
4410         barrier();
4411
4412         kfree(ret_stack);
4413 }
4414
4415 void ftrace_graph_stop(void)
4416 {
4417         ftrace_stop();
4418 }
4419 #endif