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