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