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