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