blob: 867bd1dd2dd09250f3a52663844ee1b54435bbd3 [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 Rostedt4eebcc82008-05-12 21:20:48 +020067/* ftrace_enabled is a method to turn ftrace on or off */
68int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020069static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020070
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050071/* Quick disabling of function tracer. */
72int function_trace_stop;
73
jolsa@redhat.com756d17e2009-10-13 16:33:52 -040074/* List for set_ftrace_pid's pids. */
75LIST_HEAD(ftrace_pids);
76struct ftrace_pid {
77 struct list_head list;
78 struct pid *pid;
79};
80
Steven Rostedt4eebcc82008-05-12 21:20:48 +020081/*
82 * ftrace_disabled is set when an anomaly is discovered.
83 * ftrace_disabled is much stronger than ftrace_enabled.
84 */
85static int ftrace_disabled __read_mostly;
86
Steven Rostedt52baf112009-02-14 01:15:39 -050087static DEFINE_MUTEX(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +020088
Paul McQuadebd38c0e2011-05-31 20:51:55 +010089static struct ftrace_ops ftrace_list_end __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -040090 .func = ftrace_stub,
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020091};
92
Steven Rostedtb8489142011-05-04 09:27:52 -040093static struct ftrace_ops *ftrace_global_list __read_mostly = &ftrace_list_end;
Jiri Olsae2484912012-02-15 15:51:48 +010094static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -040095static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020096ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedt6331c282011-07-13 15:11:02 -040097static ftrace_func_t __ftrace_trace_function_delay __read_mostly = ftrace_stub;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050098ftrace_func_t __ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -050099ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
Steven Rostedt2b499382011-05-03 22:49:52 -0400100static struct ftrace_ops global_ops;
Jiri Olsae2484912012-02-15 15:51:48 +0100101static struct ftrace_ops control_ops;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200102
Steven Rostedtb8489142011-05-04 09:27:52 -0400103static void
104ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip);
105
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800106/*
Steven Rostedtb8489142011-05-04 09:27:52 -0400107 * Traverse the ftrace_global_list, invoking all entries. The reason that we
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800108 * can use rcu_dereference_raw() is that elements removed from this list
109 * are simply leaked, so there is no need to interact with a grace-period
110 * mechanism. The rcu_dereference_raw() calls are needed to handle
Steven Rostedtb8489142011-05-04 09:27:52 -0400111 * concurrent insertions into the ftrace_global_list.
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800112 *
113 * Silly Alpha and silly pointer-speculation compiler optimizations!
114 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400115static void ftrace_global_list_func(unsigned long ip,
116 unsigned long parent_ip)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200117{
Steven Rostedtb1cff0a2011-05-25 14:27:43 -0400118 struct ftrace_ops *op;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200119
Steven Rostedtb1cff0a2011-05-25 14:27:43 -0400120 if (unlikely(trace_recursion_test(TRACE_GLOBAL_BIT)))
121 return;
122
123 trace_recursion_set(TRACE_GLOBAL_BIT);
124 op = rcu_dereference_raw(ftrace_global_list); /*see above*/
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200125 while (op != &ftrace_list_end) {
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200126 op->func(ip, parent_ip);
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800127 op = rcu_dereference_raw(op->next); /*see above*/
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200128 };
Steven Rostedtb1cff0a2011-05-25 14:27:43 -0400129 trace_recursion_clear(TRACE_GLOBAL_BIT);
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200130}
131
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500132static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip)
133{
Steven Rostedt0ef8cde2008-12-03 15:36:58 -0500134 if (!test_tsk_trace_trace(current))
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500135 return;
136
137 ftrace_pid_function(ip, parent_ip);
138}
139
140static void set_ftrace_pid_function(ftrace_func_t func)
141{
142 /* do not set ftrace_pid_function to itself! */
143 if (func != ftrace_pid_func)
144 ftrace_pid_function = func;
145}
146
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200147/**
Steven Rostedt3d083392008-05-12 21:20:42 +0200148 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200149 *
Steven Rostedt3d083392008-05-12 21:20:42 +0200150 * This NULLs the ftrace function and in essence stops
151 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200152 */
Steven Rostedt3d083392008-05-12 21:20:42 +0200153void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200154{
Steven Rostedt3d083392008-05-12 21:20:42 +0200155 ftrace_trace_function = ftrace_stub;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500156 __ftrace_trace_function = ftrace_stub;
Steven Rostedt6331c282011-07-13 15:11:02 -0400157 __ftrace_trace_function_delay = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500158 ftrace_pid_function = ftrace_stub;
Steven Rostedt3d083392008-05-12 21:20:42 +0200159}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200160
Steven Rostedt60a7ecf2008-11-05 16:05:44 -0500161#ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
162/*
163 * For those archs that do not test ftrace_trace_stop in their
164 * mcount call site, we need to do it from C.
165 */
166static void ftrace_test_stop_func(unsigned long ip, unsigned long parent_ip)
167{
168 if (function_trace_stop)
169 return;
170
171 __ftrace_trace_function(ip, parent_ip);
172}
173#endif
174
Jiri Olsae2484912012-02-15 15:51:48 +0100175static void control_ops_disable_all(struct ftrace_ops *ops)
176{
177 int cpu;
178
179 for_each_possible_cpu(cpu)
180 *per_cpu_ptr(ops->disabled, cpu) = 1;
181}
182
183static int control_ops_alloc(struct ftrace_ops *ops)
184{
185 int __percpu *disabled;
186
187 disabled = alloc_percpu(int);
188 if (!disabled)
189 return -ENOMEM;
190
191 ops->disabled = disabled;
192 control_ops_disable_all(ops);
193 return 0;
194}
195
196static void control_ops_free(struct ftrace_ops *ops)
197{
198 free_percpu(ops->disabled);
199}
200
Steven Rostedt2b499382011-05-03 22:49:52 -0400201static void update_global_ops(void)
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400202{
203 ftrace_func_t func;
204
205 /*
206 * If there's only one function registered, then call that
207 * function directly. Otherwise, we need to iterate over the
208 * registered callers.
209 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400210 if (ftrace_global_list == &ftrace_list_end ||
211 ftrace_global_list->next == &ftrace_list_end)
212 func = ftrace_global_list->func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400213 else
Steven Rostedtb8489142011-05-04 09:27:52 -0400214 func = ftrace_global_list_func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400215
216 /* If we filter on pids, update to use the pid function */
217 if (!list_empty(&ftrace_pids)) {
218 set_ftrace_pid_function(func);
219 func = ftrace_pid_func;
220 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400221
222 global_ops.func = func;
223}
224
225static void update_ftrace_function(void)
226{
227 ftrace_func_t func;
228
229 update_global_ops();
230
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400231 /*
232 * If we are at the end of the list and this ops is
233 * not dynamic, then have the mcount trampoline call
234 * the function directly
235 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400236 if (ftrace_ops_list == &ftrace_list_end ||
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400237 (ftrace_ops_list->next == &ftrace_list_end &&
238 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC)))
Steven Rostedtb8489142011-05-04 09:27:52 -0400239 func = ftrace_ops_list->func;
240 else
241 func = ftrace_ops_list_func;
Steven Rostedt2b499382011-05-03 22:49:52 -0400242
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400243#ifdef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
244 ftrace_trace_function = func;
245#else
Steven Rostedt6331c282011-07-13 15:11:02 -0400246#ifdef CONFIG_DYNAMIC_FTRACE
247 /* do not update till all functions have been modified */
248 __ftrace_trace_function_delay = func;
249#else
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400250 __ftrace_trace_function = func;
Steven Rostedt6331c282011-07-13 15:11:02 -0400251#endif
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400252 ftrace_trace_function = ftrace_test_stop_func;
253#endif
254}
255
Steven Rostedt2b499382011-05-03 22:49:52 -0400256static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200257{
Steven Rostedt2b499382011-05-03 22:49:52 -0400258 ops->next = *list;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200259 /*
Steven Rostedtb8489142011-05-04 09:27:52 -0400260 * We are entering ops into the list but another
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200261 * CPU might be walking that list. We need to make sure
262 * the ops->next pointer is valid before another CPU sees
Steven Rostedtb8489142011-05-04 09:27:52 -0400263 * the ops pointer included into the list.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200264 */
Steven Rostedt2b499382011-05-03 22:49:52 -0400265 rcu_assign_pointer(*list, ops);
266}
Steven Rostedt3d083392008-05-12 21:20:42 +0200267
Steven Rostedt2b499382011-05-03 22:49:52 -0400268static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
269{
270 struct ftrace_ops **p;
271
272 /*
273 * If we are removing the last function, then simply point
274 * to the ftrace_stub.
275 */
276 if (*list == ops && ops->next == &ftrace_list_end) {
277 *list = &ftrace_list_end;
278 return 0;
279 }
280
281 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
282 if (*p == ops)
283 break;
284
285 if (*p != ops)
286 return -1;
287
288 *p = (*p)->next;
289 return 0;
290}
291
Jiri Olsae2484912012-02-15 15:51:48 +0100292static void add_ftrace_list_ops(struct ftrace_ops **list,
293 struct ftrace_ops *main_ops,
294 struct ftrace_ops *ops)
295{
296 int first = *list == &ftrace_list_end;
297 add_ftrace_ops(list, ops);
298 if (first)
299 add_ftrace_ops(&ftrace_ops_list, main_ops);
300}
301
302static int remove_ftrace_list_ops(struct ftrace_ops **list,
303 struct ftrace_ops *main_ops,
304 struct ftrace_ops *ops)
305{
306 int ret = remove_ftrace_ops(list, ops);
307 if (!ret && *list == &ftrace_list_end)
308 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
309 return ret;
310}
311
Steven Rostedt2b499382011-05-03 22:49:52 -0400312static int __register_ftrace_function(struct ftrace_ops *ops)
313{
314 if (ftrace_disabled)
315 return -ENODEV;
316
317 if (FTRACE_WARN_ON(ops == &global_ops))
318 return -EINVAL;
319
Steven Rostedtb8489142011-05-04 09:27:52 -0400320 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
321 return -EBUSY;
322
Jiri Olsae2484912012-02-15 15:51:48 +0100323 /* We don't support both control and global flags set. */
324 if ((ops->flags & FL_GLOBAL_CONTROL_MASK) == FL_GLOBAL_CONTROL_MASK)
325 return -EINVAL;
326
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400327 if (!core_kernel_data((unsigned long)ops))
328 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
329
Steven Rostedtb8489142011-05-04 09:27:52 -0400330 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100331 add_ftrace_list_ops(&ftrace_global_list, &global_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400332 ops->flags |= FTRACE_OPS_FL_ENABLED;
Jiri Olsae2484912012-02-15 15:51:48 +0100333 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
334 if (control_ops_alloc(ops))
335 return -ENOMEM;
336 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400337 } else
338 add_ftrace_ops(&ftrace_ops_list, ops);
339
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400340 if (ftrace_enabled)
341 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200342
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200343 return 0;
344}
345
Ingo Molnare309b412008-05-12 21:20:51 +0200346static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200347{
Steven Rostedt2b499382011-05-03 22:49:52 -0400348 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200349
Steven Rostedt2b499382011-05-03 22:49:52 -0400350 if (ftrace_disabled)
351 return -ENODEV;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200352
Steven Rostedtb8489142011-05-04 09:27:52 -0400353 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
354 return -EBUSY;
355
Steven Rostedt2b499382011-05-03 22:49:52 -0400356 if (FTRACE_WARN_ON(ops == &global_ops))
357 return -EINVAL;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200358
Steven Rostedtb8489142011-05-04 09:27:52 -0400359 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100360 ret = remove_ftrace_list_ops(&ftrace_global_list,
361 &global_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400362 if (!ret)
363 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Jiri Olsae2484912012-02-15 15:51:48 +0100364 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
365 ret = remove_ftrace_list_ops(&ftrace_control_list,
366 &control_ops, ops);
367 if (!ret) {
368 /*
369 * The ftrace_ops is now removed from the list,
370 * so there'll be no new users. We must ensure
371 * all current users are done before we free
372 * the control data.
373 */
374 synchronize_sched();
375 control_ops_free(ops);
376 }
Steven Rostedtb8489142011-05-04 09:27:52 -0400377 } else
378 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
379
Steven Rostedt2b499382011-05-03 22:49:52 -0400380 if (ret < 0)
381 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400382
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400383 if (ftrace_enabled)
384 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200385
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400386 /*
387 * Dynamic ops may be freed, we must make sure that all
388 * callers are done before leaving this function.
389 */
390 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
391 synchronize_sched();
392
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500393 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200394}
395
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500396static void ftrace_update_pid_func(void)
397{
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400398 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500399 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900400 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500401
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400402 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500403}
404
Steven Rostedt493762f2009-03-23 17:12:36 -0400405#ifdef CONFIG_FUNCTION_PROFILER
406struct ftrace_profile {
407 struct hlist_node node;
408 unsigned long ip;
409 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400410#ifdef CONFIG_FUNCTION_GRAPH_TRACER
411 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400412 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400413#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400414};
415
416struct ftrace_profile_page {
417 struct ftrace_profile_page *next;
418 unsigned long index;
419 struct ftrace_profile records[];
420};
421
Steven Rostedtcafb1682009-03-24 20:50:39 -0400422struct ftrace_profile_stat {
423 atomic_t disabled;
424 struct hlist_head *hash;
425 struct ftrace_profile_page *pages;
426 struct ftrace_profile_page *start;
427 struct tracer_stat stat;
428};
429
Steven Rostedt493762f2009-03-23 17:12:36 -0400430#define PROFILE_RECORDS_SIZE \
431 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
432
433#define PROFILES_PER_PAGE \
434 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
435
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400436static int ftrace_profile_bits __read_mostly;
437static int ftrace_profile_enabled __read_mostly;
438
439/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400440static DEFINE_MUTEX(ftrace_profile_lock);
441
Steven Rostedtcafb1682009-03-24 20:50:39 -0400442static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400443
444#define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
445
Steven Rostedt493762f2009-03-23 17:12:36 -0400446static void *
447function_stat_next(void *v, int idx)
448{
449 struct ftrace_profile *rec = v;
450 struct ftrace_profile_page *pg;
451
452 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
453
454 again:
Li Zefan0296e422009-06-26 11:15:37 +0800455 if (idx != 0)
456 rec++;
457
Steven Rostedt493762f2009-03-23 17:12:36 -0400458 if ((void *)rec >= (void *)&pg->records[pg->index]) {
459 pg = pg->next;
460 if (!pg)
461 return NULL;
462 rec = &pg->records[0];
463 if (!rec->counter)
464 goto again;
465 }
466
467 return rec;
468}
469
470static void *function_stat_start(struct tracer_stat *trace)
471{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400472 struct ftrace_profile_stat *stat =
473 container_of(trace, struct ftrace_profile_stat, stat);
474
475 if (!stat || !stat->start)
476 return NULL;
477
478 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400479}
480
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400481#ifdef CONFIG_FUNCTION_GRAPH_TRACER
482/* function graph compares on total time */
483static int function_stat_cmp(void *p1, void *p2)
484{
485 struct ftrace_profile *a = p1;
486 struct ftrace_profile *b = p2;
487
488 if (a->time < b->time)
489 return -1;
490 if (a->time > b->time)
491 return 1;
492 else
493 return 0;
494}
495#else
496/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400497static int function_stat_cmp(void *p1, void *p2)
498{
499 struct ftrace_profile *a = p1;
500 struct ftrace_profile *b = p2;
501
502 if (a->counter < b->counter)
503 return -1;
504 if (a->counter > b->counter)
505 return 1;
506 else
507 return 0;
508}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400509#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400510
511static int function_stat_headers(struct seq_file *m)
512{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400513#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400514 seq_printf(m, " Function "
Chase Douglase330b3b2010-04-26 14:02:05 -0400515 "Hit Time Avg s^2\n"
Steven Rostedt34886c82009-03-25 21:00:47 -0400516 " -------- "
Chase Douglase330b3b2010-04-26 14:02:05 -0400517 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400518#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400519 seq_printf(m, " Function Hit\n"
520 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400521#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400522 return 0;
523}
524
525static int function_stat_show(struct seq_file *m, void *v)
526{
527 struct ftrace_profile *rec = v;
528 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800529 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400530#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400531 static struct trace_seq s;
532 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400533 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400534#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800535 mutex_lock(&ftrace_profile_lock);
536
537 /* we raced with function_profile_reset() */
538 if (unlikely(rec->counter == 0)) {
539 ret = -EBUSY;
540 goto out;
541 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400542
543 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400544 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400545
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400546#ifdef CONFIG_FUNCTION_GRAPH_TRACER
547 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400548 avg = rec->time;
549 do_div(avg, rec->counter);
550
Chase Douglase330b3b2010-04-26 14:02:05 -0400551 /* Sample standard deviation (s^2) */
552 if (rec->counter <= 1)
553 stddev = 0;
554 else {
555 stddev = rec->time_squared - rec->counter * avg * avg;
556 /*
557 * Divide only 1000 for ns^2 -> us^2 conversion.
558 * trace_print_graph_duration will divide 1000 again.
559 */
560 do_div(stddev, (rec->counter - 1) * 1000);
561 }
562
Steven Rostedt34886c82009-03-25 21:00:47 -0400563 trace_seq_init(&s);
564 trace_print_graph_duration(rec->time, &s);
565 trace_seq_puts(&s, " ");
566 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400567 trace_seq_puts(&s, " ");
568 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400569 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400570#endif
571 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800572out:
573 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400574
Li Zefan3aaba202010-08-23 16:50:12 +0800575 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400576}
577
Steven Rostedtcafb1682009-03-24 20:50:39 -0400578static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400579{
580 struct ftrace_profile_page *pg;
581
Steven Rostedtcafb1682009-03-24 20:50:39 -0400582 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400583
584 while (pg) {
585 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
586 pg->index = 0;
587 pg = pg->next;
588 }
589
Steven Rostedtcafb1682009-03-24 20:50:39 -0400590 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400591 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
592}
593
Steven Rostedtcafb1682009-03-24 20:50:39 -0400594int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400595{
596 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400597 int functions;
598 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400599 int i;
600
601 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400602 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400603 return 0;
604
Steven Rostedtcafb1682009-03-24 20:50:39 -0400605 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
606 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400607 return -ENOMEM;
608
Steven Rostedt318e0a72009-03-25 20:06:34 -0400609#ifdef CONFIG_DYNAMIC_FTRACE
610 functions = ftrace_update_tot_cnt;
611#else
612 /*
613 * We do not know the number of functions that exist because
614 * dynamic tracing is what counts them. With past experience
615 * we have around 20K functions. That should be more than enough.
616 * It is highly unlikely we will execute every function in
617 * the kernel.
618 */
619 functions = 20000;
620#endif
621
Steven Rostedtcafb1682009-03-24 20:50:39 -0400622 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400623
Steven Rostedt318e0a72009-03-25 20:06:34 -0400624 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
625
626 for (i = 0; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400627 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400628 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400629 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400630 pg = pg->next;
631 }
632
633 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400634
635 out_free:
636 pg = stat->start;
637 while (pg) {
638 unsigned long tmp = (unsigned long)pg;
639
640 pg = pg->next;
641 free_page(tmp);
642 }
643
644 free_page((unsigned long)stat->pages);
645 stat->pages = NULL;
646 stat->start = NULL;
647
648 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400649}
650
Steven Rostedtcafb1682009-03-24 20:50:39 -0400651static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400652{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400653 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400654 int size;
655
Steven Rostedtcafb1682009-03-24 20:50:39 -0400656 stat = &per_cpu(ftrace_profile_stats, cpu);
657
658 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400659 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400660 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400661 return 0;
662 }
663
664 /*
665 * We are profiling all functions, but usually only a few thousand
666 * functions are hit. We'll make a hash of 1024 items.
667 */
668 size = FTRACE_PROFILE_HASH_SIZE;
669
Steven Rostedtcafb1682009-03-24 20:50:39 -0400670 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400671
Steven Rostedtcafb1682009-03-24 20:50:39 -0400672 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400673 return -ENOMEM;
674
Steven Rostedtcafb1682009-03-24 20:50:39 -0400675 if (!ftrace_profile_bits) {
676 size--;
Steven Rostedt493762f2009-03-23 17:12:36 -0400677
Steven Rostedtcafb1682009-03-24 20:50:39 -0400678 for (; size; size >>= 1)
679 ftrace_profile_bits++;
680 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400681
Steven Rostedt318e0a72009-03-25 20:06:34 -0400682 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400683 if (ftrace_profile_pages_init(stat) < 0) {
684 kfree(stat->hash);
685 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400686 return -ENOMEM;
687 }
688
689 return 0;
690}
691
Steven Rostedtcafb1682009-03-24 20:50:39 -0400692static int ftrace_profile_init(void)
693{
694 int cpu;
695 int ret = 0;
696
697 for_each_online_cpu(cpu) {
698 ret = ftrace_profile_init_cpu(cpu);
699 if (ret)
700 break;
701 }
702
703 return ret;
704}
705
Steven Rostedt493762f2009-03-23 17:12:36 -0400706/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400707static struct ftrace_profile *
708ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400709{
710 struct ftrace_profile *rec;
711 struct hlist_head *hhd;
712 struct hlist_node *n;
713 unsigned long key;
714
715 key = hash_long(ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400716 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400717
718 if (hlist_empty(hhd))
719 return NULL;
720
721 hlist_for_each_entry_rcu(rec, n, hhd, node) {
722 if (rec->ip == ip)
723 return rec;
724 }
725
726 return NULL;
727}
728
Steven Rostedtcafb1682009-03-24 20:50:39 -0400729static void ftrace_add_profile(struct ftrace_profile_stat *stat,
730 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400731{
732 unsigned long key;
733
734 key = hash_long(rec->ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400735 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400736}
737
Steven Rostedt318e0a72009-03-25 20:06:34 -0400738/*
739 * The memory is already allocated, this simply finds a new record to use.
740 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400741static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400742ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400743{
744 struct ftrace_profile *rec = NULL;
745
Steven Rostedt318e0a72009-03-25 20:06:34 -0400746 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400747 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400748 goto out;
749
Steven Rostedt493762f2009-03-23 17:12:36 -0400750 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400751 * Try to find the function again since an NMI
752 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400753 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400754 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400755 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400756 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400757
Steven Rostedtcafb1682009-03-24 20:50:39 -0400758 if (stat->pages->index == PROFILES_PER_PAGE) {
759 if (!stat->pages->next)
760 goto out;
761 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400762 }
763
Steven Rostedtcafb1682009-03-24 20:50:39 -0400764 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400765 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400766 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400767
Steven Rostedt493762f2009-03-23 17:12:36 -0400768 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400769 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400770
771 return rec;
772}
773
Steven Rostedt493762f2009-03-23 17:12:36 -0400774static void
775function_profile_call(unsigned long ip, unsigned long parent_ip)
776{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400777 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400778 struct ftrace_profile *rec;
779 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400780
781 if (!ftrace_profile_enabled)
782 return;
783
Steven Rostedt493762f2009-03-23 17:12:36 -0400784 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400785
786 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400787 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400788 goto out;
789
790 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400791 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400792 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400793 if (!rec)
794 goto out;
795 }
796
797 rec->counter++;
798 out:
799 local_irq_restore(flags);
800}
801
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400802#ifdef CONFIG_FUNCTION_GRAPH_TRACER
803static int profile_graph_entry(struct ftrace_graph_ent *trace)
804{
805 function_profile_call(trace->func, 0);
806 return 1;
807}
808
809static void profile_graph_return(struct ftrace_graph_ret *trace)
810{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400811 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400812 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400813 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400814 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400815
816 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400817 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400818 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400819 goto out;
820
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400821 /* If the calltime was zero'd ignore it */
822 if (!trace->calltime)
823 goto out;
824
Steven Rostedta2a16d62009-03-24 23:17:58 -0400825 calltime = trace->rettime - trace->calltime;
826
827 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
828 int index;
829
830 index = trace->depth;
831
832 /* Append this call time to the parent time to subtract */
833 if (index)
834 current->ret_stack[index - 1].subtime += calltime;
835
836 if (current->ret_stack[index].subtime < calltime)
837 calltime -= current->ret_stack[index].subtime;
838 else
839 calltime = 0;
840 }
841
Steven Rostedtcafb1682009-03-24 20:50:39 -0400842 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400843 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400844 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400845 rec->time_squared += calltime * calltime;
846 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400847
Steven Rostedtcafb1682009-03-24 20:50:39 -0400848 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400849 local_irq_restore(flags);
850}
851
852static int register_ftrace_profiler(void)
853{
854 return register_ftrace_graph(&profile_graph_return,
855 &profile_graph_entry);
856}
857
858static void unregister_ftrace_profiler(void)
859{
860 unregister_ftrace_graph();
861}
862#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100863static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400864 .func = function_profile_call,
Steven Rostedt493762f2009-03-23 17:12:36 -0400865};
866
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400867static int register_ftrace_profiler(void)
868{
869 return register_ftrace_function(&ftrace_profile_ops);
870}
871
872static void unregister_ftrace_profiler(void)
873{
874 unregister_ftrace_function(&ftrace_profile_ops);
875}
876#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
877
Steven Rostedt493762f2009-03-23 17:12:36 -0400878static ssize_t
879ftrace_profile_write(struct file *filp, const char __user *ubuf,
880 size_t cnt, loff_t *ppos)
881{
882 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400883 int ret;
884
Peter Huewe22fe9b52011-06-07 21:58:27 +0200885 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
886 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400887 return ret;
888
889 val = !!val;
890
891 mutex_lock(&ftrace_profile_lock);
892 if (ftrace_profile_enabled ^ val) {
893 if (val) {
894 ret = ftrace_profile_init();
895 if (ret < 0) {
896 cnt = ret;
897 goto out;
898 }
899
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400900 ret = register_ftrace_profiler();
901 if (ret < 0) {
902 cnt = ret;
903 goto out;
904 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400905 ftrace_profile_enabled = 1;
906 } else {
907 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400908 /*
909 * unregister_ftrace_profiler calls stop_machine
910 * so this acts like an synchronize_sched.
911 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400912 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400913 }
914 }
915 out:
916 mutex_unlock(&ftrace_profile_lock);
917
Jiri Olsacf8517c2009-10-23 19:36:16 -0400918 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400919
920 return cnt;
921}
922
923static ssize_t
924ftrace_profile_read(struct file *filp, char __user *ubuf,
925 size_t cnt, loff_t *ppos)
926{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400927 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400928 int r;
929
930 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
931 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
932}
933
934static const struct file_operations ftrace_profile_fops = {
935 .open = tracing_open_generic,
936 .read = ftrace_profile_read,
937 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200938 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -0400939};
940
Steven Rostedtcafb1682009-03-24 20:50:39 -0400941/* used to initialize the real stat files */
942static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400943 .name = "functions",
944 .stat_start = function_stat_start,
945 .stat_next = function_stat_next,
946 .stat_cmp = function_stat_cmp,
947 .stat_headers = function_stat_headers,
948 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -0400949};
950
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400951static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400952{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400953 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400954 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400955 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -0400956 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400957 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -0400958
Steven Rostedtcafb1682009-03-24 20:50:39 -0400959 for_each_possible_cpu(cpu) {
960 stat = &per_cpu(ftrace_profile_stats, cpu);
961
962 /* allocate enough for function name + cpu number */
963 name = kmalloc(32, GFP_KERNEL);
964 if (!name) {
965 /*
966 * The files created are permanent, if something happens
967 * we still do not free memory.
968 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400969 WARN(1,
970 "Could not allocate stat file for cpu %d\n",
971 cpu);
972 return;
973 }
974 stat->stat = function_stats;
975 snprintf(name, 32, "function%d", cpu);
976 stat->stat.name = name;
977 ret = register_stat_tracer(&stat->stat);
978 if (ret) {
979 WARN(1,
980 "Could not register function stat for cpu %d\n",
981 cpu);
982 kfree(name);
983 return;
984 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400985 }
986
987 entry = debugfs_create_file("function_profile_enabled", 0644,
988 d_tracer, NULL, &ftrace_profile_fops);
989 if (!entry)
990 pr_warning("Could not create debugfs "
991 "'function_profile_enabled' entry\n");
992}
993
994#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400995static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400996{
997}
998#endif /* CONFIG_FUNCTION_PROFILER */
999
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001000static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1001
Steven Rostedt3d083392008-05-12 21:20:42 +02001002#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001003
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001004#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001005# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001006#endif
1007
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001008static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1009
Steven Rostedtb6887d72009-02-17 12:32:04 -05001010struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001011 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -05001012 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001013 unsigned long flags;
1014 unsigned long ip;
1015 void *data;
1016 struct rcu_head rcu;
1017};
1018
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001019struct ftrace_func_entry {
1020 struct hlist_node hlist;
1021 unsigned long ip;
1022};
1023
1024struct ftrace_hash {
1025 unsigned long size_bits;
1026 struct hlist_head *buckets;
1027 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001028 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001029};
1030
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001031/*
1032 * We make these constant because no one should touch them,
1033 * but they are used as the default "empty hash", to avoid allocating
1034 * it all the time. These are in a read only section such that if
1035 * anyone does try to modify it, it will cause an exception.
1036 */
1037static const struct hlist_head empty_buckets[1];
1038static const struct ftrace_hash empty_hash = {
1039 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001040};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001041#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001042
Steven Rostedt2b499382011-05-03 22:49:52 -04001043static struct ftrace_ops global_ops = {
Steven Rostedtf45948e2011-05-02 12:29:25 -04001044 .func = ftrace_stub,
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001045 .notrace_hash = EMPTY_HASH,
1046 .filter_hash = EMPTY_HASH,
Steven Rostedtf45948e2011-05-02 12:29:25 -04001047};
1048
Steven Rostedt41c52c02008-05-22 11:46:33 -04001049static DEFINE_MUTEX(ftrace_regex_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02001050
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001051struct ftrace_page {
1052 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001053 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001054 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001055 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001056};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001057
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001058static struct ftrace_page *ftrace_new_pgs;
1059
Steven Rostedta7900872011-12-16 16:23:44 -05001060#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1061#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001062
1063/* estimate from running different kernels */
1064#define NR_TO_INIT 10000
1065
1066static struct ftrace_page *ftrace_pages_start;
1067static struct ftrace_page *ftrace_pages;
1068
Steven Rostedt06a51d92011-12-19 19:07:36 -05001069static bool ftrace_hash_empty(struct ftrace_hash *hash)
1070{
1071 return !hash || !hash->count;
1072}
1073
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001074static struct ftrace_func_entry *
1075ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1076{
1077 unsigned long key;
1078 struct ftrace_func_entry *entry;
1079 struct hlist_head *hhd;
1080 struct hlist_node *n;
1081
Steven Rostedt06a51d92011-12-19 19:07:36 -05001082 if (ftrace_hash_empty(hash))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001083 return NULL;
1084
1085 if (hash->size_bits > 0)
1086 key = hash_long(ip, hash->size_bits);
1087 else
1088 key = 0;
1089
1090 hhd = &hash->buckets[key];
1091
1092 hlist_for_each_entry_rcu(entry, n, hhd, hlist) {
1093 if (entry->ip == ip)
1094 return entry;
1095 }
1096 return NULL;
1097}
1098
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001099static void __add_hash_entry(struct ftrace_hash *hash,
1100 struct ftrace_func_entry *entry)
1101{
1102 struct hlist_head *hhd;
1103 unsigned long key;
1104
1105 if (hash->size_bits)
1106 key = hash_long(entry->ip, hash->size_bits);
1107 else
1108 key = 0;
1109
1110 hhd = &hash->buckets[key];
1111 hlist_add_head(&entry->hlist, hhd);
1112 hash->count++;
1113}
1114
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001115static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1116{
1117 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001118
1119 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1120 if (!entry)
1121 return -ENOMEM;
1122
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001123 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001124 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001125
1126 return 0;
1127}
1128
1129static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001130free_hash_entry(struct ftrace_hash *hash,
1131 struct ftrace_func_entry *entry)
1132{
1133 hlist_del(&entry->hlist);
1134 kfree(entry);
1135 hash->count--;
1136}
1137
1138static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001139remove_hash_entry(struct ftrace_hash *hash,
1140 struct ftrace_func_entry *entry)
1141{
1142 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001143 hash->count--;
1144}
1145
1146static void ftrace_hash_clear(struct ftrace_hash *hash)
1147{
1148 struct hlist_head *hhd;
1149 struct hlist_node *tp, *tn;
1150 struct ftrace_func_entry *entry;
1151 int size = 1 << hash->size_bits;
1152 int i;
1153
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001154 if (!hash->count)
1155 return;
1156
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001157 for (i = 0; i < size; i++) {
1158 hhd = &hash->buckets[i];
1159 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001160 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001161 }
1162 FTRACE_WARN_ON(hash->count);
1163}
1164
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001165static void free_ftrace_hash(struct ftrace_hash *hash)
1166{
1167 if (!hash || hash == EMPTY_HASH)
1168 return;
1169 ftrace_hash_clear(hash);
1170 kfree(hash->buckets);
1171 kfree(hash);
1172}
1173
Steven Rostedt07fd5512011-05-05 18:03:47 -04001174static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1175{
1176 struct ftrace_hash *hash;
1177
1178 hash = container_of(rcu, struct ftrace_hash, rcu);
1179 free_ftrace_hash(hash);
1180}
1181
1182static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1183{
1184 if (!hash || hash == EMPTY_HASH)
1185 return;
1186 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1187}
1188
Jiri Olsa5500fa52012-02-15 15:51:54 +01001189void ftrace_free_filter(struct ftrace_ops *ops)
1190{
1191 free_ftrace_hash(ops->filter_hash);
1192 free_ftrace_hash(ops->notrace_hash);
1193}
1194
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001195static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1196{
1197 struct ftrace_hash *hash;
1198 int size;
1199
1200 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1201 if (!hash)
1202 return NULL;
1203
1204 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001205 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001206
1207 if (!hash->buckets) {
1208 kfree(hash);
1209 return NULL;
1210 }
1211
1212 hash->size_bits = size_bits;
1213
1214 return hash;
1215}
1216
1217static struct ftrace_hash *
1218alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1219{
1220 struct ftrace_func_entry *entry;
1221 struct ftrace_hash *new_hash;
1222 struct hlist_node *tp;
1223 int size;
1224 int ret;
1225 int i;
1226
1227 new_hash = alloc_ftrace_hash(size_bits);
1228 if (!new_hash)
1229 return NULL;
1230
1231 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001232 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001233 return new_hash;
1234
1235 size = 1 << hash->size_bits;
1236 for (i = 0; i < size; i++) {
1237 hlist_for_each_entry(entry, tp, &hash->buckets[i], hlist) {
1238 ret = add_hash_entry(new_hash, entry->ip);
1239 if (ret < 0)
1240 goto free_hash;
1241 }
1242 }
1243
1244 FTRACE_WARN_ON(new_hash->count != hash->count);
1245
1246 return new_hash;
1247
1248 free_hash:
1249 free_ftrace_hash(new_hash);
1250 return NULL;
1251}
1252
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001253static void
1254ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1255static void
1256ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1257
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001258static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001259ftrace_hash_move(struct ftrace_ops *ops, int enable,
1260 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001261{
1262 struct ftrace_func_entry *entry;
1263 struct hlist_node *tp, *tn;
1264 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001265 struct ftrace_hash *old_hash;
1266 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001267 unsigned long key;
1268 int size = src->count;
1269 int bits = 0;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001270 int ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001271 int i;
1272
1273 /*
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001274 * Remove the current set, update the hash and add
1275 * them back.
1276 */
1277 ftrace_hash_rec_disable(ops, enable);
1278
1279 /*
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001280 * If the new source is empty, just free dst and assign it
1281 * the empty_hash.
1282 */
1283 if (!src->count) {
Steven Rostedt07fd5512011-05-05 18:03:47 -04001284 free_ftrace_hash_rcu(*dst);
1285 rcu_assign_pointer(*dst, EMPTY_HASH);
Steven Rostedtd4d34b92011-11-04 20:32:39 -04001286 /* still need to update the function records */
1287 ret = 0;
1288 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001289 }
1290
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001291 /*
1292 * Make the hash size about 1/2 the # found
1293 */
1294 for (size /= 2; size; size >>= 1)
1295 bits++;
1296
1297 /* Don't allocate too much */
1298 if (bits > FTRACE_HASH_MAX_BITS)
1299 bits = FTRACE_HASH_MAX_BITS;
1300
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001301 ret = -ENOMEM;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001302 new_hash = alloc_ftrace_hash(bits);
1303 if (!new_hash)
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001304 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001305
1306 size = 1 << src->size_bits;
1307 for (i = 0; i < size; i++) {
1308 hhd = &src->buckets[i];
1309 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist) {
1310 if (bits > 0)
1311 key = hash_long(entry->ip, bits);
1312 else
1313 key = 0;
1314 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001315 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001316 }
1317 }
1318
Steven Rostedt07fd5512011-05-05 18:03:47 -04001319 old_hash = *dst;
1320 rcu_assign_pointer(*dst, new_hash);
1321 free_ftrace_hash_rcu(old_hash);
1322
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001323 ret = 0;
1324 out:
1325 /*
1326 * Enable regardless of ret:
1327 * On success, we enable the new hash.
1328 * On failure, we re-enable the original hash.
1329 */
1330 ftrace_hash_rec_enable(ops, enable);
1331
1332 return ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001333}
1334
Steven Rostedt265c8312009-02-13 12:43:56 -05001335/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001336 * Test the hashes for this ops to see if we want to call
1337 * the ops->func or not.
1338 *
1339 * It's a match if the ip is in the ops->filter_hash or
1340 * the filter_hash does not exist or is empty,
1341 * AND
1342 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001343 *
1344 * This needs to be called with preemption disabled as
1345 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001346 */
1347static int
1348ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
1349{
1350 struct ftrace_hash *filter_hash;
1351 struct ftrace_hash *notrace_hash;
1352 int ret;
1353
Steven Rostedtb8489142011-05-04 09:27:52 -04001354 filter_hash = rcu_dereference_raw(ops->filter_hash);
1355 notrace_hash = rcu_dereference_raw(ops->notrace_hash);
1356
Steven Rostedt06a51d92011-12-19 19:07:36 -05001357 if ((ftrace_hash_empty(filter_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001358 ftrace_lookup_ip(filter_hash, ip)) &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001359 (ftrace_hash_empty(notrace_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001360 !ftrace_lookup_ip(notrace_hash, ip)))
1361 ret = 1;
1362 else
1363 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001364
1365 return ret;
1366}
1367
1368/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001369 * This is a double for. Do not use 'break' to break out of the loop,
1370 * you must use a goto.
1371 */
1372#define do_for_each_ftrace_rec(pg, rec) \
1373 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1374 int _____i; \
1375 for (_____i = 0; _____i < pg->index; _____i++) { \
1376 rec = &pg->records[_____i];
1377
1378#define while_for_each_ftrace_rec() \
1379 } \
1380 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301381
Steven Rostedt5855fea2011-12-16 19:27:42 -05001382
1383static int ftrace_cmp_recs(const void *a, const void *b)
1384{
1385 const struct dyn_ftrace *reca = a;
1386 const struct dyn_ftrace *recb = b;
1387
1388 if (reca->ip > recb->ip)
1389 return 1;
1390 if (reca->ip < recb->ip)
1391 return -1;
1392 return 0;
1393}
1394
Steven Rostedtc88fd862011-08-16 09:53:39 -04001395/**
1396 * ftrace_location - return true if the ip giving is a traced location
1397 * @ip: the instruction pointer to check
1398 *
1399 * Returns 1 if @ip given is a pointer to a ftrace location.
1400 * That is, the instruction that is either a NOP or call to
1401 * the function tracer. It checks the ftrace internal tables to
1402 * determine if the address belongs or not.
1403 */
1404int ftrace_location(unsigned long ip)
1405{
1406 struct ftrace_page *pg;
1407 struct dyn_ftrace *rec;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001408 struct dyn_ftrace key;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001409
Steven Rostedt5855fea2011-12-16 19:27:42 -05001410 key.ip = ip;
1411
1412 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1413 rec = bsearch(&key, pg->records, pg->index,
1414 sizeof(struct dyn_ftrace),
1415 ftrace_cmp_recs);
1416 if (rec)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001417 return 1;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001418 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04001419
1420 return 0;
1421}
1422
Steven Rostedted926f92011-05-03 13:25:24 -04001423static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1424 int filter_hash,
1425 bool inc)
1426{
1427 struct ftrace_hash *hash;
1428 struct ftrace_hash *other_hash;
1429 struct ftrace_page *pg;
1430 struct dyn_ftrace *rec;
1431 int count = 0;
1432 int all = 0;
1433
1434 /* Only update if the ops has been registered */
1435 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1436 return;
1437
1438 /*
1439 * In the filter_hash case:
1440 * If the count is zero, we update all records.
1441 * Otherwise we just update the items in the hash.
1442 *
1443 * In the notrace_hash case:
1444 * We enable the update in the hash.
1445 * As disabling notrace means enabling the tracing,
1446 * and enabling notrace means disabling, the inc variable
1447 * gets inversed.
1448 */
1449 if (filter_hash) {
1450 hash = ops->filter_hash;
1451 other_hash = ops->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001452 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001453 all = 1;
1454 } else {
1455 inc = !inc;
1456 hash = ops->notrace_hash;
1457 other_hash = ops->filter_hash;
1458 /*
1459 * If the notrace hash has no items,
1460 * then there's nothing to do.
1461 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001462 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001463 return;
1464 }
1465
1466 do_for_each_ftrace_rec(pg, rec) {
1467 int in_other_hash = 0;
1468 int in_hash = 0;
1469 int match = 0;
1470
1471 if (all) {
1472 /*
1473 * Only the filter_hash affects all records.
1474 * Update if the record is not in the notrace hash.
1475 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001476 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001477 match = 1;
1478 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001479 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1480 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001481
1482 /*
1483 *
1484 */
1485 if (filter_hash && in_hash && !in_other_hash)
1486 match = 1;
1487 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001488 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001489 match = 1;
1490 }
1491 if (!match)
1492 continue;
1493
1494 if (inc) {
1495 rec->flags++;
1496 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1497 return;
1498 } else {
1499 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1500 return;
1501 rec->flags--;
1502 }
1503 count++;
1504 /* Shortcut, if we handled all records, we are done. */
1505 if (!all && count == hash->count)
1506 return;
1507 } while_for_each_ftrace_rec();
1508}
1509
1510static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1511 int filter_hash)
1512{
1513 __ftrace_hash_rec_update(ops, filter_hash, 0);
1514}
1515
1516static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1517 int filter_hash)
1518{
1519 __ftrace_hash_rec_update(ops, filter_hash, 1);
1520}
1521
Ingo Molnare309b412008-05-12 21:20:51 +02001522static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001523{
Steven Rostedta7900872011-12-16 16:23:44 -05001524 if (ftrace_pages->index == ftrace_pages->size) {
1525 /* We should have allocated enough */
1526 if (WARN_ON(!ftrace_pages->next))
1527 return NULL;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001528 ftrace_pages = ftrace_pages->next;
1529 }
1530
1531 return &ftrace_pages->records[ftrace_pages->index++];
1532}
1533
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001534static struct dyn_ftrace *
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001535ftrace_record_ip(unsigned long ip)
Steven Rostedt3d083392008-05-12 21:20:42 +02001536{
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001537 struct dyn_ftrace *rec;
Steven Rostedt3d083392008-05-12 21:20:42 +02001538
Steven Rostedtf3c7ac42008-11-14 16:21:19 -08001539 if (ftrace_disabled)
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001540 return NULL;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001541
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001542 rec = ftrace_alloc_dyn_node(ip);
1543 if (!rec)
1544 return NULL;
Steven Rostedt3d083392008-05-12 21:20:42 +02001545
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001546 rec->ip = ip;
Steven Rostedt3d083392008-05-12 21:20:42 +02001547
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001548 return rec;
Steven Rostedt3d083392008-05-12 21:20:42 +02001549}
1550
Steven Rostedt05736a42008-09-22 14:55:47 -07001551static void print_ip_ins(const char *fmt, unsigned char *p)
1552{
1553 int i;
1554
1555 printk(KERN_CONT "%s", fmt);
1556
1557 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1558 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1559}
1560
Steven Rostedtc88fd862011-08-16 09:53:39 -04001561/**
1562 * ftrace_bug - report and shutdown function tracer
1563 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1564 * @ip: The address that failed
1565 *
1566 * The arch code that enables or disables the function tracing
1567 * can call ftrace_bug() when it has detected a problem in
1568 * modifying the code. @failed should be one of either:
1569 * EFAULT - if the problem happens on reading the @ip address
1570 * EINVAL - if what is read at @ip is not what was expected
1571 * EPERM - if the problem happens on writting to the @ip address
1572 */
1573void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001574{
1575 switch (failed) {
1576 case -EFAULT:
1577 FTRACE_WARN_ON_ONCE(1);
1578 pr_info("ftrace faulted on modifying ");
1579 print_ip_sym(ip);
1580 break;
1581 case -EINVAL:
1582 FTRACE_WARN_ON_ONCE(1);
1583 pr_info("ftrace failed to modify ");
1584 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001585 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001586 printk(KERN_CONT "\n");
1587 break;
1588 case -EPERM:
1589 FTRACE_WARN_ON_ONCE(1);
1590 pr_info("ftrace faulted on writing ");
1591 print_ip_sym(ip);
1592 break;
1593 default:
1594 FTRACE_WARN_ON_ONCE(1);
1595 pr_info("ftrace faulted on unknown error ");
1596 print_ip_sym(ip);
1597 }
1598}
1599
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001600
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -05001601/* Return 1 if the address range is reserved for ftrace */
1602int ftrace_text_reserved(void *start, void *end)
1603{
1604 struct dyn_ftrace *rec;
1605 struct ftrace_page *pg;
1606
1607 do_for_each_ftrace_rec(pg, rec) {
1608 if (rec->ip <= (unsigned long)end &&
1609 rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
1610 return 1;
1611 } while_for_each_ftrace_rec();
1612 return 0;
1613}
1614
Steven Rostedtc88fd862011-08-16 09:53:39 -04001615static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02001616{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001617 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001618
Steven Rostedt982c3502008-11-15 16:31:41 -05001619 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001620 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05001621 *
Steven Rostedted926f92011-05-03 13:25:24 -04001622 * If the record has a ref count, then we need to enable it
1623 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001624 *
Steven Rostedted926f92011-05-03 13:25:24 -04001625 * Otherwise we make sure its disabled.
1626 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001627 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04001628 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001629 */
Steven Rostedtc88fd862011-08-16 09:53:39 -04001630 if (enable && (rec->flags & ~FTRACE_FL_MASK))
Steven Rostedted926f92011-05-03 13:25:24 -04001631 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001632
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001633 /* If the state of this record hasn't changed, then do nothing */
1634 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001635 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001636
1637 if (flag) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04001638 if (update)
1639 rec->flags |= FTRACE_FL_ENABLED;
1640 return FTRACE_UPDATE_MAKE_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001641 }
1642
Steven Rostedtc88fd862011-08-16 09:53:39 -04001643 if (update)
1644 rec->flags &= ~FTRACE_FL_ENABLED;
1645
1646 return FTRACE_UPDATE_MAKE_NOP;
1647}
1648
1649/**
1650 * ftrace_update_record, set a record that now is tracing or not
1651 * @rec: the record to update
1652 * @enable: set to 1 if the record is tracing, zero to force disable
1653 *
1654 * The records that represent all functions that can be traced need
1655 * to be updated when tracing has been enabled.
1656 */
1657int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1658{
1659 return ftrace_check_record(rec, enable, 1);
1660}
1661
1662/**
1663 * ftrace_test_record, check if the record has been enabled or not
1664 * @rec: the record to test
1665 * @enable: set to 1 to check if enabled, 0 if it is disabled
1666 *
1667 * The arch code may need to test if a record is already set to
1668 * tracing to determine how to modify the function code that it
1669 * represents.
1670 */
1671int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1672{
1673 return ftrace_check_record(rec, enable, 0);
1674}
1675
1676static int
1677__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1678{
1679 unsigned long ftrace_addr;
1680 int ret;
1681
1682 ftrace_addr = (unsigned long)FTRACE_ADDR;
1683
1684 ret = ftrace_update_record(rec, enable);
1685
1686 switch (ret) {
1687 case FTRACE_UPDATE_IGNORE:
1688 return 0;
1689
1690 case FTRACE_UPDATE_MAKE_CALL:
1691 return ftrace_make_call(rec, ftrace_addr);
1692
1693 case FTRACE_UPDATE_MAKE_NOP:
1694 return ftrace_make_nop(NULL, rec, ftrace_addr);
1695 }
1696
1697 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02001698}
1699
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001700static void ftrace_replace_code(int update)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001701{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001702 struct dyn_ftrace *rec;
1703 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001704 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001705
Steven Rostedt45a4a232011-04-21 23:16:46 -04001706 if (unlikely(ftrace_disabled))
1707 return;
1708
Steven Rostedt265c8312009-02-13 12:43:56 -05001709 do_for_each_ftrace_rec(pg, rec) {
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001710 failed = __ftrace_replace_code(rec, update);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001711 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04001712 ftrace_bug(failed, rec->ip);
1713 /* Stop processing */
1714 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001715 }
1716 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001717}
1718
Steven Rostedtc88fd862011-08-16 09:53:39 -04001719struct ftrace_rec_iter {
1720 struct ftrace_page *pg;
1721 int index;
1722};
1723
1724/**
1725 * ftrace_rec_iter_start, start up iterating over traced functions
1726 *
1727 * Returns an iterator handle that is used to iterate over all
1728 * the records that represent address locations where functions
1729 * are traced.
1730 *
1731 * May return NULL if no records are available.
1732 */
1733struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1734{
1735 /*
1736 * We only use a single iterator.
1737 * Protected by the ftrace_lock mutex.
1738 */
1739 static struct ftrace_rec_iter ftrace_rec_iter;
1740 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1741
1742 iter->pg = ftrace_pages_start;
1743 iter->index = 0;
1744
1745 /* Could have empty pages */
1746 while (iter->pg && !iter->pg->index)
1747 iter->pg = iter->pg->next;
1748
1749 if (!iter->pg)
1750 return NULL;
1751
1752 return iter;
1753}
1754
1755/**
1756 * ftrace_rec_iter_next, get the next record to process.
1757 * @iter: The handle to the iterator.
1758 *
1759 * Returns the next iterator after the given iterator @iter.
1760 */
1761struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1762{
1763 iter->index++;
1764
1765 if (iter->index >= iter->pg->index) {
1766 iter->pg = iter->pg->next;
1767 iter->index = 0;
1768
1769 /* Could have empty pages */
1770 while (iter->pg && !iter->pg->index)
1771 iter->pg = iter->pg->next;
1772 }
1773
1774 if (!iter->pg)
1775 return NULL;
1776
1777 return iter;
1778}
1779
1780/**
1781 * ftrace_rec_iter_record, get the record at the iterator location
1782 * @iter: The current iterator location
1783 *
1784 * Returns the record that the current @iter is at.
1785 */
1786struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1787{
1788 return &iter->pg->records[iter->index];
1789}
1790
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301791static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001792ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001793{
1794 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001795 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001796
1797 ip = rec->ip;
1798
Steven Rostedt45a4a232011-04-21 23:16:46 -04001799 if (unlikely(ftrace_disabled))
1800 return 0;
1801
Shaohua Li25aac9d2009-01-09 11:29:40 +08001802 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001803 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001804 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301805 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02001806 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301807 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001808}
1809
Steven Rostedt000ab692009-02-17 13:35:06 -05001810/*
1811 * archs can override this function if they must do something
1812 * before the modifying code is performed.
1813 */
1814int __weak ftrace_arch_code_modify_prepare(void)
1815{
1816 return 0;
1817}
1818
1819/*
1820 * archs can override this function if they must do something
1821 * after the modifying code is performed.
1822 */
1823int __weak ftrace_arch_code_modify_post_process(void)
1824{
1825 return 0;
1826}
1827
Ingo Molnare309b412008-05-12 21:20:51 +02001828static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02001829{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001830 int *command = data;
1831
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001832 if (*command & FTRACE_UPDATE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001833 ftrace_replace_code(1);
Steven Rostedta3583242008-11-11 15:01:42 -05001834 else if (*command & FTRACE_DISABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001835 ftrace_replace_code(0);
1836
1837 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1838 ftrace_update_ftrace_func(ftrace_trace_function);
1839
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001840 if (*command & FTRACE_START_FUNC_RET)
1841 ftrace_enable_ftrace_graph_caller();
1842 else if (*command & FTRACE_STOP_FUNC_RET)
1843 ftrace_disable_ftrace_graph_caller();
1844
Steven Rostedtc88fd862011-08-16 09:53:39 -04001845 return 0;
1846}
1847
1848/**
1849 * ftrace_run_stop_machine, go back to the stop machine method
1850 * @command: The command to tell ftrace what to do
1851 *
1852 * If an arch needs to fall back to the stop machine method, the
1853 * it can call this function.
1854 */
1855void ftrace_run_stop_machine(int command)
1856{
1857 stop_machine(__ftrace_modify_code, &command, NULL);
1858}
1859
1860/**
1861 * arch_ftrace_update_code, modify the code to trace or not trace
1862 * @command: The command that needs to be done
1863 *
1864 * Archs can override this function if it does not need to
1865 * run stop_machine() to modify code.
1866 */
1867void __weak arch_ftrace_update_code(int command)
1868{
1869 ftrace_run_stop_machine(command);
1870}
1871
1872static void ftrace_run_update_code(int command)
1873{
1874 int ret;
1875
1876 ret = ftrace_arch_code_modify_prepare();
1877 FTRACE_WARN_ON(ret);
1878 if (ret)
1879 return;
1880 /*
1881 * Do not call function tracer while we update the code.
1882 * We are in stop machine.
1883 */
1884 function_trace_stop++;
1885
1886 /*
1887 * By default we use stop_machine() to modify the code.
1888 * But archs can do what ever they want as long as it
1889 * is safe. The stop_machine() is the safest, but also
1890 * produces the most overhead.
1891 */
1892 arch_ftrace_update_code(command);
1893
Steven Rostedt6331c282011-07-13 15:11:02 -04001894#ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
1895 /*
1896 * For archs that call ftrace_test_stop_func(), we must
1897 * wait till after we update all the function callers
1898 * before we update the callback. This keeps different
1899 * ops that record different functions from corrupting
1900 * each other.
1901 */
1902 __ftrace_trace_function = __ftrace_trace_function_delay;
1903#endif
1904 function_trace_stop--;
1905
Steven Rostedt000ab692009-02-17 13:35:06 -05001906 ret = ftrace_arch_code_modify_post_process();
1907 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02001908}
1909
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001910static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001911static int ftrace_start_up;
Steven Rostedtb8489142011-05-04 09:27:52 -04001912static int global_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001913
1914static void ftrace_startup_enable(int command)
1915{
1916 if (saved_ftrace_func != ftrace_trace_function) {
1917 saved_ftrace_func = ftrace_trace_function;
1918 command |= FTRACE_UPDATE_TRACE_FUNC;
1919 }
1920
1921 if (!command || !ftrace_enabled)
1922 return;
1923
1924 ftrace_run_update_code(command);
1925}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001926
Steven Rostedta1cd6172011-05-23 15:24:25 -04001927static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001928{
Steven Rostedtb8489142011-05-04 09:27:52 -04001929 bool hash_enable = true;
1930
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001931 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04001932 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001933
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001934 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001935 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02001936
Steven Rostedtb8489142011-05-04 09:27:52 -04001937 /* ops marked global share the filter hashes */
1938 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
1939 ops = &global_ops;
1940 /* Don't update hash if global is already set */
1941 if (global_start_up)
1942 hash_enable = false;
1943 global_start_up++;
1944 }
1945
Steven Rostedted926f92011-05-03 13:25:24 -04001946 ops->flags |= FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04001947 if (hash_enable)
Steven Rostedted926f92011-05-03 13:25:24 -04001948 ftrace_hash_rec_enable(ops, 1);
1949
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001950 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04001951
1952 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02001953}
1954
Steven Rostedtbd69c302011-05-03 21:55:54 -04001955static void ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001956{
Steven Rostedtb8489142011-05-04 09:27:52 -04001957 bool hash_disable = true;
1958
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001959 if (unlikely(ftrace_disabled))
1960 return;
1961
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001962 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02001963 /*
1964 * Just warn in case of unbalance, no need to kill ftrace, it's not
1965 * critical but the ftrace_call callers may be never nopped again after
1966 * further ftrace uses.
1967 */
1968 WARN_ON_ONCE(ftrace_start_up < 0);
1969
Steven Rostedtb8489142011-05-04 09:27:52 -04001970 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
1971 ops = &global_ops;
1972 global_start_up--;
1973 WARN_ON_ONCE(global_start_up < 0);
1974 /* Don't update hash if global still has users */
1975 if (global_start_up) {
1976 WARN_ON_ONCE(!ftrace_start_up);
1977 hash_disable = false;
1978 }
1979 }
1980
1981 if (hash_disable)
Steven Rostedted926f92011-05-03 13:25:24 -04001982 ftrace_hash_rec_disable(ops, 1);
1983
Steven Rostedtb8489142011-05-04 09:27:52 -04001984 if (ops != &global_ops || !global_start_up)
Steven Rostedted926f92011-05-03 13:25:24 -04001985 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04001986
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001987 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001988
1989 if (saved_ftrace_func != ftrace_trace_function) {
1990 saved_ftrace_func = ftrace_trace_function;
1991 command |= FTRACE_UPDATE_TRACE_FUNC;
1992 }
1993
1994 if (!command || !ftrace_enabled)
Steven Rostedte6ea44e2009-02-14 01:42:44 -05001995 return;
Steven Rostedt3d083392008-05-12 21:20:42 +02001996
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001997 ftrace_run_update_code(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02001998}
1999
Ingo Molnare309b412008-05-12 21:20:51 +02002000static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002001{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002002 if (unlikely(ftrace_disabled))
2003 return;
2004
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002005 /* Force update next time */
2006 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002007 /* ftrace_start_up is true if we want ftrace running */
2008 if (ftrace_start_up)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002009 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002010}
2011
Ingo Molnare309b412008-05-12 21:20:51 +02002012static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002013{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002014 if (unlikely(ftrace_disabled))
2015 return;
2016
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002017 /* ftrace_start_up is true if ftrace is running */
2018 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04002019 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002020}
2021
Steven Rostedt3d083392008-05-12 21:20:42 +02002022static cycle_t ftrace_update_time;
2023static unsigned long ftrace_update_cnt;
2024unsigned long ftrace_update_tot_cnt;
2025
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002026static int ops_traces_mod(struct ftrace_ops *ops)
2027{
2028 struct ftrace_hash *hash;
2029
2030 hash = ops->filter_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05002031 return ftrace_hash_empty(hash);
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002032}
2033
Steven Rostedt31e88902008-11-14 16:21:19 -08002034static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02002035{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002036 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002037 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302038 cycle_t start, stop;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002039 unsigned long ref = 0;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002040 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002041
2042 /*
2043 * When adding a module, we need to check if tracers are
2044 * currently enabled and if they are set to trace all functions.
2045 * If they are, we need to enable the module functions as well
2046 * as update the reference counts for those function records.
2047 */
2048 if (mod) {
2049 struct ftrace_ops *ops;
2050
2051 for (ops = ftrace_ops_list;
2052 ops != &ftrace_list_end; ops = ops->next) {
2053 if (ops->flags & FTRACE_OPS_FL_ENABLED &&
2054 ops_traces_mod(ops))
2055 ref++;
2056 }
2057 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002058
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002059 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002060 ftrace_update_cnt = 0;
2061
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002062 for (pg = ftrace_new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302063
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002064 for (i = 0; i < pg->index; i++) {
2065 /* If something went wrong, bail without enabling anything */
2066 if (unlikely(ftrace_disabled))
2067 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002068
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002069 p = &pg->records[i];
2070 p->flags = ref;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302071
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002072 /*
2073 * Do the initial record conversion from mcount jump
2074 * to the NOP instructions.
2075 */
2076 if (!ftrace_code_disable(mod, p))
2077 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002078
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002079 ftrace_update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002080
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002081 /*
2082 * If the tracing is enabled, go ahead and enable the record.
2083 *
2084 * The reason not to enable the record immediatelly is the
2085 * inherent check of ftrace_make_nop/ftrace_make_call for
2086 * correct previous instructions. Making first the NOP
2087 * conversion puts the module to the correct state, thus
2088 * passing the ftrace_make_call check.
2089 */
2090 if (ftrace_start_up && ref) {
2091 int failed = __ftrace_replace_code(p, 1);
2092 if (failed)
2093 ftrace_bug(failed, p->ip);
2094 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002095 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002096 }
2097
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002098 ftrace_new_pgs = NULL;
2099
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002100 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002101 ftrace_update_time = stop - start;
2102 ftrace_update_tot_cnt += ftrace_update_cnt;
2103
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002104 return 0;
2105}
2106
Steven Rostedta7900872011-12-16 16:23:44 -05002107static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002108{
Steven Rostedta7900872011-12-16 16:23:44 -05002109 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002110 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002111
Steven Rostedta7900872011-12-16 16:23:44 -05002112 if (WARN_ON(!count))
2113 return -EINVAL;
2114
2115 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002116
2117 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002118 * We want to fill as much as possible. No more than a page
2119 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002120 */
Steven Rostedta7900872011-12-16 16:23:44 -05002121 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2122 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002123
Steven Rostedta7900872011-12-16 16:23:44 -05002124 again:
2125 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2126
2127 if (!pg->records) {
2128 /* if we can't allocate this size, try something smaller */
2129 if (!order)
2130 return -ENOMEM;
2131 order >>= 1;
2132 goto again;
2133 }
2134
2135 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2136 pg->size = cnt;
2137
2138 if (cnt > count)
2139 cnt = count;
2140
2141 return cnt;
2142}
2143
2144static struct ftrace_page *
2145ftrace_allocate_pages(unsigned long num_to_init)
2146{
2147 struct ftrace_page *start_pg;
2148 struct ftrace_page *pg;
2149 int order;
2150 int cnt;
2151
2152 if (!num_to_init)
2153 return 0;
2154
2155 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2156 if (!pg)
2157 return NULL;
2158
2159 /*
2160 * Try to allocate as much as possible in one continues
2161 * location that fills in all of the space. We want to
2162 * waste as little space as possible.
2163 */
2164 for (;;) {
2165 cnt = ftrace_allocate_records(pg, num_to_init);
2166 if (cnt < 0)
2167 goto free_pages;
2168
2169 num_to_init -= cnt;
2170 if (!num_to_init)
2171 break;
2172
2173 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2174 if (!pg->next)
2175 goto free_pages;
2176
2177 pg = pg->next;
2178 }
2179
2180 return start_pg;
2181
2182 free_pages:
2183 while (start_pg) {
2184 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2185 free_pages((unsigned long)pg->records, order);
2186 start_pg = pg->next;
2187 kfree(pg);
2188 pg = start_pg;
2189 }
2190 pr_info("ftrace: FAILED to allocate memory for functions\n");
2191 return NULL;
2192}
2193
2194static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2195{
2196 int cnt;
2197
2198 if (!num_to_init) {
2199 pr_info("ftrace: No functions to be traced?\n");
2200 return -1;
2201 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002202
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002203 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04002204 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08002205 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002206
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002207 return 0;
2208}
2209
Steven Rostedt5072c592008-05-12 21:20:43 +02002210#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2211
2212struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002213 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002214 loff_t func_pos;
2215 struct ftrace_page *pg;
2216 struct dyn_ftrace *func;
2217 struct ftrace_func_probe *probe;
2218 struct trace_parser parser;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002219 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002220 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002221 int hidx;
2222 int idx;
2223 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002224};
2225
Ingo Molnare309b412008-05-12 21:20:51 +02002226static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002227t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002228{
2229 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002230 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002231 struct hlist_head *hhd;
2232
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002233 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002234 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002235
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002236 if (iter->probe)
2237 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002238 retry:
2239 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2240 return NULL;
2241
2242 hhd = &ftrace_func_hash[iter->hidx];
2243
2244 if (hlist_empty(hhd)) {
2245 iter->hidx++;
2246 hnd = NULL;
2247 goto retry;
2248 }
2249
2250 if (!hnd)
2251 hnd = hhd->first;
2252 else {
2253 hnd = hnd->next;
2254 if (!hnd) {
2255 iter->hidx++;
2256 goto retry;
2257 }
2258 }
2259
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002260 if (WARN_ON_ONCE(!hnd))
2261 return NULL;
2262
2263 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2264
2265 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002266}
2267
2268static void *t_hash_start(struct seq_file *m, loff_t *pos)
2269{
2270 struct ftrace_iterator *iter = m->private;
2271 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08002272 loff_t l;
2273
Steven Rostedt69a30832011-12-19 15:21:16 -05002274 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2275 return NULL;
2276
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002277 if (iter->func_pos > *pos)
2278 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002279
Li Zefand82d6242009-06-24 09:54:54 +08002280 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002281 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002282 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08002283 if (!p)
2284 break;
2285 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002286 if (!p)
2287 return NULL;
2288
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002289 /* Only set this if we have an item */
2290 iter->flags |= FTRACE_ITER_HASH;
2291
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002292 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002293}
2294
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002295static int
2296t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002297{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002298 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002299
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002300 rec = iter->probe;
2301 if (WARN_ON_ONCE(!rec))
2302 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002303
Steven Rostedt809dcf22009-02-16 23:06:01 -05002304 if (rec->ops->print)
2305 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2306
Steven Rostedtb375a112009-09-17 00:05:58 -04002307 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002308
2309 if (rec->data)
2310 seq_printf(m, ":%p", rec->data);
2311 seq_putc(m, '\n');
2312
2313 return 0;
2314}
2315
2316static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02002317t_next(struct seq_file *m, void *v, loff_t *pos)
2318{
2319 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002320 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002321 struct dyn_ftrace *rec = NULL;
2322
Steven Rostedt45a4a232011-04-21 23:16:46 -04002323 if (unlikely(ftrace_disabled))
2324 return NULL;
2325
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002326 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002327 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002328
Steven Rostedt5072c592008-05-12 21:20:43 +02002329 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01002330 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02002331
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002332 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002333 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002334
Steven Rostedt5072c592008-05-12 21:20:43 +02002335 retry:
2336 if (iter->idx >= iter->pg->index) {
2337 if (iter->pg->next) {
2338 iter->pg = iter->pg->next;
2339 iter->idx = 0;
2340 goto retry;
2341 }
2342 } else {
2343 rec = &iter->pg->records[iter->idx++];
Steven Rostedt32082302011-12-16 14:42:37 -05002344 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedtf45948e2011-05-02 12:29:25 -04002345 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
Steven Rostedt0183fb12008-11-07 22:36:02 -05002346
Steven Rostedt41c52c02008-05-22 11:46:33 -04002347 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt647bcd02011-05-03 14:39:21 -04002348 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2349
2350 ((iter->flags & FTRACE_ITER_ENABLED) &&
2351 !(rec->flags & ~FTRACE_FL_MASK))) {
2352
Steven Rostedt5072c592008-05-12 21:20:43 +02002353 rec = NULL;
2354 goto retry;
2355 }
2356 }
2357
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002358 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002359 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002360
2361 iter->func = rec;
2362
2363 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002364}
2365
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002366static void reset_iter_read(struct ftrace_iterator *iter)
2367{
2368 iter->pos = 0;
2369 iter->func_pos = 0;
2370 iter->flags &= ~(FTRACE_ITER_PRINTALL & FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002371}
2372
2373static void *t_start(struct seq_file *m, loff_t *pos)
2374{
2375 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002376 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002377 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002378 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002379
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002380 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002381
2382 if (unlikely(ftrace_disabled))
2383 return NULL;
2384
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002385 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002386 * If an lseek was done, then reset and start from beginning.
2387 */
2388 if (*pos < iter->pos)
2389 reset_iter_read(iter);
2390
2391 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002392 * For set_ftrace_filter reading, if we have the filter
2393 * off, we can short cut and just print out that all
2394 * functions are enabled.
2395 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05002396 if (iter->flags & FTRACE_ITER_FILTER &&
2397 ftrace_hash_empty(ops->filter_hash)) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002398 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002399 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002400 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002401 /* reset in case of seek/pread */
2402 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002403 return iter;
2404 }
2405
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002406 if (iter->flags & FTRACE_ITER_HASH)
2407 return t_hash_start(m, pos);
2408
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002409 /*
2410 * Unfortunately, we need to restart at ftrace_pages_start
2411 * every time we let go of the ftrace_mutex. This is because
2412 * those pointers can change without the lock.
2413 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002414 iter->pg = ftrace_pages_start;
2415 iter->idx = 0;
2416 for (l = 0; l <= *pos; ) {
2417 p = t_next(m, p, &l);
2418 if (!p)
2419 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002420 }
walimis5821e1b2008-11-15 15:19:06 +08002421
Steven Rostedt69a30832011-12-19 15:21:16 -05002422 if (!p)
2423 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002424
2425 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002426}
2427
2428static void t_stop(struct seq_file *m, void *p)
2429{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002430 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002431}
2432
2433static int t_show(struct seq_file *m, void *v)
2434{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002435 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002436 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02002437
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002438 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002439 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002440
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002441 if (iter->flags & FTRACE_ITER_PRINTALL) {
2442 seq_printf(m, "#### all functions enabled ####\n");
2443 return 0;
2444 }
2445
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002446 rec = iter->func;
2447
Steven Rostedt5072c592008-05-12 21:20:43 +02002448 if (!rec)
2449 return 0;
2450
Steven Rostedt647bcd02011-05-03 14:39:21 -04002451 seq_printf(m, "%ps", (void *)rec->ip);
2452 if (iter->flags & FTRACE_ITER_ENABLED)
2453 seq_printf(m, " (%ld)",
2454 rec->flags & ~FTRACE_FL_MASK);
2455 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02002456
2457 return 0;
2458}
2459
James Morris88e9d342009-09-22 16:43:43 -07002460static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002461 .start = t_start,
2462 .next = t_next,
2463 .stop = t_stop,
2464 .show = t_show,
2465};
2466
Ingo Molnare309b412008-05-12 21:20:51 +02002467static int
Steven Rostedt5072c592008-05-12 21:20:43 +02002468ftrace_avail_open(struct inode *inode, struct file *file)
2469{
2470 struct ftrace_iterator *iter;
2471 int ret;
2472
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002473 if (unlikely(ftrace_disabled))
2474 return -ENODEV;
2475
Steven Rostedt5072c592008-05-12 21:20:43 +02002476 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2477 if (!iter)
2478 return -ENOMEM;
2479
2480 iter->pg = ftrace_pages_start;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002481 iter->ops = &global_ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002482
2483 ret = seq_open(file, &show_ftrace_seq_ops);
2484 if (!ret) {
2485 struct seq_file *m = file->private_data;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002486
Steven Rostedt5072c592008-05-12 21:20:43 +02002487 m->private = iter;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002488 } else {
Steven Rostedt5072c592008-05-12 21:20:43 +02002489 kfree(iter);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002490 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002491
2492 return ret;
2493}
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;
2499 int ret;
2500
2501 if (unlikely(ftrace_disabled))
2502 return -ENODEV;
2503
2504 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2505 if (!iter)
2506 return -ENOMEM;
2507
2508 iter->pg = ftrace_pages_start;
2509 iter->flags = FTRACE_ITER_ENABLED;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002510 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002511
2512 ret = seq_open(file, &show_ftrace_seq_ops);
2513 if (!ret) {
2514 struct seq_file *m = file->private_data;
2515
2516 m->private = iter;
2517 } else {
2518 kfree(iter);
2519 }
2520
2521 return ret;
2522}
2523
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002524static void ftrace_filter_reset(struct ftrace_hash *hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02002525{
Steven Rostedt52baf112009-02-14 01:15:39 -05002526 mutex_lock(&ftrace_lock);
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002527 ftrace_hash_clear(hash);
Steven Rostedt52baf112009-02-14 01:15:39 -05002528 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002529}
2530
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002531/**
2532 * ftrace_regex_open - initialize function tracer filter files
2533 * @ops: The ftrace_ops that hold the hash filters
2534 * @flag: The type of filter to process
2535 * @inode: The inode, usually passed in to your open routine
2536 * @file: The file, usually passed in to your open routine
2537 *
2538 * ftrace_regex_open() initializes the filter files for the
2539 * @ops. Depending on @flag it may process the filter hash or
2540 * the notrace hash of @ops. With this called from the open
2541 * routine, you can use ftrace_filter_write() for the write
2542 * routine if @flag has FTRACE_ITER_FILTER set, or
2543 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
2544 * ftrace_regex_lseek() should be used as the lseek routine, and
2545 * release must call ftrace_regex_release().
2546 */
2547int
Steven Rostedtf45948e2011-05-02 12:29:25 -04002548ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002549 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02002550{
2551 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002552 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02002553 int ret = 0;
2554
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002555 if (unlikely(ftrace_disabled))
2556 return -ENODEV;
2557
Steven Rostedt5072c592008-05-12 21:20:43 +02002558 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2559 if (!iter)
2560 return -ENOMEM;
2561
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002562 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2563 kfree(iter);
2564 return -ENOMEM;
2565 }
2566
Steven Rostedtf45948e2011-05-02 12:29:25 -04002567 if (flag & FTRACE_ITER_NOTRACE)
2568 hash = ops->notrace_hash;
2569 else
2570 hash = ops->filter_hash;
2571
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002572 iter->ops = ops;
2573 iter->flags = flag;
2574
2575 if (file->f_mode & FMODE_WRITE) {
2576 mutex_lock(&ftrace_lock);
2577 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2578 mutex_unlock(&ftrace_lock);
2579
2580 if (!iter->hash) {
2581 trace_parser_put(&iter->parser);
2582 kfree(iter);
2583 return -ENOMEM;
2584 }
2585 }
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002586
Steven Rostedt41c52c02008-05-22 11:46:33 -04002587 mutex_lock(&ftrace_regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002588
Steven Rostedt5072c592008-05-12 21:20:43 +02002589 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002590 (file->f_flags & O_TRUNC))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002591 ftrace_filter_reset(iter->hash);
Steven Rostedt5072c592008-05-12 21:20:43 +02002592
2593 if (file->f_mode & FMODE_READ) {
2594 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02002595
2596 ret = seq_open(file, &show_ftrace_seq_ops);
2597 if (!ret) {
2598 struct seq_file *m = file->private_data;
2599 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08002600 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002601 /* Failed */
2602 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08002603 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002604 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08002605 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002606 } else
2607 file->private_data = iter;
Steven Rostedt41c52c02008-05-22 11:46:33 -04002608 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002609
2610 return ret;
2611}
2612
Steven Rostedt41c52c02008-05-22 11:46:33 -04002613static int
2614ftrace_filter_open(struct inode *inode, struct file *file)
2615{
Steven Rostedt69a30832011-12-19 15:21:16 -05002616 return ftrace_regex_open(&global_ops,
2617 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2618 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002619}
2620
2621static int
2622ftrace_notrace_open(struct inode *inode, struct file *file)
2623{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002624 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002625 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002626}
2627
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002628loff_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04002629ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
Steven Rostedt5072c592008-05-12 21:20:43 +02002630{
2631 loff_t ret;
2632
2633 if (file->f_mode & FMODE_READ)
2634 ret = seq_lseek(file, offset, origin);
2635 else
2636 file->f_pos = ret = 1;
2637
2638 return ret;
2639}
2640
Steven Rostedt64e7c442009-02-13 17:08:48 -05002641static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002642{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002643 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08002644 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002645
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002646 switch (type) {
2647 case MATCH_FULL:
2648 if (strcmp(str, regex) == 0)
2649 matched = 1;
2650 break;
2651 case MATCH_FRONT_ONLY:
2652 if (strncmp(str, regex, len) == 0)
2653 matched = 1;
2654 break;
2655 case MATCH_MIDDLE_ONLY:
2656 if (strstr(str, regex))
2657 matched = 1;
2658 break;
2659 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08002660 slen = strlen(str);
2661 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002662 matched = 1;
2663 break;
2664 }
2665
2666 return matched;
2667}
2668
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002669static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002670enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04002671{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002672 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002673 int ret = 0;
2674
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002675 entry = ftrace_lookup_ip(hash, rec->ip);
2676 if (not) {
2677 /* Do nothing if it doesn't exist */
2678 if (!entry)
2679 return 0;
2680
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002681 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002682 } else {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002683 /* Do nothing if it exists */
2684 if (entry)
2685 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002686
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002687 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002688 }
2689 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04002690}
2691
Steven Rostedt64e7c442009-02-13 17:08:48 -05002692static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002693ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2694 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002695{
2696 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002697 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002698
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002699 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2700
2701 if (mod) {
2702 /* module lookup requires matching the module */
2703 if (!modname || strcmp(modname, mod))
2704 return 0;
2705
2706 /* blank search means to match all funcs in the mod */
2707 if (!len)
2708 return 1;
2709 }
2710
Steven Rostedt64e7c442009-02-13 17:08:48 -05002711 return ftrace_match(str, regex, len, type);
2712}
2713
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002714static int
2715match_records(struct ftrace_hash *hash, char *buff,
2716 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002717{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002718 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002719 struct ftrace_page *pg;
2720 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002721 int type = MATCH_FULL;
2722 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08002723 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002724 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002725
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002726 if (len) {
2727 type = filter_parse_regex(buff, len, &search, &not);
2728 search_len = strlen(search);
2729 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002730
Steven Rostedt52baf112009-02-14 01:15:39 -05002731 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002732
2733 if (unlikely(ftrace_disabled))
2734 goto out_unlock;
2735
Steven Rostedt265c8312009-02-13 12:43:56 -05002736 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002737 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002738 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002739 if (ret < 0) {
2740 found = ret;
2741 goto out_unlock;
2742 }
Li Zefan311d16d2009-12-08 11:15:11 +08002743 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05002744 }
2745 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002746 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05002747 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08002748
2749 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02002750}
2751
Steven Rostedt64e7c442009-02-13 17:08:48 -05002752static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002753ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002754{
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002755 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002756}
2757
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002758static int
2759ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002760{
Steven Rostedt64e7c442009-02-13 17:08:48 -05002761 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002762
Steven Rostedt64e7c442009-02-13 17:08:48 -05002763 /* blank or '*' mean the same */
2764 if (strcmp(buff, "*") == 0)
2765 buff[0] = 0;
2766
2767 /* handle the case of 'dont filter this module' */
2768 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2769 buff[0] = 0;
2770 not = 1;
2771 }
2772
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002773 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002774}
2775
Steven Rostedtf6180772009-02-14 00:40:25 -05002776/*
2777 * We register the module command as a template to show others how
2778 * to register the a command as well.
2779 */
2780
2781static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04002782ftrace_mod_callback(struct ftrace_hash *hash,
2783 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05002784{
2785 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002786 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05002787
2788 /*
2789 * cmd == 'mod' because we only registered this func
2790 * for the 'mod' ftrace_func_command.
2791 * But if you register one func with multiple commands,
2792 * you can tell which command was used by the cmd
2793 * parameter.
2794 */
2795
2796 /* we must have a module name */
2797 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002798 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002799
2800 mod = strsep(&param, ":");
2801 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002802 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002803
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002804 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002805 if (!ret)
2806 ret = -EINVAL;
2807 if (ret < 0)
2808 return ret;
2809
2810 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05002811}
2812
2813static struct ftrace_func_command ftrace_mod_cmd = {
2814 .name = "mod",
2815 .func = ftrace_mod_callback,
2816};
2817
2818static int __init ftrace_mod_cmd_init(void)
2819{
2820 return register_ftrace_command(&ftrace_mod_cmd);
2821}
2822device_initcall(ftrace_mod_cmd_init);
2823
Steven Rostedt59df055f2009-02-14 15:29:06 -05002824static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002825function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002826{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002827 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002828 struct hlist_head *hhd;
2829 struct hlist_node *n;
2830 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002831
2832 key = hash_long(ip, FTRACE_HASH_BITS);
2833
2834 hhd = &ftrace_func_hash[key];
2835
2836 if (hlist_empty(hhd))
2837 return;
2838
2839 /*
2840 * Disable preemption for these calls to prevent a RCU grace
2841 * period. This syncs the hash iteration and freeing of items
2842 * on the hash. rcu_read_lock is too dangerous here.
2843 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04002844 preempt_disable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002845 hlist_for_each_entry_rcu(entry, n, hhd, node) {
2846 if (entry->ip == ip)
2847 entry->ops->func(ip, parent_ip, &entry->data);
2848 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04002849 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002850}
2851
Steven Rostedtb6887d72009-02-17 12:32:04 -05002852static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05002853{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04002854 .func = function_trace_probe_call,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002855};
2856
Steven Rostedtb6887d72009-02-17 12:32:04 -05002857static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002858
Steven Rostedtb6887d72009-02-17 12:32:04 -05002859static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002860{
Steven Rostedtb8489142011-05-04 09:27:52 -04002861 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002862 int i;
2863
Steven Rostedtb6887d72009-02-17 12:32:04 -05002864 if (ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002865 return;
2866
2867 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2868 struct hlist_head *hhd = &ftrace_func_hash[i];
2869 if (hhd->first)
2870 break;
2871 }
2872 /* Nothing registered? */
2873 if (i == FTRACE_FUNC_HASHSIZE)
2874 return;
2875
Steven Rostedtb8489142011-05-04 09:27:52 -04002876 ret = __register_ftrace_function(&trace_probe_ops);
2877 if (!ret)
Steven Rostedta1cd6172011-05-23 15:24:25 -04002878 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04002879
Steven Rostedtb6887d72009-02-17 12:32:04 -05002880 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002881}
2882
Steven Rostedtb6887d72009-02-17 12:32:04 -05002883static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002884{
Steven Rostedtb8489142011-05-04 09:27:52 -04002885 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002886 int i;
2887
Steven Rostedtb6887d72009-02-17 12:32:04 -05002888 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002889 return;
2890
2891 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2892 struct hlist_head *hhd = &ftrace_func_hash[i];
2893 if (hhd->first)
2894 return;
2895 }
2896
2897 /* no more funcs left */
Steven Rostedtb8489142011-05-04 09:27:52 -04002898 ret = __unregister_ftrace_function(&trace_probe_ops);
2899 if (!ret)
2900 ftrace_shutdown(&trace_probe_ops, 0);
2901
Steven Rostedtb6887d72009-02-17 12:32:04 -05002902 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002903}
2904
2905
2906static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2907{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002908 struct ftrace_func_probe *entry =
2909 container_of(rhp, struct ftrace_func_probe, rcu);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002910
2911 if (entry->ops->free)
2912 entry->ops->free(&entry->data);
2913 kfree(entry);
2914}
2915
2916
2917int
Steven Rostedtb6887d72009-02-17 12:32:04 -05002918register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002919 void *data)
2920{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002921 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002922 struct ftrace_page *pg;
2923 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002924 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002925 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002926 int count = 0;
2927 char *search;
2928
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002929 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002930 len = strlen(search);
2931
Steven Rostedtb6887d72009-02-17 12:32:04 -05002932 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002933 if (WARN_ON(not))
2934 return -EINVAL;
2935
2936 mutex_lock(&ftrace_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002937
Steven Rostedt45a4a232011-04-21 23:16:46 -04002938 if (unlikely(ftrace_disabled))
2939 goto out_unlock;
2940
Steven Rostedt59df055f2009-02-14 15:29:06 -05002941 do_for_each_ftrace_rec(pg, rec) {
2942
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002943 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05002944 continue;
2945
2946 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2947 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002948 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002949 if (!count)
2950 count = -ENOMEM;
2951 goto out_unlock;
2952 }
2953
2954 count++;
2955
2956 entry->data = data;
2957
2958 /*
2959 * The caller might want to do something special
2960 * for each function we find. We call the callback
2961 * to give the caller an opportunity to do so.
2962 */
2963 if (ops->callback) {
2964 if (ops->callback(rec->ip, &entry->data) < 0) {
2965 /* caller does not like this func */
2966 kfree(entry);
2967 continue;
2968 }
2969 }
2970
2971 entry->ops = ops;
2972 entry->ip = rec->ip;
2973
2974 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2975 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
2976
2977 } while_for_each_ftrace_rec();
Steven Rostedtb6887d72009-02-17 12:32:04 -05002978 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002979
2980 out_unlock:
2981 mutex_unlock(&ftrace_lock);
2982
2983 return count;
2984}
2985
2986enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002987 PROBE_TEST_FUNC = 1,
2988 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05002989};
2990
2991static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002992__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002993 void *data, int flags)
2994{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002995 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002996 struct hlist_node *n, *tmp;
2997 char str[KSYM_SYMBOL_LEN];
2998 int type = MATCH_FULL;
2999 int i, len = 0;
3000 char *search;
3001
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003002 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003003 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003004 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003005 int not;
3006
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003007 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003008 len = strlen(search);
3009
Steven Rostedtb6887d72009-02-17 12:32:04 -05003010 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003011 if (WARN_ON(not))
3012 return;
3013 }
3014
3015 mutex_lock(&ftrace_lock);
3016 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3017 struct hlist_head *hhd = &ftrace_func_hash[i];
3018
3019 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
3020
3021 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05003022 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003023 continue;
3024
Steven Rostedtb6887d72009-02-17 12:32:04 -05003025 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003026 continue;
3027
3028 /* do this last, since it is the most expensive */
3029 if (glob) {
3030 kallsyms_lookup(entry->ip, NULL, NULL,
3031 NULL, str);
3032 if (!ftrace_match(str, glob, len, type))
3033 continue;
3034 }
3035
3036 hlist_del(&entry->node);
3037 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
3038 }
3039 }
Steven Rostedtb6887d72009-02-17 12:32:04 -05003040 __disable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003041 mutex_unlock(&ftrace_lock);
3042}
3043
3044void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003045unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003046 void *data)
3047{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003048 __unregister_ftrace_function_probe(glob, ops, data,
3049 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003050}
3051
3052void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003053unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003054{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003055 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003056}
3057
Steven Rostedtb6887d72009-02-17 12:32:04 -05003058void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003059{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003060 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003061}
3062
Steven Rostedtf6180772009-02-14 00:40:25 -05003063static LIST_HEAD(ftrace_commands);
3064static DEFINE_MUTEX(ftrace_cmd_mutex);
3065
3066int register_ftrace_command(struct ftrace_func_command *cmd)
3067{
3068 struct ftrace_func_command *p;
3069 int ret = 0;
3070
3071 mutex_lock(&ftrace_cmd_mutex);
3072 list_for_each_entry(p, &ftrace_commands, list) {
3073 if (strcmp(cmd->name, p->name) == 0) {
3074 ret = -EBUSY;
3075 goto out_unlock;
3076 }
3077 }
3078 list_add(&cmd->list, &ftrace_commands);
3079 out_unlock:
3080 mutex_unlock(&ftrace_cmd_mutex);
3081
3082 return ret;
3083}
3084
3085int unregister_ftrace_command(struct ftrace_func_command *cmd)
3086{
3087 struct ftrace_func_command *p, *n;
3088 int ret = -ENODEV;
3089
3090 mutex_lock(&ftrace_cmd_mutex);
3091 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3092 if (strcmp(cmd->name, p->name) == 0) {
3093 ret = 0;
3094 list_del_init(&p->list);
3095 goto out_unlock;
3096 }
3097 }
3098 out_unlock:
3099 mutex_unlock(&ftrace_cmd_mutex);
3100
3101 return ret;
3102}
3103
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003104static int ftrace_process_regex(struct ftrace_hash *hash,
3105 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003106{
Steven Rostedtf6180772009-02-14 00:40:25 -05003107 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003108 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08003109 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003110
3111 func = strsep(&next, ":");
3112
3113 if (!next) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003114 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003115 if (!ret)
3116 ret = -EINVAL;
3117 if (ret < 0)
3118 return ret;
3119 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003120 }
3121
Steven Rostedtf6180772009-02-14 00:40:25 -05003122 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05003123
3124 command = strsep(&next, ":");
3125
Steven Rostedtf6180772009-02-14 00:40:25 -05003126 mutex_lock(&ftrace_cmd_mutex);
3127 list_for_each_entry(p, &ftrace_commands, list) {
3128 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003129 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05003130 goto out_unlock;
3131 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05003132 }
Steven Rostedtf6180772009-02-14 00:40:25 -05003133 out_unlock:
3134 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003135
Steven Rostedtf6180772009-02-14 00:40:25 -05003136 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003137}
3138
Ingo Molnare309b412008-05-12 21:20:51 +02003139static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003140ftrace_regex_write(struct file *file, const char __user *ubuf,
3141 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02003142{
3143 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003144 struct trace_parser *parser;
3145 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02003146
Li Zefan4ba79782009-09-22 13:52:20 +08003147 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02003148 return 0;
3149
Steven Rostedt41c52c02008-05-22 11:46:33 -04003150 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003151
Steven Rostedt45a4a232011-04-21 23:16:46 -04003152 ret = -ENODEV;
3153 if (unlikely(ftrace_disabled))
3154 goto out_unlock;
3155
Steven Rostedt5072c592008-05-12 21:20:43 +02003156 if (file->f_mode & FMODE_READ) {
3157 struct seq_file *m = file->private_data;
3158 iter = m->private;
3159 } else
3160 iter = file->private_data;
3161
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003162 parser = &iter->parser;
3163 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02003164
Li Zefan4ba79782009-09-22 13:52:20 +08003165 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003166 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003167 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003168 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08003169 trace_parser_clear(parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02003170 if (ret)
Li Zefaned146b252009-11-03 08:55:38 +08003171 goto out_unlock;
Steven Rostedt5072c592008-05-12 21:20:43 +02003172 }
3173
Steven Rostedt5072c592008-05-12 21:20:43 +02003174 ret = read;
Li Zefaned146b252009-11-03 08:55:38 +08003175out_unlock:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003176 mutex_unlock(&ftrace_regex_lock);
Li Zefaned146b252009-11-03 08:55:38 +08003177
Steven Rostedt5072c592008-05-12 21:20:43 +02003178 return ret;
3179}
3180
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003181ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003182ftrace_filter_write(struct file *file, const char __user *ubuf,
3183 size_t cnt, loff_t *ppos)
3184{
3185 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3186}
3187
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003188ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003189ftrace_notrace_write(struct file *file, const char __user *ubuf,
3190 size_t cnt, loff_t *ppos)
3191{
3192 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3193}
3194
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003195static int
Steven Rostedtf45948e2011-05-02 12:29:25 -04003196ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3197 int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003198{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003199 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003200 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003201 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003202
Steven Rostedt936e0742011-05-05 22:54:01 -04003203 /* All global ops uses the global ops filters */
3204 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3205 ops = &global_ops;
3206
Steven Rostedt41c52c02008-05-22 11:46:33 -04003207 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003208 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003209
Steven Rostedtf45948e2011-05-02 12:29:25 -04003210 if (enable)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003211 orig_hash = &ops->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003212 else
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003213 orig_hash = &ops->notrace_hash;
3214
3215 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3216 if (!hash)
3217 return -ENOMEM;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003218
Steven Rostedt41c52c02008-05-22 11:46:33 -04003219 mutex_lock(&ftrace_regex_lock);
3220 if (reset)
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003221 ftrace_filter_reset(hash);
Jiri Olsaac483c42012-01-02 10:04:14 +01003222 if (buf && !ftrace_match_records(hash, buf, len)) {
3223 ret = -EINVAL;
3224 goto out_regex_unlock;
3225 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003226
3227 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003228 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt072126f2011-07-13 15:08:31 -04003229 if (!ret && ops->flags & FTRACE_OPS_FL_ENABLED
3230 && ftrace_enabled)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01003231 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt072126f2011-07-13 15:08:31 -04003232
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003233 mutex_unlock(&ftrace_lock);
3234
Jiri Olsaac483c42012-01-02 10:04:14 +01003235 out_regex_unlock:
Steven Rostedt41c52c02008-05-22 11:46:33 -04003236 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003237
3238 free_ftrace_hash(hash);
3239 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003240}
3241
Steven Rostedt77a2b372008-05-12 21:20:45 +02003242/**
3243 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003244 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02003245 * @buf - the string that holds the function filter text.
3246 * @len - the length of the string.
3247 * @reset - non zero to reset all filters before applying this filter.
3248 *
3249 * Filters denote which functions should be enabled when tracing is enabled.
3250 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3251 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003252int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003253 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02003254{
Jiri Olsaac483c42012-01-02 10:04:14 +01003255 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003256}
Steven Rostedt936e0742011-05-05 22:54:01 -04003257EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003258
Steven Rostedt41c52c02008-05-22 11:46:33 -04003259/**
3260 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003261 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04003262 * @buf - the string that holds the function notrace text.
3263 * @len - the length of the string.
3264 * @reset - non zero to reset all filters before applying this filter.
3265 *
3266 * Notrace Filters denote which functions should not be enabled when tracing
3267 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3268 * for tracing.
3269 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003270int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003271 int len, int reset)
3272{
Jiri Olsaac483c42012-01-02 10:04:14 +01003273 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04003274}
3275EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3276/**
3277 * ftrace_set_filter - set a function to filter on in ftrace
3278 * @ops - the ops to set the filter with
3279 * @buf - the string that holds the function filter text.
3280 * @len - the length of the string.
3281 * @reset - non zero to reset all filters before applying this filter.
3282 *
3283 * Filters denote which functions should be enabled when tracing is enabled.
3284 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3285 */
3286void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3287{
3288 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3289}
3290EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3291
3292/**
3293 * ftrace_set_notrace - set a function to not trace in ftrace
3294 * @ops - the ops to set the notrace filter with
3295 * @buf - the string that holds the function notrace text.
3296 * @len - the length of the string.
3297 * @reset - non zero to reset all filters before applying this filter.
3298 *
3299 * Notrace Filters denote which functions should not be enabled when tracing
3300 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3301 * for tracing.
3302 */
3303void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003304{
Steven Rostedtf45948e2011-05-02 12:29:25 -04003305 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003306}
Steven Rostedt936e0742011-05-05 22:54:01 -04003307EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003308
Steven Rostedt2af15d62009-05-28 13:37:24 -04003309/*
3310 * command line interface to allow users to set filters on boot up.
3311 */
3312#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3313static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3314static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3315
3316static int __init set_ftrace_notrace(char *str)
3317{
3318 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
3319 return 1;
3320}
3321__setup("ftrace_notrace=", set_ftrace_notrace);
3322
3323static int __init set_ftrace_filter(char *str)
3324{
3325 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
3326 return 1;
3327}
3328__setup("ftrace_filter=", set_ftrace_filter);
3329
Stefan Assmann369bc182009-10-12 22:17:21 +02003330#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08003331static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Steven Rostedt801c29f2010-03-05 20:02:19 -05003332static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
3333
Stefan Assmann369bc182009-10-12 22:17:21 +02003334static int __init set_graph_function(char *str)
3335{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02003336 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02003337 return 1;
3338}
3339__setup("ftrace_graph_filter=", set_graph_function);
3340
3341static void __init set_ftrace_early_graph(char *buf)
3342{
3343 int ret;
3344 char *func;
3345
3346 while (buf) {
3347 func = strsep(&buf, ",");
3348 /* we allow only one expression at a time */
3349 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3350 func);
3351 if (ret)
3352 printk(KERN_DEBUG "ftrace: function %s not "
3353 "traceable\n", func);
3354 }
3355}
3356#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3357
Steven Rostedt2a85a372011-12-19 21:57:44 -05003358void __init
3359ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04003360{
3361 char *func;
3362
3363 while (buf) {
3364 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04003365 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003366 }
3367}
3368
3369static void __init set_ftrace_early_filters(void)
3370{
3371 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003372 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003373 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003374 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02003375#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3376 if (ftrace_graph_buf[0])
3377 set_ftrace_early_graph(ftrace_graph_buf);
3378#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04003379}
3380
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003381int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003382{
3383 struct seq_file *m = (struct seq_file *)file->private_data;
3384 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003385 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003386 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04003387 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003388 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02003389
Steven Rostedt41c52c02008-05-22 11:46:33 -04003390 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003391 if (file->f_mode & FMODE_READ) {
3392 iter = m->private;
3393
3394 seq_release(inode, file);
3395 } else
3396 iter = file->private_data;
3397
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003398 parser = &iter->parser;
3399 if (trace_parser_loaded(parser)) {
3400 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003401 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02003402 }
3403
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003404 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003405
Steven Rostedt058e2972011-04-29 22:35:33 -04003406 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04003407 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3408
3409 if (filter_hash)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003410 orig_hash = &iter->ops->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04003411 else
3412 orig_hash = &iter->ops->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003413
Steven Rostedt058e2972011-04-29 22:35:33 -04003414 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003415 ret = ftrace_hash_move(iter->ops, filter_hash,
3416 orig_hash, iter->hash);
3417 if (!ret && (iter->ops->flags & FTRACE_OPS_FL_ENABLED)
3418 && ftrace_enabled)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01003419 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003420
Steven Rostedt058e2972011-04-29 22:35:33 -04003421 mutex_unlock(&ftrace_lock);
3422 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003423 free_ftrace_hash(iter->hash);
3424 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04003425
Steven Rostedt41c52c02008-05-22 11:46:33 -04003426 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003427 return 0;
3428}
3429
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003430static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003431 .open = ftrace_avail_open,
3432 .read = seq_read,
3433 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08003434 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02003435};
3436
Steven Rostedt647bcd02011-05-03 14:39:21 -04003437static const struct file_operations ftrace_enabled_fops = {
3438 .open = ftrace_enabled_open,
3439 .read = seq_read,
3440 .llseek = seq_lseek,
3441 .release = seq_release_private,
3442};
3443
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003444static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003445 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003446 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02003447 .write = ftrace_filter_write,
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003448 .llseek = ftrace_regex_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003449 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02003450};
3451
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003452static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04003453 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003454 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003455 .write = ftrace_notrace_write,
3456 .llseek = ftrace_regex_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003457 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003458};
3459
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003460#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3461
3462static DEFINE_MUTEX(graph_lock);
3463
3464int ftrace_graph_count;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003465int ftrace_graph_filter_enabled;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003466unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3467
3468static void *
Li Zefan85951842009-06-24 09:54:00 +08003469__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003470{
Li Zefan85951842009-06-24 09:54:00 +08003471 if (*pos >= ftrace_graph_count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003472 return NULL;
Li Zefana4ec5e02009-09-18 14:06:28 +08003473 return &ftrace_graph_funcs[*pos];
Li Zefan85951842009-06-24 09:54:00 +08003474}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003475
Li Zefan85951842009-06-24 09:54:00 +08003476static void *
3477g_next(struct seq_file *m, void *v, loff_t *pos)
3478{
3479 (*pos)++;
3480 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003481}
3482
3483static void *g_start(struct seq_file *m, loff_t *pos)
3484{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003485 mutex_lock(&graph_lock);
3486
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003487 /* Nothing, tell g_show to print all functions are enabled */
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003488 if (!ftrace_graph_filter_enabled && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003489 return (void *)1;
3490
Li Zefan85951842009-06-24 09:54:00 +08003491 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003492}
3493
3494static void g_stop(struct seq_file *m, void *p)
3495{
3496 mutex_unlock(&graph_lock);
3497}
3498
3499static int g_show(struct seq_file *m, void *v)
3500{
3501 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003502
3503 if (!ptr)
3504 return 0;
3505
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003506 if (ptr == (unsigned long *)1) {
3507 seq_printf(m, "#### all functions enabled ####\n");
3508 return 0;
3509 }
3510
Steven Rostedtb375a112009-09-17 00:05:58 -04003511 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003512
3513 return 0;
3514}
3515
James Morris88e9d342009-09-22 16:43:43 -07003516static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003517 .start = g_start,
3518 .next = g_next,
3519 .stop = g_stop,
3520 .show = g_show,
3521};
3522
3523static int
3524ftrace_graph_open(struct inode *inode, struct file *file)
3525{
3526 int ret = 0;
3527
3528 if (unlikely(ftrace_disabled))
3529 return -ENODEV;
3530
3531 mutex_lock(&graph_lock);
3532 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04003533 (file->f_flags & O_TRUNC)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003534 ftrace_graph_filter_enabled = 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003535 ftrace_graph_count = 0;
3536 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3537 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003538 mutex_unlock(&graph_lock);
3539
Li Zefana4ec5e02009-09-18 14:06:28 +08003540 if (file->f_mode & FMODE_READ)
3541 ret = seq_open(file, &ftrace_graph_seq_ops);
3542
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003543 return ret;
3544}
3545
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003546static int
Li Zefan87827112009-07-23 11:29:11 +08003547ftrace_graph_release(struct inode *inode, struct file *file)
3548{
3549 if (file->f_mode & FMODE_READ)
3550 seq_release(inode, file);
3551 return 0;
3552}
3553
3554static int
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003555ftrace_set_func(unsigned long *array, int *idx, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003556{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003557 struct dyn_ftrace *rec;
3558 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003559 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003560 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003561 int type, not;
3562 char *search;
3563 bool exists;
3564 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003565
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003566 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003567 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003568 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3569 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003570
3571 search_len = strlen(search);
3572
Steven Rostedt52baf112009-02-14 01:15:39 -05003573 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003574
3575 if (unlikely(ftrace_disabled)) {
3576 mutex_unlock(&ftrace_lock);
3577 return -ENODEV;
3578 }
3579
Steven Rostedt265c8312009-02-13 12:43:56 -05003580 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003581
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003582 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003583 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003584 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003585 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003586 if (array[i] == rec->ip) {
3587 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05003588 break;
3589 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003590 }
3591
3592 if (!not) {
3593 fail = 0;
3594 if (!exists) {
3595 array[(*idx)++] = rec->ip;
3596 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3597 goto out;
3598 }
3599 } else {
3600 if (exists) {
3601 array[i] = array[--(*idx)];
3602 array[*idx] = 0;
3603 fail = 0;
3604 }
3605 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003606 }
Steven Rostedt265c8312009-02-13 12:43:56 -05003607 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003608out:
Steven Rostedt52baf112009-02-14 01:15:39 -05003609 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003610
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003611 if (fail)
3612 return -EINVAL;
3613
3614 ftrace_graph_filter_enabled = 1;
3615 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003616}
3617
3618static ssize_t
3619ftrace_graph_write(struct file *file, const char __user *ubuf,
3620 size_t cnt, loff_t *ppos)
3621{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003622 struct trace_parser parser;
Li Zefan4ba79782009-09-22 13:52:20 +08003623 ssize_t read, ret;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003624
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003625 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003626 return 0;
3627
3628 mutex_lock(&graph_lock);
3629
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003630 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3631 ret = -ENOMEM;
Li Zefan1eb90f12009-09-22 13:52:57 +08003632 goto out_unlock;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003633 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003634
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003635 read = trace_get_user(&parser, ubuf, cnt, ppos);
3636
Li Zefan4ba79782009-09-22 13:52:20 +08003637 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003638 parser.buffer[parser.idx] = 0;
3639
3640 /* we allow only one expression at a time */
Li Zefana4ec5e02009-09-18 14:06:28 +08003641 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003642 parser.buffer);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003643 if (ret)
Li Zefan1eb90f12009-09-22 13:52:57 +08003644 goto out_free;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003645 }
3646
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003647 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08003648
3649out_free:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003650 trace_parser_put(&parser);
Li Zefan1eb90f12009-09-22 13:52:57 +08003651out_unlock:
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003652 mutex_unlock(&graph_lock);
3653
3654 return ret;
3655}
3656
3657static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08003658 .open = ftrace_graph_open,
3659 .read = seq_read,
3660 .write = ftrace_graph_write,
3661 .release = ftrace_graph_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003662 .llseek = seq_lseek,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003663};
3664#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3665
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003666static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02003667{
Steven Rostedt5072c592008-05-12 21:20:43 +02003668
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003669 trace_create_file("available_filter_functions", 0444,
3670 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02003671
Steven Rostedt647bcd02011-05-03 14:39:21 -04003672 trace_create_file("enabled_functions", 0444,
3673 d_tracer, NULL, &ftrace_enabled_fops);
3674
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003675 trace_create_file("set_ftrace_filter", 0644, d_tracer,
3676 NULL, &ftrace_filter_fops);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003677
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003678 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003679 NULL, &ftrace_notrace_fops);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04003680
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003681#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003682 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003683 NULL,
3684 &ftrace_graph_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003685#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3686
Steven Rostedt5072c592008-05-12 21:20:43 +02003687 return 0;
3688}
3689
Steven Rostedt68950612011-12-16 17:06:45 -05003690static void ftrace_swap_recs(void *a, void *b, int size)
3691{
3692 struct dyn_ftrace *reca = a;
3693 struct dyn_ftrace *recb = b;
3694 struct dyn_ftrace t;
3695
3696 t = *reca;
3697 *reca = *recb;
3698 *recb = t;
3699}
3700
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003701static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08003702 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003703 unsigned long *end)
3704{
Steven Rostedta7900872011-12-16 16:23:44 -05003705 struct ftrace_page *pg;
3706 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003707 unsigned long *p;
3708 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04003709 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05003710 int ret = -ENOMEM;
3711
3712 count = end - start;
3713
3714 if (!count)
3715 return 0;
3716
3717 pg = ftrace_allocate_pages(count);
3718 if (!pg)
3719 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003720
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003721 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05003722
Steven Rostedt32082302011-12-16 14:42:37 -05003723 /*
3724 * Core and each module needs their own pages, as
3725 * modules will free them when they are removed.
3726 * Force a new page to be allocated for modules.
3727 */
Steven Rostedta7900872011-12-16 16:23:44 -05003728 if (!mod) {
3729 WARN_ON(ftrace_pages || ftrace_pages_start);
3730 /* First initialization */
3731 ftrace_pages = ftrace_pages_start = pg;
3732 } else {
Steven Rostedt32082302011-12-16 14:42:37 -05003733 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05003734 goto out;
Steven Rostedt32082302011-12-16 14:42:37 -05003735
Steven Rostedta7900872011-12-16 16:23:44 -05003736 if (WARN_ON(ftrace_pages->next)) {
3737 /* Hmm, we have free pages? */
3738 while (ftrace_pages->next)
3739 ftrace_pages = ftrace_pages->next;
Steven Rostedt32082302011-12-16 14:42:37 -05003740 }
Steven Rostedta7900872011-12-16 16:23:44 -05003741
3742 ftrace_pages->next = pg;
3743 ftrace_pages = pg;
Steven Rostedt32082302011-12-16 14:42:37 -05003744 }
3745
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003746 p = start;
3747 while (p < end) {
3748 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08003749 /*
3750 * Some architecture linkers will pad between
3751 * the different mcount_loc sections of different
3752 * object files to satisfy alignments.
3753 * Skip any NULL pointers.
3754 */
3755 if (!addr)
3756 continue;
Steven Rostedta7900872011-12-16 16:23:44 -05003757 if (!ftrace_record_ip(addr))
3758 break;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003759 }
3760
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003761 /* These new locations need to be initialized */
3762 ftrace_new_pgs = pg;
3763
Steven Rostedt68950612011-12-16 17:06:45 -05003764 /* Make each individual set of pages sorted by ips */
3765 for (; pg; pg = pg->next)
3766 sort(pg->records, pg->index, sizeof(struct dyn_ftrace),
3767 ftrace_cmp_recs, ftrace_swap_recs);
3768
Steven Rostedta4f18ed2011-06-07 09:26:46 -04003769 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04003770 * We only need to disable interrupts on start up
3771 * because we are modifying code that an interrupt
3772 * may execute, and the modification is not atomic.
3773 * But for modules, nothing runs the code we modify
3774 * until we are finished with it, and there's no
3775 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04003776 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04003777 if (!mod)
3778 local_irq_save(flags);
Steven Rostedt31e88902008-11-14 16:21:19 -08003779 ftrace_update_code(mod);
Steven Rostedt4376cac2011-06-24 23:28:13 -04003780 if (!mod)
3781 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05003782 ret = 0;
3783 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003784 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003785
Steven Rostedta7900872011-12-16 16:23:44 -05003786 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003787}
3788
Steven Rostedt93eb6772009-04-15 13:24:06 -04003789#ifdef CONFIG_MODULES
Steven Rostedt32082302011-12-16 14:42:37 -05003790
3791#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
3792
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003793void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04003794{
3795 struct dyn_ftrace *rec;
Steven Rostedt32082302011-12-16 14:42:37 -05003796 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04003797 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05003798 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04003799
Steven Rostedt93eb6772009-04-15 13:24:06 -04003800 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003801
3802 if (ftrace_disabled)
3803 goto out_unlock;
3804
Steven Rostedt32082302011-12-16 14:42:37 -05003805 /*
3806 * Each module has its own ftrace_pages, remove
3807 * them from the list.
3808 */
3809 last_pg = &ftrace_pages_start;
3810 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
3811 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003812 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04003813 /*
Steven Rostedt32082302011-12-16 14:42:37 -05003814 * As core pages are first, the first
3815 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04003816 */
Steven Rostedt32082302011-12-16 14:42:37 -05003817 if (WARN_ON(pg == ftrace_pages_start))
3818 goto out_unlock;
3819
3820 /* Check if we are deleting the last page */
3821 if (pg == ftrace_pages)
3822 ftrace_pages = next_to_ftrace_page(last_pg);
3823
3824 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05003825 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3826 free_pages((unsigned long)pg->records, order);
3827 kfree(pg);
Steven Rostedt32082302011-12-16 14:42:37 -05003828 } else
3829 last_pg = &pg->next;
3830 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003831 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04003832 mutex_unlock(&ftrace_lock);
3833}
3834
3835static void ftrace_init_module(struct module *mod,
3836 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04003837{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04003838 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04003839 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003840 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04003841}
3842
Steven Rostedt93eb6772009-04-15 13:24:06 -04003843static int ftrace_module_notify(struct notifier_block *self,
3844 unsigned long val, void *data)
3845{
3846 struct module *mod = data;
3847
3848 switch (val) {
3849 case MODULE_STATE_COMING:
3850 ftrace_init_module(mod, mod->ftrace_callsites,
3851 mod->ftrace_callsites +
3852 mod->num_ftrace_callsites);
3853 break;
3854 case MODULE_STATE_GOING:
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003855 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04003856 break;
3857 }
3858
3859 return 0;
3860}
3861#else
3862static int ftrace_module_notify(struct notifier_block *self,
3863 unsigned long val, void *data)
3864{
3865 return 0;
3866}
3867#endif /* CONFIG_MODULES */
3868
3869struct notifier_block ftrace_module_nb = {
3870 .notifier_call = ftrace_module_notify,
3871 .priority = 0,
3872};
3873
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003874extern unsigned long __start_mcount_loc[];
3875extern unsigned long __stop_mcount_loc[];
3876
3877void __init ftrace_init(void)
3878{
3879 unsigned long count, addr, flags;
3880 int ret;
3881
3882 /* Keep the ftrace pointer to the stub */
3883 addr = (unsigned long)ftrace_stub;
3884
3885 local_irq_save(flags);
3886 ftrace_dyn_arch_init(&addr);
3887 local_irq_restore(flags);
3888
3889 /* ftrace_dyn_arch_init places the return code in addr */
3890 if (addr)
3891 goto failed;
3892
3893 count = __stop_mcount_loc - __start_mcount_loc;
3894
3895 ret = ftrace_dyn_table_alloc(count);
3896 if (ret)
3897 goto failed;
3898
3899 last_ftrace_enabled = ftrace_enabled = 1;
3900
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003901 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08003902 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003903 __stop_mcount_loc);
3904
Steven Rostedt93eb6772009-04-15 13:24:06 -04003905 ret = register_module_notifier(&ftrace_module_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08003906 if (ret)
Steven Rostedt93eb6772009-04-15 13:24:06 -04003907 pr_warning("Failed to register trace ftrace module notifier\n");
3908
Steven Rostedt2af15d62009-05-28 13:37:24 -04003909 set_ftrace_early_filters();
3910
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003911 return;
3912 failed:
3913 ftrace_disabled = 1;
3914}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003915
Steven Rostedt3d083392008-05-12 21:20:42 +02003916#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01003917
Steven Rostedt2b499382011-05-03 22:49:52 -04003918static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04003919 .func = ftrace_stub,
3920};
3921
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01003922static int __init ftrace_nodyn_init(void)
3923{
3924 ftrace_enabled = 1;
3925 return 0;
3926}
3927device_initcall(ftrace_nodyn_init);
3928
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003929static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
3930static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003931/* Keep as macros so we do not need to define the commands */
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04003932# define ftrace_startup(ops, command) \
3933 ({ \
3934 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
3935 0; \
3936 })
Steven Rostedtbd69c302011-05-03 21:55:54 -04003937# define ftrace_shutdown(ops, command) do { } while (0)
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003938# define ftrace_startup_sysctl() do { } while (0)
3939# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04003940
3941static inline int
3942ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
3943{
3944 return 1;
3945}
3946
Steven Rostedt3d083392008-05-12 21:20:42 +02003947#endif /* CONFIG_DYNAMIC_FTRACE */
3948
Steven Rostedtb8489142011-05-04 09:27:52 -04003949static void
Jiri Olsae2484912012-02-15 15:51:48 +01003950ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip)
3951{
3952 struct ftrace_ops *op;
3953
3954 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
3955 return;
3956
3957 /*
3958 * Some of the ops may be dynamically allocated,
3959 * they must be freed after a synchronize_sched().
3960 */
3961 preempt_disable_notrace();
3962 trace_recursion_set(TRACE_CONTROL_BIT);
3963 op = rcu_dereference_raw(ftrace_control_list);
3964 while (op != &ftrace_list_end) {
3965 if (!ftrace_function_local_disabled(op) &&
3966 ftrace_ops_test(op, ip))
3967 op->func(ip, parent_ip);
3968
3969 op = rcu_dereference_raw(op->next);
3970 };
3971 trace_recursion_clear(TRACE_CONTROL_BIT);
3972 preempt_enable_notrace();
3973}
3974
3975static struct ftrace_ops control_ops = {
3976 .func = ftrace_ops_control_func,
3977};
3978
3979static void
Steven Rostedtb8489142011-05-04 09:27:52 -04003980ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip)
3981{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04003982 struct ftrace_ops *op;
Steven Rostedtb8489142011-05-04 09:27:52 -04003983
Steven Rostedtb1cff0a2011-05-25 14:27:43 -04003984 if (unlikely(trace_recursion_test(TRACE_INTERNAL_BIT)))
3985 return;
3986
3987 trace_recursion_set(TRACE_INTERNAL_BIT);
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04003988 /*
3989 * Some of the ops may be dynamically allocated,
3990 * they must be freed after a synchronize_sched().
3991 */
3992 preempt_disable_notrace();
3993 op = rcu_dereference_raw(ftrace_ops_list);
Steven Rostedtb8489142011-05-04 09:27:52 -04003994 while (op != &ftrace_list_end) {
3995 if (ftrace_ops_test(op, ip))
3996 op->func(ip, parent_ip);
3997 op = rcu_dereference_raw(op->next);
3998 };
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04003999 preempt_enable_notrace();
Steven Rostedtb1cff0a2011-05-25 14:27:43 -04004000 trace_recursion_clear(TRACE_INTERNAL_BIT);
Steven Rostedtb8489142011-05-04 09:27:52 -04004001}
4002
Steven Rostedte32d8952008-12-04 00:26:41 -05004003static void clear_ftrace_swapper(void)
4004{
4005 struct task_struct *p;
4006 int cpu;
4007
4008 get_online_cpus();
4009 for_each_online_cpu(cpu) {
4010 p = idle_task(cpu);
4011 clear_tsk_trace_trace(p);
4012 }
4013 put_online_cpus();
4014}
4015
4016static void set_ftrace_swapper(void)
4017{
4018 struct task_struct *p;
4019 int cpu;
4020
4021 get_online_cpus();
4022 for_each_online_cpu(cpu) {
4023 p = idle_task(cpu);
4024 set_tsk_trace_trace(p);
4025 }
4026 put_online_cpus();
4027}
4028
4029static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004030{
4031 struct task_struct *p;
4032
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004033 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05004034 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05004035 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05004036 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004037 rcu_read_unlock();
4038
Steven Rostedte32d8952008-12-04 00:26:41 -05004039 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004040}
4041
Steven Rostedte32d8952008-12-04 00:26:41 -05004042static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004043{
4044 struct task_struct *p;
4045
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004046 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004047 do_each_pid_task(pid, PIDTYPE_PID, p) {
4048 set_tsk_trace_trace(p);
4049 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004050 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004051}
4052
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004053static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004054{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004055 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004056 clear_ftrace_swapper();
4057 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004058 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05004059}
4060
4061static void set_ftrace_pid_task(struct pid *pid)
4062{
4063 if (pid == ftrace_swapper_pid)
4064 set_ftrace_swapper();
4065 else
4066 set_ftrace_pid(pid);
4067}
4068
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004069static int ftrace_pid_add(int p)
4070{
4071 struct pid *pid;
4072 struct ftrace_pid *fpid;
4073 int ret = -EINVAL;
4074
4075 mutex_lock(&ftrace_lock);
4076
4077 if (!p)
4078 pid = ftrace_swapper_pid;
4079 else
4080 pid = find_get_pid(p);
4081
4082 if (!pid)
4083 goto out;
4084
4085 ret = 0;
4086
4087 list_for_each_entry(fpid, &ftrace_pids, list)
4088 if (fpid->pid == pid)
4089 goto out_put;
4090
4091 ret = -ENOMEM;
4092
4093 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4094 if (!fpid)
4095 goto out_put;
4096
4097 list_add(&fpid->list, &ftrace_pids);
4098 fpid->pid = pid;
4099
4100 set_ftrace_pid_task(pid);
4101
4102 ftrace_update_pid_func();
4103 ftrace_startup_enable(0);
4104
4105 mutex_unlock(&ftrace_lock);
4106 return 0;
4107
4108out_put:
4109 if (pid != ftrace_swapper_pid)
4110 put_pid(pid);
4111
4112out:
4113 mutex_unlock(&ftrace_lock);
4114 return ret;
4115}
4116
4117static void ftrace_pid_reset(void)
4118{
4119 struct ftrace_pid *fpid, *safe;
4120
4121 mutex_lock(&ftrace_lock);
4122 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4123 struct pid *pid = fpid->pid;
4124
4125 clear_ftrace_pid_task(pid);
4126
4127 list_del(&fpid->list);
4128 kfree(fpid);
4129 }
4130
4131 ftrace_update_pid_func();
4132 ftrace_startup_enable(0);
4133
4134 mutex_unlock(&ftrace_lock);
4135}
4136
4137static void *fpid_start(struct seq_file *m, loff_t *pos)
4138{
4139 mutex_lock(&ftrace_lock);
4140
4141 if (list_empty(&ftrace_pids) && (!*pos))
4142 return (void *) 1;
4143
4144 return seq_list_start(&ftrace_pids, *pos);
4145}
4146
4147static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4148{
4149 if (v == (void *)1)
4150 return NULL;
4151
4152 return seq_list_next(v, &ftrace_pids, pos);
4153}
4154
4155static void fpid_stop(struct seq_file *m, void *p)
4156{
4157 mutex_unlock(&ftrace_lock);
4158}
4159
4160static int fpid_show(struct seq_file *m, void *v)
4161{
4162 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4163
4164 if (v == (void *)1) {
4165 seq_printf(m, "no pid\n");
4166 return 0;
4167 }
4168
4169 if (fpid->pid == ftrace_swapper_pid)
4170 seq_printf(m, "swapper tasks\n");
4171 else
4172 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4173
4174 return 0;
4175}
4176
4177static const struct seq_operations ftrace_pid_sops = {
4178 .start = fpid_start,
4179 .next = fpid_next,
4180 .stop = fpid_stop,
4181 .show = fpid_show,
4182};
4183
4184static int
4185ftrace_pid_open(struct inode *inode, struct file *file)
4186{
4187 int ret = 0;
4188
4189 if ((file->f_mode & FMODE_WRITE) &&
4190 (file->f_flags & O_TRUNC))
4191 ftrace_pid_reset();
4192
4193 if (file->f_mode & FMODE_READ)
4194 ret = seq_open(file, &ftrace_pid_sops);
4195
4196 return ret;
4197}
4198
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004199static ssize_t
4200ftrace_pid_write(struct file *filp, const char __user *ubuf,
4201 size_t cnt, loff_t *ppos)
4202{
Ingo Molnar457dc922009-11-23 11:03:28 +01004203 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004204 long val;
4205 int ret;
4206
4207 if (cnt >= sizeof(buf))
4208 return -EINVAL;
4209
4210 if (copy_from_user(&buf, ubuf, cnt))
4211 return -EFAULT;
4212
4213 buf[cnt] = 0;
4214
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004215 /*
4216 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4217 * to clean the filter quietly.
4218 */
Ingo Molnar457dc922009-11-23 11:03:28 +01004219 tmp = strstrip(buf);
4220 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004221 return 1;
4222
Ingo Molnar457dc922009-11-23 11:03:28 +01004223 ret = strict_strtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004224 if (ret < 0)
4225 return ret;
4226
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004227 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004228
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004229 return ret ? ret : cnt;
4230}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004231
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004232static int
4233ftrace_pid_release(struct inode *inode, struct file *file)
4234{
4235 if (file->f_mode & FMODE_READ)
4236 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004237
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004238 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004239}
4240
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004241static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004242 .open = ftrace_pid_open,
4243 .write = ftrace_pid_write,
4244 .read = seq_read,
4245 .llseek = seq_lseek,
4246 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004247};
4248
4249static __init int ftrace_init_debugfs(void)
4250{
4251 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004252
4253 d_tracer = tracing_init_dentry();
4254 if (!d_tracer)
4255 return 0;
4256
4257 ftrace_init_dyn_debugfs(d_tracer);
4258
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004259 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4260 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04004261
4262 ftrace_profile_debugfs(d_tracer);
4263
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004264 return 0;
4265}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004266fs_initcall(ftrace_init_debugfs);
4267
Steven Rostedt3d083392008-05-12 21:20:42 +02004268/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004269 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004270 *
4271 * This function should be used by panic code. It stops ftrace
4272 * but in a not so nice way. If you need to simply kill ftrace
4273 * from a non-atomic section, use ftrace_kill.
4274 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004275void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004276{
4277 ftrace_disabled = 1;
4278 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004279 clear_ftrace_function();
4280}
4281
4282/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04004283 * Test if ftrace is dead or not.
4284 */
4285int ftrace_is_dead(void)
4286{
4287 return ftrace_disabled;
4288}
4289
4290/**
Steven Rostedt3d083392008-05-12 21:20:42 +02004291 * register_ftrace_function - register a function for profiling
4292 * @ops - ops structure that holds the function for profiling.
4293 *
4294 * Register a function to be called by all functions in the
4295 * kernel.
4296 *
4297 * Note: @ops->func and all the functions it calls must be labeled
4298 * with "notrace", otherwise it will go into a
4299 * recursive loop.
4300 */
4301int register_ftrace_function(struct ftrace_ops *ops)
4302{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004303 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004304
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004305 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004306
Steven Rostedt45a4a232011-04-21 23:16:46 -04004307 if (unlikely(ftrace_disabled))
4308 goto out_unlock;
4309
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004310 ret = __register_ftrace_function(ops);
Steven Rostedtb8489142011-05-04 09:27:52 -04004311 if (!ret)
Steven Rostedta1cd6172011-05-23 15:24:25 -04004312 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04004313
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004314
Steven Rostedt45a4a232011-04-21 23:16:46 -04004315 out_unlock:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004316 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004317 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02004318}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004319EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02004320
4321/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01004322 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02004323 * @ops - ops structure that holds the function to unregister
4324 *
4325 * Unregister a function that was added to be called by ftrace profiling.
4326 */
4327int unregister_ftrace_function(struct ftrace_ops *ops)
4328{
4329 int ret;
4330
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004331 mutex_lock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004332 ret = __unregister_ftrace_function(ops);
Steven Rostedtb8489142011-05-04 09:27:52 -04004333 if (!ret)
4334 ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004335 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004336
4337 return ret;
4338}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004339EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004340
Ingo Molnare309b412008-05-12 21:20:51 +02004341int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004342ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004343 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004344 loff_t *ppos)
4345{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004346 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004347
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004348 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004349
Steven Rostedt45a4a232011-04-21 23:16:46 -04004350 if (unlikely(ftrace_disabled))
4351 goto out;
4352
4353 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004354
Li Zefana32c7762009-06-26 16:55:51 +08004355 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004356 goto out;
4357
Li Zefana32c7762009-06-26 16:55:51 +08004358 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004359
4360 if (ftrace_enabled) {
4361
4362 ftrace_startup_sysctl();
4363
4364 /* we are starting ftrace again */
Steven Rostedtb8489142011-05-04 09:27:52 -04004365 if (ftrace_ops_list != &ftrace_list_end) {
4366 if (ftrace_ops_list->next == &ftrace_list_end)
4367 ftrace_trace_function = ftrace_ops_list->func;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004368 else
Steven Rostedtb8489142011-05-04 09:27:52 -04004369 ftrace_trace_function = ftrace_ops_list_func;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004370 }
4371
4372 } else {
4373 /* stopping ftrace calls (just send to ftrace_stub) */
4374 ftrace_trace_function = ftrace_stub;
4375
4376 ftrace_shutdown_sysctl();
4377 }
4378
4379 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004380 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004381 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02004382}
Ingo Molnarf17845e2008-10-24 12:47:10 +02004383
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004384#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004385
Steven Rostedt597af812009-04-03 15:24:12 -04004386static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004387static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004388
Steven Rostedte49dc192008-12-02 23:50:05 -05004389int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4390{
4391 return 0;
4392}
4393
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004394/* The callbacks that hook a function */
4395trace_func_graph_ret_t ftrace_graph_return =
4396 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004397trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004398
4399/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4400static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4401{
4402 int i;
4403 int ret = 0;
4404 unsigned long flags;
4405 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4406 struct task_struct *g, *t;
4407
4408 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4409 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4410 * sizeof(struct ftrace_ret_stack),
4411 GFP_KERNEL);
4412 if (!ret_stack_list[i]) {
4413 start = 0;
4414 end = i;
4415 ret = -ENOMEM;
4416 goto free;
4417 }
4418 }
4419
4420 read_lock_irqsave(&tasklist_lock, flags);
4421 do_each_thread(g, t) {
4422 if (start == end) {
4423 ret = -EAGAIN;
4424 goto unlock;
4425 }
4426
4427 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01004428 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004429 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04004430 t->curr_ret_stack = -1;
4431 /* Make sure the tasks see the -1 first: */
4432 smp_wmb();
4433 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004434 }
4435 } while_each_thread(g, t);
4436
4437unlock:
4438 read_unlock_irqrestore(&tasklist_lock, flags);
4439free:
4440 for (i = start; i < end; i++)
4441 kfree(ret_stack_list[i]);
4442 return ret;
4443}
4444
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004445static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04004446ftrace_graph_probe_sched_switch(void *ignore,
4447 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004448{
4449 unsigned long long timestamp;
4450 int index;
4451
Steven Rostedtbe6f1642009-03-24 11:06:24 -04004452 /*
4453 * Does the user want to count the time a function was asleep.
4454 * If so, do not update the time stamps.
4455 */
4456 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4457 return;
4458
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004459 timestamp = trace_clock_local();
4460
4461 prev->ftrace_timestamp = timestamp;
4462
4463 /* only process tasks that we timestamped */
4464 if (!next->ftrace_timestamp)
4465 return;
4466
4467 /*
4468 * Update all the counters in next to make up for the
4469 * time next was sleeping.
4470 */
4471 timestamp -= next->ftrace_timestamp;
4472
4473 for (index = next->curr_ret_stack; index >= 0; index--)
4474 next->ret_stack[index].calltime += timestamp;
4475}
4476
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004477/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004478static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004479{
4480 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004481 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004482
4483 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4484 sizeof(struct ftrace_ret_stack *),
4485 GFP_KERNEL);
4486
4487 if (!ret_stack_list)
4488 return -ENOMEM;
4489
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004490 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04004491 for_each_online_cpu(cpu) {
4492 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05004493 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04004494 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004495
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004496 do {
4497 ret = alloc_retstack_tasklist(ret_stack_list);
4498 } while (ret == -EAGAIN);
4499
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004500 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04004501 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004502 if (ret)
4503 pr_info("ftrace_graph: Couldn't activate tracepoint"
4504 " probe to kernel_sched_switch\n");
4505 }
4506
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004507 kfree(ret_stack_list);
4508 return ret;
4509}
4510
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004511/*
4512 * Hibernation protection.
4513 * The state of the current task is too much unstable during
4514 * suspend/restore to disk. We want to protect against that.
4515 */
4516static int
4517ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4518 void *unused)
4519{
4520 switch (state) {
4521 case PM_HIBERNATION_PREPARE:
4522 pause_graph_tracing();
4523 break;
4524
4525 case PM_POST_HIBERNATION:
4526 unpause_graph_tracing();
4527 break;
4528 }
4529 return NOTIFY_DONE;
4530}
4531
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004532int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4533 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004534{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004535 int ret = 0;
4536
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004537 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004538
Steven Rostedt05ce5812009-03-24 00:18:31 -04004539 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04004540 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04004541 ret = -EBUSY;
4542 goto out;
4543 }
4544
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004545 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
4546 register_pm_notifier(&ftrace_suspend_notifier);
4547
Steven Rostedt597af812009-04-03 15:24:12 -04004548 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004549 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004550 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04004551 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004552 goto out;
4553 }
Steven Rostedte53a6312008-11-26 00:16:25 -05004554
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004555 ftrace_graph_return = retfunc;
4556 ftrace_graph_entry = entryfunc;
Steven Rostedte53a6312008-11-26 00:16:25 -05004557
Steven Rostedta1cd6172011-05-23 15:24:25 -04004558 ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004559
4560out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004561 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004562 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004563}
4564
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004565void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004566{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004567 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004568
Steven Rostedt597af812009-04-03 15:24:12 -04004569 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004570 goto out;
4571
Steven Rostedt597af812009-04-03 15:24:12 -04004572 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004573 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004574 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedtbd69c302011-05-03 21:55:54 -04004575 ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004576 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04004577 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004578
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004579 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004580 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004581}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004582
Steven Rostedt868baf02011-02-10 21:26:13 -05004583static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
4584
4585static void
4586graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
4587{
4588 atomic_set(&t->tracing_graph_pause, 0);
4589 atomic_set(&t->trace_overrun, 0);
4590 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004591 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05004592 smp_wmb();
4593 t->ret_stack = ret_stack;
4594}
4595
4596/*
4597 * Allocate a return stack for the idle task. May be the first
4598 * time through, or it may be done by CPU hotplug online.
4599 */
4600void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
4601{
4602 t->curr_ret_stack = -1;
4603 /*
4604 * The idle task has no parent, it either has its own
4605 * stack or no stack at all.
4606 */
4607 if (t->ret_stack)
4608 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
4609
4610 if (ftrace_graph_active) {
4611 struct ftrace_ret_stack *ret_stack;
4612
4613 ret_stack = per_cpu(idle_ret_stack, cpu);
4614 if (!ret_stack) {
4615 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4616 * sizeof(struct ftrace_ret_stack),
4617 GFP_KERNEL);
4618 if (!ret_stack)
4619 return;
4620 per_cpu(idle_ret_stack, cpu) = ret_stack;
4621 }
4622 graph_init_task(t, ret_stack);
4623 }
4624}
4625
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004626/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004627void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004628{
Steven Rostedt84047e32009-06-02 16:51:55 -04004629 /* Make sure we do not use the parent ret_stack */
4630 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05004631 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04004632
Steven Rostedt597af812009-04-03 15:24:12 -04004633 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04004634 struct ftrace_ret_stack *ret_stack;
4635
4636 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004637 * sizeof(struct ftrace_ret_stack),
4638 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04004639 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004640 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05004641 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04004642 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004643}
4644
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004645void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004646{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004647 struct ftrace_ret_stack *ret_stack = t->ret_stack;
4648
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004649 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004650 /* NULL must become visible to IRQs before we free it: */
4651 barrier();
4652
4653 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004654}
Steven Rostedt14a866c2008-12-02 23:50:02 -05004655
4656void ftrace_graph_stop(void)
4657{
4658 ftrace_stop();
4659}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004660#endif