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