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