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