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