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