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