ftrace: fix !CONFIG_DYNAMIC_FTRACE ftrace_swapper_pid definition
[safe/jmp/linux-2.6] / 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/kprobes.h>
26 #include <linux/ftrace.h>
27 #include <linux/sysctl.h>
28 #include <linux/ctype.h>
29 #include <linux/list.h>
30 #include <linux/hash.h>
31
32 #include <asm/ftrace.h>
33
34 #include "trace.h"
35
36 #define FTRACE_WARN_ON(cond)                    \
37         do {                                    \
38                 if (WARN_ON(cond))              \
39                         ftrace_kill();          \
40         } while (0)
41
42 #define FTRACE_WARN_ON_ONCE(cond)               \
43         do {                                    \
44                 if (WARN_ON_ONCE(cond))         \
45                         ftrace_kill();          \
46         } while (0)
47
48 /* hash bits for specific function selection */
49 #define FTRACE_HASH_BITS 7
50 #define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
51
52 /* ftrace_enabled is a method to turn ftrace on or off */
53 int ftrace_enabled __read_mostly;
54 static int last_ftrace_enabled;
55
56 /* Quick disabling of function tracer. */
57 int function_trace_stop;
58
59 /*
60  * ftrace_disabled is set when an anomaly is discovered.
61  * ftrace_disabled is much stronger than ftrace_enabled.
62  */
63 static int ftrace_disabled __read_mostly;
64
65 static DEFINE_MUTEX(ftrace_lock);
66
67 static struct ftrace_ops ftrace_list_end __read_mostly =
68 {
69         .func = ftrace_stub,
70 };
71
72 static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
73 ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
74 ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
75 ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
76
77 static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
78 {
79         struct ftrace_ops *op = ftrace_list;
80
81         /* in case someone actually ports this to alpha! */
82         read_barrier_depends();
83
84         while (op != &ftrace_list_end) {
85                 /* silly alpha */
86                 read_barrier_depends();
87                 op->func(ip, parent_ip);
88                 op = op->next;
89         };
90 }
91
92 static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
93 {
94         if (!test_tsk_trace_trace(current))
95                 return;
96
97         ftrace_pid_function(ip, parent_ip);
98 }
99
100 static void set_ftrace_pid_function(ftrace_func_t func)
101 {
102         /* do not set ftrace_pid_function to itself! */
103         if (func != ftrace_pid_func)
104                 ftrace_pid_function = func;
105 }
106
107 /**
108  * clear_ftrace_function - reset the ftrace function
109  *
110  * This NULLs the ftrace function and in essence stops
111  * tracing.  There may be lag
112  */
113 void clear_ftrace_function(void)
114 {
115         ftrace_trace_function = ftrace_stub;
116         __ftrace_trace_function = ftrace_stub;
117         ftrace_pid_function = ftrace_stub;
118 }
119
120 #ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
121 /*
122  * For those archs that do not test ftrace_trace_stop in their
123  * mcount call site, we need to do it from C.
124  */
125 static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
126 {
127         if (function_trace_stop)
128                 return;
129
130         __ftrace_trace_function(ip, parent_ip);
131 }
132 #endif
133
134 static int __register_ftrace_function(struct ftrace_ops *ops)
135 {
136         ops->next = ftrace_list;
137         /*
138          * We are entering ops into the ftrace_list but another
139          * CPU might be walking that list. We need to make sure
140          * the ops->next pointer is valid before another CPU sees
141          * the ops pointer included into the ftrace_list.
142          */
143         smp_wmb();
144         ftrace_list = ops;
145
146         if (ftrace_enabled) {
147                 ftrace_func_t func;
148
149                 if (ops->next == &ftrace_list_end)
150                         func = ops->func;
151                 else
152                         func = ftrace_list_func;
153
154                 if (ftrace_pid_trace) {
155                         set_ftrace_pid_function(func);
156                         func = ftrace_pid_func;
157                 }
158
159                 /*
160                  * For one func, simply call it directly.
161                  * For more than one func, call the chain.
162                  */
163 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
164                 ftrace_trace_function = func;
165 #else
166                 __ftrace_trace_function = func;
167                 ftrace_trace_function = ftrace_test_stop_func;
168 #endif
169         }
170
171         return 0;
172 }
173
174 static int __unregister_ftrace_function(struct ftrace_ops *ops)
175 {
176         struct ftrace_ops **p;
177
178         /*
179          * If we are removing the last function, then simply point
180          * to the ftrace_stub.
181          */
182         if (ftrace_list == ops && ops->next == &ftrace_list_end) {
183                 ftrace_trace_function = ftrace_stub;
184                 ftrace_list = &ftrace_list_end;
185                 return 0;
186         }
187
188         for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
189                 if (*p == ops)
190                         break;
191
192         if (*p != ops)
193                 return -1;
194
195         *p = (*p)->next;
196
197         if (ftrace_enabled) {
198                 /* If we only have one func left, then call that directly */
199                 if (ftrace_list->next == &ftrace_list_end) {
200                         ftrace_func_t func = ftrace_list->func;
201
202                         if (ftrace_pid_trace) {
203                                 set_ftrace_pid_function(func);
204                                 func = ftrace_pid_func;
205                         }
206 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
207                         ftrace_trace_function = func;
208 #else
209                         __ftrace_trace_function = func;
210 #endif
211                 }
212         }
213
214         return 0;
215 }
216
217 static void ftrace_update_pid_func(void)
218 {
219         ftrace_func_t func;
220
221         mutex_lock(&ftrace_lock);
222
223         if (ftrace_trace_function == ftrace_stub)
224                 goto out;
225
226         func = ftrace_trace_function;
227
228         if (ftrace_pid_trace) {
229                 set_ftrace_pid_function(func);
230                 func = ftrace_pid_func;
231         } else {
232                 if (func == ftrace_pid_func)
233                         func = ftrace_pid_function;
234         }
235
236 #ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
237         ftrace_trace_function = func;
238 #else
239         __ftrace_trace_function = func;
240 #endif
241
242  out:
243         mutex_unlock(&ftrace_lock);
244 }
245
246 /* set when tracing only a pid */
247 struct pid *ftrace_pid_trace;
248 static struct pid * const ftrace_swapper_pid = &init_struct_pid;
249
250 #ifdef CONFIG_DYNAMIC_FTRACE
251
252 #ifndef CONFIG_FTRACE_MCOUNT_RECORD
253 # error Dynamic ftrace depends on MCOUNT_RECORD
254 #endif
255
256 static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
257
258 struct ftrace_func_hook {
259         struct hlist_node       node;
260         struct ftrace_hook_ops  *ops;
261         unsigned long           flags;
262         unsigned long           ip;
263         void                    *data;
264         struct rcu_head         rcu;
265 };
266
267
268 enum {
269         FTRACE_ENABLE_CALLS             = (1 << 0),
270         FTRACE_DISABLE_CALLS            = (1 << 1),
271         FTRACE_UPDATE_TRACE_FUNC        = (1 << 2),
272         FTRACE_ENABLE_MCOUNT            = (1 << 3),
273         FTRACE_DISABLE_MCOUNT           = (1 << 4),
274         FTRACE_START_FUNC_RET           = (1 << 5),
275         FTRACE_STOP_FUNC_RET            = (1 << 6),
276 };
277
278 static int ftrace_filtered;
279
280 static LIST_HEAD(ftrace_new_addrs);
281
282 static DEFINE_MUTEX(ftrace_regex_lock);
283
284 struct ftrace_page {
285         struct ftrace_page      *next;
286         int                     index;
287         struct dyn_ftrace       records[];
288 };
289
290 #define ENTRIES_PER_PAGE \
291   ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
292
293 /* estimate from running different kernels */
294 #define NR_TO_INIT              10000
295
296 static struct ftrace_page       *ftrace_pages_start;
297 static struct ftrace_page       *ftrace_pages;
298
299 static struct dyn_ftrace *ftrace_free_records;
300
301 /*
302  * This is a double for. Do not use 'break' to break out of the loop,
303  * you must use a goto.
304  */
305 #define do_for_each_ftrace_rec(pg, rec)                                 \
306         for (pg = ftrace_pages_start; pg; pg = pg->next) {              \
307                 int _____i;                                             \
308                 for (_____i = 0; _____i < pg->index; _____i++) {        \
309                         rec = &pg->records[_____i];
310
311 #define while_for_each_ftrace_rec()             \
312                 }                               \
313         }
314
315 #ifdef CONFIG_KPROBES
316
317 static int frozen_record_count;
318
319 static inline void freeze_record(struct dyn_ftrace *rec)
320 {
321         if (!(rec->flags & FTRACE_FL_FROZEN)) {
322                 rec->flags |= FTRACE_FL_FROZEN;
323                 frozen_record_count++;
324         }
325 }
326
327 static inline void unfreeze_record(struct dyn_ftrace *rec)
328 {
329         if (rec->flags & FTRACE_FL_FROZEN) {
330                 rec->flags &= ~FTRACE_FL_FROZEN;
331                 frozen_record_count--;
332         }
333 }
334
335 static inline int record_frozen(struct dyn_ftrace *rec)
336 {
337         return rec->flags & FTRACE_FL_FROZEN;
338 }
339 #else
340 # define freeze_record(rec)                     ({ 0; })
341 # define unfreeze_record(rec)                   ({ 0; })
342 # define record_frozen(rec)                     ({ 0; })
343 #endif /* CONFIG_KPROBES */
344
345 static void ftrace_free_rec(struct dyn_ftrace *rec)
346 {
347         rec->ip = (unsigned long)ftrace_free_records;
348         ftrace_free_records = rec;
349         rec->flags |= FTRACE_FL_FREE;
350 }
351
352 void ftrace_release(void *start, unsigned long size)
353 {
354         struct dyn_ftrace *rec;
355         struct ftrace_page *pg;
356         unsigned long s = (unsigned long)start;
357         unsigned long e = s + size;
358
359         if (ftrace_disabled || !start)
360                 return;
361
362         mutex_lock(&ftrace_lock);
363         do_for_each_ftrace_rec(pg, rec) {
364                 if ((rec->ip >= s) && (rec->ip < e))
365                         ftrace_free_rec(rec);
366         } while_for_each_ftrace_rec();
367         mutex_unlock(&ftrace_lock);
368 }
369
370 static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
371 {
372         struct dyn_ftrace *rec;
373
374         /* First check for freed records */
375         if (ftrace_free_records) {
376                 rec = ftrace_free_records;
377
378                 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
379                         FTRACE_WARN_ON_ONCE(1);
380                         ftrace_free_records = NULL;
381                         return NULL;
382                 }
383
384                 ftrace_free_records = (void *)rec->ip;
385                 memset(rec, 0, sizeof(*rec));
386                 return rec;
387         }
388
389         if (ftrace_pages->index == ENTRIES_PER_PAGE) {
390                 if (!ftrace_pages->next) {
391                         /* allocate another page */
392                         ftrace_pages->next =
393                                 (void *)get_zeroed_page(GFP_KERNEL);
394                         if (!ftrace_pages->next)
395                                 return NULL;
396                 }
397                 ftrace_pages = ftrace_pages->next;
398         }
399
400         return &ftrace_pages->records[ftrace_pages->index++];
401 }
402
403 static struct dyn_ftrace *
404 ftrace_record_ip(unsigned long ip)
405 {
406         struct dyn_ftrace *rec;
407
408         if (ftrace_disabled)
409                 return NULL;
410
411         rec = ftrace_alloc_dyn_node(ip);
412         if (!rec)
413                 return NULL;
414
415         rec->ip = ip;
416
417         list_add(&rec->list, &ftrace_new_addrs);
418
419         return rec;
420 }
421
422 static void print_ip_ins(const char *fmt, unsigned char *p)
423 {
424         int i;
425
426         printk(KERN_CONT "%s", fmt);
427
428         for (i = 0; i < MCOUNT_INSN_SIZE; i++)
429                 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
430 }
431
432 static void ftrace_bug(int failed, unsigned long ip)
433 {
434         switch (failed) {
435         case -EFAULT:
436                 FTRACE_WARN_ON_ONCE(1);
437                 pr_info("ftrace faulted on modifying ");
438                 print_ip_sym(ip);
439                 break;
440         case -EINVAL:
441                 FTRACE_WARN_ON_ONCE(1);
442                 pr_info("ftrace failed to modify ");
443                 print_ip_sym(ip);
444                 print_ip_ins(" actual: ", (unsigned char *)ip);
445                 printk(KERN_CONT "\n");
446                 break;
447         case -EPERM:
448                 FTRACE_WARN_ON_ONCE(1);
449                 pr_info("ftrace faulted on writing ");
450                 print_ip_sym(ip);
451                 break;
452         default:
453                 FTRACE_WARN_ON_ONCE(1);
454                 pr_info("ftrace faulted on unknown error ");
455                 print_ip_sym(ip);
456         }
457 }
458
459
460 static int
461 __ftrace_replace_code(struct dyn_ftrace *rec, int enable)
462 {
463         unsigned long ip, fl;
464         unsigned long ftrace_addr;
465
466         ftrace_addr = (unsigned long)FTRACE_ADDR;
467
468         ip = rec->ip;
469
470         /*
471          * If this record is not to be traced and
472          * it is not enabled then do nothing.
473          *
474          * If this record is not to be traced and
475          * it is enabled then disable it.
476          *
477          */
478         if (rec->flags & FTRACE_FL_NOTRACE) {
479                 if (rec->flags & FTRACE_FL_ENABLED)
480                         rec->flags &= ~FTRACE_FL_ENABLED;
481                 else
482                         return 0;
483
484         } else if (ftrace_filtered && enable) {
485                 /*
486                  * Filtering is on:
487                  */
488
489                 fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_ENABLED);
490
491                 /* Record is filtered and enabled, do nothing */
492                 if (fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED))
493                         return 0;
494
495                 /* Record is not filtered or enabled, do nothing */
496                 if (!fl)
497                         return 0;
498
499                 /* Record is not filtered but enabled, disable it */
500                 if (fl == FTRACE_FL_ENABLED)
501                         rec->flags &= ~FTRACE_FL_ENABLED;
502                 else
503                 /* Otherwise record is filtered but not enabled, enable it */
504                         rec->flags |= FTRACE_FL_ENABLED;
505         } else {
506                 /* Disable or not filtered */
507
508                 if (enable) {
509                         /* if record is enabled, do nothing */
510                         if (rec->flags & FTRACE_FL_ENABLED)
511                                 return 0;
512
513                         rec->flags |= FTRACE_FL_ENABLED;
514
515                 } else {
516
517                         /* if record is not enabled, do nothing */
518                         if (!(rec->flags & FTRACE_FL_ENABLED))
519                                 return 0;
520
521                         rec->flags &= ~FTRACE_FL_ENABLED;
522                 }
523         }
524
525         if (rec->flags & FTRACE_FL_ENABLED)
526                 return ftrace_make_call(rec, ftrace_addr);
527         else
528                 return ftrace_make_nop(NULL, rec, ftrace_addr);
529 }
530
531 static void ftrace_replace_code(int enable)
532 {
533         int failed;
534         struct dyn_ftrace *rec;
535         struct ftrace_page *pg;
536
537         do_for_each_ftrace_rec(pg, rec) {
538                 /*
539                  * Skip over free records and records that have
540                  * failed.
541                  */
542                 if (rec->flags & FTRACE_FL_FREE ||
543                     rec->flags & FTRACE_FL_FAILED)
544                         continue;
545
546                 /* ignore updates to this record's mcount site */
547                 if (get_kprobe((void *)rec->ip)) {
548                         freeze_record(rec);
549                         continue;
550                 } else {
551                         unfreeze_record(rec);
552                 }
553
554                 failed = __ftrace_replace_code(rec, enable);
555                 if (failed && (rec->flags & FTRACE_FL_CONVERTED)) {
556                         rec->flags |= FTRACE_FL_FAILED;
557                         if ((system_state == SYSTEM_BOOTING) ||
558                             !core_kernel_text(rec->ip)) {
559                                 ftrace_free_rec(rec);
560                         } else
561                                 ftrace_bug(failed, rec->ip);
562                 }
563         } while_for_each_ftrace_rec();
564 }
565
566 static int
567 ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
568 {
569         unsigned long ip;
570         int ret;
571
572         ip = rec->ip;
573
574         ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
575         if (ret) {
576                 ftrace_bug(ret, ip);
577                 rec->flags |= FTRACE_FL_FAILED;
578                 return 0;
579         }
580         return 1;
581 }
582
583 static int __ftrace_modify_code(void *data)
584 {
585         int *command = data;
586
587         if (*command & FTRACE_ENABLE_CALLS)
588                 ftrace_replace_code(1);
589         else if (*command & FTRACE_DISABLE_CALLS)
590                 ftrace_replace_code(0);
591
592         if (*command & FTRACE_UPDATE_TRACE_FUNC)
593                 ftrace_update_ftrace_func(ftrace_trace_function);
594
595         if (*command & FTRACE_START_FUNC_RET)
596                 ftrace_enable_ftrace_graph_caller();
597         else if (*command & FTRACE_STOP_FUNC_RET)
598                 ftrace_disable_ftrace_graph_caller();
599
600         return 0;
601 }
602
603 static void ftrace_run_update_code(int command)
604 {
605         stop_machine(__ftrace_modify_code, &command, NULL);
606 }
607
608 static ftrace_func_t saved_ftrace_func;
609 static int ftrace_start_up;
610
611 static void ftrace_startup_enable(int command)
612 {
613         if (saved_ftrace_func != ftrace_trace_function) {
614                 saved_ftrace_func = ftrace_trace_function;
615                 command |= FTRACE_UPDATE_TRACE_FUNC;
616         }
617
618         if (!command || !ftrace_enabled)
619                 return;
620
621         ftrace_run_update_code(command);
622 }
623
624 static void ftrace_startup(int command)
625 {
626         if (unlikely(ftrace_disabled))
627                 return;
628
629         ftrace_start_up++;
630         command |= FTRACE_ENABLE_CALLS;
631
632         ftrace_startup_enable(command);
633 }
634
635 static void ftrace_shutdown(int command)
636 {
637         if (unlikely(ftrace_disabled))
638                 return;
639
640         ftrace_start_up--;
641         if (!ftrace_start_up)
642                 command |= FTRACE_DISABLE_CALLS;
643
644         if (saved_ftrace_func != ftrace_trace_function) {
645                 saved_ftrace_func = ftrace_trace_function;
646                 command |= FTRACE_UPDATE_TRACE_FUNC;
647         }
648
649         if (!command || !ftrace_enabled)
650                 return;
651
652         ftrace_run_update_code(command);
653 }
654
655 static void ftrace_startup_sysctl(void)
656 {
657         int command = FTRACE_ENABLE_MCOUNT;
658
659         if (unlikely(ftrace_disabled))
660                 return;
661
662         /* Force update next time */
663         saved_ftrace_func = NULL;
664         /* ftrace_start_up is true if we want ftrace running */
665         if (ftrace_start_up)
666                 command |= FTRACE_ENABLE_CALLS;
667
668         ftrace_run_update_code(command);
669 }
670
671 static void ftrace_shutdown_sysctl(void)
672 {
673         int command = FTRACE_DISABLE_MCOUNT;
674
675         if (unlikely(ftrace_disabled))
676                 return;
677
678         /* ftrace_start_up is true if ftrace is running */
679         if (ftrace_start_up)
680                 command |= FTRACE_DISABLE_CALLS;
681
682         ftrace_run_update_code(command);
683 }
684
685 static cycle_t          ftrace_update_time;
686 static unsigned long    ftrace_update_cnt;
687 unsigned long           ftrace_update_tot_cnt;
688
689 static int ftrace_update_code(struct module *mod)
690 {
691         struct dyn_ftrace *p, *t;
692         cycle_t start, stop;
693
694         start = ftrace_now(raw_smp_processor_id());
695         ftrace_update_cnt = 0;
696
697         list_for_each_entry_safe(p, t, &ftrace_new_addrs, list) {
698
699                 /* If something went wrong, bail without enabling anything */
700                 if (unlikely(ftrace_disabled))
701                         return -1;
702
703                 list_del_init(&p->list);
704
705                 /* convert record (i.e, patch mcount-call with NOP) */
706                 if (ftrace_code_disable(mod, p)) {
707                         p->flags |= FTRACE_FL_CONVERTED;
708                         ftrace_update_cnt++;
709                 } else
710                         ftrace_free_rec(p);
711         }
712
713         stop = ftrace_now(raw_smp_processor_id());
714         ftrace_update_time = stop - start;
715         ftrace_update_tot_cnt += ftrace_update_cnt;
716
717         return 0;
718 }
719
720 static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
721 {
722         struct ftrace_page *pg;
723         int cnt;
724         int i;
725
726         /* allocate a few pages */
727         ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
728         if (!ftrace_pages_start)
729                 return -1;
730
731         /*
732          * Allocate a few more pages.
733          *
734          * TODO: have some parser search vmlinux before
735          *   final linking to find all calls to ftrace.
736          *   Then we can:
737          *    a) know how many pages to allocate.
738          *     and/or
739          *    b) set up the table then.
740          *
741          *  The dynamic code is still necessary for
742          *  modules.
743          */
744
745         pg = ftrace_pages = ftrace_pages_start;
746
747         cnt = num_to_init / ENTRIES_PER_PAGE;
748         pr_info("ftrace: allocating %ld entries in %d pages\n",
749                 num_to_init, cnt + 1);
750
751         for (i = 0; i < cnt; i++) {
752                 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
753
754                 /* If we fail, we'll try later anyway */
755                 if (!pg->next)
756                         break;
757
758                 pg = pg->next;
759         }
760
761         return 0;
762 }
763
764 enum {
765         FTRACE_ITER_FILTER      = (1 << 0),
766         FTRACE_ITER_CONT        = (1 << 1),
767         FTRACE_ITER_NOTRACE     = (1 << 2),
768         FTRACE_ITER_FAILURES    = (1 << 3),
769         FTRACE_ITER_PRINTALL    = (1 << 4),
770         FTRACE_ITER_HASH        = (1 << 5),
771 };
772
773 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
774
775 struct ftrace_iterator {
776         struct ftrace_page      *pg;
777         int                     hidx;
778         int                     idx;
779         unsigned                flags;
780         unsigned char           buffer[FTRACE_BUFF_MAX+1];
781         unsigned                buffer_idx;
782         unsigned                filtered;
783 };
784
785 static void *
786 t_hash_next(struct seq_file *m, void *v, loff_t *pos)
787 {
788         struct ftrace_iterator *iter = m->private;
789         struct hlist_node *hnd = v;
790         struct hlist_head *hhd;
791
792         WARN_ON(!(iter->flags & FTRACE_ITER_HASH));
793
794         (*pos)++;
795
796  retry:
797         if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
798                 return NULL;
799
800         hhd = &ftrace_func_hash[iter->hidx];
801
802         if (hlist_empty(hhd)) {
803                 iter->hidx++;
804                 hnd = NULL;
805                 goto retry;
806         }
807
808         if (!hnd)
809                 hnd = hhd->first;
810         else {
811                 hnd = hnd->next;
812                 if (!hnd) {
813                         iter->hidx++;
814                         goto retry;
815                 }
816         }
817
818         return hnd;
819 }
820
821 static void *t_hash_start(struct seq_file *m, loff_t *pos)
822 {
823         struct ftrace_iterator *iter = m->private;
824         void *p = NULL;
825
826         iter->flags |= FTRACE_ITER_HASH;
827
828         return t_hash_next(m, p, pos);
829 }
830
831 static int t_hash_show(struct seq_file *m, void *v)
832 {
833         struct ftrace_func_hook *rec;
834         struct hlist_node *hnd = v;
835         char str[KSYM_SYMBOL_LEN];
836
837         rec = hlist_entry(hnd, struct ftrace_func_hook, node);
838
839         if (rec->ops->print)
840                 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
841
842         kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
843         seq_printf(m, "%s:", str);
844
845         kallsyms_lookup((unsigned long)rec->ops->func, NULL, NULL, NULL, str);
846         seq_printf(m, "%s", str);
847
848         if (rec->data)
849                 seq_printf(m, ":%p", rec->data);
850         seq_putc(m, '\n');
851
852         return 0;
853 }
854
855 static void *
856 t_next(struct seq_file *m, void *v, loff_t *pos)
857 {
858         struct ftrace_iterator *iter = m->private;
859         struct dyn_ftrace *rec = NULL;
860
861         if (iter->flags & FTRACE_ITER_HASH)
862                 return t_hash_next(m, v, pos);
863
864         (*pos)++;
865
866         if (iter->flags & FTRACE_ITER_PRINTALL)
867                 return NULL;
868
869  retry:
870         if (iter->idx >= iter->pg->index) {
871                 if (iter->pg->next) {
872                         iter->pg = iter->pg->next;
873                         iter->idx = 0;
874                         goto retry;
875                 } else {
876                         iter->idx = -1;
877                 }
878         } else {
879                 rec = &iter->pg->records[iter->idx++];
880                 if ((rec->flags & FTRACE_FL_FREE) ||
881
882                     (!(iter->flags & FTRACE_ITER_FAILURES) &&
883                      (rec->flags & FTRACE_FL_FAILED)) ||
884
885                     ((iter->flags & FTRACE_ITER_FAILURES) &&
886                      !(rec->flags & FTRACE_FL_FAILED)) ||
887
888                     ((iter->flags & FTRACE_ITER_FILTER) &&
889                      !(rec->flags & FTRACE_FL_FILTER)) ||
890
891                     ((iter->flags & FTRACE_ITER_NOTRACE) &&
892                      !(rec->flags & FTRACE_FL_NOTRACE))) {
893                         rec = NULL;
894                         goto retry;
895                 }
896         }
897
898         return rec;
899 }
900
901 static void *t_start(struct seq_file *m, loff_t *pos)
902 {
903         struct ftrace_iterator *iter = m->private;
904         void *p = NULL;
905
906         mutex_lock(&ftrace_lock);
907         /*
908          * For set_ftrace_filter reading, if we have the filter
909          * off, we can short cut and just print out that all
910          * functions are enabled.
911          */
912         if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
913                 if (*pos > 0)
914                         return t_hash_start(m, pos);
915                 iter->flags |= FTRACE_ITER_PRINTALL;
916                 (*pos)++;
917                 return iter;
918         }
919
920         if (iter->flags & FTRACE_ITER_HASH)
921                 return t_hash_start(m, pos);
922
923         if (*pos > 0) {
924                 if (iter->idx < 0)
925                         return p;
926                 (*pos)--;
927                 iter->idx--;
928         }
929
930         p = t_next(m, p, pos);
931
932         if (!p)
933                 return t_hash_start(m, pos);
934
935         return p;
936 }
937
938 static void t_stop(struct seq_file *m, void *p)
939 {
940         mutex_unlock(&ftrace_lock);
941 }
942
943 static int t_show(struct seq_file *m, void *v)
944 {
945         struct ftrace_iterator *iter = m->private;
946         struct dyn_ftrace *rec = v;
947         char str[KSYM_SYMBOL_LEN];
948
949         if (iter->flags & FTRACE_ITER_HASH)
950                 return t_hash_show(m, v);
951
952         if (iter->flags & FTRACE_ITER_PRINTALL) {
953                 seq_printf(m, "#### all functions enabled ####\n");
954                 return 0;
955         }
956
957         if (!rec)
958                 return 0;
959
960         kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
961
962         seq_printf(m, "%s\n", str);
963
964         return 0;
965 }
966
967 static struct seq_operations show_ftrace_seq_ops = {
968         .start = t_start,
969         .next = t_next,
970         .stop = t_stop,
971         .show = t_show,
972 };
973
974 static int
975 ftrace_avail_open(struct inode *inode, struct file *file)
976 {
977         struct ftrace_iterator *iter;
978         int ret;
979
980         if (unlikely(ftrace_disabled))
981                 return -ENODEV;
982
983         iter = kzalloc(sizeof(*iter), GFP_KERNEL);
984         if (!iter)
985                 return -ENOMEM;
986
987         iter->pg = ftrace_pages_start;
988
989         ret = seq_open(file, &show_ftrace_seq_ops);
990         if (!ret) {
991                 struct seq_file *m = file->private_data;
992
993                 m->private = iter;
994         } else {
995                 kfree(iter);
996         }
997
998         return ret;
999 }
1000
1001 int ftrace_avail_release(struct inode *inode, struct file *file)
1002 {
1003         struct seq_file *m = (struct seq_file *)file->private_data;
1004         struct ftrace_iterator *iter = m->private;
1005
1006         seq_release(inode, file);
1007         kfree(iter);
1008
1009         return 0;
1010 }
1011
1012 static int
1013 ftrace_failures_open(struct inode *inode, struct file *file)
1014 {
1015         int ret;
1016         struct seq_file *m;
1017         struct ftrace_iterator *iter;
1018
1019         ret = ftrace_avail_open(inode, file);
1020         if (!ret) {
1021                 m = (struct seq_file *)file->private_data;
1022                 iter = (struct ftrace_iterator *)m->private;
1023                 iter->flags = FTRACE_ITER_FAILURES;
1024         }
1025
1026         return ret;
1027 }
1028
1029
1030 static void ftrace_filter_reset(int enable)
1031 {
1032         struct ftrace_page *pg;
1033         struct dyn_ftrace *rec;
1034         unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1035
1036         mutex_lock(&ftrace_lock);
1037         if (enable)
1038                 ftrace_filtered = 0;
1039         do_for_each_ftrace_rec(pg, rec) {
1040                 if (rec->flags & FTRACE_FL_FAILED)
1041                         continue;
1042                 rec->flags &= ~type;
1043         } while_for_each_ftrace_rec();
1044         mutex_unlock(&ftrace_lock);
1045 }
1046
1047 static int
1048 ftrace_regex_open(struct inode *inode, struct file *file, int enable)
1049 {
1050         struct ftrace_iterator *iter;
1051         int ret = 0;
1052
1053         if (unlikely(ftrace_disabled))
1054                 return -ENODEV;
1055
1056         iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1057         if (!iter)
1058                 return -ENOMEM;
1059
1060         mutex_lock(&ftrace_regex_lock);
1061         if ((file->f_mode & FMODE_WRITE) &&
1062             !(file->f_flags & O_APPEND))
1063                 ftrace_filter_reset(enable);
1064
1065         if (file->f_mode & FMODE_READ) {
1066                 iter->pg = ftrace_pages_start;
1067                 iter->flags = enable ? FTRACE_ITER_FILTER :
1068                         FTRACE_ITER_NOTRACE;
1069
1070                 ret = seq_open(file, &show_ftrace_seq_ops);
1071                 if (!ret) {
1072                         struct seq_file *m = file->private_data;
1073                         m->private = iter;
1074                 } else
1075                         kfree(iter);
1076         } else
1077                 file->private_data = iter;
1078         mutex_unlock(&ftrace_regex_lock);
1079
1080         return ret;
1081 }
1082
1083 static int
1084 ftrace_filter_open(struct inode *inode, struct file *file)
1085 {
1086         return ftrace_regex_open(inode, file, 1);
1087 }
1088
1089 static int
1090 ftrace_notrace_open(struct inode *inode, struct file *file)
1091 {
1092         return ftrace_regex_open(inode, file, 0);
1093 }
1094
1095 static ssize_t
1096 ftrace_regex_read(struct file *file, char __user *ubuf,
1097                        size_t cnt, loff_t *ppos)
1098 {
1099         if (file->f_mode & FMODE_READ)
1100                 return seq_read(file, ubuf, cnt, ppos);
1101         else
1102                 return -EPERM;
1103 }
1104
1105 static loff_t
1106 ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
1107 {
1108         loff_t ret;
1109
1110         if (file->f_mode & FMODE_READ)
1111                 ret = seq_lseek(file, offset, origin);
1112         else
1113                 file->f_pos = ret = 1;
1114
1115         return ret;
1116 }
1117
1118 enum {
1119         MATCH_FULL,
1120         MATCH_FRONT_ONLY,
1121         MATCH_MIDDLE_ONLY,
1122         MATCH_END_ONLY,
1123 };
1124
1125 /*
1126  * (static function - no need for kernel doc)
1127  *
1128  * Pass in a buffer containing a glob and this function will
1129  * set search to point to the search part of the buffer and
1130  * return the type of search it is (see enum above).
1131  * This does modify buff.
1132  *
1133  * Returns enum type.
1134  *  search returns the pointer to use for comparison.
1135  *  not returns 1 if buff started with a '!'
1136  *     0 otherwise.
1137  */
1138 static int
1139 ftrace_setup_glob(char *buff, int len, char **search, int *not)
1140 {
1141         int type = MATCH_FULL;
1142         int i;
1143
1144         if (buff[0] == '!') {
1145                 *not = 1;
1146                 buff++;
1147                 len--;
1148         } else
1149                 *not = 0;
1150
1151         *search = buff;
1152
1153         for (i = 0; i < len; i++) {
1154                 if (buff[i] == '*') {
1155                         if (!i) {
1156                                 *search = buff + 1;
1157                                 type = MATCH_END_ONLY;
1158                         } else {
1159                                 if (type == MATCH_END_ONLY)
1160                                         type = MATCH_MIDDLE_ONLY;
1161                                 else
1162                                         type = MATCH_FRONT_ONLY;
1163                                 buff[i] = 0;
1164                                 break;
1165                         }
1166                 }
1167         }
1168
1169         return type;
1170 }
1171
1172 static int ftrace_match(char *str, char *regex, int len, int type)
1173 {
1174         int matched = 0;
1175         char *ptr;
1176
1177         switch (type) {
1178         case MATCH_FULL:
1179                 if (strcmp(str, regex) == 0)
1180                         matched = 1;
1181                 break;
1182         case MATCH_FRONT_ONLY:
1183                 if (strncmp(str, regex, len) == 0)
1184                         matched = 1;
1185                 break;
1186         case MATCH_MIDDLE_ONLY:
1187                 if (strstr(str, regex))
1188                         matched = 1;
1189                 break;
1190         case MATCH_END_ONLY:
1191                 ptr = strstr(str, regex);
1192                 if (ptr && (ptr[len] == 0))
1193                         matched = 1;
1194                 break;
1195         }
1196
1197         return matched;
1198 }
1199
1200 static int
1201 ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
1202 {
1203         char str[KSYM_SYMBOL_LEN];
1204
1205         kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1206         return ftrace_match(str, regex, len, type);
1207 }
1208
1209 static void ftrace_match_records(char *buff, int len, int enable)
1210 {
1211         char *search;
1212         struct ftrace_page *pg;
1213         struct dyn_ftrace *rec;
1214         int type;
1215         unsigned long flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1216         unsigned search_len;
1217         int not;
1218
1219         type = ftrace_setup_glob(buff, len, &search, &not);
1220
1221         search_len = strlen(search);
1222
1223         mutex_lock(&ftrace_lock);
1224         do_for_each_ftrace_rec(pg, rec) {
1225
1226                 if (rec->flags & FTRACE_FL_FAILED)
1227                         continue;
1228
1229                 if (ftrace_match_record(rec, search, search_len, type)) {
1230                         if (not)
1231                                 rec->flags &= ~flag;
1232                         else
1233                                 rec->flags |= flag;
1234                 }
1235                 /*
1236                  * Only enable filtering if we have a function that
1237                  * is filtered on.
1238                  */
1239                 if (enable && (rec->flags & FTRACE_FL_FILTER))
1240                         ftrace_filtered = 1;
1241         } while_for_each_ftrace_rec();
1242         mutex_unlock(&ftrace_lock);
1243 }
1244
1245 static int
1246 ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
1247                            char *regex, int len, int type)
1248 {
1249         char str[KSYM_SYMBOL_LEN];
1250         char *modname;
1251
1252         kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
1253
1254         if (!modname || strcmp(modname, mod))
1255                 return 0;
1256
1257         /* blank search means to match all funcs in the mod */
1258         if (len)
1259                 return ftrace_match(str, regex, len, type);
1260         else
1261                 return 1;
1262 }
1263
1264 static void ftrace_match_module_records(char *buff, char *mod, int enable)
1265 {
1266         char *search = buff;
1267         struct ftrace_page *pg;
1268         struct dyn_ftrace *rec;
1269         int type = MATCH_FULL;
1270         unsigned long flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1271         unsigned search_len = 0;
1272         int not = 0;
1273
1274         /* blank or '*' mean the same */
1275         if (strcmp(buff, "*") == 0)
1276                 buff[0] = 0;
1277
1278         /* handle the case of 'dont filter this module' */
1279         if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
1280                 buff[0] = 0;
1281                 not = 1;
1282         }
1283
1284         if (strlen(buff)) {
1285                 type = ftrace_setup_glob(buff, strlen(buff), &search, &not);
1286                 search_len = strlen(search);
1287         }
1288
1289         mutex_lock(&ftrace_lock);
1290         do_for_each_ftrace_rec(pg, rec) {
1291
1292                 if (rec->flags & FTRACE_FL_FAILED)
1293                         continue;
1294
1295                 if (ftrace_match_module_record(rec, mod,
1296                                                search, search_len, type)) {
1297                         if (not)
1298                                 rec->flags &= ~flag;
1299                         else
1300                                 rec->flags |= flag;
1301                 }
1302                 if (enable && (rec->flags & FTRACE_FL_FILTER))
1303                         ftrace_filtered = 1;
1304
1305         } while_for_each_ftrace_rec();
1306         mutex_unlock(&ftrace_lock);
1307 }
1308
1309 /*
1310  * We register the module command as a template to show others how
1311  * to register the a command as well.
1312  */
1313
1314 static int
1315 ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
1316 {
1317         char *mod;
1318
1319         /*
1320          * cmd == 'mod' because we only registered this func
1321          * for the 'mod' ftrace_func_command.
1322          * But if you register one func with multiple commands,
1323          * you can tell which command was used by the cmd
1324          * parameter.
1325          */
1326
1327         /* we must have a module name */
1328         if (!param)
1329                 return -EINVAL;
1330
1331         mod = strsep(&param, ":");
1332         if (!strlen(mod))
1333                 return -EINVAL;
1334
1335         ftrace_match_module_records(func, mod, enable);
1336         return 0;
1337 }
1338
1339 static struct ftrace_func_command ftrace_mod_cmd = {
1340         .name                   = "mod",
1341         .func                   = ftrace_mod_callback,
1342 };
1343
1344 static int __init ftrace_mod_cmd_init(void)
1345 {
1346         return register_ftrace_command(&ftrace_mod_cmd);
1347 }
1348 device_initcall(ftrace_mod_cmd_init);
1349
1350 static void
1351 function_trace_hook_call(unsigned long ip, unsigned long parent_ip)
1352 {
1353         struct ftrace_func_hook *entry;
1354         struct hlist_head *hhd;
1355         struct hlist_node *n;
1356         unsigned long key;
1357         int resched;
1358
1359         key = hash_long(ip, FTRACE_HASH_BITS);
1360
1361         hhd = &ftrace_func_hash[key];
1362
1363         if (hlist_empty(hhd))
1364                 return;
1365
1366         /*
1367          * Disable preemption for these calls to prevent a RCU grace
1368          * period. This syncs the hash iteration and freeing of items
1369          * on the hash. rcu_read_lock is too dangerous here.
1370          */
1371         resched = ftrace_preempt_disable();
1372         hlist_for_each_entry_rcu(entry, n, hhd, node) {
1373                 if (entry->ip == ip)
1374                         entry->ops->func(ip, parent_ip, &entry->data);
1375         }
1376         ftrace_preempt_enable(resched);
1377 }
1378
1379 static struct ftrace_ops trace_hook_ops __read_mostly =
1380 {
1381         .func = function_trace_hook_call,
1382 };
1383
1384 static int ftrace_hook_registered;
1385
1386 static void __enable_ftrace_function_hook(void)
1387 {
1388         int i;
1389
1390         if (ftrace_hook_registered)
1391                 return;
1392
1393         for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1394                 struct hlist_head *hhd = &ftrace_func_hash[i];
1395                 if (hhd->first)
1396                         break;
1397         }
1398         /* Nothing registered? */
1399         if (i == FTRACE_FUNC_HASHSIZE)
1400                 return;
1401
1402         __register_ftrace_function(&trace_hook_ops);
1403         ftrace_startup(0);
1404         ftrace_hook_registered = 1;
1405 }
1406
1407 static void __disable_ftrace_function_hook(void)
1408 {
1409         int i;
1410
1411         if (!ftrace_hook_registered)
1412                 return;
1413
1414         for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1415                 struct hlist_head *hhd = &ftrace_func_hash[i];
1416                 if (hhd->first)
1417                         return;
1418         }
1419
1420         /* no more funcs left */
1421         __unregister_ftrace_function(&trace_hook_ops);
1422         ftrace_shutdown(0);
1423         ftrace_hook_registered = 0;
1424 }
1425
1426
1427 static void ftrace_free_entry_rcu(struct rcu_head *rhp)
1428 {
1429         struct ftrace_func_hook *entry =
1430                 container_of(rhp, struct ftrace_func_hook, rcu);
1431
1432         if (entry->ops->free)
1433                 entry->ops->free(&entry->data);
1434         kfree(entry);
1435 }
1436
1437
1438 int
1439 register_ftrace_function_hook(char *glob, struct ftrace_hook_ops *ops,
1440                               void *data)
1441 {
1442         struct ftrace_func_hook *entry;
1443         struct ftrace_page *pg;
1444         struct dyn_ftrace *rec;
1445         unsigned long key;
1446         int type, len, not;
1447         int count = 0;
1448         char *search;
1449
1450         type = ftrace_setup_glob(glob, strlen(glob), &search, &not);
1451         len = strlen(search);
1452
1453         /* we do not support '!' for function hooks */
1454         if (WARN_ON(not))
1455                 return -EINVAL;
1456
1457         mutex_lock(&ftrace_lock);
1458         do_for_each_ftrace_rec(pg, rec) {
1459
1460                 if (rec->flags & FTRACE_FL_FAILED)
1461                         continue;
1462
1463                 if (!ftrace_match_record(rec, search, len, type))
1464                         continue;
1465
1466                 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1467                 if (!entry) {
1468                         /* If we did not hook to any, then return error */
1469                         if (!count)
1470                                 count = -ENOMEM;
1471                         goto out_unlock;
1472                 }
1473
1474                 count++;
1475
1476                 entry->data = data;
1477
1478                 /*
1479                  * The caller might want to do something special
1480                  * for each function we find. We call the callback
1481                  * to give the caller an opportunity to do so.
1482                  */
1483                 if (ops->callback) {
1484                         if (ops->callback(rec->ip, &entry->data) < 0) {
1485                                 /* caller does not like this func */
1486                                 kfree(entry);
1487                                 continue;
1488                         }
1489                 }
1490
1491                 entry->ops = ops;
1492                 entry->ip = rec->ip;
1493
1494                 key = hash_long(entry->ip, FTRACE_HASH_BITS);
1495                 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
1496
1497         } while_for_each_ftrace_rec();
1498         __enable_ftrace_function_hook();
1499
1500  out_unlock:
1501         mutex_unlock(&ftrace_lock);
1502
1503         return count;
1504 }
1505
1506 enum {
1507         HOOK_TEST_FUNC          = 1,
1508         HOOK_TEST_DATA          = 2
1509 };
1510
1511 static void
1512 __unregister_ftrace_function_hook(char *glob, struct ftrace_hook_ops *ops,
1513                                   void *data, int flags)
1514 {
1515         struct ftrace_func_hook *entry;
1516         struct hlist_node *n, *tmp;
1517         char str[KSYM_SYMBOL_LEN];
1518         int type = MATCH_FULL;
1519         int i, len = 0;
1520         char *search;
1521
1522         if (glob && (strcmp(glob, "*") || !strlen(glob)))
1523                 glob = NULL;
1524         else {
1525                 int not;
1526
1527                 type = ftrace_setup_glob(glob, strlen(glob), &search, &not);
1528                 len = strlen(search);
1529
1530                 /* we do not support '!' for function hooks */
1531                 if (WARN_ON(not))
1532                         return;
1533         }
1534
1535         mutex_lock(&ftrace_lock);
1536         for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1537                 struct hlist_head *hhd = &ftrace_func_hash[i];
1538
1539                 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
1540
1541                         /* break up if statements for readability */
1542                         if ((flags & HOOK_TEST_FUNC) && entry->ops != ops)
1543                                 continue;
1544
1545                         if ((flags & HOOK_TEST_DATA) && entry->data != data)
1546                                 continue;
1547
1548                         /* do this last, since it is the most expensive */
1549                         if (glob) {
1550                                 kallsyms_lookup(entry->ip, NULL, NULL,
1551                                                 NULL, str);
1552                                 if (!ftrace_match(str, glob, len, type))
1553                                         continue;
1554                         }
1555
1556                         hlist_del(&entry->node);
1557                         call_rcu(&entry->rcu, ftrace_free_entry_rcu);
1558                 }
1559         }
1560         __disable_ftrace_function_hook();
1561         mutex_unlock(&ftrace_lock);
1562 }
1563
1564 void
1565 unregister_ftrace_function_hook(char *glob, struct ftrace_hook_ops *ops,
1566                                 void *data)
1567 {
1568         __unregister_ftrace_function_hook(glob, ops, data,
1569                                           HOOK_TEST_FUNC | HOOK_TEST_DATA);
1570 }
1571
1572 void
1573 unregister_ftrace_function_hook_func(char *glob, struct ftrace_hook_ops *ops)
1574 {
1575         __unregister_ftrace_function_hook(glob, ops, NULL, HOOK_TEST_FUNC);
1576 }
1577
1578 void unregister_ftrace_function_hook_all(char *glob)
1579 {
1580         __unregister_ftrace_function_hook(glob, NULL, NULL, 0);
1581 }
1582
1583 static LIST_HEAD(ftrace_commands);
1584 static DEFINE_MUTEX(ftrace_cmd_mutex);
1585
1586 int register_ftrace_command(struct ftrace_func_command *cmd)
1587 {
1588         struct ftrace_func_command *p;
1589         int ret = 0;
1590
1591         mutex_lock(&ftrace_cmd_mutex);
1592         list_for_each_entry(p, &ftrace_commands, list) {
1593                 if (strcmp(cmd->name, p->name) == 0) {
1594                         ret = -EBUSY;
1595                         goto out_unlock;
1596                 }
1597         }
1598         list_add(&cmd->list, &ftrace_commands);
1599  out_unlock:
1600         mutex_unlock(&ftrace_cmd_mutex);
1601
1602         return ret;
1603 }
1604
1605 int unregister_ftrace_command(struct ftrace_func_command *cmd)
1606 {
1607         struct ftrace_func_command *p, *n;
1608         int ret = -ENODEV;
1609
1610         mutex_lock(&ftrace_cmd_mutex);
1611         list_for_each_entry_safe(p, n, &ftrace_commands, list) {
1612                 if (strcmp(cmd->name, p->name) == 0) {
1613                         ret = 0;
1614                         list_del_init(&p->list);
1615                         goto out_unlock;
1616                 }
1617         }
1618  out_unlock:
1619         mutex_unlock(&ftrace_cmd_mutex);
1620
1621         return ret;
1622 }
1623
1624 static int ftrace_process_regex(char *buff, int len, int enable)
1625 {
1626         struct ftrace_func_command *p;
1627         char *func, *command, *next = buff;
1628         int ret = -EINVAL;
1629
1630         func = strsep(&next, ":");
1631
1632         if (!next) {
1633                 ftrace_match_records(func, len, enable);
1634                 return 0;
1635         }
1636
1637         /* command found */
1638
1639         command = strsep(&next, ":");
1640
1641         mutex_lock(&ftrace_cmd_mutex);
1642         list_for_each_entry(p, &ftrace_commands, list) {
1643                 if (strcmp(p->name, command) == 0) {
1644                         ret = p->func(func, command, next, enable);
1645                         goto out_unlock;
1646                 }
1647         }
1648  out_unlock:
1649         mutex_unlock(&ftrace_cmd_mutex);
1650
1651         return ret;
1652 }
1653
1654 static ssize_t
1655 ftrace_regex_write(struct file *file, const char __user *ubuf,
1656                    size_t cnt, loff_t *ppos, int enable)
1657 {
1658         struct ftrace_iterator *iter;
1659         char ch;
1660         size_t read = 0;
1661         ssize_t ret;
1662
1663         if (!cnt || cnt < 0)
1664                 return 0;
1665
1666         mutex_lock(&ftrace_regex_lock);
1667
1668         if (file->f_mode & FMODE_READ) {
1669                 struct seq_file *m = file->private_data;
1670                 iter = m->private;
1671         } else
1672                 iter = file->private_data;
1673
1674         if (!*ppos) {
1675                 iter->flags &= ~FTRACE_ITER_CONT;
1676                 iter->buffer_idx = 0;
1677         }
1678
1679         ret = get_user(ch, ubuf++);
1680         if (ret)
1681                 goto out;
1682         read++;
1683         cnt--;
1684
1685         if (!(iter->flags & ~FTRACE_ITER_CONT)) {
1686                 /* skip white space */
1687                 while (cnt && isspace(ch)) {
1688                         ret = get_user(ch, ubuf++);
1689                         if (ret)
1690                                 goto out;
1691                         read++;
1692                         cnt--;
1693                 }
1694
1695                 if (isspace(ch)) {
1696                         file->f_pos += read;
1697                         ret = read;
1698                         goto out;
1699                 }
1700
1701                 iter->buffer_idx = 0;
1702         }
1703
1704         while (cnt && !isspace(ch)) {
1705                 if (iter->buffer_idx < FTRACE_BUFF_MAX)
1706                         iter->buffer[iter->buffer_idx++] = ch;
1707                 else {
1708                         ret = -EINVAL;
1709                         goto out;
1710                 }
1711                 ret = get_user(ch, ubuf++);
1712                 if (ret)
1713                         goto out;
1714                 read++;
1715                 cnt--;
1716         }
1717
1718         if (isspace(ch)) {
1719                 iter->filtered++;
1720                 iter->buffer[iter->buffer_idx] = 0;
1721                 ret = ftrace_process_regex(iter->buffer,
1722                                            iter->buffer_idx, enable);
1723                 if (ret)
1724                         goto out;
1725                 iter->buffer_idx = 0;
1726         } else
1727                 iter->flags |= FTRACE_ITER_CONT;
1728
1729
1730         file->f_pos += read;
1731
1732         ret = read;
1733  out:
1734         mutex_unlock(&ftrace_regex_lock);
1735
1736         return ret;
1737 }
1738
1739 static ssize_t
1740 ftrace_filter_write(struct file *file, const char __user *ubuf,
1741                     size_t cnt, loff_t *ppos)
1742 {
1743         return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
1744 }
1745
1746 static ssize_t
1747 ftrace_notrace_write(struct file *file, const char __user *ubuf,
1748                      size_t cnt, loff_t *ppos)
1749 {
1750         return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
1751 }
1752
1753 static void
1754 ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
1755 {
1756         if (unlikely(ftrace_disabled))
1757                 return;
1758
1759         mutex_lock(&ftrace_regex_lock);
1760         if (reset)
1761                 ftrace_filter_reset(enable);
1762         if (buf)
1763                 ftrace_match_records(buf, len, enable);
1764         mutex_unlock(&ftrace_regex_lock);
1765 }
1766
1767 /**
1768  * ftrace_set_filter - set a function to filter on in ftrace
1769  * @buf - the string that holds the function filter text.
1770  * @len - the length of the string.
1771  * @reset - non zero to reset all filters before applying this filter.
1772  *
1773  * Filters denote which functions should be enabled when tracing is enabled.
1774  * If @buf is NULL and reset is set, all functions will be enabled for tracing.
1775  */
1776 void ftrace_set_filter(unsigned char *buf, int len, int reset)
1777 {
1778         ftrace_set_regex(buf, len, reset, 1);
1779 }
1780
1781 /**
1782  * ftrace_set_notrace - set a function to not trace in ftrace
1783  * @buf - the string that holds the function notrace text.
1784  * @len - the length of the string.
1785  * @reset - non zero to reset all filters before applying this filter.
1786  *
1787  * Notrace Filters denote which functions should not be enabled when tracing
1788  * is enabled. If @buf is NULL and reset is set, all functions will be enabled
1789  * for tracing.
1790  */
1791 void ftrace_set_notrace(unsigned char *buf, int len, int reset)
1792 {
1793         ftrace_set_regex(buf, len, reset, 0);
1794 }
1795
1796 static int
1797 ftrace_regex_release(struct inode *inode, struct file *file, int enable)
1798 {
1799         struct seq_file *m = (struct seq_file *)file->private_data;
1800         struct ftrace_iterator *iter;
1801
1802         mutex_lock(&ftrace_regex_lock);
1803         if (file->f_mode & FMODE_READ) {
1804                 iter = m->private;
1805
1806                 seq_release(inode, file);
1807         } else
1808                 iter = file->private_data;
1809
1810         if (iter->buffer_idx) {
1811                 iter->filtered++;
1812                 iter->buffer[iter->buffer_idx] = 0;
1813                 ftrace_match_records(iter->buffer, iter->buffer_idx, enable);
1814         }
1815
1816         mutex_lock(&ftrace_lock);
1817         if (ftrace_start_up && ftrace_enabled)
1818                 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
1819         mutex_unlock(&ftrace_lock);
1820
1821         kfree(iter);
1822         mutex_unlock(&ftrace_regex_lock);
1823         return 0;
1824 }
1825
1826 static int
1827 ftrace_filter_release(struct inode *inode, struct file *file)
1828 {
1829         return ftrace_regex_release(inode, file, 1);
1830 }
1831
1832 static int
1833 ftrace_notrace_release(struct inode *inode, struct file *file)
1834 {
1835         return ftrace_regex_release(inode, file, 0);
1836 }
1837
1838 static struct file_operations ftrace_avail_fops = {
1839         .open = ftrace_avail_open,
1840         .read = seq_read,
1841         .llseek = seq_lseek,
1842         .release = ftrace_avail_release,
1843 };
1844
1845 static struct file_operations ftrace_failures_fops = {
1846         .open = ftrace_failures_open,
1847         .read = seq_read,
1848         .llseek = seq_lseek,
1849         .release = ftrace_avail_release,
1850 };
1851
1852 static struct file_operations ftrace_filter_fops = {
1853         .open = ftrace_filter_open,
1854         .read = ftrace_regex_read,
1855         .write = ftrace_filter_write,
1856         .llseek = ftrace_regex_lseek,
1857         .release = ftrace_filter_release,
1858 };
1859
1860 static struct file_operations ftrace_notrace_fops = {
1861         .open = ftrace_notrace_open,
1862         .read = ftrace_regex_read,
1863         .write = ftrace_notrace_write,
1864         .llseek = ftrace_regex_lseek,
1865         .release = ftrace_notrace_release,
1866 };
1867
1868 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1869
1870 static DEFINE_MUTEX(graph_lock);
1871
1872 int ftrace_graph_count;
1873 unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
1874
1875 static void *
1876 g_next(struct seq_file *m, void *v, loff_t *pos)
1877 {
1878         unsigned long *array = m->private;
1879         int index = *pos;
1880
1881         (*pos)++;
1882
1883         if (index >= ftrace_graph_count)
1884                 return NULL;
1885
1886         return &array[index];
1887 }
1888
1889 static void *g_start(struct seq_file *m, loff_t *pos)
1890 {
1891         void *p = NULL;
1892
1893         mutex_lock(&graph_lock);
1894
1895         p = g_next(m, p, pos);
1896
1897         return p;
1898 }
1899
1900 static void g_stop(struct seq_file *m, void *p)
1901 {
1902         mutex_unlock(&graph_lock);
1903 }
1904
1905 static int g_show(struct seq_file *m, void *v)
1906 {
1907         unsigned long *ptr = v;
1908         char str[KSYM_SYMBOL_LEN];
1909
1910         if (!ptr)
1911                 return 0;
1912
1913         kallsyms_lookup(*ptr, NULL, NULL, NULL, str);
1914
1915         seq_printf(m, "%s\n", str);
1916
1917         return 0;
1918 }
1919
1920 static struct seq_operations ftrace_graph_seq_ops = {
1921         .start = g_start,
1922         .next = g_next,
1923         .stop = g_stop,
1924         .show = g_show,
1925 };
1926
1927 static int
1928 ftrace_graph_open(struct inode *inode, struct file *file)
1929 {
1930         int ret = 0;
1931
1932         if (unlikely(ftrace_disabled))
1933                 return -ENODEV;
1934
1935         mutex_lock(&graph_lock);
1936         if ((file->f_mode & FMODE_WRITE) &&
1937             !(file->f_flags & O_APPEND)) {
1938                 ftrace_graph_count = 0;
1939                 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
1940         }
1941
1942         if (file->f_mode & FMODE_READ) {
1943                 ret = seq_open(file, &ftrace_graph_seq_ops);
1944                 if (!ret) {
1945                         struct seq_file *m = file->private_data;
1946                         m->private = ftrace_graph_funcs;
1947                 }
1948         } else
1949                 file->private_data = ftrace_graph_funcs;
1950         mutex_unlock(&graph_lock);
1951
1952         return ret;
1953 }
1954
1955 static ssize_t
1956 ftrace_graph_read(struct file *file, char __user *ubuf,
1957                        size_t cnt, loff_t *ppos)
1958 {
1959         if (file->f_mode & FMODE_READ)
1960                 return seq_read(file, ubuf, cnt, ppos);
1961         else
1962                 return -EPERM;
1963 }
1964
1965 static int
1966 ftrace_set_func(unsigned long *array, int idx, char *buffer)
1967 {
1968         char str[KSYM_SYMBOL_LEN];
1969         struct dyn_ftrace *rec;
1970         struct ftrace_page *pg;
1971         int found = 0;
1972         int j;
1973
1974         if (ftrace_disabled)
1975                 return -ENODEV;
1976
1977         mutex_lock(&ftrace_lock);
1978         do_for_each_ftrace_rec(pg, rec) {
1979
1980                 if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
1981                         continue;
1982
1983                 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1984                 if (strcmp(str, buffer) == 0) {
1985                         /* Return 1 if we add it to the array */
1986                         found = 1;
1987                         for (j = 0; j < idx; j++)
1988                                 if (array[j] == rec->ip) {
1989                                         found = 0;
1990                                         break;
1991                                 }
1992                         if (found)
1993                                 array[idx] = rec->ip;
1994                         goto out;
1995                 }
1996         } while_for_each_ftrace_rec();
1997  out:
1998         mutex_unlock(&ftrace_lock);
1999
2000         return found ? 0 : -EINVAL;
2001 }
2002
2003 static ssize_t
2004 ftrace_graph_write(struct file *file, const char __user *ubuf,
2005                    size_t cnt, loff_t *ppos)
2006 {
2007         unsigned char buffer[FTRACE_BUFF_MAX+1];
2008         unsigned long *array;
2009         size_t read = 0;
2010         ssize_t ret;
2011         int index = 0;
2012         char ch;
2013
2014         if (!cnt || cnt < 0)
2015                 return 0;
2016
2017         mutex_lock(&graph_lock);
2018
2019         if (ftrace_graph_count >= FTRACE_GRAPH_MAX_FUNCS) {
2020                 ret = -EBUSY;
2021                 goto out;
2022         }
2023
2024         if (file->f_mode & FMODE_READ) {
2025                 struct seq_file *m = file->private_data;
2026                 array = m->private;
2027         } else
2028                 array = file->private_data;
2029
2030         ret = get_user(ch, ubuf++);
2031         if (ret)
2032                 goto out;
2033         read++;
2034         cnt--;
2035
2036         /* skip white space */
2037         while (cnt && isspace(ch)) {
2038                 ret = get_user(ch, ubuf++);
2039                 if (ret)
2040                         goto out;
2041                 read++;
2042                 cnt--;
2043         }
2044
2045         if (isspace(ch)) {
2046                 *ppos += read;
2047                 ret = read;
2048                 goto out;
2049         }
2050
2051         while (cnt && !isspace(ch)) {
2052                 if (index < FTRACE_BUFF_MAX)
2053                         buffer[index++] = ch;
2054                 else {
2055                         ret = -EINVAL;
2056                         goto out;
2057                 }
2058                 ret = get_user(ch, ubuf++);
2059                 if (ret)
2060                         goto out;
2061                 read++;
2062                 cnt--;
2063         }
2064         buffer[index] = 0;
2065
2066         /* we allow only one at a time */
2067         ret = ftrace_set_func(array, ftrace_graph_count, buffer);
2068         if (ret)
2069                 goto out;
2070
2071         ftrace_graph_count++;
2072
2073         file->f_pos += read;
2074
2075         ret = read;
2076  out:
2077         mutex_unlock(&graph_lock);
2078
2079         return ret;
2080 }
2081
2082 static const struct file_operations ftrace_graph_fops = {
2083         .open = ftrace_graph_open,
2084         .read = ftrace_graph_read,
2085         .write = ftrace_graph_write,
2086 };
2087 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2088
2089 static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
2090 {
2091         struct dentry *entry;
2092
2093         entry = debugfs_create_file("available_filter_functions", 0444,
2094                                     d_tracer, NULL, &ftrace_avail_fops);
2095         if (!entry)
2096                 pr_warning("Could not create debugfs "
2097                            "'available_filter_functions' entry\n");
2098
2099         entry = debugfs_create_file("failures", 0444,
2100                                     d_tracer, NULL, &ftrace_failures_fops);
2101         if (!entry)
2102                 pr_warning("Could not create debugfs 'failures' entry\n");
2103
2104         entry = debugfs_create_file("set_ftrace_filter", 0644, d_tracer,
2105                                     NULL, &ftrace_filter_fops);
2106         if (!entry)
2107                 pr_warning("Could not create debugfs "
2108                            "'set_ftrace_filter' entry\n");
2109
2110         entry = debugfs_create_file("set_ftrace_notrace", 0644, d_tracer,
2111                                     NULL, &ftrace_notrace_fops);
2112         if (!entry)
2113                 pr_warning("Could not create debugfs "
2114                            "'set_ftrace_notrace' entry\n");
2115
2116 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2117         entry = debugfs_create_file("set_graph_function", 0444, d_tracer,
2118                                     NULL,
2119                                     &ftrace_graph_fops);
2120         if (!entry)
2121                 pr_warning("Could not create debugfs "
2122                            "'set_graph_function' entry\n");
2123 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2124
2125         return 0;
2126 }
2127
2128 static int ftrace_convert_nops(struct module *mod,
2129                                unsigned long *start,
2130                                unsigned long *end)
2131 {
2132         unsigned long *p;
2133         unsigned long addr;
2134         unsigned long flags;
2135
2136         mutex_lock(&ftrace_lock);
2137         p = start;
2138         while (p < end) {
2139                 addr = ftrace_call_adjust(*p++);
2140                 /*
2141                  * Some architecture linkers will pad between
2142                  * the different mcount_loc sections of different
2143                  * object files to satisfy alignments.
2144                  * Skip any NULL pointers.
2145                  */
2146                 if (!addr)
2147                         continue;
2148                 ftrace_record_ip(addr);
2149         }
2150
2151         /* disable interrupts to prevent kstop machine */
2152         local_irq_save(flags);
2153         ftrace_update_code(mod);
2154         local_irq_restore(flags);
2155         mutex_unlock(&ftrace_lock);
2156
2157         return 0;
2158 }
2159
2160 void ftrace_init_module(struct module *mod,
2161                         unsigned long *start, unsigned long *end)
2162 {
2163         if (ftrace_disabled || start == end)
2164                 return;
2165         ftrace_convert_nops(mod, start, end);
2166 }
2167
2168 extern unsigned long __start_mcount_loc[];
2169 extern unsigned long __stop_mcount_loc[];
2170
2171 void __init ftrace_init(void)
2172 {
2173         unsigned long count, addr, flags;
2174         int ret;
2175
2176         /* Keep the ftrace pointer to the stub */
2177         addr = (unsigned long)ftrace_stub;
2178
2179         local_irq_save(flags);
2180         ftrace_dyn_arch_init(&addr);
2181         local_irq_restore(flags);
2182
2183         /* ftrace_dyn_arch_init places the return code in addr */
2184         if (addr)
2185                 goto failed;
2186
2187         count = __stop_mcount_loc - __start_mcount_loc;
2188
2189         ret = ftrace_dyn_table_alloc(count);
2190         if (ret)
2191                 goto failed;
2192
2193         last_ftrace_enabled = ftrace_enabled = 1;
2194
2195         ret = ftrace_convert_nops(NULL,
2196                                   __start_mcount_loc,
2197                                   __stop_mcount_loc);
2198
2199         return;
2200  failed:
2201         ftrace_disabled = 1;
2202 }
2203
2204 #else
2205
2206 static int __init ftrace_nodyn_init(void)
2207 {
2208         ftrace_enabled = 1;
2209         return 0;
2210 }
2211 device_initcall(ftrace_nodyn_init);
2212
2213 static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
2214 static inline void ftrace_startup_enable(int command) { }
2215 /* Keep as macros so we do not need to define the commands */
2216 # define ftrace_startup(command)        do { } while (0)
2217 # define ftrace_shutdown(command)       do { } while (0)
2218 # define ftrace_startup_sysctl()        do { } while (0)
2219 # define ftrace_shutdown_sysctl()       do { } while (0)
2220 #endif /* CONFIG_DYNAMIC_FTRACE */
2221
2222 static ssize_t
2223 ftrace_pid_read(struct file *file, char __user *ubuf,
2224                        size_t cnt, loff_t *ppos)
2225 {
2226         char buf[64];
2227         int r;
2228
2229         if (ftrace_pid_trace == ftrace_swapper_pid)
2230                 r = sprintf(buf, "swapper tasks\n");
2231         else if (ftrace_pid_trace)
2232                 r = sprintf(buf, "%u\n", pid_nr(ftrace_pid_trace));
2233         else
2234                 r = sprintf(buf, "no pid\n");
2235
2236         return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2237 }
2238
2239 static void clear_ftrace_swapper(void)
2240 {
2241         struct task_struct *p;
2242         int cpu;
2243
2244         get_online_cpus();
2245         for_each_online_cpu(cpu) {
2246                 p = idle_task(cpu);
2247                 clear_tsk_trace_trace(p);
2248         }
2249         put_online_cpus();
2250 }
2251
2252 static void set_ftrace_swapper(void)
2253 {
2254         struct task_struct *p;
2255         int cpu;
2256
2257         get_online_cpus();
2258         for_each_online_cpu(cpu) {
2259                 p = idle_task(cpu);
2260                 set_tsk_trace_trace(p);
2261         }
2262         put_online_cpus();
2263 }
2264
2265 static void clear_ftrace_pid(struct pid *pid)
2266 {
2267         struct task_struct *p;
2268
2269         rcu_read_lock();
2270         do_each_pid_task(pid, PIDTYPE_PID, p) {
2271                 clear_tsk_trace_trace(p);
2272         } while_each_pid_task(pid, PIDTYPE_PID, p);
2273         rcu_read_unlock();
2274
2275         put_pid(pid);
2276 }
2277
2278 static void set_ftrace_pid(struct pid *pid)
2279 {
2280         struct task_struct *p;
2281
2282         rcu_read_lock();
2283         do_each_pid_task(pid, PIDTYPE_PID, p) {
2284                 set_tsk_trace_trace(p);
2285         } while_each_pid_task(pid, PIDTYPE_PID, p);
2286         rcu_read_unlock();
2287 }
2288
2289 static void clear_ftrace_pid_task(struct pid **pid)
2290 {
2291         if (*pid == ftrace_swapper_pid)
2292                 clear_ftrace_swapper();
2293         else
2294                 clear_ftrace_pid(*pid);
2295
2296         *pid = NULL;
2297 }
2298
2299 static void set_ftrace_pid_task(struct pid *pid)
2300 {
2301         if (pid == ftrace_swapper_pid)
2302                 set_ftrace_swapper();
2303         else
2304                 set_ftrace_pid(pid);
2305 }
2306
2307 static ssize_t
2308 ftrace_pid_write(struct file *filp, const char __user *ubuf,
2309                    size_t cnt, loff_t *ppos)
2310 {
2311         struct pid *pid;
2312         char buf[64];
2313         long val;
2314         int ret;
2315
2316         if (cnt >= sizeof(buf))
2317                 return -EINVAL;
2318
2319         if (copy_from_user(&buf, ubuf, cnt))
2320                 return -EFAULT;
2321
2322         buf[cnt] = 0;
2323
2324         ret = strict_strtol(buf, 10, &val);
2325         if (ret < 0)
2326                 return ret;
2327
2328         mutex_lock(&ftrace_lock);
2329         if (val < 0) {
2330                 /* disable pid tracing */
2331                 if (!ftrace_pid_trace)
2332                         goto out;
2333
2334                 clear_ftrace_pid_task(&ftrace_pid_trace);
2335
2336         } else {
2337                 /* swapper task is special */
2338                 if (!val) {
2339                         pid = ftrace_swapper_pid;
2340                         if (pid == ftrace_pid_trace)
2341                                 goto out;
2342                 } else {
2343                         pid = find_get_pid(val);
2344
2345                         if (pid == ftrace_pid_trace) {
2346                                 put_pid(pid);
2347                                 goto out;
2348                         }
2349                 }
2350
2351                 if (ftrace_pid_trace)
2352                         clear_ftrace_pid_task(&ftrace_pid_trace);
2353
2354                 if (!pid)
2355                         goto out;
2356
2357                 ftrace_pid_trace = pid;
2358
2359                 set_ftrace_pid_task(ftrace_pid_trace);
2360         }
2361
2362         /* update the function call */
2363         ftrace_update_pid_func();
2364         ftrace_startup_enable(0);
2365
2366  out:
2367         mutex_unlock(&ftrace_lock);
2368
2369         return cnt;
2370 }
2371
2372 static struct file_operations ftrace_pid_fops = {
2373         .read = ftrace_pid_read,
2374         .write = ftrace_pid_write,
2375 };
2376
2377 static __init int ftrace_init_debugfs(void)
2378 {
2379         struct dentry *d_tracer;
2380         struct dentry *entry;
2381
2382         d_tracer = tracing_init_dentry();
2383         if (!d_tracer)
2384                 return 0;
2385
2386         ftrace_init_dyn_debugfs(d_tracer);
2387
2388         entry = debugfs_create_file("set_ftrace_pid", 0644, d_tracer,
2389                                     NULL, &ftrace_pid_fops);
2390         if (!entry)
2391                 pr_warning("Could not create debugfs "
2392                            "'set_ftrace_pid' entry\n");
2393         return 0;
2394 }
2395
2396 fs_initcall(ftrace_init_debugfs);
2397
2398 /**
2399  * ftrace_kill - kill ftrace
2400  *
2401  * This function should be used by panic code. It stops ftrace
2402  * but in a not so nice way. If you need to simply kill ftrace
2403  * from a non-atomic section, use ftrace_kill.
2404  */
2405 void ftrace_kill(void)
2406 {
2407         ftrace_disabled = 1;
2408         ftrace_enabled = 0;
2409         clear_ftrace_function();
2410 }
2411
2412 /**
2413  * register_ftrace_function - register a function for profiling
2414  * @ops - ops structure that holds the function for profiling.
2415  *
2416  * Register a function to be called by all functions in the
2417  * kernel.
2418  *
2419  * Note: @ops->func and all the functions it calls must be labeled
2420  *       with "notrace", otherwise it will go into a
2421  *       recursive loop.
2422  */
2423 int register_ftrace_function(struct ftrace_ops *ops)
2424 {
2425         int ret;
2426
2427         if (unlikely(ftrace_disabled))
2428                 return -1;
2429
2430         mutex_lock(&ftrace_lock);
2431
2432         ret = __register_ftrace_function(ops);
2433         ftrace_startup(0);
2434
2435         mutex_unlock(&ftrace_lock);
2436         return ret;
2437 }
2438
2439 /**
2440  * unregister_ftrace_function - unregister a function for profiling.
2441  * @ops - ops structure that holds the function to unregister
2442  *
2443  * Unregister a function that was added to be called by ftrace profiling.
2444  */
2445 int unregister_ftrace_function(struct ftrace_ops *ops)
2446 {
2447         int ret;
2448
2449         mutex_lock(&ftrace_lock);
2450         ret = __unregister_ftrace_function(ops);
2451         ftrace_shutdown(0);
2452         mutex_unlock(&ftrace_lock);
2453
2454         return ret;
2455 }
2456
2457 int
2458 ftrace_enable_sysctl(struct ctl_table *table, int write,
2459                      struct file *file, void __user *buffer, size_t *lenp,
2460                      loff_t *ppos)
2461 {
2462         int ret;
2463
2464         if (unlikely(ftrace_disabled))
2465                 return -ENODEV;
2466
2467         mutex_lock(&ftrace_lock);
2468
2469         ret  = proc_dointvec(table, write, file, buffer, lenp, ppos);
2470
2471         if (ret || !write || (last_ftrace_enabled == ftrace_enabled))
2472                 goto out;
2473
2474         last_ftrace_enabled = ftrace_enabled;
2475
2476         if (ftrace_enabled) {
2477
2478                 ftrace_startup_sysctl();
2479
2480                 /* we are starting ftrace again */
2481                 if (ftrace_list != &ftrace_list_end) {
2482                         if (ftrace_list->next == &ftrace_list_end)
2483                                 ftrace_trace_function = ftrace_list->func;
2484                         else
2485                                 ftrace_trace_function = ftrace_list_func;
2486                 }
2487
2488         } else {
2489                 /* stopping ftrace calls (just send to ftrace_stub) */
2490                 ftrace_trace_function = ftrace_stub;
2491
2492                 ftrace_shutdown_sysctl();
2493         }
2494
2495  out:
2496         mutex_unlock(&ftrace_lock);
2497         return ret;
2498 }
2499
2500 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
2501
2502 static atomic_t ftrace_graph_active;
2503 static struct notifier_block ftrace_suspend_notifier;
2504
2505 int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
2506 {
2507         return 0;
2508 }
2509
2510 /* The callbacks that hook a function */
2511 trace_func_graph_ret_t ftrace_graph_return =
2512                         (trace_func_graph_ret_t)ftrace_stub;
2513 trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
2514
2515 /* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
2516 static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
2517 {
2518         int i;
2519         int ret = 0;
2520         unsigned long flags;
2521         int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
2522         struct task_struct *g, *t;
2523
2524         for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
2525                 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
2526                                         * sizeof(struct ftrace_ret_stack),
2527                                         GFP_KERNEL);
2528                 if (!ret_stack_list[i]) {
2529                         start = 0;
2530                         end = i;
2531                         ret = -ENOMEM;
2532                         goto free;
2533                 }
2534         }
2535
2536         read_lock_irqsave(&tasklist_lock, flags);
2537         do_each_thread(g, t) {
2538                 if (start == end) {
2539                         ret = -EAGAIN;
2540                         goto unlock;
2541                 }
2542
2543                 if (t->ret_stack == NULL) {
2544                         t->curr_ret_stack = -1;
2545                         /* Make sure IRQs see the -1 first: */
2546                         barrier();
2547                         t->ret_stack = ret_stack_list[start++];
2548                         atomic_set(&t->tracing_graph_pause, 0);
2549                         atomic_set(&t->trace_overrun, 0);
2550                 }
2551         } while_each_thread(g, t);
2552
2553 unlock:
2554         read_unlock_irqrestore(&tasklist_lock, flags);
2555 free:
2556         for (i = start; i < end; i++)
2557                 kfree(ret_stack_list[i]);
2558         return ret;
2559 }
2560
2561 /* Allocate a return stack for each task */
2562 static int start_graph_tracing(void)
2563 {
2564         struct ftrace_ret_stack **ret_stack_list;
2565         int ret;
2566
2567         ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
2568                                 sizeof(struct ftrace_ret_stack *),
2569                                 GFP_KERNEL);
2570
2571         if (!ret_stack_list)
2572                 return -ENOMEM;
2573
2574         do {
2575                 ret = alloc_retstack_tasklist(ret_stack_list);
2576         } while (ret == -EAGAIN);
2577
2578         kfree(ret_stack_list);
2579         return ret;
2580 }
2581
2582 /*
2583  * Hibernation protection.
2584  * The state of the current task is too much unstable during
2585  * suspend/restore to disk. We want to protect against that.
2586  */
2587 static int
2588 ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
2589                                                         void *unused)
2590 {
2591         switch (state) {
2592         case PM_HIBERNATION_PREPARE:
2593                 pause_graph_tracing();
2594                 break;
2595
2596         case PM_POST_HIBERNATION:
2597                 unpause_graph_tracing();
2598                 break;
2599         }
2600         return NOTIFY_DONE;
2601 }
2602
2603 int register_ftrace_graph(trace_func_graph_ret_t retfunc,
2604                         trace_func_graph_ent_t entryfunc)
2605 {
2606         int ret = 0;
2607
2608         mutex_lock(&ftrace_lock);
2609
2610         ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
2611         register_pm_notifier(&ftrace_suspend_notifier);
2612
2613         atomic_inc(&ftrace_graph_active);
2614         ret = start_graph_tracing();
2615         if (ret) {
2616                 atomic_dec(&ftrace_graph_active);
2617                 goto out;
2618         }
2619
2620         ftrace_graph_return = retfunc;
2621         ftrace_graph_entry = entryfunc;
2622
2623         ftrace_startup(FTRACE_START_FUNC_RET);
2624
2625 out:
2626         mutex_unlock(&ftrace_lock);
2627         return ret;
2628 }
2629
2630 void unregister_ftrace_graph(void)
2631 {
2632         mutex_lock(&ftrace_lock);
2633
2634         atomic_dec(&ftrace_graph_active);
2635         ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
2636         ftrace_graph_entry = ftrace_graph_entry_stub;
2637         ftrace_shutdown(FTRACE_STOP_FUNC_RET);
2638         unregister_pm_notifier(&ftrace_suspend_notifier);
2639
2640         mutex_unlock(&ftrace_lock);
2641 }
2642
2643 /* Allocate a return stack for newly created task */
2644 void ftrace_graph_init_task(struct task_struct *t)
2645 {
2646         if (atomic_read(&ftrace_graph_active)) {
2647                 t->ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
2648                                 * sizeof(struct ftrace_ret_stack),
2649                                 GFP_KERNEL);
2650                 if (!t->ret_stack)
2651                         return;
2652                 t->curr_ret_stack = -1;
2653                 atomic_set(&t->tracing_graph_pause, 0);
2654                 atomic_set(&t->trace_overrun, 0);
2655         } else
2656                 t->ret_stack = NULL;
2657 }
2658
2659 void ftrace_graph_exit_task(struct task_struct *t)
2660 {
2661         struct ftrace_ret_stack *ret_stack = t->ret_stack;
2662
2663         t->ret_stack = NULL;
2664         /* NULL must become visible to IRQs before we free it: */
2665         barrier();
2666
2667         kfree(ret_stack);
2668 }
2669
2670 void ftrace_graph_stop(void)
2671 {
2672         ftrace_stop();
2673 }
2674 #endif
2675