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