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