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