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