blob: 4f2ab9352a682a79f6811e3c3eed88e4d20eec1d [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
13 * Copyright (C) 2004 William Lee Irwin III
14 */
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 Rostedt6912896e2008-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 Rostedt6912896e2008-10-23 09:33:03 -040047 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040048 ___r; \
49 })
Steven Rostedt6912896e2008-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 Rostedt6912896e2008-10-23 09:33:03 -040055 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040056 ___r; \
57 })
Steven Rostedt6912896e2008-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
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040067static struct ftrace_ops ftrace_list_end __read_mostly = {
68 .func = ftrace_stub,
69};
70
Steven Rostedt4eebcc82008-05-12 21:20:48 +020071/* ftrace_enabled is a method to turn ftrace on or off */
72int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020073static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020074
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050075/* Quick disabling of function tracer. */
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040076int function_trace_stop __read_mostly;
77
78/* Current function tracing op */
79struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050080
jolsa@redhat.com756d17e2009-10-13 16:33:52 -040081/* List for set_ftrace_pid's pids. */
82LIST_HEAD(ftrace_pids);
83struct ftrace_pid {
84 struct list_head list;
85 struct pid *pid;
86};
87
Steven Rostedt4eebcc82008-05-12 21:20:48 +020088/*
89 * ftrace_disabled is set when an anomaly is discovered.
90 * ftrace_disabled is much stronger than ftrace_enabled.
91 */
92static int ftrace_disabled __read_mostly;
93
Steven Rostedt52baf112009-02-14 01:15:39 -050094static DEFINE_MUTEX(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +020095
Steven Rostedtb8489142011-05-04 09:27:52 -040096static struct ftrace_ops *ftrace_global_list __read_mostly = &ftrace_list_end;
Jiri Olsae2484912012-02-15 15:51:48 +010097static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -040098static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020099ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedt6331c282011-07-13 15:11:02 -0400100static ftrace_func_t __ftrace_trace_function_delay __read_mostly = ftrace_stub;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500101ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500102ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
Steven Rostedt2b499382011-05-03 22:49:52 -0400103static struct ftrace_ops global_ops;
Jiri Olsae2484912012-02-15 15:51:48 +0100104static struct ftrace_ops control_ops;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200105
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400106#if ARCH_SUPPORTS_FTRACE_OPS
107static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
108 struct ftrace_ops *op);
109#else
110/* See comment below, where ftrace_ops_list_func is defined */
111static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
112#define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
113#endif
Steven Rostedtb8489142011-05-04 09:27:52 -0400114
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800115/*
Steven Rostedtb8489142011-05-04 09:27:52 -0400116 * Traverse the ftrace_global_list, invoking all entries. The reason that we
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800117 * can use rcu_dereference_raw() is that elements removed from this list
118 * are simply leaked, so there is no need to interact with a grace-period
119 * mechanism. The rcu_dereference_raw() calls are needed to handle
Steven Rostedtb8489142011-05-04 09:27:52 -0400120 * concurrent insertions into the ftrace_global_list.
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800121 *
122 * Silly Alpha and silly pointer-speculation compiler optimizations!
123 */
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400124static void
125ftrace_global_list_func(unsigned long ip, unsigned long parent_ip,
126 struct ftrace_ops *op)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200127{
Steven Rostedtb1cff0a2011-05-25 14:27:43 -0400128 if (unlikely(trace_recursion_test(TRACE_GLOBAL_BIT)))
129 return;
130
131 trace_recursion_set(TRACE_GLOBAL_BIT);
132 op = rcu_dereference_raw(ftrace_global_list); /*see above*/
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200133 while (op != &ftrace_list_end) {
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400134 op->func(ip, parent_ip, op);
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800135 op = rcu_dereference_raw(op->next); /*see above*/
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200136 };
Steven Rostedtb1cff0a2011-05-25 14:27:43 -0400137 trace_recursion_clear(TRACE_GLOBAL_BIT);
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200138}
139
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400140static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
141 struct ftrace_ops *op)
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500142{
Steven Rostedt0ef8cde2008-12-03 15:36:58 -0500143 if (!test_tsk_trace_trace(current))
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500144 return;
145
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400146 ftrace_pid_function(ip, parent_ip, op);
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500147}
148
149static void set_ftrace_pid_function(ftrace_func_t func)
150{
151 /* do not set ftrace_pid_function to itself! */
152 if (func != ftrace_pid_func)
153 ftrace_pid_function = func;
154}
155
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200156/**
Steven Rostedt3d083392008-05-12 21:20:42 +0200157 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200158 *
Steven Rostedt3d083392008-05-12 21:20:42 +0200159 * This NULLs the ftrace function and in essence stops
160 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200161 */
Steven Rostedt3d083392008-05-12 21:20:42 +0200162void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200163{
Steven Rostedt3d083392008-05-12 21:20:42 +0200164 ftrace_trace_function = ftrace_stub;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500165 __ftrace_trace_function = ftrace_stub;
Steven Rostedt6331c282011-07-13 15:11:02 -0400166 __ftrace_trace_function_delay = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500167 ftrace_pid_function = ftrace_stub;
Steven Rostedt3d083392008-05-12 21:20:42 +0200168}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200169
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500170#ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
171/*
172 * For those archs that do not test ftrace_trace_stop in their
173 * mcount call site, we need to do it from C.
174 */
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400175static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip,
176 struct ftrace_ops *op)
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500177{
178 if (function_trace_stop)
179 return;
180
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400181 __ftrace_trace_function(ip, parent_ip, op);
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500182}
183#endif
184
Jiri Olsae2484912012-02-15 15:51:48 +0100185static void control_ops_disable_all(struct ftrace_ops *ops)
186{
187 int cpu;
188
189 for_each_possible_cpu(cpu)
190 *per_cpu_ptr(ops->disabled, cpu) = 1;
191}
192
193static int control_ops_alloc(struct ftrace_ops *ops)
194{
195 int __percpu *disabled;
196
197 disabled = alloc_percpu(int);
198 if (!disabled)
199 return -ENOMEM;
200
201 ops->disabled = disabled;
202 control_ops_disable_all(ops);
203 return 0;
204}
205
206static void control_ops_free(struct ftrace_ops *ops)
207{
208 free_percpu(ops->disabled);
209}
210
Steven Rostedt2b499382011-05-03 22:49:52 -0400211static void update_global_ops(void)
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400212{
213 ftrace_func_t func;
214
215 /*
216 * If there's only one function registered, then call that
217 * function directly. Otherwise, we need to iterate over the
218 * registered callers.
219 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400220 if (ftrace_global_list == &ftrace_list_end ||
221 ftrace_global_list->next == &ftrace_list_end)
222 func = ftrace_global_list->func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400223 else
Steven Rostedtb8489142011-05-04 09:27:52 -0400224 func = ftrace_global_list_func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400225
226 /* If we filter on pids, update to use the pid function */
227 if (!list_empty(&ftrace_pids)) {
228 set_ftrace_pid_function(func);
229 func = ftrace_pid_func;
230 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400231
232 global_ops.func = func;
233}
234
235static void update_ftrace_function(void)
236{
237 ftrace_func_t func;
238
239 update_global_ops();
240
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400241 /*
242 * If we are at the end of the list and this ops is
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400243 * not dynamic and the arch supports passing ops, then have the
244 * mcount trampoline call the function directly.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400245 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400246 if (ftrace_ops_list == &ftrace_list_end ||
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400247 (ftrace_ops_list->next == &ftrace_list_end &&
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400248 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC) &&
249 ARCH_SUPPORTS_FTRACE_OPS)) {
250 /* Set the ftrace_ops that the arch callback uses */
251 if (ftrace_ops_list == &global_ops)
252 function_trace_op = ftrace_global_list;
253 else
254 function_trace_op = ftrace_ops_list;
Steven Rostedtb8489142011-05-04 09:27:52 -0400255 func = ftrace_ops_list->func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400256 } else {
257 /* Just use the default ftrace_ops */
258 function_trace_op = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400259 func = ftrace_ops_list_func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400260 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400261
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400262#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
263 ftrace_trace_function = func;
264#else
Steven Rostedt6331c282011-07-13 15:11:02 -0400265#ifdef CONFIG_DYNAMIC_FTRACE
266 /* do not update till all functions have been modified */
267 __ftrace_trace_function_delay = func;
268#else
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400269 __ftrace_trace_function = func;
Steven Rostedt6331c282011-07-13 15:11:02 -0400270#endif
Rajesh Bhagatdb6544e2012-02-17 13:59:15 +0530271 ftrace_trace_function =
272 (func == ftrace_stub) ? func : ftrace_test_stop_func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400273#endif
274}
275
Steven Rostedt2b499382011-05-03 22:49:52 -0400276static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200277{
Steven Rostedt2b499382011-05-03 22:49:52 -0400278 ops->next = *list;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200279 /*
Steven Rostedtb8489142011-05-04 09:27:52 -0400280 * We are entering ops into the list but another
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200281 * CPU might be walking that list. We need to make sure
282 * the ops->next pointer is valid before another CPU sees
Steven Rostedtb8489142011-05-04 09:27:52 -0400283 * the ops pointer included into the list.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200284 */
Steven Rostedt2b499382011-05-03 22:49:52 -0400285 rcu_assign_pointer(*list, ops);
286}
Steven Rostedt3d083392008-05-12 21:20:42 +0200287
Steven Rostedt2b499382011-05-03 22:49:52 -0400288static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
289{
290 struct ftrace_ops **p;
291
292 /*
293 * If we are removing the last function, then simply point
294 * to the ftrace_stub.
295 */
296 if (*list == ops && ops->next == &ftrace_list_end) {
297 *list = &ftrace_list_end;
298 return 0;
299 }
300
301 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
302 if (*p == ops)
303 break;
304
305 if (*p != ops)
306 return -1;
307
308 *p = (*p)->next;
309 return 0;
310}
311
Jiri Olsae2484912012-02-15 15:51:48 +0100312static void add_ftrace_list_ops(struct ftrace_ops **list,
313 struct ftrace_ops *main_ops,
314 struct ftrace_ops *ops)
315{
316 int first = *list == &ftrace_list_end;
317 add_ftrace_ops(list, ops);
318 if (first)
319 add_ftrace_ops(&ftrace_ops_list, main_ops);
320}
321
322static int remove_ftrace_list_ops(struct ftrace_ops **list,
323 struct ftrace_ops *main_ops,
324 struct ftrace_ops *ops)
325{
326 int ret = remove_ftrace_ops(list, ops);
327 if (!ret && *list == &ftrace_list_end)
328 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
329 return ret;
330}
331
Steven Rostedt2b499382011-05-03 22:49:52 -0400332static int __register_ftrace_function(struct ftrace_ops *ops)
333{
Borislav Petkov8d240dd2012-03-29 19:11:40 +0200334 if (unlikely(ftrace_disabled))
Steven Rostedt2b499382011-05-03 22:49:52 -0400335 return -ENODEV;
336
337 if (FTRACE_WARN_ON(ops == &global_ops))
338 return -EINVAL;
339
Steven Rostedtb8489142011-05-04 09:27:52 -0400340 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
341 return -EBUSY;
342
Jiri Olsae2484912012-02-15 15:51:48 +0100343 /* We don't support both control and global flags set. */
344 if ((ops->flags & FL_GLOBAL_CONTROL_MASK) == FL_GLOBAL_CONTROL_MASK)
345 return -EINVAL;
346
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400347 if (!core_kernel_data((unsigned long)ops))
348 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
349
Steven Rostedtb8489142011-05-04 09:27:52 -0400350 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100351 add_ftrace_list_ops(&ftrace_global_list, &global_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400352 ops->flags |= FTRACE_OPS_FL_ENABLED;
Jiri Olsae2484912012-02-15 15:51:48 +0100353 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
354 if (control_ops_alloc(ops))
355 return -ENOMEM;
356 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400357 } else
358 add_ftrace_ops(&ftrace_ops_list, ops);
359
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400360 if (ftrace_enabled)
361 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200362
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200363 return 0;
364}
365
Ingo Molnare309b412008-05-12 21:20:51 +0200366static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200367{
Steven Rostedt2b499382011-05-03 22:49:52 -0400368 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200369
Steven Rostedt2b499382011-05-03 22:49:52 -0400370 if (ftrace_disabled)
371 return -ENODEV;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200372
Steven Rostedtb8489142011-05-04 09:27:52 -0400373 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
374 return -EBUSY;
375
Steven Rostedt2b499382011-05-03 22:49:52 -0400376 if (FTRACE_WARN_ON(ops == &global_ops))
377 return -EINVAL;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200378
Steven Rostedtb8489142011-05-04 09:27:52 -0400379 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100380 ret = remove_ftrace_list_ops(&ftrace_global_list,
381 &global_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400382 if (!ret)
383 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Jiri Olsae2484912012-02-15 15:51:48 +0100384 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
385 ret = remove_ftrace_list_ops(&ftrace_control_list,
386 &control_ops, ops);
387 if (!ret) {
388 /*
389 * The ftrace_ops is now removed from the list,
390 * so there'll be no new users. We must ensure
391 * all current users are done before we free
392 * the control data.
393 */
394 synchronize_sched();
395 control_ops_free(ops);
396 }
Steven Rostedtb8489142011-05-04 09:27:52 -0400397 } else
398 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
399
Steven Rostedt2b499382011-05-03 22:49:52 -0400400 if (ret < 0)
401 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400402
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400403 if (ftrace_enabled)
404 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200405
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400406 /*
407 * Dynamic ops may be freed, we must make sure that all
408 * callers are done before leaving this function.
409 */
410 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
411 synchronize_sched();
412
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500413 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200414}
415
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500416static void ftrace_update_pid_func(void)
417{
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400418 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500419 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900420 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500421
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400422 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500423}
424
Steven Rostedt493762f2009-03-23 17:12:36 -0400425#ifdef CONFIG_FUNCTION_PROFILER
426struct ftrace_profile {
427 struct hlist_node node;
428 unsigned long ip;
429 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400430#ifdef CONFIG_FUNCTION_GRAPH_TRACER
431 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400432 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400433#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400434};
435
436struct ftrace_profile_page {
437 struct ftrace_profile_page *next;
438 unsigned long index;
439 struct ftrace_profile records[];
440};
441
Steven Rostedtcafb1682009-03-24 20:50:39 -0400442struct ftrace_profile_stat {
443 atomic_t disabled;
444 struct hlist_head *hash;
445 struct ftrace_profile_page *pages;
446 struct ftrace_profile_page *start;
447 struct tracer_stat stat;
448};
449
Steven Rostedt493762f2009-03-23 17:12:36 -0400450#define PROFILE_RECORDS_SIZE \
451 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
452
453#define PROFILES_PER_PAGE \
454 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
455
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400456static int ftrace_profile_bits __read_mostly;
457static int ftrace_profile_enabled __read_mostly;
458
459/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400460static DEFINE_MUTEX(ftrace_profile_lock);
461
Steven Rostedtcafb1682009-03-24 20:50:39 -0400462static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400463
464#define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
465
Steven Rostedt493762f2009-03-23 17:12:36 -0400466static void *
467function_stat_next(void *v, int idx)
468{
469 struct ftrace_profile *rec = v;
470 struct ftrace_profile_page *pg;
471
472 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
473
474 again:
Li Zefan0296e422009-06-26 11:15:37 +0800475 if (idx != 0)
476 rec++;
477
Steven Rostedt493762f2009-03-23 17:12:36 -0400478 if ((void *)rec >= (void *)&pg->records[pg->index]) {
479 pg = pg->next;
480 if (!pg)
481 return NULL;
482 rec = &pg->records[0];
483 if (!rec->counter)
484 goto again;
485 }
486
487 return rec;
488}
489
490static void *function_stat_start(struct tracer_stat *trace)
491{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400492 struct ftrace_profile_stat *stat =
493 container_of(trace, struct ftrace_profile_stat, stat);
494
495 if (!stat || !stat->start)
496 return NULL;
497
498 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400499}
500
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400501#ifdef CONFIG_FUNCTION_GRAPH_TRACER
502/* function graph compares on total time */
503static int function_stat_cmp(void *p1, void *p2)
504{
505 struct ftrace_profile *a = p1;
506 struct ftrace_profile *b = p2;
507
508 if (a->time < b->time)
509 return -1;
510 if (a->time > b->time)
511 return 1;
512 else
513 return 0;
514}
515#else
516/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400517static int function_stat_cmp(void *p1, void *p2)
518{
519 struct ftrace_profile *a = p1;
520 struct ftrace_profile *b = p2;
521
522 if (a->counter < b->counter)
523 return -1;
524 if (a->counter > b->counter)
525 return 1;
526 else
527 return 0;
528}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400529#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400530
531static int function_stat_headers(struct seq_file *m)
532{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400533#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400534 seq_printf(m, " Function "
Chase Douglase330b3b2010-04-26 14:02:05 -0400535 "Hit Time Avg s^2\n"
Steven Rostedt34886c82009-03-25 21:00:47 -0400536 " -------- "
Chase Douglase330b3b2010-04-26 14:02:05 -0400537 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400538#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400539 seq_printf(m, " Function Hit\n"
540 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400541#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400542 return 0;
543}
544
545static int function_stat_show(struct seq_file *m, void *v)
546{
547 struct ftrace_profile *rec = v;
548 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800549 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400550#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400551 static struct trace_seq s;
552 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400553 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400554#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800555 mutex_lock(&ftrace_profile_lock);
556
557 /* we raced with function_profile_reset() */
558 if (unlikely(rec->counter == 0)) {
559 ret = -EBUSY;
560 goto out;
561 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400562
563 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400564 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400565
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400566#ifdef CONFIG_FUNCTION_GRAPH_TRACER
567 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400568 avg = rec->time;
569 do_div(avg, rec->counter);
570
Chase Douglase330b3b2010-04-26 14:02:05 -0400571 /* Sample standard deviation (s^2) */
572 if (rec->counter <= 1)
573 stddev = 0;
574 else {
575 stddev = rec->time_squared - rec->counter * avg * avg;
576 /*
577 * Divide only 1000 for ns^2 -> us^2 conversion.
578 * trace_print_graph_duration will divide 1000 again.
579 */
580 do_div(stddev, (rec->counter - 1) * 1000);
581 }
582
Steven Rostedt34886c82009-03-25 21:00:47 -0400583 trace_seq_init(&s);
584 trace_print_graph_duration(rec->time, &s);
585 trace_seq_puts(&s, " ");
586 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400587 trace_seq_puts(&s, " ");
588 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400589 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400590#endif
591 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800592out:
593 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400594
Li Zefan3aaba202010-08-23 16:50:12 +0800595 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400596}
597
Steven Rostedtcafb1682009-03-24 20:50:39 -0400598static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400599{
600 struct ftrace_profile_page *pg;
601
Steven Rostedtcafb1682009-03-24 20:50:39 -0400602 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400603
604 while (pg) {
605 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
606 pg->index = 0;
607 pg = pg->next;
608 }
609
Steven Rostedtcafb1682009-03-24 20:50:39 -0400610 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400611 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
612}
613
Steven Rostedtcafb1682009-03-24 20:50:39 -0400614int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400615{
616 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400617 int functions;
618 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400619 int i;
620
621 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400622 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400623 return 0;
624
Steven Rostedtcafb1682009-03-24 20:50:39 -0400625 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
626 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400627 return -ENOMEM;
628
Steven Rostedt318e0a72009-03-25 20:06:34 -0400629#ifdef CONFIG_DYNAMIC_FTRACE
630 functions = ftrace_update_tot_cnt;
631#else
632 /*
633 * We do not know the number of functions that exist because
634 * dynamic tracing is what counts them. With past experience
635 * we have around 20K functions. That should be more than enough.
636 * It is highly unlikely we will execute every function in
637 * the kernel.
638 */
639 functions = 20000;
640#endif
641
Steven Rostedtcafb1682009-03-24 20:50:39 -0400642 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400643
Steven Rostedt318e0a72009-03-25 20:06:34 -0400644 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
645
646 for (i = 0; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400647 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400648 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400649 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400650 pg = pg->next;
651 }
652
653 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400654
655 out_free:
656 pg = stat->start;
657 while (pg) {
658 unsigned long tmp = (unsigned long)pg;
659
660 pg = pg->next;
661 free_page(tmp);
662 }
663
664 free_page((unsigned long)stat->pages);
665 stat->pages = NULL;
666 stat->start = NULL;
667
668 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400669}
670
Steven Rostedtcafb1682009-03-24 20:50:39 -0400671static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400672{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400673 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400674 int size;
675
Steven Rostedtcafb1682009-03-24 20:50:39 -0400676 stat = &per_cpu(ftrace_profile_stats, cpu);
677
678 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400679 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400680 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400681 return 0;
682 }
683
684 /*
685 * We are profiling all functions, but usually only a few thousand
686 * functions are hit. We'll make a hash of 1024 items.
687 */
688 size = FTRACE_PROFILE_HASH_SIZE;
689
Steven Rostedtcafb1682009-03-24 20:50:39 -0400690 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400691
Steven Rostedtcafb1682009-03-24 20:50:39 -0400692 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400693 return -ENOMEM;
694
Steven Rostedtcafb1682009-03-24 20:50:39 -0400695 if (!ftrace_profile_bits) {
696 size--;
Steven Rostedt493762f2009-03-23 17:12:36 -0400697
Steven Rostedtcafb1682009-03-24 20:50:39 -0400698 for (; size; size >>= 1)
699 ftrace_profile_bits++;
700 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400701
Steven Rostedt318e0a72009-03-25 20:06:34 -0400702 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400703 if (ftrace_profile_pages_init(stat) < 0) {
704 kfree(stat->hash);
705 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400706 return -ENOMEM;
707 }
708
709 return 0;
710}
711
Steven Rostedtcafb1682009-03-24 20:50:39 -0400712static int ftrace_profile_init(void)
713{
714 int cpu;
715 int ret = 0;
716
717 for_each_online_cpu(cpu) {
718 ret = ftrace_profile_init_cpu(cpu);
719 if (ret)
720 break;
721 }
722
723 return ret;
724}
725
Steven Rostedt493762f2009-03-23 17:12:36 -0400726/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400727static struct ftrace_profile *
728ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400729{
730 struct ftrace_profile *rec;
731 struct hlist_head *hhd;
732 struct hlist_node *n;
733 unsigned long key;
734
735 key = hash_long(ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400736 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400737
738 if (hlist_empty(hhd))
739 return NULL;
740
741 hlist_for_each_entry_rcu(rec, n, hhd, node) {
742 if (rec->ip == ip)
743 return rec;
744 }
745
746 return NULL;
747}
748
Steven Rostedtcafb1682009-03-24 20:50:39 -0400749static void ftrace_add_profile(struct ftrace_profile_stat *stat,
750 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400751{
752 unsigned long key;
753
754 key = hash_long(rec->ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400755 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400756}
757
Steven Rostedt318e0a72009-03-25 20:06:34 -0400758/*
759 * The memory is already allocated, this simply finds a new record to use.
760 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400761static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400762ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400763{
764 struct ftrace_profile *rec = NULL;
765
Steven Rostedt318e0a72009-03-25 20:06:34 -0400766 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400767 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400768 goto out;
769
Steven Rostedt493762f2009-03-23 17:12:36 -0400770 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400771 * Try to find the function again since an NMI
772 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400773 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400774 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400775 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400776 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400777
Steven Rostedtcafb1682009-03-24 20:50:39 -0400778 if (stat->pages->index == PROFILES_PER_PAGE) {
779 if (!stat->pages->next)
780 goto out;
781 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400782 }
783
Steven Rostedtcafb1682009-03-24 20:50:39 -0400784 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400785 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400786 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400787
Steven Rostedt493762f2009-03-23 17:12:36 -0400788 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400789 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400790
791 return rec;
792}
793
Steven Rostedt493762f2009-03-23 17:12:36 -0400794static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400795function_profile_call(unsigned long ip, unsigned long parent_ip,
796 struct ftrace_ops *ops)
Steven Rostedt493762f2009-03-23 17:12:36 -0400797{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400798 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400799 struct ftrace_profile *rec;
800 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400801
802 if (!ftrace_profile_enabled)
803 return;
804
Steven Rostedt493762f2009-03-23 17:12:36 -0400805 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400806
807 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400808 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400809 goto out;
810
811 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400812 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400813 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400814 if (!rec)
815 goto out;
816 }
817
818 rec->counter++;
819 out:
820 local_irq_restore(flags);
821}
822
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400823#ifdef CONFIG_FUNCTION_GRAPH_TRACER
824static int profile_graph_entry(struct ftrace_graph_ent *trace)
825{
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400826 function_profile_call(trace->func, 0, NULL);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400827 return 1;
828}
829
830static void profile_graph_return(struct ftrace_graph_ret *trace)
831{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400832 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400833 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400834 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400835 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400836
837 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400838 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400839 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400840 goto out;
841
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400842 /* If the calltime was zero'd ignore it */
843 if (!trace->calltime)
844 goto out;
845
Steven Rostedta2a16d62009-03-24 23:17:58 -0400846 calltime = trace->rettime - trace->calltime;
847
848 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
849 int index;
850
851 index = trace->depth;
852
853 /* Append this call time to the parent time to subtract */
854 if (index)
855 current->ret_stack[index - 1].subtime += calltime;
856
857 if (current->ret_stack[index].subtime < calltime)
858 calltime -= current->ret_stack[index].subtime;
859 else
860 calltime = 0;
861 }
862
Steven Rostedtcafb1682009-03-24 20:50:39 -0400863 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400864 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400865 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400866 rec->time_squared += calltime * calltime;
867 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400868
Steven Rostedtcafb1682009-03-24 20:50:39 -0400869 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400870 local_irq_restore(flags);
871}
872
873static int register_ftrace_profiler(void)
874{
875 return register_ftrace_graph(&profile_graph_return,
876 &profile_graph_entry);
877}
878
879static void unregister_ftrace_profiler(void)
880{
881 unregister_ftrace_graph();
882}
883#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100884static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400885 .func = function_profile_call,
Steven Rostedt493762f2009-03-23 17:12:36 -0400886};
887
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400888static int register_ftrace_profiler(void)
889{
890 return register_ftrace_function(&ftrace_profile_ops);
891}
892
893static void unregister_ftrace_profiler(void)
894{
895 unregister_ftrace_function(&ftrace_profile_ops);
896}
897#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
898
Steven Rostedt493762f2009-03-23 17:12:36 -0400899static ssize_t
900ftrace_profile_write(struct file *filp, const char __user *ubuf,
901 size_t cnt, loff_t *ppos)
902{
903 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400904 int ret;
905
Peter Huewe22fe9b52011-06-07 21:58:27 +0200906 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
907 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400908 return ret;
909
910 val = !!val;
911
912 mutex_lock(&ftrace_profile_lock);
913 if (ftrace_profile_enabled ^ val) {
914 if (val) {
915 ret = ftrace_profile_init();
916 if (ret < 0) {
917 cnt = ret;
918 goto out;
919 }
920
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400921 ret = register_ftrace_profiler();
922 if (ret < 0) {
923 cnt = ret;
924 goto out;
925 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400926 ftrace_profile_enabled = 1;
927 } else {
928 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400929 /*
930 * unregister_ftrace_profiler calls stop_machine
931 * so this acts like an synchronize_sched.
932 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400933 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400934 }
935 }
936 out:
937 mutex_unlock(&ftrace_profile_lock);
938
Jiri Olsacf8517c2009-10-23 19:36:16 -0400939 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400940
941 return cnt;
942}
943
944static ssize_t
945ftrace_profile_read(struct file *filp, char __user *ubuf,
946 size_t cnt, loff_t *ppos)
947{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400948 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400949 int r;
950
951 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
952 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
953}
954
955static const struct file_operations ftrace_profile_fops = {
956 .open = tracing_open_generic,
957 .read = ftrace_profile_read,
958 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200959 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -0400960};
961
Steven Rostedtcafb1682009-03-24 20:50:39 -0400962/* used to initialize the real stat files */
963static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400964 .name = "functions",
965 .stat_start = function_stat_start,
966 .stat_next = function_stat_next,
967 .stat_cmp = function_stat_cmp,
968 .stat_headers = function_stat_headers,
969 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -0400970};
971
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400972static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400973{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400974 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400975 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400976 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -0400977 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400978 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -0400979
Steven Rostedtcafb1682009-03-24 20:50:39 -0400980 for_each_possible_cpu(cpu) {
981 stat = &per_cpu(ftrace_profile_stats, cpu);
982
983 /* allocate enough for function name + cpu number */
984 name = kmalloc(32, GFP_KERNEL);
985 if (!name) {
986 /*
987 * The files created are permanent, if something happens
988 * we still do not free memory.
989 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400990 WARN(1,
991 "Could not allocate stat file for cpu %d\n",
992 cpu);
993 return;
994 }
995 stat->stat = function_stats;
996 snprintf(name, 32, "function%d", cpu);
997 stat->stat.name = name;
998 ret = register_stat_tracer(&stat->stat);
999 if (ret) {
1000 WARN(1,
1001 "Could not register function stat for cpu %d\n",
1002 cpu);
1003 kfree(name);
1004 return;
1005 }
Steven Rostedt493762f2009-03-23 17:12:36 -04001006 }
1007
1008 entry = debugfs_create_file("function_profile_enabled", 0644,
1009 d_tracer, NULL, &ftrace_profile_fops);
1010 if (!entry)
1011 pr_warning("Could not create debugfs "
1012 "'function_profile_enabled' entry\n");
1013}
1014
1015#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001016static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001017{
1018}
1019#endif /* CONFIG_FUNCTION_PROFILER */
1020
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001021static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1022
Steven Rostedt3d083392008-05-12 21:20:42 +02001023#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001024
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001025#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001026# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001027#endif
1028
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001029static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1030
Steven Rostedtb6887d72009-02-17 12:32:04 -05001031struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001032 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -05001033 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001034 unsigned long flags;
1035 unsigned long ip;
1036 void *data;
1037 struct rcu_head rcu;
1038};
1039
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001040struct ftrace_func_entry {
1041 struct hlist_node hlist;
1042 unsigned long ip;
1043};
1044
1045struct ftrace_hash {
1046 unsigned long size_bits;
1047 struct hlist_head *buckets;
1048 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001049 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001050};
1051
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001052/*
1053 * We make these constant because no one should touch them,
1054 * but they are used as the default "empty hash", to avoid allocating
1055 * it all the time. These are in a read only section such that if
1056 * anyone does try to modify it, it will cause an exception.
1057 */
1058static const struct hlist_head empty_buckets[1];
1059static const struct ftrace_hash empty_hash = {
1060 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001061};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001062#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001063
Steven Rostedt2b499382011-05-03 22:49:52 -04001064static struct ftrace_ops global_ops = {
Steven Rostedtf45948e2011-05-02 12:29:25 -04001065 .func = ftrace_stub,
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001066 .notrace_hash = EMPTY_HASH,
1067 .filter_hash = EMPTY_HASH,
Steven Rostedtf45948e2011-05-02 12:29:25 -04001068};
1069
Steven Rostedt41c52c02008-05-22 11:46:33 -04001070static DEFINE_MUTEX(ftrace_regex_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02001071
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001072struct ftrace_page {
1073 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001074 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001075 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001076 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001077};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001078
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001079static struct ftrace_page *ftrace_new_pgs;
1080
Steven Rostedta7900872011-12-16 16:23:44 -05001081#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1082#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001083
1084/* estimate from running different kernels */
1085#define NR_TO_INIT 10000
1086
1087static struct ftrace_page *ftrace_pages_start;
1088static struct ftrace_page *ftrace_pages;
1089
Steven Rostedt06a51d92011-12-19 19:07:36 -05001090static bool ftrace_hash_empty(struct ftrace_hash *hash)
1091{
1092 return !hash || !hash->count;
1093}
1094
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001095static struct ftrace_func_entry *
1096ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1097{
1098 unsigned long key;
1099 struct ftrace_func_entry *entry;
1100 struct hlist_head *hhd;
1101 struct hlist_node *n;
1102
Steven Rostedt06a51d92011-12-19 19:07:36 -05001103 if (ftrace_hash_empty(hash))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001104 return NULL;
1105
1106 if (hash->size_bits > 0)
1107 key = hash_long(ip, hash->size_bits);
1108 else
1109 key = 0;
1110
1111 hhd = &hash->buckets[key];
1112
1113 hlist_for_each_entry_rcu(entry, n, hhd, hlist) {
1114 if (entry->ip == ip)
1115 return entry;
1116 }
1117 return NULL;
1118}
1119
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001120static void __add_hash_entry(struct ftrace_hash *hash,
1121 struct ftrace_func_entry *entry)
1122{
1123 struct hlist_head *hhd;
1124 unsigned long key;
1125
1126 if (hash->size_bits)
1127 key = hash_long(entry->ip, hash->size_bits);
1128 else
1129 key = 0;
1130
1131 hhd = &hash->buckets[key];
1132 hlist_add_head(&entry->hlist, hhd);
1133 hash->count++;
1134}
1135
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001136static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1137{
1138 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001139
1140 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1141 if (!entry)
1142 return -ENOMEM;
1143
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001144 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001145 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001146
1147 return 0;
1148}
1149
1150static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001151free_hash_entry(struct ftrace_hash *hash,
1152 struct ftrace_func_entry *entry)
1153{
1154 hlist_del(&entry->hlist);
1155 kfree(entry);
1156 hash->count--;
1157}
1158
1159static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001160remove_hash_entry(struct ftrace_hash *hash,
1161 struct ftrace_func_entry *entry)
1162{
1163 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001164 hash->count--;
1165}
1166
1167static void ftrace_hash_clear(struct ftrace_hash *hash)
1168{
1169 struct hlist_head *hhd;
1170 struct hlist_node *tp, *tn;
1171 struct ftrace_func_entry *entry;
1172 int size = 1 << hash->size_bits;
1173 int i;
1174
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001175 if (!hash->count)
1176 return;
1177
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001178 for (i = 0; i < size; i++) {
1179 hhd = &hash->buckets[i];
1180 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001181 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001182 }
1183 FTRACE_WARN_ON(hash->count);
1184}
1185
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001186static void free_ftrace_hash(struct ftrace_hash *hash)
1187{
1188 if (!hash || hash == EMPTY_HASH)
1189 return;
1190 ftrace_hash_clear(hash);
1191 kfree(hash->buckets);
1192 kfree(hash);
1193}
1194
Steven Rostedt07fd5512011-05-05 18:03:47 -04001195static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1196{
1197 struct ftrace_hash *hash;
1198
1199 hash = container_of(rcu, struct ftrace_hash, rcu);
1200 free_ftrace_hash(hash);
1201}
1202
1203static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1204{
1205 if (!hash || hash == EMPTY_HASH)
1206 return;
1207 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1208}
1209
Jiri Olsa5500fa52012-02-15 15:51:54 +01001210void ftrace_free_filter(struct ftrace_ops *ops)
1211{
1212 free_ftrace_hash(ops->filter_hash);
1213 free_ftrace_hash(ops->notrace_hash);
1214}
1215
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001216static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1217{
1218 struct ftrace_hash *hash;
1219 int size;
1220
1221 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1222 if (!hash)
1223 return NULL;
1224
1225 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001226 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001227
1228 if (!hash->buckets) {
1229 kfree(hash);
1230 return NULL;
1231 }
1232
1233 hash->size_bits = size_bits;
1234
1235 return hash;
1236}
1237
1238static struct ftrace_hash *
1239alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1240{
1241 struct ftrace_func_entry *entry;
1242 struct ftrace_hash *new_hash;
1243 struct hlist_node *tp;
1244 int size;
1245 int ret;
1246 int i;
1247
1248 new_hash = alloc_ftrace_hash(size_bits);
1249 if (!new_hash)
1250 return NULL;
1251
1252 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001253 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001254 return new_hash;
1255
1256 size = 1 << hash->size_bits;
1257 for (i = 0; i < size; i++) {
1258 hlist_for_each_entry(entry, tp, &hash->buckets[i], hlist) {
1259 ret = add_hash_entry(new_hash, entry->ip);
1260 if (ret < 0)
1261 goto free_hash;
1262 }
1263 }
1264
1265 FTRACE_WARN_ON(new_hash->count != hash->count);
1266
1267 return new_hash;
1268
1269 free_hash:
1270 free_ftrace_hash(new_hash);
1271 return NULL;
1272}
1273
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001274static void
1275ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1276static void
1277ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1278
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001279static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001280ftrace_hash_move(struct ftrace_ops *ops, int enable,
1281 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001282{
1283 struct ftrace_func_entry *entry;
1284 struct hlist_node *tp, *tn;
1285 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001286 struct ftrace_hash *old_hash;
1287 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001288 unsigned long key;
1289 int size = src->count;
1290 int bits = 0;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001291 int ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001292 int i;
1293
1294 /*
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001295 * Remove the current set, update the hash and add
1296 * them back.
1297 */
1298 ftrace_hash_rec_disable(ops, enable);
1299
1300 /*
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001301 * If the new source is empty, just free dst and assign it
1302 * the empty_hash.
1303 */
1304 if (!src->count) {
Steven Rostedt07fd5512011-05-05 18:03:47 -04001305 free_ftrace_hash_rcu(*dst);
1306 rcu_assign_pointer(*dst, EMPTY_HASH);
Steven Rostedtd4d34b92011-11-04 20:32:39 -04001307 /* still need to update the function records */
1308 ret = 0;
1309 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001310 }
1311
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001312 /*
1313 * Make the hash size about 1/2 the # found
1314 */
1315 for (size /= 2; size; size >>= 1)
1316 bits++;
1317
1318 /* Don't allocate too much */
1319 if (bits > FTRACE_HASH_MAX_BITS)
1320 bits = FTRACE_HASH_MAX_BITS;
1321
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001322 ret = -ENOMEM;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001323 new_hash = alloc_ftrace_hash(bits);
1324 if (!new_hash)
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001325 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001326
1327 size = 1 << src->size_bits;
1328 for (i = 0; i < size; i++) {
1329 hhd = &src->buckets[i];
1330 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist) {
1331 if (bits > 0)
1332 key = hash_long(entry->ip, bits);
1333 else
1334 key = 0;
1335 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001336 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001337 }
1338 }
1339
Steven Rostedt07fd5512011-05-05 18:03:47 -04001340 old_hash = *dst;
1341 rcu_assign_pointer(*dst, new_hash);
1342 free_ftrace_hash_rcu(old_hash);
1343
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001344 ret = 0;
1345 out:
1346 /*
1347 * Enable regardless of ret:
1348 * On success, we enable the new hash.
1349 * On failure, we re-enable the original hash.
1350 */
1351 ftrace_hash_rec_enable(ops, enable);
1352
1353 return ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001354}
1355
Steven Rostedt265c8312009-02-13 12:43:56 -05001356/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001357 * Test the hashes for this ops to see if we want to call
1358 * the ops->func or not.
1359 *
1360 * It's a match if the ip is in the ops->filter_hash or
1361 * the filter_hash does not exist or is empty,
1362 * AND
1363 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001364 *
1365 * This needs to be called with preemption disabled as
1366 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001367 */
1368static int
1369ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
1370{
1371 struct ftrace_hash *filter_hash;
1372 struct ftrace_hash *notrace_hash;
1373 int ret;
1374
Steven Rostedtb8489142011-05-04 09:27:52 -04001375 filter_hash = rcu_dereference_raw(ops->filter_hash);
1376 notrace_hash = rcu_dereference_raw(ops->notrace_hash);
1377
Steven Rostedt06a51d92011-12-19 19:07:36 -05001378 if ((ftrace_hash_empty(filter_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001379 ftrace_lookup_ip(filter_hash, ip)) &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001380 (ftrace_hash_empty(notrace_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001381 !ftrace_lookup_ip(notrace_hash, ip)))
1382 ret = 1;
1383 else
1384 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001385
1386 return ret;
1387}
1388
1389/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001390 * This is a double for. Do not use 'break' to break out of the loop,
1391 * you must use a goto.
1392 */
1393#define do_for_each_ftrace_rec(pg, rec) \
1394 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1395 int _____i; \
1396 for (_____i = 0; _____i < pg->index; _____i++) { \
1397 rec = &pg->records[_____i];
1398
1399#define while_for_each_ftrace_rec() \
1400 } \
1401 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301402
Steven Rostedt5855fea2011-12-16 19:27:42 -05001403
1404static int ftrace_cmp_recs(const void *a, const void *b)
1405{
Steven Rostedta650e022012-04-25 13:48:13 -04001406 const struct dyn_ftrace *key = a;
1407 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001408
Steven Rostedta650e022012-04-25 13:48:13 -04001409 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001410 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001411 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1412 return 1;
1413 return 0;
1414}
1415
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001416static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001417{
1418 struct ftrace_page *pg;
1419 struct dyn_ftrace *rec;
1420 struct dyn_ftrace key;
1421
1422 key.ip = start;
1423 key.flags = end; /* overload flags, as it is unsigned long */
1424
1425 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1426 if (end < pg->records[0].ip ||
1427 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1428 continue;
1429 rec = bsearch(&key, pg->records, pg->index,
1430 sizeof(struct dyn_ftrace),
1431 ftrace_cmp_recs);
1432 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001433 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001434 }
1435
Steven Rostedt5855fea2011-12-16 19:27:42 -05001436 return 0;
1437}
1438
Steven Rostedtc88fd862011-08-16 09:53:39 -04001439/**
1440 * ftrace_location - return true if the ip giving is a traced location
1441 * @ip: the instruction pointer to check
1442 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001443 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001444 * That is, the instruction that is either a NOP or call to
1445 * the function tracer. It checks the ftrace internal tables to
1446 * determine if the address belongs or not.
1447 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001448unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001449{
Steven Rostedta650e022012-04-25 13:48:13 -04001450 return ftrace_location_range(ip, ip);
1451}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001452
Steven Rostedta650e022012-04-25 13:48:13 -04001453/**
1454 * ftrace_text_reserved - return true if range contains an ftrace location
1455 * @start: start of range to search
1456 * @end: end of range to search (inclusive). @end points to the last byte to check.
1457 *
1458 * Returns 1 if @start and @end contains a ftrace location.
1459 * That is, the instruction that is either a NOP or call to
1460 * the function tracer. It checks the ftrace internal tables to
1461 * determine if the address belongs or not.
1462 */
1463int ftrace_text_reserved(void *start, void *end)
1464{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001465 unsigned long ret;
1466
1467 ret = ftrace_location_range((unsigned long)start,
1468 (unsigned long)end);
1469
1470 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001471}
1472
Steven Rostedted926f92011-05-03 13:25:24 -04001473static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1474 int filter_hash,
1475 bool inc)
1476{
1477 struct ftrace_hash *hash;
1478 struct ftrace_hash *other_hash;
1479 struct ftrace_page *pg;
1480 struct dyn_ftrace *rec;
1481 int count = 0;
1482 int all = 0;
1483
1484 /* Only update if the ops has been registered */
1485 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1486 return;
1487
1488 /*
1489 * In the filter_hash case:
1490 * If the count is zero, we update all records.
1491 * Otherwise we just update the items in the hash.
1492 *
1493 * In the notrace_hash case:
1494 * We enable the update in the hash.
1495 * As disabling notrace means enabling the tracing,
1496 * and enabling notrace means disabling, the inc variable
1497 * gets inversed.
1498 */
1499 if (filter_hash) {
1500 hash = ops->filter_hash;
1501 other_hash = ops->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001502 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001503 all = 1;
1504 } else {
1505 inc = !inc;
1506 hash = ops->notrace_hash;
1507 other_hash = ops->filter_hash;
1508 /*
1509 * If the notrace hash has no items,
1510 * then there's nothing to do.
1511 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001512 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001513 return;
1514 }
1515
1516 do_for_each_ftrace_rec(pg, rec) {
1517 int in_other_hash = 0;
1518 int in_hash = 0;
1519 int match = 0;
1520
1521 if (all) {
1522 /*
1523 * Only the filter_hash affects all records.
1524 * Update if the record is not in the notrace hash.
1525 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001526 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001527 match = 1;
1528 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001529 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1530 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001531
1532 /*
1533 *
1534 */
1535 if (filter_hash && in_hash && !in_other_hash)
1536 match = 1;
1537 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001538 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001539 match = 1;
1540 }
1541 if (!match)
1542 continue;
1543
1544 if (inc) {
1545 rec->flags++;
1546 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1547 return;
1548 } else {
1549 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1550 return;
1551 rec->flags--;
1552 }
1553 count++;
1554 /* Shortcut, if we handled all records, we are done. */
1555 if (!all && count == hash->count)
1556 return;
1557 } while_for_each_ftrace_rec();
1558}
1559
1560static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1561 int filter_hash)
1562{
1563 __ftrace_hash_rec_update(ops, filter_hash, 0);
1564}
1565
1566static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1567 int filter_hash)
1568{
1569 __ftrace_hash_rec_update(ops, filter_hash, 1);
1570}
1571
Steven Rostedt05736a42008-09-22 14:55:47 -07001572static void print_ip_ins(const char *fmt, unsigned char *p)
1573{
1574 int i;
1575
1576 printk(KERN_CONT "%s", fmt);
1577
1578 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1579 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1580}
1581
Steven Rostedtc88fd862011-08-16 09:53:39 -04001582/**
1583 * ftrace_bug - report and shutdown function tracer
1584 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1585 * @ip: The address that failed
1586 *
1587 * The arch code that enables or disables the function tracing
1588 * can call ftrace_bug() when it has detected a problem in
1589 * modifying the code. @failed should be one of either:
1590 * EFAULT - if the problem happens on reading the @ip address
1591 * EINVAL - if what is read at @ip is not what was expected
1592 * EPERM - if the problem happens on writting to the @ip address
1593 */
1594void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001595{
1596 switch (failed) {
1597 case -EFAULT:
1598 FTRACE_WARN_ON_ONCE(1);
1599 pr_info("ftrace faulted on modifying ");
1600 print_ip_sym(ip);
1601 break;
1602 case -EINVAL:
1603 FTRACE_WARN_ON_ONCE(1);
1604 pr_info("ftrace failed to modify ");
1605 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001606 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001607 printk(KERN_CONT "\n");
1608 break;
1609 case -EPERM:
1610 FTRACE_WARN_ON_ONCE(1);
1611 pr_info("ftrace faulted on writing ");
1612 print_ip_sym(ip);
1613 break;
1614 default:
1615 FTRACE_WARN_ON_ONCE(1);
1616 pr_info("ftrace faulted on unknown error ");
1617 print_ip_sym(ip);
1618 }
1619}
1620
Steven Rostedtc88fd862011-08-16 09:53:39 -04001621static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02001622{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001623 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001624
Steven Rostedt982c3502008-11-15 16:31:41 -05001625 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001626 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05001627 *
Steven Rostedted926f92011-05-03 13:25:24 -04001628 * If the record has a ref count, then we need to enable it
1629 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001630 *
Steven Rostedted926f92011-05-03 13:25:24 -04001631 * Otherwise we make sure its disabled.
1632 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001633 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04001634 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001635 */
Steven Rostedtc88fd862011-08-16 09:53:39 -04001636 if (enable && (rec->flags & ~FTRACE_FL_MASK))
Steven Rostedted926f92011-05-03 13:25:24 -04001637 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001638
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001639 /* If the state of this record hasn't changed, then do nothing */
1640 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001641 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001642
1643 if (flag) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04001644 if (update)
1645 rec->flags |= FTRACE_FL_ENABLED;
1646 return FTRACE_UPDATE_MAKE_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001647 }
1648
Steven Rostedtc88fd862011-08-16 09:53:39 -04001649 if (update)
1650 rec->flags &= ~FTRACE_FL_ENABLED;
1651
1652 return FTRACE_UPDATE_MAKE_NOP;
1653}
1654
1655/**
1656 * ftrace_update_record, set a record that now is tracing or not
1657 * @rec: the record to update
1658 * @enable: set to 1 if the record is tracing, zero to force disable
1659 *
1660 * The records that represent all functions that can be traced need
1661 * to be updated when tracing has been enabled.
1662 */
1663int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1664{
1665 return ftrace_check_record(rec, enable, 1);
1666}
1667
1668/**
1669 * ftrace_test_record, check if the record has been enabled or not
1670 * @rec: the record to test
1671 * @enable: set to 1 to check if enabled, 0 if it is disabled
1672 *
1673 * The arch code may need to test if a record is already set to
1674 * tracing to determine how to modify the function code that it
1675 * represents.
1676 */
1677int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1678{
1679 return ftrace_check_record(rec, enable, 0);
1680}
1681
1682static int
1683__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1684{
1685 unsigned long ftrace_addr;
1686 int ret;
1687
1688 ftrace_addr = (unsigned long)FTRACE_ADDR;
1689
1690 ret = ftrace_update_record(rec, enable);
1691
1692 switch (ret) {
1693 case FTRACE_UPDATE_IGNORE:
1694 return 0;
1695
1696 case FTRACE_UPDATE_MAKE_CALL:
1697 return ftrace_make_call(rec, ftrace_addr);
1698
1699 case FTRACE_UPDATE_MAKE_NOP:
1700 return ftrace_make_nop(NULL, rec, ftrace_addr);
1701 }
1702
1703 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02001704}
1705
Steven Rostedte4f5d542012-04-27 09:13:18 -04001706void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001707{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001708 struct dyn_ftrace *rec;
1709 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001710 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001711
Steven Rostedt45a4a232011-04-21 23:16:46 -04001712 if (unlikely(ftrace_disabled))
1713 return;
1714
Steven Rostedt265c8312009-02-13 12:43:56 -05001715 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedte4f5d542012-04-27 09:13:18 -04001716 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001717 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04001718 ftrace_bug(failed, rec->ip);
1719 /* Stop processing */
1720 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001721 }
1722 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001723}
1724
Steven Rostedtc88fd862011-08-16 09:53:39 -04001725struct ftrace_rec_iter {
1726 struct ftrace_page *pg;
1727 int index;
1728};
1729
1730/**
1731 * ftrace_rec_iter_start, start up iterating over traced functions
1732 *
1733 * Returns an iterator handle that is used to iterate over all
1734 * the records that represent address locations where functions
1735 * are traced.
1736 *
1737 * May return NULL if no records are available.
1738 */
1739struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1740{
1741 /*
1742 * We only use a single iterator.
1743 * Protected by the ftrace_lock mutex.
1744 */
1745 static struct ftrace_rec_iter ftrace_rec_iter;
1746 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1747
1748 iter->pg = ftrace_pages_start;
1749 iter->index = 0;
1750
1751 /* Could have empty pages */
1752 while (iter->pg && !iter->pg->index)
1753 iter->pg = iter->pg->next;
1754
1755 if (!iter->pg)
1756 return NULL;
1757
1758 return iter;
1759}
1760
1761/**
1762 * ftrace_rec_iter_next, get the next record to process.
1763 * @iter: The handle to the iterator.
1764 *
1765 * Returns the next iterator after the given iterator @iter.
1766 */
1767struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1768{
1769 iter->index++;
1770
1771 if (iter->index >= iter->pg->index) {
1772 iter->pg = iter->pg->next;
1773 iter->index = 0;
1774
1775 /* Could have empty pages */
1776 while (iter->pg && !iter->pg->index)
1777 iter->pg = iter->pg->next;
1778 }
1779
1780 if (!iter->pg)
1781 return NULL;
1782
1783 return iter;
1784}
1785
1786/**
1787 * ftrace_rec_iter_record, get the record at the iterator location
1788 * @iter: The current iterator location
1789 *
1790 * Returns the record that the current @iter is at.
1791 */
1792struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1793{
1794 return &iter->pg->records[iter->index];
1795}
1796
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301797static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001798ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001799{
1800 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001801 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001802
1803 ip = rec->ip;
1804
Steven Rostedt45a4a232011-04-21 23:16:46 -04001805 if (unlikely(ftrace_disabled))
1806 return 0;
1807
Shaohua Li25aac9d2009-01-09 11:29:40 +08001808 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001809 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001810 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301811 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02001812 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301813 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001814}
1815
Steven Rostedt000ab692009-02-17 13:35:06 -05001816/*
1817 * archs can override this function if they must do something
1818 * before the modifying code is performed.
1819 */
1820int __weak ftrace_arch_code_modify_prepare(void)
1821{
1822 return 0;
1823}
1824
1825/*
1826 * archs can override this function if they must do something
1827 * after the modifying code is performed.
1828 */
1829int __weak ftrace_arch_code_modify_post_process(void)
1830{
1831 return 0;
1832}
1833
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001834void ftrace_modify_all_code(int command)
1835{
1836 if (command & FTRACE_UPDATE_CALLS)
1837 ftrace_replace_code(1);
1838 else if (command & FTRACE_DISABLE_CALLS)
1839 ftrace_replace_code(0);
1840
1841 if (command & FTRACE_UPDATE_TRACE_FUNC)
1842 ftrace_update_ftrace_func(ftrace_trace_function);
1843
1844 if (command & FTRACE_START_FUNC_RET)
1845 ftrace_enable_ftrace_graph_caller();
1846 else if (command & FTRACE_STOP_FUNC_RET)
1847 ftrace_disable_ftrace_graph_caller();
1848}
1849
Ingo Molnare309b412008-05-12 21:20:51 +02001850static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02001851{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001852 int *command = data;
1853
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001854 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001855
Steven Rostedtc88fd862011-08-16 09:53:39 -04001856 return 0;
1857}
1858
1859/**
1860 * ftrace_run_stop_machine, go back to the stop machine method
1861 * @command: The command to tell ftrace what to do
1862 *
1863 * If an arch needs to fall back to the stop machine method, the
1864 * it can call this function.
1865 */
1866void ftrace_run_stop_machine(int command)
1867{
1868 stop_machine(__ftrace_modify_code, &command, NULL);
1869}
1870
1871/**
1872 * arch_ftrace_update_code, modify the code to trace or not trace
1873 * @command: The command that needs to be done
1874 *
1875 * Archs can override this function if it does not need to
1876 * run stop_machine() to modify code.
1877 */
1878void __weak arch_ftrace_update_code(int command)
1879{
1880 ftrace_run_stop_machine(command);
1881}
1882
1883static void ftrace_run_update_code(int command)
1884{
1885 int ret;
1886
1887 ret = ftrace_arch_code_modify_prepare();
1888 FTRACE_WARN_ON(ret);
1889 if (ret)
1890 return;
1891 /*
1892 * Do not call function tracer while we update the code.
1893 * We are in stop machine.
1894 */
1895 function_trace_stop++;
1896
1897 /*
1898 * By default we use stop_machine() to modify the code.
1899 * But archs can do what ever they want as long as it
1900 * is safe. The stop_machine() is the safest, but also
1901 * produces the most overhead.
1902 */
1903 arch_ftrace_update_code(command);
1904
Steven Rostedt6331c282011-07-13 15:11:02 -04001905#ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
1906 /*
1907 * For archs that call ftrace_test_stop_func(), we must
1908 * wait till after we update all the function callers
1909 * before we update the callback. This keeps different
1910 * ops that record different functions from corrupting
1911 * each other.
1912 */
1913 __ftrace_trace_function = __ftrace_trace_function_delay;
1914#endif
1915 function_trace_stop--;
1916
Steven Rostedt000ab692009-02-17 13:35:06 -05001917 ret = ftrace_arch_code_modify_post_process();
1918 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02001919}
1920
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001921static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001922static int ftrace_start_up;
Steven Rostedtb8489142011-05-04 09:27:52 -04001923static int global_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001924
1925static void ftrace_startup_enable(int command)
1926{
1927 if (saved_ftrace_func != ftrace_trace_function) {
1928 saved_ftrace_func = ftrace_trace_function;
1929 command |= FTRACE_UPDATE_TRACE_FUNC;
1930 }
1931
1932 if (!command || !ftrace_enabled)
1933 return;
1934
1935 ftrace_run_update_code(command);
1936}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001937
Steven Rostedta1cd6172011-05-23 15:24:25 -04001938static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001939{
Steven Rostedtb8489142011-05-04 09:27:52 -04001940 bool hash_enable = true;
1941
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001942 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04001943 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001944
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001945 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001946 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02001947
Steven Rostedtb8489142011-05-04 09:27:52 -04001948 /* ops marked global share the filter hashes */
1949 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
1950 ops = &global_ops;
1951 /* Don't update hash if global is already set */
1952 if (global_start_up)
1953 hash_enable = false;
1954 global_start_up++;
1955 }
1956
Steven Rostedted926f92011-05-03 13:25:24 -04001957 ops->flags |= FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04001958 if (hash_enable)
Steven Rostedted926f92011-05-03 13:25:24 -04001959 ftrace_hash_rec_enable(ops, 1);
1960
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001961 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04001962
1963 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02001964}
1965
Steven Rostedtbd69c302011-05-03 21:55:54 -04001966static void ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001967{
Steven Rostedtb8489142011-05-04 09:27:52 -04001968 bool hash_disable = true;
1969
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001970 if (unlikely(ftrace_disabled))
1971 return;
1972
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001973 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02001974 /*
1975 * Just warn in case of unbalance, no need to kill ftrace, it's not
1976 * critical but the ftrace_call callers may be never nopped again after
1977 * further ftrace uses.
1978 */
1979 WARN_ON_ONCE(ftrace_start_up < 0);
1980
Steven Rostedtb8489142011-05-04 09:27:52 -04001981 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
1982 ops = &global_ops;
1983 global_start_up--;
1984 WARN_ON_ONCE(global_start_up < 0);
1985 /* Don't update hash if global still has users */
1986 if (global_start_up) {
1987 WARN_ON_ONCE(!ftrace_start_up);
1988 hash_disable = false;
1989 }
1990 }
1991
1992 if (hash_disable)
Steven Rostedted926f92011-05-03 13:25:24 -04001993 ftrace_hash_rec_disable(ops, 1);
1994
Steven Rostedtb8489142011-05-04 09:27:52 -04001995 if (ops != &global_ops || !global_start_up)
Steven Rostedted926f92011-05-03 13:25:24 -04001996 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04001997
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001998 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001999
2000 if (saved_ftrace_func != ftrace_trace_function) {
2001 saved_ftrace_func = ftrace_trace_function;
2002 command |= FTRACE_UPDATE_TRACE_FUNC;
2003 }
2004
2005 if (!command || !ftrace_enabled)
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002006 return;
Steven Rostedt3d083392008-05-12 21:20:42 +02002007
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002008 ftrace_run_update_code(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02002009}
2010
Ingo Molnare309b412008-05-12 21:20:51 +02002011static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002012{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002013 if (unlikely(ftrace_disabled))
2014 return;
2015
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002016 /* Force update next time */
2017 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002018 /* ftrace_start_up is true if we want ftrace running */
2019 if (ftrace_start_up)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002020 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002021}
2022
Ingo Molnare309b412008-05-12 21:20:51 +02002023static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002024{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002025 if (unlikely(ftrace_disabled))
2026 return;
2027
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002028 /* ftrace_start_up is true if ftrace is running */
2029 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04002030 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002031}
2032
Steven Rostedt3d083392008-05-12 21:20:42 +02002033static cycle_t ftrace_update_time;
2034static unsigned long ftrace_update_cnt;
2035unsigned long ftrace_update_tot_cnt;
2036
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002037static int ops_traces_mod(struct ftrace_ops *ops)
2038{
2039 struct ftrace_hash *hash;
2040
2041 hash = ops->filter_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05002042 return ftrace_hash_empty(hash);
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002043}
2044
Steven Rostedt31e88902008-11-14 16:21:19 -08002045static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02002046{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002047 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002048 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302049 cycle_t start, stop;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002050 unsigned long ref = 0;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002051 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002052
2053 /*
2054 * When adding a module, we need to check if tracers are
2055 * currently enabled and if they are set to trace all functions.
2056 * If they are, we need to enable the module functions as well
2057 * as update the reference counts for those function records.
2058 */
2059 if (mod) {
2060 struct ftrace_ops *ops;
2061
2062 for (ops = ftrace_ops_list;
2063 ops != &ftrace_list_end; ops = ops->next) {
2064 if (ops->flags & FTRACE_OPS_FL_ENABLED &&
2065 ops_traces_mod(ops))
2066 ref++;
2067 }
2068 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002069
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002070 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002071 ftrace_update_cnt = 0;
2072
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002073 for (pg = ftrace_new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302074
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002075 for (i = 0; i < pg->index; i++) {
2076 /* If something went wrong, bail without enabling anything */
2077 if (unlikely(ftrace_disabled))
2078 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002079
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002080 p = &pg->records[i];
2081 p->flags = ref;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302082
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002083 /*
2084 * Do the initial record conversion from mcount jump
2085 * to the NOP instructions.
2086 */
2087 if (!ftrace_code_disable(mod, p))
2088 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002089
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002090 ftrace_update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002091
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002092 /*
2093 * If the tracing is enabled, go ahead and enable the record.
2094 *
2095 * The reason not to enable the record immediatelly is the
2096 * inherent check of ftrace_make_nop/ftrace_make_call for
2097 * correct previous instructions. Making first the NOP
2098 * conversion puts the module to the correct state, thus
2099 * passing the ftrace_make_call check.
2100 */
2101 if (ftrace_start_up && ref) {
2102 int failed = __ftrace_replace_code(p, 1);
2103 if (failed)
2104 ftrace_bug(failed, p->ip);
2105 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002106 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002107 }
2108
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002109 ftrace_new_pgs = NULL;
2110
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002111 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002112 ftrace_update_time = stop - start;
2113 ftrace_update_tot_cnt += ftrace_update_cnt;
2114
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002115 return 0;
2116}
2117
Steven Rostedta7900872011-12-16 16:23:44 -05002118static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002119{
Steven Rostedta7900872011-12-16 16:23:44 -05002120 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002121 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002122
Steven Rostedta7900872011-12-16 16:23:44 -05002123 if (WARN_ON(!count))
2124 return -EINVAL;
2125
2126 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002127
2128 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002129 * We want to fill as much as possible. No more than a page
2130 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002131 */
Steven Rostedta7900872011-12-16 16:23:44 -05002132 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2133 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002134
Steven Rostedta7900872011-12-16 16:23:44 -05002135 again:
2136 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2137
2138 if (!pg->records) {
2139 /* if we can't allocate this size, try something smaller */
2140 if (!order)
2141 return -ENOMEM;
2142 order >>= 1;
2143 goto again;
2144 }
2145
2146 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2147 pg->size = cnt;
2148
2149 if (cnt > count)
2150 cnt = count;
2151
2152 return cnt;
2153}
2154
2155static struct ftrace_page *
2156ftrace_allocate_pages(unsigned long num_to_init)
2157{
2158 struct ftrace_page *start_pg;
2159 struct ftrace_page *pg;
2160 int order;
2161 int cnt;
2162
2163 if (!num_to_init)
2164 return 0;
2165
2166 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2167 if (!pg)
2168 return NULL;
2169
2170 /*
2171 * Try to allocate as much as possible in one continues
2172 * location that fills in all of the space. We want to
2173 * waste as little space as possible.
2174 */
2175 for (;;) {
2176 cnt = ftrace_allocate_records(pg, num_to_init);
2177 if (cnt < 0)
2178 goto free_pages;
2179
2180 num_to_init -= cnt;
2181 if (!num_to_init)
2182 break;
2183
2184 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2185 if (!pg->next)
2186 goto free_pages;
2187
2188 pg = pg->next;
2189 }
2190
2191 return start_pg;
2192
2193 free_pages:
2194 while (start_pg) {
2195 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2196 free_pages((unsigned long)pg->records, order);
2197 start_pg = pg->next;
2198 kfree(pg);
2199 pg = start_pg;
2200 }
2201 pr_info("ftrace: FAILED to allocate memory for functions\n");
2202 return NULL;
2203}
2204
2205static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2206{
2207 int cnt;
2208
2209 if (!num_to_init) {
2210 pr_info("ftrace: No functions to be traced?\n");
2211 return -1;
2212 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002213
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002214 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04002215 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08002216 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002217
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002218 return 0;
2219}
2220
Steven Rostedt5072c592008-05-12 21:20:43 +02002221#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2222
2223struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002224 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002225 loff_t func_pos;
2226 struct ftrace_page *pg;
2227 struct dyn_ftrace *func;
2228 struct ftrace_func_probe *probe;
2229 struct trace_parser parser;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002230 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002231 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002232 int hidx;
2233 int idx;
2234 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002235};
2236
Ingo Molnare309b412008-05-12 21:20:51 +02002237static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002238t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002239{
2240 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002241 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002242 struct hlist_head *hhd;
2243
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002244 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002245 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002246
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002247 if (iter->probe)
2248 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002249 retry:
2250 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2251 return NULL;
2252
2253 hhd = &ftrace_func_hash[iter->hidx];
2254
2255 if (hlist_empty(hhd)) {
2256 iter->hidx++;
2257 hnd = NULL;
2258 goto retry;
2259 }
2260
2261 if (!hnd)
2262 hnd = hhd->first;
2263 else {
2264 hnd = hnd->next;
2265 if (!hnd) {
2266 iter->hidx++;
2267 goto retry;
2268 }
2269 }
2270
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002271 if (WARN_ON_ONCE(!hnd))
2272 return NULL;
2273
2274 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2275
2276 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002277}
2278
2279static void *t_hash_start(struct seq_file *m, loff_t *pos)
2280{
2281 struct ftrace_iterator *iter = m->private;
2282 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08002283 loff_t l;
2284
Steven Rostedt69a30832011-12-19 15:21:16 -05002285 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2286 return NULL;
2287
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002288 if (iter->func_pos > *pos)
2289 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002290
Li Zefand82d6242009-06-24 09:54:54 +08002291 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002292 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002293 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08002294 if (!p)
2295 break;
2296 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002297 if (!p)
2298 return NULL;
2299
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002300 /* Only set this if we have an item */
2301 iter->flags |= FTRACE_ITER_HASH;
2302
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002303 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002304}
2305
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002306static int
2307t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002308{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002309 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002310
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002311 rec = iter->probe;
2312 if (WARN_ON_ONCE(!rec))
2313 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002314
Steven Rostedt809dcf22009-02-16 23:06:01 -05002315 if (rec->ops->print)
2316 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2317
Steven Rostedtb375a112009-09-17 00:05:58 -04002318 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002319
2320 if (rec->data)
2321 seq_printf(m, ":%p", rec->data);
2322 seq_putc(m, '\n');
2323
2324 return 0;
2325}
2326
2327static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02002328t_next(struct seq_file *m, void *v, loff_t *pos)
2329{
2330 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002331 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002332 struct dyn_ftrace *rec = NULL;
2333
Steven Rostedt45a4a232011-04-21 23:16:46 -04002334 if (unlikely(ftrace_disabled))
2335 return NULL;
2336
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002337 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002338 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002339
Steven Rostedt5072c592008-05-12 21:20:43 +02002340 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01002341 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02002342
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002343 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002344 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002345
Steven Rostedt5072c592008-05-12 21:20:43 +02002346 retry:
2347 if (iter->idx >= iter->pg->index) {
2348 if (iter->pg->next) {
2349 iter->pg = iter->pg->next;
2350 iter->idx = 0;
2351 goto retry;
2352 }
2353 } else {
2354 rec = &iter->pg->records[iter->idx++];
Steven Rostedt32082302011-12-16 14:42:37 -05002355 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedtf45948e2011-05-02 12:29:25 -04002356 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
Steven Rostedt0183fb12008-11-07 22:36:02 -05002357
Steven Rostedt41c52c02008-05-22 11:46:33 -04002358 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt647bcd02011-05-03 14:39:21 -04002359 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2360
2361 ((iter->flags & FTRACE_ITER_ENABLED) &&
2362 !(rec->flags & ~FTRACE_FL_MASK))) {
2363
Steven Rostedt5072c592008-05-12 21:20:43 +02002364 rec = NULL;
2365 goto retry;
2366 }
2367 }
2368
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002369 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002370 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002371
2372 iter->func = rec;
2373
2374 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002375}
2376
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002377static void reset_iter_read(struct ftrace_iterator *iter)
2378{
2379 iter->pos = 0;
2380 iter->func_pos = 0;
2381 iter->flags &= ~(FTRACE_ITER_PRINTALL & FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002382}
2383
2384static void *t_start(struct seq_file *m, loff_t *pos)
2385{
2386 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002387 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002388 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002389 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002390
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002391 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002392
2393 if (unlikely(ftrace_disabled))
2394 return NULL;
2395
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002396 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002397 * If an lseek was done, then reset and start from beginning.
2398 */
2399 if (*pos < iter->pos)
2400 reset_iter_read(iter);
2401
2402 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002403 * For set_ftrace_filter reading, if we have the filter
2404 * off, we can short cut and just print out that all
2405 * functions are enabled.
2406 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05002407 if (iter->flags & FTRACE_ITER_FILTER &&
2408 ftrace_hash_empty(ops->filter_hash)) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002409 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002410 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002411 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002412 /* reset in case of seek/pread */
2413 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002414 return iter;
2415 }
2416
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002417 if (iter->flags & FTRACE_ITER_HASH)
2418 return t_hash_start(m, pos);
2419
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002420 /*
2421 * Unfortunately, we need to restart at ftrace_pages_start
2422 * every time we let go of the ftrace_mutex. This is because
2423 * those pointers can change without the lock.
2424 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002425 iter->pg = ftrace_pages_start;
2426 iter->idx = 0;
2427 for (l = 0; l <= *pos; ) {
2428 p = t_next(m, p, &l);
2429 if (!p)
2430 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002431 }
walimis5821e1b2008-11-15 15:19:06 +08002432
Steven Rostedt69a30832011-12-19 15:21:16 -05002433 if (!p)
2434 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002435
2436 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002437}
2438
2439static void t_stop(struct seq_file *m, void *p)
2440{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002441 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002442}
2443
2444static int t_show(struct seq_file *m, void *v)
2445{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002446 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002447 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02002448
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002449 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002450 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002451
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002452 if (iter->flags & FTRACE_ITER_PRINTALL) {
2453 seq_printf(m, "#### all functions enabled ####\n");
2454 return 0;
2455 }
2456
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002457 rec = iter->func;
2458
Steven Rostedt5072c592008-05-12 21:20:43 +02002459 if (!rec)
2460 return 0;
2461
Steven Rostedt647bcd02011-05-03 14:39:21 -04002462 seq_printf(m, "%ps", (void *)rec->ip);
2463 if (iter->flags & FTRACE_ITER_ENABLED)
2464 seq_printf(m, " (%ld)",
2465 rec->flags & ~FTRACE_FL_MASK);
2466 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02002467
2468 return 0;
2469}
2470
James Morris88e9d342009-09-22 16:43:43 -07002471static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002472 .start = t_start,
2473 .next = t_next,
2474 .stop = t_stop,
2475 .show = t_show,
2476};
2477
Ingo Molnare309b412008-05-12 21:20:51 +02002478static int
Steven Rostedt5072c592008-05-12 21:20:43 +02002479ftrace_avail_open(struct inode *inode, struct file *file)
2480{
2481 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002482
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002483 if (unlikely(ftrace_disabled))
2484 return -ENODEV;
2485
Jiri Olsa50e18b92012-04-25 10:23:39 +02002486 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2487 if (iter) {
2488 iter->pg = ftrace_pages_start;
2489 iter->ops = &global_ops;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002490 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002491
Jiri Olsa50e18b92012-04-25 10:23:39 +02002492 return iter ? 0 : -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02002493}
2494
Steven Rostedt647bcd02011-05-03 14:39:21 -04002495static int
2496ftrace_enabled_open(struct inode *inode, struct file *file)
2497{
2498 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002499
2500 if (unlikely(ftrace_disabled))
2501 return -ENODEV;
2502
Jiri Olsa50e18b92012-04-25 10:23:39 +02002503 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2504 if (iter) {
2505 iter->pg = ftrace_pages_start;
2506 iter->flags = FTRACE_ITER_ENABLED;
2507 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002508 }
2509
Jiri Olsa50e18b92012-04-25 10:23:39 +02002510 return iter ? 0 : -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002511}
2512
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002513static void ftrace_filter_reset(struct ftrace_hash *hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02002514{
Steven Rostedt52baf112009-02-14 01:15:39 -05002515 mutex_lock(&ftrace_lock);
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002516 ftrace_hash_clear(hash);
Steven Rostedt52baf112009-02-14 01:15:39 -05002517 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002518}
2519
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002520/**
2521 * ftrace_regex_open - initialize function tracer filter files
2522 * @ops: The ftrace_ops that hold the hash filters
2523 * @flag: The type of filter to process
2524 * @inode: The inode, usually passed in to your open routine
2525 * @file: The file, usually passed in to your open routine
2526 *
2527 * ftrace_regex_open() initializes the filter files for the
2528 * @ops. Depending on @flag it may process the filter hash or
2529 * the notrace hash of @ops. With this called from the open
2530 * routine, you can use ftrace_filter_write() for the write
2531 * routine if @flag has FTRACE_ITER_FILTER set, or
2532 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
2533 * ftrace_regex_lseek() should be used as the lseek routine, and
2534 * release must call ftrace_regex_release().
2535 */
2536int
Steven Rostedtf45948e2011-05-02 12:29:25 -04002537ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002538 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02002539{
2540 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002541 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02002542 int ret = 0;
2543
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002544 if (unlikely(ftrace_disabled))
2545 return -ENODEV;
2546
Steven Rostedt5072c592008-05-12 21:20:43 +02002547 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2548 if (!iter)
2549 return -ENOMEM;
2550
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002551 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2552 kfree(iter);
2553 return -ENOMEM;
2554 }
2555
Steven Rostedtf45948e2011-05-02 12:29:25 -04002556 if (flag & FTRACE_ITER_NOTRACE)
2557 hash = ops->notrace_hash;
2558 else
2559 hash = ops->filter_hash;
2560
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002561 iter->ops = ops;
2562 iter->flags = flag;
2563
2564 if (file->f_mode & FMODE_WRITE) {
2565 mutex_lock(&ftrace_lock);
2566 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2567 mutex_unlock(&ftrace_lock);
2568
2569 if (!iter->hash) {
2570 trace_parser_put(&iter->parser);
2571 kfree(iter);
2572 return -ENOMEM;
2573 }
2574 }
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002575
Steven Rostedt41c52c02008-05-22 11:46:33 -04002576 mutex_lock(&ftrace_regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002577
Steven Rostedt5072c592008-05-12 21:20:43 +02002578 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002579 (file->f_flags & O_TRUNC))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002580 ftrace_filter_reset(iter->hash);
Steven Rostedt5072c592008-05-12 21:20:43 +02002581
2582 if (file->f_mode & FMODE_READ) {
2583 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02002584
2585 ret = seq_open(file, &show_ftrace_seq_ops);
2586 if (!ret) {
2587 struct seq_file *m = file->private_data;
2588 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08002589 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002590 /* Failed */
2591 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08002592 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002593 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08002594 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002595 } else
2596 file->private_data = iter;
Steven Rostedt41c52c02008-05-22 11:46:33 -04002597 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002598
2599 return ret;
2600}
2601
Steven Rostedt41c52c02008-05-22 11:46:33 -04002602static int
2603ftrace_filter_open(struct inode *inode, struct file *file)
2604{
Steven Rostedt69a30832011-12-19 15:21:16 -05002605 return ftrace_regex_open(&global_ops,
2606 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2607 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002608}
2609
2610static int
2611ftrace_notrace_open(struct inode *inode, struct file *file)
2612{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002613 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002614 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002615}
2616
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002617loff_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04002618ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
Steven Rostedt5072c592008-05-12 21:20:43 +02002619{
2620 loff_t ret;
2621
2622 if (file->f_mode & FMODE_READ)
2623 ret = seq_lseek(file, offset, origin);
2624 else
2625 file->f_pos = ret = 1;
2626
2627 return ret;
2628}
2629
Steven Rostedt64e7c442009-02-13 17:08:48 -05002630static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002631{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002632 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08002633 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002634
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002635 switch (type) {
2636 case MATCH_FULL:
2637 if (strcmp(str, regex) == 0)
2638 matched = 1;
2639 break;
2640 case MATCH_FRONT_ONLY:
2641 if (strncmp(str, regex, len) == 0)
2642 matched = 1;
2643 break;
2644 case MATCH_MIDDLE_ONLY:
2645 if (strstr(str, regex))
2646 matched = 1;
2647 break;
2648 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08002649 slen = strlen(str);
2650 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002651 matched = 1;
2652 break;
2653 }
2654
2655 return matched;
2656}
2657
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002658static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002659enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04002660{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002661 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002662 int ret = 0;
2663
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002664 entry = ftrace_lookup_ip(hash, rec->ip);
2665 if (not) {
2666 /* Do nothing if it doesn't exist */
2667 if (!entry)
2668 return 0;
2669
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002670 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002671 } else {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002672 /* Do nothing if it exists */
2673 if (entry)
2674 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002675
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002676 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002677 }
2678 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04002679}
2680
Steven Rostedt64e7c442009-02-13 17:08:48 -05002681static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002682ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2683 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002684{
2685 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002686 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002687
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002688 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2689
2690 if (mod) {
2691 /* module lookup requires matching the module */
2692 if (!modname || strcmp(modname, mod))
2693 return 0;
2694
2695 /* blank search means to match all funcs in the mod */
2696 if (!len)
2697 return 1;
2698 }
2699
Steven Rostedt64e7c442009-02-13 17:08:48 -05002700 return ftrace_match(str, regex, len, type);
2701}
2702
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002703static int
2704match_records(struct ftrace_hash *hash, char *buff,
2705 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002706{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002707 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002708 struct ftrace_page *pg;
2709 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002710 int type = MATCH_FULL;
2711 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08002712 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002713 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002714
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002715 if (len) {
2716 type = filter_parse_regex(buff, len, &search, &not);
2717 search_len = strlen(search);
2718 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002719
Steven Rostedt52baf112009-02-14 01:15:39 -05002720 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002721
2722 if (unlikely(ftrace_disabled))
2723 goto out_unlock;
2724
Steven Rostedt265c8312009-02-13 12:43:56 -05002725 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002726 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002727 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002728 if (ret < 0) {
2729 found = ret;
2730 goto out_unlock;
2731 }
Li Zefan311d16d2009-12-08 11:15:11 +08002732 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05002733 }
2734 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002735 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05002736 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08002737
2738 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02002739}
2740
Steven Rostedt64e7c442009-02-13 17:08:48 -05002741static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002742ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002743{
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002744 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002745}
2746
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002747static int
2748ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002749{
Steven Rostedt64e7c442009-02-13 17:08:48 -05002750 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002751
Steven Rostedt64e7c442009-02-13 17:08:48 -05002752 /* blank or '*' mean the same */
2753 if (strcmp(buff, "*") == 0)
2754 buff[0] = 0;
2755
2756 /* handle the case of 'dont filter this module' */
2757 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2758 buff[0] = 0;
2759 not = 1;
2760 }
2761
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002762 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002763}
2764
Steven Rostedtf6180772009-02-14 00:40:25 -05002765/*
2766 * We register the module command as a template to show others how
2767 * to register the a command as well.
2768 */
2769
2770static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04002771ftrace_mod_callback(struct ftrace_hash *hash,
2772 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05002773{
2774 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002775 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05002776
2777 /*
2778 * cmd == 'mod' because we only registered this func
2779 * for the 'mod' ftrace_func_command.
2780 * But if you register one func with multiple commands,
2781 * you can tell which command was used by the cmd
2782 * parameter.
2783 */
2784
2785 /* we must have a module name */
2786 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002787 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002788
2789 mod = strsep(&param, ":");
2790 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002791 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002792
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002793 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002794 if (!ret)
2795 ret = -EINVAL;
2796 if (ret < 0)
2797 return ret;
2798
2799 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05002800}
2801
2802static struct ftrace_func_command ftrace_mod_cmd = {
2803 .name = "mod",
2804 .func = ftrace_mod_callback,
2805};
2806
2807static int __init ftrace_mod_cmd_init(void)
2808{
2809 return register_ftrace_command(&ftrace_mod_cmd);
2810}
2811device_initcall(ftrace_mod_cmd_init);
2812
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04002813static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
2814 struct ftrace_ops *op)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002815{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002816 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002817 struct hlist_head *hhd;
2818 struct hlist_node *n;
2819 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002820
2821 key = hash_long(ip, FTRACE_HASH_BITS);
2822
2823 hhd = &ftrace_func_hash[key];
2824
2825 if (hlist_empty(hhd))
2826 return;
2827
2828 /*
2829 * Disable preemption for these calls to prevent a RCU grace
2830 * period. This syncs the hash iteration and freeing of items
2831 * on the hash. rcu_read_lock is too dangerous here.
2832 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04002833 preempt_disable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002834 hlist_for_each_entry_rcu(entry, n, hhd, node) {
2835 if (entry->ip == ip)
2836 entry->ops->func(ip, parent_ip, &entry->data);
2837 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04002838 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002839}
2840
Steven Rostedtb6887d72009-02-17 12:32:04 -05002841static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05002842{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04002843 .func = function_trace_probe_call,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002844};
2845
Steven Rostedtb6887d72009-02-17 12:32:04 -05002846static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002847
Steven Rostedtb6887d72009-02-17 12:32:04 -05002848static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002849{
Steven Rostedtb8489142011-05-04 09:27:52 -04002850 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002851 int i;
2852
Steven Rostedtb6887d72009-02-17 12:32:04 -05002853 if (ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002854 return;
2855
2856 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2857 struct hlist_head *hhd = &ftrace_func_hash[i];
2858 if (hhd->first)
2859 break;
2860 }
2861 /* Nothing registered? */
2862 if (i == FTRACE_FUNC_HASHSIZE)
2863 return;
2864
Steven Rostedtb8489142011-05-04 09:27:52 -04002865 ret = __register_ftrace_function(&trace_probe_ops);
2866 if (!ret)
Steven Rostedta1cd6172011-05-23 15:24:25 -04002867 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04002868
Steven Rostedtb6887d72009-02-17 12:32:04 -05002869 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002870}
2871
Steven Rostedtb6887d72009-02-17 12:32:04 -05002872static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002873{
Steven Rostedtb8489142011-05-04 09:27:52 -04002874 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002875 int i;
2876
Steven Rostedtb6887d72009-02-17 12:32:04 -05002877 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002878 return;
2879
2880 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2881 struct hlist_head *hhd = &ftrace_func_hash[i];
2882 if (hhd->first)
2883 return;
2884 }
2885
2886 /* no more funcs left */
Steven Rostedtb8489142011-05-04 09:27:52 -04002887 ret = __unregister_ftrace_function(&trace_probe_ops);
2888 if (!ret)
2889 ftrace_shutdown(&trace_probe_ops, 0);
2890
Steven Rostedtb6887d72009-02-17 12:32:04 -05002891 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002892}
2893
2894
2895static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2896{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002897 struct ftrace_func_probe *entry =
2898 container_of(rhp, struct ftrace_func_probe, rcu);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002899
2900 if (entry->ops->free)
2901 entry->ops->free(&entry->data);
2902 kfree(entry);
2903}
2904
2905
2906int
Steven Rostedtb6887d72009-02-17 12:32:04 -05002907register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002908 void *data)
2909{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002910 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002911 struct ftrace_page *pg;
2912 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002913 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002914 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002915 int count = 0;
2916 char *search;
2917
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002918 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002919 len = strlen(search);
2920
Steven Rostedtb6887d72009-02-17 12:32:04 -05002921 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002922 if (WARN_ON(not))
2923 return -EINVAL;
2924
2925 mutex_lock(&ftrace_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002926
Steven Rostedt45a4a232011-04-21 23:16:46 -04002927 if (unlikely(ftrace_disabled))
2928 goto out_unlock;
2929
Steven Rostedt59df055f2009-02-14 15:29:06 -05002930 do_for_each_ftrace_rec(pg, rec) {
2931
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002932 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05002933 continue;
2934
2935 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2936 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002937 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002938 if (!count)
2939 count = -ENOMEM;
2940 goto out_unlock;
2941 }
2942
2943 count++;
2944
2945 entry->data = data;
2946
2947 /*
2948 * The caller might want to do something special
2949 * for each function we find. We call the callback
2950 * to give the caller an opportunity to do so.
2951 */
2952 if (ops->callback) {
2953 if (ops->callback(rec->ip, &entry->data) < 0) {
2954 /* caller does not like this func */
2955 kfree(entry);
2956 continue;
2957 }
2958 }
2959
2960 entry->ops = ops;
2961 entry->ip = rec->ip;
2962
2963 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2964 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2965
2966 } while_for_each_ftrace_rec();
Steven Rostedtb6887d72009-02-17 12:32:04 -05002967 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002968
2969 out_unlock:
2970 mutex_unlock(&ftrace_lock);
2971
2972 return count;
2973}
2974
2975enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002976 PROBE_TEST_FUNC = 1,
2977 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05002978};
2979
2980static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002981__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002982 void *data, int flags)
2983{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002984 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002985 struct hlist_node *n, *tmp;
2986 char str[KSYM_SYMBOL_LEN];
2987 int type = MATCH_FULL;
2988 int i, len = 0;
2989 char *search;
2990
Atsushi Tsujib36461d2009-09-15 19:06:30 +09002991 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05002992 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09002993 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05002994 int not;
2995
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002996 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002997 len = strlen(search);
2998
Steven Rostedtb6887d72009-02-17 12:32:04 -05002999 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003000 if (WARN_ON(not))
3001 return;
3002 }
3003
3004 mutex_lock(&ftrace_lock);
3005 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3006 struct hlist_head *hhd = &ftrace_func_hash[i];
3007
3008 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
3009
3010 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05003011 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003012 continue;
3013
Steven Rostedtb6887d72009-02-17 12:32:04 -05003014 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003015 continue;
3016
3017 /* do this last, since it is the most expensive */
3018 if (glob) {
3019 kallsyms_lookup(entry->ip, NULL, NULL,
3020 NULL, str);
3021 if (!ftrace_match(str, glob, len, type))
3022 continue;
3023 }
3024
3025 hlist_del(&entry->node);
3026 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
3027 }
3028 }
Steven Rostedtb6887d72009-02-17 12:32:04 -05003029 __disable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003030 mutex_unlock(&ftrace_lock);
3031}
3032
3033void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003034unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003035 void *data)
3036{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003037 __unregister_ftrace_function_probe(glob, ops, data,
3038 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003039}
3040
3041void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003042unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003043{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003044 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003045}
3046
Steven Rostedtb6887d72009-02-17 12:32:04 -05003047void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003048{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003049 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003050}
3051
Steven Rostedtf6180772009-02-14 00:40:25 -05003052static LIST_HEAD(ftrace_commands);
3053static DEFINE_MUTEX(ftrace_cmd_mutex);
3054
3055int register_ftrace_command(struct ftrace_func_command *cmd)
3056{
3057 struct ftrace_func_command *p;
3058 int ret = 0;
3059
3060 mutex_lock(&ftrace_cmd_mutex);
3061 list_for_each_entry(p, &ftrace_commands, list) {
3062 if (strcmp(cmd->name, p->name) == 0) {
3063 ret = -EBUSY;
3064 goto out_unlock;
3065 }
3066 }
3067 list_add(&cmd->list, &ftrace_commands);
3068 out_unlock:
3069 mutex_unlock(&ftrace_cmd_mutex);
3070
3071 return ret;
3072}
3073
3074int unregister_ftrace_command(struct ftrace_func_command *cmd)
3075{
3076 struct ftrace_func_command *p, *n;
3077 int ret = -ENODEV;
3078
3079 mutex_lock(&ftrace_cmd_mutex);
3080 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3081 if (strcmp(cmd->name, p->name) == 0) {
3082 ret = 0;
3083 list_del_init(&p->list);
3084 goto out_unlock;
3085 }
3086 }
3087 out_unlock:
3088 mutex_unlock(&ftrace_cmd_mutex);
3089
3090 return ret;
3091}
3092
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003093static int ftrace_process_regex(struct ftrace_hash *hash,
3094 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003095{
Steven Rostedtf6180772009-02-14 00:40:25 -05003096 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003097 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08003098 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003099
3100 func = strsep(&next, ":");
3101
3102 if (!next) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003103 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003104 if (!ret)
3105 ret = -EINVAL;
3106 if (ret < 0)
3107 return ret;
3108 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003109 }
3110
Steven Rostedtf6180772009-02-14 00:40:25 -05003111 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05003112
3113 command = strsep(&next, ":");
3114
Steven Rostedtf6180772009-02-14 00:40:25 -05003115 mutex_lock(&ftrace_cmd_mutex);
3116 list_for_each_entry(p, &ftrace_commands, list) {
3117 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003118 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05003119 goto out_unlock;
3120 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05003121 }
Steven Rostedtf6180772009-02-14 00:40:25 -05003122 out_unlock:
3123 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003124
Steven Rostedtf6180772009-02-14 00:40:25 -05003125 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003126}
3127
Ingo Molnare309b412008-05-12 21:20:51 +02003128static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003129ftrace_regex_write(struct file *file, const char __user *ubuf,
3130 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02003131{
3132 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003133 struct trace_parser *parser;
3134 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02003135
Li Zefan4ba79782009-09-22 13:52:20 +08003136 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02003137 return 0;
3138
Steven Rostedt41c52c02008-05-22 11:46:33 -04003139 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003140
Steven Rostedt45a4a232011-04-21 23:16:46 -04003141 ret = -ENODEV;
3142 if (unlikely(ftrace_disabled))
3143 goto out_unlock;
3144
Steven Rostedt5072c592008-05-12 21:20:43 +02003145 if (file->f_mode & FMODE_READ) {
3146 struct seq_file *m = file->private_data;
3147 iter = m->private;
3148 } else
3149 iter = file->private_data;
3150
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003151 parser = &iter->parser;
3152 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02003153
Li Zefan4ba79782009-09-22 13:52:20 +08003154 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003155 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003156 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003157 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08003158 trace_parser_clear(parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02003159 if (ret)
Li Zefaned146b252009-11-03 08:55:38 +08003160 goto out_unlock;
Steven Rostedt5072c592008-05-12 21:20:43 +02003161 }
3162
Steven Rostedt5072c592008-05-12 21:20:43 +02003163 ret = read;
Li Zefaned146b252009-11-03 08:55:38 +08003164out_unlock:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003165 mutex_unlock(&ftrace_regex_lock);
Li Zefaned146b252009-11-03 08:55:38 +08003166
Steven Rostedt5072c592008-05-12 21:20:43 +02003167 return ret;
3168}
3169
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003170ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003171ftrace_filter_write(struct file *file, const char __user *ubuf,
3172 size_t cnt, loff_t *ppos)
3173{
3174 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3175}
3176
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003177ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003178ftrace_notrace_write(struct file *file, const char __user *ubuf,
3179 size_t cnt, loff_t *ppos)
3180{
3181 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3182}
3183
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003184static int
Steven Rostedtf45948e2011-05-02 12:29:25 -04003185ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3186 int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003187{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003188 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003189 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003190 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003191
Steven Rostedt936e0742011-05-05 22:54:01 -04003192 /* All global ops uses the global ops filters */
3193 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3194 ops = &global_ops;
3195
Steven Rostedt41c52c02008-05-22 11:46:33 -04003196 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003197 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003198
Steven Rostedtf45948e2011-05-02 12:29:25 -04003199 if (enable)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003200 orig_hash = &ops->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003201 else
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003202 orig_hash = &ops->notrace_hash;
3203
3204 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3205 if (!hash)
3206 return -ENOMEM;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003207
Steven Rostedt41c52c02008-05-22 11:46:33 -04003208 mutex_lock(&ftrace_regex_lock);
3209 if (reset)
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003210 ftrace_filter_reset(hash);
Jiri Olsaac483c42012-01-02 10:04:14 +01003211 if (buf && !ftrace_match_records(hash, buf, len)) {
3212 ret = -EINVAL;
3213 goto out_regex_unlock;
3214 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003215
3216 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003217 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt072126f2011-07-13 15:08:31 -04003218 if (!ret && ops->flags & FTRACE_OPS_FL_ENABLED
3219 && ftrace_enabled)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01003220 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt072126f2011-07-13 15:08:31 -04003221
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003222 mutex_unlock(&ftrace_lock);
3223
Jiri Olsaac483c42012-01-02 10:04:14 +01003224 out_regex_unlock:
Steven Rostedt41c52c02008-05-22 11:46:33 -04003225 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003226
3227 free_ftrace_hash(hash);
3228 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003229}
3230
Steven Rostedt77a2b372008-05-12 21:20:45 +02003231/**
3232 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003233 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02003234 * @buf - the string that holds the function filter text.
3235 * @len - the length of the string.
3236 * @reset - non zero to reset all filters before applying this filter.
3237 *
3238 * Filters denote which functions should be enabled when tracing is enabled.
3239 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3240 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003241int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003242 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02003243{
Jiri Olsaac483c42012-01-02 10:04:14 +01003244 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003245}
Steven Rostedt936e0742011-05-05 22:54:01 -04003246EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003247
Steven Rostedt41c52c02008-05-22 11:46:33 -04003248/**
3249 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003250 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04003251 * @buf - the string that holds the function notrace text.
3252 * @len - the length of the string.
3253 * @reset - non zero to reset all filters before applying this filter.
3254 *
3255 * Notrace Filters denote which functions should not be enabled when tracing
3256 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3257 * for tracing.
3258 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003259int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003260 int len, int reset)
3261{
Jiri Olsaac483c42012-01-02 10:04:14 +01003262 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04003263}
3264EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3265/**
3266 * ftrace_set_filter - set a function to filter on in ftrace
3267 * @ops - the ops to set the filter with
3268 * @buf - the string that holds the function filter text.
3269 * @len - the length of the string.
3270 * @reset - non zero to reset all filters before applying this filter.
3271 *
3272 * Filters denote which functions should be enabled when tracing is enabled.
3273 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3274 */
3275void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3276{
3277 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3278}
3279EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3280
3281/**
3282 * ftrace_set_notrace - set a function to not trace in ftrace
3283 * @ops - the ops to set the notrace filter with
3284 * @buf - the string that holds the function notrace text.
3285 * @len - the length of the string.
3286 * @reset - non zero to reset all filters before applying this filter.
3287 *
3288 * Notrace Filters denote which functions should not be enabled when tracing
3289 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3290 * for tracing.
3291 */
3292void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003293{
Steven Rostedtf45948e2011-05-02 12:29:25 -04003294 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003295}
Steven Rostedt936e0742011-05-05 22:54:01 -04003296EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003297
Steven Rostedt2af15d62009-05-28 13:37:24 -04003298/*
3299 * command line interface to allow users to set filters on boot up.
3300 */
3301#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3302static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3303static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3304
3305static int __init set_ftrace_notrace(char *str)
3306{
3307 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
3308 return 1;
3309}
3310__setup("ftrace_notrace=", set_ftrace_notrace);
3311
3312static int __init set_ftrace_filter(char *str)
3313{
3314 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
3315 return 1;
3316}
3317__setup("ftrace_filter=", set_ftrace_filter);
3318
Stefan Assmann369bc182009-10-12 22:17:21 +02003319#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08003320static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Steven Rostedt801c29f2010-03-05 20:02:19 -05003321static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
3322
Stefan Assmann369bc182009-10-12 22:17:21 +02003323static int __init set_graph_function(char *str)
3324{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02003325 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02003326 return 1;
3327}
3328__setup("ftrace_graph_filter=", set_graph_function);
3329
3330static void __init set_ftrace_early_graph(char *buf)
3331{
3332 int ret;
3333 char *func;
3334
3335 while (buf) {
3336 func = strsep(&buf, ",");
3337 /* we allow only one expression at a time */
3338 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3339 func);
3340 if (ret)
3341 printk(KERN_DEBUG "ftrace: function %s not "
3342 "traceable\n", func);
3343 }
3344}
3345#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3346
Steven Rostedt2a85a372011-12-19 21:57:44 -05003347void __init
3348ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04003349{
3350 char *func;
3351
3352 while (buf) {
3353 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04003354 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003355 }
3356}
3357
3358static void __init set_ftrace_early_filters(void)
3359{
3360 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003361 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003362 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003363 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02003364#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3365 if (ftrace_graph_buf[0])
3366 set_ftrace_early_graph(ftrace_graph_buf);
3367#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04003368}
3369
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003370int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003371{
3372 struct seq_file *m = (struct seq_file *)file->private_data;
3373 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003374 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003375 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04003376 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003377 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02003378
Steven Rostedt41c52c02008-05-22 11:46:33 -04003379 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003380 if (file->f_mode & FMODE_READ) {
3381 iter = m->private;
3382
3383 seq_release(inode, file);
3384 } else
3385 iter = file->private_data;
3386
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003387 parser = &iter->parser;
3388 if (trace_parser_loaded(parser)) {
3389 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003390 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02003391 }
3392
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003393 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003394
Steven Rostedt058e2972011-04-29 22:35:33 -04003395 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04003396 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3397
3398 if (filter_hash)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003399 orig_hash = &iter->ops->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04003400 else
3401 orig_hash = &iter->ops->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003402
Steven Rostedt058e2972011-04-29 22:35:33 -04003403 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003404 ret = ftrace_hash_move(iter->ops, filter_hash,
3405 orig_hash, iter->hash);
3406 if (!ret && (iter->ops->flags & FTRACE_OPS_FL_ENABLED)
3407 && ftrace_enabled)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01003408 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003409
Steven Rostedt058e2972011-04-29 22:35:33 -04003410 mutex_unlock(&ftrace_lock);
3411 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003412 free_ftrace_hash(iter->hash);
3413 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04003414
Steven Rostedt41c52c02008-05-22 11:46:33 -04003415 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003416 return 0;
3417}
3418
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003419static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003420 .open = ftrace_avail_open,
3421 .read = seq_read,
3422 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08003423 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02003424};
3425
Steven Rostedt647bcd02011-05-03 14:39:21 -04003426static const struct file_operations ftrace_enabled_fops = {
3427 .open = ftrace_enabled_open,
3428 .read = seq_read,
3429 .llseek = seq_lseek,
3430 .release = seq_release_private,
3431};
3432
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003433static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003434 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003435 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02003436 .write = ftrace_filter_write,
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003437 .llseek = ftrace_regex_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003438 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02003439};
3440
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003441static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04003442 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003443 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003444 .write = ftrace_notrace_write,
3445 .llseek = ftrace_regex_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003446 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003447};
3448
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003449#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3450
3451static DEFINE_MUTEX(graph_lock);
3452
3453int ftrace_graph_count;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003454int ftrace_graph_filter_enabled;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003455unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3456
3457static void *
Li Zefan85951842009-06-24 09:54:00 +08003458__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003459{
Li Zefan85951842009-06-24 09:54:00 +08003460 if (*pos >= ftrace_graph_count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003461 return NULL;
Li Zefana4ec5e02009-09-18 14:06:28 +08003462 return &ftrace_graph_funcs[*pos];
Li Zefan85951842009-06-24 09:54:00 +08003463}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003464
Li Zefan85951842009-06-24 09:54:00 +08003465static void *
3466g_next(struct seq_file *m, void *v, loff_t *pos)
3467{
3468 (*pos)++;
3469 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003470}
3471
3472static void *g_start(struct seq_file *m, loff_t *pos)
3473{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003474 mutex_lock(&graph_lock);
3475
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003476 /* Nothing, tell g_show to print all functions are enabled */
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003477 if (!ftrace_graph_filter_enabled && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003478 return (void *)1;
3479
Li Zefan85951842009-06-24 09:54:00 +08003480 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003481}
3482
3483static void g_stop(struct seq_file *m, void *p)
3484{
3485 mutex_unlock(&graph_lock);
3486}
3487
3488static int g_show(struct seq_file *m, void *v)
3489{
3490 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003491
3492 if (!ptr)
3493 return 0;
3494
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003495 if (ptr == (unsigned long *)1) {
3496 seq_printf(m, "#### all functions enabled ####\n");
3497 return 0;
3498 }
3499
Steven Rostedtb375a112009-09-17 00:05:58 -04003500 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003501
3502 return 0;
3503}
3504
James Morris88e9d342009-09-22 16:43:43 -07003505static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003506 .start = g_start,
3507 .next = g_next,
3508 .stop = g_stop,
3509 .show = g_show,
3510};
3511
3512static int
3513ftrace_graph_open(struct inode *inode, struct file *file)
3514{
3515 int ret = 0;
3516
3517 if (unlikely(ftrace_disabled))
3518 return -ENODEV;
3519
3520 mutex_lock(&graph_lock);
3521 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04003522 (file->f_flags & O_TRUNC)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003523 ftrace_graph_filter_enabled = 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003524 ftrace_graph_count = 0;
3525 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3526 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003527 mutex_unlock(&graph_lock);
3528
Li Zefana4ec5e02009-09-18 14:06:28 +08003529 if (file->f_mode & FMODE_READ)
3530 ret = seq_open(file, &ftrace_graph_seq_ops);
3531
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003532 return ret;
3533}
3534
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003535static int
Li Zefan87827112009-07-23 11:29:11 +08003536ftrace_graph_release(struct inode *inode, struct file *file)
3537{
3538 if (file->f_mode & FMODE_READ)
3539 seq_release(inode, file);
3540 return 0;
3541}
3542
3543static int
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003544ftrace_set_func(unsigned long *array, int *idx, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003545{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003546 struct dyn_ftrace *rec;
3547 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003548 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003549 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003550 int type, not;
3551 char *search;
3552 bool exists;
3553 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003554
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003555 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003556 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003557 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3558 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003559
3560 search_len = strlen(search);
3561
Steven Rostedt52baf112009-02-14 01:15:39 -05003562 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003563
3564 if (unlikely(ftrace_disabled)) {
3565 mutex_unlock(&ftrace_lock);
3566 return -ENODEV;
3567 }
3568
Steven Rostedt265c8312009-02-13 12:43:56 -05003569 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003570
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003571 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003572 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003573 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003574 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003575 if (array[i] == rec->ip) {
3576 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05003577 break;
3578 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003579 }
3580
3581 if (!not) {
3582 fail = 0;
3583 if (!exists) {
3584 array[(*idx)++] = rec->ip;
3585 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3586 goto out;
3587 }
3588 } else {
3589 if (exists) {
3590 array[i] = array[--(*idx)];
3591 array[*idx] = 0;
3592 fail = 0;
3593 }
3594 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003595 }
Steven Rostedt265c8312009-02-13 12:43:56 -05003596 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003597out:
Steven Rostedt52baf112009-02-14 01:15:39 -05003598 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003599
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003600 if (fail)
3601 return -EINVAL;
3602
3603 ftrace_graph_filter_enabled = 1;
3604 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003605}
3606
3607static ssize_t
3608ftrace_graph_write(struct file *file, const char __user *ubuf,
3609 size_t cnt, loff_t *ppos)
3610{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003611 struct trace_parser parser;
Li Zefan4ba79782009-09-22 13:52:20 +08003612 ssize_t read, ret;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003613
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003614 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003615 return 0;
3616
3617 mutex_lock(&graph_lock);
3618
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003619 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3620 ret = -ENOMEM;
Li Zefan1eb90f12009-09-22 13:52:57 +08003621 goto out_unlock;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003622 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003623
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003624 read = trace_get_user(&parser, ubuf, cnt, ppos);
3625
Li Zefan4ba79782009-09-22 13:52:20 +08003626 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003627 parser.buffer[parser.idx] = 0;
3628
3629 /* we allow only one expression at a time */
Li Zefana4ec5e02009-09-18 14:06:28 +08003630 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003631 parser.buffer);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003632 if (ret)
Li Zefan1eb90f12009-09-22 13:52:57 +08003633 goto out_free;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003634 }
3635
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003636 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08003637
3638out_free:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003639 trace_parser_put(&parser);
Li Zefan1eb90f12009-09-22 13:52:57 +08003640out_unlock:
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003641 mutex_unlock(&graph_lock);
3642
3643 return ret;
3644}
3645
3646static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08003647 .open = ftrace_graph_open,
3648 .read = seq_read,
3649 .write = ftrace_graph_write,
3650 .release = ftrace_graph_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003651 .llseek = seq_lseek,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003652};
3653#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3654
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003655static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02003656{
Steven Rostedt5072c592008-05-12 21:20:43 +02003657
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003658 trace_create_file("available_filter_functions", 0444,
3659 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02003660
Steven Rostedt647bcd02011-05-03 14:39:21 -04003661 trace_create_file("enabled_functions", 0444,
3662 d_tracer, NULL, &ftrace_enabled_fops);
3663
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003664 trace_create_file("set_ftrace_filter", 0644, d_tracer,
3665 NULL, &ftrace_filter_fops);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003666
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003667 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003668 NULL, &ftrace_notrace_fops);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04003669
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003670#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003671 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003672 NULL,
3673 &ftrace_graph_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003674#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3675
Steven Rostedt5072c592008-05-12 21:20:43 +02003676 return 0;
3677}
3678
Steven Rostedt9fd49322012-04-24 22:32:06 -04003679static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05003680{
Steven Rostedt9fd49322012-04-24 22:32:06 -04003681 const unsigned long *ipa = a;
3682 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05003683
Steven Rostedt9fd49322012-04-24 22:32:06 -04003684 if (*ipa > *ipb)
3685 return 1;
3686 if (*ipa < *ipb)
3687 return -1;
3688 return 0;
3689}
3690
3691static void ftrace_swap_ips(void *a, void *b, int size)
3692{
3693 unsigned long *ipa = a;
3694 unsigned long *ipb = b;
3695 unsigned long t;
3696
3697 t = *ipa;
3698 *ipa = *ipb;
3699 *ipb = t;
Steven Rostedt68950612011-12-16 17:06:45 -05003700}
3701
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003702static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08003703 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003704 unsigned long *end)
3705{
Steven Rostedt706c81f2012-04-24 23:45:26 -04003706 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05003707 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04003708 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05003709 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003710 unsigned long *p;
3711 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04003712 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05003713 int ret = -ENOMEM;
3714
3715 count = end - start;
3716
3717 if (!count)
3718 return 0;
3719
Steven Rostedt9fd49322012-04-24 22:32:06 -04003720 sort(start, count, sizeof(*start),
3721 ftrace_cmp_ips, ftrace_swap_ips);
3722
Steven Rostedt706c81f2012-04-24 23:45:26 -04003723 start_pg = ftrace_allocate_pages(count);
3724 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05003725 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003726
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003727 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05003728
Steven Rostedt32082302011-12-16 14:42:37 -05003729 /*
3730 * Core and each module needs their own pages, as
3731 * modules will free them when they are removed.
3732 * Force a new page to be allocated for modules.
3733 */
Steven Rostedta7900872011-12-16 16:23:44 -05003734 if (!mod) {
3735 WARN_ON(ftrace_pages || ftrace_pages_start);
3736 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04003737 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05003738 } else {
Steven Rostedt32082302011-12-16 14:42:37 -05003739 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05003740 goto out;
Steven Rostedt32082302011-12-16 14:42:37 -05003741
Steven Rostedta7900872011-12-16 16:23:44 -05003742 if (WARN_ON(ftrace_pages->next)) {
3743 /* Hmm, we have free pages? */
3744 while (ftrace_pages->next)
3745 ftrace_pages = ftrace_pages->next;
Steven Rostedt32082302011-12-16 14:42:37 -05003746 }
Steven Rostedta7900872011-12-16 16:23:44 -05003747
Steven Rostedt706c81f2012-04-24 23:45:26 -04003748 ftrace_pages->next = start_pg;
Steven Rostedt32082302011-12-16 14:42:37 -05003749 }
3750
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003751 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04003752 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003753 while (p < end) {
3754 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08003755 /*
3756 * Some architecture linkers will pad between
3757 * the different mcount_loc sections of different
3758 * object files to satisfy alignments.
3759 * Skip any NULL pointers.
3760 */
3761 if (!addr)
3762 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04003763
3764 if (pg->index == pg->size) {
3765 /* We should have allocated enough */
3766 if (WARN_ON(!pg->next))
3767 break;
3768 pg = pg->next;
3769 }
3770
3771 rec = &pg->records[pg->index++];
3772 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003773 }
3774
Steven Rostedt706c81f2012-04-24 23:45:26 -04003775 /* We should have used all pages */
3776 WARN_ON(pg->next);
3777
3778 /* Assign the last page to ftrace_pages */
3779 ftrace_pages = pg;
3780
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003781 /* These new locations need to be initialized */
Steven Rostedt706c81f2012-04-24 23:45:26 -04003782 ftrace_new_pgs = start_pg;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003783
Steven Rostedta4f18ed2011-06-07 09:26:46 -04003784 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04003785 * We only need to disable interrupts on start up
3786 * because we are modifying code that an interrupt
3787 * may execute, and the modification is not atomic.
3788 * But for modules, nothing runs the code we modify
3789 * until we are finished with it, and there's no
3790 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04003791 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04003792 if (!mod)
3793 local_irq_save(flags);
Steven Rostedt31e88902008-11-14 16:21:19 -08003794 ftrace_update_code(mod);
Steven Rostedt4376cac2011-06-24 23:28:13 -04003795 if (!mod)
3796 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05003797 ret = 0;
3798 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003799 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003800
Steven Rostedta7900872011-12-16 16:23:44 -05003801 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003802}
3803
Steven Rostedt93eb6772009-04-15 13:24:06 -04003804#ifdef CONFIG_MODULES
Steven Rostedt32082302011-12-16 14:42:37 -05003805
3806#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
3807
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003808void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04003809{
3810 struct dyn_ftrace *rec;
Steven Rostedt32082302011-12-16 14:42:37 -05003811 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04003812 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05003813 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04003814
Steven Rostedt93eb6772009-04-15 13:24:06 -04003815 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003816
3817 if (ftrace_disabled)
3818 goto out_unlock;
3819
Steven Rostedt32082302011-12-16 14:42:37 -05003820 /*
3821 * Each module has its own ftrace_pages, remove
3822 * them from the list.
3823 */
3824 last_pg = &ftrace_pages_start;
3825 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
3826 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003827 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04003828 /*
Steven Rostedt32082302011-12-16 14:42:37 -05003829 * As core pages are first, the first
3830 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04003831 */
Steven Rostedt32082302011-12-16 14:42:37 -05003832 if (WARN_ON(pg == ftrace_pages_start))
3833 goto out_unlock;
3834
3835 /* Check if we are deleting the last page */
3836 if (pg == ftrace_pages)
3837 ftrace_pages = next_to_ftrace_page(last_pg);
3838
3839 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05003840 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3841 free_pages((unsigned long)pg->records, order);
3842 kfree(pg);
Steven Rostedt32082302011-12-16 14:42:37 -05003843 } else
3844 last_pg = &pg->next;
3845 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003846 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04003847 mutex_unlock(&ftrace_lock);
3848}
3849
3850static void ftrace_init_module(struct module *mod,
3851 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04003852{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04003853 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04003854 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003855 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04003856}
3857
Steven Rostedt93eb6772009-04-15 13:24:06 -04003858static int ftrace_module_notify(struct notifier_block *self,
3859 unsigned long val, void *data)
3860{
3861 struct module *mod = data;
3862
3863 switch (val) {
3864 case MODULE_STATE_COMING:
3865 ftrace_init_module(mod, mod->ftrace_callsites,
3866 mod->ftrace_callsites +
3867 mod->num_ftrace_callsites);
3868 break;
3869 case MODULE_STATE_GOING:
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003870 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04003871 break;
3872 }
3873
3874 return 0;
3875}
3876#else
3877static int ftrace_module_notify(struct notifier_block *self,
3878 unsigned long val, void *data)
3879{
3880 return 0;
3881}
3882#endif /* CONFIG_MODULES */
3883
3884struct notifier_block ftrace_module_nb = {
3885 .notifier_call = ftrace_module_notify,
3886 .priority = 0,
3887};
3888
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003889extern unsigned long __start_mcount_loc[];
3890extern unsigned long __stop_mcount_loc[];
3891
3892void __init ftrace_init(void)
3893{
3894 unsigned long count, addr, flags;
3895 int ret;
3896
3897 /* Keep the ftrace pointer to the stub */
3898 addr = (unsigned long)ftrace_stub;
3899
3900 local_irq_save(flags);
3901 ftrace_dyn_arch_init(&addr);
3902 local_irq_restore(flags);
3903
3904 /* ftrace_dyn_arch_init places the return code in addr */
3905 if (addr)
3906 goto failed;
3907
3908 count = __stop_mcount_loc - __start_mcount_loc;
3909
3910 ret = ftrace_dyn_table_alloc(count);
3911 if (ret)
3912 goto failed;
3913
3914 last_ftrace_enabled = ftrace_enabled = 1;
3915
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003916 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08003917 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003918 __stop_mcount_loc);
3919
Steven Rostedt93eb6772009-04-15 13:24:06 -04003920 ret = register_module_notifier(&ftrace_module_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08003921 if (ret)
Steven Rostedt93eb6772009-04-15 13:24:06 -04003922 pr_warning("Failed to register trace ftrace module notifier\n");
3923
Steven Rostedt2af15d62009-05-28 13:37:24 -04003924 set_ftrace_early_filters();
3925
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003926 return;
3927 failed:
3928 ftrace_disabled = 1;
3929}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003930
Steven Rostedt3d083392008-05-12 21:20:42 +02003931#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01003932
Steven Rostedt2b499382011-05-03 22:49:52 -04003933static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04003934 .func = ftrace_stub,
3935};
3936
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01003937static int __init ftrace_nodyn_init(void)
3938{
3939 ftrace_enabled = 1;
3940 return 0;
3941}
3942device_initcall(ftrace_nodyn_init);
3943
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003944static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
3945static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003946/* Keep as macros so we do not need to define the commands */
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04003947# define ftrace_startup(ops, command) \
3948 ({ \
3949 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
3950 0; \
3951 })
Steven Rostedtbd69c302011-05-03 21:55:54 -04003952# define ftrace_shutdown(ops, command) do { } while (0)
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003953# define ftrace_startup_sysctl() do { } while (0)
3954# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04003955
3956static inline int
3957ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
3958{
3959 return 1;
3960}
3961
Steven Rostedt3d083392008-05-12 21:20:42 +02003962#endif /* CONFIG_DYNAMIC_FTRACE */
3963
Steven Rostedtb8489142011-05-04 09:27:52 -04003964static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04003965ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
3966 struct ftrace_ops *op)
Jiri Olsae2484912012-02-15 15:51:48 +01003967{
Jiri Olsae2484912012-02-15 15:51:48 +01003968 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
3969 return;
3970
3971 /*
3972 * Some of the ops may be dynamically allocated,
3973 * they must be freed after a synchronize_sched().
3974 */
3975 preempt_disable_notrace();
3976 trace_recursion_set(TRACE_CONTROL_BIT);
3977 op = rcu_dereference_raw(ftrace_control_list);
3978 while (op != &ftrace_list_end) {
3979 if (!ftrace_function_local_disabled(op) &&
3980 ftrace_ops_test(op, ip))
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04003981 op->func(ip, parent_ip, op);
Jiri Olsae2484912012-02-15 15:51:48 +01003982
3983 op = rcu_dereference_raw(op->next);
3984 };
3985 trace_recursion_clear(TRACE_CONTROL_BIT);
3986 preempt_enable_notrace();
3987}
3988
3989static struct ftrace_ops control_ops = {
3990 .func = ftrace_ops_control_func,
3991};
3992
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04003993static inline void
3994__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
3995 struct ftrace_ops *ignored)
Steven Rostedtb8489142011-05-04 09:27:52 -04003996{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04003997 struct ftrace_ops *op;
Steven Rostedtb8489142011-05-04 09:27:52 -04003998
Steven Rostedtb1cff0a2011-05-25 14:27:43 -04003999 if (unlikely(trace_recursion_test(TRACE_INTERNAL_BIT)))
4000 return;
4001
4002 trace_recursion_set(TRACE_INTERNAL_BIT);
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004003 /*
4004 * Some of the ops may be dynamically allocated,
4005 * they must be freed after a synchronize_sched().
4006 */
4007 preempt_disable_notrace();
4008 op = rcu_dereference_raw(ftrace_ops_list);
Steven Rostedtb8489142011-05-04 09:27:52 -04004009 while (op != &ftrace_list_end) {
4010 if (ftrace_ops_test(op, ip))
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004011 op->func(ip, parent_ip, op);
Steven Rostedtb8489142011-05-04 09:27:52 -04004012 op = rcu_dereference_raw(op->next);
4013 };
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004014 preempt_enable_notrace();
Steven Rostedtb1cff0a2011-05-25 14:27:43 -04004015 trace_recursion_clear(TRACE_INTERNAL_BIT);
Steven Rostedtb8489142011-05-04 09:27:52 -04004016}
4017
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004018/*
4019 * Some archs only support passing ip and parent_ip. Even though
4020 * the list function ignores the op parameter, we do not want any
4021 * C side effects, where a function is called without the caller
4022 * sending a third parameter.
4023 */
4024#if ARCH_SUPPORTS_FTRACE_OPS
4025static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
4026 struct ftrace_ops *op)
4027{
4028 __ftrace_ops_list_func(ip, parent_ip, NULL);
4029}
4030#else
4031static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4032{
4033 __ftrace_ops_list_func(ip, parent_ip, NULL);
4034}
4035#endif
4036
Steven Rostedte32d8952008-12-04 00:26:41 -05004037static void clear_ftrace_swapper(void)
4038{
4039 struct task_struct *p;
4040 int cpu;
4041
4042 get_online_cpus();
4043 for_each_online_cpu(cpu) {
4044 p = idle_task(cpu);
4045 clear_tsk_trace_trace(p);
4046 }
4047 put_online_cpus();
4048}
4049
4050static void set_ftrace_swapper(void)
4051{
4052 struct task_struct *p;
4053 int cpu;
4054
4055 get_online_cpus();
4056 for_each_online_cpu(cpu) {
4057 p = idle_task(cpu);
4058 set_tsk_trace_trace(p);
4059 }
4060 put_online_cpus();
4061}
4062
4063static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004064{
4065 struct task_struct *p;
4066
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004067 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05004068 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05004069 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05004070 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004071 rcu_read_unlock();
4072
Steven Rostedte32d8952008-12-04 00:26:41 -05004073 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004074}
4075
Steven Rostedte32d8952008-12-04 00:26:41 -05004076static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004077{
4078 struct task_struct *p;
4079
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004080 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004081 do_each_pid_task(pid, PIDTYPE_PID, p) {
4082 set_tsk_trace_trace(p);
4083 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004084 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004085}
4086
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004087static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004088{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004089 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004090 clear_ftrace_swapper();
4091 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004092 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05004093}
4094
4095static void set_ftrace_pid_task(struct pid *pid)
4096{
4097 if (pid == ftrace_swapper_pid)
4098 set_ftrace_swapper();
4099 else
4100 set_ftrace_pid(pid);
4101}
4102
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004103static int ftrace_pid_add(int p)
4104{
4105 struct pid *pid;
4106 struct ftrace_pid *fpid;
4107 int ret = -EINVAL;
4108
4109 mutex_lock(&ftrace_lock);
4110
4111 if (!p)
4112 pid = ftrace_swapper_pid;
4113 else
4114 pid = find_get_pid(p);
4115
4116 if (!pid)
4117 goto out;
4118
4119 ret = 0;
4120
4121 list_for_each_entry(fpid, &ftrace_pids, list)
4122 if (fpid->pid == pid)
4123 goto out_put;
4124
4125 ret = -ENOMEM;
4126
4127 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4128 if (!fpid)
4129 goto out_put;
4130
4131 list_add(&fpid->list, &ftrace_pids);
4132 fpid->pid = pid;
4133
4134 set_ftrace_pid_task(pid);
4135
4136 ftrace_update_pid_func();
4137 ftrace_startup_enable(0);
4138
4139 mutex_unlock(&ftrace_lock);
4140 return 0;
4141
4142out_put:
4143 if (pid != ftrace_swapper_pid)
4144 put_pid(pid);
4145
4146out:
4147 mutex_unlock(&ftrace_lock);
4148 return ret;
4149}
4150
4151static void ftrace_pid_reset(void)
4152{
4153 struct ftrace_pid *fpid, *safe;
4154
4155 mutex_lock(&ftrace_lock);
4156 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4157 struct pid *pid = fpid->pid;
4158
4159 clear_ftrace_pid_task(pid);
4160
4161 list_del(&fpid->list);
4162 kfree(fpid);
4163 }
4164
4165 ftrace_update_pid_func();
4166 ftrace_startup_enable(0);
4167
4168 mutex_unlock(&ftrace_lock);
4169}
4170
4171static void *fpid_start(struct seq_file *m, loff_t *pos)
4172{
4173 mutex_lock(&ftrace_lock);
4174
4175 if (list_empty(&ftrace_pids) && (!*pos))
4176 return (void *) 1;
4177
4178 return seq_list_start(&ftrace_pids, *pos);
4179}
4180
4181static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4182{
4183 if (v == (void *)1)
4184 return NULL;
4185
4186 return seq_list_next(v, &ftrace_pids, pos);
4187}
4188
4189static void fpid_stop(struct seq_file *m, void *p)
4190{
4191 mutex_unlock(&ftrace_lock);
4192}
4193
4194static int fpid_show(struct seq_file *m, void *v)
4195{
4196 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4197
4198 if (v == (void *)1) {
4199 seq_printf(m, "no pid\n");
4200 return 0;
4201 }
4202
4203 if (fpid->pid == ftrace_swapper_pid)
4204 seq_printf(m, "swapper tasks\n");
4205 else
4206 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4207
4208 return 0;
4209}
4210
4211static const struct seq_operations ftrace_pid_sops = {
4212 .start = fpid_start,
4213 .next = fpid_next,
4214 .stop = fpid_stop,
4215 .show = fpid_show,
4216};
4217
4218static int
4219ftrace_pid_open(struct inode *inode, struct file *file)
4220{
4221 int ret = 0;
4222
4223 if ((file->f_mode & FMODE_WRITE) &&
4224 (file->f_flags & O_TRUNC))
4225 ftrace_pid_reset();
4226
4227 if (file->f_mode & FMODE_READ)
4228 ret = seq_open(file, &ftrace_pid_sops);
4229
4230 return ret;
4231}
4232
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004233static ssize_t
4234ftrace_pid_write(struct file *filp, const char __user *ubuf,
4235 size_t cnt, loff_t *ppos)
4236{
Ingo Molnar457dc922009-11-23 11:03:28 +01004237 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004238 long val;
4239 int ret;
4240
4241 if (cnt >= sizeof(buf))
4242 return -EINVAL;
4243
4244 if (copy_from_user(&buf, ubuf, cnt))
4245 return -EFAULT;
4246
4247 buf[cnt] = 0;
4248
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004249 /*
4250 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4251 * to clean the filter quietly.
4252 */
Ingo Molnar457dc922009-11-23 11:03:28 +01004253 tmp = strstrip(buf);
4254 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004255 return 1;
4256
Ingo Molnar457dc922009-11-23 11:03:28 +01004257 ret = strict_strtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004258 if (ret < 0)
4259 return ret;
4260
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004261 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004262
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004263 return ret ? ret : cnt;
4264}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004265
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004266static int
4267ftrace_pid_release(struct inode *inode, struct file *file)
4268{
4269 if (file->f_mode & FMODE_READ)
4270 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004271
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004272 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004273}
4274
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004275static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004276 .open = ftrace_pid_open,
4277 .write = ftrace_pid_write,
4278 .read = seq_read,
4279 .llseek = seq_lseek,
4280 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004281};
4282
4283static __init int ftrace_init_debugfs(void)
4284{
4285 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004286
4287 d_tracer = tracing_init_dentry();
4288 if (!d_tracer)
4289 return 0;
4290
4291 ftrace_init_dyn_debugfs(d_tracer);
4292
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004293 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4294 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04004295
4296 ftrace_profile_debugfs(d_tracer);
4297
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004298 return 0;
4299}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004300fs_initcall(ftrace_init_debugfs);
4301
Steven Rostedt3d083392008-05-12 21:20:42 +02004302/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004303 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004304 *
4305 * This function should be used by panic code. It stops ftrace
4306 * but in a not so nice way. If you need to simply kill ftrace
4307 * from a non-atomic section, use ftrace_kill.
4308 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004309void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004310{
4311 ftrace_disabled = 1;
4312 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004313 clear_ftrace_function();
4314}
4315
4316/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04004317 * Test if ftrace is dead or not.
4318 */
4319int ftrace_is_dead(void)
4320{
4321 return ftrace_disabled;
4322}
4323
4324/**
Steven Rostedt3d083392008-05-12 21:20:42 +02004325 * register_ftrace_function - register a function for profiling
4326 * @ops - ops structure that holds the function for profiling.
4327 *
4328 * Register a function to be called by all functions in the
4329 * kernel.
4330 *
4331 * Note: @ops->func and all the functions it calls must be labeled
4332 * with "notrace", otherwise it will go into a
4333 * recursive loop.
4334 */
4335int register_ftrace_function(struct ftrace_ops *ops)
4336{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004337 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004338
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004339 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004340
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004341 ret = __register_ftrace_function(ops);
Steven Rostedtb8489142011-05-04 09:27:52 -04004342 if (!ret)
Steven Rostedta1cd6172011-05-23 15:24:25 -04004343 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04004344
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004345 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02004346
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004347 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02004348}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004349EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02004350
4351/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01004352 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02004353 * @ops - ops structure that holds the function to unregister
4354 *
4355 * Unregister a function that was added to be called by ftrace profiling.
4356 */
4357int unregister_ftrace_function(struct ftrace_ops *ops)
4358{
4359 int ret;
4360
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004361 mutex_lock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004362 ret = __unregister_ftrace_function(ops);
Steven Rostedtb8489142011-05-04 09:27:52 -04004363 if (!ret)
4364 ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004365 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004366
4367 return ret;
4368}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004369EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004370
Ingo Molnare309b412008-05-12 21:20:51 +02004371int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004372ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004373 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004374 loff_t *ppos)
4375{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004376 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004377
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004378 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004379
Steven Rostedt45a4a232011-04-21 23:16:46 -04004380 if (unlikely(ftrace_disabled))
4381 goto out;
4382
4383 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004384
Li Zefana32c7762009-06-26 16:55:51 +08004385 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004386 goto out;
4387
Li Zefana32c7762009-06-26 16:55:51 +08004388 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004389
4390 if (ftrace_enabled) {
4391
4392 ftrace_startup_sysctl();
4393
4394 /* we are starting ftrace again */
Steven Rostedtb8489142011-05-04 09:27:52 -04004395 if (ftrace_ops_list != &ftrace_list_end) {
4396 if (ftrace_ops_list->next == &ftrace_list_end)
4397 ftrace_trace_function = ftrace_ops_list->func;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004398 else
Steven Rostedtb8489142011-05-04 09:27:52 -04004399 ftrace_trace_function = ftrace_ops_list_func;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004400 }
4401
4402 } else {
4403 /* stopping ftrace calls (just send to ftrace_stub) */
4404 ftrace_trace_function = ftrace_stub;
4405
4406 ftrace_shutdown_sysctl();
4407 }
4408
4409 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004410 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004411 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02004412}
Ingo Molnarf17845e2008-10-24 12:47:10 +02004413
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004414#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004415
Steven Rostedt597af812009-04-03 15:24:12 -04004416static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004417static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004418
Steven Rostedte49dc192008-12-02 23:50:05 -05004419int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4420{
4421 return 0;
4422}
4423
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004424/* The callbacks that hook a function */
4425trace_func_graph_ret_t ftrace_graph_return =
4426 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004427trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004428
4429/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4430static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4431{
4432 int i;
4433 int ret = 0;
4434 unsigned long flags;
4435 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4436 struct task_struct *g, *t;
4437
4438 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4439 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4440 * sizeof(struct ftrace_ret_stack),
4441 GFP_KERNEL);
4442 if (!ret_stack_list[i]) {
4443 start = 0;
4444 end = i;
4445 ret = -ENOMEM;
4446 goto free;
4447 }
4448 }
4449
4450 read_lock_irqsave(&tasklist_lock, flags);
4451 do_each_thread(g, t) {
4452 if (start == end) {
4453 ret = -EAGAIN;
4454 goto unlock;
4455 }
4456
4457 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01004458 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004459 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04004460 t->curr_ret_stack = -1;
4461 /* Make sure the tasks see the -1 first: */
4462 smp_wmb();
4463 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004464 }
4465 } while_each_thread(g, t);
4466
4467unlock:
4468 read_unlock_irqrestore(&tasklist_lock, flags);
4469free:
4470 for (i = start; i < end; i++)
4471 kfree(ret_stack_list[i]);
4472 return ret;
4473}
4474
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004475static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04004476ftrace_graph_probe_sched_switch(void *ignore,
4477 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004478{
4479 unsigned long long timestamp;
4480 int index;
4481
Steven Rostedtbe6f1642009-03-24 11:06:24 -04004482 /*
4483 * Does the user want to count the time a function was asleep.
4484 * If so, do not update the time stamps.
4485 */
4486 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4487 return;
4488
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004489 timestamp = trace_clock_local();
4490
4491 prev->ftrace_timestamp = timestamp;
4492
4493 /* only process tasks that we timestamped */
4494 if (!next->ftrace_timestamp)
4495 return;
4496
4497 /*
4498 * Update all the counters in next to make up for the
4499 * time next was sleeping.
4500 */
4501 timestamp -= next->ftrace_timestamp;
4502
4503 for (index = next->curr_ret_stack; index >= 0; index--)
4504 next->ret_stack[index].calltime += timestamp;
4505}
4506
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004507/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004508static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004509{
4510 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004511 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004512
4513 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4514 sizeof(struct ftrace_ret_stack *),
4515 GFP_KERNEL);
4516
4517 if (!ret_stack_list)
4518 return -ENOMEM;
4519
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004520 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04004521 for_each_online_cpu(cpu) {
4522 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05004523 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04004524 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004525
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004526 do {
4527 ret = alloc_retstack_tasklist(ret_stack_list);
4528 } while (ret == -EAGAIN);
4529
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004530 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04004531 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004532 if (ret)
4533 pr_info("ftrace_graph: Couldn't activate tracepoint"
4534 " probe to kernel_sched_switch\n");
4535 }
4536
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004537 kfree(ret_stack_list);
4538 return ret;
4539}
4540
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004541/*
4542 * Hibernation protection.
4543 * The state of the current task is too much unstable during
4544 * suspend/restore to disk. We want to protect against that.
4545 */
4546static int
4547ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4548 void *unused)
4549{
4550 switch (state) {
4551 case PM_HIBERNATION_PREPARE:
4552 pause_graph_tracing();
4553 break;
4554
4555 case PM_POST_HIBERNATION:
4556 unpause_graph_tracing();
4557 break;
4558 }
4559 return NOTIFY_DONE;
4560}
4561
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004562int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4563 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004564{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004565 int ret = 0;
4566
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004567 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004568
Steven Rostedt05ce5812009-03-24 00:18:31 -04004569 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04004570 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04004571 ret = -EBUSY;
4572 goto out;
4573 }
4574
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004575 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
4576 register_pm_notifier(&ftrace_suspend_notifier);
4577
Steven Rostedt597af812009-04-03 15:24:12 -04004578 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004579 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004580 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04004581 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004582 goto out;
4583 }
Steven Rostedte53a6312008-11-26 00:16:25 -05004584
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004585 ftrace_graph_return = retfunc;
4586 ftrace_graph_entry = entryfunc;
Steven Rostedte53a6312008-11-26 00:16:25 -05004587
Steven Rostedta1cd6172011-05-23 15:24:25 -04004588 ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004589
4590out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004591 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004592 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004593}
4594
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004595void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004596{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004597 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004598
Steven Rostedt597af812009-04-03 15:24:12 -04004599 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004600 goto out;
4601
Steven Rostedt597af812009-04-03 15:24:12 -04004602 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004603 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004604 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedtbd69c302011-05-03 21:55:54 -04004605 ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004606 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04004607 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004608
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004609 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004610 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004611}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004612
Steven Rostedt868baf02011-02-10 21:26:13 -05004613static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
4614
4615static void
4616graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
4617{
4618 atomic_set(&t->tracing_graph_pause, 0);
4619 atomic_set(&t->trace_overrun, 0);
4620 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004621 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05004622 smp_wmb();
4623 t->ret_stack = ret_stack;
4624}
4625
4626/*
4627 * Allocate a return stack for the idle task. May be the first
4628 * time through, or it may be done by CPU hotplug online.
4629 */
4630void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
4631{
4632 t->curr_ret_stack = -1;
4633 /*
4634 * The idle task has no parent, it either has its own
4635 * stack or no stack at all.
4636 */
4637 if (t->ret_stack)
4638 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
4639
4640 if (ftrace_graph_active) {
4641 struct ftrace_ret_stack *ret_stack;
4642
4643 ret_stack = per_cpu(idle_ret_stack, cpu);
4644 if (!ret_stack) {
4645 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4646 * sizeof(struct ftrace_ret_stack),
4647 GFP_KERNEL);
4648 if (!ret_stack)
4649 return;
4650 per_cpu(idle_ret_stack, cpu) = ret_stack;
4651 }
4652 graph_init_task(t, ret_stack);
4653 }
4654}
4655
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004656/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004657void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004658{
Steven Rostedt84047e32009-06-02 16:51:55 -04004659 /* Make sure we do not use the parent ret_stack */
4660 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05004661 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04004662
Steven Rostedt597af812009-04-03 15:24:12 -04004663 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04004664 struct ftrace_ret_stack *ret_stack;
4665
4666 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004667 * sizeof(struct ftrace_ret_stack),
4668 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04004669 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004670 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05004671 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04004672 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004673}
4674
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004675void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004676{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004677 struct ftrace_ret_stack *ret_stack = t->ret_stack;
4678
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004679 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004680 /* NULL must become visible to IRQs before we free it: */
4681 barrier();
4682
4683 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004684}
Steven Rostedt14a866c2008-12-02 23:50:02 -05004685
4686void ftrace_graph_stop(void)
4687{
4688 ftrace_stop();
4689}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004690#endif