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