]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - kernel/trace/ftrace.c
tracing/events: Move TRACE_SYSTEM outside of include guard
[mirror_ubuntu-bionic-kernel.git] / kernel / trace / ftrace.c
CommitLineData
16444a8a
ACM
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
3d083395
SR
16#include <linux/stop_machine.h>
17#include <linux/clocksource.h>
18#include <linux/kallsyms.h>
5072c59f 19#include <linux/seq_file.h>
4a2b8dda 20#include <linux/suspend.h>
5072c59f 21#include <linux/debugfs.h>
3d083395 22#include <linux/hardirq.h>
2d8b820b 23#include <linux/kthread.h>
5072c59f 24#include <linux/uaccess.h>
f22f9a89 25#include <linux/kprobes.h>
2d8b820b 26#include <linux/ftrace.h>
b0fc494f 27#include <linux/sysctl.h>
5072c59f 28#include <linux/ctype.h>
3d083395 29#include <linux/list.h>
59df055f 30#include <linux/hash.h>
3d083395 31
ad8d75ff 32#include <trace/events/sched.h>
8aef2d28 33
395a59d0 34#include <asm/ftrace.h>
2af15d6a 35#include <asm/setup.h>
395a59d0 36
0706f1c4 37#include "trace_output.h"
bac429f0 38#include "trace_stat.h"
16444a8a 39
6912896e
SR
40#define FTRACE_WARN_ON(cond) \
41 do { \
42 if (WARN_ON(cond)) \
43 ftrace_kill(); \
44 } while (0)
45
46#define FTRACE_WARN_ON_ONCE(cond) \
47 do { \
48 if (WARN_ON_ONCE(cond)) \
49 ftrace_kill(); \
50 } while (0)
51
8fc0c701
SR
52/* hash bits for specific function selection */
53#define FTRACE_HASH_BITS 7
54#define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
55
4eebcc81
SR
56/* ftrace_enabled is a method to turn ftrace on or off */
57int ftrace_enabled __read_mostly;
d61f82d0 58static int last_ftrace_enabled;
b0fc494f 59
60a7ecf4
SR
60/* Quick disabling of function tracer. */
61int function_trace_stop;
62
4eebcc81
SR
63/*
64 * ftrace_disabled is set when an anomaly is discovered.
65 * ftrace_disabled is much stronger than ftrace_enabled.
66 */
67static int ftrace_disabled __read_mostly;
68
52baf119 69static DEFINE_MUTEX(ftrace_lock);
b0fc494f 70
16444a8a
ACM
71static struct ftrace_ops ftrace_list_end __read_mostly =
72{
fb9fb015 73 .func = ftrace_stub,
16444a8a
ACM
74};
75
76static struct ftrace_ops *ftrace_list __read_mostly = &ftrace_list_end;
77ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
60a7ecf4 78ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
df4fc315 79ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
16444a8a 80
f2252935 81static void ftrace_list_func(unsigned long ip, unsigned long parent_ip)
16444a8a
ACM
82{
83 struct ftrace_ops *op = ftrace_list;
84
85 /* in case someone actually ports this to alpha! */
86 read_barrier_depends();
87
88 while (op != &ftrace_list_end) {
89 /* silly alpha */
90 read_barrier_depends();
91 op->func(ip, parent_ip);
92 op = op->next;
93 };
94}
95
df4fc315
SR
96static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
97{
0ef8cde5 98 if (!test_tsk_trace_trace(current))
df4fc315
SR
99 return;
100
101 ftrace_pid_function(ip, parent_ip);
102}
103
104static void set_ftrace_pid_function(ftrace_func_t func)
105{
106 /* do not set ftrace_pid_function to itself! */
107 if (func != ftrace_pid_func)
108 ftrace_pid_function = func;
109}
110
16444a8a 111/**
3d083395 112 * clear_ftrace_function - reset the ftrace function
16444a8a 113 *
3d083395
SR
114 * This NULLs the ftrace function and in essence stops
115 * tracing. There may be lag
16444a8a 116 */
3d083395 117void clear_ftrace_function(void)
16444a8a 118{
3d083395 119 ftrace_trace_function = ftrace_stub;
60a7ecf4 120 __ftrace_trace_function = ftrace_stub;
df4fc315 121 ftrace_pid_function = ftrace_stub;
3d083395
SR
122}
123
60a7ecf4
SR
124#ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
125/*
126 * For those archs that do not test ftrace_trace_stop in their
127 * mcount call site, we need to do it from C.
128 */
129static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
130{
131 if (function_trace_stop)
132 return;
133
134 __ftrace_trace_function(ip, parent_ip);
135}
136#endif
137
e309b41d 138static int __register_ftrace_function(struct ftrace_ops *ops)
3d083395 139{
16444a8a
ACM
140 ops->next = ftrace_list;
141 /*
142 * We are entering ops into the ftrace_list but another
143 * CPU might be walking that list. We need to make sure
144 * the ops->next pointer is valid before another CPU sees
145 * the ops pointer included into the ftrace_list.
146 */
147 smp_wmb();
148 ftrace_list = ops;
3d083395 149
b0fc494f 150 if (ftrace_enabled) {
df4fc315
SR
151 ftrace_func_t func;
152
153 if (ops->next == &ftrace_list_end)
154 func = ops->func;
155 else
156 func = ftrace_list_func;
157
978f3a45 158 if (ftrace_pid_trace) {
df4fc315
SR
159 set_ftrace_pid_function(func);
160 func = ftrace_pid_func;
161 }
162
b0fc494f
SR
163 /*
164 * For one func, simply call it directly.
165 * For more than one func, call the chain.
166 */
60a7ecf4 167#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
df4fc315 168 ftrace_trace_function = func;
60a7ecf4 169#else
df4fc315 170 __ftrace_trace_function = func;
60a7ecf4
SR
171 ftrace_trace_function = ftrace_test_stop_func;
172#endif
b0fc494f 173 }
3d083395 174
16444a8a
ACM
175 return 0;
176}
177
e309b41d 178static int __unregister_ftrace_function(struct ftrace_ops *ops)
16444a8a 179{
16444a8a 180 struct ftrace_ops **p;
16444a8a
ACM
181
182 /*
3d083395
SR
183 * If we are removing the last function, then simply point
184 * to the ftrace_stub.
16444a8a
ACM
185 */
186 if (ftrace_list == ops && ops->next == &ftrace_list_end) {
187 ftrace_trace_function = ftrace_stub;
188 ftrace_list = &ftrace_list_end;
e6ea44e9 189 return 0;
16444a8a
ACM
190 }
191
192 for (p = &ftrace_list; *p != &ftrace_list_end; p = &(*p)->next)
193 if (*p == ops)
194 break;
195
e6ea44e9
SR
196 if (*p != ops)
197 return -1;
16444a8a
ACM
198
199 *p = (*p)->next;
200
b0fc494f
SR
201 if (ftrace_enabled) {
202 /* If we only have one func left, then call that directly */
df4fc315
SR
203 if (ftrace_list->next == &ftrace_list_end) {
204 ftrace_func_t func = ftrace_list->func;
205
978f3a45 206 if (ftrace_pid_trace) {
df4fc315
SR
207 set_ftrace_pid_function(func);
208 func = ftrace_pid_func;
209 }
210#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
211 ftrace_trace_function = func;
212#else
213 __ftrace_trace_function = func;
214#endif
215 }
b0fc494f 216 }
16444a8a 217
e6ea44e9 218 return 0;
3d083395
SR
219}
220
df4fc315
SR
221static void ftrace_update_pid_func(void)
222{
223 ftrace_func_t func;
224
df4fc315 225 if (ftrace_trace_function == ftrace_stub)
10dd3ebe 226 return;
df4fc315
SR
227
228 func = ftrace_trace_function;
229
978f3a45 230 if (ftrace_pid_trace) {
df4fc315
SR
231 set_ftrace_pid_function(func);
232 func = ftrace_pid_func;
233 } else {
66eafebc
LW
234 if (func == ftrace_pid_func)
235 func = ftrace_pid_function;
df4fc315
SR
236 }
237
238#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
239 ftrace_trace_function = func;
240#else
241 __ftrace_trace_function = func;
242#endif
df4fc315
SR
243}
244
493762fc
SR
245#ifdef CONFIG_FUNCTION_PROFILER
246struct ftrace_profile {
247 struct hlist_node node;
248 unsigned long ip;
249 unsigned long counter;
0706f1c4
SR
250#ifdef CONFIG_FUNCTION_GRAPH_TRACER
251 unsigned long long time;
252#endif
8fc0c701
SR
253};
254
493762fc
SR
255struct ftrace_profile_page {
256 struct ftrace_profile_page *next;
257 unsigned long index;
258 struct ftrace_profile records[];
d61f82d0
SR
259};
260
cafb168a
SR
261struct ftrace_profile_stat {
262 atomic_t disabled;
263 struct hlist_head *hash;
264 struct ftrace_profile_page *pages;
265 struct ftrace_profile_page *start;
266 struct tracer_stat stat;
267};
268
493762fc
SR
269#define PROFILE_RECORDS_SIZE \
270 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
5072c59f 271
493762fc
SR
272#define PROFILES_PER_PAGE \
273 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
3d083395 274
fb9fb015
SR
275static int ftrace_profile_bits __read_mostly;
276static int ftrace_profile_enabled __read_mostly;
277
278/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
bac429f0
SR
279static DEFINE_MUTEX(ftrace_profile_lock);
280
cafb168a 281static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
493762fc
SR
282
283#define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
284
bac429f0
SR
285static void *
286function_stat_next(void *v, int idx)
287{
493762fc
SR
288 struct ftrace_profile *rec = v;
289 struct ftrace_profile_page *pg;
bac429f0 290
493762fc 291 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
bac429f0
SR
292
293 again:
0296e425
LZ
294 if (idx != 0)
295 rec++;
296
bac429f0
SR
297 if ((void *)rec >= (void *)&pg->records[pg->index]) {
298 pg = pg->next;
299 if (!pg)
300 return NULL;
301 rec = &pg->records[0];
493762fc
SR
302 if (!rec->counter)
303 goto again;
bac429f0
SR
304 }
305
bac429f0
SR
306 return rec;
307}
308
309static void *function_stat_start(struct tracer_stat *trace)
310{
cafb168a
SR
311 struct ftrace_profile_stat *stat =
312 container_of(trace, struct ftrace_profile_stat, stat);
313
314 if (!stat || !stat->start)
315 return NULL;
316
317 return function_stat_next(&stat->start->records[0], 0);
bac429f0
SR
318}
319
0706f1c4
SR
320#ifdef CONFIG_FUNCTION_GRAPH_TRACER
321/* function graph compares on total time */
322static int function_stat_cmp(void *p1, void *p2)
323{
324 struct ftrace_profile *a = p1;
325 struct ftrace_profile *b = p2;
326
327 if (a->time < b->time)
328 return -1;
329 if (a->time > b->time)
330 return 1;
331 else
332 return 0;
333}
334#else
335/* not function graph compares against hits */
bac429f0
SR
336static int function_stat_cmp(void *p1, void *p2)
337{
493762fc
SR
338 struct ftrace_profile *a = p1;
339 struct ftrace_profile *b = p2;
bac429f0
SR
340
341 if (a->counter < b->counter)
342 return -1;
343 if (a->counter > b->counter)
344 return 1;
345 else
346 return 0;
347}
0706f1c4 348#endif
bac429f0
SR
349
350static int function_stat_headers(struct seq_file *m)
351{
0706f1c4 352#ifdef CONFIG_FUNCTION_GRAPH_TRACER
34886c8b
SR
353 seq_printf(m, " Function "
354 "Hit Time Avg\n"
355 " -------- "
356 "--- ---- ---\n");
0706f1c4 357#else
bac429f0
SR
358 seq_printf(m, " Function Hit\n"
359 " -------- ---\n");
0706f1c4 360#endif
bac429f0
SR
361 return 0;
362}
363
364static int function_stat_show(struct seq_file *m, void *v)
365{
493762fc 366 struct ftrace_profile *rec = v;
bac429f0 367 char str[KSYM_SYMBOL_LEN];
0706f1c4 368#ifdef CONFIG_FUNCTION_GRAPH_TRACER
0706f1c4 369 static DEFINE_MUTEX(mutex);
34886c8b
SR
370 static struct trace_seq s;
371 unsigned long long avg;
0706f1c4 372#endif
bac429f0
SR
373
374 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
0706f1c4
SR
375 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
376
377#ifdef CONFIG_FUNCTION_GRAPH_TRACER
378 seq_printf(m, " ");
34886c8b
SR
379 avg = rec->time;
380 do_div(avg, rec->counter);
381
382 mutex_lock(&mutex);
383 trace_seq_init(&s);
384 trace_print_graph_duration(rec->time, &s);
385 trace_seq_puts(&s, " ");
386 trace_print_graph_duration(avg, &s);
0706f1c4
SR
387 trace_print_seq(m, &s);
388 mutex_unlock(&mutex);
389#endif
390 seq_putc(m, '\n');
bac429f0 391
bac429f0
SR
392 return 0;
393}
394
cafb168a 395static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
bac429f0 396{
493762fc 397 struct ftrace_profile_page *pg;
bac429f0 398
cafb168a 399 pg = stat->pages = stat->start;
bac429f0 400
493762fc
SR
401 while (pg) {
402 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
403 pg->index = 0;
404 pg = pg->next;
bac429f0
SR
405 }
406
cafb168a 407 memset(stat->hash, 0,
493762fc
SR
408 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
409}
bac429f0 410
cafb168a 411int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
493762fc
SR
412{
413 struct ftrace_profile_page *pg;
318e0a73
SR
414 int functions;
415 int pages;
493762fc 416 int i;
bac429f0 417
493762fc 418 /* If we already allocated, do nothing */
cafb168a 419 if (stat->pages)
493762fc 420 return 0;
bac429f0 421
cafb168a
SR
422 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
423 if (!stat->pages)
493762fc 424 return -ENOMEM;
bac429f0 425
318e0a73
SR
426#ifdef CONFIG_DYNAMIC_FTRACE
427 functions = ftrace_update_tot_cnt;
428#else
429 /*
430 * We do not know the number of functions that exist because
431 * dynamic tracing is what counts them. With past experience
432 * we have around 20K functions. That should be more than enough.
433 * It is highly unlikely we will execute every function in
434 * the kernel.
435 */
436 functions = 20000;
437#endif
438
cafb168a 439 pg = stat->start = stat->pages;
bac429f0 440
318e0a73
SR
441 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
442
443 for (i = 0; i < pages; i++) {
493762fc 444 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
493762fc 445 if (!pg->next)
318e0a73 446 goto out_free;
493762fc
SR
447 pg = pg->next;
448 }
449
450 return 0;
318e0a73
SR
451
452 out_free:
453 pg = stat->start;
454 while (pg) {
455 unsigned long tmp = (unsigned long)pg;
456
457 pg = pg->next;
458 free_page(tmp);
459 }
460
461 free_page((unsigned long)stat->pages);
462 stat->pages = NULL;
463 stat->start = NULL;
464
465 return -ENOMEM;
bac429f0
SR
466}
467
cafb168a 468static int ftrace_profile_init_cpu(int cpu)
bac429f0 469{
cafb168a 470 struct ftrace_profile_stat *stat;
493762fc 471 int size;
bac429f0 472
cafb168a
SR
473 stat = &per_cpu(ftrace_profile_stats, cpu);
474
475 if (stat->hash) {
493762fc 476 /* If the profile is already created, simply reset it */
cafb168a 477 ftrace_profile_reset(stat);
493762fc
SR
478 return 0;
479 }
bac429f0 480
493762fc
SR
481 /*
482 * We are profiling all functions, but usually only a few thousand
483 * functions are hit. We'll make a hash of 1024 items.
484 */
485 size = FTRACE_PROFILE_HASH_SIZE;
bac429f0 486
cafb168a 487 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
493762fc 488
cafb168a 489 if (!stat->hash)
493762fc
SR
490 return -ENOMEM;
491
cafb168a
SR
492 if (!ftrace_profile_bits) {
493 size--;
493762fc 494
cafb168a
SR
495 for (; size; size >>= 1)
496 ftrace_profile_bits++;
497 }
493762fc 498
318e0a73 499 /* Preallocate the function profiling pages */
cafb168a
SR
500 if (ftrace_profile_pages_init(stat) < 0) {
501 kfree(stat->hash);
502 stat->hash = NULL;
493762fc
SR
503 return -ENOMEM;
504 }
505
506 return 0;
bac429f0
SR
507}
508
cafb168a
SR
509static int ftrace_profile_init(void)
510{
511 int cpu;
512 int ret = 0;
513
514 for_each_online_cpu(cpu) {
515 ret = ftrace_profile_init_cpu(cpu);
516 if (ret)
517 break;
518 }
519
520 return ret;
521}
522
493762fc 523/* interrupts must be disabled */
cafb168a
SR
524static struct ftrace_profile *
525ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
bac429f0 526{
493762fc 527 struct ftrace_profile *rec;
bac429f0
SR
528 struct hlist_head *hhd;
529 struct hlist_node *n;
bac429f0
SR
530 unsigned long key;
531
bac429f0 532 key = hash_long(ip, ftrace_profile_bits);
cafb168a 533 hhd = &stat->hash[key];
bac429f0
SR
534
535 if (hlist_empty(hhd))
536 return NULL;
537
bac429f0
SR
538 hlist_for_each_entry_rcu(rec, n, hhd, node) {
539 if (rec->ip == ip)
493762fc
SR
540 return rec;
541 }
542
543 return NULL;
544}
545
cafb168a
SR
546static void ftrace_add_profile(struct ftrace_profile_stat *stat,
547 struct ftrace_profile *rec)
493762fc
SR
548{
549 unsigned long key;
550
551 key = hash_long(rec->ip, ftrace_profile_bits);
cafb168a 552 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
493762fc
SR
553}
554
318e0a73
SR
555/*
556 * The memory is already allocated, this simply finds a new record to use.
557 */
493762fc 558static struct ftrace_profile *
318e0a73 559ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
493762fc
SR
560{
561 struct ftrace_profile *rec = NULL;
562
318e0a73 563 /* prevent recursion (from NMIs) */
cafb168a 564 if (atomic_inc_return(&stat->disabled) != 1)
493762fc
SR
565 goto out;
566
493762fc 567 /*
318e0a73
SR
568 * Try to find the function again since an NMI
569 * could have added it
493762fc 570 */
cafb168a 571 rec = ftrace_find_profiled_func(stat, ip);
493762fc 572 if (rec)
cafb168a 573 goto out;
493762fc 574
cafb168a
SR
575 if (stat->pages->index == PROFILES_PER_PAGE) {
576 if (!stat->pages->next)
577 goto out;
578 stat->pages = stat->pages->next;
bac429f0 579 }
493762fc 580
cafb168a 581 rec = &stat->pages->records[stat->pages->index++];
493762fc 582 rec->ip = ip;
cafb168a 583 ftrace_add_profile(stat, rec);
493762fc 584
bac429f0 585 out:
cafb168a 586 atomic_dec(&stat->disabled);
bac429f0
SR
587
588 return rec;
589}
590
591static void
592function_profile_call(unsigned long ip, unsigned long parent_ip)
593{
cafb168a 594 struct ftrace_profile_stat *stat;
493762fc 595 struct ftrace_profile *rec;
bac429f0
SR
596 unsigned long flags;
597
598 if (!ftrace_profile_enabled)
599 return;
600
601 local_irq_save(flags);
cafb168a
SR
602
603 stat = &__get_cpu_var(ftrace_profile_stats);
0f6ce3de 604 if (!stat->hash || !ftrace_profile_enabled)
cafb168a
SR
605 goto out;
606
607 rec = ftrace_find_profiled_func(stat, ip);
493762fc 608 if (!rec) {
318e0a73 609 rec = ftrace_profile_alloc(stat, ip);
493762fc
SR
610 if (!rec)
611 goto out;
612 }
bac429f0
SR
613
614 rec->counter++;
615 out:
616 local_irq_restore(flags);
617}
618
0706f1c4
SR
619#ifdef CONFIG_FUNCTION_GRAPH_TRACER
620static int profile_graph_entry(struct ftrace_graph_ent *trace)
621{
622 function_profile_call(trace->func, 0);
623 return 1;
624}
625
626static void profile_graph_return(struct ftrace_graph_ret *trace)
627{
cafb168a 628 struct ftrace_profile_stat *stat;
a2a16d6a 629 unsigned long long calltime;
0706f1c4 630 struct ftrace_profile *rec;
cafb168a 631 unsigned long flags;
0706f1c4
SR
632
633 local_irq_save(flags);
cafb168a 634 stat = &__get_cpu_var(ftrace_profile_stats);
0f6ce3de 635 if (!stat->hash || !ftrace_profile_enabled)
cafb168a
SR
636 goto out;
637
a2a16d6a
SR
638 calltime = trace->rettime - trace->calltime;
639
640 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
641 int index;
642
643 index = trace->depth;
644
645 /* Append this call time to the parent time to subtract */
646 if (index)
647 current->ret_stack[index - 1].subtime += calltime;
648
649 if (current->ret_stack[index].subtime < calltime)
650 calltime -= current->ret_stack[index].subtime;
651 else
652 calltime = 0;
653 }
654
cafb168a 655 rec = ftrace_find_profiled_func(stat, trace->func);
0706f1c4 656 if (rec)
a2a16d6a
SR
657 rec->time += calltime;
658
cafb168a 659 out:
0706f1c4
SR
660 local_irq_restore(flags);
661}
662
663static int register_ftrace_profiler(void)
664{
665 return register_ftrace_graph(&profile_graph_return,
666 &profile_graph_entry);
667}
668
669static void unregister_ftrace_profiler(void)
670{
671 unregister_ftrace_graph();
672}
673#else
bac429f0
SR
674static struct ftrace_ops ftrace_profile_ops __read_mostly =
675{
fb9fb015 676 .func = function_profile_call,
bac429f0
SR
677};
678
0706f1c4
SR
679static int register_ftrace_profiler(void)
680{
681 return register_ftrace_function(&ftrace_profile_ops);
682}
683
684static void unregister_ftrace_profiler(void)
685{
686 unregister_ftrace_function(&ftrace_profile_ops);
687}
688#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
689
bac429f0
SR
690static ssize_t
691ftrace_profile_write(struct file *filp, const char __user *ubuf,
692 size_t cnt, loff_t *ppos)
693{
694 unsigned long val;
fb9fb015 695 char buf[64]; /* big enough to hold a number */
bac429f0
SR
696 int ret;
697
bac429f0
SR
698 if (cnt >= sizeof(buf))
699 return -EINVAL;
700
701 if (copy_from_user(&buf, ubuf, cnt))
702 return -EFAULT;
703
704 buf[cnt] = 0;
705
706 ret = strict_strtoul(buf, 10, &val);
707 if (ret < 0)
708 return ret;
709
710 val = !!val;
711
712 mutex_lock(&ftrace_profile_lock);
713 if (ftrace_profile_enabled ^ val) {
714 if (val) {
493762fc
SR
715 ret = ftrace_profile_init();
716 if (ret < 0) {
717 cnt = ret;
718 goto out;
719 }
720
0706f1c4
SR
721 ret = register_ftrace_profiler();
722 if (ret < 0) {
723 cnt = ret;
724 goto out;
725 }
bac429f0
SR
726 ftrace_profile_enabled = 1;
727 } else {
728 ftrace_profile_enabled = 0;
0f6ce3de
SR
729 /*
730 * unregister_ftrace_profiler calls stop_machine
731 * so this acts like an synchronize_sched.
732 */
0706f1c4 733 unregister_ftrace_profiler();
bac429f0
SR
734 }
735 }
493762fc 736 out:
bac429f0
SR
737 mutex_unlock(&ftrace_profile_lock);
738
739 filp->f_pos += cnt;
740
741 return cnt;
742}
743
493762fc
SR
744static ssize_t
745ftrace_profile_read(struct file *filp, char __user *ubuf,
746 size_t cnt, loff_t *ppos)
747{
fb9fb015 748 char buf[64]; /* big enough to hold a number */
493762fc
SR
749 int r;
750
751 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
752 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
753}
754
bac429f0
SR
755static const struct file_operations ftrace_profile_fops = {
756 .open = tracing_open_generic,
757 .read = ftrace_profile_read,
758 .write = ftrace_profile_write,
759};
760
cafb168a
SR
761/* used to initialize the real stat files */
762static struct tracer_stat function_stats __initdata = {
fb9fb015
SR
763 .name = "functions",
764 .stat_start = function_stat_start,
765 .stat_next = function_stat_next,
766 .stat_cmp = function_stat_cmp,
767 .stat_headers = function_stat_headers,
768 .stat_show = function_stat_show
cafb168a
SR
769};
770
bac429f0
SR
771static void ftrace_profile_debugfs(struct dentry *d_tracer)
772{
cafb168a 773 struct ftrace_profile_stat *stat;
bac429f0 774 struct dentry *entry;
cafb168a 775 char *name;
bac429f0 776 int ret;
cafb168a
SR
777 int cpu;
778
779 for_each_possible_cpu(cpu) {
780 stat = &per_cpu(ftrace_profile_stats, cpu);
781
782 /* allocate enough for function name + cpu number */
783 name = kmalloc(32, GFP_KERNEL);
784 if (!name) {
785 /*
786 * The files created are permanent, if something happens
787 * we still do not free memory.
788 */
789 kfree(stat);
790 WARN(1,
791 "Could not allocate stat file for cpu %d\n",
792 cpu);
793 return;
794 }
795 stat->stat = function_stats;
796 snprintf(name, 32, "function%d", cpu);
797 stat->stat.name = name;
798 ret = register_stat_tracer(&stat->stat);
799 if (ret) {
800 WARN(1,
801 "Could not register function stat for cpu %d\n",
802 cpu);
803 kfree(name);
804 return;
805 }
bac429f0
SR
806 }
807
808 entry = debugfs_create_file("function_profile_enabled", 0644,
809 d_tracer, NULL, &ftrace_profile_fops);
810 if (!entry)
811 pr_warning("Could not create debugfs "
812 "'function_profile_enabled' entry\n");
813}
814
bac429f0 815#else /* CONFIG_FUNCTION_PROFILER */
bac429f0
SR
816static void ftrace_profile_debugfs(struct dentry *d_tracer)
817{
818}
bac429f0
SR
819#endif /* CONFIG_FUNCTION_PROFILER */
820
493762fc
SR
821/* set when tracing only a pid */
822struct pid *ftrace_pid_trace;
823static struct pid * const ftrace_swapper_pid = &init_struct_pid;
824
825#ifdef CONFIG_DYNAMIC_FTRACE
826
827#ifndef CONFIG_FTRACE_MCOUNT_RECORD
828# error Dynamic ftrace depends on MCOUNT_RECORD
829#endif
830
831static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
832
833struct ftrace_func_probe {
834 struct hlist_node node;
835 struct ftrace_probe_ops *ops;
836 unsigned long flags;
837 unsigned long ip;
838 void *data;
839 struct rcu_head rcu;
840};
841
842enum {
843 FTRACE_ENABLE_CALLS = (1 << 0),
844 FTRACE_DISABLE_CALLS = (1 << 1),
845 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
846 FTRACE_ENABLE_MCOUNT = (1 << 3),
847 FTRACE_DISABLE_MCOUNT = (1 << 4),
848 FTRACE_START_FUNC_RET = (1 << 5),
849 FTRACE_STOP_FUNC_RET = (1 << 6),
850};
851
852static int ftrace_filtered;
853
854static struct dyn_ftrace *ftrace_new_addrs;
855
856static DEFINE_MUTEX(ftrace_regex_lock);
857
858struct ftrace_page {
859 struct ftrace_page *next;
860 int index;
861 struct dyn_ftrace records[];
862};
863
864#define ENTRIES_PER_PAGE \
865 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
866
867/* estimate from running different kernels */
868#define NR_TO_INIT 10000
869
870static struct ftrace_page *ftrace_pages_start;
871static struct ftrace_page *ftrace_pages;
872
873static struct dyn_ftrace *ftrace_free_records;
874
875/*
876 * This is a double for. Do not use 'break' to break out of the loop,
877 * you must use a goto.
878 */
879#define do_for_each_ftrace_rec(pg, rec) \
880 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
881 int _____i; \
882 for (_____i = 0; _____i < pg->index; _____i++) { \
883 rec = &pg->records[_____i];
884
885#define while_for_each_ftrace_rec() \
886 } \
887 }
888
ecea656d 889#ifdef CONFIG_KPROBES
f17845e5
IM
890
891static int frozen_record_count;
892
ecea656d
AS
893static inline void freeze_record(struct dyn_ftrace *rec)
894{
895 if (!(rec->flags & FTRACE_FL_FROZEN)) {
896 rec->flags |= FTRACE_FL_FROZEN;
897 frozen_record_count++;
898 }
899}
900
901static inline void unfreeze_record(struct dyn_ftrace *rec)
902{
903 if (rec->flags & FTRACE_FL_FROZEN) {
904 rec->flags &= ~FTRACE_FL_FROZEN;
905 frozen_record_count--;
906 }
907}
908
909static inline int record_frozen(struct dyn_ftrace *rec)
910{
911 return rec->flags & FTRACE_FL_FROZEN;
912}
913#else
914# define freeze_record(rec) ({ 0; })
915# define unfreeze_record(rec) ({ 0; })
916# define record_frozen(rec) ({ 0; })
917#endif /* CONFIG_KPROBES */
918
e309b41d 919static void ftrace_free_rec(struct dyn_ftrace *rec)
37ad5084 920{
ee000b7f 921 rec->freelist = ftrace_free_records;
37ad5084
SR
922 ftrace_free_records = rec;
923 rec->flags |= FTRACE_FL_FREE;
924}
925
e309b41d 926static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
3c1720f0 927{
37ad5084
SR
928 struct dyn_ftrace *rec;
929
930 /* First check for freed records */
931 if (ftrace_free_records) {
932 rec = ftrace_free_records;
933
37ad5084 934 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
6912896e 935 FTRACE_WARN_ON_ONCE(1);
37ad5084
SR
936 ftrace_free_records = NULL;
937 return NULL;
938 }
939
ee000b7f 940 ftrace_free_records = rec->freelist;
37ad5084
SR
941 memset(rec, 0, sizeof(*rec));
942 return rec;
943 }
944
3c1720f0 945 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
08f5ac90
SR
946 if (!ftrace_pages->next) {
947 /* allocate another page */
948 ftrace_pages->next =
949 (void *)get_zeroed_page(GFP_KERNEL);
950 if (!ftrace_pages->next)
951 return NULL;
952 }
3c1720f0
SR
953 ftrace_pages = ftrace_pages->next;
954 }
955
956 return &ftrace_pages->records[ftrace_pages->index++];
957}
958
08f5ac90 959static struct dyn_ftrace *
d61f82d0 960ftrace_record_ip(unsigned long ip)
3d083395 961{
08f5ac90 962 struct dyn_ftrace *rec;
3d083395 963
f3c7ac40 964 if (ftrace_disabled)
08f5ac90 965 return NULL;
3d083395 966
08f5ac90
SR
967 rec = ftrace_alloc_dyn_node(ip);
968 if (!rec)
969 return NULL;
3d083395 970
08f5ac90 971 rec->ip = ip;
ee000b7f 972 rec->newlist = ftrace_new_addrs;
e94142a6 973 ftrace_new_addrs = rec;
3d083395 974
08f5ac90 975 return rec;
3d083395
SR
976}
977
b17e8a37
SR
978static void print_ip_ins(const char *fmt, unsigned char *p)
979{
980 int i;
981
982 printk(KERN_CONT "%s", fmt);
983
984 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
985 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
986}
987
31e88909 988static void ftrace_bug(int failed, unsigned long ip)
b17e8a37
SR
989{
990 switch (failed) {
991 case -EFAULT:
992 FTRACE_WARN_ON_ONCE(1);
993 pr_info("ftrace faulted on modifying ");
994 print_ip_sym(ip);
995 break;
996 case -EINVAL:
997 FTRACE_WARN_ON_ONCE(1);
998 pr_info("ftrace failed to modify ");
999 print_ip_sym(ip);
b17e8a37 1000 print_ip_ins(" actual: ", (unsigned char *)ip);
b17e8a37
SR
1001 printk(KERN_CONT "\n");
1002 break;
1003 case -EPERM:
1004 FTRACE_WARN_ON_ONCE(1);
1005 pr_info("ftrace faulted on writing ");
1006 print_ip_sym(ip);
1007 break;
1008 default:
1009 FTRACE_WARN_ON_ONCE(1);
1010 pr_info("ftrace faulted on unknown error ");
1011 print_ip_sym(ip);
1012 }
1013}
1014
3c1720f0 1015
0eb96701 1016static int
31e88909 1017__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
5072c59f 1018{
e7d3737e 1019 unsigned long ftrace_addr;
6a24a244 1020 unsigned long ip, fl;
e7d3737e 1021
f0001207 1022 ftrace_addr = (unsigned long)FTRACE_ADDR;
5072c59f
SR
1023
1024 ip = rec->ip;
1025
982c350b
SR
1026 /*
1027 * If this record is not to be traced and
1028 * it is not enabled then do nothing.
1029 *
1030 * If this record is not to be traced and
57794a9d 1031 * it is enabled then disable it.
982c350b
SR
1032 *
1033 */
1034 if (rec->flags & FTRACE_FL_NOTRACE) {
1035 if (rec->flags & FTRACE_FL_ENABLED)
1036 rec->flags &= ~FTRACE_FL_ENABLED;
1037 else
1038 return 0;
1039
1040 } else if (ftrace_filtered && enable) {
5072c59f 1041 /*
982c350b 1042 * Filtering is on:
5072c59f 1043 */
a4500b84 1044
982c350b 1045 fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_ENABLED);
5072c59f 1046
982c350b
SR
1047 /* Record is filtered and enabled, do nothing */
1048 if (fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED))
0eb96701 1049 return 0;
5072c59f 1050
57794a9d 1051 /* Record is not filtered or enabled, do nothing */
982c350b
SR
1052 if (!fl)
1053 return 0;
1054
1055 /* Record is not filtered but enabled, disable it */
1056 if (fl == FTRACE_FL_ENABLED)
5072c59f 1057 rec->flags &= ~FTRACE_FL_ENABLED;
982c350b
SR
1058 else
1059 /* Otherwise record is filtered but not enabled, enable it */
5072c59f 1060 rec->flags |= FTRACE_FL_ENABLED;
5072c59f 1061 } else {
982c350b 1062 /* Disable or not filtered */
5072c59f 1063
41c52c0d 1064 if (enable) {
982c350b 1065 /* if record is enabled, do nothing */
5072c59f 1066 if (rec->flags & FTRACE_FL_ENABLED)
0eb96701 1067 return 0;
982c350b 1068
5072c59f 1069 rec->flags |= FTRACE_FL_ENABLED;
982c350b 1070
5072c59f 1071 } else {
982c350b 1072
57794a9d 1073 /* if record is not enabled, do nothing */
5072c59f 1074 if (!(rec->flags & FTRACE_FL_ENABLED))
0eb96701 1075 return 0;
982c350b 1076
5072c59f
SR
1077 rec->flags &= ~FTRACE_FL_ENABLED;
1078 }
1079 }
1080
982c350b 1081 if (rec->flags & FTRACE_FL_ENABLED)
e7d3737e 1082 return ftrace_make_call(rec, ftrace_addr);
31e88909 1083 else
e7d3737e 1084 return ftrace_make_nop(NULL, rec, ftrace_addr);
5072c59f
SR
1085}
1086
e309b41d 1087static void ftrace_replace_code(int enable)
3c1720f0 1088{
3c1720f0
SR
1089 struct dyn_ftrace *rec;
1090 struct ftrace_page *pg;
6a24a244 1091 int failed;
3c1720f0 1092
265c831c
SR
1093 do_for_each_ftrace_rec(pg, rec) {
1094 /*
fa9d13cf
Z
1095 * Skip over free records, records that have
1096 * failed and not converted.
265c831c
SR
1097 */
1098 if (rec->flags & FTRACE_FL_FREE ||
fa9d13cf 1099 rec->flags & FTRACE_FL_FAILED ||
03303549 1100 !(rec->flags & FTRACE_FL_CONVERTED))
265c831c
SR
1101 continue;
1102
1103 /* ignore updates to this record's mcount site */
1104 if (get_kprobe((void *)rec->ip)) {
1105 freeze_record(rec);
1106 continue;
1107 } else {
1108 unfreeze_record(rec);
1109 }
f22f9a89 1110
265c831c 1111 failed = __ftrace_replace_code(rec, enable);
fa9d13cf 1112 if (failed) {
265c831c
SR
1113 rec->flags |= FTRACE_FL_FAILED;
1114 if ((system_state == SYSTEM_BOOTING) ||
1115 !core_kernel_text(rec->ip)) {
1116 ftrace_free_rec(rec);
4377245a 1117 } else {
265c831c 1118 ftrace_bug(failed, rec->ip);
4377245a
SR
1119 /* Stop processing */
1120 return;
1121 }
3c1720f0 1122 }
265c831c 1123 } while_for_each_ftrace_rec();
3c1720f0
SR
1124}
1125
492a7ea5 1126static int
31e88909 1127ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
3c1720f0
SR
1128{
1129 unsigned long ip;
593eb8a2 1130 int ret;
3c1720f0
SR
1131
1132 ip = rec->ip;
1133
25aac9dc 1134 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
593eb8a2 1135 if (ret) {
31e88909 1136 ftrace_bug(ret, ip);
3c1720f0 1137 rec->flags |= FTRACE_FL_FAILED;
492a7ea5 1138 return 0;
37ad5084 1139 }
492a7ea5 1140 return 1;
3c1720f0
SR
1141}
1142
000ab691
SR
1143/*
1144 * archs can override this function if they must do something
1145 * before the modifying code is performed.
1146 */
1147int __weak ftrace_arch_code_modify_prepare(void)
1148{
1149 return 0;
1150}
1151
1152/*
1153 * archs can override this function if they must do something
1154 * after the modifying code is performed.
1155 */
1156int __weak ftrace_arch_code_modify_post_process(void)
1157{
1158 return 0;
1159}
1160
e309b41d 1161static int __ftrace_modify_code(void *data)
3d083395 1162{
d61f82d0
SR
1163 int *command = data;
1164
a3583244 1165 if (*command & FTRACE_ENABLE_CALLS)
d61f82d0 1166 ftrace_replace_code(1);
a3583244 1167 else if (*command & FTRACE_DISABLE_CALLS)
d61f82d0
SR
1168 ftrace_replace_code(0);
1169
1170 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1171 ftrace_update_ftrace_func(ftrace_trace_function);
1172
5a45cfe1
SR
1173 if (*command & FTRACE_START_FUNC_RET)
1174 ftrace_enable_ftrace_graph_caller();
1175 else if (*command & FTRACE_STOP_FUNC_RET)
1176 ftrace_disable_ftrace_graph_caller();
1177
d61f82d0 1178 return 0;
3d083395
SR
1179}
1180
e309b41d 1181static void ftrace_run_update_code(int command)
3d083395 1182{
000ab691
SR
1183 int ret;
1184
1185 ret = ftrace_arch_code_modify_prepare();
1186 FTRACE_WARN_ON(ret);
1187 if (ret)
1188 return;
1189
784e2d76 1190 stop_machine(__ftrace_modify_code, &command, NULL);
000ab691
SR
1191
1192 ret = ftrace_arch_code_modify_post_process();
1193 FTRACE_WARN_ON(ret);
3d083395
SR
1194}
1195
d61f82d0 1196static ftrace_func_t saved_ftrace_func;
60a7ecf4 1197static int ftrace_start_up;
df4fc315
SR
1198
1199static void ftrace_startup_enable(int command)
1200{
1201 if (saved_ftrace_func != ftrace_trace_function) {
1202 saved_ftrace_func = ftrace_trace_function;
1203 command |= FTRACE_UPDATE_TRACE_FUNC;
1204 }
1205
1206 if (!command || !ftrace_enabled)
1207 return;
1208
1209 ftrace_run_update_code(command);
1210}
d61f82d0 1211
5a45cfe1 1212static void ftrace_startup(int command)
3d083395 1213{
4eebcc81
SR
1214 if (unlikely(ftrace_disabled))
1215 return;
1216
60a7ecf4 1217 ftrace_start_up++;
982c350b 1218 command |= FTRACE_ENABLE_CALLS;
d61f82d0 1219
df4fc315 1220 ftrace_startup_enable(command);
3d083395
SR
1221}
1222
5a45cfe1 1223static void ftrace_shutdown(int command)
3d083395 1224{
4eebcc81
SR
1225 if (unlikely(ftrace_disabled))
1226 return;
1227
60a7ecf4 1228 ftrace_start_up--;
9ea1a153
FW
1229 /*
1230 * Just warn in case of unbalance, no need to kill ftrace, it's not
1231 * critical but the ftrace_call callers may be never nopped again after
1232 * further ftrace uses.
1233 */
1234 WARN_ON_ONCE(ftrace_start_up < 0);
1235
60a7ecf4 1236 if (!ftrace_start_up)
d61f82d0 1237 command |= FTRACE_DISABLE_CALLS;
3d083395 1238
d61f82d0
SR
1239 if (saved_ftrace_func != ftrace_trace_function) {
1240 saved_ftrace_func = ftrace_trace_function;
1241 command |= FTRACE_UPDATE_TRACE_FUNC;
1242 }
3d083395 1243
d61f82d0 1244 if (!command || !ftrace_enabled)
e6ea44e9 1245 return;
d61f82d0
SR
1246
1247 ftrace_run_update_code(command);
3d083395
SR
1248}
1249
e309b41d 1250static void ftrace_startup_sysctl(void)
b0fc494f 1251{
d61f82d0
SR
1252 int command = FTRACE_ENABLE_MCOUNT;
1253
4eebcc81
SR
1254 if (unlikely(ftrace_disabled))
1255 return;
1256
d61f82d0
SR
1257 /* Force update next time */
1258 saved_ftrace_func = NULL;
60a7ecf4
SR
1259 /* ftrace_start_up is true if we want ftrace running */
1260 if (ftrace_start_up)
d61f82d0
SR
1261 command |= FTRACE_ENABLE_CALLS;
1262
1263 ftrace_run_update_code(command);
b0fc494f
SR
1264}
1265
e309b41d 1266static void ftrace_shutdown_sysctl(void)
b0fc494f 1267{
d61f82d0
SR
1268 int command = FTRACE_DISABLE_MCOUNT;
1269
4eebcc81
SR
1270 if (unlikely(ftrace_disabled))
1271 return;
1272
60a7ecf4
SR
1273 /* ftrace_start_up is true if ftrace is running */
1274 if (ftrace_start_up)
d61f82d0
SR
1275 command |= FTRACE_DISABLE_CALLS;
1276
1277 ftrace_run_update_code(command);
b0fc494f
SR
1278}
1279
3d083395
SR
1280static cycle_t ftrace_update_time;
1281static unsigned long ftrace_update_cnt;
1282unsigned long ftrace_update_tot_cnt;
1283
31e88909 1284static int ftrace_update_code(struct module *mod)
3d083395 1285{
e94142a6 1286 struct dyn_ftrace *p;
f22f9a89 1287 cycle_t start, stop;
3d083395 1288
750ed1a4 1289 start = ftrace_now(raw_smp_processor_id());
3d083395
SR
1290 ftrace_update_cnt = 0;
1291
e94142a6 1292 while (ftrace_new_addrs) {
3d083395 1293
08f5ac90
SR
1294 /* If something went wrong, bail without enabling anything */
1295 if (unlikely(ftrace_disabled))
1296 return -1;
f22f9a89 1297
e94142a6 1298 p = ftrace_new_addrs;
ee000b7f 1299 ftrace_new_addrs = p->newlist;
e94142a6 1300 p->flags = 0L;
f22f9a89 1301
08f5ac90 1302 /* convert record (i.e, patch mcount-call with NOP) */
31e88909 1303 if (ftrace_code_disable(mod, p)) {
08f5ac90
SR
1304 p->flags |= FTRACE_FL_CONVERTED;
1305 ftrace_update_cnt++;
1306 } else
1307 ftrace_free_rec(p);
3d083395
SR
1308 }
1309
750ed1a4 1310 stop = ftrace_now(raw_smp_processor_id());
3d083395
SR
1311 ftrace_update_time = stop - start;
1312 ftrace_update_tot_cnt += ftrace_update_cnt;
1313
16444a8a
ACM
1314 return 0;
1315}
1316
68bf21aa 1317static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
3c1720f0
SR
1318{
1319 struct ftrace_page *pg;
1320 int cnt;
1321 int i;
3c1720f0
SR
1322
1323 /* allocate a few pages */
1324 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1325 if (!ftrace_pages_start)
1326 return -1;
1327
1328 /*
1329 * Allocate a few more pages.
1330 *
1331 * TODO: have some parser search vmlinux before
1332 * final linking to find all calls to ftrace.
1333 * Then we can:
1334 * a) know how many pages to allocate.
1335 * and/or
1336 * b) set up the table then.
1337 *
1338 * The dynamic code is still necessary for
1339 * modules.
1340 */
1341
1342 pg = ftrace_pages = ftrace_pages_start;
1343
68bf21aa 1344 cnt = num_to_init / ENTRIES_PER_PAGE;
08f5ac90 1345 pr_info("ftrace: allocating %ld entries in %d pages\n",
5821e1b7 1346 num_to_init, cnt + 1);
3c1720f0
SR
1347
1348 for (i = 0; i < cnt; i++) {
1349 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1350
1351 /* If we fail, we'll try later anyway */
1352 if (!pg->next)
1353 break;
1354
1355 pg = pg->next;
1356 }
1357
1358 return 0;
1359}
1360
5072c59f
SR
1361enum {
1362 FTRACE_ITER_FILTER = (1 << 0),
1363 FTRACE_ITER_CONT = (1 << 1),
41c52c0d 1364 FTRACE_ITER_NOTRACE = (1 << 2),
eb9a7bf0 1365 FTRACE_ITER_FAILURES = (1 << 3),
0c75a3ed 1366 FTRACE_ITER_PRINTALL = (1 << 4),
8fc0c701 1367 FTRACE_ITER_HASH = (1 << 5),
5072c59f
SR
1368};
1369
1370#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1371
1372struct ftrace_iterator {
5072c59f 1373 struct ftrace_page *pg;
8fc0c701 1374 int hidx;
431aa3fb 1375 int idx;
5072c59f
SR
1376 unsigned flags;
1377 unsigned char buffer[FTRACE_BUFF_MAX+1];
1378 unsigned buffer_idx;
1379 unsigned filtered;
1380};
1381
8fc0c701
SR
1382static void *
1383t_hash_next(struct seq_file *m, void *v, loff_t *pos)
1384{
1385 struct ftrace_iterator *iter = m->private;
1386 struct hlist_node *hnd = v;
1387 struct hlist_head *hhd;
1388
1389 WARN_ON(!(iter->flags & FTRACE_ITER_HASH));
1390
1391 (*pos)++;
1392
1393 retry:
1394 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1395 return NULL;
1396
1397 hhd = &ftrace_func_hash[iter->hidx];
1398
1399 if (hlist_empty(hhd)) {
1400 iter->hidx++;
1401 hnd = NULL;
1402 goto retry;
1403 }
1404
1405 if (!hnd)
1406 hnd = hhd->first;
1407 else {
1408 hnd = hnd->next;
1409 if (!hnd) {
1410 iter->hidx++;
1411 goto retry;
1412 }
1413 }
1414
1415 return hnd;
1416}
1417
1418static void *t_hash_start(struct seq_file *m, loff_t *pos)
1419{
1420 struct ftrace_iterator *iter = m->private;
1421 void *p = NULL;
d82d6244
LZ
1422 loff_t l;
1423
1424 if (!(iter->flags & FTRACE_ITER_HASH))
1425 *pos = 0;
8fc0c701
SR
1426
1427 iter->flags |= FTRACE_ITER_HASH;
1428
d82d6244
LZ
1429 iter->hidx = 0;
1430 for (l = 0; l <= *pos; ) {
1431 p = t_hash_next(m, p, &l);
1432 if (!p)
1433 break;
1434 }
1435 return p;
8fc0c701
SR
1436}
1437
1438static int t_hash_show(struct seq_file *m, void *v)
1439{
b6887d79 1440 struct ftrace_func_probe *rec;
8fc0c701
SR
1441 struct hlist_node *hnd = v;
1442 char str[KSYM_SYMBOL_LEN];
1443
b6887d79 1444 rec = hlist_entry(hnd, struct ftrace_func_probe, node);
8fc0c701 1445
809dcf29
SR
1446 if (rec->ops->print)
1447 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1448
8fc0c701
SR
1449 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1450 seq_printf(m, "%s:", str);
1451
1452 kallsyms_lookup((unsigned long)rec->ops->func, NULL, NULL, NULL, str);
1453 seq_printf(m, "%s", str);
1454
1455 if (rec->data)
1456 seq_printf(m, ":%p", rec->data);
1457 seq_putc(m, '\n');
1458
1459 return 0;
1460}
1461
e309b41d 1462static void *
5072c59f
SR
1463t_next(struct seq_file *m, void *v, loff_t *pos)
1464{
1465 struct ftrace_iterator *iter = m->private;
1466 struct dyn_ftrace *rec = NULL;
1467
8fc0c701
SR
1468 if (iter->flags & FTRACE_ITER_HASH)
1469 return t_hash_next(m, v, pos);
1470
5072c59f
SR
1471 (*pos)++;
1472
0c75a3ed
SR
1473 if (iter->flags & FTRACE_ITER_PRINTALL)
1474 return NULL;
1475
5072c59f
SR
1476 retry:
1477 if (iter->idx >= iter->pg->index) {
1478 if (iter->pg->next) {
1479 iter->pg = iter->pg->next;
1480 iter->idx = 0;
1481 goto retry;
1482 }
1483 } else {
1484 rec = &iter->pg->records[iter->idx++];
a9fdda33
SR
1485 if ((rec->flags & FTRACE_FL_FREE) ||
1486
1487 (!(iter->flags & FTRACE_ITER_FAILURES) &&
eb9a7bf0
AS
1488 (rec->flags & FTRACE_FL_FAILED)) ||
1489
1490 ((iter->flags & FTRACE_ITER_FAILURES) &&
a9fdda33 1491 !(rec->flags & FTRACE_FL_FAILED)) ||
eb9a7bf0 1492
0183fb1c
SR
1493 ((iter->flags & FTRACE_ITER_FILTER) &&
1494 !(rec->flags & FTRACE_FL_FILTER)) ||
1495
41c52c0d
SR
1496 ((iter->flags & FTRACE_ITER_NOTRACE) &&
1497 !(rec->flags & FTRACE_FL_NOTRACE))) {
5072c59f
SR
1498 rec = NULL;
1499 goto retry;
1500 }
1501 }
1502
5072c59f
SR
1503 return rec;
1504}
1505
1506static void *t_start(struct seq_file *m, loff_t *pos)
1507{
1508 struct ftrace_iterator *iter = m->private;
1509 void *p = NULL;
694ce0a5 1510 loff_t l;
5072c59f 1511
8fc0c701 1512 mutex_lock(&ftrace_lock);
0c75a3ed
SR
1513 /*
1514 * For set_ftrace_filter reading, if we have the filter
1515 * off, we can short cut and just print out that all
1516 * functions are enabled.
1517 */
1518 if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
1519 if (*pos > 0)
8fc0c701 1520 return t_hash_start(m, pos);
0c75a3ed 1521 iter->flags |= FTRACE_ITER_PRINTALL;
0c75a3ed
SR
1522 return iter;
1523 }
1524
8fc0c701
SR
1525 if (iter->flags & FTRACE_ITER_HASH)
1526 return t_hash_start(m, pos);
1527
694ce0a5
LZ
1528 iter->pg = ftrace_pages_start;
1529 iter->idx = 0;
1530 for (l = 0; l <= *pos; ) {
1531 p = t_next(m, p, &l);
1532 if (!p)
1533 break;
50cdaf08 1534 }
5821e1b7 1535
694ce0a5 1536 if (!p && iter->flags & FTRACE_ITER_FILTER)
8fc0c701
SR
1537 return t_hash_start(m, pos);
1538
5072c59f
SR
1539 return p;
1540}
1541
1542static void t_stop(struct seq_file *m, void *p)
1543{
8fc0c701 1544 mutex_unlock(&ftrace_lock);
5072c59f
SR
1545}
1546
1547static int t_show(struct seq_file *m, void *v)
1548{
0c75a3ed 1549 struct ftrace_iterator *iter = m->private;
5072c59f
SR
1550 struct dyn_ftrace *rec = v;
1551 char str[KSYM_SYMBOL_LEN];
1552
8fc0c701
SR
1553 if (iter->flags & FTRACE_ITER_HASH)
1554 return t_hash_show(m, v);
1555
0c75a3ed
SR
1556 if (iter->flags & FTRACE_ITER_PRINTALL) {
1557 seq_printf(m, "#### all functions enabled ####\n");
1558 return 0;
1559 }
1560
5072c59f
SR
1561 if (!rec)
1562 return 0;
1563
1564 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1565
50cdaf08 1566 seq_printf(m, "%s\n", str);
5072c59f
SR
1567
1568 return 0;
1569}
1570
1571static struct seq_operations show_ftrace_seq_ops = {
1572 .start = t_start,
1573 .next = t_next,
1574 .stop = t_stop,
1575 .show = t_show,
1576};
1577
e309b41d 1578static int
5072c59f
SR
1579ftrace_avail_open(struct inode *inode, struct file *file)
1580{
1581 struct ftrace_iterator *iter;
1582 int ret;
1583
4eebcc81
SR
1584 if (unlikely(ftrace_disabled))
1585 return -ENODEV;
1586
5072c59f
SR
1587 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1588 if (!iter)
1589 return -ENOMEM;
1590
1591 iter->pg = ftrace_pages_start;
5072c59f
SR
1592
1593 ret = seq_open(file, &show_ftrace_seq_ops);
1594 if (!ret) {
1595 struct seq_file *m = file->private_data;
4bf39a94 1596
5072c59f 1597 m->private = iter;
4bf39a94 1598 } else {
5072c59f 1599 kfree(iter);
4bf39a94 1600 }
5072c59f
SR
1601
1602 return ret;
1603}
1604
1605int ftrace_avail_release(struct inode *inode, struct file *file)
1606{
1607 struct seq_file *m = (struct seq_file *)file->private_data;
1608 struct ftrace_iterator *iter = m->private;
1609
1610 seq_release(inode, file);
1611 kfree(iter);
4bf39a94 1612
5072c59f
SR
1613 return 0;
1614}
1615
eb9a7bf0
AS
1616static int
1617ftrace_failures_open(struct inode *inode, struct file *file)
1618{
1619 int ret;
1620 struct seq_file *m;
1621 struct ftrace_iterator *iter;
1622
1623 ret = ftrace_avail_open(inode, file);
1624 if (!ret) {
1625 m = (struct seq_file *)file->private_data;
1626 iter = (struct ftrace_iterator *)m->private;
1627 iter->flags = FTRACE_ITER_FAILURES;
1628 }
1629
1630 return ret;
1631}
1632
1633
41c52c0d 1634static void ftrace_filter_reset(int enable)
5072c59f
SR
1635{
1636 struct ftrace_page *pg;
1637 struct dyn_ftrace *rec;
41c52c0d 1638 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
5072c59f 1639
52baf119 1640 mutex_lock(&ftrace_lock);
41c52c0d
SR
1641 if (enable)
1642 ftrace_filtered = 0;
265c831c
SR
1643 do_for_each_ftrace_rec(pg, rec) {
1644 if (rec->flags & FTRACE_FL_FAILED)
1645 continue;
1646 rec->flags &= ~type;
1647 } while_for_each_ftrace_rec();
52baf119 1648 mutex_unlock(&ftrace_lock);
5072c59f
SR
1649}
1650
e309b41d 1651static int
41c52c0d 1652ftrace_regex_open(struct inode *inode, struct file *file, int enable)
5072c59f
SR
1653{
1654 struct ftrace_iterator *iter;
1655 int ret = 0;
1656
4eebcc81
SR
1657 if (unlikely(ftrace_disabled))
1658 return -ENODEV;
1659
5072c59f
SR
1660 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1661 if (!iter)
1662 return -ENOMEM;
1663
41c52c0d 1664 mutex_lock(&ftrace_regex_lock);
5072c59f
SR
1665 if ((file->f_mode & FMODE_WRITE) &&
1666 !(file->f_flags & O_APPEND))
41c52c0d 1667 ftrace_filter_reset(enable);
5072c59f
SR
1668
1669 if (file->f_mode & FMODE_READ) {
1670 iter->pg = ftrace_pages_start;
41c52c0d
SR
1671 iter->flags = enable ? FTRACE_ITER_FILTER :
1672 FTRACE_ITER_NOTRACE;
5072c59f
SR
1673
1674 ret = seq_open(file, &show_ftrace_seq_ops);
1675 if (!ret) {
1676 struct seq_file *m = file->private_data;
1677 m->private = iter;
1678 } else
1679 kfree(iter);
1680 } else
1681 file->private_data = iter;
41c52c0d 1682 mutex_unlock(&ftrace_regex_lock);
5072c59f
SR
1683
1684 return ret;
1685}
1686
41c52c0d
SR
1687static int
1688ftrace_filter_open(struct inode *inode, struct file *file)
1689{
1690 return ftrace_regex_open(inode, file, 1);
1691}
1692
1693static int
1694ftrace_notrace_open(struct inode *inode, struct file *file)
1695{
1696 return ftrace_regex_open(inode, file, 0);
1697}
1698
e309b41d 1699static loff_t
41c52c0d 1700ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
5072c59f
SR
1701{
1702 loff_t ret;
1703
1704 if (file->f_mode & FMODE_READ)
1705 ret = seq_lseek(file, offset, origin);
1706 else
1707 file->f_pos = ret = 1;
1708
1709 return ret;
1710}
1711
1712enum {
1713 MATCH_FULL,
1714 MATCH_FRONT_ONLY,
1715 MATCH_MIDDLE_ONLY,
1716 MATCH_END_ONLY,
1717};
1718
9f4801e3
SR
1719/*
1720 * (static function - no need for kernel doc)
1721 *
1722 * Pass in a buffer containing a glob and this function will
1723 * set search to point to the search part of the buffer and
1724 * return the type of search it is (see enum above).
1725 * This does modify buff.
1726 *
1727 * Returns enum type.
1728 * search returns the pointer to use for comparison.
1729 * not returns 1 if buff started with a '!'
1730 * 0 otherwise.
1731 */
1732static int
64e7c440 1733ftrace_setup_glob(char *buff, int len, char **search, int *not)
5072c59f 1734{
5072c59f 1735 int type = MATCH_FULL;
9f4801e3 1736 int i;
ea3a6d6d
SR
1737
1738 if (buff[0] == '!') {
9f4801e3 1739 *not = 1;
ea3a6d6d
SR
1740 buff++;
1741 len--;
9f4801e3
SR
1742 } else
1743 *not = 0;
1744
1745 *search = buff;
5072c59f
SR
1746
1747 for (i = 0; i < len; i++) {
1748 if (buff[i] == '*') {
1749 if (!i) {
9f4801e3 1750 *search = buff + 1;
5072c59f 1751 type = MATCH_END_ONLY;
5072c59f 1752 } else {
9f4801e3 1753 if (type == MATCH_END_ONLY)
5072c59f 1754 type = MATCH_MIDDLE_ONLY;
9f4801e3 1755 else
5072c59f 1756 type = MATCH_FRONT_ONLY;
5072c59f
SR
1757 buff[i] = 0;
1758 break;
1759 }
1760 }
1761 }
1762
9f4801e3
SR
1763 return type;
1764}
1765
64e7c440 1766static int ftrace_match(char *str, char *regex, int len, int type)
9f4801e3 1767{
9f4801e3
SR
1768 int matched = 0;
1769 char *ptr;
1770
9f4801e3
SR
1771 switch (type) {
1772 case MATCH_FULL:
1773 if (strcmp(str, regex) == 0)
1774 matched = 1;
1775 break;
1776 case MATCH_FRONT_ONLY:
1777 if (strncmp(str, regex, len) == 0)
1778 matched = 1;
1779 break;
1780 case MATCH_MIDDLE_ONLY:
1781 if (strstr(str, regex))
1782 matched = 1;
1783 break;
1784 case MATCH_END_ONLY:
1785 ptr = strstr(str, regex);
1786 if (ptr && (ptr[len] == 0))
1787 matched = 1;
1788 break;
1789 }
1790
1791 return matched;
1792}
1793
64e7c440
SR
1794static int
1795ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
1796{
1797 char str[KSYM_SYMBOL_LEN];
1798
1799 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1800 return ftrace_match(str, regex, len, type);
1801}
1802
9f4801e3
SR
1803static void ftrace_match_records(char *buff, int len, int enable)
1804{
6a24a244 1805 unsigned int search_len;
9f4801e3
SR
1806 struct ftrace_page *pg;
1807 struct dyn_ftrace *rec;
6a24a244
SR
1808 unsigned long flag;
1809 char *search;
9f4801e3 1810 int type;
9f4801e3
SR
1811 int not;
1812
6a24a244 1813 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
9f4801e3
SR
1814 type = ftrace_setup_glob(buff, len, &search, &not);
1815
1816 search_len = strlen(search);
1817
52baf119 1818 mutex_lock(&ftrace_lock);
265c831c 1819 do_for_each_ftrace_rec(pg, rec) {
265c831c
SR
1820
1821 if (rec->flags & FTRACE_FL_FAILED)
1822 continue;
9f4801e3
SR
1823
1824 if (ftrace_match_record(rec, search, search_len, type)) {
265c831c
SR
1825 if (not)
1826 rec->flags &= ~flag;
1827 else
1828 rec->flags |= flag;
1829 }
e68746a2
SR
1830 /*
1831 * Only enable filtering if we have a function that
1832 * is filtered on.
1833 */
1834 if (enable && (rec->flags & FTRACE_FL_FILTER))
1835 ftrace_filtered = 1;
265c831c 1836 } while_for_each_ftrace_rec();
52baf119 1837 mutex_unlock(&ftrace_lock);
5072c59f
SR
1838}
1839
64e7c440
SR
1840static int
1841ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
1842 char *regex, int len, int type)
1843{
1844 char str[KSYM_SYMBOL_LEN];
1845 char *modname;
1846
1847 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
1848
1849 if (!modname || strcmp(modname, mod))
1850 return 0;
1851
1852 /* blank search means to match all funcs in the mod */
1853 if (len)
1854 return ftrace_match(str, regex, len, type);
1855 else
1856 return 1;
1857}
1858
1859static void ftrace_match_module_records(char *buff, char *mod, int enable)
1860{
6a24a244 1861 unsigned search_len = 0;
64e7c440
SR
1862 struct ftrace_page *pg;
1863 struct dyn_ftrace *rec;
1864 int type = MATCH_FULL;
6a24a244
SR
1865 char *search = buff;
1866 unsigned long flag;
64e7c440
SR
1867 int not = 0;
1868
6a24a244
SR
1869 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1870
64e7c440
SR
1871 /* blank or '*' mean the same */
1872 if (strcmp(buff, "*") == 0)
1873 buff[0] = 0;
1874
1875 /* handle the case of 'dont filter this module' */
1876 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
1877 buff[0] = 0;
1878 not = 1;
1879 }
1880
1881 if (strlen(buff)) {
1882 type = ftrace_setup_glob(buff, strlen(buff), &search, &not);
1883 search_len = strlen(search);
1884 }
1885
52baf119 1886 mutex_lock(&ftrace_lock);
64e7c440
SR
1887 do_for_each_ftrace_rec(pg, rec) {
1888
1889 if (rec->flags & FTRACE_FL_FAILED)
1890 continue;
1891
1892 if (ftrace_match_module_record(rec, mod,
1893 search, search_len, type)) {
1894 if (not)
1895 rec->flags &= ~flag;
1896 else
1897 rec->flags |= flag;
1898 }
e68746a2
SR
1899 if (enable && (rec->flags & FTRACE_FL_FILTER))
1900 ftrace_filtered = 1;
64e7c440
SR
1901
1902 } while_for_each_ftrace_rec();
52baf119 1903 mutex_unlock(&ftrace_lock);
64e7c440
SR
1904}
1905
f6180773
SR
1906/*
1907 * We register the module command as a template to show others how
1908 * to register the a command as well.
1909 */
1910
1911static int
1912ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
1913{
1914 char *mod;
1915
1916 /*
1917 * cmd == 'mod' because we only registered this func
1918 * for the 'mod' ftrace_func_command.
1919 * But if you register one func with multiple commands,
1920 * you can tell which command was used by the cmd
1921 * parameter.
1922 */
1923
1924 /* we must have a module name */
1925 if (!param)
1926 return -EINVAL;
1927
1928 mod = strsep(&param, ":");
1929 if (!strlen(mod))
1930 return -EINVAL;
1931
1932 ftrace_match_module_records(func, mod, enable);
1933 return 0;
1934}
1935
1936static struct ftrace_func_command ftrace_mod_cmd = {
1937 .name = "mod",
1938 .func = ftrace_mod_callback,
1939};
1940
1941static int __init ftrace_mod_cmd_init(void)
1942{
1943 return register_ftrace_command(&ftrace_mod_cmd);
1944}
1945device_initcall(ftrace_mod_cmd_init);
1946
59df055f 1947static void
b6887d79 1948function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
59df055f 1949{
b6887d79 1950 struct ftrace_func_probe *entry;
59df055f
SR
1951 struct hlist_head *hhd;
1952 struct hlist_node *n;
1953 unsigned long key;
1954 int resched;
1955
1956 key = hash_long(ip, FTRACE_HASH_BITS);
1957
1958 hhd = &ftrace_func_hash[key];
1959
1960 if (hlist_empty(hhd))
1961 return;
1962
1963 /*
1964 * Disable preemption for these calls to prevent a RCU grace
1965 * period. This syncs the hash iteration and freeing of items
1966 * on the hash. rcu_read_lock is too dangerous here.
1967 */
1968 resched = ftrace_preempt_disable();
1969 hlist_for_each_entry_rcu(entry, n, hhd, node) {
1970 if (entry->ip == ip)
1971 entry->ops->func(ip, parent_ip, &entry->data);
1972 }
1973 ftrace_preempt_enable(resched);
1974}
1975
b6887d79 1976static struct ftrace_ops trace_probe_ops __read_mostly =
59df055f 1977{
fb9fb015 1978 .func = function_trace_probe_call,
59df055f
SR
1979};
1980
b6887d79 1981static int ftrace_probe_registered;
59df055f 1982
b6887d79 1983static void __enable_ftrace_function_probe(void)
59df055f
SR
1984{
1985 int i;
1986
b6887d79 1987 if (ftrace_probe_registered)
59df055f
SR
1988 return;
1989
1990 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1991 struct hlist_head *hhd = &ftrace_func_hash[i];
1992 if (hhd->first)
1993 break;
1994 }
1995 /* Nothing registered? */
1996 if (i == FTRACE_FUNC_HASHSIZE)
1997 return;
1998
b6887d79 1999 __register_ftrace_function(&trace_probe_ops);
59df055f 2000 ftrace_startup(0);
b6887d79 2001 ftrace_probe_registered = 1;
59df055f
SR
2002}
2003
b6887d79 2004static void __disable_ftrace_function_probe(void)
59df055f
SR
2005{
2006 int i;
2007
b6887d79 2008 if (!ftrace_probe_registered)
59df055f
SR
2009 return;
2010
2011 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2012 struct hlist_head *hhd = &ftrace_func_hash[i];
2013 if (hhd->first)
2014 return;
2015 }
2016
2017 /* no more funcs left */
b6887d79 2018 __unregister_ftrace_function(&trace_probe_ops);
59df055f 2019 ftrace_shutdown(0);
b6887d79 2020 ftrace_probe_registered = 0;
59df055f
SR
2021}
2022
2023
2024static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2025{
b6887d79
SR
2026 struct ftrace_func_probe *entry =
2027 container_of(rhp, struct ftrace_func_probe, rcu);
59df055f
SR
2028
2029 if (entry->ops->free)
2030 entry->ops->free(&entry->data);
2031 kfree(entry);
2032}
2033
2034
2035int
b6887d79 2036register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
59df055f
SR
2037 void *data)
2038{
b6887d79 2039 struct ftrace_func_probe *entry;
59df055f
SR
2040 struct ftrace_page *pg;
2041 struct dyn_ftrace *rec;
59df055f 2042 int type, len, not;
6a24a244 2043 unsigned long key;
59df055f
SR
2044 int count = 0;
2045 char *search;
2046
2047 type = ftrace_setup_glob(glob, strlen(glob), &search, &not);
2048 len = strlen(search);
2049
b6887d79 2050 /* we do not support '!' for function probes */
59df055f
SR
2051 if (WARN_ON(not))
2052 return -EINVAL;
2053
2054 mutex_lock(&ftrace_lock);
2055 do_for_each_ftrace_rec(pg, rec) {
2056
2057 if (rec->flags & FTRACE_FL_FAILED)
2058 continue;
2059
2060 if (!ftrace_match_record(rec, search, len, type))
2061 continue;
2062
2063 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2064 if (!entry) {
b6887d79 2065 /* If we did not process any, then return error */
59df055f
SR
2066 if (!count)
2067 count = -ENOMEM;
2068 goto out_unlock;
2069 }
2070
2071 count++;
2072
2073 entry->data = data;
2074
2075 /*
2076 * The caller might want to do something special
2077 * for each function we find. We call the callback
2078 * to give the caller an opportunity to do so.
2079 */
2080 if (ops->callback) {
2081 if (ops->callback(rec->ip, &entry->data) < 0) {
2082 /* caller does not like this func */
2083 kfree(entry);
2084 continue;
2085 }
2086 }
2087
2088 entry->ops = ops;
2089 entry->ip = rec->ip;
2090
2091 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2092 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2093
2094 } while_for_each_ftrace_rec();
b6887d79 2095 __enable_ftrace_function_probe();
59df055f
SR
2096
2097 out_unlock:
2098 mutex_unlock(&ftrace_lock);
2099
2100 return count;
2101}
2102
2103enum {
b6887d79
SR
2104 PROBE_TEST_FUNC = 1,
2105 PROBE_TEST_DATA = 2
59df055f
SR
2106};
2107
2108static void
b6887d79 2109__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
59df055f
SR
2110 void *data, int flags)
2111{
b6887d79 2112 struct ftrace_func_probe *entry;
59df055f
SR
2113 struct hlist_node *n, *tmp;
2114 char str[KSYM_SYMBOL_LEN];
2115 int type = MATCH_FULL;
2116 int i, len = 0;
2117 char *search;
2118
2119 if (glob && (strcmp(glob, "*") || !strlen(glob)))
2120 glob = NULL;
2121 else {
2122 int not;
2123
2124 type = ftrace_setup_glob(glob, strlen(glob), &search, &not);
2125 len = strlen(search);
2126
b6887d79 2127 /* we do not support '!' for function probes */
59df055f
SR
2128 if (WARN_ON(not))
2129 return;
2130 }
2131
2132 mutex_lock(&ftrace_lock);
2133 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2134 struct hlist_head *hhd = &ftrace_func_hash[i];
2135
2136 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2137
2138 /* break up if statements for readability */
b6887d79 2139 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
59df055f
SR
2140 continue;
2141
b6887d79 2142 if ((flags & PROBE_TEST_DATA) && entry->data != data)
59df055f
SR
2143 continue;
2144
2145 /* do this last, since it is the most expensive */
2146 if (glob) {
2147 kallsyms_lookup(entry->ip, NULL, NULL,
2148 NULL, str);
2149 if (!ftrace_match(str, glob, len, type))
2150 continue;
2151 }
2152
2153 hlist_del(&entry->node);
2154 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2155 }
2156 }
b6887d79 2157 __disable_ftrace_function_probe();
59df055f
SR
2158 mutex_unlock(&ftrace_lock);
2159}
2160
2161void
b6887d79 2162unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
59df055f
SR
2163 void *data)
2164{
b6887d79
SR
2165 __unregister_ftrace_function_probe(glob, ops, data,
2166 PROBE_TEST_FUNC | PROBE_TEST_DATA);
59df055f
SR
2167}
2168
2169void
b6887d79 2170unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
59df055f 2171{
b6887d79 2172 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
59df055f
SR
2173}
2174
b6887d79 2175void unregister_ftrace_function_probe_all(char *glob)
59df055f 2176{
b6887d79 2177 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
59df055f
SR
2178}
2179
f6180773
SR
2180static LIST_HEAD(ftrace_commands);
2181static DEFINE_MUTEX(ftrace_cmd_mutex);
2182
2183int register_ftrace_command(struct ftrace_func_command *cmd)
2184{
2185 struct ftrace_func_command *p;
2186 int ret = 0;
2187
2188 mutex_lock(&ftrace_cmd_mutex);
2189 list_for_each_entry(p, &ftrace_commands, list) {
2190 if (strcmp(cmd->name, p->name) == 0) {
2191 ret = -EBUSY;
2192 goto out_unlock;
2193 }
2194 }
2195 list_add(&cmd->list, &ftrace_commands);
2196 out_unlock:
2197 mutex_unlock(&ftrace_cmd_mutex);
2198
2199 return ret;
2200}
2201
2202int unregister_ftrace_command(struct ftrace_func_command *cmd)
2203{
2204 struct ftrace_func_command *p, *n;
2205 int ret = -ENODEV;
2206
2207 mutex_lock(&ftrace_cmd_mutex);
2208 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2209 if (strcmp(cmd->name, p->name) == 0) {
2210 ret = 0;
2211 list_del_init(&p->list);
2212 goto out_unlock;
2213 }
2214 }
2215 out_unlock:
2216 mutex_unlock(&ftrace_cmd_mutex);
2217
2218 return ret;
2219}
2220
64e7c440
SR
2221static int ftrace_process_regex(char *buff, int len, int enable)
2222{
f6180773 2223 char *func, *command, *next = buff;
6a24a244 2224 struct ftrace_func_command *p;
f6180773 2225 int ret = -EINVAL;
64e7c440
SR
2226
2227 func = strsep(&next, ":");
2228
2229 if (!next) {
2230 ftrace_match_records(func, len, enable);
2231 return 0;
2232 }
2233
f6180773 2234 /* command found */
64e7c440
SR
2235
2236 command = strsep(&next, ":");
2237
f6180773
SR
2238 mutex_lock(&ftrace_cmd_mutex);
2239 list_for_each_entry(p, &ftrace_commands, list) {
2240 if (strcmp(p->name, command) == 0) {
2241 ret = p->func(func, command, next, enable);
2242 goto out_unlock;
2243 }
64e7c440 2244 }
f6180773
SR
2245 out_unlock:
2246 mutex_unlock(&ftrace_cmd_mutex);
64e7c440 2247
f6180773 2248 return ret;
64e7c440
SR
2249}
2250
e309b41d 2251static ssize_t
41c52c0d
SR
2252ftrace_regex_write(struct file *file, const char __user *ubuf,
2253 size_t cnt, loff_t *ppos, int enable)
5072c59f
SR
2254{
2255 struct ftrace_iterator *iter;
2256 char ch;
2257 size_t read = 0;
2258 ssize_t ret;
2259
2260 if (!cnt || cnt < 0)
2261 return 0;
2262
41c52c0d 2263 mutex_lock(&ftrace_regex_lock);
5072c59f
SR
2264
2265 if (file->f_mode & FMODE_READ) {
2266 struct seq_file *m = file->private_data;
2267 iter = m->private;
2268 } else
2269 iter = file->private_data;
2270
2271 if (!*ppos) {
2272 iter->flags &= ~FTRACE_ITER_CONT;
2273 iter->buffer_idx = 0;
2274 }
2275
2276 ret = get_user(ch, ubuf++);
2277 if (ret)
2278 goto out;
2279 read++;
2280 cnt--;
2281
2282 if (!(iter->flags & ~FTRACE_ITER_CONT)) {
2283 /* skip white space */
2284 while (cnt && isspace(ch)) {
2285 ret = get_user(ch, ubuf++);
2286 if (ret)
2287 goto out;
2288 read++;
2289 cnt--;
2290 }
2291
5072c59f
SR
2292 if (isspace(ch)) {
2293 file->f_pos += read;
2294 ret = read;
2295 goto out;
2296 }
2297
2298 iter->buffer_idx = 0;
2299 }
2300
2301 while (cnt && !isspace(ch)) {
2302 if (iter->buffer_idx < FTRACE_BUFF_MAX)
2303 iter->buffer[iter->buffer_idx++] = ch;
2304 else {
2305 ret = -EINVAL;
2306 goto out;
2307 }
2308 ret = get_user(ch, ubuf++);
2309 if (ret)
2310 goto out;
2311 read++;
2312 cnt--;
2313 }
2314
2315 if (isspace(ch)) {
2316 iter->filtered++;
2317 iter->buffer[iter->buffer_idx] = 0;
64e7c440
SR
2318 ret = ftrace_process_regex(iter->buffer,
2319 iter->buffer_idx, enable);
2320 if (ret)
2321 goto out;
5072c59f
SR
2322 iter->buffer_idx = 0;
2323 } else
2324 iter->flags |= FTRACE_ITER_CONT;
2325
2326
2327 file->f_pos += read;
2328
2329 ret = read;
2330 out:
41c52c0d 2331 mutex_unlock(&ftrace_regex_lock);
5072c59f
SR
2332
2333 return ret;
2334}
2335
41c52c0d
SR
2336static ssize_t
2337ftrace_filter_write(struct file *file, const char __user *ubuf,
2338 size_t cnt, loff_t *ppos)
2339{
2340 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2341}
2342
2343static ssize_t
2344ftrace_notrace_write(struct file *file, const char __user *ubuf,
2345 size_t cnt, loff_t *ppos)
2346{
2347 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2348}
2349
2350static void
2351ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
2352{
2353 if (unlikely(ftrace_disabled))
2354 return;
2355
2356 mutex_lock(&ftrace_regex_lock);
2357 if (reset)
2358 ftrace_filter_reset(enable);
2359 if (buf)
7f24b31b 2360 ftrace_match_records(buf, len, enable);
41c52c0d
SR
2361 mutex_unlock(&ftrace_regex_lock);
2362}
2363
77a2b37d
SR
2364/**
2365 * ftrace_set_filter - set a function to filter on in ftrace
2366 * @buf - the string that holds the function filter text.
2367 * @len - the length of the string.
2368 * @reset - non zero to reset all filters before applying this filter.
2369 *
2370 * Filters denote which functions should be enabled when tracing is enabled.
2371 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2372 */
e309b41d 2373void ftrace_set_filter(unsigned char *buf, int len, int reset)
77a2b37d 2374{
41c52c0d
SR
2375 ftrace_set_regex(buf, len, reset, 1);
2376}
4eebcc81 2377
41c52c0d
SR
2378/**
2379 * ftrace_set_notrace - set a function to not trace in ftrace
2380 * @buf - the string that holds the function notrace text.
2381 * @len - the length of the string.
2382 * @reset - non zero to reset all filters before applying this filter.
2383 *
2384 * Notrace Filters denote which functions should not be enabled when tracing
2385 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2386 * for tracing.
2387 */
2388void ftrace_set_notrace(unsigned char *buf, int len, int reset)
2389{
2390 ftrace_set_regex(buf, len, reset, 0);
77a2b37d
SR
2391}
2392
2af15d6a
SR
2393/*
2394 * command line interface to allow users to set filters on boot up.
2395 */
2396#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2397static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
2398static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
2399
2400static int __init set_ftrace_notrace(char *str)
2401{
2402 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
2403 return 1;
2404}
2405__setup("ftrace_notrace=", set_ftrace_notrace);
2406
2407static int __init set_ftrace_filter(char *str)
2408{
2409 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
2410 return 1;
2411}
2412__setup("ftrace_filter=", set_ftrace_filter);
2413
2414static void __init set_ftrace_early_filter(char *buf, int enable)
2415{
2416 char *func;
2417
2418 while (buf) {
2419 func = strsep(&buf, ",");
2420 ftrace_set_regex(func, strlen(func), 0, enable);
2421 }
2422}
2423
2424static void __init set_ftrace_early_filters(void)
2425{
2426 if (ftrace_filter_buf[0])
2427 set_ftrace_early_filter(ftrace_filter_buf, 1);
2428 if (ftrace_notrace_buf[0])
2429 set_ftrace_early_filter(ftrace_notrace_buf, 0);
2430}
2431
e309b41d 2432static int
41c52c0d 2433ftrace_regex_release(struct inode *inode, struct file *file, int enable)
5072c59f
SR
2434{
2435 struct seq_file *m = (struct seq_file *)file->private_data;
2436 struct ftrace_iterator *iter;
2437
41c52c0d 2438 mutex_lock(&ftrace_regex_lock);
5072c59f
SR
2439 if (file->f_mode & FMODE_READ) {
2440 iter = m->private;
2441
2442 seq_release(inode, file);
2443 } else
2444 iter = file->private_data;
2445
2446 if (iter->buffer_idx) {
2447 iter->filtered++;
2448 iter->buffer[iter->buffer_idx] = 0;
7f24b31b 2449 ftrace_match_records(iter->buffer, iter->buffer_idx, enable);
5072c59f
SR
2450 }
2451
e6ea44e9 2452 mutex_lock(&ftrace_lock);
ee02a2e5 2453 if (ftrace_start_up && ftrace_enabled)
5072c59f 2454 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
e6ea44e9 2455 mutex_unlock(&ftrace_lock);
5072c59f
SR
2456
2457 kfree(iter);
41c52c0d 2458 mutex_unlock(&ftrace_regex_lock);
5072c59f
SR
2459 return 0;
2460}
2461
41c52c0d
SR
2462static int
2463ftrace_filter_release(struct inode *inode, struct file *file)
2464{
2465 return ftrace_regex_release(inode, file, 1);
2466}
2467
2468static int
2469ftrace_notrace_release(struct inode *inode, struct file *file)
2470{
2471 return ftrace_regex_release(inode, file, 0);
2472}
2473
5e2336a0 2474static const struct file_operations ftrace_avail_fops = {
5072c59f
SR
2475 .open = ftrace_avail_open,
2476 .read = seq_read,
2477 .llseek = seq_lseek,
2478 .release = ftrace_avail_release,
2479};
2480
5e2336a0 2481static const struct file_operations ftrace_failures_fops = {
eb9a7bf0
AS
2482 .open = ftrace_failures_open,
2483 .read = seq_read,
2484 .llseek = seq_lseek,
2485 .release = ftrace_avail_release,
2486};
2487
5e2336a0 2488static const struct file_operations ftrace_filter_fops = {
5072c59f 2489 .open = ftrace_filter_open,
850a80cf 2490 .read = seq_read,
5072c59f 2491 .write = ftrace_filter_write,
41c52c0d 2492 .llseek = ftrace_regex_lseek,
5072c59f
SR
2493 .release = ftrace_filter_release,
2494};
2495
5e2336a0 2496static const struct file_operations ftrace_notrace_fops = {
41c52c0d 2497 .open = ftrace_notrace_open,
850a80cf 2498 .read = seq_read,
41c52c0d
SR
2499 .write = ftrace_notrace_write,
2500 .llseek = ftrace_regex_lseek,
2501 .release = ftrace_notrace_release,
2502};
2503
ea4e2bc4
SR
2504#ifdef CONFIG_FUNCTION_GRAPH_TRACER
2505
2506static DEFINE_MUTEX(graph_lock);
2507
2508int ftrace_graph_count;
2509unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
2510
2511static void *
85951842 2512__g_next(struct seq_file *m, loff_t *pos)
ea4e2bc4
SR
2513{
2514 unsigned long *array = m->private;
ea4e2bc4 2515
85951842 2516 if (*pos >= ftrace_graph_count)
ea4e2bc4 2517 return NULL;
85951842
LZ
2518 return &array[*pos];
2519}
ea4e2bc4 2520
85951842
LZ
2521static void *
2522g_next(struct seq_file *m, void *v, loff_t *pos)
2523{
2524 (*pos)++;
2525 return __g_next(m, pos);
ea4e2bc4
SR
2526}
2527
2528static void *g_start(struct seq_file *m, loff_t *pos)
2529{
ea4e2bc4
SR
2530 mutex_lock(&graph_lock);
2531
f9349a8f
FW
2532 /* Nothing, tell g_show to print all functions are enabled */
2533 if (!ftrace_graph_count && !*pos)
2534 return (void *)1;
2535
85951842 2536 return __g_next(m, pos);
ea4e2bc4
SR
2537}
2538
2539static void g_stop(struct seq_file *m, void *p)
2540{
2541 mutex_unlock(&graph_lock);
2542}
2543
2544static int g_show(struct seq_file *m, void *v)
2545{
2546 unsigned long *ptr = v;
2547 char str[KSYM_SYMBOL_LEN];
2548
2549 if (!ptr)
2550 return 0;
2551
f9349a8f
FW
2552 if (ptr == (unsigned long *)1) {
2553 seq_printf(m, "#### all functions enabled ####\n");
2554 return 0;
2555 }
2556
ea4e2bc4
SR
2557 kallsyms_lookup(*ptr, NULL, NULL, NULL, str);
2558
2559 seq_printf(m, "%s\n", str);
2560
2561 return 0;
2562}
2563
2564static struct seq_operations ftrace_graph_seq_ops = {
2565 .start = g_start,
2566 .next = g_next,
2567 .stop = g_stop,
2568 .show = g_show,
2569};
2570
2571static int
2572ftrace_graph_open(struct inode *inode, struct file *file)
2573{
2574 int ret = 0;
2575
2576 if (unlikely(ftrace_disabled))
2577 return -ENODEV;
2578
2579 mutex_lock(&graph_lock);
2580 if ((file->f_mode & FMODE_WRITE) &&
2581 !(file->f_flags & O_APPEND)) {
2582 ftrace_graph_count = 0;
2583 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
2584 }
2585
2586 if (file->f_mode & FMODE_READ) {
2587 ret = seq_open(file, &ftrace_graph_seq_ops);
2588 if (!ret) {
2589 struct seq_file *m = file->private_data;
2590 m->private = ftrace_graph_funcs;
2591 }
2592 } else
2593 file->private_data = ftrace_graph_funcs;
2594 mutex_unlock(&graph_lock);
2595
2596 return ret;
2597}
2598
ea4e2bc4 2599static int
f9349a8f 2600ftrace_set_func(unsigned long *array, int *idx, char *buffer)
ea4e2bc4 2601{
ea4e2bc4
SR
2602 struct dyn_ftrace *rec;
2603 struct ftrace_page *pg;
f9349a8f 2604 int search_len;
ea4e2bc4 2605 int found = 0;
f9349a8f
FW
2606 int type, not;
2607 char *search;
2608 bool exists;
2609 int i;
ea4e2bc4
SR
2610
2611 if (ftrace_disabled)
2612 return -ENODEV;
2613
f9349a8f
FW
2614 /* decode regex */
2615 type = ftrace_setup_glob(buffer, strlen(buffer), &search, &not);
2616 if (not)
2617 return -EINVAL;
2618
2619 search_len = strlen(search);
2620
52baf119 2621 mutex_lock(&ftrace_lock);
265c831c
SR
2622 do_for_each_ftrace_rec(pg, rec) {
2623
f9349a8f
FW
2624 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
2625 break;
2626
265c831c
SR
2627 if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
2628 continue;
2629
f9349a8f
FW
2630 if (ftrace_match_record(rec, search, search_len, type)) {
2631 /* ensure it is not already in the array */
2632 exists = false;
2633 for (i = 0; i < *idx; i++)
2634 if (array[i] == rec->ip) {
2635 exists = true;
265c831c
SR
2636 break;
2637 }
f9349a8f
FW
2638 if (!exists) {
2639 array[(*idx)++] = rec->ip;
2640 found = 1;
2641 }
ea4e2bc4 2642 }
265c831c 2643 } while_for_each_ftrace_rec();
f9349a8f 2644
52baf119 2645 mutex_unlock(&ftrace_lock);
ea4e2bc4
SR
2646
2647 return found ? 0 : -EINVAL;
2648}
2649
2650static ssize_t
2651ftrace_graph_write(struct file *file, const char __user *ubuf,
2652 size_t cnt, loff_t *ppos)
2653{
2654 unsigned char buffer[FTRACE_BUFF_MAX+1];
2655 unsigned long *array;
2656 size_t read = 0;
2657 ssize_t ret;
2658 int index = 0;
2659 char ch;
2660
2661 if (!cnt || cnt < 0)
2662 return 0;
2663
2664 mutex_lock(&graph_lock);
2665
2666 if (ftrace_graph_count >= FTRACE_GRAPH_MAX_FUNCS) {
2667 ret = -EBUSY;
2668 goto out;
2669 }
2670
2671 if (file->f_mode & FMODE_READ) {
2672 struct seq_file *m = file->private_data;
2673 array = m->private;
2674 } else
2675 array = file->private_data;
2676
2677 ret = get_user(ch, ubuf++);
2678 if (ret)
2679 goto out;
2680 read++;
2681 cnt--;
2682
2683 /* skip white space */
2684 while (cnt && isspace(ch)) {
2685 ret = get_user(ch, ubuf++);
2686 if (ret)
2687 goto out;
2688 read++;
2689 cnt--;
2690 }
2691
2692 if (isspace(ch)) {
2693 *ppos += read;
2694 ret = read;
2695 goto out;
2696 }
2697
2698 while (cnt && !isspace(ch)) {
2699 if (index < FTRACE_BUFF_MAX)
2700 buffer[index++] = ch;
2701 else {
2702 ret = -EINVAL;
2703 goto out;
2704 }
2705 ret = get_user(ch, ubuf++);
2706 if (ret)
2707 goto out;
2708 read++;
2709 cnt--;
2710 }
2711 buffer[index] = 0;
2712
f9349a8f
FW
2713 /* we allow only one expression at a time */
2714 ret = ftrace_set_func(array, &ftrace_graph_count, buffer);
ea4e2bc4
SR
2715 if (ret)
2716 goto out;
2717
ea4e2bc4
SR
2718 file->f_pos += read;
2719
2720 ret = read;
2721 out:
2722 mutex_unlock(&graph_lock);
2723
2724 return ret;
2725}
2726
2727static const struct file_operations ftrace_graph_fops = {
2728 .open = ftrace_graph_open,
850a80cf 2729 .read = seq_read,
ea4e2bc4
SR
2730 .write = ftrace_graph_write,
2731};
2732#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2733
df4fc315 2734static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
5072c59f 2735{
5072c59f 2736
5452af66
FW
2737 trace_create_file("available_filter_functions", 0444,
2738 d_tracer, NULL, &ftrace_avail_fops);
5072c59f 2739
5452af66
FW
2740 trace_create_file("failures", 0444,
2741 d_tracer, NULL, &ftrace_failures_fops);
eb9a7bf0 2742
5452af66
FW
2743 trace_create_file("set_ftrace_filter", 0644, d_tracer,
2744 NULL, &ftrace_filter_fops);
41c52c0d 2745
5452af66 2746 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
41c52c0d 2747 NULL, &ftrace_notrace_fops);
ad90c0e3 2748
ea4e2bc4 2749#ifdef CONFIG_FUNCTION_GRAPH_TRACER
5452af66 2750 trace_create_file("set_graph_function", 0444, d_tracer,
ea4e2bc4
SR
2751 NULL,
2752 &ftrace_graph_fops);
ea4e2bc4
SR
2753#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2754
5072c59f
SR
2755 return 0;
2756}
2757
31e88909
SR
2758static int ftrace_convert_nops(struct module *mod,
2759 unsigned long *start,
68bf21aa
SR
2760 unsigned long *end)
2761{
2762 unsigned long *p;
2763 unsigned long addr;
2764 unsigned long flags;
2765
e6ea44e9 2766 mutex_lock(&ftrace_lock);
68bf21aa
SR
2767 p = start;
2768 while (p < end) {
2769 addr = ftrace_call_adjust(*p++);
20e5227e
SR
2770 /*
2771 * Some architecture linkers will pad between
2772 * the different mcount_loc sections of different
2773 * object files to satisfy alignments.
2774 * Skip any NULL pointers.
2775 */
2776 if (!addr)
2777 continue;
68bf21aa 2778 ftrace_record_ip(addr);
68bf21aa
SR
2779 }
2780
08f5ac90 2781 /* disable interrupts to prevent kstop machine */
68bf21aa 2782 local_irq_save(flags);
31e88909 2783 ftrace_update_code(mod);
68bf21aa 2784 local_irq_restore(flags);
e6ea44e9 2785 mutex_unlock(&ftrace_lock);
68bf21aa
SR
2786
2787 return 0;
2788}
2789
93eb677d
SR
2790#ifdef CONFIG_MODULES
2791void ftrace_release(void *start, void *end)
2792{
2793 struct dyn_ftrace *rec;
2794 struct ftrace_page *pg;
2795 unsigned long s = (unsigned long)start;
2796 unsigned long e = (unsigned long)end;
2797
2798 if (ftrace_disabled || !start || start == end)
2799 return;
2800
2801 mutex_lock(&ftrace_lock);
2802 do_for_each_ftrace_rec(pg, rec) {
2803 if ((rec->ip >= s) && (rec->ip < e)) {
2804 /*
2805 * rec->ip is changed in ftrace_free_rec()
2806 * It should not between s and e if record was freed.
2807 */
2808 FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
2809 ftrace_free_rec(rec);
2810 }
2811 } while_for_each_ftrace_rec();
2812 mutex_unlock(&ftrace_lock);
2813}
2814
2815static void ftrace_init_module(struct module *mod,
2816 unsigned long *start, unsigned long *end)
90d595fe 2817{
00fd61ae 2818 if (ftrace_disabled || start == end)
fed1939c 2819 return;
31e88909 2820 ftrace_convert_nops(mod, start, end);
90d595fe
SR
2821}
2822
93eb677d
SR
2823static int ftrace_module_notify(struct notifier_block *self,
2824 unsigned long val, void *data)
2825{
2826 struct module *mod = data;
2827
2828 switch (val) {
2829 case MODULE_STATE_COMING:
2830 ftrace_init_module(mod, mod->ftrace_callsites,
2831 mod->ftrace_callsites +
2832 mod->num_ftrace_callsites);
2833 break;
2834 case MODULE_STATE_GOING:
2835 ftrace_release(mod->ftrace_callsites,
2836 mod->ftrace_callsites +
2837 mod->num_ftrace_callsites);
2838 break;
2839 }
2840
2841 return 0;
2842}
2843#else
2844static int ftrace_module_notify(struct notifier_block *self,
2845 unsigned long val, void *data)
2846{
2847 return 0;
2848}
2849#endif /* CONFIG_MODULES */
2850
2851struct notifier_block ftrace_module_nb = {
2852 .notifier_call = ftrace_module_notify,
2853 .priority = 0,
2854};
2855
68bf21aa
SR
2856extern unsigned long __start_mcount_loc[];
2857extern unsigned long __stop_mcount_loc[];
2858
2859void __init ftrace_init(void)
2860{
2861 unsigned long count, addr, flags;
2862 int ret;
2863
2864 /* Keep the ftrace pointer to the stub */
2865 addr = (unsigned long)ftrace_stub;
2866
2867 local_irq_save(flags);
2868 ftrace_dyn_arch_init(&addr);
2869 local_irq_restore(flags);
2870
2871 /* ftrace_dyn_arch_init places the return code in addr */
2872 if (addr)
2873 goto failed;
2874
2875 count = __stop_mcount_loc - __start_mcount_loc;
2876
2877 ret = ftrace_dyn_table_alloc(count);
2878 if (ret)
2879 goto failed;
2880
2881 last_ftrace_enabled = ftrace_enabled = 1;
2882
31e88909
SR
2883 ret = ftrace_convert_nops(NULL,
2884 __start_mcount_loc,
68bf21aa
SR
2885 __stop_mcount_loc);
2886
93eb677d 2887 ret = register_module_notifier(&ftrace_module_nb);
24ed0c4b 2888 if (ret)
93eb677d
SR
2889 pr_warning("Failed to register trace ftrace module notifier\n");
2890
2af15d6a
SR
2891 set_ftrace_early_filters();
2892
68bf21aa
SR
2893 return;
2894 failed:
2895 ftrace_disabled = 1;
2896}
68bf21aa 2897
3d083395 2898#else
0b6e4d56
FW
2899
2900static int __init ftrace_nodyn_init(void)
2901{
2902 ftrace_enabled = 1;
2903 return 0;
2904}
2905device_initcall(ftrace_nodyn_init);
2906
df4fc315
SR
2907static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
2908static inline void ftrace_startup_enable(int command) { }
5a45cfe1
SR
2909/* Keep as macros so we do not need to define the commands */
2910# define ftrace_startup(command) do { } while (0)
2911# define ftrace_shutdown(command) do { } while (0)
c7aafc54
IM
2912# define ftrace_startup_sysctl() do { } while (0)
2913# define ftrace_shutdown_sysctl() do { } while (0)
3d083395
SR
2914#endif /* CONFIG_DYNAMIC_FTRACE */
2915
df4fc315
SR
2916static ssize_t
2917ftrace_pid_read(struct file *file, char __user *ubuf,
2918 size_t cnt, loff_t *ppos)
2919{
2920 char buf[64];
2921 int r;
2922
e32d8956
SR
2923 if (ftrace_pid_trace == ftrace_swapper_pid)
2924 r = sprintf(buf, "swapper tasks\n");
2925 else if (ftrace_pid_trace)
cc59c9e8 2926 r = sprintf(buf, "%u\n", pid_vnr(ftrace_pid_trace));
df4fc315
SR
2927 else
2928 r = sprintf(buf, "no pid\n");
2929
2930 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2931}
2932
e32d8956 2933static void clear_ftrace_swapper(void)
978f3a45
SR
2934{
2935 struct task_struct *p;
e32d8956 2936 int cpu;
978f3a45 2937
e32d8956
SR
2938 get_online_cpus();
2939 for_each_online_cpu(cpu) {
2940 p = idle_task(cpu);
978f3a45 2941 clear_tsk_trace_trace(p);
e32d8956
SR
2942 }
2943 put_online_cpus();
2944}
978f3a45 2945
e32d8956
SR
2946static void set_ftrace_swapper(void)
2947{
2948 struct task_struct *p;
2949 int cpu;
2950
2951 get_online_cpus();
2952 for_each_online_cpu(cpu) {
2953 p = idle_task(cpu);
2954 set_tsk_trace_trace(p);
2955 }
2956 put_online_cpus();
978f3a45
SR
2957}
2958
e32d8956
SR
2959static void clear_ftrace_pid(struct pid *pid)
2960{
2961 struct task_struct *p;
2962
229c4ef8 2963 rcu_read_lock();
e32d8956
SR
2964 do_each_pid_task(pid, PIDTYPE_PID, p) {
2965 clear_tsk_trace_trace(p);
2966 } while_each_pid_task(pid, PIDTYPE_PID, p);
229c4ef8
ON
2967 rcu_read_unlock();
2968
e32d8956
SR
2969 put_pid(pid);
2970}
2971
2972static void set_ftrace_pid(struct pid *pid)
978f3a45
SR
2973{
2974 struct task_struct *p;
2975
229c4ef8 2976 rcu_read_lock();
978f3a45
SR
2977 do_each_pid_task(pid, PIDTYPE_PID, p) {
2978 set_tsk_trace_trace(p);
2979 } while_each_pid_task(pid, PIDTYPE_PID, p);
229c4ef8 2980 rcu_read_unlock();
978f3a45
SR
2981}
2982
e32d8956
SR
2983static void clear_ftrace_pid_task(struct pid **pid)
2984{
2985 if (*pid == ftrace_swapper_pid)
2986 clear_ftrace_swapper();
2987 else
2988 clear_ftrace_pid(*pid);
2989
2990 *pid = NULL;
2991}
2992
2993static void set_ftrace_pid_task(struct pid *pid)
2994{
2995 if (pid == ftrace_swapper_pid)
2996 set_ftrace_swapper();
2997 else
2998 set_ftrace_pid(pid);
2999}
3000
df4fc315
SR
3001static ssize_t
3002ftrace_pid_write(struct file *filp, const char __user *ubuf,
3003 size_t cnt, loff_t *ppos)
3004{
978f3a45 3005 struct pid *pid;
df4fc315
SR
3006 char buf[64];
3007 long val;
3008 int ret;
3009
3010 if (cnt >= sizeof(buf))
3011 return -EINVAL;
3012
3013 if (copy_from_user(&buf, ubuf, cnt))
3014 return -EFAULT;
3015
3016 buf[cnt] = 0;
3017
3018 ret = strict_strtol(buf, 10, &val);
3019 if (ret < 0)
3020 return ret;
3021
e6ea44e9 3022 mutex_lock(&ftrace_lock);
978f3a45 3023 if (val < 0) {
df4fc315 3024 /* disable pid tracing */
978f3a45 3025 if (!ftrace_pid_trace)
df4fc315 3026 goto out;
978f3a45
SR
3027
3028 clear_ftrace_pid_task(&ftrace_pid_trace);
df4fc315
SR
3029
3030 } else {
e32d8956
SR
3031 /* swapper task is special */
3032 if (!val) {
3033 pid = ftrace_swapper_pid;
3034 if (pid == ftrace_pid_trace)
3035 goto out;
3036 } else {
3037 pid = find_get_pid(val);
df4fc315 3038
e32d8956
SR
3039 if (pid == ftrace_pid_trace) {
3040 put_pid(pid);
3041 goto out;
3042 }
0ef8cde5 3043 }
0ef8cde5 3044
978f3a45
SR
3045 if (ftrace_pid_trace)
3046 clear_ftrace_pid_task(&ftrace_pid_trace);
3047
3048 if (!pid)
3049 goto out;
3050
3051 ftrace_pid_trace = pid;
3052
3053 set_ftrace_pid_task(ftrace_pid_trace);
df4fc315
SR
3054 }
3055
3056 /* update the function call */
3057 ftrace_update_pid_func();
3058 ftrace_startup_enable(0);
3059
3060 out:
e6ea44e9 3061 mutex_unlock(&ftrace_lock);
df4fc315
SR
3062
3063 return cnt;
3064}
3065
5e2336a0 3066static const struct file_operations ftrace_pid_fops = {
df4fc315
SR
3067 .read = ftrace_pid_read,
3068 .write = ftrace_pid_write,
3069};
3070
3071static __init int ftrace_init_debugfs(void)
3072{
3073 struct dentry *d_tracer;
df4fc315
SR
3074
3075 d_tracer = tracing_init_dentry();
3076 if (!d_tracer)
3077 return 0;
3078
3079 ftrace_init_dyn_debugfs(d_tracer);
3080
5452af66
FW
3081 trace_create_file("set_ftrace_pid", 0644, d_tracer,
3082 NULL, &ftrace_pid_fops);
493762fc
SR
3083
3084 ftrace_profile_debugfs(d_tracer);
3085
df4fc315
SR
3086 return 0;
3087}
df4fc315
SR
3088fs_initcall(ftrace_init_debugfs);
3089
a2bb6a3d 3090/**
81adbdc0 3091 * ftrace_kill - kill ftrace
a2bb6a3d
SR
3092 *
3093 * This function should be used by panic code. It stops ftrace
3094 * but in a not so nice way. If you need to simply kill ftrace
3095 * from a non-atomic section, use ftrace_kill.
3096 */
81adbdc0 3097void ftrace_kill(void)
a2bb6a3d
SR
3098{
3099 ftrace_disabled = 1;
3100 ftrace_enabled = 0;
a2bb6a3d
SR
3101 clear_ftrace_function();
3102}
3103
16444a8a 3104/**
3d083395
SR
3105 * register_ftrace_function - register a function for profiling
3106 * @ops - ops structure that holds the function for profiling.
16444a8a 3107 *
3d083395
SR
3108 * Register a function to be called by all functions in the
3109 * kernel.
3110 *
3111 * Note: @ops->func and all the functions it calls must be labeled
3112 * with "notrace", otherwise it will go into a
3113 * recursive loop.
16444a8a 3114 */
3d083395 3115int register_ftrace_function(struct ftrace_ops *ops)
16444a8a 3116{
b0fc494f
SR
3117 int ret;
3118
4eebcc81
SR
3119 if (unlikely(ftrace_disabled))
3120 return -1;
3121
e6ea44e9 3122 mutex_lock(&ftrace_lock);
e7d3737e 3123
b0fc494f 3124 ret = __register_ftrace_function(ops);
5a45cfe1 3125 ftrace_startup(0);
b0fc494f 3126
e6ea44e9 3127 mutex_unlock(&ftrace_lock);
b0fc494f 3128 return ret;
3d083395
SR
3129}
3130
3131/**
32632920 3132 * unregister_ftrace_function - unregister a function for profiling.
3d083395
SR
3133 * @ops - ops structure that holds the function to unregister
3134 *
3135 * Unregister a function that was added to be called by ftrace profiling.
3136 */
3137int unregister_ftrace_function(struct ftrace_ops *ops)
3138{
3139 int ret;
3140
e6ea44e9 3141 mutex_lock(&ftrace_lock);
3d083395 3142 ret = __unregister_ftrace_function(ops);
5a45cfe1 3143 ftrace_shutdown(0);
e6ea44e9 3144 mutex_unlock(&ftrace_lock);
b0fc494f
SR
3145
3146 return ret;
3147}
3148
e309b41d 3149int
b0fc494f 3150ftrace_enable_sysctl(struct ctl_table *table, int write,
5072c59f 3151 struct file *file, void __user *buffer, size_t *lenp,
b0fc494f
SR
3152 loff_t *ppos)
3153{
3154 int ret;
3155
4eebcc81
SR
3156 if (unlikely(ftrace_disabled))
3157 return -ENODEV;
3158
e6ea44e9 3159 mutex_lock(&ftrace_lock);
b0fc494f 3160
5072c59f 3161 ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
b0fc494f 3162
a32c7765 3163 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
b0fc494f
SR
3164 goto out;
3165
a32c7765 3166 last_ftrace_enabled = !!ftrace_enabled;
b0fc494f
SR
3167
3168 if (ftrace_enabled) {
3169
3170 ftrace_startup_sysctl();
3171
3172 /* we are starting ftrace again */
3173 if (ftrace_list != &ftrace_list_end) {
3174 if (ftrace_list->next == &ftrace_list_end)
3175 ftrace_trace_function = ftrace_list->func;
3176 else
3177 ftrace_trace_function = ftrace_list_func;
3178 }
3179
3180 } else {
3181 /* stopping ftrace calls (just send to ftrace_stub) */
3182 ftrace_trace_function = ftrace_stub;
3183
3184 ftrace_shutdown_sysctl();
3185 }
3186
3187 out:
e6ea44e9 3188 mutex_unlock(&ftrace_lock);
3d083395 3189 return ret;
16444a8a 3190}
f17845e5 3191
fb52607a 3192#ifdef CONFIG_FUNCTION_GRAPH_TRACER
e7d3737e 3193
597af815 3194static int ftrace_graph_active;
4a2b8dda 3195static struct notifier_block ftrace_suspend_notifier;
e7d3737e 3196
e49dc19c
SR
3197int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3198{
3199 return 0;
3200}
3201
287b6e68
FW
3202/* The callbacks that hook a function */
3203trace_func_graph_ret_t ftrace_graph_return =
3204 (trace_func_graph_ret_t)ftrace_stub;
e49dc19c 3205trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
f201ae23
FW
3206
3207/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3208static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3209{
3210 int i;
3211 int ret = 0;
3212 unsigned long flags;
3213 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3214 struct task_struct *g, *t;
3215
3216 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3217 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3218 * sizeof(struct ftrace_ret_stack),
3219 GFP_KERNEL);
3220 if (!ret_stack_list[i]) {
3221 start = 0;
3222 end = i;
3223 ret = -ENOMEM;
3224 goto free;
3225 }
3226 }
3227
3228 read_lock_irqsave(&tasklist_lock, flags);
3229 do_each_thread(g, t) {
3230 if (start == end) {
3231 ret = -EAGAIN;
3232 goto unlock;
3233 }
3234
3235 if (t->ret_stack == NULL) {
380c4b14 3236 atomic_set(&t->tracing_graph_pause, 0);
f201ae23 3237 atomic_set(&t->trace_overrun, 0);
26c01624
SR
3238 t->curr_ret_stack = -1;
3239 /* Make sure the tasks see the -1 first: */
3240 smp_wmb();
3241 t->ret_stack = ret_stack_list[start++];
f201ae23
FW
3242 }
3243 } while_each_thread(g, t);
3244
3245unlock:
3246 read_unlock_irqrestore(&tasklist_lock, flags);
3247free:
3248 for (i = start; i < end; i++)
3249 kfree(ret_stack_list[i]);
3250 return ret;
3251}
3252
8aef2d28
SR
3253static void
3254ftrace_graph_probe_sched_switch(struct rq *__rq, struct task_struct *prev,
3255 struct task_struct *next)
3256{
3257 unsigned long long timestamp;
3258 int index;
3259
be6f164a
SR
3260 /*
3261 * Does the user want to count the time a function was asleep.
3262 * If so, do not update the time stamps.
3263 */
3264 if (trace_flags & TRACE_ITER_SLEEP_TIME)
3265 return;
3266
8aef2d28
SR
3267 timestamp = trace_clock_local();
3268
3269 prev->ftrace_timestamp = timestamp;
3270
3271 /* only process tasks that we timestamped */
3272 if (!next->ftrace_timestamp)
3273 return;
3274
3275 /*
3276 * Update all the counters in next to make up for the
3277 * time next was sleeping.
3278 */
3279 timestamp -= next->ftrace_timestamp;
3280
3281 for (index = next->curr_ret_stack; index >= 0; index--)
3282 next->ret_stack[index].calltime += timestamp;
3283}
3284
f201ae23 3285/* Allocate a return stack for each task */
fb52607a 3286static int start_graph_tracing(void)
f201ae23
FW
3287{
3288 struct ftrace_ret_stack **ret_stack_list;
5b058bcd 3289 int ret, cpu;
f201ae23
FW
3290
3291 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
3292 sizeof(struct ftrace_ret_stack *),
3293 GFP_KERNEL);
3294
3295 if (!ret_stack_list)
3296 return -ENOMEM;
3297
5b058bcd 3298 /* The cpu_boot init_task->ret_stack will never be freed */
179c498a
SR
3299 for_each_online_cpu(cpu) {
3300 if (!idle_task(cpu)->ret_stack)
3301 ftrace_graph_init_task(idle_task(cpu));
3302 }
5b058bcd 3303
f201ae23
FW
3304 do {
3305 ret = alloc_retstack_tasklist(ret_stack_list);
3306 } while (ret == -EAGAIN);
3307
8aef2d28
SR
3308 if (!ret) {
3309 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch);
3310 if (ret)
3311 pr_info("ftrace_graph: Couldn't activate tracepoint"
3312 " probe to kernel_sched_switch\n");
3313 }
3314
f201ae23
FW
3315 kfree(ret_stack_list);
3316 return ret;
3317}
3318
4a2b8dda
FW
3319/*
3320 * Hibernation protection.
3321 * The state of the current task is too much unstable during
3322 * suspend/restore to disk. We want to protect against that.
3323 */
3324static int
3325ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
3326 void *unused)
3327{
3328 switch (state) {
3329 case PM_HIBERNATION_PREPARE:
3330 pause_graph_tracing();
3331 break;
3332
3333 case PM_POST_HIBERNATION:
3334 unpause_graph_tracing();
3335 break;
3336 }
3337 return NOTIFY_DONE;
3338}
3339
287b6e68
FW
3340int register_ftrace_graph(trace_func_graph_ret_t retfunc,
3341 trace_func_graph_ent_t entryfunc)
15e6cb36 3342{
e7d3737e
FW
3343 int ret = 0;
3344
e6ea44e9 3345 mutex_lock(&ftrace_lock);
e7d3737e 3346
05ce5818 3347 /* we currently allow only one tracer registered at a time */
597af815 3348 if (ftrace_graph_active) {
05ce5818
SR
3349 ret = -EBUSY;
3350 goto out;
3351 }
3352
4a2b8dda
FW
3353 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
3354 register_pm_notifier(&ftrace_suspend_notifier);
3355
597af815 3356 ftrace_graph_active++;
fb52607a 3357 ret = start_graph_tracing();
f201ae23 3358 if (ret) {
597af815 3359 ftrace_graph_active--;
f201ae23
FW
3360 goto out;
3361 }
e53a6319 3362
287b6e68
FW
3363 ftrace_graph_return = retfunc;
3364 ftrace_graph_entry = entryfunc;
e53a6319 3365
5a45cfe1 3366 ftrace_startup(FTRACE_START_FUNC_RET);
e7d3737e
FW
3367
3368out:
e6ea44e9 3369 mutex_unlock(&ftrace_lock);
e7d3737e 3370 return ret;
15e6cb36
FW
3371}
3372
fb52607a 3373void unregister_ftrace_graph(void)
15e6cb36 3374{
e6ea44e9 3375 mutex_lock(&ftrace_lock);
e7d3737e 3376
597af815 3377 if (unlikely(!ftrace_graph_active))
2aad1b76
SR
3378 goto out;
3379
597af815 3380 ftrace_graph_active--;
8aef2d28 3381 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch);
287b6e68 3382 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
e49dc19c 3383 ftrace_graph_entry = ftrace_graph_entry_stub;
5a45cfe1 3384 ftrace_shutdown(FTRACE_STOP_FUNC_RET);
4a2b8dda 3385 unregister_pm_notifier(&ftrace_suspend_notifier);
e7d3737e 3386
2aad1b76 3387 out:
e6ea44e9 3388 mutex_unlock(&ftrace_lock);
15e6cb36 3389}
f201ae23
FW
3390
3391/* Allocate a return stack for newly created task */
fb52607a 3392void ftrace_graph_init_task(struct task_struct *t)
f201ae23 3393{
84047e36
SR
3394 /* Make sure we do not use the parent ret_stack */
3395 t->ret_stack = NULL;
3396
597af815 3397 if (ftrace_graph_active) {
82310a32
SR
3398 struct ftrace_ret_stack *ret_stack;
3399
3400 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
f201ae23
FW
3401 * sizeof(struct ftrace_ret_stack),
3402 GFP_KERNEL);
82310a32 3403 if (!ret_stack)
f201ae23
FW
3404 return;
3405 t->curr_ret_stack = -1;
380c4b14 3406 atomic_set(&t->tracing_graph_pause, 0);
f201ae23 3407 atomic_set(&t->trace_overrun, 0);
8aef2d28 3408 t->ftrace_timestamp = 0;
82310a32
SR
3409 /* make curr_ret_stack visable before we add the ret_stack */
3410 smp_wmb();
3411 t->ret_stack = ret_stack;
84047e36 3412 }
f201ae23
FW
3413}
3414
fb52607a 3415void ftrace_graph_exit_task(struct task_struct *t)
f201ae23 3416{
eae849ca
FW
3417 struct ftrace_ret_stack *ret_stack = t->ret_stack;
3418
f201ae23 3419 t->ret_stack = NULL;
eae849ca
FW
3420 /* NULL must become visible to IRQs before we free it: */
3421 barrier();
3422
3423 kfree(ret_stack);
f201ae23 3424}
14a866c5
SR
3425
3426void ftrace_graph_stop(void)
3427{
3428 ftrace_stop();
3429}
15e6cb36
FW
3430#endif
3431