blob: e101cf9acc0634b477f511d1bd6931f86b56c0a8 [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 Rostedt69128962008-10-23 09:33:03 -040043#define FTRACE_WARN_ON(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040044 ({ \
45 int ___r = cond; \
46 if (WARN_ON(___r)) \
Steven Rostedt69128962008-10-23 09:33:03 -040047 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040048 ___r; \
49 })
Steven Rostedt69128962008-10-23 09:33:03 -040050
51#define FTRACE_WARN_ON_ONCE(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040052 ({ \
53 int ___r = cond; \
54 if (WARN_ON_ONCE(___r)) \
Steven Rostedt69128962008-10-23 09:33:03 -040055 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040056 ___r; \
57 })
Steven Rostedt69128962008-10-23 09:33:03 -040058
Steven Rostedt8fc0c702009-02-16 15:28:00 -050059/* hash bits for specific function selection */
60#define FTRACE_HASH_BITS 7
61#define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
Steven Rostedt33dc9b12011-05-02 17:34:47 -040062#define FTRACE_HASH_DEFAULT_BITS 10
63#define FTRACE_HASH_MAX_BITS 12
Steven Rostedt8fc0c702009-02-16 15:28:00 -050064
Jiri Olsae2484912012-02-15 15:51:48 +010065#define FL_GLOBAL_CONTROL_MASK (FTRACE_OPS_FL_GLOBAL | FTRACE_OPS_FL_CONTROL)
66
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
Rajesh Bhagatdb6544e2012-02-17 13:59:15 +0530252 ftrace_trace_function =
253 (func == ftrace_stub) ? func : ftrace_test_stop_func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400254#endif
255}
256
Steven Rostedt2b499382011-05-03 22:49:52 -0400257static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200258{
Steven Rostedt2b499382011-05-03 22:49:52 -0400259 ops->next = *list;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200260 /*
Steven Rostedtb8489142011-05-04 09:27:52 -0400261 * We are entering ops into the list but another
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200262 * CPU might be walking that list. We need to make sure
263 * the ops->next pointer is valid before another CPU sees
Steven Rostedtb8489142011-05-04 09:27:52 -0400264 * the ops pointer included into the list.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200265 */
Steven Rostedt2b499382011-05-03 22:49:52 -0400266 rcu_assign_pointer(*list, ops);
267}
Steven Rostedt3d083392008-05-12 21:20:42 +0200268
Steven Rostedt2b499382011-05-03 22:49:52 -0400269static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
270{
271 struct ftrace_ops **p;
272
273 /*
274 * If we are removing the last function, then simply point
275 * to the ftrace_stub.
276 */
277 if (*list == ops && ops->next == &ftrace_list_end) {
278 *list = &ftrace_list_end;
279 return 0;
280 }
281
282 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
283 if (*p == ops)
284 break;
285
286 if (*p != ops)
287 return -1;
288
289 *p = (*p)->next;
290 return 0;
291}
292
Jiri Olsae2484912012-02-15 15:51:48 +0100293static void add_ftrace_list_ops(struct ftrace_ops **list,
294 struct ftrace_ops *main_ops,
295 struct ftrace_ops *ops)
296{
297 int first = *list == &ftrace_list_end;
298 add_ftrace_ops(list, ops);
299 if (first)
300 add_ftrace_ops(&ftrace_ops_list, main_ops);
301}
302
303static int remove_ftrace_list_ops(struct ftrace_ops **list,
304 struct ftrace_ops *main_ops,
305 struct ftrace_ops *ops)
306{
307 int ret = remove_ftrace_ops(list, ops);
308 if (!ret && *list == &ftrace_list_end)
309 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
310 return ret;
311}
312
Steven Rostedt2b499382011-05-03 22:49:52 -0400313static int __register_ftrace_function(struct ftrace_ops *ops)
314{
315 if (ftrace_disabled)
316 return -ENODEV;
317
318 if (FTRACE_WARN_ON(ops == &global_ops))
319 return -EINVAL;
320
Steven Rostedtb8489142011-05-04 09:27:52 -0400321 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
322 return -EBUSY;
323
Jiri Olsae2484912012-02-15 15:51:48 +0100324 /* We don't support both control and global flags set. */
325 if ((ops->flags & FL_GLOBAL_CONTROL_MASK) == FL_GLOBAL_CONTROL_MASK)
326 return -EINVAL;
327
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400328 if (!core_kernel_data((unsigned long)ops))
329 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
330
Steven Rostedtb8489142011-05-04 09:27:52 -0400331 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100332 add_ftrace_list_ops(&ftrace_global_list, &global_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400333 ops->flags |= FTRACE_OPS_FL_ENABLED;
Jiri Olsae2484912012-02-15 15:51:48 +0100334 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
335 if (control_ops_alloc(ops))
336 return -ENOMEM;
337 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400338 } else
339 add_ftrace_ops(&ftrace_ops_list, ops);
340
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400341 if (ftrace_enabled)
342 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200343
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200344 return 0;
345}
346
Ingo Molnare309b412008-05-12 21:20:51 +0200347static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200348{
Steven Rostedt2b499382011-05-03 22:49:52 -0400349 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200350
Steven Rostedt2b499382011-05-03 22:49:52 -0400351 if (ftrace_disabled)
352 return -ENODEV;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200353
Steven Rostedtb8489142011-05-04 09:27:52 -0400354 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
355 return -EBUSY;
356
Steven Rostedt2b499382011-05-03 22:49:52 -0400357 if (FTRACE_WARN_ON(ops == &global_ops))
358 return -EINVAL;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200359
Steven Rostedtb8489142011-05-04 09:27:52 -0400360 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100361 ret = remove_ftrace_list_ops(&ftrace_global_list,
362 &global_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400363 if (!ret)
364 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Jiri Olsae2484912012-02-15 15:51:48 +0100365 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
366 ret = remove_ftrace_list_ops(&ftrace_control_list,
367 &control_ops, ops);
368 if (!ret) {
369 /*
370 * The ftrace_ops is now removed from the list,
371 * so there'll be no new users. We must ensure
372 * all current users are done before we free
373 * the control data.
374 */
375 synchronize_sched();
376 control_ops_free(ops);
377 }
Steven Rostedtb8489142011-05-04 09:27:52 -0400378 } else
379 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
380
Steven Rostedt2b499382011-05-03 22:49:52 -0400381 if (ret < 0)
382 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400383
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400384 if (ftrace_enabled)
385 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200386
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400387 /*
388 * Dynamic ops may be freed, we must make sure that all
389 * callers are done before leaving this function.
390 */
391 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
392 synchronize_sched();
393
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500394 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200395}
396
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500397static void ftrace_update_pid_func(void)
398{
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400399 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500400 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900401 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500402
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400403 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500404}
405
Steven Rostedt493762f2009-03-23 17:12:36 -0400406#ifdef CONFIG_FUNCTION_PROFILER
407struct ftrace_profile {
408 struct hlist_node node;
409 unsigned long ip;
410 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400411#ifdef CONFIG_FUNCTION_GRAPH_TRACER
412 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400413 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400414#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400415};
416
417struct ftrace_profile_page {
418 struct ftrace_profile_page *next;
419 unsigned long index;
420 struct ftrace_profile records[];
421};
422
Steven Rostedtcafb1682009-03-24 20:50:39 -0400423struct ftrace_profile_stat {
424 atomic_t disabled;
425 struct hlist_head *hash;
426 struct ftrace_profile_page *pages;
427 struct ftrace_profile_page *start;
428 struct tracer_stat stat;
429};
430
Steven Rostedt493762f2009-03-23 17:12:36 -0400431#define PROFILE_RECORDS_SIZE \
432 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
433
434#define PROFILES_PER_PAGE \
435 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
436
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400437static int ftrace_profile_bits __read_mostly;
438static int ftrace_profile_enabled __read_mostly;
439
440/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400441static DEFINE_MUTEX(ftrace_profile_lock);
442
Steven Rostedtcafb1682009-03-24 20:50:39 -0400443static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400444
445#define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
446
Steven Rostedt493762f2009-03-23 17:12:36 -0400447static void *
448function_stat_next(void *v, int idx)
449{
450 struct ftrace_profile *rec = v;
451 struct ftrace_profile_page *pg;
452
453 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
454
455 again:
Li Zefan0296e422009-06-26 11:15:37 +0800456 if (idx != 0)
457 rec++;
458
Steven Rostedt493762f2009-03-23 17:12:36 -0400459 if ((void *)rec >= (void *)&pg->records[pg->index]) {
460 pg = pg->next;
461 if (!pg)
462 return NULL;
463 rec = &pg->records[0];
464 if (!rec->counter)
465 goto again;
466 }
467
468 return rec;
469}
470
471static void *function_stat_start(struct tracer_stat *trace)
472{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400473 struct ftrace_profile_stat *stat =
474 container_of(trace, struct ftrace_profile_stat, stat);
475
476 if (!stat || !stat->start)
477 return NULL;
478
479 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400480}
481
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400482#ifdef CONFIG_FUNCTION_GRAPH_TRACER
483/* function graph compares on total time */
484static int function_stat_cmp(void *p1, void *p2)
485{
486 struct ftrace_profile *a = p1;
487 struct ftrace_profile *b = p2;
488
489 if (a->time < b->time)
490 return -1;
491 if (a->time > b->time)
492 return 1;
493 else
494 return 0;
495}
496#else
497/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400498static int function_stat_cmp(void *p1, void *p2)
499{
500 struct ftrace_profile *a = p1;
501 struct ftrace_profile *b = p2;
502
503 if (a->counter < b->counter)
504 return -1;
505 if (a->counter > b->counter)
506 return 1;
507 else
508 return 0;
509}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400510#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400511
512static int function_stat_headers(struct seq_file *m)
513{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400514#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400515 seq_printf(m, " Function "
Chase Douglase330b3b2010-04-26 14:02:05 -0400516 "Hit Time Avg s^2\n"
Steven Rostedt34886c82009-03-25 21:00:47 -0400517 " -------- "
Chase Douglase330b3b2010-04-26 14:02:05 -0400518 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400519#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400520 seq_printf(m, " Function Hit\n"
521 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400522#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400523 return 0;
524}
525
526static int function_stat_show(struct seq_file *m, void *v)
527{
528 struct ftrace_profile *rec = v;
529 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800530 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400531#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400532 static struct trace_seq s;
533 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400534 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400535#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800536 mutex_lock(&ftrace_profile_lock);
537
538 /* we raced with function_profile_reset() */
539 if (unlikely(rec->counter == 0)) {
540 ret = -EBUSY;
541 goto out;
542 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400543
544 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400545 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400546
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400547#ifdef CONFIG_FUNCTION_GRAPH_TRACER
548 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400549 avg = rec->time;
550 do_div(avg, rec->counter);
551
Chase Douglase330b3b2010-04-26 14:02:05 -0400552 /* Sample standard deviation (s^2) */
553 if (rec->counter <= 1)
554 stddev = 0;
555 else {
556 stddev = rec->time_squared - rec->counter * avg * avg;
557 /*
558 * Divide only 1000 for ns^2 -> us^2 conversion.
559 * trace_print_graph_duration will divide 1000 again.
560 */
561 do_div(stddev, (rec->counter - 1) * 1000);
562 }
563
Steven Rostedt34886c82009-03-25 21:00:47 -0400564 trace_seq_init(&s);
565 trace_print_graph_duration(rec->time, &s);
566 trace_seq_puts(&s, " ");
567 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400568 trace_seq_puts(&s, " ");
569 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400570 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400571#endif
572 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800573out:
574 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400575
Li Zefan3aaba202010-08-23 16:50:12 +0800576 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400577}
578
Steven Rostedtcafb1682009-03-24 20:50:39 -0400579static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400580{
581 struct ftrace_profile_page *pg;
582
Steven Rostedtcafb1682009-03-24 20:50:39 -0400583 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400584
585 while (pg) {
586 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
587 pg->index = 0;
588 pg = pg->next;
589 }
590
Steven Rostedtcafb1682009-03-24 20:50:39 -0400591 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400592 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
593}
594
Steven Rostedtcafb1682009-03-24 20:50:39 -0400595int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400596{
597 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400598 int functions;
599 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400600 int i;
601
602 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400603 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400604 return 0;
605
Steven Rostedtcafb1682009-03-24 20:50:39 -0400606 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
607 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400608 return -ENOMEM;
609
Steven Rostedt318e0a72009-03-25 20:06:34 -0400610#ifdef CONFIG_DYNAMIC_FTRACE
611 functions = ftrace_update_tot_cnt;
612#else
613 /*
614 * We do not know the number of functions that exist because
615 * dynamic tracing is what counts them. With past experience
616 * we have around 20K functions. That should be more than enough.
617 * It is highly unlikely we will execute every function in
618 * the kernel.
619 */
620 functions = 20000;
621#endif
622
Steven Rostedtcafb1682009-03-24 20:50:39 -0400623 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400624
Steven Rostedt318e0a72009-03-25 20:06:34 -0400625 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
626
Namhyung Kim226c8ea2013-04-01 21:46:24 +0900627 for (i = 1; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400628 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400629 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400630 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400631 pg = pg->next;
632 }
633
634 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400635
636 out_free:
637 pg = stat->start;
638 while (pg) {
639 unsigned long tmp = (unsigned long)pg;
640
641 pg = pg->next;
642 free_page(tmp);
643 }
644
Steven Rostedt318e0a72009-03-25 20:06:34 -0400645 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 Rostedtbc4d36c2013-06-07 17:02:08 +08001002loff_t
1003ftrace_filter_lseek(struct file *file, loff_t offset, int whence)
1004{
1005 loff_t ret;
1006
1007 if (file->f_mode & FMODE_READ)
1008 ret = seq_lseek(file, offset, whence);
1009 else
1010 file->f_pos = ret = 1;
1011
1012 return ret;
1013}
1014
Steven Rostedt3d083392008-05-12 21:20:42 +02001015#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001016
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001017#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001018# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001019#endif
1020
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001021static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1022
Steven Rostedtb6887d72009-02-17 12:32:04 -05001023struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001024 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -05001025 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001026 unsigned long flags;
1027 unsigned long ip;
1028 void *data;
1029 struct rcu_head rcu;
1030};
1031
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001032struct ftrace_func_entry {
1033 struct hlist_node hlist;
1034 unsigned long ip;
1035};
1036
1037struct ftrace_hash {
1038 unsigned long size_bits;
1039 struct hlist_head *buckets;
1040 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001041 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001042};
1043
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001044/*
1045 * We make these constant because no one should touch them,
1046 * but they are used as the default "empty hash", to avoid allocating
1047 * it all the time. These are in a read only section such that if
1048 * anyone does try to modify it, it will cause an exception.
1049 */
1050static const struct hlist_head empty_buckets[1];
1051static const struct ftrace_hash empty_hash = {
1052 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001053};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001054#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001055
Steven Rostedt2b499382011-05-03 22:49:52 -04001056static struct ftrace_ops global_ops = {
Steven Rostedtf45948e2011-05-02 12:29:25 -04001057 .func = ftrace_stub,
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001058 .notrace_hash = EMPTY_HASH,
1059 .filter_hash = EMPTY_HASH,
Steven Rostedtf45948e2011-05-02 12:29:25 -04001060};
1061
Steven Rostedt41c52c02008-05-22 11:46:33 -04001062static DEFINE_MUTEX(ftrace_regex_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02001063
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001064struct ftrace_page {
1065 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001066 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001067 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001068 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001069};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001070
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001071static struct ftrace_page *ftrace_new_pgs;
1072
Steven Rostedta7900872011-12-16 16:23:44 -05001073#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1074#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001075
1076/* estimate from running different kernels */
1077#define NR_TO_INIT 10000
1078
1079static struct ftrace_page *ftrace_pages_start;
1080static struct ftrace_page *ftrace_pages;
1081
Steven Rostedt06a51d92011-12-19 19:07:36 -05001082static bool ftrace_hash_empty(struct ftrace_hash *hash)
1083{
1084 return !hash || !hash->count;
1085}
1086
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001087static struct ftrace_func_entry *
1088ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1089{
1090 unsigned long key;
1091 struct ftrace_func_entry *entry;
1092 struct hlist_head *hhd;
1093 struct hlist_node *n;
1094
Steven Rostedt06a51d92011-12-19 19:07:36 -05001095 if (ftrace_hash_empty(hash))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001096 return NULL;
1097
1098 if (hash->size_bits > 0)
1099 key = hash_long(ip, hash->size_bits);
1100 else
1101 key = 0;
1102
1103 hhd = &hash->buckets[key];
1104
1105 hlist_for_each_entry_rcu(entry, n, hhd, hlist) {
1106 if (entry->ip == ip)
1107 return entry;
1108 }
1109 return NULL;
1110}
1111
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001112static void __add_hash_entry(struct ftrace_hash *hash,
1113 struct ftrace_func_entry *entry)
1114{
1115 struct hlist_head *hhd;
1116 unsigned long key;
1117
1118 if (hash->size_bits)
1119 key = hash_long(entry->ip, hash->size_bits);
1120 else
1121 key = 0;
1122
1123 hhd = &hash->buckets[key];
1124 hlist_add_head(&entry->hlist, hhd);
1125 hash->count++;
1126}
1127
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001128static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1129{
1130 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001131
1132 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1133 if (!entry)
1134 return -ENOMEM;
1135
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001136 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001137 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001138
1139 return 0;
1140}
1141
1142static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001143free_hash_entry(struct ftrace_hash *hash,
1144 struct ftrace_func_entry *entry)
1145{
1146 hlist_del(&entry->hlist);
1147 kfree(entry);
1148 hash->count--;
1149}
1150
1151static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001152remove_hash_entry(struct ftrace_hash *hash,
1153 struct ftrace_func_entry *entry)
1154{
1155 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001156 hash->count--;
1157}
1158
1159static void ftrace_hash_clear(struct ftrace_hash *hash)
1160{
1161 struct hlist_head *hhd;
1162 struct hlist_node *tp, *tn;
1163 struct ftrace_func_entry *entry;
1164 int size = 1 << hash->size_bits;
1165 int i;
1166
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001167 if (!hash->count)
1168 return;
1169
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001170 for (i = 0; i < size; i++) {
1171 hhd = &hash->buckets[i];
1172 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001173 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001174 }
1175 FTRACE_WARN_ON(hash->count);
1176}
1177
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001178static void free_ftrace_hash(struct ftrace_hash *hash)
1179{
1180 if (!hash || hash == EMPTY_HASH)
1181 return;
1182 ftrace_hash_clear(hash);
1183 kfree(hash->buckets);
1184 kfree(hash);
1185}
1186
Steven Rostedt07fd5512011-05-05 18:03:47 -04001187static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1188{
1189 struct ftrace_hash *hash;
1190
1191 hash = container_of(rcu, struct ftrace_hash, rcu);
1192 free_ftrace_hash(hash);
1193}
1194
1195static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1196{
1197 if (!hash || hash == EMPTY_HASH)
1198 return;
1199 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1200}
1201
Jiri Olsa5500fa52012-02-15 15:51:54 +01001202void ftrace_free_filter(struct ftrace_ops *ops)
1203{
1204 free_ftrace_hash(ops->filter_hash);
1205 free_ftrace_hash(ops->notrace_hash);
1206}
1207
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001208static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1209{
1210 struct ftrace_hash *hash;
1211 int size;
1212
1213 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1214 if (!hash)
1215 return NULL;
1216
1217 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001218 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001219
1220 if (!hash->buckets) {
1221 kfree(hash);
1222 return NULL;
1223 }
1224
1225 hash->size_bits = size_bits;
1226
1227 return hash;
1228}
1229
1230static struct ftrace_hash *
1231alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1232{
1233 struct ftrace_func_entry *entry;
1234 struct ftrace_hash *new_hash;
1235 struct hlist_node *tp;
1236 int size;
1237 int ret;
1238 int i;
1239
1240 new_hash = alloc_ftrace_hash(size_bits);
1241 if (!new_hash)
1242 return NULL;
1243
1244 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001245 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001246 return new_hash;
1247
1248 size = 1 << hash->size_bits;
1249 for (i = 0; i < size; i++) {
1250 hlist_for_each_entry(entry, tp, &hash->buckets[i], hlist) {
1251 ret = add_hash_entry(new_hash, entry->ip);
1252 if (ret < 0)
1253 goto free_hash;
1254 }
1255 }
1256
1257 FTRACE_WARN_ON(new_hash->count != hash->count);
1258
1259 return new_hash;
1260
1261 free_hash:
1262 free_ftrace_hash(new_hash);
1263 return NULL;
1264}
1265
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001266static void
1267ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1268static void
1269ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1270
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001271static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001272ftrace_hash_move(struct ftrace_ops *ops, int enable,
1273 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001274{
1275 struct ftrace_func_entry *entry;
1276 struct hlist_node *tp, *tn;
1277 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001278 struct ftrace_hash *old_hash;
1279 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001280 unsigned long key;
1281 int size = src->count;
1282 int bits = 0;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001283 int ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001284 int i;
1285
1286 /*
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001287 * Remove the current set, update the hash and add
1288 * them back.
1289 */
1290 ftrace_hash_rec_disable(ops, enable);
1291
1292 /*
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001293 * If the new source is empty, just free dst and assign it
1294 * the empty_hash.
1295 */
1296 if (!src->count) {
Steven Rostedt07fd5512011-05-05 18:03:47 -04001297 free_ftrace_hash_rcu(*dst);
1298 rcu_assign_pointer(*dst, EMPTY_HASH);
Steven Rostedtd4d34b92011-11-04 20:32:39 -04001299 /* still need to update the function records */
1300 ret = 0;
1301 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001302 }
1303
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001304 /*
1305 * Make the hash size about 1/2 the # found
1306 */
1307 for (size /= 2; size; size >>= 1)
1308 bits++;
1309
1310 /* Don't allocate too much */
1311 if (bits > FTRACE_HASH_MAX_BITS)
1312 bits = FTRACE_HASH_MAX_BITS;
1313
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001314 ret = -ENOMEM;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001315 new_hash = alloc_ftrace_hash(bits);
1316 if (!new_hash)
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001317 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001318
1319 size = 1 << src->size_bits;
1320 for (i = 0; i < size; i++) {
1321 hhd = &src->buckets[i];
1322 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist) {
1323 if (bits > 0)
1324 key = hash_long(entry->ip, bits);
1325 else
1326 key = 0;
1327 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001328 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001329 }
1330 }
1331
Steven Rostedt07fd5512011-05-05 18:03:47 -04001332 old_hash = *dst;
1333 rcu_assign_pointer(*dst, new_hash);
1334 free_ftrace_hash_rcu(old_hash);
1335
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001336 ret = 0;
1337 out:
1338 /*
1339 * Enable regardless of ret:
1340 * On success, we enable the new hash.
1341 * On failure, we re-enable the original hash.
1342 */
1343 ftrace_hash_rec_enable(ops, enable);
1344
1345 return ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001346}
1347
Steven Rostedt265c8312009-02-13 12:43:56 -05001348/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001349 * Test the hashes for this ops to see if we want to call
1350 * the ops->func or not.
1351 *
1352 * It's a match if the ip is in the ops->filter_hash or
1353 * the filter_hash does not exist or is empty,
1354 * AND
1355 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001356 *
1357 * This needs to be called with preemption disabled as
1358 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001359 */
1360static int
1361ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
1362{
1363 struct ftrace_hash *filter_hash;
1364 struct ftrace_hash *notrace_hash;
1365 int ret;
1366
Steven Rostedtb8489142011-05-04 09:27:52 -04001367 filter_hash = rcu_dereference_raw(ops->filter_hash);
1368 notrace_hash = rcu_dereference_raw(ops->notrace_hash);
1369
Steven Rostedt06a51d92011-12-19 19:07:36 -05001370 if ((ftrace_hash_empty(filter_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001371 ftrace_lookup_ip(filter_hash, ip)) &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001372 (ftrace_hash_empty(notrace_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001373 !ftrace_lookup_ip(notrace_hash, ip)))
1374 ret = 1;
1375 else
1376 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001377
1378 return ret;
1379}
1380
1381/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001382 * This is a double for. Do not use 'break' to break out of the loop,
1383 * you must use a goto.
1384 */
1385#define do_for_each_ftrace_rec(pg, rec) \
1386 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1387 int _____i; \
1388 for (_____i = 0; _____i < pg->index; _____i++) { \
1389 rec = &pg->records[_____i];
1390
1391#define while_for_each_ftrace_rec() \
1392 } \
1393 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301394
Steven Rostedt5855fea2011-12-16 19:27:42 -05001395
1396static int ftrace_cmp_recs(const void *a, const void *b)
1397{
1398 const struct dyn_ftrace *reca = a;
1399 const struct dyn_ftrace *recb = b;
1400
1401 if (reca->ip > recb->ip)
1402 return 1;
1403 if (reca->ip < recb->ip)
1404 return -1;
1405 return 0;
1406}
1407
Steven Rostedtc88fd862011-08-16 09:53:39 -04001408/**
1409 * ftrace_location - return true if the ip giving is a traced location
1410 * @ip: the instruction pointer to check
1411 *
1412 * Returns 1 if @ip given is a pointer to a ftrace location.
1413 * That is, the instruction that is either a NOP or call to
1414 * the function tracer. It checks the ftrace internal tables to
1415 * determine if the address belongs or not.
1416 */
1417int ftrace_location(unsigned long ip)
1418{
1419 struct ftrace_page *pg;
1420 struct dyn_ftrace *rec;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001421 struct dyn_ftrace key;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001422
Steven Rostedt5855fea2011-12-16 19:27:42 -05001423 key.ip = ip;
1424
1425 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1426 rec = bsearch(&key, pg->records, pg->index,
1427 sizeof(struct dyn_ftrace),
1428 ftrace_cmp_recs);
1429 if (rec)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001430 return 1;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001431 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04001432
1433 return 0;
1434}
1435
Steven Rostedted926f92011-05-03 13:25:24 -04001436static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1437 int filter_hash,
1438 bool inc)
1439{
1440 struct ftrace_hash *hash;
1441 struct ftrace_hash *other_hash;
1442 struct ftrace_page *pg;
1443 struct dyn_ftrace *rec;
1444 int count = 0;
1445 int all = 0;
1446
1447 /* Only update if the ops has been registered */
1448 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1449 return;
1450
1451 /*
1452 * In the filter_hash case:
1453 * If the count is zero, we update all records.
1454 * Otherwise we just update the items in the hash.
1455 *
1456 * In the notrace_hash case:
1457 * We enable the update in the hash.
1458 * As disabling notrace means enabling the tracing,
1459 * and enabling notrace means disabling, the inc variable
1460 * gets inversed.
1461 */
1462 if (filter_hash) {
1463 hash = ops->filter_hash;
1464 other_hash = ops->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001465 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001466 all = 1;
1467 } else {
1468 inc = !inc;
1469 hash = ops->notrace_hash;
1470 other_hash = ops->filter_hash;
1471 /*
1472 * If the notrace hash has no items,
1473 * then there's nothing to do.
1474 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001475 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001476 return;
1477 }
1478
1479 do_for_each_ftrace_rec(pg, rec) {
1480 int in_other_hash = 0;
1481 int in_hash = 0;
1482 int match = 0;
1483
1484 if (all) {
1485 /*
1486 * Only the filter_hash affects all records.
1487 * Update if the record is not in the notrace hash.
1488 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001489 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001490 match = 1;
1491 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001492 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1493 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001494
1495 /*
1496 *
1497 */
1498 if (filter_hash && in_hash && !in_other_hash)
1499 match = 1;
1500 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001501 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001502 match = 1;
1503 }
1504 if (!match)
1505 continue;
1506
1507 if (inc) {
1508 rec->flags++;
1509 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1510 return;
1511 } else {
1512 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1513 return;
1514 rec->flags--;
1515 }
1516 count++;
1517 /* Shortcut, if we handled all records, we are done. */
1518 if (!all && count == hash->count)
1519 return;
1520 } while_for_each_ftrace_rec();
1521}
1522
1523static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1524 int filter_hash)
1525{
1526 __ftrace_hash_rec_update(ops, filter_hash, 0);
1527}
1528
1529static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1530 int filter_hash)
1531{
1532 __ftrace_hash_rec_update(ops, filter_hash, 1);
1533}
1534
Ingo Molnare309b412008-05-12 21:20:51 +02001535static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001536{
Steven Rostedta7900872011-12-16 16:23:44 -05001537 if (ftrace_pages->index == ftrace_pages->size) {
1538 /* We should have allocated enough */
1539 if (WARN_ON(!ftrace_pages->next))
1540 return NULL;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001541 ftrace_pages = ftrace_pages->next;
1542 }
1543
1544 return &ftrace_pages->records[ftrace_pages->index++];
1545}
1546
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001547static struct dyn_ftrace *
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001548ftrace_record_ip(unsigned long ip)
Steven Rostedt3d083392008-05-12 21:20:42 +02001549{
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001550 struct dyn_ftrace *rec;
Steven Rostedt3d083392008-05-12 21:20:42 +02001551
Steven Rostedtf3c7ac42008-11-14 16:21:19 -08001552 if (ftrace_disabled)
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001553 return NULL;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001554
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001555 rec = ftrace_alloc_dyn_node(ip);
1556 if (!rec)
1557 return NULL;
Steven Rostedt3d083392008-05-12 21:20:42 +02001558
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001559 rec->ip = ip;
Steven Rostedt3d083392008-05-12 21:20:42 +02001560
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001561 return rec;
Steven Rostedt3d083392008-05-12 21:20:42 +02001562}
1563
Steven Rostedt05736a42008-09-22 14:55:47 -07001564static void print_ip_ins(const char *fmt, unsigned char *p)
1565{
1566 int i;
1567
1568 printk(KERN_CONT "%s", fmt);
1569
1570 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1571 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1572}
1573
Steven Rostedtc88fd862011-08-16 09:53:39 -04001574/**
1575 * ftrace_bug - report and shutdown function tracer
1576 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1577 * @ip: The address that failed
1578 *
1579 * The arch code that enables or disables the function tracing
1580 * can call ftrace_bug() when it has detected a problem in
1581 * modifying the code. @failed should be one of either:
1582 * EFAULT - if the problem happens on reading the @ip address
1583 * EINVAL - if what is read at @ip is not what was expected
1584 * EPERM - if the problem happens on writting to the @ip address
1585 */
1586void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001587{
1588 switch (failed) {
1589 case -EFAULT:
1590 FTRACE_WARN_ON_ONCE(1);
1591 pr_info("ftrace faulted on modifying ");
1592 print_ip_sym(ip);
1593 break;
1594 case -EINVAL:
1595 FTRACE_WARN_ON_ONCE(1);
1596 pr_info("ftrace failed to modify ");
1597 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001598 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001599 printk(KERN_CONT "\n");
1600 break;
1601 case -EPERM:
1602 FTRACE_WARN_ON_ONCE(1);
1603 pr_info("ftrace faulted on writing ");
1604 print_ip_sym(ip);
1605 break;
1606 default:
1607 FTRACE_WARN_ON_ONCE(1);
1608 pr_info("ftrace faulted on unknown error ");
1609 print_ip_sym(ip);
1610 }
1611}
1612
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001613
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -05001614/* Return 1 if the address range is reserved for ftrace */
1615int ftrace_text_reserved(void *start, void *end)
1616{
1617 struct dyn_ftrace *rec;
1618 struct ftrace_page *pg;
1619
1620 do_for_each_ftrace_rec(pg, rec) {
1621 if (rec->ip <= (unsigned long)end &&
1622 rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
1623 return 1;
1624 } while_for_each_ftrace_rec();
1625 return 0;
1626}
1627
Steven Rostedtc88fd862011-08-16 09:53:39 -04001628static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02001629{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001630 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001631
Steven Rostedt982c3502008-11-15 16:31:41 -05001632 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001633 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05001634 *
Steven Rostedted926f92011-05-03 13:25:24 -04001635 * If the record has a ref count, then we need to enable it
1636 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001637 *
Steven Rostedted926f92011-05-03 13:25:24 -04001638 * Otherwise we make sure its disabled.
1639 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001640 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04001641 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001642 */
Steven Rostedtc88fd862011-08-16 09:53:39 -04001643 if (enable && (rec->flags & ~FTRACE_FL_MASK))
Steven Rostedted926f92011-05-03 13:25:24 -04001644 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001645
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001646 /* If the state of this record hasn't changed, then do nothing */
1647 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001648 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001649
1650 if (flag) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04001651 if (update)
1652 rec->flags |= FTRACE_FL_ENABLED;
1653 return FTRACE_UPDATE_MAKE_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001654 }
1655
Steven Rostedtc88fd862011-08-16 09:53:39 -04001656 if (update)
1657 rec->flags &= ~FTRACE_FL_ENABLED;
1658
1659 return FTRACE_UPDATE_MAKE_NOP;
1660}
1661
1662/**
1663 * ftrace_update_record, set a record that now is tracing or not
1664 * @rec: the record to update
1665 * @enable: set to 1 if the record is tracing, zero to force disable
1666 *
1667 * The records that represent all functions that can be traced need
1668 * to be updated when tracing has been enabled.
1669 */
1670int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1671{
1672 return ftrace_check_record(rec, enable, 1);
1673}
1674
1675/**
1676 * ftrace_test_record, check if the record has been enabled or not
1677 * @rec: the record to test
1678 * @enable: set to 1 to check if enabled, 0 if it is disabled
1679 *
1680 * The arch code may need to test if a record is already set to
1681 * tracing to determine how to modify the function code that it
1682 * represents.
1683 */
1684int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1685{
1686 return ftrace_check_record(rec, enable, 0);
1687}
1688
1689static int
1690__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1691{
1692 unsigned long ftrace_addr;
1693 int ret;
1694
1695 ftrace_addr = (unsigned long)FTRACE_ADDR;
1696
1697 ret = ftrace_update_record(rec, enable);
1698
1699 switch (ret) {
1700 case FTRACE_UPDATE_IGNORE:
1701 return 0;
1702
1703 case FTRACE_UPDATE_MAKE_CALL:
1704 return ftrace_make_call(rec, ftrace_addr);
1705
1706 case FTRACE_UPDATE_MAKE_NOP:
1707 return ftrace_make_nop(NULL, rec, ftrace_addr);
1708 }
1709
1710 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02001711}
1712
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001713static void ftrace_replace_code(int update)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001714{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001715 struct dyn_ftrace *rec;
1716 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001717 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001718
Steven Rostedt45a4a232011-04-21 23:16:46 -04001719 if (unlikely(ftrace_disabled))
1720 return;
1721
Steven Rostedt265c8312009-02-13 12:43:56 -05001722 do_for_each_ftrace_rec(pg, rec) {
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001723 failed = __ftrace_replace_code(rec, update);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001724 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04001725 ftrace_bug(failed, rec->ip);
1726 /* Stop processing */
1727 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001728 }
1729 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001730}
1731
Steven Rostedtc88fd862011-08-16 09:53:39 -04001732struct ftrace_rec_iter {
1733 struct ftrace_page *pg;
1734 int index;
1735};
1736
1737/**
1738 * ftrace_rec_iter_start, start up iterating over traced functions
1739 *
1740 * Returns an iterator handle that is used to iterate over all
1741 * the records that represent address locations where functions
1742 * are traced.
1743 *
1744 * May return NULL if no records are available.
1745 */
1746struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1747{
1748 /*
1749 * We only use a single iterator.
1750 * Protected by the ftrace_lock mutex.
1751 */
1752 static struct ftrace_rec_iter ftrace_rec_iter;
1753 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1754
1755 iter->pg = ftrace_pages_start;
1756 iter->index = 0;
1757
1758 /* Could have empty pages */
1759 while (iter->pg && !iter->pg->index)
1760 iter->pg = iter->pg->next;
1761
1762 if (!iter->pg)
1763 return NULL;
1764
1765 return iter;
1766}
1767
1768/**
1769 * ftrace_rec_iter_next, get the next record to process.
1770 * @iter: The handle to the iterator.
1771 *
1772 * Returns the next iterator after the given iterator @iter.
1773 */
1774struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1775{
1776 iter->index++;
1777
1778 if (iter->index >= iter->pg->index) {
1779 iter->pg = iter->pg->next;
1780 iter->index = 0;
1781
1782 /* Could have empty pages */
1783 while (iter->pg && !iter->pg->index)
1784 iter->pg = iter->pg->next;
1785 }
1786
1787 if (!iter->pg)
1788 return NULL;
1789
1790 return iter;
1791}
1792
1793/**
1794 * ftrace_rec_iter_record, get the record at the iterator location
1795 * @iter: The current iterator location
1796 *
1797 * Returns the record that the current @iter is at.
1798 */
1799struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1800{
1801 return &iter->pg->records[iter->index];
1802}
1803
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301804static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001805ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001806{
1807 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001808 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001809
1810 ip = rec->ip;
1811
Steven Rostedt45a4a232011-04-21 23:16:46 -04001812 if (unlikely(ftrace_disabled))
1813 return 0;
1814
Shaohua Li25aac9d2009-01-09 11:29:40 +08001815 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001816 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001817 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301818 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02001819 }
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301820 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001821}
1822
Steven Rostedt000ab692009-02-17 13:35:06 -05001823/*
1824 * archs can override this function if they must do something
1825 * before the modifying code is performed.
1826 */
1827int __weak ftrace_arch_code_modify_prepare(void)
1828{
1829 return 0;
1830}
1831
1832/*
1833 * archs can override this function if they must do something
1834 * after the modifying code is performed.
1835 */
1836int __weak ftrace_arch_code_modify_post_process(void)
1837{
1838 return 0;
1839}
1840
Ingo Molnare309b412008-05-12 21:20:51 +02001841static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02001842{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001843 int *command = data;
1844
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001845 if (*command & FTRACE_UPDATE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001846 ftrace_replace_code(1);
Steven Rostedta3583242008-11-11 15:01:42 -05001847 else if (*command & FTRACE_DISABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001848 ftrace_replace_code(0);
1849
1850 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1851 ftrace_update_ftrace_func(ftrace_trace_function);
1852
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001853 if (*command & FTRACE_START_FUNC_RET)
1854 ftrace_enable_ftrace_graph_caller();
1855 else if (*command & FTRACE_STOP_FUNC_RET)
1856 ftrace_disable_ftrace_graph_caller();
1857
Steven Rostedtc88fd862011-08-16 09:53:39 -04001858 return 0;
1859}
1860
1861/**
1862 * ftrace_run_stop_machine, go back to the stop machine method
1863 * @command: The command to tell ftrace what to do
1864 *
1865 * If an arch needs to fall back to the stop machine method, the
1866 * it can call this function.
1867 */
1868void ftrace_run_stop_machine(int command)
1869{
1870 stop_machine(__ftrace_modify_code, &command, NULL);
1871}
1872
1873/**
1874 * arch_ftrace_update_code, modify the code to trace or not trace
1875 * @command: The command that needs to be done
1876 *
1877 * Archs can override this function if it does not need to
1878 * run stop_machine() to modify code.
1879 */
1880void __weak arch_ftrace_update_code(int command)
1881{
1882 ftrace_run_stop_machine(command);
1883}
1884
1885static void ftrace_run_update_code(int command)
1886{
1887 int ret;
1888
1889 ret = ftrace_arch_code_modify_prepare();
1890 FTRACE_WARN_ON(ret);
1891 if (ret)
1892 return;
1893 /*
1894 * Do not call function tracer while we update the code.
1895 * We are in stop machine.
1896 */
1897 function_trace_stop++;
1898
1899 /*
1900 * By default we use stop_machine() to modify the code.
1901 * But archs can do what ever they want as long as it
1902 * is safe. The stop_machine() is the safest, but also
1903 * produces the most overhead.
1904 */
1905 arch_ftrace_update_code(command);
1906
Steven Rostedt6331c282011-07-13 15:11:02 -04001907#ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
1908 /*
1909 * For archs that call ftrace_test_stop_func(), we must
1910 * wait till after we update all the function callers
1911 * before we update the callback. This keeps different
1912 * ops that record different functions from corrupting
1913 * each other.
1914 */
1915 __ftrace_trace_function = __ftrace_trace_function_delay;
1916#endif
1917 function_trace_stop--;
1918
Steven Rostedt000ab692009-02-17 13:35:06 -05001919 ret = ftrace_arch_code_modify_post_process();
1920 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02001921}
1922
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001923static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001924static int ftrace_start_up;
Steven Rostedtb8489142011-05-04 09:27:52 -04001925static int global_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001926
1927static void ftrace_startup_enable(int command)
1928{
1929 if (saved_ftrace_func != ftrace_trace_function) {
1930 saved_ftrace_func = ftrace_trace_function;
1931 command |= FTRACE_UPDATE_TRACE_FUNC;
1932 }
1933
1934 if (!command || !ftrace_enabled)
1935 return;
1936
1937 ftrace_run_update_code(command);
1938}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001939
Steven Rostedta1cd6172011-05-23 15:24:25 -04001940static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001941{
Steven Rostedtb8489142011-05-04 09:27:52 -04001942 bool hash_enable = true;
1943
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001944 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04001945 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001946
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001947 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001948 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02001949
Steven Rostedtb8489142011-05-04 09:27:52 -04001950 /* ops marked global share the filter hashes */
1951 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
1952 ops = &global_ops;
1953 /* Don't update hash if global is already set */
1954 if (global_start_up)
1955 hash_enable = false;
1956 global_start_up++;
1957 }
1958
Steven Rostedted926f92011-05-03 13:25:24 -04001959 ops->flags |= FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04001960 if (hash_enable)
Steven Rostedted926f92011-05-03 13:25:24 -04001961 ftrace_hash_rec_enable(ops, 1);
1962
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001963 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04001964
1965 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02001966}
1967
Steven Rostedtbd69c302011-05-03 21:55:54 -04001968static void ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001969{
Steven Rostedtb8489142011-05-04 09:27:52 -04001970 bool hash_disable = true;
1971
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001972 if (unlikely(ftrace_disabled))
1973 return;
1974
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001975 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02001976 /*
1977 * Just warn in case of unbalance, no need to kill ftrace, it's not
1978 * critical but the ftrace_call callers may be never nopped again after
1979 * further ftrace uses.
1980 */
1981 WARN_ON_ONCE(ftrace_start_up < 0);
1982
Steven Rostedtb8489142011-05-04 09:27:52 -04001983 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
1984 ops = &global_ops;
1985 global_start_up--;
1986 WARN_ON_ONCE(global_start_up < 0);
1987 /* Don't update hash if global still has users */
1988 if (global_start_up) {
1989 WARN_ON_ONCE(!ftrace_start_up);
1990 hash_disable = false;
1991 }
1992 }
1993
1994 if (hash_disable)
Steven Rostedted926f92011-05-03 13:25:24 -04001995 ftrace_hash_rec_disable(ops, 1);
1996
Steven Rostedtb8489142011-05-04 09:27:52 -04001997 if (ops != &global_ops || !global_start_up)
Steven Rostedted926f92011-05-03 13:25:24 -04001998 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04001999
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002000 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002001
2002 if (saved_ftrace_func != ftrace_trace_function) {
2003 saved_ftrace_func = ftrace_trace_function;
2004 command |= FTRACE_UPDATE_TRACE_FUNC;
2005 }
2006
2007 if (!command || !ftrace_enabled)
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002008 return;
Steven Rostedt3d083392008-05-12 21:20:42 +02002009
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002010 ftrace_run_update_code(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02002011}
2012
Ingo Molnare309b412008-05-12 21:20:51 +02002013static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002014{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002015 if (unlikely(ftrace_disabled))
2016 return;
2017
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002018 /* Force update next time */
2019 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002020 /* ftrace_start_up is true if we want ftrace running */
2021 if (ftrace_start_up)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002022 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002023}
2024
Ingo Molnare309b412008-05-12 21:20:51 +02002025static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002026{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002027 if (unlikely(ftrace_disabled))
2028 return;
2029
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002030 /* ftrace_start_up is true if ftrace is running */
2031 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04002032 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002033}
2034
Steven Rostedt3d083392008-05-12 21:20:42 +02002035static cycle_t ftrace_update_time;
2036static unsigned long ftrace_update_cnt;
2037unsigned long ftrace_update_tot_cnt;
2038
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002039static int ops_traces_mod(struct ftrace_ops *ops)
2040{
2041 struct ftrace_hash *hash;
2042
2043 hash = ops->filter_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05002044 return ftrace_hash_empty(hash);
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002045}
2046
Steven Rostedt31e88902008-11-14 16:21:19 -08002047static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02002048{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002049 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002050 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302051 cycle_t start, stop;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002052 unsigned long ref = 0;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002053 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002054
2055 /*
2056 * When adding a module, we need to check if tracers are
2057 * currently enabled and if they are set to trace all functions.
2058 * If they are, we need to enable the module functions as well
2059 * as update the reference counts for those function records.
2060 */
2061 if (mod) {
2062 struct ftrace_ops *ops;
2063
2064 for (ops = ftrace_ops_list;
2065 ops != &ftrace_list_end; ops = ops->next) {
2066 if (ops->flags & FTRACE_OPS_FL_ENABLED &&
2067 ops_traces_mod(ops))
2068 ref++;
2069 }
2070 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002071
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002072 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002073 ftrace_update_cnt = 0;
2074
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002075 for (pg = ftrace_new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302076
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002077 for (i = 0; i < pg->index; i++) {
2078 /* If something went wrong, bail without enabling anything */
2079 if (unlikely(ftrace_disabled))
2080 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002081
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002082 p = &pg->records[i];
2083 p->flags = ref;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302084
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002085 /*
2086 * Do the initial record conversion from mcount jump
2087 * to the NOP instructions.
2088 */
2089 if (!ftrace_code_disable(mod, p))
2090 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002091
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002092 ftrace_update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002093
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002094 /*
2095 * If the tracing is enabled, go ahead and enable the record.
2096 *
2097 * The reason not to enable the record immediatelly is the
2098 * inherent check of ftrace_make_nop/ftrace_make_call for
2099 * correct previous instructions. Making first the NOP
2100 * conversion puts the module to the correct state, thus
2101 * passing the ftrace_make_call check.
2102 */
2103 if (ftrace_start_up && ref) {
2104 int failed = __ftrace_replace_code(p, 1);
2105 if (failed)
2106 ftrace_bug(failed, p->ip);
2107 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002108 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002109 }
2110
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002111 ftrace_new_pgs = NULL;
2112
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002113 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002114 ftrace_update_time = stop - start;
2115 ftrace_update_tot_cnt += ftrace_update_cnt;
2116
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002117 return 0;
2118}
2119
Steven Rostedta7900872011-12-16 16:23:44 -05002120static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002121{
Steven Rostedta7900872011-12-16 16:23:44 -05002122 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002123 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002124
Steven Rostedta7900872011-12-16 16:23:44 -05002125 if (WARN_ON(!count))
2126 return -EINVAL;
2127
2128 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002129
2130 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002131 * We want to fill as much as possible. No more than a page
2132 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002133 */
Steven Rostedta7900872011-12-16 16:23:44 -05002134 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2135 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002136
Steven Rostedta7900872011-12-16 16:23:44 -05002137 again:
2138 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2139
2140 if (!pg->records) {
2141 /* if we can't allocate this size, try something smaller */
2142 if (!order)
2143 return -ENOMEM;
2144 order >>= 1;
2145 goto again;
2146 }
2147
2148 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2149 pg->size = cnt;
2150
2151 if (cnt > count)
2152 cnt = count;
2153
2154 return cnt;
2155}
2156
2157static struct ftrace_page *
2158ftrace_allocate_pages(unsigned long num_to_init)
2159{
2160 struct ftrace_page *start_pg;
2161 struct ftrace_page *pg;
2162 int order;
2163 int cnt;
2164
2165 if (!num_to_init)
2166 return 0;
2167
2168 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2169 if (!pg)
2170 return NULL;
2171
2172 /*
2173 * Try to allocate as much as possible in one continues
2174 * location that fills in all of the space. We want to
2175 * waste as little space as possible.
2176 */
2177 for (;;) {
2178 cnt = ftrace_allocate_records(pg, num_to_init);
2179 if (cnt < 0)
2180 goto free_pages;
2181
2182 num_to_init -= cnt;
2183 if (!num_to_init)
2184 break;
2185
2186 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2187 if (!pg->next)
2188 goto free_pages;
2189
2190 pg = pg->next;
2191 }
2192
2193 return start_pg;
2194
2195 free_pages:
2196 while (start_pg) {
2197 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2198 free_pages((unsigned long)pg->records, order);
2199 start_pg = pg->next;
2200 kfree(pg);
2201 pg = start_pg;
2202 }
2203 pr_info("ftrace: FAILED to allocate memory for functions\n");
2204 return NULL;
2205}
2206
2207static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2208{
2209 int cnt;
2210
2211 if (!num_to_init) {
2212 pr_info("ftrace: No functions to be traced?\n");
2213 return -1;
2214 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002215
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002216 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04002217 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08002218 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002219
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002220 return 0;
2221}
2222
Steven Rostedt5072c592008-05-12 21:20:43 +02002223#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2224
2225struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002226 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002227 loff_t func_pos;
2228 struct ftrace_page *pg;
2229 struct dyn_ftrace *func;
2230 struct ftrace_func_probe *probe;
2231 struct trace_parser parser;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002232 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002233 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002234 int hidx;
2235 int idx;
2236 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002237};
2238
Ingo Molnare309b412008-05-12 21:20:51 +02002239static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002240t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002241{
2242 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002243 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002244 struct hlist_head *hhd;
2245
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002246 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002247 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002248
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002249 if (iter->probe)
2250 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002251 retry:
2252 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2253 return NULL;
2254
2255 hhd = &ftrace_func_hash[iter->hidx];
2256
2257 if (hlist_empty(hhd)) {
2258 iter->hidx++;
2259 hnd = NULL;
2260 goto retry;
2261 }
2262
2263 if (!hnd)
2264 hnd = hhd->first;
2265 else {
2266 hnd = hnd->next;
2267 if (!hnd) {
2268 iter->hidx++;
2269 goto retry;
2270 }
2271 }
2272
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002273 if (WARN_ON_ONCE(!hnd))
2274 return NULL;
2275
2276 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2277
2278 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002279}
2280
2281static void *t_hash_start(struct seq_file *m, loff_t *pos)
2282{
2283 struct ftrace_iterator *iter = m->private;
2284 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08002285 loff_t l;
2286
Steven Rostedt69a30832011-12-19 15:21:16 -05002287 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2288 return NULL;
2289
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002290 if (iter->func_pos > *pos)
2291 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002292
Li Zefand82d6242009-06-24 09:54:54 +08002293 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002294 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002295 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08002296 if (!p)
2297 break;
2298 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002299 if (!p)
2300 return NULL;
2301
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002302 /* Only set this if we have an item */
2303 iter->flags |= FTRACE_ITER_HASH;
2304
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002305 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002306}
2307
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002308static int
2309t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002310{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002311 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002312
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002313 rec = iter->probe;
2314 if (WARN_ON_ONCE(!rec))
2315 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002316
Steven Rostedt809dcf22009-02-16 23:06:01 -05002317 if (rec->ops->print)
2318 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2319
Steven Rostedtb375a112009-09-17 00:05:58 -04002320 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002321
2322 if (rec->data)
2323 seq_printf(m, ":%p", rec->data);
2324 seq_putc(m, '\n');
2325
2326 return 0;
2327}
2328
2329static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02002330t_next(struct seq_file *m, void *v, loff_t *pos)
2331{
2332 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002333 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002334 struct dyn_ftrace *rec = NULL;
2335
Steven Rostedt45a4a232011-04-21 23:16:46 -04002336 if (unlikely(ftrace_disabled))
2337 return NULL;
2338
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002339 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002340 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002341
Steven Rostedt5072c592008-05-12 21:20:43 +02002342 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01002343 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02002344
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002345 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002346 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002347
Steven Rostedt5072c592008-05-12 21:20:43 +02002348 retry:
2349 if (iter->idx >= iter->pg->index) {
2350 if (iter->pg->next) {
2351 iter->pg = iter->pg->next;
2352 iter->idx = 0;
2353 goto retry;
2354 }
2355 } else {
2356 rec = &iter->pg->records[iter->idx++];
Steven Rostedt32082302011-12-16 14:42:37 -05002357 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedtf45948e2011-05-02 12:29:25 -04002358 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
Steven Rostedt0183fb12008-11-07 22:36:02 -05002359
Steven Rostedt41c52c02008-05-22 11:46:33 -04002360 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt647bcd02011-05-03 14:39:21 -04002361 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2362
2363 ((iter->flags & FTRACE_ITER_ENABLED) &&
2364 !(rec->flags & ~FTRACE_FL_MASK))) {
2365
Steven Rostedt5072c592008-05-12 21:20:43 +02002366 rec = NULL;
2367 goto retry;
2368 }
2369 }
2370
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002371 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002372 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002373
2374 iter->func = rec;
2375
2376 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002377}
2378
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002379static void reset_iter_read(struct ftrace_iterator *iter)
2380{
2381 iter->pos = 0;
2382 iter->func_pos = 0;
Dan Carpenter455d8952012-06-09 19:10:27 +03002383 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002384}
2385
2386static void *t_start(struct seq_file *m, loff_t *pos)
2387{
2388 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002389 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002390 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002391 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002392
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002393 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002394
2395 if (unlikely(ftrace_disabled))
2396 return NULL;
2397
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002398 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002399 * If an lseek was done, then reset and start from beginning.
2400 */
2401 if (*pos < iter->pos)
2402 reset_iter_read(iter);
2403
2404 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002405 * For set_ftrace_filter reading, if we have the filter
2406 * off, we can short cut and just print out that all
2407 * functions are enabled.
2408 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05002409 if (iter->flags & FTRACE_ITER_FILTER &&
2410 ftrace_hash_empty(ops->filter_hash)) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002411 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002412 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002413 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002414 /* reset in case of seek/pread */
2415 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002416 return iter;
2417 }
2418
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002419 if (iter->flags & FTRACE_ITER_HASH)
2420 return t_hash_start(m, pos);
2421
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002422 /*
2423 * Unfortunately, we need to restart at ftrace_pages_start
2424 * every time we let go of the ftrace_mutex. This is because
2425 * those pointers can change without the lock.
2426 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002427 iter->pg = ftrace_pages_start;
2428 iter->idx = 0;
2429 for (l = 0; l <= *pos; ) {
2430 p = t_next(m, p, &l);
2431 if (!p)
2432 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002433 }
walimis5821e1b2008-11-15 15:19:06 +08002434
Steven Rostedt69a30832011-12-19 15:21:16 -05002435 if (!p)
2436 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002437
2438 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002439}
2440
2441static void t_stop(struct seq_file *m, void *p)
2442{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002443 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002444}
2445
2446static int t_show(struct seq_file *m, void *v)
2447{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002448 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002449 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02002450
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002451 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002452 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002453
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002454 if (iter->flags & FTRACE_ITER_PRINTALL) {
2455 seq_printf(m, "#### all functions enabled ####\n");
2456 return 0;
2457 }
2458
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002459 rec = iter->func;
2460
Steven Rostedt5072c592008-05-12 21:20:43 +02002461 if (!rec)
2462 return 0;
2463
Steven Rostedt647bcd02011-05-03 14:39:21 -04002464 seq_printf(m, "%ps", (void *)rec->ip);
2465 if (iter->flags & FTRACE_ITER_ENABLED)
2466 seq_printf(m, " (%ld)",
2467 rec->flags & ~FTRACE_FL_MASK);
2468 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02002469
2470 return 0;
2471}
2472
James Morris88e9d342009-09-22 16:43:43 -07002473static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002474 .start = t_start,
2475 .next = t_next,
2476 .stop = t_stop,
2477 .show = t_show,
2478};
2479
Ingo Molnare309b412008-05-12 21:20:51 +02002480static int
Steven Rostedt5072c592008-05-12 21:20:43 +02002481ftrace_avail_open(struct inode *inode, struct file *file)
2482{
2483 struct ftrace_iterator *iter;
2484 int ret;
2485
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002486 if (unlikely(ftrace_disabled))
2487 return -ENODEV;
2488
Steven Rostedt5072c592008-05-12 21:20:43 +02002489 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2490 if (!iter)
2491 return -ENOMEM;
2492
2493 iter->pg = ftrace_pages_start;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002494 iter->ops = &global_ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002495
2496 ret = seq_open(file, &show_ftrace_seq_ops);
2497 if (!ret) {
2498 struct seq_file *m = file->private_data;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002499
Steven Rostedt5072c592008-05-12 21:20:43 +02002500 m->private = iter;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002501 } else {
Steven Rostedt5072c592008-05-12 21:20:43 +02002502 kfree(iter);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002503 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002504
2505 return ret;
2506}
2507
Steven Rostedt647bcd02011-05-03 14:39:21 -04002508static int
2509ftrace_enabled_open(struct inode *inode, struct file *file)
2510{
2511 struct ftrace_iterator *iter;
2512 int ret;
2513
2514 if (unlikely(ftrace_disabled))
2515 return -ENODEV;
2516
2517 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2518 if (!iter)
2519 return -ENOMEM;
2520
2521 iter->pg = ftrace_pages_start;
2522 iter->flags = FTRACE_ITER_ENABLED;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002523 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002524
2525 ret = seq_open(file, &show_ftrace_seq_ops);
2526 if (!ret) {
2527 struct seq_file *m = file->private_data;
2528
2529 m->private = iter;
2530 } else {
2531 kfree(iter);
2532 }
2533
2534 return ret;
2535}
2536
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002537static void ftrace_filter_reset(struct ftrace_hash *hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02002538{
Steven Rostedt52baf112009-02-14 01:15:39 -05002539 mutex_lock(&ftrace_lock);
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002540 ftrace_hash_clear(hash);
Steven Rostedt52baf112009-02-14 01:15:39 -05002541 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002542}
2543
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002544/**
2545 * ftrace_regex_open - initialize function tracer filter files
2546 * @ops: The ftrace_ops that hold the hash filters
2547 * @flag: The type of filter to process
2548 * @inode: The inode, usually passed in to your open routine
2549 * @file: The file, usually passed in to your open routine
2550 *
2551 * ftrace_regex_open() initializes the filter files for the
2552 * @ops. Depending on @flag it may process the filter hash or
2553 * the notrace hash of @ops. With this called from the open
2554 * routine, you can use ftrace_filter_write() for the write
2555 * routine if @flag has FTRACE_ITER_FILTER set, or
2556 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedtbc4d36c2013-06-07 17:02:08 +08002557 * ftrace_filter_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002558 * release must call ftrace_regex_release().
2559 */
2560int
Steven Rostedtf45948e2011-05-02 12:29:25 -04002561ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002562 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02002563{
2564 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002565 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02002566 int ret = 0;
2567
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002568 if (unlikely(ftrace_disabled))
2569 return -ENODEV;
2570
Steven Rostedt5072c592008-05-12 21:20:43 +02002571 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2572 if (!iter)
2573 return -ENOMEM;
2574
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002575 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2576 kfree(iter);
2577 return -ENOMEM;
2578 }
2579
Steven Rostedtf45948e2011-05-02 12:29:25 -04002580 if (flag & FTRACE_ITER_NOTRACE)
2581 hash = ops->notrace_hash;
2582 else
2583 hash = ops->filter_hash;
2584
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002585 iter->ops = ops;
2586 iter->flags = flag;
2587
2588 if (file->f_mode & FMODE_WRITE) {
2589 mutex_lock(&ftrace_lock);
2590 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2591 mutex_unlock(&ftrace_lock);
2592
2593 if (!iter->hash) {
2594 trace_parser_put(&iter->parser);
2595 kfree(iter);
2596 return -ENOMEM;
2597 }
2598 }
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002599
Steven Rostedt41c52c02008-05-22 11:46:33 -04002600 mutex_lock(&ftrace_regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002601
Steven Rostedt5072c592008-05-12 21:20:43 +02002602 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002603 (file->f_flags & O_TRUNC))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002604 ftrace_filter_reset(iter->hash);
Steven Rostedt5072c592008-05-12 21:20:43 +02002605
2606 if (file->f_mode & FMODE_READ) {
2607 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02002608
2609 ret = seq_open(file, &show_ftrace_seq_ops);
2610 if (!ret) {
2611 struct seq_file *m = file->private_data;
2612 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08002613 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002614 /* Failed */
2615 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08002616 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002617 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08002618 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002619 } else
2620 file->private_data = iter;
Steven Rostedt41c52c02008-05-22 11:46:33 -04002621 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002622
2623 return ret;
2624}
2625
Steven Rostedt41c52c02008-05-22 11:46:33 -04002626static int
2627ftrace_filter_open(struct inode *inode, struct file *file)
2628{
Steven Rostedt69a30832011-12-19 15:21:16 -05002629 return ftrace_regex_open(&global_ops,
2630 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2631 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002632}
2633
2634static int
2635ftrace_notrace_open(struct inode *inode, struct file *file)
2636{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002637 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002638 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002639}
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 Rostedt1cf41dd2011-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 Rostedt1cf41dd2011-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 Rostedt1cf41dd2011-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 Rostedt1cf41dd2011-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 Rostedt1cf41dd2011-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 Rostedt1cf41dd2011-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 Rostedt1cf41dd2011-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 Rostedt1cf41dd2011-04-29 20:59:51 -04002755 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002756}
2757
Steven Rostedt1cf41dd2011-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 Rostedt1cf41dd2011-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 Rostedt1cf41dd2011-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
Steven Rostedt (Red Hat)52cecaa2013-03-13 11:15:19 -04003036 hlist_del_rcu(&entry->node);
3037 call_rcu_sched(&entry->rcu, ftrace_free_entry_rcu);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003038 }
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 Rostedt1cf41dd2011-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 Zefaned146b22009-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 Zefaned146b22009-11-03 08:55:38 +08003175out_unlock:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003176 mutex_unlock(&ftrace_regex_lock);
Li Zefaned146b22009-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 Rostedt1cf41dd2011-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 Rostedt1cf41dd2011-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 Rostedt5e2336a02009-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 Rostedt5e2336a02009-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,
Namhyung Kim3a22cc72013-06-07 17:01:16 +08003448 .llseek = ftrace_filter_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003449 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02003450};
3451
Steven Rostedt5e2336a02009-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,
Namhyung Kim3a22cc72013-06-07 17:01:16 +08003456 .llseek = ftrace_filter_lseek,
Steven Rostedt1cf41dd2011-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
Namhyung Kim761694a2013-04-11 16:01:38 +09003614 ftrace_graph_filter_enabled = !!(*idx);
3615
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003616 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003617}
3618
3619static ssize_t
3620ftrace_graph_write(struct file *file, const char __user *ubuf,
3621 size_t cnt, loff_t *ppos)
3622{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003623 struct trace_parser parser;
Li Zefan4ba79782009-09-22 13:52:20 +08003624 ssize_t read, ret;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003625
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003626 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003627 return 0;
3628
3629 mutex_lock(&graph_lock);
3630
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003631 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3632 ret = -ENOMEM;
Li Zefan1eb90f12009-09-22 13:52:57 +08003633 goto out_unlock;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003634 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003635
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003636 read = trace_get_user(&parser, ubuf, cnt, ppos);
3637
Li Zefan4ba79782009-09-22 13:52:20 +08003638 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003639 parser.buffer[parser.idx] = 0;
3640
3641 /* we allow only one expression at a time */
Li Zefana4ec5e02009-09-18 14:06:28 +08003642 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003643 parser.buffer);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003644 if (ret)
Li Zefan1eb90f12009-09-22 13:52:57 +08003645 goto out_free;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003646 }
3647
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003648 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08003649
3650out_free:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003651 trace_parser_put(&parser);
Li Zefan1eb90f12009-09-22 13:52:57 +08003652out_unlock:
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003653 mutex_unlock(&graph_lock);
3654
3655 return ret;
3656}
3657
3658static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08003659 .open = ftrace_graph_open,
3660 .read = seq_read,
3661 .write = ftrace_graph_write,
Namhyung Kim3a22cc72013-06-07 17:01:16 +08003662 .llseek = ftrace_filter_lseek,
Li Zefan87827112009-07-23 11:29:11 +08003663 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003664};
3665#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3666
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003667static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02003668{
Steven Rostedt5072c592008-05-12 21:20:43 +02003669
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003670 trace_create_file("available_filter_functions", 0444,
3671 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02003672
Steven Rostedt647bcd02011-05-03 14:39:21 -04003673 trace_create_file("enabled_functions", 0444,
3674 d_tracer, NULL, &ftrace_enabled_fops);
3675
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003676 trace_create_file("set_ftrace_filter", 0644, d_tracer,
3677 NULL, &ftrace_filter_fops);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003678
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003679 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003680 NULL, &ftrace_notrace_fops);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04003681
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003682#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003683 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003684 NULL,
3685 &ftrace_graph_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003686#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3687
Steven Rostedt5072c592008-05-12 21:20:43 +02003688 return 0;
3689}
3690
Steven Rostedt68950612011-12-16 17:06:45 -05003691static void ftrace_swap_recs(void *a, void *b, int size)
3692{
3693 struct dyn_ftrace *reca = a;
3694 struct dyn_ftrace *recb = b;
3695 struct dyn_ftrace t;
3696
3697 t = *reca;
3698 *reca = *recb;
3699 *recb = t;
3700}
3701
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003702static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08003703 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003704 unsigned long *end)
3705{
Steven Rostedta7900872011-12-16 16:23:44 -05003706 struct ftrace_page *pg;
3707 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003708 unsigned long *p;
3709 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04003710 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05003711 int ret = -ENOMEM;
3712
3713 count = end - start;
3714
3715 if (!count)
3716 return 0;
3717
3718 pg = ftrace_allocate_pages(count);
3719 if (!pg)
3720 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003721
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003722 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05003723
Steven Rostedt32082302011-12-16 14:42:37 -05003724 /*
3725 * Core and each module needs their own pages, as
3726 * modules will free them when they are removed.
3727 * Force a new page to be allocated for modules.
3728 */
Steven Rostedta7900872011-12-16 16:23:44 -05003729 if (!mod) {
3730 WARN_ON(ftrace_pages || ftrace_pages_start);
3731 /* First initialization */
3732 ftrace_pages = ftrace_pages_start = pg;
3733 } else {
Steven Rostedt32082302011-12-16 14:42:37 -05003734 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05003735 goto out;
Steven Rostedt32082302011-12-16 14:42:37 -05003736
Steven Rostedta7900872011-12-16 16:23:44 -05003737 if (WARN_ON(ftrace_pages->next)) {
3738 /* Hmm, we have free pages? */
3739 while (ftrace_pages->next)
3740 ftrace_pages = ftrace_pages->next;
Steven Rostedt32082302011-12-16 14:42:37 -05003741 }
Steven Rostedta7900872011-12-16 16:23:44 -05003742
3743 ftrace_pages->next = pg;
3744 ftrace_pages = pg;
Steven Rostedt32082302011-12-16 14:42:37 -05003745 }
3746
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003747 p = start;
3748 while (p < end) {
3749 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08003750 /*
3751 * Some architecture linkers will pad between
3752 * the different mcount_loc sections of different
3753 * object files to satisfy alignments.
3754 * Skip any NULL pointers.
3755 */
3756 if (!addr)
3757 continue;
Steven Rostedta7900872011-12-16 16:23:44 -05003758 if (!ftrace_record_ip(addr))
3759 break;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003760 }
3761
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003762 /* These new locations need to be initialized */
3763 ftrace_new_pgs = pg;
3764
Steven Rostedt68950612011-12-16 17:06:45 -05003765 /* Make each individual set of pages sorted by ips */
3766 for (; pg; pg = pg->next)
3767 sort(pg->records, pg->index, sizeof(struct dyn_ftrace),
3768 ftrace_cmp_recs, ftrace_swap_recs);
3769
Steven Rostedta4f18ed2011-06-07 09:26:46 -04003770 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04003771 * We only need to disable interrupts on start up
3772 * because we are modifying code that an interrupt
3773 * may execute, and the modification is not atomic.
3774 * But for modules, nothing runs the code we modify
3775 * until we are finished with it, and there's no
3776 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04003777 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04003778 if (!mod)
3779 local_irq_save(flags);
Steven Rostedt31e88902008-11-14 16:21:19 -08003780 ftrace_update_code(mod);
Steven Rostedt4376cac2011-06-24 23:28:13 -04003781 if (!mod)
3782 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05003783 ret = 0;
3784 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003785 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003786
Steven Rostedta7900872011-12-16 16:23:44 -05003787 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003788}
3789
Steven Rostedt93eb6772009-04-15 13:24:06 -04003790#ifdef CONFIG_MODULES
Steven Rostedt32082302011-12-16 14:42:37 -05003791
3792#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
3793
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003794void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04003795{
3796 struct dyn_ftrace *rec;
Steven Rostedt32082302011-12-16 14:42:37 -05003797 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04003798 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05003799 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04003800
Steven Rostedt93eb6772009-04-15 13:24:06 -04003801 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003802
3803 if (ftrace_disabled)
3804 goto out_unlock;
3805
Steven Rostedt32082302011-12-16 14:42:37 -05003806 /*
3807 * Each module has its own ftrace_pages, remove
3808 * them from the list.
3809 */
3810 last_pg = &ftrace_pages_start;
3811 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
3812 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003813 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04003814 /*
Steven Rostedt32082302011-12-16 14:42:37 -05003815 * As core pages are first, the first
3816 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04003817 */
Steven Rostedt32082302011-12-16 14:42:37 -05003818 if (WARN_ON(pg == ftrace_pages_start))
3819 goto out_unlock;
3820
3821 /* Check if we are deleting the last page */
3822 if (pg == ftrace_pages)
3823 ftrace_pages = next_to_ftrace_page(last_pg);
3824
3825 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05003826 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3827 free_pages((unsigned long)pg->records, order);
3828 kfree(pg);
Steven Rostedt32082302011-12-16 14:42:37 -05003829 } else
3830 last_pg = &pg->next;
3831 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003832 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04003833 mutex_unlock(&ftrace_lock);
3834}
3835
3836static void ftrace_init_module(struct module *mod,
3837 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04003838{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04003839 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04003840 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003841 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04003842}
3843
Steven Rostedt (Red Hat)e6842472013-02-13 15:18:38 -05003844static int ftrace_module_notify_enter(struct notifier_block *self,
3845 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04003846{
3847 struct module *mod = data;
3848
Steven Rostedt (Red Hat)e6842472013-02-13 15:18:38 -05003849 if (val == MODULE_STATE_COMING)
Steven Rostedt93eb6772009-04-15 13:24:06 -04003850 ftrace_init_module(mod, mod->ftrace_callsites,
3851 mod->ftrace_callsites +
3852 mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)e6842472013-02-13 15:18:38 -05003853 return 0;
3854}
3855
3856static int ftrace_module_notify_exit(struct notifier_block *self,
3857 unsigned long val, void *data)
3858{
3859 struct module *mod = data;
3860
3861 if (val == MODULE_STATE_GOING)
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003862 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04003863
3864 return 0;
3865}
3866#else
Steven Rostedt (Red Hat)e6842472013-02-13 15:18:38 -05003867static int ftrace_module_notify_enter(struct notifier_block *self,
3868 unsigned long val, void *data)
3869{
3870 return 0;
3871}
3872static int ftrace_module_notify_exit(struct notifier_block *self,
3873 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04003874{
3875 return 0;
3876}
3877#endif /* CONFIG_MODULES */
3878
Steven Rostedt (Red Hat)e6842472013-02-13 15:18:38 -05003879struct notifier_block ftrace_module_enter_nb = {
3880 .notifier_call = ftrace_module_notify_enter,
Steven Rostedtf2a01002012-12-14 09:48:15 -05003881 .priority = INT_MAX, /* Run before anything that can use kprobes */
Steven Rostedt93eb6772009-04-15 13:24:06 -04003882};
3883
Steven Rostedt (Red Hat)e6842472013-02-13 15:18:38 -05003884struct notifier_block ftrace_module_exit_nb = {
3885 .notifier_call = ftrace_module_notify_exit,
3886 .priority = INT_MIN, /* Run after anything that can remove kprobes */
3887};
3888
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003889extern unsigned long __start_mcount_loc[];
3890extern unsigned long __stop_mcount_loc[];
3891
3892void __init ftrace_init(void)
3893{
3894 unsigned long count, addr, flags;
3895 int ret;
3896
3897 /* Keep the ftrace pointer to the stub */
3898 addr = (unsigned long)ftrace_stub;
3899
3900 local_irq_save(flags);
3901 ftrace_dyn_arch_init(&addr);
3902 local_irq_restore(flags);
3903
3904 /* ftrace_dyn_arch_init places the return code in addr */
3905 if (addr)
3906 goto failed;
3907
3908 count = __stop_mcount_loc - __start_mcount_loc;
3909
3910 ret = ftrace_dyn_table_alloc(count);
3911 if (ret)
3912 goto failed;
3913
3914 last_ftrace_enabled = ftrace_enabled = 1;
3915
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003916 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08003917 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003918 __stop_mcount_loc);
3919
Steven Rostedt (Red Hat)e6842472013-02-13 15:18:38 -05003920 ret = register_module_notifier(&ftrace_module_enter_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08003921 if (ret)
Steven Rostedt (Red Hat)e6842472013-02-13 15:18:38 -05003922 pr_warning("Failed to register trace ftrace module enter notifier\n");
3923
3924 ret = register_module_notifier(&ftrace_module_exit_nb);
3925 if (ret)
3926 pr_warning("Failed to register trace ftrace module exit notifier\n");
Steven Rostedt93eb6772009-04-15 13:24:06 -04003927
Steven Rostedt2af15d62009-05-28 13:37:24 -04003928 set_ftrace_early_filters();
3929
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003930 return;
3931 failed:
3932 ftrace_disabled = 1;
3933}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003934
Steven Rostedt3d083392008-05-12 21:20:42 +02003935#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01003936
Steven Rostedt2b499382011-05-03 22:49:52 -04003937static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04003938 .func = ftrace_stub,
3939};
3940
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01003941static int __init ftrace_nodyn_init(void)
3942{
3943 ftrace_enabled = 1;
3944 return 0;
3945}
3946device_initcall(ftrace_nodyn_init);
3947
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003948static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
3949static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003950/* Keep as macros so we do not need to define the commands */
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04003951# define ftrace_startup(ops, command) \
3952 ({ \
3953 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
3954 0; \
3955 })
Steven Rostedtbd69c302011-05-03 21:55:54 -04003956# define ftrace_shutdown(ops, command) do { } while (0)
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003957# define ftrace_startup_sysctl() do { } while (0)
3958# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04003959
3960static inline int
3961ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
3962{
3963 return 1;
3964}
3965
Steven Rostedt3d083392008-05-12 21:20:42 +02003966#endif /* CONFIG_DYNAMIC_FTRACE */
3967
Steven Rostedtb8489142011-05-04 09:27:52 -04003968static void
Jiri Olsae2484912012-02-15 15:51:48 +01003969ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip)
3970{
3971 struct ftrace_ops *op;
3972
3973 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
3974 return;
3975
3976 /*
3977 * Some of the ops may be dynamically allocated,
3978 * they must be freed after a synchronize_sched().
3979 */
3980 preempt_disable_notrace();
3981 trace_recursion_set(TRACE_CONTROL_BIT);
3982 op = rcu_dereference_raw(ftrace_control_list);
3983 while (op != &ftrace_list_end) {
3984 if (!ftrace_function_local_disabled(op) &&
3985 ftrace_ops_test(op, ip))
3986 op->func(ip, parent_ip);
3987
3988 op = rcu_dereference_raw(op->next);
3989 };
3990 trace_recursion_clear(TRACE_CONTROL_BIT);
3991 preempt_enable_notrace();
3992}
3993
3994static struct ftrace_ops control_ops = {
3995 .func = ftrace_ops_control_func,
3996};
3997
3998static void
Steven Rostedtb8489142011-05-04 09:27:52 -04003999ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip)
4000{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004001 struct ftrace_ops *op;
Steven Rostedtb8489142011-05-04 09:27:52 -04004002
Steven Rostedtb1cff0a2011-05-25 14:27:43 -04004003 if (unlikely(trace_recursion_test(TRACE_INTERNAL_BIT)))
4004 return;
4005
4006 trace_recursion_set(TRACE_INTERNAL_BIT);
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004007 /*
4008 * Some of the ops may be dynamically allocated,
4009 * they must be freed after a synchronize_sched().
4010 */
4011 preempt_disable_notrace();
4012 op = rcu_dereference_raw(ftrace_ops_list);
Steven Rostedtb8489142011-05-04 09:27:52 -04004013 while (op != &ftrace_list_end) {
4014 if (ftrace_ops_test(op, ip))
4015 op->func(ip, parent_ip);
4016 op = rcu_dereference_raw(op->next);
4017 };
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004018 preempt_enable_notrace();
Steven Rostedtb1cff0a2011-05-25 14:27:43 -04004019 trace_recursion_clear(TRACE_INTERNAL_BIT);
Steven Rostedtb8489142011-05-04 09:27:52 -04004020}
4021
Steven Rostedte32d8952008-12-04 00:26:41 -05004022static void clear_ftrace_swapper(void)
4023{
4024 struct task_struct *p;
4025 int cpu;
4026
4027 get_online_cpus();
4028 for_each_online_cpu(cpu) {
4029 p = idle_task(cpu);
4030 clear_tsk_trace_trace(p);
4031 }
4032 put_online_cpus();
4033}
4034
4035static void set_ftrace_swapper(void)
4036{
4037 struct task_struct *p;
4038 int cpu;
4039
4040 get_online_cpus();
4041 for_each_online_cpu(cpu) {
4042 p = idle_task(cpu);
4043 set_tsk_trace_trace(p);
4044 }
4045 put_online_cpus();
4046}
4047
4048static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004049{
4050 struct task_struct *p;
4051
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004052 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05004053 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05004054 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05004055 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004056 rcu_read_unlock();
4057
Steven Rostedte32d8952008-12-04 00:26:41 -05004058 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004059}
4060
Steven Rostedte32d8952008-12-04 00:26:41 -05004061static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004062{
4063 struct task_struct *p;
4064
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004065 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004066 do_each_pid_task(pid, PIDTYPE_PID, p) {
4067 set_tsk_trace_trace(p);
4068 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004069 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004070}
4071
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004072static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004073{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004074 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004075 clear_ftrace_swapper();
4076 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004077 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05004078}
4079
4080static void set_ftrace_pid_task(struct pid *pid)
4081{
4082 if (pid == ftrace_swapper_pid)
4083 set_ftrace_swapper();
4084 else
4085 set_ftrace_pid(pid);
4086}
4087
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004088static int ftrace_pid_add(int p)
4089{
4090 struct pid *pid;
4091 struct ftrace_pid *fpid;
4092 int ret = -EINVAL;
4093
4094 mutex_lock(&ftrace_lock);
4095
4096 if (!p)
4097 pid = ftrace_swapper_pid;
4098 else
4099 pid = find_get_pid(p);
4100
4101 if (!pid)
4102 goto out;
4103
4104 ret = 0;
4105
4106 list_for_each_entry(fpid, &ftrace_pids, list)
4107 if (fpid->pid == pid)
4108 goto out_put;
4109
4110 ret = -ENOMEM;
4111
4112 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4113 if (!fpid)
4114 goto out_put;
4115
4116 list_add(&fpid->list, &ftrace_pids);
4117 fpid->pid = pid;
4118
4119 set_ftrace_pid_task(pid);
4120
4121 ftrace_update_pid_func();
4122 ftrace_startup_enable(0);
4123
4124 mutex_unlock(&ftrace_lock);
4125 return 0;
4126
4127out_put:
4128 if (pid != ftrace_swapper_pid)
4129 put_pid(pid);
4130
4131out:
4132 mutex_unlock(&ftrace_lock);
4133 return ret;
4134}
4135
4136static void ftrace_pid_reset(void)
4137{
4138 struct ftrace_pid *fpid, *safe;
4139
4140 mutex_lock(&ftrace_lock);
4141 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4142 struct pid *pid = fpid->pid;
4143
4144 clear_ftrace_pid_task(pid);
4145
4146 list_del(&fpid->list);
4147 kfree(fpid);
4148 }
4149
4150 ftrace_update_pid_func();
4151 ftrace_startup_enable(0);
4152
4153 mutex_unlock(&ftrace_lock);
4154}
4155
4156static void *fpid_start(struct seq_file *m, loff_t *pos)
4157{
4158 mutex_lock(&ftrace_lock);
4159
4160 if (list_empty(&ftrace_pids) && (!*pos))
4161 return (void *) 1;
4162
4163 return seq_list_start(&ftrace_pids, *pos);
4164}
4165
4166static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4167{
4168 if (v == (void *)1)
4169 return NULL;
4170
4171 return seq_list_next(v, &ftrace_pids, pos);
4172}
4173
4174static void fpid_stop(struct seq_file *m, void *p)
4175{
4176 mutex_unlock(&ftrace_lock);
4177}
4178
4179static int fpid_show(struct seq_file *m, void *v)
4180{
4181 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4182
4183 if (v == (void *)1) {
4184 seq_printf(m, "no pid\n");
4185 return 0;
4186 }
4187
4188 if (fpid->pid == ftrace_swapper_pid)
4189 seq_printf(m, "swapper tasks\n");
4190 else
4191 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4192
4193 return 0;
4194}
4195
4196static const struct seq_operations ftrace_pid_sops = {
4197 .start = fpid_start,
4198 .next = fpid_next,
4199 .stop = fpid_stop,
4200 .show = fpid_show,
4201};
4202
4203static int
4204ftrace_pid_open(struct inode *inode, struct file *file)
4205{
4206 int ret = 0;
4207
4208 if ((file->f_mode & FMODE_WRITE) &&
4209 (file->f_flags & O_TRUNC))
4210 ftrace_pid_reset();
4211
4212 if (file->f_mode & FMODE_READ)
4213 ret = seq_open(file, &ftrace_pid_sops);
4214
4215 return ret;
4216}
4217
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004218static ssize_t
4219ftrace_pid_write(struct file *filp, const char __user *ubuf,
4220 size_t cnt, loff_t *ppos)
4221{
Ingo Molnar457dc922009-11-23 11:03:28 +01004222 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004223 long val;
4224 int ret;
4225
4226 if (cnt >= sizeof(buf))
4227 return -EINVAL;
4228
4229 if (copy_from_user(&buf, ubuf, cnt))
4230 return -EFAULT;
4231
4232 buf[cnt] = 0;
4233
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004234 /*
4235 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4236 * to clean the filter quietly.
4237 */
Ingo Molnar457dc922009-11-23 11:03:28 +01004238 tmp = strstrip(buf);
4239 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004240 return 1;
4241
Ingo Molnar457dc922009-11-23 11:03:28 +01004242 ret = strict_strtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004243 if (ret < 0)
4244 return ret;
4245
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004246 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004247
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004248 return ret ? ret : cnt;
4249}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004250
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004251static int
4252ftrace_pid_release(struct inode *inode, struct file *file)
4253{
4254 if (file->f_mode & FMODE_READ)
4255 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004256
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004257 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004258}
4259
Steven Rostedt5e2336a02009-03-05 21:44:55 -05004260static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004261 .open = ftrace_pid_open,
4262 .write = ftrace_pid_write,
4263 .read = seq_read,
Namhyung Kim3a22cc72013-06-07 17:01:16 +08004264 .llseek = ftrace_filter_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004265 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004266};
4267
4268static __init int ftrace_init_debugfs(void)
4269{
4270 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004271
4272 d_tracer = tracing_init_dentry();
4273 if (!d_tracer)
4274 return 0;
4275
4276 ftrace_init_dyn_debugfs(d_tracer);
4277
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004278 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4279 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04004280
4281 ftrace_profile_debugfs(d_tracer);
4282
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004283 return 0;
4284}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004285fs_initcall(ftrace_init_debugfs);
4286
Steven Rostedt3d083392008-05-12 21:20:42 +02004287/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004288 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004289 *
4290 * This function should be used by panic code. It stops ftrace
4291 * but in a not so nice way. If you need to simply kill ftrace
4292 * from a non-atomic section, use ftrace_kill.
4293 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004294void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004295{
4296 ftrace_disabled = 1;
4297 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004298 clear_ftrace_function();
4299}
4300
4301/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04004302 * Test if ftrace is dead or not.
4303 */
4304int ftrace_is_dead(void)
4305{
4306 return ftrace_disabled;
4307}
4308
4309/**
Steven Rostedt3d083392008-05-12 21:20:42 +02004310 * register_ftrace_function - register a function for profiling
4311 * @ops - ops structure that holds the function for profiling.
4312 *
4313 * Register a function to be called by all functions in the
4314 * kernel.
4315 *
4316 * Note: @ops->func and all the functions it calls must be labeled
4317 * with "notrace", otherwise it will go into a
4318 * recursive loop.
4319 */
4320int register_ftrace_function(struct ftrace_ops *ops)
4321{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004322 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004323
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004324 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004325
Steven Rostedt45a4a232011-04-21 23:16:46 -04004326 if (unlikely(ftrace_disabled))
4327 goto out_unlock;
4328
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004329 ret = __register_ftrace_function(ops);
Steven Rostedtb8489142011-05-04 09:27:52 -04004330 if (!ret)
Steven Rostedta1cd6172011-05-23 15:24:25 -04004331 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04004332
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004333
Steven Rostedt45a4a232011-04-21 23:16:46 -04004334 out_unlock:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004335 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004336 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02004337}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004338EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02004339
4340/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01004341 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02004342 * @ops - ops structure that holds the function to unregister
4343 *
4344 * Unregister a function that was added to be called by ftrace profiling.
4345 */
4346int unregister_ftrace_function(struct ftrace_ops *ops)
4347{
4348 int ret;
4349
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004350 mutex_lock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004351 ret = __unregister_ftrace_function(ops);
Steven Rostedtb8489142011-05-04 09:27:52 -04004352 if (!ret)
4353 ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004354 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004355
4356 return ret;
4357}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004358EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004359
Ingo Molnare309b412008-05-12 21:20:51 +02004360int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004361ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004362 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004363 loff_t *ppos)
4364{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004365 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004366
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004367 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004368
Steven Rostedt45a4a232011-04-21 23:16:46 -04004369 if (unlikely(ftrace_disabled))
4370 goto out;
4371
4372 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004373
Li Zefana32c7762009-06-26 16:55:51 +08004374 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004375 goto out;
4376
Li Zefana32c7762009-06-26 16:55:51 +08004377 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004378
4379 if (ftrace_enabled) {
4380
4381 ftrace_startup_sysctl();
4382
4383 /* we are starting ftrace again */
Jan Kiszkab81d3242013-03-26 17:53:03 +01004384 if (ftrace_ops_list != &ftrace_list_end)
4385 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004386
4387 } else {
4388 /* stopping ftrace calls (just send to ftrace_stub) */
4389 ftrace_trace_function = ftrace_stub;
4390
4391 ftrace_shutdown_sysctl();
4392 }
4393
4394 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004395 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004396 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02004397}
Ingo Molnarf17845e2008-10-24 12:47:10 +02004398
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004399#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004400
Steven Rostedt597af812009-04-03 15:24:12 -04004401static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004402static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004403
Steven Rostedte49dc192008-12-02 23:50:05 -05004404int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4405{
4406 return 0;
4407}
4408
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004409/* The callbacks that hook a function */
4410trace_func_graph_ret_t ftrace_graph_return =
4411 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004412trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004413
4414/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4415static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4416{
4417 int i;
4418 int ret = 0;
4419 unsigned long flags;
4420 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4421 struct task_struct *g, *t;
4422
4423 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4424 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4425 * sizeof(struct ftrace_ret_stack),
4426 GFP_KERNEL);
4427 if (!ret_stack_list[i]) {
4428 start = 0;
4429 end = i;
4430 ret = -ENOMEM;
4431 goto free;
4432 }
4433 }
4434
4435 read_lock_irqsave(&tasklist_lock, flags);
4436 do_each_thread(g, t) {
4437 if (start == end) {
4438 ret = -EAGAIN;
4439 goto unlock;
4440 }
4441
4442 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01004443 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004444 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04004445 t->curr_ret_stack = -1;
4446 /* Make sure the tasks see the -1 first: */
4447 smp_wmb();
4448 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004449 }
4450 } while_each_thread(g, t);
4451
4452unlock:
4453 read_unlock_irqrestore(&tasklist_lock, flags);
4454free:
4455 for (i = start; i < end; i++)
4456 kfree(ret_stack_list[i]);
4457 return ret;
4458}
4459
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004460static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04004461ftrace_graph_probe_sched_switch(void *ignore,
4462 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004463{
4464 unsigned long long timestamp;
4465 int index;
4466
Steven Rostedtbe6f1642009-03-24 11:06:24 -04004467 /*
4468 * Does the user want to count the time a function was asleep.
4469 * If so, do not update the time stamps.
4470 */
4471 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4472 return;
4473
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004474 timestamp = trace_clock_local();
4475
4476 prev->ftrace_timestamp = timestamp;
4477
4478 /* only process tasks that we timestamped */
4479 if (!next->ftrace_timestamp)
4480 return;
4481
4482 /*
4483 * Update all the counters in next to make up for the
4484 * time next was sleeping.
4485 */
4486 timestamp -= next->ftrace_timestamp;
4487
4488 for (index = next->curr_ret_stack; index >= 0; index--)
4489 next->ret_stack[index].calltime += timestamp;
4490}
4491
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004492/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004493static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004494{
4495 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004496 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004497
4498 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4499 sizeof(struct ftrace_ret_stack *),
4500 GFP_KERNEL);
4501
4502 if (!ret_stack_list)
4503 return -ENOMEM;
4504
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004505 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04004506 for_each_online_cpu(cpu) {
4507 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05004508 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04004509 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004510
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004511 do {
4512 ret = alloc_retstack_tasklist(ret_stack_list);
4513 } while (ret == -EAGAIN);
4514
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004515 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04004516 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004517 if (ret)
4518 pr_info("ftrace_graph: Couldn't activate tracepoint"
4519 " probe to kernel_sched_switch\n");
4520 }
4521
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004522 kfree(ret_stack_list);
4523 return ret;
4524}
4525
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004526/*
4527 * Hibernation protection.
4528 * The state of the current task is too much unstable during
4529 * suspend/restore to disk. We want to protect against that.
4530 */
4531static int
4532ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4533 void *unused)
4534{
4535 switch (state) {
4536 case PM_HIBERNATION_PREPARE:
4537 pause_graph_tracing();
4538 break;
4539
4540 case PM_POST_HIBERNATION:
4541 unpause_graph_tracing();
4542 break;
4543 }
4544 return NOTIFY_DONE;
4545}
4546
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004547int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4548 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004549{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004550 int ret = 0;
4551
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004552 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004553
Steven Rostedt05ce5812009-03-24 00:18:31 -04004554 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04004555 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04004556 ret = -EBUSY;
4557 goto out;
4558 }
4559
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004560 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
4561 register_pm_notifier(&ftrace_suspend_notifier);
4562
Steven Rostedt597af812009-04-03 15:24:12 -04004563 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004564 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004565 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04004566 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004567 goto out;
4568 }
Steven Rostedte53a6312008-11-26 00:16:25 -05004569
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004570 ftrace_graph_return = retfunc;
4571 ftrace_graph_entry = entryfunc;
Steven Rostedte53a6312008-11-26 00:16:25 -05004572
Steven Rostedta1cd6172011-05-23 15:24:25 -04004573 ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004574
4575out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004576 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004577 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004578}
4579
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004580void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004581{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004582 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004583
Steven Rostedt597af812009-04-03 15:24:12 -04004584 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004585 goto out;
4586
Steven Rostedt597af812009-04-03 15:24:12 -04004587 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004588 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004589 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedtbd69c302011-05-03 21:55:54 -04004590 ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004591 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04004592 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004593
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004594 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004595 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004596}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004597
Steven Rostedt868baf02011-02-10 21:26:13 -05004598static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
4599
4600static void
4601graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
4602{
4603 atomic_set(&t->tracing_graph_pause, 0);
4604 atomic_set(&t->trace_overrun, 0);
4605 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004606 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05004607 smp_wmb();
4608 t->ret_stack = ret_stack;
4609}
4610
4611/*
4612 * Allocate a return stack for the idle task. May be the first
4613 * time through, or it may be done by CPU hotplug online.
4614 */
4615void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
4616{
4617 t->curr_ret_stack = -1;
4618 /*
4619 * The idle task has no parent, it either has its own
4620 * stack or no stack at all.
4621 */
4622 if (t->ret_stack)
4623 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
4624
4625 if (ftrace_graph_active) {
4626 struct ftrace_ret_stack *ret_stack;
4627
4628 ret_stack = per_cpu(idle_ret_stack, cpu);
4629 if (!ret_stack) {
4630 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4631 * sizeof(struct ftrace_ret_stack),
4632 GFP_KERNEL);
4633 if (!ret_stack)
4634 return;
4635 per_cpu(idle_ret_stack, cpu) = ret_stack;
4636 }
4637 graph_init_task(t, ret_stack);
4638 }
4639}
4640
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004641/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004642void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004643{
Steven Rostedt84047e32009-06-02 16:51:55 -04004644 /* Make sure we do not use the parent ret_stack */
4645 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05004646 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04004647
Steven Rostedt597af812009-04-03 15:24:12 -04004648 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04004649 struct ftrace_ret_stack *ret_stack;
4650
4651 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004652 * sizeof(struct ftrace_ret_stack),
4653 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04004654 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004655 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05004656 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04004657 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004658}
4659
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004660void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004661{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004662 struct ftrace_ret_stack *ret_stack = t->ret_stack;
4663
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004664 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004665 /* NULL must become visible to IRQs before we free it: */
4666 barrier();
4667
4668 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004669}
Steven Rostedt14a866c2008-12-02 23:50:02 -05004670
4671void ftrace_graph_stop(void)
4672{
4673 ftrace_stop();
4674}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004675#endif