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