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