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