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