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