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