98fa931b686433741c9860757f35ce06f2df5c06
[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 static int
1759 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1760 {
1761         unsigned long ftrace_old_addr;
1762         unsigned long ftrace_addr;
1763         int ret;
1764
1765         ret = ftrace_update_record(rec, enable);
1766
1767         if (rec->flags & FTRACE_FL_REGS)
1768                 ftrace_addr = (unsigned long)FTRACE_REGS_ADDR;
1769         else
1770                 ftrace_addr = (unsigned long)FTRACE_ADDR;
1771
1772         switch (ret) {
1773         case FTRACE_UPDATE_IGNORE:
1774                 return 0;
1775
1776         case FTRACE_UPDATE_MAKE_CALL:
1777                 return ftrace_make_call(rec, ftrace_addr);
1778
1779         case FTRACE_UPDATE_MAKE_NOP:
1780                 return ftrace_make_nop(NULL, rec, ftrace_addr);
1781
1782         case FTRACE_UPDATE_MODIFY_CALL_REGS:
1783         case FTRACE_UPDATE_MODIFY_CALL:
1784                 if (rec->flags & FTRACE_FL_REGS)
1785                         ftrace_old_addr = (unsigned long)FTRACE_ADDR;
1786                 else
1787                         ftrace_old_addr = (unsigned long)FTRACE_REGS_ADDR;
1788
1789                 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
1790         }
1791
1792         return -1; /* unknow ftrace bug */
1793 }
1794
1795 void __weak ftrace_replace_code(int enable)
1796 {
1797         struct dyn_ftrace *rec;
1798         struct ftrace_page *pg;
1799         int failed;
1800
1801         if (unlikely(ftrace_disabled))
1802                 return;
1803
1804         do_for_each_ftrace_rec(pg, rec) {
1805                 failed = __ftrace_replace_code(rec, enable);
1806                 if (failed) {
1807                         ftrace_bug(failed, rec->ip);
1808                         /* Stop processing */
1809                         return;
1810                 }
1811         } while_for_each_ftrace_rec();
1812 }
1813
1814 struct ftrace_rec_iter {
1815         struct ftrace_page      *pg;
1816         int                     index;
1817 };
1818
1819 /**
1820  * ftrace_rec_iter_start, start up iterating over traced functions
1821  *
1822  * Returns an iterator handle that is used to iterate over all
1823  * the records that represent address locations where functions
1824  * are traced.
1825  *
1826  * May return NULL if no records are available.
1827  */
1828 struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1829 {
1830         /*
1831          * We only use a single iterator.
1832          * Protected by the ftrace_lock mutex.
1833          */
1834         static struct ftrace_rec_iter ftrace_rec_iter;
1835         struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1836
1837         iter->pg = ftrace_pages_start;
1838         iter->index = 0;
1839
1840         /* Could have empty pages */
1841         while (iter->pg && !iter->pg->index)
1842                 iter->pg = iter->pg->next;
1843
1844         if (!iter->pg)
1845                 return NULL;
1846
1847         return iter;
1848 }
1849
1850 /**
1851  * ftrace_rec_iter_next, get the next record to process.
1852  * @iter: The handle to the iterator.
1853  *
1854  * Returns the next iterator after the given iterator @iter.
1855  */
1856 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1857 {
1858         iter->index++;
1859
1860         if (iter->index >= iter->pg->index) {
1861                 iter->pg = iter->pg->next;
1862                 iter->index = 0;
1863
1864                 /* Could have empty pages */
1865                 while (iter->pg && !iter->pg->index)
1866                         iter->pg = iter->pg->next;
1867         }
1868
1869         if (!iter->pg)
1870                 return NULL;
1871
1872         return iter;
1873 }
1874
1875 /**
1876  * ftrace_rec_iter_record, get the record at the iterator location
1877  * @iter: The current iterator location
1878  *
1879  * Returns the record that the current @iter is at.
1880  */
1881 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1882 {
1883         return &iter->pg->records[iter->index];
1884 }
1885
1886 static int
1887 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
1888 {
1889         unsigned long ip;
1890         int ret;
1891
1892         ip = rec->ip;
1893
1894         if (unlikely(ftrace_disabled))
1895                 return 0;
1896
1897         ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
1898         if (ret) {
1899                 ftrace_bug(ret, ip);
1900                 return 0;
1901         }
1902         return 1;
1903 }
1904
1905 /*
1906  * archs can override this function if they must do something
1907  * before the modifying code is performed.
1908  */
1909 int __weak ftrace_arch_code_modify_prepare(void)
1910 {
1911         return 0;
1912 }
1913
1914 /*
1915  * archs can override this function if they must do something
1916  * after the modifying code is performed.
1917  */
1918 int __weak ftrace_arch_code_modify_post_process(void)
1919 {
1920         return 0;
1921 }
1922
1923 void ftrace_modify_all_code(int command)
1924 {
1925         int update = command & FTRACE_UPDATE_TRACE_FUNC;
1926         int err = 0;
1927
1928         /*
1929          * If the ftrace_caller calls a ftrace_ops func directly,
1930          * we need to make sure that it only traces functions it
1931          * expects to trace. When doing the switch of functions,
1932          * we need to update to the ftrace_ops_list_func first
1933          * before the transition between old and new calls are set,
1934          * as the ftrace_ops_list_func will check the ops hashes
1935          * to make sure the ops are having the right functions
1936          * traced.
1937          */
1938         if (update) {
1939                 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
1940                 if (FTRACE_WARN_ON(err))
1941                         return;
1942         }
1943
1944         if (command & FTRACE_UPDATE_CALLS)
1945                 ftrace_replace_code(1);
1946         else if (command & FTRACE_DISABLE_CALLS)
1947                 ftrace_replace_code(0);
1948
1949         if (update && ftrace_trace_function != ftrace_ops_list_func) {
1950                 function_trace_op = set_function_trace_op;
1951                 smp_wmb();
1952                 /* If irqs are disabled, we are in stop machine */
1953                 if (!irqs_disabled())
1954                         smp_call_function(ftrace_sync_ipi, NULL, 1);
1955                 err = ftrace_update_ftrace_func(ftrace_trace_function);
1956                 if (FTRACE_WARN_ON(err))
1957                         return;
1958         }
1959
1960         if (command & FTRACE_START_FUNC_RET)
1961                 err = ftrace_enable_ftrace_graph_caller();
1962         else if (command & FTRACE_STOP_FUNC_RET)
1963                 err = ftrace_disable_ftrace_graph_caller();
1964         FTRACE_WARN_ON(err);
1965 }
1966
1967 static int __ftrace_modify_code(void *data)
1968 {
1969         int *command = data;
1970
1971         ftrace_modify_all_code(*command);
1972
1973         return 0;
1974 }
1975
1976 /**
1977  * ftrace_run_stop_machine, go back to the stop machine method
1978  * @command: The command to tell ftrace what to do
1979  *
1980  * If an arch needs to fall back to the stop machine method, the
1981  * it can call this function.
1982  */
1983 void ftrace_run_stop_machine(int command)
1984 {
1985         stop_machine(__ftrace_modify_code, &command, NULL);
1986 }
1987
1988 /**
1989  * arch_ftrace_update_code, modify the code to trace or not trace
1990  * @command: The command that needs to be done
1991  *
1992  * Archs can override this function if it does not need to
1993  * run stop_machine() to modify code.
1994  */
1995 void __weak arch_ftrace_update_code(int command)
1996 {
1997         ftrace_run_stop_machine(command);
1998 }
1999
2000 static void ftrace_run_update_code(int command)
2001 {
2002         int ret;
2003
2004         ret = ftrace_arch_code_modify_prepare();
2005         FTRACE_WARN_ON(ret);
2006         if (ret)
2007                 return;
2008         /*
2009          * Do not call function tracer while we update the code.
2010          * We are in stop machine.
2011          */
2012         function_trace_stop++;
2013
2014         /*
2015          * By default we use stop_machine() to modify the code.
2016          * But archs can do what ever they want as long as it
2017          * is safe. The stop_machine() is the safest, but also
2018          * produces the most overhead.
2019          */
2020         arch_ftrace_update_code(command);
2021
2022         function_trace_stop--;
2023
2024         ret = ftrace_arch_code_modify_post_process();
2025         FTRACE_WARN_ON(ret);
2026 }
2027
2028 static ftrace_func_t saved_ftrace_func;
2029 static int ftrace_start_up;
2030 static int global_start_up;
2031
2032 static void control_ops_free(struct ftrace_ops *ops)
2033 {
2034         free_percpu(ops->disabled);
2035 }
2036
2037 static void ftrace_startup_enable(int command)
2038 {
2039         if (saved_ftrace_func != ftrace_trace_function) {
2040                 saved_ftrace_func = ftrace_trace_function;
2041                 command |= FTRACE_UPDATE_TRACE_FUNC;
2042         }
2043
2044         if (!command || !ftrace_enabled)
2045                 return;
2046
2047         ftrace_run_update_code(command);
2048 }
2049
2050 static int ftrace_startup(struct ftrace_ops *ops, int command)
2051 {
2052         int ret;
2053
2054         if (unlikely(ftrace_disabled))
2055                 return -ENODEV;
2056
2057         ret = __register_ftrace_function(ops);
2058         if (ret)
2059                 return ret;
2060
2061         ftrace_start_up++;
2062         command |= FTRACE_UPDATE_CALLS;
2063
2064         ops->flags |= FTRACE_OPS_FL_ENABLED;
2065
2066         ftrace_hash_rec_enable(ops, 1);
2067
2068         ftrace_startup_enable(command);
2069
2070         return 0;
2071 }
2072
2073 static int ftrace_shutdown(struct ftrace_ops *ops, int command)
2074 {
2075         int ret;
2076
2077         if (unlikely(ftrace_disabled))
2078                 return -ENODEV;
2079
2080         ret = __unregister_ftrace_function(ops);
2081         if (ret)
2082                 return ret;
2083
2084         ftrace_start_up--;
2085         /*
2086          * Just warn in case of unbalance, no need to kill ftrace, it's not
2087          * critical but the ftrace_call callers may be never nopped again after
2088          * further ftrace uses.
2089          */
2090         WARN_ON_ONCE(ftrace_start_up < 0);
2091
2092         ftrace_hash_rec_disable(ops, 1);
2093
2094         if (!global_start_up)
2095                 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2096
2097         command |= FTRACE_UPDATE_CALLS;
2098
2099         if (saved_ftrace_func != ftrace_trace_function) {
2100                 saved_ftrace_func = ftrace_trace_function;
2101                 command |= FTRACE_UPDATE_TRACE_FUNC;
2102         }
2103
2104         if (!command || !ftrace_enabled) {
2105                 /*
2106                  * If these are control ops, they still need their
2107                  * per_cpu field freed. Since, function tracing is
2108                  * not currently active, we can just free them
2109                  * without synchronizing all CPUs.
2110                  */
2111                 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2112                         control_ops_free(ops);
2113                 return 0;
2114         }
2115
2116         ftrace_run_update_code(command);
2117
2118         /*
2119          * Dynamic ops may be freed, we must make sure that all
2120          * callers are done before leaving this function.
2121          * The same goes for freeing the per_cpu data of the control
2122          * ops.
2123          *
2124          * Again, normal synchronize_sched() is not good enough.
2125          * We need to do a hard force of sched synchronization.
2126          * This is because we use preempt_disable() to do RCU, but
2127          * the function tracers can be called where RCU is not watching
2128          * (like before user_exit()). We can not rely on the RCU
2129          * infrastructure to do the synchronization, thus we must do it
2130          * ourselves.
2131          */
2132         if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_CONTROL)) {
2133                 schedule_on_each_cpu(ftrace_sync);
2134
2135                 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2136                         control_ops_free(ops);
2137         }
2138
2139         return 0;
2140 }
2141
2142 static void ftrace_startup_sysctl(void)
2143 {
2144         if (unlikely(ftrace_disabled))
2145                 return;
2146
2147         /* Force update next time */
2148         saved_ftrace_func = NULL;
2149         /* ftrace_start_up is true if we want ftrace running */
2150         if (ftrace_start_up)
2151                 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
2152 }
2153
2154 static void ftrace_shutdown_sysctl(void)
2155 {
2156         if (unlikely(ftrace_disabled))
2157                 return;
2158
2159         /* ftrace_start_up is true if ftrace is running */
2160         if (ftrace_start_up)
2161                 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
2162 }
2163
2164 static cycle_t          ftrace_update_time;
2165 unsigned long           ftrace_update_tot_cnt;
2166
2167 static inline int ops_traces_mod(struct ftrace_ops *ops)
2168 {
2169         /*
2170          * Filter_hash being empty will default to trace module.
2171          * But notrace hash requires a test of individual module functions.
2172          */
2173         return ftrace_hash_empty(ops->filter_hash) &&
2174                 ftrace_hash_empty(ops->notrace_hash);
2175 }
2176
2177 /*
2178  * Check if the current ops references the record.
2179  *
2180  * If the ops traces all functions, then it was already accounted for.
2181  * If the ops does not trace the current record function, skip it.
2182  * If the ops ignores the function via notrace filter, skip it.
2183  */
2184 static inline bool
2185 ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2186 {
2187         /* If ops isn't enabled, ignore it */
2188         if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2189                 return 0;
2190
2191         /* If ops traces all mods, we already accounted for it */
2192         if (ops_traces_mod(ops))
2193                 return 0;
2194
2195         /* The function must be in the filter */
2196         if (!ftrace_hash_empty(ops->filter_hash) &&
2197             !ftrace_lookup_ip(ops->filter_hash, rec->ip))
2198                 return 0;
2199
2200         /* If in notrace hash, we ignore it too */
2201         if (ftrace_lookup_ip(ops->notrace_hash, rec->ip))
2202                 return 0;
2203
2204         return 1;
2205 }
2206
2207 static int referenced_filters(struct dyn_ftrace *rec)
2208 {
2209         struct ftrace_ops *ops;
2210         int cnt = 0;
2211
2212         for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
2213                 if (ops_references_rec(ops, rec))
2214                     cnt++;
2215         }
2216
2217         return cnt;
2218 }
2219
2220 static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
2221 {
2222         struct ftrace_page *pg;
2223         struct dyn_ftrace *p;
2224         cycle_t start, stop;
2225         unsigned long update_cnt = 0;
2226         unsigned long ref = 0;
2227         bool test = false;
2228         int i;
2229
2230         /*
2231          * When adding a module, we need to check if tracers are
2232          * currently enabled and if they are set to trace all functions.
2233          * If they are, we need to enable the module functions as well
2234          * as update the reference counts for those function records.
2235          */
2236         if (mod) {
2237                 struct ftrace_ops *ops;
2238
2239                 for (ops = ftrace_ops_list;
2240                      ops != &ftrace_list_end; ops = ops->next) {
2241                         if (ops->flags & FTRACE_OPS_FL_ENABLED) {
2242                                 if (ops_traces_mod(ops))
2243                                         ref++;
2244                                 else
2245                                         test = true;
2246                         }
2247                 }
2248         }
2249
2250         start = ftrace_now(raw_smp_processor_id());
2251
2252         for (pg = new_pgs; pg; pg = pg->next) {
2253
2254                 for (i = 0; i < pg->index; i++) {
2255                         int cnt = ref;
2256
2257                         /* If something went wrong, bail without enabling anything */
2258                         if (unlikely(ftrace_disabled))
2259                                 return -1;
2260
2261                         p = &pg->records[i];
2262                         if (test)
2263                                 cnt += referenced_filters(p);
2264                         p->flags = cnt;
2265
2266                         /*
2267                          * Do the initial record conversion from mcount jump
2268                          * to the NOP instructions.
2269                          */
2270                         if (!ftrace_code_disable(mod, p))
2271                                 break;
2272
2273                         update_cnt++;
2274
2275                         /*
2276                          * If the tracing is enabled, go ahead and enable the record.
2277                          *
2278                          * The reason not to enable the record immediatelly is the
2279                          * inherent check of ftrace_make_nop/ftrace_make_call for
2280                          * correct previous instructions.  Making first the NOP
2281                          * conversion puts the module to the correct state, thus
2282                          * passing the ftrace_make_call check.
2283                          */
2284                         if (ftrace_start_up && cnt) {
2285                                 int failed = __ftrace_replace_code(p, 1);
2286                                 if (failed)
2287                                         ftrace_bug(failed, p->ip);
2288                         }
2289                 }
2290         }
2291
2292         stop = ftrace_now(raw_smp_processor_id());
2293         ftrace_update_time = stop - start;
2294         ftrace_update_tot_cnt += update_cnt;
2295
2296         return 0;
2297 }
2298
2299 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
2300 {
2301         int order;
2302         int cnt;
2303
2304         if (WARN_ON(!count))
2305                 return -EINVAL;
2306
2307         order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
2308
2309         /*
2310          * We want to fill as much as possible. No more than a page
2311          * may be empty.
2312          */
2313         while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2314                 order--;
2315
2316  again:
2317         pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2318
2319         if (!pg->records) {
2320                 /* if we can't allocate this size, try something smaller */
2321                 if (!order)
2322                         return -ENOMEM;
2323                 order >>= 1;
2324                 goto again;
2325         }
2326
2327         cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2328         pg->size = cnt;
2329
2330         if (cnt > count)
2331                 cnt = count;
2332
2333         return cnt;
2334 }
2335
2336 static struct ftrace_page *
2337 ftrace_allocate_pages(unsigned long num_to_init)
2338 {
2339         struct ftrace_page *start_pg;
2340         struct ftrace_page *pg;
2341         int order;
2342         int cnt;
2343
2344         if (!num_to_init)
2345                 return 0;
2346
2347         start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2348         if (!pg)
2349                 return NULL;
2350
2351         /*
2352          * Try to allocate as much as possible in one continues
2353          * location that fills in all of the space. We want to
2354          * waste as little space as possible.
2355          */
2356         for (;;) {
2357                 cnt = ftrace_allocate_records(pg, num_to_init);
2358                 if (cnt < 0)
2359                         goto free_pages;
2360
2361                 num_to_init -= cnt;
2362                 if (!num_to_init)
2363                         break;
2364
2365                 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2366                 if (!pg->next)
2367                         goto free_pages;
2368
2369                 pg = pg->next;
2370         }
2371
2372         return start_pg;
2373
2374  free_pages:
2375         while (start_pg) {
2376                 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2377                 free_pages((unsigned long)pg->records, order);
2378                 start_pg = pg->next;
2379                 kfree(pg);
2380                 pg = start_pg;
2381         }
2382         pr_info("ftrace: FAILED to allocate memory for functions\n");
2383         return NULL;
2384 }
2385
2386 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2387
2388 struct ftrace_iterator {
2389         loff_t                          pos;
2390         loff_t                          func_pos;
2391         struct ftrace_page              *pg;
2392         struct dyn_ftrace               *func;
2393         struct ftrace_func_probe        *probe;
2394         struct trace_parser             parser;
2395         struct ftrace_hash              *hash;
2396         struct ftrace_ops               *ops;
2397         int                             hidx;
2398         int                             idx;
2399         unsigned                        flags;
2400 };
2401
2402 static void *
2403 t_hash_next(struct seq_file *m, loff_t *pos)
2404 {
2405         struct ftrace_iterator *iter = m->private;
2406         struct hlist_node *hnd = NULL;
2407         struct hlist_head *hhd;
2408
2409         (*pos)++;
2410         iter->pos = *pos;
2411
2412         if (iter->probe)
2413                 hnd = &iter->probe->node;
2414  retry:
2415         if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2416                 return NULL;
2417
2418         hhd = &ftrace_func_hash[iter->hidx];
2419
2420         if (hlist_empty(hhd)) {
2421                 iter->hidx++;
2422                 hnd = NULL;
2423                 goto retry;
2424         }
2425
2426         if (!hnd)
2427                 hnd = hhd->first;
2428         else {
2429                 hnd = hnd->next;
2430                 if (!hnd) {
2431                         iter->hidx++;
2432                         goto retry;
2433                 }
2434         }
2435
2436         if (WARN_ON_ONCE(!hnd))
2437                 return NULL;
2438
2439         iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2440
2441         return iter;
2442 }
2443
2444 static void *t_hash_start(struct seq_file *m, loff_t *pos)
2445 {
2446         struct ftrace_iterator *iter = m->private;
2447         void *p = NULL;
2448         loff_t l;
2449
2450         if (!(iter->flags & FTRACE_ITER_DO_HASH))
2451                 return NULL;
2452
2453         if (iter->func_pos > *pos)
2454                 return NULL;
2455
2456         iter->hidx = 0;
2457         for (l = 0; l <= (*pos - iter->func_pos); ) {
2458                 p = t_hash_next(m, &l);
2459                 if (!p)
2460                         break;
2461         }
2462         if (!p)
2463                 return NULL;
2464
2465         /* Only set this if we have an item */
2466         iter->flags |= FTRACE_ITER_HASH;
2467
2468         return iter;
2469 }
2470
2471 static int
2472 t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
2473 {
2474         struct ftrace_func_probe *rec;
2475
2476         rec = iter->probe;
2477         if (WARN_ON_ONCE(!rec))
2478                 return -EIO;
2479
2480         if (rec->ops->print)
2481                 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2482
2483         seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
2484
2485         if (rec->data)
2486                 seq_printf(m, ":%p", rec->data);
2487         seq_putc(m, '\n');
2488
2489         return 0;
2490 }
2491
2492 static void *
2493 t_next(struct seq_file *m, void *v, loff_t *pos)
2494 {
2495         struct ftrace_iterator *iter = m->private;
2496         struct ftrace_ops *ops = iter->ops;
2497         struct dyn_ftrace *rec = NULL;
2498
2499         if (unlikely(ftrace_disabled))
2500                 return NULL;
2501
2502         if (iter->flags & FTRACE_ITER_HASH)
2503                 return t_hash_next(m, pos);
2504
2505         (*pos)++;
2506         iter->pos = iter->func_pos = *pos;
2507
2508         if (iter->flags & FTRACE_ITER_PRINTALL)
2509                 return t_hash_start(m, pos);
2510
2511  retry:
2512         if (iter->idx >= iter->pg->index) {
2513                 if (iter->pg->next) {
2514                         iter->pg = iter->pg->next;
2515                         iter->idx = 0;
2516                         goto retry;
2517                 }
2518         } else {
2519                 rec = &iter->pg->records[iter->idx++];
2520                 if (((iter->flags & FTRACE_ITER_FILTER) &&
2521                      !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
2522
2523                     ((iter->flags & FTRACE_ITER_NOTRACE) &&
2524                      !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2525
2526                     ((iter->flags & FTRACE_ITER_ENABLED) &&
2527                      !(rec->flags & FTRACE_FL_ENABLED))) {
2528
2529                         rec = NULL;
2530                         goto retry;
2531                 }
2532         }
2533
2534         if (!rec)
2535                 return t_hash_start(m, pos);
2536
2537         iter->func = rec;
2538
2539         return iter;
2540 }
2541
2542 static void reset_iter_read(struct ftrace_iterator *iter)
2543 {
2544         iter->pos = 0;
2545         iter->func_pos = 0;
2546         iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
2547 }
2548
2549 static void *t_start(struct seq_file *m, loff_t *pos)
2550 {
2551         struct ftrace_iterator *iter = m->private;
2552         struct ftrace_ops *ops = iter->ops;
2553         void *p = NULL;
2554         loff_t l;
2555
2556         mutex_lock(&ftrace_lock);
2557
2558         if (unlikely(ftrace_disabled))
2559                 return NULL;
2560
2561         /*
2562          * If an lseek was done, then reset and start from beginning.
2563          */
2564         if (*pos < iter->pos)
2565                 reset_iter_read(iter);
2566
2567         /*
2568          * For set_ftrace_filter reading, if we have the filter
2569          * off, we can short cut and just print out that all
2570          * functions are enabled.
2571          */
2572         if (iter->flags & FTRACE_ITER_FILTER &&
2573             ftrace_hash_empty(ops->filter_hash)) {
2574                 if (*pos > 0)
2575                         return t_hash_start(m, pos);
2576                 iter->flags |= FTRACE_ITER_PRINTALL;
2577                 /* reset in case of seek/pread */
2578                 iter->flags &= ~FTRACE_ITER_HASH;
2579                 return iter;
2580         }
2581
2582         if (iter->flags & FTRACE_ITER_HASH)
2583                 return t_hash_start(m, pos);
2584
2585         /*
2586          * Unfortunately, we need to restart at ftrace_pages_start
2587          * every time we let go of the ftrace_mutex. This is because
2588          * those pointers can change without the lock.
2589          */
2590         iter->pg = ftrace_pages_start;
2591         iter->idx = 0;
2592         for (l = 0; l <= *pos; ) {
2593                 p = t_next(m, p, &l);
2594                 if (!p)
2595                         break;
2596         }
2597
2598         if (!p)
2599                 return t_hash_start(m, pos);
2600
2601         return iter;
2602 }
2603
2604 static void t_stop(struct seq_file *m, void *p)
2605 {
2606         mutex_unlock(&ftrace_lock);
2607 }
2608
2609 static int t_show(struct seq_file *m, void *v)
2610 {
2611         struct ftrace_iterator *iter = m->private;
2612         struct dyn_ftrace *rec;
2613
2614         if (iter->flags & FTRACE_ITER_HASH)
2615                 return t_hash_show(m, iter);
2616
2617         if (iter->flags & FTRACE_ITER_PRINTALL) {
2618                 seq_printf(m, "#### all functions enabled ####\n");
2619                 return 0;
2620         }
2621
2622         rec = iter->func;
2623
2624         if (!rec)
2625                 return 0;
2626
2627         seq_printf(m, "%ps", (void *)rec->ip);
2628         if (iter->flags & FTRACE_ITER_ENABLED)
2629                 seq_printf(m, " (%ld)%s",
2630                            rec->flags & ~FTRACE_FL_MASK,
2631                            rec->flags & FTRACE_FL_REGS ? " R" : "");
2632         seq_printf(m, "\n");
2633
2634         return 0;
2635 }
2636
2637 static const struct seq_operations show_ftrace_seq_ops = {
2638         .start = t_start,
2639         .next = t_next,
2640         .stop = t_stop,
2641         .show = t_show,
2642 };
2643
2644 static int
2645 ftrace_avail_open(struct inode *inode, struct file *file)
2646 {
2647         struct ftrace_iterator *iter;
2648
2649         if (unlikely(ftrace_disabled))
2650                 return -ENODEV;
2651
2652         iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2653         if (iter) {
2654                 iter->pg = ftrace_pages_start;
2655                 iter->ops = &global_ops;
2656         }
2657
2658         return iter ? 0 : -ENOMEM;
2659 }
2660
2661 static int
2662 ftrace_enabled_open(struct inode *inode, struct file *file)
2663 {
2664         struct ftrace_iterator *iter;
2665
2666         if (unlikely(ftrace_disabled))
2667                 return -ENODEV;
2668
2669         iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2670         if (iter) {
2671                 iter->pg = ftrace_pages_start;
2672                 iter->flags = FTRACE_ITER_ENABLED;
2673                 iter->ops = &global_ops;
2674         }
2675
2676         return iter ? 0 : -ENOMEM;
2677 }
2678
2679 static void ftrace_filter_reset(struct ftrace_hash *hash)
2680 {
2681         mutex_lock(&ftrace_lock);
2682         ftrace_hash_clear(hash);
2683         mutex_unlock(&ftrace_lock);
2684 }
2685
2686 /**
2687  * ftrace_regex_open - initialize function tracer filter files
2688  * @ops: The ftrace_ops that hold the hash filters
2689  * @flag: The type of filter to process
2690  * @inode: The inode, usually passed in to your open routine
2691  * @file: The file, usually passed in to your open routine
2692  *
2693  * ftrace_regex_open() initializes the filter files for the
2694  * @ops. Depending on @flag it may process the filter hash or
2695  * the notrace hash of @ops. With this called from the open
2696  * routine, you can use ftrace_filter_write() for the write
2697  * routine if @flag has FTRACE_ITER_FILTER set, or
2698  * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
2699  * tracing_lseek() should be used as the lseek routine, and
2700  * release must call ftrace_regex_release().
2701  */
2702 int
2703 ftrace_regex_open(struct ftrace_ops *ops, int flag,
2704                   struct inode *inode, struct file *file)
2705 {
2706         struct ftrace_iterator *iter;
2707         struct ftrace_hash *hash;
2708         int ret = 0;
2709
2710         ftrace_ops_init(ops);
2711
2712         if (unlikely(ftrace_disabled))
2713                 return -ENODEV;
2714
2715         iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2716         if (!iter)
2717                 return -ENOMEM;
2718
2719         if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2720                 kfree(iter);
2721                 return -ENOMEM;
2722         }
2723
2724         iter->ops = ops;
2725         iter->flags = flag;
2726
2727         mutex_lock(&ops->regex_lock);
2728
2729         if (flag & FTRACE_ITER_NOTRACE)
2730                 hash = ops->notrace_hash;
2731         else
2732                 hash = ops->filter_hash;
2733
2734         if (file->f_mode & FMODE_WRITE) {
2735                 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2736                 if (!iter->hash) {
2737                         trace_parser_put(&iter->parser);
2738                         kfree(iter);
2739                         ret = -ENOMEM;
2740                         goto out_unlock;
2741                 }
2742         }
2743
2744         if ((file->f_mode & FMODE_WRITE) &&
2745             (file->f_flags & O_TRUNC))
2746                 ftrace_filter_reset(iter->hash);
2747
2748         if (file->f_mode & FMODE_READ) {
2749                 iter->pg = ftrace_pages_start;
2750
2751                 ret = seq_open(file, &show_ftrace_seq_ops);
2752                 if (!ret) {
2753                         struct seq_file *m = file->private_data;
2754                         m->private = iter;
2755                 } else {
2756                         /* Failed */
2757                         free_ftrace_hash(iter->hash);
2758                         trace_parser_put(&iter->parser);
2759                         kfree(iter);
2760                 }
2761         } else
2762                 file->private_data = iter;
2763
2764  out_unlock:
2765         mutex_unlock(&ops->regex_lock);
2766
2767         return ret;
2768 }
2769
2770 static int
2771 ftrace_filter_open(struct inode *inode, struct file *file)
2772 {
2773         struct ftrace_ops *ops = inode->i_private;
2774
2775         return ftrace_regex_open(ops,
2776                         FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2777                         inode, file);
2778 }
2779
2780 static int
2781 ftrace_notrace_open(struct inode *inode, struct file *file)
2782 {
2783         struct ftrace_ops *ops = inode->i_private;
2784
2785         return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
2786                                  inode, file);
2787 }
2788
2789 static int ftrace_match(char *str, char *regex, int len, int type)
2790 {
2791         int matched = 0;
2792         int slen;
2793
2794         switch (type) {
2795         case MATCH_FULL:
2796                 if (strcmp(str, regex) == 0)
2797                         matched = 1;
2798                 break;
2799         case MATCH_FRONT_ONLY:
2800                 if (strncmp(str, regex, len) == 0)
2801                         matched = 1;
2802                 break;
2803         case MATCH_MIDDLE_ONLY:
2804                 if (strstr(str, regex))
2805                         matched = 1;
2806                 break;
2807         case MATCH_END_ONLY:
2808                 slen = strlen(str);
2809                 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
2810                         matched = 1;
2811                 break;
2812         }
2813
2814         return matched;
2815 }
2816
2817 static int
2818 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
2819 {
2820         struct ftrace_func_entry *entry;
2821         int ret = 0;
2822
2823         entry = ftrace_lookup_ip(hash, rec->ip);
2824         if (not) {
2825                 /* Do nothing if it doesn't exist */
2826                 if (!entry)
2827                         return 0;
2828
2829                 free_hash_entry(hash, entry);
2830         } else {
2831                 /* Do nothing if it exists */
2832                 if (entry)
2833                         return 0;
2834
2835                 ret = add_hash_entry(hash, rec->ip);
2836         }
2837         return ret;
2838 }
2839
2840 static int
2841 ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2842                     char *regex, int len, int type)
2843 {
2844         char str[KSYM_SYMBOL_LEN];
2845         char *modname;
2846
2847         kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2848
2849         if (mod) {
2850                 /* module lookup requires matching the module */
2851                 if (!modname || strcmp(modname, mod))
2852                         return 0;
2853
2854                 /* blank search means to match all funcs in the mod */
2855                 if (!len)
2856                         return 1;
2857         }
2858
2859         return ftrace_match(str, regex, len, type);
2860 }
2861
2862 static int
2863 match_records(struct ftrace_hash *hash, char *buff,
2864               int len, char *mod, int not)
2865 {
2866         unsigned search_len = 0;
2867         struct ftrace_page *pg;
2868         struct dyn_ftrace *rec;
2869         int type = MATCH_FULL;
2870         char *search = buff;
2871         int found = 0;
2872         int ret;
2873
2874         if (len) {
2875                 type = filter_parse_regex(buff, len, &search, &not);
2876                 search_len = strlen(search);
2877         }
2878
2879         mutex_lock(&ftrace_lock);
2880
2881         if (unlikely(ftrace_disabled))
2882                 goto out_unlock;
2883
2884         do_for_each_ftrace_rec(pg, rec) {
2885                 if (ftrace_match_record(rec, mod, search, search_len, type)) {
2886                         ret = enter_record(hash, rec, not);
2887                         if (ret < 0) {
2888                                 found = ret;
2889                                 goto out_unlock;
2890                         }
2891                         found = 1;
2892                 }
2893         } while_for_each_ftrace_rec();
2894  out_unlock:
2895         mutex_unlock(&ftrace_lock);
2896
2897         return found;
2898 }
2899
2900 static int
2901 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
2902 {
2903         return match_records(hash, buff, len, NULL, 0);
2904 }
2905
2906 static int
2907 ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
2908 {
2909         int not = 0;
2910
2911         /* blank or '*' mean the same */
2912         if (strcmp(buff, "*") == 0)
2913                 buff[0] = 0;
2914
2915         /* handle the case of 'dont filter this module' */
2916         if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2917                 buff[0] = 0;
2918                 not = 1;
2919         }
2920
2921         return match_records(hash, buff, strlen(buff), mod, not);
2922 }
2923
2924 /*
2925  * We register the module command as a template to show others how
2926  * to register the a command as well.
2927  */
2928
2929 static int
2930 ftrace_mod_callback(struct ftrace_hash *hash,
2931                     char *func, char *cmd, char *param, int enable)
2932 {
2933         char *mod;
2934         int ret = -EINVAL;
2935
2936         /*
2937          * cmd == 'mod' because we only registered this func
2938          * for the 'mod' ftrace_func_command.
2939          * But if you register one func with multiple commands,
2940          * you can tell which command was used by the cmd
2941          * parameter.
2942          */
2943
2944         /* we must have a module name */
2945         if (!param)
2946                 return ret;
2947
2948         mod = strsep(&param, ":");
2949         if (!strlen(mod))
2950                 return ret;
2951
2952         ret = ftrace_match_module_records(hash, func, mod);
2953         if (!ret)
2954                 ret = -EINVAL;
2955         if (ret < 0)
2956                 return ret;
2957
2958         return 0;
2959 }
2960
2961 static struct ftrace_func_command ftrace_mod_cmd = {
2962         .name                   = "mod",
2963         .func                   = ftrace_mod_callback,
2964 };
2965
2966 static int __init ftrace_mod_cmd_init(void)
2967 {
2968         return register_ftrace_command(&ftrace_mod_cmd);
2969 }
2970 core_initcall(ftrace_mod_cmd_init);
2971
2972 static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
2973                                       struct ftrace_ops *op, struct pt_regs *pt_regs)
2974 {
2975         struct ftrace_func_probe *entry;
2976         struct hlist_head *hhd;
2977         unsigned long key;
2978
2979         key = hash_long(ip, FTRACE_HASH_BITS);
2980
2981         hhd = &ftrace_func_hash[key];
2982
2983         if (hlist_empty(hhd))
2984                 return;
2985
2986         /*
2987          * Disable preemption for these calls to prevent a RCU grace
2988          * period. This syncs the hash iteration and freeing of items
2989          * on the hash. rcu_read_lock is too dangerous here.
2990          */
2991         preempt_disable_notrace();
2992         hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
2993                 if (entry->ip == ip)
2994                         entry->ops->func(ip, parent_ip, &entry->data);
2995         }
2996         preempt_enable_notrace();
2997 }
2998
2999 static struct ftrace_ops trace_probe_ops __read_mostly =
3000 {
3001         .func           = function_trace_probe_call,
3002         .flags          = FTRACE_OPS_FL_INITIALIZED,
3003         INIT_REGEX_LOCK(trace_probe_ops)
3004 };
3005
3006 static int ftrace_probe_registered;
3007
3008 static void __enable_ftrace_function_probe(void)
3009 {
3010         int ret;
3011         int i;
3012
3013         if (ftrace_probe_registered) {
3014                 /* still need to update the function call sites */
3015                 if (ftrace_enabled)
3016                         ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3017                 return;
3018         }
3019
3020         for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3021                 struct hlist_head *hhd = &ftrace_func_hash[i];
3022                 if (hhd->first)
3023                         break;
3024         }
3025         /* Nothing registered? */
3026         if (i == FTRACE_FUNC_HASHSIZE)
3027                 return;
3028
3029         ret = ftrace_startup(&trace_probe_ops, 0);
3030
3031         ftrace_probe_registered = 1;
3032 }
3033
3034 static void __disable_ftrace_function_probe(void)
3035 {
3036         int i;
3037
3038         if (!ftrace_probe_registered)
3039                 return;
3040
3041         for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3042                 struct hlist_head *hhd = &ftrace_func_hash[i];
3043                 if (hhd->first)
3044                         return;
3045         }
3046
3047         /* no more funcs left */
3048         ftrace_shutdown(&trace_probe_ops, 0);
3049
3050         ftrace_probe_registered = 0;
3051 }
3052
3053
3054 static void ftrace_free_entry(struct ftrace_func_probe *entry)
3055 {
3056         if (entry->ops->free)
3057                 entry->ops->free(entry->ops, entry->ip, &entry->data);
3058         kfree(entry);
3059 }
3060
3061 int
3062 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3063                               void *data)
3064 {
3065         struct ftrace_func_probe *entry;
3066         struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
3067         struct ftrace_hash *hash;
3068         struct ftrace_page *pg;
3069         struct dyn_ftrace *rec;
3070         int type, len, not;
3071         unsigned long key;
3072         int count = 0;
3073         char *search;
3074         int ret;
3075
3076         type = filter_parse_regex(glob, strlen(glob), &search, &not);
3077         len = strlen(search);
3078
3079         /* we do not support '!' for function probes */
3080         if (WARN_ON(not))
3081                 return -EINVAL;
3082
3083         mutex_lock(&trace_probe_ops.regex_lock);
3084
3085         hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3086         if (!hash) {
3087                 count = -ENOMEM;
3088                 goto out;
3089         }
3090
3091         if (unlikely(ftrace_disabled)) {
3092                 count = -ENODEV;
3093                 goto out;
3094         }
3095
3096         mutex_lock(&ftrace_lock);
3097
3098         do_for_each_ftrace_rec(pg, rec) {
3099
3100                 if (!ftrace_match_record(rec, NULL, search, len, type))
3101                         continue;
3102
3103                 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3104                 if (!entry) {
3105                         /* If we did not process any, then return error */
3106                         if (!count)
3107                                 count = -ENOMEM;
3108                         goto out_unlock;
3109                 }
3110
3111                 count++;
3112
3113                 entry->data = data;
3114
3115                 /*
3116                  * The caller might want to do something special
3117                  * for each function we find. We call the callback
3118                  * to give the caller an opportunity to do so.
3119                  */
3120                 if (ops->init) {
3121                         if (ops->init(ops, rec->ip, &entry->data) < 0) {
3122                                 /* caller does not like this func */
3123                                 kfree(entry);
3124                                 continue;
3125                         }
3126                 }
3127
3128                 ret = enter_record(hash, rec, 0);
3129                 if (ret < 0) {
3130                         kfree(entry);
3131                         count = ret;
3132                         goto out_unlock;
3133                 }
3134
3135                 entry->ops = ops;
3136                 entry->ip = rec->ip;
3137
3138                 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3139                 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3140
3141         } while_for_each_ftrace_rec();
3142
3143         ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3144         if (ret < 0)
3145                 count = ret;
3146
3147         __enable_ftrace_function_probe();
3148
3149  out_unlock:
3150         mutex_unlock(&ftrace_lock);
3151  out:
3152         mutex_unlock(&trace_probe_ops.regex_lock);
3153         free_ftrace_hash(hash);
3154
3155         return count;
3156 }
3157
3158 enum {
3159         PROBE_TEST_FUNC         = 1,
3160         PROBE_TEST_DATA         = 2
3161 };
3162
3163 static void
3164 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3165                                   void *data, int flags)
3166 {
3167         struct ftrace_func_entry *rec_entry;
3168         struct ftrace_func_probe *entry;
3169         struct ftrace_func_probe *p;
3170         struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
3171         struct list_head free_list;
3172         struct ftrace_hash *hash;
3173         struct hlist_node *tmp;
3174         char str[KSYM_SYMBOL_LEN];
3175         int type = MATCH_FULL;
3176         int i, len = 0;
3177         char *search;
3178
3179         if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
3180                 glob = NULL;
3181         else if (glob) {
3182                 int not;
3183
3184                 type = filter_parse_regex(glob, strlen(glob), &search, &not);
3185                 len = strlen(search);
3186
3187                 /* we do not support '!' for function probes */
3188                 if (WARN_ON(not))
3189                         return;
3190         }
3191
3192         mutex_lock(&trace_probe_ops.regex_lock);
3193
3194         hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3195         if (!hash)
3196                 /* Hmm, should report this somehow */
3197                 goto out_unlock;
3198
3199         INIT_LIST_HEAD(&free_list);
3200
3201         for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3202                 struct hlist_head *hhd = &ftrace_func_hash[i];
3203
3204                 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
3205
3206                         /* break up if statements for readability */
3207                         if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
3208                                 continue;
3209
3210                         if ((flags & PROBE_TEST_DATA) && entry->data != data)
3211                                 continue;
3212
3213                         /* do this last, since it is the most expensive */
3214                         if (glob) {
3215                                 kallsyms_lookup(entry->ip, NULL, NULL,
3216                                                 NULL, str);
3217                                 if (!ftrace_match(str, glob, len, type))
3218                                         continue;
3219                         }
3220
3221                         rec_entry = ftrace_lookup_ip(hash, entry->ip);
3222                         /* It is possible more than one entry had this ip */
3223                         if (rec_entry)
3224                                 free_hash_entry(hash, rec_entry);
3225
3226                         hlist_del_rcu(&entry->node);
3227                         list_add(&entry->free_list, &free_list);
3228                 }
3229         }
3230         mutex_lock(&ftrace_lock);
3231         __disable_ftrace_function_probe();
3232         /*
3233          * Remove after the disable is called. Otherwise, if the last
3234          * probe is removed, a null hash means *all enabled*.
3235          */
3236         ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3237         synchronize_sched();
3238         list_for_each_entry_safe(entry, p, &free_list, free_list) {
3239                 list_del(&entry->free_list);
3240                 ftrace_free_entry(entry);
3241         }
3242         mutex_unlock(&ftrace_lock);
3243                 
3244  out_unlock:
3245         mutex_unlock(&trace_probe_ops.regex_lock);
3246         free_ftrace_hash(hash);
3247 }
3248
3249 void
3250 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3251                                 void *data)
3252 {
3253         __unregister_ftrace_function_probe(glob, ops, data,
3254                                           PROBE_TEST_FUNC | PROBE_TEST_DATA);
3255 }
3256
3257 void
3258 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
3259 {
3260         __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
3261 }
3262
3263 void unregister_ftrace_function_probe_all(char *glob)
3264 {
3265         __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
3266 }
3267
3268 static LIST_HEAD(ftrace_commands);
3269 static DEFINE_MUTEX(ftrace_cmd_mutex);
3270
3271 /*
3272  * Currently we only register ftrace commands from __init, so mark this
3273  * __init too.
3274  */
3275 __init int register_ftrace_command(struct ftrace_func_command *cmd)
3276 {
3277         struct ftrace_func_command *p;
3278         int ret = 0;
3279
3280         mutex_lock(&ftrace_cmd_mutex);
3281         list_for_each_entry(p, &ftrace_commands, list) {
3282                 if (strcmp(cmd->name, p->name) == 0) {
3283                         ret = -EBUSY;
3284                         goto out_unlock;
3285                 }
3286         }
3287         list_add(&cmd->list, &ftrace_commands);
3288  out_unlock:
3289         mutex_unlock(&ftrace_cmd_mutex);
3290
3291         return ret;
3292 }
3293
3294 /*
3295  * Currently we only unregister ftrace commands from __init, so mark
3296  * this __init too.
3297  */
3298 __init int unregister_ftrace_command(struct ftrace_func_command *cmd)
3299 {
3300         struct ftrace_func_command *p, *n;
3301         int ret = -ENODEV;
3302
3303         mutex_lock(&ftrace_cmd_mutex);
3304         list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3305                 if (strcmp(cmd->name, p->name) == 0) {
3306                         ret = 0;
3307                         list_del_init(&p->list);
3308                         goto out_unlock;
3309                 }
3310         }
3311  out_unlock:
3312         mutex_unlock(&ftrace_cmd_mutex);
3313
3314         return ret;
3315 }
3316
3317 static int ftrace_process_regex(struct ftrace_hash *hash,
3318                                 char *buff, int len, int enable)
3319 {
3320         char *func, *command, *next = buff;
3321         struct ftrace_func_command *p;
3322         int ret = -EINVAL;
3323
3324         func = strsep(&next, ":");
3325
3326         if (!next) {
3327                 ret = ftrace_match_records(hash, func, len);
3328                 if (!ret)
3329                         ret = -EINVAL;
3330                 if (ret < 0)
3331                         return ret;
3332                 return 0;
3333         }
3334
3335         /* command found */
3336
3337         command = strsep(&next, ":");
3338
3339         mutex_lock(&ftrace_cmd_mutex);
3340         list_for_each_entry(p, &ftrace_commands, list) {
3341                 if (strcmp(p->name, command) == 0) {
3342                         ret = p->func(hash, func, command, next, enable);
3343                         goto out_unlock;
3344                 }
3345         }
3346  out_unlock:
3347         mutex_unlock(&ftrace_cmd_mutex);
3348
3349         return ret;
3350 }
3351
3352 static ssize_t
3353 ftrace_regex_write(struct file *file, const char __user *ubuf,
3354                    size_t cnt, loff_t *ppos, int enable)
3355 {
3356         struct ftrace_iterator *iter;
3357         struct trace_parser *parser;
3358         ssize_t ret, read;
3359
3360         if (!cnt)
3361                 return 0;
3362
3363         if (file->f_mode & FMODE_READ) {
3364                 struct seq_file *m = file->private_data;
3365                 iter = m->private;
3366         } else
3367                 iter = file->private_data;
3368
3369         if (unlikely(ftrace_disabled))
3370                 return -ENODEV;
3371
3372         /* iter->hash is a local copy, so we don't need regex_lock */
3373
3374         parser = &iter->parser;
3375         read = trace_get_user(parser, ubuf, cnt, ppos);
3376
3377         if (read >= 0 && trace_parser_loaded(parser) &&
3378             !trace_parser_cont(parser)) {
3379                 ret = ftrace_process_regex(iter->hash, parser->buffer,
3380                                            parser->idx, enable);
3381                 trace_parser_clear(parser);
3382                 if (ret < 0)
3383                         goto out;
3384         }
3385
3386         ret = read;
3387  out:
3388         return ret;
3389 }
3390
3391 ssize_t
3392 ftrace_filter_write(struct file *file, const char __user *ubuf,
3393                     size_t cnt, loff_t *ppos)
3394 {
3395         return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3396 }
3397
3398 ssize_t
3399 ftrace_notrace_write(struct file *file, const char __user *ubuf,
3400                      size_t cnt, loff_t *ppos)
3401 {
3402         return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3403 }
3404
3405 static int
3406 ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3407 {
3408         struct ftrace_func_entry *entry;
3409
3410         if (!ftrace_location(ip))
3411                 return -EINVAL;
3412
3413         if (remove) {
3414                 entry = ftrace_lookup_ip(hash, ip);
3415                 if (!entry)
3416                         return -ENOENT;
3417                 free_hash_entry(hash, entry);
3418                 return 0;
3419         }
3420
3421         return add_hash_entry(hash, ip);
3422 }
3423
3424 static void ftrace_ops_update_code(struct ftrace_ops *ops)
3425 {
3426         if (ops->flags & FTRACE_OPS_FL_ENABLED && ftrace_enabled)
3427                 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3428 }
3429
3430 static int
3431 ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3432                 unsigned long ip, int remove, int reset, int enable)
3433 {
3434         struct ftrace_hash **orig_hash;
3435         struct ftrace_hash *hash;
3436         int ret;
3437
3438         if (unlikely(ftrace_disabled))
3439                 return -ENODEV;
3440
3441         mutex_lock(&ops->regex_lock);
3442
3443         if (enable)
3444                 orig_hash = &ops->filter_hash;
3445         else
3446                 orig_hash = &ops->notrace_hash;
3447
3448         hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3449         if (!hash) {
3450                 ret = -ENOMEM;
3451                 goto out_regex_unlock;
3452         }
3453
3454         if (reset)
3455                 ftrace_filter_reset(hash);
3456         if (buf && !ftrace_match_records(hash, buf, len)) {
3457                 ret = -EINVAL;
3458                 goto out_regex_unlock;
3459         }
3460         if (ip) {
3461                 ret = ftrace_match_addr(hash, ip, remove);
3462                 if (ret < 0)
3463                         goto out_regex_unlock;
3464         }
3465
3466         mutex_lock(&ftrace_lock);
3467         ret = ftrace_hash_move(ops, enable, orig_hash, hash);
3468         if (!ret)
3469                 ftrace_ops_update_code(ops);
3470
3471         mutex_unlock(&ftrace_lock);
3472
3473  out_regex_unlock:
3474         mutex_unlock(&ops->regex_lock);
3475
3476         free_ftrace_hash(hash);
3477         return ret;
3478 }
3479
3480 static int
3481 ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3482                 int reset, int enable)
3483 {
3484         return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3485 }
3486
3487 /**
3488  * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3489  * @ops - the ops to set the filter with
3490  * @ip - the address to add to or remove from the filter.
3491  * @remove - non zero to remove the ip from the filter
3492  * @reset - non zero to reset all filters before applying this filter.
3493  *
3494  * Filters denote which functions should be enabled when tracing is enabled
3495  * If @ip is NULL, it failes to update filter.
3496  */
3497 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3498                          int remove, int reset)
3499 {
3500         ftrace_ops_init(ops);
3501         return ftrace_set_addr(ops, ip, remove, reset, 1);
3502 }
3503 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3504
3505 static int
3506 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3507                  int reset, int enable)
3508 {
3509         return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3510 }
3511
3512 /**
3513  * ftrace_set_filter - set a function to filter on in ftrace
3514  * @ops - the ops to set the filter with
3515  * @buf - the string that holds the function filter text.
3516  * @len - the length of the string.
3517  * @reset - non zero to reset all filters before applying this filter.
3518  *
3519  * Filters denote which functions should be enabled when tracing is enabled.
3520  * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3521  */
3522 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
3523                        int len, int reset)
3524 {
3525         ftrace_ops_init(ops);
3526         return ftrace_set_regex(ops, buf, len, reset, 1);
3527 }
3528 EXPORT_SYMBOL_GPL(ftrace_set_filter);
3529
3530 /**
3531  * ftrace_set_notrace - set a function to not trace in ftrace
3532  * @ops - the ops to set the notrace filter with
3533  * @buf - the string that holds the function notrace text.
3534  * @len - the length of the string.
3535  * @reset - non zero to reset all filters before applying this filter.
3536  *
3537  * Notrace Filters denote which functions should not be enabled when tracing
3538  * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3539  * for tracing.
3540  */
3541 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
3542                         int len, int reset)
3543 {
3544         ftrace_ops_init(ops);
3545         return ftrace_set_regex(ops, buf, len, reset, 0);
3546 }
3547 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3548 /**
3549  * ftrace_set_global_filter - set a function to filter on with global tracers
3550  * @buf - the string that holds the function filter text.
3551  * @len - the length of the string.
3552  * @reset - non zero to reset all filters before applying this filter.
3553  *
3554  * Filters denote which functions should be enabled when tracing is enabled.
3555  * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3556  */
3557 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3558 {
3559         ftrace_set_regex(&global_ops, buf, len, reset, 1);
3560 }
3561 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3562
3563 /**
3564  * ftrace_set_global_notrace - set a function to not trace with global tracers
3565  * @buf - the string that holds the function notrace text.
3566  * @len - the length of the string.
3567  * @reset - non zero to reset all filters before applying this filter.
3568  *
3569  * Notrace Filters denote which functions should not be enabled when tracing
3570  * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3571  * for tracing.
3572  */
3573 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
3574 {
3575         ftrace_set_regex(&global_ops, buf, len, reset, 0);
3576 }
3577 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
3578
3579 /*
3580  * command line interface to allow users to set filters on boot up.
3581  */
3582 #define FTRACE_FILTER_SIZE              COMMAND_LINE_SIZE
3583 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3584 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3585
3586 /* Used by function selftest to not test if filter is set */
3587 bool ftrace_filter_param __initdata;
3588
3589 static int __init set_ftrace_notrace(char *str)
3590 {
3591         ftrace_filter_param = true;
3592         strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
3593         return 1;
3594 }
3595 __setup("ftrace_notrace=", set_ftrace_notrace);
3596
3597 static int __init set_ftrace_filter(char *str)
3598 {
3599         ftrace_filter_param = true;
3600         strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
3601         return 1;
3602 }
3603 __setup("ftrace_filter=", set_ftrace_filter);
3604
3605 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3606 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
3607 static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
3608
3609 static int __init set_graph_function(char *str)
3610 {
3611         strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
3612         return 1;
3613 }
3614 __setup("ftrace_graph_filter=", set_graph_function);
3615
3616 static void __init set_ftrace_early_graph(char *buf)
3617 {
3618         int ret;
3619         char *func;
3620
3621         while (buf) {
3622                 func = strsep(&buf, ",");
3623                 /* we allow only one expression at a time */
3624                 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3625                                       FTRACE_GRAPH_MAX_FUNCS, func);
3626                 if (ret)
3627                         printk(KERN_DEBUG "ftrace: function %s not "
3628                                           "traceable\n", func);
3629         }
3630 }
3631 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3632
3633 void __init
3634 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
3635 {
3636         char *func;
3637
3638         ftrace_ops_init(ops);
3639
3640         while (buf) {
3641                 func = strsep(&buf, ",");
3642                 ftrace_set_regex(ops, func, strlen(func), 0, enable);
3643         }
3644 }
3645
3646 static void __init set_ftrace_early_filters(void)
3647 {
3648         if (ftrace_filter_buf[0])
3649                 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
3650         if (ftrace_notrace_buf[0])
3651                 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
3652 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3653         if (ftrace_graph_buf[0])
3654                 set_ftrace_early_graph(ftrace_graph_buf);
3655 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3656 }
3657
3658 int ftrace_regex_release(struct inode *inode, struct file *file)
3659 {
3660         struct seq_file *m = (struct seq_file *)file->private_data;
3661         struct ftrace_iterator *iter;
3662         struct ftrace_hash **orig_hash;
3663         struct trace_parser *parser;
3664         int filter_hash;
3665         int ret;
3666
3667         if (file->f_mode & FMODE_READ) {
3668                 iter = m->private;
3669                 seq_release(inode, file);
3670         } else
3671                 iter = file->private_data;
3672
3673         parser = &iter->parser;
3674         if (trace_parser_loaded(parser)) {
3675                 parser->buffer[parser->idx] = 0;
3676                 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
3677         }
3678
3679         trace_parser_put(parser);
3680
3681         mutex_lock(&iter->ops->regex_lock);
3682
3683         if (file->f_mode & FMODE_WRITE) {
3684                 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3685
3686                 if (filter_hash)
3687                         orig_hash = &iter->ops->filter_hash;
3688                 else
3689                         orig_hash = &iter->ops->notrace_hash;
3690
3691                 mutex_lock(&ftrace_lock);
3692                 ret = ftrace_hash_move(iter->ops, filter_hash,
3693                                        orig_hash, iter->hash);
3694                 if (!ret)
3695                         ftrace_ops_update_code(iter->ops);
3696
3697                 mutex_unlock(&ftrace_lock);
3698         }
3699
3700         mutex_unlock(&iter->ops->regex_lock);
3701         free_ftrace_hash(iter->hash);
3702         kfree(iter);
3703
3704         return 0;
3705 }
3706
3707 static const struct file_operations ftrace_avail_fops = {
3708         .open = ftrace_avail_open,
3709         .read = seq_read,
3710         .llseek = seq_lseek,
3711         .release = seq_release_private,
3712 };
3713
3714 static const struct file_operations ftrace_enabled_fops = {
3715         .open = ftrace_enabled_open,
3716         .read = seq_read,
3717         .llseek = seq_lseek,
3718         .release = seq_release_private,
3719 };
3720
3721 static const struct file_operations ftrace_filter_fops = {
3722         .open = ftrace_filter_open,
3723         .read = seq_read,
3724         .write = ftrace_filter_write,
3725         .llseek = tracing_lseek,
3726         .release = ftrace_regex_release,
3727 };
3728
3729 static const struct file_operations ftrace_notrace_fops = {
3730         .open = ftrace_notrace_open,
3731         .read = seq_read,
3732         .write = ftrace_notrace_write,
3733         .llseek = tracing_lseek,
3734         .release = ftrace_regex_release,
3735 };
3736
3737 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3738
3739 static DEFINE_MUTEX(graph_lock);
3740
3741 int ftrace_graph_count;
3742 int ftrace_graph_notrace_count;
3743 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3744 unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3745
3746 struct ftrace_graph_data {
3747         unsigned long *table;
3748         size_t size;
3749         int *count;
3750         const struct seq_operations *seq_ops;
3751 };
3752
3753 static void *
3754 __g_next(struct seq_file *m, loff_t *pos)
3755 {
3756         struct ftrace_graph_data *fgd = m->private;
3757
3758         if (*pos >= *fgd->count)
3759                 return NULL;
3760         return &fgd->table[*pos];
3761 }
3762
3763 static void *
3764 g_next(struct seq_file *m, void *v, loff_t *pos)
3765 {
3766         (*pos)++;
3767         return __g_next(m, pos);
3768 }
3769
3770 static void *g_start(struct seq_file *m, loff_t *pos)
3771 {
3772         struct ftrace_graph_data *fgd = m->private;
3773
3774         mutex_lock(&graph_lock);
3775
3776         /* Nothing, tell g_show to print all functions are enabled */
3777         if (!*fgd->count && !*pos)
3778                 return (void *)1;
3779
3780         return __g_next(m, pos);
3781 }
3782
3783 static void g_stop(struct seq_file *m, void *p)
3784 {
3785         mutex_unlock(&graph_lock);
3786 }
3787
3788 static int g_show(struct seq_file *m, void *v)
3789 {
3790         unsigned long *ptr = v;
3791
3792         if (!ptr)
3793                 return 0;
3794
3795         if (ptr == (unsigned long *)1) {
3796                 seq_printf(m, "#### all functions enabled ####\n");
3797                 return 0;
3798         }
3799
3800         seq_printf(m, "%ps\n", (void *)*ptr);
3801
3802         return 0;
3803 }
3804
3805 static const struct seq_operations ftrace_graph_seq_ops = {
3806         .start = g_start,
3807         .next = g_next,
3808         .stop = g_stop,
3809         .show = g_show,
3810 };
3811
3812 static int
3813 __ftrace_graph_open(struct inode *inode, struct file *file,
3814                     struct ftrace_graph_data *fgd)
3815 {
3816         int ret = 0;
3817
3818         mutex_lock(&graph_lock);
3819         if ((file->f_mode & FMODE_WRITE) &&
3820             (file->f_flags & O_TRUNC)) {
3821                 *fgd->count = 0;
3822                 memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
3823         }
3824         mutex_unlock(&graph_lock);
3825
3826         if (file->f_mode & FMODE_READ) {
3827                 ret = seq_open(file, fgd->seq_ops);
3828                 if (!ret) {
3829                         struct seq_file *m = file->private_data;
3830                         m->private = fgd;
3831                 }
3832         } else
3833                 file->private_data = fgd;
3834
3835         return ret;
3836 }
3837
3838 static int
3839 ftrace_graph_open(struct inode *inode, struct file *file)
3840 {
3841         struct ftrace_graph_data *fgd;
3842
3843         if (unlikely(ftrace_disabled))
3844                 return -ENODEV;
3845
3846         fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
3847         if (fgd == NULL)
3848                 return -ENOMEM;
3849
3850         fgd->table = ftrace_graph_funcs;
3851         fgd->size = FTRACE_GRAPH_MAX_FUNCS;
3852         fgd->count = &ftrace_graph_count;
3853         fgd->seq_ops = &ftrace_graph_seq_ops;
3854
3855         return __ftrace_graph_open(inode, file, fgd);
3856 }
3857
3858 static int
3859 ftrace_graph_notrace_open(struct inode *inode, struct file *file)
3860 {
3861         struct ftrace_graph_data *fgd;
3862
3863         if (unlikely(ftrace_disabled))
3864                 return -ENODEV;
3865
3866         fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
3867         if (fgd == NULL)
3868                 return -ENOMEM;
3869
3870         fgd->table = ftrace_graph_notrace_funcs;
3871         fgd->size = FTRACE_GRAPH_MAX_FUNCS;
3872         fgd->count = &ftrace_graph_notrace_count;
3873         fgd->seq_ops = &ftrace_graph_seq_ops;
3874
3875         return __ftrace_graph_open(inode, file, fgd);
3876 }
3877
3878 static int
3879 ftrace_graph_release(struct inode *inode, struct file *file)
3880 {
3881         if (file->f_mode & FMODE_READ) {
3882                 struct seq_file *m = file->private_data;
3883
3884                 kfree(m->private);
3885                 seq_release(inode, file);
3886         } else {
3887                 kfree(file->private_data);
3888         }
3889
3890         return 0;
3891 }
3892
3893 static int
3894 ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
3895 {
3896         struct dyn_ftrace *rec;
3897         struct ftrace_page *pg;
3898         int search_len;
3899         int fail = 1;
3900         int type, not;
3901         char *search;
3902         bool exists;
3903         int i;
3904
3905         /* decode regex */
3906         type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
3907         if (!not && *idx >= size)
3908                 return -EBUSY;
3909
3910         search_len = strlen(search);
3911
3912         mutex_lock(&ftrace_lock);
3913
3914         if (unlikely(ftrace_disabled)) {
3915                 mutex_unlock(&ftrace_lock);
3916                 return -ENODEV;
3917         }
3918
3919         do_for_each_ftrace_rec(pg, rec) {
3920
3921                 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
3922                         /* if it is in the array */
3923                         exists = false;
3924                         for (i = 0; i < *idx; i++) {
3925                                 if (array[i] == rec->ip) {
3926                                         exists = true;
3927                                         break;
3928                                 }
3929                         }
3930
3931                         if (!not) {
3932                                 fail = 0;
3933                                 if (!exists) {
3934                                         array[(*idx)++] = rec->ip;
3935                                         if (*idx >= size)
3936                                                 goto out;
3937                                 }
3938                         } else {
3939                                 if (exists) {
3940                                         array[i] = array[--(*idx)];
3941                                         array[*idx] = 0;
3942                                         fail = 0;
3943                                 }
3944                         }
3945                 }
3946         } while_for_each_ftrace_rec();
3947 out:
3948         mutex_unlock(&ftrace_lock);
3949
3950         if (fail)
3951                 return -EINVAL;
3952
3953         return 0;
3954 }
3955
3956 static ssize_t
3957 ftrace_graph_write(struct file *file, const char __user *ubuf,
3958                    size_t cnt, loff_t *ppos)
3959 {
3960         struct trace_parser parser;
3961         ssize_t read, ret = 0;
3962         struct ftrace_graph_data *fgd = file->private_data;
3963
3964         if (!cnt)
3965                 return 0;
3966
3967         if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
3968                 return -ENOMEM;
3969
3970         read = trace_get_user(&parser, ubuf, cnt, ppos);
3971
3972         if (read >= 0 && trace_parser_loaded((&parser))) {
3973                 parser.buffer[parser.idx] = 0;
3974
3975                 mutex_lock(&graph_lock);
3976
3977                 /* we allow only one expression at a time */
3978                 ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
3979                                       parser.buffer);
3980
3981                 mutex_unlock(&graph_lock);
3982         }
3983
3984         if (!ret)
3985                 ret = read;
3986
3987         trace_parser_put(&parser);
3988
3989         return ret;
3990 }
3991
3992 static const struct file_operations ftrace_graph_fops = {
3993         .open           = ftrace_graph_open,
3994         .read           = seq_read,
3995         .write          = ftrace_graph_write,
3996         .llseek         = tracing_lseek,
3997         .release        = ftrace_graph_release,
3998 };
3999
4000 static const struct file_operations ftrace_graph_notrace_fops = {
4001         .open           = ftrace_graph_notrace_open,
4002         .read           = seq_read,
4003         .write          = ftrace_graph_write,
4004         .llseek         = tracing_lseek,
4005         .release        = ftrace_graph_release,
4006 };
4007 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4008
4009 void ftrace_create_filter_files(struct ftrace_ops *ops,
4010                                 struct dentry *parent)
4011 {
4012
4013         trace_create_file("set_ftrace_filter", 0644, parent,
4014                           ops, &ftrace_filter_fops);
4015
4016         trace_create_file("set_ftrace_notrace", 0644, parent,
4017                           ops, &ftrace_notrace_fops);
4018 }
4019
4020 /*
4021  * The name "destroy_filter_files" is really a misnomer. Although
4022  * in the future, it may actualy delete the files, but this is
4023  * really intended to make sure the ops passed in are disabled
4024  * and that when this function returns, the caller is free to
4025  * free the ops.
4026  *
4027  * The "destroy" name is only to match the "create" name that this
4028  * should be paired with.
4029  */
4030 void ftrace_destroy_filter_files(struct ftrace_ops *ops)
4031 {
4032         mutex_lock(&ftrace_lock);
4033         if (ops->flags & FTRACE_OPS_FL_ENABLED)
4034                 ftrace_shutdown(ops, 0);
4035         ops->flags |= FTRACE_OPS_FL_DELETED;
4036         mutex_unlock(&ftrace_lock);
4037 }
4038
4039 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
4040 {
4041
4042         trace_create_file("available_filter_functions", 0444,
4043                         d_tracer, NULL, &ftrace_avail_fops);
4044
4045         trace_create_file("enabled_functions", 0444,
4046                         d_tracer, NULL, &ftrace_enabled_fops);
4047
4048         ftrace_create_filter_files(&global_ops, d_tracer);
4049
4050 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4051         trace_create_file("set_graph_function", 0444, d_tracer,
4052                                     NULL,
4053                                     &ftrace_graph_fops);
4054         trace_create_file("set_graph_notrace", 0444, d_tracer,
4055                                     NULL,
4056                                     &ftrace_graph_notrace_fops);
4057 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4058
4059         return 0;
4060 }
4061
4062 static int ftrace_cmp_ips(const void *a, const void *b)
4063 {
4064         const unsigned long *ipa = a;
4065         const unsigned long *ipb = b;
4066
4067         if (*ipa > *ipb)
4068                 return 1;
4069         if (*ipa < *ipb)
4070                 return -1;
4071         return 0;
4072 }
4073
4074 static void ftrace_swap_ips(void *a, void *b, int size)
4075 {
4076         unsigned long *ipa = a;
4077         unsigned long *ipb = b;
4078         unsigned long t;
4079
4080         t = *ipa;
4081         *ipa = *ipb;
4082         *ipb = t;
4083 }
4084
4085 static int ftrace_process_locs(struct module *mod,
4086                                unsigned long *start,
4087                                unsigned long *end)
4088 {
4089         struct ftrace_page *start_pg;
4090         struct ftrace_page *pg;
4091         struct dyn_ftrace *rec;
4092         unsigned long count;
4093         unsigned long *p;
4094         unsigned long addr;
4095         unsigned long flags = 0; /* Shut up gcc */
4096         int ret = -ENOMEM;
4097
4098         count = end - start;
4099
4100         if (!count)
4101                 return 0;
4102
4103         sort(start, count, sizeof(*start),
4104              ftrace_cmp_ips, ftrace_swap_ips);
4105
4106         start_pg = ftrace_allocate_pages(count);
4107         if (!start_pg)
4108                 return -ENOMEM;
4109
4110         mutex_lock(&ftrace_lock);
4111
4112         /*
4113          * Core and each module needs their own pages, as
4114          * modules will free them when they are removed.
4115          * Force a new page to be allocated for modules.
4116          */
4117         if (!mod) {
4118                 WARN_ON(ftrace_pages || ftrace_pages_start);
4119                 /* First initialization */
4120                 ftrace_pages = ftrace_pages_start = start_pg;
4121         } else {
4122                 if (!ftrace_pages)
4123                         goto out;
4124
4125                 if (WARN_ON(ftrace_pages->next)) {
4126                         /* Hmm, we have free pages? */
4127                         while (ftrace_pages->next)
4128                                 ftrace_pages = ftrace_pages->next;
4129                 }
4130
4131                 ftrace_pages->next = start_pg;
4132         }
4133
4134         p = start;
4135         pg = start_pg;
4136         while (p < end) {
4137                 addr = ftrace_call_adjust(*p++);
4138                 /*
4139                  * Some architecture linkers will pad between
4140                  * the different mcount_loc sections of different
4141                  * object files to satisfy alignments.
4142                  * Skip any NULL pointers.
4143                  */
4144                 if (!addr)
4145                         continue;
4146
4147                 if (pg->index == pg->size) {
4148                         /* We should have allocated enough */
4149                         if (WARN_ON(!pg->next))
4150                                 break;
4151                         pg = pg->next;
4152                 }
4153
4154                 rec = &pg->records[pg->index++];
4155                 rec->ip = addr;
4156         }
4157
4158         /* We should have used all pages */
4159         WARN_ON(pg->next);
4160
4161         /* Assign the last page to ftrace_pages */
4162         ftrace_pages = pg;
4163
4164         /*
4165          * We only need to disable interrupts on start up
4166          * because we are modifying code that an interrupt
4167          * may execute, and the modification is not atomic.
4168          * But for modules, nothing runs the code we modify
4169          * until we are finished with it, and there's no
4170          * reason to cause large interrupt latencies while we do it.
4171          */
4172         if (!mod)
4173                 local_irq_save(flags);
4174         ftrace_update_code(mod, start_pg);
4175         if (!mod)
4176                 local_irq_restore(flags);
4177         ret = 0;
4178  out:
4179         mutex_unlock(&ftrace_lock);
4180
4181         return ret;
4182 }
4183
4184 #ifdef CONFIG_MODULES
4185
4186 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4187
4188 void ftrace_release_mod(struct module *mod)
4189 {
4190         struct dyn_ftrace *rec;
4191         struct ftrace_page **last_pg;
4192         struct ftrace_page *pg;
4193         int order;
4194
4195         mutex_lock(&ftrace_lock);
4196
4197         if (ftrace_disabled)
4198                 goto out_unlock;
4199
4200         /*
4201          * Each module has its own ftrace_pages, remove
4202          * them from the list.
4203          */
4204         last_pg = &ftrace_pages_start;
4205         for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4206                 rec = &pg->records[0];
4207                 if (within_module_core(rec->ip, mod)) {
4208                         /*
4209                          * As core pages are first, the first
4210                          * page should never be a module page.
4211                          */
4212                         if (WARN_ON(pg == ftrace_pages_start))
4213                                 goto out_unlock;
4214
4215                         /* Check if we are deleting the last page */
4216                         if (pg == ftrace_pages)
4217                                 ftrace_pages = next_to_ftrace_page(last_pg);
4218
4219                         *last_pg = pg->next;
4220                         order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4221                         free_pages((unsigned long)pg->records, order);
4222                         kfree(pg);
4223                 } else
4224                         last_pg = &pg->next;
4225         }
4226  out_unlock:
4227         mutex_unlock(&ftrace_lock);
4228 }
4229
4230 static void ftrace_init_module(struct module *mod,
4231                                unsigned long *start, unsigned long *end)
4232 {
4233         if (ftrace_disabled || start == end)
4234                 return;
4235         ftrace_process_locs(mod, start, end);
4236 }
4237
4238 static int ftrace_module_notify_enter(struct notifier_block *self,
4239                                       unsigned long val, void *data)
4240 {
4241         struct module *mod = data;
4242
4243         if (val == MODULE_STATE_COMING)
4244                 ftrace_init_module(mod, mod->ftrace_callsites,
4245                                    mod->ftrace_callsites +
4246                                    mod->num_ftrace_callsites);
4247         return 0;
4248 }
4249
4250 static int ftrace_module_notify_exit(struct notifier_block *self,
4251                                      unsigned long val, void *data)
4252 {
4253         struct module *mod = data;
4254
4255         if (val == MODULE_STATE_GOING)
4256                 ftrace_release_mod(mod);
4257
4258         return 0;
4259 }
4260 #else
4261 static int ftrace_module_notify_enter(struct notifier_block *self,
4262                                       unsigned long val, void *data)
4263 {
4264         return 0;
4265 }
4266 static int ftrace_module_notify_exit(struct notifier_block *self,
4267                                      unsigned long val, void *data)
4268 {
4269         return 0;
4270 }
4271 #endif /* CONFIG_MODULES */
4272
4273 struct notifier_block ftrace_module_enter_nb = {
4274         .notifier_call = ftrace_module_notify_enter,
4275         .priority = INT_MAX,    /* Run before anything that can use kprobes */
4276 };
4277
4278 struct notifier_block ftrace_module_exit_nb = {
4279         .notifier_call = ftrace_module_notify_exit,
4280         .priority = INT_MIN,    /* Run after anything that can remove kprobes */
4281 };
4282
4283 void __init ftrace_init(void)
4284 {
4285         extern unsigned long __start_mcount_loc[];
4286         extern unsigned long __stop_mcount_loc[];
4287         unsigned long count, flags;
4288         int ret;
4289
4290         local_irq_save(flags);
4291         ret = ftrace_dyn_arch_init();
4292         local_irq_restore(flags);
4293         if (ret)
4294                 goto failed;
4295
4296         count = __stop_mcount_loc - __start_mcount_loc;
4297         if (!count) {
4298                 pr_info("ftrace: No functions to be traced?\n");
4299                 goto failed;
4300         }
4301
4302         pr_info("ftrace: allocating %ld entries in %ld pages\n",
4303                 count, count / ENTRIES_PER_PAGE + 1);
4304
4305         last_ftrace_enabled = ftrace_enabled = 1;
4306
4307         ret = ftrace_process_locs(NULL,
4308                                   __start_mcount_loc,
4309                                   __stop_mcount_loc);
4310
4311         ret = register_module_notifier(&ftrace_module_enter_nb);
4312         if (ret)
4313                 pr_warning("Failed to register trace ftrace module enter notifier\n");
4314
4315         ret = register_module_notifier(&ftrace_module_exit_nb);
4316         if (ret)
4317                 pr_warning("Failed to register trace ftrace module exit notifier\n");
4318
4319         set_ftrace_early_filters();
4320
4321         return;
4322  failed:
4323         ftrace_disabled = 1;
4324 }
4325
4326 #else
4327
4328 static struct ftrace_ops global_ops = {
4329         .func                   = ftrace_stub,
4330         .flags                  = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4331         INIT_REGEX_LOCK(global_ops)
4332 };
4333
4334 static int __init ftrace_nodyn_init(void)
4335 {
4336         ftrace_enabled = 1;
4337         return 0;
4338 }
4339 core_initcall(ftrace_nodyn_init);
4340
4341 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4342 static inline void ftrace_startup_enable(int command) { }
4343 /* Keep as macros so we do not need to define the commands */
4344 # define ftrace_startup(ops, command)                                   \
4345         ({                                                              \
4346                 int ___ret = __register_ftrace_function(ops);           \
4347                 if (!___ret)                                            \
4348                         (ops)->flags |= FTRACE_OPS_FL_ENABLED;          \
4349                 ___ret;                                                 \
4350         })
4351 # define ftrace_shutdown(ops, command)                                  \
4352         ({                                                              \
4353                 int ___ret = __unregister_ftrace_function(ops);         \
4354                 if (!___ret)                                            \
4355                         (ops)->flags &= ~FTRACE_OPS_FL_ENABLED;         \
4356                 ___ret;                                                 \
4357         })
4358
4359 # define ftrace_startup_sysctl()        do { } while (0)
4360 # define ftrace_shutdown_sysctl()       do { } while (0)
4361
4362 static inline int
4363 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
4364 {
4365         return 1;
4366 }
4367
4368 #endif /* CONFIG_DYNAMIC_FTRACE */
4369
4370 __init void ftrace_init_global_array_ops(struct trace_array *tr)
4371 {
4372         tr->ops = &global_ops;
4373         tr->ops->private = tr;
4374 }
4375
4376 void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
4377 {
4378         /* If we filter on pids, update to use the pid function */
4379         if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
4380                 if (WARN_ON(tr->ops->func != ftrace_stub))
4381                         printk("ftrace ops had %pS for function\n",
4382                                tr->ops->func);
4383                 /* Only the top level instance does pid tracing */
4384                 if (!list_empty(&ftrace_pids)) {
4385                         set_ftrace_pid_function(func);
4386                         func = ftrace_pid_func;
4387                 }
4388         }
4389         tr->ops->func = func;
4390         tr->ops->private = tr;
4391 }
4392
4393 void ftrace_reset_array_ops(struct trace_array *tr)
4394 {
4395         tr->ops->func = ftrace_stub;
4396 }
4397
4398 static void
4399 ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
4400                         struct ftrace_ops *op, struct pt_regs *regs)
4401 {
4402         if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4403                 return;
4404
4405         /*
4406          * Some of the ops may be dynamically allocated,
4407          * they must be freed after a synchronize_sched().
4408          */
4409         preempt_disable_notrace();
4410         trace_recursion_set(TRACE_CONTROL_BIT);
4411
4412         /*
4413          * Control funcs (perf) uses RCU. Only trace if
4414          * RCU is currently active.
4415          */
4416         if (!rcu_is_watching())
4417                 goto out;
4418
4419         do_for_each_ftrace_op(op, ftrace_control_list) {
4420                 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
4421                     !ftrace_function_local_disabled(op) &&
4422                     ftrace_ops_test(op, ip, regs))
4423                         op->func(ip, parent_ip, op, regs);
4424         } while_for_each_ftrace_op(op);
4425  out:
4426         trace_recursion_clear(TRACE_CONTROL_BIT);
4427         preempt_enable_notrace();
4428 }
4429
4430 static struct ftrace_ops control_ops = {
4431         .func   = ftrace_ops_control_func,
4432         .flags  = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4433         INIT_REGEX_LOCK(control_ops)
4434 };
4435
4436 static inline void
4437 __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
4438                        struct ftrace_ops *ignored, struct pt_regs *regs)
4439 {
4440         struct ftrace_ops *op;
4441         int bit;
4442
4443         if (function_trace_stop)
4444                 return;
4445
4446         bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4447         if (bit < 0)
4448                 return;
4449
4450         /*
4451          * Some of the ops may be dynamically allocated,
4452          * they must be freed after a synchronize_sched().
4453          */
4454         preempt_disable_notrace();
4455         do_for_each_ftrace_op(op, ftrace_ops_list) {
4456                 if (ftrace_ops_test(op, ip, regs)) {
4457                         if (WARN_ON(!op->func)) {
4458                                 function_trace_stop = 1;
4459                                 printk("op=%p %pS\n", op, op);
4460                                 goto out;
4461                         }
4462                         op->func(ip, parent_ip, op, regs);
4463                 }
4464         } while_for_each_ftrace_op(op);
4465 out:
4466         preempt_enable_notrace();
4467         trace_clear_recursion(bit);
4468 }
4469
4470 /*
4471  * Some archs only support passing ip and parent_ip. Even though
4472  * the list function ignores the op parameter, we do not want any
4473  * C side effects, where a function is called without the caller
4474  * sending a third parameter.
4475  * Archs are to support both the regs and ftrace_ops at the same time.
4476  * If they support ftrace_ops, it is assumed they support regs.
4477  * If call backs want to use regs, they must either check for regs
4478  * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4479  * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
4480  * An architecture can pass partial regs with ftrace_ops and still
4481  * set the ARCH_SUPPORT_FTARCE_OPS.
4482  */
4483 #if ARCH_SUPPORTS_FTRACE_OPS
4484 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
4485                                  struct ftrace_ops *op, struct pt_regs *regs)
4486 {
4487         __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
4488 }
4489 #else
4490 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4491 {
4492         __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
4493 }
4494 #endif
4495
4496 static void clear_ftrace_swapper(void)
4497 {
4498         struct task_struct *p;
4499         int cpu;
4500
4501         get_online_cpus();
4502         for_each_online_cpu(cpu) {
4503                 p = idle_task(cpu);
4504                 clear_tsk_trace_trace(p);
4505         }
4506         put_online_cpus();
4507 }
4508
4509 static void set_ftrace_swapper(void)
4510 {
4511         struct task_struct *p;
4512         int cpu;
4513
4514         get_online_cpus();
4515         for_each_online_cpu(cpu) {
4516                 p = idle_task(cpu);
4517                 set_tsk_trace_trace(p);
4518         }
4519         put_online_cpus();
4520 }
4521
4522 static void clear_ftrace_pid(struct pid *pid)
4523 {
4524         struct task_struct *p;
4525
4526         rcu_read_lock();
4527         do_each_pid_task(pid, PIDTYPE_PID, p) {
4528                 clear_tsk_trace_trace(p);
4529         } while_each_pid_task(pid, PIDTYPE_PID, p);
4530         rcu_read_unlock();
4531
4532         put_pid(pid);
4533 }
4534
4535 static void set_ftrace_pid(struct pid *pid)
4536 {
4537         struct task_struct *p;
4538
4539         rcu_read_lock();
4540         do_each_pid_task(pid, PIDTYPE_PID, p) {
4541                 set_tsk_trace_trace(p);
4542         } while_each_pid_task(pid, PIDTYPE_PID, p);
4543         rcu_read_unlock();
4544 }
4545
4546 static void clear_ftrace_pid_task(struct pid *pid)
4547 {
4548         if (pid == ftrace_swapper_pid)
4549                 clear_ftrace_swapper();
4550         else
4551                 clear_ftrace_pid(pid);
4552 }
4553
4554 static void set_ftrace_pid_task(struct pid *pid)
4555 {
4556         if (pid == ftrace_swapper_pid)
4557                 set_ftrace_swapper();
4558         else
4559                 set_ftrace_pid(pid);
4560 }
4561
4562 static int ftrace_pid_add(int p)
4563 {
4564         struct pid *pid;
4565         struct ftrace_pid *fpid;
4566         int ret = -EINVAL;
4567
4568         mutex_lock(&ftrace_lock);
4569
4570         if (!p)
4571                 pid = ftrace_swapper_pid;
4572         else
4573                 pid = find_get_pid(p);
4574
4575         if (!pid)
4576                 goto out;
4577
4578         ret = 0;
4579
4580         list_for_each_entry(fpid, &ftrace_pids, list)
4581                 if (fpid->pid == pid)
4582                         goto out_put;
4583
4584         ret = -ENOMEM;
4585
4586         fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4587         if (!fpid)
4588                 goto out_put;
4589
4590         list_add(&fpid->list, &ftrace_pids);
4591         fpid->pid = pid;
4592
4593         set_ftrace_pid_task(pid);
4594
4595         ftrace_update_pid_func();
4596         ftrace_startup_enable(0);
4597
4598         mutex_unlock(&ftrace_lock);
4599         return 0;
4600
4601 out_put:
4602         if (pid != ftrace_swapper_pid)
4603                 put_pid(pid);
4604
4605 out:
4606         mutex_unlock(&ftrace_lock);
4607         return ret;
4608 }
4609
4610 static void ftrace_pid_reset(void)
4611 {
4612         struct ftrace_pid *fpid, *safe;
4613
4614         mutex_lock(&ftrace_lock);
4615         list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4616                 struct pid *pid = fpid->pid;
4617
4618                 clear_ftrace_pid_task(pid);
4619
4620                 list_del(&fpid->list);
4621                 kfree(fpid);
4622         }
4623
4624         ftrace_update_pid_func();
4625         ftrace_startup_enable(0);
4626
4627         mutex_unlock(&ftrace_lock);
4628 }
4629
4630 static void *fpid_start(struct seq_file *m, loff_t *pos)
4631 {
4632         mutex_lock(&ftrace_lock);
4633
4634         if (list_empty(&ftrace_pids) && (!*pos))
4635                 return (void *) 1;
4636
4637         return seq_list_start(&ftrace_pids, *pos);
4638 }
4639
4640 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4641 {
4642         if (v == (void *)1)
4643                 return NULL;
4644
4645         return seq_list_next(v, &ftrace_pids, pos);
4646 }
4647
4648 static void fpid_stop(struct seq_file *m, void *p)
4649 {
4650         mutex_unlock(&ftrace_lock);
4651 }
4652
4653 static int fpid_show(struct seq_file *m, void *v)
4654 {
4655         const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4656
4657         if (v == (void *)1) {
4658                 seq_printf(m, "no pid\n");
4659                 return 0;
4660         }
4661
4662         if (fpid->pid == ftrace_swapper_pid)
4663                 seq_printf(m, "swapper tasks\n");
4664         else
4665                 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4666
4667         return 0;
4668 }
4669
4670 static const struct seq_operations ftrace_pid_sops = {
4671         .start = fpid_start,
4672         .next = fpid_next,
4673         .stop = fpid_stop,
4674         .show = fpid_show,
4675 };
4676
4677 static int
4678 ftrace_pid_open(struct inode *inode, struct file *file)
4679 {
4680         int ret = 0;
4681
4682         if ((file->f_mode & FMODE_WRITE) &&
4683             (file->f_flags & O_TRUNC))
4684                 ftrace_pid_reset();
4685
4686         if (file->f_mode & FMODE_READ)
4687                 ret = seq_open(file, &ftrace_pid_sops);
4688
4689         return ret;
4690 }
4691
4692 static ssize_t
4693 ftrace_pid_write(struct file *filp, const char __user *ubuf,
4694                    size_t cnt, loff_t *ppos)
4695 {
4696         char buf[64], *tmp;
4697         long val;
4698         int ret;
4699
4700         if (cnt >= sizeof(buf))
4701                 return -EINVAL;
4702
4703         if (copy_from_user(&buf, ubuf, cnt))
4704                 return -EFAULT;
4705
4706         buf[cnt] = 0;
4707
4708         /*
4709          * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4710          * to clean the filter quietly.
4711          */
4712         tmp = strstrip(buf);
4713         if (strlen(tmp) == 0)
4714                 return 1;
4715
4716         ret = kstrtol(tmp, 10, &val);
4717         if (ret < 0)
4718                 return ret;
4719
4720         ret = ftrace_pid_add(val);
4721
4722         return ret ? ret : cnt;
4723 }
4724
4725 static int
4726 ftrace_pid_release(struct inode *inode, struct file *file)
4727 {
4728         if (file->f_mode & FMODE_READ)
4729                 seq_release(inode, file);
4730
4731         return 0;
4732 }
4733
4734 static const struct file_operations ftrace_pid_fops = {
4735         .open           = ftrace_pid_open,
4736         .write          = ftrace_pid_write,
4737         .read           = seq_read,
4738         .llseek         = tracing_lseek,
4739         .release        = ftrace_pid_release,
4740 };
4741
4742 static __init int ftrace_init_debugfs(void)
4743 {
4744         struct dentry *d_tracer;
4745
4746         d_tracer = tracing_init_dentry();
4747         if (!d_tracer)
4748                 return 0;
4749
4750         ftrace_init_dyn_debugfs(d_tracer);
4751
4752         trace_create_file("set_ftrace_pid", 0644, d_tracer,
4753                             NULL, &ftrace_pid_fops);
4754
4755         ftrace_profile_debugfs(d_tracer);
4756
4757         return 0;
4758 }
4759 fs_initcall(ftrace_init_debugfs);
4760
4761 /**
4762  * ftrace_kill - kill ftrace
4763  *
4764  * This function should be used by panic code. It stops ftrace
4765  * but in a not so nice way. If you need to simply kill ftrace
4766  * from a non-atomic section, use ftrace_kill.
4767  */
4768 void ftrace_kill(void)
4769 {
4770         ftrace_disabled = 1;
4771         ftrace_enabled = 0;
4772         clear_ftrace_function();
4773 }
4774
4775 /**
4776  * Test if ftrace is dead or not.
4777  */
4778 int ftrace_is_dead(void)
4779 {
4780         return ftrace_disabled;
4781 }
4782
4783 /**
4784  * register_ftrace_function - register a function for profiling
4785  * @ops - ops structure that holds the function for profiling.
4786  *
4787  * Register a function to be called by all functions in the
4788  * kernel.
4789  *
4790  * Note: @ops->func and all the functions it calls must be labeled
4791  *       with "notrace", otherwise it will go into a
4792  *       recursive loop.
4793  */
4794 int register_ftrace_function(struct ftrace_ops *ops)
4795 {
4796         int ret = -1;
4797
4798         ftrace_ops_init(ops);
4799
4800         mutex_lock(&ftrace_lock);
4801
4802         ret = ftrace_startup(ops, 0);
4803
4804         mutex_unlock(&ftrace_lock);
4805
4806         return ret;
4807 }
4808 EXPORT_SYMBOL_GPL(register_ftrace_function);
4809
4810 /**
4811  * unregister_ftrace_function - unregister a function for profiling.
4812  * @ops - ops structure that holds the function to unregister
4813  *
4814  * Unregister a function that was added to be called by ftrace profiling.
4815  */
4816 int unregister_ftrace_function(struct ftrace_ops *ops)
4817 {
4818         int ret;
4819
4820         mutex_lock(&ftrace_lock);
4821         ret = ftrace_shutdown(ops, 0);
4822         mutex_unlock(&ftrace_lock);
4823
4824         return ret;
4825 }
4826 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
4827
4828 int
4829 ftrace_enable_sysctl(struct ctl_table *table, int write,
4830                      void __user *buffer, size_t *lenp,
4831                      loff_t *ppos)
4832 {
4833         int ret = -ENODEV;
4834
4835         mutex_lock(&ftrace_lock);
4836
4837         if (unlikely(ftrace_disabled))
4838                 goto out;
4839
4840         ret = proc_dointvec(table, write, buffer, lenp, ppos);
4841
4842         if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
4843                 goto out;
4844
4845         last_ftrace_enabled = !!ftrace_enabled;
4846
4847         if (ftrace_enabled) {
4848
4849                 ftrace_startup_sysctl();
4850
4851                 /* we are starting ftrace again */
4852                 if (ftrace_ops_list != &ftrace_list_end)
4853                         update_ftrace_function();
4854
4855         } else {
4856                 /* stopping ftrace calls (just send to ftrace_stub) */
4857                 ftrace_trace_function = ftrace_stub;
4858
4859                 ftrace_shutdown_sysctl();
4860         }
4861
4862  out:
4863         mutex_unlock(&ftrace_lock);
4864         return ret;
4865 }
4866
4867 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4868
4869 static int ftrace_graph_active;
4870
4871 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4872 {
4873         return 0;
4874 }
4875
4876 /* The callbacks that hook a function */
4877 trace_func_graph_ret_t ftrace_graph_return =
4878                         (trace_func_graph_ret_t)ftrace_stub;
4879 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
4880 static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
4881
4882 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4883 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4884 {
4885         int i;
4886         int ret = 0;
4887         unsigned long flags;
4888         int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4889         struct task_struct *g, *t;
4890
4891         for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4892                 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4893                                         * sizeof(struct ftrace_ret_stack),
4894                                         GFP_KERNEL);
4895                 if (!ret_stack_list[i]) {
4896                         start = 0;
4897                         end = i;
4898                         ret = -ENOMEM;
4899                         goto free;
4900                 }
4901         }
4902
4903         read_lock_irqsave(&tasklist_lock, flags);
4904         do_each_thread(g, t) {
4905                 if (start == end) {
4906                         ret = -EAGAIN;
4907                         goto unlock;
4908                 }
4909
4910                 if (t->ret_stack == NULL) {
4911                         atomic_set(&t->tracing_graph_pause, 0);
4912                         atomic_set(&t->trace_overrun, 0);
4913                         t->curr_ret_stack = -1;
4914                         /* Make sure the tasks see the -1 first: */
4915                         smp_wmb();
4916                         t->ret_stack = ret_stack_list[start++];
4917                 }
4918         } while_each_thread(g, t);
4919
4920 unlock:
4921         read_unlock_irqrestore(&tasklist_lock, flags);
4922 free:
4923         for (i = start; i < end; i++)
4924                 kfree(ret_stack_list[i]);
4925         return ret;
4926 }
4927
4928 static void
4929 ftrace_graph_probe_sched_switch(void *ignore,
4930                         struct task_struct *prev, struct task_struct *next)
4931 {
4932         unsigned long long timestamp;
4933         int index;
4934
4935         /*
4936          * Does the user want to count the time a function was asleep.
4937          * If so, do not update the time stamps.
4938          */
4939         if (trace_flags & TRACE_ITER_SLEEP_TIME)
4940                 return;
4941
4942         timestamp = trace_clock_local();
4943
4944         prev->ftrace_timestamp = timestamp;
4945
4946         /* only process tasks that we timestamped */
4947         if (!next->ftrace_timestamp)
4948                 return;
4949
4950         /*
4951          * Update all the counters in next to make up for the
4952          * time next was sleeping.
4953          */
4954         timestamp -= next->ftrace_timestamp;
4955
4956         for (index = next->curr_ret_stack; index >= 0; index--)
4957                 next->ret_stack[index].calltime += timestamp;
4958 }
4959
4960 /* Allocate a return stack for each task */
4961 static int start_graph_tracing(void)
4962 {
4963         struct ftrace_ret_stack **ret_stack_list;
4964         int ret, cpu;
4965
4966         ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4967                                 sizeof(struct ftrace_ret_stack *),
4968                                 GFP_KERNEL);
4969
4970         if (!ret_stack_list)
4971                 return -ENOMEM;
4972
4973         /* The cpu_boot init_task->ret_stack will never be freed */
4974         for_each_online_cpu(cpu) {
4975                 if (!idle_task(cpu)->ret_stack)
4976                         ftrace_graph_init_idle_task(idle_task(cpu), cpu);
4977         }
4978
4979         do {
4980                 ret = alloc_retstack_tasklist(ret_stack_list);
4981         } while (ret == -EAGAIN);
4982
4983         if (!ret) {
4984                 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
4985                 if (ret)
4986                         pr_info("ftrace_graph: Couldn't activate tracepoint"
4987                                 " probe to kernel_sched_switch\n");
4988         }
4989
4990         kfree(ret_stack_list);
4991         return ret;
4992 }
4993
4994 /*
4995  * Hibernation protection.
4996  * The state of the current task is too much unstable during
4997  * suspend/restore to disk. We want to protect against that.
4998  */
4999 static int
5000 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
5001                                                         void *unused)
5002 {
5003         switch (state) {
5004         case PM_HIBERNATION_PREPARE:
5005                 pause_graph_tracing();
5006                 break;
5007
5008         case PM_POST_HIBERNATION:
5009                 unpause_graph_tracing();
5010                 break;
5011         }
5012         return NOTIFY_DONE;
5013 }
5014
5015 static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
5016 {
5017         if (!ftrace_ops_test(&global_ops, trace->func, NULL))
5018                 return 0;
5019         return __ftrace_graph_entry(trace);
5020 }
5021
5022 /*
5023  * The function graph tracer should only trace the functions defined
5024  * by set_ftrace_filter and set_ftrace_notrace. If another function
5025  * tracer ops is registered, the graph tracer requires testing the
5026  * function against the global ops, and not just trace any function
5027  * that any ftrace_ops registered.
5028  */
5029 static void update_function_graph_func(void)
5030 {
5031         if (ftrace_ops_list == &ftrace_list_end ||
5032             (ftrace_ops_list == &global_ops &&
5033              global_ops.next == &ftrace_list_end))
5034                 ftrace_graph_entry = __ftrace_graph_entry;
5035         else
5036                 ftrace_graph_entry = ftrace_graph_entry_test;
5037 }
5038
5039 static struct notifier_block ftrace_suspend_notifier = {
5040         .notifier_call = ftrace_suspend_notifier_call,
5041 };
5042
5043 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
5044                         trace_func_graph_ent_t entryfunc)
5045 {
5046         int ret = 0;
5047
5048         mutex_lock(&ftrace_lock);
5049
5050         /* we currently allow only one tracer registered at a time */
5051         if (ftrace_graph_active) {
5052                 ret = -EBUSY;
5053                 goto out;
5054         }
5055
5056         register_pm_notifier(&ftrace_suspend_notifier);
5057
5058         ftrace_graph_active++;
5059         ret = start_graph_tracing();
5060         if (ret) {
5061                 ftrace_graph_active--;
5062                 goto out;
5063         }
5064
5065         ftrace_graph_return = retfunc;
5066
5067         /*
5068          * Update the indirect function to the entryfunc, and the
5069          * function that gets called to the entry_test first. Then
5070          * call the update fgraph entry function to determine if
5071          * the entryfunc should be called directly or not.
5072          */
5073         __ftrace_graph_entry = entryfunc;
5074         ftrace_graph_entry = ftrace_graph_entry_test;
5075         update_function_graph_func();
5076
5077         /* Function graph doesn't use the .func field of global_ops */
5078         global_ops.flags |= FTRACE_OPS_FL_STUB;
5079
5080         ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
5081
5082 out:
5083         mutex_unlock(&ftrace_lock);
5084         return ret;
5085 }
5086
5087 void unregister_ftrace_graph(void)
5088 {
5089         mutex_lock(&ftrace_lock);
5090
5091         if (unlikely(!ftrace_graph_active))
5092                 goto out;
5093
5094         ftrace_graph_active--;
5095         ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
5096         ftrace_graph_entry = ftrace_graph_entry_stub;
5097         __ftrace_graph_entry = ftrace_graph_entry_stub;
5098         ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
5099         global_ops.flags &= ~FTRACE_OPS_FL_STUB;
5100         unregister_pm_notifier(&ftrace_suspend_notifier);
5101         unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
5102
5103  out:
5104         mutex_unlock(&ftrace_lock);
5105 }
5106
5107 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
5108
5109 static void
5110 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
5111 {
5112         atomic_set(&t->tracing_graph_pause, 0);
5113         atomic_set(&t->trace_overrun, 0);
5114         t->ftrace_timestamp = 0;
5115         /* make curr_ret_stack visible before we add the ret_stack */
5116         smp_wmb();
5117         t->ret_stack = ret_stack;
5118 }
5119
5120 /*
5121  * Allocate a return stack for the idle task. May be the first
5122  * time through, or it may be done by CPU hotplug online.
5123  */
5124 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
5125 {
5126         t->curr_ret_stack = -1;
5127         /*
5128          * The idle task has no parent, it either has its own
5129          * stack or no stack at all.
5130          */
5131         if (t->ret_stack)
5132                 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
5133
5134         if (ftrace_graph_active) {
5135                 struct ftrace_ret_stack *ret_stack;
5136
5137                 ret_stack = per_cpu(idle_ret_stack, cpu);
5138                 if (!ret_stack) {
5139                         ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5140                                             * sizeof(struct ftrace_ret_stack),
5141                                             GFP_KERNEL);
5142                         if (!ret_stack)
5143                                 return;
5144                         per_cpu(idle_ret_stack, cpu) = ret_stack;
5145                 }
5146                 graph_init_task(t, ret_stack);
5147         }
5148 }
5149
5150 /* Allocate a return stack for newly created task */
5151 void ftrace_graph_init_task(struct task_struct *t)
5152 {
5153         /* Make sure we do not use the parent ret_stack */
5154         t->ret_stack = NULL;
5155         t->curr_ret_stack = -1;
5156
5157         if (ftrace_graph_active) {
5158                 struct ftrace_ret_stack *ret_stack;
5159
5160                 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5161                                 * sizeof(struct ftrace_ret_stack),
5162                                 GFP_KERNEL);
5163                 if (!ret_stack)
5164                         return;
5165                 graph_init_task(t, ret_stack);
5166         }
5167 }
5168
5169 void ftrace_graph_exit_task(struct task_struct *t)
5170 {
5171         struct ftrace_ret_stack *ret_stack = t->ret_stack;
5172
5173         t->ret_stack = NULL;
5174         /* NULL must become visible to IRQs before we free it: */
5175         barrier();
5176
5177         kfree(ret_stack);
5178 }
5179
5180 void ftrace_graph_stop(void)
5181 {
5182         ftrace_stop();
5183 }
5184 #endif