ftrace: Implement separate user function filtering
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / kernel / trace / ftrace.c
CommitLineData
16444a8a
ACM
1/*
2 * Infrastructure for profiling code inserted by 'gcc -pg'.
3 *
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
6 *
7 * Originally ported from the -rt patch by:
8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
9 *
10 * Based on code in the latency_tracer, that is:
11 *
12 * Copyright (C) 2004-2006 Ingo Molnar
13 * Copyright (C) 2004 William Lee Irwin III
14 */
15
3d083395
SR
16#include <linux/stop_machine.h>
17#include <linux/clocksource.h>
18#include <linux/kallsyms.h>
5072c59f 19#include <linux/seq_file.h>
4a2b8dda 20#include <linux/suspend.h>
5072c59f 21#include <linux/debugfs.h>
3d083395 22#include <linux/hardirq.h>
2d8b820b 23#include <linux/kthread.h>
5072c59f 24#include <linux/uaccess.h>
2d8b820b 25#include <linux/ftrace.h>
b0fc494f 26#include <linux/sysctl.h>
5a0e3ad6 27#include <linux/slab.h>
5072c59f 28#include <linux/ctype.h>
3d083395 29#include <linux/list.h>
59df055f 30#include <linux/hash.h>
3f379b03 31#include <linux/rcupdate.h>
3d083395 32
ad8d75ff 33#include <trace/events/sched.h>
8aef2d28 34
395a59d0 35#include <asm/ftrace.h>
2af15d6a 36#include <asm/setup.h>
395a59d0 37
0706f1c4 38#include "trace_output.h"
bac429f0 39#include "trace_stat.h"
16444a8a 40
6912896e 41#define FTRACE_WARN_ON(cond) \
0778d9ad
SR
42 ({ \
43 int ___r = cond; \
44 if (WARN_ON(___r)) \
6912896e 45 ftrace_kill(); \
0778d9ad
SR
46 ___r; \
47 })
6912896e
SR
48
49#define FTRACE_WARN_ON_ONCE(cond) \
0778d9ad
SR
50 ({ \
51 int ___r = cond; \
52 if (WARN_ON_ONCE(___r)) \
6912896e 53 ftrace_kill(); \
0778d9ad
SR
54 ___r; \
55 })
6912896e 56
8fc0c701
SR
57/* hash bits for specific function selection */
58#define FTRACE_HASH_BITS 7
59#define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
33dc9b12
SR
60#define FTRACE_HASH_DEFAULT_BITS 10
61#define FTRACE_HASH_MAX_BITS 12
8fc0c701 62
4eebcc81
SR
63/* ftrace_enabled is a method to turn ftrace on or off */
64int ftrace_enabled __read_mostly;
d61f82d0 65static int last_ftrace_enabled;
b0fc494f 66
60a7ecf4
SR
67/* Quick disabling of function tracer. */
68int function_trace_stop;
69
756d17ee 70/* List for set_ftrace_pid's pids. */
71LIST_HEAD(ftrace_pids);
72struct ftrace_pid {
73 struct list_head list;
74 struct pid *pid;
75};
76
4eebcc81
SR
77/*
78 * ftrace_disabled is set when an anomaly is discovered.
79 * ftrace_disabled is much stronger than ftrace_enabled.
80 */
81static int ftrace_disabled __read_mostly;
82
52baf119 83static DEFINE_MUTEX(ftrace_lock);
b0fc494f 84
16444a8a
ACM
85static struct ftrace_ops ftrace_list_end __read_mostly =
86{
fb9fb015 87 .func = ftrace_stub,
16444a8a
ACM
88};
89
b848914c
SR
90static struct ftrace_ops *ftrace_global_list __read_mostly = &ftrace_list_end;
91static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
16444a8a 92ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
60a7ecf4 93ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
df4fc315 94ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
2b499381 95static struct ftrace_ops global_ops;
16444a8a 96
b848914c
SR
97static void
98ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip);
99
3f379b03 100/*
b848914c 101 * Traverse the ftrace_global_list, invoking all entries. The reason that we
3f379b03
PM
102 * can use rcu_dereference_raw() is that elements removed from this list
103 * are simply leaked, so there is no need to interact with a grace-period
104 * mechanism. The rcu_dereference_raw() calls are needed to handle
b848914c 105 * concurrent insertions into the ftrace_global_list.
3f379b03
PM
106 *
107 * Silly Alpha and silly pointer-speculation compiler optimizations!
108 */
b848914c
SR
109static void ftrace_global_list_func(unsigned long ip,
110 unsigned long parent_ip)
16444a8a 111{
b848914c 112 struct ftrace_ops *op = rcu_dereference_raw(ftrace_global_list); /*see above*/
16444a8a
ACM
113
114 while (op != &ftrace_list_end) {
16444a8a 115 op->func(ip, parent_ip);
3f379b03 116 op = rcu_dereference_raw(op->next); /*see above*/
16444a8a
ACM
117 };
118}
119
df4fc315
SR
120static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
121{
0ef8cde5 122 if (!test_tsk_trace_trace(current))
df4fc315
SR
123 return;
124
125 ftrace_pid_function(ip, parent_ip);
126}
127
128static void set_ftrace_pid_function(ftrace_func_t func)
129{
130 /* do not set ftrace_pid_function to itself! */
131 if (func != ftrace_pid_func)
132 ftrace_pid_function = func;
133}
134
16444a8a 135/**
3d083395 136 * clear_ftrace_function - reset the ftrace function
16444a8a 137 *
3d083395
SR
138 * This NULLs the ftrace function and in essence stops
139 * tracing. There may be lag
16444a8a 140 */
3d083395 141void clear_ftrace_function(void)
16444a8a 142{
3d083395 143 ftrace_trace_function = ftrace_stub;
60a7ecf4 144 __ftrace_trace_function = ftrace_stub;
df4fc315 145 ftrace_pid_function = ftrace_stub;
3d083395
SR
146}
147
60a7ecf4
SR
148#ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
149/*
150 * For those archs that do not test ftrace_trace_stop in their
151 * mcount call site, we need to do it from C.
152 */
153static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
154{
155 if (function_trace_stop)
156 return;
157
158 __ftrace_trace_function(ip, parent_ip);
159}
160#endif
161
2b499381 162static void update_global_ops(void)
491d0dcf
SR
163{
164 ftrace_func_t func;
165
166 /*
167 * If there's only one function registered, then call that
168 * function directly. Otherwise, we need to iterate over the
169 * registered callers.
170 */
b848914c
SR
171 if (ftrace_global_list == &ftrace_list_end ||
172 ftrace_global_list->next == &ftrace_list_end)
173 func = ftrace_global_list->func;
491d0dcf 174 else
b848914c 175 func = ftrace_global_list_func;
491d0dcf
SR
176
177 /* If we filter on pids, update to use the pid function */
178 if (!list_empty(&ftrace_pids)) {
179 set_ftrace_pid_function(func);
180 func = ftrace_pid_func;
181 }
2b499381
SR
182
183 global_ops.func = func;
184}
185
186static void update_ftrace_function(void)
187{
188 ftrace_func_t func;
189
190 update_global_ops();
191
b848914c
SR
192 if (ftrace_ops_list == &ftrace_list_end ||
193 ftrace_ops_list->next == &ftrace_list_end)
194 func = ftrace_ops_list->func;
195 else
196 func = ftrace_ops_list_func;
2b499381 197
491d0dcf
SR
198#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
199 ftrace_trace_function = func;
200#else
201 __ftrace_trace_function = func;
202 ftrace_trace_function = ftrace_test_stop_func;
203#endif
204}
205
2b499381 206static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
3d083395 207{
2b499381 208 ops->next = *list;
16444a8a 209 /*
b848914c 210 * We are entering ops into the list but another
16444a8a
ACM
211 * CPU might be walking that list. We need to make sure
212 * the ops->next pointer is valid before another CPU sees
b848914c 213 * the ops pointer included into the list.
16444a8a 214 */
2b499381 215 rcu_assign_pointer(*list, ops);
16444a8a
ACM
216}
217
2b499381 218static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
16444a8a 219{
16444a8a 220 struct ftrace_ops **p;
16444a8a
ACM
221
222 /*
3d083395
SR
223 * If we are removing the last function, then simply point
224 * to the ftrace_stub.
16444a8a 225 */
2b499381
SR
226 if (*list == ops && ops->next == &ftrace_list_end) {
227 *list = &ftrace_list_end;
e6ea44e9 228 return 0;
16444a8a
ACM
229 }
230
2b499381 231 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
16444a8a
ACM
232 if (*p == ops)
233 break;
234
e6ea44e9
SR
235 if (*p != ops)
236 return -1;
16444a8a
ACM
237
238 *p = (*p)->next;
2b499381
SR
239 return 0;
240}
16444a8a 241
2b499381
SR
242static int __register_ftrace_function(struct ftrace_ops *ops)
243{
244 if (ftrace_disabled)
245 return -ENODEV;
246
247 if (FTRACE_WARN_ON(ops == &global_ops))
248 return -EINVAL;
249
b848914c
SR
250 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
251 return -EBUSY;
252
253 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
254 int first = ftrace_global_list == &ftrace_list_end;
255 add_ftrace_ops(&ftrace_global_list, ops);
256 ops->flags |= FTRACE_OPS_FL_ENABLED;
257 if (first)
258 add_ftrace_ops(&ftrace_ops_list, &global_ops);
259 } else
260 add_ftrace_ops(&ftrace_ops_list, ops);
261
2b499381
SR
262 if (ftrace_enabled)
263 update_ftrace_function();
264
265 return 0;
266}
267
268static int __unregister_ftrace_function(struct ftrace_ops *ops)
269{
270 int ret;
271
272 if (ftrace_disabled)
273 return -ENODEV;
274
b848914c
SR
275 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
276 return -EBUSY;
277
2b499381
SR
278 if (FTRACE_WARN_ON(ops == &global_ops))
279 return -EINVAL;
280
b848914c
SR
281 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
282 ret = remove_ftrace_ops(&ftrace_global_list, ops);
283 if (!ret && ftrace_global_list == &ftrace_list_end)
284 ret = remove_ftrace_ops(&ftrace_ops_list, &global_ops);
285 if (!ret)
286 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
287 } else
288 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
289
2b499381
SR
290 if (ret < 0)
291 return ret;
b848914c 292
491d0dcf
SR
293 if (ftrace_enabled)
294 update_ftrace_function();
16444a8a 295
e6ea44e9 296 return 0;
3d083395
SR
297}
298
df4fc315
SR
299static void ftrace_update_pid_func(void)
300{
491d0dcf 301 /* Only do something if we are tracing something */
df4fc315 302 if (ftrace_trace_function == ftrace_stub)
10dd3ebe 303 return;
df4fc315 304
491d0dcf 305 update_ftrace_function();
df4fc315
SR
306}
307
493762fc
SR
308#ifdef CONFIG_FUNCTION_PROFILER
309struct ftrace_profile {
310 struct hlist_node node;
311 unsigned long ip;
312 unsigned long counter;
0706f1c4
SR
313#ifdef CONFIG_FUNCTION_GRAPH_TRACER
314 unsigned long long time;
e330b3bc 315 unsigned long long time_squared;
0706f1c4 316#endif
8fc0c701
SR
317};
318
493762fc
SR
319struct ftrace_profile_page {
320 struct ftrace_profile_page *next;
321 unsigned long index;
322 struct ftrace_profile records[];
d61f82d0
SR
323};
324
cafb168a
SR
325struct ftrace_profile_stat {
326 atomic_t disabled;
327 struct hlist_head *hash;
328 struct ftrace_profile_page *pages;
329 struct ftrace_profile_page *start;
330 struct tracer_stat stat;
331};
332
493762fc
SR
333#define PROFILE_RECORDS_SIZE \
334 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
5072c59f 335
493762fc
SR
336#define PROFILES_PER_PAGE \
337 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
3d083395 338
fb9fb015
SR
339static int ftrace_profile_bits __read_mostly;
340static int ftrace_profile_enabled __read_mostly;
341
342/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
bac429f0
SR
343static DEFINE_MUTEX(ftrace_profile_lock);
344
cafb168a 345static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
493762fc
SR
346
347#define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
348
bac429f0
SR
349static void *
350function_stat_next(void *v, int idx)
351{
493762fc
SR
352 struct ftrace_profile *rec = v;
353 struct ftrace_profile_page *pg;
bac429f0 354
493762fc 355 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
bac429f0
SR
356
357 again:
0296e425
LZ
358 if (idx != 0)
359 rec++;
360
bac429f0
SR
361 if ((void *)rec >= (void *)&pg->records[pg->index]) {
362 pg = pg->next;
363 if (!pg)
364 return NULL;
365 rec = &pg->records[0];
493762fc
SR
366 if (!rec->counter)
367 goto again;
bac429f0
SR
368 }
369
bac429f0
SR
370 return rec;
371}
372
373static void *function_stat_start(struct tracer_stat *trace)
374{
cafb168a
SR
375 struct ftrace_profile_stat *stat =
376 container_of(trace, struct ftrace_profile_stat, stat);
377
378 if (!stat || !stat->start)
379 return NULL;
380
381 return function_stat_next(&stat->start->records[0], 0);
bac429f0
SR
382}
383
0706f1c4
SR
384#ifdef CONFIG_FUNCTION_GRAPH_TRACER
385/* function graph compares on total time */
386static int function_stat_cmp(void *p1, void *p2)
387{
388 struct ftrace_profile *a = p1;
389 struct ftrace_profile *b = p2;
390
391 if (a->time < b->time)
392 return -1;
393 if (a->time > b->time)
394 return 1;
395 else
396 return 0;
397}
398#else
399/* not function graph compares against hits */
bac429f0
SR
400static int function_stat_cmp(void *p1, void *p2)
401{
493762fc
SR
402 struct ftrace_profile *a = p1;
403 struct ftrace_profile *b = p2;
bac429f0
SR
404
405 if (a->counter < b->counter)
406 return -1;
407 if (a->counter > b->counter)
408 return 1;
409 else
410 return 0;
411}
0706f1c4 412#endif
bac429f0
SR
413
414static int function_stat_headers(struct seq_file *m)
415{
0706f1c4 416#ifdef CONFIG_FUNCTION_GRAPH_TRACER
34886c8b 417 seq_printf(m, " Function "
e330b3bc 418 "Hit Time Avg s^2\n"
34886c8b 419 " -------- "
e330b3bc 420 "--- ---- --- ---\n");
0706f1c4 421#else
bac429f0
SR
422 seq_printf(m, " Function Hit\n"
423 " -------- ---\n");
0706f1c4 424#endif
bac429f0
SR
425 return 0;
426}
427
428static int function_stat_show(struct seq_file *m, void *v)
429{
493762fc 430 struct ftrace_profile *rec = v;
bac429f0 431 char str[KSYM_SYMBOL_LEN];
3aaba20f 432 int ret = 0;
0706f1c4 433#ifdef CONFIG_FUNCTION_GRAPH_TRACER
34886c8b
SR
434 static struct trace_seq s;
435 unsigned long long avg;
e330b3bc 436 unsigned long long stddev;
0706f1c4 437#endif
3aaba20f
LZ
438 mutex_lock(&ftrace_profile_lock);
439
440 /* we raced with function_profile_reset() */
441 if (unlikely(rec->counter == 0)) {
442 ret = -EBUSY;
443 goto out;
444 }
bac429f0
SR
445
446 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
0706f1c4
SR
447 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
448
449#ifdef CONFIG_FUNCTION_GRAPH_TRACER
450 seq_printf(m, " ");
34886c8b
SR
451 avg = rec->time;
452 do_div(avg, rec->counter);
453
e330b3bc
CD
454 /* Sample standard deviation (s^2) */
455 if (rec->counter <= 1)
456 stddev = 0;
457 else {
458 stddev = rec->time_squared - rec->counter * avg * avg;
459 /*
460 * Divide only 1000 for ns^2 -> us^2 conversion.
461 * trace_print_graph_duration will divide 1000 again.
462 */
463 do_div(stddev, (rec->counter - 1) * 1000);
464 }
465
34886c8b
SR
466 trace_seq_init(&s);
467 trace_print_graph_duration(rec->time, &s);
468 trace_seq_puts(&s, " ");
469 trace_print_graph_duration(avg, &s);
e330b3bc
CD
470 trace_seq_puts(&s, " ");
471 trace_print_graph_duration(stddev, &s);
0706f1c4 472 trace_print_seq(m, &s);
0706f1c4
SR
473#endif
474 seq_putc(m, '\n');
3aaba20f
LZ
475out:
476 mutex_unlock(&ftrace_profile_lock);
bac429f0 477
3aaba20f 478 return ret;
bac429f0
SR
479}
480
cafb168a 481static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
bac429f0 482{
493762fc 483 struct ftrace_profile_page *pg;
bac429f0 484
cafb168a 485 pg = stat->pages = stat->start;
bac429f0 486
493762fc
SR
487 while (pg) {
488 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
489 pg->index = 0;
490 pg = pg->next;
bac429f0
SR
491 }
492
cafb168a 493 memset(stat->hash, 0,
493762fc
SR
494 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
495}
bac429f0 496
cafb168a 497int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
493762fc
SR
498{
499 struct ftrace_profile_page *pg;
318e0a73
SR
500 int functions;
501 int pages;
493762fc 502 int i;
bac429f0 503
493762fc 504 /* If we already allocated, do nothing */
cafb168a 505 if (stat->pages)
493762fc 506 return 0;
bac429f0 507
cafb168a
SR
508 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
509 if (!stat->pages)
493762fc 510 return -ENOMEM;
bac429f0 511
318e0a73
SR
512#ifdef CONFIG_DYNAMIC_FTRACE
513 functions = ftrace_update_tot_cnt;
514#else
515 /*
516 * We do not know the number of functions that exist because
517 * dynamic tracing is what counts them. With past experience
518 * we have around 20K functions. That should be more than enough.
519 * It is highly unlikely we will execute every function in
520 * the kernel.
521 */
522 functions = 20000;
523#endif
524
cafb168a 525 pg = stat->start = stat->pages;
bac429f0 526
318e0a73
SR
527 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
528
529 for (i = 0; i < pages; i++) {
493762fc 530 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
493762fc 531 if (!pg->next)
318e0a73 532 goto out_free;
493762fc
SR
533 pg = pg->next;
534 }
535
536 return 0;
318e0a73
SR
537
538 out_free:
539 pg = stat->start;
540 while (pg) {
541 unsigned long tmp = (unsigned long)pg;
542
543 pg = pg->next;
544 free_page(tmp);
545 }
546
547 free_page((unsigned long)stat->pages);
548 stat->pages = NULL;
549 stat->start = NULL;
550
551 return -ENOMEM;
bac429f0
SR
552}
553
cafb168a 554static int ftrace_profile_init_cpu(int cpu)
bac429f0 555{
cafb168a 556 struct ftrace_profile_stat *stat;
493762fc 557 int size;
bac429f0 558
cafb168a
SR
559 stat = &per_cpu(ftrace_profile_stats, cpu);
560
561 if (stat->hash) {
493762fc 562 /* If the profile is already created, simply reset it */
cafb168a 563 ftrace_profile_reset(stat);
493762fc
SR
564 return 0;
565 }
bac429f0 566
493762fc
SR
567 /*
568 * We are profiling all functions, but usually only a few thousand
569 * functions are hit. We'll make a hash of 1024 items.
570 */
571 size = FTRACE_PROFILE_HASH_SIZE;
bac429f0 572
cafb168a 573 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
493762fc 574
cafb168a 575 if (!stat->hash)
493762fc
SR
576 return -ENOMEM;
577
cafb168a
SR
578 if (!ftrace_profile_bits) {
579 size--;
493762fc 580
cafb168a
SR
581 for (; size; size >>= 1)
582 ftrace_profile_bits++;
583 }
493762fc 584
318e0a73 585 /* Preallocate the function profiling pages */
cafb168a
SR
586 if (ftrace_profile_pages_init(stat) < 0) {
587 kfree(stat->hash);
588 stat->hash = NULL;
493762fc
SR
589 return -ENOMEM;
590 }
591
592 return 0;
bac429f0
SR
593}
594
cafb168a
SR
595static int ftrace_profile_init(void)
596{
597 int cpu;
598 int ret = 0;
599
600 for_each_online_cpu(cpu) {
601 ret = ftrace_profile_init_cpu(cpu);
602 if (ret)
603 break;
604 }
605
606 return ret;
607}
608
493762fc 609/* interrupts must be disabled */
cafb168a
SR
610static struct ftrace_profile *
611ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
bac429f0 612{
493762fc 613 struct ftrace_profile *rec;
bac429f0
SR
614 struct hlist_head *hhd;
615 struct hlist_node *n;
bac429f0
SR
616 unsigned long key;
617
bac429f0 618 key = hash_long(ip, ftrace_profile_bits);
cafb168a 619 hhd = &stat->hash[key];
bac429f0
SR
620
621 if (hlist_empty(hhd))
622 return NULL;
623
bac429f0
SR
624 hlist_for_each_entry_rcu(rec, n, hhd, node) {
625 if (rec->ip == ip)
493762fc
SR
626 return rec;
627 }
628
629 return NULL;
630}
631
cafb168a
SR
632static void ftrace_add_profile(struct ftrace_profile_stat *stat,
633 struct ftrace_profile *rec)
493762fc
SR
634{
635 unsigned long key;
636
637 key = hash_long(rec->ip, ftrace_profile_bits);
cafb168a 638 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
493762fc
SR
639}
640
318e0a73
SR
641/*
642 * The memory is already allocated, this simply finds a new record to use.
643 */
493762fc 644static struct ftrace_profile *
318e0a73 645ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
493762fc
SR
646{
647 struct ftrace_profile *rec = NULL;
648
318e0a73 649 /* prevent recursion (from NMIs) */
cafb168a 650 if (atomic_inc_return(&stat->disabled) != 1)
493762fc
SR
651 goto out;
652
493762fc 653 /*
318e0a73
SR
654 * Try to find the function again since an NMI
655 * could have added it
493762fc 656 */
cafb168a 657 rec = ftrace_find_profiled_func(stat, ip);
493762fc 658 if (rec)
cafb168a 659 goto out;
493762fc 660
cafb168a
SR
661 if (stat->pages->index == PROFILES_PER_PAGE) {
662 if (!stat->pages->next)
663 goto out;
664 stat->pages = stat->pages->next;
bac429f0 665 }
493762fc 666
cafb168a 667 rec = &stat->pages->records[stat->pages->index++];
493762fc 668 rec->ip = ip;
cafb168a 669 ftrace_add_profile(stat, rec);
493762fc 670
bac429f0 671 out:
cafb168a 672 atomic_dec(&stat->disabled);
bac429f0
SR
673
674 return rec;
675}
676
677static void
678function_profile_call(unsigned long ip, unsigned long parent_ip)
679{
cafb168a 680 struct ftrace_profile_stat *stat;
493762fc 681 struct ftrace_profile *rec;
bac429f0
SR
682 unsigned long flags;
683
684 if (!ftrace_profile_enabled)
685 return;
686
687 local_irq_save(flags);
cafb168a
SR
688
689 stat = &__get_cpu_var(ftrace_profile_stats);
0f6ce3de 690 if (!stat->hash || !ftrace_profile_enabled)
cafb168a
SR
691 goto out;
692
693 rec = ftrace_find_profiled_func(stat, ip);
493762fc 694 if (!rec) {
318e0a73 695 rec = ftrace_profile_alloc(stat, ip);
493762fc
SR
696 if (!rec)
697 goto out;
698 }
bac429f0
SR
699
700 rec->counter++;
701 out:
702 local_irq_restore(flags);
703}
704
0706f1c4
SR
705#ifdef CONFIG_FUNCTION_GRAPH_TRACER
706static int profile_graph_entry(struct ftrace_graph_ent *trace)
707{
708 function_profile_call(trace->func, 0);
709 return 1;
710}
711
712static void profile_graph_return(struct ftrace_graph_ret *trace)
713{
cafb168a 714 struct ftrace_profile_stat *stat;
a2a16d6a 715 unsigned long long calltime;
0706f1c4 716 struct ftrace_profile *rec;
cafb168a 717 unsigned long flags;
0706f1c4
SR
718
719 local_irq_save(flags);
cafb168a 720 stat = &__get_cpu_var(ftrace_profile_stats);
0f6ce3de 721 if (!stat->hash || !ftrace_profile_enabled)
cafb168a
SR
722 goto out;
723
37e44bc5
SR
724 /* If the calltime was zero'd ignore it */
725 if (!trace->calltime)
726 goto out;
727
a2a16d6a
SR
728 calltime = trace->rettime - trace->calltime;
729
730 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
731 int index;
732
733 index = trace->depth;
734
735 /* Append this call time to the parent time to subtract */
736 if (index)
737 current->ret_stack[index - 1].subtime += calltime;
738
739 if (current->ret_stack[index].subtime < calltime)
740 calltime -= current->ret_stack[index].subtime;
741 else
742 calltime = 0;
743 }
744
cafb168a 745 rec = ftrace_find_profiled_func(stat, trace->func);
e330b3bc 746 if (rec) {
a2a16d6a 747 rec->time += calltime;
e330b3bc
CD
748 rec->time_squared += calltime * calltime;
749 }
a2a16d6a 750
cafb168a 751 out:
0706f1c4
SR
752 local_irq_restore(flags);
753}
754
755static int register_ftrace_profiler(void)
756{
757 return register_ftrace_graph(&profile_graph_return,
758 &profile_graph_entry);
759}
760
761static void unregister_ftrace_profiler(void)
762{
763 unregister_ftrace_graph();
764}
765#else
bac429f0
SR
766static struct ftrace_ops ftrace_profile_ops __read_mostly =
767{
fb9fb015 768 .func = function_profile_call,
bac429f0
SR
769};
770
0706f1c4
SR
771static int register_ftrace_profiler(void)
772{
773 return register_ftrace_function(&ftrace_profile_ops);
774}
775
776static void unregister_ftrace_profiler(void)
777{
778 unregister_ftrace_function(&ftrace_profile_ops);
779}
780#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
781
bac429f0
SR
782static ssize_t
783ftrace_profile_write(struct file *filp, const char __user *ubuf,
784 size_t cnt, loff_t *ppos)
785{
786 unsigned long val;
fb9fb015 787 char buf[64]; /* big enough to hold a number */
bac429f0
SR
788 int ret;
789
bac429f0
SR
790 if (cnt >= sizeof(buf))
791 return -EINVAL;
792
793 if (copy_from_user(&buf, ubuf, cnt))
794 return -EFAULT;
795
796 buf[cnt] = 0;
797
798 ret = strict_strtoul(buf, 10, &val);
799 if (ret < 0)
800 return ret;
801
802 val = !!val;
803
804 mutex_lock(&ftrace_profile_lock);
805 if (ftrace_profile_enabled ^ val) {
806 if (val) {
493762fc
SR
807 ret = ftrace_profile_init();
808 if (ret < 0) {
809 cnt = ret;
810 goto out;
811 }
812
0706f1c4
SR
813 ret = register_ftrace_profiler();
814 if (ret < 0) {
815 cnt = ret;
816 goto out;
817 }
bac429f0
SR
818 ftrace_profile_enabled = 1;
819 } else {
820 ftrace_profile_enabled = 0;
0f6ce3de
SR
821 /*
822 * unregister_ftrace_profiler calls stop_machine
823 * so this acts like an synchronize_sched.
824 */
0706f1c4 825 unregister_ftrace_profiler();
bac429f0
SR
826 }
827 }
493762fc 828 out:
bac429f0
SR
829 mutex_unlock(&ftrace_profile_lock);
830
cf8517cf 831 *ppos += cnt;
bac429f0
SR
832
833 return cnt;
834}
835
493762fc
SR
836static ssize_t
837ftrace_profile_read(struct file *filp, char __user *ubuf,
838 size_t cnt, loff_t *ppos)
839{
fb9fb015 840 char buf[64]; /* big enough to hold a number */
493762fc
SR
841 int r;
842
843 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
844 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
845}
846
bac429f0
SR
847static const struct file_operations ftrace_profile_fops = {
848 .open = tracing_open_generic,
849 .read = ftrace_profile_read,
850 .write = ftrace_profile_write,
6038f373 851 .llseek = default_llseek,
bac429f0
SR
852};
853
cafb168a
SR
854/* used to initialize the real stat files */
855static struct tracer_stat function_stats __initdata = {
fb9fb015
SR
856 .name = "functions",
857 .stat_start = function_stat_start,
858 .stat_next = function_stat_next,
859 .stat_cmp = function_stat_cmp,
860 .stat_headers = function_stat_headers,
861 .stat_show = function_stat_show
cafb168a
SR
862};
863
6ab5d668 864static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
bac429f0 865{
cafb168a 866 struct ftrace_profile_stat *stat;
bac429f0 867 struct dentry *entry;
cafb168a 868 char *name;
bac429f0 869 int ret;
cafb168a
SR
870 int cpu;
871
872 for_each_possible_cpu(cpu) {
873 stat = &per_cpu(ftrace_profile_stats, cpu);
874
875 /* allocate enough for function name + cpu number */
876 name = kmalloc(32, GFP_KERNEL);
877 if (!name) {
878 /*
879 * The files created are permanent, if something happens
880 * we still do not free memory.
881 */
cafb168a
SR
882 WARN(1,
883 "Could not allocate stat file for cpu %d\n",
884 cpu);
885 return;
886 }
887 stat->stat = function_stats;
888 snprintf(name, 32, "function%d", cpu);
889 stat->stat.name = name;
890 ret = register_stat_tracer(&stat->stat);
891 if (ret) {
892 WARN(1,
893 "Could not register function stat for cpu %d\n",
894 cpu);
895 kfree(name);
896 return;
897 }
bac429f0
SR
898 }
899
900 entry = debugfs_create_file("function_profile_enabled", 0644,
901 d_tracer, NULL, &ftrace_profile_fops);
902 if (!entry)
903 pr_warning("Could not create debugfs "
904 "'function_profile_enabled' entry\n");
905}
906
bac429f0 907#else /* CONFIG_FUNCTION_PROFILER */
6ab5d668 908static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
bac429f0
SR
909{
910}
bac429f0
SR
911#endif /* CONFIG_FUNCTION_PROFILER */
912
493762fc
SR
913static struct pid * const ftrace_swapper_pid = &init_struct_pid;
914
915#ifdef CONFIG_DYNAMIC_FTRACE
916
917#ifndef CONFIG_FTRACE_MCOUNT_RECORD
918# error Dynamic ftrace depends on MCOUNT_RECORD
919#endif
920
921static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
922
923struct ftrace_func_probe {
924 struct hlist_node node;
925 struct ftrace_probe_ops *ops;
926 unsigned long flags;
927 unsigned long ip;
928 void *data;
929 struct rcu_head rcu;
930};
931
932enum {
933 FTRACE_ENABLE_CALLS = (1 << 0),
934 FTRACE_DISABLE_CALLS = (1 << 1),
935 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
79e406d7
SR
936 FTRACE_START_FUNC_RET = (1 << 3),
937 FTRACE_STOP_FUNC_RET = (1 << 4),
493762fc 938};
b448c4e3
SR
939struct ftrace_func_entry {
940 struct hlist_node hlist;
941 unsigned long ip;
942};
943
944struct ftrace_hash {
945 unsigned long size_bits;
946 struct hlist_head *buckets;
947 unsigned long count;
07fd5515 948 struct rcu_head rcu;
b448c4e3
SR
949};
950
33dc9b12
SR
951/*
952 * We make these constant because no one should touch them,
953 * but they are used as the default "empty hash", to avoid allocating
954 * it all the time. These are in a read only section such that if
955 * anyone does try to modify it, it will cause an exception.
956 */
957static const struct hlist_head empty_buckets[1];
958static const struct ftrace_hash empty_hash = {
959 .buckets = (struct hlist_head *)empty_buckets,
1cf41dd7 960};
33dc9b12 961#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
493762fc 962
2b499381 963static struct ftrace_ops global_ops = {
f45948e8 964 .func = ftrace_stub,
33dc9b12
SR
965 .notrace_hash = EMPTY_HASH,
966 .filter_hash = EMPTY_HASH,
f45948e8
SR
967};
968
493762fc
SR
969static struct dyn_ftrace *ftrace_new_addrs;
970
971static DEFINE_MUTEX(ftrace_regex_lock);
972
973struct ftrace_page {
974 struct ftrace_page *next;
975 int index;
976 struct dyn_ftrace records[];
977};
978
979#define ENTRIES_PER_PAGE \
980 ((PAGE_SIZE - sizeof(struct ftrace_page)) / sizeof(struct dyn_ftrace))
981
982/* estimate from running different kernels */
983#define NR_TO_INIT 10000
984
985static struct ftrace_page *ftrace_pages_start;
986static struct ftrace_page *ftrace_pages;
987
988static struct dyn_ftrace *ftrace_free_records;
989
b448c4e3
SR
990static struct ftrace_func_entry *
991ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
992{
993 unsigned long key;
994 struct ftrace_func_entry *entry;
995 struct hlist_head *hhd;
996 struct hlist_node *n;
997
998 if (!hash->count)
999 return NULL;
1000
1001 if (hash->size_bits > 0)
1002 key = hash_long(ip, hash->size_bits);
1003 else
1004 key = 0;
1005
1006 hhd = &hash->buckets[key];
1007
1008 hlist_for_each_entry_rcu(entry, n, hhd, hlist) {
1009 if (entry->ip == ip)
1010 return entry;
1011 }
1012 return NULL;
1013}
1014
33dc9b12
SR
1015static void __add_hash_entry(struct ftrace_hash *hash,
1016 struct ftrace_func_entry *entry)
b448c4e3 1017{
b448c4e3
SR
1018 struct hlist_head *hhd;
1019 unsigned long key;
1020
b448c4e3 1021 if (hash->size_bits)
33dc9b12 1022 key = hash_long(entry->ip, hash->size_bits);
b448c4e3
SR
1023 else
1024 key = 0;
1025
b448c4e3
SR
1026 hhd = &hash->buckets[key];
1027 hlist_add_head(&entry->hlist, hhd);
1028 hash->count++;
33dc9b12
SR
1029}
1030
1031static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1032{
1033 struct ftrace_func_entry *entry;
1034
1035 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1036 if (!entry)
1037 return -ENOMEM;
1038
1039 entry->ip = ip;
1040 __add_hash_entry(hash, entry);
b448c4e3
SR
1041
1042 return 0;
1043}
1044
1045static void
33dc9b12 1046free_hash_entry(struct ftrace_hash *hash,
b448c4e3
SR
1047 struct ftrace_func_entry *entry)
1048{
1049 hlist_del(&entry->hlist);
1050 kfree(entry);
1051 hash->count--;
1052}
1053
33dc9b12
SR
1054static void
1055remove_hash_entry(struct ftrace_hash *hash,
1056 struct ftrace_func_entry *entry)
1057{
1058 hlist_del(&entry->hlist);
1059 hash->count--;
1060}
1061
b448c4e3
SR
1062static void ftrace_hash_clear(struct ftrace_hash *hash)
1063{
1064 struct hlist_head *hhd;
1065 struct hlist_node *tp, *tn;
1066 struct ftrace_func_entry *entry;
1067 int size = 1 << hash->size_bits;
1068 int i;
1069
33dc9b12
SR
1070 if (!hash->count)
1071 return;
1072
b448c4e3
SR
1073 for (i = 0; i < size; i++) {
1074 hhd = &hash->buckets[i];
1075 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist)
33dc9b12 1076 free_hash_entry(hash, entry);
b448c4e3
SR
1077 }
1078 FTRACE_WARN_ON(hash->count);
1079}
1080
33dc9b12
SR
1081static void free_ftrace_hash(struct ftrace_hash *hash)
1082{
1083 if (!hash || hash == EMPTY_HASH)
1084 return;
1085 ftrace_hash_clear(hash);
1086 kfree(hash->buckets);
1087 kfree(hash);
1088}
1089
07fd5515
SR
1090static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1091{
1092 struct ftrace_hash *hash;
1093
1094 hash = container_of(rcu, struct ftrace_hash, rcu);
1095 free_ftrace_hash(hash);
1096}
1097
1098static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1099{
1100 if (!hash || hash == EMPTY_HASH)
1101 return;
1102 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1103}
1104
33dc9b12
SR
1105static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1106{
1107 struct ftrace_hash *hash;
1108 int size;
1109
1110 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1111 if (!hash)
1112 return NULL;
1113
1114 size = 1 << size_bits;
1115 hash->buckets = kzalloc(sizeof(*hash->buckets) * size, GFP_KERNEL);
1116
1117 if (!hash->buckets) {
1118 kfree(hash);
1119 return NULL;
1120 }
1121
1122 hash->size_bits = size_bits;
1123
1124 return hash;
1125}
1126
1127static struct ftrace_hash *
1128alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1129{
1130 struct ftrace_func_entry *entry;
1131 struct ftrace_hash *new_hash;
1132 struct hlist_node *tp;
1133 int size;
1134 int ret;
1135 int i;
1136
1137 new_hash = alloc_ftrace_hash(size_bits);
1138 if (!new_hash)
1139 return NULL;
1140
1141 /* Empty hash? */
1142 if (!hash || !hash->count)
1143 return new_hash;
1144
1145 size = 1 << hash->size_bits;
1146 for (i = 0; i < size; i++) {
1147 hlist_for_each_entry(entry, tp, &hash->buckets[i], hlist) {
1148 ret = add_hash_entry(new_hash, entry->ip);
1149 if (ret < 0)
1150 goto free_hash;
1151 }
1152 }
1153
1154 FTRACE_WARN_ON(new_hash->count != hash->count);
1155
1156 return new_hash;
1157
1158 free_hash:
1159 free_ftrace_hash(new_hash);
1160 return NULL;
1161}
1162
1163static int
1164ftrace_hash_move(struct ftrace_hash **dst, struct ftrace_hash *src)
1165{
1166 struct ftrace_func_entry *entry;
1167 struct hlist_node *tp, *tn;
1168 struct hlist_head *hhd;
07fd5515
SR
1169 struct ftrace_hash *old_hash;
1170 struct ftrace_hash *new_hash;
33dc9b12
SR
1171 unsigned long key;
1172 int size = src->count;
1173 int bits = 0;
1174 int i;
1175
1176 /*
1177 * If the new source is empty, just free dst and assign it
1178 * the empty_hash.
1179 */
1180 if (!src->count) {
07fd5515
SR
1181 free_ftrace_hash_rcu(*dst);
1182 rcu_assign_pointer(*dst, EMPTY_HASH);
33dc9b12
SR
1183 return 0;
1184 }
1185
33dc9b12
SR
1186 /*
1187 * Make the hash size about 1/2 the # found
1188 */
1189 for (size /= 2; size; size >>= 1)
1190 bits++;
1191
1192 /* Don't allocate too much */
1193 if (bits > FTRACE_HASH_MAX_BITS)
1194 bits = FTRACE_HASH_MAX_BITS;
1195
07fd5515
SR
1196 new_hash = alloc_ftrace_hash(bits);
1197 if (!new_hash)
1198 return -ENOMEM;
33dc9b12
SR
1199
1200 size = 1 << src->size_bits;
1201 for (i = 0; i < size; i++) {
1202 hhd = &src->buckets[i];
1203 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist) {
1204 if (bits > 0)
1205 key = hash_long(entry->ip, bits);
1206 else
1207 key = 0;
1208 remove_hash_entry(src, entry);
07fd5515 1209 __add_hash_entry(new_hash, entry);
33dc9b12
SR
1210 }
1211 }
1212
07fd5515
SR
1213 old_hash = *dst;
1214 rcu_assign_pointer(*dst, new_hash);
1215 free_ftrace_hash_rcu(old_hash);
1216
33dc9b12
SR
1217 return 0;
1218}
1219
b848914c
SR
1220/*
1221 * Test the hashes for this ops to see if we want to call
1222 * the ops->func or not.
1223 *
1224 * It's a match if the ip is in the ops->filter_hash or
1225 * the filter_hash does not exist or is empty,
1226 * AND
1227 * the ip is not in the ops->notrace_hash.
1228 */
1229static int
1230ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
1231{
1232 struct ftrace_hash *filter_hash;
1233 struct ftrace_hash *notrace_hash;
1234 int ret;
1235
1236 /* The hashes are freed with call_rcu_sched() */
1237 preempt_disable_notrace();
1238
1239 filter_hash = rcu_dereference_raw(ops->filter_hash);
1240 notrace_hash = rcu_dereference_raw(ops->notrace_hash);
1241
1242 if ((!filter_hash || !filter_hash->count ||
1243 ftrace_lookup_ip(filter_hash, ip)) &&
1244 (!notrace_hash || !notrace_hash->count ||
1245 !ftrace_lookup_ip(notrace_hash, ip)))
1246 ret = 1;
1247 else
1248 ret = 0;
1249 preempt_enable_notrace();
1250
1251 return ret;
1252}
1253
493762fc
SR
1254/*
1255 * This is a double for. Do not use 'break' to break out of the loop,
1256 * you must use a goto.
1257 */
1258#define do_for_each_ftrace_rec(pg, rec) \
1259 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1260 int _____i; \
1261 for (_____i = 0; _____i < pg->index; _____i++) { \
1262 rec = &pg->records[_____i];
1263
1264#define while_for_each_ftrace_rec() \
1265 } \
1266 }
1267
ed926f9b
SR
1268static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1269 int filter_hash,
1270 bool inc)
1271{
1272 struct ftrace_hash *hash;
1273 struct ftrace_hash *other_hash;
1274 struct ftrace_page *pg;
1275 struct dyn_ftrace *rec;
1276 int count = 0;
1277 int all = 0;
1278
1279 /* Only update if the ops has been registered */
1280 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1281 return;
1282
1283 /*
1284 * In the filter_hash case:
1285 * If the count is zero, we update all records.
1286 * Otherwise we just update the items in the hash.
1287 *
1288 * In the notrace_hash case:
1289 * We enable the update in the hash.
1290 * As disabling notrace means enabling the tracing,
1291 * and enabling notrace means disabling, the inc variable
1292 * gets inversed.
1293 */
1294 if (filter_hash) {
1295 hash = ops->filter_hash;
1296 other_hash = ops->notrace_hash;
b848914c 1297 if (!hash || !hash->count)
ed926f9b
SR
1298 all = 1;
1299 } else {
1300 inc = !inc;
1301 hash = ops->notrace_hash;
1302 other_hash = ops->filter_hash;
1303 /*
1304 * If the notrace hash has no items,
1305 * then there's nothing to do.
1306 */
b848914c 1307 if (hash && !hash->count)
ed926f9b
SR
1308 return;
1309 }
1310
1311 do_for_each_ftrace_rec(pg, rec) {
1312 int in_other_hash = 0;
1313 int in_hash = 0;
1314 int match = 0;
1315
1316 if (all) {
1317 /*
1318 * Only the filter_hash affects all records.
1319 * Update if the record is not in the notrace hash.
1320 */
b848914c 1321 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
ed926f9b
SR
1322 match = 1;
1323 } else {
b848914c
SR
1324 in_hash = hash && !!ftrace_lookup_ip(hash, rec->ip);
1325 in_other_hash = other_hash && !!ftrace_lookup_ip(other_hash, rec->ip);
ed926f9b
SR
1326
1327 /*
1328 *
1329 */
1330 if (filter_hash && in_hash && !in_other_hash)
1331 match = 1;
1332 else if (!filter_hash && in_hash &&
1333 (in_other_hash || !other_hash->count))
1334 match = 1;
1335 }
1336 if (!match)
1337 continue;
1338
1339 if (inc) {
1340 rec->flags++;
1341 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1342 return;
1343 } else {
1344 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1345 return;
1346 rec->flags--;
1347 }
1348 count++;
1349 /* Shortcut, if we handled all records, we are done. */
1350 if (!all && count == hash->count)
1351 return;
1352 } while_for_each_ftrace_rec();
1353}
1354
1355static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1356 int filter_hash)
1357{
1358 __ftrace_hash_rec_update(ops, filter_hash, 0);
1359}
1360
1361static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1362 int filter_hash)
1363{
1364 __ftrace_hash_rec_update(ops, filter_hash, 1);
1365}
1366
e309b41d 1367static void ftrace_free_rec(struct dyn_ftrace *rec)
37ad5084 1368{
ee000b7f 1369 rec->freelist = ftrace_free_records;
37ad5084
SR
1370 ftrace_free_records = rec;
1371 rec->flags |= FTRACE_FL_FREE;
1372}
1373
e309b41d 1374static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
3c1720f0 1375{
37ad5084
SR
1376 struct dyn_ftrace *rec;
1377
1378 /* First check for freed records */
1379 if (ftrace_free_records) {
1380 rec = ftrace_free_records;
1381
37ad5084 1382 if (unlikely(!(rec->flags & FTRACE_FL_FREE))) {
6912896e 1383 FTRACE_WARN_ON_ONCE(1);
37ad5084
SR
1384 ftrace_free_records = NULL;
1385 return NULL;
1386 }
1387
ee000b7f 1388 ftrace_free_records = rec->freelist;
37ad5084
SR
1389 memset(rec, 0, sizeof(*rec));
1390 return rec;
1391 }
1392
3c1720f0 1393 if (ftrace_pages->index == ENTRIES_PER_PAGE) {
08f5ac90
SR
1394 if (!ftrace_pages->next) {
1395 /* allocate another page */
1396 ftrace_pages->next =
1397 (void *)get_zeroed_page(GFP_KERNEL);
1398 if (!ftrace_pages->next)
1399 return NULL;
1400 }
3c1720f0
SR
1401 ftrace_pages = ftrace_pages->next;
1402 }
1403
1404 return &ftrace_pages->records[ftrace_pages->index++];
1405}
1406
08f5ac90 1407static struct dyn_ftrace *
d61f82d0 1408ftrace_record_ip(unsigned long ip)
3d083395 1409{
08f5ac90 1410 struct dyn_ftrace *rec;
3d083395 1411
f3c7ac40 1412 if (ftrace_disabled)
08f5ac90 1413 return NULL;
3d083395 1414
08f5ac90
SR
1415 rec = ftrace_alloc_dyn_node(ip);
1416 if (!rec)
1417 return NULL;
3d083395 1418
08f5ac90 1419 rec->ip = ip;
ee000b7f 1420 rec->newlist = ftrace_new_addrs;
e94142a6 1421 ftrace_new_addrs = rec;
3d083395 1422
08f5ac90 1423 return rec;
3d083395
SR
1424}
1425
b17e8a37
SR
1426static void print_ip_ins(const char *fmt, unsigned char *p)
1427{
1428 int i;
1429
1430 printk(KERN_CONT "%s", fmt);
1431
1432 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1433 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1434}
1435
31e88909 1436static void ftrace_bug(int failed, unsigned long ip)
b17e8a37
SR
1437{
1438 switch (failed) {
1439 case -EFAULT:
1440 FTRACE_WARN_ON_ONCE(1);
1441 pr_info("ftrace faulted on modifying ");
1442 print_ip_sym(ip);
1443 break;
1444 case -EINVAL:
1445 FTRACE_WARN_ON_ONCE(1);
1446 pr_info("ftrace failed to modify ");
1447 print_ip_sym(ip);
b17e8a37 1448 print_ip_ins(" actual: ", (unsigned char *)ip);
b17e8a37
SR
1449 printk(KERN_CONT "\n");
1450 break;
1451 case -EPERM:
1452 FTRACE_WARN_ON_ONCE(1);
1453 pr_info("ftrace faulted on writing ");
1454 print_ip_sym(ip);
1455 break;
1456 default:
1457 FTRACE_WARN_ON_ONCE(1);
1458 pr_info("ftrace faulted on unknown error ");
1459 print_ip_sym(ip);
1460 }
1461}
1462
3c1720f0 1463
2cfa1978
MH
1464/* Return 1 if the address range is reserved for ftrace */
1465int ftrace_text_reserved(void *start, void *end)
1466{
1467 struct dyn_ftrace *rec;
1468 struct ftrace_page *pg;
1469
1470 do_for_each_ftrace_rec(pg, rec) {
1471 if (rec->ip <= (unsigned long)end &&
1472 rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
1473 return 1;
1474 } while_for_each_ftrace_rec();
1475 return 0;
1476}
1477
1478
0eb96701 1479static int
31e88909 1480__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
5072c59f 1481{
e7d3737e 1482 unsigned long ftrace_addr;
64fbcd16 1483 unsigned long flag = 0UL;
e7d3737e 1484
f0001207 1485 ftrace_addr = (unsigned long)FTRACE_ADDR;
5072c59f 1486
982c350b 1487 /*
ed926f9b 1488 * If we are enabling tracing:
982c350b 1489 *
ed926f9b
SR
1490 * If the record has a ref count, then we need to enable it
1491 * because someone is using it.
982c350b 1492 *
ed926f9b
SR
1493 * Otherwise we make sure its disabled.
1494 *
1495 * If we are disabling tracing, then disable all records that
1496 * are enabled.
982c350b 1497 */
ed926f9b
SR
1498 if (enable && (rec->flags & ~FTRACE_FL_MASK))
1499 flag = FTRACE_FL_ENABLED;
982c350b 1500
64fbcd16
XG
1501 /* If the state of this record hasn't changed, then do nothing */
1502 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
1503 return 0;
982c350b 1504
64fbcd16
XG
1505 if (flag) {
1506 rec->flags |= FTRACE_FL_ENABLED;
1507 return ftrace_make_call(rec, ftrace_addr);
5072c59f
SR
1508 }
1509
64fbcd16
XG
1510 rec->flags &= ~FTRACE_FL_ENABLED;
1511 return ftrace_make_nop(NULL, rec, ftrace_addr);
5072c59f
SR
1512}
1513
e309b41d 1514static void ftrace_replace_code(int enable)
3c1720f0 1515{
3c1720f0
SR
1516 struct dyn_ftrace *rec;
1517 struct ftrace_page *pg;
6a24a244 1518 int failed;
3c1720f0 1519
45a4a237
SR
1520 if (unlikely(ftrace_disabled))
1521 return;
1522
265c831c 1523 do_for_each_ftrace_rec(pg, rec) {
d2c8c3ea
SR
1524 /* Skip over free records */
1525 if (rec->flags & FTRACE_FL_FREE)
265c831c
SR
1526 continue;
1527
265c831c 1528 failed = __ftrace_replace_code(rec, enable);
fa9d13cf 1529 if (failed) {
3279ba37
SR
1530 ftrace_bug(failed, rec->ip);
1531 /* Stop processing */
1532 return;
3c1720f0 1533 }
265c831c 1534 } while_for_each_ftrace_rec();
3c1720f0
SR
1535}
1536
492a7ea5 1537static int
31e88909 1538ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
3c1720f0
SR
1539{
1540 unsigned long ip;
593eb8a2 1541 int ret;
3c1720f0
SR
1542
1543 ip = rec->ip;
1544
45a4a237
SR
1545 if (unlikely(ftrace_disabled))
1546 return 0;
1547
25aac9dc 1548 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
593eb8a2 1549 if (ret) {
31e88909 1550 ftrace_bug(ret, ip);
492a7ea5 1551 return 0;
37ad5084 1552 }
492a7ea5 1553 return 1;
3c1720f0
SR
1554}
1555
000ab691
SR
1556/*
1557 * archs can override this function if they must do something
1558 * before the modifying code is performed.
1559 */
1560int __weak ftrace_arch_code_modify_prepare(void)
1561{
1562 return 0;
1563}
1564
1565/*
1566 * archs can override this function if they must do something
1567 * after the modifying code is performed.
1568 */
1569int __weak ftrace_arch_code_modify_post_process(void)
1570{
1571 return 0;
1572}
1573
e309b41d 1574static int __ftrace_modify_code(void *data)
3d083395 1575{
d61f82d0
SR
1576 int *command = data;
1577
a3583244 1578 if (*command & FTRACE_ENABLE_CALLS)
d61f82d0 1579 ftrace_replace_code(1);
a3583244 1580 else if (*command & FTRACE_DISABLE_CALLS)
d61f82d0
SR
1581 ftrace_replace_code(0);
1582
1583 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1584 ftrace_update_ftrace_func(ftrace_trace_function);
1585
5a45cfe1
SR
1586 if (*command & FTRACE_START_FUNC_RET)
1587 ftrace_enable_ftrace_graph_caller();
1588 else if (*command & FTRACE_STOP_FUNC_RET)
1589 ftrace_disable_ftrace_graph_caller();
1590
d61f82d0 1591 return 0;
3d083395
SR
1592}
1593
e309b41d 1594static void ftrace_run_update_code(int command)
3d083395 1595{
000ab691
SR
1596 int ret;
1597
1598 ret = ftrace_arch_code_modify_prepare();
1599 FTRACE_WARN_ON(ret);
1600 if (ret)
1601 return;
1602
784e2d76 1603 stop_machine(__ftrace_modify_code, &command, NULL);
000ab691
SR
1604
1605 ret = ftrace_arch_code_modify_post_process();
1606 FTRACE_WARN_ON(ret);
3d083395
SR
1607}
1608
d61f82d0 1609static ftrace_func_t saved_ftrace_func;
60a7ecf4 1610static int ftrace_start_up;
b848914c 1611static int global_start_up;
df4fc315
SR
1612
1613static void ftrace_startup_enable(int command)
1614{
1615 if (saved_ftrace_func != ftrace_trace_function) {
1616 saved_ftrace_func = ftrace_trace_function;
1617 command |= FTRACE_UPDATE_TRACE_FUNC;
1618 }
1619
1620 if (!command || !ftrace_enabled)
1621 return;
1622
1623 ftrace_run_update_code(command);
1624}
d61f82d0 1625
bd69c30b 1626static void ftrace_startup(struct ftrace_ops *ops, int command)
3d083395 1627{
b848914c
SR
1628 bool hash_enable = true;
1629
4eebcc81
SR
1630 if (unlikely(ftrace_disabled))
1631 return;
1632
60a7ecf4 1633 ftrace_start_up++;
982c350b 1634 command |= FTRACE_ENABLE_CALLS;
d61f82d0 1635
b848914c
SR
1636 /* ops marked global share the filter hashes */
1637 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
1638 ops = &global_ops;
1639 /* Don't update hash if global is already set */
1640 if (global_start_up)
1641 hash_enable = false;
1642 global_start_up++;
1643 }
1644
ed926f9b 1645 ops->flags |= FTRACE_OPS_FL_ENABLED;
b848914c 1646 if (hash_enable)
ed926f9b
SR
1647 ftrace_hash_rec_enable(ops, 1);
1648
df4fc315 1649 ftrace_startup_enable(command);
3d083395
SR
1650}
1651
bd69c30b 1652static void ftrace_shutdown(struct ftrace_ops *ops, int command)
3d083395 1653{
b848914c
SR
1654 bool hash_disable = true;
1655
4eebcc81
SR
1656 if (unlikely(ftrace_disabled))
1657 return;
1658
60a7ecf4 1659 ftrace_start_up--;
9ea1a153
FW
1660 /*
1661 * Just warn in case of unbalance, no need to kill ftrace, it's not
1662 * critical but the ftrace_call callers may be never nopped again after
1663 * further ftrace uses.
1664 */
1665 WARN_ON_ONCE(ftrace_start_up < 0);
1666
b848914c
SR
1667 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
1668 ops = &global_ops;
1669 global_start_up--;
1670 WARN_ON_ONCE(global_start_up < 0);
1671 /* Don't update hash if global still has users */
1672 if (global_start_up) {
1673 WARN_ON_ONCE(!ftrace_start_up);
1674 hash_disable = false;
1675 }
1676 }
1677
1678 if (hash_disable)
ed926f9b
SR
1679 ftrace_hash_rec_disable(ops, 1);
1680
b848914c 1681 if (ops != &global_ops || !global_start_up)
ed926f9b 1682 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
b848914c
SR
1683
1684 if (!ftrace_start_up)
1685 command |= FTRACE_DISABLE_CALLS;
3d083395 1686
d61f82d0
SR
1687 if (saved_ftrace_func != ftrace_trace_function) {
1688 saved_ftrace_func = ftrace_trace_function;
1689 command |= FTRACE_UPDATE_TRACE_FUNC;
1690 }
3d083395 1691
d61f82d0 1692 if (!command || !ftrace_enabled)
e6ea44e9 1693 return;
d61f82d0
SR
1694
1695 ftrace_run_update_code(command);
3d083395
SR
1696}
1697
e309b41d 1698static void ftrace_startup_sysctl(void)
b0fc494f 1699{
4eebcc81
SR
1700 if (unlikely(ftrace_disabled))
1701 return;
1702
d61f82d0
SR
1703 /* Force update next time */
1704 saved_ftrace_func = NULL;
60a7ecf4
SR
1705 /* ftrace_start_up is true if we want ftrace running */
1706 if (ftrace_start_up)
79e406d7 1707 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
b0fc494f
SR
1708}
1709
e309b41d 1710static void ftrace_shutdown_sysctl(void)
b0fc494f 1711{
4eebcc81
SR
1712 if (unlikely(ftrace_disabled))
1713 return;
1714
60a7ecf4
SR
1715 /* ftrace_start_up is true if ftrace is running */
1716 if (ftrace_start_up)
79e406d7 1717 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
b0fc494f
SR
1718}
1719
3d083395
SR
1720static cycle_t ftrace_update_time;
1721static unsigned long ftrace_update_cnt;
1722unsigned long ftrace_update_tot_cnt;
1723
31e88909 1724static int ftrace_update_code(struct module *mod)
3d083395 1725{
e94142a6 1726 struct dyn_ftrace *p;
f22f9a89 1727 cycle_t start, stop;
3d083395 1728
750ed1a4 1729 start = ftrace_now(raw_smp_processor_id());
3d083395
SR
1730 ftrace_update_cnt = 0;
1731
e94142a6 1732 while (ftrace_new_addrs) {
3d083395 1733
08f5ac90
SR
1734 /* If something went wrong, bail without enabling anything */
1735 if (unlikely(ftrace_disabled))
1736 return -1;
f22f9a89 1737
e94142a6 1738 p = ftrace_new_addrs;
ee000b7f 1739 ftrace_new_addrs = p->newlist;
e94142a6 1740 p->flags = 0L;
f22f9a89 1741
5cb084bb 1742 /*
25985edc 1743 * Do the initial record conversion from mcount jump
5cb084bb
JO
1744 * to the NOP instructions.
1745 */
1746 if (!ftrace_code_disable(mod, p)) {
08f5ac90 1747 ftrace_free_rec(p);
d2c8c3ea
SR
1748 /* Game over */
1749 break;
5cb084bb
JO
1750 }
1751
5cb084bb
JO
1752 ftrace_update_cnt++;
1753
1754 /*
1755 * If the tracing is enabled, go ahead and enable the record.
1756 *
1757 * The reason not to enable the record immediatelly is the
1758 * inherent check of ftrace_make_nop/ftrace_make_call for
1759 * correct previous instructions. Making first the NOP
1760 * conversion puts the module to the correct state, thus
1761 * passing the ftrace_make_call check.
1762 */
1763 if (ftrace_start_up) {
1764 int failed = __ftrace_replace_code(p, 1);
1765 if (failed) {
1766 ftrace_bug(failed, p->ip);
1767 ftrace_free_rec(p);
1768 }
1769 }
3d083395
SR
1770 }
1771
750ed1a4 1772 stop = ftrace_now(raw_smp_processor_id());
3d083395
SR
1773 ftrace_update_time = stop - start;
1774 ftrace_update_tot_cnt += ftrace_update_cnt;
1775
16444a8a
ACM
1776 return 0;
1777}
1778
68bf21aa 1779static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
3c1720f0
SR
1780{
1781 struct ftrace_page *pg;
1782 int cnt;
1783 int i;
3c1720f0
SR
1784
1785 /* allocate a few pages */
1786 ftrace_pages_start = (void *)get_zeroed_page(GFP_KERNEL);
1787 if (!ftrace_pages_start)
1788 return -1;
1789
1790 /*
1791 * Allocate a few more pages.
1792 *
1793 * TODO: have some parser search vmlinux before
1794 * final linking to find all calls to ftrace.
1795 * Then we can:
1796 * a) know how many pages to allocate.
1797 * and/or
1798 * b) set up the table then.
1799 *
1800 * The dynamic code is still necessary for
1801 * modules.
1802 */
1803
1804 pg = ftrace_pages = ftrace_pages_start;
1805
68bf21aa 1806 cnt = num_to_init / ENTRIES_PER_PAGE;
08f5ac90 1807 pr_info("ftrace: allocating %ld entries in %d pages\n",
5821e1b7 1808 num_to_init, cnt + 1);
3c1720f0
SR
1809
1810 for (i = 0; i < cnt; i++) {
1811 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
1812
1813 /* If we fail, we'll try later anyway */
1814 if (!pg->next)
1815 break;
1816
1817 pg = pg->next;
1818 }
1819
1820 return 0;
1821}
1822
5072c59f
SR
1823enum {
1824 FTRACE_ITER_FILTER = (1 << 0),
689fd8b6 1825 FTRACE_ITER_NOTRACE = (1 << 1),
3499e461
SR
1826 FTRACE_ITER_PRINTALL = (1 << 2),
1827 FTRACE_ITER_HASH = (1 << 3),
647bcd03 1828 FTRACE_ITER_ENABLED = (1 << 4),
5072c59f
SR
1829};
1830
1831#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
1832
1833struct ftrace_iterator {
98c4fd04 1834 loff_t pos;
4aeb6967
SR
1835 loff_t func_pos;
1836 struct ftrace_page *pg;
1837 struct dyn_ftrace *func;
1838 struct ftrace_func_probe *probe;
1839 struct trace_parser parser;
1cf41dd7 1840 struct ftrace_hash *hash;
33dc9b12 1841 struct ftrace_ops *ops;
4aeb6967
SR
1842 int hidx;
1843 int idx;
1844 unsigned flags;
5072c59f
SR
1845};
1846
8fc0c701 1847static void *
4aeb6967 1848t_hash_next(struct seq_file *m, loff_t *pos)
8fc0c701
SR
1849{
1850 struct ftrace_iterator *iter = m->private;
4aeb6967 1851 struct hlist_node *hnd = NULL;
8fc0c701
SR
1852 struct hlist_head *hhd;
1853
8fc0c701 1854 (*pos)++;
98c4fd04 1855 iter->pos = *pos;
8fc0c701 1856
4aeb6967
SR
1857 if (iter->probe)
1858 hnd = &iter->probe->node;
8fc0c701
SR
1859 retry:
1860 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
1861 return NULL;
1862
1863 hhd = &ftrace_func_hash[iter->hidx];
1864
1865 if (hlist_empty(hhd)) {
1866 iter->hidx++;
1867 hnd = NULL;
1868 goto retry;
1869 }
1870
1871 if (!hnd)
1872 hnd = hhd->first;
1873 else {
1874 hnd = hnd->next;
1875 if (!hnd) {
1876 iter->hidx++;
1877 goto retry;
1878 }
1879 }
1880
4aeb6967
SR
1881 if (WARN_ON_ONCE(!hnd))
1882 return NULL;
1883
1884 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
1885
1886 return iter;
8fc0c701
SR
1887}
1888
1889static void *t_hash_start(struct seq_file *m, loff_t *pos)
1890{
1891 struct ftrace_iterator *iter = m->private;
1892 void *p = NULL;
d82d6244
LZ
1893 loff_t l;
1894
2bccfffd
SR
1895 if (iter->func_pos > *pos)
1896 return NULL;
8fc0c701 1897
d82d6244 1898 iter->hidx = 0;
2bccfffd 1899 for (l = 0; l <= (*pos - iter->func_pos); ) {
4aeb6967 1900 p = t_hash_next(m, &l);
d82d6244
LZ
1901 if (!p)
1902 break;
1903 }
4aeb6967
SR
1904 if (!p)
1905 return NULL;
1906
98c4fd04
SR
1907 /* Only set this if we have an item */
1908 iter->flags |= FTRACE_ITER_HASH;
1909
4aeb6967 1910 return iter;
8fc0c701
SR
1911}
1912
4aeb6967
SR
1913static int
1914t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
8fc0c701 1915{
b6887d79 1916 struct ftrace_func_probe *rec;
8fc0c701 1917
4aeb6967
SR
1918 rec = iter->probe;
1919 if (WARN_ON_ONCE(!rec))
1920 return -EIO;
8fc0c701 1921
809dcf29
SR
1922 if (rec->ops->print)
1923 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
1924
b375a11a 1925 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
8fc0c701
SR
1926
1927 if (rec->data)
1928 seq_printf(m, ":%p", rec->data);
1929 seq_putc(m, '\n');
1930
1931 return 0;
1932}
1933
e309b41d 1934static void *
5072c59f
SR
1935t_next(struct seq_file *m, void *v, loff_t *pos)
1936{
1937 struct ftrace_iterator *iter = m->private;
f45948e8 1938 struct ftrace_ops *ops = &global_ops;
5072c59f
SR
1939 struct dyn_ftrace *rec = NULL;
1940
45a4a237
SR
1941 if (unlikely(ftrace_disabled))
1942 return NULL;
1943
8fc0c701 1944 if (iter->flags & FTRACE_ITER_HASH)
4aeb6967 1945 return t_hash_next(m, pos);
8fc0c701 1946
5072c59f 1947 (*pos)++;
1106b699 1948 iter->pos = iter->func_pos = *pos;
5072c59f 1949
0c75a3ed 1950 if (iter->flags & FTRACE_ITER_PRINTALL)
57c072c7 1951 return t_hash_start(m, pos);
0c75a3ed 1952
5072c59f
SR
1953 retry:
1954 if (iter->idx >= iter->pg->index) {
1955 if (iter->pg->next) {
1956 iter->pg = iter->pg->next;
1957 iter->idx = 0;
1958 goto retry;
1959 }
1960 } else {
1961 rec = &iter->pg->records[iter->idx++];
a9fdda33
SR
1962 if ((rec->flags & FTRACE_FL_FREE) ||
1963
0183fb1c 1964 ((iter->flags & FTRACE_ITER_FILTER) &&
f45948e8 1965 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
0183fb1c 1966
41c52c0d 1967 ((iter->flags & FTRACE_ITER_NOTRACE) &&
647bcd03
SR
1968 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
1969
1970 ((iter->flags & FTRACE_ITER_ENABLED) &&
1971 !(rec->flags & ~FTRACE_FL_MASK))) {
1972
5072c59f
SR
1973 rec = NULL;
1974 goto retry;
1975 }
1976 }
1977
4aeb6967 1978 if (!rec)
57c072c7 1979 return t_hash_start(m, pos);
4aeb6967
SR
1980
1981 iter->func = rec;
1982
1983 return iter;
5072c59f
SR
1984}
1985
98c4fd04
SR
1986static void reset_iter_read(struct ftrace_iterator *iter)
1987{
1988 iter->pos = 0;
1989 iter->func_pos = 0;
1990 iter->flags &= ~(FTRACE_ITER_PRINTALL & FTRACE_ITER_HASH);
5072c59f
SR
1991}
1992
1993static void *t_start(struct seq_file *m, loff_t *pos)
1994{
1995 struct ftrace_iterator *iter = m->private;
f45948e8 1996 struct ftrace_ops *ops = &global_ops;
5072c59f 1997 void *p = NULL;
694ce0a5 1998 loff_t l;
5072c59f 1999
8fc0c701 2000 mutex_lock(&ftrace_lock);
45a4a237
SR
2001
2002 if (unlikely(ftrace_disabled))
2003 return NULL;
2004
98c4fd04
SR
2005 /*
2006 * If an lseek was done, then reset and start from beginning.
2007 */
2008 if (*pos < iter->pos)
2009 reset_iter_read(iter);
2010
0c75a3ed
SR
2011 /*
2012 * For set_ftrace_filter reading, if we have the filter
2013 * off, we can short cut and just print out that all
2014 * functions are enabled.
2015 */
f45948e8 2016 if (iter->flags & FTRACE_ITER_FILTER && !ops->filter_hash->count) {
0c75a3ed 2017 if (*pos > 0)
8fc0c701 2018 return t_hash_start(m, pos);
0c75a3ed 2019 iter->flags |= FTRACE_ITER_PRINTALL;
df091625
CW
2020 /* reset in case of seek/pread */
2021 iter->flags &= ~FTRACE_ITER_HASH;
0c75a3ed
SR
2022 return iter;
2023 }
2024
8fc0c701
SR
2025 if (iter->flags & FTRACE_ITER_HASH)
2026 return t_hash_start(m, pos);
2027
98c4fd04
SR
2028 /*
2029 * Unfortunately, we need to restart at ftrace_pages_start
2030 * every time we let go of the ftrace_mutex. This is because
2031 * those pointers can change without the lock.
2032 */
694ce0a5
LZ
2033 iter->pg = ftrace_pages_start;
2034 iter->idx = 0;
2035 for (l = 0; l <= *pos; ) {
2036 p = t_next(m, p, &l);
2037 if (!p)
2038 break;
50cdaf08 2039 }
5821e1b7 2040
4aeb6967
SR
2041 if (!p) {
2042 if (iter->flags & FTRACE_ITER_FILTER)
2043 return t_hash_start(m, pos);
8fc0c701 2044
4aeb6967
SR
2045 return NULL;
2046 }
2047
2048 return iter;
5072c59f
SR
2049}
2050
2051static void t_stop(struct seq_file *m, void *p)
2052{
8fc0c701 2053 mutex_unlock(&ftrace_lock);
5072c59f
SR
2054}
2055
2056static int t_show(struct seq_file *m, void *v)
2057{
0c75a3ed 2058 struct ftrace_iterator *iter = m->private;
4aeb6967 2059 struct dyn_ftrace *rec;
5072c59f 2060
8fc0c701 2061 if (iter->flags & FTRACE_ITER_HASH)
4aeb6967 2062 return t_hash_show(m, iter);
8fc0c701 2063
0c75a3ed
SR
2064 if (iter->flags & FTRACE_ITER_PRINTALL) {
2065 seq_printf(m, "#### all functions enabled ####\n");
2066 return 0;
2067 }
2068
4aeb6967
SR
2069 rec = iter->func;
2070
5072c59f
SR
2071 if (!rec)
2072 return 0;
2073
647bcd03
SR
2074 seq_printf(m, "%ps", (void *)rec->ip);
2075 if (iter->flags & FTRACE_ITER_ENABLED)
2076 seq_printf(m, " (%ld)",
2077 rec->flags & ~FTRACE_FL_MASK);
2078 seq_printf(m, "\n");
5072c59f
SR
2079
2080 return 0;
2081}
2082
88e9d34c 2083static const struct seq_operations show_ftrace_seq_ops = {
5072c59f
SR
2084 .start = t_start,
2085 .next = t_next,
2086 .stop = t_stop,
2087 .show = t_show,
2088};
2089
e309b41d 2090static int
5072c59f
SR
2091ftrace_avail_open(struct inode *inode, struct file *file)
2092{
2093 struct ftrace_iterator *iter;
2094 int ret;
2095
4eebcc81
SR
2096 if (unlikely(ftrace_disabled))
2097 return -ENODEV;
2098
5072c59f
SR
2099 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2100 if (!iter)
2101 return -ENOMEM;
2102
2103 iter->pg = ftrace_pages_start;
5072c59f
SR
2104
2105 ret = seq_open(file, &show_ftrace_seq_ops);
2106 if (!ret) {
2107 struct seq_file *m = file->private_data;
4bf39a94 2108
5072c59f 2109 m->private = iter;
4bf39a94 2110 } else {
5072c59f 2111 kfree(iter);
4bf39a94 2112 }
5072c59f
SR
2113
2114 return ret;
2115}
2116
647bcd03
SR
2117static int
2118ftrace_enabled_open(struct inode *inode, struct file *file)
2119{
2120 struct ftrace_iterator *iter;
2121 int ret;
2122
2123 if (unlikely(ftrace_disabled))
2124 return -ENODEV;
2125
2126 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2127 if (!iter)
2128 return -ENOMEM;
2129
2130 iter->pg = ftrace_pages_start;
2131 iter->flags = FTRACE_ITER_ENABLED;
2132
2133 ret = seq_open(file, &show_ftrace_seq_ops);
2134 if (!ret) {
2135 struct seq_file *m = file->private_data;
2136
2137 m->private = iter;
2138 } else {
2139 kfree(iter);
2140 }
2141
2142 return ret;
2143}
2144
1cf41dd7 2145static void ftrace_filter_reset(struct ftrace_hash *hash)
5072c59f 2146{
52baf119 2147 mutex_lock(&ftrace_lock);
1cf41dd7 2148 ftrace_hash_clear(hash);
52baf119 2149 mutex_unlock(&ftrace_lock);
5072c59f
SR
2150}
2151
e309b41d 2152static int
f45948e8 2153ftrace_regex_open(struct ftrace_ops *ops, int flag,
1cf41dd7 2154 struct inode *inode, struct file *file)
5072c59f
SR
2155{
2156 struct ftrace_iterator *iter;
f45948e8 2157 struct ftrace_hash *hash;
5072c59f
SR
2158 int ret = 0;
2159
4eebcc81
SR
2160 if (unlikely(ftrace_disabled))
2161 return -ENODEV;
2162
5072c59f
SR
2163 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2164 if (!iter)
2165 return -ENOMEM;
2166
689fd8b6 2167 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2168 kfree(iter);
2169 return -ENOMEM;
2170 }
2171
f45948e8
SR
2172 if (flag & FTRACE_ITER_NOTRACE)
2173 hash = ops->notrace_hash;
2174 else
2175 hash = ops->filter_hash;
2176
33dc9b12
SR
2177 iter->ops = ops;
2178 iter->flags = flag;
2179
2180 if (file->f_mode & FMODE_WRITE) {
2181 mutex_lock(&ftrace_lock);
2182 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2183 mutex_unlock(&ftrace_lock);
2184
2185 if (!iter->hash) {
2186 trace_parser_put(&iter->parser);
2187 kfree(iter);
2188 return -ENOMEM;
2189 }
2190 }
1cf41dd7 2191
41c52c0d 2192 mutex_lock(&ftrace_regex_lock);
33dc9b12 2193
5072c59f 2194 if ((file->f_mode & FMODE_WRITE) &&
8650ae32 2195 (file->f_flags & O_TRUNC))
33dc9b12 2196 ftrace_filter_reset(iter->hash);
5072c59f
SR
2197
2198 if (file->f_mode & FMODE_READ) {
2199 iter->pg = ftrace_pages_start;
5072c59f
SR
2200
2201 ret = seq_open(file, &show_ftrace_seq_ops);
2202 if (!ret) {
2203 struct seq_file *m = file->private_data;
2204 m->private = iter;
79fe249c 2205 } else {
33dc9b12
SR
2206 /* Failed */
2207 free_ftrace_hash(iter->hash);
79fe249c 2208 trace_parser_put(&iter->parser);
5072c59f 2209 kfree(iter);
79fe249c 2210 }
5072c59f
SR
2211 } else
2212 file->private_data = iter;
41c52c0d 2213 mutex_unlock(&ftrace_regex_lock);
5072c59f
SR
2214
2215 return ret;
2216}
2217
41c52c0d
SR
2218static int
2219ftrace_filter_open(struct inode *inode, struct file *file)
2220{
f45948e8 2221 return ftrace_regex_open(&global_ops, FTRACE_ITER_FILTER,
1cf41dd7 2222 inode, file);
41c52c0d
SR
2223}
2224
2225static int
2226ftrace_notrace_open(struct inode *inode, struct file *file)
2227{
f45948e8 2228 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
1cf41dd7 2229 inode, file);
41c52c0d
SR
2230}
2231
e309b41d 2232static loff_t
41c52c0d 2233ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
5072c59f
SR
2234{
2235 loff_t ret;
2236
2237 if (file->f_mode & FMODE_READ)
2238 ret = seq_lseek(file, offset, origin);
2239 else
2240 file->f_pos = ret = 1;
2241
2242 return ret;
2243}
2244
64e7c440 2245static int ftrace_match(char *str, char *regex, int len, int type)
9f4801e3 2246{
9f4801e3 2247 int matched = 0;
751e9983 2248 int slen;
9f4801e3 2249
9f4801e3
SR
2250 switch (type) {
2251 case MATCH_FULL:
2252 if (strcmp(str, regex) == 0)
2253 matched = 1;
2254 break;
2255 case MATCH_FRONT_ONLY:
2256 if (strncmp(str, regex, len) == 0)
2257 matched = 1;
2258 break;
2259 case MATCH_MIDDLE_ONLY:
2260 if (strstr(str, regex))
2261 matched = 1;
2262 break;
2263 case MATCH_END_ONLY:
751e9983
LZ
2264 slen = strlen(str);
2265 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
9f4801e3
SR
2266 matched = 1;
2267 break;
2268 }
2269
2270 return matched;
2271}
2272
b448c4e3 2273static int
1cf41dd7 2274enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
996e87be 2275{
b448c4e3 2276 struct ftrace_func_entry *entry;
b448c4e3
SR
2277 int ret = 0;
2278
1cf41dd7
SR
2279 entry = ftrace_lookup_ip(hash, rec->ip);
2280 if (not) {
2281 /* Do nothing if it doesn't exist */
2282 if (!entry)
2283 return 0;
b448c4e3 2284
33dc9b12 2285 free_hash_entry(hash, entry);
1cf41dd7
SR
2286 } else {
2287 /* Do nothing if it exists */
2288 if (entry)
2289 return 0;
b448c4e3 2290
1cf41dd7 2291 ret = add_hash_entry(hash, rec->ip);
b448c4e3
SR
2292 }
2293 return ret;
996e87be
SR
2294}
2295
64e7c440 2296static int
b9df92d2
SR
2297ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2298 char *regex, int len, int type)
64e7c440
SR
2299{
2300 char str[KSYM_SYMBOL_LEN];
b9df92d2
SR
2301 char *modname;
2302
2303 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2304
2305 if (mod) {
2306 /* module lookup requires matching the module */
2307 if (!modname || strcmp(modname, mod))
2308 return 0;
2309
2310 /* blank search means to match all funcs in the mod */
2311 if (!len)
2312 return 1;
2313 }
64e7c440 2314
64e7c440
SR
2315 return ftrace_match(str, regex, len, type);
2316}
2317
1cf41dd7
SR
2318static int
2319match_records(struct ftrace_hash *hash, char *buff,
2320 int len, char *mod, int not)
9f4801e3 2321{
b9df92d2 2322 unsigned search_len = 0;
9f4801e3
SR
2323 struct ftrace_page *pg;
2324 struct dyn_ftrace *rec;
b9df92d2
SR
2325 int type = MATCH_FULL;
2326 char *search = buff;
311d16da 2327 int found = 0;
b448c4e3 2328 int ret;
9f4801e3 2329
b9df92d2
SR
2330 if (len) {
2331 type = filter_parse_regex(buff, len, &search, &not);
2332 search_len = strlen(search);
2333 }
9f4801e3 2334
52baf119 2335 mutex_lock(&ftrace_lock);
265c831c 2336
b9df92d2
SR
2337 if (unlikely(ftrace_disabled))
2338 goto out_unlock;
9f4801e3 2339
265c831c 2340 do_for_each_ftrace_rec(pg, rec) {
265c831c 2341
b9df92d2 2342 if (ftrace_match_record(rec, mod, search, search_len, type)) {
1cf41dd7 2343 ret = enter_record(hash, rec, not);
b448c4e3
SR
2344 if (ret < 0) {
2345 found = ret;
2346 goto out_unlock;
2347 }
311d16da 2348 found = 1;
265c831c
SR
2349 }
2350 } while_for_each_ftrace_rec();
b9df92d2 2351 out_unlock:
52baf119 2352 mutex_unlock(&ftrace_lock);
311d16da
LZ
2353
2354 return found;
5072c59f
SR
2355}
2356
64e7c440 2357static int
1cf41dd7 2358ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
64e7c440 2359{
1cf41dd7 2360 return match_records(hash, buff, len, NULL, 0);
64e7c440
SR
2361}
2362
1cf41dd7
SR
2363static int
2364ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
64e7c440 2365{
64e7c440 2366 int not = 0;
6a24a244 2367
64e7c440
SR
2368 /* blank or '*' mean the same */
2369 if (strcmp(buff, "*") == 0)
2370 buff[0] = 0;
2371
2372 /* handle the case of 'dont filter this module' */
2373 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2374 buff[0] = 0;
2375 not = 1;
2376 }
2377
1cf41dd7 2378 return match_records(hash, buff, strlen(buff), mod, not);
64e7c440
SR
2379}
2380
f6180773
SR
2381/*
2382 * We register the module command as a template to show others how
2383 * to register the a command as well.
2384 */
2385
2386static int
2387ftrace_mod_callback(char *func, char *cmd, char *param, int enable)
2388{
f45948e8 2389 struct ftrace_ops *ops = &global_ops;
1cf41dd7 2390 struct ftrace_hash *hash;
f6180773 2391 char *mod;
b448c4e3 2392 int ret = -EINVAL;
f6180773
SR
2393
2394 /*
2395 * cmd == 'mod' because we only registered this func
2396 * for the 'mod' ftrace_func_command.
2397 * But if you register one func with multiple commands,
2398 * you can tell which command was used by the cmd
2399 * parameter.
2400 */
2401
2402 /* we must have a module name */
2403 if (!param)
b448c4e3 2404 return ret;
f6180773
SR
2405
2406 mod = strsep(&param, ":");
2407 if (!strlen(mod))
b448c4e3 2408 return ret;
f6180773 2409
1cf41dd7 2410 if (enable)
f45948e8 2411 hash = ops->filter_hash;
1cf41dd7 2412 else
f45948e8 2413 hash = ops->notrace_hash;
1cf41dd7
SR
2414
2415 ret = ftrace_match_module_records(hash, func, mod);
b448c4e3
SR
2416 if (!ret)
2417 ret = -EINVAL;
2418 if (ret < 0)
2419 return ret;
2420
2421 return 0;
f6180773
SR
2422}
2423
2424static struct ftrace_func_command ftrace_mod_cmd = {
2425 .name = "mod",
2426 .func = ftrace_mod_callback,
2427};
2428
2429static int __init ftrace_mod_cmd_init(void)
2430{
2431 return register_ftrace_command(&ftrace_mod_cmd);
2432}
2433device_initcall(ftrace_mod_cmd_init);
2434
59df055f 2435static void
b6887d79 2436function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
59df055f 2437{
b6887d79 2438 struct ftrace_func_probe *entry;
59df055f
SR
2439 struct hlist_head *hhd;
2440 struct hlist_node *n;
2441 unsigned long key;
59df055f
SR
2442
2443 key = hash_long(ip, FTRACE_HASH_BITS);
2444
2445 hhd = &ftrace_func_hash[key];
2446
2447 if (hlist_empty(hhd))
2448 return;
2449
2450 /*
2451 * Disable preemption for these calls to prevent a RCU grace
2452 * period. This syncs the hash iteration and freeing of items
2453 * on the hash. rcu_read_lock is too dangerous here.
2454 */
5168ae50 2455 preempt_disable_notrace();
59df055f
SR
2456 hlist_for_each_entry_rcu(entry, n, hhd, node) {
2457 if (entry->ip == ip)
2458 entry->ops->func(ip, parent_ip, &entry->data);
2459 }
5168ae50 2460 preempt_enable_notrace();
59df055f
SR
2461}
2462
b6887d79 2463static struct ftrace_ops trace_probe_ops __read_mostly =
59df055f 2464{
fb9fb015 2465 .func = function_trace_probe_call,
59df055f
SR
2466};
2467
b6887d79 2468static int ftrace_probe_registered;
59df055f 2469
b6887d79 2470static void __enable_ftrace_function_probe(void)
59df055f 2471{
b848914c 2472 int ret;
59df055f
SR
2473 int i;
2474
b6887d79 2475 if (ftrace_probe_registered)
59df055f
SR
2476 return;
2477
2478 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2479 struct hlist_head *hhd = &ftrace_func_hash[i];
2480 if (hhd->first)
2481 break;
2482 }
2483 /* Nothing registered? */
2484 if (i == FTRACE_FUNC_HASHSIZE)
2485 return;
2486
b848914c
SR
2487 ret = __register_ftrace_function(&trace_probe_ops);
2488 if (!ret)
2489 ftrace_startup(&trace_probe_ops, 0);
2490
b6887d79 2491 ftrace_probe_registered = 1;
59df055f
SR
2492}
2493
b6887d79 2494static void __disable_ftrace_function_probe(void)
59df055f 2495{
b848914c 2496 int ret;
59df055f
SR
2497 int i;
2498
b6887d79 2499 if (!ftrace_probe_registered)
59df055f
SR
2500 return;
2501
2502 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2503 struct hlist_head *hhd = &ftrace_func_hash[i];
2504 if (hhd->first)
2505 return;
2506 }
2507
2508 /* no more funcs left */
b848914c
SR
2509 ret = __unregister_ftrace_function(&trace_probe_ops);
2510 if (!ret)
2511 ftrace_shutdown(&trace_probe_ops, 0);
2512
b6887d79 2513 ftrace_probe_registered = 0;
59df055f
SR
2514}
2515
2516
2517static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2518{
b6887d79
SR
2519 struct ftrace_func_probe *entry =
2520 container_of(rhp, struct ftrace_func_probe, rcu);
59df055f
SR
2521
2522 if (entry->ops->free)
2523 entry->ops->free(&entry->data);
2524 kfree(entry);
2525}
2526
2527
2528int
b6887d79 2529register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
59df055f
SR
2530 void *data)
2531{
b6887d79 2532 struct ftrace_func_probe *entry;
59df055f
SR
2533 struct ftrace_page *pg;
2534 struct dyn_ftrace *rec;
59df055f 2535 int type, len, not;
6a24a244 2536 unsigned long key;
59df055f
SR
2537 int count = 0;
2538 char *search;
2539
3f6fe06d 2540 type = filter_parse_regex(glob, strlen(glob), &search, &not);
59df055f
SR
2541 len = strlen(search);
2542
b6887d79 2543 /* we do not support '!' for function probes */
59df055f
SR
2544 if (WARN_ON(not))
2545 return -EINVAL;
2546
2547 mutex_lock(&ftrace_lock);
59df055f 2548
45a4a237
SR
2549 if (unlikely(ftrace_disabled))
2550 goto out_unlock;
59df055f 2551
45a4a237 2552 do_for_each_ftrace_rec(pg, rec) {
59df055f 2553
b9df92d2 2554 if (!ftrace_match_record(rec, NULL, search, len, type))
59df055f
SR
2555 continue;
2556
2557 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2558 if (!entry) {
b6887d79 2559 /* If we did not process any, then return error */
59df055f
SR
2560 if (!count)
2561 count = -ENOMEM;
2562 goto out_unlock;
2563 }
2564
2565 count++;
2566
2567 entry->data = data;
2568
2569 /*
2570 * The caller might want to do something special
2571 * for each function we find. We call the callback
2572 * to give the caller an opportunity to do so.
2573 */
2574 if (ops->callback) {
2575 if (ops->callback(rec->ip, &entry->data) < 0) {
2576 /* caller does not like this func */
2577 kfree(entry);
2578 continue;
2579 }
2580 }
2581
2582 entry->ops = ops;
2583 entry->ip = rec->ip;
2584
2585 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2586 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2587
2588 } while_for_each_ftrace_rec();
b6887d79 2589 __enable_ftrace_function_probe();
59df055f
SR
2590
2591 out_unlock:
2592 mutex_unlock(&ftrace_lock);
2593
2594 return count;
2595}
2596
2597enum {
b6887d79
SR
2598 PROBE_TEST_FUNC = 1,
2599 PROBE_TEST_DATA = 2
59df055f
SR
2600};
2601
2602static void
b6887d79 2603__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
59df055f
SR
2604 void *data, int flags)
2605{
b6887d79 2606 struct ftrace_func_probe *entry;
59df055f
SR
2607 struct hlist_node *n, *tmp;
2608 char str[KSYM_SYMBOL_LEN];
2609 int type = MATCH_FULL;
2610 int i, len = 0;
2611 char *search;
2612
b36461da 2613 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
59df055f 2614 glob = NULL;
b36461da 2615 else if (glob) {
59df055f
SR
2616 int not;
2617
3f6fe06d 2618 type = filter_parse_regex(glob, strlen(glob), &search, &not);
59df055f
SR
2619 len = strlen(search);
2620
b6887d79 2621 /* we do not support '!' for function probes */
59df055f
SR
2622 if (WARN_ON(not))
2623 return;
2624 }
2625
2626 mutex_lock(&ftrace_lock);
2627 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2628 struct hlist_head *hhd = &ftrace_func_hash[i];
2629
2630 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
2631
2632 /* break up if statements for readability */
b6887d79 2633 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
59df055f
SR
2634 continue;
2635
b6887d79 2636 if ((flags & PROBE_TEST_DATA) && entry->data != data)
59df055f
SR
2637 continue;
2638
2639 /* do this last, since it is the most expensive */
2640 if (glob) {
2641 kallsyms_lookup(entry->ip, NULL, NULL,
2642 NULL, str);
2643 if (!ftrace_match(str, glob, len, type))
2644 continue;
2645 }
2646
2647 hlist_del(&entry->node);
2648 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
2649 }
2650 }
b6887d79 2651 __disable_ftrace_function_probe();
59df055f
SR
2652 mutex_unlock(&ftrace_lock);
2653}
2654
2655void
b6887d79 2656unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
59df055f
SR
2657 void *data)
2658{
b6887d79
SR
2659 __unregister_ftrace_function_probe(glob, ops, data,
2660 PROBE_TEST_FUNC | PROBE_TEST_DATA);
59df055f
SR
2661}
2662
2663void
b6887d79 2664unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
59df055f 2665{
b6887d79 2666 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
59df055f
SR
2667}
2668
b6887d79 2669void unregister_ftrace_function_probe_all(char *glob)
59df055f 2670{
b6887d79 2671 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
59df055f
SR
2672}
2673
f6180773
SR
2674static LIST_HEAD(ftrace_commands);
2675static DEFINE_MUTEX(ftrace_cmd_mutex);
2676
2677int register_ftrace_command(struct ftrace_func_command *cmd)
2678{
2679 struct ftrace_func_command *p;
2680 int ret = 0;
2681
2682 mutex_lock(&ftrace_cmd_mutex);
2683 list_for_each_entry(p, &ftrace_commands, list) {
2684 if (strcmp(cmd->name, p->name) == 0) {
2685 ret = -EBUSY;
2686 goto out_unlock;
2687 }
2688 }
2689 list_add(&cmd->list, &ftrace_commands);
2690 out_unlock:
2691 mutex_unlock(&ftrace_cmd_mutex);
2692
2693 return ret;
2694}
2695
2696int unregister_ftrace_command(struct ftrace_func_command *cmd)
2697{
2698 struct ftrace_func_command *p, *n;
2699 int ret = -ENODEV;
2700
2701 mutex_lock(&ftrace_cmd_mutex);
2702 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
2703 if (strcmp(cmd->name, p->name) == 0) {
2704 ret = 0;
2705 list_del_init(&p->list);
2706 goto out_unlock;
2707 }
2708 }
2709 out_unlock:
2710 mutex_unlock(&ftrace_cmd_mutex);
2711
2712 return ret;
2713}
2714
33dc9b12
SR
2715static int ftrace_process_regex(struct ftrace_hash *hash,
2716 char *buff, int len, int enable)
64e7c440 2717{
f6180773 2718 char *func, *command, *next = buff;
6a24a244 2719 struct ftrace_func_command *p;
b448c4e3 2720 int ret;
64e7c440
SR
2721
2722 func = strsep(&next, ":");
2723
2724 if (!next) {
1cf41dd7 2725 ret = ftrace_match_records(hash, func, len);
b448c4e3
SR
2726 if (!ret)
2727 ret = -EINVAL;
2728 if (ret < 0)
2729 return ret;
2730 return 0;
64e7c440
SR
2731 }
2732
f6180773 2733 /* command found */
64e7c440
SR
2734
2735 command = strsep(&next, ":");
2736
f6180773
SR
2737 mutex_lock(&ftrace_cmd_mutex);
2738 list_for_each_entry(p, &ftrace_commands, list) {
2739 if (strcmp(p->name, command) == 0) {
2740 ret = p->func(func, command, next, enable);
2741 goto out_unlock;
2742 }
64e7c440 2743 }
f6180773
SR
2744 out_unlock:
2745 mutex_unlock(&ftrace_cmd_mutex);
64e7c440 2746
f6180773 2747 return ret;
64e7c440
SR
2748}
2749
e309b41d 2750static ssize_t
41c52c0d
SR
2751ftrace_regex_write(struct file *file, const char __user *ubuf,
2752 size_t cnt, loff_t *ppos, int enable)
5072c59f
SR
2753{
2754 struct ftrace_iterator *iter;
689fd8b6 2755 struct trace_parser *parser;
2756 ssize_t ret, read;
5072c59f 2757
4ba7978e 2758 if (!cnt)
5072c59f
SR
2759 return 0;
2760
41c52c0d 2761 mutex_lock(&ftrace_regex_lock);
5072c59f 2762
45a4a237
SR
2763 ret = -ENODEV;
2764 if (unlikely(ftrace_disabled))
2765 goto out_unlock;
2766
5072c59f
SR
2767 if (file->f_mode & FMODE_READ) {
2768 struct seq_file *m = file->private_data;
2769 iter = m->private;
2770 } else
2771 iter = file->private_data;
2772
689fd8b6 2773 parser = &iter->parser;
2774 read = trace_get_user(parser, ubuf, cnt, ppos);
5072c59f 2775
4ba7978e 2776 if (read >= 0 && trace_parser_loaded(parser) &&
689fd8b6 2777 !trace_parser_cont(parser)) {
33dc9b12 2778 ret = ftrace_process_regex(iter->hash, parser->buffer,
689fd8b6 2779 parser->idx, enable);
313254a9 2780 trace_parser_clear(parser);
5072c59f 2781 if (ret)
ed146b25 2782 goto out_unlock;
eda1e328 2783 }
5072c59f 2784
5072c59f 2785 ret = read;
ed146b25 2786out_unlock:
689fd8b6 2787 mutex_unlock(&ftrace_regex_lock);
ed146b25 2788
5072c59f
SR
2789 return ret;
2790}
2791
41c52c0d
SR
2792static ssize_t
2793ftrace_filter_write(struct file *file, const char __user *ubuf,
2794 size_t cnt, loff_t *ppos)
2795{
2796 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
2797}
2798
2799static ssize_t
2800ftrace_notrace_write(struct file *file, const char __user *ubuf,
2801 size_t cnt, loff_t *ppos)
2802{
2803 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
2804}
2805
33dc9b12 2806static int
f45948e8
SR
2807ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
2808 int reset, int enable)
41c52c0d 2809{
33dc9b12 2810 struct ftrace_hash **orig_hash;
f45948e8 2811 struct ftrace_hash *hash;
33dc9b12 2812 int ret;
f45948e8 2813
41c52c0d 2814 if (unlikely(ftrace_disabled))
33dc9b12 2815 return -ENODEV;
41c52c0d 2816
f45948e8 2817 if (enable)
33dc9b12 2818 orig_hash = &ops->filter_hash;
f45948e8 2819 else
33dc9b12
SR
2820 orig_hash = &ops->notrace_hash;
2821
2822 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
2823 if (!hash)
2824 return -ENOMEM;
f45948e8 2825
41c52c0d
SR
2826 mutex_lock(&ftrace_regex_lock);
2827 if (reset)
1cf41dd7 2828 ftrace_filter_reset(hash);
41c52c0d 2829 if (buf)
1cf41dd7 2830 ftrace_match_records(hash, buf, len);
33dc9b12
SR
2831
2832 mutex_lock(&ftrace_lock);
2833 ret = ftrace_hash_move(orig_hash, hash);
2834 mutex_unlock(&ftrace_lock);
2835
41c52c0d 2836 mutex_unlock(&ftrace_regex_lock);
33dc9b12
SR
2837
2838 free_ftrace_hash(hash);
2839 return ret;
41c52c0d
SR
2840}
2841
77a2b37d
SR
2842/**
2843 * ftrace_set_filter - set a function to filter on in ftrace
2844 * @buf - the string that holds the function filter text.
2845 * @len - the length of the string.
2846 * @reset - non zero to reset all filters before applying this filter.
2847 *
2848 * Filters denote which functions should be enabled when tracing is enabled.
2849 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
2850 */
e309b41d 2851void ftrace_set_filter(unsigned char *buf, int len, int reset)
77a2b37d 2852{
f45948e8 2853 ftrace_set_regex(&global_ops, buf, len, reset, 1);
41c52c0d 2854}
4eebcc81 2855
41c52c0d
SR
2856/**
2857 * ftrace_set_notrace - set a function to not trace in ftrace
2858 * @buf - the string that holds the function notrace text.
2859 * @len - the length of the string.
2860 * @reset - non zero to reset all filters before applying this filter.
2861 *
2862 * Notrace Filters denote which functions should not be enabled when tracing
2863 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
2864 * for tracing.
2865 */
2866void ftrace_set_notrace(unsigned char *buf, int len, int reset)
2867{
f45948e8 2868 ftrace_set_regex(&global_ops, buf, len, reset, 0);
77a2b37d
SR
2869}
2870
2af15d6a
SR
2871/*
2872 * command line interface to allow users to set filters on boot up.
2873 */
2874#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
2875static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
2876static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
2877
2878static int __init set_ftrace_notrace(char *str)
2879{
2880 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
2881 return 1;
2882}
2883__setup("ftrace_notrace=", set_ftrace_notrace);
2884
2885static int __init set_ftrace_filter(char *str)
2886{
2887 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
2888 return 1;
2889}
2890__setup("ftrace_filter=", set_ftrace_filter);
2891
369bc18f 2892#ifdef CONFIG_FUNCTION_GRAPH_TRACER
f6060f46 2893static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
801c29fd
SR
2894static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
2895
369bc18f
SA
2896static int __init set_graph_function(char *str)
2897{
06f43d66 2898 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
369bc18f
SA
2899 return 1;
2900}
2901__setup("ftrace_graph_filter=", set_graph_function);
2902
2903static void __init set_ftrace_early_graph(char *buf)
2904{
2905 int ret;
2906 char *func;
2907
2908 while (buf) {
2909 func = strsep(&buf, ",");
2910 /* we allow only one expression at a time */
2911 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
2912 func);
2913 if (ret)
2914 printk(KERN_DEBUG "ftrace: function %s not "
2915 "traceable\n", func);
2916 }
2917}
2918#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2919
f45948e8
SR
2920static void __init
2921set_ftrace_early_filter(struct ftrace_ops *ops, char *buf, int enable)
2af15d6a
SR
2922{
2923 char *func;
2924
2925 while (buf) {
2926 func = strsep(&buf, ",");
f45948e8 2927 ftrace_set_regex(ops, func, strlen(func), 0, enable);
2af15d6a
SR
2928 }
2929}
2930
2931static void __init set_ftrace_early_filters(void)
2932{
2933 if (ftrace_filter_buf[0])
f45948e8 2934 set_ftrace_early_filter(&global_ops, ftrace_filter_buf, 1);
2af15d6a 2935 if (ftrace_notrace_buf[0])
f45948e8 2936 set_ftrace_early_filter(&global_ops, ftrace_notrace_buf, 0);
369bc18f
SA
2937#ifdef CONFIG_FUNCTION_GRAPH_TRACER
2938 if (ftrace_graph_buf[0])
2939 set_ftrace_early_graph(ftrace_graph_buf);
2940#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
2af15d6a
SR
2941}
2942
e309b41d 2943static int
1cf41dd7 2944ftrace_regex_release(struct inode *inode, struct file *file)
5072c59f
SR
2945{
2946 struct seq_file *m = (struct seq_file *)file->private_data;
2947 struct ftrace_iterator *iter;
33dc9b12 2948 struct ftrace_hash **orig_hash;
689fd8b6 2949 struct trace_parser *parser;
ed926f9b 2950 int filter_hash;
33dc9b12 2951 int ret;
5072c59f 2952
41c52c0d 2953 mutex_lock(&ftrace_regex_lock);
5072c59f
SR
2954 if (file->f_mode & FMODE_READ) {
2955 iter = m->private;
2956
2957 seq_release(inode, file);
2958 } else
2959 iter = file->private_data;
2960
689fd8b6 2961 parser = &iter->parser;
2962 if (trace_parser_loaded(parser)) {
2963 parser->buffer[parser->idx] = 0;
1cf41dd7 2964 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
5072c59f
SR
2965 }
2966
689fd8b6 2967 trace_parser_put(parser);
689fd8b6 2968
058e297d 2969 if (file->f_mode & FMODE_WRITE) {
ed926f9b
SR
2970 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
2971
2972 if (filter_hash)
33dc9b12 2973 orig_hash = &iter->ops->filter_hash;
ed926f9b
SR
2974 else
2975 orig_hash = &iter->ops->notrace_hash;
33dc9b12 2976
058e297d 2977 mutex_lock(&ftrace_lock);
ed926f9b
SR
2978 /*
2979 * Remove the current set, update the hash and add
2980 * them back.
2981 */
2982 ftrace_hash_rec_disable(iter->ops, filter_hash);
33dc9b12 2983 ret = ftrace_hash_move(orig_hash, iter->hash);
ed926f9b
SR
2984 if (!ret) {
2985 ftrace_hash_rec_enable(iter->ops, filter_hash);
2986 if (iter->ops->flags & FTRACE_OPS_FL_ENABLED
2987 && ftrace_enabled)
2988 ftrace_run_update_code(FTRACE_ENABLE_CALLS);
2989 }
058e297d
SR
2990 mutex_unlock(&ftrace_lock);
2991 }
33dc9b12
SR
2992 free_ftrace_hash(iter->hash);
2993 kfree(iter);
058e297d 2994
41c52c0d 2995 mutex_unlock(&ftrace_regex_lock);
5072c59f
SR
2996 return 0;
2997}
2998
5e2336a0 2999static const struct file_operations ftrace_avail_fops = {
5072c59f
SR
3000 .open = ftrace_avail_open,
3001 .read = seq_read,
3002 .llseek = seq_lseek,
3be04b47 3003 .release = seq_release_private,
5072c59f
SR
3004};
3005
647bcd03
SR
3006static const struct file_operations ftrace_enabled_fops = {
3007 .open = ftrace_enabled_open,
3008 .read = seq_read,
3009 .llseek = seq_lseek,
3010 .release = seq_release_private,
3011};
3012
5e2336a0 3013static const struct file_operations ftrace_filter_fops = {
5072c59f 3014 .open = ftrace_filter_open,
850a80cf 3015 .read = seq_read,
5072c59f 3016 .write = ftrace_filter_write,
98c4fd04 3017 .llseek = ftrace_regex_lseek,
1cf41dd7 3018 .release = ftrace_regex_release,
5072c59f
SR
3019};
3020
5e2336a0 3021static const struct file_operations ftrace_notrace_fops = {
41c52c0d 3022 .open = ftrace_notrace_open,
850a80cf 3023 .read = seq_read,
41c52c0d
SR
3024 .write = ftrace_notrace_write,
3025 .llseek = ftrace_regex_lseek,
1cf41dd7 3026 .release = ftrace_regex_release,
41c52c0d
SR
3027};
3028
ea4e2bc4
SR
3029#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3030
3031static DEFINE_MUTEX(graph_lock);
3032
3033int ftrace_graph_count;
c7c6b1fe 3034int ftrace_graph_filter_enabled;
ea4e2bc4
SR
3035unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3036
3037static void *
85951842 3038__g_next(struct seq_file *m, loff_t *pos)
ea4e2bc4 3039{
85951842 3040 if (*pos >= ftrace_graph_count)
ea4e2bc4 3041 return NULL;
a4ec5e0c 3042 return &ftrace_graph_funcs[*pos];
85951842 3043}
ea4e2bc4 3044
85951842
LZ
3045static void *
3046g_next(struct seq_file *m, void *v, loff_t *pos)
3047{
3048 (*pos)++;
3049 return __g_next(m, pos);
ea4e2bc4
SR
3050}
3051
3052static void *g_start(struct seq_file *m, loff_t *pos)
3053{
ea4e2bc4
SR
3054 mutex_lock(&graph_lock);
3055
f9349a8f 3056 /* Nothing, tell g_show to print all functions are enabled */
c7c6b1fe 3057 if (!ftrace_graph_filter_enabled && !*pos)
f9349a8f
FW
3058 return (void *)1;
3059
85951842 3060 return __g_next(m, pos);
ea4e2bc4
SR
3061}
3062
3063static void g_stop(struct seq_file *m, void *p)
3064{
3065 mutex_unlock(&graph_lock);
3066}
3067
3068static int g_show(struct seq_file *m, void *v)
3069{
3070 unsigned long *ptr = v;
ea4e2bc4
SR
3071
3072 if (!ptr)
3073 return 0;
3074
f9349a8f
FW
3075 if (ptr == (unsigned long *)1) {
3076 seq_printf(m, "#### all functions enabled ####\n");
3077 return 0;
3078 }
3079
b375a11a 3080 seq_printf(m, "%ps\n", (void *)*ptr);
ea4e2bc4
SR
3081
3082 return 0;
3083}
3084
88e9d34c 3085static const struct seq_operations ftrace_graph_seq_ops = {
ea4e2bc4
SR
3086 .start = g_start,
3087 .next = g_next,
3088 .stop = g_stop,
3089 .show = g_show,
3090};
3091
3092static int
3093ftrace_graph_open(struct inode *inode, struct file *file)
3094{
3095 int ret = 0;
3096
3097 if (unlikely(ftrace_disabled))
3098 return -ENODEV;
3099
3100 mutex_lock(&graph_lock);
3101 if ((file->f_mode & FMODE_WRITE) &&
8650ae32 3102 (file->f_flags & O_TRUNC)) {
c7c6b1fe 3103 ftrace_graph_filter_enabled = 0;
ea4e2bc4
SR
3104 ftrace_graph_count = 0;
3105 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3106 }
a4ec5e0c 3107 mutex_unlock(&graph_lock);
ea4e2bc4 3108
a4ec5e0c 3109 if (file->f_mode & FMODE_READ)
ea4e2bc4 3110 ret = seq_open(file, &ftrace_graph_seq_ops);
ea4e2bc4
SR
3111
3112 return ret;
3113}
3114
87827111
LZ
3115static int
3116ftrace_graph_release(struct inode *inode, struct file *file)
3117{
3118 if (file->f_mode & FMODE_READ)
3119 seq_release(inode, file);
3120 return 0;
3121}
3122
ea4e2bc4 3123static int
f9349a8f 3124ftrace_set_func(unsigned long *array, int *idx, char *buffer)
ea4e2bc4 3125{
ea4e2bc4
SR
3126 struct dyn_ftrace *rec;
3127 struct ftrace_page *pg;
f9349a8f 3128 int search_len;
c7c6b1fe 3129 int fail = 1;
f9349a8f
FW
3130 int type, not;
3131 char *search;
3132 bool exists;
3133 int i;
ea4e2bc4 3134
f9349a8f 3135 /* decode regex */
3f6fe06d 3136 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
c7c6b1fe
LZ
3137 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3138 return -EBUSY;
f9349a8f
FW
3139
3140 search_len = strlen(search);
3141
52baf119 3142 mutex_lock(&ftrace_lock);
45a4a237
SR
3143
3144 if (unlikely(ftrace_disabled)) {
3145 mutex_unlock(&ftrace_lock);
3146 return -ENODEV;
3147 }
3148
265c831c
SR
3149 do_for_each_ftrace_rec(pg, rec) {
3150
45a4a237 3151 if (rec->flags & FTRACE_FL_FREE)
265c831c
SR
3152 continue;
3153
b9df92d2 3154 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
c7c6b1fe 3155 /* if it is in the array */
f9349a8f 3156 exists = false;
c7c6b1fe 3157 for (i = 0; i < *idx; i++) {
f9349a8f
FW
3158 if (array[i] == rec->ip) {
3159 exists = true;
265c831c
SR
3160 break;
3161 }
c7c6b1fe
LZ
3162 }
3163
3164 if (!not) {
3165 fail = 0;
3166 if (!exists) {
3167 array[(*idx)++] = rec->ip;
3168 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3169 goto out;
3170 }
3171 } else {
3172 if (exists) {
3173 array[i] = array[--(*idx)];
3174 array[*idx] = 0;
3175 fail = 0;
3176 }
3177 }
ea4e2bc4 3178 }
265c831c 3179 } while_for_each_ftrace_rec();
c7c6b1fe 3180out:
52baf119 3181 mutex_unlock(&ftrace_lock);
ea4e2bc4 3182
c7c6b1fe
LZ
3183 if (fail)
3184 return -EINVAL;
3185
3186 ftrace_graph_filter_enabled = 1;
3187 return 0;
ea4e2bc4
SR
3188}
3189
3190static ssize_t
3191ftrace_graph_write(struct file *file, const char __user *ubuf,
3192 size_t cnt, loff_t *ppos)
3193{
689fd8b6 3194 struct trace_parser parser;
4ba7978e 3195 ssize_t read, ret;
ea4e2bc4 3196
c7c6b1fe 3197 if (!cnt)
ea4e2bc4
SR
3198 return 0;
3199
3200 mutex_lock(&graph_lock);
3201
689fd8b6 3202 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3203 ret = -ENOMEM;
1eb90f13 3204 goto out_unlock;
ea4e2bc4
SR
3205 }
3206
689fd8b6 3207 read = trace_get_user(&parser, ubuf, cnt, ppos);
ea4e2bc4 3208
4ba7978e 3209 if (read >= 0 && trace_parser_loaded((&parser))) {
689fd8b6 3210 parser.buffer[parser.idx] = 0;
3211
3212 /* we allow only one expression at a time */
a4ec5e0c 3213 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
689fd8b6 3214 parser.buffer);
ea4e2bc4 3215 if (ret)
1eb90f13 3216 goto out_free;
ea4e2bc4 3217 }
ea4e2bc4
SR
3218
3219 ret = read;
1eb90f13
LZ
3220
3221out_free:
689fd8b6 3222 trace_parser_put(&parser);
1eb90f13 3223out_unlock:
ea4e2bc4
SR
3224 mutex_unlock(&graph_lock);
3225
3226 return ret;
3227}
3228
3229static const struct file_operations ftrace_graph_fops = {
87827111
LZ
3230 .open = ftrace_graph_open,
3231 .read = seq_read,
3232 .write = ftrace_graph_write,
3233 .release = ftrace_graph_release,
6038f373 3234 .llseek = seq_lseek,
ea4e2bc4
SR
3235};
3236#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3237
df4fc315 3238static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
5072c59f 3239{
5072c59f 3240
5452af66
FW
3241 trace_create_file("available_filter_functions", 0444,
3242 d_tracer, NULL, &ftrace_avail_fops);
5072c59f 3243
647bcd03
SR
3244 trace_create_file("enabled_functions", 0444,
3245 d_tracer, NULL, &ftrace_enabled_fops);
3246
5452af66
FW
3247 trace_create_file("set_ftrace_filter", 0644, d_tracer,
3248 NULL, &ftrace_filter_fops);
41c52c0d 3249
5452af66 3250 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
41c52c0d 3251 NULL, &ftrace_notrace_fops);
ad90c0e3 3252
ea4e2bc4 3253#ifdef CONFIG_FUNCTION_GRAPH_TRACER
5452af66 3254 trace_create_file("set_graph_function", 0444, d_tracer,
ea4e2bc4
SR
3255 NULL,
3256 &ftrace_graph_fops);
ea4e2bc4
SR
3257#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3258
5072c59f
SR
3259 return 0;
3260}
3261
5cb084bb 3262static int ftrace_process_locs(struct module *mod,
31e88909 3263 unsigned long *start,
68bf21aa
SR
3264 unsigned long *end)
3265{
3266 unsigned long *p;
3267 unsigned long addr;
68bf21aa 3268
e6ea44e9 3269 mutex_lock(&ftrace_lock);
68bf21aa
SR
3270 p = start;
3271 while (p < end) {
3272 addr = ftrace_call_adjust(*p++);
20e5227e
SR
3273 /*
3274 * Some architecture linkers will pad between
3275 * the different mcount_loc sections of different
3276 * object files to satisfy alignments.
3277 * Skip any NULL pointers.
3278 */
3279 if (!addr)
3280 continue;
68bf21aa 3281 ftrace_record_ip(addr);
68bf21aa
SR
3282 }
3283
31e88909 3284 ftrace_update_code(mod);
e6ea44e9 3285 mutex_unlock(&ftrace_lock);
68bf21aa
SR
3286
3287 return 0;
3288}
3289
93eb677d 3290#ifdef CONFIG_MODULES
e7247a15 3291void ftrace_release_mod(struct module *mod)
93eb677d
SR
3292{
3293 struct dyn_ftrace *rec;
3294 struct ftrace_page *pg;
93eb677d 3295
45a4a237
SR
3296 mutex_lock(&ftrace_lock);
3297
e7247a15 3298 if (ftrace_disabled)
45a4a237 3299 goto out_unlock;
93eb677d 3300
93eb677d 3301 do_for_each_ftrace_rec(pg, rec) {
e7247a15 3302 if (within_module_core(rec->ip, mod)) {
93eb677d
SR
3303 /*
3304 * rec->ip is changed in ftrace_free_rec()
3305 * It should not between s and e if record was freed.
3306 */
3307 FTRACE_WARN_ON(rec->flags & FTRACE_FL_FREE);
3308 ftrace_free_rec(rec);
3309 }
3310 } while_for_each_ftrace_rec();
45a4a237 3311 out_unlock:
93eb677d
SR
3312 mutex_unlock(&ftrace_lock);
3313}
3314
3315static void ftrace_init_module(struct module *mod,
3316 unsigned long *start, unsigned long *end)
90d595fe 3317{
00fd61ae 3318 if (ftrace_disabled || start == end)
fed1939c 3319 return;
5cb084bb 3320 ftrace_process_locs(mod, start, end);
90d595fe
SR
3321}
3322
93eb677d
SR
3323static int ftrace_module_notify(struct notifier_block *self,
3324 unsigned long val, void *data)
3325{
3326 struct module *mod = data;
3327
3328 switch (val) {
3329 case MODULE_STATE_COMING:
3330 ftrace_init_module(mod, mod->ftrace_callsites,
3331 mod->ftrace_callsites +
3332 mod->num_ftrace_callsites);
3333 break;
3334 case MODULE_STATE_GOING:
e7247a15 3335 ftrace_release_mod(mod);
93eb677d
SR
3336 break;
3337 }
3338
3339 return 0;
3340}
3341#else
3342static int ftrace_module_notify(struct notifier_block *self,
3343 unsigned long val, void *data)
3344{
3345 return 0;
3346}
3347#endif /* CONFIG_MODULES */
3348
3349struct notifier_block ftrace_module_nb = {
3350 .notifier_call = ftrace_module_notify,
3351 .priority = 0,
3352};
3353
68bf21aa
SR
3354extern unsigned long __start_mcount_loc[];
3355extern unsigned long __stop_mcount_loc[];
3356
3357void __init ftrace_init(void)
3358{
3359 unsigned long count, addr, flags;
3360 int ret;
3361
3362 /* Keep the ftrace pointer to the stub */
3363 addr = (unsigned long)ftrace_stub;
3364
3365 local_irq_save(flags);
3366 ftrace_dyn_arch_init(&addr);
3367 local_irq_restore(flags);
3368
3369 /* ftrace_dyn_arch_init places the return code in addr */
3370 if (addr)
3371 goto failed;
3372
3373 count = __stop_mcount_loc - __start_mcount_loc;
3374
3375 ret = ftrace_dyn_table_alloc(count);
3376 if (ret)
3377 goto failed;
3378
3379 last_ftrace_enabled = ftrace_enabled = 1;
3380
5cb084bb 3381 ret = ftrace_process_locs(NULL,
31e88909 3382 __start_mcount_loc,
68bf21aa
SR
3383 __stop_mcount_loc);
3384
93eb677d 3385 ret = register_module_notifier(&ftrace_module_nb);
24ed0c4b 3386 if (ret)
93eb677d
SR
3387 pr_warning("Failed to register trace ftrace module notifier\n");
3388
2af15d6a
SR
3389 set_ftrace_early_filters();
3390
68bf21aa
SR
3391 return;
3392 failed:
3393 ftrace_disabled = 1;
3394}
68bf21aa 3395
3d083395 3396#else
0b6e4d56 3397
2b499381 3398static struct ftrace_ops global_ops = {
bd69c30b
SR
3399 .func = ftrace_stub,
3400};
3401
0b6e4d56
FW
3402static int __init ftrace_nodyn_init(void)
3403{
3404 ftrace_enabled = 1;
3405 return 0;
3406}
3407device_initcall(ftrace_nodyn_init);
3408
df4fc315
SR
3409static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
3410static inline void ftrace_startup_enable(int command) { }
5a45cfe1 3411/* Keep as macros so we do not need to define the commands */
bd69c30b
SR
3412# define ftrace_startup(ops, command) do { } while (0)
3413# define ftrace_shutdown(ops, command) do { } while (0)
c7aafc54
IM
3414# define ftrace_startup_sysctl() do { } while (0)
3415# define ftrace_shutdown_sysctl() do { } while (0)
b848914c
SR
3416
3417static inline int
3418ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
3419{
3420 return 1;
3421}
3422
3d083395
SR
3423#endif /* CONFIG_DYNAMIC_FTRACE */
3424
b848914c
SR
3425static void
3426ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip)
3427{
3428 /* see comment above ftrace_global_list_func */
3429 struct ftrace_ops *op = rcu_dereference_raw(ftrace_ops_list);
3430
3431 while (op != &ftrace_list_end) {
3432 if (ftrace_ops_test(op, ip))
3433 op->func(ip, parent_ip);
3434 op = rcu_dereference_raw(op->next);
3435 };
3436}
3437
e32d8956 3438static void clear_ftrace_swapper(void)
978f3a45
SR
3439{
3440 struct task_struct *p;
e32d8956 3441 int cpu;
978f3a45 3442
e32d8956
SR
3443 get_online_cpus();
3444 for_each_online_cpu(cpu) {
3445 p = idle_task(cpu);
978f3a45 3446 clear_tsk_trace_trace(p);
e32d8956
SR
3447 }
3448 put_online_cpus();
3449}
978f3a45 3450
e32d8956
SR
3451static void set_ftrace_swapper(void)
3452{
3453 struct task_struct *p;
3454 int cpu;
3455
3456 get_online_cpus();
3457 for_each_online_cpu(cpu) {
3458 p = idle_task(cpu);
3459 set_tsk_trace_trace(p);
3460 }
3461 put_online_cpus();
978f3a45
SR
3462}
3463
e32d8956
SR
3464static void clear_ftrace_pid(struct pid *pid)
3465{
3466 struct task_struct *p;
3467
229c4ef8 3468 rcu_read_lock();
e32d8956
SR
3469 do_each_pid_task(pid, PIDTYPE_PID, p) {
3470 clear_tsk_trace_trace(p);
3471 } while_each_pid_task(pid, PIDTYPE_PID, p);
229c4ef8
ON
3472 rcu_read_unlock();
3473
e32d8956
SR
3474 put_pid(pid);
3475}
3476
3477static void set_ftrace_pid(struct pid *pid)
978f3a45
SR
3478{
3479 struct task_struct *p;
3480
229c4ef8 3481 rcu_read_lock();
978f3a45
SR
3482 do_each_pid_task(pid, PIDTYPE_PID, p) {
3483 set_tsk_trace_trace(p);
3484 } while_each_pid_task(pid, PIDTYPE_PID, p);
229c4ef8 3485 rcu_read_unlock();
978f3a45
SR
3486}
3487
756d17ee 3488static void clear_ftrace_pid_task(struct pid *pid)
e32d8956 3489{
756d17ee 3490 if (pid == ftrace_swapper_pid)
e32d8956
SR
3491 clear_ftrace_swapper();
3492 else
756d17ee 3493 clear_ftrace_pid(pid);
e32d8956
SR
3494}
3495
3496static void set_ftrace_pid_task(struct pid *pid)
3497{
3498 if (pid == ftrace_swapper_pid)
3499 set_ftrace_swapper();
3500 else
3501 set_ftrace_pid(pid);
3502}
3503
756d17ee 3504static int ftrace_pid_add(int p)
df4fc315 3505{
978f3a45 3506 struct pid *pid;
756d17ee 3507 struct ftrace_pid *fpid;
3508 int ret = -EINVAL;
df4fc315 3509
756d17ee 3510 mutex_lock(&ftrace_lock);
df4fc315 3511
756d17ee 3512 if (!p)
3513 pid = ftrace_swapper_pid;
3514 else
3515 pid = find_get_pid(p);
df4fc315 3516
756d17ee 3517 if (!pid)
3518 goto out;
df4fc315 3519
756d17ee 3520 ret = 0;
df4fc315 3521
756d17ee 3522 list_for_each_entry(fpid, &ftrace_pids, list)
3523 if (fpid->pid == pid)
3524 goto out_put;
978f3a45 3525
756d17ee 3526 ret = -ENOMEM;
df4fc315 3527
756d17ee 3528 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
3529 if (!fpid)
3530 goto out_put;
df4fc315 3531
756d17ee 3532 list_add(&fpid->list, &ftrace_pids);
3533 fpid->pid = pid;
0ef8cde5 3534
756d17ee 3535 set_ftrace_pid_task(pid);
978f3a45 3536
756d17ee 3537 ftrace_update_pid_func();
3538 ftrace_startup_enable(0);
3539
3540 mutex_unlock(&ftrace_lock);
3541 return 0;
3542
3543out_put:
3544 if (pid != ftrace_swapper_pid)
3545 put_pid(pid);
978f3a45 3546
756d17ee 3547out:
3548 mutex_unlock(&ftrace_lock);
3549 return ret;
3550}
3551
3552static void ftrace_pid_reset(void)
3553{
3554 struct ftrace_pid *fpid, *safe;
978f3a45 3555
756d17ee 3556 mutex_lock(&ftrace_lock);
3557 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
3558 struct pid *pid = fpid->pid;
3559
3560 clear_ftrace_pid_task(pid);
3561
3562 list_del(&fpid->list);
3563 kfree(fpid);
df4fc315
SR
3564 }
3565
df4fc315
SR
3566 ftrace_update_pid_func();
3567 ftrace_startup_enable(0);
3568
e6ea44e9 3569 mutex_unlock(&ftrace_lock);
756d17ee 3570}
df4fc315 3571
756d17ee 3572static void *fpid_start(struct seq_file *m, loff_t *pos)
3573{
3574 mutex_lock(&ftrace_lock);
3575
3576 if (list_empty(&ftrace_pids) && (!*pos))
3577 return (void *) 1;
3578
3579 return seq_list_start(&ftrace_pids, *pos);
3580}
3581
3582static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
3583{
3584 if (v == (void *)1)
3585 return NULL;
3586
3587 return seq_list_next(v, &ftrace_pids, pos);
3588}
3589
3590static void fpid_stop(struct seq_file *m, void *p)
3591{
3592 mutex_unlock(&ftrace_lock);
3593}
3594
3595static int fpid_show(struct seq_file *m, void *v)
3596{
3597 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
3598
3599 if (v == (void *)1) {
3600 seq_printf(m, "no pid\n");
3601 return 0;
3602 }
3603
3604 if (fpid->pid == ftrace_swapper_pid)
3605 seq_printf(m, "swapper tasks\n");
3606 else
3607 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
3608
3609 return 0;
3610}
3611
3612static const struct seq_operations ftrace_pid_sops = {
3613 .start = fpid_start,
3614 .next = fpid_next,
3615 .stop = fpid_stop,
3616 .show = fpid_show,
3617};
3618
3619static int
3620ftrace_pid_open(struct inode *inode, struct file *file)
3621{
3622 int ret = 0;
3623
3624 if ((file->f_mode & FMODE_WRITE) &&
3625 (file->f_flags & O_TRUNC))
3626 ftrace_pid_reset();
3627
3628 if (file->f_mode & FMODE_READ)
3629 ret = seq_open(file, &ftrace_pid_sops);
3630
3631 return ret;
3632}
3633
df4fc315
SR
3634static ssize_t
3635ftrace_pid_write(struct file *filp, const char __user *ubuf,
3636 size_t cnt, loff_t *ppos)
3637{
457dc928 3638 char buf[64], *tmp;
df4fc315
SR
3639 long val;
3640 int ret;
3641
3642 if (cnt >= sizeof(buf))
3643 return -EINVAL;
3644
3645 if (copy_from_user(&buf, ubuf, cnt))
3646 return -EFAULT;
3647
3648 buf[cnt] = 0;
3649
756d17ee 3650 /*
3651 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
3652 * to clean the filter quietly.
3653 */
457dc928
IM
3654 tmp = strstrip(buf);
3655 if (strlen(tmp) == 0)
756d17ee 3656 return 1;
3657
457dc928 3658 ret = strict_strtol(tmp, 10, &val);
df4fc315
SR
3659 if (ret < 0)
3660 return ret;
3661
756d17ee 3662 ret = ftrace_pid_add(val);
df4fc315 3663
756d17ee 3664 return ret ? ret : cnt;
3665}
df4fc315 3666
756d17ee 3667static int
3668ftrace_pid_release(struct inode *inode, struct file *file)
3669{
3670 if (file->f_mode & FMODE_READ)
3671 seq_release(inode, file);
df4fc315 3672
756d17ee 3673 return 0;
df4fc315
SR
3674}
3675
5e2336a0 3676static const struct file_operations ftrace_pid_fops = {
756d17ee 3677 .open = ftrace_pid_open,
3678 .write = ftrace_pid_write,
3679 .read = seq_read,
3680 .llseek = seq_lseek,
3681 .release = ftrace_pid_release,
df4fc315
SR
3682};
3683
3684static __init int ftrace_init_debugfs(void)
3685{
3686 struct dentry *d_tracer;
df4fc315
SR
3687
3688 d_tracer = tracing_init_dentry();
3689 if (!d_tracer)
3690 return 0;
3691
3692 ftrace_init_dyn_debugfs(d_tracer);
3693
5452af66
FW
3694 trace_create_file("set_ftrace_pid", 0644, d_tracer,
3695 NULL, &ftrace_pid_fops);
493762fc
SR
3696
3697 ftrace_profile_debugfs(d_tracer);
3698
df4fc315
SR
3699 return 0;
3700}
df4fc315
SR
3701fs_initcall(ftrace_init_debugfs);
3702
a2bb6a3d 3703/**
81adbdc0 3704 * ftrace_kill - kill ftrace
a2bb6a3d
SR
3705 *
3706 * This function should be used by panic code. It stops ftrace
3707 * but in a not so nice way. If you need to simply kill ftrace
3708 * from a non-atomic section, use ftrace_kill.
3709 */
81adbdc0 3710void ftrace_kill(void)
a2bb6a3d
SR
3711{
3712 ftrace_disabled = 1;
3713 ftrace_enabled = 0;
a2bb6a3d
SR
3714 clear_ftrace_function();
3715}
3716
16444a8a 3717/**
3d083395
SR
3718 * register_ftrace_function - register a function for profiling
3719 * @ops - ops structure that holds the function for profiling.
16444a8a 3720 *
3d083395
SR
3721 * Register a function to be called by all functions in the
3722 * kernel.
3723 *
3724 * Note: @ops->func and all the functions it calls must be labeled
3725 * with "notrace", otherwise it will go into a
3726 * recursive loop.
16444a8a 3727 */
3d083395 3728int register_ftrace_function(struct ftrace_ops *ops)
16444a8a 3729{
45a4a237 3730 int ret = -1;
4eebcc81 3731
e6ea44e9 3732 mutex_lock(&ftrace_lock);
e7d3737e 3733
45a4a237
SR
3734 if (unlikely(ftrace_disabled))
3735 goto out_unlock;
3736
b0fc494f 3737 ret = __register_ftrace_function(ops);
b848914c
SR
3738 if (!ret)
3739 ftrace_startup(ops, 0);
3740
b0fc494f 3741
45a4a237 3742 out_unlock:
e6ea44e9 3743 mutex_unlock(&ftrace_lock);
b0fc494f 3744 return ret;
3d083395
SR
3745}
3746
3747/**
32632920 3748 * unregister_ftrace_function - unregister a function for profiling.
3d083395
SR
3749 * @ops - ops structure that holds the function to unregister
3750 *
3751 * Unregister a function that was added to be called by ftrace profiling.
3752 */
3753int unregister_ftrace_function(struct ftrace_ops *ops)
3754{
3755 int ret;
3756
e6ea44e9 3757 mutex_lock(&ftrace_lock);
3d083395 3758 ret = __unregister_ftrace_function(ops);
b848914c
SR
3759 if (!ret)
3760 ftrace_shutdown(ops, 0);
e6ea44e9 3761 mutex_unlock(&ftrace_lock);
b0fc494f
SR
3762
3763 return ret;
3764}
3765
e309b41d 3766int
b0fc494f 3767ftrace_enable_sysctl(struct ctl_table *table, int write,
8d65af78 3768 void __user *buffer, size_t *lenp,
b0fc494f
SR
3769 loff_t *ppos)
3770{
45a4a237 3771 int ret = -ENODEV;
4eebcc81 3772
e6ea44e9 3773 mutex_lock(&ftrace_lock);
b0fc494f 3774
45a4a237
SR
3775 if (unlikely(ftrace_disabled))
3776 goto out;
3777
3778 ret = proc_dointvec(table, write, buffer, lenp, ppos);
b0fc494f 3779
a32c7765 3780 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
b0fc494f
SR
3781 goto out;
3782
a32c7765 3783 last_ftrace_enabled = !!ftrace_enabled;
b0fc494f
SR
3784
3785 if (ftrace_enabled) {
3786
3787 ftrace_startup_sysctl();
3788
3789 /* we are starting ftrace again */
b848914c
SR
3790 if (ftrace_ops_list != &ftrace_list_end) {
3791 if (ftrace_ops_list->next == &ftrace_list_end)
3792 ftrace_trace_function = ftrace_ops_list->func;
b0fc494f 3793 else
b848914c 3794 ftrace_trace_function = ftrace_ops_list_func;
b0fc494f
SR
3795 }
3796
3797 } else {
3798 /* stopping ftrace calls (just send to ftrace_stub) */
3799 ftrace_trace_function = ftrace_stub;
3800
3801 ftrace_shutdown_sysctl();
3802 }
3803
3804 out:
e6ea44e9 3805 mutex_unlock(&ftrace_lock);
3d083395 3806 return ret;
16444a8a 3807}
f17845e5 3808
fb52607a 3809#ifdef CONFIG_FUNCTION_GRAPH_TRACER
e7d3737e 3810
597af815 3811static int ftrace_graph_active;
4a2b8dda 3812static struct notifier_block ftrace_suspend_notifier;
e7d3737e 3813
e49dc19c
SR
3814int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
3815{
3816 return 0;
3817}
3818
287b6e68
FW
3819/* The callbacks that hook a function */
3820trace_func_graph_ret_t ftrace_graph_return =
3821 (trace_func_graph_ret_t)ftrace_stub;
e49dc19c 3822trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
f201ae23
FW
3823
3824/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
3825static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
3826{
3827 int i;
3828 int ret = 0;
3829 unsigned long flags;
3830 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
3831 struct task_struct *g, *t;
3832
3833 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
3834 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
3835 * sizeof(struct ftrace_ret_stack),
3836 GFP_KERNEL);
3837 if (!ret_stack_list[i]) {
3838 start = 0;
3839 end = i;
3840 ret = -ENOMEM;
3841 goto free;
3842 }
3843 }
3844
3845 read_lock_irqsave(&tasklist_lock, flags);
3846 do_each_thread(g, t) {
3847 if (start == end) {
3848 ret = -EAGAIN;
3849 goto unlock;
3850 }
3851
3852 if (t->ret_stack == NULL) {
380c4b14 3853 atomic_set(&t->tracing_graph_pause, 0);
f201ae23 3854 atomic_set(&t->trace_overrun, 0);
26c01624
SR
3855 t->curr_ret_stack = -1;
3856 /* Make sure the tasks see the -1 first: */
3857 smp_wmb();
3858 t->ret_stack = ret_stack_list[start++];
f201ae23
FW
3859 }
3860 } while_each_thread(g, t);
3861
3862unlock:
3863 read_unlock_irqrestore(&tasklist_lock, flags);
3864free:
3865 for (i = start; i < end; i++)
3866 kfree(ret_stack_list[i]);
3867 return ret;
3868}
3869
8aef2d28 3870static void
38516ab5
SR
3871ftrace_graph_probe_sched_switch(void *ignore,
3872 struct task_struct *prev, struct task_struct *next)
8aef2d28
SR
3873{
3874 unsigned long long timestamp;
3875 int index;
3876
be6f164a
SR
3877 /*
3878 * Does the user want to count the time a function was asleep.
3879 * If so, do not update the time stamps.
3880 */
3881 if (trace_flags & TRACE_ITER_SLEEP_TIME)
3882 return;
3883
8aef2d28
SR
3884 timestamp = trace_clock_local();
3885
3886 prev->ftrace_timestamp = timestamp;
3887
3888 /* only process tasks that we timestamped */
3889 if (!next->ftrace_timestamp)
3890 return;
3891
3892 /*
3893 * Update all the counters in next to make up for the
3894 * time next was sleeping.
3895 */
3896 timestamp -= next->ftrace_timestamp;
3897
3898 for (index = next->curr_ret_stack; index >= 0; index--)
3899 next->ret_stack[index].calltime += timestamp;
3900}
3901
f201ae23 3902/* Allocate a return stack for each task */
fb52607a 3903static int start_graph_tracing(void)
f201ae23
FW
3904{
3905 struct ftrace_ret_stack **ret_stack_list;
5b058bcd 3906 int ret, cpu;
f201ae23
FW
3907
3908 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
3909 sizeof(struct ftrace_ret_stack *),
3910 GFP_KERNEL);
3911
3912 if (!ret_stack_list)
3913 return -ENOMEM;
3914
5b058bcd 3915 /* The cpu_boot init_task->ret_stack will never be freed */
179c498a
SR
3916 for_each_online_cpu(cpu) {
3917 if (!idle_task(cpu)->ret_stack)
868baf07 3918 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
179c498a 3919 }
5b058bcd 3920
f201ae23
FW
3921 do {
3922 ret = alloc_retstack_tasklist(ret_stack_list);
3923 } while (ret == -EAGAIN);
3924
8aef2d28 3925 if (!ret) {
38516ab5 3926 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
8aef2d28
SR
3927 if (ret)
3928 pr_info("ftrace_graph: Couldn't activate tracepoint"
3929 " probe to kernel_sched_switch\n");
3930 }
3931
f201ae23
FW
3932 kfree(ret_stack_list);
3933 return ret;
3934}
3935
4a2b8dda
FW
3936/*
3937 * Hibernation protection.
3938 * The state of the current task is too much unstable during
3939 * suspend/restore to disk. We want to protect against that.
3940 */
3941static int
3942ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
3943 void *unused)
3944{
3945 switch (state) {
3946 case PM_HIBERNATION_PREPARE:
3947 pause_graph_tracing();
3948 break;
3949
3950 case PM_POST_HIBERNATION:
3951 unpause_graph_tracing();
3952 break;
3953 }
3954 return NOTIFY_DONE;
3955}
3956
287b6e68
FW
3957int register_ftrace_graph(trace_func_graph_ret_t retfunc,
3958 trace_func_graph_ent_t entryfunc)
15e6cb36 3959{
e7d3737e
FW
3960 int ret = 0;
3961
e6ea44e9 3962 mutex_lock(&ftrace_lock);
e7d3737e 3963
05ce5818 3964 /* we currently allow only one tracer registered at a time */
597af815 3965 if (ftrace_graph_active) {
05ce5818
SR
3966 ret = -EBUSY;
3967 goto out;
3968 }
3969
4a2b8dda
FW
3970 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
3971 register_pm_notifier(&ftrace_suspend_notifier);
3972
597af815 3973 ftrace_graph_active++;
fb52607a 3974 ret = start_graph_tracing();
f201ae23 3975 if (ret) {
597af815 3976 ftrace_graph_active--;
f201ae23
FW
3977 goto out;
3978 }
e53a6319 3979
287b6e68
FW
3980 ftrace_graph_return = retfunc;
3981 ftrace_graph_entry = entryfunc;
e53a6319 3982
bd69c30b 3983 ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
e7d3737e
FW
3984
3985out:
e6ea44e9 3986 mutex_unlock(&ftrace_lock);
e7d3737e 3987 return ret;
15e6cb36
FW
3988}
3989
fb52607a 3990void unregister_ftrace_graph(void)
15e6cb36 3991{
e6ea44e9 3992 mutex_lock(&ftrace_lock);
e7d3737e 3993
597af815 3994 if (unlikely(!ftrace_graph_active))
2aad1b76
SR
3995 goto out;
3996
597af815 3997 ftrace_graph_active--;
287b6e68 3998 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
e49dc19c 3999 ftrace_graph_entry = ftrace_graph_entry_stub;
bd69c30b 4000 ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
4a2b8dda 4001 unregister_pm_notifier(&ftrace_suspend_notifier);
38516ab5 4002 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
e7d3737e 4003
2aad1b76 4004 out:
e6ea44e9 4005 mutex_unlock(&ftrace_lock);
15e6cb36 4006}
f201ae23 4007
868baf07
SR
4008static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
4009
4010static void
4011graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
4012{
4013 atomic_set(&t->tracing_graph_pause, 0);
4014 atomic_set(&t->trace_overrun, 0);
4015 t->ftrace_timestamp = 0;
25985edc 4016 /* make curr_ret_stack visible before we add the ret_stack */
868baf07
SR
4017 smp_wmb();
4018 t->ret_stack = ret_stack;
4019}
4020
4021/*
4022 * Allocate a return stack for the idle task. May be the first
4023 * time through, or it may be done by CPU hotplug online.
4024 */
4025void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
4026{
4027 t->curr_ret_stack = -1;
4028 /*
4029 * The idle task has no parent, it either has its own
4030 * stack or no stack at all.
4031 */
4032 if (t->ret_stack)
4033 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
4034
4035 if (ftrace_graph_active) {
4036 struct ftrace_ret_stack *ret_stack;
4037
4038 ret_stack = per_cpu(idle_ret_stack, cpu);
4039 if (!ret_stack) {
4040 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4041 * sizeof(struct ftrace_ret_stack),
4042 GFP_KERNEL);
4043 if (!ret_stack)
4044 return;
4045 per_cpu(idle_ret_stack, cpu) = ret_stack;
4046 }
4047 graph_init_task(t, ret_stack);
4048 }
4049}
4050
f201ae23 4051/* Allocate a return stack for newly created task */
fb52607a 4052void ftrace_graph_init_task(struct task_struct *t)
f201ae23 4053{
84047e36
SR
4054 /* Make sure we do not use the parent ret_stack */
4055 t->ret_stack = NULL;
ea14eb71 4056 t->curr_ret_stack = -1;
84047e36 4057
597af815 4058 if (ftrace_graph_active) {
82310a32
SR
4059 struct ftrace_ret_stack *ret_stack;
4060
4061 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
f201ae23
FW
4062 * sizeof(struct ftrace_ret_stack),
4063 GFP_KERNEL);
82310a32 4064 if (!ret_stack)
f201ae23 4065 return;
868baf07 4066 graph_init_task(t, ret_stack);
84047e36 4067 }
f201ae23
FW
4068}
4069
fb52607a 4070void ftrace_graph_exit_task(struct task_struct *t)
f201ae23 4071{
eae849ca
FW
4072 struct ftrace_ret_stack *ret_stack = t->ret_stack;
4073
f201ae23 4074 t->ret_stack = NULL;
eae849ca
FW
4075 /* NULL must become visible to IRQs before we free it: */
4076 barrier();
4077
4078 kfree(ret_stack);
f201ae23 4079}
14a866c5
SR
4080
4081void ftrace_graph_stop(void)
4082{
4083 ftrace_stop();
4084}
15e6cb36 4085#endif