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