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