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