OSDN Git Service

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