GNU Linux-libre 4.19.281-gnu1
[releases.git] / kernel / trace / ftrace.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Infrastructure for profiling code inserted by 'gcc -pg'.
4  *
5  * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
6  * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
7  *
8  * Originally ported from the -rt patch by:
9  *   Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
10  *
11  * Based on code in the latency_tracer, that is:
12  *
13  *  Copyright (C) 2004-2006 Ingo Molnar
14  *  Copyright (C) 2004 Nadia Yvette Chambers
15  */
16
17 #include <linux/stop_machine.h>
18 #include <linux/clocksource.h>
19 #include <linux/sched/task.h>
20 #include <linux/kallsyms.h>
21 #include <linux/seq_file.h>
22 #include <linux/suspend.h>
23 #include <linux/tracefs.h>
24 #include <linux/hardirq.h>
25 #include <linux/kthread.h>
26 #include <linux/uaccess.h>
27 #include <linux/bsearch.h>
28 #include <linux/module.h>
29 #include <linux/ftrace.h>
30 #include <linux/sysctl.h>
31 #include <linux/slab.h>
32 #include <linux/ctype.h>
33 #include <linux/sort.h>
34 #include <linux/list.h>
35 #include <linux/hash.h>
36 #include <linux/rcupdate.h>
37 #include <linux/kprobes.h>
38
39 #include <trace/events/sched.h>
40
41 #include <asm/sections.h>
42 #include <asm/setup.h>
43
44 #include "trace_output.h"
45 #include "trace_stat.h"
46
47 #define FTRACE_WARN_ON(cond)                    \
48         ({                                      \
49                 int ___r = cond;                \
50                 if (WARN_ON(___r))              \
51                         ftrace_kill();          \
52                 ___r;                           \
53         })
54
55 #define FTRACE_WARN_ON_ONCE(cond)               \
56         ({                                      \
57                 int ___r = cond;                \
58                 if (WARN_ON_ONCE(___r))         \
59                         ftrace_kill();          \
60                 ___r;                           \
61         })
62
63 /* hash bits for specific function selection */
64 #define FTRACE_HASH_BITS 7
65 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
66 #define FTRACE_HASH_DEFAULT_BITS 10
67 #define FTRACE_HASH_MAX_BITS 12
68
69 #ifdef CONFIG_DYNAMIC_FTRACE
70 #define INIT_OPS_HASH(opsname)  \
71         .func_hash              = &opsname.local_hash,                  \
72         .local_hash.regex_lock  = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
73 #define ASSIGN_OPS_HASH(opsname, val) \
74         .func_hash              = val, \
75         .local_hash.regex_lock  = __MUTEX_INITIALIZER(opsname.local_hash.regex_lock),
76 #else
77 #define INIT_OPS_HASH(opsname)
78 #define ASSIGN_OPS_HASH(opsname, val)
79 #endif
80
81 static struct ftrace_ops ftrace_list_end __read_mostly = {
82         .func           = ftrace_stub,
83         .flags          = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
84         INIT_OPS_HASH(ftrace_list_end)
85 };
86
87 /* ftrace_enabled is a method to turn ftrace on or off */
88 int ftrace_enabled __read_mostly;
89 static int last_ftrace_enabled;
90
91 /* Current function tracing op */
92 struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
93 /* What to set function_trace_op to */
94 static struct ftrace_ops *set_function_trace_op;
95
96 static bool ftrace_pids_enabled(struct ftrace_ops *ops)
97 {
98         struct trace_array *tr;
99
100         if (!(ops->flags & FTRACE_OPS_FL_PID) || !ops->private)
101                 return false;
102
103         tr = ops->private;
104
105         return tr->function_pids != NULL;
106 }
107
108 static void ftrace_update_trampoline(struct ftrace_ops *ops);
109
110 /*
111  * ftrace_disabled is set when an anomaly is discovered.
112  * ftrace_disabled is much stronger than ftrace_enabled.
113  */
114 static int ftrace_disabled __read_mostly;
115
116 static DEFINE_MUTEX(ftrace_lock);
117
118 static struct ftrace_ops __rcu *ftrace_ops_list __read_mostly = &ftrace_list_end;
119 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
120 static struct ftrace_ops global_ops;
121
122 #if ARCH_SUPPORTS_FTRACE_OPS
123 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
124                                  struct ftrace_ops *op, struct pt_regs *regs);
125 #else
126 /* See comment below, where ftrace_ops_list_func is defined */
127 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
128 #define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
129 #endif
130
131 /*
132  * Traverse the ftrace_global_list, invoking all entries.  The reason that we
133  * can use rcu_dereference_raw_notrace() is that elements removed from this list
134  * are simply leaked, so there is no need to interact with a grace-period
135  * mechanism.  The rcu_dereference_raw_notrace() calls are needed to handle
136  * concurrent insertions into the ftrace_global_list.
137  *
138  * Silly Alpha and silly pointer-speculation compiler optimizations!
139  */
140 #define do_for_each_ftrace_op(op, list)                 \
141         op = rcu_dereference_raw_notrace(list);                 \
142         do
143
144 /*
145  * Optimized for just a single item in the list (as that is the normal case).
146  */
147 #define while_for_each_ftrace_op(op)                            \
148         while (likely(op = rcu_dereference_raw_notrace((op)->next)) &&  \
149                unlikely((op) != &ftrace_list_end))
150
151 static inline void ftrace_ops_init(struct ftrace_ops *ops)
152 {
153 #ifdef CONFIG_DYNAMIC_FTRACE
154         if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
155                 mutex_init(&ops->local_hash.regex_lock);
156                 ops->func_hash = &ops->local_hash;
157                 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
158         }
159 #endif
160 }
161
162 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
163                             struct ftrace_ops *op, struct pt_regs *regs)
164 {
165         struct trace_array *tr = op->private;
166
167         if (tr && this_cpu_read(tr->trace_buffer.data->ftrace_ignore_pid))
168                 return;
169
170         op->saved_func(ip, parent_ip, op, regs);
171 }
172
173 static void ftrace_sync(struct work_struct *work)
174 {
175         /*
176          * This function is just a stub to implement a hard force
177          * of synchronize_sched(). This requires synchronizing
178          * tasks even in userspace and idle.
179          *
180          * Yes, function tracing is rude.
181          */
182 }
183
184 static void ftrace_sync_ipi(void *data)
185 {
186         /* Probably not needed, but do it anyway */
187         smp_rmb();
188 }
189
190 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
191 static void update_function_graph_func(void);
192
193 /* Both enabled by default (can be cleared by function_graph tracer flags */
194 static bool fgraph_sleep_time = true;
195 static bool fgraph_graph_time = true;
196
197 #else
198 static inline void update_function_graph_func(void) { }
199 #endif
200
201
202 static ftrace_func_t ftrace_ops_get_list_func(struct ftrace_ops *ops)
203 {
204         /*
205          * If this is a dynamic, RCU, or per CPU ops, or we force list func,
206          * then it needs to call the list anyway.
207          */
208         if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_RCU) ||
209             FTRACE_FORCE_LIST_FUNC)
210                 return ftrace_ops_list_func;
211
212         return ftrace_ops_get_func(ops);
213 }
214
215 static void update_ftrace_function(void)
216 {
217         ftrace_func_t func;
218
219         /*
220          * Prepare the ftrace_ops that the arch callback will use.
221          * If there's only one ftrace_ops registered, the ftrace_ops_list
222          * will point to the ops we want.
223          */
224         set_function_trace_op = rcu_dereference_protected(ftrace_ops_list,
225                                                 lockdep_is_held(&ftrace_lock));
226
227         /* If there's no ftrace_ops registered, just call the stub function */
228         if (set_function_trace_op == &ftrace_list_end) {
229                 func = ftrace_stub;
230
231         /*
232          * If we are at the end of the list and this ops is
233          * recursion safe and not dynamic and the arch supports passing ops,
234          * then have the mcount trampoline call the function directly.
235          */
236         } else if (rcu_dereference_protected(ftrace_ops_list->next,
237                         lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
238                 func = ftrace_ops_get_list_func(ftrace_ops_list);
239
240         } else {
241                 /* Just use the default ftrace_ops */
242                 set_function_trace_op = &ftrace_list_end;
243                 func = ftrace_ops_list_func;
244         }
245
246         update_function_graph_func();
247
248         /* If there's no change, then do nothing more here */
249         if (ftrace_trace_function == func)
250                 return;
251
252         /*
253          * If we are using the list function, it doesn't care
254          * about the function_trace_ops.
255          */
256         if (func == ftrace_ops_list_func) {
257                 ftrace_trace_function = func;
258                 /*
259                  * Don't even bother setting function_trace_ops,
260                  * it would be racy to do so anyway.
261                  */
262                 return;
263         }
264
265 #ifndef CONFIG_DYNAMIC_FTRACE
266         /*
267          * For static tracing, we need to be a bit more careful.
268          * The function change takes affect immediately. Thus,
269          * we need to coorditate the setting of the function_trace_ops
270          * with the setting of the ftrace_trace_function.
271          *
272          * Set the function to the list ops, which will call the
273          * function we want, albeit indirectly, but it handles the
274          * ftrace_ops and doesn't depend on function_trace_op.
275          */
276         ftrace_trace_function = ftrace_ops_list_func;
277         /*
278          * Make sure all CPUs see this. Yes this is slow, but static
279          * tracing is slow and nasty to have enabled.
280          */
281         schedule_on_each_cpu(ftrace_sync);
282         /* Now all cpus are using the list ops. */
283         function_trace_op = set_function_trace_op;
284         /* Make sure the function_trace_op is visible on all CPUs */
285         smp_wmb();
286         /* Nasty way to force a rmb on all cpus */
287         smp_call_function(ftrace_sync_ipi, NULL, 1);
288         /* OK, we are all set to update the ftrace_trace_function now! */
289 #endif /* !CONFIG_DYNAMIC_FTRACE */
290
291         ftrace_trace_function = func;
292 }
293
294 static void add_ftrace_ops(struct ftrace_ops __rcu **list,
295                            struct ftrace_ops *ops)
296 {
297         rcu_assign_pointer(ops->next, *list);
298
299         /*
300          * We are entering ops into the list but another
301          * CPU might be walking that list. We need to make sure
302          * the ops->next pointer is valid before another CPU sees
303          * the ops pointer included into the list.
304          */
305         rcu_assign_pointer(*list, ops);
306 }
307
308 static int remove_ftrace_ops(struct ftrace_ops __rcu **list,
309                              struct ftrace_ops *ops)
310 {
311         struct ftrace_ops **p;
312
313         /*
314          * If we are removing the last function, then simply point
315          * to the ftrace_stub.
316          */
317         if (rcu_dereference_protected(*list,
318                         lockdep_is_held(&ftrace_lock)) == ops &&
319             rcu_dereference_protected(ops->next,
320                         lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
321                 *list = &ftrace_list_end;
322                 return 0;
323         }
324
325         for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
326                 if (*p == ops)
327                         break;
328
329         if (*p != ops)
330                 return -1;
331
332         *p = (*p)->next;
333         return 0;
334 }
335
336 static void ftrace_update_trampoline(struct ftrace_ops *ops);
337
338 static int __register_ftrace_function(struct ftrace_ops *ops)
339 {
340         if (ops->flags & FTRACE_OPS_FL_DELETED)
341                 return -EINVAL;
342
343         if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
344                 return -EBUSY;
345
346 #ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
347         /*
348          * If the ftrace_ops specifies SAVE_REGS, then it only can be used
349          * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
350          * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
351          */
352         if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
353             !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
354                 return -EINVAL;
355
356         if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
357                 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
358 #endif
359
360         if (!core_kernel_data((unsigned long)ops))
361                 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
362
363         add_ftrace_ops(&ftrace_ops_list, ops);
364
365         /* Always save the function, and reset at unregistering */
366         ops->saved_func = ops->func;
367
368         if (ftrace_pids_enabled(ops))
369                 ops->func = ftrace_pid_func;
370
371         ftrace_update_trampoline(ops);
372
373         if (ftrace_enabled)
374                 update_ftrace_function();
375
376         return 0;
377 }
378
379 static int __unregister_ftrace_function(struct ftrace_ops *ops)
380 {
381         int ret;
382
383         if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
384                 return -EBUSY;
385
386         ret = remove_ftrace_ops(&ftrace_ops_list, ops);
387
388         if (ret < 0)
389                 return ret;
390
391         if (ftrace_enabled)
392                 update_ftrace_function();
393
394         ops->func = ops->saved_func;
395
396         return 0;
397 }
398
399 static void ftrace_update_pid_func(void)
400 {
401         struct ftrace_ops *op;
402
403         /* Only do something if we are tracing something */
404         if (ftrace_trace_function == ftrace_stub)
405                 return;
406
407         do_for_each_ftrace_op(op, ftrace_ops_list) {
408                 if (op->flags & FTRACE_OPS_FL_PID) {
409                         op->func = ftrace_pids_enabled(op) ?
410                                 ftrace_pid_func : op->saved_func;
411                         ftrace_update_trampoline(op);
412                 }
413         } while_for_each_ftrace_op(op);
414
415         update_ftrace_function();
416 }
417
418 #ifdef CONFIG_FUNCTION_PROFILER
419 struct ftrace_profile {
420         struct hlist_node               node;
421         unsigned long                   ip;
422         unsigned long                   counter;
423 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
424         unsigned long long              time;
425         unsigned long long              time_squared;
426 #endif
427 };
428
429 struct ftrace_profile_page {
430         struct ftrace_profile_page      *next;
431         unsigned long                   index;
432         struct ftrace_profile           records[];
433 };
434
435 struct ftrace_profile_stat {
436         atomic_t                        disabled;
437         struct hlist_head               *hash;
438         struct ftrace_profile_page      *pages;
439         struct ftrace_profile_page      *start;
440         struct tracer_stat              stat;
441 };
442
443 #define PROFILE_RECORDS_SIZE                                            \
444         (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
445
446 #define PROFILES_PER_PAGE                                       \
447         (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
448
449 static int ftrace_profile_enabled __read_mostly;
450
451 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
452 static DEFINE_MUTEX(ftrace_profile_lock);
453
454 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
455
456 #define FTRACE_PROFILE_HASH_BITS 10
457 #define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
458
459 static void *
460 function_stat_next(void *v, int idx)
461 {
462         struct ftrace_profile *rec = v;
463         struct ftrace_profile_page *pg;
464
465         pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
466
467  again:
468         if (idx != 0)
469                 rec++;
470
471         if ((void *)rec >= (void *)&pg->records[pg->index]) {
472                 pg = pg->next;
473                 if (!pg)
474                         return NULL;
475                 rec = &pg->records[0];
476                 if (!rec->counter)
477                         goto again;
478         }
479
480         return rec;
481 }
482
483 static void *function_stat_start(struct tracer_stat *trace)
484 {
485         struct ftrace_profile_stat *stat =
486                 container_of(trace, struct ftrace_profile_stat, stat);
487
488         if (!stat || !stat->start)
489                 return NULL;
490
491         return function_stat_next(&stat->start->records[0], 0);
492 }
493
494 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
495 /* function graph compares on total time */
496 static int function_stat_cmp(void *p1, void *p2)
497 {
498         struct ftrace_profile *a = p1;
499         struct ftrace_profile *b = p2;
500
501         if (a->time < b->time)
502                 return -1;
503         if (a->time > b->time)
504                 return 1;
505         else
506                 return 0;
507 }
508 #else
509 /* not function graph compares against hits */
510 static int function_stat_cmp(void *p1, void *p2)
511 {
512         struct ftrace_profile *a = p1;
513         struct ftrace_profile *b = p2;
514
515         if (a->counter < b->counter)
516                 return -1;
517         if (a->counter > b->counter)
518                 return 1;
519         else
520                 return 0;
521 }
522 #endif
523
524 static int function_stat_headers(struct seq_file *m)
525 {
526 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
527         seq_puts(m, "  Function                               "
528                  "Hit    Time            Avg             s^2\n"
529                     "  --------                               "
530                  "---    ----            ---             ---\n");
531 #else
532         seq_puts(m, "  Function                               Hit\n"
533                     "  --------                               ---\n");
534 #endif
535         return 0;
536 }
537
538 static int function_stat_show(struct seq_file *m, void *v)
539 {
540         struct ftrace_profile *rec = v;
541         char str[KSYM_SYMBOL_LEN];
542         int ret = 0;
543 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
544         static struct trace_seq s;
545         unsigned long long avg;
546         unsigned long long stddev;
547 #endif
548         mutex_lock(&ftrace_profile_lock);
549
550         /* we raced with function_profile_reset() */
551         if (unlikely(rec->counter == 0)) {
552                 ret = -EBUSY;
553                 goto out;
554         }
555
556 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
557         avg = div64_ul(rec->time, rec->counter);
558         if (tracing_thresh && (avg < tracing_thresh))
559                 goto out;
560 #endif
561
562         kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
563         seq_printf(m, "  %-30.30s  %10lu", str, rec->counter);
564
565 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
566         seq_puts(m, "    ");
567
568         /* Sample standard deviation (s^2) */
569         if (rec->counter <= 1)
570                 stddev = 0;
571         else {
572                 /*
573                  * Apply Welford's method:
574                  * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
575                  */
576                 stddev = rec->counter * rec->time_squared -
577                          rec->time * rec->time;
578
579                 /*
580                  * Divide only 1000 for ns^2 -> us^2 conversion.
581                  * trace_print_graph_duration will divide 1000 again.
582                  */
583                 stddev = div64_ul(stddev,
584                                   rec->counter * (rec->counter - 1) * 1000);
585         }
586
587         trace_seq_init(&s);
588         trace_print_graph_duration(rec->time, &s);
589         trace_seq_puts(&s, "    ");
590         trace_print_graph_duration(avg, &s);
591         trace_seq_puts(&s, "    ");
592         trace_print_graph_duration(stddev, &s);
593         trace_print_seq(m, &s);
594 #endif
595         seq_putc(m, '\n');
596 out:
597         mutex_unlock(&ftrace_profile_lock);
598
599         return ret;
600 }
601
602 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
603 {
604         struct ftrace_profile_page *pg;
605
606         pg = stat->pages = stat->start;
607
608         while (pg) {
609                 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
610                 pg->index = 0;
611                 pg = pg->next;
612         }
613
614         memset(stat->hash, 0,
615                FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
616 }
617
618 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
619 {
620         struct ftrace_profile_page *pg;
621         int functions;
622         int pages;
623         int i;
624
625         /* If we already allocated, do nothing */
626         if (stat->pages)
627                 return 0;
628
629         stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
630         if (!stat->pages)
631                 return -ENOMEM;
632
633 #ifdef CONFIG_DYNAMIC_FTRACE
634         functions = ftrace_update_tot_cnt;
635 #else
636         /*
637          * We do not know the number of functions that exist because
638          * dynamic tracing is what counts them. With past experience
639          * we have around 20K functions. That should be more than enough.
640          * It is highly unlikely we will execute every function in
641          * the kernel.
642          */
643         functions = 20000;
644 #endif
645
646         pg = stat->start = stat->pages;
647
648         pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
649
650         for (i = 1; i < pages; i++) {
651                 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
652                 if (!pg->next)
653                         goto out_free;
654                 pg = pg->next;
655         }
656
657         return 0;
658
659  out_free:
660         pg = stat->start;
661         while (pg) {
662                 unsigned long tmp = (unsigned long)pg;
663
664                 pg = pg->next;
665                 free_page(tmp);
666         }
667
668         stat->pages = NULL;
669         stat->start = NULL;
670
671         return -ENOMEM;
672 }
673
674 static int ftrace_profile_init_cpu(int cpu)
675 {
676         struct ftrace_profile_stat *stat;
677         int size;
678
679         stat = &per_cpu(ftrace_profile_stats, cpu);
680
681         if (stat->hash) {
682                 /* If the profile is already created, simply reset it */
683                 ftrace_profile_reset(stat);
684                 return 0;
685         }
686
687         /*
688          * We are profiling all functions, but usually only a few thousand
689          * functions are hit. We'll make a hash of 1024 items.
690          */
691         size = FTRACE_PROFILE_HASH_SIZE;
692
693         stat->hash = kcalloc(size, sizeof(struct hlist_head), GFP_KERNEL);
694
695         if (!stat->hash)
696                 return -ENOMEM;
697
698         /* Preallocate the function profiling pages */
699         if (ftrace_profile_pages_init(stat) < 0) {
700                 kfree(stat->hash);
701                 stat->hash = NULL;
702                 return -ENOMEM;
703         }
704
705         return 0;
706 }
707
708 static int ftrace_profile_init(void)
709 {
710         int cpu;
711         int ret = 0;
712
713         for_each_possible_cpu(cpu) {
714                 ret = ftrace_profile_init_cpu(cpu);
715                 if (ret)
716                         break;
717         }
718
719         return ret;
720 }
721
722 /* interrupts must be disabled */
723 static struct ftrace_profile *
724 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
725 {
726         struct ftrace_profile *rec;
727         struct hlist_head *hhd;
728         unsigned long key;
729
730         key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
731         hhd = &stat->hash[key];
732
733         if (hlist_empty(hhd))
734                 return NULL;
735
736         hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
737                 if (rec->ip == ip)
738                         return rec;
739         }
740
741         return NULL;
742 }
743
744 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
745                                struct ftrace_profile *rec)
746 {
747         unsigned long key;
748
749         key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
750         hlist_add_head_rcu(&rec->node, &stat->hash[key]);
751 }
752
753 /*
754  * The memory is already allocated, this simply finds a new record to use.
755  */
756 static struct ftrace_profile *
757 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
758 {
759         struct ftrace_profile *rec = NULL;
760
761         /* prevent recursion (from NMIs) */
762         if (atomic_inc_return(&stat->disabled) != 1)
763                 goto out;
764
765         /*
766          * Try to find the function again since an NMI
767          * could have added it
768          */
769         rec = ftrace_find_profiled_func(stat, ip);
770         if (rec)
771                 goto out;
772
773         if (stat->pages->index == PROFILES_PER_PAGE) {
774                 if (!stat->pages->next)
775                         goto out;
776                 stat->pages = stat->pages->next;
777         }
778
779         rec = &stat->pages->records[stat->pages->index++];
780         rec->ip = ip;
781         ftrace_add_profile(stat, rec);
782
783  out:
784         atomic_dec(&stat->disabled);
785
786         return rec;
787 }
788
789 static void
790 function_profile_call(unsigned long ip, unsigned long parent_ip,
791                       struct ftrace_ops *ops, struct pt_regs *regs)
792 {
793         struct ftrace_profile_stat *stat;
794         struct ftrace_profile *rec;
795         unsigned long flags;
796
797         if (!ftrace_profile_enabled)
798                 return;
799
800         local_irq_save(flags);
801
802         stat = this_cpu_ptr(&ftrace_profile_stats);
803         if (!stat->hash || !ftrace_profile_enabled)
804                 goto out;
805
806         rec = ftrace_find_profiled_func(stat, ip);
807         if (!rec) {
808                 rec = ftrace_profile_alloc(stat, ip);
809                 if (!rec)
810                         goto out;
811         }
812
813         rec->counter++;
814  out:
815         local_irq_restore(flags);
816 }
817
818 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
819 static int profile_graph_entry(struct ftrace_graph_ent *trace)
820 {
821         int index = current->curr_ret_stack;
822
823         function_profile_call(trace->func, 0, NULL, NULL);
824
825         /* If function graph is shutting down, ret_stack can be NULL */
826         if (!current->ret_stack)
827                 return 0;
828
829         if (index >= 0 && index < FTRACE_RETFUNC_DEPTH)
830                 current->ret_stack[index].subtime = 0;
831
832         return 1;
833 }
834
835 static void profile_graph_return(struct ftrace_graph_ret *trace)
836 {
837         struct ftrace_profile_stat *stat;
838         unsigned long long calltime;
839         struct ftrace_profile *rec;
840         unsigned long flags;
841
842         local_irq_save(flags);
843         stat = this_cpu_ptr(&ftrace_profile_stats);
844         if (!stat->hash || !ftrace_profile_enabled)
845                 goto out;
846
847         /* If the calltime was zero'd ignore it */
848         if (!trace->calltime)
849                 goto out;
850
851         calltime = trace->rettime - trace->calltime;
852
853         if (!fgraph_graph_time) {
854                 int index;
855
856                 index = current->curr_ret_stack;
857
858                 /* Append this call time to the parent time to subtract */
859                 if (index)
860                         current->ret_stack[index - 1].subtime += calltime;
861
862                 if (current->ret_stack[index].subtime < calltime)
863                         calltime -= current->ret_stack[index].subtime;
864                 else
865                         calltime = 0;
866         }
867
868         rec = ftrace_find_profiled_func(stat, trace->func);
869         if (rec) {
870                 rec->time += calltime;
871                 rec->time_squared += calltime * calltime;
872         }
873
874  out:
875         local_irq_restore(flags);
876 }
877
878 static int register_ftrace_profiler(void)
879 {
880         return register_ftrace_graph(&profile_graph_return,
881                                      &profile_graph_entry);
882 }
883
884 static void unregister_ftrace_profiler(void)
885 {
886         unregister_ftrace_graph();
887 }
888 #else
889 static struct ftrace_ops ftrace_profile_ops __read_mostly = {
890         .func           = function_profile_call,
891         .flags          = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
892         INIT_OPS_HASH(ftrace_profile_ops)
893 };
894
895 static int register_ftrace_profiler(void)
896 {
897         return register_ftrace_function(&ftrace_profile_ops);
898 }
899
900 static void unregister_ftrace_profiler(void)
901 {
902         unregister_ftrace_function(&ftrace_profile_ops);
903 }
904 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
905
906 static ssize_t
907 ftrace_profile_write(struct file *filp, const char __user *ubuf,
908                      size_t cnt, loff_t *ppos)
909 {
910         unsigned long val;
911         int ret;
912
913         ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
914         if (ret)
915                 return ret;
916
917         val = !!val;
918
919         mutex_lock(&ftrace_profile_lock);
920         if (ftrace_profile_enabled ^ val) {
921                 if (val) {
922                         ret = ftrace_profile_init();
923                         if (ret < 0) {
924                                 cnt = ret;
925                                 goto out;
926                         }
927
928                         ret = register_ftrace_profiler();
929                         if (ret < 0) {
930                                 cnt = ret;
931                                 goto out;
932                         }
933                         ftrace_profile_enabled = 1;
934                 } else {
935                         ftrace_profile_enabled = 0;
936                         /*
937                          * unregister_ftrace_profiler calls stop_machine
938                          * so this acts like an synchronize_sched.
939                          */
940                         unregister_ftrace_profiler();
941                 }
942         }
943  out:
944         mutex_unlock(&ftrace_profile_lock);
945
946         *ppos += cnt;
947
948         return cnt;
949 }
950
951 static ssize_t
952 ftrace_profile_read(struct file *filp, char __user *ubuf,
953                      size_t cnt, loff_t *ppos)
954 {
955         char buf[64];           /* big enough to hold a number */
956         int r;
957
958         r = sprintf(buf, "%u\n", ftrace_profile_enabled);
959         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
960 }
961
962 static const struct file_operations ftrace_profile_fops = {
963         .open           = tracing_open_generic,
964         .read           = ftrace_profile_read,
965         .write          = ftrace_profile_write,
966         .llseek         = default_llseek,
967 };
968
969 /* used to initialize the real stat files */
970 static struct tracer_stat function_stats __initdata = {
971         .name           = "functions",
972         .stat_start     = function_stat_start,
973         .stat_next      = function_stat_next,
974         .stat_cmp       = function_stat_cmp,
975         .stat_headers   = function_stat_headers,
976         .stat_show      = function_stat_show
977 };
978
979 static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
980 {
981         struct ftrace_profile_stat *stat;
982         struct dentry *entry;
983         char *name;
984         int ret;
985         int cpu;
986
987         for_each_possible_cpu(cpu) {
988                 stat = &per_cpu(ftrace_profile_stats, cpu);
989
990                 name = kasprintf(GFP_KERNEL, "function%d", cpu);
991                 if (!name) {
992                         /*
993                          * The files created are permanent, if something happens
994                          * we still do not free memory.
995                          */
996                         WARN(1,
997                              "Could not allocate stat file for cpu %d\n",
998                              cpu);
999                         return;
1000                 }
1001                 stat->stat = function_stats;
1002                 stat->stat.name = name;
1003                 ret = register_stat_tracer(&stat->stat);
1004                 if (ret) {
1005                         WARN(1,
1006                              "Could not register function stat for cpu %d\n",
1007                              cpu);
1008                         kfree(name);
1009                         return;
1010                 }
1011         }
1012
1013         entry = tracefs_create_file("function_profile_enabled", 0644,
1014                                     d_tracer, NULL, &ftrace_profile_fops);
1015         if (!entry)
1016                 pr_warn("Could not create tracefs 'function_profile_enabled' entry\n");
1017 }
1018
1019 #else /* CONFIG_FUNCTION_PROFILER */
1020 static __init void ftrace_profile_tracefs(struct dentry *d_tracer)
1021 {
1022 }
1023 #endif /* CONFIG_FUNCTION_PROFILER */
1024
1025 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1026 static int ftrace_graph_active;
1027 #else
1028 # define ftrace_graph_active 0
1029 #endif
1030
1031 #ifdef CONFIG_DYNAMIC_FTRACE
1032
1033 static struct ftrace_ops *removed_ops;
1034
1035 /*
1036  * Set when doing a global update, like enabling all recs or disabling them.
1037  * It is not set when just updating a single ftrace_ops.
1038  */
1039 static bool update_all_ops;
1040
1041 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1042 # error Dynamic ftrace depends on MCOUNT_RECORD
1043 #endif
1044
1045 struct ftrace_func_entry {
1046         struct hlist_node hlist;
1047         unsigned long ip;
1048 };
1049
1050 struct ftrace_func_probe {
1051         struct ftrace_probe_ops *probe_ops;
1052         struct ftrace_ops       ops;
1053         struct trace_array      *tr;
1054         struct list_head        list;
1055         void                    *data;
1056         int                     ref;
1057 };
1058
1059 /*
1060  * We make these constant because no one should touch them,
1061  * but they are used as the default "empty hash", to avoid allocating
1062  * it all the time. These are in a read only section such that if
1063  * anyone does try to modify it, it will cause an exception.
1064  */
1065 static const struct hlist_head empty_buckets[1];
1066 static const struct ftrace_hash empty_hash = {
1067         .buckets = (struct hlist_head *)empty_buckets,
1068 };
1069 #define EMPTY_HASH      ((struct ftrace_hash *)&empty_hash)
1070
1071 static struct ftrace_ops global_ops = {
1072         .func                           = ftrace_stub,
1073         .local_hash.notrace_hash        = EMPTY_HASH,
1074         .local_hash.filter_hash         = EMPTY_HASH,
1075         INIT_OPS_HASH(global_ops)
1076         .flags                          = FTRACE_OPS_FL_RECURSION_SAFE |
1077                                           FTRACE_OPS_FL_INITIALIZED |
1078                                           FTRACE_OPS_FL_PID,
1079 };
1080
1081 /*
1082  * Used by the stack undwinder to know about dynamic ftrace trampolines.
1083  */
1084 struct ftrace_ops *ftrace_ops_trampoline(unsigned long addr)
1085 {
1086         struct ftrace_ops *op = NULL;
1087
1088         /*
1089          * Some of the ops may be dynamically allocated,
1090          * they are freed after a synchronize_sched().
1091          */
1092         preempt_disable_notrace();
1093
1094         do_for_each_ftrace_op(op, ftrace_ops_list) {
1095                 /*
1096                  * This is to check for dynamically allocated trampolines.
1097                  * Trampolines that are in kernel text will have
1098                  * core_kernel_text() return true.
1099                  */
1100                 if (op->trampoline && op->trampoline_size)
1101                         if (addr >= op->trampoline &&
1102                             addr < op->trampoline + op->trampoline_size) {
1103                                 preempt_enable_notrace();
1104                                 return op;
1105                         }
1106         } while_for_each_ftrace_op(op);
1107         preempt_enable_notrace();
1108
1109         return NULL;
1110 }
1111
1112 /*
1113  * This is used by __kernel_text_address() to return true if the
1114  * address is on a dynamically allocated trampoline that would
1115  * not return true for either core_kernel_text() or
1116  * is_module_text_address().
1117  */
1118 bool is_ftrace_trampoline(unsigned long addr)
1119 {
1120         return ftrace_ops_trampoline(addr) != NULL;
1121 }
1122
1123 struct ftrace_page {
1124         struct ftrace_page      *next;
1125         struct dyn_ftrace       *records;
1126         int                     index;
1127         int                     size;
1128 };
1129
1130 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1131 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1132
1133 /* estimate from running different kernels */
1134 #define NR_TO_INIT              10000
1135
1136 static struct ftrace_page       *ftrace_pages_start;
1137 static struct ftrace_page       *ftrace_pages;
1138
1139 static __always_inline unsigned long
1140 ftrace_hash_key(struct ftrace_hash *hash, unsigned long ip)
1141 {
1142         if (hash->size_bits > 0)
1143                 return hash_long(ip, hash->size_bits);
1144
1145         return 0;
1146 }
1147
1148 /* Only use this function if ftrace_hash_empty() has already been tested */
1149 static __always_inline struct ftrace_func_entry *
1150 __ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1151 {
1152         unsigned long key;
1153         struct ftrace_func_entry *entry;
1154         struct hlist_head *hhd;
1155
1156         key = ftrace_hash_key(hash, ip);
1157         hhd = &hash->buckets[key];
1158
1159         hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
1160                 if (entry->ip == ip)
1161                         return entry;
1162         }
1163         return NULL;
1164 }
1165
1166 /**
1167  * ftrace_lookup_ip - Test to see if an ip exists in an ftrace_hash
1168  * @hash: The hash to look at
1169  * @ip: The instruction pointer to test
1170  *
1171  * Search a given @hash to see if a given instruction pointer (@ip)
1172  * exists in it.
1173  *
1174  * Returns the entry that holds the @ip if found. NULL otherwise.
1175  */
1176 struct ftrace_func_entry *
1177 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1178 {
1179         if (ftrace_hash_empty(hash))
1180                 return NULL;
1181
1182         return __ftrace_lookup_ip(hash, ip);
1183 }
1184
1185 static void __add_hash_entry(struct ftrace_hash *hash,
1186                              struct ftrace_func_entry *entry)
1187 {
1188         struct hlist_head *hhd;
1189         unsigned long key;
1190
1191         key = ftrace_hash_key(hash, entry->ip);
1192         hhd = &hash->buckets[key];
1193         hlist_add_head(&entry->hlist, hhd);
1194         hash->count++;
1195 }
1196
1197 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1198 {
1199         struct ftrace_func_entry *entry;
1200
1201         entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1202         if (!entry)
1203                 return -ENOMEM;
1204
1205         entry->ip = ip;
1206         __add_hash_entry(hash, entry);
1207
1208         return 0;
1209 }
1210
1211 static void
1212 free_hash_entry(struct ftrace_hash *hash,
1213                   struct ftrace_func_entry *entry)
1214 {
1215         hlist_del(&entry->hlist);
1216         kfree(entry);
1217         hash->count--;
1218 }
1219
1220 static void
1221 remove_hash_entry(struct ftrace_hash *hash,
1222                   struct ftrace_func_entry *entry)
1223 {
1224         hlist_del_rcu(&entry->hlist);
1225         hash->count--;
1226 }
1227
1228 static void ftrace_hash_clear(struct ftrace_hash *hash)
1229 {
1230         struct hlist_head *hhd;
1231         struct hlist_node *tn;
1232         struct ftrace_func_entry *entry;
1233         int size = 1 << hash->size_bits;
1234         int i;
1235
1236         if (!hash->count)
1237                 return;
1238
1239         for (i = 0; i < size; i++) {
1240                 hhd = &hash->buckets[i];
1241                 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
1242                         free_hash_entry(hash, entry);
1243         }
1244         FTRACE_WARN_ON(hash->count);
1245 }
1246
1247 static void free_ftrace_mod(struct ftrace_mod_load *ftrace_mod)
1248 {
1249         list_del(&ftrace_mod->list);
1250         kfree(ftrace_mod->module);
1251         kfree(ftrace_mod->func);
1252         kfree(ftrace_mod);
1253 }
1254
1255 static void clear_ftrace_mod_list(struct list_head *head)
1256 {
1257         struct ftrace_mod_load *p, *n;
1258
1259         /* stack tracer isn't supported yet */
1260         if (!head)
1261                 return;
1262
1263         mutex_lock(&ftrace_lock);
1264         list_for_each_entry_safe(p, n, head, list)
1265                 free_ftrace_mod(p);
1266         mutex_unlock(&ftrace_lock);
1267 }
1268
1269 static void free_ftrace_hash(struct ftrace_hash *hash)
1270 {
1271         if (!hash || hash == EMPTY_HASH)
1272                 return;
1273         ftrace_hash_clear(hash);
1274         kfree(hash->buckets);
1275         kfree(hash);
1276 }
1277
1278 static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1279 {
1280         struct ftrace_hash *hash;
1281
1282         hash = container_of(rcu, struct ftrace_hash, rcu);
1283         free_ftrace_hash(hash);
1284 }
1285
1286 static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1287 {
1288         if (!hash || hash == EMPTY_HASH)
1289                 return;
1290         call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1291 }
1292
1293 void ftrace_free_filter(struct ftrace_ops *ops)
1294 {
1295         ftrace_ops_init(ops);
1296         free_ftrace_hash(ops->func_hash->filter_hash);
1297         free_ftrace_hash(ops->func_hash->notrace_hash);
1298 }
1299
1300 static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1301 {
1302         struct ftrace_hash *hash;
1303         int size;
1304
1305         hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1306         if (!hash)
1307                 return NULL;
1308
1309         size = 1 << size_bits;
1310         hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
1311
1312         if (!hash->buckets) {
1313                 kfree(hash);
1314                 return NULL;
1315         }
1316
1317         hash->size_bits = size_bits;
1318
1319         return hash;
1320 }
1321
1322
1323 static int ftrace_add_mod(struct trace_array *tr,
1324                           const char *func, const char *module,
1325                           int enable)
1326 {
1327         struct ftrace_mod_load *ftrace_mod;
1328         struct list_head *mod_head = enable ? &tr->mod_trace : &tr->mod_notrace;
1329
1330         ftrace_mod = kzalloc(sizeof(*ftrace_mod), GFP_KERNEL);
1331         if (!ftrace_mod)
1332                 return -ENOMEM;
1333
1334         INIT_LIST_HEAD(&ftrace_mod->list);
1335         ftrace_mod->func = kstrdup(func, GFP_KERNEL);
1336         ftrace_mod->module = kstrdup(module, GFP_KERNEL);
1337         ftrace_mod->enable = enable;
1338
1339         if (!ftrace_mod->func || !ftrace_mod->module)
1340                 goto out_free;
1341
1342         list_add(&ftrace_mod->list, mod_head);
1343
1344         return 0;
1345
1346  out_free:
1347         free_ftrace_mod(ftrace_mod);
1348
1349         return -ENOMEM;
1350 }
1351
1352 static struct ftrace_hash *
1353 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1354 {
1355         struct ftrace_func_entry *entry;
1356         struct ftrace_hash *new_hash;
1357         int size;
1358         int ret;
1359         int i;
1360
1361         new_hash = alloc_ftrace_hash(size_bits);
1362         if (!new_hash)
1363                 return NULL;
1364
1365         if (hash)
1366                 new_hash->flags = hash->flags;
1367
1368         /* Empty hash? */
1369         if (ftrace_hash_empty(hash))
1370                 return new_hash;
1371
1372         size = 1 << hash->size_bits;
1373         for (i = 0; i < size; i++) {
1374                 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
1375                         ret = add_hash_entry(new_hash, entry->ip);
1376                         if (ret < 0)
1377                                 goto free_hash;
1378                 }
1379         }
1380
1381         FTRACE_WARN_ON(new_hash->count != hash->count);
1382
1383         return new_hash;
1384
1385  free_hash:
1386         free_ftrace_hash(new_hash);
1387         return NULL;
1388 }
1389
1390 static void
1391 ftrace_hash_rec_disable_modify(struct ftrace_ops *ops, int filter_hash);
1392 static void
1393 ftrace_hash_rec_enable_modify(struct ftrace_ops *ops, int filter_hash);
1394
1395 static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1396                                        struct ftrace_hash *new_hash);
1397
1398 static struct ftrace_hash *
1399 __ftrace_hash_move(struct ftrace_hash *src)
1400 {
1401         struct ftrace_func_entry *entry;
1402         struct hlist_node *tn;
1403         struct hlist_head *hhd;
1404         struct ftrace_hash *new_hash;
1405         int size = src->count;
1406         int bits = 0;
1407         int i;
1408
1409         /*
1410          * If the new source is empty, just return the empty_hash.
1411          */
1412         if (ftrace_hash_empty(src))
1413                 return EMPTY_HASH;
1414
1415         /*
1416          * Make the hash size about 1/2 the # found
1417          */
1418         for (size /= 2; size; size >>= 1)
1419                 bits++;
1420
1421         /* Don't allocate too much */
1422         if (bits > FTRACE_HASH_MAX_BITS)
1423                 bits = FTRACE_HASH_MAX_BITS;
1424
1425         new_hash = alloc_ftrace_hash(bits);
1426         if (!new_hash)
1427                 return NULL;
1428
1429         new_hash->flags = src->flags;
1430
1431         size = 1 << src->size_bits;
1432         for (i = 0; i < size; i++) {
1433                 hhd = &src->buckets[i];
1434                 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
1435                         remove_hash_entry(src, entry);
1436                         __add_hash_entry(new_hash, entry);
1437                 }
1438         }
1439
1440         return new_hash;
1441 }
1442
1443 static int
1444 ftrace_hash_move(struct ftrace_ops *ops, int enable,
1445                  struct ftrace_hash **dst, struct ftrace_hash *src)
1446 {
1447         struct ftrace_hash *new_hash;
1448         int ret;
1449
1450         /* Reject setting notrace hash on IPMODIFY ftrace_ops */
1451         if (ops->flags & FTRACE_OPS_FL_IPMODIFY && !enable)
1452                 return -EINVAL;
1453
1454         new_hash = __ftrace_hash_move(src);
1455         if (!new_hash)
1456                 return -ENOMEM;
1457
1458         /* Make sure this can be applied if it is IPMODIFY ftrace_ops */
1459         if (enable) {
1460                 /* IPMODIFY should be updated only when filter_hash updating */
1461                 ret = ftrace_hash_ipmodify_update(ops, new_hash);
1462                 if (ret < 0) {
1463                         free_ftrace_hash(new_hash);
1464                         return ret;
1465                 }
1466         }
1467
1468         /*
1469          * Remove the current set, update the hash and add
1470          * them back.
1471          */
1472         ftrace_hash_rec_disable_modify(ops, enable);
1473
1474         rcu_assign_pointer(*dst, new_hash);
1475
1476         ftrace_hash_rec_enable_modify(ops, enable);
1477
1478         return 0;
1479 }
1480
1481 static bool hash_contains_ip(unsigned long ip,
1482                              struct ftrace_ops_hash *hash)
1483 {
1484         /*
1485          * The function record is a match if it exists in the filter
1486          * hash and not in the notrace hash. Note, an emty hash is
1487          * considered a match for the filter hash, but an empty
1488          * notrace hash is considered not in the notrace hash.
1489          */
1490         return (ftrace_hash_empty(hash->filter_hash) ||
1491                 __ftrace_lookup_ip(hash->filter_hash, ip)) &&
1492                 (ftrace_hash_empty(hash->notrace_hash) ||
1493                  !__ftrace_lookup_ip(hash->notrace_hash, ip));
1494 }
1495
1496 /*
1497  * Test the hashes for this ops to see if we want to call
1498  * the ops->func or not.
1499  *
1500  * It's a match if the ip is in the ops->filter_hash or
1501  * the filter_hash does not exist or is empty,
1502  *  AND
1503  * the ip is not in the ops->notrace_hash.
1504  *
1505  * This needs to be called with preemption disabled as
1506  * the hashes are freed with call_rcu_sched().
1507  */
1508 static int
1509 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
1510 {
1511         struct ftrace_ops_hash hash;
1512         int ret;
1513
1514 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1515         /*
1516          * There's a small race when adding ops that the ftrace handler
1517          * that wants regs, may be called without them. We can not
1518          * allow that handler to be called if regs is NULL.
1519          */
1520         if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1521                 return 0;
1522 #endif
1523
1524         rcu_assign_pointer(hash.filter_hash, ops->func_hash->filter_hash);
1525         rcu_assign_pointer(hash.notrace_hash, ops->func_hash->notrace_hash);
1526
1527         if (hash_contains_ip(ip, &hash))
1528                 ret = 1;
1529         else
1530                 ret = 0;
1531
1532         return ret;
1533 }
1534
1535 /*
1536  * This is a double for. Do not use 'break' to break out of the loop,
1537  * you must use a goto.
1538  */
1539 #define do_for_each_ftrace_rec(pg, rec)                                 \
1540         for (pg = ftrace_pages_start; pg; pg = pg->next) {              \
1541                 int _____i;                                             \
1542                 for (_____i = 0; _____i < pg->index; _____i++) {        \
1543                         rec = &pg->records[_____i];
1544
1545 #define while_for_each_ftrace_rec()             \
1546                 }                               \
1547         }
1548
1549
1550 static int ftrace_cmp_recs(const void *a, const void *b)
1551 {
1552         const struct dyn_ftrace *key = a;
1553         const struct dyn_ftrace *rec = b;
1554
1555         if (key->flags < rec->ip)
1556                 return -1;
1557         if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1558                 return 1;
1559         return 0;
1560 }
1561
1562 /**
1563  * ftrace_location_range - return the first address of a traced location
1564  *      if it touches the given ip range
1565  * @start: start of range to search.
1566  * @end: end of range to search (inclusive). @end points to the last byte
1567  *      to check.
1568  *
1569  * Returns rec->ip if the related ftrace location is a least partly within
1570  * the given address range. That is, the first address of the instruction
1571  * that is either a NOP or call to the function tracer. It checks the ftrace
1572  * internal tables to determine if the address belongs or not.
1573  */
1574 unsigned long ftrace_location_range(unsigned long start, unsigned long end)
1575 {
1576         struct ftrace_page *pg;
1577         struct dyn_ftrace *rec;
1578         struct dyn_ftrace key;
1579
1580         key.ip = start;
1581         key.flags = end;        /* overload flags, as it is unsigned long */
1582
1583         for (pg = ftrace_pages_start; pg; pg = pg->next) {
1584                 if (pg->index == 0 ||
1585                     end < pg->records[0].ip ||
1586                     start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1587                         continue;
1588                 rec = bsearch(&key, pg->records, pg->index,
1589                               sizeof(struct dyn_ftrace),
1590                               ftrace_cmp_recs);
1591                 if (rec)
1592                         return rec->ip;
1593         }
1594
1595         return 0;
1596 }
1597
1598 /**
1599  * ftrace_location - return true if the ip giving is a traced location
1600  * @ip: the instruction pointer to check
1601  *
1602  * Returns rec->ip if @ip given is a pointer to a ftrace location.
1603  * That is, the instruction that is either a NOP or call to
1604  * the function tracer. It checks the ftrace internal tables to
1605  * determine if the address belongs or not.
1606  */
1607 unsigned long ftrace_location(unsigned long ip)
1608 {
1609         return ftrace_location_range(ip, ip);
1610 }
1611
1612 /**
1613  * ftrace_text_reserved - return true if range contains an ftrace location
1614  * @start: start of range to search
1615  * @end: end of range to search (inclusive). @end points to the last byte to check.
1616  *
1617  * Returns 1 if @start and @end contains a ftrace location.
1618  * That is, the instruction that is either a NOP or call to
1619  * the function tracer. It checks the ftrace internal tables to
1620  * determine if the address belongs or not.
1621  */
1622 int ftrace_text_reserved(const void *start, const void *end)
1623 {
1624         unsigned long ret;
1625
1626         ret = ftrace_location_range((unsigned long)start,
1627                                     (unsigned long)end);
1628
1629         return (int)!!ret;
1630 }
1631
1632 /* Test if ops registered to this rec needs regs */
1633 static bool test_rec_ops_needs_regs(struct dyn_ftrace *rec)
1634 {
1635         struct ftrace_ops *ops;
1636         bool keep_regs = false;
1637
1638         for (ops = ftrace_ops_list;
1639              ops != &ftrace_list_end; ops = ops->next) {
1640                 /* pass rec in as regs to have non-NULL val */
1641                 if (ftrace_ops_test(ops, rec->ip, rec)) {
1642                         if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1643                                 keep_regs = true;
1644                                 break;
1645                         }
1646                 }
1647         }
1648
1649         return  keep_regs;
1650 }
1651
1652 static struct ftrace_ops *
1653 ftrace_find_tramp_ops_any(struct dyn_ftrace *rec);
1654 static struct ftrace_ops *
1655 ftrace_find_tramp_ops_any_other(struct dyn_ftrace *rec, struct ftrace_ops *op_exclude);
1656 static struct ftrace_ops *
1657 ftrace_find_tramp_ops_next(struct dyn_ftrace *rec, struct ftrace_ops *ops);
1658
1659 static bool __ftrace_hash_rec_update(struct ftrace_ops *ops,
1660                                      int filter_hash,
1661                                      bool inc)
1662 {
1663         struct ftrace_hash *hash;
1664         struct ftrace_hash *other_hash;
1665         struct ftrace_page *pg;
1666         struct dyn_ftrace *rec;
1667         bool update = false;
1668         int count = 0;
1669         int all = false;
1670
1671         /* Only update if the ops has been registered */
1672         if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1673                 return false;
1674
1675         /*
1676          * In the filter_hash case:
1677          *   If the count is zero, we update all records.
1678          *   Otherwise we just update the items in the hash.
1679          *
1680          * In the notrace_hash case:
1681          *   We enable the update in the hash.
1682          *   As disabling notrace means enabling the tracing,
1683          *   and enabling notrace means disabling, the inc variable
1684          *   gets inversed.
1685          */
1686         if (filter_hash) {
1687                 hash = ops->func_hash->filter_hash;
1688                 other_hash = ops->func_hash->notrace_hash;
1689                 if (ftrace_hash_empty(hash))
1690                         all = true;
1691         } else {
1692                 inc = !inc;
1693                 hash = ops->func_hash->notrace_hash;
1694                 other_hash = ops->func_hash->filter_hash;
1695                 /*
1696                  * If the notrace hash has no items,
1697                  * then there's nothing to do.
1698                  */
1699                 if (ftrace_hash_empty(hash))
1700                         return false;
1701         }
1702
1703         do_for_each_ftrace_rec(pg, rec) {
1704                 int in_other_hash = 0;
1705                 int in_hash = 0;
1706                 int match = 0;
1707
1708                 if (rec->flags & FTRACE_FL_DISABLED)
1709                         continue;
1710
1711                 if (all) {
1712                         /*
1713                          * Only the filter_hash affects all records.
1714                          * Update if the record is not in the notrace hash.
1715                          */
1716                         if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1717                                 match = 1;
1718                 } else {
1719                         in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1720                         in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1721
1722                         /*
1723                          * If filter_hash is set, we want to match all functions
1724                          * that are in the hash but not in the other hash.
1725                          *
1726                          * If filter_hash is not set, then we are decrementing.
1727                          * That means we match anything that is in the hash
1728                          * and also in the other_hash. That is, we need to turn
1729                          * off functions in the other hash because they are disabled
1730                          * by this hash.
1731                          */
1732                         if (filter_hash && in_hash && !in_other_hash)
1733                                 match = 1;
1734                         else if (!filter_hash && in_hash &&
1735                                  (in_other_hash || ftrace_hash_empty(other_hash)))
1736                                 match = 1;
1737                 }
1738                 if (!match)
1739                         continue;
1740
1741                 if (inc) {
1742                         rec->flags++;
1743                         if (FTRACE_WARN_ON(ftrace_rec_count(rec) == FTRACE_REF_MAX))
1744                                 return false;
1745
1746                         /*
1747                          * If there's only a single callback registered to a
1748                          * function, and the ops has a trampoline registered
1749                          * for it, then we can call it directly.
1750                          */
1751                         if (ftrace_rec_count(rec) == 1 && ops->trampoline)
1752                                 rec->flags |= FTRACE_FL_TRAMP;
1753                         else
1754                                 /*
1755                                  * If we are adding another function callback
1756                                  * to this function, and the previous had a
1757                                  * custom trampoline in use, then we need to go
1758                                  * back to the default trampoline.
1759                                  */
1760                                 rec->flags &= ~FTRACE_FL_TRAMP;
1761
1762                         /*
1763                          * If any ops wants regs saved for this function
1764                          * then all ops will get saved regs.
1765                          */
1766                         if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1767                                 rec->flags |= FTRACE_FL_REGS;
1768                 } else {
1769                         if (FTRACE_WARN_ON(ftrace_rec_count(rec) == 0))
1770                                 return false;
1771                         rec->flags--;
1772
1773                         /*
1774                          * If the rec had REGS enabled and the ops that is
1775                          * being removed had REGS set, then see if there is
1776                          * still any ops for this record that wants regs.
1777                          * If not, we can stop recording them.
1778                          */
1779                         if (ftrace_rec_count(rec) > 0 &&
1780                             rec->flags & FTRACE_FL_REGS &&
1781                             ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
1782                                 if (!test_rec_ops_needs_regs(rec))
1783                                         rec->flags &= ~FTRACE_FL_REGS;
1784                         }
1785
1786                         /*
1787                          * The TRAMP needs to be set only if rec count
1788                          * is decremented to one, and the ops that is
1789                          * left has a trampoline. As TRAMP can only be
1790                          * enabled if there is only a single ops attached
1791                          * to it.
1792                          */
1793                         if (ftrace_rec_count(rec) == 1 &&
1794                             ftrace_find_tramp_ops_any_other(rec, ops))
1795                                 rec->flags |= FTRACE_FL_TRAMP;
1796                         else
1797                                 rec->flags &= ~FTRACE_FL_TRAMP;
1798
1799                         /*
1800                          * flags will be cleared in ftrace_check_record()
1801                          * if rec count is zero.
1802                          */
1803                 }
1804                 count++;
1805
1806                 /* Must match FTRACE_UPDATE_CALLS in ftrace_modify_all_code() */
1807                 update |= ftrace_test_record(rec, 1) != FTRACE_UPDATE_IGNORE;
1808
1809                 /* Shortcut, if we handled all records, we are done. */
1810                 if (!all && count == hash->count)
1811                         return update;
1812         } while_for_each_ftrace_rec();
1813
1814         return update;
1815 }
1816
1817 static bool ftrace_hash_rec_disable(struct ftrace_ops *ops,
1818                                     int filter_hash)
1819 {
1820         return __ftrace_hash_rec_update(ops, filter_hash, 0);
1821 }
1822
1823 static bool ftrace_hash_rec_enable(struct ftrace_ops *ops,
1824                                    int filter_hash)
1825 {
1826         return __ftrace_hash_rec_update(ops, filter_hash, 1);
1827 }
1828
1829 static void ftrace_hash_rec_update_modify(struct ftrace_ops *ops,
1830                                           int filter_hash, int inc)
1831 {
1832         struct ftrace_ops *op;
1833
1834         __ftrace_hash_rec_update(ops, filter_hash, inc);
1835
1836         if (ops->func_hash != &global_ops.local_hash)
1837                 return;
1838
1839         /*
1840          * If the ops shares the global_ops hash, then we need to update
1841          * all ops that are enabled and use this hash.
1842          */
1843         do_for_each_ftrace_op(op, ftrace_ops_list) {
1844                 /* Already done */
1845                 if (op == ops)
1846                         continue;
1847                 if (op->func_hash == &global_ops.local_hash)
1848                         __ftrace_hash_rec_update(op, filter_hash, inc);
1849         } while_for_each_ftrace_op(op);
1850 }
1851
1852 static void ftrace_hash_rec_disable_modify(struct ftrace_ops *ops,
1853                                            int filter_hash)
1854 {
1855         ftrace_hash_rec_update_modify(ops, filter_hash, 0);
1856 }
1857
1858 static void ftrace_hash_rec_enable_modify(struct ftrace_ops *ops,
1859                                           int filter_hash)
1860 {
1861         ftrace_hash_rec_update_modify(ops, filter_hash, 1);
1862 }
1863
1864 /*
1865  * Try to update IPMODIFY flag on each ftrace_rec. Return 0 if it is OK
1866  * or no-needed to update, -EBUSY if it detects a conflict of the flag
1867  * on a ftrace_rec, and -EINVAL if the new_hash tries to trace all recs.
1868  * Note that old_hash and new_hash has below meanings
1869  *  - If the hash is NULL, it hits all recs (if IPMODIFY is set, this is rejected)
1870  *  - If the hash is EMPTY_HASH, it hits nothing
1871  *  - Anything else hits the recs which match the hash entries.
1872  */
1873 static int __ftrace_hash_update_ipmodify(struct ftrace_ops *ops,
1874                                          struct ftrace_hash *old_hash,
1875                                          struct ftrace_hash *new_hash)
1876 {
1877         struct ftrace_page *pg;
1878         struct dyn_ftrace *rec, *end = NULL;
1879         int in_old, in_new;
1880
1881         /* Only update if the ops has been registered */
1882         if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1883                 return 0;
1884
1885         if (!(ops->flags & FTRACE_OPS_FL_IPMODIFY))
1886                 return 0;
1887
1888         /*
1889          * Since the IPMODIFY is a very address sensitive action, we do not
1890          * allow ftrace_ops to set all functions to new hash.
1891          */
1892         if (!new_hash || !old_hash)
1893                 return -EINVAL;
1894
1895         /* Update rec->flags */
1896         do_for_each_ftrace_rec(pg, rec) {
1897
1898                 if (rec->flags & FTRACE_FL_DISABLED)
1899                         continue;
1900
1901                 /* We need to update only differences of filter_hash */
1902                 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1903                 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1904                 if (in_old == in_new)
1905                         continue;
1906
1907                 if (in_new) {
1908                         /* New entries must ensure no others are using it */
1909                         if (rec->flags & FTRACE_FL_IPMODIFY)
1910                                 goto rollback;
1911                         rec->flags |= FTRACE_FL_IPMODIFY;
1912                 } else /* Removed entry */
1913                         rec->flags &= ~FTRACE_FL_IPMODIFY;
1914         } while_for_each_ftrace_rec();
1915
1916         return 0;
1917
1918 rollback:
1919         end = rec;
1920
1921         /* Roll back what we did above */
1922         do_for_each_ftrace_rec(pg, rec) {
1923
1924                 if (rec->flags & FTRACE_FL_DISABLED)
1925                         continue;
1926
1927                 if (rec == end)
1928                         goto err_out;
1929
1930                 in_old = !!ftrace_lookup_ip(old_hash, rec->ip);
1931                 in_new = !!ftrace_lookup_ip(new_hash, rec->ip);
1932                 if (in_old == in_new)
1933                         continue;
1934
1935                 if (in_new)
1936                         rec->flags &= ~FTRACE_FL_IPMODIFY;
1937                 else
1938                         rec->flags |= FTRACE_FL_IPMODIFY;
1939         } while_for_each_ftrace_rec();
1940
1941 err_out:
1942         return -EBUSY;
1943 }
1944
1945 static int ftrace_hash_ipmodify_enable(struct ftrace_ops *ops)
1946 {
1947         struct ftrace_hash *hash = ops->func_hash->filter_hash;
1948
1949         if (ftrace_hash_empty(hash))
1950                 hash = NULL;
1951
1952         return __ftrace_hash_update_ipmodify(ops, EMPTY_HASH, hash);
1953 }
1954
1955 /* Disabling always succeeds */
1956 static void ftrace_hash_ipmodify_disable(struct ftrace_ops *ops)
1957 {
1958         struct ftrace_hash *hash = ops->func_hash->filter_hash;
1959
1960         if (ftrace_hash_empty(hash))
1961                 hash = NULL;
1962
1963         __ftrace_hash_update_ipmodify(ops, hash, EMPTY_HASH);
1964 }
1965
1966 static int ftrace_hash_ipmodify_update(struct ftrace_ops *ops,
1967                                        struct ftrace_hash *new_hash)
1968 {
1969         struct ftrace_hash *old_hash = ops->func_hash->filter_hash;
1970
1971         if (ftrace_hash_empty(old_hash))
1972                 old_hash = NULL;
1973
1974         if (ftrace_hash_empty(new_hash))
1975                 new_hash = NULL;
1976
1977         return __ftrace_hash_update_ipmodify(ops, old_hash, new_hash);
1978 }
1979
1980 static void print_ip_ins(const char *fmt, const unsigned char *p)
1981 {
1982         char ins[MCOUNT_INSN_SIZE];
1983         int i;
1984
1985         if (probe_kernel_read(ins, p, MCOUNT_INSN_SIZE)) {
1986                 printk(KERN_CONT "%s[FAULT] %px\n", fmt, p);
1987                 return;
1988         }
1989
1990         printk(KERN_CONT "%s", fmt);
1991
1992         for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1993                 printk(KERN_CONT "%s%02x", i ? ":" : "", ins[i]);
1994 }
1995
1996 enum ftrace_bug_type ftrace_bug_type;
1997 const void *ftrace_expected;
1998
1999 static void print_bug_type(void)
2000 {
2001         switch (ftrace_bug_type) {
2002         case FTRACE_BUG_UNKNOWN:
2003                 break;
2004         case FTRACE_BUG_INIT:
2005                 pr_info("Initializing ftrace call sites\n");
2006                 break;
2007         case FTRACE_BUG_NOP:
2008                 pr_info("Setting ftrace call site to NOP\n");
2009                 break;
2010         case FTRACE_BUG_CALL:
2011                 pr_info("Setting ftrace call site to call ftrace function\n");
2012                 break;
2013         case FTRACE_BUG_UPDATE:
2014                 pr_info("Updating ftrace call site to call a different ftrace function\n");
2015                 break;
2016         }
2017 }
2018
2019 /**
2020  * ftrace_bug - report and shutdown function tracer
2021  * @failed: The failed type (EFAULT, EINVAL, EPERM)
2022  * @rec: The record that failed
2023  *
2024  * The arch code that enables or disables the function tracing
2025  * can call ftrace_bug() when it has detected a problem in
2026  * modifying the code. @failed should be one of either:
2027  * EFAULT - if the problem happens on reading the @ip address
2028  * EINVAL - if what is read at @ip is not what was expected
2029  * EPERM - if the problem happens on writting to the @ip address
2030  */
2031 void ftrace_bug(int failed, struct dyn_ftrace *rec)
2032 {
2033         unsigned long ip = rec ? rec->ip : 0;
2034
2035         switch (failed) {
2036         case -EFAULT:
2037                 FTRACE_WARN_ON_ONCE(1);
2038                 pr_info("ftrace faulted on modifying ");
2039                 print_ip_sym(ip);
2040                 break;
2041         case -EINVAL:
2042                 FTRACE_WARN_ON_ONCE(1);
2043                 pr_info("ftrace failed to modify ");
2044                 print_ip_sym(ip);
2045                 print_ip_ins(" actual:   ", (unsigned char *)ip);
2046                 pr_cont("\n");
2047                 if (ftrace_expected) {
2048                         print_ip_ins(" expected: ", ftrace_expected);
2049                         pr_cont("\n");
2050                 }
2051                 break;
2052         case -EPERM:
2053                 FTRACE_WARN_ON_ONCE(1);
2054                 pr_info("ftrace faulted on writing ");
2055                 print_ip_sym(ip);
2056                 break;
2057         default:
2058                 FTRACE_WARN_ON_ONCE(1);
2059                 pr_info("ftrace faulted on unknown error ");
2060                 print_ip_sym(ip);
2061         }
2062         print_bug_type();
2063         if (rec) {
2064                 struct ftrace_ops *ops = NULL;
2065
2066                 pr_info("ftrace record flags: %lx\n", rec->flags);
2067                 pr_cont(" (%ld)%s", ftrace_rec_count(rec),
2068                         rec->flags & FTRACE_FL_REGS ? " R" : "  ");
2069                 if (rec->flags & FTRACE_FL_TRAMP_EN) {
2070                         ops = ftrace_find_tramp_ops_any(rec);
2071                         if (ops) {
2072                                 do {
2073                                         pr_cont("\ttramp: %pS (%pS)",
2074                                                 (void *)ops->trampoline,
2075                                                 (void *)ops->func);
2076                                         ops = ftrace_find_tramp_ops_next(rec, ops);
2077                                 } while (ops);
2078                         } else
2079                                 pr_cont("\ttramp: ERROR!");
2080
2081                 }
2082                 ip = ftrace_get_addr_curr(rec);
2083                 pr_cont("\n expected tramp: %lx\n", ip);
2084         }
2085 }
2086
2087 static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
2088 {
2089         unsigned long flag = 0UL;
2090
2091         ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2092
2093         if (rec->flags & FTRACE_FL_DISABLED)
2094                 return FTRACE_UPDATE_IGNORE;
2095
2096         /*
2097          * If we are updating calls:
2098          *
2099          *   If the record has a ref count, then we need to enable it
2100          *   because someone is using it.
2101          *
2102          *   Otherwise we make sure its disabled.
2103          *
2104          * If we are disabling calls, then disable all records that
2105          * are enabled.
2106          */
2107         if (enable && ftrace_rec_count(rec))
2108                 flag = FTRACE_FL_ENABLED;
2109
2110         /*
2111          * If enabling and the REGS flag does not match the REGS_EN, or
2112          * the TRAMP flag doesn't match the TRAMP_EN, then do not ignore
2113          * this record. Set flags to fail the compare against ENABLED.
2114          */
2115         if (flag) {
2116                 if (!(rec->flags & FTRACE_FL_REGS) != 
2117                     !(rec->flags & FTRACE_FL_REGS_EN))
2118                         flag |= FTRACE_FL_REGS;
2119
2120                 if (!(rec->flags & FTRACE_FL_TRAMP) != 
2121                     !(rec->flags & FTRACE_FL_TRAMP_EN))
2122                         flag |= FTRACE_FL_TRAMP;
2123         }
2124
2125         /* If the state of this record hasn't changed, then do nothing */
2126         if ((rec->flags & FTRACE_FL_ENABLED) == flag)
2127                 return FTRACE_UPDATE_IGNORE;
2128
2129         if (flag) {
2130                 /* Save off if rec is being enabled (for return value) */
2131                 flag ^= rec->flags & FTRACE_FL_ENABLED;
2132
2133                 if (update) {
2134                         rec->flags |= FTRACE_FL_ENABLED;
2135                         if (flag & FTRACE_FL_REGS) {
2136                                 if (rec->flags & FTRACE_FL_REGS)
2137                                         rec->flags |= FTRACE_FL_REGS_EN;
2138                                 else
2139                                         rec->flags &= ~FTRACE_FL_REGS_EN;
2140                         }
2141                         if (flag & FTRACE_FL_TRAMP) {
2142                                 if (rec->flags & FTRACE_FL_TRAMP)
2143                                         rec->flags |= FTRACE_FL_TRAMP_EN;
2144                                 else
2145                                         rec->flags &= ~FTRACE_FL_TRAMP_EN;
2146                         }
2147                 }
2148
2149                 /*
2150                  * If this record is being updated from a nop, then
2151                  *   return UPDATE_MAKE_CALL.
2152                  * Otherwise,
2153                  *   return UPDATE_MODIFY_CALL to tell the caller to convert
2154                  *   from the save regs, to a non-save regs function or
2155                  *   vice versa, or from a trampoline call.
2156                  */
2157                 if (flag & FTRACE_FL_ENABLED) {
2158                         ftrace_bug_type = FTRACE_BUG_CALL;
2159                         return FTRACE_UPDATE_MAKE_CALL;
2160                 }
2161
2162                 ftrace_bug_type = FTRACE_BUG_UPDATE;
2163                 return FTRACE_UPDATE_MODIFY_CALL;
2164         }
2165
2166         if (update) {
2167                 /* If there's no more users, clear all flags */
2168                 if (!ftrace_rec_count(rec))
2169                         rec->flags = 0;
2170                 else
2171                         /*
2172                          * Just disable the record, but keep the ops TRAMP
2173                          * and REGS states. The _EN flags must be disabled though.
2174                          */
2175                         rec->flags &= ~(FTRACE_FL_ENABLED | FTRACE_FL_TRAMP_EN |
2176                                         FTRACE_FL_REGS_EN);
2177         }
2178
2179         ftrace_bug_type = FTRACE_BUG_NOP;
2180         return FTRACE_UPDATE_MAKE_NOP;
2181 }
2182
2183 /**
2184  * ftrace_update_record, set a record that now is tracing or not
2185  * @rec: the record to update
2186  * @enable: set to 1 if the record is tracing, zero to force disable
2187  *
2188  * The records that represent all functions that can be traced need
2189  * to be updated when tracing has been enabled.
2190  */
2191 int ftrace_update_record(struct dyn_ftrace *rec, int enable)
2192 {
2193         return ftrace_check_record(rec, enable, 1);
2194 }
2195
2196 /**
2197  * ftrace_test_record, check if the record has been enabled or not
2198  * @rec: the record to test
2199  * @enable: set to 1 to check if enabled, 0 if it is disabled
2200  *
2201  * The arch code may need to test if a record is already set to
2202  * tracing to determine how to modify the function code that it
2203  * represents.
2204  */
2205 int ftrace_test_record(struct dyn_ftrace *rec, int enable)
2206 {
2207         return ftrace_check_record(rec, enable, 0);
2208 }
2209
2210 static struct ftrace_ops *
2211 ftrace_find_tramp_ops_any(struct dyn_ftrace *rec)
2212 {
2213         struct ftrace_ops *op;
2214         unsigned long ip = rec->ip;
2215
2216         do_for_each_ftrace_op(op, ftrace_ops_list) {
2217
2218                 if (!op->trampoline)
2219                         continue;
2220
2221                 if (hash_contains_ip(ip, op->func_hash))
2222                         return op;
2223         } while_for_each_ftrace_op(op);
2224
2225         return NULL;
2226 }
2227
2228 static struct ftrace_ops *
2229 ftrace_find_tramp_ops_any_other(struct dyn_ftrace *rec, struct ftrace_ops *op_exclude)
2230 {
2231         struct ftrace_ops *op;
2232         unsigned long ip = rec->ip;
2233
2234         do_for_each_ftrace_op(op, ftrace_ops_list) {
2235
2236                 if (op == op_exclude || !op->trampoline)
2237                         continue;
2238
2239                 if (hash_contains_ip(ip, op->func_hash))
2240                         return op;
2241         } while_for_each_ftrace_op(op);
2242
2243         return NULL;
2244 }
2245
2246 static struct ftrace_ops *
2247 ftrace_find_tramp_ops_next(struct dyn_ftrace *rec,
2248                            struct ftrace_ops *op)
2249 {
2250         unsigned long ip = rec->ip;
2251
2252         while_for_each_ftrace_op(op) {
2253
2254                 if (!op->trampoline)
2255                         continue;
2256
2257                 if (hash_contains_ip(ip, op->func_hash))
2258                         return op;
2259         } 
2260
2261         return NULL;
2262 }
2263
2264 static struct ftrace_ops *
2265 ftrace_find_tramp_ops_curr(struct dyn_ftrace *rec)
2266 {
2267         struct ftrace_ops *op;
2268         unsigned long ip = rec->ip;
2269
2270         /*
2271          * Need to check removed ops first.
2272          * If they are being removed, and this rec has a tramp,
2273          * and this rec is in the ops list, then it would be the
2274          * one with the tramp.
2275          */
2276         if (removed_ops) {
2277                 if (hash_contains_ip(ip, &removed_ops->old_hash))
2278                         return removed_ops;
2279         }
2280
2281         /*
2282          * Need to find the current trampoline for a rec.
2283          * Now, a trampoline is only attached to a rec if there
2284          * was a single 'ops' attached to it. But this can be called
2285          * when we are adding another op to the rec or removing the
2286          * current one. Thus, if the op is being added, we can
2287          * ignore it because it hasn't attached itself to the rec
2288          * yet.
2289          *
2290          * If an ops is being modified (hooking to different functions)
2291          * then we don't care about the new functions that are being
2292          * added, just the old ones (that are probably being removed).
2293          *
2294          * If we are adding an ops to a function that already is using
2295          * a trampoline, it needs to be removed (trampolines are only
2296          * for single ops connected), then an ops that is not being
2297          * modified also needs to be checked.
2298          */
2299         do_for_each_ftrace_op(op, ftrace_ops_list) {
2300
2301                 if (!op->trampoline)
2302                         continue;
2303
2304                 /*
2305                  * If the ops is being added, it hasn't gotten to
2306                  * the point to be removed from this tree yet.
2307                  */
2308                 if (op->flags & FTRACE_OPS_FL_ADDING)
2309                         continue;
2310
2311
2312                 /*
2313                  * If the ops is being modified and is in the old
2314                  * hash, then it is probably being removed from this
2315                  * function.
2316                  */
2317                 if ((op->flags & FTRACE_OPS_FL_MODIFYING) &&
2318                     hash_contains_ip(ip, &op->old_hash))
2319                         return op;
2320                 /*
2321                  * If the ops is not being added or modified, and it's
2322                  * in its normal filter hash, then this must be the one
2323                  * we want!
2324                  */
2325                 if (!(op->flags & FTRACE_OPS_FL_MODIFYING) &&
2326                     hash_contains_ip(ip, op->func_hash))
2327                         return op;
2328
2329         } while_for_each_ftrace_op(op);
2330
2331         return NULL;
2332 }
2333
2334 static struct ftrace_ops *
2335 ftrace_find_tramp_ops_new(struct dyn_ftrace *rec)
2336 {
2337         struct ftrace_ops *op;
2338         unsigned long ip = rec->ip;
2339
2340         do_for_each_ftrace_op(op, ftrace_ops_list) {
2341                 /* pass rec in as regs to have non-NULL val */
2342                 if (hash_contains_ip(ip, op->func_hash))
2343                         return op;
2344         } while_for_each_ftrace_op(op);
2345
2346         return NULL;
2347 }
2348
2349 /**
2350  * ftrace_get_addr_new - Get the call address to set to
2351  * @rec:  The ftrace record descriptor
2352  *
2353  * If the record has the FTRACE_FL_REGS set, that means that it
2354  * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
2355  * is not not set, then it wants to convert to the normal callback.
2356  *
2357  * Returns the address of the trampoline to set to
2358  */
2359 unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec)
2360 {
2361         struct ftrace_ops *ops;
2362
2363         /* Trampolines take precedence over regs */
2364         if (rec->flags & FTRACE_FL_TRAMP) {
2365                 ops = ftrace_find_tramp_ops_new(rec);
2366                 if (FTRACE_WARN_ON(!ops || !ops->trampoline)) {
2367                         pr_warn("Bad trampoline accounting at: %p (%pS) (%lx)\n",
2368                                 (void *)rec->ip, (void *)rec->ip, rec->flags);
2369                         /* Ftrace is shutting down, return anything */
2370                         return (unsigned long)FTRACE_ADDR;
2371                 }
2372                 return ops->trampoline;
2373         }
2374
2375         if (rec->flags & FTRACE_FL_REGS)
2376                 return (unsigned long)FTRACE_REGS_ADDR;
2377         else
2378                 return (unsigned long)FTRACE_ADDR;
2379 }
2380
2381 /**
2382  * ftrace_get_addr_curr - Get the call address that is already there
2383  * @rec:  The ftrace record descriptor
2384  *
2385  * The FTRACE_FL_REGS_EN is set when the record already points to
2386  * a function that saves all the regs. Basically the '_EN' version
2387  * represents the current state of the function.
2388  *
2389  * Returns the address of the trampoline that is currently being called
2390  */
2391 unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec)
2392 {
2393         struct ftrace_ops *ops;
2394
2395         /* Trampolines take precedence over regs */
2396         if (rec->flags & FTRACE_FL_TRAMP_EN) {
2397                 ops = ftrace_find_tramp_ops_curr(rec);
2398                 if (FTRACE_WARN_ON(!ops)) {
2399                         pr_warn("Bad trampoline accounting at: %p (%pS)\n",
2400                                 (void *)rec->ip, (void *)rec->ip);
2401                         /* Ftrace is shutting down, return anything */
2402                         return (unsigned long)FTRACE_ADDR;
2403                 }
2404                 return ops->trampoline;
2405         }
2406
2407         if (rec->flags & FTRACE_FL_REGS_EN)
2408                 return (unsigned long)FTRACE_REGS_ADDR;
2409         else
2410                 return (unsigned long)FTRACE_ADDR;
2411 }
2412
2413 static int
2414 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
2415 {
2416         unsigned long ftrace_old_addr;
2417         unsigned long ftrace_addr;
2418         int ret;
2419
2420         ftrace_addr = ftrace_get_addr_new(rec);
2421
2422         /* This needs to be done before we call ftrace_update_record */
2423         ftrace_old_addr = ftrace_get_addr_curr(rec);
2424
2425         ret = ftrace_update_record(rec, enable);
2426
2427         ftrace_bug_type = FTRACE_BUG_UNKNOWN;
2428
2429         switch (ret) {
2430         case FTRACE_UPDATE_IGNORE:
2431                 return 0;
2432
2433         case FTRACE_UPDATE_MAKE_CALL:
2434                 ftrace_bug_type = FTRACE_BUG_CALL;
2435                 return ftrace_make_call(rec, ftrace_addr);
2436
2437         case FTRACE_UPDATE_MAKE_NOP:
2438                 ftrace_bug_type = FTRACE_BUG_NOP;
2439                 return ftrace_make_nop(NULL, rec, ftrace_old_addr);
2440
2441         case FTRACE_UPDATE_MODIFY_CALL:
2442                 ftrace_bug_type = FTRACE_BUG_UPDATE;
2443                 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
2444         }
2445
2446         return -1; /* unknow ftrace bug */
2447 }
2448
2449 void __weak ftrace_replace_code(int enable)
2450 {
2451         struct dyn_ftrace *rec;
2452         struct ftrace_page *pg;
2453         int failed;
2454
2455         if (unlikely(ftrace_disabled))
2456                 return;
2457
2458         do_for_each_ftrace_rec(pg, rec) {
2459
2460                 if (rec->flags & FTRACE_FL_DISABLED)
2461                         continue;
2462
2463                 failed = __ftrace_replace_code(rec, enable);
2464                 if (failed) {
2465                         ftrace_bug(failed, rec);
2466                         /* Stop processing */
2467                         return;
2468                 }
2469         } while_for_each_ftrace_rec();
2470 }
2471
2472 struct ftrace_rec_iter {
2473         struct ftrace_page      *pg;
2474         int                     index;
2475 };
2476
2477 /**
2478  * ftrace_rec_iter_start, start up iterating over traced functions
2479  *
2480  * Returns an iterator handle that is used to iterate over all
2481  * the records that represent address locations where functions
2482  * are traced.
2483  *
2484  * May return NULL if no records are available.
2485  */
2486 struct ftrace_rec_iter *ftrace_rec_iter_start(void)
2487 {
2488         /*
2489          * We only use a single iterator.
2490          * Protected by the ftrace_lock mutex.
2491          */
2492         static struct ftrace_rec_iter ftrace_rec_iter;
2493         struct ftrace_rec_iter *iter = &ftrace_rec_iter;
2494
2495         iter->pg = ftrace_pages_start;
2496         iter->index = 0;
2497
2498         /* Could have empty pages */
2499         while (iter->pg && !iter->pg->index)
2500                 iter->pg = iter->pg->next;
2501
2502         if (!iter->pg)
2503                 return NULL;
2504
2505         return iter;
2506 }
2507
2508 /**
2509  * ftrace_rec_iter_next, get the next record to process.
2510  * @iter: The handle to the iterator.
2511  *
2512  * Returns the next iterator after the given iterator @iter.
2513  */
2514 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
2515 {
2516         iter->index++;
2517
2518         if (iter->index >= iter->pg->index) {
2519                 iter->pg = iter->pg->next;
2520                 iter->index = 0;
2521
2522                 /* Could have empty pages */
2523                 while (iter->pg && !iter->pg->index)
2524                         iter->pg = iter->pg->next;
2525         }
2526
2527         if (!iter->pg)
2528                 return NULL;
2529
2530         return iter;
2531 }
2532
2533 /**
2534  * ftrace_rec_iter_record, get the record at the iterator location
2535  * @iter: The current iterator location
2536  *
2537  * Returns the record that the current @iter is at.
2538  */
2539 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
2540 {
2541         return &iter->pg->records[iter->index];
2542 }
2543
2544 static int
2545 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
2546 {
2547         int ret;
2548
2549         if (unlikely(ftrace_disabled))
2550                 return 0;
2551
2552         ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
2553         if (ret) {
2554                 ftrace_bug_type = FTRACE_BUG_INIT;
2555                 ftrace_bug(ret, rec);
2556                 return 0;
2557         }
2558         return 1;
2559 }
2560
2561 /*
2562  * archs can override this function if they must do something
2563  * before the modifying code is performed.
2564  */
2565 int __weak ftrace_arch_code_modify_prepare(void)
2566 {
2567         return 0;
2568 }
2569
2570 /*
2571  * archs can override this function if they must do something
2572  * after the modifying code is performed.
2573  */
2574 int __weak ftrace_arch_code_modify_post_process(void)
2575 {
2576         return 0;
2577 }
2578
2579 void ftrace_modify_all_code(int command)
2580 {
2581         int update = command & FTRACE_UPDATE_TRACE_FUNC;
2582         int err = 0;
2583
2584         /*
2585          * If the ftrace_caller calls a ftrace_ops func directly,
2586          * we need to make sure that it only traces functions it
2587          * expects to trace. When doing the switch of functions,
2588          * we need to update to the ftrace_ops_list_func first
2589          * before the transition between old and new calls are set,
2590          * as the ftrace_ops_list_func will check the ops hashes
2591          * to make sure the ops are having the right functions
2592          * traced.
2593          */
2594         if (update) {
2595                 err = ftrace_update_ftrace_func(ftrace_ops_list_func);
2596                 if (FTRACE_WARN_ON(err))
2597                         return;
2598         }
2599
2600         if (command & FTRACE_UPDATE_CALLS)
2601                 ftrace_replace_code(1);
2602         else if (command & FTRACE_DISABLE_CALLS)
2603                 ftrace_replace_code(0);
2604
2605         if (update && ftrace_trace_function != ftrace_ops_list_func) {
2606                 function_trace_op = set_function_trace_op;
2607                 smp_wmb();
2608                 /* If irqs are disabled, we are in stop machine */
2609                 if (!irqs_disabled())
2610                         smp_call_function(ftrace_sync_ipi, NULL, 1);
2611                 err = ftrace_update_ftrace_func(ftrace_trace_function);
2612                 if (FTRACE_WARN_ON(err))
2613                         return;
2614         }
2615
2616         if (command & FTRACE_START_FUNC_RET)
2617                 err = ftrace_enable_ftrace_graph_caller();
2618         else if (command & FTRACE_STOP_FUNC_RET)
2619                 err = ftrace_disable_ftrace_graph_caller();
2620         FTRACE_WARN_ON(err);
2621 }
2622
2623 static int __ftrace_modify_code(void *data)
2624 {
2625         int *command = data;
2626
2627         ftrace_modify_all_code(*command);
2628
2629         return 0;
2630 }
2631
2632 /**
2633  * ftrace_run_stop_machine, go back to the stop machine method
2634  * @command: The command to tell ftrace what to do
2635  *
2636  * If an arch needs to fall back to the stop machine method, the
2637  * it can call this function.
2638  */
2639 void ftrace_run_stop_machine(int command)
2640 {
2641         stop_machine(__ftrace_modify_code, &command, NULL);
2642 }
2643
2644 /**
2645  * arch_ftrace_update_code, modify the code to trace or not trace
2646  * @command: The command that needs to be done
2647  *
2648  * Archs can override this function if it does not need to
2649  * run stop_machine() to modify code.
2650  */
2651 void __weak arch_ftrace_update_code(int command)
2652 {
2653         ftrace_run_stop_machine(command);
2654 }
2655
2656 static void ftrace_run_update_code(int command)
2657 {
2658         int ret;
2659
2660         ret = ftrace_arch_code_modify_prepare();
2661         FTRACE_WARN_ON(ret);
2662         if (ret)
2663                 return;
2664
2665         /*
2666          * By default we use stop_machine() to modify the code.
2667          * But archs can do what ever they want as long as it
2668          * is safe. The stop_machine() is the safest, but also
2669          * produces the most overhead.
2670          */
2671         arch_ftrace_update_code(command);
2672
2673         ret = ftrace_arch_code_modify_post_process();
2674         FTRACE_WARN_ON(ret);
2675 }
2676
2677 static void ftrace_run_modify_code(struct ftrace_ops *ops, int command,
2678                                    struct ftrace_ops_hash *old_hash)
2679 {
2680         ops->flags |= FTRACE_OPS_FL_MODIFYING;
2681         ops->old_hash.filter_hash = old_hash->filter_hash;
2682         ops->old_hash.notrace_hash = old_hash->notrace_hash;
2683         ftrace_run_update_code(command);
2684         ops->old_hash.filter_hash = NULL;
2685         ops->old_hash.notrace_hash = NULL;
2686         ops->flags &= ~FTRACE_OPS_FL_MODIFYING;
2687 }
2688
2689 static ftrace_func_t saved_ftrace_func;
2690 static int ftrace_start_up;
2691
2692 void __weak arch_ftrace_trampoline_free(struct ftrace_ops *ops)
2693 {
2694 }
2695
2696 static void ftrace_startup_enable(int command)
2697 {
2698         if (saved_ftrace_func != ftrace_trace_function) {
2699                 saved_ftrace_func = ftrace_trace_function;
2700                 command |= FTRACE_UPDATE_TRACE_FUNC;
2701         }
2702
2703         if (!command || !ftrace_enabled)
2704                 return;
2705
2706         ftrace_run_update_code(command);
2707 }
2708
2709 static void ftrace_startup_all(int command)
2710 {
2711         update_all_ops = true;
2712         ftrace_startup_enable(command);
2713         update_all_ops = false;
2714 }
2715
2716 static int ftrace_startup(struct ftrace_ops *ops, int command)
2717 {
2718         int ret;
2719
2720         if (unlikely(ftrace_disabled))
2721                 return -ENODEV;
2722
2723         ret = __register_ftrace_function(ops);
2724         if (ret)
2725                 return ret;
2726
2727         ftrace_start_up++;
2728
2729         /*
2730          * Note that ftrace probes uses this to start up
2731          * and modify functions it will probe. But we still
2732          * set the ADDING flag for modification, as probes
2733          * do not have trampolines. If they add them in the
2734          * future, then the probes will need to distinguish
2735          * between adding and updating probes.
2736          */
2737         ops->flags |= FTRACE_OPS_FL_ENABLED | FTRACE_OPS_FL_ADDING;
2738
2739         ret = ftrace_hash_ipmodify_enable(ops);
2740         if (ret < 0) {
2741                 /* Rollback registration process */
2742                 __unregister_ftrace_function(ops);
2743                 ftrace_start_up--;
2744                 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2745                 return ret;
2746         }
2747
2748         if (ftrace_hash_rec_enable(ops, 1))
2749                 command |= FTRACE_UPDATE_CALLS;
2750
2751         ftrace_startup_enable(command);
2752
2753         /*
2754          * If ftrace is in an undefined state, we just remove ops from list
2755          * to prevent the NULL pointer, instead of totally rolling it back and
2756          * free trampoline, because those actions could cause further damage.
2757          */
2758         if (unlikely(ftrace_disabled)) {
2759                 __unregister_ftrace_function(ops);
2760                 return -ENODEV;
2761         }
2762
2763         ops->flags &= ~FTRACE_OPS_FL_ADDING;
2764
2765         return 0;
2766 }
2767
2768 static int ftrace_shutdown(struct ftrace_ops *ops, int command)
2769 {
2770         int ret;
2771
2772         if (unlikely(ftrace_disabled))
2773                 return -ENODEV;
2774
2775         ret = __unregister_ftrace_function(ops);
2776         if (ret)
2777                 return ret;
2778
2779         ftrace_start_up--;
2780         /*
2781          * Just warn in case of unbalance, no need to kill ftrace, it's not
2782          * critical but the ftrace_call callers may be never nopped again after
2783          * further ftrace uses.
2784          */
2785         WARN_ON_ONCE(ftrace_start_up < 0);
2786
2787         /* Disabling ipmodify never fails */
2788         ftrace_hash_ipmodify_disable(ops);
2789
2790         if (ftrace_hash_rec_disable(ops, 1))
2791                 command |= FTRACE_UPDATE_CALLS;
2792
2793         ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2794
2795         if (saved_ftrace_func != ftrace_trace_function) {
2796                 saved_ftrace_func = ftrace_trace_function;
2797                 command |= FTRACE_UPDATE_TRACE_FUNC;
2798         }
2799
2800         if (!command || !ftrace_enabled) {
2801                 /*
2802                  * If these are dynamic or per_cpu ops, they still
2803                  * need their data freed. Since, function tracing is
2804                  * not currently active, we can just free them
2805                  * without synchronizing all CPUs.
2806                  */
2807                 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
2808                         goto free_ops;
2809
2810                 return 0;
2811         }
2812
2813         /*
2814          * If the ops uses a trampoline, then it needs to be
2815          * tested first on update.
2816          */
2817         ops->flags |= FTRACE_OPS_FL_REMOVING;
2818         removed_ops = ops;
2819
2820         /* The trampoline logic checks the old hashes */
2821         ops->old_hash.filter_hash = ops->func_hash->filter_hash;
2822         ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
2823
2824         ftrace_run_update_code(command);
2825
2826         /*
2827          * If there's no more ops registered with ftrace, run a
2828          * sanity check to make sure all rec flags are cleared.
2829          */
2830         if (rcu_dereference_protected(ftrace_ops_list,
2831                         lockdep_is_held(&ftrace_lock)) == &ftrace_list_end) {
2832                 struct ftrace_page *pg;
2833                 struct dyn_ftrace *rec;
2834
2835                 do_for_each_ftrace_rec(pg, rec) {
2836                         if (FTRACE_WARN_ON_ONCE(rec->flags & ~FTRACE_FL_DISABLED))
2837                                 pr_warn("  %pS flags:%lx\n",
2838                                         (void *)rec->ip, rec->flags);
2839                 } while_for_each_ftrace_rec();
2840         }
2841
2842         ops->old_hash.filter_hash = NULL;
2843         ops->old_hash.notrace_hash = NULL;
2844
2845         removed_ops = NULL;
2846         ops->flags &= ~FTRACE_OPS_FL_REMOVING;
2847
2848         /*
2849          * Dynamic ops may be freed, we must make sure that all
2850          * callers are done before leaving this function.
2851          * The same goes for freeing the per_cpu data of the per_cpu
2852          * ops.
2853          */
2854         if (ops->flags & FTRACE_OPS_FL_DYNAMIC) {
2855                 /*
2856                  * We need to do a hard force of sched synchronization.
2857                  * This is because we use preempt_disable() to do RCU, but
2858                  * the function tracers can be called where RCU is not watching
2859                  * (like before user_exit()). We can not rely on the RCU
2860                  * infrastructure to do the synchronization, thus we must do it
2861                  * ourselves.
2862                  */
2863                 schedule_on_each_cpu(ftrace_sync);
2864
2865                 /*
2866                  * When the kernel is preeptive, tasks can be preempted
2867                  * while on a ftrace trampoline. Just scheduling a task on
2868                  * a CPU is not good enough to flush them. Calling
2869                  * synchornize_rcu_tasks() will wait for those tasks to
2870                  * execute and either schedule voluntarily or enter user space.
2871                  */
2872                 if (IS_ENABLED(CONFIG_PREEMPT))
2873                         synchronize_rcu_tasks();
2874
2875  free_ops:
2876                 arch_ftrace_trampoline_free(ops);
2877         }
2878
2879         return 0;
2880 }
2881
2882 static void ftrace_startup_sysctl(void)
2883 {
2884         int command;
2885
2886         if (unlikely(ftrace_disabled))
2887                 return;
2888
2889         /* Force update next time */
2890         saved_ftrace_func = NULL;
2891         /* ftrace_start_up is true if we want ftrace running */
2892         if (ftrace_start_up) {
2893                 command = FTRACE_UPDATE_CALLS;
2894                 if (ftrace_graph_active)
2895                         command |= FTRACE_START_FUNC_RET;
2896                 ftrace_startup_enable(command);
2897         }
2898 }
2899
2900 static void ftrace_shutdown_sysctl(void)
2901 {
2902         int command;
2903
2904         if (unlikely(ftrace_disabled))
2905                 return;
2906
2907         /* ftrace_start_up is true if ftrace is running */
2908         if (ftrace_start_up) {
2909                 command = FTRACE_DISABLE_CALLS;
2910                 if (ftrace_graph_active)
2911                         command |= FTRACE_STOP_FUNC_RET;
2912                 ftrace_run_update_code(command);
2913         }
2914 }
2915
2916 static u64              ftrace_update_time;
2917 unsigned long           ftrace_update_tot_cnt;
2918
2919 static inline int ops_traces_mod(struct ftrace_ops *ops)
2920 {
2921         /*
2922          * Filter_hash being empty will default to trace module.
2923          * But notrace hash requires a test of individual module functions.
2924          */
2925         return ftrace_hash_empty(ops->func_hash->filter_hash) &&
2926                 ftrace_hash_empty(ops->func_hash->notrace_hash);
2927 }
2928
2929 /*
2930  * Check if the current ops references the record.
2931  *
2932  * If the ops traces all functions, then it was already accounted for.
2933  * If the ops does not trace the current record function, skip it.
2934  * If the ops ignores the function via notrace filter, skip it.
2935  */
2936 static inline bool
2937 ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2938 {
2939         /* If ops isn't enabled, ignore it */
2940         if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2941                 return false;
2942
2943         /* If ops traces all then it includes this function */
2944         if (ops_traces_mod(ops))
2945                 return true;
2946
2947         /* The function must be in the filter */
2948         if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
2949             !__ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
2950                 return false;
2951
2952         /* If in notrace hash, we ignore it too */
2953         if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
2954                 return false;
2955
2956         return true;
2957 }
2958
2959 static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
2960 {
2961         struct ftrace_page *pg;
2962         struct dyn_ftrace *p;
2963         u64 start, stop;
2964         unsigned long update_cnt = 0;
2965         unsigned long rec_flags = 0;
2966         int i;
2967
2968         start = ftrace_now(raw_smp_processor_id());
2969
2970         /*
2971          * When a module is loaded, this function is called to convert
2972          * the calls to mcount in its text to nops, and also to create
2973          * an entry in the ftrace data. Now, if ftrace is activated
2974          * after this call, but before the module sets its text to
2975          * read-only, the modification of enabling ftrace can fail if
2976          * the read-only is done while ftrace is converting the calls.
2977          * To prevent this, the module's records are set as disabled
2978          * and will be enabled after the call to set the module's text
2979          * to read-only.
2980          */
2981         if (mod)
2982                 rec_flags |= FTRACE_FL_DISABLED;
2983
2984         for (pg = new_pgs; pg; pg = pg->next) {
2985
2986                 for (i = 0; i < pg->index; i++) {
2987
2988                         /* If something went wrong, bail without enabling anything */
2989                         if (unlikely(ftrace_disabled))
2990                                 return -1;
2991
2992                         p = &pg->records[i];
2993                         p->flags = rec_flags;
2994
2995                         /*
2996                          * Do the initial record conversion from mcount jump
2997                          * to the NOP instructions.
2998                          */
2999                         if (!__is_defined(CC_USING_NOP_MCOUNT) &&
3000                             !ftrace_code_disable(mod, p))
3001                                 break;
3002
3003                         update_cnt++;
3004                 }
3005         }
3006
3007         stop = ftrace_now(raw_smp_processor_id());
3008         ftrace_update_time = stop - start;
3009         ftrace_update_tot_cnt += update_cnt;
3010
3011         return 0;
3012 }
3013
3014 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
3015 {
3016         int order;
3017         int cnt;
3018
3019         if (WARN_ON(!count))
3020                 return -EINVAL;
3021
3022         order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
3023
3024         /*
3025          * We want to fill as much as possible. No more than a page
3026          * may be empty.
3027          */
3028         while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
3029                 order--;
3030
3031  again:
3032         pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
3033
3034         if (!pg->records) {
3035                 /* if we can't allocate this size, try something smaller */
3036                 if (!order)
3037                         return -ENOMEM;
3038                 order--;
3039                 goto again;
3040         }
3041
3042         cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
3043         pg->size = cnt;
3044
3045         if (cnt > count)
3046                 cnt = count;
3047
3048         return cnt;
3049 }
3050
3051 static struct ftrace_page *
3052 ftrace_allocate_pages(unsigned long num_to_init)
3053 {
3054         struct ftrace_page *start_pg;
3055         struct ftrace_page *pg;
3056         int order;
3057         int cnt;
3058
3059         if (!num_to_init)
3060                 return 0;
3061
3062         start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
3063         if (!pg)
3064                 return NULL;
3065
3066         /*
3067          * Try to allocate as much as possible in one continues
3068          * location that fills in all of the space. We want to
3069          * waste as little space as possible.
3070          */
3071         for (;;) {
3072                 cnt = ftrace_allocate_records(pg, num_to_init);
3073                 if (cnt < 0)
3074                         goto free_pages;
3075
3076                 num_to_init -= cnt;
3077                 if (!num_to_init)
3078                         break;
3079
3080                 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
3081                 if (!pg->next)
3082                         goto free_pages;
3083
3084                 pg = pg->next;
3085         }
3086
3087         return start_pg;
3088
3089  free_pages:
3090         pg = start_pg;
3091         while (pg) {
3092                 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3093                 free_pages((unsigned long)pg->records, order);
3094                 start_pg = pg->next;
3095                 kfree(pg);
3096                 pg = start_pg;
3097         }
3098         pr_info("ftrace: FAILED to allocate memory for functions\n");
3099         return NULL;
3100 }
3101
3102 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
3103
3104 struct ftrace_iterator {
3105         loff_t                          pos;
3106         loff_t                          func_pos;
3107         loff_t                          mod_pos;
3108         struct ftrace_page              *pg;
3109         struct dyn_ftrace               *func;
3110         struct ftrace_func_probe        *probe;
3111         struct ftrace_func_entry        *probe_entry;
3112         struct trace_parser             parser;
3113         struct ftrace_hash              *hash;
3114         struct ftrace_ops               *ops;
3115         struct trace_array              *tr;
3116         struct list_head                *mod_list;
3117         int                             pidx;
3118         int                             idx;
3119         unsigned                        flags;
3120 };
3121
3122 static void *
3123 t_probe_next(struct seq_file *m, loff_t *pos)
3124 {
3125         struct ftrace_iterator *iter = m->private;
3126         struct trace_array *tr = iter->ops->private;
3127         struct list_head *func_probes;
3128         struct ftrace_hash *hash;
3129         struct list_head *next;
3130         struct hlist_node *hnd = NULL;
3131         struct hlist_head *hhd;
3132         int size;
3133
3134         (*pos)++;
3135         iter->pos = *pos;
3136
3137         if (!tr)
3138                 return NULL;
3139
3140         func_probes = &tr->func_probes;
3141         if (list_empty(func_probes))
3142                 return NULL;
3143
3144         if (!iter->probe) {
3145                 next = func_probes->next;
3146                 iter->probe = list_entry(next, struct ftrace_func_probe, list);
3147         }
3148
3149         if (iter->probe_entry)
3150                 hnd = &iter->probe_entry->hlist;
3151
3152         hash = iter->probe->ops.func_hash->filter_hash;
3153
3154         /*
3155          * A probe being registered may temporarily have an empty hash
3156          * and it's at the end of the func_probes list.
3157          */
3158         if (!hash || hash == EMPTY_HASH)
3159                 return NULL;
3160
3161         size = 1 << hash->size_bits;
3162
3163  retry:
3164         if (iter->pidx >= size) {
3165                 if (iter->probe->list.next == func_probes)
3166                         return NULL;
3167                 next = iter->probe->list.next;
3168                 iter->probe = list_entry(next, struct ftrace_func_probe, list);
3169                 hash = iter->probe->ops.func_hash->filter_hash;
3170                 size = 1 << hash->size_bits;
3171                 iter->pidx = 0;
3172         }
3173
3174         hhd = &hash->buckets[iter->pidx];
3175
3176         if (hlist_empty(hhd)) {
3177                 iter->pidx++;
3178                 hnd = NULL;
3179                 goto retry;
3180         }
3181
3182         if (!hnd)
3183                 hnd = hhd->first;
3184         else {
3185                 hnd = hnd->next;
3186                 if (!hnd) {
3187                         iter->pidx++;
3188                         goto retry;
3189                 }
3190         }
3191
3192         if (WARN_ON_ONCE(!hnd))
3193                 return NULL;
3194
3195         iter->probe_entry = hlist_entry(hnd, struct ftrace_func_entry, hlist);
3196
3197         return iter;
3198 }
3199
3200 static void *t_probe_start(struct seq_file *m, loff_t *pos)
3201 {
3202         struct ftrace_iterator *iter = m->private;
3203         void *p = NULL;
3204         loff_t l;
3205
3206         if (!(iter->flags & FTRACE_ITER_DO_PROBES))
3207                 return NULL;
3208
3209         if (iter->mod_pos > *pos)
3210                 return NULL;
3211
3212         iter->probe = NULL;
3213         iter->probe_entry = NULL;
3214         iter->pidx = 0;
3215         for (l = 0; l <= (*pos - iter->mod_pos); ) {
3216                 p = t_probe_next(m, &l);
3217                 if (!p)
3218                         break;
3219         }
3220         if (!p)
3221                 return NULL;
3222
3223         /* Only set this if we have an item */
3224         iter->flags |= FTRACE_ITER_PROBE;
3225
3226         return iter;
3227 }
3228
3229 static int
3230 t_probe_show(struct seq_file *m, struct ftrace_iterator *iter)
3231 {
3232         struct ftrace_func_entry *probe_entry;
3233         struct ftrace_probe_ops *probe_ops;
3234         struct ftrace_func_probe *probe;
3235
3236         probe = iter->probe;
3237         probe_entry = iter->probe_entry;
3238
3239         if (WARN_ON_ONCE(!probe || !probe_entry))
3240                 return -EIO;
3241
3242         probe_ops = probe->probe_ops;
3243
3244         if (probe_ops->print)
3245                 return probe_ops->print(m, probe_entry->ip, probe_ops, probe->data);
3246
3247         seq_printf(m, "%ps:%ps\n", (void *)probe_entry->ip,
3248                    (void *)probe_ops->func);
3249
3250         return 0;
3251 }
3252
3253 static void *
3254 t_mod_next(struct seq_file *m, loff_t *pos)
3255 {
3256         struct ftrace_iterator *iter = m->private;
3257         struct trace_array *tr = iter->tr;
3258
3259         (*pos)++;
3260         iter->pos = *pos;
3261
3262         iter->mod_list = iter->mod_list->next;
3263
3264         if (iter->mod_list == &tr->mod_trace ||
3265             iter->mod_list == &tr->mod_notrace) {
3266                 iter->flags &= ~FTRACE_ITER_MOD;
3267                 return NULL;
3268         }
3269
3270         iter->mod_pos = *pos;
3271
3272         return iter;
3273 }
3274
3275 static void *t_mod_start(struct seq_file *m, loff_t *pos)
3276 {
3277         struct ftrace_iterator *iter = m->private;
3278         void *p = NULL;
3279         loff_t l;
3280
3281         if (iter->func_pos > *pos)
3282                 return NULL;
3283
3284         iter->mod_pos = iter->func_pos;
3285
3286         /* probes are only available if tr is set */
3287         if (!iter->tr)
3288                 return NULL;
3289
3290         for (l = 0; l <= (*pos - iter->func_pos); ) {
3291                 p = t_mod_next(m, &l);
3292                 if (!p)
3293                         break;
3294         }
3295         if (!p) {
3296                 iter->flags &= ~FTRACE_ITER_MOD;
3297                 return t_probe_start(m, pos);
3298         }
3299
3300         /* Only set this if we have an item */
3301         iter->flags |= FTRACE_ITER_MOD;
3302
3303         return iter;
3304 }
3305
3306 static int
3307 t_mod_show(struct seq_file *m, struct ftrace_iterator *iter)
3308 {
3309         struct ftrace_mod_load *ftrace_mod;
3310         struct trace_array *tr = iter->tr;
3311
3312         if (WARN_ON_ONCE(!iter->mod_list) ||
3313                          iter->mod_list == &tr->mod_trace ||
3314                          iter->mod_list == &tr->mod_notrace)
3315                 return -EIO;
3316
3317         ftrace_mod = list_entry(iter->mod_list, struct ftrace_mod_load, list);
3318
3319         if (ftrace_mod->func)
3320                 seq_printf(m, "%s", ftrace_mod->func);
3321         else
3322                 seq_putc(m, '*');
3323
3324         seq_printf(m, ":mod:%s\n", ftrace_mod->module);
3325
3326         return 0;
3327 }
3328
3329 static void *
3330 t_func_next(struct seq_file *m, loff_t *pos)
3331 {
3332         struct ftrace_iterator *iter = m->private;
3333         struct dyn_ftrace *rec = NULL;
3334
3335         (*pos)++;
3336
3337  retry:
3338         if (iter->idx >= iter->pg->index) {
3339                 if (iter->pg->next) {
3340                         iter->pg = iter->pg->next;
3341                         iter->idx = 0;
3342                         goto retry;
3343                 }
3344         } else {
3345                 rec = &iter->pg->records[iter->idx++];
3346                 if (((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3347                      !ftrace_lookup_ip(iter->hash, rec->ip)) ||
3348
3349                     ((iter->flags & FTRACE_ITER_ENABLED) &&
3350                      !(rec->flags & FTRACE_FL_ENABLED))) {
3351
3352                         rec = NULL;
3353                         goto retry;
3354                 }
3355         }
3356
3357         if (!rec)
3358                 return NULL;
3359
3360         iter->pos = iter->func_pos = *pos;
3361         iter->func = rec;
3362
3363         return iter;
3364 }
3365
3366 static void *
3367 t_next(struct seq_file *m, void *v, loff_t *pos)
3368 {
3369         struct ftrace_iterator *iter = m->private;
3370         loff_t l = *pos; /* t_probe_start() must use original pos */
3371         void *ret;
3372
3373         if (unlikely(ftrace_disabled))
3374                 return NULL;
3375
3376         if (iter->flags & FTRACE_ITER_PROBE)
3377                 return t_probe_next(m, pos);
3378
3379         if (iter->flags & FTRACE_ITER_MOD)
3380                 return t_mod_next(m, pos);
3381
3382         if (iter->flags & FTRACE_ITER_PRINTALL) {
3383                 /* next must increment pos, and t_probe_start does not */
3384                 (*pos)++;
3385                 return t_mod_start(m, &l);
3386         }
3387
3388         ret = t_func_next(m, pos);
3389
3390         if (!ret)
3391                 return t_mod_start(m, &l);
3392
3393         return ret;
3394 }
3395
3396 static void reset_iter_read(struct ftrace_iterator *iter)
3397 {
3398         iter->pos = 0;
3399         iter->func_pos = 0;
3400         iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_PROBE | FTRACE_ITER_MOD);
3401 }
3402
3403 static void *t_start(struct seq_file *m, loff_t *pos)
3404 {
3405         struct ftrace_iterator *iter = m->private;
3406         void *p = NULL;
3407         loff_t l;
3408
3409         mutex_lock(&ftrace_lock);
3410
3411         if (unlikely(ftrace_disabled))
3412                 return NULL;
3413
3414         /*
3415          * If an lseek was done, then reset and start from beginning.
3416          */
3417         if (*pos < iter->pos)
3418                 reset_iter_read(iter);
3419
3420         /*
3421          * For set_ftrace_filter reading, if we have the filter
3422          * off, we can short cut and just print out that all
3423          * functions are enabled.
3424          */
3425         if ((iter->flags & (FTRACE_ITER_FILTER | FTRACE_ITER_NOTRACE)) &&
3426             ftrace_hash_empty(iter->hash)) {
3427                 iter->func_pos = 1; /* Account for the message */
3428                 if (*pos > 0)
3429                         return t_mod_start(m, pos);
3430                 iter->flags |= FTRACE_ITER_PRINTALL;
3431                 /* reset in case of seek/pread */
3432                 iter->flags &= ~FTRACE_ITER_PROBE;
3433                 return iter;
3434         }
3435
3436         if (iter->flags & FTRACE_ITER_MOD)
3437                 return t_mod_start(m, pos);
3438
3439         /*
3440          * Unfortunately, we need to restart at ftrace_pages_start
3441          * every time we let go of the ftrace_mutex. This is because
3442          * those pointers can change without the lock.
3443          */
3444         iter->pg = ftrace_pages_start;
3445         iter->idx = 0;
3446         for (l = 0; l <= *pos; ) {
3447                 p = t_func_next(m, &l);
3448                 if (!p)
3449                         break;
3450         }
3451
3452         if (!p)
3453                 return t_mod_start(m, pos);
3454
3455         return iter;
3456 }
3457
3458 static void t_stop(struct seq_file *m, void *p)
3459 {
3460         mutex_unlock(&ftrace_lock);
3461 }
3462
3463 void * __weak
3464 arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3465 {
3466         return NULL;
3467 }
3468
3469 static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
3470                                 struct dyn_ftrace *rec)
3471 {
3472         void *ptr;
3473
3474         ptr = arch_ftrace_trampoline_func(ops, rec);
3475         if (ptr)
3476                 seq_printf(m, " ->%pS", ptr);
3477 }
3478
3479 static int t_show(struct seq_file *m, void *v)
3480 {
3481         struct ftrace_iterator *iter = m->private;
3482         struct dyn_ftrace *rec;
3483
3484         if (iter->flags & FTRACE_ITER_PROBE)
3485                 return t_probe_show(m, iter);
3486
3487         if (iter->flags & FTRACE_ITER_MOD)
3488                 return t_mod_show(m, iter);
3489
3490         if (iter->flags & FTRACE_ITER_PRINTALL) {
3491                 if (iter->flags & FTRACE_ITER_NOTRACE)
3492                         seq_puts(m, "#### no functions disabled ####\n");
3493                 else
3494                         seq_puts(m, "#### all functions enabled ####\n");
3495                 return 0;
3496         }
3497
3498         rec = iter->func;
3499
3500         if (!rec)
3501                 return 0;
3502
3503         seq_printf(m, "%ps", (void *)rec->ip);
3504         if (iter->flags & FTRACE_ITER_ENABLED) {
3505                 struct ftrace_ops *ops;
3506
3507                 seq_printf(m, " (%ld)%s%s",
3508                            ftrace_rec_count(rec),
3509                            rec->flags & FTRACE_FL_REGS ? " R" : "  ",
3510                            rec->flags & FTRACE_FL_IPMODIFY ? " I" : "  ");
3511                 if (rec->flags & FTRACE_FL_TRAMP_EN) {
3512                         ops = ftrace_find_tramp_ops_any(rec);
3513                         if (ops) {
3514                                 do {
3515                                         seq_printf(m, "\ttramp: %pS (%pS)",
3516                                                    (void *)ops->trampoline,
3517                                                    (void *)ops->func);
3518                                         add_trampoline_func(m, ops, rec);
3519                                         ops = ftrace_find_tramp_ops_next(rec, ops);
3520                                 } while (ops);
3521                         } else
3522                                 seq_puts(m, "\ttramp: ERROR!");
3523                 } else {
3524                         add_trampoline_func(m, NULL, rec);
3525                 }
3526         }       
3527
3528         seq_putc(m, '\n');
3529
3530         return 0;
3531 }
3532
3533 static const struct seq_operations show_ftrace_seq_ops = {
3534         .start = t_start,
3535         .next = t_next,
3536         .stop = t_stop,
3537         .show = t_show,
3538 };
3539
3540 static int
3541 ftrace_avail_open(struct inode *inode, struct file *file)
3542 {
3543         struct ftrace_iterator *iter;
3544
3545         if (unlikely(ftrace_disabled))
3546                 return -ENODEV;
3547
3548         iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3549         if (!iter)
3550                 return -ENOMEM;
3551
3552         iter->pg = ftrace_pages_start;
3553         iter->ops = &global_ops;
3554
3555         return 0;
3556 }
3557
3558 static int
3559 ftrace_enabled_open(struct inode *inode, struct file *file)
3560 {
3561         struct ftrace_iterator *iter;
3562
3563         iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3564         if (!iter)
3565                 return -ENOMEM;
3566
3567         iter->pg = ftrace_pages_start;
3568         iter->flags = FTRACE_ITER_ENABLED;
3569         iter->ops = &global_ops;
3570
3571         return 0;
3572 }
3573
3574 /**
3575  * ftrace_regex_open - initialize function tracer filter files
3576  * @ops: The ftrace_ops that hold the hash filters
3577  * @flag: The type of filter to process
3578  * @inode: The inode, usually passed in to your open routine
3579  * @file: The file, usually passed in to your open routine
3580  *
3581  * ftrace_regex_open() initializes the filter files for the
3582  * @ops. Depending on @flag it may process the filter hash or
3583  * the notrace hash of @ops. With this called from the open
3584  * routine, you can use ftrace_filter_write() for the write
3585  * routine if @flag has FTRACE_ITER_FILTER set, or
3586  * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
3587  * tracing_lseek() should be used as the lseek routine, and
3588  * release must call ftrace_regex_release().
3589  */
3590 int
3591 ftrace_regex_open(struct ftrace_ops *ops, int flag,
3592                   struct inode *inode, struct file *file)
3593 {
3594         struct ftrace_iterator *iter;
3595         struct ftrace_hash *hash;
3596         struct list_head *mod_head;
3597         struct trace_array *tr = ops->private;
3598         int ret = -ENOMEM;
3599
3600         ftrace_ops_init(ops);
3601
3602         if (unlikely(ftrace_disabled))
3603                 return -ENODEV;
3604
3605         if (tr && trace_array_get(tr) < 0)
3606                 return -ENODEV;
3607
3608         iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3609         if (!iter)
3610                 goto out;
3611
3612         if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX))
3613                 goto out;
3614
3615         iter->ops = ops;
3616         iter->flags = flag;
3617         iter->tr = tr;
3618
3619         mutex_lock(&ops->func_hash->regex_lock);
3620
3621         if (flag & FTRACE_ITER_NOTRACE) {
3622                 hash = ops->func_hash->notrace_hash;
3623                 mod_head = tr ? &tr->mod_notrace : NULL;
3624         } else {
3625                 hash = ops->func_hash->filter_hash;
3626                 mod_head = tr ? &tr->mod_trace : NULL;
3627         }
3628
3629         iter->mod_list = mod_head;
3630
3631         if (file->f_mode & FMODE_WRITE) {
3632                 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3633
3634                 if (file->f_flags & O_TRUNC) {
3635                         iter->hash = alloc_ftrace_hash(size_bits);
3636                         clear_ftrace_mod_list(mod_head);
3637                 } else {
3638                         iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
3639                 }
3640
3641                 if (!iter->hash) {
3642                         trace_parser_put(&iter->parser);
3643                         goto out_unlock;
3644                 }
3645         } else
3646                 iter->hash = hash;
3647
3648         ret = 0;
3649
3650         if (file->f_mode & FMODE_READ) {
3651                 iter->pg = ftrace_pages_start;
3652
3653                 ret = seq_open(file, &show_ftrace_seq_ops);
3654                 if (!ret) {
3655                         struct seq_file *m = file->private_data;
3656                         m->private = iter;
3657                 } else {
3658                         /* Failed */
3659                         free_ftrace_hash(iter->hash);
3660                         trace_parser_put(&iter->parser);
3661                 }
3662         } else
3663                 file->private_data = iter;
3664
3665  out_unlock:
3666         mutex_unlock(&ops->func_hash->regex_lock);
3667
3668  out:
3669         if (ret) {
3670                 kfree(iter);
3671                 if (tr)
3672                         trace_array_put(tr);
3673         }
3674
3675         return ret;
3676 }
3677
3678 static int
3679 ftrace_filter_open(struct inode *inode, struct file *file)
3680 {
3681         struct ftrace_ops *ops = inode->i_private;
3682
3683         return ftrace_regex_open(ops,
3684                         FTRACE_ITER_FILTER | FTRACE_ITER_DO_PROBES,
3685                         inode, file);
3686 }
3687
3688 static int
3689 ftrace_notrace_open(struct inode *inode, struct file *file)
3690 {
3691         struct ftrace_ops *ops = inode->i_private;
3692
3693         return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
3694                                  inode, file);
3695 }
3696
3697 /* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */
3698 struct ftrace_glob {
3699         char *search;
3700         unsigned len;
3701         int type;
3702 };
3703
3704 /*
3705  * If symbols in an architecture don't correspond exactly to the user-visible
3706  * name of what they represent, it is possible to define this function to
3707  * perform the necessary adjustments.
3708 */
3709 char * __weak arch_ftrace_match_adjust(char *str, const char *search)
3710 {
3711         return str;
3712 }
3713
3714 static int ftrace_match(char *str, struct ftrace_glob *g)
3715 {
3716         int matched = 0;
3717         int slen;
3718
3719         str = arch_ftrace_match_adjust(str, g->search);
3720
3721         switch (g->type) {
3722         case MATCH_FULL:
3723                 if (strcmp(str, g->search) == 0)
3724                         matched = 1;
3725                 break;
3726         case MATCH_FRONT_ONLY:
3727                 if (strncmp(str, g->search, g->len) == 0)
3728                         matched = 1;
3729                 break;
3730         case MATCH_MIDDLE_ONLY:
3731                 if (strstr(str, g->search))
3732                         matched = 1;
3733                 break;
3734         case MATCH_END_ONLY:
3735                 slen = strlen(str);
3736                 if (slen >= g->len &&
3737                     memcmp(str + slen - g->len, g->search, g->len) == 0)
3738                         matched = 1;
3739                 break;
3740         case MATCH_GLOB:
3741                 if (glob_match(g->search, str))
3742                         matched = 1;
3743                 break;
3744         }
3745
3746         return matched;
3747 }
3748
3749 static int
3750 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int clear_filter)
3751 {
3752         struct ftrace_func_entry *entry;
3753         int ret = 0;
3754
3755         entry = ftrace_lookup_ip(hash, rec->ip);
3756         if (clear_filter) {
3757                 /* Do nothing if it doesn't exist */
3758                 if (!entry)
3759                         return 0;
3760
3761                 free_hash_entry(hash, entry);
3762         } else {
3763                 /* Do nothing if it exists */
3764                 if (entry)
3765                         return 0;
3766
3767                 ret = add_hash_entry(hash, rec->ip);
3768         }
3769         return ret;
3770 }
3771
3772 static int
3773 ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g,
3774                 struct ftrace_glob *mod_g, int exclude_mod)
3775 {
3776         char str[KSYM_SYMBOL_LEN];
3777         char *modname;
3778
3779         kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3780
3781         if (mod_g) {
3782                 int mod_matches = (modname) ? ftrace_match(modname, mod_g) : 0;
3783
3784                 /* blank module name to match all modules */
3785                 if (!mod_g->len) {
3786                         /* blank module globbing: modname xor exclude_mod */
3787                         if (!exclude_mod != !modname)
3788                                 goto func_match;
3789                         return 0;
3790                 }
3791
3792                 /*
3793                  * exclude_mod is set to trace everything but the given
3794                  * module. If it is set and the module matches, then
3795                  * return 0. If it is not set, and the module doesn't match
3796                  * also return 0. Otherwise, check the function to see if
3797                  * that matches.
3798                  */
3799                 if (!mod_matches == !exclude_mod)
3800                         return 0;
3801 func_match:
3802                 /* blank search means to match all funcs in the mod */
3803                 if (!func_g->len)
3804                         return 1;
3805         }
3806
3807         return ftrace_match(str, func_g);
3808 }
3809
3810 static int
3811 match_records(struct ftrace_hash *hash, char *func, int len, char *mod)
3812 {
3813         struct ftrace_page *pg;
3814         struct dyn_ftrace *rec;
3815         struct ftrace_glob func_g = { .type = MATCH_FULL };
3816         struct ftrace_glob mod_g = { .type = MATCH_FULL };
3817         struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL;
3818         int exclude_mod = 0;
3819         int found = 0;
3820         int ret;
3821         int clear_filter = 0;
3822
3823         if (func) {
3824                 func_g.type = filter_parse_regex(func, len, &func_g.search,
3825                                                  &clear_filter);
3826                 func_g.len = strlen(func_g.search);
3827         }
3828
3829         if (mod) {
3830                 mod_g.type = filter_parse_regex(mod, strlen(mod),
3831                                 &mod_g.search, &exclude_mod);
3832                 mod_g.len = strlen(mod_g.search);
3833         }
3834
3835         mutex_lock(&ftrace_lock);
3836
3837         if (unlikely(ftrace_disabled))
3838                 goto out_unlock;
3839
3840         do_for_each_ftrace_rec(pg, rec) {
3841
3842                 if (rec->flags & FTRACE_FL_DISABLED)
3843                         continue;
3844
3845                 if (ftrace_match_record(rec, &func_g, mod_match, exclude_mod)) {
3846                         ret = enter_record(hash, rec, clear_filter);
3847                         if (ret < 0) {
3848                                 found = ret;
3849                                 goto out_unlock;
3850                         }
3851                         found = 1;
3852                 }
3853         } while_for_each_ftrace_rec();
3854  out_unlock:
3855         mutex_unlock(&ftrace_lock);
3856
3857         return found;
3858 }
3859
3860 static int
3861 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
3862 {
3863         return match_records(hash, buff, len, NULL);
3864 }
3865
3866 static void ftrace_ops_update_code(struct ftrace_ops *ops,
3867                                    struct ftrace_ops_hash *old_hash)
3868 {
3869         struct ftrace_ops *op;
3870
3871         if (!ftrace_enabled)
3872                 return;
3873
3874         if (ops->flags & FTRACE_OPS_FL_ENABLED) {
3875                 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
3876                 return;
3877         }
3878
3879         /*
3880          * If this is the shared global_ops filter, then we need to
3881          * check if there is another ops that shares it, is enabled.
3882          * If so, we still need to run the modify code.
3883          */
3884         if (ops->func_hash != &global_ops.local_hash)
3885                 return;
3886
3887         do_for_each_ftrace_op(op, ftrace_ops_list) {
3888                 if (op->func_hash == &global_ops.local_hash &&
3889                     op->flags & FTRACE_OPS_FL_ENABLED) {
3890                         ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash);
3891                         /* Only need to do this once */
3892                         return;
3893                 }
3894         } while_for_each_ftrace_op(op);
3895 }
3896
3897 static int ftrace_hash_move_and_update_ops(struct ftrace_ops *ops,
3898                                            struct ftrace_hash **orig_hash,
3899                                            struct ftrace_hash *hash,
3900                                            int enable)
3901 {
3902         struct ftrace_ops_hash old_hash_ops;
3903         struct ftrace_hash *old_hash;
3904         int ret;
3905
3906         old_hash = *orig_hash;
3907         old_hash_ops.filter_hash = ops->func_hash->filter_hash;
3908         old_hash_ops.notrace_hash = ops->func_hash->notrace_hash;
3909         ret = ftrace_hash_move(ops, enable, orig_hash, hash);
3910         if (!ret) {
3911                 ftrace_ops_update_code(ops, &old_hash_ops);
3912                 free_ftrace_hash_rcu(old_hash);
3913         }
3914         return ret;
3915 }
3916
3917 static bool module_exists(const char *module)
3918 {
3919         /* All modules have the symbol __this_module */
3920         const char this_mod[] = "__this_module";
3921         char modname[MAX_PARAM_PREFIX_LEN + sizeof(this_mod) + 2];
3922         unsigned long val;
3923         int n;
3924
3925         n = snprintf(modname, sizeof(modname), "%s:%s", module, this_mod);
3926
3927         if (n > sizeof(modname) - 1)
3928                 return false;
3929
3930         val = module_kallsyms_lookup_name(modname);
3931         return val != 0;
3932 }
3933
3934 static int cache_mod(struct trace_array *tr,
3935                      const char *func, char *module, int enable)
3936 {
3937         struct ftrace_mod_load *ftrace_mod, *n;
3938         struct list_head *head = enable ? &tr->mod_trace : &tr->mod_notrace;
3939         int ret;
3940
3941         mutex_lock(&ftrace_lock);
3942
3943         /* We do not cache inverse filters */
3944         if (func[0] == '!') {
3945                 func++;
3946                 ret = -EINVAL;
3947
3948                 /* Look to remove this hash */
3949                 list_for_each_entry_safe(ftrace_mod, n, head, list) {
3950                         if (strcmp(ftrace_mod->module, module) != 0)
3951                                 continue;
3952
3953                         /* no func matches all */
3954                         if (strcmp(func, "*") == 0 ||
3955                             (ftrace_mod->func &&
3956                              strcmp(ftrace_mod->func, func) == 0)) {
3957                                 ret = 0;
3958                                 free_ftrace_mod(ftrace_mod);
3959                                 continue;
3960                         }
3961                 }
3962                 goto out;
3963         }
3964
3965         ret = -EINVAL;
3966         /* We only care about modules that have not been loaded yet */
3967         if (module_exists(module))
3968                 goto out;
3969
3970         /* Save this string off, and execute it when the module is loaded */
3971         ret = ftrace_add_mod(tr, func, module, enable);
3972  out:
3973         mutex_unlock(&ftrace_lock);
3974
3975         return ret;
3976 }
3977
3978 static int
3979 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3980                  int reset, int enable);
3981
3982 #ifdef CONFIG_MODULES
3983 static void process_mod_list(struct list_head *head, struct ftrace_ops *ops,
3984                              char *mod, bool enable)
3985 {
3986         struct ftrace_mod_load *ftrace_mod, *n;
3987         struct ftrace_hash **orig_hash, *new_hash;
3988         LIST_HEAD(process_mods);
3989         char *func;
3990         int ret;
3991
3992         mutex_lock(&ops->func_hash->regex_lock);
3993
3994         if (enable)
3995                 orig_hash = &ops->func_hash->filter_hash;
3996         else
3997                 orig_hash = &ops->func_hash->notrace_hash;
3998
3999         new_hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS,
4000                                               *orig_hash);
4001         if (!new_hash)
4002                 goto out; /* warn? */
4003
4004         mutex_lock(&ftrace_lock);
4005
4006         list_for_each_entry_safe(ftrace_mod, n, head, list) {
4007
4008                 if (strcmp(ftrace_mod->module, mod) != 0)
4009                         continue;
4010
4011                 if (ftrace_mod->func)
4012                         func = kstrdup(ftrace_mod->func, GFP_KERNEL);
4013                 else
4014                         func = kstrdup("*", GFP_KERNEL);
4015
4016                 if (!func) /* warn? */
4017                         continue;
4018
4019                 list_del(&ftrace_mod->list);
4020                 list_add(&ftrace_mod->list, &process_mods);
4021
4022                 /* Use the newly allocated func, as it may be "*" */
4023                 kfree(ftrace_mod->func);
4024                 ftrace_mod->func = func;
4025         }
4026
4027         mutex_unlock(&ftrace_lock);
4028
4029         list_for_each_entry_safe(ftrace_mod, n, &process_mods, list) {
4030
4031                 func = ftrace_mod->func;
4032
4033                 /* Grabs ftrace_lock, which is why we have this extra step */
4034                 match_records(new_hash, func, strlen(func), mod);
4035                 free_ftrace_mod(ftrace_mod);
4036         }
4037
4038         if (enable && list_empty(head))
4039                 new_hash->flags &= ~FTRACE_HASH_FL_MOD;
4040
4041         mutex_lock(&ftrace_lock);
4042
4043         ret = ftrace_hash_move_and_update_ops(ops, orig_hash,
4044                                               new_hash, enable);
4045         mutex_unlock(&ftrace_lock);
4046
4047  out:
4048         mutex_unlock(&ops->func_hash->regex_lock);
4049
4050         free_ftrace_hash(new_hash);
4051 }
4052
4053 static void process_cached_mods(const char *mod_name)
4054 {
4055         struct trace_array *tr;
4056         char *mod;
4057
4058         mod = kstrdup(mod_name, GFP_KERNEL);
4059         if (!mod)
4060                 return;
4061
4062         mutex_lock(&trace_types_lock);
4063         list_for_each_entry(tr, &ftrace_trace_arrays, list) {
4064                 if (!list_empty(&tr->mod_trace))
4065                         process_mod_list(&tr->mod_trace, tr->ops, mod, true);
4066                 if (!list_empty(&tr->mod_notrace))
4067                         process_mod_list(&tr->mod_notrace, tr->ops, mod, false);
4068         }
4069         mutex_unlock(&trace_types_lock);
4070
4071         kfree(mod);
4072 }
4073 #endif
4074
4075 /*
4076  * We register the module command as a template to show others how
4077  * to register the a command as well.
4078  */
4079
4080 static int
4081 ftrace_mod_callback(struct trace_array *tr, struct ftrace_hash *hash,
4082                     char *func_orig, char *cmd, char *module, int enable)
4083 {
4084         char *func;
4085         int ret;
4086
4087         /* match_records() modifies func, and we need the original */
4088         func = kstrdup(func_orig, GFP_KERNEL);
4089         if (!func)
4090                 return -ENOMEM;
4091
4092         /*
4093          * cmd == 'mod' because we only registered this func
4094          * for the 'mod' ftrace_func_command.
4095          * But if you register one func with multiple commands,
4096          * you can tell which command was used by the cmd
4097          * parameter.
4098          */
4099         ret = match_records(hash, func, strlen(func), module);
4100         kfree(func);
4101
4102         if (!ret)
4103                 return cache_mod(tr, func_orig, module, enable);
4104         if (ret < 0)
4105                 return ret;
4106         return 0;
4107 }
4108
4109 static struct ftrace_func_command ftrace_mod_cmd = {
4110         .name                   = "mod",
4111         .func                   = ftrace_mod_callback,
4112 };
4113
4114 static int __init ftrace_mod_cmd_init(void)
4115 {
4116         return register_ftrace_command(&ftrace_mod_cmd);
4117 }
4118 core_initcall(ftrace_mod_cmd_init);
4119
4120 static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
4121                                       struct ftrace_ops *op, struct pt_regs *pt_regs)
4122 {
4123         struct ftrace_probe_ops *probe_ops;
4124         struct ftrace_func_probe *probe;
4125
4126         probe = container_of(op, struct ftrace_func_probe, ops);
4127         probe_ops = probe->probe_ops;
4128
4129         /*
4130          * Disable preemption for these calls to prevent a RCU grace
4131          * period. This syncs the hash iteration and freeing of items
4132          * on the hash. rcu_read_lock is too dangerous here.
4133          */
4134         preempt_disable_notrace();
4135         probe_ops->func(ip, parent_ip, probe->tr, probe_ops, probe->data);
4136         preempt_enable_notrace();
4137 }
4138
4139 struct ftrace_func_map {
4140         struct ftrace_func_entry        entry;
4141         void                            *data;
4142 };
4143
4144 struct ftrace_func_mapper {
4145         struct ftrace_hash              hash;
4146 };
4147
4148 /**
4149  * allocate_ftrace_func_mapper - allocate a new ftrace_func_mapper
4150  *
4151  * Returns a ftrace_func_mapper descriptor that can be used to map ips to data.
4152  */
4153 struct ftrace_func_mapper *allocate_ftrace_func_mapper(void)
4154 {
4155         struct ftrace_hash *hash;
4156
4157         /*
4158          * The mapper is simply a ftrace_hash, but since the entries
4159          * in the hash are not ftrace_func_entry type, we define it
4160          * as a separate structure.
4161          */
4162         hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4163         return (struct ftrace_func_mapper *)hash;
4164 }
4165
4166 /**
4167  * ftrace_func_mapper_find_ip - Find some data mapped to an ip
4168  * @mapper: The mapper that has the ip maps
4169  * @ip: the instruction pointer to find the data for
4170  *
4171  * Returns the data mapped to @ip if found otherwise NULL. The return
4172  * is actually the address of the mapper data pointer. The address is
4173  * returned for use cases where the data is no bigger than a long, and
4174  * the user can use the data pointer as its data instead of having to
4175  * allocate more memory for the reference.
4176  */
4177 void **ftrace_func_mapper_find_ip(struct ftrace_func_mapper *mapper,
4178                                   unsigned long ip)
4179 {
4180         struct ftrace_func_entry *entry;
4181         struct ftrace_func_map *map;
4182
4183         entry = ftrace_lookup_ip(&mapper->hash, ip);
4184         if (!entry)
4185                 return NULL;
4186
4187         map = (struct ftrace_func_map *)entry;
4188         return &map->data;
4189 }
4190
4191 /**
4192  * ftrace_func_mapper_add_ip - Map some data to an ip
4193  * @mapper: The mapper that has the ip maps
4194  * @ip: The instruction pointer address to map @data to
4195  * @data: The data to map to @ip
4196  *
4197  * Returns 0 on succes otherwise an error.
4198  */
4199 int ftrace_func_mapper_add_ip(struct ftrace_func_mapper *mapper,
4200                               unsigned long ip, void *data)
4201 {
4202         struct ftrace_func_entry *entry;
4203         struct ftrace_func_map *map;
4204
4205         entry = ftrace_lookup_ip(&mapper->hash, ip);
4206         if (entry)
4207                 return -EBUSY;
4208
4209         map = kmalloc(sizeof(*map), GFP_KERNEL);
4210         if (!map)
4211                 return -ENOMEM;
4212
4213         map->entry.ip = ip;
4214         map->data = data;
4215
4216         __add_hash_entry(&mapper->hash, &map->entry);
4217
4218         return 0;
4219 }
4220
4221 /**
4222  * ftrace_func_mapper_remove_ip - Remove an ip from the mapping
4223  * @mapper: The mapper that has the ip maps
4224  * @ip: The instruction pointer address to remove the data from
4225  *
4226  * Returns the data if it is found, otherwise NULL.
4227  * Note, if the data pointer is used as the data itself, (see 
4228  * ftrace_func_mapper_find_ip(), then the return value may be meaningless,
4229  * if the data pointer was set to zero.
4230  */
4231 void *ftrace_func_mapper_remove_ip(struct ftrace_func_mapper *mapper,
4232                                    unsigned long ip)
4233 {
4234         struct ftrace_func_entry *entry;
4235         struct ftrace_func_map *map;
4236         void *data;
4237
4238         entry = ftrace_lookup_ip(&mapper->hash, ip);
4239         if (!entry)
4240                 return NULL;
4241
4242         map = (struct ftrace_func_map *)entry;
4243         data = map->data;
4244
4245         remove_hash_entry(&mapper->hash, entry);
4246         kfree(entry);
4247
4248         return data;
4249 }
4250
4251 /**
4252  * free_ftrace_func_mapper - free a mapping of ips and data
4253  * @mapper: The mapper that has the ip maps
4254  * @free_func: A function to be called on each data item.
4255  *
4256  * This is used to free the function mapper. The @free_func is optional
4257  * and can be used if the data needs to be freed as well.
4258  */
4259 void free_ftrace_func_mapper(struct ftrace_func_mapper *mapper,
4260                              ftrace_mapper_func free_func)
4261 {
4262         struct ftrace_func_entry *entry;
4263         struct ftrace_func_map *map;
4264         struct hlist_head *hhd;
4265         int size, i;
4266
4267         if (!mapper)
4268                 return;
4269
4270         if (free_func && mapper->hash.count) {
4271                 size = 1 << mapper->hash.size_bits;
4272                 for (i = 0; i < size; i++) {
4273                         hhd = &mapper->hash.buckets[i];
4274                         hlist_for_each_entry(entry, hhd, hlist) {
4275                                 map = (struct ftrace_func_map *)entry;
4276                                 free_func(map);
4277                         }
4278                 }
4279         }
4280         free_ftrace_hash(&mapper->hash);
4281 }
4282
4283 static void release_probe(struct ftrace_func_probe *probe)
4284 {
4285         struct ftrace_probe_ops *probe_ops;
4286
4287         mutex_lock(&ftrace_lock);
4288
4289         WARN_ON(probe->ref <= 0);
4290
4291         /* Subtract the ref that was used to protect this instance */
4292         probe->ref--;
4293
4294         if (!probe->ref) {
4295                 probe_ops = probe->probe_ops;
4296                 /*
4297                  * Sending zero as ip tells probe_ops to free
4298                  * the probe->data itself
4299                  */
4300                 if (probe_ops->free)
4301                         probe_ops->free(probe_ops, probe->tr, 0, probe->data);
4302                 list_del(&probe->list);
4303                 kfree(probe);
4304         }
4305         mutex_unlock(&ftrace_lock);
4306 }
4307
4308 static void acquire_probe_locked(struct ftrace_func_probe *probe)
4309 {
4310         /*
4311          * Add one ref to keep it from being freed when releasing the
4312          * ftrace_lock mutex.
4313          */
4314         probe->ref++;
4315 }
4316
4317 int
4318 register_ftrace_function_probe(char *glob, struct trace_array *tr,
4319                                struct ftrace_probe_ops *probe_ops,
4320                                void *data)
4321 {
4322         struct ftrace_func_entry *entry;
4323         struct ftrace_func_probe *probe;
4324         struct ftrace_hash **orig_hash;
4325         struct ftrace_hash *old_hash;
4326         struct ftrace_hash *hash;
4327         int count = 0;
4328         int size;
4329         int ret;
4330         int i;
4331
4332         if (WARN_ON(!tr))
4333                 return -EINVAL;
4334
4335         /* We do not support '!' for function probes */
4336         if (WARN_ON(glob[0] == '!'))
4337                 return -EINVAL;
4338
4339
4340         mutex_lock(&ftrace_lock);
4341         /* Check if the probe_ops is already registered */
4342         list_for_each_entry(probe, &tr->func_probes, list) {
4343                 if (probe->probe_ops == probe_ops)
4344                         break;
4345         }
4346         if (&probe->list == &tr->func_probes) {
4347                 probe = kzalloc(sizeof(*probe), GFP_KERNEL);
4348                 if (!probe) {
4349                         mutex_unlock(&ftrace_lock);
4350                         return -ENOMEM;
4351                 }
4352                 probe->probe_ops = probe_ops;
4353                 probe->ops.func = function_trace_probe_call;
4354                 probe->tr = tr;
4355                 ftrace_ops_init(&probe->ops);
4356                 list_add(&probe->list, &tr->func_probes);
4357         }
4358
4359         acquire_probe_locked(probe);
4360
4361         mutex_unlock(&ftrace_lock);
4362
4363         /*
4364          * Note, there's a small window here that the func_hash->filter_hash
4365          * may be NULL or empty. Need to be carefule when reading the loop.
4366          */
4367         mutex_lock(&probe->ops.func_hash->regex_lock);
4368
4369         orig_hash = &probe->ops.func_hash->filter_hash;
4370         old_hash = *orig_hash;
4371         hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
4372
4373         if (!hash) {
4374                 ret = -ENOMEM;
4375                 goto out;
4376         }
4377
4378         ret = ftrace_match_records(hash, glob, strlen(glob));
4379
4380         /* Nothing found? */
4381         if (!ret)
4382                 ret = -EINVAL;
4383
4384         if (ret < 0)
4385                 goto out;
4386
4387         size = 1 << hash->size_bits;
4388         for (i = 0; i < size; i++) {
4389                 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4390                         if (ftrace_lookup_ip(old_hash, entry->ip))
4391                                 continue;
4392                         /*
4393                          * The caller might want to do something special
4394                          * for each function we find. We call the callback
4395                          * to give the caller an opportunity to do so.
4396                          */
4397                         if (probe_ops->init) {
4398                                 ret = probe_ops->init(probe_ops, tr,
4399                                                       entry->ip, data,
4400                                                       &probe->data);
4401                                 if (ret < 0) {
4402                                         if (probe_ops->free && count)
4403                                                 probe_ops->free(probe_ops, tr,
4404                                                                 0, probe->data);
4405                                         probe->data = NULL;
4406                                         goto out;
4407                                 }
4408                         }
4409                         count++;
4410                 }
4411         }
4412
4413         mutex_lock(&ftrace_lock);
4414
4415         if (!count) {
4416                 /* Nothing was added? */
4417                 ret = -EINVAL;
4418                 goto out_unlock;
4419         }
4420
4421         ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
4422                                               hash, 1);
4423         if (ret < 0)
4424                 goto err_unlock;
4425
4426         /* One ref for each new function traced */
4427         probe->ref += count;
4428
4429         if (!(probe->ops.flags & FTRACE_OPS_FL_ENABLED))
4430                 ret = ftrace_startup(&probe->ops, 0);
4431
4432  out_unlock:
4433         mutex_unlock(&ftrace_lock);
4434
4435         if (!ret)
4436                 ret = count;
4437  out:
4438         mutex_unlock(&probe->ops.func_hash->regex_lock);
4439         free_ftrace_hash(hash);
4440
4441         release_probe(probe);
4442
4443         return ret;
4444
4445  err_unlock:
4446         if (!probe_ops->free || !count)
4447                 goto out_unlock;
4448
4449         /* Failed to do the move, need to call the free functions */
4450         for (i = 0; i < size; i++) {
4451                 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
4452                         if (ftrace_lookup_ip(old_hash, entry->ip))
4453                                 continue;
4454                         probe_ops->free(probe_ops, tr, entry->ip, probe->data);
4455                 }
4456         }
4457         goto out_unlock;
4458 }
4459
4460 int
4461 unregister_ftrace_function_probe_func(char *glob, struct trace_array *tr,
4462                                       struct ftrace_probe_ops *probe_ops)
4463 {
4464         struct ftrace_ops_hash old_hash_ops;
4465         struct ftrace_func_entry *entry;
4466         struct ftrace_func_probe *probe;
4467         struct ftrace_glob func_g;
4468         struct ftrace_hash **orig_hash;
4469         struct ftrace_hash *old_hash;
4470         struct ftrace_hash *hash = NULL;
4471         struct hlist_node *tmp;
4472         struct hlist_head hhd;
4473         char str[KSYM_SYMBOL_LEN];
4474         int count = 0;
4475         int i, ret = -ENODEV;
4476         int size;
4477
4478         if (!glob || !strlen(glob) || !strcmp(glob, "*"))
4479                 func_g.search = NULL;
4480         else {
4481                 int not;
4482
4483                 func_g.type = filter_parse_regex(glob, strlen(glob),
4484                                                  &func_g.search, &not);
4485                 func_g.len = strlen(func_g.search);
4486
4487                 /* we do not support '!' for function probes */
4488                 if (WARN_ON(not))
4489                         return -EINVAL;
4490         }
4491
4492         mutex_lock(&ftrace_lock);
4493         /* Check if the probe_ops is already registered */
4494         list_for_each_entry(probe, &tr->func_probes, list) {
4495                 if (probe->probe_ops == probe_ops)
4496                         break;
4497         }
4498         if (&probe->list == &tr->func_probes)
4499                 goto err_unlock_ftrace;
4500
4501         ret = -EINVAL;
4502         if (!(probe->ops.flags & FTRACE_OPS_FL_INITIALIZED))
4503                 goto err_unlock_ftrace;
4504
4505         acquire_probe_locked(probe);
4506
4507         mutex_unlock(&ftrace_lock);
4508
4509         mutex_lock(&probe->ops.func_hash->regex_lock);
4510
4511         orig_hash = &probe->ops.func_hash->filter_hash;
4512         old_hash = *orig_hash;
4513
4514         if (ftrace_hash_empty(old_hash))
4515                 goto out_unlock;
4516
4517         old_hash_ops.filter_hash = old_hash;
4518         /* Probes only have filters */
4519         old_hash_ops.notrace_hash = NULL;
4520
4521         ret = -ENOMEM;
4522         hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
4523         if (!hash)
4524                 goto out_unlock;
4525
4526         INIT_HLIST_HEAD(&hhd);
4527
4528         size = 1 << hash->size_bits;
4529         for (i = 0; i < size; i++) {
4530                 hlist_for_each_entry_safe(entry, tmp, &hash->buckets[i], hlist) {
4531
4532                         if (func_g.search) {
4533                                 kallsyms_lookup(entry->ip, NULL, NULL,
4534                                                 NULL, str);
4535                                 if (!ftrace_match(str, &func_g))
4536                                         continue;
4537                         }
4538                         count++;
4539                         remove_hash_entry(hash, entry);
4540                         hlist_add_head(&entry->hlist, &hhd);
4541                 }
4542         }
4543
4544         /* Nothing found? */
4545         if (!count) {
4546                 ret = -EINVAL;
4547                 goto out_unlock;
4548         }
4549
4550         mutex_lock(&ftrace_lock);
4551
4552         WARN_ON(probe->ref < count);
4553
4554         probe->ref -= count;
4555
4556         if (ftrace_hash_empty(hash))
4557                 ftrace_shutdown(&probe->ops, 0);
4558
4559         ret = ftrace_hash_move_and_update_ops(&probe->ops, orig_hash,
4560                                               hash, 1);
4561
4562         /* still need to update the function call sites */
4563         if (ftrace_enabled && !ftrace_hash_empty(hash))
4564                 ftrace_run_modify_code(&probe->ops, FTRACE_UPDATE_CALLS,
4565                                        &old_hash_ops);
4566         synchronize_sched();
4567
4568         hlist_for_each_entry_safe(entry, tmp, &hhd, hlist) {
4569                 hlist_del(&entry->hlist);
4570                 if (probe_ops->free)
4571                         probe_ops->free(probe_ops, tr, entry->ip, probe->data);
4572                 kfree(entry);
4573         }
4574         mutex_unlock(&ftrace_lock);
4575
4576  out_unlock:
4577         mutex_unlock(&probe->ops.func_hash->regex_lock);
4578         free_ftrace_hash(hash);
4579
4580         release_probe(probe);
4581
4582         return ret;
4583
4584  err_unlock_ftrace:
4585         mutex_unlock(&ftrace_lock);
4586         return ret;
4587 }
4588
4589 void clear_ftrace_function_probes(struct trace_array *tr)
4590 {
4591         struct ftrace_func_probe *probe, *n;
4592
4593         list_for_each_entry_safe(probe, n, &tr->func_probes, list)
4594                 unregister_ftrace_function_probe_func(NULL, tr, probe->probe_ops);
4595 }
4596
4597 static LIST_HEAD(ftrace_commands);
4598 static DEFINE_MUTEX(ftrace_cmd_mutex);
4599
4600 /*
4601  * Currently we only register ftrace commands from __init, so mark this
4602  * __init too.
4603  */
4604 __init int register_ftrace_command(struct ftrace_func_command *cmd)
4605 {
4606         struct ftrace_func_command *p;
4607         int ret = 0;
4608
4609         mutex_lock(&ftrace_cmd_mutex);
4610         list_for_each_entry(p, &ftrace_commands, list) {
4611                 if (strcmp(cmd->name, p->name) == 0) {
4612                         ret = -EBUSY;
4613                         goto out_unlock;
4614                 }
4615         }
4616         list_add(&cmd->list, &ftrace_commands);
4617  out_unlock:
4618         mutex_unlock(&ftrace_cmd_mutex);
4619
4620         return ret;
4621 }
4622
4623 /*
4624  * Currently we only unregister ftrace commands from __init, so mark
4625  * this __init too.
4626  */
4627 __init int unregister_ftrace_command(struct ftrace_func_command *cmd)
4628 {
4629         struct ftrace_func_command *p, *n;
4630         int ret = -ENODEV;
4631
4632         mutex_lock(&ftrace_cmd_mutex);
4633         list_for_each_entry_safe(p, n, &ftrace_commands, list) {
4634                 if (strcmp(cmd->name, p->name) == 0) {
4635                         ret = 0;
4636                         list_del_init(&p->list);
4637                         goto out_unlock;
4638                 }
4639         }
4640  out_unlock:
4641         mutex_unlock(&ftrace_cmd_mutex);
4642
4643         return ret;
4644 }
4645
4646 static int ftrace_process_regex(struct ftrace_iterator *iter,
4647                                 char *buff, int len, int enable)
4648 {
4649         struct ftrace_hash *hash = iter->hash;
4650         struct trace_array *tr = iter->ops->private;
4651         char *func, *command, *next = buff;
4652         struct ftrace_func_command *p;
4653         int ret = -EINVAL;
4654
4655         func = strsep(&next, ":");
4656
4657         if (!next) {
4658                 ret = ftrace_match_records(hash, func, len);
4659                 if (!ret)
4660                         ret = -EINVAL;
4661                 if (ret < 0)
4662                         return ret;
4663                 return 0;
4664         }
4665
4666         /* command found */
4667
4668         command = strsep(&next, ":");
4669
4670         mutex_lock(&ftrace_cmd_mutex);
4671         list_for_each_entry(p, &ftrace_commands, list) {
4672                 if (strcmp(p->name, command) == 0) {
4673                         ret = p->func(tr, hash, func, command, next, enable);
4674                         goto out_unlock;
4675                 }
4676         }
4677  out_unlock:
4678         mutex_unlock(&ftrace_cmd_mutex);
4679
4680         return ret;
4681 }
4682
4683 static ssize_t
4684 ftrace_regex_write(struct file *file, const char __user *ubuf,
4685                    size_t cnt, loff_t *ppos, int enable)
4686 {
4687         struct ftrace_iterator *iter;
4688         struct trace_parser *parser;
4689         ssize_t ret, read;
4690
4691         if (!cnt)
4692                 return 0;
4693
4694         if (file->f_mode & FMODE_READ) {
4695                 struct seq_file *m = file->private_data;
4696                 iter = m->private;
4697         } else
4698                 iter = file->private_data;
4699
4700         if (unlikely(ftrace_disabled))
4701                 return -ENODEV;
4702
4703         /* iter->hash is a local copy, so we don't need regex_lock */
4704
4705         parser = &iter->parser;
4706         read = trace_get_user(parser, ubuf, cnt, ppos);
4707
4708         if (read >= 0 && trace_parser_loaded(parser) &&
4709             !trace_parser_cont(parser)) {
4710                 ret = ftrace_process_regex(iter, parser->buffer,
4711                                            parser->idx, enable);
4712                 trace_parser_clear(parser);
4713                 if (ret < 0)
4714                         goto out;
4715         }
4716
4717         ret = read;
4718  out:
4719         return ret;
4720 }
4721
4722 ssize_t
4723 ftrace_filter_write(struct file *file, const char __user *ubuf,
4724                     size_t cnt, loff_t *ppos)
4725 {
4726         return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
4727 }
4728
4729 ssize_t
4730 ftrace_notrace_write(struct file *file, const char __user *ubuf,
4731                      size_t cnt, loff_t *ppos)
4732 {
4733         return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
4734 }
4735
4736 static int
4737 ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
4738 {
4739         struct ftrace_func_entry *entry;
4740
4741         if (!ftrace_location(ip))
4742                 return -EINVAL;
4743
4744         if (remove) {
4745                 entry = ftrace_lookup_ip(hash, ip);
4746                 if (!entry)
4747                         return -ENOENT;
4748                 free_hash_entry(hash, entry);
4749                 return 0;
4750         }
4751
4752         return add_hash_entry(hash, ip);
4753 }
4754
4755 static int
4756 ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
4757                 unsigned long ip, int remove, int reset, int enable)
4758 {
4759         struct ftrace_hash **orig_hash;
4760         struct ftrace_hash *hash;
4761         int ret;
4762
4763         if (unlikely(ftrace_disabled))
4764                 return -ENODEV;
4765
4766         mutex_lock(&ops->func_hash->regex_lock);
4767
4768         if (enable)
4769                 orig_hash = &ops->func_hash->filter_hash;
4770         else
4771                 orig_hash = &ops->func_hash->notrace_hash;
4772
4773         if (reset)
4774                 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4775         else
4776                 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
4777
4778         if (!hash) {
4779                 ret = -ENOMEM;
4780                 goto out_regex_unlock;
4781         }
4782
4783         if (buf && !ftrace_match_records(hash, buf, len)) {
4784                 ret = -EINVAL;
4785                 goto out_regex_unlock;
4786         }
4787         if (ip) {
4788                 ret = ftrace_match_addr(hash, ip, remove);
4789                 if (ret < 0)
4790                         goto out_regex_unlock;
4791         }
4792
4793         mutex_lock(&ftrace_lock);
4794         ret = ftrace_hash_move_and_update_ops(ops, orig_hash, hash, enable);
4795         mutex_unlock(&ftrace_lock);
4796
4797  out_regex_unlock:
4798         mutex_unlock(&ops->func_hash->regex_lock);
4799
4800         free_ftrace_hash(hash);
4801         return ret;
4802 }
4803
4804 static int
4805 ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
4806                 int reset, int enable)
4807 {
4808         return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
4809 }
4810
4811 /**
4812  * ftrace_set_filter_ip - set a function to filter on in ftrace by address
4813  * @ops - the ops to set the filter with
4814  * @ip - the address to add to or remove from the filter.
4815  * @remove - non zero to remove the ip from the filter
4816  * @reset - non zero to reset all filters before applying this filter.
4817  *
4818  * Filters denote which functions should be enabled when tracing is enabled
4819  * If @ip is NULL, it failes to update filter.
4820  */
4821 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
4822                          int remove, int reset)
4823 {
4824         ftrace_ops_init(ops);
4825         return ftrace_set_addr(ops, ip, remove, reset, 1);
4826 }
4827 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
4828
4829 /**
4830  * ftrace_ops_set_global_filter - setup ops to use global filters
4831  * @ops - the ops which will use the global filters
4832  *
4833  * ftrace users who need global function trace filtering should call this.
4834  * It can set the global filter only if ops were not initialized before.
4835  */
4836 void ftrace_ops_set_global_filter(struct ftrace_ops *ops)
4837 {
4838         if (ops->flags & FTRACE_OPS_FL_INITIALIZED)
4839                 return;
4840
4841         ftrace_ops_init(ops);
4842         ops->func_hash = &global_ops.local_hash;
4843 }
4844 EXPORT_SYMBOL_GPL(ftrace_ops_set_global_filter);
4845
4846 static int
4847 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
4848                  int reset, int enable)
4849 {
4850         return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
4851 }
4852
4853 /**
4854  * ftrace_set_filter - set a function to filter on in ftrace
4855  * @ops - the ops to set the filter with
4856  * @buf - the string that holds the function filter text.
4857  * @len - the length of the string.
4858  * @reset - non zero to reset all filters before applying this filter.
4859  *
4860  * Filters denote which functions should be enabled when tracing is enabled.
4861  * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4862  */
4863 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
4864                        int len, int reset)
4865 {
4866         ftrace_ops_init(ops);
4867         return ftrace_set_regex(ops, buf, len, reset, 1);
4868 }
4869 EXPORT_SYMBOL_GPL(ftrace_set_filter);
4870
4871 /**
4872  * ftrace_set_notrace - set a function to not trace in ftrace
4873  * @ops - the ops to set the notrace filter with
4874  * @buf - the string that holds the function notrace text.
4875  * @len - the length of the string.
4876  * @reset - non zero to reset all filters before applying this filter.
4877  *
4878  * Notrace Filters denote which functions should not be enabled when tracing
4879  * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4880  * for tracing.
4881  */
4882 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
4883                         int len, int reset)
4884 {
4885         ftrace_ops_init(ops);
4886         return ftrace_set_regex(ops, buf, len, reset, 0);
4887 }
4888 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
4889 /**
4890  * ftrace_set_global_filter - set a function to filter on with global tracers
4891  * @buf - the string that holds the function filter text.
4892  * @len - the length of the string.
4893  * @reset - non zero to reset all filters before applying this filter.
4894  *
4895  * Filters denote which functions should be enabled when tracing is enabled.
4896  * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4897  */
4898 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
4899 {
4900         ftrace_set_regex(&global_ops, buf, len, reset, 1);
4901 }
4902 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
4903
4904 /**
4905  * ftrace_set_global_notrace - set a function to not trace with global tracers
4906  * @buf - the string that holds the function notrace text.
4907  * @len - the length of the string.
4908  * @reset - non zero to reset all filters before applying this filter.
4909  *
4910  * Notrace Filters denote which functions should not be enabled when tracing
4911  * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4912  * for tracing.
4913  */
4914 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
4915 {
4916         ftrace_set_regex(&global_ops, buf, len, reset, 0);
4917 }
4918 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
4919
4920 /*
4921  * command line interface to allow users to set filters on boot up.
4922  */
4923 #define FTRACE_FILTER_SIZE              COMMAND_LINE_SIZE
4924 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4925 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
4926
4927 /* Used by function selftest to not test if filter is set */
4928 bool ftrace_filter_param __initdata;
4929
4930 static int __init set_ftrace_notrace(char *str)
4931 {
4932         ftrace_filter_param = true;
4933         strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
4934         return 1;
4935 }
4936 __setup("ftrace_notrace=", set_ftrace_notrace);
4937
4938 static int __init set_ftrace_filter(char *str)
4939 {
4940         ftrace_filter_param = true;
4941         strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
4942         return 1;
4943 }
4944 __setup("ftrace_filter=", set_ftrace_filter);
4945
4946 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4947 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
4948 static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4949 static int ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer);
4950
4951 static int __init set_graph_function(char *str)
4952 {
4953         strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
4954         return 1;
4955 }
4956 __setup("ftrace_graph_filter=", set_graph_function);
4957
4958 static int __init set_graph_notrace_function(char *str)
4959 {
4960         strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
4961         return 1;
4962 }
4963 __setup("ftrace_graph_notrace=", set_graph_notrace_function);
4964
4965 static int __init set_graph_max_depth_function(char *str)
4966 {
4967         if (!str)
4968                 return 0;
4969         fgraph_max_depth = simple_strtoul(str, NULL, 0);
4970         return 1;
4971 }
4972 __setup("ftrace_graph_max_depth=", set_graph_max_depth_function);
4973
4974 static void __init set_ftrace_early_graph(char *buf, int enable)
4975 {
4976         int ret;
4977         char *func;
4978         struct ftrace_hash *hash;
4979
4980         hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4981         if (WARN_ON(!hash))
4982                 return;
4983
4984         while (buf) {
4985                 func = strsep(&buf, ",");
4986                 /* we allow only one expression at a time */
4987                 ret = ftrace_graph_set_hash(hash, func);
4988                 if (ret)
4989                         printk(KERN_DEBUG "ftrace: function %s not "
4990                                           "traceable\n", func);
4991         }
4992
4993         if (enable)
4994                 ftrace_graph_hash = hash;
4995         else
4996                 ftrace_graph_notrace_hash = hash;
4997 }
4998 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4999
5000 void __init
5001 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
5002 {
5003         char *func;
5004
5005         ftrace_ops_init(ops);
5006
5007         while (buf) {
5008                 func = strsep(&buf, ",");
5009                 ftrace_set_regex(ops, func, strlen(func), 0, enable);
5010         }
5011 }
5012
5013 static void __init set_ftrace_early_filters(void)
5014 {
5015         if (ftrace_filter_buf[0])
5016                 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
5017         if (ftrace_notrace_buf[0])
5018                 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
5019 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5020         if (ftrace_graph_buf[0])
5021                 set_ftrace_early_graph(ftrace_graph_buf, 1);
5022         if (ftrace_graph_notrace_buf[0])
5023                 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
5024 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5025 }
5026
5027 int ftrace_regex_release(struct inode *inode, struct file *file)
5028 {
5029         struct seq_file *m = (struct seq_file *)file->private_data;
5030         struct ftrace_iterator *iter;
5031         struct ftrace_hash **orig_hash;
5032         struct trace_parser *parser;
5033         int filter_hash;
5034         int ret;
5035
5036         if (file->f_mode & FMODE_READ) {
5037                 iter = m->private;
5038                 seq_release(inode, file);
5039         } else
5040                 iter = file->private_data;
5041
5042         parser = &iter->parser;
5043         if (trace_parser_loaded(parser)) {
5044                 int enable = !(iter->flags & FTRACE_ITER_NOTRACE);
5045
5046                 ftrace_process_regex(iter, parser->buffer,
5047                                      parser->idx, enable);
5048         }
5049
5050         trace_parser_put(parser);
5051
5052         mutex_lock(&iter->ops->func_hash->regex_lock);
5053
5054         if (file->f_mode & FMODE_WRITE) {
5055                 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
5056
5057                 if (filter_hash) {
5058                         orig_hash = &iter->ops->func_hash->filter_hash;
5059                         if (iter->tr) {
5060                                 if (list_empty(&iter->tr->mod_trace))
5061                                         iter->hash->flags &= ~FTRACE_HASH_FL_MOD;
5062                                 else
5063                                         iter->hash->flags |= FTRACE_HASH_FL_MOD;
5064                         }
5065                 } else
5066                         orig_hash = &iter->ops->func_hash->notrace_hash;
5067
5068                 mutex_lock(&ftrace_lock);
5069                 ret = ftrace_hash_move_and_update_ops(iter->ops, orig_hash,
5070                                                       iter->hash, filter_hash);
5071                 mutex_unlock(&ftrace_lock);
5072         } else {
5073                 /* For read only, the hash is the ops hash */
5074                 iter->hash = NULL;
5075         }
5076
5077         mutex_unlock(&iter->ops->func_hash->regex_lock);
5078         free_ftrace_hash(iter->hash);
5079         if (iter->tr)
5080                 trace_array_put(iter->tr);
5081         kfree(iter);
5082
5083         return 0;
5084 }
5085
5086 static const struct file_operations ftrace_avail_fops = {
5087         .open = ftrace_avail_open,
5088         .read = seq_read,
5089         .llseek = seq_lseek,
5090         .release = seq_release_private,
5091 };
5092
5093 static const struct file_operations ftrace_enabled_fops = {
5094         .open = ftrace_enabled_open,
5095         .read = seq_read,
5096         .llseek = seq_lseek,
5097         .release = seq_release_private,
5098 };
5099
5100 static const struct file_operations ftrace_filter_fops = {
5101         .open = ftrace_filter_open,
5102         .read = seq_read,
5103         .write = ftrace_filter_write,
5104         .llseek = tracing_lseek,
5105         .release = ftrace_regex_release,
5106 };
5107
5108 static const struct file_operations ftrace_notrace_fops = {
5109         .open = ftrace_notrace_open,
5110         .read = seq_read,
5111         .write = ftrace_notrace_write,
5112         .llseek = tracing_lseek,
5113         .release = ftrace_regex_release,
5114 };
5115
5116 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5117
5118 static DEFINE_MUTEX(graph_lock);
5119
5120 struct ftrace_hash __rcu *ftrace_graph_hash = EMPTY_HASH;
5121 struct ftrace_hash __rcu *ftrace_graph_notrace_hash = EMPTY_HASH;
5122
5123 enum graph_filter_type {
5124         GRAPH_FILTER_NOTRACE    = 0,
5125         GRAPH_FILTER_FUNCTION,
5126 };
5127
5128 #define FTRACE_GRAPH_EMPTY      ((void *)1)
5129
5130 struct ftrace_graph_data {
5131         struct ftrace_hash              *hash;
5132         struct ftrace_func_entry        *entry;
5133         int                             idx;   /* for hash table iteration */
5134         enum graph_filter_type          type;
5135         struct ftrace_hash              *new_hash;
5136         const struct seq_operations     *seq_ops;
5137         struct trace_parser             parser;
5138 };
5139
5140 static void *
5141 __g_next(struct seq_file *m, loff_t *pos)
5142 {
5143         struct ftrace_graph_data *fgd = m->private;
5144         struct ftrace_func_entry *entry = fgd->entry;
5145         struct hlist_head *head;
5146         int i, idx = fgd->idx;
5147
5148         if (*pos >= fgd->hash->count)
5149                 return NULL;
5150
5151         if (entry) {
5152                 hlist_for_each_entry_continue(entry, hlist) {
5153                         fgd->entry = entry;
5154                         return entry;
5155                 }
5156
5157                 idx++;
5158         }
5159
5160         for (i = idx; i < 1 << fgd->hash->size_bits; i++) {
5161                 head = &fgd->hash->buckets[i];
5162                 hlist_for_each_entry(entry, head, hlist) {
5163                         fgd->entry = entry;
5164                         fgd->idx = i;
5165                         return entry;
5166                 }
5167         }
5168         return NULL;
5169 }
5170
5171 static void *
5172 g_next(struct seq_file *m, void *v, loff_t *pos)
5173 {
5174         (*pos)++;
5175         return __g_next(m, pos);
5176 }
5177
5178 static void *g_start(struct seq_file *m, loff_t *pos)
5179 {
5180         struct ftrace_graph_data *fgd = m->private;
5181
5182         mutex_lock(&graph_lock);
5183
5184         if (fgd->type == GRAPH_FILTER_FUNCTION)
5185                 fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
5186                                         lockdep_is_held(&graph_lock));
5187         else
5188                 fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5189                                         lockdep_is_held(&graph_lock));
5190
5191         /* Nothing, tell g_show to print all functions are enabled */
5192         if (ftrace_hash_empty(fgd->hash) && !*pos)
5193                 return FTRACE_GRAPH_EMPTY;
5194
5195         fgd->idx = 0;
5196         fgd->entry = NULL;
5197         return __g_next(m, pos);
5198 }
5199
5200 static void g_stop(struct seq_file *m, void *p)
5201 {
5202         mutex_unlock(&graph_lock);
5203 }
5204
5205 static int g_show(struct seq_file *m, void *v)
5206 {
5207         struct ftrace_func_entry *entry = v;
5208
5209         if (!entry)
5210                 return 0;
5211
5212         if (entry == FTRACE_GRAPH_EMPTY) {
5213                 struct ftrace_graph_data *fgd = m->private;
5214
5215                 if (fgd->type == GRAPH_FILTER_FUNCTION)
5216                         seq_puts(m, "#### all functions enabled ####\n");
5217                 else
5218                         seq_puts(m, "#### no functions disabled ####\n");
5219                 return 0;
5220         }
5221
5222         seq_printf(m, "%ps\n", (void *)entry->ip);
5223
5224         return 0;
5225 }
5226
5227 static const struct seq_operations ftrace_graph_seq_ops = {
5228         .start = g_start,
5229         .next = g_next,
5230         .stop = g_stop,
5231         .show = g_show,
5232 };
5233
5234 static int
5235 __ftrace_graph_open(struct inode *inode, struct file *file,
5236                     struct ftrace_graph_data *fgd)
5237 {
5238         int ret = 0;
5239         struct ftrace_hash *new_hash = NULL;
5240
5241         if (file->f_mode & FMODE_WRITE) {
5242                 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
5243
5244                 if (trace_parser_get_init(&fgd->parser, FTRACE_BUFF_MAX))
5245                         return -ENOMEM;
5246
5247                 if (file->f_flags & O_TRUNC)
5248                         new_hash = alloc_ftrace_hash(size_bits);
5249                 else
5250                         new_hash = alloc_and_copy_ftrace_hash(size_bits,
5251                                                               fgd->hash);
5252                 if (!new_hash) {
5253                         ret = -ENOMEM;
5254                         goto out;
5255                 }
5256         }
5257
5258         if (file->f_mode & FMODE_READ) {
5259                 ret = seq_open(file, &ftrace_graph_seq_ops);
5260                 if (!ret) {
5261                         struct seq_file *m = file->private_data;
5262                         m->private = fgd;
5263                 } else {
5264                         /* Failed */
5265                         free_ftrace_hash(new_hash);
5266                         new_hash = NULL;
5267                 }
5268         } else
5269                 file->private_data = fgd;
5270
5271 out:
5272         if (ret < 0 && file->f_mode & FMODE_WRITE)
5273                 trace_parser_put(&fgd->parser);
5274
5275         fgd->new_hash = new_hash;
5276
5277         /*
5278          * All uses of fgd->hash must be taken with the graph_lock
5279          * held. The graph_lock is going to be released, so force
5280          * fgd->hash to be reinitialized when it is taken again.
5281          */
5282         fgd->hash = NULL;
5283
5284         return ret;
5285 }
5286
5287 static int
5288 ftrace_graph_open(struct inode *inode, struct file *file)
5289 {
5290         struct ftrace_graph_data *fgd;
5291         int ret;
5292
5293         if (unlikely(ftrace_disabled))
5294                 return -ENODEV;
5295
5296         fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
5297         if (fgd == NULL)
5298                 return -ENOMEM;
5299
5300         mutex_lock(&graph_lock);
5301
5302         fgd->hash = rcu_dereference_protected(ftrace_graph_hash,
5303                                         lockdep_is_held(&graph_lock));
5304         fgd->type = GRAPH_FILTER_FUNCTION;
5305         fgd->seq_ops = &ftrace_graph_seq_ops;
5306
5307         ret = __ftrace_graph_open(inode, file, fgd);
5308         if (ret < 0)
5309                 kfree(fgd);
5310
5311         mutex_unlock(&graph_lock);
5312         return ret;
5313 }
5314
5315 static int
5316 ftrace_graph_notrace_open(struct inode *inode, struct file *file)
5317 {
5318         struct ftrace_graph_data *fgd;
5319         int ret;
5320
5321         if (unlikely(ftrace_disabled))
5322                 return -ENODEV;
5323
5324         fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
5325         if (fgd == NULL)
5326                 return -ENOMEM;
5327
5328         mutex_lock(&graph_lock);
5329
5330         fgd->hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5331                                         lockdep_is_held(&graph_lock));
5332         fgd->type = GRAPH_FILTER_NOTRACE;
5333         fgd->seq_ops = &ftrace_graph_seq_ops;
5334
5335         ret = __ftrace_graph_open(inode, file, fgd);
5336         if (ret < 0)
5337                 kfree(fgd);
5338
5339         mutex_unlock(&graph_lock);
5340         return ret;
5341 }
5342
5343 static int
5344 ftrace_graph_release(struct inode *inode, struct file *file)
5345 {
5346         struct ftrace_graph_data *fgd;
5347         struct ftrace_hash *old_hash, *new_hash;
5348         struct trace_parser *parser;
5349         int ret = 0;
5350
5351         if (file->f_mode & FMODE_READ) {
5352                 struct seq_file *m = file->private_data;
5353
5354                 fgd = m->private;
5355                 seq_release(inode, file);
5356         } else {
5357                 fgd = file->private_data;
5358         }
5359
5360
5361         if (file->f_mode & FMODE_WRITE) {
5362
5363                 parser = &fgd->parser;
5364
5365                 if (trace_parser_loaded((parser))) {
5366                         ret = ftrace_graph_set_hash(fgd->new_hash,
5367                                                     parser->buffer);
5368                 }
5369
5370                 trace_parser_put(parser);
5371
5372                 new_hash = __ftrace_hash_move(fgd->new_hash);
5373                 if (!new_hash) {
5374                         ret = -ENOMEM;
5375                         goto out;
5376                 }
5377
5378                 mutex_lock(&graph_lock);
5379
5380                 if (fgd->type == GRAPH_FILTER_FUNCTION) {
5381                         old_hash = rcu_dereference_protected(ftrace_graph_hash,
5382                                         lockdep_is_held(&graph_lock));
5383                         rcu_assign_pointer(ftrace_graph_hash, new_hash);
5384                 } else {
5385                         old_hash = rcu_dereference_protected(ftrace_graph_notrace_hash,
5386                                         lockdep_is_held(&graph_lock));
5387                         rcu_assign_pointer(ftrace_graph_notrace_hash, new_hash);
5388                 }
5389
5390                 mutex_unlock(&graph_lock);
5391
5392                 /*
5393                  * We need to do a hard force of sched synchronization.
5394                  * This is because we use preempt_disable() to do RCU, but
5395                  * the function tracers can be called where RCU is not watching
5396                  * (like before user_exit()). We can not rely on the RCU
5397                  * infrastructure to do the synchronization, thus we must do it
5398                  * ourselves.
5399                  */
5400                 schedule_on_each_cpu(ftrace_sync);
5401
5402                 free_ftrace_hash(old_hash);
5403         }
5404
5405  out:
5406         free_ftrace_hash(fgd->new_hash);
5407         kfree(fgd);
5408
5409         return ret;
5410 }
5411
5412 static int
5413 ftrace_graph_set_hash(struct ftrace_hash *hash, char *buffer)
5414 {
5415         struct ftrace_glob func_g;
5416         struct dyn_ftrace *rec;
5417         struct ftrace_page *pg;
5418         struct ftrace_func_entry *entry;
5419         int fail = 1;
5420         int not;
5421
5422         /* decode regex */
5423         func_g.type = filter_parse_regex(buffer, strlen(buffer),
5424                                          &func_g.search, &not);
5425
5426         func_g.len = strlen(func_g.search);
5427
5428         mutex_lock(&ftrace_lock);
5429
5430         if (unlikely(ftrace_disabled)) {
5431                 mutex_unlock(&ftrace_lock);
5432                 return -ENODEV;
5433         }
5434
5435         do_for_each_ftrace_rec(pg, rec) {
5436
5437                 if (rec->flags & FTRACE_FL_DISABLED)
5438                         continue;
5439
5440                 if (ftrace_match_record(rec, &func_g, NULL, 0)) {
5441                         entry = ftrace_lookup_ip(hash, rec->ip);
5442
5443                         if (!not) {
5444                                 fail = 0;
5445
5446                                 if (entry)
5447                                         continue;
5448                                 if (add_hash_entry(hash, rec->ip) < 0)
5449                                         goto out;
5450                         } else {
5451                                 if (entry) {
5452                                         free_hash_entry(hash, entry);
5453                                         fail = 0;
5454                                 }
5455                         }
5456                 }
5457         } while_for_each_ftrace_rec();
5458 out:
5459         mutex_unlock(&ftrace_lock);
5460
5461         if (fail)
5462                 return -EINVAL;
5463
5464         return 0;
5465 }
5466
5467 static ssize_t
5468 ftrace_graph_write(struct file *file, const char __user *ubuf,
5469                    size_t cnt, loff_t *ppos)
5470 {
5471         ssize_t read, ret = 0;
5472         struct ftrace_graph_data *fgd = file->private_data;
5473         struct trace_parser *parser;
5474
5475         if (!cnt)
5476                 return 0;
5477
5478         /* Read mode uses seq functions */
5479         if (file->f_mode & FMODE_READ) {
5480                 struct seq_file *m = file->private_data;
5481                 fgd = m->private;
5482         }
5483
5484         parser = &fgd->parser;
5485
5486         read = trace_get_user(parser, ubuf, cnt, ppos);
5487
5488         if (read >= 0 && trace_parser_loaded(parser) &&
5489             !trace_parser_cont(parser)) {
5490
5491                 ret = ftrace_graph_set_hash(fgd->new_hash,
5492                                             parser->buffer);
5493                 trace_parser_clear(parser);
5494         }
5495
5496         if (!ret)
5497                 ret = read;
5498
5499         return ret;
5500 }
5501
5502 static const struct file_operations ftrace_graph_fops = {
5503         .open           = ftrace_graph_open,
5504         .read           = seq_read,
5505         .write          = ftrace_graph_write,
5506         .llseek         = tracing_lseek,
5507         .release        = ftrace_graph_release,
5508 };
5509
5510 static const struct file_operations ftrace_graph_notrace_fops = {
5511         .open           = ftrace_graph_notrace_open,
5512         .read           = seq_read,
5513         .write          = ftrace_graph_write,
5514         .llseek         = tracing_lseek,
5515         .release        = ftrace_graph_release,
5516 };
5517 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5518
5519 void ftrace_create_filter_files(struct ftrace_ops *ops,
5520                                 struct dentry *parent)
5521 {
5522
5523         trace_create_file("set_ftrace_filter", 0644, parent,
5524                           ops, &ftrace_filter_fops);
5525
5526         trace_create_file("set_ftrace_notrace", 0644, parent,
5527                           ops, &ftrace_notrace_fops);
5528 }
5529
5530 /*
5531  * The name "destroy_filter_files" is really a misnomer. Although
5532  * in the future, it may actualy delete the files, but this is
5533  * really intended to make sure the ops passed in are disabled
5534  * and that when this function returns, the caller is free to
5535  * free the ops.
5536  *
5537  * The "destroy" name is only to match the "create" name that this
5538  * should be paired with.
5539  */
5540 void ftrace_destroy_filter_files(struct ftrace_ops *ops)
5541 {
5542         mutex_lock(&ftrace_lock);
5543         if (ops->flags & FTRACE_OPS_FL_ENABLED)
5544                 ftrace_shutdown(ops, 0);
5545         ops->flags |= FTRACE_OPS_FL_DELETED;
5546         ftrace_free_filter(ops);
5547         mutex_unlock(&ftrace_lock);
5548 }
5549
5550 static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer)
5551 {
5552
5553         trace_create_file("available_filter_functions", 0444,
5554                         d_tracer, NULL, &ftrace_avail_fops);
5555
5556         trace_create_file("enabled_functions", 0444,
5557                         d_tracer, NULL, &ftrace_enabled_fops);
5558
5559         ftrace_create_filter_files(&global_ops, d_tracer);
5560
5561 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5562         trace_create_file("set_graph_function", 0644, d_tracer,
5563                                     NULL,
5564                                     &ftrace_graph_fops);
5565         trace_create_file("set_graph_notrace", 0644, d_tracer,
5566                                     NULL,
5567                                     &ftrace_graph_notrace_fops);
5568 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
5569
5570         return 0;
5571 }
5572
5573 static int ftrace_cmp_ips(const void *a, const void *b)
5574 {
5575         const unsigned long *ipa = a;
5576         const unsigned long *ipb = b;
5577
5578         if (*ipa > *ipb)
5579                 return 1;
5580         if (*ipa < *ipb)
5581                 return -1;
5582         return 0;
5583 }
5584
5585 static int ftrace_process_locs(struct module *mod,
5586                                unsigned long *start,
5587                                unsigned long *end)
5588 {
5589         struct ftrace_page *start_pg;
5590         struct ftrace_page *pg;
5591         struct dyn_ftrace *rec;
5592         unsigned long count;
5593         unsigned long *p;
5594         unsigned long addr;
5595         unsigned long flags = 0; /* Shut up gcc */
5596         int ret = -ENOMEM;
5597
5598         count = end - start;
5599
5600         if (!count)
5601                 return 0;
5602
5603         sort(start, count, sizeof(*start),
5604              ftrace_cmp_ips, NULL);
5605
5606         start_pg = ftrace_allocate_pages(count);
5607         if (!start_pg)
5608                 return -ENOMEM;
5609
5610         mutex_lock(&ftrace_lock);
5611
5612         /*
5613          * Core and each module needs their own pages, as
5614          * modules will free them when they are removed.
5615          * Force a new page to be allocated for modules.
5616          */
5617         if (!mod) {
5618                 WARN_ON(ftrace_pages || ftrace_pages_start);
5619                 /* First initialization */
5620                 ftrace_pages = ftrace_pages_start = start_pg;
5621         } else {
5622                 if (!ftrace_pages)
5623                         goto out;
5624
5625                 if (WARN_ON(ftrace_pages->next)) {
5626                         /* Hmm, we have free pages? */
5627                         while (ftrace_pages->next)
5628                                 ftrace_pages = ftrace_pages->next;
5629                 }
5630
5631                 ftrace_pages->next = start_pg;
5632         }
5633
5634         p = start;
5635         pg = start_pg;
5636         while (p < end) {
5637                 addr = ftrace_call_adjust(*p++);
5638                 /*
5639                  * Some architecture linkers will pad between
5640                  * the different mcount_loc sections of different
5641                  * object files to satisfy alignments.
5642                  * Skip any NULL pointers.
5643                  */
5644                 if (!addr)
5645                         continue;
5646
5647                 if (pg->index == pg->size) {
5648                         /* We should have allocated enough */
5649                         if (WARN_ON(!pg->next))
5650                                 break;
5651                         pg = pg->next;
5652                 }
5653
5654                 rec = &pg->records[pg->index++];
5655                 rec->ip = addr;
5656         }
5657
5658         /* We should have used all pages */
5659         WARN_ON(pg->next);
5660
5661         /* Assign the last page to ftrace_pages */
5662         ftrace_pages = pg;
5663
5664         /*
5665          * We only need to disable interrupts on start up
5666          * because we are modifying code that an interrupt
5667          * may execute, and the modification is not atomic.
5668          * But for modules, nothing runs the code we modify
5669          * until we are finished with it, and there's no
5670          * reason to cause large interrupt latencies while we do it.
5671          */
5672         if (!mod)
5673                 local_irq_save(flags);
5674         ftrace_update_code(mod, start_pg);
5675         if (!mod)
5676                 local_irq_restore(flags);
5677         ret = 0;
5678  out:
5679         mutex_unlock(&ftrace_lock);
5680
5681         return ret;
5682 }
5683
5684 struct ftrace_mod_func {
5685         struct list_head        list;
5686         char                    *name;
5687         unsigned long           ip;
5688         unsigned int            size;
5689 };
5690
5691 struct ftrace_mod_map {
5692         struct rcu_head         rcu;
5693         struct list_head        list;
5694         struct module           *mod;
5695         unsigned long           start_addr;
5696         unsigned long           end_addr;
5697         struct list_head        funcs;
5698         unsigned int            num_funcs;
5699 };
5700
5701 #ifdef CONFIG_MODULES
5702
5703 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
5704
5705 static LIST_HEAD(ftrace_mod_maps);
5706
5707 static int referenced_filters(struct dyn_ftrace *rec)
5708 {
5709         struct ftrace_ops *ops;
5710         int cnt = 0;
5711
5712         for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
5713                 if (ops_references_rec(ops, rec)) {
5714                         cnt++;
5715                         if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
5716                                 rec->flags |= FTRACE_FL_REGS;
5717                 }
5718         }
5719
5720         return cnt;
5721 }
5722
5723 static void
5724 clear_mod_from_hash(struct ftrace_page *pg, struct ftrace_hash *hash)
5725 {
5726         struct ftrace_func_entry *entry;
5727         struct dyn_ftrace *rec;
5728         int i;
5729
5730         if (ftrace_hash_empty(hash))
5731                 return;
5732
5733         for (i = 0; i < pg->index; i++) {
5734                 rec = &pg->records[i];
5735                 entry = __ftrace_lookup_ip(hash, rec->ip);
5736                 /*
5737                  * Do not allow this rec to match again.
5738                  * Yeah, it may waste some memory, but will be removed
5739                  * if/when the hash is modified again.
5740                  */
5741                 if (entry)
5742                         entry->ip = 0;
5743         }
5744 }
5745
5746 /* Clear any records from hashs */
5747 static void clear_mod_from_hashes(struct ftrace_page *pg)
5748 {
5749         struct trace_array *tr;
5750
5751         mutex_lock(&trace_types_lock);
5752         list_for_each_entry(tr, &ftrace_trace_arrays, list) {
5753                 if (!tr->ops || !tr->ops->func_hash)
5754                         continue;
5755                 mutex_lock(&tr->ops->func_hash->regex_lock);
5756                 clear_mod_from_hash(pg, tr->ops->func_hash->filter_hash);
5757                 clear_mod_from_hash(pg, tr->ops->func_hash->notrace_hash);
5758                 mutex_unlock(&tr->ops->func_hash->regex_lock);
5759         }
5760         mutex_unlock(&trace_types_lock);
5761 }
5762
5763 static void ftrace_free_mod_map(struct rcu_head *rcu)
5764 {
5765         struct ftrace_mod_map *mod_map = container_of(rcu, struct ftrace_mod_map, rcu);
5766         struct ftrace_mod_func *mod_func;
5767         struct ftrace_mod_func *n;
5768
5769         /* All the contents of mod_map are now not visible to readers */
5770         list_for_each_entry_safe(mod_func, n, &mod_map->funcs, list) {
5771                 kfree(mod_func->name);
5772                 list_del(&mod_func->list);
5773                 kfree(mod_func);
5774         }
5775
5776         kfree(mod_map);
5777 }
5778
5779 void ftrace_release_mod(struct module *mod)
5780 {
5781         struct ftrace_mod_map *mod_map;
5782         struct ftrace_mod_map *n;
5783         struct dyn_ftrace *rec;
5784         struct ftrace_page **last_pg;
5785         struct ftrace_page *tmp_page = NULL;
5786         struct ftrace_page *pg;
5787         int order;
5788
5789         mutex_lock(&ftrace_lock);
5790
5791         if (ftrace_disabled)
5792                 goto out_unlock;
5793
5794         list_for_each_entry_safe(mod_map, n, &ftrace_mod_maps, list) {
5795                 if (mod_map->mod == mod) {
5796                         list_del_rcu(&mod_map->list);
5797                         call_rcu_sched(&mod_map->rcu, ftrace_free_mod_map);
5798                         break;
5799                 }
5800         }
5801
5802         /*
5803          * Each module has its own ftrace_pages, remove
5804          * them from the list.
5805          */
5806         last_pg = &ftrace_pages_start;
5807         for (pg = ftrace_pages_start; pg; pg = *last_pg) {
5808                 rec = &pg->records[0];
5809                 if (within_module_core(rec->ip, mod) ||
5810                     within_module_init(rec->ip, mod)) {
5811                         /*
5812                          * As core pages are first, the first
5813                          * page should never be a module page.
5814                          */
5815                         if (WARN_ON(pg == ftrace_pages_start))
5816                                 goto out_unlock;
5817
5818                         /* Check if we are deleting the last page */
5819                         if (pg == ftrace_pages)
5820                                 ftrace_pages = next_to_ftrace_page(last_pg);
5821
5822                         ftrace_update_tot_cnt -= pg->index;
5823                         *last_pg = pg->next;
5824
5825                         pg->next = tmp_page;
5826                         tmp_page = pg;
5827                 } else
5828                         last_pg = &pg->next;
5829         }
5830  out_unlock:
5831         mutex_unlock(&ftrace_lock);
5832
5833         for (pg = tmp_page; pg; pg = tmp_page) {
5834
5835                 /* Needs to be called outside of ftrace_lock */
5836                 clear_mod_from_hashes(pg);
5837
5838                 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
5839                 free_pages((unsigned long)pg->records, order);
5840                 tmp_page = pg->next;
5841                 kfree(pg);
5842         }
5843 }
5844
5845 void ftrace_module_enable(struct module *mod)
5846 {
5847         struct dyn_ftrace *rec;
5848         struct ftrace_page *pg;
5849
5850         mutex_lock(&ftrace_lock);
5851
5852         if (ftrace_disabled)
5853                 goto out_unlock;
5854
5855         /*
5856          * If the tracing is enabled, go ahead and enable the record.
5857          *
5858          * The reason not to enable the record immediatelly is the
5859          * inherent check of ftrace_make_nop/ftrace_make_call for
5860          * correct previous instructions.  Making first the NOP
5861          * conversion puts the module to the correct state, thus
5862          * passing the ftrace_make_call check.
5863          *
5864          * We also delay this to after the module code already set the
5865          * text to read-only, as we now need to set it back to read-write
5866          * so that we can modify the text.
5867          */
5868         if (ftrace_start_up)
5869                 ftrace_arch_code_modify_prepare();
5870
5871         do_for_each_ftrace_rec(pg, rec) {
5872                 int cnt;
5873                 /*
5874                  * do_for_each_ftrace_rec() is a double loop.
5875                  * module text shares the pg. If a record is
5876                  * not part of this module, then skip this pg,
5877                  * which the "break" will do.
5878                  */
5879                 if (!within_module_core(rec->ip, mod) &&
5880                     !within_module_init(rec->ip, mod))
5881                         break;
5882
5883                 cnt = 0;
5884
5885                 /*
5886                  * When adding a module, we need to check if tracers are
5887                  * currently enabled and if they are, and can trace this record,
5888                  * we need to enable the module functions as well as update the
5889                  * reference counts for those function records.
5890                  */
5891                 if (ftrace_start_up)
5892                         cnt += referenced_filters(rec);
5893
5894                 rec->flags &= ~FTRACE_FL_DISABLED;
5895                 rec->flags += cnt;
5896
5897                 if (ftrace_start_up && cnt) {
5898                         int failed = __ftrace_replace_code(rec, 1);
5899                         if (failed) {
5900                                 ftrace_bug(failed, rec);
5901                                 goto out_loop;
5902                         }
5903                 }
5904
5905         } while_for_each_ftrace_rec();
5906
5907  out_loop:
5908         if (ftrace_start_up)
5909                 ftrace_arch_code_modify_post_process();
5910
5911  out_unlock:
5912         mutex_unlock(&ftrace_lock);
5913
5914         process_cached_mods(mod->name);
5915 }
5916
5917 void ftrace_module_init(struct module *mod)
5918 {
5919         if (ftrace_disabled || !mod->num_ftrace_callsites)
5920                 return;
5921
5922         ftrace_process_locs(mod, mod->ftrace_callsites,
5923                             mod->ftrace_callsites + mod->num_ftrace_callsites);
5924 }
5925
5926 static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
5927                                 struct dyn_ftrace *rec)
5928 {
5929         struct ftrace_mod_func *mod_func;
5930         unsigned long symsize;
5931         unsigned long offset;
5932         char str[KSYM_SYMBOL_LEN];
5933         char *modname;
5934         const char *ret;
5935
5936         ret = kallsyms_lookup(rec->ip, &symsize, &offset, &modname, str);
5937         if (!ret)
5938                 return;
5939
5940         mod_func = kmalloc(sizeof(*mod_func), GFP_KERNEL);
5941         if (!mod_func)
5942                 return;
5943
5944         mod_func->name = kstrdup(str, GFP_KERNEL);
5945         if (!mod_func->name) {
5946                 kfree(mod_func);
5947                 return;
5948         }
5949
5950         mod_func->ip = rec->ip - offset;
5951         mod_func->size = symsize;
5952
5953         mod_map->num_funcs++;
5954
5955         list_add_rcu(&mod_func->list, &mod_map->funcs);
5956 }
5957
5958 static struct ftrace_mod_map *
5959 allocate_ftrace_mod_map(struct module *mod,
5960                         unsigned long start, unsigned long end)
5961 {
5962         struct ftrace_mod_map *mod_map;
5963
5964         mod_map = kmalloc(sizeof(*mod_map), GFP_KERNEL);
5965         if (!mod_map)
5966                 return NULL;
5967
5968         mod_map->mod = mod;
5969         mod_map->start_addr = start;
5970         mod_map->end_addr = end;
5971         mod_map->num_funcs = 0;
5972
5973         INIT_LIST_HEAD_RCU(&mod_map->funcs);
5974
5975         list_add_rcu(&mod_map->list, &ftrace_mod_maps);
5976
5977         return mod_map;
5978 }
5979
5980 static const char *
5981 ftrace_func_address_lookup(struct ftrace_mod_map *mod_map,
5982                            unsigned long addr, unsigned long *size,
5983                            unsigned long *off, char *sym)
5984 {
5985         struct ftrace_mod_func *found_func =  NULL;
5986         struct ftrace_mod_func *mod_func;
5987
5988         list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
5989                 if (addr >= mod_func->ip &&
5990                     addr < mod_func->ip + mod_func->size) {
5991                         found_func = mod_func;
5992                         break;
5993                 }
5994         }
5995
5996         if (found_func) {
5997                 if (size)
5998                         *size = found_func->size;
5999                 if (off)
6000                         *off = addr - found_func->ip;
6001                 if (sym)
6002                         strlcpy(sym, found_func->name, KSYM_NAME_LEN);
6003
6004                 return found_func->name;
6005         }
6006
6007         return NULL;
6008 }
6009
6010 const char *
6011 ftrace_mod_address_lookup(unsigned long addr, unsigned long *size,
6012                    unsigned long *off, char **modname, char *sym)
6013 {
6014         struct ftrace_mod_map *mod_map;
6015         const char *ret = NULL;
6016
6017         /* mod_map is freed via call_rcu_sched() */
6018         preempt_disable();
6019         list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
6020                 ret = ftrace_func_address_lookup(mod_map, addr, size, off, sym);
6021                 if (ret) {
6022                         if (modname)
6023                                 *modname = mod_map->mod->name;
6024                         break;
6025                 }
6026         }
6027         preempt_enable();
6028
6029         return ret;
6030 }
6031
6032 int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
6033                            char *type, char *name,
6034                            char *module_name, int *exported)
6035 {
6036         struct ftrace_mod_map *mod_map;
6037         struct ftrace_mod_func *mod_func;
6038
6039         preempt_disable();
6040         list_for_each_entry_rcu(mod_map, &ftrace_mod_maps, list) {
6041
6042                 if (symnum >= mod_map->num_funcs) {
6043                         symnum -= mod_map->num_funcs;
6044                         continue;
6045                 }
6046
6047                 list_for_each_entry_rcu(mod_func, &mod_map->funcs, list) {
6048                         if (symnum > 1) {
6049                                 symnum--;
6050                                 continue;
6051                         }
6052
6053                         *value = mod_func->ip;
6054                         *type = 'T';
6055                         strlcpy(name, mod_func->name, KSYM_NAME_LEN);
6056                         strlcpy(module_name, mod_map->mod->name, MODULE_NAME_LEN);
6057                         *exported = 1;
6058                         preempt_enable();
6059                         return 0;
6060                 }
6061                 WARN_ON(1);
6062                 break;
6063         }
6064         preempt_enable();
6065         return -ERANGE;
6066 }
6067
6068 #else
6069 static void save_ftrace_mod_rec(struct ftrace_mod_map *mod_map,
6070                                 struct dyn_ftrace *rec) { }
6071 static inline struct ftrace_mod_map *
6072 allocate_ftrace_mod_map(struct module *mod,
6073                         unsigned long start, unsigned long end)
6074 {
6075         return NULL;
6076 }
6077 #endif /* CONFIG_MODULES */
6078
6079 struct ftrace_init_func {
6080         struct list_head list;
6081         unsigned long ip;
6082 };
6083
6084 /* Clear any init ips from hashes */
6085 static void
6086 clear_func_from_hash(struct ftrace_init_func *func, struct ftrace_hash *hash)
6087 {
6088         struct ftrace_func_entry *entry;
6089
6090         if (ftrace_hash_empty(hash))
6091                 return;
6092
6093         entry = __ftrace_lookup_ip(hash, func->ip);
6094
6095         /*
6096          * Do not allow this rec to match again.
6097          * Yeah, it may waste some memory, but will be removed
6098          * if/when the hash is modified again.
6099          */
6100         if (entry)
6101                 entry->ip = 0;
6102 }
6103
6104 static void
6105 clear_func_from_hashes(struct ftrace_init_func *func)
6106 {
6107         struct trace_array *tr;
6108
6109         mutex_lock(&trace_types_lock);
6110         list_for_each_entry(tr, &ftrace_trace_arrays, list) {
6111                 if (!tr->ops || !tr->ops->func_hash)
6112                         continue;
6113                 mutex_lock(&tr->ops->func_hash->regex_lock);
6114                 clear_func_from_hash(func, tr->ops->func_hash->filter_hash);
6115                 clear_func_from_hash(func, tr->ops->func_hash->notrace_hash);
6116                 mutex_unlock(&tr->ops->func_hash->regex_lock);
6117         }
6118         mutex_unlock(&trace_types_lock);
6119 }
6120
6121 static void add_to_clear_hash_list(struct list_head *clear_list,
6122                                    struct dyn_ftrace *rec)
6123 {
6124         struct ftrace_init_func *func;
6125
6126         func = kmalloc(sizeof(*func), GFP_KERNEL);
6127         if (!func) {
6128                 WARN_ONCE(1, "alloc failure, ftrace filter could be stale\n");
6129                 return;
6130         }
6131
6132         func->ip = rec->ip;
6133         list_add(&func->list, clear_list);
6134 }
6135
6136 void ftrace_free_mem(struct module *mod, void *start_ptr, void *end_ptr)
6137 {
6138         unsigned long start = (unsigned long)(start_ptr);
6139         unsigned long end = (unsigned long)(end_ptr);
6140         struct ftrace_page **last_pg = &ftrace_pages_start;
6141         struct ftrace_page *pg;
6142         struct dyn_ftrace *rec;
6143         struct dyn_ftrace key;
6144         struct ftrace_mod_map *mod_map = NULL;
6145         struct ftrace_init_func *func, *func_next;
6146         struct list_head clear_hash;
6147         int order;
6148
6149         INIT_LIST_HEAD(&clear_hash);
6150
6151         key.ip = start;
6152         key.flags = end;        /* overload flags, as it is unsigned long */
6153
6154         mutex_lock(&ftrace_lock);
6155
6156         /*
6157          * If we are freeing module init memory, then check if
6158          * any tracer is active. If so, we need to save a mapping of
6159          * the module functions being freed with the address.
6160          */
6161         if (mod && ftrace_ops_list != &ftrace_list_end)
6162                 mod_map = allocate_ftrace_mod_map(mod, start, end);
6163
6164         for (pg = ftrace_pages_start; pg; last_pg = &pg->next, pg = *last_pg) {
6165                 if (end < pg->records[0].ip ||
6166                     start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
6167                         continue;
6168  again:
6169                 rec = bsearch(&key, pg->records, pg->index,
6170                               sizeof(struct dyn_ftrace),
6171                               ftrace_cmp_recs);
6172                 if (!rec)
6173                         continue;
6174
6175                 /* rec will be cleared from hashes after ftrace_lock unlock */
6176                 add_to_clear_hash_list(&clear_hash, rec);
6177
6178                 if (mod_map)
6179                         save_ftrace_mod_rec(mod_map, rec);
6180
6181                 pg->index--;
6182                 ftrace_update_tot_cnt--;
6183                 if (!pg->index) {
6184                         *last_pg = pg->next;
6185                         order = get_count_order(pg->size / ENTRIES_PER_PAGE);
6186                         free_pages((unsigned long)pg->records, order);
6187                         kfree(pg);
6188                         pg = container_of(last_pg, struct ftrace_page, next);
6189                         if (!(*last_pg))
6190                                 ftrace_pages = pg;
6191                         continue;
6192                 }
6193                 memmove(rec, rec + 1,
6194                         (pg->index - (rec - pg->records)) * sizeof(*rec));
6195                 /* More than one function may be in this block */
6196                 goto again;
6197         }
6198         mutex_unlock(&ftrace_lock);
6199
6200         list_for_each_entry_safe(func, func_next, &clear_hash, list) {
6201                 clear_func_from_hashes(func);
6202                 kfree(func);
6203         }
6204 }
6205
6206 void __init ftrace_free_init_mem(void)
6207 {
6208         void *start = (void *)(&__init_begin);
6209         void *end = (void *)(&__init_end);
6210
6211         ftrace_free_mem(NULL, start, end);
6212 }
6213
6214 void __init ftrace_init(void)
6215 {
6216         extern unsigned long __start_mcount_loc[];
6217         extern unsigned long __stop_mcount_loc[];
6218         unsigned long count, flags;
6219         int ret;
6220
6221         local_irq_save(flags);
6222         ret = ftrace_dyn_arch_init();
6223         local_irq_restore(flags);
6224         if (ret)
6225                 goto failed;
6226
6227         count = __stop_mcount_loc - __start_mcount_loc;
6228         if (!count) {
6229                 pr_info("ftrace: No functions to be traced?\n");
6230                 goto failed;
6231         }
6232
6233         pr_info("ftrace: allocating %ld entries in %ld pages\n",
6234                 count, DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
6235
6236         last_ftrace_enabled = ftrace_enabled = 1;
6237
6238         ret = ftrace_process_locs(NULL,
6239                                   __start_mcount_loc,
6240                                   __stop_mcount_loc);
6241
6242         set_ftrace_early_filters();
6243
6244         return;
6245  failed:
6246         ftrace_disabled = 1;
6247 }
6248
6249 /* Do nothing if arch does not support this */
6250 void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
6251 {
6252 }
6253
6254 static void ftrace_update_trampoline(struct ftrace_ops *ops)
6255 {
6256         arch_ftrace_update_trampoline(ops);
6257 }
6258
6259 void ftrace_init_trace_array(struct trace_array *tr)
6260 {
6261         INIT_LIST_HEAD(&tr->func_probes);
6262         INIT_LIST_HEAD(&tr->mod_trace);
6263         INIT_LIST_HEAD(&tr->mod_notrace);
6264 }
6265 #else
6266
6267 static struct ftrace_ops global_ops = {
6268         .func                   = ftrace_stub,
6269         .flags                  = FTRACE_OPS_FL_RECURSION_SAFE |
6270                                   FTRACE_OPS_FL_INITIALIZED |
6271                                   FTRACE_OPS_FL_PID,
6272 };
6273
6274 static int __init ftrace_nodyn_init(void)
6275 {
6276         ftrace_enabled = 1;
6277         return 0;
6278 }
6279 core_initcall(ftrace_nodyn_init);
6280
6281 static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; }
6282 static inline void ftrace_startup_enable(int command) { }
6283 static inline void ftrace_startup_all(int command) { }
6284 /* Keep as macros so we do not need to define the commands */
6285 # define ftrace_startup(ops, command)                                   \
6286         ({                                                              \
6287                 int ___ret = __register_ftrace_function(ops);           \
6288                 if (!___ret)                                            \
6289                         (ops)->flags |= FTRACE_OPS_FL_ENABLED;          \
6290                 ___ret;                                                 \
6291         })
6292 # define ftrace_shutdown(ops, command)                                  \
6293         ({                                                              \
6294                 int ___ret = __unregister_ftrace_function(ops);         \
6295                 if (!___ret)                                            \
6296                         (ops)->flags &= ~FTRACE_OPS_FL_ENABLED;         \
6297                 ___ret;                                                 \
6298         })
6299
6300 # define ftrace_startup_sysctl()        do { } while (0)
6301 # define ftrace_shutdown_sysctl()       do { } while (0)
6302
6303 static inline int
6304 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
6305 {
6306         return 1;
6307 }
6308
6309 static void ftrace_update_trampoline(struct ftrace_ops *ops)
6310 {
6311 }
6312
6313 #endif /* CONFIG_DYNAMIC_FTRACE */
6314
6315 __init void ftrace_init_global_array_ops(struct trace_array *tr)
6316 {
6317         tr->ops = &global_ops;
6318         tr->ops->private = tr;
6319         ftrace_init_trace_array(tr);
6320 }
6321
6322 void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
6323 {
6324         /* If we filter on pids, update to use the pid function */
6325         if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
6326                 if (WARN_ON(tr->ops->func != ftrace_stub))
6327                         printk("ftrace ops had %pS for function\n",
6328                                tr->ops->func);
6329         }
6330         tr->ops->func = func;
6331         tr->ops->private = tr;
6332 }
6333
6334 void ftrace_reset_array_ops(struct trace_array *tr)
6335 {
6336         tr->ops->func = ftrace_stub;
6337 }
6338
6339 static nokprobe_inline void
6340 __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
6341                        struct ftrace_ops *ignored, struct pt_regs *regs)
6342 {
6343         struct ftrace_ops *op;
6344         int bit;
6345
6346         bit = trace_test_and_set_recursion(TRACE_LIST_START);
6347         if (bit < 0)
6348                 return;
6349
6350         /*
6351          * Some of the ops may be dynamically allocated,
6352          * they must be freed after a synchronize_sched().
6353          */
6354         preempt_disable_notrace();
6355
6356         do_for_each_ftrace_op(op, ftrace_ops_list) {
6357                 /*
6358                  * Check the following for each ops before calling their func:
6359                  *  if RCU flag is set, then rcu_is_watching() must be true
6360                  *  if PER_CPU is set, then ftrace_function_local_disable()
6361                  *                          must be false
6362                  *  Otherwise test if the ip matches the ops filter
6363                  *
6364                  * If any of the above fails then the op->func() is not executed.
6365                  */
6366                 if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) &&
6367                     ftrace_ops_test(op, ip, regs)) {
6368                         if (FTRACE_WARN_ON(!op->func)) {
6369                                 pr_warn("op=%p %pS\n", op, op);
6370                                 goto out;
6371                         }
6372                         op->func(ip, parent_ip, op, regs);
6373                 }
6374         } while_for_each_ftrace_op(op);
6375 out:
6376         preempt_enable_notrace();
6377         trace_clear_recursion(bit);
6378 }
6379
6380 /*
6381  * Some archs only support passing ip and parent_ip. Even though
6382  * the list function ignores the op parameter, we do not want any
6383  * C side effects, where a function is called without the caller
6384  * sending a third parameter.
6385  * Archs are to support both the regs and ftrace_ops at the same time.
6386  * If they support ftrace_ops, it is assumed they support regs.
6387  * If call backs want to use regs, they must either check for regs
6388  * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
6389  * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
6390  * An architecture can pass partial regs with ftrace_ops and still
6391  * set the ARCH_SUPPORTS_FTRACE_OPS.
6392  */
6393 #if ARCH_SUPPORTS_FTRACE_OPS
6394 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
6395                                  struct ftrace_ops *op, struct pt_regs *regs)
6396 {
6397         __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
6398 }
6399 NOKPROBE_SYMBOL(ftrace_ops_list_func);
6400 #else
6401 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
6402 {
6403         __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
6404 }
6405 NOKPROBE_SYMBOL(ftrace_ops_no_ops);
6406 #endif
6407
6408 /*
6409  * If there's only one function registered but it does not support
6410  * recursion, needs RCU protection and/or requires per cpu handling, then
6411  * this function will be called by the mcount trampoline.
6412  */
6413 static void ftrace_ops_assist_func(unsigned long ip, unsigned long parent_ip,
6414                                    struct ftrace_ops *op, struct pt_regs *regs)
6415 {
6416         int bit;
6417
6418         bit = trace_test_and_set_recursion(TRACE_LIST_START);
6419         if (bit < 0)
6420                 return;
6421
6422         preempt_disable_notrace();
6423
6424         if (!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching())
6425                 op->func(ip, parent_ip, op, regs);
6426
6427         preempt_enable_notrace();
6428         trace_clear_recursion(bit);
6429 }
6430 NOKPROBE_SYMBOL(ftrace_ops_assist_func);
6431
6432 /**
6433  * ftrace_ops_get_func - get the function a trampoline should call
6434  * @ops: the ops to get the function for
6435  *
6436  * Normally the mcount trampoline will call the ops->func, but there
6437  * are times that it should not. For example, if the ops does not
6438  * have its own recursion protection, then it should call the
6439  * ftrace_ops_assist_func() instead.
6440  *
6441  * Returns the function that the trampoline should call for @ops.
6442  */
6443 ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
6444 {
6445         /*
6446          * If the function does not handle recursion, needs to be RCU safe,
6447          * or does per cpu logic, then we need to call the assist handler.
6448          */
6449         if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE) ||
6450             ops->flags & FTRACE_OPS_FL_RCU)
6451                 return ftrace_ops_assist_func;
6452
6453         return ops->func;
6454 }
6455
6456 static void
6457 ftrace_filter_pid_sched_switch_probe(void *data, bool preempt,
6458                     struct task_struct *prev, struct task_struct *next)
6459 {
6460         struct trace_array *tr = data;
6461         struct trace_pid_list *pid_list;
6462
6463         pid_list = rcu_dereference_sched(tr->function_pids);
6464
6465         this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
6466                        trace_ignore_this_task(pid_list, next));
6467 }
6468
6469 static void
6470 ftrace_pid_follow_sched_process_fork(void *data,
6471                                      struct task_struct *self,
6472                                      struct task_struct *task)
6473 {
6474         struct trace_pid_list *pid_list;
6475         struct trace_array *tr = data;
6476
6477         pid_list = rcu_dereference_sched(tr->function_pids);
6478         trace_filter_add_remove_task(pid_list, self, task);
6479 }
6480
6481 static void
6482 ftrace_pid_follow_sched_process_exit(void *data, struct task_struct *task)
6483 {
6484         struct trace_pid_list *pid_list;
6485         struct trace_array *tr = data;
6486
6487         pid_list = rcu_dereference_sched(tr->function_pids);
6488         trace_filter_add_remove_task(pid_list, NULL, task);
6489 }
6490
6491 void ftrace_pid_follow_fork(struct trace_array *tr, bool enable)
6492 {
6493         if (enable) {
6494                 register_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
6495                                                   tr);
6496                 register_trace_sched_process_free(ftrace_pid_follow_sched_process_exit,
6497                                                   tr);
6498         } else {
6499                 unregister_trace_sched_process_fork(ftrace_pid_follow_sched_process_fork,
6500                                                     tr);
6501                 unregister_trace_sched_process_free(ftrace_pid_follow_sched_process_exit,
6502                                                     tr);
6503         }
6504 }
6505
6506 static void clear_ftrace_pids(struct trace_array *tr)
6507 {
6508         struct trace_pid_list *pid_list;
6509         int cpu;
6510
6511         pid_list = rcu_dereference_protected(tr->function_pids,
6512                                              lockdep_is_held(&ftrace_lock));
6513         if (!pid_list)
6514                 return;
6515
6516         unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
6517
6518         for_each_possible_cpu(cpu)
6519                 per_cpu_ptr(tr->trace_buffer.data, cpu)->ftrace_ignore_pid = false;
6520
6521         rcu_assign_pointer(tr->function_pids, NULL);
6522
6523         /* Wait till all users are no longer using pid filtering */
6524         synchronize_sched();
6525
6526         trace_free_pid_list(pid_list);
6527 }
6528
6529 void ftrace_clear_pids(struct trace_array *tr)
6530 {
6531         mutex_lock(&ftrace_lock);
6532
6533         clear_ftrace_pids(tr);
6534
6535         mutex_unlock(&ftrace_lock);
6536 }
6537
6538 static void ftrace_pid_reset(struct trace_array *tr)
6539 {
6540         mutex_lock(&ftrace_lock);
6541         clear_ftrace_pids(tr);
6542
6543         ftrace_update_pid_func();
6544         ftrace_startup_all(0);
6545
6546         mutex_unlock(&ftrace_lock);
6547 }
6548
6549 /* Greater than any max PID */
6550 #define FTRACE_NO_PIDS          (void *)(PID_MAX_LIMIT + 1)
6551
6552 static void *fpid_start(struct seq_file *m, loff_t *pos)
6553         __acquires(RCU)
6554 {
6555         struct trace_pid_list *pid_list;
6556         struct trace_array *tr = m->private;
6557
6558         mutex_lock(&ftrace_lock);
6559         rcu_read_lock_sched();
6560
6561         pid_list = rcu_dereference_sched(tr->function_pids);
6562
6563         if (!pid_list)
6564                 return !(*pos) ? FTRACE_NO_PIDS : NULL;
6565
6566         return trace_pid_start(pid_list, pos);
6567 }
6568
6569 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
6570 {
6571         struct trace_array *tr = m->private;
6572         struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_pids);
6573
6574         if (v == FTRACE_NO_PIDS) {
6575                 (*pos)++;
6576                 return NULL;
6577         }
6578         return trace_pid_next(pid_list, v, pos);
6579 }
6580
6581 static void fpid_stop(struct seq_file *m, void *p)
6582         __releases(RCU)
6583 {
6584         rcu_read_unlock_sched();
6585         mutex_unlock(&ftrace_lock);
6586 }
6587
6588 static int fpid_show(struct seq_file *m, void *v)
6589 {
6590         if (v == FTRACE_NO_PIDS) {
6591                 seq_puts(m, "no pid\n");
6592                 return 0;
6593         }
6594
6595         return trace_pid_show(m, v);
6596 }
6597
6598 static const struct seq_operations ftrace_pid_sops = {
6599         .start = fpid_start,
6600         .next = fpid_next,
6601         .stop = fpid_stop,
6602         .show = fpid_show,
6603 };
6604
6605 static int
6606 ftrace_pid_open(struct inode *inode, struct file *file)
6607 {
6608         struct trace_array *tr = inode->i_private;
6609         struct seq_file *m;
6610         int ret = 0;
6611
6612         if (trace_array_get(tr) < 0)
6613                 return -ENODEV;
6614
6615         if ((file->f_mode & FMODE_WRITE) &&
6616             (file->f_flags & O_TRUNC))
6617                 ftrace_pid_reset(tr);
6618
6619         ret = seq_open(file, &ftrace_pid_sops);
6620         if (ret < 0) {
6621                 trace_array_put(tr);
6622         } else {
6623                 m = file->private_data;
6624                 /* copy tr over to seq ops */
6625                 m->private = tr;
6626         }
6627
6628         return ret;
6629 }
6630
6631 static void ignore_task_cpu(void *data)
6632 {
6633         struct trace_array *tr = data;
6634         struct trace_pid_list *pid_list;
6635
6636         /*
6637          * This function is called by on_each_cpu() while the
6638          * event_mutex is held.
6639          */
6640         pid_list = rcu_dereference_protected(tr->function_pids,
6641                                              mutex_is_locked(&ftrace_lock));
6642
6643         this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
6644                        trace_ignore_this_task(pid_list, current));
6645 }
6646
6647 static ssize_t
6648 ftrace_pid_write(struct file *filp, const char __user *ubuf,
6649                    size_t cnt, loff_t *ppos)
6650 {
6651         struct seq_file *m = filp->private_data;
6652         struct trace_array *tr = m->private;
6653         struct trace_pid_list *filtered_pids = NULL;
6654         struct trace_pid_list *pid_list;
6655         ssize_t ret;
6656
6657         if (!cnt)
6658                 return 0;
6659
6660         mutex_lock(&ftrace_lock);
6661
6662         filtered_pids = rcu_dereference_protected(tr->function_pids,
6663                                              lockdep_is_held(&ftrace_lock));
6664
6665         ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
6666         if (ret < 0)
6667                 goto out;
6668
6669         rcu_assign_pointer(tr->function_pids, pid_list);
6670
6671         if (filtered_pids) {
6672                 synchronize_sched();
6673                 trace_free_pid_list(filtered_pids);
6674         } else if (pid_list) {
6675                 /* Register a probe to set whether to ignore the tracing of a task */
6676                 register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
6677         }
6678
6679         /*
6680          * Ignoring of pids is done at task switch. But we have to
6681          * check for those tasks that are currently running.
6682          * Always do this in case a pid was appended or removed.
6683          */
6684         on_each_cpu(ignore_task_cpu, tr, 1);
6685
6686         ftrace_update_pid_func();
6687         ftrace_startup_all(0);
6688  out:
6689         mutex_unlock(&ftrace_lock);
6690
6691         if (ret > 0)
6692                 *ppos += ret;
6693
6694         return ret;
6695 }
6696
6697 static int
6698 ftrace_pid_release(struct inode *inode, struct file *file)
6699 {
6700         struct trace_array *tr = inode->i_private;
6701
6702         trace_array_put(tr);
6703
6704         return seq_release(inode, file);
6705 }
6706
6707 static const struct file_operations ftrace_pid_fops = {
6708         .open           = ftrace_pid_open,
6709         .write          = ftrace_pid_write,
6710         .read           = seq_read,
6711         .llseek         = tracing_lseek,
6712         .release        = ftrace_pid_release,
6713 };
6714
6715 void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer)
6716 {
6717         trace_create_file("set_ftrace_pid", 0644, d_tracer,
6718                             tr, &ftrace_pid_fops);
6719 }
6720
6721 void __init ftrace_init_tracefs_toplevel(struct trace_array *tr,
6722                                          struct dentry *d_tracer)
6723 {
6724         /* Only the top level directory has the dyn_tracefs and profile */
6725         WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL));
6726
6727         ftrace_init_dyn_tracefs(d_tracer);
6728         ftrace_profile_tracefs(d_tracer);
6729 }
6730
6731 /**
6732  * ftrace_kill - kill ftrace
6733  *
6734  * This function should be used by panic code. It stops ftrace
6735  * but in a not so nice way. If you need to simply kill ftrace
6736  * from a non-atomic section, use ftrace_kill.
6737  */
6738 void ftrace_kill(void)
6739 {
6740         ftrace_disabled = 1;
6741         ftrace_enabled = 0;
6742         ftrace_trace_function = ftrace_stub;
6743 }
6744
6745 /**
6746  * Test if ftrace is dead or not.
6747  */
6748 int ftrace_is_dead(void)
6749 {
6750         return ftrace_disabled;
6751 }
6752
6753 /**
6754  * register_ftrace_function - register a function for profiling
6755  * @ops - ops structure that holds the function for profiling.
6756  *
6757  * Register a function to be called by all functions in the
6758  * kernel.
6759  *
6760  * Note: @ops->func and all the functions it calls must be labeled
6761  *       with "notrace", otherwise it will go into a
6762  *       recursive loop.
6763  */
6764 int register_ftrace_function(struct ftrace_ops *ops)
6765 {
6766         int ret = -1;
6767
6768         ftrace_ops_init(ops);
6769
6770         mutex_lock(&ftrace_lock);
6771
6772         ret = ftrace_startup(ops, 0);
6773
6774         mutex_unlock(&ftrace_lock);
6775
6776         return ret;
6777 }
6778 EXPORT_SYMBOL_GPL(register_ftrace_function);
6779
6780 /**
6781  * unregister_ftrace_function - unregister a function for profiling.
6782  * @ops - ops structure that holds the function to unregister
6783  *
6784  * Unregister a function that was added to be called by ftrace profiling.
6785  */
6786 int unregister_ftrace_function(struct ftrace_ops *ops)
6787 {
6788         int ret;
6789
6790         mutex_lock(&ftrace_lock);
6791         ret = ftrace_shutdown(ops, 0);
6792         mutex_unlock(&ftrace_lock);
6793
6794         return ret;
6795 }
6796 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
6797
6798 int
6799 ftrace_enable_sysctl(struct ctl_table *table, int write,
6800                      void __user *buffer, size_t *lenp,
6801                      loff_t *ppos)
6802 {
6803         int ret = -ENODEV;
6804
6805         mutex_lock(&ftrace_lock);
6806
6807         if (unlikely(ftrace_disabled))
6808                 goto out;
6809
6810         ret = proc_dointvec(table, write, buffer, lenp, ppos);
6811
6812         if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
6813                 goto out;
6814
6815         last_ftrace_enabled = !!ftrace_enabled;
6816
6817         if (ftrace_enabled) {
6818
6819                 /* we are starting ftrace again */
6820                 if (rcu_dereference_protected(ftrace_ops_list,
6821                         lockdep_is_held(&ftrace_lock)) != &ftrace_list_end)
6822                         update_ftrace_function();
6823
6824                 ftrace_startup_sysctl();
6825
6826         } else {
6827                 /* stopping ftrace calls (just send to ftrace_stub) */
6828                 ftrace_trace_function = ftrace_stub;
6829
6830                 ftrace_shutdown_sysctl();
6831         }
6832
6833  out:
6834         mutex_unlock(&ftrace_lock);
6835         return ret;
6836 }
6837
6838 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
6839
6840 static struct ftrace_ops graph_ops = {
6841         .func                   = ftrace_stub,
6842         .flags                  = FTRACE_OPS_FL_RECURSION_SAFE |
6843                                    FTRACE_OPS_FL_INITIALIZED |
6844                                    FTRACE_OPS_FL_PID |
6845                                    FTRACE_OPS_FL_STUB,
6846 #ifdef FTRACE_GRAPH_TRAMP_ADDR
6847         .trampoline             = FTRACE_GRAPH_TRAMP_ADDR,
6848         /* trampoline_size is only needed for dynamically allocated tramps */
6849 #endif
6850         ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
6851 };
6852
6853 void ftrace_graph_sleep_time_control(bool enable)
6854 {
6855         fgraph_sleep_time = enable;
6856 }
6857
6858 void ftrace_graph_graph_time_control(bool enable)
6859 {
6860         fgraph_graph_time = enable;
6861 }
6862
6863 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
6864 {
6865         return 0;
6866 }
6867
6868 /* The callbacks that hook a function */
6869 trace_func_graph_ret_t ftrace_graph_return =
6870                         (trace_func_graph_ret_t)ftrace_stub;
6871 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
6872 static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
6873
6874 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
6875 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
6876 {
6877         int i;
6878         int ret = 0;
6879         int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
6880         struct task_struct *g, *t;
6881
6882         for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
6883                 ret_stack_list[i] =
6884                         kmalloc_array(FTRACE_RETFUNC_DEPTH,
6885                                       sizeof(struct ftrace_ret_stack),
6886                                       GFP_KERNEL);
6887                 if (!ret_stack_list[i]) {
6888                         start = 0;
6889                         end = i;
6890                         ret = -ENOMEM;
6891                         goto free;
6892                 }
6893         }
6894
6895         read_lock(&tasklist_lock);
6896         do_each_thread(g, t) {
6897                 if (start == end) {
6898                         ret = -EAGAIN;
6899                         goto unlock;
6900                 }
6901
6902                 if (t->ret_stack == NULL) {
6903                         atomic_set(&t->trace_overrun, 0);
6904                         t->curr_ret_stack = -1;
6905                         t->curr_ret_depth = -1;
6906                         /* Make sure the tasks see the -1 first: */
6907                         smp_wmb();
6908                         t->ret_stack = ret_stack_list[start++];
6909                 }
6910         } while_each_thread(g, t);
6911
6912 unlock:
6913         read_unlock(&tasklist_lock);
6914 free:
6915         for (i = start; i < end; i++)
6916                 kfree(ret_stack_list[i]);
6917         return ret;
6918 }
6919
6920 static void
6921 ftrace_graph_probe_sched_switch(void *ignore, bool preempt,
6922                         struct task_struct *prev, struct task_struct *next)
6923 {
6924         unsigned long long timestamp;
6925         int index;
6926
6927         /*
6928          * Does the user want to count the time a function was asleep.
6929          * If so, do not update the time stamps.
6930          */
6931         if (fgraph_sleep_time)
6932                 return;
6933
6934         timestamp = trace_clock_local();
6935
6936         prev->ftrace_timestamp = timestamp;
6937
6938         /* only process tasks that we timestamped */
6939         if (!next->ftrace_timestamp)
6940                 return;
6941
6942         /*
6943          * Update all the counters in next to make up for the
6944          * time next was sleeping.
6945          */
6946         timestamp -= next->ftrace_timestamp;
6947
6948         for (index = next->curr_ret_stack; index >= 0; index--)
6949                 next->ret_stack[index].calltime += timestamp;
6950 }
6951
6952 /* Allocate a return stack for each task */
6953 static int start_graph_tracing(void)
6954 {
6955         struct ftrace_ret_stack **ret_stack_list;
6956         int ret, cpu;
6957
6958         ret_stack_list = kmalloc_array(FTRACE_RETSTACK_ALLOC_SIZE,
6959                                        sizeof(struct ftrace_ret_stack *),
6960                                        GFP_KERNEL);
6961
6962         if (!ret_stack_list)
6963                 return -ENOMEM;
6964
6965         /* The cpu_boot init_task->ret_stack will never be freed */
6966         for_each_online_cpu(cpu) {
6967                 if (!idle_task(cpu)->ret_stack)
6968                         ftrace_graph_init_idle_task(idle_task(cpu), cpu);
6969         }
6970
6971         do {
6972                 ret = alloc_retstack_tasklist(ret_stack_list);
6973         } while (ret == -EAGAIN);
6974
6975         if (!ret) {
6976                 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
6977                 if (ret)
6978                         pr_info("ftrace_graph: Couldn't activate tracepoint"
6979                                 " probe to kernel_sched_switch\n");
6980         }
6981
6982         kfree(ret_stack_list);
6983         return ret;
6984 }
6985
6986 /*
6987  * Hibernation protection.
6988  * The state of the current task is too much unstable during
6989  * suspend/restore to disk. We want to protect against that.
6990  */
6991 static int
6992 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
6993                                                         void *unused)
6994 {
6995         switch (state) {
6996         case PM_HIBERNATION_PREPARE:
6997                 pause_graph_tracing();
6998                 break;
6999
7000         case PM_POST_HIBERNATION:
7001                 unpause_graph_tracing();
7002                 break;
7003         }
7004         return NOTIFY_DONE;
7005 }
7006
7007 static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
7008 {
7009         if (!ftrace_ops_test(&global_ops, trace->func, NULL))
7010                 return 0;
7011         return __ftrace_graph_entry(trace);
7012 }
7013
7014 /*
7015  * The function graph tracer should only trace the functions defined
7016  * by set_ftrace_filter and set_ftrace_notrace. If another function
7017  * tracer ops is registered, the graph tracer requires testing the
7018  * function against the global ops, and not just trace any function
7019  * that any ftrace_ops registered.
7020  */
7021 static void update_function_graph_func(void)
7022 {
7023         struct ftrace_ops *op;
7024         bool do_test = false;
7025
7026         /*
7027          * The graph and global ops share the same set of functions
7028          * to test. If any other ops is on the list, then
7029          * the graph tracing needs to test if its the function
7030          * it should call.
7031          */
7032         do_for_each_ftrace_op(op, ftrace_ops_list) {
7033                 if (op != &global_ops && op != &graph_ops &&
7034                     op != &ftrace_list_end) {
7035                         do_test = true;
7036                         /* in double loop, break out with goto */
7037                         goto out;
7038                 }
7039         } while_for_each_ftrace_op(op);
7040  out:
7041         if (do_test)
7042                 ftrace_graph_entry = ftrace_graph_entry_test;
7043         else
7044                 ftrace_graph_entry = __ftrace_graph_entry;
7045 }
7046
7047 static struct notifier_block ftrace_suspend_notifier = {
7048         .notifier_call = ftrace_suspend_notifier_call,
7049 };
7050
7051 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
7052                         trace_func_graph_ent_t entryfunc)
7053 {
7054         int ret = 0;
7055
7056         mutex_lock(&ftrace_lock);
7057
7058         /* we currently allow only one tracer registered at a time */
7059         if (ftrace_graph_active) {
7060                 ret = -EBUSY;
7061                 goto out;
7062         }
7063
7064         register_pm_notifier(&ftrace_suspend_notifier);
7065
7066         ftrace_graph_active++;
7067         ret = start_graph_tracing();
7068         if (ret) {
7069                 ftrace_graph_active--;
7070                 goto out;
7071         }
7072
7073         ftrace_graph_return = retfunc;
7074
7075         /*
7076          * Update the indirect function to the entryfunc, and the
7077          * function that gets called to the entry_test first. Then
7078          * call the update fgraph entry function to determine if
7079          * the entryfunc should be called directly or not.
7080          */
7081         __ftrace_graph_entry = entryfunc;
7082         ftrace_graph_entry = ftrace_graph_entry_test;
7083         update_function_graph_func();
7084
7085         ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
7086 out:
7087         mutex_unlock(&ftrace_lock);
7088         return ret;
7089 }
7090
7091 void unregister_ftrace_graph(void)
7092 {
7093         mutex_lock(&ftrace_lock);
7094
7095         if (unlikely(!ftrace_graph_active))
7096                 goto out;
7097
7098         ftrace_graph_active--;
7099         ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
7100         ftrace_graph_entry = ftrace_graph_entry_stub;
7101         __ftrace_graph_entry = ftrace_graph_entry_stub;
7102         ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
7103         unregister_pm_notifier(&ftrace_suspend_notifier);
7104         unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
7105
7106  out:
7107         mutex_unlock(&ftrace_lock);
7108 }
7109
7110 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
7111
7112 static void
7113 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
7114 {
7115         atomic_set(&t->trace_overrun, 0);
7116         t->ftrace_timestamp = 0;
7117         /* make curr_ret_stack visible before we add the ret_stack */
7118         smp_wmb();
7119         t->ret_stack = ret_stack;
7120 }
7121
7122 /*
7123  * Allocate a return stack for the idle task. May be the first
7124  * time through, or it may be done by CPU hotplug online.
7125  */
7126 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
7127 {
7128         t->curr_ret_stack = -1;
7129         t->curr_ret_depth = -1;
7130         /*
7131          * The idle task has no parent, it either has its own
7132          * stack or no stack at all.
7133          */
7134         if (t->ret_stack)
7135                 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
7136
7137         if (ftrace_graph_active) {
7138                 struct ftrace_ret_stack *ret_stack;
7139
7140                 ret_stack = per_cpu(idle_ret_stack, cpu);
7141                 if (!ret_stack) {
7142                         ret_stack =
7143                                 kmalloc_array(FTRACE_RETFUNC_DEPTH,
7144                                               sizeof(struct ftrace_ret_stack),
7145                                               GFP_KERNEL);
7146                         if (!ret_stack)
7147                                 return;
7148                         per_cpu(idle_ret_stack, cpu) = ret_stack;
7149                 }
7150                 graph_init_task(t, ret_stack);
7151         }
7152 }
7153
7154 /* Allocate a return stack for newly created task */
7155 void ftrace_graph_init_task(struct task_struct *t)
7156 {
7157         /* Make sure we do not use the parent ret_stack */
7158         t->ret_stack = NULL;
7159         t->curr_ret_stack = -1;
7160         t->curr_ret_depth = -1;
7161
7162         if (ftrace_graph_active) {
7163                 struct ftrace_ret_stack *ret_stack;
7164
7165                 ret_stack = kmalloc_array(FTRACE_RETFUNC_DEPTH,
7166                                           sizeof(struct ftrace_ret_stack),
7167                                           GFP_KERNEL);
7168                 if (!ret_stack)
7169                         return;
7170                 graph_init_task(t, ret_stack);
7171         }
7172 }
7173
7174 void ftrace_graph_exit_task(struct task_struct *t)
7175 {
7176         struct ftrace_ret_stack *ret_stack = t->ret_stack;
7177
7178         t->ret_stack = NULL;
7179         /* NULL must become visible to IRQs before we free it: */
7180         barrier();
7181
7182         kfree(ret_stack);
7183 }
7184 #endif