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