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