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