]> git.karo-electronics.de Git - karo-tx-linux.git/blob - kernel/trace/ftrace.c
tracing: Fix possible NULL pointer dereferences
[karo-tx-linux.git] / kernel / trace / ftrace.c
1 /*
2  * Infrastructure for profiling code inserted by 'gcc -pg'.
3  *
4  * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5  * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
6  *
7  * Originally ported from the -rt patch by:
8  *   Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
9  *
10  * Based on code in the latency_tracer, that is:
11  *
12  *  Copyright (C) 2004-2006 Ingo Molnar
13  *  Copyright (C) 2004 Nadia Yvette Chambers
14  */
15
16 #include <linux/stop_machine.h>
17 #include <linux/clocksource.h>
18 #include <linux/kallsyms.h>
19 #include <linux/seq_file.h>
20 #include <linux/suspend.h>
21 #include <linux/debugfs.h>
22 #include <linux/hardirq.h>
23 #include <linux/kthread.h>
24 #include <linux/uaccess.h>
25 #include <linux/bsearch.h>
26 #include <linux/module.h>
27 #include <linux/ftrace.h>
28 #include <linux/sysctl.h>
29 #include <linux/slab.h>
30 #include <linux/ctype.h>
31 #include <linux/sort.h>
32 #include <linux/list.h>
33 #include <linux/hash.h>
34 #include <linux/rcupdate.h>
35
36 #include <trace/events/sched.h>
37
38 #include <asm/setup.h>
39
40 #include "trace_output.h"
41 #include "trace_stat.h"
42
43 #define FTRACE_WARN_ON(cond)                    \
44         ({                                      \
45                 int ___r = cond;                \
46                 if (WARN_ON(___r))              \
47                         ftrace_kill();          \
48                 ___r;                           \
49         })
50
51 #define FTRACE_WARN_ON_ONCE(cond)               \
52         ({                                      \
53                 int ___r = cond;                \
54                 if (WARN_ON_ONCE(___r))         \
55                         ftrace_kill();          \
56                 ___r;                           \
57         })
58
59 /* hash bits for specific function selection */
60 #define FTRACE_HASH_BITS 7
61 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
62 #define FTRACE_HASH_DEFAULT_BITS 10
63 #define FTRACE_HASH_MAX_BITS 12
64
65 #define FL_GLOBAL_CONTROL_MASK (FTRACE_OPS_FL_GLOBAL | FTRACE_OPS_FL_CONTROL)
66
67 static struct ftrace_ops ftrace_list_end __read_mostly = {
68         .func           = ftrace_stub,
69         .flags          = FTRACE_OPS_FL_RECURSION_SAFE,
70 };
71
72 /* ftrace_enabled is a method to turn ftrace on or off */
73 int ftrace_enabled __read_mostly;
74 static int last_ftrace_enabled;
75
76 /* Quick disabling of function tracer. */
77 int function_trace_stop __read_mostly;
78
79 /* Current function tracing op */
80 struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
81
82 /* List for set_ftrace_pid's pids. */
83 LIST_HEAD(ftrace_pids);
84 struct ftrace_pid {
85         struct list_head list;
86         struct pid *pid;
87 };
88
89 /*
90  * ftrace_disabled is set when an anomaly is discovered.
91  * ftrace_disabled is much stronger than ftrace_enabled.
92  */
93 static int ftrace_disabled __read_mostly;
94
95 static DEFINE_MUTEX(ftrace_lock);
96
97 static struct ftrace_ops *ftrace_global_list __read_mostly = &ftrace_list_end;
98 static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end;
99 static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
100 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
101 ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
102 static struct ftrace_ops global_ops;
103 static struct ftrace_ops control_ops;
104
105 #if ARCH_SUPPORTS_FTRACE_OPS
106 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
107                                  struct ftrace_ops *op, struct pt_regs *regs);
108 #else
109 /* See comment below, where ftrace_ops_list_func is defined */
110 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
111 #define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
112 #endif
113
114 /**
115  * ftrace_nr_registered_ops - return number of ops registered
116  *
117  * Returns the number of ftrace_ops registered and tracing functions
118  */
119 int ftrace_nr_registered_ops(void)
120 {
121         struct ftrace_ops *ops;
122         int cnt = 0;
123
124         mutex_lock(&ftrace_lock);
125
126         for (ops = ftrace_ops_list;
127              ops != &ftrace_list_end; ops = ops->next)
128                 cnt++;
129
130         mutex_unlock(&ftrace_lock);
131
132         return cnt;
133 }
134
135 /*
136  * Traverse the ftrace_global_list, invoking all entries.  The reason that we
137  * can use rcu_dereference_raw() is that elements removed from this list
138  * are simply leaked, so there is no need to interact with a grace-period
139  * mechanism.  The rcu_dereference_raw() calls are needed to handle
140  * concurrent insertions into the ftrace_global_list.
141  *
142  * Silly Alpha and silly pointer-speculation compiler optimizations!
143  */
144 static void
145 ftrace_global_list_func(unsigned long ip, unsigned long parent_ip,
146                         struct ftrace_ops *op, struct pt_regs *regs)
147 {
148         if (unlikely(trace_recursion_test(TRACE_GLOBAL_BIT)))
149                 return;
150
151         trace_recursion_set(TRACE_GLOBAL_BIT);
152         op = rcu_dereference_raw(ftrace_global_list); /*see above*/
153         while (op != &ftrace_list_end) {
154                 op->func(ip, parent_ip, op, regs);
155                 op = rcu_dereference_raw(op->next); /*see above*/
156         };
157         trace_recursion_clear(TRACE_GLOBAL_BIT);
158 }
159
160 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
161                             struct ftrace_ops *op, struct pt_regs *regs)
162 {
163         if (!test_tsk_trace_trace(current))
164                 return;
165
166         ftrace_pid_function(ip, parent_ip, op, regs);
167 }
168
169 static void set_ftrace_pid_function(ftrace_func_t func)
170 {
171         /* do not set ftrace_pid_function to itself! */
172         if (func != ftrace_pid_func)
173                 ftrace_pid_function = func;
174 }
175
176 /**
177  * clear_ftrace_function - reset the ftrace function
178  *
179  * This NULLs the ftrace function and in essence stops
180  * tracing.  There may be lag
181  */
182 void clear_ftrace_function(void)
183 {
184         ftrace_trace_function = ftrace_stub;
185         ftrace_pid_function = ftrace_stub;
186 }
187
188 static void control_ops_disable_all(struct ftrace_ops *ops)
189 {
190         int cpu;
191
192         for_each_possible_cpu(cpu)
193                 *per_cpu_ptr(ops->disabled, cpu) = 1;
194 }
195
196 static int control_ops_alloc(struct ftrace_ops *ops)
197 {
198         int __percpu *disabled;
199
200         disabled = alloc_percpu(int);
201         if (!disabled)
202                 return -ENOMEM;
203
204         ops->disabled = disabled;
205         control_ops_disable_all(ops);
206         return 0;
207 }
208
209 static void control_ops_free(struct ftrace_ops *ops)
210 {
211         free_percpu(ops->disabled);
212 }
213
214 static void update_global_ops(void)
215 {
216         ftrace_func_t func;
217
218         /*
219          * If there's only one function registered, then call that
220          * function directly. Otherwise, we need to iterate over the
221          * registered callers.
222          */
223         if (ftrace_global_list == &ftrace_list_end ||
224             ftrace_global_list->next == &ftrace_list_end)
225                 func = ftrace_global_list->func;
226         else
227                 func = ftrace_global_list_func;
228
229         /* If we filter on pids, update to use the pid function */
230         if (!list_empty(&ftrace_pids)) {
231                 set_ftrace_pid_function(func);
232                 func = ftrace_pid_func;
233         }
234
235         global_ops.func = func;
236 }
237
238 static void update_ftrace_function(void)
239 {
240         ftrace_func_t func;
241
242         update_global_ops();
243
244         /*
245          * If we are at the end of the list and this ops is
246          * recursion safe and not dynamic and the arch supports passing ops,
247          * then have the mcount trampoline call the function directly.
248          */
249         if (ftrace_ops_list == &ftrace_list_end ||
250             (ftrace_ops_list->next == &ftrace_list_end &&
251              !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC) &&
252              (ftrace_ops_list->flags & FTRACE_OPS_FL_RECURSION_SAFE) &&
253              !FTRACE_FORCE_LIST_FUNC)) {
254                 /* Set the ftrace_ops that the arch callback uses */
255                 if (ftrace_ops_list == &global_ops)
256                         function_trace_op = ftrace_global_list;
257                 else
258                         function_trace_op = ftrace_ops_list;
259                 func = ftrace_ops_list->func;
260         } else {
261                 /* Just use the default ftrace_ops */
262                 function_trace_op = &ftrace_list_end;
263                 func = ftrace_ops_list_func;
264         }
265
266         ftrace_trace_function = func;
267 }
268
269 static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
270 {
271         ops->next = *list;
272         /*
273          * We are entering ops into the list but another
274          * CPU might be walking that list. We need to make sure
275          * the ops->next pointer is valid before another CPU sees
276          * the ops pointer included into the list.
277          */
278         rcu_assign_pointer(*list, ops);
279 }
280
281 static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
282 {
283         struct ftrace_ops **p;
284
285         /*
286          * If we are removing the last function, then simply point
287          * to the ftrace_stub.
288          */
289         if (*list == ops && ops->next == &ftrace_list_end) {
290                 *list = &ftrace_list_end;
291                 return 0;
292         }
293
294         for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
295                 if (*p == ops)
296                         break;
297
298         if (*p != ops)
299                 return -1;
300
301         *p = (*p)->next;
302         return 0;
303 }
304
305 static void add_ftrace_list_ops(struct ftrace_ops **list,
306                                 struct ftrace_ops *main_ops,
307                                 struct ftrace_ops *ops)
308 {
309         int first = *list == &ftrace_list_end;
310         add_ftrace_ops(list, ops);
311         if (first)
312                 add_ftrace_ops(&ftrace_ops_list, main_ops);
313 }
314
315 static int remove_ftrace_list_ops(struct ftrace_ops **list,
316                                   struct ftrace_ops *main_ops,
317                                   struct ftrace_ops *ops)
318 {
319         int ret = remove_ftrace_ops(list, ops);
320         if (!ret && *list == &ftrace_list_end)
321                 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
322         return ret;
323 }
324
325 static int __register_ftrace_function(struct ftrace_ops *ops)
326 {
327         if (unlikely(ftrace_disabled))
328                 return -ENODEV;
329
330         if (FTRACE_WARN_ON(ops == &global_ops))
331                 return -EINVAL;
332
333         if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
334                 return -EBUSY;
335
336         /* We don't support both control and global flags set. */
337         if ((ops->flags & FL_GLOBAL_CONTROL_MASK) == FL_GLOBAL_CONTROL_MASK)
338                 return -EINVAL;
339
340 #ifndef ARCH_SUPPORTS_FTRACE_SAVE_REGS
341         /*
342          * If the ftrace_ops specifies SAVE_REGS, then it only can be used
343          * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
344          * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
345          */
346         if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
347             !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
348                 return -EINVAL;
349
350         if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
351                 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
352 #endif
353
354         if (!core_kernel_data((unsigned long)ops))
355                 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
356
357         if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
358                 add_ftrace_list_ops(&ftrace_global_list, &global_ops, ops);
359                 ops->flags |= FTRACE_OPS_FL_ENABLED;
360         } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
361                 if (control_ops_alloc(ops))
362                         return -ENOMEM;
363                 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
364         } else
365                 add_ftrace_ops(&ftrace_ops_list, ops);
366
367         if (ftrace_enabled)
368                 update_ftrace_function();
369
370         return 0;
371 }
372
373 static int __unregister_ftrace_function(struct ftrace_ops *ops)
374 {
375         int ret;
376
377         if (ftrace_disabled)
378                 return -ENODEV;
379
380         if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
381                 return -EBUSY;
382
383         if (FTRACE_WARN_ON(ops == &global_ops))
384                 return -EINVAL;
385
386         if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
387                 ret = remove_ftrace_list_ops(&ftrace_global_list,
388                                              &global_ops, ops);
389                 if (!ret)
390                         ops->flags &= ~FTRACE_OPS_FL_ENABLED;
391         } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
392                 ret = remove_ftrace_list_ops(&ftrace_control_list,
393                                              &control_ops, ops);
394                 if (!ret) {
395                         /*
396                          * The ftrace_ops is now removed from the list,
397                          * so there'll be no new users. We must ensure
398                          * all current users are done before we free
399                          * the control data.
400                          */
401                         synchronize_sched();
402                         control_ops_free(ops);
403                 }
404         } else
405                 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
406
407         if (ret < 0)
408                 return ret;
409
410         if (ftrace_enabled)
411                 update_ftrace_function();
412
413         /*
414          * Dynamic ops may be freed, we must make sure that all
415          * callers are done before leaving this function.
416          */
417         if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
418                 synchronize_sched();
419
420         return 0;
421 }
422
423 static void ftrace_update_pid_func(void)
424 {
425         /* Only do something if we are tracing something */
426         if (ftrace_trace_function == ftrace_stub)
427                 return;
428
429         update_ftrace_function();
430 }
431
432 #ifdef CONFIG_FUNCTION_PROFILER
433 struct ftrace_profile {
434         struct hlist_node               node;
435         unsigned long                   ip;
436         unsigned long                   counter;
437 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
438         unsigned long long              time;
439         unsigned long long              time_squared;
440 #endif
441 };
442
443 struct ftrace_profile_page {
444         struct ftrace_profile_page      *next;
445         unsigned long                   index;
446         struct ftrace_profile           records[];
447 };
448
449 struct ftrace_profile_stat {
450         atomic_t                        disabled;
451         struct hlist_head               *hash;
452         struct ftrace_profile_page      *pages;
453         struct ftrace_profile_page      *start;
454         struct tracer_stat              stat;
455 };
456
457 #define PROFILE_RECORDS_SIZE                                            \
458         (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
459
460 #define PROFILES_PER_PAGE                                       \
461         (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
462
463 static int ftrace_profile_bits __read_mostly;
464 static int ftrace_profile_enabled __read_mostly;
465
466 /* ftrace_profile_lock - synchronize the enable and disable of the profiler */
467 static DEFINE_MUTEX(ftrace_profile_lock);
468
469 static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
470
471 #define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
472
473 static void *
474 function_stat_next(void *v, int idx)
475 {
476         struct ftrace_profile *rec = v;
477         struct ftrace_profile_page *pg;
478
479         pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
480
481  again:
482         if (idx != 0)
483                 rec++;
484
485         if ((void *)rec >= (void *)&pg->records[pg->index]) {
486                 pg = pg->next;
487                 if (!pg)
488                         return NULL;
489                 rec = &pg->records[0];
490                 if (!rec->counter)
491                         goto again;
492         }
493
494         return rec;
495 }
496
497 static void *function_stat_start(struct tracer_stat *trace)
498 {
499         struct ftrace_profile_stat *stat =
500                 container_of(trace, struct ftrace_profile_stat, stat);
501
502         if (!stat || !stat->start)
503                 return NULL;
504
505         return function_stat_next(&stat->start->records[0], 0);
506 }
507
508 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
509 /* function graph compares on total time */
510 static int function_stat_cmp(void *p1, void *p2)
511 {
512         struct ftrace_profile *a = p1;
513         struct ftrace_profile *b = p2;
514
515         if (a->time < b->time)
516                 return -1;
517         if (a->time > b->time)
518                 return 1;
519         else
520                 return 0;
521 }
522 #else
523 /* not function graph compares against hits */
524 static int function_stat_cmp(void *p1, void *p2)
525 {
526         struct ftrace_profile *a = p1;
527         struct ftrace_profile *b = p2;
528
529         if (a->counter < b->counter)
530                 return -1;
531         if (a->counter > b->counter)
532                 return 1;
533         else
534                 return 0;
535 }
536 #endif
537
538 static int function_stat_headers(struct seq_file *m)
539 {
540 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
541         seq_printf(m, "  Function                               "
542                    "Hit    Time            Avg             s^2\n"
543                       "  --------                               "
544                    "---    ----            ---             ---\n");
545 #else
546         seq_printf(m, "  Function                               Hit\n"
547                       "  --------                               ---\n");
548 #endif
549         return 0;
550 }
551
552 static int function_stat_show(struct seq_file *m, void *v)
553 {
554         struct ftrace_profile *rec = v;
555         char str[KSYM_SYMBOL_LEN];
556         int ret = 0;
557 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
558         static struct trace_seq s;
559         unsigned long long avg;
560         unsigned long long stddev;
561 #endif
562         mutex_lock(&ftrace_profile_lock);
563
564         /* we raced with function_profile_reset() */
565         if (unlikely(rec->counter == 0)) {
566                 ret = -EBUSY;
567                 goto out;
568         }
569
570         kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
571         seq_printf(m, "  %-30.30s  %10lu", str, rec->counter);
572
573 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
574         seq_printf(m, "    ");
575         avg = rec->time;
576         do_div(avg, rec->counter);
577
578         /* Sample standard deviation (s^2) */
579         if (rec->counter <= 1)
580                 stddev = 0;
581         else {
582                 stddev = rec->time_squared - rec->counter * avg * avg;
583                 /*
584                  * Divide only 1000 for ns^2 -> us^2 conversion.
585                  * trace_print_graph_duration will divide 1000 again.
586                  */
587                 do_div(stddev, (rec->counter - 1) * 1000);
588         }
589
590         trace_seq_init(&s);
591         trace_print_graph_duration(rec->time, &s);
592         trace_seq_puts(&s, "    ");
593         trace_print_graph_duration(avg, &s);
594         trace_seq_puts(&s, "    ");
595         trace_print_graph_duration(stddev, &s);
596         trace_print_seq(m, &s);
597 #endif
598         seq_putc(m, '\n');
599 out:
600         mutex_unlock(&ftrace_profile_lock);
601
602         return ret;
603 }
604
605 static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
606 {
607         struct ftrace_profile_page *pg;
608
609         pg = stat->pages = stat->start;
610
611         while (pg) {
612                 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
613                 pg->index = 0;
614                 pg = pg->next;
615         }
616
617         memset(stat->hash, 0,
618                FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
619 }
620
621 int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
622 {
623         struct ftrace_profile_page *pg;
624         int functions;
625         int pages;
626         int i;
627
628         /* If we already allocated, do nothing */
629         if (stat->pages)
630                 return 0;
631
632         stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
633         if (!stat->pages)
634                 return -ENOMEM;
635
636 #ifdef CONFIG_DYNAMIC_FTRACE
637         functions = ftrace_update_tot_cnt;
638 #else
639         /*
640          * We do not know the number of functions that exist because
641          * dynamic tracing is what counts them. With past experience
642          * we have around 20K functions. That should be more than enough.
643          * It is highly unlikely we will execute every function in
644          * the kernel.
645          */
646         functions = 20000;
647 #endif
648
649         pg = stat->start = stat->pages;
650
651         pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
652
653         for (i = 0; i < pages; i++) {
654                 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
655                 if (!pg->next)
656                         goto out_free;
657                 pg = pg->next;
658         }
659
660         return 0;
661
662  out_free:
663         pg = stat->start;
664         while (pg) {
665                 unsigned long tmp = (unsigned long)pg;
666
667                 pg = pg->next;
668                 free_page(tmp);
669         }
670
671         stat->pages = NULL;
672         stat->start = NULL;
673
674         return -ENOMEM;
675 }
676
677 static int ftrace_profile_init_cpu(int cpu)
678 {
679         struct ftrace_profile_stat *stat;
680         int size;
681
682         stat = &per_cpu(ftrace_profile_stats, cpu);
683
684         if (stat->hash) {
685                 /* If the profile is already created, simply reset it */
686                 ftrace_profile_reset(stat);
687                 return 0;
688         }
689
690         /*
691          * We are profiling all functions, but usually only a few thousand
692          * functions are hit. We'll make a hash of 1024 items.
693          */
694         size = FTRACE_PROFILE_HASH_SIZE;
695
696         stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
697
698         if (!stat->hash)
699                 return -ENOMEM;
700
701         if (!ftrace_profile_bits) {
702                 size--;
703
704                 for (; size; size >>= 1)
705                         ftrace_profile_bits++;
706         }
707
708         /* Preallocate the function profiling pages */
709         if (ftrace_profile_pages_init(stat) < 0) {
710                 kfree(stat->hash);
711                 stat->hash = NULL;
712                 return -ENOMEM;
713         }
714
715         return 0;
716 }
717
718 static int ftrace_profile_init(void)
719 {
720         int cpu;
721         int ret = 0;
722
723         for_each_online_cpu(cpu) {
724                 ret = ftrace_profile_init_cpu(cpu);
725                 if (ret)
726                         break;
727         }
728
729         return ret;
730 }
731
732 /* interrupts must be disabled */
733 static struct ftrace_profile *
734 ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
735 {
736         struct ftrace_profile *rec;
737         struct hlist_head *hhd;
738         struct hlist_node *n;
739         unsigned long key;
740
741         key = hash_long(ip, ftrace_profile_bits);
742         hhd = &stat->hash[key];
743
744         if (hlist_empty(hhd))
745                 return NULL;
746
747         hlist_for_each_entry_rcu(rec, n, hhd, node) {
748                 if (rec->ip == ip)
749                         return rec;
750         }
751
752         return NULL;
753 }
754
755 static void ftrace_add_profile(struct ftrace_profile_stat *stat,
756                                struct ftrace_profile *rec)
757 {
758         unsigned long key;
759
760         key = hash_long(rec->ip, ftrace_profile_bits);
761         hlist_add_head_rcu(&rec->node, &stat->hash[key]);
762 }
763
764 /*
765  * The memory is already allocated, this simply finds a new record to use.
766  */
767 static struct ftrace_profile *
768 ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
769 {
770         struct ftrace_profile *rec = NULL;
771
772         /* prevent recursion (from NMIs) */
773         if (atomic_inc_return(&stat->disabled) != 1)
774                 goto out;
775
776         /*
777          * Try to find the function again since an NMI
778          * could have added it
779          */
780         rec = ftrace_find_profiled_func(stat, ip);
781         if (rec)
782                 goto out;
783
784         if (stat->pages->index == PROFILES_PER_PAGE) {
785                 if (!stat->pages->next)
786                         goto out;
787                 stat->pages = stat->pages->next;
788         }
789
790         rec = &stat->pages->records[stat->pages->index++];
791         rec->ip = ip;
792         ftrace_add_profile(stat, rec);
793
794  out:
795         atomic_dec(&stat->disabled);
796
797         return rec;
798 }
799
800 static void
801 function_profile_call(unsigned long ip, unsigned long parent_ip,
802                       struct ftrace_ops *ops, struct pt_regs *regs)
803 {
804         struct ftrace_profile_stat *stat;
805         struct ftrace_profile *rec;
806         unsigned long flags;
807
808         if (!ftrace_profile_enabled)
809                 return;
810
811         local_irq_save(flags);
812
813         stat = &__get_cpu_var(ftrace_profile_stats);
814         if (!stat->hash || !ftrace_profile_enabled)
815                 goto out;
816
817         rec = ftrace_find_profiled_func(stat, ip);
818         if (!rec) {
819                 rec = ftrace_profile_alloc(stat, ip);
820                 if (!rec)
821                         goto out;
822         }
823
824         rec->counter++;
825  out:
826         local_irq_restore(flags);
827 }
828
829 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
830 static int profile_graph_entry(struct ftrace_graph_ent *trace)
831 {
832         function_profile_call(trace->func, 0, NULL, NULL);
833         return 1;
834 }
835
836 static void profile_graph_return(struct ftrace_graph_ret *trace)
837 {
838         struct ftrace_profile_stat *stat;
839         unsigned long long calltime;
840         struct ftrace_profile *rec;
841         unsigned long flags;
842
843         local_irq_save(flags);
844         stat = &__get_cpu_var(ftrace_profile_stats);
845         if (!stat->hash || !ftrace_profile_enabled)
846                 goto out;
847
848         /* If the calltime was zero'd ignore it */
849         if (!trace->calltime)
850                 goto out;
851
852         calltime = trace->rettime - trace->calltime;
853
854         if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
855                 int index;
856
857                 index = trace->depth;
858
859                 /* Append this call time to the parent time to subtract */
860                 if (index)
861                         current->ret_stack[index - 1].subtime += calltime;
862
863                 if (current->ret_stack[index].subtime < calltime)
864                         calltime -= current->ret_stack[index].subtime;
865                 else
866                         calltime = 0;
867         }
868
869         rec = ftrace_find_profiled_func(stat, trace->func);
870         if (rec) {
871                 rec->time += calltime;
872                 rec->time_squared += calltime * calltime;
873         }
874
875  out:
876         local_irq_restore(flags);
877 }
878
879 static int register_ftrace_profiler(void)
880 {
881         return register_ftrace_graph(&profile_graph_return,
882                                      &profile_graph_entry);
883 }
884
885 static void unregister_ftrace_profiler(void)
886 {
887         unregister_ftrace_graph();
888 }
889 #else
890 static struct ftrace_ops ftrace_profile_ops __read_mostly = {
891         .func           = function_profile_call,
892         .flags          = FTRACE_OPS_FL_RECURSION_SAFE,
893 };
894
895 static int register_ftrace_profiler(void)
896 {
897         return register_ftrace_function(&ftrace_profile_ops);
898 }
899
900 static void unregister_ftrace_profiler(void)
901 {
902         unregister_ftrace_function(&ftrace_profile_ops);
903 }
904 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
905
906 static ssize_t
907 ftrace_profile_write(struct file *filp, const char __user *ubuf,
908                      size_t cnt, loff_t *ppos)
909 {
910         unsigned long val;
911         int ret;
912
913         ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
914         if (ret)
915                 return ret;
916
917         val = !!val;
918
919         mutex_lock(&ftrace_profile_lock);
920         if (ftrace_profile_enabled ^ val) {
921                 if (val) {
922                         ret = ftrace_profile_init();
923                         if (ret < 0) {
924                                 cnt = ret;
925                                 goto out;
926                         }
927
928                         ret = register_ftrace_profiler();
929                         if (ret < 0) {
930                                 cnt = ret;
931                                 goto out;
932                         }
933                         ftrace_profile_enabled = 1;
934                 } else {
935                         ftrace_profile_enabled = 0;
936                         /*
937                          * unregister_ftrace_profiler calls stop_machine
938                          * so this acts like an synchronize_sched.
939                          */
940                         unregister_ftrace_profiler();
941                 }
942         }
943  out:
944         mutex_unlock(&ftrace_profile_lock);
945
946         *ppos += cnt;
947
948         return cnt;
949 }
950
951 static ssize_t
952 ftrace_profile_read(struct file *filp, char __user *ubuf,
953                      size_t cnt, loff_t *ppos)
954 {
955         char buf[64];           /* big enough to hold a number */
956         int r;
957
958         r = sprintf(buf, "%u\n", ftrace_profile_enabled);
959         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
960 }
961
962 static const struct file_operations ftrace_profile_fops = {
963         .open           = tracing_open_generic,
964         .read           = ftrace_profile_read,
965         .write          = ftrace_profile_write,
966         .llseek         = default_llseek,
967 };
968
969 /* used to initialize the real stat files */
970 static struct tracer_stat function_stats __initdata = {
971         .name           = "functions",
972         .stat_start     = function_stat_start,
973         .stat_next      = function_stat_next,
974         .stat_cmp       = function_stat_cmp,
975         .stat_headers   = function_stat_headers,
976         .stat_show      = function_stat_show
977 };
978
979 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
980 {
981         struct ftrace_profile_stat *stat;
982         struct dentry *entry;
983         char *name;
984         int ret;
985         int cpu;
986
987         for_each_possible_cpu(cpu) {
988                 stat = &per_cpu(ftrace_profile_stats, cpu);
989
990                 /* allocate enough for function name + cpu number */
991                 name = kmalloc(32, GFP_KERNEL);
992                 if (!name) {
993                         /*
994                          * The files created are permanent, if something happens
995                          * we still do not free memory.
996                          */
997                         WARN(1,
998                              "Could not allocate stat file for cpu %d\n",
999                              cpu);
1000                         return;
1001                 }
1002                 stat->stat = function_stats;
1003                 snprintf(name, 32, "function%d", cpu);
1004                 stat->stat.name = name;
1005                 ret = register_stat_tracer(&stat->stat);
1006                 if (ret) {
1007                         WARN(1,
1008                              "Could not register function stat for cpu %d\n",
1009                              cpu);
1010                         kfree(name);
1011                         return;
1012                 }
1013         }
1014
1015         entry = debugfs_create_file("function_profile_enabled", 0644,
1016                                     d_tracer, NULL, &ftrace_profile_fops);
1017         if (!entry)
1018                 pr_warning("Could not create debugfs "
1019                            "'function_profile_enabled' entry\n");
1020 }
1021
1022 #else /* CONFIG_FUNCTION_PROFILER */
1023 static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
1024 {
1025 }
1026 #endif /* CONFIG_FUNCTION_PROFILER */
1027
1028 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1029
1030 #ifdef CONFIG_DYNAMIC_FTRACE
1031
1032 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
1033 # error Dynamic ftrace depends on MCOUNT_RECORD
1034 #endif
1035
1036 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1037
1038 struct ftrace_func_probe {
1039         struct hlist_node       node;
1040         struct ftrace_probe_ops *ops;
1041         unsigned long           flags;
1042         unsigned long           ip;
1043         void                    *data;
1044         struct rcu_head         rcu;
1045 };
1046
1047 struct ftrace_func_entry {
1048         struct hlist_node hlist;
1049         unsigned long ip;
1050 };
1051
1052 struct ftrace_hash {
1053         unsigned long           size_bits;
1054         struct hlist_head       *buckets;
1055         unsigned long           count;
1056         struct rcu_head         rcu;
1057 };
1058
1059 /*
1060  * We make these constant because no one should touch them,
1061  * but they are used as the default "empty hash", to avoid allocating
1062  * it all the time. These are in a read only section such that if
1063  * anyone does try to modify it, it will cause an exception.
1064  */
1065 static const struct hlist_head empty_buckets[1];
1066 static const struct ftrace_hash empty_hash = {
1067         .buckets = (struct hlist_head *)empty_buckets,
1068 };
1069 #define EMPTY_HASH      ((struct ftrace_hash *)&empty_hash)
1070
1071 static struct ftrace_ops global_ops = {
1072         .func                   = ftrace_stub,
1073         .notrace_hash           = EMPTY_HASH,
1074         .filter_hash            = EMPTY_HASH,
1075         .flags                  = FTRACE_OPS_FL_RECURSION_SAFE,
1076 };
1077
1078 static DEFINE_MUTEX(ftrace_regex_lock);
1079
1080 struct ftrace_page {
1081         struct ftrace_page      *next;
1082         struct dyn_ftrace       *records;
1083         int                     index;
1084         int                     size;
1085 };
1086
1087 static struct ftrace_page *ftrace_new_pgs;
1088
1089 #define ENTRY_SIZE sizeof(struct dyn_ftrace)
1090 #define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
1091
1092 /* estimate from running different kernels */
1093 #define NR_TO_INIT              10000
1094
1095 static struct ftrace_page       *ftrace_pages_start;
1096 static struct ftrace_page       *ftrace_pages;
1097
1098 static bool ftrace_hash_empty(struct ftrace_hash *hash)
1099 {
1100         return !hash || !hash->count;
1101 }
1102
1103 static struct ftrace_func_entry *
1104 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1105 {
1106         unsigned long key;
1107         struct ftrace_func_entry *entry;
1108         struct hlist_head *hhd;
1109         struct hlist_node *n;
1110
1111         if (ftrace_hash_empty(hash))
1112                 return NULL;
1113
1114         if (hash->size_bits > 0)
1115                 key = hash_long(ip, hash->size_bits);
1116         else
1117                 key = 0;
1118
1119         hhd = &hash->buckets[key];
1120
1121         hlist_for_each_entry_rcu(entry, n, hhd, hlist) {
1122                 if (entry->ip == ip)
1123                         return entry;
1124         }
1125         return NULL;
1126 }
1127
1128 static void __add_hash_entry(struct ftrace_hash *hash,
1129                              struct ftrace_func_entry *entry)
1130 {
1131         struct hlist_head *hhd;
1132         unsigned long key;
1133
1134         if (hash->size_bits)
1135                 key = hash_long(entry->ip, hash->size_bits);
1136         else
1137                 key = 0;
1138
1139         hhd = &hash->buckets[key];
1140         hlist_add_head(&entry->hlist, hhd);
1141         hash->count++;
1142 }
1143
1144 static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1145 {
1146         struct ftrace_func_entry *entry;
1147
1148         entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1149         if (!entry)
1150                 return -ENOMEM;
1151
1152         entry->ip = ip;
1153         __add_hash_entry(hash, entry);
1154
1155         return 0;
1156 }
1157
1158 static void
1159 free_hash_entry(struct ftrace_hash *hash,
1160                   struct ftrace_func_entry *entry)
1161 {
1162         hlist_del(&entry->hlist);
1163         kfree(entry);
1164         hash->count--;
1165 }
1166
1167 static void
1168 remove_hash_entry(struct ftrace_hash *hash,
1169                   struct ftrace_func_entry *entry)
1170 {
1171         hlist_del(&entry->hlist);
1172         hash->count--;
1173 }
1174
1175 static void ftrace_hash_clear(struct ftrace_hash *hash)
1176 {
1177         struct hlist_head *hhd;
1178         struct hlist_node *tp, *tn;
1179         struct ftrace_func_entry *entry;
1180         int size = 1 << hash->size_bits;
1181         int i;
1182
1183         if (!hash->count)
1184                 return;
1185
1186         for (i = 0; i < size; i++) {
1187                 hhd = &hash->buckets[i];
1188                 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist)
1189                         free_hash_entry(hash, entry);
1190         }
1191         FTRACE_WARN_ON(hash->count);
1192 }
1193
1194 static void free_ftrace_hash(struct ftrace_hash *hash)
1195 {
1196         if (!hash || hash == EMPTY_HASH)
1197                 return;
1198         ftrace_hash_clear(hash);
1199         kfree(hash->buckets);
1200         kfree(hash);
1201 }
1202
1203 static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1204 {
1205         struct ftrace_hash *hash;
1206
1207         hash = container_of(rcu, struct ftrace_hash, rcu);
1208         free_ftrace_hash(hash);
1209 }
1210
1211 static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1212 {
1213         if (!hash || hash == EMPTY_HASH)
1214                 return;
1215         call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1216 }
1217
1218 void ftrace_free_filter(struct ftrace_ops *ops)
1219 {
1220         free_ftrace_hash(ops->filter_hash);
1221         free_ftrace_hash(ops->notrace_hash);
1222 }
1223
1224 static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1225 {
1226         struct ftrace_hash *hash;
1227         int size;
1228
1229         hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1230         if (!hash)
1231                 return NULL;
1232
1233         size = 1 << size_bits;
1234         hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
1235
1236         if (!hash->buckets) {
1237                 kfree(hash);
1238                 return NULL;
1239         }
1240
1241         hash->size_bits = size_bits;
1242
1243         return hash;
1244 }
1245
1246 static struct ftrace_hash *
1247 alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1248 {
1249         struct ftrace_func_entry *entry;
1250         struct ftrace_hash *new_hash;
1251         struct hlist_node *tp;
1252         int size;
1253         int ret;
1254         int i;
1255
1256         new_hash = alloc_ftrace_hash(size_bits);
1257         if (!new_hash)
1258                 return NULL;
1259
1260         /* Empty hash? */
1261         if (ftrace_hash_empty(hash))
1262                 return new_hash;
1263
1264         size = 1 << hash->size_bits;
1265         for (i = 0; i < size; i++) {
1266                 hlist_for_each_entry(entry, tp, &hash->buckets[i], hlist) {
1267                         ret = add_hash_entry(new_hash, entry->ip);
1268                         if (ret < 0)
1269                                 goto free_hash;
1270                 }
1271         }
1272
1273         FTRACE_WARN_ON(new_hash->count != hash->count);
1274
1275         return new_hash;
1276
1277  free_hash:
1278         free_ftrace_hash(new_hash);
1279         return NULL;
1280 }
1281
1282 static void
1283 ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1284 static void
1285 ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1286
1287 static int
1288 ftrace_hash_move(struct ftrace_ops *ops, int enable,
1289                  struct ftrace_hash **dst, struct ftrace_hash *src)
1290 {
1291         struct ftrace_func_entry *entry;
1292         struct hlist_node *tp, *tn;
1293         struct hlist_head *hhd;
1294         struct ftrace_hash *old_hash;
1295         struct ftrace_hash *new_hash;
1296         unsigned long key;
1297         int size = src->count;
1298         int bits = 0;
1299         int ret;
1300         int i;
1301
1302         /*
1303          * Remove the current set, update the hash and add
1304          * them back.
1305          */
1306         ftrace_hash_rec_disable(ops, enable);
1307
1308         /*
1309          * If the new source is empty, just free dst and assign it
1310          * the empty_hash.
1311          */
1312         if (!src->count) {
1313                 free_ftrace_hash_rcu(*dst);
1314                 rcu_assign_pointer(*dst, EMPTY_HASH);
1315                 /* still need to update the function records */
1316                 ret = 0;
1317                 goto out;
1318         }
1319
1320         /*
1321          * Make the hash size about 1/2 the # found
1322          */
1323         for (size /= 2; size; size >>= 1)
1324                 bits++;
1325
1326         /* Don't allocate too much */
1327         if (bits > FTRACE_HASH_MAX_BITS)
1328                 bits = FTRACE_HASH_MAX_BITS;
1329
1330         ret = -ENOMEM;
1331         new_hash = alloc_ftrace_hash(bits);
1332         if (!new_hash)
1333                 goto out;
1334
1335         size = 1 << src->size_bits;
1336         for (i = 0; i < size; i++) {
1337                 hhd = &src->buckets[i];
1338                 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist) {
1339                         if (bits > 0)
1340                                 key = hash_long(entry->ip, bits);
1341                         else
1342                                 key = 0;
1343                         remove_hash_entry(src, entry);
1344                         __add_hash_entry(new_hash, entry);
1345                 }
1346         }
1347
1348         old_hash = *dst;
1349         rcu_assign_pointer(*dst, new_hash);
1350         free_ftrace_hash_rcu(old_hash);
1351
1352         ret = 0;
1353  out:
1354         /*
1355          * Enable regardless of ret:
1356          *  On success, we enable the new hash.
1357          *  On failure, we re-enable the original hash.
1358          */
1359         ftrace_hash_rec_enable(ops, enable);
1360
1361         return ret;
1362 }
1363
1364 /*
1365  * Test the hashes for this ops to see if we want to call
1366  * the ops->func or not.
1367  *
1368  * It's a match if the ip is in the ops->filter_hash or
1369  * the filter_hash does not exist or is empty,
1370  *  AND
1371  * the ip is not in the ops->notrace_hash.
1372  *
1373  * This needs to be called with preemption disabled as
1374  * the hashes are freed with call_rcu_sched().
1375  */
1376 static int
1377 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
1378 {
1379         struct ftrace_hash *filter_hash;
1380         struct ftrace_hash *notrace_hash;
1381         int ret;
1382
1383         filter_hash = rcu_dereference_raw(ops->filter_hash);
1384         notrace_hash = rcu_dereference_raw(ops->notrace_hash);
1385
1386         if ((ftrace_hash_empty(filter_hash) ||
1387              ftrace_lookup_ip(filter_hash, ip)) &&
1388             (ftrace_hash_empty(notrace_hash) ||
1389              !ftrace_lookup_ip(notrace_hash, ip)))
1390                 ret = 1;
1391         else
1392                 ret = 0;
1393
1394         return ret;
1395 }
1396
1397 /*
1398  * This is a double for. Do not use 'break' to break out of the loop,
1399  * you must use a goto.
1400  */
1401 #define do_for_each_ftrace_rec(pg, rec)                                 \
1402         for (pg = ftrace_pages_start; pg; pg = pg->next) {              \
1403                 int _____i;                                             \
1404                 for (_____i = 0; _____i < pg->index; _____i++) {        \
1405                         rec = &pg->records[_____i];
1406
1407 #define while_for_each_ftrace_rec()             \
1408                 }                               \
1409         }
1410
1411
1412 static int ftrace_cmp_recs(const void *a, const void *b)
1413 {
1414         const struct dyn_ftrace *key = a;
1415         const struct dyn_ftrace *rec = b;
1416
1417         if (key->flags < rec->ip)
1418                 return -1;
1419         if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1420                 return 1;
1421         return 0;
1422 }
1423
1424 static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
1425 {
1426         struct ftrace_page *pg;
1427         struct dyn_ftrace *rec;
1428         struct dyn_ftrace key;
1429
1430         key.ip = start;
1431         key.flags = end;        /* overload flags, as it is unsigned long */
1432
1433         for (pg = ftrace_pages_start; pg; pg = pg->next) {
1434                 if (end < pg->records[0].ip ||
1435                     start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1436                         continue;
1437                 rec = bsearch(&key, pg->records, pg->index,
1438                               sizeof(struct dyn_ftrace),
1439                               ftrace_cmp_recs);
1440                 if (rec)
1441                         return rec->ip;
1442         }
1443
1444         return 0;
1445 }
1446
1447 /**
1448  * ftrace_location - return true if the ip giving is a traced location
1449  * @ip: the instruction pointer to check
1450  *
1451  * Returns rec->ip if @ip given is a pointer to a ftrace location.
1452  * That is, the instruction that is either a NOP or call to
1453  * the function tracer. It checks the ftrace internal tables to
1454  * determine if the address belongs or not.
1455  */
1456 unsigned long ftrace_location(unsigned long ip)
1457 {
1458         return ftrace_location_range(ip, ip);
1459 }
1460
1461 /**
1462  * ftrace_text_reserved - return true if range contains an ftrace location
1463  * @start: start of range to search
1464  * @end: end of range to search (inclusive). @end points to the last byte to check.
1465  *
1466  * Returns 1 if @start and @end contains a ftrace location.
1467  * That is, the instruction that is either a NOP or call to
1468  * the function tracer. It checks the ftrace internal tables to
1469  * determine if the address belongs or not.
1470  */
1471 int ftrace_text_reserved(void *start, void *end)
1472 {
1473         unsigned long ret;
1474
1475         ret = ftrace_location_range((unsigned long)start,
1476                                     (unsigned long)end);
1477
1478         return (int)!!ret;
1479 }
1480
1481 static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1482                                      int filter_hash,
1483                                      bool inc)
1484 {
1485         struct ftrace_hash *hash;
1486         struct ftrace_hash *other_hash;
1487         struct ftrace_page *pg;
1488         struct dyn_ftrace *rec;
1489         int count = 0;
1490         int all = 0;
1491
1492         /* Only update if the ops has been registered */
1493         if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1494                 return;
1495
1496         /*
1497          * In the filter_hash case:
1498          *   If the count is zero, we update all records.
1499          *   Otherwise we just update the items in the hash.
1500          *
1501          * In the notrace_hash case:
1502          *   We enable the update in the hash.
1503          *   As disabling notrace means enabling the tracing,
1504          *   and enabling notrace means disabling, the inc variable
1505          *   gets inversed.
1506          */
1507         if (filter_hash) {
1508                 hash = ops->filter_hash;
1509                 other_hash = ops->notrace_hash;
1510                 if (ftrace_hash_empty(hash))
1511                         all = 1;
1512         } else {
1513                 inc = !inc;
1514                 hash = ops->notrace_hash;
1515                 other_hash = ops->filter_hash;
1516                 /*
1517                  * If the notrace hash has no items,
1518                  * then there's nothing to do.
1519                  */
1520                 if (ftrace_hash_empty(hash))
1521                         return;
1522         }
1523
1524         do_for_each_ftrace_rec(pg, rec) {
1525                 int in_other_hash = 0;
1526                 int in_hash = 0;
1527                 int match = 0;
1528
1529                 if (all) {
1530                         /*
1531                          * Only the filter_hash affects all records.
1532                          * Update if the record is not in the notrace hash.
1533                          */
1534                         if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
1535                                 match = 1;
1536                 } else {
1537                         in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1538                         in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
1539
1540                         /*
1541                          *
1542                          */
1543                         if (filter_hash && in_hash && !in_other_hash)
1544                                 match = 1;
1545                         else if (!filter_hash && in_hash &&
1546                                  (in_other_hash || ftrace_hash_empty(other_hash)))
1547                                 match = 1;
1548                 }
1549                 if (!match)
1550                         continue;
1551
1552                 if (inc) {
1553                         rec->flags++;
1554                         if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1555                                 return;
1556                         /*
1557                          * If any ops wants regs saved for this function
1558                          * then all ops will get saved regs.
1559                          */
1560                         if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1561                                 rec->flags |= FTRACE_FL_REGS;
1562                 } else {
1563                         if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1564                                 return;
1565                         rec->flags--;
1566                 }
1567                 count++;
1568                 /* Shortcut, if we handled all records, we are done. */
1569                 if (!all && count == hash->count)
1570                         return;
1571         } while_for_each_ftrace_rec();
1572 }
1573
1574 static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1575                                     int filter_hash)
1576 {
1577         __ftrace_hash_rec_update(ops, filter_hash, 0);
1578 }
1579
1580 static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1581                                    int filter_hash)
1582 {
1583         __ftrace_hash_rec_update(ops, filter_hash, 1);
1584 }
1585
1586 static void print_ip_ins(const char *fmt, unsigned char *p)
1587 {
1588         int i;
1589
1590         printk(KERN_CONT "%s", fmt);
1591
1592         for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1593                 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1594 }
1595
1596 /**
1597  * ftrace_bug - report and shutdown function tracer
1598  * @failed: The failed type (EFAULT, EINVAL, EPERM)
1599  * @ip: The address that failed
1600  *
1601  * The arch code that enables or disables the function tracing
1602  * can call ftrace_bug() when it has detected a problem in
1603  * modifying the code. @failed should be one of either:
1604  * EFAULT - if the problem happens on reading the @ip address
1605  * EINVAL - if what is read at @ip is not what was expected
1606  * EPERM - if the problem happens on writting to the @ip address
1607  */
1608 void ftrace_bug(int failed, unsigned long ip)
1609 {
1610         switch (failed) {
1611         case -EFAULT:
1612                 FTRACE_WARN_ON_ONCE(1);
1613                 pr_info("ftrace faulted on modifying ");
1614                 print_ip_sym(ip);
1615                 break;
1616         case -EINVAL:
1617                 FTRACE_WARN_ON_ONCE(1);
1618                 pr_info("ftrace failed to modify ");
1619                 print_ip_sym(ip);
1620                 print_ip_ins(" actual: ", (unsigned char *)ip);
1621                 printk(KERN_CONT "\n");
1622                 break;
1623         case -EPERM:
1624                 FTRACE_WARN_ON_ONCE(1);
1625                 pr_info("ftrace faulted on writing ");
1626                 print_ip_sym(ip);
1627                 break;
1628         default:
1629                 FTRACE_WARN_ON_ONCE(1);
1630                 pr_info("ftrace faulted on unknown error ");
1631                 print_ip_sym(ip);
1632         }
1633 }
1634
1635 static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
1636 {
1637         unsigned long flag = 0UL;
1638
1639         /*
1640          * If we are updating calls:
1641          *
1642          *   If the record has a ref count, then we need to enable it
1643          *   because someone is using it.
1644          *
1645          *   Otherwise we make sure its disabled.
1646          *
1647          * If we are disabling calls, then disable all records that
1648          * are enabled.
1649          */
1650         if (enable && (rec->flags & ~FTRACE_FL_MASK))
1651                 flag = FTRACE_FL_ENABLED;
1652
1653         /*
1654          * If enabling and the REGS flag does not match the REGS_EN, then
1655          * do not ignore this record. Set flags to fail the compare against
1656          * ENABLED.
1657          */
1658         if (flag &&
1659             (!(rec->flags & FTRACE_FL_REGS) != !(rec->flags & FTRACE_FL_REGS_EN)))
1660                 flag |= FTRACE_FL_REGS;
1661
1662         /* If the state of this record hasn't changed, then do nothing */
1663         if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1664                 return FTRACE_UPDATE_IGNORE;
1665
1666         if (flag) {
1667                 /* Save off if rec is being enabled (for return value) */
1668                 flag ^= rec->flags & FTRACE_FL_ENABLED;
1669
1670                 if (update) {
1671                         rec->flags |= FTRACE_FL_ENABLED;
1672                         if (flag & FTRACE_FL_REGS) {
1673                                 if (rec->flags & FTRACE_FL_REGS)
1674                                         rec->flags |= FTRACE_FL_REGS_EN;
1675                                 else
1676                                         rec->flags &= ~FTRACE_FL_REGS_EN;
1677                         }
1678                 }
1679
1680                 /*
1681                  * If this record is being updated from a nop, then
1682                  *   return UPDATE_MAKE_CALL.
1683                  * Otherwise, if the EN flag is set, then return
1684                  *   UPDATE_MODIFY_CALL_REGS to tell the caller to convert
1685                  *   from the non-save regs, to a save regs function.
1686                  * Otherwise,
1687                  *   return UPDATE_MODIFY_CALL to tell the caller to convert
1688                  *   from the save regs, to a non-save regs function.
1689                  */
1690                 if (flag & FTRACE_FL_ENABLED)
1691                         return FTRACE_UPDATE_MAKE_CALL;
1692                 else if (rec->flags & FTRACE_FL_REGS_EN)
1693                         return FTRACE_UPDATE_MODIFY_CALL_REGS;
1694                 else
1695                         return FTRACE_UPDATE_MODIFY_CALL;
1696         }
1697
1698         if (update) {
1699                 /* If there's no more users, clear all flags */
1700                 if (!(rec->flags & ~FTRACE_FL_MASK))
1701                         rec->flags = 0;
1702                 else
1703                         /* Just disable the record (keep REGS state) */
1704                         rec->flags &= ~FTRACE_FL_ENABLED;
1705         }
1706
1707         return FTRACE_UPDATE_MAKE_NOP;
1708 }
1709
1710 /**
1711  * ftrace_update_record, set a record that now is tracing or not
1712  * @rec: the record to update
1713  * @enable: set to 1 if the record is tracing, zero to force disable
1714  *
1715  * The records that represent all functions that can be traced need
1716  * to be updated when tracing has been enabled.
1717  */
1718 int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1719 {
1720         return ftrace_check_record(rec, enable, 1);
1721 }
1722
1723 /**
1724  * ftrace_test_record, check if the record has been enabled or not
1725  * @rec: the record to test
1726  * @enable: set to 1 to check if enabled, 0 if it is disabled
1727  *
1728  * The arch code may need to test if a record is already set to
1729  * tracing to determine how to modify the function code that it
1730  * represents.
1731  */
1732 int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1733 {
1734         return ftrace_check_record(rec, enable, 0);
1735 }
1736
1737 static int
1738 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1739 {
1740         unsigned long ftrace_old_addr;
1741         unsigned long ftrace_addr;
1742         int ret;
1743
1744         ret = ftrace_update_record(rec, enable);
1745
1746         if (rec->flags & FTRACE_FL_REGS)
1747                 ftrace_addr = (unsigned long)FTRACE_REGS_ADDR;
1748         else
1749                 ftrace_addr = (unsigned long)FTRACE_ADDR;
1750
1751         switch (ret) {
1752         case FTRACE_UPDATE_IGNORE:
1753                 return 0;
1754
1755         case FTRACE_UPDATE_MAKE_CALL:
1756                 return ftrace_make_call(rec, ftrace_addr);
1757
1758         case FTRACE_UPDATE_MAKE_NOP:
1759                 return ftrace_make_nop(NULL, rec, ftrace_addr);
1760
1761         case FTRACE_UPDATE_MODIFY_CALL_REGS:
1762         case FTRACE_UPDATE_MODIFY_CALL:
1763                 if (rec->flags & FTRACE_FL_REGS)
1764                         ftrace_old_addr = (unsigned long)FTRACE_ADDR;
1765                 else
1766                         ftrace_old_addr = (unsigned long)FTRACE_REGS_ADDR;
1767
1768                 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
1769         }
1770
1771         return -1; /* unknow ftrace bug */
1772 }
1773
1774 void __weak ftrace_replace_code(int enable)
1775 {
1776         struct dyn_ftrace *rec;
1777         struct ftrace_page *pg;
1778         int failed;
1779
1780         if (unlikely(ftrace_disabled))
1781                 return;
1782
1783         do_for_each_ftrace_rec(pg, rec) {
1784                 failed = __ftrace_replace_code(rec, enable);
1785                 if (failed) {
1786                         ftrace_bug(failed, rec->ip);
1787                         /* Stop processing */
1788                         return;
1789                 }
1790         } while_for_each_ftrace_rec();
1791 }
1792
1793 struct ftrace_rec_iter {
1794         struct ftrace_page      *pg;
1795         int                     index;
1796 };
1797
1798 /**
1799  * ftrace_rec_iter_start, start up iterating over traced functions
1800  *
1801  * Returns an iterator handle that is used to iterate over all
1802  * the records that represent address locations where functions
1803  * are traced.
1804  *
1805  * May return NULL if no records are available.
1806  */
1807 struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1808 {
1809         /*
1810          * We only use a single iterator.
1811          * Protected by the ftrace_lock mutex.
1812          */
1813         static struct ftrace_rec_iter ftrace_rec_iter;
1814         struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1815
1816         iter->pg = ftrace_pages_start;
1817         iter->index = 0;
1818
1819         /* Could have empty pages */
1820         while (iter->pg && !iter->pg->index)
1821                 iter->pg = iter->pg->next;
1822
1823         if (!iter->pg)
1824                 return NULL;
1825
1826         return iter;
1827 }
1828
1829 /**
1830  * ftrace_rec_iter_next, get the next record to process.
1831  * @iter: The handle to the iterator.
1832  *
1833  * Returns the next iterator after the given iterator @iter.
1834  */
1835 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1836 {
1837         iter->index++;
1838
1839         if (iter->index >= iter->pg->index) {
1840                 iter->pg = iter->pg->next;
1841                 iter->index = 0;
1842
1843                 /* Could have empty pages */
1844                 while (iter->pg && !iter->pg->index)
1845                         iter->pg = iter->pg->next;
1846         }
1847
1848         if (!iter->pg)
1849                 return NULL;
1850
1851         return iter;
1852 }
1853
1854 /**
1855  * ftrace_rec_iter_record, get the record at the iterator location
1856  * @iter: The current iterator location
1857  *
1858  * Returns the record that the current @iter is at.
1859  */
1860 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1861 {
1862         return &iter->pg->records[iter->index];
1863 }
1864
1865 static int
1866 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
1867 {
1868         unsigned long ip;
1869         int ret;
1870
1871         ip = rec->ip;
1872
1873         if (unlikely(ftrace_disabled))
1874                 return 0;
1875
1876         ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
1877         if (ret) {
1878                 ftrace_bug(ret, ip);
1879                 return 0;
1880         }
1881         return 1;
1882 }
1883
1884 /*
1885  * archs can override this function if they must do something
1886  * before the modifying code is performed.
1887  */
1888 int __weak ftrace_arch_code_modify_prepare(void)
1889 {
1890         return 0;
1891 }
1892
1893 /*
1894  * archs can override this function if they must do something
1895  * after the modifying code is performed.
1896  */
1897 int __weak ftrace_arch_code_modify_post_process(void)
1898 {
1899         return 0;
1900 }
1901
1902 void ftrace_modify_all_code(int command)
1903 {
1904         if (command & FTRACE_UPDATE_CALLS)
1905                 ftrace_replace_code(1);
1906         else if (command & FTRACE_DISABLE_CALLS)
1907                 ftrace_replace_code(0);
1908
1909         if (command & FTRACE_UPDATE_TRACE_FUNC)
1910                 ftrace_update_ftrace_func(ftrace_trace_function);
1911
1912         if (command & FTRACE_START_FUNC_RET)
1913                 ftrace_enable_ftrace_graph_caller();
1914         else if (command & FTRACE_STOP_FUNC_RET)
1915                 ftrace_disable_ftrace_graph_caller();
1916 }
1917
1918 static int __ftrace_modify_code(void *data)
1919 {
1920         int *command = data;
1921
1922         ftrace_modify_all_code(*command);
1923
1924         return 0;
1925 }
1926
1927 /**
1928  * ftrace_run_stop_machine, go back to the stop machine method
1929  * @command: The command to tell ftrace what to do
1930  *
1931  * If an arch needs to fall back to the stop machine method, the
1932  * it can call this function.
1933  */
1934 void ftrace_run_stop_machine(int command)
1935 {
1936         stop_machine(__ftrace_modify_code, &command, NULL);
1937 }
1938
1939 /**
1940  * arch_ftrace_update_code, modify the code to trace or not trace
1941  * @command: The command that needs to be done
1942  *
1943  * Archs can override this function if it does not need to
1944  * run stop_machine() to modify code.
1945  */
1946 void __weak arch_ftrace_update_code(int command)
1947 {
1948         ftrace_run_stop_machine(command);
1949 }
1950
1951 static void ftrace_run_update_code(int command)
1952 {
1953         int ret;
1954
1955         ret = ftrace_arch_code_modify_prepare();
1956         FTRACE_WARN_ON(ret);
1957         if (ret)
1958                 return;
1959         /*
1960          * Do not call function tracer while we update the code.
1961          * We are in stop machine.
1962          */
1963         function_trace_stop++;
1964
1965         /*
1966          * By default we use stop_machine() to modify the code.
1967          * But archs can do what ever they want as long as it
1968          * is safe. The stop_machine() is the safest, but also
1969          * produces the most overhead.
1970          */
1971         arch_ftrace_update_code(command);
1972
1973         function_trace_stop--;
1974
1975         ret = ftrace_arch_code_modify_post_process();
1976         FTRACE_WARN_ON(ret);
1977 }
1978
1979 static ftrace_func_t saved_ftrace_func;
1980 static int ftrace_start_up;
1981 static int global_start_up;
1982
1983 static void ftrace_startup_enable(int command)
1984 {
1985         if (saved_ftrace_func != ftrace_trace_function) {
1986                 saved_ftrace_func = ftrace_trace_function;
1987                 command |= FTRACE_UPDATE_TRACE_FUNC;
1988         }
1989
1990         if (!command || !ftrace_enabled)
1991                 return;
1992
1993         ftrace_run_update_code(command);
1994 }
1995
1996 static int ftrace_startup(struct ftrace_ops *ops, int command)
1997 {
1998         bool hash_enable = true;
1999
2000         if (unlikely(ftrace_disabled))
2001                 return -ENODEV;
2002
2003         ftrace_start_up++;
2004         command |= FTRACE_UPDATE_CALLS;
2005
2006         /* ops marked global share the filter hashes */
2007         if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2008                 ops = &global_ops;
2009                 /* Don't update hash if global is already set */
2010                 if (global_start_up)
2011                         hash_enable = false;
2012                 global_start_up++;
2013         }
2014
2015         ops->flags |= FTRACE_OPS_FL_ENABLED;
2016         if (hash_enable)
2017                 ftrace_hash_rec_enable(ops, 1);
2018
2019         ftrace_startup_enable(command);
2020
2021         return 0;
2022 }
2023
2024 static void ftrace_shutdown(struct ftrace_ops *ops, int command)
2025 {
2026         bool hash_disable = true;
2027
2028         if (unlikely(ftrace_disabled))
2029                 return;
2030
2031         ftrace_start_up--;
2032         /*
2033          * Just warn in case of unbalance, no need to kill ftrace, it's not
2034          * critical but the ftrace_call callers may be never nopped again after
2035          * further ftrace uses.
2036          */
2037         WARN_ON_ONCE(ftrace_start_up < 0);
2038
2039         if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2040                 ops = &global_ops;
2041                 global_start_up--;
2042                 WARN_ON_ONCE(global_start_up < 0);
2043                 /* Don't update hash if global still has users */
2044                 if (global_start_up) {
2045                         WARN_ON_ONCE(!ftrace_start_up);
2046                         hash_disable = false;
2047                 }
2048         }
2049
2050         if (hash_disable)
2051                 ftrace_hash_rec_disable(ops, 1);
2052
2053         if (ops != &global_ops || !global_start_up)
2054                 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
2055
2056         command |= FTRACE_UPDATE_CALLS;
2057
2058         if (saved_ftrace_func != ftrace_trace_function) {
2059                 saved_ftrace_func = ftrace_trace_function;
2060                 command |= FTRACE_UPDATE_TRACE_FUNC;
2061         }
2062
2063         if (!command || !ftrace_enabled)
2064                 return;
2065
2066         ftrace_run_update_code(command);
2067 }
2068
2069 static void ftrace_startup_sysctl(void)
2070 {
2071         if (unlikely(ftrace_disabled))
2072                 return;
2073
2074         /* Force update next time */
2075         saved_ftrace_func = NULL;
2076         /* ftrace_start_up is true if we want ftrace running */
2077         if (ftrace_start_up)
2078                 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
2079 }
2080
2081 static void ftrace_shutdown_sysctl(void)
2082 {
2083         if (unlikely(ftrace_disabled))
2084                 return;
2085
2086         /* ftrace_start_up is true if ftrace is running */
2087         if (ftrace_start_up)
2088                 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
2089 }
2090
2091 static cycle_t          ftrace_update_time;
2092 static unsigned long    ftrace_update_cnt;
2093 unsigned long           ftrace_update_tot_cnt;
2094
2095 static int ops_traces_mod(struct ftrace_ops *ops)
2096 {
2097         struct ftrace_hash *hash;
2098
2099         hash = ops->filter_hash;
2100         return ftrace_hash_empty(hash);
2101 }
2102
2103 static int ftrace_update_code(struct module *mod)
2104 {
2105         struct ftrace_page *pg;
2106         struct dyn_ftrace *p;
2107         cycle_t start, stop;
2108         unsigned long ref = 0;
2109         int i;
2110
2111         /*
2112          * When adding a module, we need to check if tracers are
2113          * currently enabled and if they are set to trace all functions.
2114          * If they are, we need to enable the module functions as well
2115          * as update the reference counts for those function records.
2116          */
2117         if (mod) {
2118                 struct ftrace_ops *ops;
2119
2120                 for (ops = ftrace_ops_list;
2121                      ops != &ftrace_list_end; ops = ops->next) {
2122                         if (ops->flags & FTRACE_OPS_FL_ENABLED &&
2123                             ops_traces_mod(ops))
2124                                 ref++;
2125                 }
2126         }
2127
2128         start = ftrace_now(raw_smp_processor_id());
2129         ftrace_update_cnt = 0;
2130
2131         for (pg = ftrace_new_pgs; pg; pg = pg->next) {
2132
2133                 for (i = 0; i < pg->index; i++) {
2134                         /* If something went wrong, bail without enabling anything */
2135                         if (unlikely(ftrace_disabled))
2136                                 return -1;
2137
2138                         p = &pg->records[i];
2139                         p->flags = ref;
2140
2141                         /*
2142                          * Do the initial record conversion from mcount jump
2143                          * to the NOP instructions.
2144                          */
2145                         if (!ftrace_code_disable(mod, p))
2146                                 break;
2147
2148                         ftrace_update_cnt++;
2149
2150                         /*
2151                          * If the tracing is enabled, go ahead and enable the record.
2152                          *
2153                          * The reason not to enable the record immediatelly is the
2154                          * inherent check of ftrace_make_nop/ftrace_make_call for
2155                          * correct previous instructions.  Making first the NOP
2156                          * conversion puts the module to the correct state, thus
2157                          * passing the ftrace_make_call check.
2158                          */
2159                         if (ftrace_start_up && ref) {
2160                                 int failed = __ftrace_replace_code(p, 1);
2161                                 if (failed)
2162                                         ftrace_bug(failed, p->ip);
2163                         }
2164                 }
2165         }
2166
2167         ftrace_new_pgs = NULL;
2168
2169         stop = ftrace_now(raw_smp_processor_id());
2170         ftrace_update_time = stop - start;
2171         ftrace_update_tot_cnt += ftrace_update_cnt;
2172
2173         return 0;
2174 }
2175
2176 static int ftrace_allocate_records(struct ftrace_page *pg, int count)
2177 {
2178         int order;
2179         int cnt;
2180
2181         if (WARN_ON(!count))
2182                 return -EINVAL;
2183
2184         order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
2185
2186         /*
2187          * We want to fill as much as possible. No more than a page
2188          * may be empty.
2189          */
2190         while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2191                 order--;
2192
2193  again:
2194         pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2195
2196         if (!pg->records) {
2197                 /* if we can't allocate this size, try something smaller */
2198                 if (!order)
2199                         return -ENOMEM;
2200                 order >>= 1;
2201                 goto again;
2202         }
2203
2204         cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2205         pg->size = cnt;
2206
2207         if (cnt > count)
2208                 cnt = count;
2209
2210         return cnt;
2211 }
2212
2213 static struct ftrace_page *
2214 ftrace_allocate_pages(unsigned long num_to_init)
2215 {
2216         struct ftrace_page *start_pg;
2217         struct ftrace_page *pg;
2218         int order;
2219         int cnt;
2220
2221         if (!num_to_init)
2222                 return 0;
2223
2224         start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2225         if (!pg)
2226                 return NULL;
2227
2228         /*
2229          * Try to allocate as much as possible in one continues
2230          * location that fills in all of the space. We want to
2231          * waste as little space as possible.
2232          */
2233         for (;;) {
2234                 cnt = ftrace_allocate_records(pg, num_to_init);
2235                 if (cnt < 0)
2236                         goto free_pages;
2237
2238                 num_to_init -= cnt;
2239                 if (!num_to_init)
2240                         break;
2241
2242                 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2243                 if (!pg->next)
2244                         goto free_pages;
2245
2246                 pg = pg->next;
2247         }
2248
2249         return start_pg;
2250
2251  free_pages:
2252         while (start_pg) {
2253                 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2254                 free_pages((unsigned long)pg->records, order);
2255                 start_pg = pg->next;
2256                 kfree(pg);
2257                 pg = start_pg;
2258         }
2259         pr_info("ftrace: FAILED to allocate memory for functions\n");
2260         return NULL;
2261 }
2262
2263 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2264 {
2265         int cnt;
2266
2267         if (!num_to_init) {
2268                 pr_info("ftrace: No functions to be traced?\n");
2269                 return -1;
2270         }
2271
2272         cnt = num_to_init / ENTRIES_PER_PAGE;
2273         pr_info("ftrace: allocating %ld entries in %d pages\n",
2274                 num_to_init, cnt + 1);
2275
2276         return 0;
2277 }
2278
2279 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2280
2281 struct ftrace_iterator {
2282         loff_t                          pos;
2283         loff_t                          func_pos;
2284         struct ftrace_page              *pg;
2285         struct dyn_ftrace               *func;
2286         struct ftrace_func_probe        *probe;
2287         struct trace_parser             parser;
2288         struct ftrace_hash              *hash;
2289         struct ftrace_ops               *ops;
2290         int                             hidx;
2291         int                             idx;
2292         unsigned                        flags;
2293 };
2294
2295 static void *
2296 t_hash_next(struct seq_file *m, loff_t *pos)
2297 {
2298         struct ftrace_iterator *iter = m->private;
2299         struct hlist_node *hnd = NULL;
2300         struct hlist_head *hhd;
2301
2302         (*pos)++;
2303         iter->pos = *pos;
2304
2305         if (iter->probe)
2306                 hnd = &iter->probe->node;
2307  retry:
2308         if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2309                 return NULL;
2310
2311         hhd = &ftrace_func_hash[iter->hidx];
2312
2313         if (hlist_empty(hhd)) {
2314                 iter->hidx++;
2315                 hnd = NULL;
2316                 goto retry;
2317         }
2318
2319         if (!hnd)
2320                 hnd = hhd->first;
2321         else {
2322                 hnd = hnd->next;
2323                 if (!hnd) {
2324                         iter->hidx++;
2325                         goto retry;
2326                 }
2327         }
2328
2329         if (WARN_ON_ONCE(!hnd))
2330                 return NULL;
2331
2332         iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2333
2334         return iter;
2335 }
2336
2337 static void *t_hash_start(struct seq_file *m, loff_t *pos)
2338 {
2339         struct ftrace_iterator *iter = m->private;
2340         void *p = NULL;
2341         loff_t l;
2342
2343         if (!(iter->flags & FTRACE_ITER_DO_HASH))
2344                 return NULL;
2345
2346         if (iter->func_pos > *pos)
2347                 return NULL;
2348
2349         iter->hidx = 0;
2350         for (l = 0; l <= (*pos - iter->func_pos); ) {
2351                 p = t_hash_next(m, &l);
2352                 if (!p)
2353                         break;
2354         }
2355         if (!p)
2356                 return NULL;
2357
2358         /* Only set this if we have an item */
2359         iter->flags |= FTRACE_ITER_HASH;
2360
2361         return iter;
2362 }
2363
2364 static int
2365 t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
2366 {
2367         struct ftrace_func_probe *rec;
2368
2369         rec = iter->probe;
2370         if (WARN_ON_ONCE(!rec))
2371                 return -EIO;
2372
2373         if (rec->ops->print)
2374                 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2375
2376         seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
2377
2378         if (rec->data)
2379                 seq_printf(m, ":%p", rec->data);
2380         seq_putc(m, '\n');
2381
2382         return 0;
2383 }
2384
2385 static void *
2386 t_next(struct seq_file *m, void *v, loff_t *pos)
2387 {
2388         struct ftrace_iterator *iter = m->private;
2389         struct ftrace_ops *ops = iter->ops;
2390         struct dyn_ftrace *rec = NULL;
2391
2392         if (unlikely(ftrace_disabled))
2393                 return NULL;
2394
2395         if (iter->flags & FTRACE_ITER_HASH)
2396                 return t_hash_next(m, pos);
2397
2398         (*pos)++;
2399         iter->pos = iter->func_pos = *pos;
2400
2401         if (iter->flags & FTRACE_ITER_PRINTALL)
2402                 return t_hash_start(m, pos);
2403
2404  retry:
2405         if (iter->idx >= iter->pg->index) {
2406                 if (iter->pg->next) {
2407                         iter->pg = iter->pg->next;
2408                         iter->idx = 0;
2409                         goto retry;
2410                 }
2411         } else {
2412                 rec = &iter->pg->records[iter->idx++];
2413                 if (((iter->flags & FTRACE_ITER_FILTER) &&
2414                      !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
2415
2416                     ((iter->flags & FTRACE_ITER_NOTRACE) &&
2417                      !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2418
2419                     ((iter->flags & FTRACE_ITER_ENABLED) &&
2420                      !(rec->flags & ~FTRACE_FL_MASK))) {
2421
2422                         rec = NULL;
2423                         goto retry;
2424                 }
2425         }
2426
2427         if (!rec)
2428                 return t_hash_start(m, pos);
2429
2430         iter->func = rec;
2431
2432         return iter;
2433 }
2434
2435 static void reset_iter_read(struct ftrace_iterator *iter)
2436 {
2437         iter->pos = 0;
2438         iter->func_pos = 0;
2439         iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
2440 }
2441
2442 static void *t_start(struct seq_file *m, loff_t *pos)
2443 {
2444         struct ftrace_iterator *iter = m->private;
2445         struct ftrace_ops *ops = iter->ops;
2446         void *p = NULL;
2447         loff_t l;
2448
2449         mutex_lock(&ftrace_lock);
2450
2451         if (unlikely(ftrace_disabled))
2452                 return NULL;
2453
2454         /*
2455          * If an lseek was done, then reset and start from beginning.
2456          */
2457         if (*pos < iter->pos)
2458                 reset_iter_read(iter);
2459
2460         /*
2461          * For set_ftrace_filter reading, if we have the filter
2462          * off, we can short cut and just print out that all
2463          * functions are enabled.
2464          */
2465         if (iter->flags & FTRACE_ITER_FILTER &&
2466             ftrace_hash_empty(ops->filter_hash)) {
2467                 if (*pos > 0)
2468                         return t_hash_start(m, pos);
2469                 iter->flags |= FTRACE_ITER_PRINTALL;
2470                 /* reset in case of seek/pread */
2471                 iter->flags &= ~FTRACE_ITER_HASH;
2472                 return iter;
2473         }
2474
2475         if (iter->flags & FTRACE_ITER_HASH)
2476                 return t_hash_start(m, pos);
2477
2478         /*
2479          * Unfortunately, we need to restart at ftrace_pages_start
2480          * every time we let go of the ftrace_mutex. This is because
2481          * those pointers can change without the lock.
2482          */
2483         iter->pg = ftrace_pages_start;
2484         iter->idx = 0;
2485         for (l = 0; l <= *pos; ) {
2486                 p = t_next(m, p, &l);
2487                 if (!p)
2488                         break;
2489         }
2490
2491         if (!p)
2492                 return t_hash_start(m, pos);
2493
2494         return iter;
2495 }
2496
2497 static void t_stop(struct seq_file *m, void *p)
2498 {
2499         mutex_unlock(&ftrace_lock);
2500 }
2501
2502 static int t_show(struct seq_file *m, void *v)
2503 {
2504         struct ftrace_iterator *iter = m->private;
2505         struct dyn_ftrace *rec;
2506
2507         if (iter->flags & FTRACE_ITER_HASH)
2508                 return t_hash_show(m, iter);
2509
2510         if (iter->flags & FTRACE_ITER_PRINTALL) {
2511                 seq_printf(m, "#### all functions enabled ####\n");
2512                 return 0;
2513         }
2514
2515         rec = iter->func;
2516
2517         if (!rec)
2518                 return 0;
2519
2520         seq_printf(m, "%ps", (void *)rec->ip);
2521         if (iter->flags & FTRACE_ITER_ENABLED)
2522                 seq_printf(m, " (%ld)%s",
2523                            rec->flags & ~FTRACE_FL_MASK,
2524                            rec->flags & FTRACE_FL_REGS ? " R" : "");
2525         seq_printf(m, "\n");
2526
2527         return 0;
2528 }
2529
2530 static const struct seq_operations show_ftrace_seq_ops = {
2531         .start = t_start,
2532         .next = t_next,
2533         .stop = t_stop,
2534         .show = t_show,
2535 };
2536
2537 static int
2538 ftrace_avail_open(struct inode *inode, struct file *file)
2539 {
2540         struct ftrace_iterator *iter;
2541
2542         if (unlikely(ftrace_disabled))
2543                 return -ENODEV;
2544
2545         iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2546         if (iter) {
2547                 iter->pg = ftrace_pages_start;
2548                 iter->ops = &global_ops;
2549         }
2550
2551         return iter ? 0 : -ENOMEM;
2552 }
2553
2554 static int
2555 ftrace_enabled_open(struct inode *inode, struct file *file)
2556 {
2557         struct ftrace_iterator *iter;
2558
2559         if (unlikely(ftrace_disabled))
2560                 return -ENODEV;
2561
2562         iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2563         if (iter) {
2564                 iter->pg = ftrace_pages_start;
2565                 iter->flags = FTRACE_ITER_ENABLED;
2566                 iter->ops = &global_ops;
2567         }
2568
2569         return iter ? 0 : -ENOMEM;
2570 }
2571
2572 static void ftrace_filter_reset(struct ftrace_hash *hash)
2573 {
2574         mutex_lock(&ftrace_lock);
2575         ftrace_hash_clear(hash);
2576         mutex_unlock(&ftrace_lock);
2577 }
2578
2579 /**
2580  * ftrace_regex_open - initialize function tracer filter files
2581  * @ops: The ftrace_ops that hold the hash filters
2582  * @flag: The type of filter to process
2583  * @inode: The inode, usually passed in to your open routine
2584  * @file: The file, usually passed in to your open routine
2585  *
2586  * ftrace_regex_open() initializes the filter files for the
2587  * @ops. Depending on @flag it may process the filter hash or
2588  * the notrace hash of @ops. With this called from the open
2589  * routine, you can use ftrace_filter_write() for the write
2590  * routine if @flag has FTRACE_ITER_FILTER set, or
2591  * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
2592  * ftrace_regex_lseek() should be used as the lseek routine, and
2593  * release must call ftrace_regex_release().
2594  */
2595 int
2596 ftrace_regex_open(struct ftrace_ops *ops, int flag,
2597                   struct inode *inode, struct file *file)
2598 {
2599         struct ftrace_iterator *iter;
2600         struct ftrace_hash *hash;
2601         int ret = 0;
2602
2603         if (unlikely(ftrace_disabled))
2604                 return -ENODEV;
2605
2606         iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2607         if (!iter)
2608                 return -ENOMEM;
2609
2610         if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2611                 kfree(iter);
2612                 return -ENOMEM;
2613         }
2614
2615         if (flag & FTRACE_ITER_NOTRACE)
2616                 hash = ops->notrace_hash;
2617         else
2618                 hash = ops->filter_hash;
2619
2620         iter->ops = ops;
2621         iter->flags = flag;
2622
2623         if (file->f_mode & FMODE_WRITE) {
2624                 mutex_lock(&ftrace_lock);
2625                 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2626                 mutex_unlock(&ftrace_lock);
2627
2628                 if (!iter->hash) {
2629                         trace_parser_put(&iter->parser);
2630                         kfree(iter);
2631                         return -ENOMEM;
2632                 }
2633         }
2634
2635         mutex_lock(&ftrace_regex_lock);
2636
2637         if ((file->f_mode & FMODE_WRITE) &&
2638             (file->f_flags & O_TRUNC))
2639                 ftrace_filter_reset(iter->hash);
2640
2641         if (file->f_mode & FMODE_READ) {
2642                 iter->pg = ftrace_pages_start;
2643
2644                 ret = seq_open(file, &show_ftrace_seq_ops);
2645                 if (!ret) {
2646                         struct seq_file *m = file->private_data;
2647                         m->private = iter;
2648                 } else {
2649                         /* Failed */
2650                         free_ftrace_hash(iter->hash);
2651                         trace_parser_put(&iter->parser);
2652                         kfree(iter);
2653                 }
2654         } else
2655                 file->private_data = iter;
2656         mutex_unlock(&ftrace_regex_lock);
2657
2658         return ret;
2659 }
2660
2661 static int
2662 ftrace_filter_open(struct inode *inode, struct file *file)
2663 {
2664         return ftrace_regex_open(&global_ops,
2665                         FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2666                         inode, file);
2667 }
2668
2669 static int
2670 ftrace_notrace_open(struct inode *inode, struct file *file)
2671 {
2672         return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
2673                                  inode, file);
2674 }
2675
2676 loff_t
2677 ftrace_filter_lseek(struct file *file, loff_t offset, int whence)
2678 {
2679         loff_t ret;
2680
2681         if (file->f_mode & FMODE_READ)
2682                 ret = seq_lseek(file, offset, whence);
2683         else
2684                 file->f_pos = ret = 1;
2685
2686         return ret;
2687 }
2688
2689 static int ftrace_match(char *str, char *regex, int len, int type)
2690 {
2691         int matched = 0;
2692         int slen;
2693
2694         switch (type) {
2695         case MATCH_FULL:
2696                 if (strcmp(str, regex) == 0)
2697                         matched = 1;
2698                 break;
2699         case MATCH_FRONT_ONLY:
2700                 if (strncmp(str, regex, len) == 0)
2701                         matched = 1;
2702                 break;
2703         case MATCH_MIDDLE_ONLY:
2704                 if (strstr(str, regex))
2705                         matched = 1;
2706                 break;
2707         case MATCH_END_ONLY:
2708                 slen = strlen(str);
2709                 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
2710                         matched = 1;
2711                 break;
2712         }
2713
2714         return matched;
2715 }
2716
2717 static int
2718 enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
2719 {
2720         struct ftrace_func_entry *entry;
2721         int ret = 0;
2722
2723         entry = ftrace_lookup_ip(hash, rec->ip);
2724         if (not) {
2725                 /* Do nothing if it doesn't exist */
2726                 if (!entry)
2727                         return 0;
2728
2729                 free_hash_entry(hash, entry);
2730         } else {
2731                 /* Do nothing if it exists */
2732                 if (entry)
2733                         return 0;
2734
2735                 ret = add_hash_entry(hash, rec->ip);
2736         }
2737         return ret;
2738 }
2739
2740 static int
2741 ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2742                     char *regex, int len, int type)
2743 {
2744         char str[KSYM_SYMBOL_LEN];
2745         char *modname;
2746
2747         kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2748
2749         if (mod) {
2750                 /* module lookup requires matching the module */
2751                 if (!modname || strcmp(modname, mod))
2752                         return 0;
2753
2754                 /* blank search means to match all funcs in the mod */
2755                 if (!len)
2756                         return 1;
2757         }
2758
2759         return ftrace_match(str, regex, len, type);
2760 }
2761
2762 static int
2763 match_records(struct ftrace_hash *hash, char *buff,
2764               int len, char *mod, int not)
2765 {
2766         unsigned search_len = 0;
2767         struct ftrace_page *pg;
2768         struct dyn_ftrace *rec;
2769         int type = MATCH_FULL;
2770         char *search = buff;
2771         int found = 0;
2772         int ret;
2773
2774         if (len) {
2775                 type = filter_parse_regex(buff, len, &search, &not);
2776                 search_len = strlen(search);
2777         }
2778
2779         mutex_lock(&ftrace_lock);
2780
2781         if (unlikely(ftrace_disabled))
2782                 goto out_unlock;
2783
2784         do_for_each_ftrace_rec(pg, rec) {
2785                 if (ftrace_match_record(rec, mod, search, search_len, type)) {
2786                         ret = enter_record(hash, rec, not);
2787                         if (ret < 0) {
2788                                 found = ret;
2789                                 goto out_unlock;
2790                         }
2791                         found = 1;
2792                 }
2793         } while_for_each_ftrace_rec();
2794  out_unlock:
2795         mutex_unlock(&ftrace_lock);
2796
2797         return found;
2798 }
2799
2800 static int
2801 ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
2802 {
2803         return match_records(hash, buff, len, NULL, 0);
2804 }
2805
2806 static int
2807 ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
2808 {
2809         int not = 0;
2810
2811         /* blank or '*' mean the same */
2812         if (strcmp(buff, "*") == 0)
2813                 buff[0] = 0;
2814
2815         /* handle the case of 'dont filter this module' */
2816         if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2817                 buff[0] = 0;
2818                 not = 1;
2819         }
2820
2821         return match_records(hash, buff, strlen(buff), mod, not);
2822 }
2823
2824 /*
2825  * We register the module command as a template to show others how
2826  * to register the a command as well.
2827  */
2828
2829 static int
2830 ftrace_mod_callback(struct ftrace_hash *hash,
2831                     char *func, char *cmd, char *param, int enable)
2832 {
2833         char *mod;
2834         int ret = -EINVAL;
2835
2836         /*
2837          * cmd == 'mod' because we only registered this func
2838          * for the 'mod' ftrace_func_command.
2839          * But if you register one func with multiple commands,
2840          * you can tell which command was used by the cmd
2841          * parameter.
2842          */
2843
2844         /* we must have a module name */
2845         if (!param)
2846                 return ret;
2847
2848         mod = strsep(&param, ":");
2849         if (!strlen(mod))
2850                 return ret;
2851
2852         ret = ftrace_match_module_records(hash, func, mod);
2853         if (!ret)
2854                 ret = -EINVAL;
2855         if (ret < 0)
2856                 return ret;
2857
2858         return 0;
2859 }
2860
2861 static struct ftrace_func_command ftrace_mod_cmd = {
2862         .name                   = "mod",
2863         .func                   = ftrace_mod_callback,
2864 };
2865
2866 static int __init ftrace_mod_cmd_init(void)
2867 {
2868         return register_ftrace_command(&ftrace_mod_cmd);
2869 }
2870 core_initcall(ftrace_mod_cmd_init);
2871
2872 static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
2873                                       struct ftrace_ops *op, struct pt_regs *pt_regs)
2874 {
2875         struct ftrace_func_probe *entry;
2876         struct hlist_head *hhd;
2877         struct hlist_node *n;
2878         unsigned long key;
2879
2880         key = hash_long(ip, FTRACE_HASH_BITS);
2881
2882         hhd = &ftrace_func_hash[key];
2883
2884         if (hlist_empty(hhd))
2885                 return;
2886
2887         /*
2888          * Disable preemption for these calls to prevent a RCU grace
2889          * period. This syncs the hash iteration and freeing of items
2890          * on the hash. rcu_read_lock is too dangerous here.
2891          */
2892         preempt_disable_notrace();
2893         hlist_for_each_entry_rcu(entry, n, hhd, node) {
2894                 if (entry->ip == ip)
2895                         entry->ops->func(ip, parent_ip, &entry->data);
2896         }
2897         preempt_enable_notrace();
2898 }
2899
2900 static struct ftrace_ops trace_probe_ops __read_mostly =
2901 {
2902         .func           = function_trace_probe_call,
2903 };
2904
2905 static int ftrace_probe_registered;
2906
2907 static void __enable_ftrace_function_probe(void)
2908 {
2909         int ret;
2910         int i;
2911
2912         if (ftrace_probe_registered)
2913                 return;
2914
2915         for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2916                 struct hlist_head *hhd = &ftrace_func_hash[i];
2917                 if (hhd->first)
2918                         break;
2919         }
2920         /* Nothing registered? */
2921         if (i == FTRACE_FUNC_HASHSIZE)
2922                 return;
2923
2924         ret = __register_ftrace_function(&trace_probe_ops);
2925         if (!ret)
2926                 ret = ftrace_startup(&trace_probe_ops, 0);
2927
2928         ftrace_probe_registered = 1;
2929 }
2930
2931 static void __disable_ftrace_function_probe(void)
2932 {
2933         int ret;
2934         int i;
2935
2936         if (!ftrace_probe_registered)
2937                 return;
2938
2939         for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2940                 struct hlist_head *hhd = &ftrace_func_hash[i];
2941                 if (hhd->first)
2942                         return;
2943         }
2944
2945         /* no more funcs left */
2946         ret = __unregister_ftrace_function(&trace_probe_ops);
2947         if (!ret)
2948                 ftrace_shutdown(&trace_probe_ops, 0);
2949
2950         ftrace_probe_registered = 0;
2951 }
2952
2953
2954 static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2955 {
2956         struct ftrace_func_probe *entry =
2957                 container_of(rhp, struct ftrace_func_probe, rcu);
2958
2959         if (entry->ops->free)
2960                 entry->ops->free(&entry->data);
2961         kfree(entry);
2962 }
2963
2964
2965 int
2966 register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
2967                               void *data)
2968 {
2969         struct ftrace_func_probe *entry;
2970         struct ftrace_page *pg;
2971         struct dyn_ftrace *rec;
2972         int type, len, not;
2973         unsigned long key;
2974         int count = 0;
2975         char *search;
2976
2977         type = filter_parse_regex(glob, strlen(glob), &search, &not);
2978         len = strlen(search);
2979
2980         /* we do not support '!' for function probes */
2981         if (WARN_ON(not))
2982                 return -EINVAL;
2983
2984         mutex_lock(&ftrace_lock);
2985
2986         if (unlikely(ftrace_disabled))
2987                 goto out_unlock;
2988
2989         do_for_each_ftrace_rec(pg, rec) {
2990
2991                 if (!ftrace_match_record(rec, NULL, search, len, type))
2992                         continue;
2993
2994                 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2995                 if (!entry) {
2996                         /* If we did not process any, then return error */
2997                         if (!count)
2998                                 count = -ENOMEM;
2999                         goto out_unlock;
3000                 }
3001
3002                 count++;
3003
3004                 entry->data = data;
3005
3006                 /*
3007                  * The caller might want to do something special
3008                  * for each function we find. We call the callback
3009                  * to give the caller an opportunity to do so.
3010                  */
3011                 if (ops->callback) {
3012                         if (ops->callback(rec->ip, &entry->data) < 0) {
3013                                 /* caller does not like this func */
3014                                 kfree(entry);
3015                                 continue;
3016                         }
3017                 }
3018
3019                 entry->ops = ops;
3020                 entry->ip = rec->ip;
3021
3022                 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3023                 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3024
3025         } while_for_each_ftrace_rec();
3026         __enable_ftrace_function_probe();
3027
3028  out_unlock:
3029         mutex_unlock(&ftrace_lock);
3030
3031         return count;
3032 }
3033
3034 enum {
3035         PROBE_TEST_FUNC         = 1,
3036         PROBE_TEST_DATA         = 2
3037 };
3038
3039 static void
3040 __unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3041                                   void *data, int flags)
3042 {
3043         struct ftrace_func_probe *entry;
3044         struct hlist_node *n, *tmp;
3045         char str[KSYM_SYMBOL_LEN];
3046         int type = MATCH_FULL;
3047         int i, len = 0;
3048         char *search;
3049
3050         if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
3051                 glob = NULL;
3052         else if (glob) {
3053                 int not;
3054
3055                 type = filter_parse_regex(glob, strlen(glob), &search, &not);
3056                 len = strlen(search);
3057
3058                 /* we do not support '!' for function probes */
3059                 if (WARN_ON(not))
3060                         return;
3061         }
3062
3063         mutex_lock(&ftrace_lock);
3064         for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3065                 struct hlist_head *hhd = &ftrace_func_hash[i];
3066
3067                 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
3068
3069                         /* break up if statements for readability */
3070                         if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
3071                                 continue;
3072
3073                         if ((flags & PROBE_TEST_DATA) && entry->data != data)
3074                                 continue;
3075
3076                         /* do this last, since it is the most expensive */
3077                         if (glob) {
3078                                 kallsyms_lookup(entry->ip, NULL, NULL,
3079                                                 NULL, str);
3080                                 if (!ftrace_match(str, glob, len, type))
3081                                         continue;
3082                         }
3083
3084                         hlist_del_rcu(&entry->node);
3085                         call_rcu_sched(&entry->rcu, ftrace_free_entry_rcu);
3086                 }
3087         }
3088         __disable_ftrace_function_probe();
3089         mutex_unlock(&ftrace_lock);
3090 }
3091
3092 void
3093 unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
3094                                 void *data)
3095 {
3096         __unregister_ftrace_function_probe(glob, ops, data,
3097                                           PROBE_TEST_FUNC | PROBE_TEST_DATA);
3098 }
3099
3100 void
3101 unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
3102 {
3103         __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
3104 }
3105
3106 void unregister_ftrace_function_probe_all(char *glob)
3107 {
3108         __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
3109 }
3110
3111 static LIST_HEAD(ftrace_commands);
3112 static DEFINE_MUTEX(ftrace_cmd_mutex);
3113
3114 int register_ftrace_command(struct ftrace_func_command *cmd)
3115 {
3116         struct ftrace_func_command *p;
3117         int ret = 0;
3118
3119         mutex_lock(&ftrace_cmd_mutex);
3120         list_for_each_entry(p, &ftrace_commands, list) {
3121                 if (strcmp(cmd->name, p->name) == 0) {
3122                         ret = -EBUSY;
3123                         goto out_unlock;
3124                 }
3125         }
3126         list_add(&cmd->list, &ftrace_commands);
3127  out_unlock:
3128         mutex_unlock(&ftrace_cmd_mutex);
3129
3130         return ret;
3131 }
3132
3133 int unregister_ftrace_command(struct ftrace_func_command *cmd)
3134 {
3135         struct ftrace_func_command *p, *n;
3136         int ret = -ENODEV;
3137
3138         mutex_lock(&ftrace_cmd_mutex);
3139         list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3140                 if (strcmp(cmd->name, p->name) == 0) {
3141                         ret = 0;
3142                         list_del_init(&p->list);
3143                         goto out_unlock;
3144                 }
3145         }
3146  out_unlock:
3147         mutex_unlock(&ftrace_cmd_mutex);
3148
3149         return ret;
3150 }
3151
3152 static int ftrace_process_regex(struct ftrace_hash *hash,
3153                                 char *buff, int len, int enable)
3154 {
3155         char *func, *command, *next = buff;
3156         struct ftrace_func_command *p;
3157         int ret = -EINVAL;
3158
3159         func = strsep(&next, ":");
3160
3161         if (!next) {
3162                 ret = ftrace_match_records(hash, func, len);
3163                 if (!ret)
3164                         ret = -EINVAL;
3165                 if (ret < 0)
3166                         return ret;
3167                 return 0;
3168         }
3169
3170         /* command found */
3171
3172         command = strsep(&next, ":");
3173
3174         mutex_lock(&ftrace_cmd_mutex);
3175         list_for_each_entry(p, &ftrace_commands, list) {
3176                 if (strcmp(p->name, command) == 0) {
3177                         ret = p->func(hash, func, command, next, enable);
3178                         goto out_unlock;
3179                 }
3180         }
3181  out_unlock:
3182         mutex_unlock(&ftrace_cmd_mutex);
3183
3184         return ret;
3185 }
3186
3187 static ssize_t
3188 ftrace_regex_write(struct file *file, const char __user *ubuf,
3189                    size_t cnt, loff_t *ppos, int enable)
3190 {
3191         struct ftrace_iterator *iter;
3192         struct trace_parser *parser;
3193         ssize_t ret, read;
3194
3195         if (!cnt)
3196                 return 0;
3197
3198         mutex_lock(&ftrace_regex_lock);
3199
3200         ret = -ENODEV;
3201         if (unlikely(ftrace_disabled))
3202                 goto out_unlock;
3203
3204         if (file->f_mode & FMODE_READ) {
3205                 struct seq_file *m = file->private_data;
3206                 iter = m->private;
3207         } else
3208                 iter = file->private_data;
3209
3210         parser = &iter->parser;
3211         read = trace_get_user(parser, ubuf, cnt, ppos);
3212
3213         if (read >= 0 && trace_parser_loaded(parser) &&
3214             !trace_parser_cont(parser)) {
3215                 ret = ftrace_process_regex(iter->hash, parser->buffer,
3216                                            parser->idx, enable);
3217                 trace_parser_clear(parser);
3218                 if (ret)
3219                         goto out_unlock;
3220         }
3221
3222         ret = read;
3223 out_unlock:
3224         mutex_unlock(&ftrace_regex_lock);
3225
3226         return ret;
3227 }
3228
3229 ssize_t
3230 ftrace_filter_write(struct file *file, const char __user *ubuf,
3231                     size_t cnt, loff_t *ppos)
3232 {
3233         return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3234 }
3235
3236 ssize_t
3237 ftrace_notrace_write(struct file *file, const char __user *ubuf,
3238                      size_t cnt, loff_t *ppos)
3239 {
3240         return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3241 }
3242
3243 static int
3244 ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3245 {
3246         struct ftrace_func_entry *entry;
3247
3248         if (!ftrace_location(ip))
3249                 return -EINVAL;
3250
3251         if (remove) {
3252                 entry = ftrace_lookup_ip(hash, ip);
3253                 if (!entry)
3254                         return -ENOENT;
3255                 free_hash_entry(hash, entry);
3256                 return 0;
3257         }
3258
3259         return add_hash_entry(hash, ip);
3260 }
3261
3262 static int
3263 ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3264                 unsigned long ip, int remove, int reset, int enable)
3265 {
3266         struct ftrace_hash **orig_hash;
3267         struct ftrace_hash *hash;
3268         int ret;
3269
3270         /* All global ops uses the global ops filters */
3271         if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3272                 ops = &global_ops;
3273
3274         if (unlikely(ftrace_disabled))
3275                 return -ENODEV;
3276
3277         if (enable)
3278                 orig_hash = &ops->filter_hash;
3279         else
3280                 orig_hash = &ops->notrace_hash;
3281
3282         hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3283         if (!hash)
3284                 return -ENOMEM;
3285
3286         mutex_lock(&ftrace_regex_lock);
3287         if (reset)
3288                 ftrace_filter_reset(hash);
3289         if (buf && !ftrace_match_records(hash, buf, len)) {
3290                 ret = -EINVAL;
3291                 goto out_regex_unlock;
3292         }
3293         if (ip) {
3294                 ret = ftrace_match_addr(hash, ip, remove);
3295                 if (ret < 0)
3296                         goto out_regex_unlock;
3297         }
3298
3299         mutex_lock(&ftrace_lock);
3300         ret = ftrace_hash_move(ops, enable, orig_hash, hash);
3301         if (!ret && ops->flags & FTRACE_OPS_FL_ENABLED
3302             && ftrace_enabled)
3303                 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3304
3305         mutex_unlock(&ftrace_lock);
3306
3307  out_regex_unlock:
3308         mutex_unlock(&ftrace_regex_lock);
3309
3310         free_ftrace_hash(hash);
3311         return ret;
3312 }
3313
3314 static int
3315 ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3316                 int reset, int enable)
3317 {
3318         return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3319 }
3320
3321 /**
3322  * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3323  * @ops - the ops to set the filter with
3324  * @ip - the address to add to or remove from the filter.
3325  * @remove - non zero to remove the ip from the filter
3326  * @reset - non zero to reset all filters before applying this filter.
3327  *
3328  * Filters denote which functions should be enabled when tracing is enabled
3329  * If @ip is NULL, it failes to update filter.
3330  */
3331 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3332                          int remove, int reset)
3333 {
3334         return ftrace_set_addr(ops, ip, remove, reset, 1);
3335 }
3336 EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3337
3338 static int
3339 ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3340                  int reset, int enable)
3341 {
3342         return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3343 }
3344
3345 /**
3346  * ftrace_set_filter - set a function to filter on in ftrace
3347  * @ops - the ops to set the filter with
3348  * @buf - the string that holds the function filter text.
3349  * @len - the length of the string.
3350  * @reset - non zero to reset all filters before applying this filter.
3351  *
3352  * Filters denote which functions should be enabled when tracing is enabled.
3353  * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3354  */
3355 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
3356                        int len, int reset)
3357 {
3358         return ftrace_set_regex(ops, buf, len, reset, 1);
3359 }
3360 EXPORT_SYMBOL_GPL(ftrace_set_filter);
3361
3362 /**
3363  * ftrace_set_notrace - set a function to not trace in ftrace
3364  * @ops - the ops to set the notrace filter with
3365  * @buf - the string that holds the function notrace text.
3366  * @len - the length of the string.
3367  * @reset - non zero to reset all filters before applying this filter.
3368  *
3369  * Notrace Filters denote which functions should not be enabled when tracing
3370  * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3371  * for tracing.
3372  */
3373 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
3374                         int len, int reset)
3375 {
3376         return ftrace_set_regex(ops, buf, len, reset, 0);
3377 }
3378 EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3379 /**
3380  * ftrace_set_filter - set a function to filter on in ftrace
3381  * @ops - the ops to set the filter with
3382  * @buf - the string that holds the function filter text.
3383  * @len - the length of the string.
3384  * @reset - non zero to reset all filters before applying this filter.
3385  *
3386  * Filters denote which functions should be enabled when tracing is enabled.
3387  * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3388  */
3389 void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3390 {
3391         ftrace_set_regex(&global_ops, buf, len, reset, 1);
3392 }
3393 EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3394
3395 /**
3396  * ftrace_set_notrace - set a function to not trace in ftrace
3397  * @ops - the ops to set the notrace filter with
3398  * @buf - the string that holds the function notrace text.
3399  * @len - the length of the string.
3400  * @reset - non zero to reset all filters before applying this filter.
3401  *
3402  * Notrace Filters denote which functions should not be enabled when tracing
3403  * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3404  * for tracing.
3405  */
3406 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
3407 {
3408         ftrace_set_regex(&global_ops, buf, len, reset, 0);
3409 }
3410 EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
3411
3412 /*
3413  * command line interface to allow users to set filters on boot up.
3414  */
3415 #define FTRACE_FILTER_SIZE              COMMAND_LINE_SIZE
3416 static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3417 static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3418
3419 static int __init set_ftrace_notrace(char *str)
3420 {
3421         strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
3422         return 1;
3423 }
3424 __setup("ftrace_notrace=", set_ftrace_notrace);
3425
3426 static int __init set_ftrace_filter(char *str)
3427 {
3428         strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
3429         return 1;
3430 }
3431 __setup("ftrace_filter=", set_ftrace_filter);
3432
3433 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3434 static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
3435 static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
3436
3437 static int __init set_graph_function(char *str)
3438 {
3439         strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
3440         return 1;
3441 }
3442 __setup("ftrace_graph_filter=", set_graph_function);
3443
3444 static void __init set_ftrace_early_graph(char *buf)
3445 {
3446         int ret;
3447         char *func;
3448
3449         while (buf) {
3450                 func = strsep(&buf, ",");
3451                 /* we allow only one expression at a time */
3452                 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3453                                       func);
3454                 if (ret)
3455                         printk(KERN_DEBUG "ftrace: function %s not "
3456                                           "traceable\n", func);
3457         }
3458 }
3459 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3460
3461 void __init
3462 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
3463 {
3464         char *func;
3465
3466         while (buf) {
3467                 func = strsep(&buf, ",");
3468                 ftrace_set_regex(ops, func, strlen(func), 0, enable);
3469         }
3470 }
3471
3472 static void __init set_ftrace_early_filters(void)
3473 {
3474         if (ftrace_filter_buf[0])
3475                 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
3476         if (ftrace_notrace_buf[0])
3477                 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
3478 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3479         if (ftrace_graph_buf[0])
3480                 set_ftrace_early_graph(ftrace_graph_buf);
3481 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3482 }
3483
3484 int ftrace_regex_release(struct inode *inode, struct file *file)
3485 {
3486         struct seq_file *m = (struct seq_file *)file->private_data;
3487         struct ftrace_iterator *iter;
3488         struct ftrace_hash **orig_hash;
3489         struct trace_parser *parser;
3490         int filter_hash;
3491         int ret;
3492
3493         mutex_lock(&ftrace_regex_lock);
3494         if (file->f_mode & FMODE_READ) {
3495                 iter = m->private;
3496
3497                 seq_release(inode, file);
3498         } else
3499                 iter = file->private_data;
3500
3501         parser = &iter->parser;
3502         if (trace_parser_loaded(parser)) {
3503                 parser->buffer[parser->idx] = 0;
3504                 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
3505         }
3506
3507         trace_parser_put(parser);
3508
3509         if (file->f_mode & FMODE_WRITE) {
3510                 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3511
3512                 if (filter_hash)
3513                         orig_hash = &iter->ops->filter_hash;
3514                 else
3515                         orig_hash = &iter->ops->notrace_hash;
3516
3517                 mutex_lock(&ftrace_lock);
3518                 ret = ftrace_hash_move(iter->ops, filter_hash,
3519                                        orig_hash, iter->hash);
3520                 if (!ret && (iter->ops->flags & FTRACE_OPS_FL_ENABLED)
3521                     && ftrace_enabled)
3522                         ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3523
3524                 mutex_unlock(&ftrace_lock);
3525         }
3526         free_ftrace_hash(iter->hash);
3527         kfree(iter);
3528
3529         mutex_unlock(&ftrace_regex_lock);
3530         return 0;
3531 }
3532
3533 static const struct file_operations ftrace_avail_fops = {
3534         .open = ftrace_avail_open,
3535         .read = seq_read,
3536         .llseek = seq_lseek,
3537         .release = seq_release_private,
3538 };
3539
3540 static const struct file_operations ftrace_enabled_fops = {
3541         .open = ftrace_enabled_open,
3542         .read = seq_read,
3543         .llseek = seq_lseek,
3544         .release = seq_release_private,
3545 };
3546
3547 static const struct file_operations ftrace_filter_fops = {
3548         .open = ftrace_filter_open,
3549         .read = seq_read,
3550         .write = ftrace_filter_write,
3551         .llseek = ftrace_filter_lseek,
3552         .release = ftrace_regex_release,
3553 };
3554
3555 static const struct file_operations ftrace_notrace_fops = {
3556         .open = ftrace_notrace_open,
3557         .read = seq_read,
3558         .write = ftrace_notrace_write,
3559         .llseek = ftrace_filter_lseek,
3560         .release = ftrace_regex_release,
3561 };
3562
3563 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3564
3565 static DEFINE_MUTEX(graph_lock);
3566
3567 int ftrace_graph_count;
3568 int ftrace_graph_filter_enabled;
3569 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3570
3571 static void *
3572 __g_next(struct seq_file *m, loff_t *pos)
3573 {
3574         if (*pos >= ftrace_graph_count)
3575                 return NULL;
3576         return &ftrace_graph_funcs[*pos];
3577 }
3578
3579 static void *
3580 g_next(struct seq_file *m, void *v, loff_t *pos)
3581 {
3582         (*pos)++;
3583         return __g_next(m, pos);
3584 }
3585
3586 static void *g_start(struct seq_file *m, loff_t *pos)
3587 {
3588         mutex_lock(&graph_lock);
3589
3590         /* Nothing, tell g_show to print all functions are enabled */
3591         if (!ftrace_graph_filter_enabled && !*pos)
3592                 return (void *)1;
3593
3594         return __g_next(m, pos);
3595 }
3596
3597 static void g_stop(struct seq_file *m, void *p)
3598 {
3599         mutex_unlock(&graph_lock);
3600 }
3601
3602 static int g_show(struct seq_file *m, void *v)
3603 {
3604         unsigned long *ptr = v;
3605
3606         if (!ptr)
3607                 return 0;
3608
3609         if (ptr == (unsigned long *)1) {
3610                 seq_printf(m, "#### all functions enabled ####\n");
3611                 return 0;
3612         }
3613
3614         seq_printf(m, "%ps\n", (void *)*ptr);
3615
3616         return 0;
3617 }
3618
3619 static const struct seq_operations ftrace_graph_seq_ops = {
3620         .start = g_start,
3621         .next = g_next,
3622         .stop = g_stop,
3623         .show = g_show,
3624 };
3625
3626 static int
3627 ftrace_graph_open(struct inode *inode, struct file *file)
3628 {
3629         int ret = 0;
3630
3631         if (unlikely(ftrace_disabled))
3632                 return -ENODEV;
3633
3634         mutex_lock(&graph_lock);
3635         if ((file->f_mode & FMODE_WRITE) &&
3636             (file->f_flags & O_TRUNC)) {
3637                 ftrace_graph_filter_enabled = 0;
3638                 ftrace_graph_count = 0;
3639                 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3640         }
3641         mutex_unlock(&graph_lock);
3642
3643         if (file->f_mode & FMODE_READ)
3644                 ret = seq_open(file, &ftrace_graph_seq_ops);
3645
3646         return ret;
3647 }
3648
3649 static int
3650 ftrace_graph_release(struct inode *inode, struct file *file)
3651 {
3652         if (file->f_mode & FMODE_READ)
3653                 seq_release(inode, file);
3654         return 0;
3655 }
3656
3657 static int
3658 ftrace_set_func(unsigned long *array, int *idx, char *buffer)
3659 {
3660         struct dyn_ftrace *rec;
3661         struct ftrace_page *pg;
3662         int search_len;
3663         int fail = 1;
3664         int type, not;
3665         char *search;
3666         bool exists;
3667         int i;
3668
3669         /* decode regex */
3670         type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
3671         if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3672                 return -EBUSY;
3673
3674         search_len = strlen(search);
3675
3676         mutex_lock(&ftrace_lock);
3677
3678         if (unlikely(ftrace_disabled)) {
3679                 mutex_unlock(&ftrace_lock);
3680                 return -ENODEV;
3681         }
3682
3683         do_for_each_ftrace_rec(pg, rec) {
3684
3685                 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
3686                         /* if it is in the array */
3687                         exists = false;
3688                         for (i = 0; i < *idx; i++) {
3689                                 if (array[i] == rec->ip) {
3690                                         exists = true;
3691                                         break;
3692                                 }
3693                         }
3694
3695                         if (!not) {
3696                                 fail = 0;
3697                                 if (!exists) {
3698                                         array[(*idx)++] = rec->ip;
3699                                         if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3700                                                 goto out;
3701                                 }
3702                         } else {
3703                                 if (exists) {
3704                                         array[i] = array[--(*idx)];
3705                                         array[*idx] = 0;
3706                                         fail = 0;
3707                                 }
3708                         }
3709                 }
3710         } while_for_each_ftrace_rec();
3711 out:
3712         mutex_unlock(&ftrace_lock);
3713
3714         if (fail)
3715                 return -EINVAL;
3716
3717         ftrace_graph_filter_enabled = 1;
3718         return 0;
3719 }
3720
3721 static ssize_t
3722 ftrace_graph_write(struct file *file, const char __user *ubuf,
3723                    size_t cnt, loff_t *ppos)
3724 {
3725         struct trace_parser parser;
3726         ssize_t read, ret;
3727
3728         if (!cnt)
3729                 return 0;
3730
3731         mutex_lock(&graph_lock);
3732
3733         if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3734                 ret = -ENOMEM;
3735                 goto out_unlock;
3736         }
3737
3738         read = trace_get_user(&parser, ubuf, cnt, ppos);
3739
3740         if (read >= 0 && trace_parser_loaded((&parser))) {
3741                 parser.buffer[parser.idx] = 0;
3742
3743                 /* we allow only one expression at a time */
3744                 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3745                                         parser.buffer);
3746                 if (ret)
3747                         goto out_free;
3748         }
3749
3750         ret = read;
3751
3752 out_free:
3753         trace_parser_put(&parser);
3754 out_unlock:
3755         mutex_unlock(&graph_lock);
3756
3757         return ret;
3758 }
3759
3760 static const struct file_operations ftrace_graph_fops = {
3761         .open           = ftrace_graph_open,
3762         .read           = seq_read,
3763         .write          = ftrace_graph_write,
3764         .llseek         = ftrace_filter_lseek,
3765         .release        = ftrace_graph_release,
3766 };
3767 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3768
3769 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
3770 {
3771
3772         trace_create_file("available_filter_functions", 0444,
3773                         d_tracer, NULL, &ftrace_avail_fops);
3774
3775         trace_create_file("enabled_functions", 0444,
3776                         d_tracer, NULL, &ftrace_enabled_fops);
3777
3778         trace_create_file("set_ftrace_filter", 0644, d_tracer,
3779                         NULL, &ftrace_filter_fops);
3780
3781         trace_create_file("set_ftrace_notrace", 0644, d_tracer,
3782                                     NULL, &ftrace_notrace_fops);
3783
3784 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
3785         trace_create_file("set_graph_function", 0444, d_tracer,
3786                                     NULL,
3787                                     &ftrace_graph_fops);
3788 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3789
3790         return 0;
3791 }
3792
3793 static int ftrace_cmp_ips(const void *a, const void *b)
3794 {
3795         const unsigned long *ipa = a;
3796         const unsigned long *ipb = b;
3797
3798         if (*ipa > *ipb)
3799                 return 1;
3800         if (*ipa < *ipb)
3801                 return -1;
3802         return 0;
3803 }
3804
3805 static void ftrace_swap_ips(void *a, void *b, int size)
3806 {
3807         unsigned long *ipa = a;
3808         unsigned long *ipb = b;
3809         unsigned long t;
3810
3811         t = *ipa;
3812         *ipa = *ipb;
3813         *ipb = t;
3814 }
3815
3816 static int ftrace_process_locs(struct module *mod,
3817                                unsigned long *start,
3818                                unsigned long *end)
3819 {
3820         struct ftrace_page *start_pg;
3821         struct ftrace_page *pg;
3822         struct dyn_ftrace *rec;
3823         unsigned long count;
3824         unsigned long *p;
3825         unsigned long addr;
3826         unsigned long flags = 0; /* Shut up gcc */
3827         int ret = -ENOMEM;
3828
3829         count = end - start;
3830
3831         if (!count)
3832                 return 0;
3833
3834         sort(start, count, sizeof(*start),
3835              ftrace_cmp_ips, ftrace_swap_ips);
3836
3837         start_pg = ftrace_allocate_pages(count);
3838         if (!start_pg)
3839                 return -ENOMEM;
3840
3841         mutex_lock(&ftrace_lock);
3842
3843         /*
3844          * Core and each module needs their own pages, as
3845          * modules will free them when they are removed.
3846          * Force a new page to be allocated for modules.
3847          */
3848         if (!mod) {
3849                 WARN_ON(ftrace_pages || ftrace_pages_start);
3850                 /* First initialization */
3851                 ftrace_pages = ftrace_pages_start = start_pg;
3852         } else {
3853                 if (!ftrace_pages)
3854                         goto out;
3855
3856                 if (WARN_ON(ftrace_pages->next)) {
3857                         /* Hmm, we have free pages? */
3858                         while (ftrace_pages->next)
3859                                 ftrace_pages = ftrace_pages->next;
3860                 }
3861
3862                 ftrace_pages->next = start_pg;
3863         }
3864
3865         p = start;
3866         pg = start_pg;
3867         while (p < end) {
3868                 addr = ftrace_call_adjust(*p++);
3869                 /*
3870                  * Some architecture linkers will pad between
3871                  * the different mcount_loc sections of different
3872                  * object files to satisfy alignments.
3873                  * Skip any NULL pointers.
3874                  */
3875                 if (!addr)
3876                         continue;
3877
3878                 if (pg->index == pg->size) {
3879                         /* We should have allocated enough */
3880                         if (WARN_ON(!pg->next))
3881                                 break;
3882                         pg = pg->next;
3883                 }
3884
3885                 rec = &pg->records[pg->index++];
3886                 rec->ip = addr;
3887         }
3888
3889         /* We should have used all pages */
3890         WARN_ON(pg->next);
3891
3892         /* Assign the last page to ftrace_pages */
3893         ftrace_pages = pg;
3894
3895         /* These new locations need to be initialized */
3896         ftrace_new_pgs = start_pg;
3897
3898         /*
3899          * We only need to disable interrupts on start up
3900          * because we are modifying code that an interrupt
3901          * may execute, and the modification is not atomic.
3902          * But for modules, nothing runs the code we modify
3903          * until we are finished with it, and there's no
3904          * reason to cause large interrupt latencies while we do it.
3905          */
3906         if (!mod)
3907                 local_irq_save(flags);
3908         ftrace_update_code(mod);
3909         if (!mod)
3910                 local_irq_restore(flags);
3911         ret = 0;
3912  out:
3913         mutex_unlock(&ftrace_lock);
3914
3915         return ret;
3916 }
3917
3918 #ifdef CONFIG_MODULES
3919
3920 #define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
3921
3922 void ftrace_release_mod(struct module *mod)
3923 {
3924         struct dyn_ftrace *rec;
3925         struct ftrace_page **last_pg;
3926         struct ftrace_page *pg;
3927         int order;
3928
3929         mutex_lock(&ftrace_lock);
3930
3931         if (ftrace_disabled)
3932                 goto out_unlock;
3933
3934         /*
3935          * Each module has its own ftrace_pages, remove
3936          * them from the list.
3937          */
3938         last_pg = &ftrace_pages_start;
3939         for (pg = ftrace_pages_start; pg; pg = *last_pg) {
3940                 rec = &pg->records[0];
3941                 if (within_module_core(rec->ip, mod)) {
3942                         /*
3943                          * As core pages are first, the first
3944                          * page should never be a module page.
3945                          */
3946                         if (WARN_ON(pg == ftrace_pages_start))
3947                                 goto out_unlock;
3948
3949                         /* Check if we are deleting the last page */
3950                         if (pg == ftrace_pages)
3951                                 ftrace_pages = next_to_ftrace_page(last_pg);
3952
3953                         *last_pg = pg->next;
3954                         order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3955                         free_pages((unsigned long)pg->records, order);
3956                         kfree(pg);
3957                 } else
3958                         last_pg = &pg->next;
3959         }
3960  out_unlock:
3961         mutex_unlock(&ftrace_lock);
3962 }
3963
3964 static void ftrace_init_module(struct module *mod,
3965                                unsigned long *start, unsigned long *end)
3966 {
3967         if (ftrace_disabled || start == end)
3968                 return;
3969         ftrace_process_locs(mod, start, end);
3970 }
3971
3972 static int ftrace_module_notify_enter(struct notifier_block *self,
3973                                       unsigned long val, void *data)
3974 {
3975         struct module *mod = data;
3976
3977         if (val == MODULE_STATE_COMING)
3978                 ftrace_init_module(mod, mod->ftrace_callsites,
3979                                    mod->ftrace_callsites +
3980                                    mod->num_ftrace_callsites);
3981         return 0;
3982 }
3983
3984 static int ftrace_module_notify_exit(struct notifier_block *self,
3985                                      unsigned long val, void *data)
3986 {
3987         struct module *mod = data;
3988
3989         if (val == MODULE_STATE_GOING)
3990                 ftrace_release_mod(mod);
3991
3992         return 0;
3993 }
3994 #else
3995 static int ftrace_module_notify_enter(struct notifier_block *self,
3996                                       unsigned long val, void *data)
3997 {
3998         return 0;
3999 }
4000 static int ftrace_module_notify_exit(struct notifier_block *self,
4001                                      unsigned long val, void *data)
4002 {
4003         return 0;
4004 }
4005 #endif /* CONFIG_MODULES */
4006
4007 struct notifier_block ftrace_module_enter_nb = {
4008         .notifier_call = ftrace_module_notify_enter,
4009         .priority = INT_MAX,    /* Run before anything that can use kprobes */
4010 };
4011
4012 struct notifier_block ftrace_module_exit_nb = {
4013         .notifier_call = ftrace_module_notify_exit,
4014         .priority = INT_MIN,    /* Run after anything that can remove kprobes */
4015 };
4016
4017 extern unsigned long __start_mcount_loc[];
4018 extern unsigned long __stop_mcount_loc[];
4019
4020 void __init ftrace_init(void)
4021 {
4022         unsigned long count, addr, flags;
4023         int ret;
4024
4025         /* Keep the ftrace pointer to the stub */
4026         addr = (unsigned long)ftrace_stub;
4027
4028         local_irq_save(flags);
4029         ftrace_dyn_arch_init(&addr);
4030         local_irq_restore(flags);
4031
4032         /* ftrace_dyn_arch_init places the return code in addr */
4033         if (addr)
4034                 goto failed;
4035
4036         count = __stop_mcount_loc - __start_mcount_loc;
4037
4038         ret = ftrace_dyn_table_alloc(count);
4039         if (ret)
4040                 goto failed;
4041
4042         last_ftrace_enabled = ftrace_enabled = 1;
4043
4044         ret = ftrace_process_locs(NULL,
4045                                   __start_mcount_loc,
4046                                   __stop_mcount_loc);
4047
4048         ret = register_module_notifier(&ftrace_module_enter_nb);
4049         if (ret)
4050                 pr_warning("Failed to register trace ftrace module enter notifier\n");
4051
4052         ret = register_module_notifier(&ftrace_module_exit_nb);
4053         if (ret)
4054                 pr_warning("Failed to register trace ftrace module exit notifier\n");
4055
4056         set_ftrace_early_filters();
4057
4058         return;
4059  failed:
4060         ftrace_disabled = 1;
4061 }
4062
4063 #else
4064
4065 static struct ftrace_ops global_ops = {
4066         .func                   = ftrace_stub,
4067         .flags                  = FTRACE_OPS_FL_RECURSION_SAFE,
4068 };
4069
4070 static int __init ftrace_nodyn_init(void)
4071 {
4072         ftrace_enabled = 1;
4073         return 0;
4074 }
4075 core_initcall(ftrace_nodyn_init);
4076
4077 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4078 static inline void ftrace_startup_enable(int command) { }
4079 /* Keep as macros so we do not need to define the commands */
4080 # define ftrace_startup(ops, command)                   \
4081         ({                                              \
4082                 (ops)->flags |= FTRACE_OPS_FL_ENABLED;  \
4083                 0;                                      \
4084         })
4085 # define ftrace_shutdown(ops, command)  do { } while (0)
4086 # define ftrace_startup_sysctl()        do { } while (0)
4087 # define ftrace_shutdown_sysctl()       do { } while (0)
4088
4089 static inline int
4090 ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
4091 {
4092         return 1;
4093 }
4094
4095 #endif /* CONFIG_DYNAMIC_FTRACE */
4096
4097 static void
4098 ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
4099                         struct ftrace_ops *op, struct pt_regs *regs)
4100 {
4101         if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4102                 return;
4103
4104         /*
4105          * Some of the ops may be dynamically allocated,
4106          * they must be freed after a synchronize_sched().
4107          */
4108         preempt_disable_notrace();
4109         trace_recursion_set(TRACE_CONTROL_BIT);
4110         op = rcu_dereference_raw(ftrace_control_list);
4111         while (op != &ftrace_list_end) {
4112                 if (!ftrace_function_local_disabled(op) &&
4113                     ftrace_ops_test(op, ip))
4114                         op->func(ip, parent_ip, op, regs);
4115
4116                 op = rcu_dereference_raw(op->next);
4117         };
4118         trace_recursion_clear(TRACE_CONTROL_BIT);
4119         preempt_enable_notrace();
4120 }
4121
4122 static struct ftrace_ops control_ops = {
4123         .func = ftrace_ops_control_func,
4124         .flags = FTRACE_OPS_FL_RECURSION_SAFE,
4125 };
4126
4127 static inline void
4128 __ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
4129                        struct ftrace_ops *ignored, struct pt_regs *regs)
4130 {
4131         struct ftrace_ops *op;
4132
4133         if (function_trace_stop)
4134                 return;
4135
4136         if (unlikely(trace_recursion_test(TRACE_INTERNAL_BIT)))
4137                 return;
4138
4139         trace_recursion_set(TRACE_INTERNAL_BIT);
4140         /*
4141          * Some of the ops may be dynamically allocated,
4142          * they must be freed after a synchronize_sched().
4143          */
4144         preempt_disable_notrace();
4145         op = rcu_dereference_raw(ftrace_ops_list);
4146         while (op != &ftrace_list_end) {
4147                 if (ftrace_ops_test(op, ip))
4148                         op->func(ip, parent_ip, op, regs);
4149                 op = rcu_dereference_raw(op->next);
4150         };
4151         preempt_enable_notrace();
4152         trace_recursion_clear(TRACE_INTERNAL_BIT);
4153 }
4154
4155 /*
4156  * Some archs only support passing ip and parent_ip. Even though
4157  * the list function ignores the op parameter, we do not want any
4158  * C side effects, where a function is called without the caller
4159  * sending a third parameter.
4160  * Archs are to support both the regs and ftrace_ops at the same time.
4161  * If they support ftrace_ops, it is assumed they support regs.
4162  * If call backs want to use regs, they must either check for regs
4163  * being NULL, or ARCH_SUPPORTS_FTRACE_SAVE_REGS.
4164  * Note, ARCH_SUPPORT_SAVE_REGS expects a full regs to be saved.
4165  * An architecture can pass partial regs with ftrace_ops and still
4166  * set the ARCH_SUPPORT_FTARCE_OPS.
4167  */
4168 #if ARCH_SUPPORTS_FTRACE_OPS
4169 static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
4170                                  struct ftrace_ops *op, struct pt_regs *regs)
4171 {
4172         __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
4173 }
4174 #else
4175 static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4176 {
4177         __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
4178 }
4179 #endif
4180
4181 static void clear_ftrace_swapper(void)
4182 {
4183         struct task_struct *p;
4184         int cpu;
4185
4186         get_online_cpus();
4187         for_each_online_cpu(cpu) {
4188                 p = idle_task(cpu);
4189                 clear_tsk_trace_trace(p);
4190         }
4191         put_online_cpus();
4192 }
4193
4194 static void set_ftrace_swapper(void)
4195 {
4196         struct task_struct *p;
4197         int cpu;
4198
4199         get_online_cpus();
4200         for_each_online_cpu(cpu) {
4201                 p = idle_task(cpu);
4202                 set_tsk_trace_trace(p);
4203         }
4204         put_online_cpus();
4205 }
4206
4207 static void clear_ftrace_pid(struct pid *pid)
4208 {
4209         struct task_struct *p;
4210
4211         rcu_read_lock();
4212         do_each_pid_task(pid, PIDTYPE_PID, p) {
4213                 clear_tsk_trace_trace(p);
4214         } while_each_pid_task(pid, PIDTYPE_PID, p);
4215         rcu_read_unlock();
4216
4217         put_pid(pid);
4218 }
4219
4220 static void set_ftrace_pid(struct pid *pid)
4221 {
4222         struct task_struct *p;
4223
4224         rcu_read_lock();
4225         do_each_pid_task(pid, PIDTYPE_PID, p) {
4226                 set_tsk_trace_trace(p);
4227         } while_each_pid_task(pid, PIDTYPE_PID, p);
4228         rcu_read_unlock();
4229 }
4230
4231 static void clear_ftrace_pid_task(struct pid *pid)
4232 {
4233         if (pid == ftrace_swapper_pid)
4234                 clear_ftrace_swapper();
4235         else
4236                 clear_ftrace_pid(pid);
4237 }
4238
4239 static void set_ftrace_pid_task(struct pid *pid)
4240 {
4241         if (pid == ftrace_swapper_pid)
4242                 set_ftrace_swapper();
4243         else
4244                 set_ftrace_pid(pid);
4245 }
4246
4247 static int ftrace_pid_add(int p)
4248 {
4249         struct pid *pid;
4250         struct ftrace_pid *fpid;
4251         int ret = -EINVAL;
4252
4253         mutex_lock(&ftrace_lock);
4254
4255         if (!p)
4256                 pid = ftrace_swapper_pid;
4257         else
4258                 pid = find_get_pid(p);
4259
4260         if (!pid)
4261                 goto out;
4262
4263         ret = 0;
4264
4265         list_for_each_entry(fpid, &ftrace_pids, list)
4266                 if (fpid->pid == pid)
4267                         goto out_put;
4268
4269         ret = -ENOMEM;
4270
4271         fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4272         if (!fpid)
4273                 goto out_put;
4274
4275         list_add(&fpid->list, &ftrace_pids);
4276         fpid->pid = pid;
4277
4278         set_ftrace_pid_task(pid);
4279
4280         ftrace_update_pid_func();
4281         ftrace_startup_enable(0);
4282
4283         mutex_unlock(&ftrace_lock);
4284         return 0;
4285
4286 out_put:
4287         if (pid != ftrace_swapper_pid)
4288                 put_pid(pid);
4289
4290 out:
4291         mutex_unlock(&ftrace_lock);
4292         return ret;
4293 }
4294
4295 static void ftrace_pid_reset(void)
4296 {
4297         struct ftrace_pid *fpid, *safe;
4298
4299         mutex_lock(&ftrace_lock);
4300         list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4301                 struct pid *pid = fpid->pid;
4302
4303                 clear_ftrace_pid_task(pid);
4304
4305                 list_del(&fpid->list);
4306                 kfree(fpid);
4307         }
4308
4309         ftrace_update_pid_func();
4310         ftrace_startup_enable(0);
4311
4312         mutex_unlock(&ftrace_lock);
4313 }
4314
4315 static void *fpid_start(struct seq_file *m, loff_t *pos)
4316 {
4317         mutex_lock(&ftrace_lock);
4318
4319         if (list_empty(&ftrace_pids) && (!*pos))
4320                 return (void *) 1;
4321
4322         return seq_list_start(&ftrace_pids, *pos);
4323 }
4324
4325 static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4326 {
4327         if (v == (void *)1)
4328                 return NULL;
4329
4330         return seq_list_next(v, &ftrace_pids, pos);
4331 }
4332
4333 static void fpid_stop(struct seq_file *m, void *p)
4334 {
4335         mutex_unlock(&ftrace_lock);
4336 }
4337
4338 static int fpid_show(struct seq_file *m, void *v)
4339 {
4340         const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4341
4342         if (v == (void *)1) {
4343                 seq_printf(m, "no pid\n");
4344                 return 0;
4345         }
4346
4347         if (fpid->pid == ftrace_swapper_pid)
4348                 seq_printf(m, "swapper tasks\n");
4349         else
4350                 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4351
4352         return 0;
4353 }
4354
4355 static const struct seq_operations ftrace_pid_sops = {
4356         .start = fpid_start,
4357         .next = fpid_next,
4358         .stop = fpid_stop,
4359         .show = fpid_show,
4360 };
4361
4362 static int
4363 ftrace_pid_open(struct inode *inode, struct file *file)
4364 {
4365         int ret = 0;
4366
4367         if ((file->f_mode & FMODE_WRITE) &&
4368             (file->f_flags & O_TRUNC))
4369                 ftrace_pid_reset();
4370
4371         if (file->f_mode & FMODE_READ)
4372                 ret = seq_open(file, &ftrace_pid_sops);
4373
4374         return ret;
4375 }
4376
4377 static ssize_t
4378 ftrace_pid_write(struct file *filp, const char __user *ubuf,
4379                    size_t cnt, loff_t *ppos)
4380 {
4381         char buf[64], *tmp;
4382         long val;
4383         int ret;
4384
4385         if (cnt >= sizeof(buf))
4386                 return -EINVAL;
4387
4388         if (copy_from_user(&buf, ubuf, cnt))
4389                 return -EFAULT;
4390
4391         buf[cnt] = 0;
4392
4393         /*
4394          * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4395          * to clean the filter quietly.
4396          */
4397         tmp = strstrip(buf);
4398         if (strlen(tmp) == 0)
4399                 return 1;
4400
4401         ret = kstrtol(tmp, 10, &val);
4402         if (ret < 0)
4403                 return ret;
4404
4405         ret = ftrace_pid_add(val);
4406
4407         return ret ? ret : cnt;
4408 }
4409
4410 static int
4411 ftrace_pid_release(struct inode *inode, struct file *file)
4412 {
4413         if (file->f_mode & FMODE_READ)
4414                 seq_release(inode, file);
4415
4416         return 0;
4417 }
4418
4419 static const struct file_operations ftrace_pid_fops = {
4420         .open           = ftrace_pid_open,
4421         .write          = ftrace_pid_write,
4422         .read           = seq_read,
4423         .llseek         = ftrace_filter_lseek,
4424         .release        = ftrace_pid_release,
4425 };
4426
4427 static __init int ftrace_init_debugfs(void)
4428 {
4429         struct dentry *d_tracer;
4430
4431         d_tracer = tracing_init_dentry();
4432         if (!d_tracer)
4433                 return 0;
4434
4435         ftrace_init_dyn_debugfs(d_tracer);
4436
4437         trace_create_file("set_ftrace_pid", 0644, d_tracer,
4438                             NULL, &ftrace_pid_fops);
4439
4440         ftrace_profile_debugfs(d_tracer);
4441
4442         return 0;
4443 }
4444 fs_initcall(ftrace_init_debugfs);
4445
4446 /**
4447  * ftrace_kill - kill ftrace
4448  *
4449  * This function should be used by panic code. It stops ftrace
4450  * but in a not so nice way. If you need to simply kill ftrace
4451  * from a non-atomic section, use ftrace_kill.
4452  */
4453 void ftrace_kill(void)
4454 {
4455         ftrace_disabled = 1;
4456         ftrace_enabled = 0;
4457         clear_ftrace_function();
4458 }
4459
4460 /**
4461  * Test if ftrace is dead or not.
4462  */
4463 int ftrace_is_dead(void)
4464 {
4465         return ftrace_disabled;
4466 }
4467
4468 /**
4469  * register_ftrace_function - register a function for profiling
4470  * @ops - ops structure that holds the function for profiling.
4471  *
4472  * Register a function to be called by all functions in the
4473  * kernel.
4474  *
4475  * Note: @ops->func and all the functions it calls must be labeled
4476  *       with "notrace", otherwise it will go into a
4477  *       recursive loop.
4478  */
4479 int register_ftrace_function(struct ftrace_ops *ops)
4480 {
4481         int ret = -1;
4482
4483         mutex_lock(&ftrace_lock);
4484
4485         ret = __register_ftrace_function(ops);
4486         if (!ret)
4487                 ret = ftrace_startup(ops, 0);
4488
4489         mutex_unlock(&ftrace_lock);
4490
4491         return ret;
4492 }
4493 EXPORT_SYMBOL_GPL(register_ftrace_function);
4494
4495 /**
4496  * unregister_ftrace_function - unregister a function for profiling.
4497  * @ops - ops structure that holds the function to unregister
4498  *
4499  * Unregister a function that was added to be called by ftrace profiling.
4500  */
4501 int unregister_ftrace_function(struct ftrace_ops *ops)
4502 {
4503         int ret;
4504
4505         mutex_lock(&ftrace_lock);
4506         ret = __unregister_ftrace_function(ops);
4507         if (!ret)
4508                 ftrace_shutdown(ops, 0);
4509         mutex_unlock(&ftrace_lock);
4510
4511         return ret;
4512 }
4513 EXPORT_SYMBOL_GPL(unregister_ftrace_function);
4514
4515 int
4516 ftrace_enable_sysctl(struct ctl_table *table, int write,
4517                      void __user *buffer, size_t *lenp,
4518                      loff_t *ppos)
4519 {
4520         int ret = -ENODEV;
4521
4522         mutex_lock(&ftrace_lock);
4523
4524         if (unlikely(ftrace_disabled))
4525                 goto out;
4526
4527         ret = proc_dointvec(table, write, buffer, lenp, ppos);
4528
4529         if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
4530                 goto out;
4531
4532         last_ftrace_enabled = !!ftrace_enabled;
4533
4534         if (ftrace_enabled) {
4535
4536                 ftrace_startup_sysctl();
4537
4538                 /* we are starting ftrace again */
4539                 if (ftrace_ops_list != &ftrace_list_end)
4540                         update_ftrace_function();
4541
4542         } else {
4543                 /* stopping ftrace calls (just send to ftrace_stub) */
4544                 ftrace_trace_function = ftrace_stub;
4545
4546                 ftrace_shutdown_sysctl();
4547         }
4548
4549  out:
4550         mutex_unlock(&ftrace_lock);
4551         return ret;
4552 }
4553
4554 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
4555
4556 static int ftrace_graph_active;
4557 static struct notifier_block ftrace_suspend_notifier;
4558
4559 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4560 {
4561         return 0;
4562 }
4563
4564 /* The callbacks that hook a function */
4565 trace_func_graph_ret_t ftrace_graph_return =
4566                         (trace_func_graph_ret_t)ftrace_stub;
4567 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
4568
4569 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4570 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4571 {
4572         int i;
4573         int ret = 0;
4574         unsigned long flags;
4575         int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4576         struct task_struct *g, *t;
4577
4578         for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4579                 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4580                                         * sizeof(struct ftrace_ret_stack),
4581                                         GFP_KERNEL);
4582                 if (!ret_stack_list[i]) {
4583                         start = 0;
4584                         end = i;
4585                         ret = -ENOMEM;
4586                         goto free;
4587                 }
4588         }
4589
4590         read_lock_irqsave(&tasklist_lock, flags);
4591         do_each_thread(g, t) {
4592                 if (start == end) {
4593                         ret = -EAGAIN;
4594                         goto unlock;
4595                 }
4596
4597                 if (t->ret_stack == NULL) {
4598                         atomic_set(&t->tracing_graph_pause, 0);
4599                         atomic_set(&t->trace_overrun, 0);
4600                         t->curr_ret_stack = -1;
4601                         /* Make sure the tasks see the -1 first: */
4602                         smp_wmb();
4603                         t->ret_stack = ret_stack_list[start++];
4604                 }
4605         } while_each_thread(g, t);
4606
4607 unlock:
4608         read_unlock_irqrestore(&tasklist_lock, flags);
4609 free:
4610         for (i = start; i < end; i++)
4611                 kfree(ret_stack_list[i]);
4612         return ret;
4613 }
4614
4615 static void
4616 ftrace_graph_probe_sched_switch(void *ignore,
4617                         struct task_struct *prev, struct task_struct *next)
4618 {
4619         unsigned long long timestamp;
4620         int index;
4621
4622         /*
4623          * Does the user want to count the time a function was asleep.
4624          * If so, do not update the time stamps.
4625          */
4626         if (trace_flags & TRACE_ITER_SLEEP_TIME)
4627                 return;
4628
4629         timestamp = trace_clock_local();
4630
4631         prev->ftrace_timestamp = timestamp;
4632
4633         /* only process tasks that we timestamped */
4634         if (!next->ftrace_timestamp)
4635                 return;
4636
4637         /*
4638          * Update all the counters in next to make up for the
4639          * time next was sleeping.
4640          */
4641         timestamp -= next->ftrace_timestamp;
4642
4643         for (index = next->curr_ret_stack; index >= 0; index--)
4644                 next->ret_stack[index].calltime += timestamp;
4645 }
4646
4647 /* Allocate a return stack for each task */
4648 static int start_graph_tracing(void)
4649 {
4650         struct ftrace_ret_stack **ret_stack_list;
4651         int ret, cpu;
4652
4653         ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4654                                 sizeof(struct ftrace_ret_stack *),
4655                                 GFP_KERNEL);
4656
4657         if (!ret_stack_list)
4658                 return -ENOMEM;
4659
4660         /* The cpu_boot init_task->ret_stack will never be freed */
4661         for_each_online_cpu(cpu) {
4662                 if (!idle_task(cpu)->ret_stack)
4663                         ftrace_graph_init_idle_task(idle_task(cpu), cpu);
4664         }
4665
4666         do {
4667                 ret = alloc_retstack_tasklist(ret_stack_list);
4668         } while (ret == -EAGAIN);
4669
4670         if (!ret) {
4671                 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
4672                 if (ret)
4673                         pr_info("ftrace_graph: Couldn't activate tracepoint"
4674                                 " probe to kernel_sched_switch\n");
4675         }
4676
4677         kfree(ret_stack_list);
4678         return ret;
4679 }
4680
4681 /*
4682  * Hibernation protection.
4683  * The state of the current task is too much unstable during
4684  * suspend/restore to disk. We want to protect against that.
4685  */
4686 static int
4687 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4688                                                         void *unused)
4689 {
4690         switch (state) {
4691         case PM_HIBERNATION_PREPARE:
4692                 pause_graph_tracing();
4693                 break;
4694
4695         case PM_POST_HIBERNATION:
4696                 unpause_graph_tracing();
4697                 break;
4698         }
4699         return NOTIFY_DONE;
4700 }
4701
4702 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4703                         trace_func_graph_ent_t entryfunc)
4704 {
4705         int ret = 0;
4706
4707         mutex_lock(&ftrace_lock);
4708
4709         /* we currently allow only one tracer registered at a time */
4710         if (ftrace_graph_active) {
4711                 ret = -EBUSY;
4712                 goto out;
4713         }
4714
4715         ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
4716         register_pm_notifier(&ftrace_suspend_notifier);
4717
4718         ftrace_graph_active++;
4719         ret = start_graph_tracing();
4720         if (ret) {
4721                 ftrace_graph_active--;
4722                 goto out;
4723         }
4724
4725         ftrace_graph_return = retfunc;
4726         ftrace_graph_entry = entryfunc;
4727
4728         ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
4729
4730 out:
4731         mutex_unlock(&ftrace_lock);
4732         return ret;
4733 }
4734
4735 void unregister_ftrace_graph(void)
4736 {
4737         mutex_lock(&ftrace_lock);
4738
4739         if (unlikely(!ftrace_graph_active))
4740                 goto out;
4741
4742         ftrace_graph_active--;
4743         ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
4744         ftrace_graph_entry = ftrace_graph_entry_stub;
4745         ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
4746         unregister_pm_notifier(&ftrace_suspend_notifier);
4747         unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
4748
4749  out:
4750         mutex_unlock(&ftrace_lock);
4751 }
4752
4753 static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
4754
4755 static void
4756 graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
4757 {
4758         atomic_set(&t->tracing_graph_pause, 0);
4759         atomic_set(&t->trace_overrun, 0);
4760         t->ftrace_timestamp = 0;
4761         /* make curr_ret_stack visible before we add the ret_stack */
4762         smp_wmb();
4763         t->ret_stack = ret_stack;
4764 }
4765
4766 /*
4767  * Allocate a return stack for the idle task. May be the first
4768  * time through, or it may be done by CPU hotplug online.
4769  */
4770 void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
4771 {
4772         t->curr_ret_stack = -1;
4773         /*
4774          * The idle task has no parent, it either has its own
4775          * stack or no stack at all.
4776          */
4777         if (t->ret_stack)
4778                 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
4779
4780         if (ftrace_graph_active) {
4781                 struct ftrace_ret_stack *ret_stack;
4782
4783                 ret_stack = per_cpu(idle_ret_stack, cpu);
4784                 if (!ret_stack) {
4785                         ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4786                                             * sizeof(struct ftrace_ret_stack),
4787                                             GFP_KERNEL);
4788                         if (!ret_stack)
4789                                 return;
4790                         per_cpu(idle_ret_stack, cpu) = ret_stack;
4791                 }
4792                 graph_init_task(t, ret_stack);
4793         }
4794 }
4795
4796 /* Allocate a return stack for newly created task */
4797 void ftrace_graph_init_task(struct task_struct *t)
4798 {
4799         /* Make sure we do not use the parent ret_stack */
4800         t->ret_stack = NULL;
4801         t->curr_ret_stack = -1;
4802
4803         if (ftrace_graph_active) {
4804                 struct ftrace_ret_stack *ret_stack;
4805
4806                 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4807                                 * sizeof(struct ftrace_ret_stack),
4808                                 GFP_KERNEL);
4809                 if (!ret_stack)
4810                         return;
4811                 graph_init_task(t, ret_stack);
4812         }
4813 }
4814
4815 void ftrace_graph_exit_task(struct task_struct *t)
4816 {
4817         struct ftrace_ret_stack *ret_stack = t->ret_stack;
4818
4819         t->ret_stack = NULL;
4820         /* NULL must become visible to IRQs before we free it: */
4821         barrier();
4822
4823         kfree(ret_stack);
4824 }
4825
4826 void ftrace_graph_stop(void)
4827 {
4828         ftrace_stop();
4829 }
4830 #endif