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