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