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