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