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