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