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