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