ftrace: Unify effect of writing to trace_options and option/*
[deliverable/linux.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
6ab5d668 771static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
bac429f0 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 */
cafb168a
SR
789 WARN(1,
790 "Could not allocate stat file for cpu %d\n",
791 cpu);
792 return;
793 }
794 stat->stat = function_stats;
795 snprintf(name, 32, "function%d", cpu);
796 stat->stat.name = name;
797 ret = register_stat_tracer(&stat->stat);
798 if (ret) {
799 WARN(1,
800 "Could not register function stat for cpu %d\n",
801 cpu);
802 kfree(name);
803 return;
804 }
bac429f0
SR
805 }
806
807 entry = debugfs_create_file("function_profile_enabled", 0644,
808 d_tracer, NULL, &ftrace_profile_fops);
809 if (!entry)
810 pr_warning("Could not create debugfs "
811 "'function_profile_enabled' entry\n");
812}
813
bac429f0 814#else /* CONFIG_FUNCTION_PROFILER */
6ab5d668 815static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
bac429f0
SR
816{
817}
bac429f0
SR
818#endif /* CONFIG_FUNCTION_PROFILER */
819
493762fc
SR
820/* set when tracing only a pid */
821struct pid *ftrace_pid_trace;
822static struct pid * const ftrace_swapper_pid = &init_struct_pid;
823
824#ifdef CONFIG_DYNAMIC_FTRACE
825
826#ifndef CONFIG_FTRACE_MCOUNT_RECORD
827# error Dynamic ftrace depends on MCOUNT_RECORD
828#endif
829
830static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
831
832struct ftrace_func_probe {
833 struct hlist_node node;
834 struct ftrace_probe_ops *ops;
835 unsigned long flags;
836 unsigned long ip;
837 void *data;
838 struct rcu_head rcu;
839};
840
841enum {
842 FTRACE_ENABLE_CALLS = (1 << 0),
843 FTRACE_DISABLE_CALLS = (1 << 1),
844 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
845 FTRACE_ENABLE_MCOUNT = (1 << 3),
846 FTRACE_DISABLE_MCOUNT = (1 << 4),
847 FTRACE_START_FUNC_RET = (1 << 5),
848 FTRACE_STOP_FUNC_RET = (1 << 6),
849};
850
851static int ftrace_filtered;
852
853static struct dyn_ftrace *ftrace_new_addrs;
854
855static DEFINE_MUTEX(ftrace_regex_lock);
856
857struct ftrace_page {
858 struct ftrace_page *next;
859 int index;
860 struct dyn_ftrace records[];
861};
862
863#define ENTRIES_PER_PAGE \
864 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
865
866/* estimate from running different kernels */
867#define NR_TO_INIT 10000
868
869static struct ftrace_page *ftrace_pages_start;
870static struct ftrace_page *ftrace_pages;
871
872static struct dyn_ftrace *ftrace_free_records;
873
874/*
875 * This is a double for. Do not use 'break' to break out of the loop,
876 * you must use a goto.
877 */
878#define do_for_each_ftrace_rec(pg, rec) \
879 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
880 int _____i; \
881 for (_____i = 0; _____i < pg->index; _____i++) { \
882 rec = &pg->records[_____i];
883
884#define while_for_each_ftrace_rec() \
885 } \
886 }
887
ecea656d 888#ifdef CONFIG_KPROBES
f17845e5
IM
889
890static int frozen_record_count;
891
ecea656d
AS
892static inline void freeze_record(struct dyn_ftrace *rec)
893{
894 if (!(rec->flags & FTRACE_FL_FROZEN)) {
895 rec->flags |= FTRACE_FL_FROZEN;
896 frozen_record_count++;
897 }
898}
899
900static inline void unfreeze_record(struct dyn_ftrace *rec)
901{
902 if (rec->flags & FTRACE_FL_FROZEN) {
903 rec->flags &= ~FTRACE_FL_FROZEN;
904 frozen_record_count--;
905 }
906}
907
908static inline int record_frozen(struct dyn_ftrace *rec)
909{
910 return rec->flags & FTRACE_FL_FROZEN;
911}
912#else
913# define freeze_record(rec) ({ 0; })
914# define unfreeze_record(rec) ({ 0; })
915# define record_frozen(rec) ({ 0; })
916#endif /* CONFIG_KPROBES */
917
e309b41d 918static void ftrace_free_rec(struct dyn_ftrace *rec)
37ad5084 919{
ee000b7f 920 rec->freelist = ftrace_free_records;
37ad5084
SR
921 ftrace_free_records = rec;
922 rec->flags |= FTRACE_FL_FREE;
923}
924
e309b41d 925static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
3c1720f0 926{
37ad5084
SR
927 struct dyn_ftrace *rec;
928
929 /* First check for freed records */
930 if (ftrace_free_records) {
931 rec = ftrace_free_records;
932
37ad5084 933 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
6912896e 934 FTRACE_WARN_ON_ONCE(1);
37ad5084
SR
935 ftrace_free_records = NULL;
936 return NULL;
937 }
938
ee000b7f 939 ftrace_free_records = rec->freelist;
37ad5084
SR
940 memset(rec, 0, sizeof(*rec));
941 return rec;
942 }
943
3c1720f0 944 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
08f5ac90
SR
945 if (!ftrace_pages->next) {
946 /* allocate another page */
947 ftrace_pages->next =
948 (void *)get_zeroed_page(GFP_KERNEL);
949 if (!ftrace_pages->next)
950 return NULL;
951 }
3c1720f0
SR
952 ftrace_pages = ftrace_pages->next;
953 }
954
955 return &ftrace_pages->records[ftrace_pages->index++];
956}
957
08f5ac90 958static struct dyn_ftrace *
d61f82d0 959ftrace_record_ip(unsigned long ip)
3d083395 960{
08f5ac90 961 struct dyn_ftrace *rec;
3d083395 962
f3c7ac40 963 if (ftrace_disabled)
08f5ac90 964 return NULL;
3d083395 965
08f5ac90
SR
966 rec = ftrace_alloc_dyn_node(ip);
967 if (!rec)
968 return NULL;
3d083395 969
08f5ac90 970 rec->ip = ip;
ee000b7f 971 rec->newlist = ftrace_new_addrs;
e94142a6 972 ftrace_new_addrs = rec;
3d083395 973
08f5ac90 974 return rec;
3d083395
SR
975}
976
b17e8a37
SR
977static void print_ip_ins(const char *fmt, unsigned char *p)
978{
979 int i;
980
981 printk(KERN_CONT "%s", fmt);
982
983 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
984 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
985}
986
31e88909 987static void ftrace_bug(int failed, unsigned long ip)
b17e8a37
SR
988{
989 switch (failed) {
990 case -EFAULT:
991 FTRACE_WARN_ON_ONCE(1);
992 pr_info("ftrace faulted on modifying ");
993 print_ip_sym(ip);
994 break;
995 case -EINVAL:
996 FTRACE_WARN_ON_ONCE(1);
997 pr_info("ftrace failed to modify ");
998 print_ip_sym(ip);
b17e8a37 999 print_ip_ins(" actual: ", (unsigned char *)ip);
b17e8a37
SR
1000 printk(KERN_CONT "\n");
1001 break;
1002 case -EPERM:
1003 FTRACE_WARN_ON_ONCE(1);
1004 pr_info("ftrace faulted on writing ");
1005 print_ip_sym(ip);
1006 break;
1007 default:
1008 FTRACE_WARN_ON_ONCE(1);
1009 pr_info("ftrace faulted on unknown error ");
1010 print_ip_sym(ip);
1011 }
1012}
1013
3c1720f0 1014
0eb96701 1015static int
31e88909 1016__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
5072c59f 1017{
e7d3737e 1018 unsigned long ftrace_addr;
6a24a244 1019 unsigned long ip, fl;
e7d3737e 1020
f0001207 1021 ftrace_addr = (unsigned long)FTRACE_ADDR;
5072c59f
SR
1022
1023 ip = rec->ip;
1024
982c350b
SR
1025 /*
1026 * If this record is not to be traced and
1027 * it is not enabled then do nothing.
1028 *
1029 * If this record is not to be traced and
57794a9d 1030 * it is enabled then disable it.
982c350b
SR
1031 *
1032 */
1033 if (rec->flags & FTRACE_FL_NOTRACE) {
1034 if (rec->flags & FTRACE_FL_ENABLED)
1035 rec->flags &= ~FTRACE_FL_ENABLED;
1036 else
1037 return 0;
1038
1039 } else if (ftrace_filtered && enable) {
5072c59f 1040 /*
982c350b 1041 * Filtering is on:
5072c59f 1042 */
a4500b84 1043
982c350b 1044 fl = rec->flags & (FTRACE_FL_FILTER | FTRACE_FL_ENABLED);
5072c59f 1045
982c350b
SR
1046 /* Record is filtered and enabled, do nothing */
1047 if (fl == (FTRACE_FL_FILTER | FTRACE_FL_ENABLED))
0eb96701 1048 return 0;
5072c59f 1049
57794a9d 1050 /* Record is not filtered or enabled, do nothing */
982c350b
SR
1051 if (!fl)
1052 return 0;
1053
1054 /* Record is not filtered but enabled, disable it */
1055 if (fl == FTRACE_FL_ENABLED)
5072c59f 1056 rec->flags &= ~FTRACE_FL_ENABLED;
982c350b
SR
1057 else
1058 /* Otherwise record is filtered but not enabled, enable it */
5072c59f 1059 rec->flags |= FTRACE_FL_ENABLED;
5072c59f 1060 } else {
982c350b 1061 /* Disable or not filtered */
5072c59f 1062
41c52c0d 1063 if (enable) {
982c350b 1064 /* if record is enabled, do nothing */
5072c59f 1065 if (rec->flags & FTRACE_FL_ENABLED)
0eb96701 1066 return 0;
982c350b 1067
5072c59f 1068 rec->flags |= FTRACE_FL_ENABLED;
982c350b 1069
5072c59f 1070 } else {
982c350b 1071
57794a9d 1072 /* if record is not enabled, do nothing */
5072c59f 1073 if (!(rec->flags & FTRACE_FL_ENABLED))
0eb96701 1074 return 0;
982c350b 1075
5072c59f
SR
1076 rec->flags &= ~FTRACE_FL_ENABLED;
1077 }
1078 }
1079
982c350b 1080 if (rec->flags & FTRACE_FL_ENABLED)
e7d3737e 1081 return ftrace_make_call(rec, ftrace_addr);
31e88909 1082 else
e7d3737e 1083 return ftrace_make_nop(NULL, rec, ftrace_addr);
5072c59f
SR
1084}
1085
e309b41d 1086static void ftrace_replace_code(int enable)
3c1720f0 1087{
3c1720f0
SR
1088 struct dyn_ftrace *rec;
1089 struct ftrace_page *pg;
6a24a244 1090 int failed;
3c1720f0 1091
265c831c
SR
1092 do_for_each_ftrace_rec(pg, rec) {
1093 /*
fa9d13cf
Z
1094 * Skip over free records, records that have
1095 * failed and not converted.
265c831c
SR
1096 */
1097 if (rec->flags & FTRACE_FL_FREE ||
fa9d13cf 1098 rec->flags & FTRACE_FL_FAILED ||
03303549 1099 !(rec->flags & FTRACE_FL_CONVERTED))
265c831c
SR
1100 continue;
1101
1102 /* ignore updates to this record's mcount site */
1103 if (get_kprobe((void *)rec->ip)) {
1104 freeze_record(rec);
1105 continue;
1106 } else {
1107 unfreeze_record(rec);
1108 }
f22f9a89 1109
265c831c 1110 failed = __ftrace_replace_code(rec, enable);
fa9d13cf 1111 if (failed) {
265c831c
SR
1112 rec->flags |= FTRACE_FL_FAILED;
1113 if ((system_state == SYSTEM_BOOTING) ||
1114 !core_kernel_text(rec->ip)) {
1115 ftrace_free_rec(rec);
4377245a 1116 } else {
265c831c 1117 ftrace_bug(failed, rec->ip);
4377245a
SR
1118 /* Stop processing */
1119 return;
1120 }
3c1720f0 1121 }
265c831c 1122 } while_for_each_ftrace_rec();
3c1720f0
SR
1123}
1124
492a7ea5 1125static int
31e88909 1126ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
3c1720f0
SR
1127{
1128 unsigned long ip;
593eb8a2 1129 int ret;
3c1720f0
SR
1130
1131 ip = rec->ip;
1132
25aac9dc 1133 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
593eb8a2 1134 if (ret) {
31e88909 1135 ftrace_bug(ret, ip);
3c1720f0 1136 rec->flags |= FTRACE_FL_FAILED;
492a7ea5 1137 return 0;
37ad5084 1138 }
492a7ea5 1139 return 1;
3c1720f0
SR
1140}
1141
000ab691
SR
1142/*
1143 * archs can override this function if they must do something
1144 * before the modifying code is performed.
1145 */
1146int __weak ftrace_arch_code_modify_prepare(void)
1147{
1148 return 0;
1149}
1150
1151/*
1152 * archs can override this function if they must do something
1153 * after the modifying code is performed.
1154 */
1155int __weak ftrace_arch_code_modify_post_process(void)
1156{
1157 return 0;
1158}
1159
e309b41d 1160static int __ftrace_modify_code(void *data)
3d083395 1161{
d61f82d0
SR
1162 int *command = data;
1163
a3583244 1164 if (*command & FTRACE_ENABLE_CALLS)
d61f82d0 1165 ftrace_replace_code(1);
a3583244 1166 else if (*command & FTRACE_DISABLE_CALLS)
d61f82d0
SR
1167 ftrace_replace_code(0);
1168
1169 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1170 ftrace_update_ftrace_func(ftrace_trace_function);
1171
5a45cfe1
SR
1172 if (*command & FTRACE_START_FUNC_RET)
1173 ftrace_enable_ftrace_graph_caller();
1174 else if (*command & FTRACE_STOP_FUNC_RET)
1175 ftrace_disable_ftrace_graph_caller();
1176
d61f82d0 1177 return 0;
3d083395
SR
1178}
1179
e309b41d 1180static void ftrace_run_update_code(int command)
3d083395 1181{
000ab691
SR
1182 int ret;
1183
1184 ret = ftrace_arch_code_modify_prepare();
1185 FTRACE_WARN_ON(ret);
1186 if (ret)
1187 return;
1188
784e2d76 1189 stop_machine(__ftrace_modify_code, &command, NULL);
000ab691
SR
1190
1191 ret = ftrace_arch_code_modify_post_process();
1192 FTRACE_WARN_ON(ret);
3d083395
SR
1193}
1194
d61f82d0 1195static ftrace_func_t saved_ftrace_func;
60a7ecf4 1196static int ftrace_start_up;
df4fc315
SR
1197
1198static void ftrace_startup_enable(int command)
1199{
1200 if (saved_ftrace_func != ftrace_trace_function) {
1201 saved_ftrace_func = ftrace_trace_function;
1202 command |= FTRACE_UPDATE_TRACE_FUNC;
1203 }
1204
1205 if (!command || !ftrace_enabled)
1206 return;
1207
1208 ftrace_run_update_code(command);
1209}
d61f82d0 1210
5a45cfe1 1211static void ftrace_startup(int command)
3d083395 1212{
4eebcc81
SR
1213 if (unlikely(ftrace_disabled))
1214 return;
1215
60a7ecf4 1216 ftrace_start_up++;
982c350b 1217 command |= FTRACE_ENABLE_CALLS;
d61f82d0 1218
df4fc315 1219 ftrace_startup_enable(command);
3d083395
SR
1220}
1221
5a45cfe1 1222static void ftrace_shutdown(int command)
3d083395 1223{
4eebcc81
SR
1224 if (unlikely(ftrace_disabled))
1225 return;
1226
60a7ecf4 1227 ftrace_start_up--;
9ea1a153
FW
1228 /*
1229 * Just warn in case of unbalance, no need to kill ftrace, it's not
1230 * critical but the ftrace_call callers may be never nopped again after
1231 * further ftrace uses.
1232 */
1233 WARN_ON_ONCE(ftrace_start_up < 0);
1234
60a7ecf4 1235 if (!ftrace_start_up)
d61f82d0 1236 command |= FTRACE_DISABLE_CALLS;
3d083395 1237
d61f82d0
SR
1238 if (saved_ftrace_func != ftrace_trace_function) {
1239 saved_ftrace_func = ftrace_trace_function;
1240 command |= FTRACE_UPDATE_TRACE_FUNC;
1241 }
3d083395 1242
d61f82d0 1243 if (!command || !ftrace_enabled)
e6ea44e9 1244 return;
d61f82d0
SR
1245
1246 ftrace_run_update_code(command);
3d083395
SR
1247}
1248
e309b41d 1249static void ftrace_startup_sysctl(void)
b0fc494f 1250{
d61f82d0
SR
1251 int command = FTRACE_ENABLE_MCOUNT;
1252
4eebcc81
SR
1253 if (unlikely(ftrace_disabled))
1254 return;
1255
d61f82d0
SR
1256 /* Force update next time */
1257 saved_ftrace_func = NULL;
60a7ecf4
SR
1258 /* ftrace_start_up is true if we want ftrace running */
1259 if (ftrace_start_up)
d61f82d0
SR
1260 command |= FTRACE_ENABLE_CALLS;
1261
1262 ftrace_run_update_code(command);
b0fc494f
SR
1263}
1264
e309b41d 1265static void ftrace_shutdown_sysctl(void)
b0fc494f 1266{
d61f82d0
SR
1267 int command = FTRACE_DISABLE_MCOUNT;
1268
4eebcc81
SR
1269 if (unlikely(ftrace_disabled))
1270 return;
1271
60a7ecf4
SR
1272 /* ftrace_start_up is true if ftrace is running */
1273 if (ftrace_start_up)
d61f82d0
SR
1274 command |= FTRACE_DISABLE_CALLS;
1275
1276 ftrace_run_update_code(command);
b0fc494f
SR
1277}
1278
3d083395
SR
1279static cycle_t ftrace_update_time;
1280static unsigned long ftrace_update_cnt;
1281unsigned long ftrace_update_tot_cnt;
1282
31e88909 1283static int ftrace_update_code(struct module *mod)
3d083395 1284{
e94142a6 1285 struct dyn_ftrace *p;
f22f9a89 1286 cycle_t start, stop;
3d083395 1287
750ed1a4 1288 start = ftrace_now(raw_smp_processor_id());
3d083395
SR
1289 ftrace_update_cnt = 0;
1290
e94142a6 1291 while (ftrace_new_addrs) {
3d083395 1292
08f5ac90
SR
1293 /* If something went wrong, bail without enabling anything */
1294 if (unlikely(ftrace_disabled))
1295 return -1;
f22f9a89 1296
e94142a6 1297 p = ftrace_new_addrs;
ee000b7f 1298 ftrace_new_addrs = p->newlist;
e94142a6 1299 p->flags = 0L;
f22f9a89 1300
08f5ac90 1301 /* convert record (i.e, patch mcount-call with NOP) */
31e88909 1302 if (ftrace_code_disable(mod, p)) {
08f5ac90
SR
1303 p->flags |= FTRACE_FL_CONVERTED;
1304 ftrace_update_cnt++;
1305 } else
1306 ftrace_free_rec(p);
3d083395
SR
1307 }
1308
750ed1a4 1309 stop = ftrace_now(raw_smp_processor_id());
3d083395
SR
1310 ftrace_update_time = stop - start;
1311 ftrace_update_tot_cnt += ftrace_update_cnt;
1312
16444a8a
ACM
1313 return 0;
1314}
1315
68bf21aa 1316static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
3c1720f0
SR
1317{
1318 struct ftrace_page *pg;
1319 int cnt;
1320 int i;
3c1720f0
SR
1321
1322 /* allocate a few pages */
1323 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1324 if (!ftrace_pages_start)
1325 return -1;
1326
1327 /*
1328 * Allocate a few more pages.
1329 *
1330 * TODO: have some parser search vmlinux before
1331 * final linking to find all calls to ftrace.
1332 * Then we can:
1333 * a) know how many pages to allocate.
1334 * and/or
1335 * b) set up the table then.
1336 *
1337 * The dynamic code is still necessary for
1338 * modules.
1339 */
1340
1341 pg = ftrace_pages = ftrace_pages_start;
1342
68bf21aa 1343 cnt = num_to_init / ENTRIES_PER_PAGE;
08f5ac90 1344 pr_info("ftrace: allocating %ld entries in %d pages\n",
5821e1b7 1345 num_to_init, cnt + 1);
3c1720f0
SR
1346
1347 for (i = 0; i < cnt; i++) {
1348 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1349
1350 /* If we fail, we'll try later anyway */
1351 if (!pg->next)
1352 break;
1353
1354 pg = pg->next;
1355 }
1356
1357 return 0;
1358}
1359
5072c59f
SR
1360enum {
1361 FTRACE_ITER_FILTER = (1 << 0),
1362 FTRACE_ITER_CONT = (1 << 1),
41c52c0d 1363 FTRACE_ITER_NOTRACE = (1 << 2),
eb9a7bf0 1364 FTRACE_ITER_FAILURES = (1 << 3),
0c75a3ed 1365 FTRACE_ITER_PRINTALL = (1 << 4),
8fc0c701 1366 FTRACE_ITER_HASH = (1 << 5),
5072c59f
SR
1367};
1368
1369#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1370
1371struct ftrace_iterator {
5072c59f 1372 struct ftrace_page *pg;
8fc0c701 1373 int hidx;
431aa3fb 1374 int idx;
5072c59f
SR
1375 unsigned flags;
1376 unsigned char buffer[FTRACE_BUFF_MAX+1];
1377 unsigned buffer_idx;
1378 unsigned filtered;
1379};
1380
8fc0c701
SR
1381static void *
1382t_hash_next(struct seq_file *m, void *v, loff_t *pos)
1383{
1384 struct ftrace_iterator *iter = m->private;
1385 struct hlist_node *hnd = v;
1386 struct hlist_head *hhd;
1387
1388 WARN_ON(!(iter->flags & FTRACE_ITER_HASH));
1389
1390 (*pos)++;
1391
1392 retry:
1393 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1394 return NULL;
1395
1396 hhd = &ftrace_func_hash[iter->hidx];
1397
1398 if (hlist_empty(hhd)) {
1399 iter->hidx++;
1400 hnd = NULL;
1401 goto retry;
1402 }
1403
1404 if (!hnd)
1405 hnd = hhd->first;
1406 else {
1407 hnd = hnd->next;
1408 if (!hnd) {
1409 iter->hidx++;
1410 goto retry;
1411 }
1412 }
1413
1414 return hnd;
1415}
1416
1417static void *t_hash_start(struct seq_file *m, loff_t *pos)
1418{
1419 struct ftrace_iterator *iter = m->private;
1420 void *p = NULL;
d82d6244
LZ
1421 loff_t l;
1422
1423 if (!(iter->flags & FTRACE_ITER_HASH))
1424 *pos = 0;
8fc0c701
SR
1425
1426 iter->flags |= FTRACE_ITER_HASH;
1427
d82d6244
LZ
1428 iter->hidx = 0;
1429 for (l = 0; l <= *pos; ) {
1430 p = t_hash_next(m, p, &l);
1431 if (!p)
1432 break;
1433 }
1434 return p;
8fc0c701
SR
1435}
1436
1437static int t_hash_show(struct seq_file *m, void *v)
1438{
b6887d79 1439 struct ftrace_func_probe *rec;
8fc0c701
SR
1440 struct hlist_node *hnd = v;
1441 char str[KSYM_SYMBOL_LEN];
1442
b6887d79 1443 rec = hlist_entry(hnd, struct ftrace_func_probe, node);
8fc0c701 1444
809dcf29
SR
1445 if (rec->ops->print)
1446 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1447
8fc0c701
SR
1448 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1449 seq_printf(m, "%s:", str);
1450
1451 kallsyms_lookup((unsigned long)rec->ops->func, NULL, NULL, NULL, str);
1452 seq_printf(m, "%s", str);
1453
1454 if (rec->data)
1455 seq_printf(m, ":%p", rec->data);
1456 seq_putc(m, '\n');
1457
1458 return 0;
1459}
1460
e309b41d 1461static void *
5072c59f
SR
1462t_next(struct seq_file *m, void *v, loff_t *pos)
1463{
1464 struct ftrace_iterator *iter = m->private;
1465 struct dyn_ftrace *rec = NULL;
1466
8fc0c701
SR
1467 if (iter->flags & FTRACE_ITER_HASH)
1468 return t_hash_next(m, v, pos);
1469
5072c59f
SR
1470 (*pos)++;
1471
0c75a3ed
SR
1472 if (iter->flags & FTRACE_ITER_PRINTALL)
1473 return NULL;
1474
5072c59f
SR
1475 retry:
1476 if (iter->idx >= iter->pg->index) {
1477 if (iter->pg->next) {
1478 iter->pg = iter->pg->next;
1479 iter->idx = 0;
1480 goto retry;
1481 }
1482 } else {
1483 rec = &iter->pg->records[iter->idx++];
a9fdda33
SR
1484 if ((rec->flags & FTRACE_FL_FREE) ||
1485
1486 (!(iter->flags & FTRACE_ITER_FAILURES) &&
eb9a7bf0
AS
1487 (rec->flags & FTRACE_FL_FAILED)) ||
1488
1489 ((iter->flags & FTRACE_ITER_FAILURES) &&
a9fdda33 1490 !(rec->flags & FTRACE_FL_FAILED)) ||
eb9a7bf0 1491
0183fb1c
SR
1492 ((iter->flags & FTRACE_ITER_FILTER) &&
1493 !(rec->flags & FTRACE_FL_FILTER)) ||
1494
41c52c0d
SR
1495 ((iter->flags & FTRACE_ITER_NOTRACE) &&
1496 !(rec->flags & FTRACE_FL_NOTRACE))) {
5072c59f
SR
1497 rec = NULL;
1498 goto retry;
1499 }
1500 }
1501
5072c59f
SR
1502 return rec;
1503}
1504
1505static void *t_start(struct seq_file *m, loff_t *pos)
1506{
1507 struct ftrace_iterator *iter = m->private;
1508 void *p = NULL;
694ce0a5 1509 loff_t l;
5072c59f 1510
8fc0c701 1511 mutex_lock(&ftrace_lock);
0c75a3ed
SR
1512 /*
1513 * For set_ftrace_filter reading, if we have the filter
1514 * off, we can short cut and just print out that all
1515 * functions are enabled.
1516 */
1517 if (iter->flags & FTRACE_ITER_FILTER && !ftrace_filtered) {
1518 if (*pos > 0)
8fc0c701 1519 return t_hash_start(m, pos);
0c75a3ed 1520 iter->flags |= FTRACE_ITER_PRINTALL;
0c75a3ed
SR
1521 return iter;
1522 }
1523
8fc0c701
SR
1524 if (iter->flags & FTRACE_ITER_HASH)
1525 return t_hash_start(m, pos);
1526
694ce0a5
LZ
1527 iter->pg = ftrace_pages_start;
1528 iter->idx = 0;
1529 for (l = 0; l <= *pos; ) {
1530 p = t_next(m, p, &l);
1531 if (!p)
1532 break;
50cdaf08 1533 }
5821e1b7 1534
694ce0a5 1535 if (!p && iter->flags & FTRACE_ITER_FILTER)
8fc0c701
SR
1536 return t_hash_start(m, pos);
1537
5072c59f
SR
1538 return p;
1539}
1540
1541static void t_stop(struct seq_file *m, void *p)
1542{
8fc0c701 1543 mutex_unlock(&ftrace_lock);
5072c59f
SR
1544}
1545
1546static int t_show(struct seq_file *m, void *v)
1547{
0c75a3ed 1548 struct ftrace_iterator *iter = m->private;
5072c59f
SR
1549 struct dyn_ftrace *rec = v;
1550 char str[KSYM_SYMBOL_LEN];
1551
8fc0c701
SR
1552 if (iter->flags & FTRACE_ITER_HASH)
1553 return t_hash_show(m, v);
1554
0c75a3ed
SR
1555 if (iter->flags & FTRACE_ITER_PRINTALL) {
1556 seq_printf(m, "#### all functions enabled ####\n");
1557 return 0;
1558 }
1559
5072c59f
SR
1560 if (!rec)
1561 return 0;
1562
1563 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1564
50cdaf08 1565 seq_printf(m, "%s\n", str);
5072c59f
SR
1566
1567 return 0;
1568}
1569
1570static struct seq_operations show_ftrace_seq_ops = {
1571 .start = t_start,
1572 .next = t_next,
1573 .stop = t_stop,
1574 .show = t_show,
1575};
1576
e309b41d 1577static int
5072c59f
SR
1578ftrace_avail_open(struct inode *inode, struct file *file)
1579{
1580 struct ftrace_iterator *iter;
1581 int ret;
1582
4eebcc81
SR
1583 if (unlikely(ftrace_disabled))
1584 return -ENODEV;
1585
5072c59f
SR
1586 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1587 if (!iter)
1588 return -ENOMEM;
1589
1590 iter->pg = ftrace_pages_start;
5072c59f
SR
1591
1592 ret = seq_open(file, &show_ftrace_seq_ops);
1593 if (!ret) {
1594 struct seq_file *m = file->private_data;
4bf39a94 1595
5072c59f 1596 m->private = iter;
4bf39a94 1597 } else {
5072c59f 1598 kfree(iter);
4bf39a94 1599 }
5072c59f
SR
1600
1601 return ret;
1602}
1603
1604int ftrace_avail_release(struct inode *inode, struct file *file)
1605{
1606 struct seq_file *m = (struct seq_file *)file->private_data;
1607 struct ftrace_iterator *iter = m->private;
1608
1609 seq_release(inode, file);
1610 kfree(iter);
4bf39a94 1611
5072c59f
SR
1612 return 0;
1613}
1614
eb9a7bf0
AS
1615static int
1616ftrace_failures_open(struct inode *inode, struct file *file)
1617{
1618 int ret;
1619 struct seq_file *m;
1620 struct ftrace_iterator *iter;
1621
1622 ret = ftrace_avail_open(inode, file);
1623 if (!ret) {
1624 m = (struct seq_file *)file->private_data;
1625 iter = (struct ftrace_iterator *)m->private;
1626 iter->flags = FTRACE_ITER_FAILURES;
1627 }
1628
1629 return ret;
1630}
1631
1632
41c52c0d 1633static void ftrace_filter_reset(int enable)
5072c59f
SR
1634{
1635 struct ftrace_page *pg;
1636 struct dyn_ftrace *rec;
41c52c0d 1637 unsigned long type = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
5072c59f 1638
52baf119 1639 mutex_lock(&ftrace_lock);
41c52c0d
SR
1640 if (enable)
1641 ftrace_filtered = 0;
265c831c
SR
1642 do_for_each_ftrace_rec(pg, rec) {
1643 if (rec->flags & FTRACE_FL_FAILED)
1644 continue;
1645 rec->flags &= ~type;
1646 } while_for_each_ftrace_rec();
52baf119 1647 mutex_unlock(&ftrace_lock);
5072c59f
SR
1648}
1649
e309b41d 1650static int
41c52c0d 1651ftrace_regex_open(struct inode *inode, struct file *file, int enable)
5072c59f
SR
1652{
1653 struct ftrace_iterator *iter;
1654 int ret = 0;
1655
4eebcc81
SR
1656 if (unlikely(ftrace_disabled))
1657 return -ENODEV;
1658
5072c59f
SR
1659 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
1660 if (!iter)
1661 return -ENOMEM;
1662
41c52c0d 1663 mutex_lock(&ftrace_regex_lock);
5072c59f 1664 if ((file->f_mode & FMODE_WRITE) &&
8650ae32 1665 (file->f_flags & O_TRUNC))
41c52c0d 1666 ftrace_filter_reset(enable);
5072c59f
SR
1667
1668 if (file->f_mode & FMODE_READ) {
1669 iter->pg = ftrace_pages_start;
41c52c0d
SR
1670 iter->flags = enable ? FTRACE_ITER_FILTER :
1671 FTRACE_ITER_NOTRACE;
5072c59f
SR
1672
1673 ret = seq_open(file, &show_ftrace_seq_ops);
1674 if (!ret) {
1675 struct seq_file *m = file->private_data;
1676 m->private = iter;
1677 } else
1678 kfree(iter);
1679 } else
1680 file->private_data = iter;
41c52c0d 1681 mutex_unlock(&ftrace_regex_lock);
5072c59f
SR
1682
1683 return ret;
1684}
1685
41c52c0d
SR
1686static int
1687ftrace_filter_open(struct inode *inode, struct file *file)
1688{
1689 return ftrace_regex_open(inode, file, 1);
1690}
1691
1692static int
1693ftrace_notrace_open(struct inode *inode, struct file *file)
1694{
1695 return ftrace_regex_open(inode, file, 0);
1696}
1697
e309b41d 1698static loff_t
41c52c0d 1699ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
5072c59f
SR
1700{
1701 loff_t ret;
1702
1703 if (file->f_mode & FMODE_READ)
1704 ret = seq_lseek(file, offset, origin);
1705 else
1706 file->f_pos = ret = 1;
1707
1708 return ret;
1709}
1710
1711enum {
1712 MATCH_FULL,
1713 MATCH_FRONT_ONLY,
1714 MATCH_MIDDLE_ONLY,
1715 MATCH_END_ONLY,
1716};
1717
9f4801e3
SR
1718/*
1719 * (static function - no need for kernel doc)
1720 *
1721 * Pass in a buffer containing a glob and this function will
1722 * set search to point to the search part of the buffer and
1723 * return the type of search it is (see enum above).
1724 * This does modify buff.
1725 *
1726 * Returns enum type.
1727 * search returns the pointer to use for comparison.
1728 * not returns 1 if buff started with a '!'
1729 * 0 otherwise.
1730 */
1731static int
64e7c440 1732ftrace_setup_glob(char *buff, int len, char **search, int *not)
5072c59f 1733{
5072c59f 1734 int type = MATCH_FULL;
9f4801e3 1735 int i;
ea3a6d6d
SR
1736
1737 if (buff[0] == '!') {
9f4801e3 1738 *not = 1;
ea3a6d6d
SR
1739 buff++;
1740 len--;
9f4801e3
SR
1741 } else
1742 *not = 0;
1743
1744 *search = buff;
5072c59f
SR
1745
1746 for (i = 0; i < len; i++) {
1747 if (buff[i] == '*') {
1748 if (!i) {
9f4801e3 1749 *search = buff + 1;
5072c59f 1750 type = MATCH_END_ONLY;
5072c59f 1751 } else {
9f4801e3 1752 if (type == MATCH_END_ONLY)
5072c59f 1753 type = MATCH_MIDDLE_ONLY;
9f4801e3 1754 else
5072c59f 1755 type = MATCH_FRONT_ONLY;
5072c59f
SR
1756 buff[i] = 0;
1757 break;
1758 }
1759 }
1760 }
1761
9f4801e3
SR
1762 return type;
1763}
1764
64e7c440 1765static int ftrace_match(char *str, char *regex, int len, int type)
9f4801e3 1766{
9f4801e3
SR
1767 int matched = 0;
1768 char *ptr;
1769
9f4801e3
SR
1770 switch (type) {
1771 case MATCH_FULL:
1772 if (strcmp(str, regex) == 0)
1773 matched = 1;
1774 break;
1775 case MATCH_FRONT_ONLY:
1776 if (strncmp(str, regex, len) == 0)
1777 matched = 1;
1778 break;
1779 case MATCH_MIDDLE_ONLY:
1780 if (strstr(str, regex))
1781 matched = 1;
1782 break;
1783 case MATCH_END_ONLY:
1784 ptr = strstr(str, regex);
1785 if (ptr && (ptr[len] == 0))
1786 matched = 1;
1787 break;
1788 }
1789
1790 return matched;
1791}
1792
64e7c440
SR
1793static int
1794ftrace_match_record(struct dyn_ftrace *rec, char *regex, int len, int type)
1795{
1796 char str[KSYM_SYMBOL_LEN];
1797
1798 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
1799 return ftrace_match(str, regex, len, type);
1800}
1801
9f4801e3
SR
1802static void ftrace_match_records(char *buff, int len, int enable)
1803{
6a24a244 1804 unsigned int search_len;
9f4801e3
SR
1805 struct ftrace_page *pg;
1806 struct dyn_ftrace *rec;
6a24a244
SR
1807 unsigned long flag;
1808 char *search;
9f4801e3 1809 int type;
9f4801e3
SR
1810 int not;
1811
6a24a244 1812 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
9f4801e3
SR
1813 type = ftrace_setup_glob(buff, len, &search, &not);
1814
1815 search_len = strlen(search);
1816
52baf119 1817 mutex_lock(&ftrace_lock);
265c831c 1818 do_for_each_ftrace_rec(pg, rec) {
265c831c
SR
1819
1820 if (rec->flags & FTRACE_FL_FAILED)
1821 continue;
9f4801e3
SR
1822
1823 if (ftrace_match_record(rec, search, search_len, type)) {
265c831c
SR
1824 if (not)
1825 rec->flags &= ~flag;
1826 else
1827 rec->flags |= flag;
1828 }
e68746a2
SR
1829 /*
1830 * Only enable filtering if we have a function that
1831 * is filtered on.
1832 */
1833 if (enable && (rec->flags & FTRACE_FL_FILTER))
1834 ftrace_filtered = 1;
265c831c 1835 } while_for_each_ftrace_rec();
52baf119 1836 mutex_unlock(&ftrace_lock);
5072c59f
SR
1837}
1838
64e7c440
SR
1839static int
1840ftrace_match_module_record(struct dyn_ftrace *rec, char *mod,
1841 char *regex, int len, int type)
1842{
1843 char str[KSYM_SYMBOL_LEN];
1844 char *modname;
1845
1846 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
1847
1848 if (!modname || strcmp(modname, mod))
1849 return 0;
1850
1851 /* blank search means to match all funcs in the mod */
1852 if (len)
1853 return ftrace_match(str, regex, len, type);
1854 else
1855 return 1;
1856}
1857
1858static void ftrace_match_module_records(char *buff, char *mod, int enable)
1859{
6a24a244 1860 unsigned search_len = 0;
64e7c440
SR
1861 struct ftrace_page *pg;
1862 struct dyn_ftrace *rec;
1863 int type = MATCH_FULL;
6a24a244
SR
1864 char *search = buff;
1865 unsigned long flag;
64e7c440
SR
1866 int not = 0;
1867
6a24a244
SR
1868 flag = enable ? FTRACE_FL_FILTER : FTRACE_FL_NOTRACE;
1869
64e7c440
SR
1870 /* blank or '*' mean the same */
1871 if (strcmp(buff, "*") == 0)
1872 buff[0] = 0;
1873
1874 /* handle the case of 'dont filter this module' */
1875 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
1876 buff[0] = 0;
1877 not = 1;
1878 }
1879
1880 if (strlen(buff)) {
1881 type = ftrace_setup_glob(buff, strlen(buff), &search, &not);
1882 search_len = strlen(search);
1883 }
1884
52baf119 1885 mutex_lock(&ftrace_lock);
64e7c440
SR
1886 do_for_each_ftrace_rec(pg, rec) {
1887
1888 if (rec->flags & FTRACE_FL_FAILED)
1889 continue;
1890
1891 if (ftrace_match_module_record(rec, mod,
1892 search, search_len, type)) {
1893 if (not)
1894 rec->flags &= ~flag;
1895 else
1896 rec->flags |= flag;
1897 }
e68746a2
SR
1898 if (enable && (rec->flags & FTRACE_FL_FILTER))
1899 ftrace_filtered = 1;
64e7c440
SR
1900
1901 } while_for_each_ftrace_rec();
52baf119 1902 mutex_unlock(&ftrace_lock);
64e7c440
SR
1903}
1904
f6180773
SR
1905/*
1906 * We register the module command as a template to show others how
1907 * to register the a command as well.
1908 */
1909
1910static int
1911ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
1912{
1913 char *mod;
1914
1915 /*
1916 * cmd == 'mod' because we only registered this func
1917 * for the 'mod' ftrace_func_command.
1918 * But if you register one func with multiple commands,
1919 * you can tell which command was used by the cmd
1920 * parameter.
1921 */
1922
1923 /* we must have a module name */
1924 if (!param)
1925 return -EINVAL;
1926
1927 mod = strsep(&param, ":");
1928 if (!strlen(mod))
1929 return -EINVAL;
1930
1931 ftrace_match_module_records(func, mod, enable);
1932 return 0;
1933}
1934
1935static struct ftrace_func_command ftrace_mod_cmd = {
1936 .name = "mod",
1937 .func = ftrace_mod_callback,
1938};
1939
1940static int __init ftrace_mod_cmd_init(void)
1941{
1942 return register_ftrace_command(&ftrace_mod_cmd);
1943}
1944device_initcall(ftrace_mod_cmd_init);
1945
59df055f 1946static void
b6887d79 1947function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
59df055f 1948{
b6887d79 1949 struct ftrace_func_probe *entry;
59df055f
SR
1950 struct hlist_head *hhd;
1951 struct hlist_node *n;
1952 unsigned long key;
1953 int resched;
1954
1955 key = hash_long(ip, FTRACE_HASH_BITS);
1956
1957 hhd = &ftrace_func_hash[key];
1958
1959 if (hlist_empty(hhd))
1960 return;
1961
1962 /*
1963 * Disable preemption for these calls to prevent a RCU grace
1964 * period. This syncs the hash iteration and freeing of items
1965 * on the hash. rcu_read_lock is too dangerous here.
1966 */
1967 resched = ftrace_preempt_disable();
1968 hlist_for_each_entry_rcu(entry, n, hhd, node) {
1969 if (entry->ip == ip)
1970 entry->ops->func(ip, parent_ip, &entry->data);
1971 }
1972 ftrace_preempt_enable(resched);
1973}
1974
b6887d79 1975static struct ftrace_ops trace_probe_ops __read_mostly =
59df055f 1976{
fb9fb015 1977 .func = function_trace_probe_call,
59df055f
SR
1978};
1979
b6887d79 1980static int ftrace_probe_registered;
59df055f 1981
b6887d79 1982static void __enable_ftrace_function_probe(void)
59df055f
SR
1983{
1984 int i;
1985
b6887d79 1986 if (ftrace_probe_registered)
59df055f
SR
1987 return;
1988
1989 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
1990 struct hlist_head *hhd = &ftrace_func_hash[i];
1991 if (hhd->first)
1992 break;
1993 }
1994 /* Nothing registered? */
1995 if (i == FTRACE_FUNC_HASHSIZE)
1996 return;
1997
b6887d79 1998 __register_ftrace_function(&trace_probe_ops);
59df055f 1999 ftrace_startup(0);
b6887d79 2000 ftrace_probe_registered = 1;
59df055f
SR
2001}
2002
b6887d79 2003static void __disable_ftrace_function_probe(void)
59df055f
SR
2004{
2005 int i;
2006
b6887d79 2007 if (!ftrace_probe_registered)
59df055f
SR
2008 return;
2009
2010 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2011 struct hlist_head *hhd = &ftrace_func_hash[i];
2012 if (hhd->first)
2013 return;
2014 }
2015
2016 /* no more funcs left */
b6887d79 2017 __unregister_ftrace_function(&trace_probe_ops);
59df055f 2018 ftrace_shutdown(0);
b6887d79 2019 ftrace_probe_registered = 0;
59df055f
SR
2020}
2021
2022
2023static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2024{
b6887d79
SR
2025 struct ftrace_func_probe *entry =
2026 container_of(rhp, struct ftrace_func_probe, rcu);
59df055f
SR
2027
2028 if (entry->ops->free)
2029 entry->ops->free(&entry->data);
2030 kfree(entry);
2031}
2032
2033
2034int
b6887d79 2035register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
59df055f
SR
2036 void *data)
2037{
b6887d79 2038 struct ftrace_func_probe *entry;
59df055f
SR
2039 struct ftrace_page *pg;
2040 struct dyn_ftrace *rec;
59df055f 2041 int type, len, not;
6a24a244 2042 unsigned long key;
59df055f
SR
2043 int count = 0;
2044 char *search;
2045
2046 type = ftrace_setup_glob(glob, strlen(glob), &search, &not);
2047 len = strlen(search);
2048
b6887d79 2049 /* we do not support '!' for function probes */
59df055f
SR
2050 if (WARN_ON(not))
2051 return -EINVAL;
2052
2053 mutex_lock(&ftrace_lock);
2054 do_for_each_ftrace_rec(pg, rec) {
2055
2056 if (rec->flags & FTRACE_FL_FAILED)
2057 continue;
2058
2059 if (!ftrace_match_record(rec, search, len, type))
2060 continue;
2061
2062 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2063 if (!entry) {
b6887d79 2064 /* If we did not process any, then return error */
59df055f
SR
2065 if (!count)
2066 count = -ENOMEM;
2067 goto out_unlock;
2068 }
2069
2070 count++;
2071
2072 entry->data = data;
2073
2074 /*
2075 * The caller might want to do something special
2076 * for each function we find. We call the callback
2077 * to give the caller an opportunity to do so.
2078 */
2079 if (ops->callback) {
2080 if (ops->callback(rec->ip, &entry->data) < 0) {
2081 /* caller does not like this func */
2082 kfree(entry);
2083 continue;
2084 }
2085 }
2086
2087 entry->ops = ops;
2088 entry->ip = rec->ip;
2089
2090 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2091 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2092
2093 } while_for_each_ftrace_rec();
b6887d79 2094 __enable_ftrace_function_probe();
59df055f
SR
2095
2096 out_unlock:
2097 mutex_unlock(&ftrace_lock);
2098
2099 return count;
2100}
2101
2102enum {
b6887d79
SR
2103 PROBE_TEST_FUNC = 1,
2104 PROBE_TEST_DATA = 2
59df055f
SR
2105};
2106
2107static void
b6887d79 2108__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
59df055f
SR
2109 void *data, int flags)
2110{
b6887d79 2111 struct ftrace_func_probe *entry;
59df055f
SR
2112 struct hlist_node *n, *tmp;
2113 char str[KSYM_SYMBOL_LEN];
2114 int type = MATCH_FULL;
2115 int i, len = 0;
2116 char *search;
2117
2118 if (glob && (strcmp(glob, "*") || !strlen(glob)))
2119 glob = NULL;
2120 else {
2121 int not;
2122
2123 type = ftrace_setup_glob(glob, strlen(glob), &search, &not);
2124 len = strlen(search);
2125
b6887d79 2126 /* we do not support '!' for function probes */
59df055f
SR
2127 if (WARN_ON(not))
2128 return;
2129 }
2130
2131 mutex_lock(&ftrace_lock);
2132 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2133 struct hlist_head *hhd = &ftrace_func_hash[i];
2134
2135 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2136
2137 /* break up if statements for readability */
b6887d79 2138 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
59df055f
SR
2139 continue;
2140
b6887d79 2141 if ((flags & PROBE_TEST_DATA) && entry->data != data)
59df055f
SR
2142 continue;
2143
2144 /* do this last, since it is the most expensive */
2145 if (glob) {
2146 kallsyms_lookup(entry->ip, NULL, NULL,
2147 NULL, str);
2148 if (!ftrace_match(str, glob, len, type))
2149 continue;
2150 }
2151
2152 hlist_del(&entry->node);
2153 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2154 }
2155 }
b6887d79 2156 __disable_ftrace_function_probe();
59df055f
SR
2157 mutex_unlock(&ftrace_lock);
2158}
2159
2160void
b6887d79 2161unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
59df055f
SR
2162 void *data)
2163{
b6887d79
SR
2164 __unregister_ftrace_function_probe(glob, ops, data,
2165 PROBE_TEST_FUNC | PROBE_TEST_DATA);
59df055f
SR
2166}
2167
2168void
b6887d79 2169unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
59df055f 2170{
b6887d79 2171 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
59df055f
SR
2172}
2173
b6887d79 2174void unregister_ftrace_function_probe_all(char *glob)
59df055f 2175{
b6887d79 2176 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
59df055f
SR
2177}
2178
f6180773
SR
2179static LIST_HEAD(ftrace_commands);
2180static DEFINE_MUTEX(ftrace_cmd_mutex);
2181
2182int register_ftrace_command(struct ftrace_func_command *cmd)
2183{
2184 struct ftrace_func_command *p;
2185 int ret = 0;
2186
2187 mutex_lock(&ftrace_cmd_mutex);
2188 list_for_each_entry(p, &ftrace_commands, list) {
2189 if (strcmp(cmd->name, p->name) == 0) {
2190 ret = -EBUSY;
2191 goto out_unlock;
2192 }
2193 }
2194 list_add(&cmd->list, &ftrace_commands);
2195 out_unlock:
2196 mutex_unlock(&ftrace_cmd_mutex);
2197
2198 return ret;
2199}
2200
2201int unregister_ftrace_command(struct ftrace_func_command *cmd)
2202{
2203 struct ftrace_func_command *p, *n;
2204 int ret = -ENODEV;
2205
2206 mutex_lock(&ftrace_cmd_mutex);
2207 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2208 if (strcmp(cmd->name, p->name) == 0) {
2209 ret = 0;
2210 list_del_init(&p->list);
2211 goto out_unlock;
2212 }
2213 }
2214 out_unlock:
2215 mutex_unlock(&ftrace_cmd_mutex);
2216
2217 return ret;
2218}
2219
64e7c440
SR
2220static int ftrace_process_regex(char *buff, int len, int enable)
2221{
f6180773 2222 char *func, *command, *next = buff;
6a24a244 2223 struct ftrace_func_command *p;
f6180773 2224 int ret = -EINVAL;
64e7c440
SR
2225
2226 func = strsep(&next, ":");
2227
2228 if (!next) {
2229 ftrace_match_records(func, len, enable);
2230 return 0;
2231 }
2232
f6180773 2233 /* command found */
64e7c440
SR
2234
2235 command = strsep(&next, ":");
2236
f6180773
SR
2237 mutex_lock(&ftrace_cmd_mutex);
2238 list_for_each_entry(p, &ftrace_commands, list) {
2239 if (strcmp(p->name, command) == 0) {
2240 ret = p->func(func, command, next, enable);
2241 goto out_unlock;
2242 }
64e7c440 2243 }
f6180773
SR
2244 out_unlock:
2245 mutex_unlock(&ftrace_cmd_mutex);
64e7c440 2246
f6180773 2247 return ret;
64e7c440
SR
2248}
2249
e309b41d 2250static ssize_t
41c52c0d
SR
2251ftrace_regex_write(struct file *file, const char __user *ubuf,
2252 size_t cnt, loff_t *ppos, int enable)
5072c59f
SR
2253{
2254 struct ftrace_iterator *iter;
2255 char ch;
2256 size_t read = 0;
2257 ssize_t ret;
2258
2259 if (!cnt || cnt < 0)
2260 return 0;
2261
41c52c0d 2262 mutex_lock(&ftrace_regex_lock);
5072c59f
SR
2263
2264 if (file->f_mode & FMODE_READ) {
2265 struct seq_file *m = file->private_data;
2266 iter = m->private;
2267 } else
2268 iter = file->private_data;
2269
2270 if (!*ppos) {
2271 iter->flags &= ~FTRACE_ITER_CONT;
2272 iter->buffer_idx = 0;
2273 }
2274
2275 ret = get_user(ch, ubuf++);
2276 if (ret)
2277 goto out;
2278 read++;
2279 cnt--;
2280
2281 if (!(iter->flags & ~FTRACE_ITER_CONT)) {
2282 /* skip white space */
2283 while (cnt && isspace(ch)) {
2284 ret = get_user(ch, ubuf++);
2285 if (ret)
2286 goto out;
2287 read++;
2288 cnt--;
2289 }
2290
5072c59f
SR
2291 if (isspace(ch)) {
2292 file->f_pos += read;
2293 ret = read;
2294 goto out;
2295 }
2296
2297 iter->buffer_idx = 0;
2298 }
2299
2300 while (cnt && !isspace(ch)) {
2301 if (iter->buffer_idx < FTRACE_BUFF_MAX)
2302 iter->buffer[iter->buffer_idx++] = ch;
2303 else {
2304 ret = -EINVAL;
2305 goto out;
2306 }
2307 ret = get_user(ch, ubuf++);
2308 if (ret)
2309 goto out;
2310 read++;
2311 cnt--;
2312 }
2313
2314 if (isspace(ch)) {
2315 iter->filtered++;
2316 iter->buffer[iter->buffer_idx] = 0;
64e7c440
SR
2317 ret = ftrace_process_regex(iter->buffer,
2318 iter->buffer_idx, enable);
2319 if (ret)
2320 goto out;
5072c59f
SR
2321 iter->buffer_idx = 0;
2322 } else
2323 iter->flags |= FTRACE_ITER_CONT;
2324
2325
2326 file->f_pos += read;
2327
2328 ret = read;
2329 out:
41c52c0d 2330 mutex_unlock(&ftrace_regex_lock);
5072c59f
SR
2331
2332 return ret;
2333}
2334
41c52c0d
SR
2335static ssize_t
2336ftrace_filter_write(struct file *file, const char __user *ubuf,
2337 size_t cnt, loff_t *ppos)
2338{
2339 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2340}
2341
2342static ssize_t
2343ftrace_notrace_write(struct file *file, const char __user *ubuf,
2344 size_t cnt, loff_t *ppos)
2345{
2346 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2347}
2348
2349static void
2350ftrace_set_regex(unsigned char *buf, int len, int reset, int enable)
2351{
2352 if (unlikely(ftrace_disabled))
2353 return;
2354
2355 mutex_lock(&ftrace_regex_lock);
2356 if (reset)
2357 ftrace_filter_reset(enable);
2358 if (buf)
7f24b31b 2359 ftrace_match_records(buf, len, enable);
41c52c0d
SR
2360 mutex_unlock(&ftrace_regex_lock);
2361}
2362
77a2b37d
SR
2363/**
2364 * ftrace_set_filter - set a function to filter on in ftrace
2365 * @buf - the string that holds the function filter text.
2366 * @len - the length of the string.
2367 * @reset - non zero to reset all filters before applying this filter.
2368 *
2369 * Filters denote which functions should be enabled when tracing is enabled.
2370 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2371 */
e309b41d 2372void ftrace_set_filter(unsigned char *buf, int len, int reset)
77a2b37d 2373{
41c52c0d
SR
2374 ftrace_set_regex(buf, len, reset, 1);
2375}
4eebcc81 2376
41c52c0d
SR
2377/**
2378 * ftrace_set_notrace - set a function to not trace in ftrace
2379 * @buf - the string that holds the function notrace text.
2380 * @len - the length of the string.
2381 * @reset - non zero to reset all filters before applying this filter.
2382 *
2383 * Notrace Filters denote which functions should not be enabled when tracing
2384 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2385 * for tracing.
2386 */
2387void ftrace_set_notrace(unsigned char *buf, int len, int reset)
2388{
2389 ftrace_set_regex(buf, len, reset, 0);
77a2b37d
SR
2390}
2391
2af15d6a
SR
2392/*
2393 * command line interface to allow users to set filters on boot up.
2394 */
2395#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2396static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
2397static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
2398
2399static int __init set_ftrace_notrace(char *str)
2400{
2401 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
2402 return 1;
2403}
2404__setup("ftrace_notrace=", set_ftrace_notrace);
2405
2406static int __init set_ftrace_filter(char *str)
2407{
2408 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
2409 return 1;
2410}
2411__setup("ftrace_filter=", set_ftrace_filter);
2412
2413static void __init set_ftrace_early_filter(char *buf, int enable)
2414{
2415 char *func;
2416
2417 while (buf) {
2418 func = strsep(&buf, ",");
2419 ftrace_set_regex(func, strlen(func), 0, enable);
2420 }
2421}
2422
2423static void __init set_ftrace_early_filters(void)
2424{
2425 if (ftrace_filter_buf[0])
2426 set_ftrace_early_filter(ftrace_filter_buf, 1);
2427 if (ftrace_notrace_buf[0])
2428 set_ftrace_early_filter(ftrace_notrace_buf, 0);
2429}
2430
e309b41d 2431static int
41c52c0d 2432ftrace_regex_release(struct inode *inode, struct file *file, int enable)
5072c59f
SR
2433{
2434 struct seq_file *m = (struct seq_file *)file->private_data;
2435 struct ftrace_iterator *iter;
2436
41c52c0d 2437 mutex_lock(&ftrace_regex_lock);
5072c59f
SR
2438 if (file->f_mode & FMODE_READ) {
2439 iter = m->private;
2440
2441 seq_release(inode, file);
2442 } else
2443 iter = file->private_data;
2444
2445 if (iter->buffer_idx) {
2446 iter->filtered++;
2447 iter->buffer[iter->buffer_idx] = 0;
7f24b31b 2448 ftrace_match_records(iter->buffer, iter->buffer_idx, enable);
5072c59f
SR
2449 }
2450
e6ea44e9 2451 mutex_lock(&ftrace_lock);
ee02a2e5 2452 if (ftrace_start_up && ftrace_enabled)
5072c59f 2453 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
e6ea44e9 2454 mutex_unlock(&ftrace_lock);
5072c59f
SR
2455
2456 kfree(iter);
41c52c0d 2457 mutex_unlock(&ftrace_regex_lock);
5072c59f
SR
2458 return 0;
2459}
2460
41c52c0d
SR
2461static int
2462ftrace_filter_release(struct inode *inode, struct file *file)
2463{
2464 return ftrace_regex_release(inode, file, 1);
2465}
2466
2467static int
2468ftrace_notrace_release(struct inode *inode, struct file *file)
2469{
2470 return ftrace_regex_release(inode, file, 0);
2471}
2472
5e2336a0 2473static const struct file_operations ftrace_avail_fops = {
5072c59f
SR
2474 .open = ftrace_avail_open,
2475 .read = seq_read,
2476 .llseek = seq_lseek,
2477 .release = ftrace_avail_release,
2478};
2479
5e2336a0 2480static const struct file_operations ftrace_failures_fops = {
eb9a7bf0
AS
2481 .open = ftrace_failures_open,
2482 .read = seq_read,
2483 .llseek = seq_lseek,
2484 .release = ftrace_avail_release,
2485};
2486
5e2336a0 2487static const struct file_operations ftrace_filter_fops = {
5072c59f 2488 .open = ftrace_filter_open,
850a80cf 2489 .read = seq_read,
5072c59f 2490 .write = ftrace_filter_write,
41c52c0d 2491 .llseek = ftrace_regex_lseek,
5072c59f
SR
2492 .release = ftrace_filter_release,
2493};
2494
5e2336a0 2495static const struct file_operations ftrace_notrace_fops = {
41c52c0d 2496 .open = ftrace_notrace_open,
850a80cf 2497 .read = seq_read,
41c52c0d
SR
2498 .write = ftrace_notrace_write,
2499 .llseek = ftrace_regex_lseek,
2500 .release = ftrace_notrace_release,
2501};
2502
ea4e2bc4
SR
2503#ifdef CONFIG_FUNCTION_GRAPH_TRACER
2504
2505static DEFINE_MUTEX(graph_lock);
2506
2507int ftrace_graph_count;
2508unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
2509
2510static void *
85951842 2511__g_next(struct seq_file *m, loff_t *pos)
ea4e2bc4
SR
2512{
2513 unsigned long *array = m->private;
ea4e2bc4 2514
85951842 2515 if (*pos >= ftrace_graph_count)
ea4e2bc4 2516 return NULL;
85951842
LZ
2517 return &array[*pos];
2518}
ea4e2bc4 2519
85951842
LZ
2520static void *
2521g_next(struct seq_file *m, void *v, loff_t *pos)
2522{
2523 (*pos)++;
2524 return __g_next(m, pos);
ea4e2bc4
SR
2525}
2526
2527static void *g_start(struct seq_file *m, loff_t *pos)
2528{
ea4e2bc4
SR
2529 mutex_lock(&graph_lock);
2530
f9349a8f
FW
2531 /* Nothing, tell g_show to print all functions are enabled */
2532 if (!ftrace_graph_count && !*pos)
2533 return (void *)1;
2534
85951842 2535 return __g_next(m, pos);
ea4e2bc4
SR
2536}
2537
2538static void g_stop(struct seq_file *m, void *p)
2539{
2540 mutex_unlock(&graph_lock);
2541}
2542
2543static int g_show(struct seq_file *m, void *v)
2544{
2545 unsigned long *ptr = v;
2546 char str[KSYM_SYMBOL_LEN];
2547
2548 if (!ptr)
2549 return 0;
2550
f9349a8f
FW
2551 if (ptr == (unsigned long *)1) {
2552 seq_printf(m, "#### all functions enabled ####\n");
2553 return 0;
2554 }
2555
ea4e2bc4
SR
2556 kallsyms_lookup(*ptr, NULL, NULL, NULL, str);
2557
2558 seq_printf(m, "%s\n", str);
2559
2560 return 0;
2561}
2562
2563static struct seq_operations ftrace_graph_seq_ops = {
2564 .start = g_start,
2565 .next = g_next,
2566 .stop = g_stop,
2567 .show = g_show,
2568};
2569
2570static int
2571ftrace_graph_open(struct inode *inode, struct file *file)
2572{
2573 int ret = 0;
2574
2575 if (unlikely(ftrace_disabled))
2576 return -ENODEV;
2577
2578 mutex_lock(&graph_lock);
2579 if ((file->f_mode & FMODE_WRITE) &&
8650ae32 2580 (file->f_flags & O_TRUNC)) {
ea4e2bc4
SR
2581 ftrace_graph_count = 0;
2582 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
2583 }
2584
2585 if (file->f_mode & FMODE_READ) {
2586 ret = seq_open(file, &ftrace_graph_seq_ops);
2587 if (!ret) {
2588 struct seq_file *m = file->private_data;
2589 m->private = ftrace_graph_funcs;
2590 }
2591 } else
2592 file->private_data = ftrace_graph_funcs;
2593 mutex_unlock(&graph_lock);
2594
2595 return ret;
2596}
2597
87827111
LZ
2598static int
2599ftrace_graph_release(struct inode *inode, struct file *file)
2600{
2601 if (file->f_mode & FMODE_READ)
2602 seq_release(inode, file);
2603 return 0;
2604}
2605
ea4e2bc4 2606static int
f9349a8f 2607ftrace_set_func(unsigned long *array, int *idx, char *buffer)
ea4e2bc4 2608{
ea4e2bc4
SR
2609 struct dyn_ftrace *rec;
2610 struct ftrace_page *pg;
f9349a8f 2611 int search_len;
ea4e2bc4 2612 int found = 0;
f9349a8f
FW
2613 int type, not;
2614 char *search;
2615 bool exists;
2616 int i;
ea4e2bc4
SR
2617
2618 if (ftrace_disabled)
2619 return -ENODEV;
2620
f9349a8f
FW
2621 /* decode regex */
2622 type = ftrace_setup_glob(buffer, strlen(buffer), &search, &not);
2623 if (not)
2624 return -EINVAL;
2625
2626 search_len = strlen(search);
2627
52baf119 2628 mutex_lock(&ftrace_lock);
265c831c
SR
2629 do_for_each_ftrace_rec(pg, rec) {
2630
f9349a8f
FW
2631 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
2632 break;
2633
265c831c
SR
2634 if (rec->flags & (FTRACE_FL_FAILED | FTRACE_FL_FREE))
2635 continue;
2636
f9349a8f
FW
2637 if (ftrace_match_record(rec, search, search_len, type)) {
2638 /* ensure it is not already in the array */
2639 exists = false;
2640 for (i = 0; i < *idx; i++)
2641 if (array[i] == rec->ip) {
2642 exists = true;
265c831c
SR
2643 break;
2644 }
f9349a8f
FW
2645 if (!exists) {
2646 array[(*idx)++] = rec->ip;
2647 found = 1;
2648 }
ea4e2bc4 2649 }
265c831c 2650 } while_for_each_ftrace_rec();
f9349a8f 2651
52baf119 2652 mutex_unlock(&ftrace_lock);
ea4e2bc4
SR
2653
2654 return found ? 0 : -EINVAL;
2655}
2656
2657static ssize_t
2658ftrace_graph_write(struct file *file, const char __user *ubuf,
2659 size_t cnt, loff_t *ppos)
2660{
2661 unsigned char buffer[FTRACE_BUFF_MAX+1];
2662 unsigned long *array;
2663 size_t read = 0;
2664 ssize_t ret;
2665 int index = 0;
2666 char ch;
2667
2668 if (!cnt || cnt < 0)
2669 return 0;
2670
2671 mutex_lock(&graph_lock);
2672
2673 if (ftrace_graph_count >= FTRACE_GRAPH_MAX_FUNCS) {
2674 ret = -EBUSY;
2675 goto out;
2676 }
2677
2678 if (file->f_mode & FMODE_READ) {
2679 struct seq_file *m = file->private_data;
2680 array = m->private;
2681 } else
2682 array = file->private_data;
2683
2684 ret = get_user(ch, ubuf++);
2685 if (ret)
2686 goto out;
2687 read++;
2688 cnt--;
2689
2690 /* skip white space */
2691 while (cnt && isspace(ch)) {
2692 ret = get_user(ch, ubuf++);
2693 if (ret)
2694 goto out;
2695 read++;
2696 cnt--;
2697 }
2698
2699 if (isspace(ch)) {
2700 *ppos += read;
2701 ret = read;
2702 goto out;
2703 }
2704
2705 while (cnt && !isspace(ch)) {
2706 if (index < FTRACE_BUFF_MAX)
2707 buffer[index++] = ch;
2708 else {
2709 ret = -EINVAL;
2710 goto out;
2711 }
2712 ret = get_user(ch, ubuf++);
2713 if (ret)
2714 goto out;
2715 read++;
2716 cnt--;
2717 }
2718 buffer[index] = 0;
2719
f9349a8f
FW
2720 /* we allow only one expression at a time */
2721 ret = ftrace_set_func(array, &ftrace_graph_count, buffer);
ea4e2bc4
SR
2722 if (ret)
2723 goto out;
2724
ea4e2bc4
SR
2725 file->f_pos += read;
2726
2727 ret = read;
2728 out:
2729 mutex_unlock(&graph_lock);
2730
2731 return ret;
2732}
2733
2734static const struct file_operations ftrace_graph_fops = {
87827111
LZ
2735 .open = ftrace_graph_open,
2736 .read = seq_read,
2737 .write = ftrace_graph_write,
2738 .release = ftrace_graph_release,
ea4e2bc4
SR
2739};
2740#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2741
df4fc315 2742static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
5072c59f 2743{
5072c59f 2744
5452af66
FW
2745 trace_create_file("available_filter_functions", 0444,
2746 d_tracer, NULL, &ftrace_avail_fops);
5072c59f 2747
5452af66
FW
2748 trace_create_file("failures", 0444,
2749 d_tracer, NULL, &ftrace_failures_fops);
eb9a7bf0 2750
5452af66
FW
2751 trace_create_file("set_ftrace_filter", 0644, d_tracer,
2752 NULL, &ftrace_filter_fops);
41c52c0d 2753
5452af66 2754 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
41c52c0d 2755 NULL, &ftrace_notrace_fops);
ad90c0e3 2756
ea4e2bc4 2757#ifdef CONFIG_FUNCTION_GRAPH_TRACER
5452af66 2758 trace_create_file("set_graph_function", 0444, d_tracer,
ea4e2bc4
SR
2759 NULL,
2760 &ftrace_graph_fops);
ea4e2bc4
SR
2761#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2762
5072c59f
SR
2763 return 0;
2764}
2765
31e88909
SR
2766static int ftrace_convert_nops(struct module *mod,
2767 unsigned long *start,
68bf21aa
SR
2768 unsigned long *end)
2769{
2770 unsigned long *p;
2771 unsigned long addr;
2772 unsigned long flags;
2773
e6ea44e9 2774 mutex_lock(&ftrace_lock);
68bf21aa
SR
2775 p = start;
2776 while (p < end) {
2777 addr = ftrace_call_adjust(*p++);
20e5227e
SR
2778 /*
2779 * Some architecture linkers will pad between
2780 * the different mcount_loc sections of different
2781 * object files to satisfy alignments.
2782 * Skip any NULL pointers.
2783 */
2784 if (!addr)
2785 continue;
68bf21aa 2786 ftrace_record_ip(addr);
68bf21aa
SR
2787 }
2788
08f5ac90 2789 /* disable interrupts to prevent kstop machine */
68bf21aa 2790 local_irq_save(flags);
31e88909 2791 ftrace_update_code(mod);
68bf21aa 2792 local_irq_restore(flags);
e6ea44e9 2793 mutex_unlock(&ftrace_lock);
68bf21aa
SR
2794
2795 return 0;
2796}
2797
93eb677d
SR
2798#ifdef CONFIG_MODULES
2799void ftrace_release(void *start, void *end)
2800{
2801 struct dyn_ftrace *rec;
2802 struct ftrace_page *pg;
2803 unsigned long s = (unsigned long)start;
2804 unsigned long e = (unsigned long)end;
2805
2806 if (ftrace_disabled || !start || start == end)
2807 return;
2808
2809 mutex_lock(&ftrace_lock);
2810 do_for_each_ftrace_rec(pg, rec) {
2811 if ((rec->ip >= s) && (rec->ip < e)) {
2812 /*
2813 * rec->ip is changed in ftrace_free_rec()
2814 * It should not between s and e if record was freed.
2815 */
2816 FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
2817 ftrace_free_rec(rec);
2818 }
2819 } while_for_each_ftrace_rec();
2820 mutex_unlock(&ftrace_lock);
2821}
2822
2823static void ftrace_init_module(struct module *mod,
2824 unsigned long *start, unsigned long *end)
90d595fe 2825{
00fd61ae 2826 if (ftrace_disabled || start == end)
fed1939c 2827 return;
31e88909 2828 ftrace_convert_nops(mod, start, end);
90d595fe
SR
2829}
2830
93eb677d
SR
2831static int ftrace_module_notify(struct notifier_block *self,
2832 unsigned long val, void *data)
2833{
2834 struct module *mod = data;
2835
2836 switch (val) {
2837 case MODULE_STATE_COMING:
2838 ftrace_init_module(mod, mod->ftrace_callsites,
2839 mod->ftrace_callsites +
2840 mod->num_ftrace_callsites);
2841 break;
2842 case MODULE_STATE_GOING:
2843 ftrace_release(mod->ftrace_callsites,
2844 mod->ftrace_callsites +
2845 mod->num_ftrace_callsites);
2846 break;
2847 }
2848
2849 return 0;
2850}
2851#else
2852static int ftrace_module_notify(struct notifier_block *self,
2853 unsigned long val, void *data)
2854{
2855 return 0;
2856}
2857#endif /* CONFIG_MODULES */
2858
2859struct notifier_block ftrace_module_nb = {
2860 .notifier_call = ftrace_module_notify,
2861 .priority = 0,
2862};
2863
68bf21aa
SR
2864extern unsigned long __start_mcount_loc[];
2865extern unsigned long __stop_mcount_loc[];
2866
2867void __init ftrace_init(void)
2868{
2869 unsigned long count, addr, flags;
2870 int ret;
2871
2872 /* Keep the ftrace pointer to the stub */
2873 addr = (unsigned long)ftrace_stub;
2874
2875 local_irq_save(flags);
2876 ftrace_dyn_arch_init(&addr);
2877 local_irq_restore(flags);
2878
2879 /* ftrace_dyn_arch_init places the return code in addr */
2880 if (addr)
2881 goto failed;
2882
2883 count = __stop_mcount_loc - __start_mcount_loc;
2884
2885 ret = ftrace_dyn_table_alloc(count);
2886 if (ret)
2887 goto failed;
2888
2889 last_ftrace_enabled = ftrace_enabled = 1;
2890
31e88909
SR
2891 ret = ftrace_convert_nops(NULL,
2892 __start_mcount_loc,
68bf21aa
SR
2893 __stop_mcount_loc);
2894
93eb677d 2895 ret = register_module_notifier(&ftrace_module_nb);
24ed0c4b 2896 if (ret)
93eb677d
SR
2897 pr_warning("Failed to register trace ftrace module notifier\n");
2898
2af15d6a
SR
2899 set_ftrace_early_filters();
2900
68bf21aa
SR
2901 return;
2902 failed:
2903 ftrace_disabled = 1;
2904}
68bf21aa 2905
3d083395 2906#else
0b6e4d56
FW
2907
2908static int __init ftrace_nodyn_init(void)
2909{
2910 ftrace_enabled = 1;
2911 return 0;
2912}
2913device_initcall(ftrace_nodyn_init);
2914
df4fc315
SR
2915static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
2916static inline void ftrace_startup_enable(int command) { }
5a45cfe1
SR
2917/* Keep as macros so we do not need to define the commands */
2918# define ftrace_startup(command) do { } while (0)
2919# define ftrace_shutdown(command) do { } while (0)
c7aafc54
IM
2920# define ftrace_startup_sysctl() do { } while (0)
2921# define ftrace_shutdown_sysctl() do { } while (0)
3d083395
SR
2922#endif /* CONFIG_DYNAMIC_FTRACE */
2923
df4fc315
SR
2924static ssize_t
2925ftrace_pid_read(struct file *file, char __user *ubuf,
2926 size_t cnt, loff_t *ppos)
2927{
2928 char buf[64];
2929 int r;
2930
e32d8956
SR
2931 if (ftrace_pid_trace == ftrace_swapper_pid)
2932 r = sprintf(buf, "swapper tasks\n");
2933 else if (ftrace_pid_trace)
cc59c9e8 2934 r = sprintf(buf, "%u\n", pid_vnr(ftrace_pid_trace));
df4fc315
SR
2935 else
2936 r = sprintf(buf, "no pid\n");
2937
2938 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
2939}
2940
e32d8956 2941static void clear_ftrace_swapper(void)
978f3a45
SR
2942{
2943 struct task_struct *p;
e32d8956 2944 int cpu;
978f3a45 2945
e32d8956
SR
2946 get_online_cpus();
2947 for_each_online_cpu(cpu) {
2948 p = idle_task(cpu);
978f3a45 2949 clear_tsk_trace_trace(p);
e32d8956
SR
2950 }
2951 put_online_cpus();
2952}
978f3a45 2953
e32d8956
SR
2954static void set_ftrace_swapper(void)
2955{
2956 struct task_struct *p;
2957 int cpu;
2958
2959 get_online_cpus();
2960 for_each_online_cpu(cpu) {
2961 p = idle_task(cpu);
2962 set_tsk_trace_trace(p);
2963 }
2964 put_online_cpus();
978f3a45
SR
2965}
2966
e32d8956
SR
2967static void clear_ftrace_pid(struct pid *pid)
2968{
2969 struct task_struct *p;
2970
229c4ef8 2971 rcu_read_lock();
e32d8956
SR
2972 do_each_pid_task(pid, PIDTYPE_PID, p) {
2973 clear_tsk_trace_trace(p);
2974 } while_each_pid_task(pid, PIDTYPE_PID, p);
229c4ef8
ON
2975 rcu_read_unlock();
2976
e32d8956
SR
2977 put_pid(pid);
2978}
2979
2980static void set_ftrace_pid(struct pid *pid)
978f3a45
SR
2981{
2982 struct task_struct *p;
2983
229c4ef8 2984 rcu_read_lock();
978f3a45
SR
2985 do_each_pid_task(pid, PIDTYPE_PID, p) {
2986 set_tsk_trace_trace(p);
2987 } while_each_pid_task(pid, PIDTYPE_PID, p);
229c4ef8 2988 rcu_read_unlock();
978f3a45
SR
2989}
2990
e32d8956
SR
2991static void clear_ftrace_pid_task(struct pid **pid)
2992{
2993 if (*pid == ftrace_swapper_pid)
2994 clear_ftrace_swapper();
2995 else
2996 clear_ftrace_pid(*pid);
2997
2998 *pid = NULL;
2999}
3000
3001static void set_ftrace_pid_task(struct pid *pid)
3002{
3003 if (pid == ftrace_swapper_pid)
3004 set_ftrace_swapper();
3005 else
3006 set_ftrace_pid(pid);
3007}
3008
df4fc315
SR
3009static ssize_t
3010ftrace_pid_write(struct file *filp, const char __user *ubuf,
3011 size_t cnt, loff_t *ppos)
3012{
978f3a45 3013 struct pid *pid;
df4fc315
SR
3014 char buf[64];
3015 long val;
3016 int ret;
3017
3018 if (cnt >= sizeof(buf))
3019 return -EINVAL;
3020
3021 if (copy_from_user(&buf, ubuf, cnt))
3022 return -EFAULT;
3023
3024 buf[cnt] = 0;
3025
3026 ret = strict_strtol(buf, 10, &val);
3027 if (ret < 0)
3028 return ret;
3029
e6ea44e9 3030 mutex_lock(&ftrace_lock);
978f3a45 3031 if (val < 0) {
df4fc315 3032 /* disable pid tracing */
978f3a45 3033 if (!ftrace_pid_trace)
df4fc315 3034 goto out;
978f3a45
SR
3035
3036 clear_ftrace_pid_task(&ftrace_pid_trace);
df4fc315
SR
3037
3038 } else {
e32d8956
SR
3039 /* swapper task is special */
3040 if (!val) {
3041 pid = ftrace_swapper_pid;
3042 if (pid == ftrace_pid_trace)
3043 goto out;
3044 } else {
3045 pid = find_get_pid(val);
df4fc315 3046
e32d8956
SR
3047 if (pid == ftrace_pid_trace) {
3048 put_pid(pid);
3049 goto out;
3050 }
0ef8cde5 3051 }
0ef8cde5 3052
978f3a45
SR
3053 if (ftrace_pid_trace)
3054 clear_ftrace_pid_task(&ftrace_pid_trace);
3055
3056 if (!pid)
3057 goto out;
3058
3059 ftrace_pid_trace = pid;
3060
3061 set_ftrace_pid_task(ftrace_pid_trace);
df4fc315
SR
3062 }
3063
3064 /* update the function call */
3065 ftrace_update_pid_func();
3066 ftrace_startup_enable(0);
3067
3068 out:
e6ea44e9 3069 mutex_unlock(&ftrace_lock);
df4fc315
SR
3070
3071 return cnt;
3072}
3073
5e2336a0 3074static const struct file_operations ftrace_pid_fops = {
df4fc315
SR
3075 .read = ftrace_pid_read,
3076 .write = ftrace_pid_write,
3077};
3078
3079static __init int ftrace_init_debugfs(void)
3080{
3081 struct dentry *d_tracer;
df4fc315
SR
3082
3083 d_tracer = tracing_init_dentry();
3084 if (!d_tracer)
3085 return 0;
3086
3087 ftrace_init_dyn_debugfs(d_tracer);
3088
5452af66
FW
3089 trace_create_file("set_ftrace_pid", 0644, d_tracer,
3090 NULL, &ftrace_pid_fops);
493762fc
SR
3091
3092 ftrace_profile_debugfs(d_tracer);
3093
df4fc315
SR
3094 return 0;
3095}
df4fc315
SR
3096fs_initcall(ftrace_init_debugfs);
3097
a2bb6a3d 3098/**
81adbdc0 3099 * ftrace_kill - kill ftrace
a2bb6a3d
SR
3100 *
3101 * This function should be used by panic code. It stops ftrace
3102 * but in a not so nice way. If you need to simply kill ftrace
3103 * from a non-atomic section, use ftrace_kill.
3104 */
81adbdc0 3105void ftrace_kill(void)
a2bb6a3d
SR
3106{
3107 ftrace_disabled = 1;
3108 ftrace_enabled = 0;
a2bb6a3d
SR
3109 clear_ftrace_function();
3110}
3111
16444a8a 3112/**
3d083395
SR
3113 * register_ftrace_function - register a function for profiling
3114 * @ops - ops structure that holds the function for profiling.
16444a8a 3115 *
3d083395
SR
3116 * Register a function to be called by all functions in the
3117 * kernel.
3118 *
3119 * Note: @ops->func and all the functions it calls must be labeled
3120 * with "notrace", otherwise it will go into a
3121 * recursive loop.
16444a8a 3122 */
3d083395 3123int register_ftrace_function(struct ftrace_ops *ops)
16444a8a 3124{
b0fc494f
SR
3125 int ret;
3126
4eebcc81
SR
3127 if (unlikely(ftrace_disabled))
3128 return -1;
3129
e6ea44e9 3130 mutex_lock(&ftrace_lock);
e7d3737e 3131
b0fc494f 3132 ret = __register_ftrace_function(ops);
5a45cfe1 3133 ftrace_startup(0);
b0fc494f 3134
e6ea44e9 3135 mutex_unlock(&ftrace_lock);
b0fc494f 3136 return ret;
3d083395
SR
3137}
3138
3139/**
32632920 3140 * unregister_ftrace_function - unregister a function for profiling.
3d083395
SR
3141 * @ops - ops structure that holds the function to unregister
3142 *
3143 * Unregister a function that was added to be called by ftrace profiling.
3144 */
3145int unregister_ftrace_function(struct ftrace_ops *ops)
3146{
3147 int ret;
3148
e6ea44e9 3149 mutex_lock(&ftrace_lock);
3d083395 3150 ret = __unregister_ftrace_function(ops);
5a45cfe1 3151 ftrace_shutdown(0);
e6ea44e9 3152 mutex_unlock(&ftrace_lock);
b0fc494f
SR
3153
3154 return ret;
3155}
3156
e309b41d 3157int
b0fc494f 3158ftrace_enable_sysctl(struct ctl_table *table, int write,
5072c59f 3159 struct file *file, void __user *buffer, size_t *lenp,
b0fc494f
SR
3160 loff_t *ppos)
3161{
3162 int ret;
3163
4eebcc81
SR
3164 if (unlikely(ftrace_disabled))
3165 return -ENODEV;
3166
e6ea44e9 3167 mutex_lock(&ftrace_lock);
b0fc494f 3168
5072c59f 3169 ret = proc_dointvec(table, write, file, buffer, lenp, ppos);
b0fc494f 3170
a32c7765 3171 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
b0fc494f
SR
3172 goto out;
3173
a32c7765 3174 last_ftrace_enabled = !!ftrace_enabled;
b0fc494f
SR
3175
3176 if (ftrace_enabled) {
3177
3178 ftrace_startup_sysctl();
3179
3180 /* we are starting ftrace again */
3181 if (ftrace_list != &ftrace_list_end) {
3182 if (ftrace_list->next == &ftrace_list_end)
3183 ftrace_trace_function = ftrace_list->func;
3184 else
3185 ftrace_trace_function = ftrace_list_func;
3186 }
3187
3188 } else {
3189 /* stopping ftrace calls (just send to ftrace_stub) */
3190 ftrace_trace_function = ftrace_stub;
3191
3192 ftrace_shutdown_sysctl();
3193 }
3194
3195 out:
e6ea44e9 3196 mutex_unlock(&ftrace_lock);
3d083395 3197 return ret;
16444a8a 3198}
f17845e5 3199
fb52607a 3200#ifdef CONFIG_FUNCTION_GRAPH_TRACER
e7d3737e 3201
597af815 3202static int ftrace_graph_active;
4a2b8dda 3203static struct notifier_block ftrace_suspend_notifier;
e7d3737e 3204
e49dc19c
SR
3205int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3206{
3207 return 0;
3208}
3209
287b6e68
FW
3210/* The callbacks that hook a function */
3211trace_func_graph_ret_t ftrace_graph_return =
3212 (trace_func_graph_ret_t)ftrace_stub;
e49dc19c 3213trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
f201ae23
FW
3214
3215/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3216static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3217{
3218 int i;
3219 int ret = 0;
3220 unsigned long flags;
3221 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3222 struct task_struct *g, *t;
3223
3224 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3225 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3226 * sizeof(struct ftrace_ret_stack),
3227 GFP_KERNEL);
3228 if (!ret_stack_list[i]) {
3229 start = 0;
3230 end = i;
3231 ret = -ENOMEM;
3232 goto free;
3233 }
3234 }
3235
3236 read_lock_irqsave(&tasklist_lock, flags);
3237 do_each_thread(g, t) {
3238 if (start == end) {
3239 ret = -EAGAIN;
3240 goto unlock;
3241 }
3242
3243 if (t->ret_stack == NULL) {
380c4b14 3244 atomic_set(&t->tracing_graph_pause, 0);
f201ae23 3245 atomic_set(&t->trace_overrun, 0);
26c01624
SR
3246 t->curr_ret_stack = -1;
3247 /* Make sure the tasks see the -1 first: */
3248 smp_wmb();
3249 t->ret_stack = ret_stack_list[start++];
f201ae23
FW
3250 }
3251 } while_each_thread(g, t);
3252
3253unlock:
3254 read_unlock_irqrestore(&tasklist_lock, flags);
3255free:
3256 for (i = start; i < end; i++)
3257 kfree(ret_stack_list[i]);
3258 return ret;
3259}
3260
8aef2d28
SR
3261static void
3262ftrace_graph_probe_sched_switch(struct rq *__rq, struct task_struct *prev,
3263 struct task_struct *next)
3264{
3265 unsigned long long timestamp;
3266 int index;
3267
be6f164a
SR
3268 /*
3269 * Does the user want to count the time a function was asleep.
3270 * If so, do not update the time stamps.
3271 */
3272 if (trace_flags & TRACE_ITER_SLEEP_TIME)
3273 return;
3274
8aef2d28
SR
3275 timestamp = trace_clock_local();
3276
3277 prev->ftrace_timestamp = timestamp;
3278
3279 /* only process tasks that we timestamped */
3280 if (!next->ftrace_timestamp)
3281 return;
3282
3283 /*
3284 * Update all the counters in next to make up for the
3285 * time next was sleeping.
3286 */
3287 timestamp -= next->ftrace_timestamp;
3288
3289 for (index = next->curr_ret_stack; index >= 0; index--)
3290 next->ret_stack[index].calltime += timestamp;
3291}
3292
f201ae23 3293/* Allocate a return stack for each task */
fb52607a 3294static int start_graph_tracing(void)
f201ae23
FW
3295{
3296 struct ftrace_ret_stack **ret_stack_list;
5b058bcd 3297 int ret, cpu;
f201ae23
FW
3298
3299 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
3300 sizeof(struct ftrace_ret_stack *),
3301 GFP_KERNEL);
3302
3303 if (!ret_stack_list)
3304 return -ENOMEM;
3305
5b058bcd 3306 /* The cpu_boot init_task->ret_stack will never be freed */
179c498a
SR
3307 for_each_online_cpu(cpu) {
3308 if (!idle_task(cpu)->ret_stack)
3309 ftrace_graph_init_task(idle_task(cpu));
3310 }
5b058bcd 3311
f201ae23
FW
3312 do {
3313 ret = alloc_retstack_tasklist(ret_stack_list);
3314 } while (ret == -EAGAIN);
3315
8aef2d28
SR
3316 if (!ret) {
3317 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch);
3318 if (ret)
3319 pr_info("ftrace_graph: Couldn't activate tracepoint"
3320 " probe to kernel_sched_switch\n");
3321 }
3322
f201ae23
FW
3323 kfree(ret_stack_list);
3324 return ret;
3325}
3326
4a2b8dda
FW
3327/*
3328 * Hibernation protection.
3329 * The state of the current task is too much unstable during
3330 * suspend/restore to disk. We want to protect against that.
3331 */
3332static int
3333ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
3334 void *unused)
3335{
3336 switch (state) {
3337 case PM_HIBERNATION_PREPARE:
3338 pause_graph_tracing();
3339 break;
3340
3341 case PM_POST_HIBERNATION:
3342 unpause_graph_tracing();
3343 break;
3344 }
3345 return NOTIFY_DONE;
3346}
3347
287b6e68
FW
3348int register_ftrace_graph(trace_func_graph_ret_t retfunc,
3349 trace_func_graph_ent_t entryfunc)
15e6cb36 3350{
e7d3737e
FW
3351 int ret = 0;
3352
e6ea44e9 3353 mutex_lock(&ftrace_lock);
e7d3737e 3354
05ce5818 3355 /* we currently allow only one tracer registered at a time */
597af815 3356 if (ftrace_graph_active) {
05ce5818
SR
3357 ret = -EBUSY;
3358 goto out;
3359 }
3360
4a2b8dda
FW
3361 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
3362 register_pm_notifier(&ftrace_suspend_notifier);
3363
597af815 3364 ftrace_graph_active++;
fb52607a 3365 ret = start_graph_tracing();
f201ae23 3366 if (ret) {
597af815 3367 ftrace_graph_active--;
f201ae23
FW
3368 goto out;
3369 }
e53a6319 3370
287b6e68
FW
3371 ftrace_graph_return = retfunc;
3372 ftrace_graph_entry = entryfunc;
e53a6319 3373
5a45cfe1 3374 ftrace_startup(FTRACE_START_FUNC_RET);
e7d3737e
FW
3375
3376out:
e6ea44e9 3377 mutex_unlock(&ftrace_lock);
e7d3737e 3378 return ret;
15e6cb36
FW
3379}
3380
fb52607a 3381void unregister_ftrace_graph(void)
15e6cb36 3382{
e6ea44e9 3383 mutex_lock(&ftrace_lock);
e7d3737e 3384
597af815 3385 if (unlikely(!ftrace_graph_active))
2aad1b76
SR
3386 goto out;
3387
597af815 3388 ftrace_graph_active--;
8aef2d28 3389 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch);
287b6e68 3390 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
e49dc19c 3391 ftrace_graph_entry = ftrace_graph_entry_stub;
5a45cfe1 3392 ftrace_shutdown(FTRACE_STOP_FUNC_RET);
4a2b8dda 3393 unregister_pm_notifier(&ftrace_suspend_notifier);
e7d3737e 3394
2aad1b76 3395 out:
e6ea44e9 3396 mutex_unlock(&ftrace_lock);
15e6cb36 3397}
f201ae23
FW
3398
3399/* Allocate a return stack for newly created task */
fb52607a 3400void ftrace_graph_init_task(struct task_struct *t)
f201ae23 3401{
84047e36
SR
3402 /* Make sure we do not use the parent ret_stack */
3403 t->ret_stack = NULL;
3404
597af815 3405 if (ftrace_graph_active) {
82310a32
SR
3406 struct ftrace_ret_stack *ret_stack;
3407
3408 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
f201ae23
FW
3409 * sizeof(struct ftrace_ret_stack),
3410 GFP_KERNEL);
82310a32 3411 if (!ret_stack)
f201ae23
FW
3412 return;
3413 t->curr_ret_stack = -1;
380c4b14 3414 atomic_set(&t->tracing_graph_pause, 0);
f201ae23 3415 atomic_set(&t->trace_overrun, 0);
8aef2d28 3416 t->ftrace_timestamp = 0;
82310a32
SR
3417 /* make curr_ret_stack visable before we add the ret_stack */
3418 smp_wmb();
3419 t->ret_stack = ret_stack;
84047e36 3420 }
f201ae23
FW
3421}
3422
fb52607a 3423void ftrace_graph_exit_task(struct task_struct *t)
f201ae23 3424{
eae849ca
FW
3425 struct ftrace_ret_stack *ret_stack = t->ret_stack;
3426
f201ae23 3427 t->ret_stack = NULL;
eae849ca
FW
3428 /* NULL must become visible to IRQs before we free it: */
3429 barrier();
3430
3431 kfree(ret_stack);
f201ae23 3432}
14a866c5
SR
3433
3434void ftrace_graph_stop(void)
3435{
3436 ftrace_stop();
3437}
15e6cb36
FW
3438#endif
3439
This page took 0.279172 seconds and 5 git commands to generate.