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