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