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