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