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