blob: c55f7e274613b19ca432e545462cade4b856ce8d [file] [log] [blame]
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02001/*
2 * Infrastructure for profiling code inserted by 'gcc -pg'.
3 *
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
6 *
7 * Originally ported from the -rt patch by:
8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
9 *
10 * Based on code in the latency_tracer, that is:
11 *
12 * Copyright (C) 2004-2006 Ingo Molnar
13 * Copyright (C) 2004 William Lee Irwin III
14 */
15
Steven Rostedt3d083392008-05-12 21:20:42 +020016#include <linux/stop_machine.h>
17#include <linux/clocksource.h>
18#include <linux/kallsyms.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020019#include <linux/seq_file.h>
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -080020#include <linux/suspend.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020021#include <linux/debugfs.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020022#include <linux/hardirq.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010023#include <linux/kthread.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020024#include <linux/uaccess.h>
Steven Rostedt5855fea2011-12-16 19:27:42 -050025#include <linux/bsearch.h>
Paul Gortmaker56d82e02011-05-26 17:53:52 -040026#include <linux/module.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010027#include <linux/ftrace.h>
Steven Rostedtb0fc4942008-05-12 21:20:43 +020028#include <linux/sysctl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020030#include <linux/ctype.h>
Steven Rostedt68950612011-12-16 17:06:45 -050031#include <linux/sort.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020032#include <linux/list.h>
Steven Rostedt59df055f2009-02-14 15:29:06 -050033#include <linux/hash.h>
Paul E. McKenney3f379b02010-03-05 15:03:25 -080034#include <linux/rcupdate.h>
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020035
Steven Rostedtad8d75f2009-04-14 19:39:12 -040036#include <trace/events/sched.h>
Steven Rostedt8aef2d22009-03-24 01:10:15 -040037
Steven Rostedt2af15d62009-05-28 13:37:24 -040038#include <asm/setup.h>
Abhishek Sagar395a59d2008-06-21 23:47:27 +053039
Steven Rostedt0706f1c2009-03-23 23:12:58 -040040#include "trace_output.h"
Steven Rostedtbac429f2009-03-20 12:50:56 -040041#include "trace_stat.h"
Steven Rostedt3d083392008-05-12 21:20:42 +020042
Steven Rostedt6912896e2008-10-23 09:33:03 -040043#define FTRACE_WARN_ON(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040044 ({ \
45 int ___r = cond; \
46 if (WARN_ON(___r)) \
Steven Rostedt6912896e2008-10-23 09:33:03 -040047 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040048 ___r; \
49 })
Steven Rostedt6912896e2008-10-23 09:33:03 -040050
51#define FTRACE_WARN_ON_ONCE(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040052 ({ \
53 int ___r = cond; \
54 if (WARN_ON_ONCE(___r)) \
Steven Rostedt6912896e2008-10-23 09:33:03 -040055 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040056 ___r; \
57 })
Steven Rostedt6912896e2008-10-23 09:33:03 -040058
Steven Rostedt8fc0c702009-02-16 15:28:00 -050059/* hash bits for specific function selection */
60#define FTRACE_HASH_BITS 7
61#define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
Steven Rostedt33dc9b12011-05-02 17:34:47 -040062#define FTRACE_HASH_DEFAULT_BITS 10
63#define FTRACE_HASH_MAX_BITS 12
Steven Rostedt8fc0c702009-02-16 15:28:00 -050064
Jiri Olsae2484912012-02-15 15:51:48 +010065#define FL_GLOBAL_CONTROL_MASK (FTRACE_OPS_FL_GLOBAL | FTRACE_OPS_FL_CONTROL)
66
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040067static struct ftrace_ops ftrace_list_end __read_mostly = {
68 .func = ftrace_stub,
69};
70
Steven Rostedt4eebcc82008-05-12 21:20:48 +020071/* ftrace_enabled is a method to turn ftrace on or off */
72int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020073static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020074
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050075/* Quick disabling of function tracer. */
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040076int function_trace_stop __read_mostly;
77
78/* Current function tracing op */
79struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050080
jolsa@redhat.com756d17e2009-10-13 16:33:52 -040081/* List for set_ftrace_pid's pids. */
82LIST_HEAD(ftrace_pids);
83struct ftrace_pid {
84 struct list_head list;
85 struct pid *pid;
86};
87
Steven Rostedt4eebcc82008-05-12 21:20:48 +020088/*
89 * ftrace_disabled is set when an anomaly is discovered.
90 * ftrace_disabled is much stronger than ftrace_enabled.
91 */
92static int ftrace_disabled __read_mostly;
93
Steven Rostedt52baf112009-02-14 01:15:39 -050094static DEFINE_MUTEX(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +020095
Steven Rostedtb8489142011-05-04 09:27:52 -040096static struct ftrace_ops *ftrace_global_list __read_mostly = &ftrace_list_end;
Jiri Olsae2484912012-02-15 15:51:48 +010097static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -040098static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020099ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500100ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
Steven Rostedt2b499382011-05-03 22:49:52 -0400101static struct ftrace_ops global_ops;
Jiri Olsae2484912012-02-15 15:51:48 +0100102static struct ftrace_ops control_ops;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200103
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400104#if ARCH_SUPPORTS_FTRACE_OPS
105static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400106 struct ftrace_ops *op, struct pt_regs *regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400107#else
108/* See comment below, where ftrace_ops_list_func is defined */
109static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
110#define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
111#endif
Steven Rostedtb8489142011-05-04 09:27:52 -0400112
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800113/*
Steven Rostedtb8489142011-05-04 09:27:52 -0400114 * Traverse the ftrace_global_list, invoking all entries. The reason that we
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800115 * can use rcu_dereference_raw() is that elements removed from this list
116 * are simply leaked, so there is no need to interact with a grace-period
117 * mechanism. The rcu_dereference_raw() calls are needed to handle
Steven Rostedtb8489142011-05-04 09:27:52 -0400118 * concurrent insertions into the ftrace_global_list.
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800119 *
120 * Silly Alpha and silly pointer-speculation compiler optimizations!
121 */
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400122static void
123ftrace_global_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400124 struct ftrace_ops *op, struct pt_regs *regs)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200125{
Steven Rostedtb1cff0a2011-05-25 14:27:43 -0400126 if (unlikely(trace_recursion_test(TRACE_GLOBAL_BIT)))
127 return;
128
129 trace_recursion_set(TRACE_GLOBAL_BIT);
130 op = rcu_dereference_raw(ftrace_global_list); /*see above*/
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200131 while (op != &ftrace_list_end) {
Steven Rostedta1e2e312011-08-09 12:50:46 -0400132 op->func(ip, parent_ip, op, regs);
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800133 op = rcu_dereference_raw(op->next); /*see above*/
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200134 };
Steven Rostedtb1cff0a2011-05-25 14:27:43 -0400135 trace_recursion_clear(TRACE_GLOBAL_BIT);
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200136}
137
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400138static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400139 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500140{
Steven Rostedt0ef8cde2008-12-03 15:36:58 -0500141 if (!test_tsk_trace_trace(current))
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500142 return;
143
Steven Rostedta1e2e312011-08-09 12:50:46 -0400144 ftrace_pid_function(ip, parent_ip, op, regs);
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500145}
146
147static void set_ftrace_pid_function(ftrace_func_t func)
148{
149 /* do not set ftrace_pid_function to itself! */
150 if (func != ftrace_pid_func)
151 ftrace_pid_function = func;
152}
153
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200154/**
Steven Rostedt3d083392008-05-12 21:20:42 +0200155 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200156 *
Steven Rostedt3d083392008-05-12 21:20:42 +0200157 * This NULLs the ftrace function and in essence stops
158 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200159 */
Steven Rostedt3d083392008-05-12 21:20:42 +0200160void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200161{
Steven Rostedt3d083392008-05-12 21:20:42 +0200162 ftrace_trace_function = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500163 ftrace_pid_function = ftrace_stub;
Steven Rostedt3d083392008-05-12 21:20:42 +0200164}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200165
Jiri Olsae2484912012-02-15 15:51:48 +0100166static void control_ops_disable_all(struct ftrace_ops *ops)
167{
168 int cpu;
169
170 for_each_possible_cpu(cpu)
171 *per_cpu_ptr(ops->disabled, cpu) = 1;
172}
173
174static int control_ops_alloc(struct ftrace_ops *ops)
175{
176 int __percpu *disabled;
177
178 disabled = alloc_percpu(int);
179 if (!disabled)
180 return -ENOMEM;
181
182 ops->disabled = disabled;
183 control_ops_disable_all(ops);
184 return 0;
185}
186
187static void control_ops_free(struct ftrace_ops *ops)
188{
189 free_percpu(ops->disabled);
190}
191
Steven Rostedt2b499382011-05-03 22:49:52 -0400192static void update_global_ops(void)
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400193{
194 ftrace_func_t func;
195
196 /*
197 * If there's only one function registered, then call that
198 * function directly. Otherwise, we need to iterate over the
199 * registered callers.
200 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400201 if (ftrace_global_list == &ftrace_list_end ||
202 ftrace_global_list->next == &ftrace_list_end)
203 func = ftrace_global_list->func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400204 else
Steven Rostedtb8489142011-05-04 09:27:52 -0400205 func = ftrace_global_list_func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400206
207 /* If we filter on pids, update to use the pid function */
208 if (!list_empty(&ftrace_pids)) {
209 set_ftrace_pid_function(func);
210 func = ftrace_pid_func;
211 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400212
213 global_ops.func = func;
214}
215
216static void update_ftrace_function(void)
217{
218 ftrace_func_t func;
219
220 update_global_ops();
221
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400222 /*
223 * If we are at the end of the list and this ops is
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400224 * not dynamic and the arch supports passing ops, then have the
225 * mcount trampoline call the function directly.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400226 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400227 if (ftrace_ops_list == &ftrace_list_end ||
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400228 (ftrace_ops_list->next == &ftrace_list_end &&
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400229 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC) &&
Steven Rostedtccf36722012-06-05 09:44:25 -0400230 !FTRACE_FORCE_LIST_FUNC)) {
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400231 /* Set the ftrace_ops that the arch callback uses */
232 if (ftrace_ops_list == &global_ops)
233 function_trace_op = ftrace_global_list;
234 else
235 function_trace_op = ftrace_ops_list;
Steven Rostedtb8489142011-05-04 09:27:52 -0400236 func = ftrace_ops_list->func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400237 } else {
238 /* Just use the default ftrace_ops */
239 function_trace_op = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400240 func = ftrace_ops_list_func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400241 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400242
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400243 ftrace_trace_function = func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400244}
245
Steven Rostedt2b499382011-05-03 22:49:52 -0400246static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200247{
Steven Rostedt2b499382011-05-03 22:49:52 -0400248 ops->next = *list;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200249 /*
Steven Rostedtb8489142011-05-04 09:27:52 -0400250 * We are entering ops into the list but another
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200251 * CPU might be walking that list. We need to make sure
252 * the ops->next pointer is valid before another CPU sees
Steven Rostedtb8489142011-05-04 09:27:52 -0400253 * the ops pointer included into the list.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200254 */
Steven Rostedt2b499382011-05-03 22:49:52 -0400255 rcu_assign_pointer(*list, ops);
256}
Steven Rostedt3d083392008-05-12 21:20:42 +0200257
Steven Rostedt2b499382011-05-03 22:49:52 -0400258static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
259{
260 struct ftrace_ops **p;
261
262 /*
263 * If we are removing the last function, then simply point
264 * to the ftrace_stub.
265 */
266 if (*list == ops && ops->next == &ftrace_list_end) {
267 *list = &ftrace_list_end;
268 return 0;
269 }
270
271 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
272 if (*p == ops)
273 break;
274
275 if (*p != ops)
276 return -1;
277
278 *p = (*p)->next;
279 return 0;
280}
281
Jiri Olsae2484912012-02-15 15:51:48 +0100282static void add_ftrace_list_ops(struct ftrace_ops **list,
283 struct ftrace_ops *main_ops,
284 struct ftrace_ops *ops)
285{
286 int first = *list == &ftrace_list_end;
287 add_ftrace_ops(list, ops);
288 if (first)
289 add_ftrace_ops(&ftrace_ops_list, main_ops);
290}
291
292static int remove_ftrace_list_ops(struct ftrace_ops **list,
293 struct ftrace_ops *main_ops,
294 struct ftrace_ops *ops)
295{
296 int ret = remove_ftrace_ops(list, ops);
297 if (!ret && *list == &ftrace_list_end)
298 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
299 return ret;
300}
301
Steven Rostedt2b499382011-05-03 22:49:52 -0400302static int __register_ftrace_function(struct ftrace_ops *ops)
303{
Borislav Petkov8d240dd2012-03-29 19:11:40 +0200304 if (unlikely(ftrace_disabled))
Steven Rostedt2b499382011-05-03 22:49:52 -0400305 return -ENODEV;
306
307 if (FTRACE_WARN_ON(ops == &global_ops))
308 return -EINVAL;
309
Steven Rostedtb8489142011-05-04 09:27:52 -0400310 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
311 return -EBUSY;
312
Jiri Olsae2484912012-02-15 15:51:48 +0100313 /* We don't support both control and global flags set. */
314 if ((ops->flags & FL_GLOBAL_CONTROL_MASK) == FL_GLOBAL_CONTROL_MASK)
315 return -EINVAL;
316
Steven Rostedt08f6fba2012-04-30 16:20:23 -0400317#ifndef ARCH_SUPPORTS_FTRACE_SAVE_REGS
318 /*
319 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
320 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
321 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
322 */
323 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
324 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
325 return -EINVAL;
326
327 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
328 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
329#endif
330
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400331 if (!core_kernel_data((unsigned long)ops))
332 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
333
Steven Rostedtb8489142011-05-04 09:27:52 -0400334 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100335 add_ftrace_list_ops(&ftrace_global_list, &global_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400336 ops->flags |= FTRACE_OPS_FL_ENABLED;
Jiri Olsae2484912012-02-15 15:51:48 +0100337 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
338 if (control_ops_alloc(ops))
339 return -ENOMEM;
340 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400341 } else
342 add_ftrace_ops(&ftrace_ops_list, ops);
343
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400344 if (ftrace_enabled)
345 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200346
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200347 return 0;
348}
349
Ingo Molnare309b412008-05-12 21:20:51 +0200350static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200351{
Steven Rostedt2b499382011-05-03 22:49:52 -0400352 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200353
Steven Rostedt2b499382011-05-03 22:49:52 -0400354 if (ftrace_disabled)
355 return -ENODEV;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200356
Steven Rostedtb8489142011-05-04 09:27:52 -0400357 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
358 return -EBUSY;
359
Steven Rostedt2b499382011-05-03 22:49:52 -0400360 if (FTRACE_WARN_ON(ops == &global_ops))
361 return -EINVAL;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200362
Steven Rostedtb8489142011-05-04 09:27:52 -0400363 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100364 ret = remove_ftrace_list_ops(&ftrace_global_list,
365 &global_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400366 if (!ret)
367 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Jiri Olsae2484912012-02-15 15:51:48 +0100368 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
369 ret = remove_ftrace_list_ops(&ftrace_control_list,
370 &control_ops, ops);
371 if (!ret) {
372 /*
373 * The ftrace_ops is now removed from the list,
374 * so there'll be no new users. We must ensure
375 * all current users are done before we free
376 * the control data.
377 */
378 synchronize_sched();
379 control_ops_free(ops);
380 }
Steven Rostedtb8489142011-05-04 09:27:52 -0400381 } else
382 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
383
Steven Rostedt2b499382011-05-03 22:49:52 -0400384 if (ret < 0)
385 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400386
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400387 if (ftrace_enabled)
388 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200389
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400390 /*
391 * Dynamic ops may be freed, we must make sure that all
392 * callers are done before leaving this function.
393 */
394 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
395 synchronize_sched();
396
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500397 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200398}
399
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500400static void ftrace_update_pid_func(void)
401{
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400402 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500403 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900404 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500405
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400406 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500407}
408
Steven Rostedt493762f2009-03-23 17:12:36 -0400409#ifdef CONFIG_FUNCTION_PROFILER
410struct ftrace_profile {
411 struct hlist_node node;
412 unsigned long ip;
413 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400414#ifdef CONFIG_FUNCTION_GRAPH_TRACER
415 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400416 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400417#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400418};
419
420struct ftrace_profile_page {
421 struct ftrace_profile_page *next;
422 unsigned long index;
423 struct ftrace_profile records[];
424};
425
Steven Rostedtcafb1682009-03-24 20:50:39 -0400426struct ftrace_profile_stat {
427 atomic_t disabled;
428 struct hlist_head *hash;
429 struct ftrace_profile_page *pages;
430 struct ftrace_profile_page *start;
431 struct tracer_stat stat;
432};
433
Steven Rostedt493762f2009-03-23 17:12:36 -0400434#define PROFILE_RECORDS_SIZE \
435 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
436
437#define PROFILES_PER_PAGE \
438 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
439
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400440static int ftrace_profile_bits __read_mostly;
441static int ftrace_profile_enabled __read_mostly;
442
443/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400444static DEFINE_MUTEX(ftrace_profile_lock);
445
Steven Rostedtcafb1682009-03-24 20:50:39 -0400446static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400447
448#define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
449
Steven Rostedt493762f2009-03-23 17:12:36 -0400450static void *
451function_stat_next(void *v, int idx)
452{
453 struct ftrace_profile *rec = v;
454 struct ftrace_profile_page *pg;
455
456 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
457
458 again:
Li Zefan0296e422009-06-26 11:15:37 +0800459 if (idx != 0)
460 rec++;
461
Steven Rostedt493762f2009-03-23 17:12:36 -0400462 if ((void *)rec >= (void *)&pg->records[pg->index]) {
463 pg = pg->next;
464 if (!pg)
465 return NULL;
466 rec = &pg->records[0];
467 if (!rec->counter)
468 goto again;
469 }
470
471 return rec;
472}
473
474static void *function_stat_start(struct tracer_stat *trace)
475{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400476 struct ftrace_profile_stat *stat =
477 container_of(trace, struct ftrace_profile_stat, stat);
478
479 if (!stat || !stat->start)
480 return NULL;
481
482 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400483}
484
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400485#ifdef CONFIG_FUNCTION_GRAPH_TRACER
486/* function graph compares on total time */
487static int function_stat_cmp(void *p1, void *p2)
488{
489 struct ftrace_profile *a = p1;
490 struct ftrace_profile *b = p2;
491
492 if (a->time < b->time)
493 return -1;
494 if (a->time > b->time)
495 return 1;
496 else
497 return 0;
498}
499#else
500/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400501static int function_stat_cmp(void *p1, void *p2)
502{
503 struct ftrace_profile *a = p1;
504 struct ftrace_profile *b = p2;
505
506 if (a->counter < b->counter)
507 return -1;
508 if (a->counter > b->counter)
509 return 1;
510 else
511 return 0;
512}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400513#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400514
515static int function_stat_headers(struct seq_file *m)
516{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400517#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400518 seq_printf(m, " Function "
Chase Douglase330b3b2010-04-26 14:02:05 -0400519 "Hit Time Avg s^2\n"
Steven Rostedt34886c82009-03-25 21:00:47 -0400520 " -------- "
Chase Douglase330b3b2010-04-26 14:02:05 -0400521 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400522#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400523 seq_printf(m, " Function Hit\n"
524 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400525#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400526 return 0;
527}
528
529static int function_stat_show(struct seq_file *m, void *v)
530{
531 struct ftrace_profile *rec = v;
532 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800533 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400534#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400535 static struct trace_seq s;
536 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400537 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400538#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800539 mutex_lock(&ftrace_profile_lock);
540
541 /* we raced with function_profile_reset() */
542 if (unlikely(rec->counter == 0)) {
543 ret = -EBUSY;
544 goto out;
545 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400546
547 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400548 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400549
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400550#ifdef CONFIG_FUNCTION_GRAPH_TRACER
551 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400552 avg = rec->time;
553 do_div(avg, rec->counter);
554
Chase Douglase330b3b2010-04-26 14:02:05 -0400555 /* Sample standard deviation (s^2) */
556 if (rec->counter <= 1)
557 stddev = 0;
558 else {
559 stddev = rec->time_squared - rec->counter * avg * avg;
560 /*
561 * Divide only 1000 for ns^2 -> us^2 conversion.
562 * trace_print_graph_duration will divide 1000 again.
563 */
564 do_div(stddev, (rec->counter - 1) * 1000);
565 }
566
Steven Rostedt34886c82009-03-25 21:00:47 -0400567 trace_seq_init(&s);
568 trace_print_graph_duration(rec->time, &s);
569 trace_seq_puts(&s, " ");
570 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400571 trace_seq_puts(&s, " ");
572 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400573 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400574#endif
575 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800576out:
577 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400578
Li Zefan3aaba202010-08-23 16:50:12 +0800579 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400580}
581
Steven Rostedtcafb1682009-03-24 20:50:39 -0400582static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400583{
584 struct ftrace_profile_page *pg;
585
Steven Rostedtcafb1682009-03-24 20:50:39 -0400586 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400587
588 while (pg) {
589 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
590 pg->index = 0;
591 pg = pg->next;
592 }
593
Steven Rostedtcafb1682009-03-24 20:50:39 -0400594 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400595 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
596}
597
Steven Rostedtcafb1682009-03-24 20:50:39 -0400598int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400599{
600 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400601 int functions;
602 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400603 int i;
604
605 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400606 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400607 return 0;
608
Steven Rostedtcafb1682009-03-24 20:50:39 -0400609 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
610 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400611 return -ENOMEM;
612
Steven Rostedt318e0a72009-03-25 20:06:34 -0400613#ifdef CONFIG_DYNAMIC_FTRACE
614 functions = ftrace_update_tot_cnt;
615#else
616 /*
617 * We do not know the number of functions that exist because
618 * dynamic tracing is what counts them. With past experience
619 * we have around 20K functions. That should be more than enough.
620 * It is highly unlikely we will execute every function in
621 * the kernel.
622 */
623 functions = 20000;
624#endif
625
Steven Rostedtcafb1682009-03-24 20:50:39 -0400626 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400627
Steven Rostedt318e0a72009-03-25 20:06:34 -0400628 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
629
630 for (i = 0; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400631 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400632 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400633 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400634 pg = pg->next;
635 }
636
637 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400638
639 out_free:
640 pg = stat->start;
641 while (pg) {
642 unsigned long tmp = (unsigned long)pg;
643
644 pg = pg->next;
645 free_page(tmp);
646 }
647
648 free_page((unsigned long)stat->pages);
649 stat->pages = NULL;
650 stat->start = NULL;
651
652 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400653}
654
Steven Rostedtcafb1682009-03-24 20:50:39 -0400655static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400656{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400657 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400658 int size;
659
Steven Rostedtcafb1682009-03-24 20:50:39 -0400660 stat = &per_cpu(ftrace_profile_stats, cpu);
661
662 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400663 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400664 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400665 return 0;
666 }
667
668 /*
669 * We are profiling all functions, but usually only a few thousand
670 * functions are hit. We'll make a hash of 1024 items.
671 */
672 size = FTRACE_PROFILE_HASH_SIZE;
673
Steven Rostedtcafb1682009-03-24 20:50:39 -0400674 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400675
Steven Rostedtcafb1682009-03-24 20:50:39 -0400676 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400677 return -ENOMEM;
678
Steven Rostedtcafb1682009-03-24 20:50:39 -0400679 if (!ftrace_profile_bits) {
680 size--;
Steven Rostedt493762f2009-03-23 17:12:36 -0400681
Steven Rostedtcafb1682009-03-24 20:50:39 -0400682 for (; size; size >>= 1)
683 ftrace_profile_bits++;
684 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400685
Steven Rostedt318e0a72009-03-25 20:06:34 -0400686 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400687 if (ftrace_profile_pages_init(stat) < 0) {
688 kfree(stat->hash);
689 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400690 return -ENOMEM;
691 }
692
693 return 0;
694}
695
Steven Rostedtcafb1682009-03-24 20:50:39 -0400696static int ftrace_profile_init(void)
697{
698 int cpu;
699 int ret = 0;
700
701 for_each_online_cpu(cpu) {
702 ret = ftrace_profile_init_cpu(cpu);
703 if (ret)
704 break;
705 }
706
707 return ret;
708}
709
Steven Rostedt493762f2009-03-23 17:12:36 -0400710/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400711static struct ftrace_profile *
712ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400713{
714 struct ftrace_profile *rec;
715 struct hlist_head *hhd;
716 struct hlist_node *n;
717 unsigned long key;
718
719 key = hash_long(ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400720 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400721
722 if (hlist_empty(hhd))
723 return NULL;
724
725 hlist_for_each_entry_rcu(rec, n, hhd, node) {
726 if (rec->ip == ip)
727 return rec;
728 }
729
730 return NULL;
731}
732
Steven Rostedtcafb1682009-03-24 20:50:39 -0400733static void ftrace_add_profile(struct ftrace_profile_stat *stat,
734 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400735{
736 unsigned long key;
737
738 key = hash_long(rec->ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400739 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400740}
741
Steven Rostedt318e0a72009-03-25 20:06:34 -0400742/*
743 * The memory is already allocated, this simply finds a new record to use.
744 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400745static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400746ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400747{
748 struct ftrace_profile *rec = NULL;
749
Steven Rostedt318e0a72009-03-25 20:06:34 -0400750 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400751 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400752 goto out;
753
Steven Rostedt493762f2009-03-23 17:12:36 -0400754 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400755 * Try to find the function again since an NMI
756 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400757 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400758 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400759 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400760 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400761
Steven Rostedtcafb1682009-03-24 20:50:39 -0400762 if (stat->pages->index == PROFILES_PER_PAGE) {
763 if (!stat->pages->next)
764 goto out;
765 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400766 }
767
Steven Rostedtcafb1682009-03-24 20:50:39 -0400768 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400769 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400770 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400771
Steven Rostedt493762f2009-03-23 17:12:36 -0400772 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400773 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400774
775 return rec;
776}
777
Steven Rostedt493762f2009-03-23 17:12:36 -0400778static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400779function_profile_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400780 struct ftrace_ops *ops, struct pt_regs *regs)
Steven Rostedt493762f2009-03-23 17:12:36 -0400781{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400782 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400783 struct ftrace_profile *rec;
784 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400785
786 if (!ftrace_profile_enabled)
787 return;
788
Steven Rostedt493762f2009-03-23 17:12:36 -0400789 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400790
791 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400792 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400793 goto out;
794
795 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400796 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400797 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400798 if (!rec)
799 goto out;
800 }
801
802 rec->counter++;
803 out:
804 local_irq_restore(flags);
805}
806
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400807#ifdef CONFIG_FUNCTION_GRAPH_TRACER
808static int profile_graph_entry(struct ftrace_graph_ent *trace)
809{
Steven Rostedta1e2e312011-08-09 12:50:46 -0400810 function_profile_call(trace->func, 0, NULL, NULL);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400811 return 1;
812}
813
814static void profile_graph_return(struct ftrace_graph_ret *trace)
815{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400816 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400817 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400818 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400819 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400820
821 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400822 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400823 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400824 goto out;
825
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400826 /* If the calltime was zero'd ignore it */
827 if (!trace->calltime)
828 goto out;
829
Steven Rostedta2a16d62009-03-24 23:17:58 -0400830 calltime = trace->rettime - trace->calltime;
831
832 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
833 int index;
834
835 index = trace->depth;
836
837 /* Append this call time to the parent time to subtract */
838 if (index)
839 current->ret_stack[index - 1].subtime += calltime;
840
841 if (current->ret_stack[index].subtime < calltime)
842 calltime -= current->ret_stack[index].subtime;
843 else
844 calltime = 0;
845 }
846
Steven Rostedtcafb1682009-03-24 20:50:39 -0400847 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400848 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400849 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400850 rec->time_squared += calltime * calltime;
851 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400852
Steven Rostedtcafb1682009-03-24 20:50:39 -0400853 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400854 local_irq_restore(flags);
855}
856
857static int register_ftrace_profiler(void)
858{
859 return register_ftrace_graph(&profile_graph_return,
860 &profile_graph_entry);
861}
862
863static void unregister_ftrace_profiler(void)
864{
865 unregister_ftrace_graph();
866}
867#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100868static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400869 .func = function_profile_call,
Steven Rostedt493762f2009-03-23 17:12:36 -0400870};
871
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400872static int register_ftrace_profiler(void)
873{
874 return register_ftrace_function(&ftrace_profile_ops);
875}
876
877static void unregister_ftrace_profiler(void)
878{
879 unregister_ftrace_function(&ftrace_profile_ops);
880}
881#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
882
Steven Rostedt493762f2009-03-23 17:12:36 -0400883static ssize_t
884ftrace_profile_write(struct file *filp, const char __user *ubuf,
885 size_t cnt, loff_t *ppos)
886{
887 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400888 int ret;
889
Peter Huewe22fe9b52011-06-07 21:58:27 +0200890 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
891 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400892 return ret;
893
894 val = !!val;
895
896 mutex_lock(&ftrace_profile_lock);
897 if (ftrace_profile_enabled ^ val) {
898 if (val) {
899 ret = ftrace_profile_init();
900 if (ret < 0) {
901 cnt = ret;
902 goto out;
903 }
904
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400905 ret = register_ftrace_profiler();
906 if (ret < 0) {
907 cnt = ret;
908 goto out;
909 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400910 ftrace_profile_enabled = 1;
911 } else {
912 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400913 /*
914 * unregister_ftrace_profiler calls stop_machine
915 * so this acts like an synchronize_sched.
916 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400917 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400918 }
919 }
920 out:
921 mutex_unlock(&ftrace_profile_lock);
922
Jiri Olsacf8517c2009-10-23 19:36:16 -0400923 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400924
925 return cnt;
926}
927
928static ssize_t
929ftrace_profile_read(struct file *filp, char __user *ubuf,
930 size_t cnt, loff_t *ppos)
931{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400932 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400933 int r;
934
935 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
936 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
937}
938
939static const struct file_operations ftrace_profile_fops = {
940 .open = tracing_open_generic,
941 .read = ftrace_profile_read,
942 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200943 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -0400944};
945
Steven Rostedtcafb1682009-03-24 20:50:39 -0400946/* used to initialize the real stat files */
947static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400948 .name = "functions",
949 .stat_start = function_stat_start,
950 .stat_next = function_stat_next,
951 .stat_cmp = function_stat_cmp,
952 .stat_headers = function_stat_headers,
953 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -0400954};
955
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400956static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400957{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400958 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400959 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400960 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -0400961 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400962 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -0400963
Steven Rostedtcafb1682009-03-24 20:50:39 -0400964 for_each_possible_cpu(cpu) {
965 stat = &per_cpu(ftrace_profile_stats, cpu);
966
967 /* allocate enough for function name + cpu number */
968 name = kmalloc(32, GFP_KERNEL);
969 if (!name) {
970 /*
971 * The files created are permanent, if something happens
972 * we still do not free memory.
973 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400974 WARN(1,
975 "Could not allocate stat file for cpu %d\n",
976 cpu);
977 return;
978 }
979 stat->stat = function_stats;
980 snprintf(name, 32, "function%d", cpu);
981 stat->stat.name = name;
982 ret = register_stat_tracer(&stat->stat);
983 if (ret) {
984 WARN(1,
985 "Could not register function stat for cpu %d\n",
986 cpu);
987 kfree(name);
988 return;
989 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400990 }
991
992 entry = debugfs_create_file("function_profile_enabled", 0644,
993 d_tracer, NULL, &ftrace_profile_fops);
994 if (!entry)
995 pr_warning("Could not create debugfs "
996 "'function_profile_enabled' entry\n");
997}
998
999#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001000static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001001{
1002}
1003#endif /* CONFIG_FUNCTION_PROFILER */
1004
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001005static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1006
Steven Rostedt3d083392008-05-12 21:20:42 +02001007#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001008
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001009#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001010# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001011#endif
1012
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001013static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1014
Steven Rostedtb6887d72009-02-17 12:32:04 -05001015struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001016 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -05001017 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001018 unsigned long flags;
1019 unsigned long ip;
1020 void *data;
1021 struct rcu_head rcu;
1022};
1023
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001024struct ftrace_func_entry {
1025 struct hlist_node hlist;
1026 unsigned long ip;
1027};
1028
1029struct ftrace_hash {
1030 unsigned long size_bits;
1031 struct hlist_head *buckets;
1032 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001033 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001034};
1035
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001036/*
1037 * We make these constant because no one should touch them,
1038 * but they are used as the default "empty hash", to avoid allocating
1039 * it all the time. These are in a read only section such that if
1040 * anyone does try to modify it, it will cause an exception.
1041 */
1042static const struct hlist_head empty_buckets[1];
1043static const struct ftrace_hash empty_hash = {
1044 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001045};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001046#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001047
Steven Rostedt2b499382011-05-03 22:49:52 -04001048static struct ftrace_ops global_ops = {
Steven Rostedtf45948e2011-05-02 12:29:25 -04001049 .func = ftrace_stub,
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001050 .notrace_hash = EMPTY_HASH,
1051 .filter_hash = EMPTY_HASH,
Steven Rostedtf45948e2011-05-02 12:29:25 -04001052};
1053
Steven Rostedt41c52c02008-05-22 11:46:33 -04001054static DEFINE_MUTEX(ftrace_regex_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02001055
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001056struct ftrace_page {
1057 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001058 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001059 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001060 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001061};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001062
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001063static struct ftrace_page *ftrace_new_pgs;
1064
Steven Rostedta7900872011-12-16 16:23:44 -05001065#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1066#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001067
1068/* estimate from running different kernels */
1069#define NR_TO_INIT 10000
1070
1071static struct ftrace_page *ftrace_pages_start;
1072static struct ftrace_page *ftrace_pages;
1073
Steven Rostedt06a51d92011-12-19 19:07:36 -05001074static bool ftrace_hash_empty(struct ftrace_hash *hash)
1075{
1076 return !hash || !hash->count;
1077}
1078
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001079static struct ftrace_func_entry *
1080ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1081{
1082 unsigned long key;
1083 struct ftrace_func_entry *entry;
1084 struct hlist_head *hhd;
1085 struct hlist_node *n;
1086
Steven Rostedt06a51d92011-12-19 19:07:36 -05001087 if (ftrace_hash_empty(hash))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001088 return NULL;
1089
1090 if (hash->size_bits > 0)
1091 key = hash_long(ip, hash->size_bits);
1092 else
1093 key = 0;
1094
1095 hhd = &hash->buckets[key];
1096
1097 hlist_for_each_entry_rcu(entry, n, hhd, hlist) {
1098 if (entry->ip == ip)
1099 return entry;
1100 }
1101 return NULL;
1102}
1103
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001104static void __add_hash_entry(struct ftrace_hash *hash,
1105 struct ftrace_func_entry *entry)
1106{
1107 struct hlist_head *hhd;
1108 unsigned long key;
1109
1110 if (hash->size_bits)
1111 key = hash_long(entry->ip, hash->size_bits);
1112 else
1113 key = 0;
1114
1115 hhd = &hash->buckets[key];
1116 hlist_add_head(&entry->hlist, hhd);
1117 hash->count++;
1118}
1119
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001120static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1121{
1122 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001123
1124 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1125 if (!entry)
1126 return -ENOMEM;
1127
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001128 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001129 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001130
1131 return 0;
1132}
1133
1134static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001135free_hash_entry(struct ftrace_hash *hash,
1136 struct ftrace_func_entry *entry)
1137{
1138 hlist_del(&entry->hlist);
1139 kfree(entry);
1140 hash->count--;
1141}
1142
1143static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001144remove_hash_entry(struct ftrace_hash *hash,
1145 struct ftrace_func_entry *entry)
1146{
1147 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001148 hash->count--;
1149}
1150
1151static void ftrace_hash_clear(struct ftrace_hash *hash)
1152{
1153 struct hlist_head *hhd;
1154 struct hlist_node *tp, *tn;
1155 struct ftrace_func_entry *entry;
1156 int size = 1 << hash->size_bits;
1157 int i;
1158
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001159 if (!hash->count)
1160 return;
1161
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001162 for (i = 0; i < size; i++) {
1163 hhd = &hash->buckets[i];
1164 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001165 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001166 }
1167 FTRACE_WARN_ON(hash->count);
1168}
1169
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001170static void free_ftrace_hash(struct ftrace_hash *hash)
1171{
1172 if (!hash || hash == EMPTY_HASH)
1173 return;
1174 ftrace_hash_clear(hash);
1175 kfree(hash->buckets);
1176 kfree(hash);
1177}
1178
Steven Rostedt07fd5512011-05-05 18:03:47 -04001179static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1180{
1181 struct ftrace_hash *hash;
1182
1183 hash = container_of(rcu, struct ftrace_hash, rcu);
1184 free_ftrace_hash(hash);
1185}
1186
1187static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1188{
1189 if (!hash || hash == EMPTY_HASH)
1190 return;
1191 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1192}
1193
Jiri Olsa5500fa52012-02-15 15:51:54 +01001194void ftrace_free_filter(struct ftrace_ops *ops)
1195{
1196 free_ftrace_hash(ops->filter_hash);
1197 free_ftrace_hash(ops->notrace_hash);
1198}
1199
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001200static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1201{
1202 struct ftrace_hash *hash;
1203 int size;
1204
1205 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1206 if (!hash)
1207 return NULL;
1208
1209 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001210 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001211
1212 if (!hash->buckets) {
1213 kfree(hash);
1214 return NULL;
1215 }
1216
1217 hash->size_bits = size_bits;
1218
1219 return hash;
1220}
1221
1222static struct ftrace_hash *
1223alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1224{
1225 struct ftrace_func_entry *entry;
1226 struct ftrace_hash *new_hash;
1227 struct hlist_node *tp;
1228 int size;
1229 int ret;
1230 int i;
1231
1232 new_hash = alloc_ftrace_hash(size_bits);
1233 if (!new_hash)
1234 return NULL;
1235
1236 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001237 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001238 return new_hash;
1239
1240 size = 1 << hash->size_bits;
1241 for (i = 0; i < size; i++) {
1242 hlist_for_each_entry(entry, tp, &hash->buckets[i], hlist) {
1243 ret = add_hash_entry(new_hash, entry->ip);
1244 if (ret < 0)
1245 goto free_hash;
1246 }
1247 }
1248
1249 FTRACE_WARN_ON(new_hash->count != hash->count);
1250
1251 return new_hash;
1252
1253 free_hash:
1254 free_ftrace_hash(new_hash);
1255 return NULL;
1256}
1257
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001258static void
1259ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1260static void
1261ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1262
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001263static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001264ftrace_hash_move(struct ftrace_ops *ops, int enable,
1265 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001266{
1267 struct ftrace_func_entry *entry;
1268 struct hlist_node *tp, *tn;
1269 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001270 struct ftrace_hash *old_hash;
1271 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001272 unsigned long key;
1273 int size = src->count;
1274 int bits = 0;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001275 int ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001276 int i;
1277
1278 /*
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001279 * Remove the current set, update the hash and add
1280 * them back.
1281 */
1282 ftrace_hash_rec_disable(ops, enable);
1283
1284 /*
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001285 * If the new source is empty, just free dst and assign it
1286 * the empty_hash.
1287 */
1288 if (!src->count) {
Steven Rostedt07fd5512011-05-05 18:03:47 -04001289 free_ftrace_hash_rcu(*dst);
1290 rcu_assign_pointer(*dst, EMPTY_HASH);
Steven Rostedtd4d34b92011-11-04 20:32:39 -04001291 /* still need to update the function records */
1292 ret = 0;
1293 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001294 }
1295
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001296 /*
1297 * Make the hash size about 1/2 the # found
1298 */
1299 for (size /= 2; size; size >>= 1)
1300 bits++;
1301
1302 /* Don't allocate too much */
1303 if (bits > FTRACE_HASH_MAX_BITS)
1304 bits = FTRACE_HASH_MAX_BITS;
1305
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001306 ret = -ENOMEM;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001307 new_hash = alloc_ftrace_hash(bits);
1308 if (!new_hash)
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001309 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001310
1311 size = 1 << src->size_bits;
1312 for (i = 0; i < size; i++) {
1313 hhd = &src->buckets[i];
1314 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist) {
1315 if (bits > 0)
1316 key = hash_long(entry->ip, bits);
1317 else
1318 key = 0;
1319 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001320 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001321 }
1322 }
1323
Steven Rostedt07fd5512011-05-05 18:03:47 -04001324 old_hash = *dst;
1325 rcu_assign_pointer(*dst, new_hash);
1326 free_ftrace_hash_rcu(old_hash);
1327
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001328 ret = 0;
1329 out:
1330 /*
1331 * Enable regardless of ret:
1332 * On success, we enable the new hash.
1333 * On failure, we re-enable the original hash.
1334 */
1335 ftrace_hash_rec_enable(ops, enable);
1336
1337 return ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001338}
1339
Steven Rostedt265c8312009-02-13 12:43:56 -05001340/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001341 * Test the hashes for this ops to see if we want to call
1342 * the ops->func or not.
1343 *
1344 * It's a match if the ip is in the ops->filter_hash or
1345 * the filter_hash does not exist or is empty,
1346 * AND
1347 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001348 *
1349 * This needs to be called with preemption disabled as
1350 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001351 */
1352static int
1353ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
1354{
1355 struct ftrace_hash *filter_hash;
1356 struct ftrace_hash *notrace_hash;
1357 int ret;
1358
Steven Rostedtb8489142011-05-04 09:27:52 -04001359 filter_hash = rcu_dereference_raw(ops->filter_hash);
1360 notrace_hash = rcu_dereference_raw(ops->notrace_hash);
1361
Steven Rostedt06a51d92011-12-19 19:07:36 -05001362 if ((ftrace_hash_empty(filter_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001363 ftrace_lookup_ip(filter_hash, ip)) &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001364 (ftrace_hash_empty(notrace_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001365 !ftrace_lookup_ip(notrace_hash, ip)))
1366 ret = 1;
1367 else
1368 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001369
1370 return ret;
1371}
1372
1373/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001374 * This is a double for. Do not use 'break' to break out of the loop,
1375 * you must use a goto.
1376 */
1377#define do_for_each_ftrace_rec(pg, rec) \
1378 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1379 int _____i; \
1380 for (_____i = 0; _____i < pg->index; _____i++) { \
1381 rec = &pg->records[_____i];
1382
1383#define while_for_each_ftrace_rec() \
1384 } \
1385 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301386
Steven Rostedt5855fea2011-12-16 19:27:42 -05001387
1388static int ftrace_cmp_recs(const void *a, const void *b)
1389{
Steven Rostedta650e022012-04-25 13:48:13 -04001390 const struct dyn_ftrace *key = a;
1391 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001392
Steven Rostedta650e022012-04-25 13:48:13 -04001393 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001394 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001395 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1396 return 1;
1397 return 0;
1398}
1399
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001400static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001401{
1402 struct ftrace_page *pg;
1403 struct dyn_ftrace *rec;
1404 struct dyn_ftrace key;
1405
1406 key.ip = start;
1407 key.flags = end; /* overload flags, as it is unsigned long */
1408
1409 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1410 if (end < pg->records[0].ip ||
1411 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1412 continue;
1413 rec = bsearch(&key, pg->records, pg->index,
1414 sizeof(struct dyn_ftrace),
1415 ftrace_cmp_recs);
1416 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001417 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001418 }
1419
Steven Rostedt5855fea2011-12-16 19:27:42 -05001420 return 0;
1421}
1422
Steven Rostedtc88fd862011-08-16 09:53:39 -04001423/**
1424 * ftrace_location - return true if the ip giving is a traced location
1425 * @ip: the instruction pointer to check
1426 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001427 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001428 * That is, the instruction that is either a NOP or call to
1429 * the function tracer. It checks the ftrace internal tables to
1430 * determine if the address belongs or not.
1431 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001432unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001433{
Steven Rostedta650e022012-04-25 13:48:13 -04001434 return ftrace_location_range(ip, ip);
1435}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001436
Steven Rostedta650e022012-04-25 13:48:13 -04001437/**
1438 * ftrace_text_reserved - return true if range contains an ftrace location
1439 * @start: start of range to search
1440 * @end: end of range to search (inclusive). @end points to the last byte to check.
1441 *
1442 * Returns 1 if @start and @end contains a ftrace location.
1443 * That is, the instruction that is either a NOP or call to
1444 * the function tracer. It checks the ftrace internal tables to
1445 * determine if the address belongs or not.
1446 */
1447int ftrace_text_reserved(void *start, void *end)
1448{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001449 unsigned long ret;
1450
1451 ret = ftrace_location_range((unsigned long)start,
1452 (unsigned long)end);
1453
1454 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001455}
1456
Steven Rostedted926f92011-05-03 13:25:24 -04001457static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1458 int filter_hash,
1459 bool inc)
1460{
1461 struct ftrace_hash *hash;
1462 struct ftrace_hash *other_hash;
1463 struct ftrace_page *pg;
1464 struct dyn_ftrace *rec;
1465 int count = 0;
1466 int all = 0;
1467
1468 /* Only update if the ops has been registered */
1469 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1470 return;
1471
1472 /*
1473 * In the filter_hash case:
1474 * If the count is zero, we update all records.
1475 * Otherwise we just update the items in the hash.
1476 *
1477 * In the notrace_hash case:
1478 * We enable the update in the hash.
1479 * As disabling notrace means enabling the tracing,
1480 * and enabling notrace means disabling, the inc variable
1481 * gets inversed.
1482 */
1483 if (filter_hash) {
1484 hash = ops->filter_hash;
1485 other_hash = ops->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001486 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001487 all = 1;
1488 } else {
1489 inc = !inc;
1490 hash = ops->notrace_hash;
1491 other_hash = ops->filter_hash;
1492 /*
1493 * If the notrace hash has no items,
1494 * then there's nothing to do.
1495 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001496 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001497 return;
1498 }
1499
1500 do_for_each_ftrace_rec(pg, rec) {
1501 int in_other_hash = 0;
1502 int in_hash = 0;
1503 int match = 0;
1504
1505 if (all) {
1506 /*
1507 * Only the filter_hash affects all records.
1508 * Update if the record is not in the notrace hash.
1509 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001510 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001511 match = 1;
1512 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001513 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1514 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001515
1516 /*
1517 *
1518 */
1519 if (filter_hash && in_hash && !in_other_hash)
1520 match = 1;
1521 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001522 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001523 match = 1;
1524 }
1525 if (!match)
1526 continue;
1527
1528 if (inc) {
1529 rec->flags++;
1530 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1531 return;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001532 /*
1533 * If any ops wants regs saved for this function
1534 * then all ops will get saved regs.
1535 */
1536 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1537 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001538 } else {
1539 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1540 return;
1541 rec->flags--;
1542 }
1543 count++;
1544 /* Shortcut, if we handled all records, we are done. */
1545 if (!all && count == hash->count)
1546 return;
1547 } while_for_each_ftrace_rec();
1548}
1549
1550static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1551 int filter_hash)
1552{
1553 __ftrace_hash_rec_update(ops, filter_hash, 0);
1554}
1555
1556static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1557 int filter_hash)
1558{
1559 __ftrace_hash_rec_update(ops, filter_hash, 1);
1560}
1561
Steven Rostedt05736a42008-09-22 14:55:47 -07001562static void print_ip_ins(const char *fmt, unsigned char *p)
1563{
1564 int i;
1565
1566 printk(KERN_CONT "%s", fmt);
1567
1568 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1569 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1570}
1571
Steven Rostedtc88fd862011-08-16 09:53:39 -04001572/**
1573 * ftrace_bug - report and shutdown function tracer
1574 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1575 * @ip: The address that failed
1576 *
1577 * The arch code that enables or disables the function tracing
1578 * can call ftrace_bug() when it has detected a problem in
1579 * modifying the code. @failed should be one of either:
1580 * EFAULT - if the problem happens on reading the @ip address
1581 * EINVAL - if what is read at @ip is not what was expected
1582 * EPERM - if the problem happens on writting to the @ip address
1583 */
1584void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001585{
1586 switch (failed) {
1587 case -EFAULT:
1588 FTRACE_WARN_ON_ONCE(1);
1589 pr_info("ftrace faulted on modifying ");
1590 print_ip_sym(ip);
1591 break;
1592 case -EINVAL:
1593 FTRACE_WARN_ON_ONCE(1);
1594 pr_info("ftrace failed to modify ");
1595 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001596 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001597 printk(KERN_CONT "\n");
1598 break;
1599 case -EPERM:
1600 FTRACE_WARN_ON_ONCE(1);
1601 pr_info("ftrace faulted on writing ");
1602 print_ip_sym(ip);
1603 break;
1604 default:
1605 FTRACE_WARN_ON_ONCE(1);
1606 pr_info("ftrace faulted on unknown error ");
1607 print_ip_sym(ip);
1608 }
1609}
1610
Steven Rostedtc88fd862011-08-16 09:53:39 -04001611static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02001612{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001613 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001614
Steven Rostedt982c3502008-11-15 16:31:41 -05001615 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001616 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05001617 *
Steven Rostedted926f92011-05-03 13:25:24 -04001618 * If the record has a ref count, then we need to enable it
1619 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001620 *
Steven Rostedted926f92011-05-03 13:25:24 -04001621 * Otherwise we make sure its disabled.
1622 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001623 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04001624 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001625 */
Steven Rostedtc88fd862011-08-16 09:53:39 -04001626 if (enable && (rec->flags & ~FTRACE_FL_MASK))
Steven Rostedted926f92011-05-03 13:25:24 -04001627 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001628
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001629 /*
1630 * If enabling and the REGS flag does not match the REGS_EN, then
1631 * do not ignore this record. Set flags to fail the compare against
1632 * ENABLED.
1633 */
1634 if (flag &&
1635 (!(rec->flags & FTRACE_FL_REGS) != !(rec->flags & FTRACE_FL_REGS_EN)))
1636 flag |= FTRACE_FL_REGS;
1637
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001638 /* If the state of this record hasn't changed, then do nothing */
1639 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001640 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001641
1642 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001643 /* Save off if rec is being enabled (for return value) */
1644 flag ^= rec->flags & FTRACE_FL_ENABLED;
1645
1646 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04001647 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001648 if (flag & FTRACE_FL_REGS) {
1649 if (rec->flags & FTRACE_FL_REGS)
1650 rec->flags |= FTRACE_FL_REGS_EN;
1651 else
1652 rec->flags &= ~FTRACE_FL_REGS_EN;
1653 }
1654 }
1655
1656 /*
1657 * If this record is being updated from a nop, then
1658 * return UPDATE_MAKE_CALL.
1659 * Otherwise, if the EN flag is set, then return
1660 * UPDATE_MODIFY_CALL_REGS to tell the caller to convert
1661 * from the non-save regs, to a save regs function.
1662 * Otherwise,
1663 * return UPDATE_MODIFY_CALL to tell the caller to convert
1664 * from the save regs, to a non-save regs function.
1665 */
1666 if (flag & FTRACE_FL_ENABLED)
1667 return FTRACE_UPDATE_MAKE_CALL;
1668 else if (rec->flags & FTRACE_FL_REGS_EN)
1669 return FTRACE_UPDATE_MODIFY_CALL_REGS;
1670 else
1671 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001672 }
1673
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001674 if (update) {
1675 /* If there's no more users, clear all flags */
1676 if (!(rec->flags & ~FTRACE_FL_MASK))
1677 rec->flags = 0;
1678 else
1679 /* Just disable the record (keep REGS state) */
1680 rec->flags &= ~FTRACE_FL_ENABLED;
1681 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04001682
1683 return FTRACE_UPDATE_MAKE_NOP;
1684}
1685
1686/**
1687 * ftrace_update_record, set a record that now is tracing or not
1688 * @rec: the record to update
1689 * @enable: set to 1 if the record is tracing, zero to force disable
1690 *
1691 * The records that represent all functions that can be traced need
1692 * to be updated when tracing has been enabled.
1693 */
1694int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1695{
1696 return ftrace_check_record(rec, enable, 1);
1697}
1698
1699/**
1700 * ftrace_test_record, check if the record has been enabled or not
1701 * @rec: the record to test
1702 * @enable: set to 1 to check if enabled, 0 if it is disabled
1703 *
1704 * The arch code may need to test if a record is already set to
1705 * tracing to determine how to modify the function code that it
1706 * represents.
1707 */
1708int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1709{
1710 return ftrace_check_record(rec, enable, 0);
1711}
1712
1713static int
1714__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1715{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001716 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001717 unsigned long ftrace_addr;
1718 int ret;
1719
Steven Rostedtc88fd862011-08-16 09:53:39 -04001720 ret = ftrace_update_record(rec, enable);
1721
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001722 if (rec->flags & FTRACE_FL_REGS)
1723 ftrace_addr = (unsigned long)FTRACE_REGS_ADDR;
1724 else
1725 ftrace_addr = (unsigned long)FTRACE_ADDR;
1726
Steven Rostedtc88fd862011-08-16 09:53:39 -04001727 switch (ret) {
1728 case FTRACE_UPDATE_IGNORE:
1729 return 0;
1730
1731 case FTRACE_UPDATE_MAKE_CALL:
1732 return ftrace_make_call(rec, ftrace_addr);
1733
1734 case FTRACE_UPDATE_MAKE_NOP:
1735 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001736
1737 case FTRACE_UPDATE_MODIFY_CALL_REGS:
1738 case FTRACE_UPDATE_MODIFY_CALL:
1739 if (rec->flags & FTRACE_FL_REGS)
1740 ftrace_old_addr = (unsigned long)FTRACE_ADDR;
1741 else
1742 ftrace_old_addr = (unsigned long)FTRACE_REGS_ADDR;
1743
1744 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04001745 }
1746
1747 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02001748}
1749
Steven Rostedte4f5d542012-04-27 09:13:18 -04001750void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001751{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001752 struct dyn_ftrace *rec;
1753 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001754 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001755
Steven Rostedt45a4a232011-04-21 23:16:46 -04001756 if (unlikely(ftrace_disabled))
1757 return;
1758
Steven Rostedt265c8312009-02-13 12:43:56 -05001759 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedte4f5d542012-04-27 09:13:18 -04001760 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001761 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04001762 ftrace_bug(failed, rec->ip);
1763 /* Stop processing */
1764 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001765 }
1766 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001767}
1768
Steven Rostedtc88fd862011-08-16 09:53:39 -04001769struct ftrace_rec_iter {
1770 struct ftrace_page *pg;
1771 int index;
1772};
1773
1774/**
1775 * ftrace_rec_iter_start, start up iterating over traced functions
1776 *
1777 * Returns an iterator handle that is used to iterate over all
1778 * the records that represent address locations where functions
1779 * are traced.
1780 *
1781 * May return NULL if no records are available.
1782 */
1783struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1784{
1785 /*
1786 * We only use a single iterator.
1787 * Protected by the ftrace_lock mutex.
1788 */
1789 static struct ftrace_rec_iter ftrace_rec_iter;
1790 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1791
1792 iter->pg = ftrace_pages_start;
1793 iter->index = 0;
1794
1795 /* Could have empty pages */
1796 while (iter->pg && !iter->pg->index)
1797 iter->pg = iter->pg->next;
1798
1799 if (!iter->pg)
1800 return NULL;
1801
1802 return iter;
1803}
1804
1805/**
1806 * ftrace_rec_iter_next, get the next record to process.
1807 * @iter: The handle to the iterator.
1808 *
1809 * Returns the next iterator after the given iterator @iter.
1810 */
1811struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1812{
1813 iter->index++;
1814
1815 if (iter->index >= iter->pg->index) {
1816 iter->pg = iter->pg->next;
1817 iter->index = 0;
1818
1819 /* Could have empty pages */
1820 while (iter->pg && !iter->pg->index)
1821 iter->pg = iter->pg->next;
1822 }
1823
1824 if (!iter->pg)
1825 return NULL;
1826
1827 return iter;
1828}
1829
1830/**
1831 * ftrace_rec_iter_record, get the record at the iterator location
1832 * @iter: The current iterator location
1833 *
1834 * Returns the record that the current @iter is at.
1835 */
1836struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1837{
1838 return &iter->pg->records[iter->index];
1839}
1840
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301841static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001842ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001843{
1844 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001845 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001846
1847 ip = rec->ip;
1848
Steven Rostedt45a4a232011-04-21 23:16:46 -04001849 if (unlikely(ftrace_disabled))
1850 return 0;
1851
Shaohua Li25aac9d2009-01-09 11:29:40 +08001852 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001853 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001854 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301855 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02001856 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301857 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001858}
1859
Steven Rostedt000ab692009-02-17 13:35:06 -05001860/*
1861 * archs can override this function if they must do something
1862 * before the modifying code is performed.
1863 */
1864int __weak ftrace_arch_code_modify_prepare(void)
1865{
1866 return 0;
1867}
1868
1869/*
1870 * archs can override this function if they must do something
1871 * after the modifying code is performed.
1872 */
1873int __weak ftrace_arch_code_modify_post_process(void)
1874{
1875 return 0;
1876}
1877
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001878void ftrace_modify_all_code(int command)
1879{
1880 if (command & FTRACE_UPDATE_CALLS)
1881 ftrace_replace_code(1);
1882 else if (command & FTRACE_DISABLE_CALLS)
1883 ftrace_replace_code(0);
1884
1885 if (command & FTRACE_UPDATE_TRACE_FUNC)
1886 ftrace_update_ftrace_func(ftrace_trace_function);
1887
1888 if (command & FTRACE_START_FUNC_RET)
1889 ftrace_enable_ftrace_graph_caller();
1890 else if (command & FTRACE_STOP_FUNC_RET)
1891 ftrace_disable_ftrace_graph_caller();
1892}
1893
Ingo Molnare309b412008-05-12 21:20:51 +02001894static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02001895{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001896 int *command = data;
1897
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001898 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001899
Steven Rostedtc88fd862011-08-16 09:53:39 -04001900 return 0;
1901}
1902
1903/**
1904 * ftrace_run_stop_machine, go back to the stop machine method
1905 * @command: The command to tell ftrace what to do
1906 *
1907 * If an arch needs to fall back to the stop machine method, the
1908 * it can call this function.
1909 */
1910void ftrace_run_stop_machine(int command)
1911{
1912 stop_machine(__ftrace_modify_code, &command, NULL);
1913}
1914
1915/**
1916 * arch_ftrace_update_code, modify the code to trace or not trace
1917 * @command: The command that needs to be done
1918 *
1919 * Archs can override this function if it does not need to
1920 * run stop_machine() to modify code.
1921 */
1922void __weak arch_ftrace_update_code(int command)
1923{
1924 ftrace_run_stop_machine(command);
1925}
1926
1927static void ftrace_run_update_code(int command)
1928{
1929 int ret;
1930
1931 ret = ftrace_arch_code_modify_prepare();
1932 FTRACE_WARN_ON(ret);
1933 if (ret)
1934 return;
1935 /*
1936 * Do not call function tracer while we update the code.
1937 * We are in stop machine.
1938 */
1939 function_trace_stop++;
1940
1941 /*
1942 * By default we use stop_machine() to modify the code.
1943 * But archs can do what ever they want as long as it
1944 * is safe. The stop_machine() is the safest, but also
1945 * produces the most overhead.
1946 */
1947 arch_ftrace_update_code(command);
1948
Steven Rostedt6331c282011-07-13 15:11:02 -04001949 function_trace_stop--;
1950
Steven Rostedt000ab692009-02-17 13:35:06 -05001951 ret = ftrace_arch_code_modify_post_process();
1952 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02001953}
1954
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001955static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001956static int ftrace_start_up;
Steven Rostedtb8489142011-05-04 09:27:52 -04001957static int global_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001958
1959static void ftrace_startup_enable(int command)
1960{
1961 if (saved_ftrace_func != ftrace_trace_function) {
1962 saved_ftrace_func = ftrace_trace_function;
1963 command |= FTRACE_UPDATE_TRACE_FUNC;
1964 }
1965
1966 if (!command || !ftrace_enabled)
1967 return;
1968
1969 ftrace_run_update_code(command);
1970}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001971
Steven Rostedta1cd6172011-05-23 15:24:25 -04001972static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001973{
Steven Rostedtb8489142011-05-04 09:27:52 -04001974 bool hash_enable = true;
1975
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001976 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04001977 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001978
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001979 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001980 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02001981
Steven Rostedtb8489142011-05-04 09:27:52 -04001982 /* ops marked global share the filter hashes */
1983 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
1984 ops = &global_ops;
1985 /* Don't update hash if global is already set */
1986 if (global_start_up)
1987 hash_enable = false;
1988 global_start_up++;
1989 }
1990
Steven Rostedted926f92011-05-03 13:25:24 -04001991 ops->flags |= FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04001992 if (hash_enable)
Steven Rostedted926f92011-05-03 13:25:24 -04001993 ftrace_hash_rec_enable(ops, 1);
1994
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001995 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04001996
1997 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02001998}
1999
Steven Rostedtbd69c302011-05-03 21:55:54 -04002000static void ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002001{
Steven Rostedtb8489142011-05-04 09:27:52 -04002002 bool hash_disable = true;
2003
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002004 if (unlikely(ftrace_disabled))
2005 return;
2006
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002007 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002008 /*
2009 * Just warn in case of unbalance, no need to kill ftrace, it's not
2010 * critical but the ftrace_call callers may be never nopped again after
2011 * further ftrace uses.
2012 */
2013 WARN_ON_ONCE(ftrace_start_up < 0);
2014
Steven Rostedtb8489142011-05-04 09:27:52 -04002015 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2016 ops = &global_ops;
2017 global_start_up--;
2018 WARN_ON_ONCE(global_start_up < 0);
2019 /* Don't update hash if global still has users */
2020 if (global_start_up) {
2021 WARN_ON_ONCE(!ftrace_start_up);
2022 hash_disable = false;
2023 }
2024 }
2025
2026 if (hash_disable)
Steven Rostedted926f92011-05-03 13:25:24 -04002027 ftrace_hash_rec_disable(ops, 1);
2028
Steven Rostedtb8489142011-05-04 09:27:52 -04002029 if (ops != &global_ops || !global_start_up)
Steven Rostedted926f92011-05-03 13:25:24 -04002030 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002031
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002032 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002033
2034 if (saved_ftrace_func != ftrace_trace_function) {
2035 saved_ftrace_func = ftrace_trace_function;
2036 command |= FTRACE_UPDATE_TRACE_FUNC;
2037 }
2038
2039 if (!command || !ftrace_enabled)
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002040 return;
Steven Rostedt3d083392008-05-12 21:20:42 +02002041
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002042 ftrace_run_update_code(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02002043}
2044
Ingo Molnare309b412008-05-12 21:20:51 +02002045static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002046{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002047 if (unlikely(ftrace_disabled))
2048 return;
2049
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002050 /* Force update next time */
2051 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002052 /* ftrace_start_up is true if we want ftrace running */
2053 if (ftrace_start_up)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002054 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002055}
2056
Ingo Molnare309b412008-05-12 21:20:51 +02002057static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002058{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002059 if (unlikely(ftrace_disabled))
2060 return;
2061
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002062 /* ftrace_start_up is true if ftrace is running */
2063 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04002064 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002065}
2066
Steven Rostedt3d083392008-05-12 21:20:42 +02002067static cycle_t ftrace_update_time;
2068static unsigned long ftrace_update_cnt;
2069unsigned long ftrace_update_tot_cnt;
2070
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002071static int ops_traces_mod(struct ftrace_ops *ops)
2072{
2073 struct ftrace_hash *hash;
2074
2075 hash = ops->filter_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05002076 return ftrace_hash_empty(hash);
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002077}
2078
Steven Rostedt31e88902008-11-14 16:21:19 -08002079static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02002080{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002081 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002082 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302083 cycle_t start, stop;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002084 unsigned long ref = 0;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002085 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002086
2087 /*
2088 * When adding a module, we need to check if tracers are
2089 * currently enabled and if they are set to trace all functions.
2090 * If they are, we need to enable the module functions as well
2091 * as update the reference counts for those function records.
2092 */
2093 if (mod) {
2094 struct ftrace_ops *ops;
2095
2096 for (ops = ftrace_ops_list;
2097 ops != &ftrace_list_end; ops = ops->next) {
2098 if (ops->flags & FTRACE_OPS_FL_ENABLED &&
2099 ops_traces_mod(ops))
2100 ref++;
2101 }
2102 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002103
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002104 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002105 ftrace_update_cnt = 0;
2106
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002107 for (pg = ftrace_new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302108
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002109 for (i = 0; i < pg->index; i++) {
2110 /* If something went wrong, bail without enabling anything */
2111 if (unlikely(ftrace_disabled))
2112 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002113
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002114 p = &pg->records[i];
2115 p->flags = ref;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302116
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002117 /*
2118 * Do the initial record conversion from mcount jump
2119 * to the NOP instructions.
2120 */
2121 if (!ftrace_code_disable(mod, p))
2122 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002123
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002124 ftrace_update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002125
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002126 /*
2127 * If the tracing is enabled, go ahead and enable the record.
2128 *
2129 * The reason not to enable the record immediatelly is the
2130 * inherent check of ftrace_make_nop/ftrace_make_call for
2131 * correct previous instructions. Making first the NOP
2132 * conversion puts the module to the correct state, thus
2133 * passing the ftrace_make_call check.
2134 */
2135 if (ftrace_start_up && ref) {
2136 int failed = __ftrace_replace_code(p, 1);
2137 if (failed)
2138 ftrace_bug(failed, p->ip);
2139 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002140 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002141 }
2142
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002143 ftrace_new_pgs = NULL;
2144
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002145 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002146 ftrace_update_time = stop - start;
2147 ftrace_update_tot_cnt += ftrace_update_cnt;
2148
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002149 return 0;
2150}
2151
Steven Rostedta7900872011-12-16 16:23:44 -05002152static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002153{
Steven Rostedta7900872011-12-16 16:23:44 -05002154 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002155 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002156
Steven Rostedta7900872011-12-16 16:23:44 -05002157 if (WARN_ON(!count))
2158 return -EINVAL;
2159
2160 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002161
2162 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002163 * We want to fill as much as possible. No more than a page
2164 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002165 */
Steven Rostedta7900872011-12-16 16:23:44 -05002166 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2167 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002168
Steven Rostedta7900872011-12-16 16:23:44 -05002169 again:
2170 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2171
2172 if (!pg->records) {
2173 /* if we can't allocate this size, try something smaller */
2174 if (!order)
2175 return -ENOMEM;
2176 order >>= 1;
2177 goto again;
2178 }
2179
2180 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2181 pg->size = cnt;
2182
2183 if (cnt > count)
2184 cnt = count;
2185
2186 return cnt;
2187}
2188
2189static struct ftrace_page *
2190ftrace_allocate_pages(unsigned long num_to_init)
2191{
2192 struct ftrace_page *start_pg;
2193 struct ftrace_page *pg;
2194 int order;
2195 int cnt;
2196
2197 if (!num_to_init)
2198 return 0;
2199
2200 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2201 if (!pg)
2202 return NULL;
2203
2204 /*
2205 * Try to allocate as much as possible in one continues
2206 * location that fills in all of the space. We want to
2207 * waste as little space as possible.
2208 */
2209 for (;;) {
2210 cnt = ftrace_allocate_records(pg, num_to_init);
2211 if (cnt < 0)
2212 goto free_pages;
2213
2214 num_to_init -= cnt;
2215 if (!num_to_init)
2216 break;
2217
2218 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2219 if (!pg->next)
2220 goto free_pages;
2221
2222 pg = pg->next;
2223 }
2224
2225 return start_pg;
2226
2227 free_pages:
2228 while (start_pg) {
2229 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2230 free_pages((unsigned long)pg->records, order);
2231 start_pg = pg->next;
2232 kfree(pg);
2233 pg = start_pg;
2234 }
2235 pr_info("ftrace: FAILED to allocate memory for functions\n");
2236 return NULL;
2237}
2238
2239static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2240{
2241 int cnt;
2242
2243 if (!num_to_init) {
2244 pr_info("ftrace: No functions to be traced?\n");
2245 return -1;
2246 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002247
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002248 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04002249 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08002250 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002251
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002252 return 0;
2253}
2254
Steven Rostedt5072c592008-05-12 21:20:43 +02002255#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2256
2257struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002258 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002259 loff_t func_pos;
2260 struct ftrace_page *pg;
2261 struct dyn_ftrace *func;
2262 struct ftrace_func_probe *probe;
2263 struct trace_parser parser;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002264 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002265 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002266 int hidx;
2267 int idx;
2268 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002269};
2270
Ingo Molnare309b412008-05-12 21:20:51 +02002271static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002272t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002273{
2274 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002275 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002276 struct hlist_head *hhd;
2277
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002278 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002279 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002280
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002281 if (iter->probe)
2282 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002283 retry:
2284 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2285 return NULL;
2286
2287 hhd = &ftrace_func_hash[iter->hidx];
2288
2289 if (hlist_empty(hhd)) {
2290 iter->hidx++;
2291 hnd = NULL;
2292 goto retry;
2293 }
2294
2295 if (!hnd)
2296 hnd = hhd->first;
2297 else {
2298 hnd = hnd->next;
2299 if (!hnd) {
2300 iter->hidx++;
2301 goto retry;
2302 }
2303 }
2304
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002305 if (WARN_ON_ONCE(!hnd))
2306 return NULL;
2307
2308 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2309
2310 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002311}
2312
2313static void *t_hash_start(struct seq_file *m, loff_t *pos)
2314{
2315 struct ftrace_iterator *iter = m->private;
2316 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08002317 loff_t l;
2318
Steven Rostedt69a30832011-12-19 15:21:16 -05002319 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2320 return NULL;
2321
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002322 if (iter->func_pos > *pos)
2323 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002324
Li Zefand82d6242009-06-24 09:54:54 +08002325 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002326 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002327 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08002328 if (!p)
2329 break;
2330 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002331 if (!p)
2332 return NULL;
2333
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002334 /* Only set this if we have an item */
2335 iter->flags |= FTRACE_ITER_HASH;
2336
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002337 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002338}
2339
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002340static int
2341t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002342{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002343 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002344
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002345 rec = iter->probe;
2346 if (WARN_ON_ONCE(!rec))
2347 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002348
Steven Rostedt809dcf22009-02-16 23:06:01 -05002349 if (rec->ops->print)
2350 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2351
Steven Rostedtb375a112009-09-17 00:05:58 -04002352 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002353
2354 if (rec->data)
2355 seq_printf(m, ":%p", rec->data);
2356 seq_putc(m, '\n');
2357
2358 return 0;
2359}
2360
2361static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02002362t_next(struct seq_file *m, void *v, loff_t *pos)
2363{
2364 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002365 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002366 struct dyn_ftrace *rec = NULL;
2367
Steven Rostedt45a4a232011-04-21 23:16:46 -04002368 if (unlikely(ftrace_disabled))
2369 return NULL;
2370
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002371 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002372 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002373
Steven Rostedt5072c592008-05-12 21:20:43 +02002374 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01002375 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02002376
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002377 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002378 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002379
Steven Rostedt5072c592008-05-12 21:20:43 +02002380 retry:
2381 if (iter->idx >= iter->pg->index) {
2382 if (iter->pg->next) {
2383 iter->pg = iter->pg->next;
2384 iter->idx = 0;
2385 goto retry;
2386 }
2387 } else {
2388 rec = &iter->pg->records[iter->idx++];
Steven Rostedt32082302011-12-16 14:42:37 -05002389 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedtf45948e2011-05-02 12:29:25 -04002390 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
Steven Rostedt0183fb12008-11-07 22:36:02 -05002391
Steven Rostedt41c52c02008-05-22 11:46:33 -04002392 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt647bcd02011-05-03 14:39:21 -04002393 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2394
2395 ((iter->flags & FTRACE_ITER_ENABLED) &&
2396 !(rec->flags & ~FTRACE_FL_MASK))) {
2397
Steven Rostedt5072c592008-05-12 21:20:43 +02002398 rec = NULL;
2399 goto retry;
2400 }
2401 }
2402
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002403 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002404 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002405
2406 iter->func = rec;
2407
2408 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002409}
2410
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002411static void reset_iter_read(struct ftrace_iterator *iter)
2412{
2413 iter->pos = 0;
2414 iter->func_pos = 0;
2415 iter->flags &= ~(FTRACE_ITER_PRINTALL & FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002416}
2417
2418static void *t_start(struct seq_file *m, loff_t *pos)
2419{
2420 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002421 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002422 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002423 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002424
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002425 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002426
2427 if (unlikely(ftrace_disabled))
2428 return NULL;
2429
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002430 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002431 * If an lseek was done, then reset and start from beginning.
2432 */
2433 if (*pos < iter->pos)
2434 reset_iter_read(iter);
2435
2436 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002437 * For set_ftrace_filter reading, if we have the filter
2438 * off, we can short cut and just print out that all
2439 * functions are enabled.
2440 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05002441 if (iter->flags & FTRACE_ITER_FILTER &&
2442 ftrace_hash_empty(ops->filter_hash)) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002443 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002444 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002445 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002446 /* reset in case of seek/pread */
2447 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002448 return iter;
2449 }
2450
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002451 if (iter->flags & FTRACE_ITER_HASH)
2452 return t_hash_start(m, pos);
2453
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002454 /*
2455 * Unfortunately, we need to restart at ftrace_pages_start
2456 * every time we let go of the ftrace_mutex. This is because
2457 * those pointers can change without the lock.
2458 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002459 iter->pg = ftrace_pages_start;
2460 iter->idx = 0;
2461 for (l = 0; l <= *pos; ) {
2462 p = t_next(m, p, &l);
2463 if (!p)
2464 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002465 }
walimis5821e1b2008-11-15 15:19:06 +08002466
Steven Rostedt69a30832011-12-19 15:21:16 -05002467 if (!p)
2468 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002469
2470 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002471}
2472
2473static void t_stop(struct seq_file *m, void *p)
2474{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002475 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002476}
2477
2478static int t_show(struct seq_file *m, void *v)
2479{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002480 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002481 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02002482
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002483 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002484 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002485
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002486 if (iter->flags & FTRACE_ITER_PRINTALL) {
2487 seq_printf(m, "#### all functions enabled ####\n");
2488 return 0;
2489 }
2490
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002491 rec = iter->func;
2492
Steven Rostedt5072c592008-05-12 21:20:43 +02002493 if (!rec)
2494 return 0;
2495
Steven Rostedt647bcd02011-05-03 14:39:21 -04002496 seq_printf(m, "%ps", (void *)rec->ip);
2497 if (iter->flags & FTRACE_ITER_ENABLED)
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002498 seq_printf(m, " (%ld)%s",
2499 rec->flags & ~FTRACE_FL_MASK,
2500 rec->flags & FTRACE_FL_REGS ? " R" : "");
Steven Rostedt647bcd02011-05-03 14:39:21 -04002501 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02002502
2503 return 0;
2504}
2505
James Morris88e9d342009-09-22 16:43:43 -07002506static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002507 .start = t_start,
2508 .next = t_next,
2509 .stop = t_stop,
2510 .show = t_show,
2511};
2512
Ingo Molnare309b412008-05-12 21:20:51 +02002513static int
Steven Rostedt5072c592008-05-12 21:20:43 +02002514ftrace_avail_open(struct inode *inode, struct file *file)
2515{
2516 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002517
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002518 if (unlikely(ftrace_disabled))
2519 return -ENODEV;
2520
Jiri Olsa50e18b92012-04-25 10:23:39 +02002521 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2522 if (iter) {
2523 iter->pg = ftrace_pages_start;
2524 iter->ops = &global_ops;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002525 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002526
Jiri Olsa50e18b92012-04-25 10:23:39 +02002527 return iter ? 0 : -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02002528}
2529
Steven Rostedt647bcd02011-05-03 14:39:21 -04002530static int
2531ftrace_enabled_open(struct inode *inode, struct file *file)
2532{
2533 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002534
2535 if (unlikely(ftrace_disabled))
2536 return -ENODEV;
2537
Jiri Olsa50e18b92012-04-25 10:23:39 +02002538 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2539 if (iter) {
2540 iter->pg = ftrace_pages_start;
2541 iter->flags = FTRACE_ITER_ENABLED;
2542 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002543 }
2544
Jiri Olsa50e18b92012-04-25 10:23:39 +02002545 return iter ? 0 : -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002546}
2547
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002548static void ftrace_filter_reset(struct ftrace_hash *hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02002549{
Steven Rostedt52baf112009-02-14 01:15:39 -05002550 mutex_lock(&ftrace_lock);
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002551 ftrace_hash_clear(hash);
Steven Rostedt52baf112009-02-14 01:15:39 -05002552 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002553}
2554
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002555/**
2556 * ftrace_regex_open - initialize function tracer filter files
2557 * @ops: The ftrace_ops that hold the hash filters
2558 * @flag: The type of filter to process
2559 * @inode: The inode, usually passed in to your open routine
2560 * @file: The file, usually passed in to your open routine
2561 *
2562 * ftrace_regex_open() initializes the filter files for the
2563 * @ops. Depending on @flag it may process the filter hash or
2564 * the notrace hash of @ops. With this called from the open
2565 * routine, you can use ftrace_filter_write() for the write
2566 * routine if @flag has FTRACE_ITER_FILTER set, or
2567 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
2568 * ftrace_regex_lseek() should be used as the lseek routine, and
2569 * release must call ftrace_regex_release().
2570 */
2571int
Steven Rostedtf45948e2011-05-02 12:29:25 -04002572ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002573 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02002574{
2575 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002576 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02002577 int ret = 0;
2578
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002579 if (unlikely(ftrace_disabled))
2580 return -ENODEV;
2581
Steven Rostedt5072c592008-05-12 21:20:43 +02002582 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2583 if (!iter)
2584 return -ENOMEM;
2585
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002586 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2587 kfree(iter);
2588 return -ENOMEM;
2589 }
2590
Steven Rostedtf45948e2011-05-02 12:29:25 -04002591 if (flag & FTRACE_ITER_NOTRACE)
2592 hash = ops->notrace_hash;
2593 else
2594 hash = ops->filter_hash;
2595
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002596 iter->ops = ops;
2597 iter->flags = flag;
2598
2599 if (file->f_mode & FMODE_WRITE) {
2600 mutex_lock(&ftrace_lock);
2601 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2602 mutex_unlock(&ftrace_lock);
2603
2604 if (!iter->hash) {
2605 trace_parser_put(&iter->parser);
2606 kfree(iter);
2607 return -ENOMEM;
2608 }
2609 }
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002610
Steven Rostedt41c52c02008-05-22 11:46:33 -04002611 mutex_lock(&ftrace_regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002612
Steven Rostedt5072c592008-05-12 21:20:43 +02002613 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002614 (file->f_flags & O_TRUNC))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002615 ftrace_filter_reset(iter->hash);
Steven Rostedt5072c592008-05-12 21:20:43 +02002616
2617 if (file->f_mode & FMODE_READ) {
2618 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02002619
2620 ret = seq_open(file, &show_ftrace_seq_ops);
2621 if (!ret) {
2622 struct seq_file *m = file->private_data;
2623 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08002624 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002625 /* Failed */
2626 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08002627 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002628 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08002629 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002630 } else
2631 file->private_data = iter;
Steven Rostedt41c52c02008-05-22 11:46:33 -04002632 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002633
2634 return ret;
2635}
2636
Steven Rostedt41c52c02008-05-22 11:46:33 -04002637static int
2638ftrace_filter_open(struct inode *inode, struct file *file)
2639{
Steven Rostedt69a30832011-12-19 15:21:16 -05002640 return ftrace_regex_open(&global_ops,
2641 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2642 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002643}
2644
2645static int
2646ftrace_notrace_open(struct inode *inode, struct file *file)
2647{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002648 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002649 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002650}
2651
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002652loff_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04002653ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
Steven Rostedt5072c592008-05-12 21:20:43 +02002654{
2655 loff_t ret;
2656
2657 if (file->f_mode & FMODE_READ)
2658 ret = seq_lseek(file, offset, origin);
2659 else
2660 file->f_pos = ret = 1;
2661
2662 return ret;
2663}
2664
Steven Rostedt64e7c442009-02-13 17:08:48 -05002665static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002666{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002667 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08002668 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002669
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002670 switch (type) {
2671 case MATCH_FULL:
2672 if (strcmp(str, regex) == 0)
2673 matched = 1;
2674 break;
2675 case MATCH_FRONT_ONLY:
2676 if (strncmp(str, regex, len) == 0)
2677 matched = 1;
2678 break;
2679 case MATCH_MIDDLE_ONLY:
2680 if (strstr(str, regex))
2681 matched = 1;
2682 break;
2683 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08002684 slen = strlen(str);
2685 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002686 matched = 1;
2687 break;
2688 }
2689
2690 return matched;
2691}
2692
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002693static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002694enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04002695{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002696 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002697 int ret = 0;
2698
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002699 entry = ftrace_lookup_ip(hash, rec->ip);
2700 if (not) {
2701 /* Do nothing if it doesn't exist */
2702 if (!entry)
2703 return 0;
2704
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002705 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002706 } else {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002707 /* Do nothing if it exists */
2708 if (entry)
2709 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002710
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002711 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002712 }
2713 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04002714}
2715
Steven Rostedt64e7c442009-02-13 17:08:48 -05002716static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002717ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2718 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002719{
2720 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002721 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002722
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002723 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2724
2725 if (mod) {
2726 /* module lookup requires matching the module */
2727 if (!modname || strcmp(modname, mod))
2728 return 0;
2729
2730 /* blank search means to match all funcs in the mod */
2731 if (!len)
2732 return 1;
2733 }
2734
Steven Rostedt64e7c442009-02-13 17:08:48 -05002735 return ftrace_match(str, regex, len, type);
2736}
2737
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002738static int
2739match_records(struct ftrace_hash *hash, char *buff,
2740 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002741{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002742 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002743 struct ftrace_page *pg;
2744 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002745 int type = MATCH_FULL;
2746 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08002747 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002748 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002749
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002750 if (len) {
2751 type = filter_parse_regex(buff, len, &search, &not);
2752 search_len = strlen(search);
2753 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002754
Steven Rostedt52baf112009-02-14 01:15:39 -05002755 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002756
2757 if (unlikely(ftrace_disabled))
2758 goto out_unlock;
2759
Steven Rostedt265c8312009-02-13 12:43:56 -05002760 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002761 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002762 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002763 if (ret < 0) {
2764 found = ret;
2765 goto out_unlock;
2766 }
Li Zefan311d16d2009-12-08 11:15:11 +08002767 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05002768 }
2769 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002770 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05002771 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08002772
2773 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02002774}
2775
Steven Rostedt64e7c442009-02-13 17:08:48 -05002776static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002777ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002778{
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002779 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002780}
2781
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002782static int
2783ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002784{
Steven Rostedt64e7c442009-02-13 17:08:48 -05002785 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002786
Steven Rostedt64e7c442009-02-13 17:08:48 -05002787 /* blank or '*' mean the same */
2788 if (strcmp(buff, "*") == 0)
2789 buff[0] = 0;
2790
2791 /* handle the case of 'dont filter this module' */
2792 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2793 buff[0] = 0;
2794 not = 1;
2795 }
2796
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002797 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002798}
2799
Steven Rostedtf6180772009-02-14 00:40:25 -05002800/*
2801 * We register the module command as a template to show others how
2802 * to register the a command as well.
2803 */
2804
2805static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04002806ftrace_mod_callback(struct ftrace_hash *hash,
2807 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05002808{
2809 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002810 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05002811
2812 /*
2813 * cmd == 'mod' because we only registered this func
2814 * for the 'mod' ftrace_func_command.
2815 * But if you register one func with multiple commands,
2816 * you can tell which command was used by the cmd
2817 * parameter.
2818 */
2819
2820 /* we must have a module name */
2821 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002822 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002823
2824 mod = strsep(&param, ":");
2825 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002826 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002827
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002828 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002829 if (!ret)
2830 ret = -EINVAL;
2831 if (ret < 0)
2832 return ret;
2833
2834 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05002835}
2836
2837static struct ftrace_func_command ftrace_mod_cmd = {
2838 .name = "mod",
2839 .func = ftrace_mod_callback,
2840};
2841
2842static int __init ftrace_mod_cmd_init(void)
2843{
2844 return register_ftrace_command(&ftrace_mod_cmd);
2845}
2846device_initcall(ftrace_mod_cmd_init);
2847
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04002848static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04002849 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002850{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002851 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002852 struct hlist_head *hhd;
2853 struct hlist_node *n;
2854 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002855
2856 key = hash_long(ip, FTRACE_HASH_BITS);
2857
2858 hhd = &ftrace_func_hash[key];
2859
2860 if (hlist_empty(hhd))
2861 return;
2862
2863 /*
2864 * Disable preemption for these calls to prevent a RCU grace
2865 * period. This syncs the hash iteration and freeing of items
2866 * on the hash. rcu_read_lock is too dangerous here.
2867 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04002868 preempt_disable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002869 hlist_for_each_entry_rcu(entry, n, hhd, node) {
2870 if (entry->ip == ip)
2871 entry->ops->func(ip, parent_ip, &entry->data);
2872 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04002873 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002874}
2875
Steven Rostedtb6887d72009-02-17 12:32:04 -05002876static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05002877{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04002878 .func = function_trace_probe_call,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002879};
2880
Steven Rostedtb6887d72009-02-17 12:32:04 -05002881static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002882
Steven Rostedtb6887d72009-02-17 12:32:04 -05002883static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002884{
Steven Rostedtb8489142011-05-04 09:27:52 -04002885 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002886 int i;
2887
Steven Rostedtb6887d72009-02-17 12:32:04 -05002888 if (ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002889 return;
2890
2891 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2892 struct hlist_head *hhd = &ftrace_func_hash[i];
2893 if (hhd->first)
2894 break;
2895 }
2896 /* Nothing registered? */
2897 if (i == FTRACE_FUNC_HASHSIZE)
2898 return;
2899
Steven Rostedtb8489142011-05-04 09:27:52 -04002900 ret = __register_ftrace_function(&trace_probe_ops);
2901 if (!ret)
Steven Rostedta1cd6172011-05-23 15:24:25 -04002902 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04002903
Steven Rostedtb6887d72009-02-17 12:32:04 -05002904 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002905}
2906
Steven Rostedtb6887d72009-02-17 12:32:04 -05002907static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002908{
Steven Rostedtb8489142011-05-04 09:27:52 -04002909 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002910 int i;
2911
Steven Rostedtb6887d72009-02-17 12:32:04 -05002912 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002913 return;
2914
2915 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2916 struct hlist_head *hhd = &ftrace_func_hash[i];
2917 if (hhd->first)
2918 return;
2919 }
2920
2921 /* no more funcs left */
Steven Rostedtb8489142011-05-04 09:27:52 -04002922 ret = __unregister_ftrace_function(&trace_probe_ops);
2923 if (!ret)
2924 ftrace_shutdown(&trace_probe_ops, 0);
2925
Steven Rostedtb6887d72009-02-17 12:32:04 -05002926 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002927}
2928
2929
2930static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2931{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002932 struct ftrace_func_probe *entry =
2933 container_of(rhp, struct ftrace_func_probe, rcu);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002934
2935 if (entry->ops->free)
2936 entry->ops->free(&entry->data);
2937 kfree(entry);
2938}
2939
2940
2941int
Steven Rostedtb6887d72009-02-17 12:32:04 -05002942register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002943 void *data)
2944{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002945 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002946 struct ftrace_page *pg;
2947 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002948 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002949 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002950 int count = 0;
2951 char *search;
2952
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002953 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002954 len = strlen(search);
2955
Steven Rostedtb6887d72009-02-17 12:32:04 -05002956 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002957 if (WARN_ON(not))
2958 return -EINVAL;
2959
2960 mutex_lock(&ftrace_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002961
Steven Rostedt45a4a232011-04-21 23:16:46 -04002962 if (unlikely(ftrace_disabled))
2963 goto out_unlock;
2964
Steven Rostedt59df055f2009-02-14 15:29:06 -05002965 do_for_each_ftrace_rec(pg, rec) {
2966
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002967 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05002968 continue;
2969
2970 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2971 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002972 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002973 if (!count)
2974 count = -ENOMEM;
2975 goto out_unlock;
2976 }
2977
2978 count++;
2979
2980 entry->data = data;
2981
2982 /*
2983 * The caller might want to do something special
2984 * for each function we find. We call the callback
2985 * to give the caller an opportunity to do so.
2986 */
2987 if (ops->callback) {
2988 if (ops->callback(rec->ip, &entry->data) < 0) {
2989 /* caller does not like this func */
2990 kfree(entry);
2991 continue;
2992 }
2993 }
2994
2995 entry->ops = ops;
2996 entry->ip = rec->ip;
2997
2998 key = hash_long(entry->ip, FTRACE_HASH_BITS);
2999 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3000
3001 } while_for_each_ftrace_rec();
Steven Rostedtb6887d72009-02-17 12:32:04 -05003002 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003003
3004 out_unlock:
3005 mutex_unlock(&ftrace_lock);
3006
3007 return count;
3008}
3009
3010enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003011 PROBE_TEST_FUNC = 1,
3012 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05003013};
3014
3015static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003016__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003017 void *data, int flags)
3018{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003019 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003020 struct hlist_node *n, *tmp;
3021 char str[KSYM_SYMBOL_LEN];
3022 int type = MATCH_FULL;
3023 int i, len = 0;
3024 char *search;
3025
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003026 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003027 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003028 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003029 int not;
3030
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003031 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003032 len = strlen(search);
3033
Steven Rostedtb6887d72009-02-17 12:32:04 -05003034 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003035 if (WARN_ON(not))
3036 return;
3037 }
3038
3039 mutex_lock(&ftrace_lock);
3040 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3041 struct hlist_head *hhd = &ftrace_func_hash[i];
3042
3043 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
3044
3045 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05003046 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003047 continue;
3048
Steven Rostedtb6887d72009-02-17 12:32:04 -05003049 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003050 continue;
3051
3052 /* do this last, since it is the most expensive */
3053 if (glob) {
3054 kallsyms_lookup(entry->ip, NULL, NULL,
3055 NULL, str);
3056 if (!ftrace_match(str, glob, len, type))
3057 continue;
3058 }
3059
3060 hlist_del(&entry->node);
3061 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
3062 }
3063 }
Steven Rostedtb6887d72009-02-17 12:32:04 -05003064 __disable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003065 mutex_unlock(&ftrace_lock);
3066}
3067
3068void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003069unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003070 void *data)
3071{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003072 __unregister_ftrace_function_probe(glob, ops, data,
3073 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003074}
3075
3076void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003077unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003078{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003079 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003080}
3081
Steven Rostedtb6887d72009-02-17 12:32:04 -05003082void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003083{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003084 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003085}
3086
Steven Rostedtf6180772009-02-14 00:40:25 -05003087static LIST_HEAD(ftrace_commands);
3088static DEFINE_MUTEX(ftrace_cmd_mutex);
3089
3090int register_ftrace_command(struct ftrace_func_command *cmd)
3091{
3092 struct ftrace_func_command *p;
3093 int ret = 0;
3094
3095 mutex_lock(&ftrace_cmd_mutex);
3096 list_for_each_entry(p, &ftrace_commands, list) {
3097 if (strcmp(cmd->name, p->name) == 0) {
3098 ret = -EBUSY;
3099 goto out_unlock;
3100 }
3101 }
3102 list_add(&cmd->list, &ftrace_commands);
3103 out_unlock:
3104 mutex_unlock(&ftrace_cmd_mutex);
3105
3106 return ret;
3107}
3108
3109int unregister_ftrace_command(struct ftrace_func_command *cmd)
3110{
3111 struct ftrace_func_command *p, *n;
3112 int ret = -ENODEV;
3113
3114 mutex_lock(&ftrace_cmd_mutex);
3115 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3116 if (strcmp(cmd->name, p->name) == 0) {
3117 ret = 0;
3118 list_del_init(&p->list);
3119 goto out_unlock;
3120 }
3121 }
3122 out_unlock:
3123 mutex_unlock(&ftrace_cmd_mutex);
3124
3125 return ret;
3126}
3127
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003128static int ftrace_process_regex(struct ftrace_hash *hash,
3129 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003130{
Steven Rostedtf6180772009-02-14 00:40:25 -05003131 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003132 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08003133 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003134
3135 func = strsep(&next, ":");
3136
3137 if (!next) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003138 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003139 if (!ret)
3140 ret = -EINVAL;
3141 if (ret < 0)
3142 return ret;
3143 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003144 }
3145
Steven Rostedtf6180772009-02-14 00:40:25 -05003146 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05003147
3148 command = strsep(&next, ":");
3149
Steven Rostedtf6180772009-02-14 00:40:25 -05003150 mutex_lock(&ftrace_cmd_mutex);
3151 list_for_each_entry(p, &ftrace_commands, list) {
3152 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003153 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05003154 goto out_unlock;
3155 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05003156 }
Steven Rostedtf6180772009-02-14 00:40:25 -05003157 out_unlock:
3158 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003159
Steven Rostedtf6180772009-02-14 00:40:25 -05003160 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003161}
3162
Ingo Molnare309b412008-05-12 21:20:51 +02003163static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003164ftrace_regex_write(struct file *file, const char __user *ubuf,
3165 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02003166{
3167 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003168 struct trace_parser *parser;
3169 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02003170
Li Zefan4ba79782009-09-22 13:52:20 +08003171 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02003172 return 0;
3173
Steven Rostedt41c52c02008-05-22 11:46:33 -04003174 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003175
Steven Rostedt45a4a232011-04-21 23:16:46 -04003176 ret = -ENODEV;
3177 if (unlikely(ftrace_disabled))
3178 goto out_unlock;
3179
Steven Rostedt5072c592008-05-12 21:20:43 +02003180 if (file->f_mode & FMODE_READ) {
3181 struct seq_file *m = file->private_data;
3182 iter = m->private;
3183 } else
3184 iter = file->private_data;
3185
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003186 parser = &iter->parser;
3187 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02003188
Li Zefan4ba79782009-09-22 13:52:20 +08003189 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003190 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003191 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003192 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08003193 trace_parser_clear(parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02003194 if (ret)
Li Zefaned146b252009-11-03 08:55:38 +08003195 goto out_unlock;
Steven Rostedt5072c592008-05-12 21:20:43 +02003196 }
3197
Steven Rostedt5072c592008-05-12 21:20:43 +02003198 ret = read;
Li Zefaned146b252009-11-03 08:55:38 +08003199out_unlock:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003200 mutex_unlock(&ftrace_regex_lock);
Li Zefaned146b252009-11-03 08:55:38 +08003201
Steven Rostedt5072c592008-05-12 21:20:43 +02003202 return ret;
3203}
3204
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003205ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003206ftrace_filter_write(struct file *file, const char __user *ubuf,
3207 size_t cnt, loff_t *ppos)
3208{
3209 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3210}
3211
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003212ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003213ftrace_notrace_write(struct file *file, const char __user *ubuf,
3214 size_t cnt, loff_t *ppos)
3215{
3216 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3217}
3218
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003219static int
Steven Rostedtf45948e2011-05-02 12:29:25 -04003220ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3221 int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003222{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003223 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003224 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003225 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003226
Steven Rostedt936e0742011-05-05 22:54:01 -04003227 /* All global ops uses the global ops filters */
3228 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3229 ops = &global_ops;
3230
Steven Rostedt41c52c02008-05-22 11:46:33 -04003231 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003232 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003233
Steven Rostedtf45948e2011-05-02 12:29:25 -04003234 if (enable)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003235 orig_hash = &ops->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003236 else
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003237 orig_hash = &ops->notrace_hash;
3238
3239 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3240 if (!hash)
3241 return -ENOMEM;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003242
Steven Rostedt41c52c02008-05-22 11:46:33 -04003243 mutex_lock(&ftrace_regex_lock);
3244 if (reset)
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003245 ftrace_filter_reset(hash);
Jiri Olsaac483c42012-01-02 10:04:14 +01003246 if (buf && !ftrace_match_records(hash, buf, len)) {
3247 ret = -EINVAL;
3248 goto out_regex_unlock;
3249 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003250
3251 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003252 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt072126f2011-07-13 15:08:31 -04003253 if (!ret && ops->flags & FTRACE_OPS_FL_ENABLED
3254 && ftrace_enabled)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01003255 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt072126f2011-07-13 15:08:31 -04003256
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003257 mutex_unlock(&ftrace_lock);
3258
Jiri Olsaac483c42012-01-02 10:04:14 +01003259 out_regex_unlock:
Steven Rostedt41c52c02008-05-22 11:46:33 -04003260 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003261
3262 free_ftrace_hash(hash);
3263 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003264}
3265
Steven Rostedt77a2b372008-05-12 21:20:45 +02003266/**
3267 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003268 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02003269 * @buf - the string that holds the function filter text.
3270 * @len - the length of the string.
3271 * @reset - non zero to reset all filters before applying this filter.
3272 *
3273 * Filters denote which functions should be enabled when tracing is enabled.
3274 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3275 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003276int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003277 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02003278{
Jiri Olsaac483c42012-01-02 10:04:14 +01003279 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003280}
Steven Rostedt936e0742011-05-05 22:54:01 -04003281EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003282
Steven Rostedt41c52c02008-05-22 11:46:33 -04003283/**
3284 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003285 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04003286 * @buf - the string that holds the function notrace text.
3287 * @len - the length of the string.
3288 * @reset - non zero to reset all filters before applying this filter.
3289 *
3290 * Notrace Filters denote which functions should not be enabled when tracing
3291 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3292 * for tracing.
3293 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003294int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003295 int len, int reset)
3296{
Jiri Olsaac483c42012-01-02 10:04:14 +01003297 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04003298}
3299EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3300/**
3301 * ftrace_set_filter - set a function to filter on in ftrace
3302 * @ops - the ops to set the filter with
3303 * @buf - the string that holds the function filter text.
3304 * @len - the length of the string.
3305 * @reset - non zero to reset all filters before applying this filter.
3306 *
3307 * Filters denote which functions should be enabled when tracing is enabled.
3308 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3309 */
3310void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3311{
3312 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3313}
3314EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3315
3316/**
3317 * ftrace_set_notrace - set a function to not trace in ftrace
3318 * @ops - the ops to set the notrace filter with
3319 * @buf - the string that holds the function notrace text.
3320 * @len - the length of the string.
3321 * @reset - non zero to reset all filters before applying this filter.
3322 *
3323 * Notrace Filters denote which functions should not be enabled when tracing
3324 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3325 * for tracing.
3326 */
3327void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003328{
Steven Rostedtf45948e2011-05-02 12:29:25 -04003329 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003330}
Steven Rostedt936e0742011-05-05 22:54:01 -04003331EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003332
Steven Rostedt2af15d62009-05-28 13:37:24 -04003333/*
3334 * command line interface to allow users to set filters on boot up.
3335 */
3336#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3337static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3338static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3339
3340static int __init set_ftrace_notrace(char *str)
3341{
3342 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
3343 return 1;
3344}
3345__setup("ftrace_notrace=", set_ftrace_notrace);
3346
3347static int __init set_ftrace_filter(char *str)
3348{
3349 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
3350 return 1;
3351}
3352__setup("ftrace_filter=", set_ftrace_filter);
3353
Stefan Assmann369bc182009-10-12 22:17:21 +02003354#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08003355static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Steven Rostedt801c29f2010-03-05 20:02:19 -05003356static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
3357
Stefan Assmann369bc182009-10-12 22:17:21 +02003358static int __init set_graph_function(char *str)
3359{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02003360 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02003361 return 1;
3362}
3363__setup("ftrace_graph_filter=", set_graph_function);
3364
3365static void __init set_ftrace_early_graph(char *buf)
3366{
3367 int ret;
3368 char *func;
3369
3370 while (buf) {
3371 func = strsep(&buf, ",");
3372 /* we allow only one expression at a time */
3373 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3374 func);
3375 if (ret)
3376 printk(KERN_DEBUG "ftrace: function %s not "
3377 "traceable\n", func);
3378 }
3379}
3380#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3381
Steven Rostedt2a85a372011-12-19 21:57:44 -05003382void __init
3383ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04003384{
3385 char *func;
3386
3387 while (buf) {
3388 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04003389 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003390 }
3391}
3392
3393static void __init set_ftrace_early_filters(void)
3394{
3395 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003396 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003397 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003398 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02003399#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3400 if (ftrace_graph_buf[0])
3401 set_ftrace_early_graph(ftrace_graph_buf);
3402#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04003403}
3404
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003405int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003406{
3407 struct seq_file *m = (struct seq_file *)file->private_data;
3408 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003409 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003410 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04003411 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003412 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02003413
Steven Rostedt41c52c02008-05-22 11:46:33 -04003414 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003415 if (file->f_mode & FMODE_READ) {
3416 iter = m->private;
3417
3418 seq_release(inode, file);
3419 } else
3420 iter = file->private_data;
3421
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003422 parser = &iter->parser;
3423 if (trace_parser_loaded(parser)) {
3424 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003425 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02003426 }
3427
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003428 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003429
Steven Rostedt058e2972011-04-29 22:35:33 -04003430 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04003431 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3432
3433 if (filter_hash)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003434 orig_hash = &iter->ops->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04003435 else
3436 orig_hash = &iter->ops->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003437
Steven Rostedt058e2972011-04-29 22:35:33 -04003438 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003439 ret = ftrace_hash_move(iter->ops, filter_hash,
3440 orig_hash, iter->hash);
3441 if (!ret && (iter->ops->flags & FTRACE_OPS_FL_ENABLED)
3442 && ftrace_enabled)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01003443 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003444
Steven Rostedt058e2972011-04-29 22:35:33 -04003445 mutex_unlock(&ftrace_lock);
3446 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003447 free_ftrace_hash(iter->hash);
3448 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04003449
Steven Rostedt41c52c02008-05-22 11:46:33 -04003450 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003451 return 0;
3452}
3453
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003454static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003455 .open = ftrace_avail_open,
3456 .read = seq_read,
3457 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08003458 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02003459};
3460
Steven Rostedt647bcd02011-05-03 14:39:21 -04003461static const struct file_operations ftrace_enabled_fops = {
3462 .open = ftrace_enabled_open,
3463 .read = seq_read,
3464 .llseek = seq_lseek,
3465 .release = seq_release_private,
3466};
3467
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003468static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003469 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003470 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02003471 .write = ftrace_filter_write,
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003472 .llseek = ftrace_regex_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003473 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02003474};
3475
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003476static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04003477 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003478 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003479 .write = ftrace_notrace_write,
3480 .llseek = ftrace_regex_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003481 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003482};
3483
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003484#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3485
3486static DEFINE_MUTEX(graph_lock);
3487
3488int ftrace_graph_count;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003489int ftrace_graph_filter_enabled;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003490unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3491
3492static void *
Li Zefan85951842009-06-24 09:54:00 +08003493__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003494{
Li Zefan85951842009-06-24 09:54:00 +08003495 if (*pos >= ftrace_graph_count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003496 return NULL;
Li Zefana4ec5e02009-09-18 14:06:28 +08003497 return &ftrace_graph_funcs[*pos];
Li Zefan85951842009-06-24 09:54:00 +08003498}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003499
Li Zefan85951842009-06-24 09:54:00 +08003500static void *
3501g_next(struct seq_file *m, void *v, loff_t *pos)
3502{
3503 (*pos)++;
3504 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003505}
3506
3507static void *g_start(struct seq_file *m, loff_t *pos)
3508{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003509 mutex_lock(&graph_lock);
3510
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003511 /* Nothing, tell g_show to print all functions are enabled */
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003512 if (!ftrace_graph_filter_enabled && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003513 return (void *)1;
3514
Li Zefan85951842009-06-24 09:54:00 +08003515 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003516}
3517
3518static void g_stop(struct seq_file *m, void *p)
3519{
3520 mutex_unlock(&graph_lock);
3521}
3522
3523static int g_show(struct seq_file *m, void *v)
3524{
3525 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003526
3527 if (!ptr)
3528 return 0;
3529
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003530 if (ptr == (unsigned long *)1) {
3531 seq_printf(m, "#### all functions enabled ####\n");
3532 return 0;
3533 }
3534
Steven Rostedtb375a112009-09-17 00:05:58 -04003535 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003536
3537 return 0;
3538}
3539
James Morris88e9d342009-09-22 16:43:43 -07003540static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003541 .start = g_start,
3542 .next = g_next,
3543 .stop = g_stop,
3544 .show = g_show,
3545};
3546
3547static int
3548ftrace_graph_open(struct inode *inode, struct file *file)
3549{
3550 int ret = 0;
3551
3552 if (unlikely(ftrace_disabled))
3553 return -ENODEV;
3554
3555 mutex_lock(&graph_lock);
3556 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04003557 (file->f_flags & O_TRUNC)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003558 ftrace_graph_filter_enabled = 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003559 ftrace_graph_count = 0;
3560 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3561 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003562 mutex_unlock(&graph_lock);
3563
Li Zefana4ec5e02009-09-18 14:06:28 +08003564 if (file->f_mode & FMODE_READ)
3565 ret = seq_open(file, &ftrace_graph_seq_ops);
3566
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003567 return ret;
3568}
3569
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003570static int
Li Zefan87827112009-07-23 11:29:11 +08003571ftrace_graph_release(struct inode *inode, struct file *file)
3572{
3573 if (file->f_mode & FMODE_READ)
3574 seq_release(inode, file);
3575 return 0;
3576}
3577
3578static int
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003579ftrace_set_func(unsigned long *array, int *idx, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003580{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003581 struct dyn_ftrace *rec;
3582 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003583 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003584 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003585 int type, not;
3586 char *search;
3587 bool exists;
3588 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003589
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003590 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003591 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003592 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3593 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003594
3595 search_len = strlen(search);
3596
Steven Rostedt52baf112009-02-14 01:15:39 -05003597 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003598
3599 if (unlikely(ftrace_disabled)) {
3600 mutex_unlock(&ftrace_lock);
3601 return -ENODEV;
3602 }
3603
Steven Rostedt265c8312009-02-13 12:43:56 -05003604 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003605
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003606 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003607 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003608 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003609 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003610 if (array[i] == rec->ip) {
3611 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05003612 break;
3613 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003614 }
3615
3616 if (!not) {
3617 fail = 0;
3618 if (!exists) {
3619 array[(*idx)++] = rec->ip;
3620 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3621 goto out;
3622 }
3623 } else {
3624 if (exists) {
3625 array[i] = array[--(*idx)];
3626 array[*idx] = 0;
3627 fail = 0;
3628 }
3629 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003630 }
Steven Rostedt265c8312009-02-13 12:43:56 -05003631 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003632out:
Steven Rostedt52baf112009-02-14 01:15:39 -05003633 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003634
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003635 if (fail)
3636 return -EINVAL;
3637
3638 ftrace_graph_filter_enabled = 1;
3639 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003640}
3641
3642static ssize_t
3643ftrace_graph_write(struct file *file, const char __user *ubuf,
3644 size_t cnt, loff_t *ppos)
3645{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003646 struct trace_parser parser;
Li Zefan4ba79782009-09-22 13:52:20 +08003647 ssize_t read, ret;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003648
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003649 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003650 return 0;
3651
3652 mutex_lock(&graph_lock);
3653
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003654 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3655 ret = -ENOMEM;
Li Zefan1eb90f12009-09-22 13:52:57 +08003656 goto out_unlock;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003657 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003658
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003659 read = trace_get_user(&parser, ubuf, cnt, ppos);
3660
Li Zefan4ba79782009-09-22 13:52:20 +08003661 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003662 parser.buffer[parser.idx] = 0;
3663
3664 /* we allow only one expression at a time */
Li Zefana4ec5e02009-09-18 14:06:28 +08003665 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003666 parser.buffer);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003667 if (ret)
Li Zefan1eb90f12009-09-22 13:52:57 +08003668 goto out_free;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003669 }
3670
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003671 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08003672
3673out_free:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003674 trace_parser_put(&parser);
Li Zefan1eb90f12009-09-22 13:52:57 +08003675out_unlock:
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003676 mutex_unlock(&graph_lock);
3677
3678 return ret;
3679}
3680
3681static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08003682 .open = ftrace_graph_open,
3683 .read = seq_read,
3684 .write = ftrace_graph_write,
3685 .release = ftrace_graph_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003686 .llseek = seq_lseek,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003687};
3688#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3689
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003690static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02003691{
Steven Rostedt5072c592008-05-12 21:20:43 +02003692
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003693 trace_create_file("available_filter_functions", 0444,
3694 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02003695
Steven Rostedt647bcd02011-05-03 14:39:21 -04003696 trace_create_file("enabled_functions", 0444,
3697 d_tracer, NULL, &ftrace_enabled_fops);
3698
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003699 trace_create_file("set_ftrace_filter", 0644, d_tracer,
3700 NULL, &ftrace_filter_fops);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003701
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003702 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003703 NULL, &ftrace_notrace_fops);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04003704
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003705#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003706 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003707 NULL,
3708 &ftrace_graph_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003709#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3710
Steven Rostedt5072c592008-05-12 21:20:43 +02003711 return 0;
3712}
3713
Steven Rostedt9fd49322012-04-24 22:32:06 -04003714static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05003715{
Steven Rostedt9fd49322012-04-24 22:32:06 -04003716 const unsigned long *ipa = a;
3717 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05003718
Steven Rostedt9fd49322012-04-24 22:32:06 -04003719 if (*ipa > *ipb)
3720 return 1;
3721 if (*ipa < *ipb)
3722 return -1;
3723 return 0;
3724}
3725
3726static void ftrace_swap_ips(void *a, void *b, int size)
3727{
3728 unsigned long *ipa = a;
3729 unsigned long *ipb = b;
3730 unsigned long t;
3731
3732 t = *ipa;
3733 *ipa = *ipb;
3734 *ipb = t;
Steven Rostedt68950612011-12-16 17:06:45 -05003735}
3736
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003737static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08003738 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003739 unsigned long *end)
3740{
Steven Rostedt706c81f2012-04-24 23:45:26 -04003741 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05003742 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04003743 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05003744 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003745 unsigned long *p;
3746 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04003747 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05003748 int ret = -ENOMEM;
3749
3750 count = end - start;
3751
3752 if (!count)
3753 return 0;
3754
Steven Rostedt9fd49322012-04-24 22:32:06 -04003755 sort(start, count, sizeof(*start),
3756 ftrace_cmp_ips, ftrace_swap_ips);
3757
Steven Rostedt706c81f2012-04-24 23:45:26 -04003758 start_pg = ftrace_allocate_pages(count);
3759 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05003760 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003761
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003762 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05003763
Steven Rostedt32082302011-12-16 14:42:37 -05003764 /*
3765 * Core and each module needs their own pages, as
3766 * modules will free them when they are removed.
3767 * Force a new page to be allocated for modules.
3768 */
Steven Rostedta7900872011-12-16 16:23:44 -05003769 if (!mod) {
3770 WARN_ON(ftrace_pages || ftrace_pages_start);
3771 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04003772 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05003773 } else {
Steven Rostedt32082302011-12-16 14:42:37 -05003774 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05003775 goto out;
Steven Rostedt32082302011-12-16 14:42:37 -05003776
Steven Rostedta7900872011-12-16 16:23:44 -05003777 if (WARN_ON(ftrace_pages->next)) {
3778 /* Hmm, we have free pages? */
3779 while (ftrace_pages->next)
3780 ftrace_pages = ftrace_pages->next;
Steven Rostedt32082302011-12-16 14:42:37 -05003781 }
Steven Rostedta7900872011-12-16 16:23:44 -05003782
Steven Rostedt706c81f2012-04-24 23:45:26 -04003783 ftrace_pages->next = start_pg;
Steven Rostedt32082302011-12-16 14:42:37 -05003784 }
3785
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003786 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04003787 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003788 while (p < end) {
3789 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08003790 /*
3791 * Some architecture linkers will pad between
3792 * the different mcount_loc sections of different
3793 * object files to satisfy alignments.
3794 * Skip any NULL pointers.
3795 */
3796 if (!addr)
3797 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04003798
3799 if (pg->index == pg->size) {
3800 /* We should have allocated enough */
3801 if (WARN_ON(!pg->next))
3802 break;
3803 pg = pg->next;
3804 }
3805
3806 rec = &pg->records[pg->index++];
3807 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003808 }
3809
Steven Rostedt706c81f2012-04-24 23:45:26 -04003810 /* We should have used all pages */
3811 WARN_ON(pg->next);
3812
3813 /* Assign the last page to ftrace_pages */
3814 ftrace_pages = pg;
3815
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003816 /* These new locations need to be initialized */
Steven Rostedt706c81f2012-04-24 23:45:26 -04003817 ftrace_new_pgs = start_pg;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003818
Steven Rostedta4f18ed2011-06-07 09:26:46 -04003819 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04003820 * We only need to disable interrupts on start up
3821 * because we are modifying code that an interrupt
3822 * may execute, and the modification is not atomic.
3823 * But for modules, nothing runs the code we modify
3824 * until we are finished with it, and there's no
3825 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04003826 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04003827 if (!mod)
3828 local_irq_save(flags);
Steven Rostedt31e88902008-11-14 16:21:19 -08003829 ftrace_update_code(mod);
Steven Rostedt4376cac2011-06-24 23:28:13 -04003830 if (!mod)
3831 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05003832 ret = 0;
3833 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003834 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003835
Steven Rostedta7900872011-12-16 16:23:44 -05003836 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003837}
3838
Steven Rostedt93eb6772009-04-15 13:24:06 -04003839#ifdef CONFIG_MODULES
Steven Rostedt32082302011-12-16 14:42:37 -05003840
3841#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
3842
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003843void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04003844{
3845 struct dyn_ftrace *rec;
Steven Rostedt32082302011-12-16 14:42:37 -05003846 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04003847 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05003848 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04003849
Steven Rostedt93eb6772009-04-15 13:24:06 -04003850 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003851
3852 if (ftrace_disabled)
3853 goto out_unlock;
3854
Steven Rostedt32082302011-12-16 14:42:37 -05003855 /*
3856 * Each module has its own ftrace_pages, remove
3857 * them from the list.
3858 */
3859 last_pg = &ftrace_pages_start;
3860 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
3861 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003862 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04003863 /*
Steven Rostedt32082302011-12-16 14:42:37 -05003864 * As core pages are first, the first
3865 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04003866 */
Steven Rostedt32082302011-12-16 14:42:37 -05003867 if (WARN_ON(pg == ftrace_pages_start))
3868 goto out_unlock;
3869
3870 /* Check if we are deleting the last page */
3871 if (pg == ftrace_pages)
3872 ftrace_pages = next_to_ftrace_page(last_pg);
3873
3874 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05003875 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3876 free_pages((unsigned long)pg->records, order);
3877 kfree(pg);
Steven Rostedt32082302011-12-16 14:42:37 -05003878 } else
3879 last_pg = &pg->next;
3880 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003881 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04003882 mutex_unlock(&ftrace_lock);
3883}
3884
3885static void ftrace_init_module(struct module *mod,
3886 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04003887{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04003888 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04003889 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003890 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04003891}
3892
Steven Rostedt93eb6772009-04-15 13:24:06 -04003893static int ftrace_module_notify(struct notifier_block *self,
3894 unsigned long val, void *data)
3895{
3896 struct module *mod = data;
3897
3898 switch (val) {
3899 case MODULE_STATE_COMING:
3900 ftrace_init_module(mod, mod->ftrace_callsites,
3901 mod->ftrace_callsites +
3902 mod->num_ftrace_callsites);
3903 break;
3904 case MODULE_STATE_GOING:
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003905 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04003906 break;
3907 }
3908
3909 return 0;
3910}
3911#else
3912static int ftrace_module_notify(struct notifier_block *self,
3913 unsigned long val, void *data)
3914{
3915 return 0;
3916}
3917#endif /* CONFIG_MODULES */
3918
3919struct notifier_block ftrace_module_nb = {
3920 .notifier_call = ftrace_module_notify,
3921 .priority = 0,
3922};
3923
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003924extern unsigned long __start_mcount_loc[];
3925extern unsigned long __stop_mcount_loc[];
3926
3927void __init ftrace_init(void)
3928{
3929 unsigned long count, addr, flags;
3930 int ret;
3931
3932 /* Keep the ftrace pointer to the stub */
3933 addr = (unsigned long)ftrace_stub;
3934
3935 local_irq_save(flags);
3936 ftrace_dyn_arch_init(&addr);
3937 local_irq_restore(flags);
3938
3939 /* ftrace_dyn_arch_init places the return code in addr */
3940 if (addr)
3941 goto failed;
3942
3943 count = __stop_mcount_loc - __start_mcount_loc;
3944
3945 ret = ftrace_dyn_table_alloc(count);
3946 if (ret)
3947 goto failed;
3948
3949 last_ftrace_enabled = ftrace_enabled = 1;
3950
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003951 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08003952 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003953 __stop_mcount_loc);
3954
Steven Rostedt93eb6772009-04-15 13:24:06 -04003955 ret = register_module_notifier(&ftrace_module_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08003956 if (ret)
Steven Rostedt93eb6772009-04-15 13:24:06 -04003957 pr_warning("Failed to register trace ftrace module notifier\n");
3958
Steven Rostedt2af15d62009-05-28 13:37:24 -04003959 set_ftrace_early_filters();
3960
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003961 return;
3962 failed:
3963 ftrace_disabled = 1;
3964}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003965
Steven Rostedt3d083392008-05-12 21:20:42 +02003966#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01003967
Steven Rostedt2b499382011-05-03 22:49:52 -04003968static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04003969 .func = ftrace_stub,
3970};
3971
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01003972static int __init ftrace_nodyn_init(void)
3973{
3974 ftrace_enabled = 1;
3975 return 0;
3976}
3977device_initcall(ftrace_nodyn_init);
3978
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003979static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
3980static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003981/* Keep as macros so we do not need to define the commands */
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04003982# define ftrace_startup(ops, command) \
3983 ({ \
3984 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
3985 0; \
3986 })
Steven Rostedtbd69c302011-05-03 21:55:54 -04003987# define ftrace_shutdown(ops, command) do { } while (0)
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003988# define ftrace_startup_sysctl() do { } while (0)
3989# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04003990
3991static inline int
3992ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
3993{
3994 return 1;
3995}
3996
Steven Rostedt3d083392008-05-12 21:20:42 +02003997#endif /* CONFIG_DYNAMIC_FTRACE */
3998
Steven Rostedtb8489142011-05-04 09:27:52 -04003999static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004000ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004001 struct ftrace_ops *op, struct pt_regs *regs)
Jiri Olsae2484912012-02-15 15:51:48 +01004002{
Jiri Olsae2484912012-02-15 15:51:48 +01004003 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4004 return;
4005
4006 /*
4007 * Some of the ops may be dynamically allocated,
4008 * they must be freed after a synchronize_sched().
4009 */
4010 preempt_disable_notrace();
4011 trace_recursion_set(TRACE_CONTROL_BIT);
4012 op = rcu_dereference_raw(ftrace_control_list);
4013 while (op != &ftrace_list_end) {
4014 if (!ftrace_function_local_disabled(op) &&
4015 ftrace_ops_test(op, ip))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004016 op->func(ip, parent_ip, op, regs);
Jiri Olsae2484912012-02-15 15:51:48 +01004017
4018 op = rcu_dereference_raw(op->next);
4019 };
4020 trace_recursion_clear(TRACE_CONTROL_BIT);
4021 preempt_enable_notrace();
4022}
4023
4024static struct ftrace_ops control_ops = {
4025 .func = ftrace_ops_control_func,
4026};
4027
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004028static inline void
4029__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004030 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004031{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004032 struct ftrace_ops *op;
Steven Rostedtb8489142011-05-04 09:27:52 -04004033
Steven Rostedtccf36722012-06-05 09:44:25 -04004034 if (function_trace_stop)
4035 return;
4036
Steven Rostedtb1cff0a2011-05-25 14:27:43 -04004037 if (unlikely(trace_recursion_test(TRACE_INTERNAL_BIT)))
4038 return;
4039
4040 trace_recursion_set(TRACE_INTERNAL_BIT);
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004041 /*
4042 * Some of the ops may be dynamically allocated,
4043 * they must be freed after a synchronize_sched().
4044 */
4045 preempt_disable_notrace();
4046 op = rcu_dereference_raw(ftrace_ops_list);
Steven Rostedtb8489142011-05-04 09:27:52 -04004047 while (op != &ftrace_list_end) {
4048 if (ftrace_ops_test(op, ip))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004049 op->func(ip, parent_ip, op, regs);
Steven Rostedtb8489142011-05-04 09:27:52 -04004050 op = rcu_dereference_raw(op->next);
4051 };
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004052 preempt_enable_notrace();
Steven Rostedtb1cff0a2011-05-25 14:27:43 -04004053 trace_recursion_clear(TRACE_INTERNAL_BIT);
Steven Rostedtb8489142011-05-04 09:27:52 -04004054}
4055
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004056/*
4057 * Some archs only support passing ip and parent_ip. Even though
4058 * the list function ignores the op parameter, we do not want any
4059 * C side effects, where a function is called without the caller
4060 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004061 * Archs are to support both the regs and ftrace_ops at the same time.
4062 * If they support ftrace_ops, it is assumed they support regs.
4063 * If call backs want to use regs, they must either check for regs
4064 * being NULL, or ARCH_SUPPORTS_FTRACE_SAVE_REGS.
4065 * Note, ARCH_SUPPORT_SAVE_REGS expects a full regs to be saved.
4066 * An architecture can pass partial regs with ftrace_ops and still
4067 * set the ARCH_SUPPORT_FTARCE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004068 */
4069#if ARCH_SUPPORTS_FTRACE_OPS
4070static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004071 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004072{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004073 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004074}
4075#else
4076static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4077{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004078 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004079}
4080#endif
4081
Steven Rostedte32d8952008-12-04 00:26:41 -05004082static void clear_ftrace_swapper(void)
4083{
4084 struct task_struct *p;
4085 int cpu;
4086
4087 get_online_cpus();
4088 for_each_online_cpu(cpu) {
4089 p = idle_task(cpu);
4090 clear_tsk_trace_trace(p);
4091 }
4092 put_online_cpus();
4093}
4094
4095static void set_ftrace_swapper(void)
4096{
4097 struct task_struct *p;
4098 int cpu;
4099
4100 get_online_cpus();
4101 for_each_online_cpu(cpu) {
4102 p = idle_task(cpu);
4103 set_tsk_trace_trace(p);
4104 }
4105 put_online_cpus();
4106}
4107
4108static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004109{
4110 struct task_struct *p;
4111
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004112 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05004113 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05004114 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05004115 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004116 rcu_read_unlock();
4117
Steven Rostedte32d8952008-12-04 00:26:41 -05004118 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004119}
4120
Steven Rostedte32d8952008-12-04 00:26:41 -05004121static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004122{
4123 struct task_struct *p;
4124
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004125 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004126 do_each_pid_task(pid, PIDTYPE_PID, p) {
4127 set_tsk_trace_trace(p);
4128 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004129 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004130}
4131
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004132static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004133{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004134 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004135 clear_ftrace_swapper();
4136 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004137 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05004138}
4139
4140static void set_ftrace_pid_task(struct pid *pid)
4141{
4142 if (pid == ftrace_swapper_pid)
4143 set_ftrace_swapper();
4144 else
4145 set_ftrace_pid(pid);
4146}
4147
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004148static int ftrace_pid_add(int p)
4149{
4150 struct pid *pid;
4151 struct ftrace_pid *fpid;
4152 int ret = -EINVAL;
4153
4154 mutex_lock(&ftrace_lock);
4155
4156 if (!p)
4157 pid = ftrace_swapper_pid;
4158 else
4159 pid = find_get_pid(p);
4160
4161 if (!pid)
4162 goto out;
4163
4164 ret = 0;
4165
4166 list_for_each_entry(fpid, &ftrace_pids, list)
4167 if (fpid->pid == pid)
4168 goto out_put;
4169
4170 ret = -ENOMEM;
4171
4172 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4173 if (!fpid)
4174 goto out_put;
4175
4176 list_add(&fpid->list, &ftrace_pids);
4177 fpid->pid = pid;
4178
4179 set_ftrace_pid_task(pid);
4180
4181 ftrace_update_pid_func();
4182 ftrace_startup_enable(0);
4183
4184 mutex_unlock(&ftrace_lock);
4185 return 0;
4186
4187out_put:
4188 if (pid != ftrace_swapper_pid)
4189 put_pid(pid);
4190
4191out:
4192 mutex_unlock(&ftrace_lock);
4193 return ret;
4194}
4195
4196static void ftrace_pid_reset(void)
4197{
4198 struct ftrace_pid *fpid, *safe;
4199
4200 mutex_lock(&ftrace_lock);
4201 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4202 struct pid *pid = fpid->pid;
4203
4204 clear_ftrace_pid_task(pid);
4205
4206 list_del(&fpid->list);
4207 kfree(fpid);
4208 }
4209
4210 ftrace_update_pid_func();
4211 ftrace_startup_enable(0);
4212
4213 mutex_unlock(&ftrace_lock);
4214}
4215
4216static void *fpid_start(struct seq_file *m, loff_t *pos)
4217{
4218 mutex_lock(&ftrace_lock);
4219
4220 if (list_empty(&ftrace_pids) && (!*pos))
4221 return (void *) 1;
4222
4223 return seq_list_start(&ftrace_pids, *pos);
4224}
4225
4226static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4227{
4228 if (v == (void *)1)
4229 return NULL;
4230
4231 return seq_list_next(v, &ftrace_pids, pos);
4232}
4233
4234static void fpid_stop(struct seq_file *m, void *p)
4235{
4236 mutex_unlock(&ftrace_lock);
4237}
4238
4239static int fpid_show(struct seq_file *m, void *v)
4240{
4241 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4242
4243 if (v == (void *)1) {
4244 seq_printf(m, "no pid\n");
4245 return 0;
4246 }
4247
4248 if (fpid->pid == ftrace_swapper_pid)
4249 seq_printf(m, "swapper tasks\n");
4250 else
4251 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4252
4253 return 0;
4254}
4255
4256static const struct seq_operations ftrace_pid_sops = {
4257 .start = fpid_start,
4258 .next = fpid_next,
4259 .stop = fpid_stop,
4260 .show = fpid_show,
4261};
4262
4263static int
4264ftrace_pid_open(struct inode *inode, struct file *file)
4265{
4266 int ret = 0;
4267
4268 if ((file->f_mode & FMODE_WRITE) &&
4269 (file->f_flags & O_TRUNC))
4270 ftrace_pid_reset();
4271
4272 if (file->f_mode & FMODE_READ)
4273 ret = seq_open(file, &ftrace_pid_sops);
4274
4275 return ret;
4276}
4277
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004278static ssize_t
4279ftrace_pid_write(struct file *filp, const char __user *ubuf,
4280 size_t cnt, loff_t *ppos)
4281{
Ingo Molnar457dc922009-11-23 11:03:28 +01004282 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004283 long val;
4284 int ret;
4285
4286 if (cnt >= sizeof(buf))
4287 return -EINVAL;
4288
4289 if (copy_from_user(&buf, ubuf, cnt))
4290 return -EFAULT;
4291
4292 buf[cnt] = 0;
4293
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004294 /*
4295 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4296 * to clean the filter quietly.
4297 */
Ingo Molnar457dc922009-11-23 11:03:28 +01004298 tmp = strstrip(buf);
4299 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004300 return 1;
4301
Ingo Molnar457dc922009-11-23 11:03:28 +01004302 ret = strict_strtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004303 if (ret < 0)
4304 return ret;
4305
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004306 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004307
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004308 return ret ? ret : cnt;
4309}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004310
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004311static int
4312ftrace_pid_release(struct inode *inode, struct file *file)
4313{
4314 if (file->f_mode & FMODE_READ)
4315 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004316
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004317 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004318}
4319
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004320static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004321 .open = ftrace_pid_open,
4322 .write = ftrace_pid_write,
4323 .read = seq_read,
4324 .llseek = seq_lseek,
4325 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004326};
4327
4328static __init int ftrace_init_debugfs(void)
4329{
4330 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004331
4332 d_tracer = tracing_init_dentry();
4333 if (!d_tracer)
4334 return 0;
4335
4336 ftrace_init_dyn_debugfs(d_tracer);
4337
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004338 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4339 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04004340
4341 ftrace_profile_debugfs(d_tracer);
4342
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004343 return 0;
4344}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004345fs_initcall(ftrace_init_debugfs);
4346
Steven Rostedt3d083392008-05-12 21:20:42 +02004347/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004348 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004349 *
4350 * This function should be used by panic code. It stops ftrace
4351 * but in a not so nice way. If you need to simply kill ftrace
4352 * from a non-atomic section, use ftrace_kill.
4353 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004354void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004355{
4356 ftrace_disabled = 1;
4357 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004358 clear_ftrace_function();
4359}
4360
4361/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04004362 * Test if ftrace is dead or not.
4363 */
4364int ftrace_is_dead(void)
4365{
4366 return ftrace_disabled;
4367}
4368
4369/**
Steven Rostedt3d083392008-05-12 21:20:42 +02004370 * register_ftrace_function - register a function for profiling
4371 * @ops - ops structure that holds the function for profiling.
4372 *
4373 * Register a function to be called by all functions in the
4374 * kernel.
4375 *
4376 * Note: @ops->func and all the functions it calls must be labeled
4377 * with "notrace", otherwise it will go into a
4378 * recursive loop.
4379 */
4380int register_ftrace_function(struct ftrace_ops *ops)
4381{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004382 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004383
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004384 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004385
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004386 ret = __register_ftrace_function(ops);
Steven Rostedtb8489142011-05-04 09:27:52 -04004387 if (!ret)
Steven Rostedta1cd6172011-05-23 15:24:25 -04004388 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04004389
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004390 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02004391
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004392 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02004393}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004394EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02004395
4396/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01004397 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02004398 * @ops - ops structure that holds the function to unregister
4399 *
4400 * Unregister a function that was added to be called by ftrace profiling.
4401 */
4402int unregister_ftrace_function(struct ftrace_ops *ops)
4403{
4404 int ret;
4405
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004406 mutex_lock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004407 ret = __unregister_ftrace_function(ops);
Steven Rostedtb8489142011-05-04 09:27:52 -04004408 if (!ret)
4409 ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004410 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004411
4412 return ret;
4413}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004414EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004415
Ingo Molnare309b412008-05-12 21:20:51 +02004416int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004417ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004418 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004419 loff_t *ppos)
4420{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004421 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004422
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004423 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004424
Steven Rostedt45a4a232011-04-21 23:16:46 -04004425 if (unlikely(ftrace_disabled))
4426 goto out;
4427
4428 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004429
Li Zefana32c7762009-06-26 16:55:51 +08004430 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004431 goto out;
4432
Li Zefana32c7762009-06-26 16:55:51 +08004433 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004434
4435 if (ftrace_enabled) {
4436
4437 ftrace_startup_sysctl();
4438
4439 /* we are starting ftrace again */
Steven Rostedtb8489142011-05-04 09:27:52 -04004440 if (ftrace_ops_list != &ftrace_list_end) {
4441 if (ftrace_ops_list->next == &ftrace_list_end)
4442 ftrace_trace_function = ftrace_ops_list->func;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004443 else
Steven Rostedtb8489142011-05-04 09:27:52 -04004444 ftrace_trace_function = ftrace_ops_list_func;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004445 }
4446
4447 } else {
4448 /* stopping ftrace calls (just send to ftrace_stub) */
4449 ftrace_trace_function = ftrace_stub;
4450
4451 ftrace_shutdown_sysctl();
4452 }
4453
4454 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004455 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004456 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02004457}
Ingo Molnarf17845e2008-10-24 12:47:10 +02004458
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004459#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004460
Steven Rostedt597af812009-04-03 15:24:12 -04004461static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004462static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004463
Steven Rostedte49dc192008-12-02 23:50:05 -05004464int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4465{
4466 return 0;
4467}
4468
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004469/* The callbacks that hook a function */
4470trace_func_graph_ret_t ftrace_graph_return =
4471 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004472trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004473
4474/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4475static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4476{
4477 int i;
4478 int ret = 0;
4479 unsigned long flags;
4480 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4481 struct task_struct *g, *t;
4482
4483 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4484 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4485 * sizeof(struct ftrace_ret_stack),
4486 GFP_KERNEL);
4487 if (!ret_stack_list[i]) {
4488 start = 0;
4489 end = i;
4490 ret = -ENOMEM;
4491 goto free;
4492 }
4493 }
4494
4495 read_lock_irqsave(&tasklist_lock, flags);
4496 do_each_thread(g, t) {
4497 if (start == end) {
4498 ret = -EAGAIN;
4499 goto unlock;
4500 }
4501
4502 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01004503 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004504 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04004505 t->curr_ret_stack = -1;
4506 /* Make sure the tasks see the -1 first: */
4507 smp_wmb();
4508 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004509 }
4510 } while_each_thread(g, t);
4511
4512unlock:
4513 read_unlock_irqrestore(&tasklist_lock, flags);
4514free:
4515 for (i = start; i < end; i++)
4516 kfree(ret_stack_list[i]);
4517 return ret;
4518}
4519
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004520static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04004521ftrace_graph_probe_sched_switch(void *ignore,
4522 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004523{
4524 unsigned long long timestamp;
4525 int index;
4526
Steven Rostedtbe6f1642009-03-24 11:06:24 -04004527 /*
4528 * Does the user want to count the time a function was asleep.
4529 * If so, do not update the time stamps.
4530 */
4531 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4532 return;
4533
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004534 timestamp = trace_clock_local();
4535
4536 prev->ftrace_timestamp = timestamp;
4537
4538 /* only process tasks that we timestamped */
4539 if (!next->ftrace_timestamp)
4540 return;
4541
4542 /*
4543 * Update all the counters in next to make up for the
4544 * time next was sleeping.
4545 */
4546 timestamp -= next->ftrace_timestamp;
4547
4548 for (index = next->curr_ret_stack; index >= 0; index--)
4549 next->ret_stack[index].calltime += timestamp;
4550}
4551
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004552/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004553static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004554{
4555 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004556 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004557
4558 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4559 sizeof(struct ftrace_ret_stack *),
4560 GFP_KERNEL);
4561
4562 if (!ret_stack_list)
4563 return -ENOMEM;
4564
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004565 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04004566 for_each_online_cpu(cpu) {
4567 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05004568 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04004569 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004570
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004571 do {
4572 ret = alloc_retstack_tasklist(ret_stack_list);
4573 } while (ret == -EAGAIN);
4574
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004575 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04004576 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004577 if (ret)
4578 pr_info("ftrace_graph: Couldn't activate tracepoint"
4579 " probe to kernel_sched_switch\n");
4580 }
4581
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004582 kfree(ret_stack_list);
4583 return ret;
4584}
4585
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004586/*
4587 * Hibernation protection.
4588 * The state of the current task is too much unstable during
4589 * suspend/restore to disk. We want to protect against that.
4590 */
4591static int
4592ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4593 void *unused)
4594{
4595 switch (state) {
4596 case PM_HIBERNATION_PREPARE:
4597 pause_graph_tracing();
4598 break;
4599
4600 case PM_POST_HIBERNATION:
4601 unpause_graph_tracing();
4602 break;
4603 }
4604 return NOTIFY_DONE;
4605}
4606
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004607int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4608 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004609{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004610 int ret = 0;
4611
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004612 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004613
Steven Rostedt05ce5812009-03-24 00:18:31 -04004614 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04004615 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04004616 ret = -EBUSY;
4617 goto out;
4618 }
4619
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004620 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
4621 register_pm_notifier(&ftrace_suspend_notifier);
4622
Steven Rostedt597af812009-04-03 15:24:12 -04004623 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004624 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004625 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04004626 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004627 goto out;
4628 }
Steven Rostedte53a6312008-11-26 00:16:25 -05004629
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004630 ftrace_graph_return = retfunc;
4631 ftrace_graph_entry = entryfunc;
Steven Rostedte53a6312008-11-26 00:16:25 -05004632
Steven Rostedta1cd6172011-05-23 15:24:25 -04004633 ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004634
4635out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004636 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004637 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004638}
4639
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004640void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004641{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004642 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004643
Steven Rostedt597af812009-04-03 15:24:12 -04004644 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004645 goto out;
4646
Steven Rostedt597af812009-04-03 15:24:12 -04004647 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004648 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004649 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedtbd69c302011-05-03 21:55:54 -04004650 ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004651 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04004652 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004653
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004654 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004655 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004656}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004657
Steven Rostedt868baf02011-02-10 21:26:13 -05004658static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
4659
4660static void
4661graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
4662{
4663 atomic_set(&t->tracing_graph_pause, 0);
4664 atomic_set(&t->trace_overrun, 0);
4665 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004666 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05004667 smp_wmb();
4668 t->ret_stack = ret_stack;
4669}
4670
4671/*
4672 * Allocate a return stack for the idle task. May be the first
4673 * time through, or it may be done by CPU hotplug online.
4674 */
4675void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
4676{
4677 t->curr_ret_stack = -1;
4678 /*
4679 * The idle task has no parent, it either has its own
4680 * stack or no stack at all.
4681 */
4682 if (t->ret_stack)
4683 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
4684
4685 if (ftrace_graph_active) {
4686 struct ftrace_ret_stack *ret_stack;
4687
4688 ret_stack = per_cpu(idle_ret_stack, cpu);
4689 if (!ret_stack) {
4690 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4691 * sizeof(struct ftrace_ret_stack),
4692 GFP_KERNEL);
4693 if (!ret_stack)
4694 return;
4695 per_cpu(idle_ret_stack, cpu) = ret_stack;
4696 }
4697 graph_init_task(t, ret_stack);
4698 }
4699}
4700
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004701/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004702void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004703{
Steven Rostedt84047e32009-06-02 16:51:55 -04004704 /* Make sure we do not use the parent ret_stack */
4705 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05004706 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04004707
Steven Rostedt597af812009-04-03 15:24:12 -04004708 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04004709 struct ftrace_ret_stack *ret_stack;
4710
4711 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004712 * sizeof(struct ftrace_ret_stack),
4713 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04004714 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004715 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05004716 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04004717 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004718}
4719
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004720void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004721{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004722 struct ftrace_ret_stack *ret_stack = t->ret_stack;
4723
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004724 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004725 /* NULL must become visible to IRQs before we free it: */
4726 barrier();
4727
4728 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004729}
Steven Rostedt14a866c2008-12-02 23:50:02 -05004730
4731void ftrace_graph_stop(void)
4732{
4733 ftrace_stop();
4734}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004735#endif