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