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