blob: 98ae4ed965db8043382d2c6705f0bf324d95ff76 [file] [log] [blame]
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02001/*
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
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +010013 * Copyright (C) 2004 Nadia Yvette Chambers
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020014 */
15
Steven Rostedt3d083392008-05-12 21:20:42 +020016#include <linux/stop_machine.h>
17#include <linux/clocksource.h>
18#include <linux/kallsyms.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020019#include <linux/seq_file.h>
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -080020#include <linux/suspend.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020021#include <linux/debugfs.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020022#include <linux/hardirq.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010023#include <linux/kthread.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020024#include <linux/uaccess.h>
Steven Rostedt5855fea2011-12-16 19:27:42 -050025#include <linux/bsearch.h>
Paul Gortmaker56d82e02011-05-26 17:53:52 -040026#include <linux/module.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010027#include <linux/ftrace.h>
Steven Rostedtb0fc4942008-05-12 21:20:43 +020028#include <linux/sysctl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020030#include <linux/ctype.h>
Steven Rostedt68950612011-12-16 17:06:45 -050031#include <linux/sort.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020032#include <linux/list.h>
Steven Rostedt59df055f2009-02-14 15:29:06 -050033#include <linux/hash.h>
Paul E. McKenney3f379b02010-03-05 15:03:25 -080034#include <linux/rcupdate.h>
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020035
Steven Rostedtad8d75f2009-04-14 19:39:12 -040036#include <trace/events/sched.h>
Steven Rostedt8aef2d22009-03-24 01:10:15 -040037
Steven Rostedt2af15d62009-05-28 13:37:24 -040038#include <asm/setup.h>
Abhishek Sagar395a59d2008-06-21 23:47:27 +053039
Steven Rostedt0706f1c2009-03-23 23:12:58 -040040#include "trace_output.h"
Steven Rostedtbac429f2009-03-20 12:50:56 -040041#include "trace_stat.h"
Steven Rostedt3d083392008-05-12 21:20:42 +020042
Steven Rostedt69128962008-10-23 09:33:03 -040043#define FTRACE_WARN_ON(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040044 ({ \
45 int ___r = cond; \
46 if (WARN_ON(___r)) \
Steven Rostedt69128962008-10-23 09:33:03 -040047 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040048 ___r; \
49 })
Steven Rostedt69128962008-10-23 09:33:03 -040050
51#define FTRACE_WARN_ON_ONCE(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040052 ({ \
53 int ___r = cond; \
54 if (WARN_ON_ONCE(___r)) \
Steven Rostedt69128962008-10-23 09:33:03 -040055 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040056 ___r; \
57 })
Steven Rostedt69128962008-10-23 09:33:03 -040058
Steven Rostedt8fc0c702009-02-16 15:28:00 -050059/* hash bits for specific function selection */
60#define FTRACE_HASH_BITS 7
61#define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
Steven Rostedt33dc9b12011-05-02 17:34:47 -040062#define FTRACE_HASH_DEFAULT_BITS 10
63#define FTRACE_HASH_MAX_BITS 12
Steven Rostedt8fc0c702009-02-16 15:28:00 -050064
Jiri Olsae2484912012-02-15 15:51:48 +010065#define FL_GLOBAL_CONTROL_MASK (FTRACE_OPS_FL_GLOBAL | FTRACE_OPS_FL_CONTROL)
66
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +090067#ifdef CONFIG_DYNAMIC_FTRACE
68#define INIT_REGEX_LOCK(opsname) \
69 .regex_lock = __MUTEX_INITIALIZER(opsname.regex_lock),
70#else
71#define INIT_REGEX_LOCK(opsname)
72#endif
73
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040074static struct ftrace_ops ftrace_list_end __read_mostly = {
75 .func = ftrace_stub,
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -040076 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040077};
78
Steven Rostedt4eebcc82008-05-12 21:20:48 +020079/* ftrace_enabled is a method to turn ftrace on or off */
80int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020081static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020082
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050083/* Quick disabling of function tracer. */
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040084int function_trace_stop __read_mostly;
85
86/* Current function tracing op */
87struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -050088/* What to set function_trace_op to */
89static struct ftrace_ops *set_function_trace_op;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050090
jolsa@redhat.com756d17e2009-10-13 16:33:52 -040091/* List for set_ftrace_pid's pids. */
92LIST_HEAD(ftrace_pids);
93struct ftrace_pid {
94 struct list_head list;
95 struct pid *pid;
96};
97
Steven Rostedt4eebcc82008-05-12 21:20:48 +020098/*
99 * ftrace_disabled is set when an anomaly is discovered.
100 * ftrace_disabled is much stronger than ftrace_enabled.
101 */
102static int ftrace_disabled __read_mostly;
103
Steven Rostedt52baf112009-02-14 01:15:39 -0500104static DEFINE_MUTEX(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +0200105
Steven Rostedtb8489142011-05-04 09:27:52 -0400106static struct ftrace_ops *ftrace_global_list __read_mostly = &ftrace_list_end;
Jiri Olsae2484912012-02-15 15:51:48 +0100107static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400108static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200109ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500110ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
Steven Rostedt2b499382011-05-03 22:49:52 -0400111static struct ftrace_ops global_ops;
Jiri Olsae2484912012-02-15 15:51:48 +0100112static struct ftrace_ops control_ops;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200113
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400114#if ARCH_SUPPORTS_FTRACE_OPS
115static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400116 struct ftrace_ops *op, struct pt_regs *regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400117#else
118/* See comment below, where ftrace_ops_list_func is defined */
119static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
120#define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
121#endif
Steven Rostedtb8489142011-05-04 09:27:52 -0400122
Steven Rostedt0a016402012-11-02 17:03:03 -0400123/*
124 * Traverse the ftrace_global_list, invoking all entries. The reason that we
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400125 * can use rcu_dereference_raw_notrace() is that elements removed from this list
Steven Rostedt0a016402012-11-02 17:03:03 -0400126 * are simply leaked, so there is no need to interact with a grace-period
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400127 * mechanism. The rcu_dereference_raw_notrace() calls are needed to handle
Steven Rostedt0a016402012-11-02 17:03:03 -0400128 * concurrent insertions into the ftrace_global_list.
129 *
130 * Silly Alpha and silly pointer-speculation compiler optimizations!
131 */
132#define do_for_each_ftrace_op(op, list) \
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400133 op = rcu_dereference_raw_notrace(list); \
Steven Rostedt0a016402012-11-02 17:03:03 -0400134 do
135
136/*
137 * Optimized for just a single item in the list (as that is the normal case).
138 */
139#define while_for_each_ftrace_op(op) \
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400140 while (likely(op = rcu_dereference_raw_notrace((op)->next)) && \
Steven Rostedt0a016402012-11-02 17:03:03 -0400141 unlikely((op) != &ftrace_list_end))
142
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900143static inline void ftrace_ops_init(struct ftrace_ops *ops)
144{
145#ifdef CONFIG_DYNAMIC_FTRACE
146 if (!(ops->flags & FTRACE_OPS_FL_INITIALIZED)) {
147 mutex_init(&ops->regex_lock);
148 ops->flags |= FTRACE_OPS_FL_INITIALIZED;
149 }
150#endif
151}
152
Steven Rostedtea701f12012-07-20 13:08:05 -0400153/**
154 * ftrace_nr_registered_ops - return number of ops registered
155 *
156 * Returns the number of ftrace_ops registered and tracing functions
157 */
158int ftrace_nr_registered_ops(void)
159{
160 struct ftrace_ops *ops;
161 int cnt = 0;
162
163 mutex_lock(&ftrace_lock);
164
165 for (ops = ftrace_ops_list;
166 ops != &ftrace_list_end; ops = ops->next)
167 cnt++;
168
169 mutex_unlock(&ftrace_lock);
170
171 return cnt;
172}
173
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400174static void
175ftrace_global_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400176 struct ftrace_ops *op, struct pt_regs *regs)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200177{
Steven Rostedtc29f1222012-11-02 17:17:59 -0400178 int bit;
179
Steven Rostedtedc15ca2012-11-02 17:47:21 -0400180 bit = trace_test_and_set_recursion(TRACE_GLOBAL_START, TRACE_GLOBAL_MAX);
181 if (bit < 0)
Steven Rostedtb1cff0a2011-05-25 14:27:43 -0400182 return;
183
Steven Rostedt0a016402012-11-02 17:03:03 -0400184 do_for_each_ftrace_op(op, ftrace_global_list) {
Steven Rostedta1e2e312011-08-09 12:50:46 -0400185 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -0400186 } while_for_each_ftrace_op(op);
Steven Rostedtedc15ca2012-11-02 17:47:21 -0400187
188 trace_clear_recursion(bit);
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200189}
190
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400191static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400192 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500193{
Steven Rostedt0ef8cde2008-12-03 15:36:58 -0500194 if (!test_tsk_trace_trace(current))
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500195 return;
196
Steven Rostedta1e2e312011-08-09 12:50:46 -0400197 ftrace_pid_function(ip, parent_ip, op, regs);
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500198}
199
200static void set_ftrace_pid_function(ftrace_func_t func)
201{
202 /* do not set ftrace_pid_function to itself! */
203 if (func != ftrace_pid_func)
204 ftrace_pid_function = func;
205}
206
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200207/**
Steven Rostedt3d083392008-05-12 21:20:42 +0200208 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200209 *
Steven Rostedt3d083392008-05-12 21:20:42 +0200210 * This NULLs the ftrace function and in essence stops
211 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200212 */
Steven Rostedt3d083392008-05-12 21:20:42 +0200213void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200214{
Steven Rostedt3d083392008-05-12 21:20:42 +0200215 ftrace_trace_function = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500216 ftrace_pid_function = ftrace_stub;
Steven Rostedt3d083392008-05-12 21:20:42 +0200217}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200218
Jiri Olsae2484912012-02-15 15:51:48 +0100219static void control_ops_disable_all(struct ftrace_ops *ops)
220{
221 int cpu;
222
223 for_each_possible_cpu(cpu)
224 *per_cpu_ptr(ops->disabled, cpu) = 1;
225}
226
227static int control_ops_alloc(struct ftrace_ops *ops)
228{
229 int __percpu *disabled;
230
231 disabled = alloc_percpu(int);
232 if (!disabled)
233 return -ENOMEM;
234
235 ops->disabled = disabled;
236 control_ops_disable_all(ops);
237 return 0;
238}
239
240static void control_ops_free(struct ftrace_ops *ops)
241{
242 free_percpu(ops->disabled);
243}
244
Steven Rostedt2b499382011-05-03 22:49:52 -0400245static void update_global_ops(void)
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400246{
Steven Rostedt (Red Hat)e6435e92014-01-10 14:31:31 -0500247 ftrace_func_t func = ftrace_global_list_func;
248 void *private = NULL;
249
250 /* The list has its own recursion protection. */
251 global_ops.flags |= FTRACE_OPS_FL_RECURSION_SAFE;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400252
253 /*
254 * If there's only one function registered, then call that
255 * function directly. Otherwise, we need to iterate over the
256 * registered callers.
257 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400258 if (ftrace_global_list == &ftrace_list_end ||
Steven Rostedt63503792012-11-02 16:58:56 -0400259 ftrace_global_list->next == &ftrace_list_end) {
Steven Rostedtb8489142011-05-04 09:27:52 -0400260 func = ftrace_global_list->func;
Steven Rostedt (Red Hat)e6435e92014-01-10 14:31:31 -0500261 private = ftrace_global_list->private;
Steven Rostedt63503792012-11-02 16:58:56 -0400262 /*
263 * As we are calling the function directly.
264 * If it does not have recursion protection,
265 * the function_trace_op needs to be updated
266 * accordingly.
267 */
Steven Rostedt (Red Hat)e6435e92014-01-10 14:31:31 -0500268 if (!(ftrace_global_list->flags & FTRACE_OPS_FL_RECURSION_SAFE))
Steven Rostedt63503792012-11-02 16:58:56 -0400269 global_ops.flags &= ~FTRACE_OPS_FL_RECURSION_SAFE;
Steven Rostedt63503792012-11-02 16:58:56 -0400270 }
271
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400272 /* If we filter on pids, update to use the pid function */
273 if (!list_empty(&ftrace_pids)) {
274 set_ftrace_pid_function(func);
275 func = ftrace_pid_func;
276 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400277
278 global_ops.func = func;
Steven Rostedt (Red Hat)e6435e92014-01-10 14:31:31 -0500279 global_ops.private = private;
Steven Rostedt2b499382011-05-03 22:49:52 -0400280}
281
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500282static void ftrace_sync(struct work_struct *work)
283{
284 /*
285 * This function is just a stub to implement a hard force
286 * of synchronize_sched(). This requires synchronizing
287 * tasks even in userspace and idle.
288 *
289 * Yes, function tracing is rude.
290 */
291}
292
293static void ftrace_sync_ipi(void *data)
294{
295 /* Probably not needed, but do it anyway */
296 smp_rmb();
297}
298
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -0500299#ifdef CONFIG_FUNCTION_GRAPH_TRACER
300static void update_function_graph_func(void);
301#else
302static inline void update_function_graph_func(void) { }
303#endif
304
Steven Rostedt2b499382011-05-03 22:49:52 -0400305static void update_ftrace_function(void)
306{
307 ftrace_func_t func;
308
309 update_global_ops();
310
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400311 /*
312 * If we are at the end of the list and this ops is
Steven Rostedt47409742012-07-20 11:04:44 -0400313 * recursion safe and not dynamic and the arch supports passing ops,
314 * then have the mcount trampoline call the function directly.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400315 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400316 if (ftrace_ops_list == &ftrace_list_end ||
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400317 (ftrace_ops_list->next == &ftrace_list_end &&
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400318 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC) &&
Steven Rostedt47409742012-07-20 11:04:44 -0400319 (ftrace_ops_list->flags & FTRACE_OPS_FL_RECURSION_SAFE) &&
Steven Rostedtccf36722012-06-05 09:44:25 -0400320 !FTRACE_FORCE_LIST_FUNC)) {
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400321 /* Set the ftrace_ops that the arch callback uses */
322 if (ftrace_ops_list == &global_ops)
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500323 set_function_trace_op = ftrace_global_list;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400324 else
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500325 set_function_trace_op = ftrace_ops_list;
Steven Rostedtb8489142011-05-04 09:27:52 -0400326 func = ftrace_ops_list->func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400327 } else {
328 /* Just use the default ftrace_ops */
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500329 set_function_trace_op = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400330 func = ftrace_ops_list_func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400331 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400332
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500333 /* If there's no change, then do nothing more here */
334 if (ftrace_trace_function == func)
335 return;
336
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -0500337 update_function_graph_func();
338
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -0500339 /*
340 * If we are using the list function, it doesn't care
341 * about the function_trace_ops.
342 */
343 if (func == ftrace_ops_list_func) {
344 ftrace_trace_function = func;
345 /*
346 * Don't even bother setting function_trace_ops,
347 * it would be racy to do so anyway.
348 */
349 return;
350 }
351
352#ifndef CONFIG_DYNAMIC_FTRACE
353 /*
354 * For static tracing, we need to be a bit more careful.
355 * The function change takes affect immediately. Thus,
356 * we need to coorditate the setting of the function_trace_ops
357 * with the setting of the ftrace_trace_function.
358 *
359 * Set the function to the list ops, which will call the
360 * function we want, albeit indirectly, but it handles the
361 * ftrace_ops and doesn't depend on function_trace_op.
362 */
363 ftrace_trace_function = ftrace_ops_list_func;
364 /*
365 * Make sure all CPUs see this. Yes this is slow, but static
366 * tracing is slow and nasty to have enabled.
367 */
368 schedule_on_each_cpu(ftrace_sync);
369 /* Now all cpus are using the list ops. */
370 function_trace_op = set_function_trace_op;
371 /* Make sure the function_trace_op is visible on all CPUs */
372 smp_wmb();
373 /* Nasty way to force a rmb on all cpus */
374 smp_call_function(ftrace_sync_ipi, NULL, 1);
375 /* OK, we are all set to update the ftrace_trace_function now! */
376#endif /* !CONFIG_DYNAMIC_FTRACE */
377
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400378 ftrace_trace_function = func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400379}
380
Steven Rostedt2b499382011-05-03 22:49:52 -0400381static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200382{
Steven Rostedt2b499382011-05-03 22:49:52 -0400383 ops->next = *list;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200384 /*
Steven Rostedtb8489142011-05-04 09:27:52 -0400385 * We are entering ops into the list but another
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200386 * CPU might be walking that list. We need to make sure
387 * the ops->next pointer is valid before another CPU sees
Steven Rostedtb8489142011-05-04 09:27:52 -0400388 * the ops pointer included into the list.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200389 */
Steven Rostedt2b499382011-05-03 22:49:52 -0400390 rcu_assign_pointer(*list, ops);
391}
Steven Rostedt3d083392008-05-12 21:20:42 +0200392
Steven Rostedt2b499382011-05-03 22:49:52 -0400393static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
394{
395 struct ftrace_ops **p;
396
397 /*
398 * If we are removing the last function, then simply point
399 * to the ftrace_stub.
400 */
401 if (*list == ops && ops->next == &ftrace_list_end) {
402 *list = &ftrace_list_end;
403 return 0;
404 }
405
406 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
407 if (*p == ops)
408 break;
409
410 if (*p != ops)
411 return -1;
412
413 *p = (*p)->next;
414 return 0;
415}
416
Jiri Olsae2484912012-02-15 15:51:48 +0100417static void add_ftrace_list_ops(struct ftrace_ops **list,
418 struct ftrace_ops *main_ops,
419 struct ftrace_ops *ops)
420{
421 int first = *list == &ftrace_list_end;
422 add_ftrace_ops(list, ops);
423 if (first)
424 add_ftrace_ops(&ftrace_ops_list, main_ops);
425}
426
427static int remove_ftrace_list_ops(struct ftrace_ops **list,
428 struct ftrace_ops *main_ops,
429 struct ftrace_ops *ops)
430{
431 int ret = remove_ftrace_ops(list, ops);
432 if (!ret && *list == &ftrace_list_end)
433 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
434 return ret;
435}
436
Steven Rostedt2b499382011-05-03 22:49:52 -0400437static int __register_ftrace_function(struct ftrace_ops *ops)
438{
Steven Rostedt2b499382011-05-03 22:49:52 -0400439 if (FTRACE_WARN_ON(ops == &global_ops))
440 return -EINVAL;
441
Steven Rostedtb8489142011-05-04 09:27:52 -0400442 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
443 return -EBUSY;
444
Jiri Olsae2484912012-02-15 15:51:48 +0100445 /* We don't support both control and global flags set. */
446 if ((ops->flags & FL_GLOBAL_CONTROL_MASK) == FL_GLOBAL_CONTROL_MASK)
447 return -EINVAL;
448
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +0900449#ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
Steven Rostedt08f6fba2012-04-30 16:20:23 -0400450 /*
451 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
452 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
453 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
454 */
455 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
456 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
457 return -EINVAL;
458
459 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
460 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
461#endif
462
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400463 if (!core_kernel_data((unsigned long)ops))
464 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
465
Steven Rostedtb8489142011-05-04 09:27:52 -0400466 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100467 add_ftrace_list_ops(&ftrace_global_list, &global_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400468 ops->flags |= FTRACE_OPS_FL_ENABLED;
Jiri Olsae2484912012-02-15 15:51:48 +0100469 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
470 if (control_ops_alloc(ops))
471 return -ENOMEM;
472 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400473 } else
474 add_ftrace_ops(&ftrace_ops_list, ops);
475
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400476 if (ftrace_enabled)
477 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200478
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200479 return 0;
480}
481
Ingo Molnare309b412008-05-12 21:20:51 +0200482static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200483{
Steven Rostedt2b499382011-05-03 22:49:52 -0400484 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200485
Steven Rostedtb8489142011-05-04 09:27:52 -0400486 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
487 return -EBUSY;
488
Steven Rostedt2b499382011-05-03 22:49:52 -0400489 if (FTRACE_WARN_ON(ops == &global_ops))
490 return -EINVAL;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200491
Steven Rostedtb8489142011-05-04 09:27:52 -0400492 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100493 ret = remove_ftrace_list_ops(&ftrace_global_list,
494 &global_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400495 if (!ret)
496 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Jiri Olsae2484912012-02-15 15:51:48 +0100497 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
498 ret = remove_ftrace_list_ops(&ftrace_control_list,
499 &control_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400500 } else
501 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
502
Steven Rostedt2b499382011-05-03 22:49:52 -0400503 if (ret < 0)
504 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400505
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400506 if (ftrace_enabled)
507 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200508
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500509 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200510}
511
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500512static void ftrace_update_pid_func(void)
513{
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400514 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500515 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900516 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500517
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400518 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500519}
520
Steven Rostedt493762f2009-03-23 17:12:36 -0400521#ifdef CONFIG_FUNCTION_PROFILER
522struct ftrace_profile {
523 struct hlist_node node;
524 unsigned long ip;
525 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400526#ifdef CONFIG_FUNCTION_GRAPH_TRACER
527 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400528 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400529#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400530};
531
532struct ftrace_profile_page {
533 struct ftrace_profile_page *next;
534 unsigned long index;
535 struct ftrace_profile records[];
536};
537
Steven Rostedtcafb1682009-03-24 20:50:39 -0400538struct ftrace_profile_stat {
539 atomic_t disabled;
540 struct hlist_head *hash;
541 struct ftrace_profile_page *pages;
542 struct ftrace_profile_page *start;
543 struct tracer_stat stat;
544};
545
Steven Rostedt493762f2009-03-23 17:12:36 -0400546#define PROFILE_RECORDS_SIZE \
547 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
548
549#define PROFILES_PER_PAGE \
550 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
551
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400552static int ftrace_profile_enabled __read_mostly;
553
554/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400555static DEFINE_MUTEX(ftrace_profile_lock);
556
Steven Rostedtcafb1682009-03-24 20:50:39 -0400557static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400558
Namhyung Kim20079eb2013-04-10 08:55:50 +0900559#define FTRACE_PROFILE_HASH_BITS 10
560#define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
Steven Rostedt493762f2009-03-23 17:12:36 -0400561
Steven Rostedt493762f2009-03-23 17:12:36 -0400562static void *
563function_stat_next(void *v, int idx)
564{
565 struct ftrace_profile *rec = v;
566 struct ftrace_profile_page *pg;
567
568 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
569
570 again:
Li Zefan0296e422009-06-26 11:15:37 +0800571 if (idx != 0)
572 rec++;
573
Steven Rostedt493762f2009-03-23 17:12:36 -0400574 if ((void *)rec >= (void *)&pg->records[pg->index]) {
575 pg = pg->next;
576 if (!pg)
577 return NULL;
578 rec = &pg->records[0];
579 if (!rec->counter)
580 goto again;
581 }
582
583 return rec;
584}
585
586static void *function_stat_start(struct tracer_stat *trace)
587{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400588 struct ftrace_profile_stat *stat =
589 container_of(trace, struct ftrace_profile_stat, stat);
590
591 if (!stat || !stat->start)
592 return NULL;
593
594 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400595}
596
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400597#ifdef CONFIG_FUNCTION_GRAPH_TRACER
598/* function graph compares on total time */
599static int function_stat_cmp(void *p1, void *p2)
600{
601 struct ftrace_profile *a = p1;
602 struct ftrace_profile *b = p2;
603
604 if (a->time < b->time)
605 return -1;
606 if (a->time > b->time)
607 return 1;
608 else
609 return 0;
610}
611#else
612/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400613static int function_stat_cmp(void *p1, void *p2)
614{
615 struct ftrace_profile *a = p1;
616 struct ftrace_profile *b = p2;
617
618 if (a->counter < b->counter)
619 return -1;
620 if (a->counter > b->counter)
621 return 1;
622 else
623 return 0;
624}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400625#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400626
627static int function_stat_headers(struct seq_file *m)
628{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400629#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400630 seq_printf(m, " Function "
Chase Douglase330b3b2010-04-26 14:02:05 -0400631 "Hit Time Avg s^2\n"
Steven Rostedt34886c82009-03-25 21:00:47 -0400632 " -------- "
Chase Douglase330b3b2010-04-26 14:02:05 -0400633 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400634#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400635 seq_printf(m, " Function Hit\n"
636 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400637#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400638 return 0;
639}
640
641static int function_stat_show(struct seq_file *m, void *v)
642{
643 struct ftrace_profile *rec = v;
644 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800645 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400646#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400647 static struct trace_seq s;
648 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400649 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400650#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800651 mutex_lock(&ftrace_profile_lock);
652
653 /* we raced with function_profile_reset() */
654 if (unlikely(rec->counter == 0)) {
655 ret = -EBUSY;
656 goto out;
657 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400658
659 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400660 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400661
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400662#ifdef CONFIG_FUNCTION_GRAPH_TRACER
663 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400664 avg = rec->time;
665 do_div(avg, rec->counter);
666
Chase Douglase330b3b2010-04-26 14:02:05 -0400667 /* Sample standard deviation (s^2) */
668 if (rec->counter <= 1)
669 stddev = 0;
670 else {
Juri Lelli52d85d72013-06-12 12:03:18 +0200671 /*
672 * Apply Welford's method:
673 * s^2 = 1 / (n * (n-1)) * (n * \Sum (x_i)^2 - (\Sum x_i)^2)
674 */
675 stddev = rec->counter * rec->time_squared -
676 rec->time * rec->time;
677
Chase Douglase330b3b2010-04-26 14:02:05 -0400678 /*
679 * Divide only 1000 for ns^2 -> us^2 conversion.
680 * trace_print_graph_duration will divide 1000 again.
681 */
Juri Lelli52d85d72013-06-12 12:03:18 +0200682 do_div(stddev, rec->counter * (rec->counter - 1) * 1000);
Chase Douglase330b3b2010-04-26 14:02:05 -0400683 }
684
Steven Rostedt34886c82009-03-25 21:00:47 -0400685 trace_seq_init(&s);
686 trace_print_graph_duration(rec->time, &s);
687 trace_seq_puts(&s, " ");
688 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400689 trace_seq_puts(&s, " ");
690 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400691 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400692#endif
693 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800694out:
695 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400696
Li Zefan3aaba202010-08-23 16:50:12 +0800697 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400698}
699
Steven Rostedtcafb1682009-03-24 20:50:39 -0400700static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400701{
702 struct ftrace_profile_page *pg;
703
Steven Rostedtcafb1682009-03-24 20:50:39 -0400704 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400705
706 while (pg) {
707 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
708 pg->index = 0;
709 pg = pg->next;
710 }
711
Steven Rostedtcafb1682009-03-24 20:50:39 -0400712 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400713 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
714}
715
Steven Rostedtcafb1682009-03-24 20:50:39 -0400716int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400717{
718 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400719 int functions;
720 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400721 int i;
722
723 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400724 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400725 return 0;
726
Steven Rostedtcafb1682009-03-24 20:50:39 -0400727 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
728 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400729 return -ENOMEM;
730
Steven Rostedt318e0a72009-03-25 20:06:34 -0400731#ifdef CONFIG_DYNAMIC_FTRACE
732 functions = ftrace_update_tot_cnt;
733#else
734 /*
735 * We do not know the number of functions that exist because
736 * dynamic tracing is what counts them. With past experience
737 * we have around 20K functions. That should be more than enough.
738 * It is highly unlikely we will execute every function in
739 * the kernel.
740 */
741 functions = 20000;
742#endif
743
Steven Rostedtcafb1682009-03-24 20:50:39 -0400744 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400745
Steven Rostedt318e0a72009-03-25 20:06:34 -0400746 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
747
Namhyung Kim39e30cd2013-04-01 21:46:24 +0900748 for (i = 1; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400749 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400750 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400751 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400752 pg = pg->next;
753 }
754
755 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400756
757 out_free:
758 pg = stat->start;
759 while (pg) {
760 unsigned long tmp = (unsigned long)pg;
761
762 pg = pg->next;
763 free_page(tmp);
764 }
765
Steven Rostedt318e0a72009-03-25 20:06:34 -0400766 stat->pages = NULL;
767 stat->start = NULL;
768
769 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400770}
771
Steven Rostedtcafb1682009-03-24 20:50:39 -0400772static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400773{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400774 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400775 int size;
776
Steven Rostedtcafb1682009-03-24 20:50:39 -0400777 stat = &per_cpu(ftrace_profile_stats, cpu);
778
779 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400780 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400781 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400782 return 0;
783 }
784
785 /*
786 * We are profiling all functions, but usually only a few thousand
787 * functions are hit. We'll make a hash of 1024 items.
788 */
789 size = FTRACE_PROFILE_HASH_SIZE;
790
Steven Rostedtcafb1682009-03-24 20:50:39 -0400791 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400792
Steven Rostedtcafb1682009-03-24 20:50:39 -0400793 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400794 return -ENOMEM;
795
Steven Rostedt318e0a72009-03-25 20:06:34 -0400796 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400797 if (ftrace_profile_pages_init(stat) < 0) {
798 kfree(stat->hash);
799 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400800 return -ENOMEM;
801 }
802
803 return 0;
804}
805
Steven Rostedtcafb1682009-03-24 20:50:39 -0400806static int ftrace_profile_init(void)
807{
808 int cpu;
809 int ret = 0;
810
Miao Xiec4602c12013-12-16 15:20:01 +0800811 for_each_possible_cpu(cpu) {
Steven Rostedtcafb1682009-03-24 20:50:39 -0400812 ret = ftrace_profile_init_cpu(cpu);
813 if (ret)
814 break;
815 }
816
817 return ret;
818}
819
Steven Rostedt493762f2009-03-23 17:12:36 -0400820/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400821static struct ftrace_profile *
822ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400823{
824 struct ftrace_profile *rec;
825 struct hlist_head *hhd;
Steven Rostedt493762f2009-03-23 17:12:36 -0400826 unsigned long key;
827
Namhyung Kim20079eb2013-04-10 08:55:50 +0900828 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400829 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400830
831 if (hlist_empty(hhd))
832 return NULL;
833
Steven Rostedt1bb539c2013-05-28 14:38:43 -0400834 hlist_for_each_entry_rcu_notrace(rec, hhd, node) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400835 if (rec->ip == ip)
836 return rec;
837 }
838
839 return NULL;
840}
841
Steven Rostedtcafb1682009-03-24 20:50:39 -0400842static void ftrace_add_profile(struct ftrace_profile_stat *stat,
843 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400844{
845 unsigned long key;
846
Namhyung Kim20079eb2013-04-10 08:55:50 +0900847 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400848 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400849}
850
Steven Rostedt318e0a72009-03-25 20:06:34 -0400851/*
852 * The memory is already allocated, this simply finds a new record to use.
853 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400854static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400855ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400856{
857 struct ftrace_profile *rec = NULL;
858
Steven Rostedt318e0a72009-03-25 20:06:34 -0400859 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400860 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400861 goto out;
862
Steven Rostedt493762f2009-03-23 17:12:36 -0400863 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400864 * Try to find the function again since an NMI
865 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400866 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400867 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400868 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400869 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400870
Steven Rostedtcafb1682009-03-24 20:50:39 -0400871 if (stat->pages->index == PROFILES_PER_PAGE) {
872 if (!stat->pages->next)
873 goto out;
874 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400875 }
876
Steven Rostedtcafb1682009-03-24 20:50:39 -0400877 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400878 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400879 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400880
Steven Rostedt493762f2009-03-23 17:12:36 -0400881 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400882 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400883
884 return rec;
885}
886
Steven Rostedt493762f2009-03-23 17:12:36 -0400887static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400888function_profile_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400889 struct ftrace_ops *ops, struct pt_regs *regs)
Steven Rostedt493762f2009-03-23 17:12:36 -0400890{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400891 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400892 struct ftrace_profile *rec;
893 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400894
895 if (!ftrace_profile_enabled)
896 return;
897
Steven Rostedt493762f2009-03-23 17:12:36 -0400898 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400899
900 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400901 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400902 goto out;
903
904 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400905 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400906 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400907 if (!rec)
908 goto out;
909 }
910
911 rec->counter++;
912 out:
913 local_irq_restore(flags);
914}
915
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400916#ifdef CONFIG_FUNCTION_GRAPH_TRACER
917static int profile_graph_entry(struct ftrace_graph_ent *trace)
918{
Steven Rostedta1e2e312011-08-09 12:50:46 -0400919 function_profile_call(trace->func, 0, NULL, NULL);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400920 return 1;
921}
922
923static void profile_graph_return(struct ftrace_graph_ret *trace)
924{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400925 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400926 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400927 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400928 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400929
930 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400931 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400932 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400933 goto out;
934
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400935 /* If the calltime was zero'd ignore it */
936 if (!trace->calltime)
937 goto out;
938
Steven Rostedta2a16d62009-03-24 23:17:58 -0400939 calltime = trace->rettime - trace->calltime;
940
941 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
942 int index;
943
944 index = trace->depth;
945
946 /* Append this call time to the parent time to subtract */
947 if (index)
948 current->ret_stack[index - 1].subtime += calltime;
949
950 if (current->ret_stack[index].subtime < calltime)
951 calltime -= current->ret_stack[index].subtime;
952 else
953 calltime = 0;
954 }
955
Steven Rostedtcafb1682009-03-24 20:50:39 -0400956 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400957 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400958 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400959 rec->time_squared += calltime * calltime;
960 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400961
Steven Rostedtcafb1682009-03-24 20:50:39 -0400962 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400963 local_irq_restore(flags);
964}
965
966static int register_ftrace_profiler(void)
967{
968 return register_ftrace_graph(&profile_graph_return,
969 &profile_graph_entry);
970}
971
972static void unregister_ftrace_profiler(void)
973{
974 unregister_ftrace_graph();
975}
976#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100977static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400978 .func = function_profile_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +0900979 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
980 INIT_REGEX_LOCK(ftrace_profile_ops)
Steven Rostedt493762f2009-03-23 17:12:36 -0400981};
982
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400983static int register_ftrace_profiler(void)
984{
985 return register_ftrace_function(&ftrace_profile_ops);
986}
987
988static void unregister_ftrace_profiler(void)
989{
990 unregister_ftrace_function(&ftrace_profile_ops);
991}
992#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
993
Steven Rostedt493762f2009-03-23 17:12:36 -0400994static ssize_t
995ftrace_profile_write(struct file *filp, const char __user *ubuf,
996 size_t cnt, loff_t *ppos)
997{
998 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400999 int ret;
1000
Peter Huewe22fe9b52011-06-07 21:58:27 +02001001 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1002 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -04001003 return ret;
1004
1005 val = !!val;
1006
1007 mutex_lock(&ftrace_profile_lock);
1008 if (ftrace_profile_enabled ^ val) {
1009 if (val) {
1010 ret = ftrace_profile_init();
1011 if (ret < 0) {
1012 cnt = ret;
1013 goto out;
1014 }
1015
Steven Rostedt0706f1c2009-03-23 23:12:58 -04001016 ret = register_ftrace_profiler();
1017 if (ret < 0) {
1018 cnt = ret;
1019 goto out;
1020 }
Steven Rostedt493762f2009-03-23 17:12:36 -04001021 ftrace_profile_enabled = 1;
1022 } else {
1023 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -04001024 /*
1025 * unregister_ftrace_profiler calls stop_machine
1026 * so this acts like an synchronize_sched.
1027 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -04001028 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -04001029 }
1030 }
1031 out:
1032 mutex_unlock(&ftrace_profile_lock);
1033
Jiri Olsacf8517c2009-10-23 19:36:16 -04001034 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -04001035
1036 return cnt;
1037}
1038
1039static ssize_t
1040ftrace_profile_read(struct file *filp, char __user *ubuf,
1041 size_t cnt, loff_t *ppos)
1042{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001043 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -04001044 int r;
1045
1046 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
1047 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
1048}
1049
1050static const struct file_operations ftrace_profile_fops = {
1051 .open = tracing_open_generic,
1052 .read = ftrace_profile_read,
1053 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001054 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -04001055};
1056
Steven Rostedtcafb1682009-03-24 20:50:39 -04001057/* used to initialize the real stat files */
1058static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -04001059 .name = "functions",
1060 .stat_start = function_stat_start,
1061 .stat_next = function_stat_next,
1062 .stat_cmp = function_stat_cmp,
1063 .stat_headers = function_stat_headers,
1064 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -04001065};
1066
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001067static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001068{
Steven Rostedtcafb1682009-03-24 20:50:39 -04001069 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -04001070 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001071 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -04001072 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001073 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -04001074
Steven Rostedtcafb1682009-03-24 20:50:39 -04001075 for_each_possible_cpu(cpu) {
1076 stat = &per_cpu(ftrace_profile_stats, cpu);
1077
1078 /* allocate enough for function name + cpu number */
1079 name = kmalloc(32, GFP_KERNEL);
1080 if (!name) {
1081 /*
1082 * The files created are permanent, if something happens
1083 * we still do not free memory.
1084 */
Steven Rostedtcafb1682009-03-24 20:50:39 -04001085 WARN(1,
1086 "Could not allocate stat file for cpu %d\n",
1087 cpu);
1088 return;
1089 }
1090 stat->stat = function_stats;
1091 snprintf(name, 32, "function%d", cpu);
1092 stat->stat.name = name;
1093 ret = register_stat_tracer(&stat->stat);
1094 if (ret) {
1095 WARN(1,
1096 "Could not register function stat for cpu %d\n",
1097 cpu);
1098 kfree(name);
1099 return;
1100 }
Steven Rostedt493762f2009-03-23 17:12:36 -04001101 }
1102
1103 entry = debugfs_create_file("function_profile_enabled", 0644,
1104 d_tracer, NULL, &ftrace_profile_fops);
1105 if (!entry)
1106 pr_warning("Could not create debugfs "
1107 "'function_profile_enabled' entry\n");
1108}
1109
1110#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001111static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001112{
1113}
1114#endif /* CONFIG_FUNCTION_PROFILER */
1115
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001116static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1117
Steven Rostedt3d083392008-05-12 21:20:42 +02001118#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001119
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001120#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001121# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001122#endif
1123
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001124static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1125
Steven Rostedtb6887d72009-02-17 12:32:04 -05001126struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001127 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -05001128 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001129 unsigned long flags;
1130 unsigned long ip;
1131 void *data;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04001132 struct list_head free_list;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001133};
1134
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001135struct ftrace_func_entry {
1136 struct hlist_node hlist;
1137 unsigned long ip;
1138};
1139
1140struct ftrace_hash {
1141 unsigned long size_bits;
1142 struct hlist_head *buckets;
1143 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001144 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001145};
1146
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001147/*
1148 * We make these constant because no one should touch them,
1149 * but they are used as the default "empty hash", to avoid allocating
1150 * it all the time. These are in a read only section such that if
1151 * anyone does try to modify it, it will cause an exception.
1152 */
1153static const struct hlist_head empty_buckets[1];
1154static const struct ftrace_hash empty_hash = {
1155 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001156};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001157#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001158
Steven Rostedt2b499382011-05-03 22:49:52 -04001159static struct ftrace_ops global_ops = {
Steven Rostedtf45948e2011-05-02 12:29:25 -04001160 .func = ftrace_stub,
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001161 .notrace_hash = EMPTY_HASH,
1162 .filter_hash = EMPTY_HASH,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001163 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
1164 INIT_REGEX_LOCK(global_ops)
Steven Rostedtf45948e2011-05-02 12:29:25 -04001165};
1166
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001167struct ftrace_page {
1168 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001169 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001170 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001171 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001172};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001173
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001174static struct ftrace_page *ftrace_new_pgs;
1175
Steven Rostedta7900872011-12-16 16:23:44 -05001176#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1177#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001178
1179/* estimate from running different kernels */
1180#define NR_TO_INIT 10000
1181
1182static struct ftrace_page *ftrace_pages_start;
1183static struct ftrace_page *ftrace_pages;
1184
Steven Rostedt06a51d92011-12-19 19:07:36 -05001185static bool ftrace_hash_empty(struct ftrace_hash *hash)
1186{
1187 return !hash || !hash->count;
1188}
1189
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001190static struct ftrace_func_entry *
1191ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1192{
1193 unsigned long key;
1194 struct ftrace_func_entry *entry;
1195 struct hlist_head *hhd;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001196
Steven Rostedt06a51d92011-12-19 19:07:36 -05001197 if (ftrace_hash_empty(hash))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001198 return NULL;
1199
1200 if (hash->size_bits > 0)
1201 key = hash_long(ip, hash->size_bits);
1202 else
1203 key = 0;
1204
1205 hhd = &hash->buckets[key];
1206
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001207 hlist_for_each_entry_rcu_notrace(entry, hhd, hlist) {
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001208 if (entry->ip == ip)
1209 return entry;
1210 }
1211 return NULL;
1212}
1213
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001214static void __add_hash_entry(struct ftrace_hash *hash,
1215 struct ftrace_func_entry *entry)
1216{
1217 struct hlist_head *hhd;
1218 unsigned long key;
1219
1220 if (hash->size_bits)
1221 key = hash_long(entry->ip, hash->size_bits);
1222 else
1223 key = 0;
1224
1225 hhd = &hash->buckets[key];
1226 hlist_add_head(&entry->hlist, hhd);
1227 hash->count++;
1228}
1229
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001230static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1231{
1232 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001233
1234 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1235 if (!entry)
1236 return -ENOMEM;
1237
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001238 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001239 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001240
1241 return 0;
1242}
1243
1244static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001245free_hash_entry(struct ftrace_hash *hash,
1246 struct ftrace_func_entry *entry)
1247{
1248 hlist_del(&entry->hlist);
1249 kfree(entry);
1250 hash->count--;
1251}
1252
1253static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001254remove_hash_entry(struct ftrace_hash *hash,
1255 struct ftrace_func_entry *entry)
1256{
1257 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001258 hash->count--;
1259}
1260
1261static void ftrace_hash_clear(struct ftrace_hash *hash)
1262{
1263 struct hlist_head *hhd;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001264 struct hlist_node *tn;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001265 struct ftrace_func_entry *entry;
1266 int size = 1 << hash->size_bits;
1267 int i;
1268
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001269 if (!hash->count)
1270 return;
1271
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001272 for (i = 0; i < size; i++) {
1273 hhd = &hash->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001274 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001275 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001276 }
1277 FTRACE_WARN_ON(hash->count);
1278}
1279
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001280static void free_ftrace_hash(struct ftrace_hash *hash)
1281{
1282 if (!hash || hash == EMPTY_HASH)
1283 return;
1284 ftrace_hash_clear(hash);
1285 kfree(hash->buckets);
1286 kfree(hash);
1287}
1288
Steven Rostedt07fd5512011-05-05 18:03:47 -04001289static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1290{
1291 struct ftrace_hash *hash;
1292
1293 hash = container_of(rcu, struct ftrace_hash, rcu);
1294 free_ftrace_hash(hash);
1295}
1296
1297static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1298{
1299 if (!hash || hash == EMPTY_HASH)
1300 return;
1301 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1302}
1303
Jiri Olsa5500fa52012-02-15 15:51:54 +01001304void ftrace_free_filter(struct ftrace_ops *ops)
1305{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09001306 ftrace_ops_init(ops);
Jiri Olsa5500fa52012-02-15 15:51:54 +01001307 free_ftrace_hash(ops->filter_hash);
1308 free_ftrace_hash(ops->notrace_hash);
1309}
1310
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001311static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1312{
1313 struct ftrace_hash *hash;
1314 int size;
1315
1316 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1317 if (!hash)
1318 return NULL;
1319
1320 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001321 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001322
1323 if (!hash->buckets) {
1324 kfree(hash);
1325 return NULL;
1326 }
1327
1328 hash->size_bits = size_bits;
1329
1330 return hash;
1331}
1332
1333static struct ftrace_hash *
1334alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1335{
1336 struct ftrace_func_entry *entry;
1337 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001338 int size;
1339 int ret;
1340 int i;
1341
1342 new_hash = alloc_ftrace_hash(size_bits);
1343 if (!new_hash)
1344 return NULL;
1345
1346 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001347 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001348 return new_hash;
1349
1350 size = 1 << hash->size_bits;
1351 for (i = 0; i < size; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001352 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001353 ret = add_hash_entry(new_hash, entry->ip);
1354 if (ret < 0)
1355 goto free_hash;
1356 }
1357 }
1358
1359 FTRACE_WARN_ON(new_hash->count != hash->count);
1360
1361 return new_hash;
1362
1363 free_hash:
1364 free_ftrace_hash(new_hash);
1365 return NULL;
1366}
1367
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001368static void
1369ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1370static void
1371ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1372
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001373static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001374ftrace_hash_move(struct ftrace_ops *ops, int enable,
1375 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001376{
1377 struct ftrace_func_entry *entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001378 struct hlist_node *tn;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001379 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001380 struct ftrace_hash *old_hash;
1381 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001382 int size = src->count;
1383 int bits = 0;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001384 int ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001385 int i;
1386
1387 /*
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001388 * Remove the current set, update the hash and add
1389 * them back.
1390 */
1391 ftrace_hash_rec_disable(ops, enable);
1392
1393 /*
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001394 * If the new source is empty, just free dst and assign it
1395 * the empty_hash.
1396 */
1397 if (!src->count) {
Steven Rostedt07fd5512011-05-05 18:03:47 -04001398 free_ftrace_hash_rcu(*dst);
1399 rcu_assign_pointer(*dst, EMPTY_HASH);
Steven Rostedtd4d34b92011-11-04 20:32:39 -04001400 /* still need to update the function records */
1401 ret = 0;
1402 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001403 }
1404
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001405 /*
1406 * Make the hash size about 1/2 the # found
1407 */
1408 for (size /= 2; size; size >>= 1)
1409 bits++;
1410
1411 /* Don't allocate too much */
1412 if (bits > FTRACE_HASH_MAX_BITS)
1413 bits = FTRACE_HASH_MAX_BITS;
1414
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001415 ret = -ENOMEM;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001416 new_hash = alloc_ftrace_hash(bits);
1417 if (!new_hash)
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001418 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001419
1420 size = 1 << src->size_bits;
1421 for (i = 0; i < size; i++) {
1422 hhd = &src->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001423 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001424 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001425 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001426 }
1427 }
1428
Steven Rostedt07fd5512011-05-05 18:03:47 -04001429 old_hash = *dst;
1430 rcu_assign_pointer(*dst, new_hash);
1431 free_ftrace_hash_rcu(old_hash);
1432
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001433 ret = 0;
1434 out:
1435 /*
1436 * Enable regardless of ret:
1437 * On success, we enable the new hash.
1438 * On failure, we re-enable the original hash.
1439 */
1440 ftrace_hash_rec_enable(ops, enable);
1441
1442 return ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001443}
1444
Steven Rostedt265c8312009-02-13 12:43:56 -05001445/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001446 * Test the hashes for this ops to see if we want to call
1447 * the ops->func or not.
1448 *
1449 * It's a match if the ip is in the ops->filter_hash or
1450 * the filter_hash does not exist or is empty,
1451 * AND
1452 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001453 *
1454 * This needs to be called with preemption disabled as
1455 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001456 */
1457static int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001458ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04001459{
1460 struct ftrace_hash *filter_hash;
1461 struct ftrace_hash *notrace_hash;
1462 int ret;
1463
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04001464#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
1465 /*
1466 * There's a small race when adding ops that the ftrace handler
1467 * that wants regs, may be called without them. We can not
1468 * allow that handler to be called if regs is NULL.
1469 */
1470 if (regs == NULL && (ops->flags & FTRACE_OPS_FL_SAVE_REGS))
1471 return 0;
1472#endif
1473
Steven Rostedt1bb539c2013-05-28 14:38:43 -04001474 filter_hash = rcu_dereference_raw_notrace(ops->filter_hash);
1475 notrace_hash = rcu_dereference_raw_notrace(ops->notrace_hash);
Steven Rostedtb8489142011-05-04 09:27:52 -04001476
Steven Rostedt06a51d92011-12-19 19:07:36 -05001477 if ((ftrace_hash_empty(filter_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001478 ftrace_lookup_ip(filter_hash, ip)) &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001479 (ftrace_hash_empty(notrace_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001480 !ftrace_lookup_ip(notrace_hash, ip)))
1481 ret = 1;
1482 else
1483 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001484
1485 return ret;
1486}
1487
1488/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001489 * This is a double for. Do not use 'break' to break out of the loop,
1490 * you must use a goto.
1491 */
1492#define do_for_each_ftrace_rec(pg, rec) \
1493 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1494 int _____i; \
1495 for (_____i = 0; _____i < pg->index; _____i++) { \
1496 rec = &pg->records[_____i];
1497
1498#define while_for_each_ftrace_rec() \
1499 } \
1500 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301501
Steven Rostedt5855fea2011-12-16 19:27:42 -05001502
1503static int ftrace_cmp_recs(const void *a, const void *b)
1504{
Steven Rostedta650e022012-04-25 13:48:13 -04001505 const struct dyn_ftrace *key = a;
1506 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001507
Steven Rostedta650e022012-04-25 13:48:13 -04001508 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001509 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001510 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1511 return 1;
1512 return 0;
1513}
1514
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001515static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001516{
1517 struct ftrace_page *pg;
1518 struct dyn_ftrace *rec;
1519 struct dyn_ftrace key;
1520
1521 key.ip = start;
1522 key.flags = end; /* overload flags, as it is unsigned long */
1523
1524 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1525 if (end < pg->records[0].ip ||
1526 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1527 continue;
1528 rec = bsearch(&key, pg->records, pg->index,
1529 sizeof(struct dyn_ftrace),
1530 ftrace_cmp_recs);
1531 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001532 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001533 }
1534
Steven Rostedt5855fea2011-12-16 19:27:42 -05001535 return 0;
1536}
1537
Steven Rostedtc88fd862011-08-16 09:53:39 -04001538/**
1539 * ftrace_location - return true if the ip giving is a traced location
1540 * @ip: the instruction pointer to check
1541 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001542 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001543 * That is, the instruction that is either a NOP or call to
1544 * the function tracer. It checks the ftrace internal tables to
1545 * determine if the address belongs or not.
1546 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001547unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001548{
Steven Rostedta650e022012-04-25 13:48:13 -04001549 return ftrace_location_range(ip, ip);
1550}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001551
Steven Rostedta650e022012-04-25 13:48:13 -04001552/**
1553 * ftrace_text_reserved - return true if range contains an ftrace location
1554 * @start: start of range to search
1555 * @end: end of range to search (inclusive). @end points to the last byte to check.
1556 *
1557 * Returns 1 if @start and @end contains a ftrace location.
1558 * That is, the instruction that is either a NOP or call to
1559 * the function tracer. It checks the ftrace internal tables to
1560 * determine if the address belongs or not.
1561 */
1562int ftrace_text_reserved(void *start, void *end)
1563{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001564 unsigned long ret;
1565
1566 ret = ftrace_location_range((unsigned long)start,
1567 (unsigned long)end);
1568
1569 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001570}
1571
Steven Rostedted926f92011-05-03 13:25:24 -04001572static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1573 int filter_hash,
1574 bool inc)
1575{
1576 struct ftrace_hash *hash;
1577 struct ftrace_hash *other_hash;
1578 struct ftrace_page *pg;
1579 struct dyn_ftrace *rec;
1580 int count = 0;
1581 int all = 0;
1582
1583 /* Only update if the ops has been registered */
1584 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1585 return;
1586
1587 /*
1588 * In the filter_hash case:
1589 * If the count is zero, we update all records.
1590 * Otherwise we just update the items in the hash.
1591 *
1592 * In the notrace_hash case:
1593 * We enable the update in the hash.
1594 * As disabling notrace means enabling the tracing,
1595 * and enabling notrace means disabling, the inc variable
1596 * gets inversed.
1597 */
1598 if (filter_hash) {
1599 hash = ops->filter_hash;
1600 other_hash = ops->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001601 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001602 all = 1;
1603 } else {
1604 inc = !inc;
1605 hash = ops->notrace_hash;
1606 other_hash = ops->filter_hash;
1607 /*
1608 * If the notrace hash has no items,
1609 * then there's nothing to do.
1610 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001611 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001612 return;
1613 }
1614
1615 do_for_each_ftrace_rec(pg, rec) {
1616 int in_other_hash = 0;
1617 int in_hash = 0;
1618 int match = 0;
1619
1620 if (all) {
1621 /*
1622 * Only the filter_hash affects all records.
1623 * Update if the record is not in the notrace hash.
1624 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001625 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001626 match = 1;
1627 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001628 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1629 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001630
1631 /*
1632 *
1633 */
1634 if (filter_hash && in_hash && !in_other_hash)
1635 match = 1;
1636 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001637 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001638 match = 1;
1639 }
1640 if (!match)
1641 continue;
1642
1643 if (inc) {
1644 rec->flags++;
1645 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1646 return;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001647 /*
1648 * If any ops wants regs saved for this function
1649 * then all ops will get saved regs.
1650 */
1651 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1652 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001653 } else {
1654 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1655 return;
1656 rec->flags--;
1657 }
1658 count++;
1659 /* Shortcut, if we handled all records, we are done. */
1660 if (!all && count == hash->count)
1661 return;
1662 } while_for_each_ftrace_rec();
1663}
1664
1665static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1666 int filter_hash)
1667{
1668 __ftrace_hash_rec_update(ops, filter_hash, 0);
1669}
1670
1671static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1672 int filter_hash)
1673{
1674 __ftrace_hash_rec_update(ops, filter_hash, 1);
1675}
1676
Steven Rostedt05736a42008-09-22 14:55:47 -07001677static void print_ip_ins(const char *fmt, unsigned char *p)
1678{
1679 int i;
1680
1681 printk(KERN_CONT "%s", fmt);
1682
1683 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1684 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1685}
1686
Steven Rostedtc88fd862011-08-16 09:53:39 -04001687/**
1688 * ftrace_bug - report and shutdown function tracer
1689 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1690 * @ip: The address that failed
1691 *
1692 * The arch code that enables or disables the function tracing
1693 * can call ftrace_bug() when it has detected a problem in
1694 * modifying the code. @failed should be one of either:
1695 * EFAULT - if the problem happens on reading the @ip address
1696 * EINVAL - if what is read at @ip is not what was expected
1697 * EPERM - if the problem happens on writting to the @ip address
1698 */
1699void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001700{
1701 switch (failed) {
1702 case -EFAULT:
1703 FTRACE_WARN_ON_ONCE(1);
1704 pr_info("ftrace faulted on modifying ");
1705 print_ip_sym(ip);
1706 break;
1707 case -EINVAL:
1708 FTRACE_WARN_ON_ONCE(1);
1709 pr_info("ftrace failed to modify ");
1710 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001711 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001712 printk(KERN_CONT "\n");
1713 break;
1714 case -EPERM:
1715 FTRACE_WARN_ON_ONCE(1);
1716 pr_info("ftrace faulted on writing ");
1717 print_ip_sym(ip);
1718 break;
1719 default:
1720 FTRACE_WARN_ON_ONCE(1);
1721 pr_info("ftrace faulted on unknown error ");
1722 print_ip_sym(ip);
1723 }
1724}
1725
Steven Rostedtc88fd862011-08-16 09:53:39 -04001726static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02001727{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001728 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001729
Steven Rostedt982c3502008-11-15 16:31:41 -05001730 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001731 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05001732 *
Steven Rostedted926f92011-05-03 13:25:24 -04001733 * If the record has a ref count, then we need to enable it
1734 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001735 *
Steven Rostedted926f92011-05-03 13:25:24 -04001736 * Otherwise we make sure its disabled.
1737 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001738 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04001739 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001740 */
Steven Rostedtc88fd862011-08-16 09:53:39 -04001741 if (enable && (rec->flags & ~FTRACE_FL_MASK))
Steven Rostedted926f92011-05-03 13:25:24 -04001742 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001743
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001744 /*
1745 * If enabling and the REGS flag does not match the REGS_EN, then
1746 * do not ignore this record. Set flags to fail the compare against
1747 * ENABLED.
1748 */
1749 if (flag &&
1750 (!(rec->flags & FTRACE_FL_REGS) != !(rec->flags & FTRACE_FL_REGS_EN)))
1751 flag |= FTRACE_FL_REGS;
1752
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001753 /* If the state of this record hasn't changed, then do nothing */
1754 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001755 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001756
1757 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001758 /* Save off if rec is being enabled (for return value) */
1759 flag ^= rec->flags & FTRACE_FL_ENABLED;
1760
1761 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04001762 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001763 if (flag & FTRACE_FL_REGS) {
1764 if (rec->flags & FTRACE_FL_REGS)
1765 rec->flags |= FTRACE_FL_REGS_EN;
1766 else
1767 rec->flags &= ~FTRACE_FL_REGS_EN;
1768 }
1769 }
1770
1771 /*
1772 * If this record is being updated from a nop, then
1773 * return UPDATE_MAKE_CALL.
1774 * Otherwise, if the EN flag is set, then return
1775 * UPDATE_MODIFY_CALL_REGS to tell the caller to convert
1776 * from the non-save regs, to a save regs function.
1777 * Otherwise,
1778 * return UPDATE_MODIFY_CALL to tell the caller to convert
1779 * from the save regs, to a non-save regs function.
1780 */
1781 if (flag & FTRACE_FL_ENABLED)
1782 return FTRACE_UPDATE_MAKE_CALL;
1783 else if (rec->flags & FTRACE_FL_REGS_EN)
1784 return FTRACE_UPDATE_MODIFY_CALL_REGS;
1785 else
1786 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001787 }
1788
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001789 if (update) {
1790 /* If there's no more users, clear all flags */
1791 if (!(rec->flags & ~FTRACE_FL_MASK))
1792 rec->flags = 0;
1793 else
1794 /* Just disable the record (keep REGS state) */
1795 rec->flags &= ~FTRACE_FL_ENABLED;
1796 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04001797
1798 return FTRACE_UPDATE_MAKE_NOP;
1799}
1800
1801/**
1802 * ftrace_update_record, set a record that now is tracing or not
1803 * @rec: the record to update
1804 * @enable: set to 1 if the record is tracing, zero to force disable
1805 *
1806 * The records that represent all functions that can be traced need
1807 * to be updated when tracing has been enabled.
1808 */
1809int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1810{
1811 return ftrace_check_record(rec, enable, 1);
1812}
1813
1814/**
1815 * ftrace_test_record, check if the record has been enabled or not
1816 * @rec: the record to test
1817 * @enable: set to 1 to check if enabled, 0 if it is disabled
1818 *
1819 * The arch code may need to test if a record is already set to
1820 * tracing to determine how to modify the function code that it
1821 * represents.
1822 */
1823int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1824{
1825 return ftrace_check_record(rec, enable, 0);
1826}
1827
1828static int
1829__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1830{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001831 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001832 unsigned long ftrace_addr;
1833 int ret;
1834
Steven Rostedtc88fd862011-08-16 09:53:39 -04001835 ret = ftrace_update_record(rec, enable);
1836
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001837 if (rec->flags & FTRACE_FL_REGS)
1838 ftrace_addr = (unsigned long)FTRACE_REGS_ADDR;
1839 else
1840 ftrace_addr = (unsigned long)FTRACE_ADDR;
1841
Steven Rostedtc88fd862011-08-16 09:53:39 -04001842 switch (ret) {
1843 case FTRACE_UPDATE_IGNORE:
1844 return 0;
1845
1846 case FTRACE_UPDATE_MAKE_CALL:
1847 return ftrace_make_call(rec, ftrace_addr);
1848
1849 case FTRACE_UPDATE_MAKE_NOP:
1850 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001851
1852 case FTRACE_UPDATE_MODIFY_CALL_REGS:
1853 case FTRACE_UPDATE_MODIFY_CALL:
1854 if (rec->flags & FTRACE_FL_REGS)
1855 ftrace_old_addr = (unsigned long)FTRACE_ADDR;
1856 else
1857 ftrace_old_addr = (unsigned long)FTRACE_REGS_ADDR;
1858
1859 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04001860 }
1861
1862 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02001863}
1864
Steven Rostedte4f5d542012-04-27 09:13:18 -04001865void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001866{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001867 struct dyn_ftrace *rec;
1868 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001869 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001870
Steven Rostedt45a4a232011-04-21 23:16:46 -04001871 if (unlikely(ftrace_disabled))
1872 return;
1873
Steven Rostedt265c8312009-02-13 12:43:56 -05001874 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedte4f5d542012-04-27 09:13:18 -04001875 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001876 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04001877 ftrace_bug(failed, rec->ip);
1878 /* Stop processing */
1879 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001880 }
1881 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001882}
1883
Steven Rostedtc88fd862011-08-16 09:53:39 -04001884struct ftrace_rec_iter {
1885 struct ftrace_page *pg;
1886 int index;
1887};
1888
1889/**
1890 * ftrace_rec_iter_start, start up iterating over traced functions
1891 *
1892 * Returns an iterator handle that is used to iterate over all
1893 * the records that represent address locations where functions
1894 * are traced.
1895 *
1896 * May return NULL if no records are available.
1897 */
1898struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1899{
1900 /*
1901 * We only use a single iterator.
1902 * Protected by the ftrace_lock mutex.
1903 */
1904 static struct ftrace_rec_iter ftrace_rec_iter;
1905 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1906
1907 iter->pg = ftrace_pages_start;
1908 iter->index = 0;
1909
1910 /* Could have empty pages */
1911 while (iter->pg && !iter->pg->index)
1912 iter->pg = iter->pg->next;
1913
1914 if (!iter->pg)
1915 return NULL;
1916
1917 return iter;
1918}
1919
1920/**
1921 * ftrace_rec_iter_next, get the next record to process.
1922 * @iter: The handle to the iterator.
1923 *
1924 * Returns the next iterator after the given iterator @iter.
1925 */
1926struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1927{
1928 iter->index++;
1929
1930 if (iter->index >= iter->pg->index) {
1931 iter->pg = iter->pg->next;
1932 iter->index = 0;
1933
1934 /* Could have empty pages */
1935 while (iter->pg && !iter->pg->index)
1936 iter->pg = iter->pg->next;
1937 }
1938
1939 if (!iter->pg)
1940 return NULL;
1941
1942 return iter;
1943}
1944
1945/**
1946 * ftrace_rec_iter_record, get the record at the iterator location
1947 * @iter: The current iterator location
1948 *
1949 * Returns the record that the current @iter is at.
1950 */
1951struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1952{
1953 return &iter->pg->records[iter->index];
1954}
1955
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301956static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001957ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001958{
1959 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001960 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001961
1962 ip = rec->ip;
1963
Steven Rostedt45a4a232011-04-21 23:16:46 -04001964 if (unlikely(ftrace_disabled))
1965 return 0;
1966
Shaohua Li25aac9d2009-01-09 11:29:40 +08001967 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001968 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001969 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301970 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02001971 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301972 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001973}
1974
Steven Rostedt000ab692009-02-17 13:35:06 -05001975/*
1976 * archs can override this function if they must do something
1977 * before the modifying code is performed.
1978 */
1979int __weak ftrace_arch_code_modify_prepare(void)
1980{
1981 return 0;
1982}
1983
1984/*
1985 * archs can override this function if they must do something
1986 * after the modifying code is performed.
1987 */
1988int __weak ftrace_arch_code_modify_post_process(void)
1989{
1990 return 0;
1991}
1992
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001993void ftrace_modify_all_code(int command)
1994{
Steven Rostedt (Red Hat)59338f72013-08-31 01:04:07 -04001995 int update = command & FTRACE_UPDATE_TRACE_FUNC;
1996
1997 /*
1998 * If the ftrace_caller calls a ftrace_ops func directly,
1999 * we need to make sure that it only traces functions it
2000 * expects to trace. When doing the switch of functions,
2001 * we need to update to the ftrace_ops_list_func first
2002 * before the transition between old and new calls are set,
2003 * as the ftrace_ops_list_func will check the ops hashes
2004 * to make sure the ops are having the right functions
2005 * traced.
2006 */
2007 if (update)
2008 ftrace_update_ftrace_func(ftrace_ops_list_func);
2009
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002010 if (command & FTRACE_UPDATE_CALLS)
2011 ftrace_replace_code(1);
2012 else if (command & FTRACE_DISABLE_CALLS)
2013 ftrace_replace_code(0);
2014
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002015 if (update && ftrace_trace_function != ftrace_ops_list_func) {
2016 function_trace_op = set_function_trace_op;
2017 smp_wmb();
2018 /* If irqs are disabled, we are in stop machine */
2019 if (!irqs_disabled())
2020 smp_call_function(ftrace_sync_ipi, NULL, 1);
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002021 ftrace_update_ftrace_func(ftrace_trace_function);
Steven Rostedt (Red Hat)405e1d82013-11-08 14:17:30 -05002022 }
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002023
2024 if (command & FTRACE_START_FUNC_RET)
2025 ftrace_enable_ftrace_graph_caller();
2026 else if (command & FTRACE_STOP_FUNC_RET)
2027 ftrace_disable_ftrace_graph_caller();
2028}
2029
Ingo Molnare309b412008-05-12 21:20:51 +02002030static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02002031{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002032 int *command = data;
2033
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04002034 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05002035
Steven Rostedtc88fd862011-08-16 09:53:39 -04002036 return 0;
2037}
2038
2039/**
2040 * ftrace_run_stop_machine, go back to the stop machine method
2041 * @command: The command to tell ftrace what to do
2042 *
2043 * If an arch needs to fall back to the stop machine method, the
2044 * it can call this function.
2045 */
2046void ftrace_run_stop_machine(int command)
2047{
2048 stop_machine(__ftrace_modify_code, &command, NULL);
2049}
2050
2051/**
2052 * arch_ftrace_update_code, modify the code to trace or not trace
2053 * @command: The command that needs to be done
2054 *
2055 * Archs can override this function if it does not need to
2056 * run stop_machine() to modify code.
2057 */
2058void __weak arch_ftrace_update_code(int command)
2059{
2060 ftrace_run_stop_machine(command);
2061}
2062
2063static void ftrace_run_update_code(int command)
2064{
2065 int ret;
2066
2067 ret = ftrace_arch_code_modify_prepare();
2068 FTRACE_WARN_ON(ret);
2069 if (ret)
2070 return;
2071 /*
2072 * Do not call function tracer while we update the code.
2073 * We are in stop machine.
2074 */
2075 function_trace_stop++;
2076
2077 /*
2078 * By default we use stop_machine() to modify the code.
2079 * But archs can do what ever they want as long as it
2080 * is safe. The stop_machine() is the safest, but also
2081 * produces the most overhead.
2082 */
2083 arch_ftrace_update_code(command);
2084
Steven Rostedt6331c282011-07-13 15:11:02 -04002085 function_trace_stop--;
2086
Steven Rostedt000ab692009-02-17 13:35:06 -05002087 ret = ftrace_arch_code_modify_post_process();
2088 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002089}
2090
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002091static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002092static int ftrace_start_up;
Steven Rostedtb8489142011-05-04 09:27:52 -04002093static int global_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002094
2095static void ftrace_startup_enable(int command)
2096{
2097 if (saved_ftrace_func != ftrace_trace_function) {
2098 saved_ftrace_func = ftrace_trace_function;
2099 command |= FTRACE_UPDATE_TRACE_FUNC;
2100 }
2101
2102 if (!command || !ftrace_enabled)
2103 return;
2104
2105 ftrace_run_update_code(command);
2106}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002107
Steven Rostedta1cd6172011-05-23 15:24:25 -04002108static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002109{
Steven Rostedtb8489142011-05-04 09:27:52 -04002110 bool hash_enable = true;
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002111 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002112
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002113 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002114 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002115
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002116 ret = __register_ftrace_function(ops);
2117 if (ret)
2118 return ret;
2119
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002120 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002121 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02002122
Steven Rostedtb8489142011-05-04 09:27:52 -04002123 /* ops marked global share the filter hashes */
2124 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2125 ops = &global_ops;
2126 /* Don't update hash if global is already set */
2127 if (global_start_up)
2128 hash_enable = false;
2129 global_start_up++;
2130 }
2131
Steven Rostedted926f92011-05-03 13:25:24 -04002132 ops->flags |= FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002133 if (hash_enable)
Steven Rostedted926f92011-05-03 13:25:24 -04002134 ftrace_hash_rec_enable(ops, 1);
2135
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002136 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002137
2138 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002139}
2140
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002141static int ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002142{
Steven Rostedtb8489142011-05-04 09:27:52 -04002143 bool hash_disable = true;
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002144 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04002145
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002146 if (unlikely(ftrace_disabled))
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002147 return -ENODEV;
2148
2149 ret = __unregister_ftrace_function(ops);
2150 if (ret)
2151 return ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002152
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002153 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002154 /*
2155 * Just warn in case of unbalance, no need to kill ftrace, it's not
2156 * critical but the ftrace_call callers may be never nopped again after
2157 * further ftrace uses.
2158 */
2159 WARN_ON_ONCE(ftrace_start_up < 0);
2160
Steven Rostedtb8489142011-05-04 09:27:52 -04002161 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2162 ops = &global_ops;
2163 global_start_up--;
2164 WARN_ON_ONCE(global_start_up < 0);
2165 /* Don't update hash if global still has users */
2166 if (global_start_up) {
2167 WARN_ON_ONCE(!ftrace_start_up);
2168 hash_disable = false;
2169 }
2170 }
2171
2172 if (hash_disable)
Steven Rostedted926f92011-05-03 13:25:24 -04002173 ftrace_hash_rec_disable(ops, 1);
2174
Steven Rostedtb8489142011-05-04 09:27:52 -04002175 if (ops != &global_ops || !global_start_up)
Steven Rostedted926f92011-05-03 13:25:24 -04002176 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002177
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002178 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002179
2180 if (saved_ftrace_func != ftrace_trace_function) {
2181 saved_ftrace_func = ftrace_trace_function;
2182 command |= FTRACE_UPDATE_TRACE_FUNC;
2183 }
2184
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002185 if (!command || !ftrace_enabled) {
2186 /*
2187 * If these are control ops, they still need their
2188 * per_cpu field freed. Since, function tracing is
2189 * not currently active, we can just free them
2190 * without synchronizing all CPUs.
2191 */
2192 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2193 control_ops_free(ops);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002194 return 0;
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002195 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002196
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002197 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)a4c35ed22014-01-13 12:56:21 -05002198
2199 /*
2200 * Dynamic ops may be freed, we must make sure that all
2201 * callers are done before leaving this function.
2202 * The same goes for freeing the per_cpu data of the control
2203 * ops.
2204 *
2205 * Again, normal synchronize_sched() is not good enough.
2206 * We need to do a hard force of sched synchronization.
2207 * This is because we use preempt_disable() to do RCU, but
2208 * the function tracers can be called where RCU is not watching
2209 * (like before user_exit()). We can not rely on the RCU
2210 * infrastructure to do the synchronization, thus we must do it
2211 * ourselves.
2212 */
2213 if (ops->flags & (FTRACE_OPS_FL_DYNAMIC | FTRACE_OPS_FL_CONTROL)) {
2214 schedule_on_each_cpu(ftrace_sync);
2215
2216 if (ops->flags & FTRACE_OPS_FL_CONTROL)
2217 control_ops_free(ops);
2218 }
2219
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05002220 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002221}
2222
Ingo Molnare309b412008-05-12 21:20:51 +02002223static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002224{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002225 if (unlikely(ftrace_disabled))
2226 return;
2227
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002228 /* Force update next time */
2229 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002230 /* ftrace_start_up is true if we want ftrace running */
2231 if (ftrace_start_up)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002232 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002233}
2234
Ingo Molnare309b412008-05-12 21:20:51 +02002235static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002236{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002237 if (unlikely(ftrace_disabled))
2238 return;
2239
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002240 /* ftrace_start_up is true if ftrace is running */
2241 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04002242 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002243}
2244
Steven Rostedt3d083392008-05-12 21:20:42 +02002245static cycle_t ftrace_update_time;
2246static unsigned long ftrace_update_cnt;
2247unsigned long ftrace_update_tot_cnt;
2248
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002249static inline int ops_traces_mod(struct ftrace_ops *ops)
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002250{
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002251 /*
2252 * Filter_hash being empty will default to trace module.
2253 * But notrace hash requires a test of individual module functions.
2254 */
2255 return ftrace_hash_empty(ops->filter_hash) &&
2256 ftrace_hash_empty(ops->notrace_hash);
2257}
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002258
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002259/*
2260 * Check if the current ops references the record.
2261 *
2262 * If the ops traces all functions, then it was already accounted for.
2263 * If the ops does not trace the current record function, skip it.
2264 * If the ops ignores the function via notrace filter, skip it.
2265 */
2266static inline bool
2267ops_references_rec(struct ftrace_ops *ops, struct dyn_ftrace *rec)
2268{
2269 /* If ops isn't enabled, ignore it */
2270 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
2271 return 0;
2272
2273 /* If ops traces all mods, we already accounted for it */
2274 if (ops_traces_mod(ops))
2275 return 0;
2276
2277 /* The function must be in the filter */
2278 if (!ftrace_hash_empty(ops->filter_hash) &&
2279 !ftrace_lookup_ip(ops->filter_hash, rec->ip))
2280 return 0;
2281
2282 /* If in notrace hash, we ignore it too */
2283 if (ftrace_lookup_ip(ops->notrace_hash, rec->ip))
2284 return 0;
2285
2286 return 1;
2287}
2288
2289static int referenced_filters(struct dyn_ftrace *rec)
2290{
2291 struct ftrace_ops *ops;
2292 int cnt = 0;
2293
2294 for (ops = ftrace_ops_list; ops != &ftrace_list_end; ops = ops->next) {
2295 if (ops_references_rec(ops, rec))
2296 cnt++;
2297 }
2298
2299 return cnt;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002300}
2301
Steven Rostedt31e88902008-11-14 16:21:19 -08002302static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02002303{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002304 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002305 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302306 cycle_t start, stop;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002307 unsigned long ref = 0;
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002308 bool test = false;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002309 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002310
2311 /*
2312 * When adding a module, we need to check if tracers are
2313 * currently enabled and if they are set to trace all functions.
2314 * If they are, we need to enable the module functions as well
2315 * as update the reference counts for those function records.
2316 */
2317 if (mod) {
2318 struct ftrace_ops *ops;
2319
2320 for (ops = ftrace_ops_list;
2321 ops != &ftrace_list_end; ops = ops->next) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002322 if (ops->flags & FTRACE_OPS_FL_ENABLED) {
2323 if (ops_traces_mod(ops))
2324 ref++;
2325 else
2326 test = true;
2327 }
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002328 }
2329 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002330
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002331 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002332 ftrace_update_cnt = 0;
2333
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002334 for (pg = ftrace_new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302335
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002336 for (i = 0; i < pg->index; i++) {
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002337 int cnt = ref;
2338
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002339 /* If something went wrong, bail without enabling anything */
2340 if (unlikely(ftrace_disabled))
2341 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002342
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002343 p = &pg->records[i];
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002344 if (test)
2345 cnt += referenced_filters(p);
2346 p->flags = cnt;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302347
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002348 /*
2349 * Do the initial record conversion from mcount jump
2350 * to the NOP instructions.
2351 */
2352 if (!ftrace_code_disable(mod, p))
2353 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002354
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002355 ftrace_update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002356
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002357 /*
2358 * If the tracing is enabled, go ahead and enable the record.
2359 *
2360 * The reason not to enable the record immediatelly is the
2361 * inherent check of ftrace_make_nop/ftrace_make_call for
2362 * correct previous instructions. Making first the NOP
2363 * conversion puts the module to the correct state, thus
2364 * passing the ftrace_make_call check.
2365 */
Steven Rostedt (Red Hat)8c4f3c32013-07-30 00:04:32 -04002366 if (ftrace_start_up && cnt) {
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002367 int failed = __ftrace_replace_code(p, 1);
2368 if (failed)
2369 ftrace_bug(failed, p->ip);
2370 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002371 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002372 }
2373
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002374 ftrace_new_pgs = NULL;
2375
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002376 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002377 ftrace_update_time = stop - start;
2378 ftrace_update_tot_cnt += ftrace_update_cnt;
2379
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002380 return 0;
2381}
2382
Steven Rostedta7900872011-12-16 16:23:44 -05002383static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002384{
Steven Rostedta7900872011-12-16 16:23:44 -05002385 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002386 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002387
Steven Rostedta7900872011-12-16 16:23:44 -05002388 if (WARN_ON(!count))
2389 return -EINVAL;
2390
2391 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002392
2393 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002394 * We want to fill as much as possible. No more than a page
2395 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002396 */
Steven Rostedta7900872011-12-16 16:23:44 -05002397 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2398 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002399
Steven Rostedta7900872011-12-16 16:23:44 -05002400 again:
2401 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2402
2403 if (!pg->records) {
2404 /* if we can't allocate this size, try something smaller */
2405 if (!order)
2406 return -ENOMEM;
2407 order >>= 1;
2408 goto again;
2409 }
2410
2411 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2412 pg->size = cnt;
2413
2414 if (cnt > count)
2415 cnt = count;
2416
2417 return cnt;
2418}
2419
2420static struct ftrace_page *
2421ftrace_allocate_pages(unsigned long num_to_init)
2422{
2423 struct ftrace_page *start_pg;
2424 struct ftrace_page *pg;
2425 int order;
2426 int cnt;
2427
2428 if (!num_to_init)
2429 return 0;
2430
2431 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2432 if (!pg)
2433 return NULL;
2434
2435 /*
2436 * Try to allocate as much as possible in one continues
2437 * location that fills in all of the space. We want to
2438 * waste as little space as possible.
2439 */
2440 for (;;) {
2441 cnt = ftrace_allocate_records(pg, num_to_init);
2442 if (cnt < 0)
2443 goto free_pages;
2444
2445 num_to_init -= cnt;
2446 if (!num_to_init)
2447 break;
2448
2449 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2450 if (!pg->next)
2451 goto free_pages;
2452
2453 pg = pg->next;
2454 }
2455
2456 return start_pg;
2457
2458 free_pages:
2459 while (start_pg) {
2460 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2461 free_pages((unsigned long)pg->records, order);
2462 start_pg = pg->next;
2463 kfree(pg);
2464 pg = start_pg;
2465 }
2466 pr_info("ftrace: FAILED to allocate memory for functions\n");
2467 return NULL;
2468}
2469
2470static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2471{
2472 int cnt;
2473
2474 if (!num_to_init) {
2475 pr_info("ftrace: No functions to be traced?\n");
2476 return -1;
2477 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002478
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002479 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04002480 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08002481 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002482
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002483 return 0;
2484}
2485
Steven Rostedt5072c592008-05-12 21:20:43 +02002486#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2487
2488struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002489 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002490 loff_t func_pos;
2491 struct ftrace_page *pg;
2492 struct dyn_ftrace *func;
2493 struct ftrace_func_probe *probe;
2494 struct trace_parser parser;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002495 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002496 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002497 int hidx;
2498 int idx;
2499 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002500};
2501
Ingo Molnare309b412008-05-12 21:20:51 +02002502static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002503t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002504{
2505 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002506 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002507 struct hlist_head *hhd;
2508
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002509 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002510 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002511
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002512 if (iter->probe)
2513 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002514 retry:
2515 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2516 return NULL;
2517
2518 hhd = &ftrace_func_hash[iter->hidx];
2519
2520 if (hlist_empty(hhd)) {
2521 iter->hidx++;
2522 hnd = NULL;
2523 goto retry;
2524 }
2525
2526 if (!hnd)
2527 hnd = hhd->first;
2528 else {
2529 hnd = hnd->next;
2530 if (!hnd) {
2531 iter->hidx++;
2532 goto retry;
2533 }
2534 }
2535
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002536 if (WARN_ON_ONCE(!hnd))
2537 return NULL;
2538
2539 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2540
2541 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002542}
2543
2544static void *t_hash_start(struct seq_file *m, loff_t *pos)
2545{
2546 struct ftrace_iterator *iter = m->private;
2547 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08002548 loff_t l;
2549
Steven Rostedt69a30832011-12-19 15:21:16 -05002550 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2551 return NULL;
2552
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002553 if (iter->func_pos > *pos)
2554 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002555
Li Zefand82d6242009-06-24 09:54:54 +08002556 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002557 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002558 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08002559 if (!p)
2560 break;
2561 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002562 if (!p)
2563 return NULL;
2564
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002565 /* Only set this if we have an item */
2566 iter->flags |= FTRACE_ITER_HASH;
2567
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002568 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002569}
2570
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002571static int
2572t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002573{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002574 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002575
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002576 rec = iter->probe;
2577 if (WARN_ON_ONCE(!rec))
2578 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002579
Steven Rostedt809dcf22009-02-16 23:06:01 -05002580 if (rec->ops->print)
2581 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2582
Steven Rostedtb375a112009-09-17 00:05:58 -04002583 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002584
2585 if (rec->data)
2586 seq_printf(m, ":%p", rec->data);
2587 seq_putc(m, '\n');
2588
2589 return 0;
2590}
2591
2592static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02002593t_next(struct seq_file *m, void *v, loff_t *pos)
2594{
2595 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002596 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002597 struct dyn_ftrace *rec = NULL;
2598
Steven Rostedt45a4a232011-04-21 23:16:46 -04002599 if (unlikely(ftrace_disabled))
2600 return NULL;
2601
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002602 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002603 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002604
Steven Rostedt5072c592008-05-12 21:20:43 +02002605 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01002606 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02002607
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002608 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002609 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002610
Steven Rostedt5072c592008-05-12 21:20:43 +02002611 retry:
2612 if (iter->idx >= iter->pg->index) {
2613 if (iter->pg->next) {
2614 iter->pg = iter->pg->next;
2615 iter->idx = 0;
2616 goto retry;
2617 }
2618 } else {
2619 rec = &iter->pg->records[iter->idx++];
Steven Rostedt32082302011-12-16 14:42:37 -05002620 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedtf45948e2011-05-02 12:29:25 -04002621 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
Steven Rostedt0183fb12008-11-07 22:36:02 -05002622
Steven Rostedt41c52c02008-05-22 11:46:33 -04002623 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt647bcd02011-05-03 14:39:21 -04002624 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2625
2626 ((iter->flags & FTRACE_ITER_ENABLED) &&
Steven Rostedt (Red Hat)23ea9c42013-05-09 19:31:48 -04002627 !(rec->flags & FTRACE_FL_ENABLED))) {
Steven Rostedt647bcd02011-05-03 14:39:21 -04002628
Steven Rostedt5072c592008-05-12 21:20:43 +02002629 rec = NULL;
2630 goto retry;
2631 }
2632 }
2633
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002634 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002635 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002636
2637 iter->func = rec;
2638
2639 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002640}
2641
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002642static void reset_iter_read(struct ftrace_iterator *iter)
2643{
2644 iter->pos = 0;
2645 iter->func_pos = 0;
Dan Carpenter70f77b32012-06-09 19:10:27 +03002646 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002647}
2648
2649static void *t_start(struct seq_file *m, loff_t *pos)
2650{
2651 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002652 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002653 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002654 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002655
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002656 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002657
2658 if (unlikely(ftrace_disabled))
2659 return NULL;
2660
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002661 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002662 * If an lseek was done, then reset and start from beginning.
2663 */
2664 if (*pos < iter->pos)
2665 reset_iter_read(iter);
2666
2667 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002668 * For set_ftrace_filter reading, if we have the filter
2669 * off, we can short cut and just print out that all
2670 * functions are enabled.
2671 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05002672 if (iter->flags & FTRACE_ITER_FILTER &&
2673 ftrace_hash_empty(ops->filter_hash)) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002674 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002675 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002676 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002677 /* reset in case of seek/pread */
2678 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002679 return iter;
2680 }
2681
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002682 if (iter->flags & FTRACE_ITER_HASH)
2683 return t_hash_start(m, pos);
2684
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002685 /*
2686 * Unfortunately, we need to restart at ftrace_pages_start
2687 * every time we let go of the ftrace_mutex. This is because
2688 * those pointers can change without the lock.
2689 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002690 iter->pg = ftrace_pages_start;
2691 iter->idx = 0;
2692 for (l = 0; l <= *pos; ) {
2693 p = t_next(m, p, &l);
2694 if (!p)
2695 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002696 }
walimis5821e1b2008-11-15 15:19:06 +08002697
Steven Rostedt69a30832011-12-19 15:21:16 -05002698 if (!p)
2699 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002700
2701 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002702}
2703
2704static void t_stop(struct seq_file *m, void *p)
2705{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002706 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002707}
2708
2709static int t_show(struct seq_file *m, void *v)
2710{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002711 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002712 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02002713
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002714 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002715 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002716
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002717 if (iter->flags & FTRACE_ITER_PRINTALL) {
2718 seq_printf(m, "#### all functions enabled ####\n");
2719 return 0;
2720 }
2721
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002722 rec = iter->func;
2723
Steven Rostedt5072c592008-05-12 21:20:43 +02002724 if (!rec)
2725 return 0;
2726
Steven Rostedt647bcd02011-05-03 14:39:21 -04002727 seq_printf(m, "%ps", (void *)rec->ip);
2728 if (iter->flags & FTRACE_ITER_ENABLED)
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002729 seq_printf(m, " (%ld)%s",
2730 rec->flags & ~FTRACE_FL_MASK,
2731 rec->flags & FTRACE_FL_REGS ? " R" : "");
Steven Rostedt647bcd02011-05-03 14:39:21 -04002732 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02002733
2734 return 0;
2735}
2736
James Morris88e9d342009-09-22 16:43:43 -07002737static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002738 .start = t_start,
2739 .next = t_next,
2740 .stop = t_stop,
2741 .show = t_show,
2742};
2743
Ingo Molnare309b412008-05-12 21:20:51 +02002744static int
Steven Rostedt5072c592008-05-12 21:20:43 +02002745ftrace_avail_open(struct inode *inode, struct file *file)
2746{
2747 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002748
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002749 if (unlikely(ftrace_disabled))
2750 return -ENODEV;
2751
Jiri Olsa50e18b92012-04-25 10:23:39 +02002752 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2753 if (iter) {
2754 iter->pg = ftrace_pages_start;
2755 iter->ops = &global_ops;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002756 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002757
Jiri Olsa50e18b92012-04-25 10:23:39 +02002758 return iter ? 0 : -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02002759}
2760
Steven Rostedt647bcd02011-05-03 14:39:21 -04002761static int
2762ftrace_enabled_open(struct inode *inode, struct file *file)
2763{
2764 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002765
2766 if (unlikely(ftrace_disabled))
2767 return -ENODEV;
2768
Jiri Olsa50e18b92012-04-25 10:23:39 +02002769 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2770 if (iter) {
2771 iter->pg = ftrace_pages_start;
2772 iter->flags = FTRACE_ITER_ENABLED;
2773 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002774 }
2775
Jiri Olsa50e18b92012-04-25 10:23:39 +02002776 return iter ? 0 : -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002777}
2778
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002779static void ftrace_filter_reset(struct ftrace_hash *hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02002780{
Steven Rostedt52baf112009-02-14 01:15:39 -05002781 mutex_lock(&ftrace_lock);
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002782 ftrace_hash_clear(hash);
Steven Rostedt52baf112009-02-14 01:15:39 -05002783 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002784}
2785
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002786/**
2787 * ftrace_regex_open - initialize function tracer filter files
2788 * @ops: The ftrace_ops that hold the hash filters
2789 * @flag: The type of filter to process
2790 * @inode: The inode, usually passed in to your open routine
2791 * @file: The file, usually passed in to your open routine
2792 *
2793 * ftrace_regex_open() initializes the filter files for the
2794 * @ops. Depending on @flag it may process the filter hash or
2795 * the notrace hash of @ops. With this called from the open
2796 * routine, you can use ftrace_filter_write() for the write
2797 * routine if @flag has FTRACE_ITER_FILTER set, or
2798 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05002799 * tracing_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002800 * release must call ftrace_regex_release().
2801 */
2802int
Steven Rostedtf45948e2011-05-02 12:29:25 -04002803ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002804 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02002805{
2806 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002807 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02002808 int ret = 0;
2809
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09002810 ftrace_ops_init(ops);
2811
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002812 if (unlikely(ftrace_disabled))
2813 return -ENODEV;
2814
Steven Rostedt5072c592008-05-12 21:20:43 +02002815 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2816 if (!iter)
2817 return -ENOMEM;
2818
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002819 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2820 kfree(iter);
2821 return -ENOMEM;
2822 }
2823
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09002824 iter->ops = ops;
2825 iter->flags = flag;
2826
2827 mutex_lock(&ops->regex_lock);
2828
Steven Rostedtf45948e2011-05-02 12:29:25 -04002829 if (flag & FTRACE_ITER_NOTRACE)
2830 hash = ops->notrace_hash;
2831 else
2832 hash = ops->filter_hash;
2833
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002834 if (file->f_mode & FMODE_WRITE) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002835 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002836 if (!iter->hash) {
2837 trace_parser_put(&iter->parser);
2838 kfree(iter);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09002839 ret = -ENOMEM;
2840 goto out_unlock;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002841 }
2842 }
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002843
Steven Rostedt5072c592008-05-12 21:20:43 +02002844 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002845 (file->f_flags & O_TRUNC))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002846 ftrace_filter_reset(iter->hash);
Steven Rostedt5072c592008-05-12 21:20:43 +02002847
2848 if (file->f_mode & FMODE_READ) {
2849 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02002850
2851 ret = seq_open(file, &show_ftrace_seq_ops);
2852 if (!ret) {
2853 struct seq_file *m = file->private_data;
2854 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08002855 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002856 /* Failed */
2857 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08002858 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002859 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08002860 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002861 } else
2862 file->private_data = iter;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09002863
2864 out_unlock:
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09002865 mutex_unlock(&ops->regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002866
2867 return ret;
2868}
2869
Steven Rostedt41c52c02008-05-22 11:46:33 -04002870static int
2871ftrace_filter_open(struct inode *inode, struct file *file)
2872{
Steven Rostedt69a30832011-12-19 15:21:16 -05002873 return ftrace_regex_open(&global_ops,
2874 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2875 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002876}
2877
2878static int
2879ftrace_notrace_open(struct inode *inode, struct file *file)
2880{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002881 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002882 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002883}
2884
Steven Rostedt64e7c442009-02-13 17:08:48 -05002885static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002886{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002887 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08002888 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002889
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002890 switch (type) {
2891 case MATCH_FULL:
2892 if (strcmp(str, regex) == 0)
2893 matched = 1;
2894 break;
2895 case MATCH_FRONT_ONLY:
2896 if (strncmp(str, regex, len) == 0)
2897 matched = 1;
2898 break;
2899 case MATCH_MIDDLE_ONLY:
2900 if (strstr(str, regex))
2901 matched = 1;
2902 break;
2903 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08002904 slen = strlen(str);
2905 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002906 matched = 1;
2907 break;
2908 }
2909
2910 return matched;
2911}
2912
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002913static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002914enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04002915{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002916 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002917 int ret = 0;
2918
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002919 entry = ftrace_lookup_ip(hash, rec->ip);
2920 if (not) {
2921 /* Do nothing if it doesn't exist */
2922 if (!entry)
2923 return 0;
2924
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002925 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002926 } else {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002927 /* Do nothing if it exists */
2928 if (entry)
2929 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002930
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002931 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002932 }
2933 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04002934}
2935
Steven Rostedt64e7c442009-02-13 17:08:48 -05002936static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002937ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2938 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002939{
2940 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002941 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002942
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002943 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2944
2945 if (mod) {
2946 /* module lookup requires matching the module */
2947 if (!modname || strcmp(modname, mod))
2948 return 0;
2949
2950 /* blank search means to match all funcs in the mod */
2951 if (!len)
2952 return 1;
2953 }
2954
Steven Rostedt64e7c442009-02-13 17:08:48 -05002955 return ftrace_match(str, regex, len, type);
2956}
2957
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002958static int
2959match_records(struct ftrace_hash *hash, char *buff,
2960 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002961{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002962 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002963 struct ftrace_page *pg;
2964 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002965 int type = MATCH_FULL;
2966 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08002967 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002968 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002969
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002970 if (len) {
2971 type = filter_parse_regex(buff, len, &search, &not);
2972 search_len = strlen(search);
2973 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002974
Steven Rostedt52baf112009-02-14 01:15:39 -05002975 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002976
2977 if (unlikely(ftrace_disabled))
2978 goto out_unlock;
2979
Steven Rostedt265c8312009-02-13 12:43:56 -05002980 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002981 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002982 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002983 if (ret < 0) {
2984 found = ret;
2985 goto out_unlock;
2986 }
Li Zefan311d16d2009-12-08 11:15:11 +08002987 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05002988 }
2989 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002990 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05002991 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08002992
2993 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02002994}
2995
Steven Rostedt64e7c442009-02-13 17:08:48 -05002996static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002997ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002998{
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002999 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003000}
3001
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003002static int
3003ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003004{
Steven Rostedt64e7c442009-02-13 17:08:48 -05003005 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003006
Steven Rostedt64e7c442009-02-13 17:08:48 -05003007 /* blank or '*' mean the same */
3008 if (strcmp(buff, "*") == 0)
3009 buff[0] = 0;
3010
3011 /* handle the case of 'dont filter this module' */
3012 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
3013 buff[0] = 0;
3014 not = 1;
3015 }
3016
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003017 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003018}
3019
Steven Rostedtf6180772009-02-14 00:40:25 -05003020/*
3021 * We register the module command as a template to show others how
3022 * to register the a command as well.
3023 */
3024
3025static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003026ftrace_mod_callback(struct ftrace_hash *hash,
3027 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05003028{
3029 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003030 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05003031
3032 /*
3033 * cmd == 'mod' because we only registered this func
3034 * for the 'mod' ftrace_func_command.
3035 * But if you register one func with multiple commands,
3036 * you can tell which command was used by the cmd
3037 * parameter.
3038 */
3039
3040 /* we must have a module name */
3041 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003042 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05003043
3044 mod = strsep(&param, ":");
3045 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003046 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05003047
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003048 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003049 if (!ret)
3050 ret = -EINVAL;
3051 if (ret < 0)
3052 return ret;
3053
3054 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05003055}
3056
3057static struct ftrace_func_command ftrace_mod_cmd = {
3058 .name = "mod",
3059 .func = ftrace_mod_callback,
3060};
3061
3062static int __init ftrace_mod_cmd_init(void)
3063{
3064 return register_ftrace_command(&ftrace_mod_cmd);
3065}
Steven Rostedt6f415672012-10-05 12:13:07 -04003066core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05003067
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04003068static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04003069 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003070{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003071 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003072 struct hlist_head *hhd;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003073 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003074
3075 key = hash_long(ip, FTRACE_HASH_BITS);
3076
3077 hhd = &ftrace_func_hash[key];
3078
3079 if (hlist_empty(hhd))
3080 return;
3081
3082 /*
3083 * Disable preemption for these calls to prevent a RCU grace
3084 * period. This syncs the hash iteration and freeing of items
3085 * on the hash. rcu_read_lock is too dangerous here.
3086 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04003087 preempt_disable_notrace();
Steven Rostedt1bb539c2013-05-28 14:38:43 -04003088 hlist_for_each_entry_rcu_notrace(entry, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003089 if (entry->ip == ip)
3090 entry->ops->func(ip, parent_ip, &entry->data);
3091 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04003092 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003093}
3094
Steven Rostedtb6887d72009-02-17 12:32:04 -05003095static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05003096{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04003097 .func = function_trace_probe_call,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003098 .flags = FTRACE_OPS_FL_INITIALIZED,
3099 INIT_REGEX_LOCK(trace_probe_ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003100};
3101
Steven Rostedtb6887d72009-02-17 12:32:04 -05003102static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003103
Steven Rostedtb6887d72009-02-17 12:32:04 -05003104static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003105{
Steven Rostedtb8489142011-05-04 09:27:52 -04003106 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003107 int i;
3108
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003109 if (ftrace_probe_registered) {
3110 /* still need to update the function call sites */
3111 if (ftrace_enabled)
3112 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003113 return;
Steven Rostedt (Red Hat)19dd6032013-05-09 19:37:36 -04003114 }
Steven Rostedt59df055f2009-02-14 15:29:06 -05003115
3116 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3117 struct hlist_head *hhd = &ftrace_func_hash[i];
3118 if (hhd->first)
3119 break;
3120 }
3121 /* Nothing registered? */
3122 if (i == FTRACE_FUNC_HASHSIZE)
3123 return;
3124
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003125 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003126
Steven Rostedtb6887d72009-02-17 12:32:04 -05003127 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003128}
3129
Steven Rostedtb6887d72009-02-17 12:32:04 -05003130static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003131{
3132 int i;
3133
Steven Rostedtb6887d72009-02-17 12:32:04 -05003134 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003135 return;
3136
3137 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3138 struct hlist_head *hhd = &ftrace_func_hash[i];
3139 if (hhd->first)
3140 return;
3141 }
3142
3143 /* no more funcs left */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05003144 ftrace_shutdown(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04003145
Steven Rostedtb6887d72009-02-17 12:32:04 -05003146 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003147}
3148
3149
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003150static void ftrace_free_entry(struct ftrace_func_probe *entry)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003151{
Steven Rostedt59df055f2009-02-14 15:29:06 -05003152 if (entry->ops->free)
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003153 entry->ops->free(entry->ops, entry->ip, &entry->data);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003154 kfree(entry);
3155}
3156
Steven Rostedt59df055f2009-02-14 15:29:06 -05003157int
Steven Rostedtb6887d72009-02-17 12:32:04 -05003158register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003159 void *data)
3160{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003161 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003162 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
3163 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003164 struct ftrace_page *pg;
3165 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003166 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003167 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003168 int count = 0;
3169 char *search;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003170 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003171
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003172 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003173 len = strlen(search);
3174
Steven Rostedtb6887d72009-02-17 12:32:04 -05003175 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003176 if (WARN_ON(not))
3177 return -EINVAL;
3178
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003179 mutex_lock(&trace_probe_ops.regex_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003180
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003181 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3182 if (!hash) {
3183 count = -ENOMEM;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003184 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003185 }
3186
3187 if (unlikely(ftrace_disabled)) {
3188 count = -ENODEV;
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003189 goto out;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003190 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003191
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003192 mutex_lock(&ftrace_lock);
3193
Steven Rostedt59df055f2009-02-14 15:29:06 -05003194 do_for_each_ftrace_rec(pg, rec) {
3195
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003196 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003197 continue;
3198
3199 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3200 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003201 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003202 if (!count)
3203 count = -ENOMEM;
3204 goto out_unlock;
3205 }
3206
3207 count++;
3208
3209 entry->data = data;
3210
3211 /*
3212 * The caller might want to do something special
3213 * for each function we find. We call the callback
3214 * to give the caller an opportunity to do so.
3215 */
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003216 if (ops->init) {
3217 if (ops->init(ops, rec->ip, &entry->data) < 0) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003218 /* caller does not like this func */
3219 kfree(entry);
3220 continue;
3221 }
3222 }
3223
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003224 ret = enter_record(hash, rec, 0);
3225 if (ret < 0) {
3226 kfree(entry);
3227 count = ret;
3228 goto out_unlock;
3229 }
3230
Steven Rostedt59df055f2009-02-14 15:29:06 -05003231 entry->ops = ops;
3232 entry->ip = rec->ip;
3233
3234 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3235 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3236
3237 } while_for_each_ftrace_rec();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003238
3239 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3240 if (ret < 0)
3241 count = ret;
3242
Steven Rostedtb6887d72009-02-17 12:32:04 -05003243 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003244
3245 out_unlock:
Steven Rostedt (Red Hat)5ae0bf52013-05-09 18:20:37 -04003246 mutex_unlock(&ftrace_lock);
3247 out:
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003248 mutex_unlock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003249 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003250
3251 return count;
3252}
3253
3254enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003255 PROBE_TEST_FUNC = 1,
3256 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05003257};
3258
3259static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003260__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003261 void *data, int flags)
3262{
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003263 struct ftrace_func_entry *rec_entry;
Steven Rostedtb6887d72009-02-17 12:32:04 -05003264 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003265 struct ftrace_func_probe *p;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003266 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003267 struct list_head free_list;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003268 struct ftrace_hash *hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003269 struct hlist_node *tmp;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003270 char str[KSYM_SYMBOL_LEN];
3271 int type = MATCH_FULL;
3272 int i, len = 0;
3273 char *search;
3274
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003275 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003276 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003277 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003278 int not;
3279
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003280 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003281 len = strlen(search);
3282
Steven Rostedtb6887d72009-02-17 12:32:04 -05003283 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003284 if (WARN_ON(not))
3285 return;
3286 }
3287
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003288 mutex_lock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003289
3290 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3291 if (!hash)
3292 /* Hmm, should report this somehow */
3293 goto out_unlock;
3294
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003295 INIT_LIST_HEAD(&free_list);
3296
Steven Rostedt59df055f2009-02-14 15:29:06 -05003297 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3298 struct hlist_head *hhd = &ftrace_func_hash[i];
3299
Sasha Levinb67bfe02013-02-27 17:06:00 -08003300 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003301
3302 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05003303 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003304 continue;
3305
Steven Rostedtb6887d72009-02-17 12:32:04 -05003306 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003307 continue;
3308
3309 /* do this last, since it is the most expensive */
3310 if (glob) {
3311 kallsyms_lookup(entry->ip, NULL, NULL,
3312 NULL, str);
3313 if (!ftrace_match(str, glob, len, type))
3314 continue;
3315 }
3316
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003317 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3318 /* It is possible more than one entry had this ip */
3319 if (rec_entry)
3320 free_hash_entry(hash, rec_entry);
3321
Steven Rostedt (Red Hat)740466b2013-03-13 11:15:19 -04003322 hlist_del_rcu(&entry->node);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003323 list_add(&entry->free_list, &free_list);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003324 }
3325 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003326 mutex_lock(&ftrace_lock);
Steven Rostedtb6887d72009-02-17 12:32:04 -05003327 __disable_ftrace_function_probe();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003328 /*
3329 * Remove after the disable is called. Otherwise, if the last
3330 * probe is removed, a null hash means *all enabled*.
3331 */
3332 ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003333 synchronize_sched();
3334 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3335 list_del(&entry->free_list);
3336 ftrace_free_entry(entry);
3337 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003338 mutex_unlock(&ftrace_lock);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003339
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003340 out_unlock:
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003341 mutex_unlock(&trace_probe_ops.regex_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003342 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003343}
3344
3345void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003346unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003347 void *data)
3348{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003349 __unregister_ftrace_function_probe(glob, ops, data,
3350 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003351}
3352
3353void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003354unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003355{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003356 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003357}
3358
Steven Rostedtb6887d72009-02-17 12:32:04 -05003359void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003360{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003361 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003362}
3363
Steven Rostedtf6180772009-02-14 00:40:25 -05003364static LIST_HEAD(ftrace_commands);
3365static DEFINE_MUTEX(ftrace_cmd_mutex);
3366
Tom Zanussi38de93a2013-10-24 08:34:18 -05003367/*
3368 * Currently we only register ftrace commands from __init, so mark this
3369 * __init too.
3370 */
3371__init int register_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003372{
3373 struct ftrace_func_command *p;
3374 int ret = 0;
3375
3376 mutex_lock(&ftrace_cmd_mutex);
3377 list_for_each_entry(p, &ftrace_commands, list) {
3378 if (strcmp(cmd->name, p->name) == 0) {
3379 ret = -EBUSY;
3380 goto out_unlock;
3381 }
3382 }
3383 list_add(&cmd->list, &ftrace_commands);
3384 out_unlock:
3385 mutex_unlock(&ftrace_cmd_mutex);
3386
3387 return ret;
3388}
3389
Tom Zanussi38de93a2013-10-24 08:34:18 -05003390/*
3391 * Currently we only unregister ftrace commands from __init, so mark
3392 * this __init too.
3393 */
3394__init int unregister_ftrace_command(struct ftrace_func_command *cmd)
Steven Rostedtf6180772009-02-14 00:40:25 -05003395{
3396 struct ftrace_func_command *p, *n;
3397 int ret = -ENODEV;
3398
3399 mutex_lock(&ftrace_cmd_mutex);
3400 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3401 if (strcmp(cmd->name, p->name) == 0) {
3402 ret = 0;
3403 list_del_init(&p->list);
3404 goto out_unlock;
3405 }
3406 }
3407 out_unlock:
3408 mutex_unlock(&ftrace_cmd_mutex);
3409
3410 return ret;
3411}
3412
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003413static int ftrace_process_regex(struct ftrace_hash *hash,
3414 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003415{
Steven Rostedtf6180772009-02-14 00:40:25 -05003416 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003417 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08003418 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003419
3420 func = strsep(&next, ":");
3421
3422 if (!next) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003423 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003424 if (!ret)
3425 ret = -EINVAL;
3426 if (ret < 0)
3427 return ret;
3428 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003429 }
3430
Steven Rostedtf6180772009-02-14 00:40:25 -05003431 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05003432
3433 command = strsep(&next, ":");
3434
Steven Rostedtf6180772009-02-14 00:40:25 -05003435 mutex_lock(&ftrace_cmd_mutex);
3436 list_for_each_entry(p, &ftrace_commands, list) {
3437 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003438 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05003439 goto out_unlock;
3440 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05003441 }
Steven Rostedtf6180772009-02-14 00:40:25 -05003442 out_unlock:
3443 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003444
Steven Rostedtf6180772009-02-14 00:40:25 -05003445 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003446}
3447
Ingo Molnare309b412008-05-12 21:20:51 +02003448static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003449ftrace_regex_write(struct file *file, const char __user *ubuf,
3450 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02003451{
3452 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003453 struct trace_parser *parser;
3454 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02003455
Li Zefan4ba79782009-09-22 13:52:20 +08003456 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02003457 return 0;
3458
Steven Rostedt5072c592008-05-12 21:20:43 +02003459 if (file->f_mode & FMODE_READ) {
3460 struct seq_file *m = file->private_data;
3461 iter = m->private;
3462 } else
3463 iter = file->private_data;
3464
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003465 if (unlikely(ftrace_disabled))
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003466 return -ENODEV;
3467
3468 /* iter->hash is a local copy, so we don't need regex_lock */
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003469
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003470 parser = &iter->parser;
3471 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02003472
Li Zefan4ba79782009-09-22 13:52:20 +08003473 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003474 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003475 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003476 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08003477 trace_parser_clear(parser);
Steven Rostedt (Red Hat)7c088b52013-05-09 11:35:12 -04003478 if (ret < 0)
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003479 goto out;
Steven Rostedt5072c592008-05-12 21:20:43 +02003480 }
3481
Steven Rostedt5072c592008-05-12 21:20:43 +02003482 ret = read;
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003483 out:
Steven Rostedt5072c592008-05-12 21:20:43 +02003484 return ret;
3485}
3486
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003487ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003488ftrace_filter_write(struct file *file, const char __user *ubuf,
3489 size_t cnt, loff_t *ppos)
3490{
3491 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3492}
3493
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003494ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003495ftrace_notrace_write(struct file *file, const char __user *ubuf,
3496 size_t cnt, loff_t *ppos)
3497{
3498 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3499}
3500
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003501static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003502ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3503{
3504 struct ftrace_func_entry *entry;
3505
3506 if (!ftrace_location(ip))
3507 return -EINVAL;
3508
3509 if (remove) {
3510 entry = ftrace_lookup_ip(hash, ip);
3511 if (!entry)
3512 return -ENOENT;
3513 free_hash_entry(hash, entry);
3514 return 0;
3515 }
3516
3517 return add_hash_entry(hash, ip);
3518}
3519
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003520static void ftrace_ops_update_code(struct ftrace_ops *ops)
3521{
3522 if (ops->flags & FTRACE_OPS_FL_ENABLED && ftrace_enabled)
3523 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
3524}
3525
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003526static int
3527ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3528 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003529{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003530 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003531 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003532 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003533
Steven Rostedt936e0742011-05-05 22:54:01 -04003534 /* All global ops uses the global ops filters */
3535 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3536 ops = &global_ops;
3537
Steven Rostedt41c52c02008-05-22 11:46:33 -04003538 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003539 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003540
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003541 mutex_lock(&ops->regex_lock);
3542
Steven Rostedtf45948e2011-05-02 12:29:25 -04003543 if (enable)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003544 orig_hash = &ops->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003545 else
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003546 orig_hash = &ops->notrace_hash;
3547
3548 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003549 if (!hash) {
3550 ret = -ENOMEM;
3551 goto out_regex_unlock;
3552 }
Steven Rostedtf45948e2011-05-02 12:29:25 -04003553
Steven Rostedt41c52c02008-05-22 11:46:33 -04003554 if (reset)
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003555 ftrace_filter_reset(hash);
Jiri Olsaac483c42012-01-02 10:04:14 +01003556 if (buf && !ftrace_match_records(hash, buf, len)) {
3557 ret = -EINVAL;
3558 goto out_regex_unlock;
3559 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003560 if (ip) {
3561 ret = ftrace_match_addr(hash, ip, remove);
3562 if (ret < 0)
3563 goto out_regex_unlock;
3564 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003565
3566 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003567 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003568 if (!ret)
3569 ftrace_ops_update_code(ops);
Steven Rostedt072126f2011-07-13 15:08:31 -04003570
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003571 mutex_unlock(&ftrace_lock);
3572
Jiri Olsaac483c42012-01-02 10:04:14 +01003573 out_regex_unlock:
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003574 mutex_unlock(&ops->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003575
3576 free_ftrace_hash(hash);
3577 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003578}
3579
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003580static int
3581ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3582 int reset, int enable)
3583{
3584 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3585}
3586
3587/**
3588 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3589 * @ops - the ops to set the filter with
3590 * @ip - the address to add to or remove from the filter.
3591 * @remove - non zero to remove the ip from the filter
3592 * @reset - non zero to reset all filters before applying this filter.
3593 *
3594 * Filters denote which functions should be enabled when tracing is enabled
3595 * If @ip is NULL, it failes to update filter.
3596 */
3597int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3598 int remove, int reset)
3599{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003600 ftrace_ops_init(ops);
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003601 return ftrace_set_addr(ops, ip, remove, reset, 1);
3602}
3603EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3604
3605static int
3606ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3607 int reset, int enable)
3608{
3609 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3610}
3611
Steven Rostedt77a2b372008-05-12 21:20:45 +02003612/**
3613 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003614 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02003615 * @buf - the string that holds the function filter text.
3616 * @len - the length of the string.
3617 * @reset - non zero to reset all filters before applying this filter.
3618 *
3619 * Filters denote which functions should be enabled when tracing is enabled.
3620 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3621 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003622int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003623 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02003624{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003625 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003626 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003627}
Steven Rostedt936e0742011-05-05 22:54:01 -04003628EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003629
Steven Rostedt41c52c02008-05-22 11:46:33 -04003630/**
3631 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003632 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04003633 * @buf - the string that holds the function notrace text.
3634 * @len - the length of the string.
3635 * @reset - non zero to reset all filters before applying this filter.
3636 *
3637 * Notrace Filters denote which functions should not be enabled when tracing
3638 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3639 * for tracing.
3640 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003641int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003642 int len, int reset)
3643{
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003644 ftrace_ops_init(ops);
Jiri Olsaac483c42012-01-02 10:04:14 +01003645 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04003646}
3647EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3648/**
3649 * ftrace_set_filter - set a function to filter on in ftrace
3650 * @ops - the ops to set the filter with
3651 * @buf - the string that holds the function filter text.
3652 * @len - the length of the string.
3653 * @reset - non zero to reset all filters before applying this filter.
3654 *
3655 * Filters denote which functions should be enabled when tracing is enabled.
3656 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3657 */
3658void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3659{
3660 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3661}
3662EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3663
3664/**
3665 * ftrace_set_notrace - set a function to not trace in ftrace
3666 * @ops - the ops to set the notrace filter with
3667 * @buf - the string that holds the function notrace text.
3668 * @len - the length of the string.
3669 * @reset - non zero to reset all filters before applying this filter.
3670 *
3671 * Notrace Filters denote which functions should not be enabled when tracing
3672 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3673 * for tracing.
3674 */
3675void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003676{
Steven Rostedtf45948e2011-05-02 12:29:25 -04003677 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003678}
Steven Rostedt936e0742011-05-05 22:54:01 -04003679EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003680
Steven Rostedt2af15d62009-05-28 13:37:24 -04003681/*
3682 * command line interface to allow users to set filters on boot up.
3683 */
3684#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3685static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3686static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3687
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003688/* Used by function selftest to not test if filter is set */
3689bool ftrace_filter_param __initdata;
3690
Steven Rostedt2af15d62009-05-28 13:37:24 -04003691static int __init set_ftrace_notrace(char *str)
3692{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003693 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08003694 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003695 return 1;
3696}
3697__setup("ftrace_notrace=", set_ftrace_notrace);
3698
3699static int __init set_ftrace_filter(char *str)
3700{
Steven Rostedt (Red Hat)f1ed7c72013-06-27 22:18:06 -04003701 ftrace_filter_param = true;
Chen Gang75761cc2013-04-08 12:12:39 +08003702 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003703 return 1;
3704}
3705__setup("ftrace_filter=", set_ftrace_filter);
3706
Stefan Assmann369bc182009-10-12 22:17:21 +02003707#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08003708static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003709static int ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer);
Steven Rostedt801c29f2010-03-05 20:02:19 -05003710
Stefan Assmann369bc182009-10-12 22:17:21 +02003711static int __init set_graph_function(char *str)
3712{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02003713 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02003714 return 1;
3715}
3716__setup("ftrace_graph_filter=", set_graph_function);
3717
3718static void __init set_ftrace_early_graph(char *buf)
3719{
3720 int ret;
3721 char *func;
3722
3723 while (buf) {
3724 func = strsep(&buf, ",");
3725 /* we allow only one expression at a time */
3726 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003727 FTRACE_GRAPH_MAX_FUNCS, func);
Stefan Assmann369bc182009-10-12 22:17:21 +02003728 if (ret)
3729 printk(KERN_DEBUG "ftrace: function %s not "
3730 "traceable\n", func);
3731 }
3732}
3733#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3734
Steven Rostedt2a85a372011-12-19 21:57:44 -05003735void __init
3736ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04003737{
3738 char *func;
3739
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09003740 ftrace_ops_init(ops);
3741
Steven Rostedt2af15d62009-05-28 13:37:24 -04003742 while (buf) {
3743 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04003744 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003745 }
3746}
3747
3748static void __init set_ftrace_early_filters(void)
3749{
3750 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003751 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003752 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003753 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02003754#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3755 if (ftrace_graph_buf[0])
3756 set_ftrace_early_graph(ftrace_graph_buf);
3757#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04003758}
3759
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003760int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003761{
3762 struct seq_file *m = (struct seq_file *)file->private_data;
3763 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003764 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003765 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04003766 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003767 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02003768
Steven Rostedt5072c592008-05-12 21:20:43 +02003769 if (file->f_mode & FMODE_READ) {
3770 iter = m->private;
Steven Rostedt5072c592008-05-12 21:20:43 +02003771 seq_release(inode, file);
3772 } else
3773 iter = file->private_data;
3774
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003775 parser = &iter->parser;
3776 if (trace_parser_loaded(parser)) {
3777 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003778 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02003779 }
3780
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003781 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003782
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003783 mutex_lock(&iter->ops->regex_lock);
3784
Steven Rostedt058e2972011-04-29 22:35:33 -04003785 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04003786 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3787
3788 if (filter_hash)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003789 orig_hash = &iter->ops->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04003790 else
3791 orig_hash = &iter->ops->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003792
Steven Rostedt058e2972011-04-29 22:35:33 -04003793 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003794 ret = ftrace_hash_move(iter->ops, filter_hash,
3795 orig_hash, iter->hash);
Steven Rostedt (Red Hat)1c80c432013-07-25 20:22:00 -04003796 if (!ret)
3797 ftrace_ops_update_code(iter->ops);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003798
Steven Rostedt058e2972011-04-29 22:35:33 -04003799 mutex_unlock(&ftrace_lock);
3800 }
Masami Hiramatsu3f2367b2013-05-09 14:44:21 +09003801
3802 mutex_unlock(&iter->ops->regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003803 free_ftrace_hash(iter->hash);
3804 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04003805
Steven Rostedt5072c592008-05-12 21:20:43 +02003806 return 0;
3807}
3808
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003809static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003810 .open = ftrace_avail_open,
3811 .read = seq_read,
3812 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08003813 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02003814};
3815
Steven Rostedt647bcd02011-05-03 14:39:21 -04003816static const struct file_operations ftrace_enabled_fops = {
3817 .open = ftrace_enabled_open,
3818 .read = seq_read,
3819 .llseek = seq_lseek,
3820 .release = seq_release_private,
3821};
3822
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003823static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003824 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003825 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02003826 .write = ftrace_filter_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003827 .llseek = tracing_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003828 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02003829};
3830
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003831static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04003832 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003833 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003834 .write = ftrace_notrace_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05003835 .llseek = tracing_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003836 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003837};
3838
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003839#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3840
3841static DEFINE_MUTEX(graph_lock);
3842
3843int ftrace_graph_count;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09003844int ftrace_graph_notrace_count;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003845unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Namhyung Kim29ad23b2013-10-14 17:24:26 +09003846unsigned long ftrace_graph_notrace_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003847
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003848struct ftrace_graph_data {
3849 unsigned long *table;
3850 size_t size;
3851 int *count;
3852 const struct seq_operations *seq_ops;
3853};
3854
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003855static void *
Li Zefan85951842009-06-24 09:54:00 +08003856__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003857{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003858 struct ftrace_graph_data *fgd = m->private;
3859
3860 if (*pos >= *fgd->count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003861 return NULL;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003862 return &fgd->table[*pos];
Li Zefan85951842009-06-24 09:54:00 +08003863}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003864
Li Zefan85951842009-06-24 09:54:00 +08003865static void *
3866g_next(struct seq_file *m, void *v, loff_t *pos)
3867{
3868 (*pos)++;
3869 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003870}
3871
3872static void *g_start(struct seq_file *m, loff_t *pos)
3873{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003874 struct ftrace_graph_data *fgd = m->private;
3875
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003876 mutex_lock(&graph_lock);
3877
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003878 /* Nothing, tell g_show to print all functions are enabled */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003879 if (!*fgd->count && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003880 return (void *)1;
3881
Li Zefan85951842009-06-24 09:54:00 +08003882 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003883}
3884
3885static void g_stop(struct seq_file *m, void *p)
3886{
3887 mutex_unlock(&graph_lock);
3888}
3889
3890static int g_show(struct seq_file *m, void *v)
3891{
3892 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003893
3894 if (!ptr)
3895 return 0;
3896
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003897 if (ptr == (unsigned long *)1) {
3898 seq_printf(m, "#### all functions enabled ####\n");
3899 return 0;
3900 }
3901
Steven Rostedtb375a112009-09-17 00:05:58 -04003902 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003903
3904 return 0;
3905}
3906
James Morris88e9d342009-09-22 16:43:43 -07003907static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003908 .start = g_start,
3909 .next = g_next,
3910 .stop = g_stop,
3911 .show = g_show,
3912};
3913
3914static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003915__ftrace_graph_open(struct inode *inode, struct file *file,
3916 struct ftrace_graph_data *fgd)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003917{
3918 int ret = 0;
3919
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003920 mutex_lock(&graph_lock);
3921 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04003922 (file->f_flags & O_TRUNC)) {
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003923 *fgd->count = 0;
3924 memset(fgd->table, 0, fgd->size * sizeof(*fgd->table));
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003925 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003926 mutex_unlock(&graph_lock);
3927
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003928 if (file->f_mode & FMODE_READ) {
3929 ret = seq_open(file, fgd->seq_ops);
3930 if (!ret) {
3931 struct seq_file *m = file->private_data;
3932 m->private = fgd;
3933 }
3934 } else
3935 file->private_data = fgd;
Li Zefana4ec5e02009-09-18 14:06:28 +08003936
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003937 return ret;
3938}
3939
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003940static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003941ftrace_graph_open(struct inode *inode, struct file *file)
3942{
3943 struct ftrace_graph_data *fgd;
3944
3945 if (unlikely(ftrace_disabled))
3946 return -ENODEV;
3947
3948 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
3949 if (fgd == NULL)
3950 return -ENOMEM;
3951
3952 fgd->table = ftrace_graph_funcs;
3953 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
3954 fgd->count = &ftrace_graph_count;
3955 fgd->seq_ops = &ftrace_graph_seq_ops;
3956
3957 return __ftrace_graph_open(inode, file, fgd);
3958}
3959
3960static int
Namhyung Kim29ad23b2013-10-14 17:24:26 +09003961ftrace_graph_notrace_open(struct inode *inode, struct file *file)
3962{
3963 struct ftrace_graph_data *fgd;
3964
3965 if (unlikely(ftrace_disabled))
3966 return -ENODEV;
3967
3968 fgd = kmalloc(sizeof(*fgd), GFP_KERNEL);
3969 if (fgd == NULL)
3970 return -ENOMEM;
3971
3972 fgd->table = ftrace_graph_notrace_funcs;
3973 fgd->size = FTRACE_GRAPH_MAX_FUNCS;
3974 fgd->count = &ftrace_graph_notrace_count;
3975 fgd->seq_ops = &ftrace_graph_seq_ops;
3976
3977 return __ftrace_graph_open(inode, file, fgd);
3978}
3979
3980static int
Li Zefan87827112009-07-23 11:29:11 +08003981ftrace_graph_release(struct inode *inode, struct file *file)
3982{
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003983 if (file->f_mode & FMODE_READ) {
3984 struct seq_file *m = file->private_data;
3985
3986 kfree(m->private);
Li Zefan87827112009-07-23 11:29:11 +08003987 seq_release(inode, file);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003988 } else {
3989 kfree(file->private_data);
3990 }
3991
Li Zefan87827112009-07-23 11:29:11 +08003992 return 0;
3993}
3994
3995static int
Namhyung Kimfaf982a2013-10-14 17:24:24 +09003996ftrace_set_func(unsigned long *array, int *idx, int size, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003997{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003998 struct dyn_ftrace *rec;
3999 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004000 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004001 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004002 int type, not;
4003 char *search;
4004 bool exists;
4005 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004006
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004007 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02004008 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004009 if (!not && *idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004010 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004011
4012 search_len = strlen(search);
4013
Steven Rostedt52baf112009-02-14 01:15:39 -05004014 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004015
4016 if (unlikely(ftrace_disabled)) {
4017 mutex_unlock(&ftrace_lock);
4018 return -ENODEV;
4019 }
4020
Steven Rostedt265c8312009-02-13 12:43:56 -05004021 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004022
Steven Rostedtb9df92d2011-04-28 20:32:08 -04004023 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004024 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004025 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004026 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01004027 if (array[i] == rec->ip) {
4028 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05004029 break;
4030 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004031 }
4032
4033 if (!not) {
4034 fail = 0;
4035 if (!exists) {
4036 array[(*idx)++] = rec->ip;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004037 if (*idx >= size)
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004038 goto out;
4039 }
4040 } else {
4041 if (exists) {
4042 array[i] = array[--(*idx)];
4043 array[*idx] = 0;
4044 fail = 0;
4045 }
4046 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004047 }
Steven Rostedt265c8312009-02-13 12:43:56 -05004048 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004049out:
Steven Rostedt52baf112009-02-14 01:15:39 -05004050 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004051
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004052 if (fail)
4053 return -EINVAL;
4054
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004055 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004056}
4057
4058static ssize_t
4059ftrace_graph_write(struct file *file, const char __user *ubuf,
4060 size_t cnt, loff_t *ppos)
4061{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004062 struct trace_parser parser;
Namhyung Kim6a101082013-10-14 17:24:25 +09004063 ssize_t read, ret = 0;
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004064 struct ftrace_graph_data *fgd = file->private_data;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004065
Li Zefanc7c6b1f2010-02-10 15:43:04 +08004066 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004067 return 0;
4068
Namhyung Kim6a101082013-10-14 17:24:25 +09004069 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX))
4070 return -ENOMEM;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004071
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004072 read = trace_get_user(&parser, ubuf, cnt, ppos);
4073
Li Zefan4ba79782009-09-22 13:52:20 +08004074 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004075 parser.buffer[parser.idx] = 0;
4076
Namhyung Kim6a101082013-10-14 17:24:25 +09004077 mutex_lock(&graph_lock);
4078
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004079 /* we allow only one expression at a time */
Namhyung Kimfaf982a2013-10-14 17:24:24 +09004080 ret = ftrace_set_func(fgd->table, fgd->count, fgd->size,
4081 parser.buffer);
Namhyung Kim6a101082013-10-14 17:24:25 +09004082
4083 mutex_unlock(&graph_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004084 }
4085
Namhyung Kim6a101082013-10-14 17:24:25 +09004086 if (!ret)
4087 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08004088
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02004089 trace_parser_put(&parser);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004090
4091 return ret;
4092}
4093
4094static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08004095 .open = ftrace_graph_open,
4096 .read = seq_read,
4097 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004098 .llseek = tracing_lseek,
Li Zefan87827112009-07-23 11:29:11 +08004099 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004100};
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004101
4102static const struct file_operations ftrace_graph_notrace_fops = {
4103 .open = ftrace_graph_notrace_open,
4104 .read = seq_read,
4105 .write = ftrace_graph_write,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004106 .llseek = tracing_lseek,
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004107 .release = ftrace_graph_release,
4108};
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004109#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4110
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004111static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02004112{
Steven Rostedt5072c592008-05-12 21:20:43 +02004113
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004114 trace_create_file("available_filter_functions", 0444,
4115 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02004116
Steven Rostedt647bcd02011-05-03 14:39:21 -04004117 trace_create_file("enabled_functions", 0444,
4118 d_tracer, NULL, &ftrace_enabled_fops);
4119
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004120 trace_create_file("set_ftrace_filter", 0644, d_tracer,
4121 NULL, &ftrace_filter_fops);
Steven Rostedt41c52c02008-05-22 11:46:33 -04004122
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004123 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
Steven Rostedt41c52c02008-05-22 11:46:33 -04004124 NULL, &ftrace_notrace_fops);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04004125
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004126#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004127 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004128 NULL,
4129 &ftrace_graph_fops);
Namhyung Kim29ad23b2013-10-14 17:24:26 +09004130 trace_create_file("set_graph_notrace", 0444, d_tracer,
4131 NULL,
4132 &ftrace_graph_notrace_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05004133#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
4134
Steven Rostedt5072c592008-05-12 21:20:43 +02004135 return 0;
4136}
4137
Steven Rostedt9fd49322012-04-24 22:32:06 -04004138static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05004139{
Steven Rostedt9fd49322012-04-24 22:32:06 -04004140 const unsigned long *ipa = a;
4141 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05004142
Steven Rostedt9fd49322012-04-24 22:32:06 -04004143 if (*ipa > *ipb)
4144 return 1;
4145 if (*ipa < *ipb)
4146 return -1;
4147 return 0;
4148}
4149
4150static void ftrace_swap_ips(void *a, void *b, int size)
4151{
4152 unsigned long *ipa = a;
4153 unsigned long *ipb = b;
4154 unsigned long t;
4155
4156 t = *ipa;
4157 *ipa = *ipb;
4158 *ipb = t;
Steven Rostedt68950612011-12-16 17:06:45 -05004159}
4160
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004161static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08004162 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004163 unsigned long *end)
4164{
Steven Rostedt706c81f2012-04-24 23:45:26 -04004165 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004166 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004167 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05004168 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004169 unsigned long *p;
4170 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04004171 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05004172 int ret = -ENOMEM;
4173
4174 count = end - start;
4175
4176 if (!count)
4177 return 0;
4178
Steven Rostedt9fd49322012-04-24 22:32:06 -04004179 sort(start, count, sizeof(*start),
4180 ftrace_cmp_ips, ftrace_swap_ips);
4181
Steven Rostedt706c81f2012-04-24 23:45:26 -04004182 start_pg = ftrace_allocate_pages(count);
4183 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05004184 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004185
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004186 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05004187
Steven Rostedt32082302011-12-16 14:42:37 -05004188 /*
4189 * Core and each module needs their own pages, as
4190 * modules will free them when they are removed.
4191 * Force a new page to be allocated for modules.
4192 */
Steven Rostedta7900872011-12-16 16:23:44 -05004193 if (!mod) {
4194 WARN_ON(ftrace_pages || ftrace_pages_start);
4195 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04004196 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004197 } else {
Steven Rostedt32082302011-12-16 14:42:37 -05004198 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05004199 goto out;
Steven Rostedt32082302011-12-16 14:42:37 -05004200
Steven Rostedta7900872011-12-16 16:23:44 -05004201 if (WARN_ON(ftrace_pages->next)) {
4202 /* Hmm, we have free pages? */
4203 while (ftrace_pages->next)
4204 ftrace_pages = ftrace_pages->next;
Steven Rostedt32082302011-12-16 14:42:37 -05004205 }
Steven Rostedta7900872011-12-16 16:23:44 -05004206
Steven Rostedt706c81f2012-04-24 23:45:26 -04004207 ftrace_pages->next = start_pg;
Steven Rostedt32082302011-12-16 14:42:37 -05004208 }
4209
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004210 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004211 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004212 while (p < end) {
4213 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08004214 /*
4215 * Some architecture linkers will pad between
4216 * the different mcount_loc sections of different
4217 * object files to satisfy alignments.
4218 * Skip any NULL pointers.
4219 */
4220 if (!addr)
4221 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04004222
4223 if (pg->index == pg->size) {
4224 /* We should have allocated enough */
4225 if (WARN_ON(!pg->next))
4226 break;
4227 pg = pg->next;
4228 }
4229
4230 rec = &pg->records[pg->index++];
4231 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004232 }
4233
Steven Rostedt706c81f2012-04-24 23:45:26 -04004234 /* We should have used all pages */
4235 WARN_ON(pg->next);
4236
4237 /* Assign the last page to ftrace_pages */
4238 ftrace_pages = pg;
4239
Steven Rostedt85ae32a2011-12-16 16:30:31 -05004240 /* These new locations need to be initialized */
Steven Rostedt706c81f2012-04-24 23:45:26 -04004241 ftrace_new_pgs = start_pg;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05004242
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004243 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04004244 * We only need to disable interrupts on start up
4245 * because we are modifying code that an interrupt
4246 * may execute, and the modification is not atomic.
4247 * But for modules, nothing runs the code we modify
4248 * until we are finished with it, and there's no
4249 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04004250 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04004251 if (!mod)
4252 local_irq_save(flags);
Steven Rostedt31e88902008-11-14 16:21:19 -08004253 ftrace_update_code(mod);
Steven Rostedt4376cac2011-06-24 23:28:13 -04004254 if (!mod)
4255 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05004256 ret = 0;
4257 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004258 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004259
Steven Rostedta7900872011-12-16 16:23:44 -05004260 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004261}
4262
Steven Rostedt93eb6772009-04-15 13:24:06 -04004263#ifdef CONFIG_MODULES
Steven Rostedt32082302011-12-16 14:42:37 -05004264
4265#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
4266
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004267void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004268{
4269 struct dyn_ftrace *rec;
Steven Rostedt32082302011-12-16 14:42:37 -05004270 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004271 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05004272 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04004273
Steven Rostedt93eb6772009-04-15 13:24:06 -04004274 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004275
4276 if (ftrace_disabled)
4277 goto out_unlock;
4278
Steven Rostedt32082302011-12-16 14:42:37 -05004279 /*
4280 * Each module has its own ftrace_pages, remove
4281 * them from the list.
4282 */
4283 last_pg = &ftrace_pages_start;
4284 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4285 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004286 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04004287 /*
Steven Rostedt32082302011-12-16 14:42:37 -05004288 * As core pages are first, the first
4289 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04004290 */
Steven Rostedt32082302011-12-16 14:42:37 -05004291 if (WARN_ON(pg == ftrace_pages_start))
4292 goto out_unlock;
4293
4294 /* Check if we are deleting the last page */
4295 if (pg == ftrace_pages)
4296 ftrace_pages = next_to_ftrace_page(last_pg);
4297
4298 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05004299 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4300 free_pages((unsigned long)pg->records, order);
4301 kfree(pg);
Steven Rostedt32082302011-12-16 14:42:37 -05004302 } else
4303 last_pg = &pg->next;
4304 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04004305 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04004306 mutex_unlock(&ftrace_lock);
4307}
4308
4309static void ftrace_init_module(struct module *mod,
4310 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04004311{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04004312 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04004313 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004314 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04004315}
4316
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004317static int ftrace_module_notify_enter(struct notifier_block *self,
4318 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004319{
4320 struct module *mod = data;
4321
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004322 if (val == MODULE_STATE_COMING)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004323 ftrace_init_module(mod, mod->ftrace_callsites,
4324 mod->ftrace_callsites +
4325 mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004326 return 0;
4327}
4328
4329static int ftrace_module_notify_exit(struct notifier_block *self,
4330 unsigned long val, void *data)
4331{
4332 struct module *mod = data;
4333
4334 if (val == MODULE_STATE_GOING)
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004335 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04004336
4337 return 0;
4338}
4339#else
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004340static int ftrace_module_notify_enter(struct notifier_block *self,
4341 unsigned long val, void *data)
4342{
4343 return 0;
4344}
4345static int ftrace_module_notify_exit(struct notifier_block *self,
4346 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004347{
4348 return 0;
4349}
4350#endif /* CONFIG_MODULES */
4351
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004352struct notifier_block ftrace_module_enter_nb = {
4353 .notifier_call = ftrace_module_notify_enter,
Steven Rostedtc1bf08a2012-12-14 09:48:15 -05004354 .priority = INT_MAX, /* Run before anything that can use kprobes */
Steven Rostedt93eb6772009-04-15 13:24:06 -04004355};
4356
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004357struct notifier_block ftrace_module_exit_nb = {
4358 .notifier_call = ftrace_module_notify_exit,
4359 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4360};
4361
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004362extern unsigned long __start_mcount_loc[];
4363extern unsigned long __stop_mcount_loc[];
4364
4365void __init ftrace_init(void)
4366{
4367 unsigned long count, addr, flags;
4368 int ret;
4369
4370 /* Keep the ftrace pointer to the stub */
4371 addr = (unsigned long)ftrace_stub;
4372
4373 local_irq_save(flags);
4374 ftrace_dyn_arch_init(&addr);
4375 local_irq_restore(flags);
4376
4377 /* ftrace_dyn_arch_init places the return code in addr */
4378 if (addr)
4379 goto failed;
4380
4381 count = __stop_mcount_loc - __start_mcount_loc;
4382
4383 ret = ftrace_dyn_table_alloc(count);
4384 if (ret)
4385 goto failed;
4386
4387 last_ftrace_enabled = ftrace_enabled = 1;
4388
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004389 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08004390 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004391 __stop_mcount_loc);
4392
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004393 ret = register_module_notifier(&ftrace_module_enter_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08004394 if (ret)
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004395 pr_warning("Failed to register trace ftrace module enter notifier\n");
4396
4397 ret = register_module_notifier(&ftrace_module_exit_nb);
4398 if (ret)
4399 pr_warning("Failed to register trace ftrace module exit notifier\n");
Steven Rostedt93eb6772009-04-15 13:24:06 -04004400
Steven Rostedt2af15d62009-05-28 13:37:24 -04004401 set_ftrace_early_filters();
4402
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004403 return;
4404 failed:
4405 ftrace_disabled = 1;
4406}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004407
Steven Rostedt3d083392008-05-12 21:20:42 +02004408#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004409
Steven Rostedt2b499382011-05-03 22:49:52 -04004410static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04004411 .func = ftrace_stub,
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004412 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4413 INIT_REGEX_LOCK(global_ops)
Steven Rostedtbd69c302011-05-03 21:55:54 -04004414};
4415
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004416static int __init ftrace_nodyn_init(void)
4417{
4418 ftrace_enabled = 1;
4419 return 0;
4420}
Steven Rostedt6f415672012-10-05 12:13:07 -04004421core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004422
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004423static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4424static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05004425/* Keep as macros so we do not need to define the commands */
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004426# define ftrace_startup(ops, command) \
4427 ({ \
4428 int ___ret = __register_ftrace_function(ops); \
4429 if (!___ret) \
4430 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4431 ___ret; \
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04004432 })
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004433# define ftrace_shutdown(ops, command) __unregister_ftrace_function(ops)
4434
Ingo Molnarc7aafc52008-05-12 21:20:45 +02004435# define ftrace_startup_sysctl() do { } while (0)
4436# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04004437
4438static inline int
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004439ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004440{
4441 return 1;
4442}
4443
Steven Rostedt3d083392008-05-12 21:20:42 +02004444#endif /* CONFIG_DYNAMIC_FTRACE */
4445
Steven Rostedtb8489142011-05-04 09:27:52 -04004446static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004447ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004448 struct ftrace_ops *op, struct pt_regs *regs)
Jiri Olsae2484912012-02-15 15:51:48 +01004449{
Jiri Olsae2484912012-02-15 15:51:48 +01004450 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4451 return;
4452
4453 /*
4454 * Some of the ops may be dynamically allocated,
4455 * they must be freed after a synchronize_sched().
4456 */
4457 preempt_disable_notrace();
4458 trace_recursion_set(TRACE_CONTROL_BIT);
Steven Rostedt (Red Hat)b5aa3a42013-11-04 18:34:44 -05004459
4460 /*
4461 * Control funcs (perf) uses RCU. Only trace if
4462 * RCU is currently active.
4463 */
4464 if (!rcu_is_watching())
4465 goto out;
4466
Steven Rostedt0a016402012-11-02 17:03:03 -04004467 do_for_each_ftrace_op(op, ftrace_control_list) {
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -04004468 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
4469 !ftrace_function_local_disabled(op) &&
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004470 ftrace_ops_test(op, ip, regs))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004471 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04004472 } while_for_each_ftrace_op(op);
Steven Rostedt (Red Hat)b5aa3a42013-11-04 18:34:44 -05004473 out:
Jiri Olsae2484912012-02-15 15:51:48 +01004474 trace_recursion_clear(TRACE_CONTROL_BIT);
4475 preempt_enable_notrace();
4476}
4477
4478static struct ftrace_ops control_ops = {
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004479 .func = ftrace_ops_control_func,
4480 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_INITIALIZED,
4481 INIT_REGEX_LOCK(control_ops)
Jiri Olsae2484912012-02-15 15:51:48 +01004482};
4483
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004484static inline void
4485__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004486 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004487{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004488 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004489 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04004490
Steven Rostedtccf36722012-06-05 09:44:25 -04004491 if (function_trace_stop)
4492 return;
4493
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004494 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4495 if (bit < 0)
4496 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04004497
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004498 /*
4499 * Some of the ops may be dynamically allocated,
4500 * they must be freed after a synchronize_sched().
4501 */
4502 preempt_disable_notrace();
Steven Rostedt0a016402012-11-02 17:03:03 -04004503 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedt (Red Hat)195a8af2013-07-23 22:06:15 -04004504 if (ftrace_ops_test(op, ip, regs))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004505 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04004506 } while_for_each_ftrace_op(op);
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004507 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004508 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04004509}
4510
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004511/*
4512 * Some archs only support passing ip and parent_ip. Even though
4513 * the list function ignores the op parameter, we do not want any
4514 * C side effects, where a function is called without the caller
4515 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004516 * Archs are to support both the regs and ftrace_ops at the same time.
4517 * If they support ftrace_ops, it is assumed they support regs.
4518 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09004519 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4520 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004521 * An architecture can pass partial regs with ftrace_ops and still
4522 * set the ARCH_SUPPORT_FTARCE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004523 */
4524#if ARCH_SUPPORTS_FTRACE_OPS
4525static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004526 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004527{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004528 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004529}
4530#else
4531static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4532{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004533 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004534}
4535#endif
4536
Steven Rostedte32d8952008-12-04 00:26:41 -05004537static void clear_ftrace_swapper(void)
4538{
4539 struct task_struct *p;
4540 int cpu;
4541
4542 get_online_cpus();
4543 for_each_online_cpu(cpu) {
4544 p = idle_task(cpu);
4545 clear_tsk_trace_trace(p);
4546 }
4547 put_online_cpus();
4548}
4549
4550static void set_ftrace_swapper(void)
4551{
4552 struct task_struct *p;
4553 int cpu;
4554
4555 get_online_cpus();
4556 for_each_online_cpu(cpu) {
4557 p = idle_task(cpu);
4558 set_tsk_trace_trace(p);
4559 }
4560 put_online_cpus();
4561}
4562
4563static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004564{
4565 struct task_struct *p;
4566
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004567 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05004568 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05004569 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05004570 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004571 rcu_read_unlock();
4572
Steven Rostedte32d8952008-12-04 00:26:41 -05004573 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004574}
4575
Steven Rostedte32d8952008-12-04 00:26:41 -05004576static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004577{
4578 struct task_struct *p;
4579
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004580 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004581 do_each_pid_task(pid, PIDTYPE_PID, p) {
4582 set_tsk_trace_trace(p);
4583 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004584 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004585}
4586
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004587static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004588{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004589 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004590 clear_ftrace_swapper();
4591 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004592 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05004593}
4594
4595static void set_ftrace_pid_task(struct pid *pid)
4596{
4597 if (pid == ftrace_swapper_pid)
4598 set_ftrace_swapper();
4599 else
4600 set_ftrace_pid(pid);
4601}
4602
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004603static int ftrace_pid_add(int p)
4604{
4605 struct pid *pid;
4606 struct ftrace_pid *fpid;
4607 int ret = -EINVAL;
4608
4609 mutex_lock(&ftrace_lock);
4610
4611 if (!p)
4612 pid = ftrace_swapper_pid;
4613 else
4614 pid = find_get_pid(p);
4615
4616 if (!pid)
4617 goto out;
4618
4619 ret = 0;
4620
4621 list_for_each_entry(fpid, &ftrace_pids, list)
4622 if (fpid->pid == pid)
4623 goto out_put;
4624
4625 ret = -ENOMEM;
4626
4627 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4628 if (!fpid)
4629 goto out_put;
4630
4631 list_add(&fpid->list, &ftrace_pids);
4632 fpid->pid = pid;
4633
4634 set_ftrace_pid_task(pid);
4635
4636 ftrace_update_pid_func();
4637 ftrace_startup_enable(0);
4638
4639 mutex_unlock(&ftrace_lock);
4640 return 0;
4641
4642out_put:
4643 if (pid != ftrace_swapper_pid)
4644 put_pid(pid);
4645
4646out:
4647 mutex_unlock(&ftrace_lock);
4648 return ret;
4649}
4650
4651static void ftrace_pid_reset(void)
4652{
4653 struct ftrace_pid *fpid, *safe;
4654
4655 mutex_lock(&ftrace_lock);
4656 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4657 struct pid *pid = fpid->pid;
4658
4659 clear_ftrace_pid_task(pid);
4660
4661 list_del(&fpid->list);
4662 kfree(fpid);
4663 }
4664
4665 ftrace_update_pid_func();
4666 ftrace_startup_enable(0);
4667
4668 mutex_unlock(&ftrace_lock);
4669}
4670
4671static void *fpid_start(struct seq_file *m, loff_t *pos)
4672{
4673 mutex_lock(&ftrace_lock);
4674
4675 if (list_empty(&ftrace_pids) && (!*pos))
4676 return (void *) 1;
4677
4678 return seq_list_start(&ftrace_pids, *pos);
4679}
4680
4681static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4682{
4683 if (v == (void *)1)
4684 return NULL;
4685
4686 return seq_list_next(v, &ftrace_pids, pos);
4687}
4688
4689static void fpid_stop(struct seq_file *m, void *p)
4690{
4691 mutex_unlock(&ftrace_lock);
4692}
4693
4694static int fpid_show(struct seq_file *m, void *v)
4695{
4696 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4697
4698 if (v == (void *)1) {
4699 seq_printf(m, "no pid\n");
4700 return 0;
4701 }
4702
4703 if (fpid->pid == ftrace_swapper_pid)
4704 seq_printf(m, "swapper tasks\n");
4705 else
4706 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4707
4708 return 0;
4709}
4710
4711static const struct seq_operations ftrace_pid_sops = {
4712 .start = fpid_start,
4713 .next = fpid_next,
4714 .stop = fpid_stop,
4715 .show = fpid_show,
4716};
4717
4718static int
4719ftrace_pid_open(struct inode *inode, struct file *file)
4720{
4721 int ret = 0;
4722
4723 if ((file->f_mode & FMODE_WRITE) &&
4724 (file->f_flags & O_TRUNC))
4725 ftrace_pid_reset();
4726
4727 if (file->f_mode & FMODE_READ)
4728 ret = seq_open(file, &ftrace_pid_sops);
4729
4730 return ret;
4731}
4732
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004733static ssize_t
4734ftrace_pid_write(struct file *filp, const char __user *ubuf,
4735 size_t cnt, loff_t *ppos)
4736{
Ingo Molnar457dc922009-11-23 11:03:28 +01004737 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004738 long val;
4739 int ret;
4740
4741 if (cnt >= sizeof(buf))
4742 return -EINVAL;
4743
4744 if (copy_from_user(&buf, ubuf, cnt))
4745 return -EFAULT;
4746
4747 buf[cnt] = 0;
4748
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004749 /*
4750 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4751 * to clean the filter quietly.
4752 */
Ingo Molnar457dc922009-11-23 11:03:28 +01004753 tmp = strstrip(buf);
4754 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004755 return 1;
4756
Daniel Walterbcd83ea2012-09-26 22:08:38 +02004757 ret = kstrtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004758 if (ret < 0)
4759 return ret;
4760
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004761 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004762
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004763 return ret ? ret : cnt;
4764}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004765
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004766static int
4767ftrace_pid_release(struct inode *inode, struct file *file)
4768{
4769 if (file->f_mode & FMODE_READ)
4770 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004771
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004772 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004773}
4774
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004775static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004776 .open = ftrace_pid_open,
4777 .write = ftrace_pid_write,
4778 .read = seq_read,
Steven Rostedt (Red Hat)098c8792013-12-21 17:39:40 -05004779 .llseek = tracing_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004780 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004781};
4782
4783static __init int ftrace_init_debugfs(void)
4784{
4785 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004786
4787 d_tracer = tracing_init_dentry();
4788 if (!d_tracer)
4789 return 0;
4790
4791 ftrace_init_dyn_debugfs(d_tracer);
4792
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004793 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4794 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04004795
4796 ftrace_profile_debugfs(d_tracer);
4797
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004798 return 0;
4799}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004800fs_initcall(ftrace_init_debugfs);
4801
Steven Rostedt3d083392008-05-12 21:20:42 +02004802/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004803 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004804 *
4805 * This function should be used by panic code. It stops ftrace
4806 * but in a not so nice way. If you need to simply kill ftrace
4807 * from a non-atomic section, use ftrace_kill.
4808 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004809void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004810{
4811 ftrace_disabled = 1;
4812 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004813 clear_ftrace_function();
4814}
4815
4816/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04004817 * Test if ftrace is dead or not.
4818 */
4819int ftrace_is_dead(void)
4820{
4821 return ftrace_disabled;
4822}
4823
4824/**
Steven Rostedt3d083392008-05-12 21:20:42 +02004825 * register_ftrace_function - register a function for profiling
4826 * @ops - ops structure that holds the function for profiling.
4827 *
4828 * Register a function to be called by all functions in the
4829 * kernel.
4830 *
4831 * Note: @ops->func and all the functions it calls must be labeled
4832 * with "notrace", otherwise it will go into a
4833 * recursive loop.
4834 */
4835int register_ftrace_function(struct ftrace_ops *ops)
4836{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004837 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004838
Masami Hiramatsuf04f24fb2013-05-09 14:44:17 +09004839 ftrace_ops_init(ops);
4840
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004841 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004842
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004843 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04004844
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004845 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02004846
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004847 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02004848}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004849EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02004850
4851/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01004852 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02004853 * @ops - ops structure that holds the function to unregister
4854 *
4855 * Unregister a function that was added to be called by ftrace profiling.
4856 */
4857int unregister_ftrace_function(struct ftrace_ops *ops)
4858{
4859 int ret;
4860
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004861 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05004862 ret = ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004863 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004864
4865 return ret;
4866}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004867EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004868
Ingo Molnare309b412008-05-12 21:20:51 +02004869int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004870ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004871 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004872 loff_t *ppos)
4873{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004874 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004875
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004876 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004877
Steven Rostedt45a4a232011-04-21 23:16:46 -04004878 if (unlikely(ftrace_disabled))
4879 goto out;
4880
4881 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004882
Li Zefana32c7762009-06-26 16:55:51 +08004883 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004884 goto out;
4885
Li Zefana32c7762009-06-26 16:55:51 +08004886 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004887
4888 if (ftrace_enabled) {
4889
4890 ftrace_startup_sysctl();
4891
4892 /* we are starting ftrace again */
Jan Kiszka5000c412013-03-26 17:53:03 +01004893 if (ftrace_ops_list != &ftrace_list_end)
4894 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004895
4896 } else {
4897 /* stopping ftrace calls (just send to ftrace_stub) */
4898 ftrace_trace_function = ftrace_stub;
4899
4900 ftrace_shutdown_sysctl();
4901 }
4902
4903 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004904 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004905 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02004906}
Ingo Molnarf17845e2008-10-24 12:47:10 +02004907
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004908#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004909
Steven Rostedt597af812009-04-03 15:24:12 -04004910static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004911static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004912
Steven Rostedte49dc192008-12-02 23:50:05 -05004913int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4914{
4915 return 0;
4916}
4917
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004918/* The callbacks that hook a function */
4919trace_func_graph_ret_t ftrace_graph_return =
4920 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004921trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05004922static trace_func_graph_ent_t __ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004923
4924/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4925static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4926{
4927 int i;
4928 int ret = 0;
4929 unsigned long flags;
4930 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4931 struct task_struct *g, *t;
4932
4933 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4934 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4935 * sizeof(struct ftrace_ret_stack),
4936 GFP_KERNEL);
4937 if (!ret_stack_list[i]) {
4938 start = 0;
4939 end = i;
4940 ret = -ENOMEM;
4941 goto free;
4942 }
4943 }
4944
4945 read_lock_irqsave(&tasklist_lock, flags);
4946 do_each_thread(g, t) {
4947 if (start == end) {
4948 ret = -EAGAIN;
4949 goto unlock;
4950 }
4951
4952 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01004953 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004954 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04004955 t->curr_ret_stack = -1;
4956 /* Make sure the tasks see the -1 first: */
4957 smp_wmb();
4958 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004959 }
4960 } while_each_thread(g, t);
4961
4962unlock:
4963 read_unlock_irqrestore(&tasklist_lock, flags);
4964free:
4965 for (i = start; i < end; i++)
4966 kfree(ret_stack_list[i]);
4967 return ret;
4968}
4969
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004970static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04004971ftrace_graph_probe_sched_switch(void *ignore,
4972 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004973{
4974 unsigned long long timestamp;
4975 int index;
4976
Steven Rostedtbe6f1642009-03-24 11:06:24 -04004977 /*
4978 * Does the user want to count the time a function was asleep.
4979 * If so, do not update the time stamps.
4980 */
4981 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4982 return;
4983
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004984 timestamp = trace_clock_local();
4985
4986 prev->ftrace_timestamp = timestamp;
4987
4988 /* only process tasks that we timestamped */
4989 if (!next->ftrace_timestamp)
4990 return;
4991
4992 /*
4993 * Update all the counters in next to make up for the
4994 * time next was sleeping.
4995 */
4996 timestamp -= next->ftrace_timestamp;
4997
4998 for (index = next->curr_ret_stack; index >= 0; index--)
4999 next->ret_stack[index].calltime += timestamp;
5000}
5001
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005002/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005003static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005004{
5005 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005006 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005007
5008 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
5009 sizeof(struct ftrace_ret_stack *),
5010 GFP_KERNEL);
5011
5012 if (!ret_stack_list)
5013 return -ENOMEM;
5014
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005015 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04005016 for_each_online_cpu(cpu) {
5017 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05005018 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04005019 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01005020
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005021 do {
5022 ret = alloc_retstack_tasklist(ret_stack_list);
5023 } while (ret == -EAGAIN);
5024
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005025 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04005026 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04005027 if (ret)
5028 pr_info("ftrace_graph: Couldn't activate tracepoint"
5029 " probe to kernel_sched_switch\n");
5030 }
5031
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005032 kfree(ret_stack_list);
5033 return ret;
5034}
5035
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005036/*
5037 * Hibernation protection.
5038 * The state of the current task is too much unstable during
5039 * suspend/restore to disk. We want to protect against that.
5040 */
5041static int
5042ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
5043 void *unused)
5044{
5045 switch (state) {
5046 case PM_HIBERNATION_PREPARE:
5047 pause_graph_tracing();
5048 break;
5049
5050 case PM_POST_HIBERNATION:
5051 unpause_graph_tracing();
5052 break;
5053 }
5054 return NOTIFY_DONE;
5055}
5056
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005057/* Just a place holder for function graph */
5058static struct ftrace_ops fgraph_ops __read_mostly = {
5059 .func = ftrace_stub,
5060 .flags = FTRACE_OPS_FL_STUB | FTRACE_OPS_FL_GLOBAL |
5061 FTRACE_OPS_FL_RECURSION_SAFE,
5062};
5063
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005064static int ftrace_graph_entry_test(struct ftrace_graph_ent *trace)
5065{
5066 if (!ftrace_ops_test(&global_ops, trace->func, NULL))
5067 return 0;
5068 return __ftrace_graph_entry(trace);
5069}
5070
5071/*
5072 * The function graph tracer should only trace the functions defined
5073 * by set_ftrace_filter and set_ftrace_notrace. If another function
5074 * tracer ops is registered, the graph tracer requires testing the
5075 * function against the global ops, and not just trace any function
5076 * that any ftrace_ops registered.
5077 */
5078static void update_function_graph_func(void)
5079{
5080 if (ftrace_ops_list == &ftrace_list_end ||
5081 (ftrace_ops_list == &global_ops &&
5082 global_ops.next == &ftrace_list_end))
5083 ftrace_graph_entry = __ftrace_graph_entry;
5084 else
5085 ftrace_graph_entry = ftrace_graph_entry_test;
5086}
5087
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005088int register_ftrace_graph(trace_func_graph_ret_t retfunc,
5089 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005090{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005091 int ret = 0;
5092
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005093 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005094
Steven Rostedt05ce5812009-03-24 00:18:31 -04005095 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04005096 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04005097 ret = -EBUSY;
5098 goto out;
5099 }
5100
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005101 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
5102 register_pm_notifier(&ftrace_suspend_notifier);
5103
Steven Rostedt597af812009-04-03 15:24:12 -04005104 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005105 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005106 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04005107 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005108 goto out;
5109 }
Steven Rostedte53a6312008-11-26 00:16:25 -05005110
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005111 ftrace_graph_return = retfunc;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005112
5113 /*
5114 * Update the indirect function to the entryfunc, and the
5115 * function that gets called to the entry_test first. Then
5116 * call the update fgraph entry function to determine if
5117 * the entryfunc should be called directly or not.
5118 */
5119 __ftrace_graph_entry = entryfunc;
5120 ftrace_graph_entry = ftrace_graph_entry_test;
5121 update_function_graph_func();
Steven Rostedte53a6312008-11-26 00:16:25 -05005122
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005123 ret = ftrace_startup(&fgraph_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005124
5125out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005126 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005127 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005128}
5129
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005130void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005131{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005132 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005133
Steven Rostedt597af812009-04-03 15:24:12 -04005134 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005135 goto out;
5136
Steven Rostedt597af812009-04-03 15:24:12 -04005137 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01005138 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05005139 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)23a8e842014-01-13 10:30:23 -05005140 __ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedt (Red Hat)8a56d772013-11-25 20:59:46 -05005141 ftrace_shutdown(&fgraph_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08005142 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04005143 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01005144
Steven Rostedt2aad1b72009-03-30 11:11:28 -04005145 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05005146 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005147}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005148
Steven Rostedt868baf02011-02-10 21:26:13 -05005149static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
5150
5151static void
5152graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
5153{
5154 atomic_set(&t->tracing_graph_pause, 0);
5155 atomic_set(&t->trace_overrun, 0);
5156 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005157 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05005158 smp_wmb();
5159 t->ret_stack = ret_stack;
5160}
5161
5162/*
5163 * Allocate a return stack for the idle task. May be the first
5164 * time through, or it may be done by CPU hotplug online.
5165 */
5166void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
5167{
5168 t->curr_ret_stack = -1;
5169 /*
5170 * The idle task has no parent, it either has its own
5171 * stack or no stack at all.
5172 */
5173 if (t->ret_stack)
5174 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
5175
5176 if (ftrace_graph_active) {
5177 struct ftrace_ret_stack *ret_stack;
5178
5179 ret_stack = per_cpu(idle_ret_stack, cpu);
5180 if (!ret_stack) {
5181 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
5182 * sizeof(struct ftrace_ret_stack),
5183 GFP_KERNEL);
5184 if (!ret_stack)
5185 return;
5186 per_cpu(idle_ret_stack, cpu) = ret_stack;
5187 }
5188 graph_init_task(t, ret_stack);
5189 }
5190}
5191
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005192/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005193void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005194{
Steven Rostedt84047e32009-06-02 16:51:55 -04005195 /* Make sure we do not use the parent ret_stack */
5196 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05005197 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04005198
Steven Rostedt597af812009-04-03 15:24:12 -04005199 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04005200 struct ftrace_ret_stack *ret_stack;
5201
5202 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005203 * sizeof(struct ftrace_ret_stack),
5204 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04005205 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005206 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05005207 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04005208 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005209}
5210
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01005211void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005212{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005213 struct ftrace_ret_stack *ret_stack = t->ret_stack;
5214
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005215 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01005216 /* NULL must become visible to IRQs before we free it: */
5217 barrier();
5218
5219 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01005220}
Steven Rostedt14a866c2008-12-02 23:50:02 -05005221
5222void ftrace_graph_stop(void)
5223{
5224 ftrace_stop();
5225}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01005226#endif