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