GNU Linux-libre 4.9.337-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         /*
2741          * If ftrace is in an undefined state, we just remove ops from list
2742          * to prevent the NULL pointer, instead of totally rolling it back and
2743          * free trampoline, because those actions could cause further damage.
2744          */
2745         if (unlikely(ftrace_disabled)) {
2746                 __unregister_ftrace_function(ops);
2747                 return -ENODEV;
2748         }
2749
2750         ops->flags &= ~FTRACE_OPS_FL_ADDING;
2751
2752         return 0;
2753 }
2754
2755 static int ftrace_shutdown(struct ftrace_ops *ops, int command)
2756 {
2757         int ret;
2758
2759         if (unlikely(ftrace_disabled))
2760                 return -ENODEV;
2761
2762         ret = __unregister_ftrace_function(ops);
2763         if (ret)
2764                 return ret;
2765
2766         ftrace_start_up--;
2767         /*
2768          * Just warn in case of unbalance, no need to kill ftrace, it's not
2769          * critical but the ftrace_call callers may be never nopped again after
2770          * further ftrace uses.
2771          */
2772         WARN_ON_ONCE(ftrace_start_up < 0);
2773
2774         /* Disabling ipmodify never fails */
2775         ftrace_hash_ipmodify_disable(ops);
2776
2777         if (ftrace_hash_rec_disable(ops, 1))
2778                 command |= FTRACE_UPDATE_CALLS;
2779
2780         ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2781
2782         if (saved_ftrace_func != ftrace_trace_function) {
2783                 saved_ftrace_func = ftrace_trace_function;
2784                 command |= FTRACE_UPDATE_TRACE_FUNC;
2785         }
2786
2787         if (!command || !ftrace_enabled) {
2788                 /*
2789                  * If these are dynamic or per_cpu ops, they still
2790                  * need their data freed. Since, function tracing is
2791                  * not currently active, we can just free them
2792                  * without synchronizing all CPUs.
2793                  */
2794                 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_PER_CPU))
2795                         goto free_ops;
2796
2797                 return 0;
2798         }
2799
2800         /*
2801          * If the ops uses a trampoline, then it needs to be
2802          * tested first on update.
2803          */
2804         ops->flags |= FTRACE_OPS_FL_REMOVING;
2805         removed_ops = ops;
2806
2807         /* The trampoline logic checks the old hashes */
2808         ops->old_hash.filter_hash = ops->func_hash->filter_hash;
2809         ops->old_hash.notrace_hash = ops->func_hash->notrace_hash;
2810
2811         ftrace_run_update_code(command);
2812
2813         /*
2814          * If there's no more ops registered with ftrace, run a
2815          * sanity check to make sure all rec flags are cleared.
2816          */
2817         if (ftrace_ops_list == &ftrace_list_end) {
2818                 struct ftrace_page *pg;
2819                 struct dyn_ftrace *rec;
2820
2821                 do_for_each_ftrace_rec(pg, rec) {
2822                         if (FTRACE_WARN_ON_ONCE(rec->flags & ~FTRACE_FL_DISABLED))
2823                                 pr_warn("  %pS flags:%lx\n",
2824                                         (void *)rec->ip, rec->flags);
2825                 } while_for_each_ftrace_rec();
2826         }
2827
2828         ops->old_hash.filter_hash = NULL;
2829         ops->old_hash.notrace_hash = NULL;
2830
2831         removed_ops = NULL;
2832         ops->flags &= ~FTRACE_OPS_FL_REMOVING;
2833
2834         /*
2835          * Dynamic ops may be freed, we must make sure that all
2836          * callers are done before leaving this function.
2837          * The same goes for freeing the per_cpu data of the per_cpu
2838          * ops.
2839          *
2840          * Again, normal synchronize_sched() is not good enough.
2841          * We need to do a hard force of sched synchronization.
2842          * This is because we use preempt_disable() to do RCU, but
2843          * the function tracers can be called where RCU is not watching
2844          * (like before user_exit()). We can not rely on the RCU
2845          * infrastructure to do the synchronization, thus we must do it
2846          * ourselves.
2847          */
2848         if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_PER_CPU)) {
2849                 schedule_on_each_cpu(ftrace_sync);
2850
2851  free_ops:
2852                 arch_ftrace_trampoline_free(ops);
2853
2854                 if (ops->flags & FTRACE_OPS_FL_PER_CPU)
2855                         per_cpu_ops_free(ops);
2856         }
2857
2858         return 0;
2859 }
2860
2861 static void ftrace_startup_sysctl(void)
2862 {
2863         int command;
2864
2865         if (unlikely(ftrace_disabled))
2866                 return;
2867
2868         /* Force update next time */
2869         saved_ftrace_func = NULL;
2870         /* ftrace_start_up is true if we want ftrace running */
2871         if (ftrace_start_up) {
2872                 command = FTRACE_UPDATE_CALLS;
2873                 if (ftrace_graph_active)
2874                         command |= FTRACE_START_FUNC_RET;
2875                 ftrace_startup_enable(command);
2876         }
2877 }
2878
2879 static void ftrace_shutdown_sysctl(void)
2880 {
2881         int command;
2882
2883         if (unlikely(ftrace_disabled))
2884                 return;
2885
2886         /* ftrace_start_up is true if ftrace is running */
2887         if (ftrace_start_up) {
2888                 command = FTRACE_DISABLE_CALLS;
2889                 if (ftrace_graph_active)
2890                         command |= FTRACE_STOP_FUNC_RET;
2891                 ftrace_run_update_code(command);
2892         }
2893 }
2894
2895 static cycle_t          ftrace_update_time;
2896 unsigned long           ftrace_update_tot_cnt;
2897
2898 static inline int ops_traces_mod(struct ftrace_ops *ops)
2899 {
2900         /*
2901          * Filter_hash being empty will default to trace module.
2902          * But notrace hash requires a test of individual module functions.
2903          */
2904         return ftrace_hash_empty(ops->func_hash->filter_hash) &&
2905                 ftrace_hash_empty(ops->func_hash->notrace_hash);
2906 }
2907
2908 /*
2909  * Check if the current ops references the record.
2910  *
2911  * If the ops traces all functions, then it was already accounted for.
2912  * If the ops does not trace the current record function, skip it.
2913  * If the ops ignores the function via notrace filter, skip it.
2914  */
2915 static inline bool
2916 ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2917 {
2918         /* If ops isn't enabled, ignore it */
2919         if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2920                 return 0;
2921
2922         /* If ops traces all then it includes this function */
2923         if (ops_traces_mod(ops))
2924                 return 1;
2925
2926         /* The function must be in the filter */
2927         if (!ftrace_hash_empty(ops->func_hash->filter_hash) &&
2928             !ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))
2929                 return 0;
2930
2931         /* If in notrace hash, we ignore it too */
2932         if (ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip))
2933                 return 0;
2934
2935         return 1;
2936 }
2937
2938 static int ftrace_update_code(struct module *mod, struct ftrace_page *new_pgs)
2939 {
2940         struct ftrace_page *pg;
2941         struct dyn_ftrace *p;
2942         cycle_t start, stop;
2943         unsigned long update_cnt = 0;
2944         unsigned long rec_flags = 0;
2945         int i;
2946
2947         start = ftrace_now(raw_smp_processor_id());
2948
2949         /*
2950          * When a module is loaded, this function is called to convert
2951          * the calls to mcount in its text to nops, and also to create
2952          * an entry in the ftrace data. Now, if ftrace is activated
2953          * after this call, but before the module sets its text to
2954          * read-only, the modification of enabling ftrace can fail if
2955          * the read-only is done while ftrace is converting the calls.
2956          * To prevent this, the module's records are set as disabled
2957          * and will be enabled after the call to set the module's text
2958          * to read-only.
2959          */
2960         if (mod)
2961                 rec_flags |= FTRACE_FL_DISABLED;
2962
2963         for (pg = new_pgs; pg; pg = pg->next) {
2964
2965                 for (i = 0; i < pg->index; i++) {
2966
2967                         /* If something went wrong, bail without enabling anything */
2968                         if (unlikely(ftrace_disabled))
2969                                 return -1;
2970
2971                         p = &pg->records[i];
2972                         p->flags = rec_flags;
2973
2974                         /*
2975                          * Do the initial record conversion from mcount jump
2976                          * to the NOP instructions.
2977                          */
2978                         if (!ftrace_code_disable(mod, p))
2979                                 break;
2980
2981                         update_cnt++;
2982                 }
2983         }
2984
2985         stop = ftrace_now(raw_smp_processor_id());
2986         ftrace_update_time = stop - start;
2987         ftrace_update_tot_cnt += update_cnt;
2988
2989         return 0;
2990 }
2991
2992 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
2993 {
2994         int order;
2995         int cnt;
2996
2997         if (WARN_ON(!count))
2998                 return -EINVAL;
2999
3000         order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
3001
3002         /*
3003          * We want to fill as much as possible. No more than a page
3004          * may be empty.
3005          */
3006         while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
3007                 order--;
3008
3009  again:
3010         pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
3011
3012         if (!pg->records) {
3013                 /* if we can't allocate this size, try something smaller */
3014                 if (!order)
3015                         return -ENOMEM;
3016                 order--;
3017                 goto again;
3018         }
3019
3020         cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
3021         pg->size = cnt;
3022
3023         if (cnt > count)
3024                 cnt = count;
3025
3026         return cnt;
3027 }
3028
3029 static struct ftrace_page *
3030 ftrace_allocate_pages(unsigned long num_to_init)
3031 {
3032         struct ftrace_page *start_pg;
3033         struct ftrace_page *pg;
3034         int order;
3035         int cnt;
3036
3037         if (!num_to_init)
3038                 return 0;
3039
3040         start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
3041         if (!pg)
3042                 return NULL;
3043
3044         /*
3045          * Try to allocate as much as possible in one continues
3046          * location that fills in all of the space. We want to
3047          * waste as little space as possible.
3048          */
3049         for (;;) {
3050                 cnt = ftrace_allocate_records(pg, num_to_init);
3051                 if (cnt < 0)
3052                         goto free_pages;
3053
3054                 num_to_init -= cnt;
3055                 if (!num_to_init)
3056                         break;
3057
3058                 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
3059                 if (!pg->next)
3060                         goto free_pages;
3061
3062                 pg = pg->next;
3063         }
3064
3065         return start_pg;
3066
3067  free_pages:
3068         pg = start_pg;
3069         while (pg) {
3070                 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3071                 free_pages((unsigned long)pg->records, order);
3072                 start_pg = pg->next;
3073                 kfree(pg);
3074                 pg = start_pg;
3075         }
3076         pr_info("ftrace: FAILED to allocate memory for functions\n");
3077         return NULL;
3078 }
3079
3080 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
3081
3082 struct ftrace_iterator {
3083         loff_t                          pos;
3084         loff_t                          func_pos;
3085         struct ftrace_page              *pg;
3086         struct dyn_ftrace               *func;
3087         struct ftrace_func_probe        *probe;
3088         struct trace_parser             parser;
3089         struct ftrace_hash              *hash;
3090         struct ftrace_ops               *ops;
3091         int                             hidx;
3092         int                             idx;
3093         unsigned                        flags;
3094 };
3095
3096 static void *
3097 t_hash_next(struct seq_file *m, loff_t *pos)
3098 {
3099         struct ftrace_iterator *iter = m->private;
3100         struct hlist_node *hnd = NULL;
3101         struct hlist_head *hhd;
3102
3103         (*pos)++;
3104         iter->pos = *pos;
3105
3106         if (iter->probe)
3107                 hnd = &iter->probe->node;
3108  retry:
3109         if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
3110                 return NULL;
3111
3112         hhd = &ftrace_func_hash[iter->hidx];
3113
3114         if (hlist_empty(hhd)) {
3115                 iter->hidx++;
3116                 hnd = NULL;
3117                 goto retry;
3118         }
3119
3120         if (!hnd)
3121                 hnd = hhd->first;
3122         else {
3123                 hnd = hnd->next;
3124                 if (!hnd) {
3125                         iter->hidx++;
3126                         goto retry;
3127                 }
3128         }
3129
3130         if (WARN_ON_ONCE(!hnd))
3131                 return NULL;
3132
3133         iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
3134
3135         return iter;
3136 }
3137
3138 static void *t_hash_start(struct seq_file *m, loff_t *pos)
3139 {
3140         struct ftrace_iterator *iter = m->private;
3141         void *p = NULL;
3142         loff_t l;
3143
3144         if (!(iter->flags & FTRACE_ITER_DO_HASH))
3145                 return NULL;
3146
3147         if (iter->func_pos > *pos)
3148                 return NULL;
3149
3150         iter->hidx = 0;
3151         for (l = 0; l <= (*pos - iter->func_pos); ) {
3152                 p = t_hash_next(m, &l);
3153                 if (!p)
3154                         break;
3155         }
3156         if (!p)
3157                 return NULL;
3158
3159         /* Only set this if we have an item */
3160         iter->flags |= FTRACE_ITER_HASH;
3161
3162         return iter;
3163 }
3164
3165 static int
3166 t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
3167 {
3168         struct ftrace_func_probe *rec;
3169
3170         rec = iter->probe;
3171         if (WARN_ON_ONCE(!rec))
3172                 return -EIO;
3173
3174         if (rec->ops->print)
3175                 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
3176
3177         seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
3178
3179         if (rec->data)
3180                 seq_printf(m, ":%p", rec->data);
3181         seq_putc(m, '\n');
3182
3183         return 0;
3184 }
3185
3186 static void *
3187 t_next(struct seq_file *m, void *v, loff_t *pos)
3188 {
3189         struct ftrace_iterator *iter = m->private;
3190         struct ftrace_ops *ops = iter->ops;
3191         struct dyn_ftrace *rec = NULL;
3192
3193         if (unlikely(ftrace_disabled))
3194                 return NULL;
3195
3196         if (iter->flags & FTRACE_ITER_HASH)
3197                 return t_hash_next(m, pos);
3198
3199         (*pos)++;
3200         iter->pos = iter->func_pos = *pos;
3201
3202         if (iter->flags & FTRACE_ITER_PRINTALL)
3203                 return t_hash_start(m, pos);
3204
3205  retry:
3206         if (iter->idx >= iter->pg->index) {
3207                 if (iter->pg->next) {
3208                         iter->pg = iter->pg->next;
3209                         iter->idx = 0;
3210                         goto retry;
3211                 }
3212         } else {
3213                 rec = &iter->pg->records[iter->idx++];
3214                 if (((iter->flags & FTRACE_ITER_FILTER) &&
3215                      !(ftrace_lookup_ip(ops->func_hash->filter_hash, rec->ip))) ||
3216
3217                     ((iter->flags & FTRACE_ITER_NOTRACE) &&
3218                      !ftrace_lookup_ip(ops->func_hash->notrace_hash, rec->ip)) ||
3219
3220                     ((iter->flags & FTRACE_ITER_ENABLED) &&
3221                      !(rec->flags & FTRACE_FL_ENABLED))) {
3222
3223                         rec = NULL;
3224                         goto retry;
3225                 }
3226         }
3227
3228         if (!rec)
3229                 return t_hash_start(m, pos);
3230
3231         iter->func = rec;
3232
3233         return iter;
3234 }
3235
3236 static void reset_iter_read(struct ftrace_iterator *iter)
3237 {
3238         iter->pos = 0;
3239         iter->func_pos = 0;
3240         iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
3241 }
3242
3243 static void *t_start(struct seq_file *m, loff_t *pos)
3244 {
3245         struct ftrace_iterator *iter = m->private;
3246         struct ftrace_ops *ops = iter->ops;
3247         void *p = NULL;
3248         loff_t l;
3249
3250         mutex_lock(&ftrace_lock);
3251
3252         if (unlikely(ftrace_disabled))
3253                 return NULL;
3254
3255         /*
3256          * If an lseek was done, then reset and start from beginning.
3257          */
3258         if (*pos < iter->pos)
3259                 reset_iter_read(iter);
3260
3261         /*
3262          * For set_ftrace_filter reading, if we have the filter
3263          * off, we can short cut and just print out that all
3264          * functions are enabled.
3265          */
3266         if ((iter->flags & FTRACE_ITER_FILTER &&
3267              ftrace_hash_empty(ops->func_hash->filter_hash)) ||
3268             (iter->flags & FTRACE_ITER_NOTRACE &&
3269              ftrace_hash_empty(ops->func_hash->notrace_hash))) {
3270                 if (*pos > 0)
3271                         return t_hash_start(m, pos);
3272                 iter->flags |= FTRACE_ITER_PRINTALL;
3273                 /* reset in case of seek/pread */
3274                 iter->flags &= ~FTRACE_ITER_HASH;
3275                 return iter;
3276         }
3277
3278         if (iter->flags & FTRACE_ITER_HASH)
3279                 return t_hash_start(m, pos);
3280
3281         /*
3282          * Unfortunately, we need to restart at ftrace_pages_start
3283          * every time we let go of the ftrace_mutex. This is because
3284          * those pointers can change without the lock.
3285          */
3286         iter->pg = ftrace_pages_start;
3287         iter->idx = 0;
3288         for (l = 0; l <= *pos; ) {
3289                 p = t_next(m, p, &l);
3290                 if (!p)
3291                         break;
3292         }
3293
3294         if (!p)
3295                 return t_hash_start(m, pos);
3296
3297         return iter;
3298 }
3299
3300 static void t_stop(struct seq_file *m, void *p)
3301 {
3302         mutex_unlock(&ftrace_lock);
3303 }
3304
3305 void * __weak
3306 arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
3307 {
3308         return NULL;
3309 }
3310
3311 static void add_trampoline_func(struct seq_file *m, struct ftrace_ops *ops,
3312                                 struct dyn_ftrace *rec)
3313 {
3314         void *ptr;
3315
3316         ptr = arch_ftrace_trampoline_func(ops, rec);
3317         if (ptr)
3318                 seq_printf(m, " ->%pS", ptr);
3319 }
3320
3321 static int t_show(struct seq_file *m, void *v)
3322 {
3323         struct ftrace_iterator *iter = m->private;
3324         struct dyn_ftrace *rec;
3325
3326         if (iter->flags & FTRACE_ITER_HASH)
3327                 return t_hash_show(m, iter);
3328
3329         if (iter->flags & FTRACE_ITER_PRINTALL) {
3330                 if (iter->flags & FTRACE_ITER_NOTRACE)
3331                         seq_puts(m, "#### no functions disabled ####\n");
3332                 else
3333                         seq_puts(m, "#### all functions enabled ####\n");
3334                 return 0;
3335         }
3336
3337         rec = iter->func;
3338
3339         if (!rec)
3340                 return 0;
3341
3342         seq_printf(m, "%ps", (void *)rec->ip);
3343         if (iter->flags & FTRACE_ITER_ENABLED) {
3344                 struct ftrace_ops *ops;
3345
3346                 seq_printf(m, " (%ld)%s%s",
3347                            ftrace_rec_count(rec),
3348                            rec->flags & FTRACE_FL_REGS ? " R" : "  ",
3349                            rec->flags & FTRACE_FL_IPMODIFY ? " I" : "  ");
3350                 if (rec->flags & FTRACE_FL_TRAMP_EN) {
3351                         ops = ftrace_find_tramp_ops_any(rec);
3352                         if (ops) {
3353                                 do {
3354                                         seq_printf(m, "\ttramp: %pS (%pS)",
3355                                                    (void *)ops->trampoline,
3356                                                    (void *)ops->func);
3357                                         add_trampoline_func(m, ops, rec);
3358                                         ops = ftrace_find_tramp_ops_next(rec, ops);
3359                                 } while (ops);
3360                         } else
3361                                 seq_puts(m, "\ttramp: ERROR!");
3362                 } else {
3363                         add_trampoline_func(m, NULL, rec);
3364                 }
3365         }       
3366
3367         seq_putc(m, '\n');
3368
3369         return 0;
3370 }
3371
3372 static const struct seq_operations show_ftrace_seq_ops = {
3373         .start = t_start,
3374         .next = t_next,
3375         .stop = t_stop,
3376         .show = t_show,
3377 };
3378
3379 static int
3380 ftrace_avail_open(struct inode *inode, struct file *file)
3381 {
3382         struct ftrace_iterator *iter;
3383
3384         if (unlikely(ftrace_disabled))
3385                 return -ENODEV;
3386
3387         iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3388         if (iter) {
3389                 iter->pg = ftrace_pages_start;
3390                 iter->ops = &global_ops;
3391         }
3392
3393         return iter ? 0 : -ENOMEM;
3394 }
3395
3396 static int
3397 ftrace_enabled_open(struct inode *inode, struct file *file)
3398 {
3399         struct ftrace_iterator *iter;
3400
3401         iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
3402         if (iter) {
3403                 iter->pg = ftrace_pages_start;
3404                 iter->flags = FTRACE_ITER_ENABLED;
3405                 iter->ops = &global_ops;
3406         }
3407
3408         return iter ? 0 : -ENOMEM;
3409 }
3410
3411 /**
3412  * ftrace_regex_open - initialize function tracer filter files
3413  * @ops: The ftrace_ops that hold the hash filters
3414  * @flag: The type of filter to process
3415  * @inode: The inode, usually passed in to your open routine
3416  * @file: The file, usually passed in to your open routine
3417  *
3418  * ftrace_regex_open() initializes the filter files for the
3419  * @ops. Depending on @flag it may process the filter hash or
3420  * the notrace hash of @ops. With this called from the open
3421  * routine, you can use ftrace_filter_write() for the write
3422  * routine if @flag has FTRACE_ITER_FILTER set, or
3423  * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
3424  * tracing_lseek() should be used as the lseek routine, and
3425  * release must call ftrace_regex_release().
3426  */
3427 int
3428 ftrace_regex_open(struct ftrace_ops *ops, int flag,
3429                   struct inode *inode, struct file *file)
3430 {
3431         struct ftrace_iterator *iter;
3432         struct ftrace_hash *hash;
3433         int ret = 0;
3434
3435         ftrace_ops_init(ops);
3436
3437         if (unlikely(ftrace_disabled))
3438                 return -ENODEV;
3439
3440         iter = kzalloc(sizeof(*iter), GFP_KERNEL);
3441         if (!iter)
3442                 return -ENOMEM;
3443
3444         if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
3445                 kfree(iter);
3446                 return -ENOMEM;
3447         }
3448
3449         iter->ops = ops;
3450         iter->flags = flag;
3451
3452         mutex_lock(&ops->func_hash->regex_lock);
3453
3454         if (flag & FTRACE_ITER_NOTRACE)
3455                 hash = ops->func_hash->notrace_hash;
3456         else
3457                 hash = ops->func_hash->filter_hash;
3458
3459         if (file->f_mode & FMODE_WRITE) {
3460                 const int size_bits = FTRACE_HASH_DEFAULT_BITS;
3461
3462                 if (file->f_flags & O_TRUNC)
3463                         iter->hash = alloc_ftrace_hash(size_bits);
3464                 else
3465                         iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
3466
3467                 if (!iter->hash) {
3468                         trace_parser_put(&iter->parser);
3469                         kfree(iter);
3470                         ret = -ENOMEM;
3471                         goto out_unlock;
3472                 }
3473         }
3474
3475         if (file->f_mode & FMODE_READ) {
3476                 iter->pg = ftrace_pages_start;
3477
3478                 ret = seq_open(file, &show_ftrace_seq_ops);
3479                 if (!ret) {
3480                         struct seq_file *m = file->private_data;
3481                         m->private = iter;
3482                 } else {
3483                         /* Failed */
3484                         free_ftrace_hash(iter->hash);
3485                         trace_parser_put(&iter->parser);
3486                         kfree(iter);
3487                 }
3488         } else
3489                 file->private_data = iter;
3490
3491  out_unlock:
3492         mutex_unlock(&ops->func_hash->regex_lock);
3493
3494         return ret;
3495 }
3496
3497 static int
3498 ftrace_filter_open(struct inode *inode, struct file *file)
3499 {
3500         struct ftrace_ops *ops = inode->i_private;
3501
3502         return ftrace_regex_open(ops,
3503                         FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
3504                         inode, file);
3505 }
3506
3507 static int
3508 ftrace_notrace_open(struct inode *inode, struct file *file)
3509 {
3510         struct ftrace_ops *ops = inode->i_private;
3511
3512         return ftrace_regex_open(ops, FTRACE_ITER_NOTRACE,
3513                                  inode, file);
3514 }
3515
3516 /* Type for quick search ftrace basic regexes (globs) from filter_parse_regex */
3517 struct ftrace_glob {
3518         char *search;
3519         unsigned len;
3520         int type;
3521 };
3522
3523 /*
3524  * If symbols in an architecture don't correspond exactly to the user-visible
3525  * name of what they represent, it is possible to define this function to
3526  * perform the necessary adjustments.
3527 */
3528 char * __weak arch_ftrace_match_adjust(char *str, const char *search)
3529 {
3530         return str;
3531 }
3532
3533 static int ftrace_match(char *str, struct ftrace_glob *g)
3534 {
3535         int matched = 0;
3536         int slen;
3537
3538         str = arch_ftrace_match_adjust(str, g->search);
3539
3540         switch (g->type) {
3541         case MATCH_FULL:
3542                 if (strcmp(str, g->search) == 0)
3543                         matched = 1;
3544                 break;
3545         case MATCH_FRONT_ONLY:
3546                 if (strncmp(str, g->search, g->len) == 0)
3547                         matched = 1;
3548                 break;
3549         case MATCH_MIDDLE_ONLY:
3550                 if (strstr(str, g->search))
3551                         matched = 1;
3552                 break;
3553         case MATCH_END_ONLY:
3554                 slen = strlen(str);
3555                 if (slen >= g->len &&
3556                     memcmp(str + slen - g->len, g->search, g->len) == 0)
3557                         matched = 1;
3558                 break;
3559         }
3560
3561         return matched;
3562 }
3563
3564 static int
3565 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int clear_filter)
3566 {
3567         struct ftrace_func_entry *entry;
3568         int ret = 0;
3569
3570         entry = ftrace_lookup_ip(hash, rec->ip);
3571         if (clear_filter) {
3572                 /* Do nothing if it doesn't exist */
3573                 if (!entry)
3574                         return 0;
3575
3576                 free_hash_entry(hash, entry);
3577         } else {
3578                 /* Do nothing if it exists */
3579                 if (entry)
3580                         return 0;
3581
3582                 ret = add_hash_entry(hash, rec->ip);
3583         }
3584         return ret;
3585 }
3586
3587 static int
3588 ftrace_match_record(struct dyn_ftrace *rec, struct ftrace_glob *func_g,
3589                 struct ftrace_glob *mod_g, int exclude_mod)
3590 {
3591         char str[KSYM_SYMBOL_LEN];
3592         char *modname;
3593
3594         kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
3595
3596         if (mod_g) {
3597                 int mod_matches = (modname) ? ftrace_match(modname, mod_g) : 0;
3598
3599                 /* blank module name to match all modules */
3600                 if (!mod_g->len) {
3601                         /* blank module globbing: modname xor exclude_mod */
3602                         if ((!exclude_mod) != (!modname))
3603                                 goto func_match;
3604                         return 0;
3605                 }
3606
3607                 /* not matching the module */
3608                 if (!modname || !mod_matches) {
3609                         if (exclude_mod)
3610                                 goto func_match;
3611                         else
3612                                 return 0;
3613                 }
3614
3615                 if (mod_matches && exclude_mod)
3616                         return 0;
3617
3618 func_match:
3619                 /* blank search means to match all funcs in the mod */
3620                 if (!func_g->len)
3621                         return 1;
3622         }
3623
3624         return ftrace_match(str, func_g);
3625 }
3626
3627 static int
3628 match_records(struct ftrace_hash *hash, char *func, int len, char *mod)
3629 {
3630         struct ftrace_page *pg;
3631         struct dyn_ftrace *rec;
3632         struct ftrace_glob func_g = { .type = MATCH_FULL };
3633         struct ftrace_glob mod_g = { .type = MATCH_FULL };
3634         struct ftrace_glob *mod_match = (mod) ? &mod_g : NULL;
3635         int exclude_mod = 0;
3636         int found = 0;
3637         int ret;
3638         int clear_filter = 0;
3639
3640         if (func) {
3641                 func_g.type = filter_parse_regex(func, len, &func_g.search,
3642                                                  &clear_filter);
3643                 func_g.len = strlen(func_g.search);
3644         }
3645
3646         if (mod) {
3647                 mod_g.type = filter_parse_regex(mod, strlen(mod),
3648                                 &mod_g.search, &exclude_mod);
3649                 mod_g.len = strlen(mod_g.search);
3650         }
3651
3652         mutex_lock(&ftrace_lock);
3653
3654         if (unlikely(ftrace_disabled))
3655                 goto out_unlock;
3656
3657         do_for_each_ftrace_rec(pg, rec) {
3658
3659                 if (rec->flags & FTRACE_FL_DISABLED)
3660                         continue;
3661
3662                 if (ftrace_match_record(rec, &func_g, mod_match, exclude_mod)) {
3663                         ret = enter_record(hash, rec, clear_filter);
3664                         if (ret < 0) {
3665                                 found = ret;
3666                                 goto out_unlock;
3667                         }
3668                         found = 1;
3669                 }
3670         } while_for_each_ftrace_rec();
3671  out_unlock:
3672         mutex_unlock(&ftrace_lock);
3673
3674         return found;
3675 }
3676
3677 static int
3678 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
3679 {
3680         return match_records(hash, buff, len, NULL);
3681 }
3682
3683
3684 /*
3685  * We register the module command as a template to show others how
3686  * to register the a command as well.
3687  */
3688
3689 static int
3690 ftrace_mod_callback(struct ftrace_hash *hash,
3691                     char *func, char *cmd, char *module, int enable)
3692 {
3693         int ret;
3694
3695         /*
3696          * cmd == 'mod' because we only registered this func
3697          * for the 'mod' ftrace_func_command.
3698          * But if you register one func with multiple commands,
3699          * you can tell which command was used by the cmd
3700          * parameter.
3701          */
3702         ret = match_records(hash, func, strlen(func), module);
3703         if (!ret)
3704                 return -EINVAL;
3705         if (ret < 0)
3706                 return ret;
3707         return 0;
3708 }
3709
3710 static struct ftrace_func_command ftrace_mod_cmd = {
3711         .name                   = "mod",
3712         .func                   = ftrace_mod_callback,
3713 };
3714
3715 static int __init ftrace_mod_cmd_init(void)
3716 {
3717         return register_ftrace_command(&ftrace_mod_cmd);
3718 }
3719 core_initcall(ftrace_mod_cmd_init);
3720
3721 static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
3722                                       struct ftrace_ops *op, struct pt_regs *pt_regs)
3723 {
3724         struct ftrace_func_probe *entry;
3725         struct hlist_head *hhd;
3726         unsigned long key;
3727
3728         key = hash_long(ip, FTRACE_HASH_BITS);
3729
3730         hhd = &ftrace_func_hash[key];
3731
3732         if (hlist_empty(hhd))
3733                 return;
3734
3735         /*
3736          * Disable preemption for these calls to prevent a RCU grace
3737          * period. This syncs the hash iteration and freeing of items
3738          * on the hash. rcu_read_lock is too dangerous here.
3739          */
3740         preempt_disable_notrace();
3741         hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
3742                 if (entry->ip == ip)
3743                         entry->ops->func(ip, parent_ip, &entry->data);
3744         }
3745         preempt_enable_notrace();
3746 }
3747
3748 static struct ftrace_ops trace_probe_ops __read_mostly =
3749 {
3750         .func           = function_trace_probe_call,
3751         .flags          = FTRACE_OPS_FL_INITIALIZED,
3752         INIT_OPS_HASH(trace_probe_ops)
3753 };
3754
3755 static int ftrace_probe_registered;
3756
3757 static void __enable_ftrace_function_probe(struct ftrace_ops_hash *old_hash)
3758 {
3759         int ret;
3760         int i;
3761
3762         if (ftrace_probe_registered) {
3763                 /* still need to update the function call sites */
3764                 if (ftrace_enabled)
3765                         ftrace_run_modify_code(&trace_probe_ops, FTRACE_UPDATE_CALLS,
3766                                                old_hash);
3767                 return;
3768         }
3769
3770         for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3771                 struct hlist_head *hhd = &ftrace_func_hash[i];
3772                 if (hhd->first)
3773                         break;
3774         }
3775         /* Nothing registered? */
3776         if (i == FTRACE_FUNC_HASHSIZE)
3777                 return;
3778
3779         ret = ftrace_startup(&trace_probe_ops, 0);
3780
3781         ftrace_probe_registered = 1;
3782 }
3783
3784 static bool __disable_ftrace_function_probe(void)
3785 {
3786         int i;
3787
3788         if (!ftrace_probe_registered)
3789                 return false;
3790
3791         for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3792                 struct hlist_head *hhd = &ftrace_func_hash[i];
3793                 if (hhd->first)
3794                         return false;
3795         }
3796
3797         /* no more funcs left */
3798         ftrace_shutdown(&trace_probe_ops, 0);
3799
3800         ftrace_probe_registered = 0;
3801         return true;
3802 }
3803
3804
3805 static void ftrace_free_entry(struct ftrace_func_probe *entry)
3806 {
3807         if (entry->ops->free)
3808                 entry->ops->free(entry->ops, entry->ip, &entry->data);
3809         kfree(entry);
3810 }
3811
3812 int
3813 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3814                               void *data)
3815 {
3816         struct ftrace_ops_hash old_hash_ops;
3817         struct ftrace_func_probe *entry;
3818         struct ftrace_glob func_g;
3819         struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
3820         struct ftrace_hash *old_hash = *orig_hash;
3821         struct ftrace_hash *hash;
3822         struct ftrace_page *pg;
3823         struct dyn_ftrace *rec;
3824         int not;
3825         unsigned long key;
3826         int count = 0;
3827         int ret;
3828
3829         func_g.type = filter_parse_regex(glob, strlen(glob),
3830                         &func_g.search, &not);
3831         func_g.len = strlen(func_g.search);
3832
3833         /* we do not support '!' for function probes */
3834         if (WARN_ON(not))
3835                 return -EINVAL;
3836
3837         mutex_lock(&trace_probe_ops.func_hash->regex_lock);
3838
3839         old_hash_ops.filter_hash = old_hash;
3840         /* Probes only have filters */
3841         old_hash_ops.notrace_hash = NULL;
3842
3843         hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, old_hash);
3844         if (!hash) {
3845                 count = -ENOMEM;
3846                 goto out;
3847         }
3848
3849         if (unlikely(ftrace_disabled)) {
3850                 count = -ENODEV;
3851                 goto out;
3852         }
3853
3854         mutex_lock(&ftrace_lock);
3855
3856         do_for_each_ftrace_rec(pg, rec) {
3857
3858                 if (rec->flags & FTRACE_FL_DISABLED)
3859                         continue;
3860
3861                 if (!ftrace_match_record(rec, &func_g, NULL, 0))
3862                         continue;
3863
3864                 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3865                 if (!entry) {
3866                         /* If we did not process any, then return error */
3867                         if (!count)
3868                                 count = -ENOMEM;
3869                         goto out_unlock;
3870                 }
3871
3872                 count++;
3873
3874                 entry->data = data;
3875
3876                 /*
3877                  * The caller might want to do something special
3878                  * for each function we find. We call the callback
3879                  * to give the caller an opportunity to do so.
3880                  */
3881                 if (ops->init) {
3882                         if (ops->init(ops, rec->ip, &entry->data) < 0) {
3883                                 /* caller does not like this func */
3884                                 kfree(entry);
3885                                 continue;
3886                         }
3887                 }
3888
3889                 ret = enter_record(hash, rec, 0);
3890                 if (ret < 0) {
3891                         kfree(entry);
3892                         count = ret;
3893                         goto out_unlock;
3894                 }
3895
3896                 entry->ops = ops;
3897                 entry->ip = rec->ip;
3898
3899                 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3900                 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3901
3902         } while_for_each_ftrace_rec();
3903
3904         ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3905
3906         __enable_ftrace_function_probe(&old_hash_ops);
3907
3908         if (!ret)
3909                 free_ftrace_hash_rcu(old_hash);
3910         else
3911                 count = ret;
3912
3913  out_unlock:
3914         mutex_unlock(&ftrace_lock);
3915  out:
3916         mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
3917         free_ftrace_hash(hash);
3918
3919         return count;
3920 }
3921
3922 enum {
3923         PROBE_TEST_FUNC         = 1,
3924         PROBE_TEST_DATA         = 2
3925 };
3926
3927 static void
3928 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3929                                   void *data, int flags)
3930 {
3931         struct ftrace_ops_hash old_hash_ops;
3932         struct ftrace_func_entry *rec_entry;
3933         struct ftrace_func_probe *entry;
3934         struct ftrace_func_probe *p;
3935         struct ftrace_glob func_g;
3936         struct ftrace_hash **orig_hash = &trace_probe_ops.func_hash->filter_hash;
3937         struct ftrace_hash *old_hash = *orig_hash;
3938         struct list_head free_list;
3939         struct ftrace_hash *hash;
3940         struct hlist_node *tmp;
3941         char str[KSYM_SYMBOL_LEN];
3942         int i, ret;
3943         bool disabled;
3944
3945         if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
3946                 func_g.search = NULL;
3947         else if (glob) {
3948                 int not;
3949
3950                 func_g.type = filter_parse_regex(glob, strlen(glob),
3951                                                  &func_g.search, &not);
3952                 func_g.len = strlen(func_g.search);
3953
3954                 /* we do not support '!' for function probes */
3955                 if (WARN_ON(not))
3956                         return;
3957         }
3958
3959         mutex_lock(&trace_probe_ops.func_hash->regex_lock);
3960
3961         old_hash_ops.filter_hash = old_hash;
3962         /* Probes only have filters */
3963         old_hash_ops.notrace_hash = NULL;
3964
3965         hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3966         if (!hash)
3967                 /* Hmm, should report this somehow */
3968                 goto out_unlock;
3969
3970         INIT_LIST_HEAD(&free_list);
3971
3972         for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3973                 struct hlist_head *hhd = &ftrace_func_hash[i];
3974
3975                 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
3976
3977                         /* break up if statements for readability */
3978                         if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
3979                                 continue;
3980
3981                         if ((flags & PROBE_TEST_DATA) && entry->data != data)
3982                                 continue;
3983
3984                         /* do this last, since it is the most expensive */
3985                         if (func_g.search) {
3986                                 kallsyms_lookup(entry->ip, NULL, NULL,
3987                                                 NULL, str);
3988                                 if (!ftrace_match(str, &func_g))
3989                                         continue;
3990                         }
3991
3992                         rec_entry = ftrace_lookup_ip(hash, entry->ip);
3993                         /* It is possible more than one entry had this ip */
3994                         if (rec_entry)
3995                                 free_hash_entry(hash, rec_entry);
3996
3997                         hlist_del_rcu(&entry->node);
3998                         list_add(&entry->free_list, &free_list);
3999                 }
4000         }
4001         mutex_lock(&ftrace_lock);
4002         disabled = __disable_ftrace_function_probe();
4003         /*
4004          * Remove after the disable is called. Otherwise, if the last
4005          * probe is removed, a null hash means *all enabled*.
4006          */
4007         ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
4008
4009         /* still need to update the function call sites */
4010         if (ftrace_enabled && !disabled)
4011                 ftrace_run_modify_code(&trace_probe_ops, FTRACE_UPDATE_CALLS,
4012                                        &old_hash_ops);
4013         synchronize_sched();
4014         if (!ret)
4015                 free_ftrace_hash_rcu(old_hash);
4016
4017         list_for_each_entry_safe(entry, p, &free_list, free_list) {
4018                 list_del(&entry->free_list);
4019                 ftrace_free_entry(entry);
4020         }
4021         mutex_unlock(&ftrace_lock);
4022
4023  out_unlock:
4024         mutex_unlock(&trace_probe_ops.func_hash->regex_lock);
4025         free_ftrace_hash(hash);
4026 }
4027
4028 void
4029 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
4030                                 void *data)
4031 {
4032         __unregister_ftrace_function_probe(glob, ops, data,
4033                                           PROBE_TEST_FUNC | PROBE_TEST_DATA);
4034 }
4035
4036 void
4037 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
4038 {
4039         __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
4040 }
4041
4042 void unregister_ftrace_function_probe_all(char *glob)
4043 {
4044         __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
4045 }
4046
4047 static LIST_HEAD(ftrace_commands);
4048 static DEFINE_MUTEX(ftrace_cmd_mutex);
4049
4050 /*
4051  * Currently we only register ftrace commands from __init, so mark this
4052  * __init too.
4053  */
4054 __init int register_ftrace_command(struct ftrace_func_command *cmd)
4055 {
4056         struct ftrace_func_command *p;
4057         int ret = 0;
4058
4059         mutex_lock(&ftrace_cmd_mutex);
4060         list_for_each_entry(p, &ftrace_commands, list) {
4061                 if (strcmp(cmd->name, p->name) == 0) {
4062                         ret = -EBUSY;
4063                         goto out_unlock;
4064                 }
4065         }
4066         list_add(&cmd->list, &ftrace_commands);
4067  out_unlock:
4068         mutex_unlock(&ftrace_cmd_mutex);
4069
4070         return ret;
4071 }
4072
4073 /*
4074  * Currently we only unregister ftrace commands from __init, so mark
4075  * this __init too.
4076  */
4077 __init int unregister_ftrace_command(struct ftrace_func_command *cmd)
4078 {
4079         struct ftrace_func_command *p, *n;
4080         int ret = -ENODEV;
4081
4082         mutex_lock(&ftrace_cmd_mutex);
4083         list_for_each_entry_safe(p, n, &ftrace_commands, list) {
4084                 if (strcmp(cmd->name, p->name) == 0) {
4085                         ret = 0;
4086                         list_del_init(&p->list);
4087                         goto out_unlock;
4088                 }
4089         }
4090  out_unlock:
4091         mutex_unlock(&ftrace_cmd_mutex);
4092
4093         return ret;
4094 }
4095
4096 static int ftrace_process_regex(struct ftrace_hash *hash,
4097                                 char *buff, int len, int enable)
4098 {
4099         char *func, *command, *next = buff;
4100         struct ftrace_func_command *p;
4101         int ret = -EINVAL;
4102
4103         func = strsep(&next, ":");
4104
4105         if (!next) {
4106                 ret = ftrace_match_records(hash, func, len);
4107                 if (!ret)
4108                         ret = -EINVAL;
4109                 if (ret < 0)
4110                         return ret;
4111                 return 0;
4112         }
4113
4114         /* command found */
4115
4116         command = strsep(&next, ":");
4117
4118         mutex_lock(&ftrace_cmd_mutex);
4119         list_for_each_entry(p, &ftrace_commands, list) {
4120                 if (strcmp(p->name, command) == 0) {
4121                         ret = p->func(hash, func, command, next, enable);
4122                         goto out_unlock;
4123                 }
4124         }
4125  out_unlock:
4126         mutex_unlock(&ftrace_cmd_mutex);
4127
4128         return ret;
4129 }
4130
4131 static ssize_t
4132 ftrace_regex_write(struct file *file, const char __user *ubuf,
4133                    size_t cnt, loff_t *ppos, int enable)
4134 {
4135         struct ftrace_iterator *iter;
4136         struct trace_parser *parser;
4137         ssize_t ret, read;
4138
4139         if (!cnt)
4140                 return 0;
4141
4142         if (file->f_mode & FMODE_READ) {
4143                 struct seq_file *m = file->private_data;
4144                 iter = m->private;
4145         } else
4146                 iter = file->private_data;
4147
4148         if (unlikely(ftrace_disabled))
4149                 return -ENODEV;
4150
4151         /* iter->hash is a local copy, so we don't need regex_lock */
4152
4153         parser = &iter->parser;
4154         read = trace_get_user(parser, ubuf, cnt, ppos);
4155
4156         if (read >= 0 && trace_parser_loaded(parser) &&
4157             !trace_parser_cont(parser)) {
4158                 ret = ftrace_process_regex(iter->hash, parser->buffer,
4159                                            parser->idx, enable);
4160                 trace_parser_clear(parser);
4161                 if (ret < 0)
4162                         goto out;
4163         }
4164
4165         ret = read;
4166  out:
4167         return ret;
4168 }
4169
4170 ssize_t
4171 ftrace_filter_write(struct file *file, const char __user *ubuf,
4172                     size_t cnt, loff_t *ppos)
4173 {
4174         return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
4175 }
4176
4177 ssize_t
4178 ftrace_notrace_write(struct file *file, const char __user *ubuf,
4179                      size_t cnt, loff_t *ppos)
4180 {
4181         return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
4182 }
4183
4184 static int
4185 ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
4186 {
4187         struct ftrace_func_entry *entry;
4188
4189         if (!ftrace_location(ip))
4190                 return -EINVAL;
4191
4192         if (remove) {
4193                 entry = ftrace_lookup_ip(hash, ip);
4194                 if (!entry)
4195                         return -ENOENT;
4196                 free_hash_entry(hash, entry);
4197                 return 0;
4198         }
4199
4200         return add_hash_entry(hash, ip);
4201 }
4202
4203 static void ftrace_ops_update_code(struct ftrace_ops *ops,
4204                                    struct ftrace_ops_hash *old_hash)
4205 {
4206         struct ftrace_ops *op;
4207
4208         if (!ftrace_enabled)
4209                 return;
4210
4211         if (ops->flags & FTRACE_OPS_FL_ENABLED) {
4212                 ftrace_run_modify_code(ops, FTRACE_UPDATE_CALLS, old_hash);
4213                 return;
4214         }
4215
4216         /*
4217          * If this is the shared global_ops filter, then we need to
4218          * check if there is another ops that shares it, is enabled.
4219          * If so, we still need to run the modify code.
4220          */
4221         if (ops->func_hash != &global_ops.local_hash)
4222                 return;
4223
4224         do_for_each_ftrace_op(op, ftrace_ops_list) {
4225                 if (op->func_hash == &global_ops.local_hash &&
4226                     op->flags & FTRACE_OPS_FL_ENABLED) {
4227                         ftrace_run_modify_code(op, FTRACE_UPDATE_CALLS, old_hash);
4228                         /* Only need to do this once */
4229                         return;
4230                 }
4231         } while_for_each_ftrace_op(op);
4232 }
4233
4234 static int
4235 ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
4236                 unsigned long ip, int remove, int reset, int enable)
4237 {
4238         struct ftrace_hash **orig_hash;
4239         struct ftrace_ops_hash old_hash_ops;
4240         struct ftrace_hash *old_hash;
4241         struct ftrace_hash *hash;
4242         int ret;
4243
4244         if (unlikely(ftrace_disabled))
4245                 return -ENODEV;
4246
4247         mutex_lock(&ops->func_hash->regex_lock);
4248
4249         if (enable)
4250                 orig_hash = &ops->func_hash->filter_hash;
4251         else
4252                 orig_hash = &ops->func_hash->notrace_hash;
4253
4254         if (reset)
4255                 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS);
4256         else
4257                 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
4258
4259         if (!hash) {
4260                 ret = -ENOMEM;
4261                 goto out_regex_unlock;
4262         }
4263
4264         if (buf && !ftrace_match_records(hash, buf, len)) {
4265                 ret = -EINVAL;
4266                 goto out_regex_unlock;
4267         }
4268         if (ip) {
4269                 ret = ftrace_match_addr(hash, ip, remove);
4270                 if (ret < 0)
4271                         goto out_regex_unlock;
4272         }
4273
4274         mutex_lock(&ftrace_lock);
4275         old_hash = *orig_hash;
4276         old_hash_ops.filter_hash = ops->func_hash->filter_hash;
4277         old_hash_ops.notrace_hash = ops->func_hash->notrace_hash;
4278         ret = ftrace_hash_move(ops, enable, orig_hash, hash);
4279         if (!ret) {
4280                 ftrace_ops_update_code(ops, &old_hash_ops);
4281                 free_ftrace_hash_rcu(old_hash);
4282         }
4283         mutex_unlock(&ftrace_lock);
4284
4285  out_regex_unlock:
4286         mutex_unlock(&ops->func_hash->regex_lock);
4287
4288         free_ftrace_hash(hash);
4289         return ret;
4290 }
4291
4292 static int
4293 ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
4294                 int reset, int enable)
4295 {
4296         return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
4297 }
4298
4299 /**
4300  * ftrace_set_filter_ip - set a function to filter on in ftrace by address
4301  * @ops - the ops to set the filter with
4302  * @ip - the address to add to or remove from the filter.
4303  * @remove - non zero to remove the ip from the filter
4304  * @reset - non zero to reset all filters before applying this filter.
4305  *
4306  * Filters denote which functions should be enabled when tracing is enabled
4307  * If @ip is NULL, it failes to update filter.
4308  */
4309 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
4310                          int remove, int reset)
4311 {
4312         ftrace_ops_init(ops);
4313         return ftrace_set_addr(ops, ip, remove, reset, 1);
4314 }
4315 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
4316
4317 static int
4318 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
4319                  int reset, int enable)
4320 {
4321         return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
4322 }
4323
4324 /**
4325  * ftrace_set_filter - set a function to filter on in ftrace
4326  * @ops - the ops to set the filter with
4327  * @buf - the string that holds the function filter text.
4328  * @len - the length of the string.
4329  * @reset - non zero to reset all filters before applying this filter.
4330  *
4331  * Filters denote which functions should be enabled when tracing is enabled.
4332  * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4333  */
4334 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
4335                        int len, int reset)
4336 {
4337         ftrace_ops_init(ops);
4338         return ftrace_set_regex(ops, buf, len, reset, 1);
4339 }
4340 EXPORT_SYMBOL_GPL(ftrace_set_filter);
4341
4342 /**
4343  * ftrace_set_notrace - set a function to not trace in ftrace
4344  * @ops - the ops to set the notrace filter with
4345  * @buf - the string that holds the function notrace text.
4346  * @len - the length of the string.
4347  * @reset - non zero to reset all filters before applying this filter.
4348  *
4349  * Notrace Filters denote which functions should not be enabled when tracing
4350  * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4351  * for tracing.
4352  */
4353 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
4354                         int len, int reset)
4355 {
4356         ftrace_ops_init(ops);
4357         return ftrace_set_regex(ops, buf, len, reset, 0);
4358 }
4359 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
4360 /**
4361  * ftrace_set_global_filter - set a function to filter on with global tracers
4362  * @buf - the string that holds the function filter text.
4363  * @len - the length of the string.
4364  * @reset - non zero to reset all filters before applying this filter.
4365  *
4366  * Filters denote which functions should be enabled when tracing is enabled.
4367  * If @buf is NULL and reset is set, all functions will be enabled for tracing.
4368  */
4369 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
4370 {
4371         ftrace_set_regex(&global_ops, buf, len, reset, 1);
4372 }
4373 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
4374
4375 /**
4376  * ftrace_set_global_notrace - set a function to not trace with global tracers
4377  * @buf - the string that holds the function notrace text.
4378  * @len - the length of the string.
4379  * @reset - non zero to reset all filters before applying this filter.
4380  *
4381  * Notrace Filters denote which functions should not be enabled when tracing
4382  * is enabled. If @buf is NULL and reset is set, all functions will be enabled
4383  * for tracing.
4384  */
4385 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
4386 {
4387         ftrace_set_regex(&global_ops, buf, len, reset, 0);
4388 }
4389 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
4390
4391 /*
4392  * command line interface to allow users to set filters on boot up.
4393  */
4394 #define FTRACE_FILTER_SIZE              COMMAND_LINE_SIZE
4395 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4396 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
4397
4398 /* Used by function selftest to not test if filter is set */
4399 bool ftrace_filter_param __initdata;
4400
4401 static int __init set_ftrace_notrace(char *str)
4402 {
4403         ftrace_filter_param = true;
4404         strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
4405         return 1;
4406 }
4407 __setup("ftrace_notrace=", set_ftrace_notrace);
4408
4409 static int __init set_ftrace_filter(char *str)
4410 {
4411         ftrace_filter_param = true;
4412         strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
4413         return 1;
4414 }
4415 __setup("ftrace_filter=", set_ftrace_filter);
4416
4417 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4418 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
4419 static char ftrace_graph_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
4420 static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
4421
4422 static int __init set_graph_function(char *str)
4423 {
4424         strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
4425         return 1;
4426 }
4427 __setup("ftrace_graph_filter=", set_graph_function);
4428
4429 static int __init set_graph_notrace_function(char *str)
4430 {
4431         strlcpy(ftrace_graph_notrace_buf, str, FTRACE_FILTER_SIZE);
4432         return 1;
4433 }
4434 __setup("ftrace_graph_notrace=", set_graph_notrace_function);
4435
4436 static void __init set_ftrace_early_graph(char *buf, int enable)
4437 {
4438         int ret;
4439         char *func;
4440         unsigned long *table = ftrace_graph_funcs;
4441         int *count = &ftrace_graph_count;
4442
4443         if (!enable) {
4444                 table = ftrace_graph_notrace_funcs;
4445                 count = &ftrace_graph_notrace_count;
4446         }
4447
4448         while (buf) {
4449                 func = strsep(&buf, ",");
4450                 /* we allow only one expression at a time */
4451                 ret = ftrace_set_func(table, count, FTRACE_GRAPH_MAX_FUNCS, func);
4452                 if (ret)
4453                         printk(KERN_DEBUG "ftrace: function %s not "
4454                                           "traceable\n", func);
4455         }
4456 }
4457 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4458
4459 void __init
4460 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
4461 {
4462         char *func;
4463
4464         ftrace_ops_init(ops);
4465
4466         while (buf) {
4467                 func = strsep(&buf, ",");
4468                 ftrace_set_regex(ops, func, strlen(func), 0, enable);
4469         }
4470 }
4471
4472 static void __init set_ftrace_early_filters(void)
4473 {
4474         if (ftrace_filter_buf[0])
4475                 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
4476         if (ftrace_notrace_buf[0])
4477                 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
4478 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4479         if (ftrace_graph_buf[0])
4480                 set_ftrace_early_graph(ftrace_graph_buf, 1);
4481         if (ftrace_graph_notrace_buf[0])
4482                 set_ftrace_early_graph(ftrace_graph_notrace_buf, 0);
4483 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4484 }
4485
4486 int ftrace_regex_release(struct inode *inode, struct file *file)
4487 {
4488         struct seq_file *m = (struct seq_file *)file->private_data;
4489         struct ftrace_ops_hash old_hash_ops;
4490         struct ftrace_iterator *iter;
4491         struct ftrace_hash **orig_hash;
4492         struct ftrace_hash *old_hash;
4493         struct trace_parser *parser;
4494         int filter_hash;
4495         int ret;
4496
4497         if (file->f_mode & FMODE_READ) {
4498                 iter = m->private;
4499                 seq_release(inode, file);
4500         } else
4501                 iter = file->private_data;
4502
4503         parser = &iter->parser;
4504         if (trace_parser_loaded(parser)) {
4505                 int enable = !(iter->flags & FTRACE_ITER_NOTRACE);
4506
4507                 parser->buffer[parser->idx] = 0;
4508                 ftrace_process_regex(iter->hash, parser->buffer,
4509                                      parser->idx, enable);
4510         }
4511
4512         trace_parser_put(parser);
4513
4514         mutex_lock(&iter->ops->func_hash->regex_lock);
4515
4516         if (file->f_mode & FMODE_WRITE) {
4517                 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
4518
4519                 if (filter_hash)
4520                         orig_hash = &iter->ops->func_hash->filter_hash;
4521                 else
4522                         orig_hash = &iter->ops->func_hash->notrace_hash;
4523
4524                 mutex_lock(&ftrace_lock);
4525                 old_hash = *orig_hash;
4526                 old_hash_ops.filter_hash = iter->ops->func_hash->filter_hash;
4527                 old_hash_ops.notrace_hash = iter->ops->func_hash->notrace_hash;
4528                 ret = ftrace_hash_move(iter->ops, filter_hash,
4529                                        orig_hash, iter->hash);
4530                 if (!ret) {
4531                         ftrace_ops_update_code(iter->ops, &old_hash_ops);
4532                         free_ftrace_hash_rcu(old_hash);
4533                 }
4534                 mutex_unlock(&ftrace_lock);
4535         }
4536
4537         mutex_unlock(&iter->ops->func_hash->regex_lock);
4538         free_ftrace_hash(iter->hash);
4539         kfree(iter);
4540
4541         return 0;
4542 }
4543
4544 static const struct file_operations ftrace_avail_fops = {
4545         .open = ftrace_avail_open,
4546         .read = seq_read,
4547         .llseek = seq_lseek,
4548         .release = seq_release_private,
4549 };
4550
4551 static const struct file_operations ftrace_enabled_fops = {
4552         .open = ftrace_enabled_open,
4553         .read = seq_read,
4554         .llseek = seq_lseek,
4555         .release = seq_release_private,
4556 };
4557
4558 static const struct file_operations ftrace_filter_fops = {
4559         .open = ftrace_filter_open,
4560         .read = seq_read,
4561         .write = ftrace_filter_write,
4562         .llseek = tracing_lseek,
4563         .release = ftrace_regex_release,
4564 };
4565
4566 static const struct file_operations ftrace_notrace_fops = {
4567         .open = ftrace_notrace_open,
4568         .read = seq_read,
4569         .write = ftrace_notrace_write,
4570         .llseek = tracing_lseek,
4571         .release = ftrace_regex_release,
4572 };
4573
4574 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4575
4576 static DEFINE_MUTEX(graph_lock);
4577
4578 int ftrace_graph_count;
4579 int ftrace_graph_notrace_count;
4580 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
4581 unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
4582
4583 struct ftrace_graph_data {
4584         unsigned long *table;
4585         size_t size;
4586         int *count;
4587         const struct seq_operations *seq_ops;
4588 };
4589
4590 static void *
4591 __g_next(struct seq_file *m, loff_t *pos)
4592 {
4593         struct ftrace_graph_data *fgd = m->private;
4594
4595         if (*pos >= *fgd->count)
4596                 return NULL;
4597         return &fgd->table[*pos];
4598 }
4599
4600 static void *
4601 g_next(struct seq_file *m, void *v, loff_t *pos)
4602 {
4603         (*pos)++;
4604         return __g_next(m, pos);
4605 }
4606
4607 static void *g_start(struct seq_file *m, loff_t *pos)
4608 {
4609         struct ftrace_graph_data *fgd = m->private;
4610
4611         mutex_lock(&graph_lock);
4612
4613         /* Nothing, tell g_show to print all functions are enabled */
4614         if (!*fgd->count && !*pos)
4615                 return (void *)1;
4616
4617         return __g_next(m, pos);
4618 }
4619
4620 static void g_stop(struct seq_file *m, void *p)
4621 {
4622         mutex_unlock(&graph_lock);
4623 }
4624
4625 static int g_show(struct seq_file *m, void *v)
4626 {
4627         unsigned long *ptr = v;
4628
4629         if (!ptr)
4630                 return 0;
4631
4632         if (ptr == (unsigned long *)1) {
4633                 struct ftrace_graph_data *fgd = m->private;
4634
4635                 if (fgd->table == ftrace_graph_funcs)
4636                         seq_puts(m, "#### all functions enabled ####\n");
4637                 else
4638                         seq_puts(m, "#### no functions disabled ####\n");
4639                 return 0;
4640         }
4641
4642         seq_printf(m, "%ps\n", (void *)*ptr);
4643
4644         return 0;
4645 }
4646
4647 static const struct seq_operations ftrace_graph_seq_ops = {
4648         .start = g_start,
4649         .next = g_next,
4650         .stop = g_stop,
4651         .show = g_show,
4652 };
4653
4654 static int
4655 __ftrace_graph_open(struct inode *inode, struct file *file,
4656                     struct ftrace_graph_data *fgd)
4657 {
4658         int ret = 0;
4659
4660         mutex_lock(&graph_lock);
4661         if ((file->f_mode & FMODE_WRITE) &&
4662             (file->f_flags & O_TRUNC)) {
4663                 *fgd->count = 0;
4664                 memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
4665         }
4666         mutex_unlock(&graph_lock);
4667
4668         if (file->f_mode & FMODE_READ) {
4669                 ret = seq_open(file, fgd->seq_ops);
4670                 if (!ret) {
4671                         struct seq_file *m = file->private_data;
4672                         m->private = fgd;
4673                 }
4674         } else
4675                 file->private_data = fgd;
4676
4677         return ret;
4678 }
4679
4680 static int
4681 ftrace_graph_open(struct inode *inode, struct file *file)
4682 {
4683         struct ftrace_graph_data *fgd;
4684
4685         if (unlikely(ftrace_disabled))
4686                 return -ENODEV;
4687
4688         fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4689         if (fgd == NULL)
4690                 return -ENOMEM;
4691
4692         fgd->table = ftrace_graph_funcs;
4693         fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4694         fgd->count = &ftrace_graph_count;
4695         fgd->seq_ops = &ftrace_graph_seq_ops;
4696
4697         return __ftrace_graph_open(inode, file, fgd);
4698 }
4699
4700 static int
4701 ftrace_graph_notrace_open(struct inode *inode, struct file *file)
4702 {
4703         struct ftrace_graph_data *fgd;
4704
4705         if (unlikely(ftrace_disabled))
4706                 return -ENODEV;
4707
4708         fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
4709         if (fgd == NULL)
4710                 return -ENOMEM;
4711
4712         fgd->table = ftrace_graph_notrace_funcs;
4713         fgd->size = FTRACE_GRAPH_MAX_FUNCS;
4714         fgd->count = &ftrace_graph_notrace_count;
4715         fgd->seq_ops = &ftrace_graph_seq_ops;
4716
4717         return __ftrace_graph_open(inode, file, fgd);
4718 }
4719
4720 static int
4721 ftrace_graph_release(struct inode *inode, struct file *file)
4722 {
4723         if (file->f_mode & FMODE_READ) {
4724                 struct seq_file *m = file->private_data;
4725
4726                 kfree(m->private);
4727                 seq_release(inode, file);
4728         } else {
4729                 kfree(file->private_data);
4730         }
4731
4732         return 0;
4733 }
4734
4735 static int
4736 ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
4737 {
4738         struct ftrace_glob func_g;
4739         struct dyn_ftrace *rec;
4740         struct ftrace_page *pg;
4741         int fail = 1;
4742         int not;
4743         bool exists;
4744         int i;
4745
4746         /* decode regex */
4747         func_g.type = filter_parse_regex(buffer, strlen(buffer),
4748                                          &func_g.search, &not);
4749         if (!not && *idx >= size)
4750                 return -EBUSY;
4751
4752         func_g.len = strlen(func_g.search);
4753
4754         mutex_lock(&ftrace_lock);
4755
4756         if (unlikely(ftrace_disabled)) {
4757                 mutex_unlock(&ftrace_lock);
4758                 return -ENODEV;
4759         }
4760
4761         do_for_each_ftrace_rec(pg, rec) {
4762
4763                 if (rec->flags & FTRACE_FL_DISABLED)
4764                         continue;
4765
4766                 if (ftrace_match_record(rec, &func_g, NULL, 0)) {
4767                         /* if it is in the array */
4768                         exists = false;
4769                         for (i = 0; i < *idx; i++) {
4770                                 if (array[i] == rec->ip) {
4771                                         exists = true;
4772                                         break;
4773                                 }
4774                         }
4775
4776                         if (!not) {
4777                                 fail = 0;
4778                                 if (!exists) {
4779                                         array[(*idx)++] = rec->ip;
4780                                         if (*idx >= size)
4781                                                 goto out;
4782                                 }
4783                         } else {
4784                                 if (exists) {
4785                                         array[i] = array[--(*idx)];
4786                                         array[*idx] = 0;
4787                                         fail = 0;
4788                                 }
4789                         }
4790                 }
4791         } while_for_each_ftrace_rec();
4792 out:
4793         mutex_unlock(&ftrace_lock);
4794
4795         if (fail)
4796                 return -EINVAL;
4797
4798         return 0;
4799 }
4800
4801 static ssize_t
4802 ftrace_graph_write(struct file *file, const char __user *ubuf,
4803                    size_t cnt, loff_t *ppos)
4804 {
4805         struct trace_parser parser;
4806         ssize_t read, ret = 0;
4807         struct ftrace_graph_data *fgd = file->private_data;
4808
4809         if (!cnt)
4810                 return 0;
4811
4812         if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
4813                 return -ENOMEM;
4814
4815         read = trace_get_user(&parser, ubuf, cnt, ppos);
4816
4817         if (read >= 0 && trace_parser_loaded((&parser))) {
4818                 parser.buffer[parser.idx] = 0;
4819
4820                 mutex_lock(&graph_lock);
4821
4822                 /* we allow only one expression at a time */
4823                 ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
4824                                       parser.buffer);
4825
4826                 mutex_unlock(&graph_lock);
4827         }
4828
4829         if (!ret)
4830                 ret = read;
4831
4832         trace_parser_put(&parser);
4833
4834         return ret;
4835 }
4836
4837 static const struct file_operations ftrace_graph_fops = {
4838         .open           = ftrace_graph_open,
4839         .read           = seq_read,
4840         .write          = ftrace_graph_write,
4841         .llseek         = tracing_lseek,
4842         .release        = ftrace_graph_release,
4843 };
4844
4845 static const struct file_operations ftrace_graph_notrace_fops = {
4846         .open           = ftrace_graph_notrace_open,
4847         .read           = seq_read,
4848         .write          = ftrace_graph_write,
4849         .llseek         = tracing_lseek,
4850         .release        = ftrace_graph_release,
4851 };
4852 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4853
4854 void ftrace_create_filter_files(struct ftrace_ops *ops,
4855                                 struct dentry *parent)
4856 {
4857
4858         trace_create_file("set_ftrace_filter", 0644, parent,
4859                           ops, &ftrace_filter_fops);
4860
4861         trace_create_file("set_ftrace_notrace", 0644, parent,
4862                           ops, &ftrace_notrace_fops);
4863 }
4864
4865 /*
4866  * The name "destroy_filter_files" is really a misnomer. Although
4867  * in the future, it may actualy delete the files, but this is
4868  * really intended to make sure the ops passed in are disabled
4869  * and that when this function returns, the caller is free to
4870  * free the ops.
4871  *
4872  * The "destroy" name is only to match the "create" name that this
4873  * should be paired with.
4874  */
4875 void ftrace_destroy_filter_files(struct ftrace_ops *ops)
4876 {
4877         mutex_lock(&ftrace_lock);
4878         if (ops->flags & FTRACE_OPS_FL_ENABLED)
4879                 ftrace_shutdown(ops, 0);
4880         ops->flags |= FTRACE_OPS_FL_DELETED;
4881         ftrace_free_filter(ops);
4882         mutex_unlock(&ftrace_lock);
4883 }
4884
4885 static __init int ftrace_init_dyn_tracefs(struct dentry *d_tracer)
4886 {
4887
4888         trace_create_file("available_filter_functions", 0444,
4889                         d_tracer, NULL, &ftrace_avail_fops);
4890
4891         trace_create_file("enabled_functions", 0444,
4892                         d_tracer, NULL, &ftrace_enabled_fops);
4893
4894         ftrace_create_filter_files(&global_ops, d_tracer);
4895
4896 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4897         trace_create_file("set_graph_function", 0444, d_tracer,
4898                                     NULL,
4899                                     &ftrace_graph_fops);
4900         trace_create_file("set_graph_notrace", 0444, d_tracer,
4901                                     NULL,
4902                                     &ftrace_graph_notrace_fops);
4903 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4904
4905         return 0;
4906 }
4907
4908 static int ftrace_cmp_ips(const void *a, const void *b)
4909 {
4910         const unsigned long *ipa = a;
4911         const unsigned long *ipb = b;
4912
4913         if (*ipa > *ipb)
4914                 return 1;
4915         if (*ipa < *ipb)
4916                 return -1;
4917         return 0;
4918 }
4919
4920 static int ftrace_process_locs(struct module *mod,
4921                                unsigned long *start,
4922                                unsigned long *end)
4923 {
4924         struct ftrace_page *start_pg;
4925         struct ftrace_page *pg;
4926         struct dyn_ftrace *rec;
4927         unsigned long count;
4928         unsigned long *p;
4929         unsigned long addr;
4930         unsigned long flags = 0; /* Shut up gcc */
4931         int ret = -ENOMEM;
4932
4933         count = end - start;
4934
4935         if (!count)
4936                 return 0;
4937
4938         sort(start, count, sizeof(*start),
4939              ftrace_cmp_ips, NULL);
4940
4941         start_pg = ftrace_allocate_pages(count);
4942         if (!start_pg)
4943                 return -ENOMEM;
4944
4945         mutex_lock(&ftrace_lock);
4946
4947         /*
4948          * Core and each module needs their own pages, as
4949          * modules will free them when they are removed.
4950          * Force a new page to be allocated for modules.
4951          */
4952         if (!mod) {
4953                 WARN_ON(ftrace_pages || ftrace_pages_start);
4954                 /* First initialization */
4955                 ftrace_pages = ftrace_pages_start = start_pg;
4956         } else {
4957                 if (!ftrace_pages)
4958                         goto out;
4959
4960                 if (WARN_ON(ftrace_pages->next)) {
4961                         /* Hmm, we have free pages? */
4962                         while (ftrace_pages->next)
4963                                 ftrace_pages = ftrace_pages->next;
4964                 }
4965
4966                 ftrace_pages->next = start_pg;
4967         }
4968
4969         p = start;
4970         pg = start_pg;
4971         while (p < end) {
4972                 addr = ftrace_call_adjust(*p++);
4973                 /*
4974                  * Some architecture linkers will pad between
4975                  * the different mcount_loc sections of different
4976                  * object files to satisfy alignments.
4977                  * Skip any NULL pointers.
4978                  */
4979                 if (!addr)
4980                         continue;
4981
4982                 if (pg->index == pg->size) {
4983                         /* We should have allocated enough */
4984                         if (WARN_ON(!pg->next))
4985                                 break;
4986                         pg = pg->next;
4987                 }
4988
4989                 rec = &pg->records[pg->index++];
4990                 rec->ip = addr;
4991         }
4992
4993         /* We should have used all pages */
4994         WARN_ON(pg->next);
4995
4996         /* Assign the last page to ftrace_pages */
4997         ftrace_pages = pg;
4998
4999         /*
5000          * We only need to disable interrupts on start up
5001          * because we are modifying code that an interrupt
5002          * may execute, and the modification is not atomic.
5003          * But for modules, nothing runs the code we modify
5004          * until we are finished with it, and there's no
5005          * reason to cause large interrupt latencies while we do it.
5006          */
5007         if (!mod)
5008                 local_irq_save(flags);
5009         ftrace_update_code(mod, start_pg);
5010         if (!mod)
5011                 local_irq_restore(flags);
5012         ret = 0;
5013  out:
5014         mutex_unlock(&ftrace_lock);
5015
5016         return ret;
5017 }
5018
5019 #ifdef CONFIG_MODULES
5020
5021 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
5022
5023 static int referenced_filters(struct dyn_ftrace *rec)
5024 {
5025         struct ftrace_ops *ops;
5026         int cnt = 0;
5027
5028         for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
5029                 if (ops_references_rec(ops, rec)) {
5030                         cnt++;
5031                         if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
5032                                 rec->flags |= FTRACE_FL_REGS;
5033                 }
5034         }
5035
5036         return cnt;
5037 }
5038
5039 void ftrace_release_mod(struct module *mod)
5040 {
5041         struct dyn_ftrace *rec;
5042         struct ftrace_page **last_pg;
5043         struct ftrace_page *pg;
5044         int order;
5045
5046         mutex_lock(&ftrace_lock);
5047
5048         if (ftrace_disabled)
5049                 goto out_unlock;
5050
5051         /*
5052          * Each module has its own ftrace_pages, remove
5053          * them from the list.
5054          */
5055         last_pg = &ftrace_pages_start;
5056         for (pg = ftrace_pages_start; pg; pg = *last_pg) {
5057                 rec = &pg->records[0];
5058                 if (within_module_core(rec->ip, mod)) {
5059                         /*
5060                          * As core pages are first, the first
5061                          * page should never be a module page.
5062                          */
5063                         if (WARN_ON(pg == ftrace_pages_start))
5064                                 goto out_unlock;
5065
5066                         /* Check if we are deleting the last page */
5067                         if (pg == ftrace_pages)
5068                                 ftrace_pages = next_to_ftrace_page(last_pg);
5069
5070                         *last_pg = pg->next;
5071                         order = get_count_order(pg->size / ENTRIES_PER_PAGE);
5072                         free_pages((unsigned long)pg->records, order);
5073                         kfree(pg);
5074                 } else
5075                         last_pg = &pg->next;
5076         }
5077  out_unlock:
5078         mutex_unlock(&ftrace_lock);
5079 }
5080
5081 void ftrace_module_enable(struct module *mod)
5082 {
5083         struct dyn_ftrace *rec;
5084         struct ftrace_page *pg;
5085
5086         mutex_lock(&ftrace_lock);
5087
5088         if (ftrace_disabled)
5089                 goto out_unlock;
5090
5091         /*
5092          * If the tracing is enabled, go ahead and enable the record.
5093          *
5094          * The reason not to enable the record immediatelly is the
5095          * inherent check of ftrace_make_nop/ftrace_make_call for
5096          * correct previous instructions.  Making first the NOP
5097          * conversion puts the module to the correct state, thus
5098          * passing the ftrace_make_call check.
5099          *
5100          * We also delay this to after the module code already set the
5101          * text to read-only, as we now need to set it back to read-write
5102          * so that we can modify the text.
5103          */
5104         if (ftrace_start_up)
5105                 ftrace_arch_code_modify_prepare();
5106
5107         do_for_each_ftrace_rec(pg, rec) {
5108                 int cnt;
5109                 /*
5110                  * do_for_each_ftrace_rec() is a double loop.
5111                  * module text shares the pg. If a record is
5112                  * not part of this module, then skip this pg,
5113                  * which the "break" will do.
5114                  */
5115                 if (!within_module_core(rec->ip, mod))
5116                         break;
5117
5118                 cnt = 0;
5119
5120                 /*
5121                  * When adding a module, we need to check if tracers are
5122                  * currently enabled and if they are, and can trace this record,
5123                  * we need to enable the module functions as well as update the
5124                  * reference counts for those function records.
5125                  */
5126                 if (ftrace_start_up)
5127                         cnt += referenced_filters(rec);
5128
5129                 rec->flags &= ~FTRACE_FL_DISABLED;
5130                 rec->flags += cnt;
5131
5132                 if (ftrace_start_up && cnt) {
5133                         int failed = __ftrace_replace_code(rec, 1);
5134                         if (failed) {
5135                                 ftrace_bug(failed, rec);
5136                                 goto out_loop;
5137                         }
5138                 }
5139
5140         } while_for_each_ftrace_rec();
5141
5142  out_loop:
5143         if (ftrace_start_up)
5144                 ftrace_arch_code_modify_post_process();
5145
5146  out_unlock:
5147         mutex_unlock(&ftrace_lock);
5148 }
5149
5150 void ftrace_module_init(struct module *mod)
5151 {
5152         if (ftrace_disabled || !mod->num_ftrace_callsites)
5153                 return;
5154
5155         ftrace_process_locs(mod, mod->ftrace_callsites,
5156                             mod->ftrace_callsites + mod->num_ftrace_callsites);
5157 }
5158 #endif /* CONFIG_MODULES */
5159
5160 void __init ftrace_init(void)
5161 {
5162         extern unsigned long __start_mcount_loc[];
5163         extern unsigned long __stop_mcount_loc[];
5164         unsigned long count, flags;
5165         int ret;
5166
5167         local_irq_save(flags);
5168         ret = ftrace_dyn_arch_init();
5169         local_irq_restore(flags);
5170         if (ret)
5171                 goto failed;
5172
5173         count = __stop_mcount_loc - __start_mcount_loc;
5174         if (!count) {
5175                 pr_info("ftrace: No functions to be traced?\n");
5176                 goto failed;
5177         }
5178
5179         pr_info("ftrace: allocating %ld entries in %ld pages\n",
5180                 count, DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
5181
5182         last_ftrace_enabled = ftrace_enabled = 1;
5183
5184         ret = ftrace_process_locs(NULL,
5185                                   __start_mcount_loc,
5186                                   __stop_mcount_loc);
5187
5188         set_ftrace_early_filters();
5189
5190         return;
5191  failed:
5192         ftrace_disabled = 1;
5193 }
5194
5195 /* Do nothing if arch does not support this */
5196 void __weak arch_ftrace_update_trampoline(struct ftrace_ops *ops)
5197 {
5198 }
5199
5200 static void ftrace_update_trampoline(struct ftrace_ops *ops)
5201 {
5202
5203 /*
5204  * Currently there's no safe way to free a trampoline when the kernel
5205  * is configured with PREEMPT. That is because a task could be preempted
5206  * when it jumped to the trampoline, it may be preempted for a long time
5207  * depending on the system load, and currently there's no way to know
5208  * when it will be off the trampoline. If the trampoline is freed
5209  * too early, when the task runs again, it will be executing on freed
5210  * memory and crash.
5211  */
5212 #ifdef CONFIG_PREEMPT
5213         /* Currently, only non dynamic ops can have a trampoline */
5214         if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
5215                 return;
5216 #endif
5217
5218         arch_ftrace_update_trampoline(ops);
5219 }
5220
5221 #else
5222
5223 static struct ftrace_ops global_ops = {
5224         .func                   = ftrace_stub,
5225         .flags                  = FTRACE_OPS_FL_RECURSION_SAFE |
5226                                   FTRACE_OPS_FL_INITIALIZED |
5227                                   FTRACE_OPS_FL_PID,
5228 };
5229
5230 static int __init ftrace_nodyn_init(void)
5231 {
5232         ftrace_enabled = 1;
5233         return 0;
5234 }
5235 core_initcall(ftrace_nodyn_init);
5236
5237 static inline int ftrace_init_dyn_tracefs(struct dentry *d_tracer) { return 0; }
5238 static inline void ftrace_startup_enable(int command) { }
5239 static inline void ftrace_startup_all(int command) { }
5240 /* Keep as macros so we do not need to define the commands */
5241 # define ftrace_startup(ops, command)                                   \
5242         ({                                                              \
5243                 int ___ret = __register_ftrace_function(ops);           \
5244                 if (!___ret)                                            \
5245                         (ops)->flags |= FTRACE_OPS_FL_ENABLED;          \
5246                 ___ret;                                                 \
5247         })
5248 # define ftrace_shutdown(ops, command)                                  \
5249         ({                                                              \
5250                 int ___ret = __unregister_ftrace_function(ops);         \
5251                 if (!___ret)                                            \
5252                         (ops)->flags &= ~FTRACE_OPS_FL_ENABLED;         \
5253                 ___ret;                                                 \
5254         })
5255
5256 # define ftrace_startup_sysctl()        do { } while (0)
5257 # define ftrace_shutdown_sysctl()       do { } while (0)
5258
5259 static inline int
5260 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
5261 {
5262         return 1;
5263 }
5264
5265 static void ftrace_update_trampoline(struct ftrace_ops *ops)
5266 {
5267 }
5268
5269 #endif /* CONFIG_DYNAMIC_FTRACE */
5270
5271 __init void ftrace_init_global_array_ops(struct trace_array *tr)
5272 {
5273         tr->ops = &global_ops;
5274         tr->ops->private = tr;
5275 }
5276
5277 void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func)
5278 {
5279         /* If we filter on pids, update to use the pid function */
5280         if (tr->flags & TRACE_ARRAY_FL_GLOBAL) {
5281                 if (WARN_ON(tr->ops->func != ftrace_stub))
5282                         printk("ftrace ops had %pS for function\n",
5283                                tr->ops->func);
5284         }
5285         tr->ops->func = func;
5286         tr->ops->private = tr;
5287 }
5288
5289 void ftrace_reset_array_ops(struct trace_array *tr)
5290 {
5291         tr->ops->func = ftrace_stub;
5292 }
5293
5294 static nokprobe_inline void
5295 __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
5296                        struct ftrace_ops *ignored, struct pt_regs *regs)
5297 {
5298         struct ftrace_ops *op;
5299         int bit;
5300
5301         bit = trace_test_and_set_recursion(TRACE_LIST_START);
5302         if (bit < 0)
5303                 return;
5304
5305         /*
5306          * Some of the ops may be dynamically allocated,
5307          * they must be freed after a synchronize_sched().
5308          */
5309         preempt_disable_notrace();
5310
5311         do_for_each_ftrace_op(op, ftrace_ops_list) {
5312                 /*
5313                  * Check the following for each ops before calling their func:
5314                  *  if RCU flag is set, then rcu_is_watching() must be true
5315                  *  if PER_CPU is set, then ftrace_function_local_disable()
5316                  *                          must be false
5317                  *  Otherwise test if the ip matches the ops filter
5318                  *
5319                  * If any of the above fails then the op->func() is not executed.
5320                  */
5321                 if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) &&
5322                     (!(op->flags & FTRACE_OPS_FL_PER_CPU) ||
5323                      !ftrace_function_local_disabled(op)) &&
5324                     ftrace_ops_test(op, ip, regs)) {
5325                     
5326                         if (FTRACE_WARN_ON(!op->func)) {
5327                                 pr_warn("op=%p %pS\n", op, op);
5328                                 goto out;
5329                         }
5330                         op->func(ip, parent_ip, op, regs);
5331                 }
5332         } while_for_each_ftrace_op(op);
5333 out:
5334         preempt_enable_notrace();
5335         trace_clear_recursion(bit);
5336 }
5337
5338 /*
5339  * Some archs only support passing ip and parent_ip. Even though
5340  * the list function ignores the op parameter, we do not want any
5341  * C side effects, where a function is called without the caller
5342  * sending a third parameter.
5343  * Archs are to support both the regs and ftrace_ops at the same time.
5344  * If they support ftrace_ops, it is assumed they support regs.
5345  * If call backs want to use regs, they must either check for regs
5346  * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
5347  * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
5348  * An architecture can pass partial regs with ftrace_ops and still
5349  * set the ARCH_SUPPORTS_FTRACE_OPS.
5350  */
5351 #if ARCH_SUPPORTS_FTRACE_OPS
5352 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
5353                                  struct ftrace_ops *op, struct pt_regs *regs)
5354 {
5355         __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
5356 }
5357 NOKPROBE_SYMBOL(ftrace_ops_list_func);
5358 #else
5359 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
5360 {
5361         __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
5362 }
5363 NOKPROBE_SYMBOL(ftrace_ops_no_ops);
5364 #endif
5365
5366 /*
5367  * If there's only one function registered but it does not support
5368  * recursion, needs RCU protection and/or requires per cpu handling, then
5369  * this function will be called by the mcount trampoline.
5370  */
5371 static void ftrace_ops_assist_func(unsigned long ip, unsigned long parent_ip,
5372                                    struct ftrace_ops *op, struct pt_regs *regs)
5373 {
5374         int bit;
5375
5376         bit = trace_test_and_set_recursion(TRACE_LIST_START);
5377         if (bit < 0)
5378                 return;
5379
5380         preempt_disable_notrace();
5381
5382         if ((!(op->flags & FTRACE_OPS_FL_RCU) || rcu_is_watching()) &&
5383             (!(op->flags & FTRACE_OPS_FL_PER_CPU) ||
5384              !ftrace_function_local_disabled(op))) {
5385                 op->func(ip, parent_ip, op, regs);
5386         }
5387
5388         preempt_enable_notrace();
5389         trace_clear_recursion(bit);
5390 }
5391 NOKPROBE_SYMBOL(ftrace_ops_assist_func);
5392
5393 /**
5394  * ftrace_ops_get_func - get the function a trampoline should call
5395  * @ops: the ops to get the function for
5396  *
5397  * Normally the mcount trampoline will call the ops->func, but there
5398  * are times that it should not. For example, if the ops does not
5399  * have its own recursion protection, then it should call the
5400  * ftrace_ops_recurs_func() instead.
5401  *
5402  * Returns the function that the trampoline should call for @ops.
5403  */
5404 ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops)
5405 {
5406         /*
5407          * If the function does not handle recursion, needs to be RCU safe,
5408          * or does per cpu logic, then we need to call the assist handler.
5409          */
5410         if (!(ops->flags & FTRACE_OPS_FL_RECURSION_SAFE) ||
5411             ops->flags & (FTRACE_OPS_FL_RCU | FTRACE_OPS_FL_PER_CPU))
5412                 return ftrace_ops_assist_func;
5413
5414         return ops->func;
5415 }
5416
5417 static void
5418 ftrace_filter_pid_sched_switch_probe(void *data, bool preempt,
5419                     struct task_struct *prev, struct task_struct *next)
5420 {
5421         struct trace_array *tr = data;
5422         struct trace_pid_list *pid_list;
5423
5424         pid_list = rcu_dereference_sched(tr->function_pids);
5425
5426         this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
5427                        trace_ignore_this_task(pid_list, next));
5428 }
5429
5430 static void clear_ftrace_pids(struct trace_array *tr)
5431 {
5432         struct trace_pid_list *pid_list;
5433         int cpu;
5434
5435         pid_list = rcu_dereference_protected(tr->function_pids,
5436                                              lockdep_is_held(&ftrace_lock));
5437         if (!pid_list)
5438                 return;
5439
5440         unregister_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
5441
5442         for_each_possible_cpu(cpu)
5443                 per_cpu_ptr(tr->trace_buffer.data, cpu)->ftrace_ignore_pid = false;
5444
5445         rcu_assign_pointer(tr->function_pids, NULL);
5446
5447         /* Wait till all users are no longer using pid filtering */
5448         synchronize_sched();
5449
5450         trace_free_pid_list(pid_list);
5451 }
5452
5453 void ftrace_clear_pids(struct trace_array *tr)
5454 {
5455         mutex_lock(&ftrace_lock);
5456
5457         clear_ftrace_pids(tr);
5458
5459         mutex_unlock(&ftrace_lock);
5460 }
5461
5462 static void ftrace_pid_reset(struct trace_array *tr)
5463 {
5464         mutex_lock(&ftrace_lock);
5465         clear_ftrace_pids(tr);
5466
5467         ftrace_update_pid_func();
5468         ftrace_startup_all(0);
5469
5470         mutex_unlock(&ftrace_lock);
5471 }
5472
5473 /* Greater than any max PID */
5474 #define FTRACE_NO_PIDS          (void *)(PID_MAX_LIMIT + 1)
5475
5476 static void *fpid_start(struct seq_file *m, loff_t *pos)
5477         __acquires(RCU)
5478 {
5479         struct trace_pid_list *pid_list;
5480         struct trace_array *tr = m->private;
5481
5482         mutex_lock(&ftrace_lock);
5483         rcu_read_lock_sched();
5484
5485         pid_list = rcu_dereference_sched(tr->function_pids);
5486
5487         if (!pid_list)
5488                 return !(*pos) ? FTRACE_NO_PIDS : NULL;
5489
5490         return trace_pid_start(pid_list, pos);
5491 }
5492
5493 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
5494 {
5495         struct trace_array *tr = m->private;
5496         struct trace_pid_list *pid_list = rcu_dereference_sched(tr->function_pids);
5497
5498         if (v == FTRACE_NO_PIDS) {
5499                 (*pos)++;
5500                 return NULL;
5501         }
5502         return trace_pid_next(pid_list, v, pos);
5503 }
5504
5505 static void fpid_stop(struct seq_file *m, void *p)
5506         __releases(RCU)
5507 {
5508         rcu_read_unlock_sched();
5509         mutex_unlock(&ftrace_lock);
5510 }
5511
5512 static int fpid_show(struct seq_file *m, void *v)
5513 {
5514         if (v == FTRACE_NO_PIDS) {
5515                 seq_puts(m, "no pid\n");
5516                 return 0;
5517         }
5518
5519         return trace_pid_show(m, v);
5520 }
5521
5522 static const struct seq_operations ftrace_pid_sops = {
5523         .start = fpid_start,
5524         .next = fpid_next,
5525         .stop = fpid_stop,
5526         .show = fpid_show,
5527 };
5528
5529 static int
5530 ftrace_pid_open(struct inode *inode, struct file *file)
5531 {
5532         struct trace_array *tr = inode->i_private;
5533         struct seq_file *m;
5534         int ret = 0;
5535
5536         if (trace_array_get(tr) < 0)
5537                 return -ENODEV;
5538
5539         if ((file->f_mode & FMODE_WRITE) &&
5540             (file->f_flags & O_TRUNC))
5541                 ftrace_pid_reset(tr);
5542
5543         ret = seq_open(file, &ftrace_pid_sops);
5544         if (ret < 0) {
5545                 trace_array_put(tr);
5546         } else {
5547                 m = file->private_data;
5548                 /* copy tr over to seq ops */
5549                 m->private = tr;
5550         }
5551
5552         return ret;
5553 }
5554
5555 static void ignore_task_cpu(void *data)
5556 {
5557         struct trace_array *tr = data;
5558         struct trace_pid_list *pid_list;
5559
5560         /*
5561          * This function is called by on_each_cpu() while the
5562          * event_mutex is held.
5563          */
5564         pid_list = rcu_dereference_protected(tr->function_pids,
5565                                              mutex_is_locked(&ftrace_lock));
5566
5567         this_cpu_write(tr->trace_buffer.data->ftrace_ignore_pid,
5568                        trace_ignore_this_task(pid_list, current));
5569 }
5570
5571 static ssize_t
5572 ftrace_pid_write(struct file *filp, const char __user *ubuf,
5573                    size_t cnt, loff_t *ppos)
5574 {
5575         struct seq_file *m = filp->private_data;
5576         struct trace_array *tr = m->private;
5577         struct trace_pid_list *filtered_pids = NULL;
5578         struct trace_pid_list *pid_list;
5579         ssize_t ret;
5580
5581         if (!cnt)
5582                 return 0;
5583
5584         mutex_lock(&ftrace_lock);
5585
5586         filtered_pids = rcu_dereference_protected(tr->function_pids,
5587                                              lockdep_is_held(&ftrace_lock));
5588
5589         ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
5590         if (ret < 0)
5591                 goto out;
5592
5593         rcu_assign_pointer(tr->function_pids, pid_list);
5594
5595         if (filtered_pids) {
5596                 synchronize_sched();
5597                 trace_free_pid_list(filtered_pids);
5598         } else if (pid_list) {
5599                 /* Register a probe to set whether to ignore the tracing of a task */
5600                 register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);
5601         }
5602
5603         /*
5604          * Ignoring of pids is done at task switch. But we have to
5605          * check for those tasks that are currently running.
5606          * Always do this in case a pid was appended or removed.
5607          */
5608         on_each_cpu(ignore_task_cpu, tr, 1);
5609
5610         ftrace_update_pid_func();
5611         ftrace_startup_all(0);
5612  out:
5613         mutex_unlock(&ftrace_lock);
5614
5615         if (ret > 0)
5616                 *ppos += ret;
5617
5618         return ret;
5619 }
5620
5621 static int
5622 ftrace_pid_release(struct inode *inode, struct file *file)
5623 {
5624         struct trace_array *tr = inode->i_private;
5625
5626         trace_array_put(tr);
5627
5628         return seq_release(inode, file);
5629 }
5630
5631 static const struct file_operations ftrace_pid_fops = {
5632         .open           = ftrace_pid_open,
5633         .write          = ftrace_pid_write,
5634         .read           = seq_read,
5635         .llseek         = tracing_lseek,
5636         .release        = ftrace_pid_release,
5637 };
5638
5639 void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer)
5640 {
5641         trace_create_file("set_ftrace_pid", 0644, d_tracer,
5642                             tr, &ftrace_pid_fops);
5643 }
5644
5645 void __init ftrace_init_tracefs_toplevel(struct trace_array *tr,
5646                                          struct dentry *d_tracer)
5647 {
5648         /* Only the top level directory has the dyn_tracefs and profile */
5649         WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL));
5650
5651         ftrace_init_dyn_tracefs(d_tracer);
5652         ftrace_profile_tracefs(d_tracer);
5653 }
5654
5655 /**
5656  * ftrace_kill - kill ftrace
5657  *
5658  * This function should be used by panic code. It stops ftrace
5659  * but in a not so nice way. If you need to simply kill ftrace
5660  * from a non-atomic section, use ftrace_kill.
5661  */
5662 void ftrace_kill(void)
5663 {
5664         ftrace_disabled = 1;
5665         ftrace_enabled = 0;
5666         clear_ftrace_function();
5667 }
5668
5669 /**
5670  * Test if ftrace is dead or not.
5671  */
5672 int ftrace_is_dead(void)
5673 {
5674         return ftrace_disabled;
5675 }
5676
5677 /**
5678  * register_ftrace_function - register a function for profiling
5679  * @ops - ops structure that holds the function for profiling.
5680  *
5681  * Register a function to be called by all functions in the
5682  * kernel.
5683  *
5684  * Note: @ops->func and all the functions it calls must be labeled
5685  *       with "notrace", otherwise it will go into a
5686  *       recursive loop.
5687  */
5688 int register_ftrace_function(struct ftrace_ops *ops)
5689 {
5690         int ret = -1;
5691
5692         ftrace_ops_init(ops);
5693
5694         mutex_lock(&ftrace_lock);
5695
5696         ret = ftrace_startup(ops, 0);
5697
5698         mutex_unlock(&ftrace_lock);
5699
5700         return ret;
5701 }
5702 EXPORT_SYMBOL_GPL(register_ftrace_function);
5703
5704 /**
5705  * unregister_ftrace_function - unregister a function for profiling.
5706  * @ops - ops structure that holds the function to unregister
5707  *
5708  * Unregister a function that was added to be called by ftrace profiling.
5709  */
5710 int unregister_ftrace_function(struct ftrace_ops *ops)
5711 {
5712         int ret;
5713
5714         mutex_lock(&ftrace_lock);
5715         ret = ftrace_shutdown(ops, 0);
5716         mutex_unlock(&ftrace_lock);
5717
5718         return ret;
5719 }
5720 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
5721
5722 int
5723 ftrace_enable_sysctl(struct ctl_table *table, int write,
5724                      void __user *buffer, size_t *lenp,
5725                      loff_t *ppos)
5726 {
5727         int ret = -ENODEV;
5728
5729         mutex_lock(&ftrace_lock);
5730
5731         if (unlikely(ftrace_disabled))
5732                 goto out;
5733
5734         ret = proc_dointvec(table, write, buffer, lenp, ppos);
5735
5736         if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
5737                 goto out;
5738
5739         last_ftrace_enabled = !!ftrace_enabled;
5740
5741         if (ftrace_enabled) {
5742
5743                 /* we are starting ftrace again */
5744                 if (ftrace_ops_list != &ftrace_list_end)
5745                         update_ftrace_function();
5746
5747                 ftrace_startup_sysctl();
5748
5749         } else {
5750                 /* stopping ftrace calls (just send to ftrace_stub) */
5751                 ftrace_trace_function = ftrace_stub;
5752
5753                 ftrace_shutdown_sysctl();
5754         }
5755
5756  out:
5757         mutex_unlock(&ftrace_lock);
5758         return ret;
5759 }
5760
5761 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
5762
5763 static struct ftrace_ops graph_ops = {
5764         .func                   = ftrace_stub,
5765         .flags                  = FTRACE_OPS_FL_RECURSION_SAFE |
5766                                    FTRACE_OPS_FL_INITIALIZED |
5767                                    FTRACE_OPS_FL_PID |
5768                                    FTRACE_OPS_FL_STUB,
5769 #ifdef FTRACE_GRAPH_TRAMP_ADDR
5770         .trampoline             = FTRACE_GRAPH_TRAMP_ADDR,
5771         /* trampoline_size is only needed for dynamically allocated tramps */
5772 #endif
5773         ASSIGN_OPS_HASH(graph_ops, &global_ops.local_hash)
5774 };
5775
5776 void ftrace_graph_sleep_time_control(bool enable)
5777 {
5778         fgraph_sleep_time = enable;
5779 }
5780
5781 void ftrace_graph_graph_time_control(bool enable)
5782 {
5783         fgraph_graph_time = enable;
5784 }
5785
5786 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
5787 {
5788         return 0;
5789 }
5790
5791 /* The callbacks that hook a function */
5792 trace_func_graph_ret_t ftrace_graph_return =
5793                         (trace_func_graph_ret_t)ftrace_stub;
5794 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
5795 static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
5796
5797 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
5798 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
5799 {
5800         int i;
5801         int ret = 0;
5802         int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
5803         struct task_struct *g, *t;
5804
5805         for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
5806                 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
5807                                         * sizeof(struct ftrace_ret_stack),
5808                                         GFP_KERNEL);
5809                 if (!ret_stack_list[i]) {
5810                         start = 0;
5811                         end = i;
5812                         ret = -ENOMEM;
5813                         goto free;
5814                 }
5815         }
5816
5817         read_lock(&tasklist_lock);
5818         do_each_thread(g, t) {
5819                 if (start == end) {
5820                         ret = -EAGAIN;
5821                         goto unlock;
5822                 }
5823
5824                 if (t->ret_stack == NULL) {
5825                         atomic_set(&t->trace_overrun, 0);
5826                         t->curr_ret_stack = -1;
5827                         /* Make sure the tasks see the -1 first: */
5828                         smp_wmb();
5829                         t->ret_stack = ret_stack_list[start++];
5830                 }
5831         } while_each_thread(g, t);
5832
5833 unlock:
5834         read_unlock(&tasklist_lock);
5835 free:
5836         for (i = start; i < end; i++)
5837                 kfree(ret_stack_list[i]);
5838         return ret;
5839 }
5840
5841 static void
5842 ftrace_graph_probe_sched_switch(void *ignore, bool preempt,
5843                         struct task_struct *prev, struct task_struct *next)
5844 {
5845         unsigned long long timestamp;
5846         int index;
5847
5848         /*
5849          * Does the user want to count the time a function was asleep.
5850          * If so, do not update the time stamps.
5851          */
5852         if (fgraph_sleep_time)
5853                 return;
5854
5855         timestamp = trace_clock_local();
5856
5857         prev->ftrace_timestamp = timestamp;
5858
5859         /* only process tasks that we timestamped */
5860         if (!next->ftrace_timestamp)
5861                 return;
5862
5863         /*
5864          * Update all the counters in next to make up for the
5865          * time next was sleeping.
5866          */
5867         timestamp -= next->ftrace_timestamp;
5868
5869         for (index = next->curr_ret_stack; index >= 0; index--)
5870                 next->ret_stack[index].calltime += timestamp;
5871 }
5872
5873 /* Allocate a return stack for each task */
5874 static int start_graph_tracing(void)
5875 {
5876         struct ftrace_ret_stack **ret_stack_list;
5877         int ret, cpu;
5878
5879         ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
5880                                 sizeof(struct ftrace_ret_stack *),
5881                                 GFP_KERNEL);
5882
5883         if (!ret_stack_list)
5884                 return -ENOMEM;
5885
5886         /* The cpu_boot init_task->ret_stack will never be freed */
5887         for_each_online_cpu(cpu) {
5888                 if (!idle_task(cpu)->ret_stack)
5889                         ftrace_graph_init_idle_task(idle_task(cpu), cpu);
5890         }
5891
5892         do {
5893                 ret = alloc_retstack_tasklist(ret_stack_list);
5894         } while (ret == -EAGAIN);
5895
5896         if (!ret) {
5897                 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
5898                 if (ret)
5899                         pr_info("ftrace_graph: Couldn't activate tracepoint"
5900                                 " probe to kernel_sched_switch\n");
5901         }
5902
5903         kfree(ret_stack_list);
5904         return ret;
5905 }
5906
5907 /*
5908  * Hibernation protection.
5909  * The state of the current task is too much unstable during
5910  * suspend/restore to disk. We want to protect against that.
5911  */
5912 static int
5913 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
5914                                                         void *unused)
5915 {
5916         switch (state) {
5917         case PM_HIBERNATION_PREPARE:
5918                 pause_graph_tracing();
5919                 break;
5920
5921         case PM_POST_HIBERNATION:
5922                 unpause_graph_tracing();
5923                 break;
5924         }
5925         return NOTIFY_DONE;
5926 }
5927
5928 static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
5929 {
5930         if (!ftrace_ops_test(&global_ops, trace->func, NULL))
5931                 return 0;
5932         return __ftrace_graph_entry(trace);
5933 }
5934
5935 /*
5936  * The function graph tracer should only trace the functions defined
5937  * by set_ftrace_filter and set_ftrace_notrace. If another function
5938  * tracer ops is registered, the graph tracer requires testing the
5939  * function against the global ops, and not just trace any function
5940  * that any ftrace_ops registered.
5941  */
5942 static void update_function_graph_func(void)
5943 {
5944         struct ftrace_ops *op;
5945         bool do_test = false;
5946
5947         /*
5948          * The graph and global ops share the same set of functions
5949          * to test. If any other ops is on the list, then
5950          * the graph tracing needs to test if its the function
5951          * it should call.
5952          */
5953         do_for_each_ftrace_op(op, ftrace_ops_list) {
5954                 if (op != &global_ops && op != &graph_ops &&
5955                     op != &ftrace_list_end) {
5956                         do_test = true;
5957                         /* in double loop, break out with goto */
5958                         goto out;
5959                 }
5960         } while_for_each_ftrace_op(op);
5961  out:
5962         if (do_test)
5963                 ftrace_graph_entry = ftrace_graph_entry_test;
5964         else
5965                 ftrace_graph_entry = __ftrace_graph_entry;
5966 }
5967
5968 static struct notifier_block ftrace_suspend_notifier = {
5969         .notifier_call = ftrace_suspend_notifier_call,
5970 };
5971
5972 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
5973                         trace_func_graph_ent_t entryfunc)
5974 {
5975         int ret = 0;
5976
5977         mutex_lock(&ftrace_lock);
5978
5979         /* we currently allow only one tracer registered at a time */
5980         if (ftrace_graph_active) {
5981                 ret = -EBUSY;
5982                 goto out;
5983         }
5984
5985         register_pm_notifier(&ftrace_suspend_notifier);
5986
5987         ftrace_graph_active++;
5988         ret = start_graph_tracing();
5989         if (ret) {
5990                 ftrace_graph_active--;
5991                 goto out;
5992         }
5993
5994         ftrace_graph_return = retfunc;
5995
5996         /*
5997          * Update the indirect function to the entryfunc, and the
5998          * function that gets called to the entry_test first. Then
5999          * call the update fgraph entry function to determine if
6000          * the entryfunc should be called directly or not.
6001          */
6002         __ftrace_graph_entry = entryfunc;
6003         ftrace_graph_entry = ftrace_graph_entry_test;
6004         update_function_graph_func();
6005
6006         ret = ftrace_startup(&graph_ops, FTRACE_START_FUNC_RET);
6007 out:
6008         mutex_unlock(&ftrace_lock);
6009         return ret;
6010 }
6011
6012 void unregister_ftrace_graph(void)
6013 {
6014         mutex_lock(&ftrace_lock);
6015
6016         if (unlikely(!ftrace_graph_active))
6017                 goto out;
6018
6019         ftrace_graph_active--;
6020         ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
6021         ftrace_graph_entry = ftrace_graph_entry_stub;
6022         __ftrace_graph_entry = ftrace_graph_entry_stub;
6023         ftrace_shutdown(&graph_ops, FTRACE_STOP_FUNC_RET);
6024         unregister_pm_notifier(&ftrace_suspend_notifier);
6025         unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
6026
6027  out:
6028         mutex_unlock(&ftrace_lock);
6029 }
6030
6031 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
6032
6033 static void
6034 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
6035 {
6036         atomic_set(&t->trace_overrun, 0);
6037         t->ftrace_timestamp = 0;
6038         /* make curr_ret_stack visible before we add the ret_stack */
6039         smp_wmb();
6040         t->ret_stack = ret_stack;
6041 }
6042
6043 /*
6044  * Allocate a return stack for the idle task. May be the first
6045  * time through, or it may be done by CPU hotplug online.
6046  */
6047 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
6048 {
6049         t->curr_ret_stack = -1;
6050         /*
6051          * The idle task has no parent, it either has its own
6052          * stack or no stack at all.
6053          */
6054         if (t->ret_stack)
6055                 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
6056
6057         if (ftrace_graph_active) {
6058                 struct ftrace_ret_stack *ret_stack;
6059
6060                 ret_stack = per_cpu(idle_ret_stack, cpu);
6061                 if (!ret_stack) {
6062                         ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
6063                                             * sizeof(struct ftrace_ret_stack),
6064                                             GFP_KERNEL);
6065                         if (!ret_stack)
6066                                 return;
6067                         per_cpu(idle_ret_stack, cpu) = ret_stack;
6068                 }
6069                 graph_init_task(t, ret_stack);
6070         }
6071 }
6072
6073 /* Allocate a return stack for newly created task */
6074 void ftrace_graph_init_task(struct task_struct *t)
6075 {
6076         /* Make sure we do not use the parent ret_stack */
6077         t->ret_stack = NULL;
6078         t->curr_ret_stack = -1;
6079
6080         if (ftrace_graph_active) {
6081                 struct ftrace_ret_stack *ret_stack;
6082
6083                 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
6084                                 * sizeof(struct ftrace_ret_stack),
6085                                 GFP_KERNEL);
6086                 if (!ret_stack)
6087                         return;
6088                 graph_init_task(t, ret_stack);
6089         }
6090 }
6091
6092 void ftrace_graph_exit_task(struct task_struct *t)
6093 {
6094         struct ftrace_ret_stack *ret_stack = t->ret_stack;
6095
6096         t->ret_stack = NULL;
6097         /* NULL must become visible to IRQs before we free it: */
6098         barrier();
6099
6100         kfree(ret_stack);
6101 }
6102 #endif