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