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