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