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