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