blob: 8c8169bf5020bf332e91d441522282327dccc92b [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{
Steven Rostedt2b499382011-05-03 22:49:52 -0400315 if (FTRACE_WARN_ON(ops == &global_ops))
316 return -EINVAL;
317
Steven Rostedtb8489142011-05-04 09:27:52 -0400318 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
319 return -EBUSY;
320
Jiri Olsae2484912012-02-15 15:51:48 +0100321 /* We don't support both control and global flags set. */
322 if ((ops->flags & FL_GLOBAL_CONTROL_MASK) == FL_GLOBAL_CONTROL_MASK)
323 return -EINVAL;
324
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400325 if (!core_kernel_data((unsigned long)ops))
326 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
327
Steven Rostedtb8489142011-05-04 09:27:52 -0400328 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100329 add_ftrace_list_ops(&ftrace_global_list, &global_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400330 ops->flags |= FTRACE_OPS_FL_ENABLED;
Jiri Olsae2484912012-02-15 15:51:48 +0100331 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
332 if (control_ops_alloc(ops))
333 return -ENOMEM;
334 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400335 } else
336 add_ftrace_ops(&ftrace_ops_list, ops);
337
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400338 if (ftrace_enabled)
339 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200340
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200341 return 0;
342}
343
Ingo Molnare309b412008-05-12 21:20:51 +0200344static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200345{
Steven Rostedt2b499382011-05-03 22:49:52 -0400346 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200347
Steven Rostedtb8489142011-05-04 09:27:52 -0400348 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
349 return -EBUSY;
350
Steven Rostedt2b499382011-05-03 22:49:52 -0400351 if (FTRACE_WARN_ON(ops == &global_ops))
352 return -EINVAL;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200353
Steven Rostedtb8489142011-05-04 09:27:52 -0400354 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100355 ret = remove_ftrace_list_ops(&ftrace_global_list,
356 &global_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400357 if (!ret)
358 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Jiri Olsae2484912012-02-15 15:51:48 +0100359 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
360 ret = remove_ftrace_list_ops(&ftrace_control_list,
361 &control_ops, ops);
362 if (!ret) {
363 /*
364 * The ftrace_ops is now removed from the list,
365 * so there'll be no new users. We must ensure
366 * all current users are done before we free
367 * the control data.
368 */
369 synchronize_sched();
370 control_ops_free(ops);
371 }
Steven Rostedtb8489142011-05-04 09:27:52 -0400372 } else
373 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
374
Steven Rostedt2b499382011-05-03 22:49:52 -0400375 if (ret < 0)
376 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400377
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400378 if (ftrace_enabled)
379 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200380
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400381 /*
382 * Dynamic ops may be freed, we must make sure that all
383 * callers are done before leaving this function.
384 */
385 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
386 synchronize_sched();
387
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500388 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200389}
390
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500391static void ftrace_update_pid_func(void)
392{
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400393 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500394 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900395 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500396
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400397 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500398}
399
Steven Rostedt493762f2009-03-23 17:12:36 -0400400#ifdef CONFIG_FUNCTION_PROFILER
401struct ftrace_profile {
402 struct hlist_node node;
403 unsigned long ip;
404 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400405#ifdef CONFIG_FUNCTION_GRAPH_TRACER
406 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400407 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400408#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400409};
410
411struct ftrace_profile_page {
412 struct ftrace_profile_page *next;
413 unsigned long index;
414 struct ftrace_profile records[];
415};
416
Steven Rostedtcafb1682009-03-24 20:50:39 -0400417struct ftrace_profile_stat {
418 atomic_t disabled;
419 struct hlist_head *hash;
420 struct ftrace_profile_page *pages;
421 struct ftrace_profile_page *start;
422 struct tracer_stat stat;
423};
424
Steven Rostedt493762f2009-03-23 17:12:36 -0400425#define PROFILE_RECORDS_SIZE \
426 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
427
428#define PROFILES_PER_PAGE \
429 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
430
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400431static int ftrace_profile_bits __read_mostly;
432static int ftrace_profile_enabled __read_mostly;
433
434/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400435static DEFINE_MUTEX(ftrace_profile_lock);
436
Steven Rostedtcafb1682009-03-24 20:50:39 -0400437static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400438
439#define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
440
Steven Rostedt493762f2009-03-23 17:12:36 -0400441static void *
442function_stat_next(void *v, int idx)
443{
444 struct ftrace_profile *rec = v;
445 struct ftrace_profile_page *pg;
446
447 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
448
449 again:
Li Zefan0296e422009-06-26 11:15:37 +0800450 if (idx != 0)
451 rec++;
452
Steven Rostedt493762f2009-03-23 17:12:36 -0400453 if ((void *)rec >= (void *)&pg->records[pg->index]) {
454 pg = pg->next;
455 if (!pg)
456 return NULL;
457 rec = &pg->records[0];
458 if (!rec->counter)
459 goto again;
460 }
461
462 return rec;
463}
464
465static void *function_stat_start(struct tracer_stat *trace)
466{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400467 struct ftrace_profile_stat *stat =
468 container_of(trace, struct ftrace_profile_stat, stat);
469
470 if (!stat || !stat->start)
471 return NULL;
472
473 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400474}
475
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400476#ifdef CONFIG_FUNCTION_GRAPH_TRACER
477/* function graph compares on total time */
478static int function_stat_cmp(void *p1, void *p2)
479{
480 struct ftrace_profile *a = p1;
481 struct ftrace_profile *b = p2;
482
483 if (a->time < b->time)
484 return -1;
485 if (a->time > b->time)
486 return 1;
487 else
488 return 0;
489}
490#else
491/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400492static int function_stat_cmp(void *p1, void *p2)
493{
494 struct ftrace_profile *a = p1;
495 struct ftrace_profile *b = p2;
496
497 if (a->counter < b->counter)
498 return -1;
499 if (a->counter > b->counter)
500 return 1;
501 else
502 return 0;
503}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400504#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400505
506static int function_stat_headers(struct seq_file *m)
507{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400508#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400509 seq_printf(m, " Function "
Chase Douglase330b3b2010-04-26 14:02:05 -0400510 "Hit Time Avg s^2\n"
Steven Rostedt34886c82009-03-25 21:00:47 -0400511 " -------- "
Chase Douglase330b3b2010-04-26 14:02:05 -0400512 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400513#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400514 seq_printf(m, " Function Hit\n"
515 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400516#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400517 return 0;
518}
519
520static int function_stat_show(struct seq_file *m, void *v)
521{
522 struct ftrace_profile *rec = v;
523 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800524 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400525#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400526 static struct trace_seq s;
527 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400528 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400529#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800530 mutex_lock(&ftrace_profile_lock);
531
532 /* we raced with function_profile_reset() */
533 if (unlikely(rec->counter == 0)) {
534 ret = -EBUSY;
535 goto out;
536 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400537
538 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400539 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400540
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400541#ifdef CONFIG_FUNCTION_GRAPH_TRACER
542 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400543 avg = rec->time;
544 do_div(avg, rec->counter);
545
Chase Douglase330b3b2010-04-26 14:02:05 -0400546 /* Sample standard deviation (s^2) */
547 if (rec->counter <= 1)
548 stddev = 0;
549 else {
550 stddev = rec->time_squared - rec->counter * avg * avg;
551 /*
552 * Divide only 1000 for ns^2 -> us^2 conversion.
553 * trace_print_graph_duration will divide 1000 again.
554 */
555 do_div(stddev, (rec->counter - 1) * 1000);
556 }
557
Steven Rostedt34886c82009-03-25 21:00:47 -0400558 trace_seq_init(&s);
559 trace_print_graph_duration(rec->time, &s);
560 trace_seq_puts(&s, " ");
561 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400562 trace_seq_puts(&s, " ");
563 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400564 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400565#endif
566 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800567out:
568 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400569
Li Zefan3aaba202010-08-23 16:50:12 +0800570 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400571}
572
Steven Rostedtcafb1682009-03-24 20:50:39 -0400573static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400574{
575 struct ftrace_profile_page *pg;
576
Steven Rostedtcafb1682009-03-24 20:50:39 -0400577 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400578
579 while (pg) {
580 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
581 pg->index = 0;
582 pg = pg->next;
583 }
584
Steven Rostedtcafb1682009-03-24 20:50:39 -0400585 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400586 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
587}
588
Steven Rostedtcafb1682009-03-24 20:50:39 -0400589int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400590{
591 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400592 int functions;
593 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400594 int i;
595
596 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400597 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400598 return 0;
599
Steven Rostedtcafb1682009-03-24 20:50:39 -0400600 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
601 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400602 return -ENOMEM;
603
Steven Rostedt318e0a72009-03-25 20:06:34 -0400604#ifdef CONFIG_DYNAMIC_FTRACE
605 functions = ftrace_update_tot_cnt;
606#else
607 /*
608 * We do not know the number of functions that exist because
609 * dynamic tracing is what counts them. With past experience
610 * we have around 20K functions. That should be more than enough.
611 * It is highly unlikely we will execute every function in
612 * the kernel.
613 */
614 functions = 20000;
615#endif
616
Steven Rostedtcafb1682009-03-24 20:50:39 -0400617 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400618
Steven Rostedt318e0a72009-03-25 20:06:34 -0400619 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
620
Namhyung Kim226c8ea2013-04-01 21:46:24 +0900621 for (i = 1; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400622 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400623 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400624 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400625 pg = pg->next;
626 }
627
628 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400629
630 out_free:
631 pg = stat->start;
632 while (pg) {
633 unsigned long tmp = (unsigned long)pg;
634
635 pg = pg->next;
636 free_page(tmp);
637 }
638
Steven Rostedt318e0a72009-03-25 20:06:34 -0400639 stat->pages = NULL;
640 stat->start = NULL;
641
642 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400643}
644
Steven Rostedtcafb1682009-03-24 20:50:39 -0400645static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400646{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400647 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400648 int size;
649
Steven Rostedtcafb1682009-03-24 20:50:39 -0400650 stat = &per_cpu(ftrace_profile_stats, cpu);
651
652 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400653 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400654 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400655 return 0;
656 }
657
658 /*
659 * We are profiling all functions, but usually only a few thousand
660 * functions are hit. We'll make a hash of 1024 items.
661 */
662 size = FTRACE_PROFILE_HASH_SIZE;
663
Steven Rostedtcafb1682009-03-24 20:50:39 -0400664 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400665
Steven Rostedtcafb1682009-03-24 20:50:39 -0400666 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400667 return -ENOMEM;
668
Steven Rostedtcafb1682009-03-24 20:50:39 -0400669 if (!ftrace_profile_bits) {
670 size--;
Steven Rostedt493762f2009-03-23 17:12:36 -0400671
Steven Rostedtcafb1682009-03-24 20:50:39 -0400672 for (; size; size >>= 1)
673 ftrace_profile_bits++;
674 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400675
Steven Rostedt318e0a72009-03-25 20:06:34 -0400676 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400677 if (ftrace_profile_pages_init(stat) < 0) {
678 kfree(stat->hash);
679 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400680 return -ENOMEM;
681 }
682
683 return 0;
684}
685
Steven Rostedtcafb1682009-03-24 20:50:39 -0400686static int ftrace_profile_init(void)
687{
688 int cpu;
689 int ret = 0;
690
691 for_each_online_cpu(cpu) {
692 ret = ftrace_profile_init_cpu(cpu);
693 if (ret)
694 break;
695 }
696
697 return ret;
698}
699
Steven Rostedt493762f2009-03-23 17:12:36 -0400700/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400701static struct ftrace_profile *
702ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400703{
704 struct ftrace_profile *rec;
705 struct hlist_head *hhd;
706 struct hlist_node *n;
707 unsigned long key;
708
709 key = hash_long(ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400710 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400711
712 if (hlist_empty(hhd))
713 return NULL;
714
715 hlist_for_each_entry_rcu(rec, n, hhd, node) {
716 if (rec->ip == ip)
717 return rec;
718 }
719
720 return NULL;
721}
722
Steven Rostedtcafb1682009-03-24 20:50:39 -0400723static void ftrace_add_profile(struct ftrace_profile_stat *stat,
724 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400725{
726 unsigned long key;
727
728 key = hash_long(rec->ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400729 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400730}
731
Steven Rostedt318e0a72009-03-25 20:06:34 -0400732/*
733 * The memory is already allocated, this simply finds a new record to use.
734 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400735static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400736ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400737{
738 struct ftrace_profile *rec = NULL;
739
Steven Rostedt318e0a72009-03-25 20:06:34 -0400740 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400741 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400742 goto out;
743
Steven Rostedt493762f2009-03-23 17:12:36 -0400744 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400745 * Try to find the function again since an NMI
746 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400747 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400748 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400749 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400750 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400751
Steven Rostedtcafb1682009-03-24 20:50:39 -0400752 if (stat->pages->index == PROFILES_PER_PAGE) {
753 if (!stat->pages->next)
754 goto out;
755 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400756 }
757
Steven Rostedtcafb1682009-03-24 20:50:39 -0400758 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400759 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400760 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400761
Steven Rostedt493762f2009-03-23 17:12:36 -0400762 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400763 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400764
765 return rec;
766}
767
Steven Rostedt493762f2009-03-23 17:12:36 -0400768static void
769function_profile_call(unsigned long ip, unsigned long parent_ip)
770{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400771 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400772 struct ftrace_profile *rec;
773 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400774
775 if (!ftrace_profile_enabled)
776 return;
777
Steven Rostedt493762f2009-03-23 17:12:36 -0400778 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400779
780 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400781 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400782 goto out;
783
784 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400785 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400786 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400787 if (!rec)
788 goto out;
789 }
790
791 rec->counter++;
792 out:
793 local_irq_restore(flags);
794}
795
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400796#ifdef CONFIG_FUNCTION_GRAPH_TRACER
797static int profile_graph_entry(struct ftrace_graph_ent *trace)
798{
799 function_profile_call(trace->func, 0);
800 return 1;
801}
802
803static void profile_graph_return(struct ftrace_graph_ret *trace)
804{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400805 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400806 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400807 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400808 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400809
810 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400811 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400812 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400813 goto out;
814
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400815 /* If the calltime was zero'd ignore it */
816 if (!trace->calltime)
817 goto out;
818
Steven Rostedta2a16d62009-03-24 23:17:58 -0400819 calltime = trace->rettime - trace->calltime;
820
821 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
822 int index;
823
824 index = trace->depth;
825
826 /* Append this call time to the parent time to subtract */
827 if (index)
828 current->ret_stack[index - 1].subtime += calltime;
829
830 if (current->ret_stack[index].subtime < calltime)
831 calltime -= current->ret_stack[index].subtime;
832 else
833 calltime = 0;
834 }
835
Steven Rostedtcafb1682009-03-24 20:50:39 -0400836 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400837 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400838 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400839 rec->time_squared += calltime * calltime;
840 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400841
Steven Rostedtcafb1682009-03-24 20:50:39 -0400842 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400843 local_irq_restore(flags);
844}
845
846static int register_ftrace_profiler(void)
847{
848 return register_ftrace_graph(&profile_graph_return,
849 &profile_graph_entry);
850}
851
852static void unregister_ftrace_profiler(void)
853{
854 unregister_ftrace_graph();
855}
856#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100857static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400858 .func = function_profile_call,
Steven Rostedt493762f2009-03-23 17:12:36 -0400859};
860
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400861static int register_ftrace_profiler(void)
862{
863 return register_ftrace_function(&ftrace_profile_ops);
864}
865
866static void unregister_ftrace_profiler(void)
867{
868 unregister_ftrace_function(&ftrace_profile_ops);
869}
870#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
871
Steven Rostedt493762f2009-03-23 17:12:36 -0400872static ssize_t
873ftrace_profile_write(struct file *filp, const char __user *ubuf,
874 size_t cnt, loff_t *ppos)
875{
876 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400877 int ret;
878
Peter Huewe22fe9b52011-06-07 21:58:27 +0200879 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
880 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400881 return ret;
882
883 val = !!val;
884
885 mutex_lock(&ftrace_profile_lock);
886 if (ftrace_profile_enabled ^ val) {
887 if (val) {
888 ret = ftrace_profile_init();
889 if (ret < 0) {
890 cnt = ret;
891 goto out;
892 }
893
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400894 ret = register_ftrace_profiler();
895 if (ret < 0) {
896 cnt = ret;
897 goto out;
898 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400899 ftrace_profile_enabled = 1;
900 } else {
901 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400902 /*
903 * unregister_ftrace_profiler calls stop_machine
904 * so this acts like an synchronize_sched.
905 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400906 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400907 }
908 }
909 out:
910 mutex_unlock(&ftrace_profile_lock);
911
Jiri Olsacf8517c2009-10-23 19:36:16 -0400912 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400913
914 return cnt;
915}
916
917static ssize_t
918ftrace_profile_read(struct file *filp, char __user *ubuf,
919 size_t cnt, loff_t *ppos)
920{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400921 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400922 int r;
923
924 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
925 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
926}
927
928static const struct file_operations ftrace_profile_fops = {
929 .open = tracing_open_generic,
930 .read = ftrace_profile_read,
931 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200932 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -0400933};
934
Steven Rostedtcafb1682009-03-24 20:50:39 -0400935/* used to initialize the real stat files */
936static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400937 .name = "functions",
938 .stat_start = function_stat_start,
939 .stat_next = function_stat_next,
940 .stat_cmp = function_stat_cmp,
941 .stat_headers = function_stat_headers,
942 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -0400943};
944
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400945static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400946{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400947 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400948 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400949 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -0400950 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400951 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -0400952
Steven Rostedtcafb1682009-03-24 20:50:39 -0400953 for_each_possible_cpu(cpu) {
954 stat = &per_cpu(ftrace_profile_stats, cpu);
955
956 /* allocate enough for function name + cpu number */
957 name = kmalloc(32, GFP_KERNEL);
958 if (!name) {
959 /*
960 * The files created are permanent, if something happens
961 * we still do not free memory.
962 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400963 WARN(1,
964 "Could not allocate stat file for cpu %d\n",
965 cpu);
966 return;
967 }
968 stat->stat = function_stats;
969 snprintf(name, 32, "function%d", cpu);
970 stat->stat.name = name;
971 ret = register_stat_tracer(&stat->stat);
972 if (ret) {
973 WARN(1,
974 "Could not register function stat for cpu %d\n",
975 cpu);
976 kfree(name);
977 return;
978 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400979 }
980
981 entry = debugfs_create_file("function_profile_enabled", 0644,
982 d_tracer, NULL, &ftrace_profile_fops);
983 if (!entry)
984 pr_warning("Could not create debugfs "
985 "'function_profile_enabled' entry\n");
986}
987
988#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400989static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400990{
991}
992#endif /* CONFIG_FUNCTION_PROFILER */
993
Ingo Molnar73d3fd92009-02-17 11:48:18 +0100994static struct pid * const ftrace_swapper_pid = &init_struct_pid;
995
Steven Rostedtbc4d36c2013-06-07 17:02:08 +0800996loff_t
997ftrace_filter_lseek(struct file *file, loff_t offset, int whence)
998{
999 loff_t ret;
1000
1001 if (file->f_mode & FMODE_READ)
1002 ret = seq_lseek(file, offset, whence);
1003 else
1004 file->f_pos = ret = 1;
1005
1006 return ret;
1007}
1008
Steven Rostedt3d083392008-05-12 21:20:42 +02001009#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001010
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001011#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001012# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001013#endif
1014
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001015static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1016
Steven Rostedtb6887d72009-02-17 12:32:04 -05001017struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001018 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -05001019 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001020 unsigned long flags;
1021 unsigned long ip;
1022 void *data;
1023 struct rcu_head rcu;
1024};
1025
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001026struct ftrace_func_entry {
1027 struct hlist_node hlist;
1028 unsigned long ip;
1029};
1030
1031struct ftrace_hash {
1032 unsigned long size_bits;
1033 struct hlist_head *buckets;
1034 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001035 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001036};
1037
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001038/*
1039 * We make these constant because no one should touch them,
1040 * but they are used as the default "empty hash", to avoid allocating
1041 * it all the time. These are in a read only section such that if
1042 * anyone does try to modify it, it will cause an exception.
1043 */
1044static const struct hlist_head empty_buckets[1];
1045static const struct ftrace_hash empty_hash = {
1046 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001047};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001048#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001049
Steven Rostedt2b499382011-05-03 22:49:52 -04001050static struct ftrace_ops global_ops = {
Steven Rostedtf45948e2011-05-02 12:29:25 -04001051 .func = ftrace_stub,
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001052 .notrace_hash = EMPTY_HASH,
1053 .filter_hash = EMPTY_HASH,
Steven Rostedtf45948e2011-05-02 12:29:25 -04001054};
1055
Steven Rostedt41c52c02008-05-22 11:46:33 -04001056static DEFINE_MUTEX(ftrace_regex_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02001057
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001058struct ftrace_page {
1059 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001060 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001061 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001062 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001063};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001064
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001065static struct ftrace_page *ftrace_new_pgs;
1066
Steven Rostedta7900872011-12-16 16:23:44 -05001067#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1068#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001069
1070/* estimate from running different kernels */
1071#define NR_TO_INIT 10000
1072
1073static struct ftrace_page *ftrace_pages_start;
1074static struct ftrace_page *ftrace_pages;
1075
Steven Rostedt06a51d92011-12-19 19:07:36 -05001076static bool ftrace_hash_empty(struct ftrace_hash *hash)
1077{
1078 return !hash || !hash->count;
1079}
1080
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001081static struct ftrace_func_entry *
1082ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1083{
1084 unsigned long key;
1085 struct ftrace_func_entry *entry;
1086 struct hlist_head *hhd;
1087 struct hlist_node *n;
1088
Steven Rostedt06a51d92011-12-19 19:07:36 -05001089 if (ftrace_hash_empty(hash))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001090 return NULL;
1091
1092 if (hash->size_bits > 0)
1093 key = hash_long(ip, hash->size_bits);
1094 else
1095 key = 0;
1096
1097 hhd = &hash->buckets[key];
1098
1099 hlist_for_each_entry_rcu(entry, n, hhd, hlist) {
1100 if (entry->ip == ip)
1101 return entry;
1102 }
1103 return NULL;
1104}
1105
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001106static void __add_hash_entry(struct ftrace_hash *hash,
1107 struct ftrace_func_entry *entry)
1108{
1109 struct hlist_head *hhd;
1110 unsigned long key;
1111
1112 if (hash->size_bits)
1113 key = hash_long(entry->ip, hash->size_bits);
1114 else
1115 key = 0;
1116
1117 hhd = &hash->buckets[key];
1118 hlist_add_head(&entry->hlist, hhd);
1119 hash->count++;
1120}
1121
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001122static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1123{
1124 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001125
1126 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1127 if (!entry)
1128 return -ENOMEM;
1129
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001130 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001131 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001132
1133 return 0;
1134}
1135
1136static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001137free_hash_entry(struct ftrace_hash *hash,
1138 struct ftrace_func_entry *entry)
1139{
1140 hlist_del(&entry->hlist);
1141 kfree(entry);
1142 hash->count--;
1143}
1144
1145static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001146remove_hash_entry(struct ftrace_hash *hash,
1147 struct ftrace_func_entry *entry)
1148{
1149 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001150 hash->count--;
1151}
1152
1153static void ftrace_hash_clear(struct ftrace_hash *hash)
1154{
1155 struct hlist_head *hhd;
1156 struct hlist_node *tp, *tn;
1157 struct ftrace_func_entry *entry;
1158 int size = 1 << hash->size_bits;
1159 int i;
1160
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001161 if (!hash->count)
1162 return;
1163
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001164 for (i = 0; i < size; i++) {
1165 hhd = &hash->buckets[i];
1166 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001167 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001168 }
1169 FTRACE_WARN_ON(hash->count);
1170}
1171
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001172static void free_ftrace_hash(struct ftrace_hash *hash)
1173{
1174 if (!hash || hash == EMPTY_HASH)
1175 return;
1176 ftrace_hash_clear(hash);
1177 kfree(hash->buckets);
1178 kfree(hash);
1179}
1180
Steven Rostedt07fd5512011-05-05 18:03:47 -04001181static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1182{
1183 struct ftrace_hash *hash;
1184
1185 hash = container_of(rcu, struct ftrace_hash, rcu);
1186 free_ftrace_hash(hash);
1187}
1188
1189static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1190{
1191 if (!hash || hash == EMPTY_HASH)
1192 return;
1193 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1194}
1195
Jiri Olsa5500fa52012-02-15 15:51:54 +01001196void ftrace_free_filter(struct ftrace_ops *ops)
1197{
1198 free_ftrace_hash(ops->filter_hash);
1199 free_ftrace_hash(ops->notrace_hash);
1200}
1201
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001202static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1203{
1204 struct ftrace_hash *hash;
1205 int size;
1206
1207 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1208 if (!hash)
1209 return NULL;
1210
1211 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001212 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001213
1214 if (!hash->buckets) {
1215 kfree(hash);
1216 return NULL;
1217 }
1218
1219 hash->size_bits = size_bits;
1220
1221 return hash;
1222}
1223
1224static struct ftrace_hash *
1225alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1226{
1227 struct ftrace_func_entry *entry;
1228 struct ftrace_hash *new_hash;
1229 struct hlist_node *tp;
1230 int size;
1231 int ret;
1232 int i;
1233
1234 new_hash = alloc_ftrace_hash(size_bits);
1235 if (!new_hash)
1236 return NULL;
1237
1238 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001239 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001240 return new_hash;
1241
1242 size = 1 << hash->size_bits;
1243 for (i = 0; i < size; i++) {
1244 hlist_for_each_entry(entry, tp, &hash->buckets[i], hlist) {
1245 ret = add_hash_entry(new_hash, entry->ip);
1246 if (ret < 0)
1247 goto free_hash;
1248 }
1249 }
1250
1251 FTRACE_WARN_ON(new_hash->count != hash->count);
1252
1253 return new_hash;
1254
1255 free_hash:
1256 free_ftrace_hash(new_hash);
1257 return NULL;
1258}
1259
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001260static void
1261ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1262static void
1263ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1264
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001265static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001266ftrace_hash_move(struct ftrace_ops *ops, int enable,
1267 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001268{
1269 struct ftrace_func_entry *entry;
1270 struct hlist_node *tp, *tn;
1271 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001272 struct ftrace_hash *old_hash;
1273 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001274 unsigned long key;
1275 int size = src->count;
1276 int bits = 0;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001277 int ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001278 int i;
1279
1280 /*
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001281 * Remove the current set, update the hash and add
1282 * them back.
1283 */
1284 ftrace_hash_rec_disable(ops, enable);
1285
1286 /*
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001287 * If the new source is empty, just free dst and assign it
1288 * the empty_hash.
1289 */
1290 if (!src->count) {
Steven Rostedt07fd5512011-05-05 18:03:47 -04001291 free_ftrace_hash_rcu(*dst);
1292 rcu_assign_pointer(*dst, EMPTY_HASH);
Steven Rostedtd4d34b92011-11-04 20:32:39 -04001293 /* still need to update the function records */
1294 ret = 0;
1295 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001296 }
1297
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001298 /*
1299 * Make the hash size about 1/2 the # found
1300 */
1301 for (size /= 2; size; size >>= 1)
1302 bits++;
1303
1304 /* Don't allocate too much */
1305 if (bits > FTRACE_HASH_MAX_BITS)
1306 bits = FTRACE_HASH_MAX_BITS;
1307
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001308 ret = -ENOMEM;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001309 new_hash = alloc_ftrace_hash(bits);
1310 if (!new_hash)
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001311 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001312
1313 size = 1 << src->size_bits;
1314 for (i = 0; i < size; i++) {
1315 hhd = &src->buckets[i];
1316 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist) {
1317 if (bits > 0)
1318 key = hash_long(entry->ip, bits);
1319 else
1320 key = 0;
1321 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001322 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001323 }
1324 }
1325
Steven Rostedt07fd5512011-05-05 18:03:47 -04001326 old_hash = *dst;
1327 rcu_assign_pointer(*dst, new_hash);
1328 free_ftrace_hash_rcu(old_hash);
1329
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001330 ret = 0;
1331 out:
1332 /*
1333 * Enable regardless of ret:
1334 * On success, we enable the new hash.
1335 * On failure, we re-enable the original hash.
1336 */
1337 ftrace_hash_rec_enable(ops, enable);
1338
1339 return ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001340}
1341
Steven Rostedt265c8312009-02-13 12:43:56 -05001342/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001343 * Test the hashes for this ops to see if we want to call
1344 * the ops->func or not.
1345 *
1346 * It's a match if the ip is in the ops->filter_hash or
1347 * the filter_hash does not exist or is empty,
1348 * AND
1349 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001350 *
1351 * This needs to be called with preemption disabled as
1352 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001353 */
1354static int
1355ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
1356{
1357 struct ftrace_hash *filter_hash;
1358 struct ftrace_hash *notrace_hash;
1359 int ret;
1360
Steven Rostedtb8489142011-05-04 09:27:52 -04001361 filter_hash = rcu_dereference_raw(ops->filter_hash);
1362 notrace_hash = rcu_dereference_raw(ops->notrace_hash);
1363
Steven Rostedt06a51d92011-12-19 19:07:36 -05001364 if ((ftrace_hash_empty(filter_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001365 ftrace_lookup_ip(filter_hash, ip)) &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001366 (ftrace_hash_empty(notrace_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001367 !ftrace_lookup_ip(notrace_hash, ip)))
1368 ret = 1;
1369 else
1370 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001371
1372 return ret;
1373}
1374
1375/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001376 * This is a double for. Do not use 'break' to break out of the loop,
1377 * you must use a goto.
1378 */
1379#define do_for_each_ftrace_rec(pg, rec) \
1380 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1381 int _____i; \
1382 for (_____i = 0; _____i < pg->index; _____i++) { \
1383 rec = &pg->records[_____i];
1384
1385#define while_for_each_ftrace_rec() \
1386 } \
1387 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301388
Steven Rostedt5855fea2011-12-16 19:27:42 -05001389
1390static int ftrace_cmp_recs(const void *a, const void *b)
1391{
1392 const struct dyn_ftrace *reca = a;
1393 const struct dyn_ftrace *recb = b;
1394
1395 if (reca->ip > recb->ip)
1396 return 1;
1397 if (reca->ip < recb->ip)
1398 return -1;
1399 return 0;
1400}
1401
Steven Rostedtc88fd862011-08-16 09:53:39 -04001402/**
1403 * ftrace_location - return true if the ip giving is a traced location
1404 * @ip: the instruction pointer to check
1405 *
1406 * Returns 1 if @ip given is a pointer to a ftrace location.
1407 * That is, the instruction that is either a NOP or call to
1408 * the function tracer. It checks the ftrace internal tables to
1409 * determine if the address belongs or not.
1410 */
1411int ftrace_location(unsigned long ip)
1412{
1413 struct ftrace_page *pg;
1414 struct dyn_ftrace *rec;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001415 struct dyn_ftrace key;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001416
Steven Rostedt5855fea2011-12-16 19:27:42 -05001417 key.ip = ip;
1418
1419 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1420 rec = bsearch(&key, pg->records, pg->index,
1421 sizeof(struct dyn_ftrace),
1422 ftrace_cmp_recs);
1423 if (rec)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001424 return 1;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001425 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04001426
1427 return 0;
1428}
1429
Steven Rostedted926f92011-05-03 13:25:24 -04001430static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1431 int filter_hash,
1432 bool inc)
1433{
1434 struct ftrace_hash *hash;
1435 struct ftrace_hash *other_hash;
1436 struct ftrace_page *pg;
1437 struct dyn_ftrace *rec;
1438 int count = 0;
1439 int all = 0;
1440
1441 /* Only update if the ops has been registered */
1442 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1443 return;
1444
1445 /*
1446 * In the filter_hash case:
1447 * If the count is zero, we update all records.
1448 * Otherwise we just update the items in the hash.
1449 *
1450 * In the notrace_hash case:
1451 * We enable the update in the hash.
1452 * As disabling notrace means enabling the tracing,
1453 * and enabling notrace means disabling, the inc variable
1454 * gets inversed.
1455 */
1456 if (filter_hash) {
1457 hash = ops->filter_hash;
1458 other_hash = ops->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001459 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001460 all = 1;
1461 } else {
1462 inc = !inc;
1463 hash = ops->notrace_hash;
1464 other_hash = ops->filter_hash;
1465 /*
1466 * If the notrace hash has no items,
1467 * then there's nothing to do.
1468 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001469 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001470 return;
1471 }
1472
1473 do_for_each_ftrace_rec(pg, rec) {
1474 int in_other_hash = 0;
1475 int in_hash = 0;
1476 int match = 0;
1477
1478 if (all) {
1479 /*
1480 * Only the filter_hash affects all records.
1481 * Update if the record is not in the notrace hash.
1482 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001483 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001484 match = 1;
1485 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001486 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1487 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001488
1489 /*
1490 *
1491 */
1492 if (filter_hash && in_hash && !in_other_hash)
1493 match = 1;
1494 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001495 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001496 match = 1;
1497 }
1498 if (!match)
1499 continue;
1500
1501 if (inc) {
1502 rec->flags++;
1503 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1504 return;
1505 } else {
1506 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1507 return;
1508 rec->flags--;
1509 }
1510 count++;
1511 /* Shortcut, if we handled all records, we are done. */
1512 if (!all && count == hash->count)
1513 return;
1514 } while_for_each_ftrace_rec();
1515}
1516
1517static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1518 int filter_hash)
1519{
1520 __ftrace_hash_rec_update(ops, filter_hash, 0);
1521}
1522
1523static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1524 int filter_hash)
1525{
1526 __ftrace_hash_rec_update(ops, filter_hash, 1);
1527}
1528
Ingo Molnare309b412008-05-12 21:20:51 +02001529static struct dyn_ftrace *ftrace_alloc_dyn_node(unsigned long ip)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001530{
Steven Rostedta7900872011-12-16 16:23:44 -05001531 if (ftrace_pages->index == ftrace_pages->size) {
1532 /* We should have allocated enough */
1533 if (WARN_ON(!ftrace_pages->next))
1534 return NULL;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001535 ftrace_pages = ftrace_pages->next;
1536 }
1537
1538 return &ftrace_pages->records[ftrace_pages->index++];
1539}
1540
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001541static struct dyn_ftrace *
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001542ftrace_record_ip(unsigned long ip)
Steven Rostedt3d083392008-05-12 21:20:42 +02001543{
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001544 struct dyn_ftrace *rec;
Steven Rostedt3d083392008-05-12 21:20:42 +02001545
Steven Rostedtf3c7ac42008-11-14 16:21:19 -08001546 if (ftrace_disabled)
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001547 return NULL;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001548
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001549 rec = ftrace_alloc_dyn_node(ip);
1550 if (!rec)
1551 return NULL;
Steven Rostedt3d083392008-05-12 21:20:42 +02001552
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001553 rec->ip = ip;
Steven Rostedt3d083392008-05-12 21:20:42 +02001554
Steven Rostedt08f5ac902008-10-23 09:33:07 -04001555 return rec;
Steven Rostedt3d083392008-05-12 21:20:42 +02001556}
1557
Steven Rostedt05736a42008-09-22 14:55:47 -07001558static void print_ip_ins(const char *fmt, unsigned char *p)
1559{
1560 int i;
1561
1562 printk(KERN_CONT "%s", fmt);
1563
1564 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1565 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1566}
1567
Steven Rostedtc88fd862011-08-16 09:53:39 -04001568/**
1569 * ftrace_bug - report and shutdown function tracer
1570 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1571 * @ip: The address that failed
1572 *
1573 * The arch code that enables or disables the function tracing
1574 * can call ftrace_bug() when it has detected a problem in
1575 * modifying the code. @failed should be one of either:
1576 * EFAULT - if the problem happens on reading the @ip address
1577 * EINVAL - if what is read at @ip is not what was expected
1578 * EPERM - if the problem happens on writting to the @ip address
1579 */
1580void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001581{
1582 switch (failed) {
1583 case -EFAULT:
1584 FTRACE_WARN_ON_ONCE(1);
1585 pr_info("ftrace faulted on modifying ");
1586 print_ip_sym(ip);
1587 break;
1588 case -EINVAL:
1589 FTRACE_WARN_ON_ONCE(1);
1590 pr_info("ftrace failed to modify ");
1591 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001592 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001593 printk(KERN_CONT "\n");
1594 break;
1595 case -EPERM:
1596 FTRACE_WARN_ON_ONCE(1);
1597 pr_info("ftrace faulted on writing ");
1598 print_ip_sym(ip);
1599 break;
1600 default:
1601 FTRACE_WARN_ON_ONCE(1);
1602 pr_info("ftrace faulted on unknown error ");
1603 print_ip_sym(ip);
1604 }
1605}
1606
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001607
Masami Hiramatsu2cfa1972010-02-02 16:49:11 -05001608/* Return 1 if the address range is reserved for ftrace */
1609int ftrace_text_reserved(void *start, void *end)
1610{
1611 struct dyn_ftrace *rec;
1612 struct ftrace_page *pg;
1613
1614 do_for_each_ftrace_rec(pg, rec) {
1615 if (rec->ip <= (unsigned long)end &&
1616 rec->ip + MCOUNT_INSN_SIZE > (unsigned long)start)
1617 return 1;
1618 } while_for_each_ftrace_rec();
1619 return 0;
1620}
1621
Steven Rostedtc88fd862011-08-16 09:53:39 -04001622static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02001623{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001624 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001625
Steven Rostedt982c3502008-11-15 16:31:41 -05001626 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001627 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05001628 *
Steven Rostedted926f92011-05-03 13:25:24 -04001629 * If the record has a ref count, then we need to enable it
1630 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001631 *
Steven Rostedted926f92011-05-03 13:25:24 -04001632 * Otherwise we make sure its disabled.
1633 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001634 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04001635 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001636 */
Steven Rostedtc88fd862011-08-16 09:53:39 -04001637 if (enable && (rec->flags & ~FTRACE_FL_MASK))
Steven Rostedted926f92011-05-03 13:25:24 -04001638 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001639
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001640 /* If the state of this record hasn't changed, then do nothing */
1641 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001642 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001643
1644 if (flag) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04001645 if (update)
1646 rec->flags |= FTRACE_FL_ENABLED;
1647 return FTRACE_UPDATE_MAKE_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001648 }
1649
Steven Rostedtc88fd862011-08-16 09:53:39 -04001650 if (update)
1651 rec->flags &= ~FTRACE_FL_ENABLED;
1652
1653 return FTRACE_UPDATE_MAKE_NOP;
1654}
1655
1656/**
1657 * ftrace_update_record, set a record that now is tracing or not
1658 * @rec: the record to update
1659 * @enable: set to 1 if the record is tracing, zero to force disable
1660 *
1661 * The records that represent all functions that can be traced need
1662 * to be updated when tracing has been enabled.
1663 */
1664int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1665{
1666 return ftrace_check_record(rec, enable, 1);
1667}
1668
1669/**
1670 * ftrace_test_record, check if the record has been enabled or not
1671 * @rec: the record to test
1672 * @enable: set to 1 to check if enabled, 0 if it is disabled
1673 *
1674 * The arch code may need to test if a record is already set to
1675 * tracing to determine how to modify the function code that it
1676 * represents.
1677 */
1678int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1679{
1680 return ftrace_check_record(rec, enable, 0);
1681}
1682
1683static int
1684__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1685{
1686 unsigned long ftrace_addr;
1687 int ret;
1688
1689 ftrace_addr = (unsigned long)FTRACE_ADDR;
1690
1691 ret = ftrace_update_record(rec, enable);
1692
1693 switch (ret) {
1694 case FTRACE_UPDATE_IGNORE:
1695 return 0;
1696
1697 case FTRACE_UPDATE_MAKE_CALL:
1698 return ftrace_make_call(rec, ftrace_addr);
1699
1700 case FTRACE_UPDATE_MAKE_NOP:
1701 return ftrace_make_nop(NULL, rec, ftrace_addr);
1702 }
1703
1704 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02001705}
1706
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001707static void ftrace_replace_code(int update)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001708{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001709 struct dyn_ftrace *rec;
1710 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001711 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001712
Steven Rostedt45a4a232011-04-21 23:16:46 -04001713 if (unlikely(ftrace_disabled))
1714 return;
1715
Steven Rostedt265c8312009-02-13 12:43:56 -05001716 do_for_each_ftrace_rec(pg, rec) {
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001717 failed = __ftrace_replace_code(rec, update);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001718 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04001719 ftrace_bug(failed, rec->ip);
1720 /* Stop processing */
1721 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001722 }
1723 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001724}
1725
Steven Rostedtc88fd862011-08-16 09:53:39 -04001726struct ftrace_rec_iter {
1727 struct ftrace_page *pg;
1728 int index;
1729};
1730
1731/**
1732 * ftrace_rec_iter_start, start up iterating over traced functions
1733 *
1734 * Returns an iterator handle that is used to iterate over all
1735 * the records that represent address locations where functions
1736 * are traced.
1737 *
1738 * May return NULL if no records are available.
1739 */
1740struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1741{
1742 /*
1743 * We only use a single iterator.
1744 * Protected by the ftrace_lock mutex.
1745 */
1746 static struct ftrace_rec_iter ftrace_rec_iter;
1747 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1748
1749 iter->pg = ftrace_pages_start;
1750 iter->index = 0;
1751
1752 /* Could have empty pages */
1753 while (iter->pg && !iter->pg->index)
1754 iter->pg = iter->pg->next;
1755
1756 if (!iter->pg)
1757 return NULL;
1758
1759 return iter;
1760}
1761
1762/**
1763 * ftrace_rec_iter_next, get the next record to process.
1764 * @iter: The handle to the iterator.
1765 *
1766 * Returns the next iterator after the given iterator @iter.
1767 */
1768struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1769{
1770 iter->index++;
1771
1772 if (iter->index >= iter->pg->index) {
1773 iter->pg = iter->pg->next;
1774 iter->index = 0;
1775
1776 /* Could have empty pages */
1777 while (iter->pg && !iter->pg->index)
1778 iter->pg = iter->pg->next;
1779 }
1780
1781 if (!iter->pg)
1782 return NULL;
1783
1784 return iter;
1785}
1786
1787/**
1788 * ftrace_rec_iter_record, get the record at the iterator location
1789 * @iter: The current iterator location
1790 *
1791 * Returns the record that the current @iter is at.
1792 */
1793struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1794{
1795 return &iter->pg->records[iter->index];
1796}
1797
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301798static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001799ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001800{
1801 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001802 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001803
1804 ip = rec->ip;
1805
Steven Rostedt45a4a232011-04-21 23:16:46 -04001806 if (unlikely(ftrace_disabled))
1807 return 0;
1808
Shaohua Li25aac9d2009-01-09 11:29:40 +08001809 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001810 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001811 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301812 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02001813 }
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301814 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001815}
1816
Steven Rostedt000ab692009-02-17 13:35:06 -05001817/*
1818 * archs can override this function if they must do something
1819 * before the modifying code is performed.
1820 */
1821int __weak ftrace_arch_code_modify_prepare(void)
1822{
1823 return 0;
1824}
1825
1826/*
1827 * archs can override this function if they must do something
1828 * after the modifying code is performed.
1829 */
1830int __weak ftrace_arch_code_modify_post_process(void)
1831{
1832 return 0;
1833}
1834
Ingo Molnare309b412008-05-12 21:20:51 +02001835static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02001836{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001837 int *command = data;
1838
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001839 if (*command & FTRACE_UPDATE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001840 ftrace_replace_code(1);
Steven Rostedta3583242008-11-11 15:01:42 -05001841 else if (*command & FTRACE_DISABLE_CALLS)
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001842 ftrace_replace_code(0);
1843
1844 if (*command & FTRACE_UPDATE_TRACE_FUNC)
1845 ftrace_update_ftrace_func(ftrace_trace_function);
1846
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001847 if (*command & FTRACE_START_FUNC_RET)
1848 ftrace_enable_ftrace_graph_caller();
1849 else if (*command & FTRACE_STOP_FUNC_RET)
1850 ftrace_disable_ftrace_graph_caller();
1851
Steven Rostedtc88fd862011-08-16 09:53:39 -04001852 return 0;
1853}
1854
1855/**
1856 * ftrace_run_stop_machine, go back to the stop machine method
1857 * @command: The command to tell ftrace what to do
1858 *
1859 * If an arch needs to fall back to the stop machine method, the
1860 * it can call this function.
1861 */
1862void ftrace_run_stop_machine(int command)
1863{
1864 stop_machine(__ftrace_modify_code, &command, NULL);
1865}
1866
1867/**
1868 * arch_ftrace_update_code, modify the code to trace or not trace
1869 * @command: The command that needs to be done
1870 *
1871 * Archs can override this function if it does not need to
1872 * run stop_machine() to modify code.
1873 */
1874void __weak arch_ftrace_update_code(int command)
1875{
1876 ftrace_run_stop_machine(command);
1877}
1878
1879static void ftrace_run_update_code(int command)
1880{
1881 int ret;
1882
1883 ret = ftrace_arch_code_modify_prepare();
1884 FTRACE_WARN_ON(ret);
1885 if (ret)
1886 return;
1887 /*
1888 * Do not call function tracer while we update the code.
1889 * We are in stop machine.
1890 */
1891 function_trace_stop++;
1892
1893 /*
1894 * By default we use stop_machine() to modify the code.
1895 * But archs can do what ever they want as long as it
1896 * is safe. The stop_machine() is the safest, but also
1897 * produces the most overhead.
1898 */
1899 arch_ftrace_update_code(command);
1900
Steven Rostedt6331c282011-07-13 15:11:02 -04001901#ifndef CONFIG_HAVE_FUNCTION_TRACE_MCOUNT_TEST
1902 /*
1903 * For archs that call ftrace_test_stop_func(), we must
1904 * wait till after we update all the function callers
1905 * before we update the callback. This keeps different
1906 * ops that record different functions from corrupting
1907 * each other.
1908 */
1909 __ftrace_trace_function = __ftrace_trace_function_delay;
1910#endif
1911 function_trace_stop--;
1912
Steven Rostedt000ab692009-02-17 13:35:06 -05001913 ret = ftrace_arch_code_modify_post_process();
1914 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02001915}
1916
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001917static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001918static int ftrace_start_up;
Steven Rostedtb8489142011-05-04 09:27:52 -04001919static int global_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001920
1921static void ftrace_startup_enable(int command)
1922{
1923 if (saved_ftrace_func != ftrace_trace_function) {
1924 saved_ftrace_func = ftrace_trace_function;
1925 command |= FTRACE_UPDATE_TRACE_FUNC;
1926 }
1927
1928 if (!command || !ftrace_enabled)
1929 return;
1930
1931 ftrace_run_update_code(command);
1932}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001933
Steven Rostedta1cd6172011-05-23 15:24:25 -04001934static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001935{
Steven Rostedtb8489142011-05-04 09:27:52 -04001936 bool hash_enable = true;
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05001937 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04001938
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001939 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04001940 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001941
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05001942 ret = __register_ftrace_function(ops);
1943 if (ret)
1944 return ret;
1945
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001946 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001947 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02001948
Steven Rostedtb8489142011-05-04 09:27:52 -04001949 /* ops marked global share the filter hashes */
1950 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
1951 ops = &global_ops;
1952 /* Don't update hash if global is already set */
1953 if (global_start_up)
1954 hash_enable = false;
1955 global_start_up++;
1956 }
1957
Steven Rostedted926f92011-05-03 13:25:24 -04001958 ops->flags |= FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04001959 if (hash_enable)
Steven Rostedted926f92011-05-03 13:25:24 -04001960 ftrace_hash_rec_enable(ops, 1);
1961
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001962 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04001963
1964 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02001965}
1966
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05001967static int ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001968{
Steven Rostedtb8489142011-05-04 09:27:52 -04001969 bool hash_disable = true;
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05001970 int ret;
Steven Rostedtb8489142011-05-04 09:27:52 -04001971
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001972 if (unlikely(ftrace_disabled))
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05001973 return -ENODEV;
1974
1975 ret = __unregister_ftrace_function(ops);
1976 if (ret)
1977 return ret;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001978
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001979 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02001980 /*
1981 * Just warn in case of unbalance, no need to kill ftrace, it's not
1982 * critical but the ftrace_call callers may be never nopped again after
1983 * further ftrace uses.
1984 */
1985 WARN_ON_ONCE(ftrace_start_up < 0);
1986
Steven Rostedtb8489142011-05-04 09:27:52 -04001987 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
1988 ops = &global_ops;
1989 global_start_up--;
1990 WARN_ON_ONCE(global_start_up < 0);
1991 /* Don't update hash if global still has users */
1992 if (global_start_up) {
1993 WARN_ON_ONCE(!ftrace_start_up);
1994 hash_disable = false;
1995 }
1996 }
1997
1998 if (hash_disable)
Steven Rostedted926f92011-05-03 13:25:24 -04001999 ftrace_hash_rec_disable(ops, 1);
2000
Steven Rostedtb8489142011-05-04 09:27:52 -04002001 if (ops != &global_ops || !global_start_up)
Steven Rostedted926f92011-05-03 13:25:24 -04002002 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002003
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002004 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002005
2006 if (saved_ftrace_func != ftrace_trace_function) {
2007 saved_ftrace_func = ftrace_trace_function;
2008 command |= FTRACE_UPDATE_TRACE_FUNC;
2009 }
2010
2011 if (!command || !ftrace_enabled)
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05002012 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002013
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002014 ftrace_run_update_code(command);
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05002015 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002016}
2017
Ingo Molnare309b412008-05-12 21:20:51 +02002018static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002019{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002020 if (unlikely(ftrace_disabled))
2021 return;
2022
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002023 /* Force update next time */
2024 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002025 /* ftrace_start_up is true if we want ftrace running */
2026 if (ftrace_start_up)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002027 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002028}
2029
Ingo Molnare309b412008-05-12 21:20:51 +02002030static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002031{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002032 if (unlikely(ftrace_disabled))
2033 return;
2034
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002035 /* ftrace_start_up is true if ftrace is running */
2036 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04002037 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002038}
2039
Steven Rostedt3d083392008-05-12 21:20:42 +02002040static cycle_t ftrace_update_time;
2041static unsigned long ftrace_update_cnt;
2042unsigned long ftrace_update_tot_cnt;
2043
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002044static int ops_traces_mod(struct ftrace_ops *ops)
2045{
2046 struct ftrace_hash *hash;
2047
2048 hash = ops->filter_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05002049 return ftrace_hash_empty(hash);
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002050}
2051
Steven Rostedt31e88902008-11-14 16:21:19 -08002052static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02002053{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002054 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002055 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302056 cycle_t start, stop;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002057 unsigned long ref = 0;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002058 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002059
2060 /*
2061 * When adding a module, we need to check if tracers are
2062 * currently enabled and if they are set to trace all functions.
2063 * If they are, we need to enable the module functions as well
2064 * as update the reference counts for those function records.
2065 */
2066 if (mod) {
2067 struct ftrace_ops *ops;
2068
2069 for (ops = ftrace_ops_list;
2070 ops != &ftrace_list_end; ops = ops->next) {
2071 if (ops->flags & FTRACE_OPS_FL_ENABLED &&
2072 ops_traces_mod(ops))
2073 ref++;
2074 }
2075 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002076
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002077 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002078 ftrace_update_cnt = 0;
2079
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002080 for (pg = ftrace_new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302081
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002082 for (i = 0; i < pg->index; i++) {
2083 /* If something went wrong, bail without enabling anything */
2084 if (unlikely(ftrace_disabled))
2085 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002086
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002087 p = &pg->records[i];
2088 p->flags = ref;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302089
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002090 /*
2091 * Do the initial record conversion from mcount jump
2092 * to the NOP instructions.
2093 */
2094 if (!ftrace_code_disable(mod, p))
2095 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002096
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002097 ftrace_update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002098
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002099 /*
2100 * If the tracing is enabled, go ahead and enable the record.
2101 *
2102 * The reason not to enable the record immediatelly is the
2103 * inherent check of ftrace_make_nop/ftrace_make_call for
2104 * correct previous instructions. Making first the NOP
2105 * conversion puts the module to the correct state, thus
2106 * passing the ftrace_make_call check.
2107 */
2108 if (ftrace_start_up && ref) {
2109 int failed = __ftrace_replace_code(p, 1);
2110 if (failed)
2111 ftrace_bug(failed, p->ip);
2112 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002113 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002114 }
2115
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002116 ftrace_new_pgs = NULL;
2117
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002118 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002119 ftrace_update_time = stop - start;
2120 ftrace_update_tot_cnt += ftrace_update_cnt;
2121
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002122 return 0;
2123}
2124
Steven Rostedta7900872011-12-16 16:23:44 -05002125static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002126{
Steven Rostedta7900872011-12-16 16:23:44 -05002127 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002128 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002129
Steven Rostedta7900872011-12-16 16:23:44 -05002130 if (WARN_ON(!count))
2131 return -EINVAL;
2132
2133 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002134
2135 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002136 * We want to fill as much as possible. No more than a page
2137 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002138 */
Steven Rostedta7900872011-12-16 16:23:44 -05002139 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2140 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002141
Steven Rostedta7900872011-12-16 16:23:44 -05002142 again:
2143 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2144
2145 if (!pg->records) {
2146 /* if we can't allocate this size, try something smaller */
2147 if (!order)
2148 return -ENOMEM;
2149 order >>= 1;
2150 goto again;
2151 }
2152
2153 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2154 pg->size = cnt;
2155
2156 if (cnt > count)
2157 cnt = count;
2158
2159 return cnt;
2160}
2161
2162static struct ftrace_page *
2163ftrace_allocate_pages(unsigned long num_to_init)
2164{
2165 struct ftrace_page *start_pg;
2166 struct ftrace_page *pg;
2167 int order;
2168 int cnt;
2169
2170 if (!num_to_init)
2171 return 0;
2172
2173 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2174 if (!pg)
2175 return NULL;
2176
2177 /*
2178 * Try to allocate as much as possible in one continues
2179 * location that fills in all of the space. We want to
2180 * waste as little space as possible.
2181 */
2182 for (;;) {
2183 cnt = ftrace_allocate_records(pg, num_to_init);
2184 if (cnt < 0)
2185 goto free_pages;
2186
2187 num_to_init -= cnt;
2188 if (!num_to_init)
2189 break;
2190
2191 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2192 if (!pg->next)
2193 goto free_pages;
2194
2195 pg = pg->next;
2196 }
2197
2198 return start_pg;
2199
2200 free_pages:
2201 while (start_pg) {
2202 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2203 free_pages((unsigned long)pg->records, order);
2204 start_pg = pg->next;
2205 kfree(pg);
2206 pg = start_pg;
2207 }
2208 pr_info("ftrace: FAILED to allocate memory for functions\n");
2209 return NULL;
2210}
2211
2212static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2213{
2214 int cnt;
2215
2216 if (!num_to_init) {
2217 pr_info("ftrace: No functions to be traced?\n");
2218 return -1;
2219 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002220
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002221 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04002222 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08002223 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002224
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002225 return 0;
2226}
2227
Steven Rostedt5072c592008-05-12 21:20:43 +02002228#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2229
2230struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002231 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002232 loff_t func_pos;
2233 struct ftrace_page *pg;
2234 struct dyn_ftrace *func;
2235 struct ftrace_func_probe *probe;
2236 struct trace_parser parser;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002237 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002238 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002239 int hidx;
2240 int idx;
2241 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002242};
2243
Ingo Molnare309b412008-05-12 21:20:51 +02002244static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002245t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002246{
2247 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002248 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002249 struct hlist_head *hhd;
2250
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002251 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002252 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002253
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002254 if (iter->probe)
2255 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002256 retry:
2257 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2258 return NULL;
2259
2260 hhd = &ftrace_func_hash[iter->hidx];
2261
2262 if (hlist_empty(hhd)) {
2263 iter->hidx++;
2264 hnd = NULL;
2265 goto retry;
2266 }
2267
2268 if (!hnd)
2269 hnd = hhd->first;
2270 else {
2271 hnd = hnd->next;
2272 if (!hnd) {
2273 iter->hidx++;
2274 goto retry;
2275 }
2276 }
2277
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002278 if (WARN_ON_ONCE(!hnd))
2279 return NULL;
2280
2281 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2282
2283 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002284}
2285
2286static void *t_hash_start(struct seq_file *m, loff_t *pos)
2287{
2288 struct ftrace_iterator *iter = m->private;
2289 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08002290 loff_t l;
2291
Steven Rostedt69a30832011-12-19 15:21:16 -05002292 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2293 return NULL;
2294
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002295 if (iter->func_pos > *pos)
2296 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002297
Li Zefand82d6242009-06-24 09:54:54 +08002298 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002299 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002300 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08002301 if (!p)
2302 break;
2303 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002304 if (!p)
2305 return NULL;
2306
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002307 /* Only set this if we have an item */
2308 iter->flags |= FTRACE_ITER_HASH;
2309
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002310 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002311}
2312
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002313static int
2314t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002315{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002316 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002317
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002318 rec = iter->probe;
2319 if (WARN_ON_ONCE(!rec))
2320 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002321
Steven Rostedt809dcf22009-02-16 23:06:01 -05002322 if (rec->ops->print)
2323 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2324
Steven Rostedtb375a112009-09-17 00:05:58 -04002325 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002326
2327 if (rec->data)
2328 seq_printf(m, ":%p", rec->data);
2329 seq_putc(m, '\n');
2330
2331 return 0;
2332}
2333
2334static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02002335t_next(struct seq_file *m, void *v, loff_t *pos)
2336{
2337 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002338 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002339 struct dyn_ftrace *rec = NULL;
2340
Steven Rostedt45a4a232011-04-21 23:16:46 -04002341 if (unlikely(ftrace_disabled))
2342 return NULL;
2343
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002344 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002345 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002346
Steven Rostedt5072c592008-05-12 21:20:43 +02002347 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01002348 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02002349
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002350 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002351 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002352
Steven Rostedt5072c592008-05-12 21:20:43 +02002353 retry:
2354 if (iter->idx >= iter->pg->index) {
2355 if (iter->pg->next) {
2356 iter->pg = iter->pg->next;
2357 iter->idx = 0;
2358 goto retry;
2359 }
2360 } else {
2361 rec = &iter->pg->records[iter->idx++];
Steven Rostedt32082302011-12-16 14:42:37 -05002362 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedtf45948e2011-05-02 12:29:25 -04002363 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
Steven Rostedt0183fb12008-11-07 22:36:02 -05002364
Steven Rostedt41c52c02008-05-22 11:46:33 -04002365 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt647bcd02011-05-03 14:39:21 -04002366 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2367
2368 ((iter->flags & FTRACE_ITER_ENABLED) &&
2369 !(rec->flags & ~FTRACE_FL_MASK))) {
2370
Steven Rostedt5072c592008-05-12 21:20:43 +02002371 rec = NULL;
2372 goto retry;
2373 }
2374 }
2375
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002376 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002377 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002378
2379 iter->func = rec;
2380
2381 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002382}
2383
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002384static void reset_iter_read(struct ftrace_iterator *iter)
2385{
2386 iter->pos = 0;
2387 iter->func_pos = 0;
Dan Carpenter455d8952012-06-09 19:10:27 +03002388 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002389}
2390
2391static void *t_start(struct seq_file *m, loff_t *pos)
2392{
2393 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002394 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002395 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002396 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002397
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002398 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002399
2400 if (unlikely(ftrace_disabled))
2401 return NULL;
2402
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002403 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002404 * If an lseek was done, then reset and start from beginning.
2405 */
2406 if (*pos < iter->pos)
2407 reset_iter_read(iter);
2408
2409 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002410 * For set_ftrace_filter reading, if we have the filter
2411 * off, we can short cut and just print out that all
2412 * functions are enabled.
2413 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05002414 if (iter->flags & FTRACE_ITER_FILTER &&
2415 ftrace_hash_empty(ops->filter_hash)) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002416 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002417 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002418 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002419 /* reset in case of seek/pread */
2420 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002421 return iter;
2422 }
2423
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002424 if (iter->flags & FTRACE_ITER_HASH)
2425 return t_hash_start(m, pos);
2426
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002427 /*
2428 * Unfortunately, we need to restart at ftrace_pages_start
2429 * every time we let go of the ftrace_mutex. This is because
2430 * those pointers can change without the lock.
2431 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002432 iter->pg = ftrace_pages_start;
2433 iter->idx = 0;
2434 for (l = 0; l <= *pos; ) {
2435 p = t_next(m, p, &l);
2436 if (!p)
2437 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002438 }
walimis5821e1b2008-11-15 15:19:06 +08002439
Steven Rostedt69a30832011-12-19 15:21:16 -05002440 if (!p)
2441 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002442
2443 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002444}
2445
2446static void t_stop(struct seq_file *m, void *p)
2447{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002448 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002449}
2450
2451static int t_show(struct seq_file *m, void *v)
2452{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002453 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002454 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02002455
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002456 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002457 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002458
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002459 if (iter->flags & FTRACE_ITER_PRINTALL) {
2460 seq_printf(m, "#### all functions enabled ####\n");
2461 return 0;
2462 }
2463
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002464 rec = iter->func;
2465
Steven Rostedt5072c592008-05-12 21:20:43 +02002466 if (!rec)
2467 return 0;
2468
Steven Rostedt647bcd02011-05-03 14:39:21 -04002469 seq_printf(m, "%ps", (void *)rec->ip);
2470 if (iter->flags & FTRACE_ITER_ENABLED)
2471 seq_printf(m, " (%ld)",
2472 rec->flags & ~FTRACE_FL_MASK);
2473 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02002474
2475 return 0;
2476}
2477
James Morris88e9d342009-09-22 16:43:43 -07002478static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002479 .start = t_start,
2480 .next = t_next,
2481 .stop = t_stop,
2482 .show = t_show,
2483};
2484
Ingo Molnare309b412008-05-12 21:20:51 +02002485static int
Steven Rostedt5072c592008-05-12 21:20:43 +02002486ftrace_avail_open(struct inode *inode, struct file *file)
2487{
2488 struct ftrace_iterator *iter;
2489 int ret;
2490
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002491 if (unlikely(ftrace_disabled))
2492 return -ENODEV;
2493
Steven Rostedt5072c592008-05-12 21:20:43 +02002494 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2495 if (!iter)
2496 return -ENOMEM;
2497
2498 iter->pg = ftrace_pages_start;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002499 iter->ops = &global_ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002500
2501 ret = seq_open(file, &show_ftrace_seq_ops);
2502 if (!ret) {
2503 struct seq_file *m = file->private_data;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002504
Steven Rostedt5072c592008-05-12 21:20:43 +02002505 m->private = iter;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002506 } else {
Steven Rostedt5072c592008-05-12 21:20:43 +02002507 kfree(iter);
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002508 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002509
2510 return ret;
2511}
2512
Steven Rostedt647bcd02011-05-03 14:39:21 -04002513static int
2514ftrace_enabled_open(struct inode *inode, struct file *file)
2515{
2516 struct ftrace_iterator *iter;
2517 int ret;
2518
2519 if (unlikely(ftrace_disabled))
2520 return -ENODEV;
2521
2522 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2523 if (!iter)
2524 return -ENOMEM;
2525
2526 iter->pg = ftrace_pages_start;
2527 iter->flags = FTRACE_ITER_ENABLED;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002528 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002529
2530 ret = seq_open(file, &show_ftrace_seq_ops);
2531 if (!ret) {
2532 struct seq_file *m = file->private_data;
2533
2534 m->private = iter;
2535 } else {
2536 kfree(iter);
2537 }
2538
2539 return ret;
2540}
2541
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002542static void ftrace_filter_reset(struct ftrace_hash *hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02002543{
Steven Rostedt52baf112009-02-14 01:15:39 -05002544 mutex_lock(&ftrace_lock);
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002545 ftrace_hash_clear(hash);
Steven Rostedt52baf112009-02-14 01:15:39 -05002546 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002547}
2548
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002549/**
2550 * ftrace_regex_open - initialize function tracer filter files
2551 * @ops: The ftrace_ops that hold the hash filters
2552 * @flag: The type of filter to process
2553 * @inode: The inode, usually passed in to your open routine
2554 * @file: The file, usually passed in to your open routine
2555 *
2556 * ftrace_regex_open() initializes the filter files for the
2557 * @ops. Depending on @flag it may process the filter hash or
2558 * the notrace hash of @ops. With this called from the open
2559 * routine, you can use ftrace_filter_write() for the write
2560 * routine if @flag has FTRACE_ITER_FILTER set, or
2561 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedtbc4d36c2013-06-07 17:02:08 +08002562 * ftrace_filter_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002563 * release must call ftrace_regex_release().
2564 */
2565int
Steven Rostedtf45948e2011-05-02 12:29:25 -04002566ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002567 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02002568{
2569 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002570 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02002571 int ret = 0;
2572
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002573 if (unlikely(ftrace_disabled))
2574 return -ENODEV;
2575
Steven Rostedt5072c592008-05-12 21:20:43 +02002576 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2577 if (!iter)
2578 return -ENOMEM;
2579
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002580 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2581 kfree(iter);
2582 return -ENOMEM;
2583 }
2584
Steven Rostedtf45948e2011-05-02 12:29:25 -04002585 if (flag & FTRACE_ITER_NOTRACE)
2586 hash = ops->notrace_hash;
2587 else
2588 hash = ops->filter_hash;
2589
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002590 iter->ops = ops;
2591 iter->flags = flag;
2592
2593 if (file->f_mode & FMODE_WRITE) {
2594 mutex_lock(&ftrace_lock);
2595 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2596 mutex_unlock(&ftrace_lock);
2597
2598 if (!iter->hash) {
2599 trace_parser_put(&iter->parser);
2600 kfree(iter);
2601 return -ENOMEM;
2602 }
2603 }
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002604
Steven Rostedt41c52c02008-05-22 11:46:33 -04002605 mutex_lock(&ftrace_regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002606
Steven Rostedt5072c592008-05-12 21:20:43 +02002607 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002608 (file->f_flags & O_TRUNC))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002609 ftrace_filter_reset(iter->hash);
Steven Rostedt5072c592008-05-12 21:20:43 +02002610
2611 if (file->f_mode & FMODE_READ) {
2612 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02002613
2614 ret = seq_open(file, &show_ftrace_seq_ops);
2615 if (!ret) {
2616 struct seq_file *m = file->private_data;
2617 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08002618 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002619 /* Failed */
2620 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08002621 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002622 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08002623 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002624 } else
2625 file->private_data = iter;
Steven Rostedt41c52c02008-05-22 11:46:33 -04002626 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002627
2628 return ret;
2629}
2630
Steven Rostedt41c52c02008-05-22 11:46:33 -04002631static int
2632ftrace_filter_open(struct inode *inode, struct file *file)
2633{
Steven Rostedt69a30832011-12-19 15:21:16 -05002634 return ftrace_regex_open(&global_ops,
2635 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2636 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002637}
2638
2639static int
2640ftrace_notrace_open(struct inode *inode, struct file *file)
2641{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002642 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002643 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002644}
2645
Steven Rostedt64e7c442009-02-13 17:08:48 -05002646static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002647{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002648 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08002649 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002650
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002651 switch (type) {
2652 case MATCH_FULL:
2653 if (strcmp(str, regex) == 0)
2654 matched = 1;
2655 break;
2656 case MATCH_FRONT_ONLY:
2657 if (strncmp(str, regex, len) == 0)
2658 matched = 1;
2659 break;
2660 case MATCH_MIDDLE_ONLY:
2661 if (strstr(str, regex))
2662 matched = 1;
2663 break;
2664 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08002665 slen = strlen(str);
2666 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002667 matched = 1;
2668 break;
2669 }
2670
2671 return matched;
2672}
2673
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002674static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002675enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04002676{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002677 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002678 int ret = 0;
2679
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002680 entry = ftrace_lookup_ip(hash, rec->ip);
2681 if (not) {
2682 /* Do nothing if it doesn't exist */
2683 if (!entry)
2684 return 0;
2685
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002686 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002687 } else {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002688 /* Do nothing if it exists */
2689 if (entry)
2690 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002691
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002692 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002693 }
2694 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04002695}
2696
Steven Rostedt64e7c442009-02-13 17:08:48 -05002697static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002698ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2699 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002700{
2701 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002702 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002703
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002704 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2705
2706 if (mod) {
2707 /* module lookup requires matching the module */
2708 if (!modname || strcmp(modname, mod))
2709 return 0;
2710
2711 /* blank search means to match all funcs in the mod */
2712 if (!len)
2713 return 1;
2714 }
2715
Steven Rostedt64e7c442009-02-13 17:08:48 -05002716 return ftrace_match(str, regex, len, type);
2717}
2718
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002719static int
2720match_records(struct ftrace_hash *hash, char *buff,
2721 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002722{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002723 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002724 struct ftrace_page *pg;
2725 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002726 int type = MATCH_FULL;
2727 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08002728 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002729 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002730
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002731 if (len) {
2732 type = filter_parse_regex(buff, len, &search, &not);
2733 search_len = strlen(search);
2734 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002735
Steven Rostedt52baf112009-02-14 01:15:39 -05002736 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002737
2738 if (unlikely(ftrace_disabled))
2739 goto out_unlock;
2740
Steven Rostedt265c8312009-02-13 12:43:56 -05002741 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002742 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002743 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002744 if (ret < 0) {
2745 found = ret;
2746 goto out_unlock;
2747 }
Li Zefan311d16d2009-12-08 11:15:11 +08002748 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05002749 }
2750 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002751 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05002752 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08002753
2754 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02002755}
2756
Steven Rostedt64e7c442009-02-13 17:08:48 -05002757static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002758ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002759{
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002760 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002761}
2762
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002763static int
2764ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002765{
Steven Rostedt64e7c442009-02-13 17:08:48 -05002766 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002767
Steven Rostedt64e7c442009-02-13 17:08:48 -05002768 /* blank or '*' mean the same */
2769 if (strcmp(buff, "*") == 0)
2770 buff[0] = 0;
2771
2772 /* handle the case of 'dont filter this module' */
2773 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2774 buff[0] = 0;
2775 not = 1;
2776 }
2777
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002778 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002779}
2780
Steven Rostedtf6180772009-02-14 00:40:25 -05002781/*
2782 * We register the module command as a template to show others how
2783 * to register the a command as well.
2784 */
2785
2786static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04002787ftrace_mod_callback(struct ftrace_hash *hash,
2788 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05002789{
2790 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002791 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05002792
2793 /*
2794 * cmd == 'mod' because we only registered this func
2795 * for the 'mod' ftrace_func_command.
2796 * But if you register one func with multiple commands,
2797 * you can tell which command was used by the cmd
2798 * parameter.
2799 */
2800
2801 /* we must have a module name */
2802 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002803 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002804
2805 mod = strsep(&param, ":");
2806 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002807 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002808
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002809 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002810 if (!ret)
2811 ret = -EINVAL;
2812 if (ret < 0)
2813 return ret;
2814
2815 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05002816}
2817
2818static struct ftrace_func_command ftrace_mod_cmd = {
2819 .name = "mod",
2820 .func = ftrace_mod_callback,
2821};
2822
2823static int __init ftrace_mod_cmd_init(void)
2824{
2825 return register_ftrace_command(&ftrace_mod_cmd);
2826}
2827device_initcall(ftrace_mod_cmd_init);
2828
Steven Rostedt59df055f2009-02-14 15:29:06 -05002829static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05002830function_trace_probe_call(unsigned long ip, unsigned long parent_ip)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002831{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002832 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002833 struct hlist_head *hhd;
2834 struct hlist_node *n;
2835 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002836
2837 key = hash_long(ip, FTRACE_HASH_BITS);
2838
2839 hhd = &ftrace_func_hash[key];
2840
2841 if (hlist_empty(hhd))
2842 return;
2843
2844 /*
2845 * Disable preemption for these calls to prevent a RCU grace
2846 * period. This syncs the hash iteration and freeing of items
2847 * on the hash. rcu_read_lock is too dangerous here.
2848 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04002849 preempt_disable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002850 hlist_for_each_entry_rcu(entry, n, hhd, node) {
2851 if (entry->ip == ip)
2852 entry->ops->func(ip, parent_ip, &entry->data);
2853 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04002854 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002855}
2856
Steven Rostedtb6887d72009-02-17 12:32:04 -05002857static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05002858{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04002859 .func = function_trace_probe_call,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002860};
2861
Steven Rostedtb6887d72009-02-17 12:32:04 -05002862static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002863
Steven Rostedtb6887d72009-02-17 12:32:04 -05002864static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002865{
Steven Rostedtb8489142011-05-04 09:27:52 -04002866 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002867 int i;
2868
Steven Rostedtb6887d72009-02-17 12:32:04 -05002869 if (ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002870 return;
2871
2872 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2873 struct hlist_head *hhd = &ftrace_func_hash[i];
2874 if (hhd->first)
2875 break;
2876 }
2877 /* Nothing registered? */
2878 if (i == FTRACE_FUNC_HASHSIZE)
2879 return;
2880
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05002881 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04002882
Steven Rostedtb6887d72009-02-17 12:32:04 -05002883 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002884}
2885
Steven Rostedtb6887d72009-02-17 12:32:04 -05002886static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002887{
2888 int i;
2889
Steven Rostedtb6887d72009-02-17 12:32:04 -05002890 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002891 return;
2892
2893 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2894 struct hlist_head *hhd = &ftrace_func_hash[i];
2895 if (hhd->first)
2896 return;
2897 }
2898
2899 /* no more funcs left */
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05002900 ftrace_shutdown(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04002901
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 Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05003951# define ftrace_startup(ops, command) \
3952 ({ \
3953 int ___ret = __register_ftrace_function(ops); \
3954 if (!___ret) \
3955 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
3956 ___ret; \
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04003957 })
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05003958# define ftrace_shutdown(ops, command) __unregister_ftrace_function(ops)
3959
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003960# define ftrace_startup_sysctl() do { } while (0)
3961# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04003962
3963static inline int
3964ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
3965{
3966 return 1;
3967}
3968
Steven Rostedt3d083392008-05-12 21:20:42 +02003969#endif /* CONFIG_DYNAMIC_FTRACE */
3970
Steven Rostedtb8489142011-05-04 09:27:52 -04003971static void
Jiri Olsae2484912012-02-15 15:51:48 +01003972ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip)
3973{
3974 struct ftrace_ops *op;
3975
3976 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
3977 return;
3978
3979 /*
3980 * Some of the ops may be dynamically allocated,
3981 * they must be freed after a synchronize_sched().
3982 */
3983 preempt_disable_notrace();
3984 trace_recursion_set(TRACE_CONTROL_BIT);
3985 op = rcu_dereference_raw(ftrace_control_list);
3986 while (op != &ftrace_list_end) {
3987 if (!ftrace_function_local_disabled(op) &&
3988 ftrace_ops_test(op, ip))
3989 op->func(ip, parent_ip);
3990
3991 op = rcu_dereference_raw(op->next);
3992 };
3993 trace_recursion_clear(TRACE_CONTROL_BIT);
3994 preempt_enable_notrace();
3995}
3996
3997static struct ftrace_ops control_ops = {
3998 .func = ftrace_ops_control_func,
3999};
4000
4001static void
Steven Rostedtb8489142011-05-04 09:27:52 -04004002ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip)
4003{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004004 struct ftrace_ops *op;
Steven Rostedtb8489142011-05-04 09:27:52 -04004005
Steven Rostedtb1cff0a2011-05-25 14:27:43 -04004006 if (unlikely(trace_recursion_test(TRACE_INTERNAL_BIT)))
4007 return;
4008
4009 trace_recursion_set(TRACE_INTERNAL_BIT);
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004010 /*
4011 * Some of the ops may be dynamically allocated,
4012 * they must be freed after a synchronize_sched().
4013 */
4014 preempt_disable_notrace();
4015 op = rcu_dereference_raw(ftrace_ops_list);
Steven Rostedtb8489142011-05-04 09:27:52 -04004016 while (op != &ftrace_list_end) {
4017 if (ftrace_ops_test(op, ip))
4018 op->func(ip, parent_ip);
4019 op = rcu_dereference_raw(op->next);
4020 };
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004021 preempt_enable_notrace();
Steven Rostedtb1cff0a2011-05-25 14:27:43 -04004022 trace_recursion_clear(TRACE_INTERNAL_BIT);
Steven Rostedtb8489142011-05-04 09:27:52 -04004023}
4024
Steven Rostedte32d8952008-12-04 00:26:41 -05004025static void clear_ftrace_swapper(void)
4026{
4027 struct task_struct *p;
4028 int cpu;
4029
4030 get_online_cpus();
4031 for_each_online_cpu(cpu) {
4032 p = idle_task(cpu);
4033 clear_tsk_trace_trace(p);
4034 }
4035 put_online_cpus();
4036}
4037
4038static void set_ftrace_swapper(void)
4039{
4040 struct task_struct *p;
4041 int cpu;
4042
4043 get_online_cpus();
4044 for_each_online_cpu(cpu) {
4045 p = idle_task(cpu);
4046 set_tsk_trace_trace(p);
4047 }
4048 put_online_cpus();
4049}
4050
4051static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004052{
4053 struct task_struct *p;
4054
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004055 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05004056 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05004057 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05004058 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004059 rcu_read_unlock();
4060
Steven Rostedte32d8952008-12-04 00:26:41 -05004061 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004062}
4063
Steven Rostedte32d8952008-12-04 00:26:41 -05004064static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004065{
4066 struct task_struct *p;
4067
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004068 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004069 do_each_pid_task(pid, PIDTYPE_PID, p) {
4070 set_tsk_trace_trace(p);
4071 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004072 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004073}
4074
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004075static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004076{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004077 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004078 clear_ftrace_swapper();
4079 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004080 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05004081}
4082
4083static void set_ftrace_pid_task(struct pid *pid)
4084{
4085 if (pid == ftrace_swapper_pid)
4086 set_ftrace_swapper();
4087 else
4088 set_ftrace_pid(pid);
4089}
4090
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004091static int ftrace_pid_add(int p)
4092{
4093 struct pid *pid;
4094 struct ftrace_pid *fpid;
4095 int ret = -EINVAL;
4096
4097 mutex_lock(&ftrace_lock);
4098
4099 if (!p)
4100 pid = ftrace_swapper_pid;
4101 else
4102 pid = find_get_pid(p);
4103
4104 if (!pid)
4105 goto out;
4106
4107 ret = 0;
4108
4109 list_for_each_entry(fpid, &ftrace_pids, list)
4110 if (fpid->pid == pid)
4111 goto out_put;
4112
4113 ret = -ENOMEM;
4114
4115 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4116 if (!fpid)
4117 goto out_put;
4118
4119 list_add(&fpid->list, &ftrace_pids);
4120 fpid->pid = pid;
4121
4122 set_ftrace_pid_task(pid);
4123
4124 ftrace_update_pid_func();
4125 ftrace_startup_enable(0);
4126
4127 mutex_unlock(&ftrace_lock);
4128 return 0;
4129
4130out_put:
4131 if (pid != ftrace_swapper_pid)
4132 put_pid(pid);
4133
4134out:
4135 mutex_unlock(&ftrace_lock);
4136 return ret;
4137}
4138
4139static void ftrace_pid_reset(void)
4140{
4141 struct ftrace_pid *fpid, *safe;
4142
4143 mutex_lock(&ftrace_lock);
4144 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4145 struct pid *pid = fpid->pid;
4146
4147 clear_ftrace_pid_task(pid);
4148
4149 list_del(&fpid->list);
4150 kfree(fpid);
4151 }
4152
4153 ftrace_update_pid_func();
4154 ftrace_startup_enable(0);
4155
4156 mutex_unlock(&ftrace_lock);
4157}
4158
4159static void *fpid_start(struct seq_file *m, loff_t *pos)
4160{
4161 mutex_lock(&ftrace_lock);
4162
4163 if (list_empty(&ftrace_pids) && (!*pos))
4164 return (void *) 1;
4165
4166 return seq_list_start(&ftrace_pids, *pos);
4167}
4168
4169static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4170{
4171 if (v == (void *)1)
4172 return NULL;
4173
4174 return seq_list_next(v, &ftrace_pids, pos);
4175}
4176
4177static void fpid_stop(struct seq_file *m, void *p)
4178{
4179 mutex_unlock(&ftrace_lock);
4180}
4181
4182static int fpid_show(struct seq_file *m, void *v)
4183{
4184 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4185
4186 if (v == (void *)1) {
4187 seq_printf(m, "no pid\n");
4188 return 0;
4189 }
4190
4191 if (fpid->pid == ftrace_swapper_pid)
4192 seq_printf(m, "swapper tasks\n");
4193 else
4194 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4195
4196 return 0;
4197}
4198
4199static const struct seq_operations ftrace_pid_sops = {
4200 .start = fpid_start,
4201 .next = fpid_next,
4202 .stop = fpid_stop,
4203 .show = fpid_show,
4204};
4205
4206static int
4207ftrace_pid_open(struct inode *inode, struct file *file)
4208{
4209 int ret = 0;
4210
4211 if ((file->f_mode & FMODE_WRITE) &&
4212 (file->f_flags & O_TRUNC))
4213 ftrace_pid_reset();
4214
4215 if (file->f_mode & FMODE_READ)
4216 ret = seq_open(file, &ftrace_pid_sops);
4217
4218 return ret;
4219}
4220
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004221static ssize_t
4222ftrace_pid_write(struct file *filp, const char __user *ubuf,
4223 size_t cnt, loff_t *ppos)
4224{
Ingo Molnar457dc922009-11-23 11:03:28 +01004225 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004226 long val;
4227 int ret;
4228
4229 if (cnt >= sizeof(buf))
4230 return -EINVAL;
4231
4232 if (copy_from_user(&buf, ubuf, cnt))
4233 return -EFAULT;
4234
4235 buf[cnt] = 0;
4236
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004237 /*
4238 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4239 * to clean the filter quietly.
4240 */
Ingo Molnar457dc922009-11-23 11:03:28 +01004241 tmp = strstrip(buf);
4242 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004243 return 1;
4244
Ingo Molnar457dc922009-11-23 11:03:28 +01004245 ret = strict_strtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004246 if (ret < 0)
4247 return ret;
4248
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004249 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004250
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004251 return ret ? ret : cnt;
4252}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004253
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004254static int
4255ftrace_pid_release(struct inode *inode, struct file *file)
4256{
4257 if (file->f_mode & FMODE_READ)
4258 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004259
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004260 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004261}
4262
Steven Rostedt5e2336a02009-03-05 21:44:55 -05004263static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004264 .open = ftrace_pid_open,
4265 .write = ftrace_pid_write,
4266 .read = seq_read,
Namhyung Kim3a22cc72013-06-07 17:01:16 +08004267 .llseek = ftrace_filter_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004268 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004269};
4270
4271static __init int ftrace_init_debugfs(void)
4272{
4273 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004274
4275 d_tracer = tracing_init_dentry();
4276 if (!d_tracer)
4277 return 0;
4278
4279 ftrace_init_dyn_debugfs(d_tracer);
4280
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004281 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4282 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04004283
4284 ftrace_profile_debugfs(d_tracer);
4285
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004286 return 0;
4287}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004288fs_initcall(ftrace_init_debugfs);
4289
Steven Rostedt3d083392008-05-12 21:20:42 +02004290/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004291 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004292 *
4293 * This function should be used by panic code. It stops ftrace
4294 * but in a not so nice way. If you need to simply kill ftrace
4295 * from a non-atomic section, use ftrace_kill.
4296 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004297void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004298{
4299 ftrace_disabled = 1;
4300 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004301 clear_ftrace_function();
4302}
4303
4304/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04004305 * Test if ftrace is dead or not.
4306 */
4307int ftrace_is_dead(void)
4308{
4309 return ftrace_disabled;
4310}
4311
4312/**
Steven Rostedt3d083392008-05-12 21:20:42 +02004313 * register_ftrace_function - register a function for profiling
4314 * @ops - ops structure that holds the function for profiling.
4315 *
4316 * Register a function to be called by all functions in the
4317 * kernel.
4318 *
4319 * Note: @ops->func and all the functions it calls must be labeled
4320 * with "notrace", otherwise it will go into a
4321 * recursive loop.
4322 */
4323int register_ftrace_function(struct ftrace_ops *ops)
4324{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004325 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004326
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004327 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004328
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05004329 ret = ftrace_startup(ops, 0);
Steven Rostedt45a4a232011-04-21 23:16:46 -04004330
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004331 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004332 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02004333}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004334EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02004335
4336/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01004337 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02004338 * @ops - ops structure that holds the function to unregister
4339 *
4340 * Unregister a function that was added to be called by ftrace profiling.
4341 */
4342int unregister_ftrace_function(struct ftrace_ops *ops)
4343{
4344 int ret;
4345
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004346 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05004347 ret = ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004348 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004349
4350 return ret;
4351}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004352EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004353
Ingo Molnare309b412008-05-12 21:20:51 +02004354int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004355ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004356 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004357 loff_t *ppos)
4358{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004359 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004360
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004361 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004362
Steven Rostedt45a4a232011-04-21 23:16:46 -04004363 if (unlikely(ftrace_disabled))
4364 goto out;
4365
4366 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004367
Li Zefana32c7762009-06-26 16:55:51 +08004368 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004369 goto out;
4370
Li Zefana32c7762009-06-26 16:55:51 +08004371 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004372
4373 if (ftrace_enabled) {
4374
4375 ftrace_startup_sysctl();
4376
4377 /* we are starting ftrace again */
Jan Kiszkab81d3242013-03-26 17:53:03 +01004378 if (ftrace_ops_list != &ftrace_list_end)
4379 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004380
4381 } else {
4382 /* stopping ftrace calls (just send to ftrace_stub) */
4383 ftrace_trace_function = ftrace_stub;
4384
4385 ftrace_shutdown_sysctl();
4386 }
4387
4388 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004389 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004390 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02004391}
Ingo Molnarf17845e2008-10-24 12:47:10 +02004392
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004393#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004394
Steven Rostedt597af812009-04-03 15:24:12 -04004395static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004396static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004397
Steven Rostedte49dc192008-12-02 23:50:05 -05004398int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4399{
4400 return 0;
4401}
4402
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004403/* The callbacks that hook a function */
4404trace_func_graph_ret_t ftrace_graph_return =
4405 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004406trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004407
4408/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4409static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4410{
4411 int i;
4412 int ret = 0;
4413 unsigned long flags;
4414 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4415 struct task_struct *g, *t;
4416
4417 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4418 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4419 * sizeof(struct ftrace_ret_stack),
4420 GFP_KERNEL);
4421 if (!ret_stack_list[i]) {
4422 start = 0;
4423 end = i;
4424 ret = -ENOMEM;
4425 goto free;
4426 }
4427 }
4428
4429 read_lock_irqsave(&tasklist_lock, flags);
4430 do_each_thread(g, t) {
4431 if (start == end) {
4432 ret = -EAGAIN;
4433 goto unlock;
4434 }
4435
4436 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01004437 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004438 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04004439 t->curr_ret_stack = -1;
4440 /* Make sure the tasks see the -1 first: */
4441 smp_wmb();
4442 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004443 }
4444 } while_each_thread(g, t);
4445
4446unlock:
4447 read_unlock_irqrestore(&tasklist_lock, flags);
4448free:
4449 for (i = start; i < end; i++)
4450 kfree(ret_stack_list[i]);
4451 return ret;
4452}
4453
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004454static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04004455ftrace_graph_probe_sched_switch(void *ignore,
4456 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004457{
4458 unsigned long long timestamp;
4459 int index;
4460
Steven Rostedtbe6f1642009-03-24 11:06:24 -04004461 /*
4462 * Does the user want to count the time a function was asleep.
4463 * If so, do not update the time stamps.
4464 */
4465 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4466 return;
4467
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004468 timestamp = trace_clock_local();
4469
4470 prev->ftrace_timestamp = timestamp;
4471
4472 /* only process tasks that we timestamped */
4473 if (!next->ftrace_timestamp)
4474 return;
4475
4476 /*
4477 * Update all the counters in next to make up for the
4478 * time next was sleeping.
4479 */
4480 timestamp -= next->ftrace_timestamp;
4481
4482 for (index = next->curr_ret_stack; index >= 0; index--)
4483 next->ret_stack[index].calltime += timestamp;
4484}
4485
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004486/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004487static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004488{
4489 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004490 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004491
4492 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4493 sizeof(struct ftrace_ret_stack *),
4494 GFP_KERNEL);
4495
4496 if (!ret_stack_list)
4497 return -ENOMEM;
4498
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004499 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04004500 for_each_online_cpu(cpu) {
4501 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05004502 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04004503 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004504
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004505 do {
4506 ret = alloc_retstack_tasklist(ret_stack_list);
4507 } while (ret == -EAGAIN);
4508
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004509 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04004510 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004511 if (ret)
4512 pr_info("ftrace_graph: Couldn't activate tracepoint"
4513 " probe to kernel_sched_switch\n");
4514 }
4515
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004516 kfree(ret_stack_list);
4517 return ret;
4518}
4519
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004520/*
4521 * Hibernation protection.
4522 * The state of the current task is too much unstable during
4523 * suspend/restore to disk. We want to protect against that.
4524 */
4525static int
4526ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4527 void *unused)
4528{
4529 switch (state) {
4530 case PM_HIBERNATION_PREPARE:
4531 pause_graph_tracing();
4532 break;
4533
4534 case PM_POST_HIBERNATION:
4535 unpause_graph_tracing();
4536 break;
4537 }
4538 return NOTIFY_DONE;
4539}
4540
Steven Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05004541/* Just a place holder for function graph */
4542static struct ftrace_ops fgraph_ops __read_mostly = {
4543 .func = ftrace_stub,
4544 .flags = FTRACE_OPS_FL_GLOBAL,
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 Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05004573 ret = ftrace_startup(&fgraph_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 Rostedt (Red Hat)51d351d2013-11-25 20:59:46 -05004590 ftrace_shutdown(&fgraph_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