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