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