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