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