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