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