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