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