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