blob: ad765b4ba4268122618bc0e31b8f6e8290158ac7 [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,
Steven Rostedt47409742012-07-20 11:04:44 -040069 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040070};
71
Steven Rostedt4eebcc82008-05-12 21:20:48 +020072/* ftrace_enabled is a method to turn ftrace on or off */
73int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020074static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020075
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050076/* Quick disabling of function tracer. */
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040077int function_trace_stop __read_mostly;
78
79/* Current function tracing op */
80struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050081
jolsa@redhat.com756d17e2009-10-13 16:33:52 -040082/* List for set_ftrace_pid's pids. */
83LIST_HEAD(ftrace_pids);
84struct ftrace_pid {
85 struct list_head list;
86 struct pid *pid;
87};
88
Steven Rostedt4eebcc82008-05-12 21:20:48 +020089/*
90 * ftrace_disabled is set when an anomaly is discovered.
91 * ftrace_disabled is much stronger than ftrace_enabled.
92 */
93static int ftrace_disabled __read_mostly;
94
Steven Rostedt52baf112009-02-14 01:15:39 -050095static DEFINE_MUTEX(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +020096
Steven Rostedtb8489142011-05-04 09:27:52 -040097static struct ftrace_ops *ftrace_global_list __read_mostly = &ftrace_list_end;
Jiri Olsae2484912012-02-15 15:51:48 +010098static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -040099static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200100ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500101ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
Steven Rostedt2b499382011-05-03 22:49:52 -0400102static struct ftrace_ops global_ops;
Jiri Olsae2484912012-02-15 15:51:48 +0100103static struct ftrace_ops control_ops;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200104
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400105#if ARCH_SUPPORTS_FTRACE_OPS
106static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400107 struct ftrace_ops *op, struct pt_regs *regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400108#else
109/* See comment below, where ftrace_ops_list_func is defined */
110static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
111#define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
112#endif
Steven Rostedtb8489142011-05-04 09:27:52 -0400113
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800114/*
Steven Rostedtb8489142011-05-04 09:27:52 -0400115 * Traverse the ftrace_global_list, invoking all entries. The reason that we
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800116 * can use rcu_dereference_raw() is that elements removed from this list
117 * are simply leaked, so there is no need to interact with a grace-period
118 * mechanism. The rcu_dereference_raw() calls are needed to handle
Steven Rostedtb8489142011-05-04 09:27:52 -0400119 * concurrent insertions into the ftrace_global_list.
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800120 *
121 * Silly Alpha and silly pointer-speculation compiler optimizations!
122 */
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400123static void
124ftrace_global_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400125 struct ftrace_ops *op, struct pt_regs *regs)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200126{
Steven Rostedtb1cff0a2011-05-25 14:27:43 -0400127 if (unlikely(trace_recursion_test(TRACE_GLOBAL_BIT)))
128 return;
129
130 trace_recursion_set(TRACE_GLOBAL_BIT);
131 op = rcu_dereference_raw(ftrace_global_list); /*see above*/
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200132 while (op != &ftrace_list_end) {
Steven Rostedta1e2e312011-08-09 12:50:46 -0400133 op->func(ip, parent_ip, op, regs);
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800134 op = rcu_dereference_raw(op->next); /*see above*/
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200135 };
Steven Rostedtb1cff0a2011-05-25 14:27:43 -0400136 trace_recursion_clear(TRACE_GLOBAL_BIT);
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200137}
138
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400139static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400140 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500141{
Steven Rostedt0ef8cde2008-12-03 15:36:58 -0500142 if (!test_tsk_trace_trace(current))
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500143 return;
144
Steven Rostedta1e2e312011-08-09 12:50:46 -0400145 ftrace_pid_function(ip, parent_ip, op, regs);
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500146}
147
148static void set_ftrace_pid_function(ftrace_func_t func)
149{
150 /* do not set ftrace_pid_function to itself! */
151 if (func != ftrace_pid_func)
152 ftrace_pid_function = func;
153}
154
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200155/**
Steven Rostedt3d083392008-05-12 21:20:42 +0200156 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200157 *
Steven Rostedt3d083392008-05-12 21:20:42 +0200158 * This NULLs the ftrace function and in essence stops
159 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200160 */
Steven Rostedt3d083392008-05-12 21:20:42 +0200161void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200162{
Steven Rostedt3d083392008-05-12 21:20:42 +0200163 ftrace_trace_function = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500164 ftrace_pid_function = ftrace_stub;
Steven Rostedt3d083392008-05-12 21:20:42 +0200165}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200166
Jiri Olsae2484912012-02-15 15:51:48 +0100167static void control_ops_disable_all(struct ftrace_ops *ops)
168{
169 int cpu;
170
171 for_each_possible_cpu(cpu)
172 *per_cpu_ptr(ops->disabled, cpu) = 1;
173}
174
175static int control_ops_alloc(struct ftrace_ops *ops)
176{
177 int __percpu *disabled;
178
179 disabled = alloc_percpu(int);
180 if (!disabled)
181 return -ENOMEM;
182
183 ops->disabled = disabled;
184 control_ops_disable_all(ops);
185 return 0;
186}
187
188static void control_ops_free(struct ftrace_ops *ops)
189{
190 free_percpu(ops->disabled);
191}
192
Steven Rostedt2b499382011-05-03 22:49:52 -0400193static void update_global_ops(void)
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400194{
195 ftrace_func_t func;
196
197 /*
198 * If there's only one function registered, then call that
199 * function directly. Otherwise, we need to iterate over the
200 * registered callers.
201 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400202 if (ftrace_global_list == &ftrace_list_end ||
203 ftrace_global_list->next == &ftrace_list_end)
204 func = ftrace_global_list->func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400205 else
Steven Rostedtb8489142011-05-04 09:27:52 -0400206 func = ftrace_global_list_func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400207
208 /* If we filter on pids, update to use the pid function */
209 if (!list_empty(&ftrace_pids)) {
210 set_ftrace_pid_function(func);
211 func = ftrace_pid_func;
212 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400213
214 global_ops.func = func;
215}
216
217static void update_ftrace_function(void)
218{
219 ftrace_func_t func;
220
221 update_global_ops();
222
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400223 /*
224 * If we are at the end of the list and this ops is
Steven Rostedt47409742012-07-20 11:04:44 -0400225 * recursion safe and not dynamic and the arch supports passing ops,
226 * then have the mcount trampoline call the function directly.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400227 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400228 if (ftrace_ops_list == &ftrace_list_end ||
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400229 (ftrace_ops_list->next == &ftrace_list_end &&
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400230 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC) &&
Steven Rostedt47409742012-07-20 11:04:44 -0400231 (ftrace_ops_list->flags & FTRACE_OPS_FL_RECURSION_SAFE) &&
Steven Rostedtccf36722012-06-05 09:44:25 -0400232 !FTRACE_FORCE_LIST_FUNC)) {
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400233 /* Set the ftrace_ops that the arch callback uses */
234 if (ftrace_ops_list == &global_ops)
235 function_trace_op = ftrace_global_list;
236 else
237 function_trace_op = ftrace_ops_list;
Steven Rostedtb8489142011-05-04 09:27:52 -0400238 func = ftrace_ops_list->func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400239 } else {
240 /* Just use the default ftrace_ops */
241 function_trace_op = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400242 func = ftrace_ops_list_func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400243 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400244
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400245 ftrace_trace_function = func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400246}
247
Steven Rostedt2b499382011-05-03 22:49:52 -0400248static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200249{
Steven Rostedt2b499382011-05-03 22:49:52 -0400250 ops->next = *list;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200251 /*
Steven Rostedtb8489142011-05-04 09:27:52 -0400252 * We are entering ops into the list but another
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200253 * CPU might be walking that list. We need to make sure
254 * the ops->next pointer is valid before another CPU sees
Steven Rostedtb8489142011-05-04 09:27:52 -0400255 * the ops pointer included into the list.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200256 */
Steven Rostedt2b499382011-05-03 22:49:52 -0400257 rcu_assign_pointer(*list, ops);
258}
Steven Rostedt3d083392008-05-12 21:20:42 +0200259
Steven Rostedt2b499382011-05-03 22:49:52 -0400260static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
261{
262 struct ftrace_ops **p;
263
264 /*
265 * If we are removing the last function, then simply point
266 * to the ftrace_stub.
267 */
268 if (*list == ops && ops->next == &ftrace_list_end) {
269 *list = &ftrace_list_end;
270 return 0;
271 }
272
273 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
274 if (*p == ops)
275 break;
276
277 if (*p != ops)
278 return -1;
279
280 *p = (*p)->next;
281 return 0;
282}
283
Jiri Olsae2484912012-02-15 15:51:48 +0100284static void add_ftrace_list_ops(struct ftrace_ops **list,
285 struct ftrace_ops *main_ops,
286 struct ftrace_ops *ops)
287{
288 int first = *list == &ftrace_list_end;
289 add_ftrace_ops(list, ops);
290 if (first)
291 add_ftrace_ops(&ftrace_ops_list, main_ops);
292}
293
294static int remove_ftrace_list_ops(struct ftrace_ops **list,
295 struct ftrace_ops *main_ops,
296 struct ftrace_ops *ops)
297{
298 int ret = remove_ftrace_ops(list, ops);
299 if (!ret && *list == &ftrace_list_end)
300 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
301 return ret;
302}
303
Steven Rostedt2b499382011-05-03 22:49:52 -0400304static int __register_ftrace_function(struct ftrace_ops *ops)
305{
Borislav Petkov8d240dd2012-03-29 19:11:40 +0200306 if (unlikely(ftrace_disabled))
Steven Rostedt2b499382011-05-03 22:49:52 -0400307 return -ENODEV;
308
309 if (FTRACE_WARN_ON(ops == &global_ops))
310 return -EINVAL;
311
Steven Rostedtb8489142011-05-04 09:27:52 -0400312 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
313 return -EBUSY;
314
Jiri Olsae2484912012-02-15 15:51:48 +0100315 /* We don't support both control and global flags set. */
316 if ((ops->flags & FL_GLOBAL_CONTROL_MASK) == FL_GLOBAL_CONTROL_MASK)
317 return -EINVAL;
318
Steven Rostedt08f6fba2012-04-30 16:20:23 -0400319#ifndef ARCH_SUPPORTS_FTRACE_SAVE_REGS
320 /*
321 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
322 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
323 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
324 */
325 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
326 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
327 return -EINVAL;
328
329 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
330 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
331#endif
332
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400333 if (!core_kernel_data((unsigned long)ops))
334 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
335
Steven Rostedtb8489142011-05-04 09:27:52 -0400336 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100337 add_ftrace_list_ops(&ftrace_global_list, &global_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400338 ops->flags |= FTRACE_OPS_FL_ENABLED;
Jiri Olsae2484912012-02-15 15:51:48 +0100339 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
340 if (control_ops_alloc(ops))
341 return -ENOMEM;
342 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400343 } else
344 add_ftrace_ops(&ftrace_ops_list, ops);
345
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400346 if (ftrace_enabled)
347 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200348
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200349 return 0;
350}
351
Ingo Molnare309b412008-05-12 21:20:51 +0200352static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200353{
Steven Rostedt2b499382011-05-03 22:49:52 -0400354 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200355
Steven Rostedt2b499382011-05-03 22:49:52 -0400356 if (ftrace_disabled)
357 return -ENODEV;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200358
Steven Rostedtb8489142011-05-04 09:27:52 -0400359 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
360 return -EBUSY;
361
Steven Rostedt2b499382011-05-03 22:49:52 -0400362 if (FTRACE_WARN_ON(ops == &global_ops))
363 return -EINVAL;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200364
Steven Rostedtb8489142011-05-04 09:27:52 -0400365 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100366 ret = remove_ftrace_list_ops(&ftrace_global_list,
367 &global_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400368 if (!ret)
369 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Jiri Olsae2484912012-02-15 15:51:48 +0100370 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
371 ret = remove_ftrace_list_ops(&ftrace_control_list,
372 &control_ops, ops);
373 if (!ret) {
374 /*
375 * The ftrace_ops is now removed from the list,
376 * so there'll be no new users. We must ensure
377 * all current users are done before we free
378 * the control data.
379 */
380 synchronize_sched();
381 control_ops_free(ops);
382 }
Steven Rostedtb8489142011-05-04 09:27:52 -0400383 } else
384 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
385
Steven Rostedt2b499382011-05-03 22:49:52 -0400386 if (ret < 0)
387 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400388
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400389 if (ftrace_enabled)
390 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200391
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400392 /*
393 * Dynamic ops may be freed, we must make sure that all
394 * callers are done before leaving this function.
395 */
396 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
397 synchronize_sched();
398
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500399 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200400}
401
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500402static void ftrace_update_pid_func(void)
403{
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400404 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500405 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900406 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500407
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400408 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500409}
410
Steven Rostedt493762f2009-03-23 17:12:36 -0400411#ifdef CONFIG_FUNCTION_PROFILER
412struct ftrace_profile {
413 struct hlist_node node;
414 unsigned long ip;
415 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400416#ifdef CONFIG_FUNCTION_GRAPH_TRACER
417 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400418 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400419#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400420};
421
422struct ftrace_profile_page {
423 struct ftrace_profile_page *next;
424 unsigned long index;
425 struct ftrace_profile records[];
426};
427
Steven Rostedtcafb1682009-03-24 20:50:39 -0400428struct ftrace_profile_stat {
429 atomic_t disabled;
430 struct hlist_head *hash;
431 struct ftrace_profile_page *pages;
432 struct ftrace_profile_page *start;
433 struct tracer_stat stat;
434};
435
Steven Rostedt493762f2009-03-23 17:12:36 -0400436#define PROFILE_RECORDS_SIZE \
437 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
438
439#define PROFILES_PER_PAGE \
440 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
441
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400442static int ftrace_profile_bits __read_mostly;
443static int ftrace_profile_enabled __read_mostly;
444
445/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400446static DEFINE_MUTEX(ftrace_profile_lock);
447
Steven Rostedtcafb1682009-03-24 20:50:39 -0400448static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400449
450#define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
451
Steven Rostedt493762f2009-03-23 17:12:36 -0400452static void *
453function_stat_next(void *v, int idx)
454{
455 struct ftrace_profile *rec = v;
456 struct ftrace_profile_page *pg;
457
458 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
459
460 again:
Li Zefan0296e422009-06-26 11:15:37 +0800461 if (idx != 0)
462 rec++;
463
Steven Rostedt493762f2009-03-23 17:12:36 -0400464 if ((void *)rec >= (void *)&pg->records[pg->index]) {
465 pg = pg->next;
466 if (!pg)
467 return NULL;
468 rec = &pg->records[0];
469 if (!rec->counter)
470 goto again;
471 }
472
473 return rec;
474}
475
476static void *function_stat_start(struct tracer_stat *trace)
477{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400478 struct ftrace_profile_stat *stat =
479 container_of(trace, struct ftrace_profile_stat, stat);
480
481 if (!stat || !stat->start)
482 return NULL;
483
484 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400485}
486
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400487#ifdef CONFIG_FUNCTION_GRAPH_TRACER
488/* function graph compares on total time */
489static int function_stat_cmp(void *p1, void *p2)
490{
491 struct ftrace_profile *a = p1;
492 struct ftrace_profile *b = p2;
493
494 if (a->time < b->time)
495 return -1;
496 if (a->time > b->time)
497 return 1;
498 else
499 return 0;
500}
501#else
502/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400503static int function_stat_cmp(void *p1, void *p2)
504{
505 struct ftrace_profile *a = p1;
506 struct ftrace_profile *b = p2;
507
508 if (a->counter < b->counter)
509 return -1;
510 if (a->counter > b->counter)
511 return 1;
512 else
513 return 0;
514}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400515#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400516
517static int function_stat_headers(struct seq_file *m)
518{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400519#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400520 seq_printf(m, " Function "
Chase Douglase330b3b2010-04-26 14:02:05 -0400521 "Hit Time Avg s^2\n"
Steven Rostedt34886c82009-03-25 21:00:47 -0400522 " -------- "
Chase Douglase330b3b2010-04-26 14:02:05 -0400523 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400524#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400525 seq_printf(m, " Function Hit\n"
526 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400527#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400528 return 0;
529}
530
531static int function_stat_show(struct seq_file *m, void *v)
532{
533 struct ftrace_profile *rec = v;
534 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800535 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400536#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400537 static struct trace_seq s;
538 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400539 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400540#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800541 mutex_lock(&ftrace_profile_lock);
542
543 /* we raced with function_profile_reset() */
544 if (unlikely(rec->counter == 0)) {
545 ret = -EBUSY;
546 goto out;
547 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400548
549 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400550 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400551
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400552#ifdef CONFIG_FUNCTION_GRAPH_TRACER
553 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400554 avg = rec->time;
555 do_div(avg, rec->counter);
556
Chase Douglase330b3b2010-04-26 14:02:05 -0400557 /* Sample standard deviation (s^2) */
558 if (rec->counter <= 1)
559 stddev = 0;
560 else {
561 stddev = rec->time_squared - rec->counter * avg * avg;
562 /*
563 * Divide only 1000 for ns^2 -> us^2 conversion.
564 * trace_print_graph_duration will divide 1000 again.
565 */
566 do_div(stddev, (rec->counter - 1) * 1000);
567 }
568
Steven Rostedt34886c82009-03-25 21:00:47 -0400569 trace_seq_init(&s);
570 trace_print_graph_duration(rec->time, &s);
571 trace_seq_puts(&s, " ");
572 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400573 trace_seq_puts(&s, " ");
574 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400575 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400576#endif
577 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800578out:
579 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400580
Li Zefan3aaba202010-08-23 16:50:12 +0800581 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400582}
583
Steven Rostedtcafb1682009-03-24 20:50:39 -0400584static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400585{
586 struct ftrace_profile_page *pg;
587
Steven Rostedtcafb1682009-03-24 20:50:39 -0400588 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400589
590 while (pg) {
591 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
592 pg->index = 0;
593 pg = pg->next;
594 }
595
Steven Rostedtcafb1682009-03-24 20:50:39 -0400596 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400597 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
598}
599
Steven Rostedtcafb1682009-03-24 20:50:39 -0400600int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400601{
602 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400603 int functions;
604 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400605 int i;
606
607 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400608 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400609 return 0;
610
Steven Rostedtcafb1682009-03-24 20:50:39 -0400611 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
612 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400613 return -ENOMEM;
614
Steven Rostedt318e0a72009-03-25 20:06:34 -0400615#ifdef CONFIG_DYNAMIC_FTRACE
616 functions = ftrace_update_tot_cnt;
617#else
618 /*
619 * We do not know the number of functions that exist because
620 * dynamic tracing is what counts them. With past experience
621 * we have around 20K functions. That should be more than enough.
622 * It is highly unlikely we will execute every function in
623 * the kernel.
624 */
625 functions = 20000;
626#endif
627
Steven Rostedtcafb1682009-03-24 20:50:39 -0400628 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400629
Steven Rostedt318e0a72009-03-25 20:06:34 -0400630 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
631
632 for (i = 0; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400633 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400634 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400635 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400636 pg = pg->next;
637 }
638
639 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400640
641 out_free:
642 pg = stat->start;
643 while (pg) {
644 unsigned long tmp = (unsigned long)pg;
645
646 pg = pg->next;
647 free_page(tmp);
648 }
649
650 free_page((unsigned long)stat->pages);
651 stat->pages = NULL;
652 stat->start = NULL;
653
654 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400655}
656
Steven Rostedtcafb1682009-03-24 20:50:39 -0400657static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400658{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400659 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400660 int size;
661
Steven Rostedtcafb1682009-03-24 20:50:39 -0400662 stat = &per_cpu(ftrace_profile_stats, cpu);
663
664 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400665 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400666 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400667 return 0;
668 }
669
670 /*
671 * We are profiling all functions, but usually only a few thousand
672 * functions are hit. We'll make a hash of 1024 items.
673 */
674 size = FTRACE_PROFILE_HASH_SIZE;
675
Steven Rostedtcafb1682009-03-24 20:50:39 -0400676 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400677
Steven Rostedtcafb1682009-03-24 20:50:39 -0400678 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400679 return -ENOMEM;
680
Steven Rostedtcafb1682009-03-24 20:50:39 -0400681 if (!ftrace_profile_bits) {
682 size--;
Steven Rostedt493762f2009-03-23 17:12:36 -0400683
Steven Rostedtcafb1682009-03-24 20:50:39 -0400684 for (; size; size >>= 1)
685 ftrace_profile_bits++;
686 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400687
Steven Rostedt318e0a72009-03-25 20:06:34 -0400688 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400689 if (ftrace_profile_pages_init(stat) < 0) {
690 kfree(stat->hash);
691 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400692 return -ENOMEM;
693 }
694
695 return 0;
696}
697
Steven Rostedtcafb1682009-03-24 20:50:39 -0400698static int ftrace_profile_init(void)
699{
700 int cpu;
701 int ret = 0;
702
703 for_each_online_cpu(cpu) {
704 ret = ftrace_profile_init_cpu(cpu);
705 if (ret)
706 break;
707 }
708
709 return ret;
710}
711
Steven Rostedt493762f2009-03-23 17:12:36 -0400712/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400713static struct ftrace_profile *
714ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400715{
716 struct ftrace_profile *rec;
717 struct hlist_head *hhd;
718 struct hlist_node *n;
719 unsigned long key;
720
721 key = hash_long(ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400722 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400723
724 if (hlist_empty(hhd))
725 return NULL;
726
727 hlist_for_each_entry_rcu(rec, n, hhd, node) {
728 if (rec->ip == ip)
729 return rec;
730 }
731
732 return NULL;
733}
734
Steven Rostedtcafb1682009-03-24 20:50:39 -0400735static void ftrace_add_profile(struct ftrace_profile_stat *stat,
736 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400737{
738 unsigned long key;
739
740 key = hash_long(rec->ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400741 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400742}
743
Steven Rostedt318e0a72009-03-25 20:06:34 -0400744/*
745 * The memory is already allocated, this simply finds a new record to use.
746 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400747static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400748ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400749{
750 struct ftrace_profile *rec = NULL;
751
Steven Rostedt318e0a72009-03-25 20:06:34 -0400752 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400753 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400754 goto out;
755
Steven Rostedt493762f2009-03-23 17:12:36 -0400756 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400757 * Try to find the function again since an NMI
758 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400759 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400760 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400761 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400762 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400763
Steven Rostedtcafb1682009-03-24 20:50:39 -0400764 if (stat->pages->index == PROFILES_PER_PAGE) {
765 if (!stat->pages->next)
766 goto out;
767 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400768 }
769
Steven Rostedtcafb1682009-03-24 20:50:39 -0400770 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400771 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400772 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400773
Steven Rostedt493762f2009-03-23 17:12:36 -0400774 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400775 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400776
777 return rec;
778}
779
Steven Rostedt493762f2009-03-23 17:12:36 -0400780static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400781function_profile_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400782 struct ftrace_ops *ops, struct pt_regs *regs)
Steven Rostedt493762f2009-03-23 17:12:36 -0400783{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400784 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400785 struct ftrace_profile *rec;
786 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400787
788 if (!ftrace_profile_enabled)
789 return;
790
Steven Rostedt493762f2009-03-23 17:12:36 -0400791 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400792
793 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400794 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400795 goto out;
796
797 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400798 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400799 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400800 if (!rec)
801 goto out;
802 }
803
804 rec->counter++;
805 out:
806 local_irq_restore(flags);
807}
808
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400809#ifdef CONFIG_FUNCTION_GRAPH_TRACER
810static int profile_graph_entry(struct ftrace_graph_ent *trace)
811{
Steven Rostedta1e2e312011-08-09 12:50:46 -0400812 function_profile_call(trace->func, 0, NULL, NULL);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400813 return 1;
814}
815
816static void profile_graph_return(struct ftrace_graph_ret *trace)
817{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400818 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400819 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400820 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400821 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400822
823 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400824 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400825 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400826 goto out;
827
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400828 /* If the calltime was zero'd ignore it */
829 if (!trace->calltime)
830 goto out;
831
Steven Rostedta2a16d62009-03-24 23:17:58 -0400832 calltime = trace->rettime - trace->calltime;
833
834 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
835 int index;
836
837 index = trace->depth;
838
839 /* Append this call time to the parent time to subtract */
840 if (index)
841 current->ret_stack[index - 1].subtime += calltime;
842
843 if (current->ret_stack[index].subtime < calltime)
844 calltime -= current->ret_stack[index].subtime;
845 else
846 calltime = 0;
847 }
848
Steven Rostedtcafb1682009-03-24 20:50:39 -0400849 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400850 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400851 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400852 rec->time_squared += calltime * calltime;
853 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400854
Steven Rostedtcafb1682009-03-24 20:50:39 -0400855 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400856 local_irq_restore(flags);
857}
858
859static int register_ftrace_profiler(void)
860{
861 return register_ftrace_graph(&profile_graph_return,
862 &profile_graph_entry);
863}
864
865static void unregister_ftrace_profiler(void)
866{
867 unregister_ftrace_graph();
868}
869#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100870static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400871 .func = function_profile_call,
Steven Rostedt47409742012-07-20 11:04:44 -0400872 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Steven Rostedt493762f2009-03-23 17:12:36 -0400873};
874
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400875static int register_ftrace_profiler(void)
876{
877 return register_ftrace_function(&ftrace_profile_ops);
878}
879
880static void unregister_ftrace_profiler(void)
881{
882 unregister_ftrace_function(&ftrace_profile_ops);
883}
884#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
885
Steven Rostedt493762f2009-03-23 17:12:36 -0400886static ssize_t
887ftrace_profile_write(struct file *filp, const char __user *ubuf,
888 size_t cnt, loff_t *ppos)
889{
890 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400891 int ret;
892
Peter Huewe22fe9b52011-06-07 21:58:27 +0200893 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
894 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400895 return ret;
896
897 val = !!val;
898
899 mutex_lock(&ftrace_profile_lock);
900 if (ftrace_profile_enabled ^ val) {
901 if (val) {
902 ret = ftrace_profile_init();
903 if (ret < 0) {
904 cnt = ret;
905 goto out;
906 }
907
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400908 ret = register_ftrace_profiler();
909 if (ret < 0) {
910 cnt = ret;
911 goto out;
912 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400913 ftrace_profile_enabled = 1;
914 } else {
915 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400916 /*
917 * unregister_ftrace_profiler calls stop_machine
918 * so this acts like an synchronize_sched.
919 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400920 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400921 }
922 }
923 out:
924 mutex_unlock(&ftrace_profile_lock);
925
Jiri Olsacf8517c2009-10-23 19:36:16 -0400926 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400927
928 return cnt;
929}
930
931static ssize_t
932ftrace_profile_read(struct file *filp, char __user *ubuf,
933 size_t cnt, loff_t *ppos)
934{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400935 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400936 int r;
937
938 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
939 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
940}
941
942static const struct file_operations ftrace_profile_fops = {
943 .open = tracing_open_generic,
944 .read = ftrace_profile_read,
945 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200946 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -0400947};
948
Steven Rostedtcafb1682009-03-24 20:50:39 -0400949/* used to initialize the real stat files */
950static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400951 .name = "functions",
952 .stat_start = function_stat_start,
953 .stat_next = function_stat_next,
954 .stat_cmp = function_stat_cmp,
955 .stat_headers = function_stat_headers,
956 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -0400957};
958
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400959static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400960{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400961 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400962 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400963 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -0400964 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400965 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -0400966
Steven Rostedtcafb1682009-03-24 20:50:39 -0400967 for_each_possible_cpu(cpu) {
968 stat = &per_cpu(ftrace_profile_stats, cpu);
969
970 /* allocate enough for function name + cpu number */
971 name = kmalloc(32, GFP_KERNEL);
972 if (!name) {
973 /*
974 * The files created are permanent, if something happens
975 * we still do not free memory.
976 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400977 WARN(1,
978 "Could not allocate stat file for cpu %d\n",
979 cpu);
980 return;
981 }
982 stat->stat = function_stats;
983 snprintf(name, 32, "function%d", cpu);
984 stat->stat.name = name;
985 ret = register_stat_tracer(&stat->stat);
986 if (ret) {
987 WARN(1,
988 "Could not register function stat for cpu %d\n",
989 cpu);
990 kfree(name);
991 return;
992 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400993 }
994
995 entry = debugfs_create_file("function_profile_enabled", 0644,
996 d_tracer, NULL, &ftrace_profile_fops);
997 if (!entry)
998 pr_warning("Could not create debugfs "
999 "'function_profile_enabled' entry\n");
1000}
1001
1002#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001003static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001004{
1005}
1006#endif /* CONFIG_FUNCTION_PROFILER */
1007
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001008static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1009
Steven Rostedt3d083392008-05-12 21:20:42 +02001010#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001011
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001012#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001013# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001014#endif
1015
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001016static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1017
Steven Rostedtb6887d72009-02-17 12:32:04 -05001018struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001019 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -05001020 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001021 unsigned long flags;
1022 unsigned long ip;
1023 void *data;
1024 struct rcu_head rcu;
1025};
1026
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001027struct ftrace_func_entry {
1028 struct hlist_node hlist;
1029 unsigned long ip;
1030};
1031
1032struct ftrace_hash {
1033 unsigned long size_bits;
1034 struct hlist_head *buckets;
1035 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001036 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001037};
1038
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001039/*
1040 * We make these constant because no one should touch them,
1041 * but they are used as the default "empty hash", to avoid allocating
1042 * it all the time. These are in a read only section such that if
1043 * anyone does try to modify it, it will cause an exception.
1044 */
1045static const struct hlist_head empty_buckets[1];
1046static const struct ftrace_hash empty_hash = {
1047 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001048};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001049#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001050
Steven Rostedt2b499382011-05-03 22:49:52 -04001051static struct ftrace_ops global_ops = {
Steven Rostedtf45948e2011-05-02 12:29:25 -04001052 .func = ftrace_stub,
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001053 .notrace_hash = EMPTY_HASH,
1054 .filter_hash = EMPTY_HASH,
Steven Rostedt47409742012-07-20 11:04:44 -04001055 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Steven Rostedtf45948e2011-05-02 12:29:25 -04001056};
1057
Steven Rostedt41c52c02008-05-22 11:46:33 -04001058static DEFINE_MUTEX(ftrace_regex_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02001059
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001060struct ftrace_page {
1061 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001062 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001063 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001064 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001065};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001066
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001067static struct ftrace_page *ftrace_new_pgs;
1068
Steven Rostedta7900872011-12-16 16:23:44 -05001069#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1070#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001071
1072/* estimate from running different kernels */
1073#define NR_TO_INIT 10000
1074
1075static struct ftrace_page *ftrace_pages_start;
1076static struct ftrace_page *ftrace_pages;
1077
Steven Rostedt06a51d92011-12-19 19:07:36 -05001078static bool ftrace_hash_empty(struct ftrace_hash *hash)
1079{
1080 return !hash || !hash->count;
1081}
1082
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001083static struct ftrace_func_entry *
1084ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1085{
1086 unsigned long key;
1087 struct ftrace_func_entry *entry;
1088 struct hlist_head *hhd;
1089 struct hlist_node *n;
1090
Steven Rostedt06a51d92011-12-19 19:07:36 -05001091 if (ftrace_hash_empty(hash))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001092 return NULL;
1093
1094 if (hash->size_bits > 0)
1095 key = hash_long(ip, hash->size_bits);
1096 else
1097 key = 0;
1098
1099 hhd = &hash->buckets[key];
1100
1101 hlist_for_each_entry_rcu(entry, n, hhd, hlist) {
1102 if (entry->ip == ip)
1103 return entry;
1104 }
1105 return NULL;
1106}
1107
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001108static void __add_hash_entry(struct ftrace_hash *hash,
1109 struct ftrace_func_entry *entry)
1110{
1111 struct hlist_head *hhd;
1112 unsigned long key;
1113
1114 if (hash->size_bits)
1115 key = hash_long(entry->ip, hash->size_bits);
1116 else
1117 key = 0;
1118
1119 hhd = &hash->buckets[key];
1120 hlist_add_head(&entry->hlist, hhd);
1121 hash->count++;
1122}
1123
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001124static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1125{
1126 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001127
1128 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1129 if (!entry)
1130 return -ENOMEM;
1131
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001132 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001133 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001134
1135 return 0;
1136}
1137
1138static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001139free_hash_entry(struct ftrace_hash *hash,
1140 struct ftrace_func_entry *entry)
1141{
1142 hlist_del(&entry->hlist);
1143 kfree(entry);
1144 hash->count--;
1145}
1146
1147static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001148remove_hash_entry(struct ftrace_hash *hash,
1149 struct ftrace_func_entry *entry)
1150{
1151 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001152 hash->count--;
1153}
1154
1155static void ftrace_hash_clear(struct ftrace_hash *hash)
1156{
1157 struct hlist_head *hhd;
1158 struct hlist_node *tp, *tn;
1159 struct ftrace_func_entry *entry;
1160 int size = 1 << hash->size_bits;
1161 int i;
1162
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001163 if (!hash->count)
1164 return;
1165
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001166 for (i = 0; i < size; i++) {
1167 hhd = &hash->buckets[i];
1168 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001169 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001170 }
1171 FTRACE_WARN_ON(hash->count);
1172}
1173
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001174static void free_ftrace_hash(struct ftrace_hash *hash)
1175{
1176 if (!hash || hash == EMPTY_HASH)
1177 return;
1178 ftrace_hash_clear(hash);
1179 kfree(hash->buckets);
1180 kfree(hash);
1181}
1182
Steven Rostedt07fd5512011-05-05 18:03:47 -04001183static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1184{
1185 struct ftrace_hash *hash;
1186
1187 hash = container_of(rcu, struct ftrace_hash, rcu);
1188 free_ftrace_hash(hash);
1189}
1190
1191static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1192{
1193 if (!hash || hash == EMPTY_HASH)
1194 return;
1195 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1196}
1197
Jiri Olsa5500fa52012-02-15 15:51:54 +01001198void ftrace_free_filter(struct ftrace_ops *ops)
1199{
1200 free_ftrace_hash(ops->filter_hash);
1201 free_ftrace_hash(ops->notrace_hash);
1202}
1203
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001204static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1205{
1206 struct ftrace_hash *hash;
1207 int size;
1208
1209 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1210 if (!hash)
1211 return NULL;
1212
1213 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001214 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001215
1216 if (!hash->buckets) {
1217 kfree(hash);
1218 return NULL;
1219 }
1220
1221 hash->size_bits = size_bits;
1222
1223 return hash;
1224}
1225
1226static struct ftrace_hash *
1227alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1228{
1229 struct ftrace_func_entry *entry;
1230 struct ftrace_hash *new_hash;
1231 struct hlist_node *tp;
1232 int size;
1233 int ret;
1234 int i;
1235
1236 new_hash = alloc_ftrace_hash(size_bits);
1237 if (!new_hash)
1238 return NULL;
1239
1240 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001241 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001242 return new_hash;
1243
1244 size = 1 << hash->size_bits;
1245 for (i = 0; i < size; i++) {
1246 hlist_for_each_entry(entry, tp, &hash->buckets[i], hlist) {
1247 ret = add_hash_entry(new_hash, entry->ip);
1248 if (ret < 0)
1249 goto free_hash;
1250 }
1251 }
1252
1253 FTRACE_WARN_ON(new_hash->count != hash->count);
1254
1255 return new_hash;
1256
1257 free_hash:
1258 free_ftrace_hash(new_hash);
1259 return NULL;
1260}
1261
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001262static void
1263ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1264static void
1265ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1266
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001267static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001268ftrace_hash_move(struct ftrace_ops *ops, int enable,
1269 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001270{
1271 struct ftrace_func_entry *entry;
1272 struct hlist_node *tp, *tn;
1273 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001274 struct ftrace_hash *old_hash;
1275 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001276 unsigned long key;
1277 int size = src->count;
1278 int bits = 0;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001279 int ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001280 int i;
1281
1282 /*
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001283 * Remove the current set, update the hash and add
1284 * them back.
1285 */
1286 ftrace_hash_rec_disable(ops, enable);
1287
1288 /*
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001289 * If the new source is empty, just free dst and assign it
1290 * the empty_hash.
1291 */
1292 if (!src->count) {
Steven Rostedt07fd5512011-05-05 18:03:47 -04001293 free_ftrace_hash_rcu(*dst);
1294 rcu_assign_pointer(*dst, EMPTY_HASH);
Steven Rostedtd4d34b982011-11-04 20:32:39 -04001295 /* still need to update the function records */
1296 ret = 0;
1297 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001298 }
1299
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001300 /*
1301 * Make the hash size about 1/2 the # found
1302 */
1303 for (size /= 2; size; size >>= 1)
1304 bits++;
1305
1306 /* Don't allocate too much */
1307 if (bits > FTRACE_HASH_MAX_BITS)
1308 bits = FTRACE_HASH_MAX_BITS;
1309
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001310 ret = -ENOMEM;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001311 new_hash = alloc_ftrace_hash(bits);
1312 if (!new_hash)
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001313 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001314
1315 size = 1 << src->size_bits;
1316 for (i = 0; i < size; i++) {
1317 hhd = &src->buckets[i];
1318 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist) {
1319 if (bits > 0)
1320 key = hash_long(entry->ip, bits);
1321 else
1322 key = 0;
1323 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001324 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001325 }
1326 }
1327
Steven Rostedt07fd5512011-05-05 18:03:47 -04001328 old_hash = *dst;
1329 rcu_assign_pointer(*dst, new_hash);
1330 free_ftrace_hash_rcu(old_hash);
1331
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001332 ret = 0;
1333 out:
1334 /*
1335 * Enable regardless of ret:
1336 * On success, we enable the new hash.
1337 * On failure, we re-enable the original hash.
1338 */
1339 ftrace_hash_rec_enable(ops, enable);
1340
1341 return ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001342}
1343
Steven Rostedt265c8312009-02-13 12:43:56 -05001344/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001345 * Test the hashes for this ops to see if we want to call
1346 * the ops->func or not.
1347 *
1348 * It's a match if the ip is in the ops->filter_hash or
1349 * the filter_hash does not exist or is empty,
1350 * AND
1351 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001352 *
1353 * This needs to be called with preemption disabled as
1354 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001355 */
1356static int
1357ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
1358{
1359 struct ftrace_hash *filter_hash;
1360 struct ftrace_hash *notrace_hash;
1361 int ret;
1362
Steven Rostedtb8489142011-05-04 09:27:52 -04001363 filter_hash = rcu_dereference_raw(ops->filter_hash);
1364 notrace_hash = rcu_dereference_raw(ops->notrace_hash);
1365
Steven Rostedt06a51d92011-12-19 19:07:36 -05001366 if ((ftrace_hash_empty(filter_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001367 ftrace_lookup_ip(filter_hash, ip)) &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001368 (ftrace_hash_empty(notrace_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001369 !ftrace_lookup_ip(notrace_hash, ip)))
1370 ret = 1;
1371 else
1372 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001373
1374 return ret;
1375}
1376
1377/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001378 * This is a double for. Do not use 'break' to break out of the loop,
1379 * you must use a goto.
1380 */
1381#define do_for_each_ftrace_rec(pg, rec) \
1382 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1383 int _____i; \
1384 for (_____i = 0; _____i < pg->index; _____i++) { \
1385 rec = &pg->records[_____i];
1386
1387#define while_for_each_ftrace_rec() \
1388 } \
1389 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301390
Steven Rostedt5855fea2011-12-16 19:27:42 -05001391
1392static int ftrace_cmp_recs(const void *a, const void *b)
1393{
Steven Rostedta650e022012-04-25 13:48:13 -04001394 const struct dyn_ftrace *key = a;
1395 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001396
Steven Rostedta650e022012-04-25 13:48:13 -04001397 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001398 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001399 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1400 return 1;
1401 return 0;
1402}
1403
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001404static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001405{
1406 struct ftrace_page *pg;
1407 struct dyn_ftrace *rec;
1408 struct dyn_ftrace key;
1409
1410 key.ip = start;
1411 key.flags = end; /* overload flags, as it is unsigned long */
1412
1413 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1414 if (end < pg->records[0].ip ||
1415 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1416 continue;
1417 rec = bsearch(&key, pg->records, pg->index,
1418 sizeof(struct dyn_ftrace),
1419 ftrace_cmp_recs);
1420 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001421 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001422 }
1423
Steven Rostedt5855fea2011-12-16 19:27:42 -05001424 return 0;
1425}
1426
Steven Rostedtc88fd862011-08-16 09:53:39 -04001427/**
1428 * ftrace_location - return true if the ip giving is a traced location
1429 * @ip: the instruction pointer to check
1430 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001431 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001432 * That is, the instruction that is either a NOP or call to
1433 * the function tracer. It checks the ftrace internal tables to
1434 * determine if the address belongs or not.
1435 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001436unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001437{
Steven Rostedta650e022012-04-25 13:48:13 -04001438 return ftrace_location_range(ip, ip);
1439}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001440
Steven Rostedta650e022012-04-25 13:48:13 -04001441/**
1442 * ftrace_text_reserved - return true if range contains an ftrace location
1443 * @start: start of range to search
1444 * @end: end of range to search (inclusive). @end points to the last byte to check.
1445 *
1446 * Returns 1 if @start and @end contains a ftrace location.
1447 * That is, the instruction that is either a NOP or call to
1448 * the function tracer. It checks the ftrace internal tables to
1449 * determine if the address belongs or not.
1450 */
1451int ftrace_text_reserved(void *start, void *end)
1452{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001453 unsigned long ret;
1454
1455 ret = ftrace_location_range((unsigned long)start,
1456 (unsigned long)end);
1457
1458 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001459}
1460
Steven Rostedted926f92011-05-03 13:25:24 -04001461static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1462 int filter_hash,
1463 bool inc)
1464{
1465 struct ftrace_hash *hash;
1466 struct ftrace_hash *other_hash;
1467 struct ftrace_page *pg;
1468 struct dyn_ftrace *rec;
1469 int count = 0;
1470 int all = 0;
1471
1472 /* Only update if the ops has been registered */
1473 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1474 return;
1475
1476 /*
1477 * In the filter_hash case:
1478 * If the count is zero, we update all records.
1479 * Otherwise we just update the items in the hash.
1480 *
1481 * In the notrace_hash case:
1482 * We enable the update in the hash.
1483 * As disabling notrace means enabling the tracing,
1484 * and enabling notrace means disabling, the inc variable
1485 * gets inversed.
1486 */
1487 if (filter_hash) {
1488 hash = ops->filter_hash;
1489 other_hash = ops->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001490 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001491 all = 1;
1492 } else {
1493 inc = !inc;
1494 hash = ops->notrace_hash;
1495 other_hash = ops->filter_hash;
1496 /*
1497 * If the notrace hash has no items,
1498 * then there's nothing to do.
1499 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001500 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001501 return;
1502 }
1503
1504 do_for_each_ftrace_rec(pg, rec) {
1505 int in_other_hash = 0;
1506 int in_hash = 0;
1507 int match = 0;
1508
1509 if (all) {
1510 /*
1511 * Only the filter_hash affects all records.
1512 * Update if the record is not in the notrace hash.
1513 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001514 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001515 match = 1;
1516 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001517 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1518 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001519
1520 /*
1521 *
1522 */
1523 if (filter_hash && in_hash && !in_other_hash)
1524 match = 1;
1525 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001526 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001527 match = 1;
1528 }
1529 if (!match)
1530 continue;
1531
1532 if (inc) {
1533 rec->flags++;
1534 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1535 return;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001536 /*
1537 * If any ops wants regs saved for this function
1538 * then all ops will get saved regs.
1539 */
1540 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1541 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001542 } else {
1543 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1544 return;
1545 rec->flags--;
1546 }
1547 count++;
1548 /* Shortcut, if we handled all records, we are done. */
1549 if (!all && count == hash->count)
1550 return;
1551 } while_for_each_ftrace_rec();
1552}
1553
1554static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1555 int filter_hash)
1556{
1557 __ftrace_hash_rec_update(ops, filter_hash, 0);
1558}
1559
1560static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1561 int filter_hash)
1562{
1563 __ftrace_hash_rec_update(ops, filter_hash, 1);
1564}
1565
Steven Rostedt05736a42008-09-22 14:55:47 -07001566static void print_ip_ins(const char *fmt, unsigned char *p)
1567{
1568 int i;
1569
1570 printk(KERN_CONT "%s", fmt);
1571
1572 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1573 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1574}
1575
Steven Rostedtc88fd862011-08-16 09:53:39 -04001576/**
1577 * ftrace_bug - report and shutdown function tracer
1578 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1579 * @ip: The address that failed
1580 *
1581 * The arch code that enables or disables the function tracing
1582 * can call ftrace_bug() when it has detected a problem in
1583 * modifying the code. @failed should be one of either:
1584 * EFAULT - if the problem happens on reading the @ip address
1585 * EINVAL - if what is read at @ip is not what was expected
1586 * EPERM - if the problem happens on writting to the @ip address
1587 */
1588void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001589{
1590 switch (failed) {
1591 case -EFAULT:
1592 FTRACE_WARN_ON_ONCE(1);
1593 pr_info("ftrace faulted on modifying ");
1594 print_ip_sym(ip);
1595 break;
1596 case -EINVAL:
1597 FTRACE_WARN_ON_ONCE(1);
1598 pr_info("ftrace failed to modify ");
1599 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001600 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001601 printk(KERN_CONT "\n");
1602 break;
1603 case -EPERM:
1604 FTRACE_WARN_ON_ONCE(1);
1605 pr_info("ftrace faulted on writing ");
1606 print_ip_sym(ip);
1607 break;
1608 default:
1609 FTRACE_WARN_ON_ONCE(1);
1610 pr_info("ftrace faulted on unknown error ");
1611 print_ip_sym(ip);
1612 }
1613}
1614
Steven Rostedtc88fd862011-08-16 09:53:39 -04001615static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02001616{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001617 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001618
Steven Rostedt982c3502008-11-15 16:31:41 -05001619 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001620 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05001621 *
Steven Rostedted926f92011-05-03 13:25:24 -04001622 * If the record has a ref count, then we need to enable it
1623 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001624 *
Steven Rostedted926f92011-05-03 13:25:24 -04001625 * Otherwise we make sure its disabled.
1626 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001627 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04001628 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001629 */
Steven Rostedtc88fd862011-08-16 09:53:39 -04001630 if (enable && (rec->flags & ~FTRACE_FL_MASK))
Steven Rostedted926f92011-05-03 13:25:24 -04001631 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001632
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001633 /*
1634 * If enabling and the REGS flag does not match the REGS_EN, then
1635 * do not ignore this record. Set flags to fail the compare against
1636 * ENABLED.
1637 */
1638 if (flag &&
1639 (!(rec->flags & FTRACE_FL_REGS) != !(rec->flags & FTRACE_FL_REGS_EN)))
1640 flag |= FTRACE_FL_REGS;
1641
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001642 /* If the state of this record hasn't changed, then do nothing */
1643 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001644 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001645
1646 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001647 /* Save off if rec is being enabled (for return value) */
1648 flag ^= rec->flags & FTRACE_FL_ENABLED;
1649
1650 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04001651 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001652 if (flag & FTRACE_FL_REGS) {
1653 if (rec->flags & FTRACE_FL_REGS)
1654 rec->flags |= FTRACE_FL_REGS_EN;
1655 else
1656 rec->flags &= ~FTRACE_FL_REGS_EN;
1657 }
1658 }
1659
1660 /*
1661 * If this record is being updated from a nop, then
1662 * return UPDATE_MAKE_CALL.
1663 * Otherwise, if the EN flag is set, then return
1664 * UPDATE_MODIFY_CALL_REGS to tell the caller to convert
1665 * from the non-save regs, to a save regs function.
1666 * Otherwise,
1667 * return UPDATE_MODIFY_CALL to tell the caller to convert
1668 * from the save regs, to a non-save regs function.
1669 */
1670 if (flag & FTRACE_FL_ENABLED)
1671 return FTRACE_UPDATE_MAKE_CALL;
1672 else if (rec->flags & FTRACE_FL_REGS_EN)
1673 return FTRACE_UPDATE_MODIFY_CALL_REGS;
1674 else
1675 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001676 }
1677
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001678 if (update) {
1679 /* If there's no more users, clear all flags */
1680 if (!(rec->flags & ~FTRACE_FL_MASK))
1681 rec->flags = 0;
1682 else
1683 /* Just disable the record (keep REGS state) */
1684 rec->flags &= ~FTRACE_FL_ENABLED;
1685 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04001686
1687 return FTRACE_UPDATE_MAKE_NOP;
1688}
1689
1690/**
1691 * ftrace_update_record, set a record that now is tracing or not
1692 * @rec: the record to update
1693 * @enable: set to 1 if the record is tracing, zero to force disable
1694 *
1695 * The records that represent all functions that can be traced need
1696 * to be updated when tracing has been enabled.
1697 */
1698int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1699{
1700 return ftrace_check_record(rec, enable, 1);
1701}
1702
1703/**
1704 * ftrace_test_record, check if the record has been enabled or not
1705 * @rec: the record to test
1706 * @enable: set to 1 to check if enabled, 0 if it is disabled
1707 *
1708 * The arch code may need to test if a record is already set to
1709 * tracing to determine how to modify the function code that it
1710 * represents.
1711 */
1712int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1713{
1714 return ftrace_check_record(rec, enable, 0);
1715}
1716
1717static int
1718__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1719{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001720 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001721 unsigned long ftrace_addr;
1722 int ret;
1723
Steven Rostedtc88fd862011-08-16 09:53:39 -04001724 ret = ftrace_update_record(rec, enable);
1725
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001726 if (rec->flags & FTRACE_FL_REGS)
1727 ftrace_addr = (unsigned long)FTRACE_REGS_ADDR;
1728 else
1729 ftrace_addr = (unsigned long)FTRACE_ADDR;
1730
Steven Rostedtc88fd862011-08-16 09:53:39 -04001731 switch (ret) {
1732 case FTRACE_UPDATE_IGNORE:
1733 return 0;
1734
1735 case FTRACE_UPDATE_MAKE_CALL:
1736 return ftrace_make_call(rec, ftrace_addr);
1737
1738 case FTRACE_UPDATE_MAKE_NOP:
1739 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001740
1741 case FTRACE_UPDATE_MODIFY_CALL_REGS:
1742 case FTRACE_UPDATE_MODIFY_CALL:
1743 if (rec->flags & FTRACE_FL_REGS)
1744 ftrace_old_addr = (unsigned long)FTRACE_ADDR;
1745 else
1746 ftrace_old_addr = (unsigned long)FTRACE_REGS_ADDR;
1747
1748 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04001749 }
1750
1751 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02001752}
1753
Steven Rostedte4f5d542012-04-27 09:13:18 -04001754void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001755{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001756 struct dyn_ftrace *rec;
1757 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001758 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001759
Steven Rostedt45a4a232011-04-21 23:16:46 -04001760 if (unlikely(ftrace_disabled))
1761 return;
1762
Steven Rostedt265c8312009-02-13 12:43:56 -05001763 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedte4f5d542012-04-27 09:13:18 -04001764 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001765 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04001766 ftrace_bug(failed, rec->ip);
1767 /* Stop processing */
1768 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001769 }
1770 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001771}
1772
Steven Rostedtc88fd862011-08-16 09:53:39 -04001773struct ftrace_rec_iter {
1774 struct ftrace_page *pg;
1775 int index;
1776};
1777
1778/**
1779 * ftrace_rec_iter_start, start up iterating over traced functions
1780 *
1781 * Returns an iterator handle that is used to iterate over all
1782 * the records that represent address locations where functions
1783 * are traced.
1784 *
1785 * May return NULL if no records are available.
1786 */
1787struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1788{
1789 /*
1790 * We only use a single iterator.
1791 * Protected by the ftrace_lock mutex.
1792 */
1793 static struct ftrace_rec_iter ftrace_rec_iter;
1794 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1795
1796 iter->pg = ftrace_pages_start;
1797 iter->index = 0;
1798
1799 /* Could have empty pages */
1800 while (iter->pg && !iter->pg->index)
1801 iter->pg = iter->pg->next;
1802
1803 if (!iter->pg)
1804 return NULL;
1805
1806 return iter;
1807}
1808
1809/**
1810 * ftrace_rec_iter_next, get the next record to process.
1811 * @iter: The handle to the iterator.
1812 *
1813 * Returns the next iterator after the given iterator @iter.
1814 */
1815struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1816{
1817 iter->index++;
1818
1819 if (iter->index >= iter->pg->index) {
1820 iter->pg = iter->pg->next;
1821 iter->index = 0;
1822
1823 /* Could have empty pages */
1824 while (iter->pg && !iter->pg->index)
1825 iter->pg = iter->pg->next;
1826 }
1827
1828 if (!iter->pg)
1829 return NULL;
1830
1831 return iter;
1832}
1833
1834/**
1835 * ftrace_rec_iter_record, get the record at the iterator location
1836 * @iter: The current iterator location
1837 *
1838 * Returns the record that the current @iter is at.
1839 */
1840struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1841{
1842 return &iter->pg->records[iter->index];
1843}
1844
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301845static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001846ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001847{
1848 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001849 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001850
1851 ip = rec->ip;
1852
Steven Rostedt45a4a232011-04-21 23:16:46 -04001853 if (unlikely(ftrace_disabled))
1854 return 0;
1855
Shaohua Li25aac9d2009-01-09 11:29:40 +08001856 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001857 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001858 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301859 return 0;
Steven Rostedt37ad50842008-05-12 21:20:48 +02001860 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301861 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001862}
1863
Steven Rostedt000ab692009-02-17 13:35:06 -05001864/*
1865 * archs can override this function if they must do something
1866 * before the modifying code is performed.
1867 */
1868int __weak ftrace_arch_code_modify_prepare(void)
1869{
1870 return 0;
1871}
1872
1873/*
1874 * archs can override this function if they must do something
1875 * after the modifying code is performed.
1876 */
1877int __weak ftrace_arch_code_modify_post_process(void)
1878{
1879 return 0;
1880}
1881
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001882void ftrace_modify_all_code(int command)
1883{
1884 if (command & FTRACE_UPDATE_CALLS)
1885 ftrace_replace_code(1);
1886 else if (command & FTRACE_DISABLE_CALLS)
1887 ftrace_replace_code(0);
1888
1889 if (command & FTRACE_UPDATE_TRACE_FUNC)
1890 ftrace_update_ftrace_func(ftrace_trace_function);
1891
1892 if (command & FTRACE_START_FUNC_RET)
1893 ftrace_enable_ftrace_graph_caller();
1894 else if (command & FTRACE_STOP_FUNC_RET)
1895 ftrace_disable_ftrace_graph_caller();
1896}
1897
Ingo Molnare309b412008-05-12 21:20:51 +02001898static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02001899{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001900 int *command = data;
1901
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001902 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001903
Steven Rostedtc88fd862011-08-16 09:53:39 -04001904 return 0;
1905}
1906
1907/**
1908 * ftrace_run_stop_machine, go back to the stop machine method
1909 * @command: The command to tell ftrace what to do
1910 *
1911 * If an arch needs to fall back to the stop machine method, the
1912 * it can call this function.
1913 */
1914void ftrace_run_stop_machine(int command)
1915{
1916 stop_machine(__ftrace_modify_code, &command, NULL);
1917}
1918
1919/**
1920 * arch_ftrace_update_code, modify the code to trace or not trace
1921 * @command: The command that needs to be done
1922 *
1923 * Archs can override this function if it does not need to
1924 * run stop_machine() to modify code.
1925 */
1926void __weak arch_ftrace_update_code(int command)
1927{
1928 ftrace_run_stop_machine(command);
1929}
1930
1931static void ftrace_run_update_code(int command)
1932{
1933 int ret;
1934
1935 ret = ftrace_arch_code_modify_prepare();
1936 FTRACE_WARN_ON(ret);
1937 if (ret)
1938 return;
1939 /*
1940 * Do not call function tracer while we update the code.
1941 * We are in stop machine.
1942 */
1943 function_trace_stop++;
1944
1945 /*
1946 * By default we use stop_machine() to modify the code.
1947 * But archs can do what ever they want as long as it
1948 * is safe. The stop_machine() is the safest, but also
1949 * produces the most overhead.
1950 */
1951 arch_ftrace_update_code(command);
1952
Steven Rostedt6331c282011-07-13 15:11:02 -04001953 function_trace_stop--;
1954
Steven Rostedt000ab692009-02-17 13:35:06 -05001955 ret = ftrace_arch_code_modify_post_process();
1956 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02001957}
1958
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001959static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001960static int ftrace_start_up;
Steven Rostedtb8489142011-05-04 09:27:52 -04001961static int global_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001962
1963static void ftrace_startup_enable(int command)
1964{
1965 if (saved_ftrace_func != ftrace_trace_function) {
1966 saved_ftrace_func = ftrace_trace_function;
1967 command |= FTRACE_UPDATE_TRACE_FUNC;
1968 }
1969
1970 if (!command || !ftrace_enabled)
1971 return;
1972
1973 ftrace_run_update_code(command);
1974}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001975
Steven Rostedta1cd6172011-05-23 15:24:25 -04001976static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001977{
Steven Rostedtb8489142011-05-04 09:27:52 -04001978 bool hash_enable = true;
1979
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001980 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04001981 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02001982
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001983 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001984 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02001985
Steven Rostedtb8489142011-05-04 09:27:52 -04001986 /* ops marked global share the filter hashes */
1987 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
1988 ops = &global_ops;
1989 /* Don't update hash if global is already set */
1990 if (global_start_up)
1991 hash_enable = false;
1992 global_start_up++;
1993 }
1994
Steven Rostedted926f92011-05-03 13:25:24 -04001995 ops->flags |= FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04001996 if (hash_enable)
Steven Rostedted926f92011-05-03 13:25:24 -04001997 ftrace_hash_rec_enable(ops, 1);
1998
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001999 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002000
2001 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002002}
2003
Steven Rostedtbd69c302011-05-03 21:55:54 -04002004static void ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002005{
Steven Rostedtb8489142011-05-04 09:27:52 -04002006 bool hash_disable = true;
2007
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002008 if (unlikely(ftrace_disabled))
2009 return;
2010
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002011 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002012 /*
2013 * Just warn in case of unbalance, no need to kill ftrace, it's not
2014 * critical but the ftrace_call callers may be never nopped again after
2015 * further ftrace uses.
2016 */
2017 WARN_ON_ONCE(ftrace_start_up < 0);
2018
Steven Rostedtb8489142011-05-04 09:27:52 -04002019 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2020 ops = &global_ops;
2021 global_start_up--;
2022 WARN_ON_ONCE(global_start_up < 0);
2023 /* Don't update hash if global still has users */
2024 if (global_start_up) {
2025 WARN_ON_ONCE(!ftrace_start_up);
2026 hash_disable = false;
2027 }
2028 }
2029
2030 if (hash_disable)
Steven Rostedted926f92011-05-03 13:25:24 -04002031 ftrace_hash_rec_disable(ops, 1);
2032
Steven Rostedtb8489142011-05-04 09:27:52 -04002033 if (ops != &global_ops || !global_start_up)
Steven Rostedted926f92011-05-03 13:25:24 -04002034 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002035
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002036 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002037
2038 if (saved_ftrace_func != ftrace_trace_function) {
2039 saved_ftrace_func = ftrace_trace_function;
2040 command |= FTRACE_UPDATE_TRACE_FUNC;
2041 }
2042
2043 if (!command || !ftrace_enabled)
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002044 return;
Steven Rostedt3d083392008-05-12 21:20:42 +02002045
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002046 ftrace_run_update_code(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02002047}
2048
Ingo Molnare309b412008-05-12 21:20:51 +02002049static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002050{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002051 if (unlikely(ftrace_disabled))
2052 return;
2053
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002054 /* Force update next time */
2055 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002056 /* ftrace_start_up is true if we want ftrace running */
2057 if (ftrace_start_up)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002058 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002059}
2060
Ingo Molnare309b412008-05-12 21:20:51 +02002061static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002062{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002063 if (unlikely(ftrace_disabled))
2064 return;
2065
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002066 /* ftrace_start_up is true if ftrace is running */
2067 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04002068 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002069}
2070
Steven Rostedt3d083392008-05-12 21:20:42 +02002071static cycle_t ftrace_update_time;
2072static unsigned long ftrace_update_cnt;
2073unsigned long ftrace_update_tot_cnt;
2074
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002075static int ops_traces_mod(struct ftrace_ops *ops)
2076{
2077 struct ftrace_hash *hash;
2078
2079 hash = ops->filter_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05002080 return ftrace_hash_empty(hash);
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002081}
2082
Steven Rostedt31e88902008-11-14 16:21:19 -08002083static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02002084{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002085 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002086 struct dyn_ftrace *p;
Abhishek Sagarf22f9a892008-06-21 23:50:29 +05302087 cycle_t start, stop;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002088 unsigned long ref = 0;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002089 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002090
2091 /*
2092 * When adding a module, we need to check if tracers are
2093 * currently enabled and if they are set to trace all functions.
2094 * If they are, we need to enable the module functions as well
2095 * as update the reference counts for those function records.
2096 */
2097 if (mod) {
2098 struct ftrace_ops *ops;
2099
2100 for (ops = ftrace_ops_list;
2101 ops != &ftrace_list_end; ops = ops->next) {
2102 if (ops->flags & FTRACE_OPS_FL_ENABLED &&
2103 ops_traces_mod(ops))
2104 ref++;
2105 }
2106 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002107
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002108 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002109 ftrace_update_cnt = 0;
2110
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002111 for (pg = ftrace_new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a892008-06-21 23:50:29 +05302112
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002113 for (i = 0; i < pg->index; i++) {
2114 /* If something went wrong, bail without enabling anything */
2115 if (unlikely(ftrace_disabled))
2116 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002117
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002118 p = &pg->records[i];
2119 p->flags = ref;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302120
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002121 /*
2122 * Do the initial record conversion from mcount jump
2123 * to the NOP instructions.
2124 */
2125 if (!ftrace_code_disable(mod, p))
2126 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002127
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002128 ftrace_update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002129
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002130 /*
2131 * If the tracing is enabled, go ahead and enable the record.
2132 *
2133 * The reason not to enable the record immediatelly is the
2134 * inherent check of ftrace_make_nop/ftrace_make_call for
2135 * correct previous instructions. Making first the NOP
2136 * conversion puts the module to the correct state, thus
2137 * passing the ftrace_make_call check.
2138 */
2139 if (ftrace_start_up && ref) {
2140 int failed = __ftrace_replace_code(p, 1);
2141 if (failed)
2142 ftrace_bug(failed, p->ip);
2143 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002144 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002145 }
2146
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002147 ftrace_new_pgs = NULL;
2148
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002149 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002150 ftrace_update_time = stop - start;
2151 ftrace_update_tot_cnt += ftrace_update_cnt;
2152
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002153 return 0;
2154}
2155
Steven Rostedta7900872011-12-16 16:23:44 -05002156static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002157{
Steven Rostedta7900872011-12-16 16:23:44 -05002158 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002159 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002160
Steven Rostedta7900872011-12-16 16:23:44 -05002161 if (WARN_ON(!count))
2162 return -EINVAL;
2163
2164 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002165
2166 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002167 * We want to fill as much as possible. No more than a page
2168 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002169 */
Steven Rostedta7900872011-12-16 16:23:44 -05002170 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2171 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002172
Steven Rostedta7900872011-12-16 16:23:44 -05002173 again:
2174 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2175
2176 if (!pg->records) {
2177 /* if we can't allocate this size, try something smaller */
2178 if (!order)
2179 return -ENOMEM;
2180 order >>= 1;
2181 goto again;
2182 }
2183
2184 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2185 pg->size = cnt;
2186
2187 if (cnt > count)
2188 cnt = count;
2189
2190 return cnt;
2191}
2192
2193static struct ftrace_page *
2194ftrace_allocate_pages(unsigned long num_to_init)
2195{
2196 struct ftrace_page *start_pg;
2197 struct ftrace_page *pg;
2198 int order;
2199 int cnt;
2200
2201 if (!num_to_init)
2202 return 0;
2203
2204 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2205 if (!pg)
2206 return NULL;
2207
2208 /*
2209 * Try to allocate as much as possible in one continues
2210 * location that fills in all of the space. We want to
2211 * waste as little space as possible.
2212 */
2213 for (;;) {
2214 cnt = ftrace_allocate_records(pg, num_to_init);
2215 if (cnt < 0)
2216 goto free_pages;
2217
2218 num_to_init -= cnt;
2219 if (!num_to_init)
2220 break;
2221
2222 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2223 if (!pg->next)
2224 goto free_pages;
2225
2226 pg = pg->next;
2227 }
2228
2229 return start_pg;
2230
2231 free_pages:
2232 while (start_pg) {
2233 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2234 free_pages((unsigned long)pg->records, order);
2235 start_pg = pg->next;
2236 kfree(pg);
2237 pg = start_pg;
2238 }
2239 pr_info("ftrace: FAILED to allocate memory for functions\n");
2240 return NULL;
2241}
2242
2243static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2244{
2245 int cnt;
2246
2247 if (!num_to_init) {
2248 pr_info("ftrace: No functions to be traced?\n");
2249 return -1;
2250 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002251
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002252 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04002253 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08002254 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002255
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002256 return 0;
2257}
2258
Steven Rostedt5072c592008-05-12 21:20:43 +02002259#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2260
2261struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002262 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002263 loff_t func_pos;
2264 struct ftrace_page *pg;
2265 struct dyn_ftrace *func;
2266 struct ftrace_func_probe *probe;
2267 struct trace_parser parser;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002268 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002269 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002270 int hidx;
2271 int idx;
2272 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002273};
2274
Ingo Molnare309b412008-05-12 21:20:51 +02002275static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002276t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002277{
2278 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002279 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002280 struct hlist_head *hhd;
2281
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002282 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002283 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002284
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002285 if (iter->probe)
2286 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002287 retry:
2288 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2289 return NULL;
2290
2291 hhd = &ftrace_func_hash[iter->hidx];
2292
2293 if (hlist_empty(hhd)) {
2294 iter->hidx++;
2295 hnd = NULL;
2296 goto retry;
2297 }
2298
2299 if (!hnd)
2300 hnd = hhd->first;
2301 else {
2302 hnd = hnd->next;
2303 if (!hnd) {
2304 iter->hidx++;
2305 goto retry;
2306 }
2307 }
2308
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002309 if (WARN_ON_ONCE(!hnd))
2310 return NULL;
2311
2312 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2313
2314 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002315}
2316
2317static void *t_hash_start(struct seq_file *m, loff_t *pos)
2318{
2319 struct ftrace_iterator *iter = m->private;
2320 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08002321 loff_t l;
2322
Steven Rostedt69a30832011-12-19 15:21:16 -05002323 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2324 return NULL;
2325
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002326 if (iter->func_pos > *pos)
2327 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002328
Li Zefand82d6242009-06-24 09:54:54 +08002329 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002330 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002331 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08002332 if (!p)
2333 break;
2334 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002335 if (!p)
2336 return NULL;
2337
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002338 /* Only set this if we have an item */
2339 iter->flags |= FTRACE_ITER_HASH;
2340
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002341 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002342}
2343
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002344static int
2345t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002346{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002347 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002348
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002349 rec = iter->probe;
2350 if (WARN_ON_ONCE(!rec))
2351 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002352
Steven Rostedt809dcf22009-02-16 23:06:01 -05002353 if (rec->ops->print)
2354 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2355
Steven Rostedtb375a112009-09-17 00:05:58 -04002356 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002357
2358 if (rec->data)
2359 seq_printf(m, ":%p", rec->data);
2360 seq_putc(m, '\n');
2361
2362 return 0;
2363}
2364
2365static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02002366t_next(struct seq_file *m, void *v, loff_t *pos)
2367{
2368 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002369 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002370 struct dyn_ftrace *rec = NULL;
2371
Steven Rostedt45a4a232011-04-21 23:16:46 -04002372 if (unlikely(ftrace_disabled))
2373 return NULL;
2374
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002375 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002376 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002377
Steven Rostedt5072c592008-05-12 21:20:43 +02002378 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01002379 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02002380
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002381 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002382 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002383
Steven Rostedt5072c592008-05-12 21:20:43 +02002384 retry:
2385 if (iter->idx >= iter->pg->index) {
2386 if (iter->pg->next) {
2387 iter->pg = iter->pg->next;
2388 iter->idx = 0;
2389 goto retry;
2390 }
2391 } else {
2392 rec = &iter->pg->records[iter->idx++];
Steven Rostedt320823092011-12-16 14:42:37 -05002393 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedtf45948e2011-05-02 12:29:25 -04002394 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
Steven Rostedt0183fb1c2008-11-07 22:36:02 -05002395
Steven Rostedt41c52c02008-05-22 11:46:33 -04002396 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt647bcd02011-05-03 14:39:21 -04002397 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2398
2399 ((iter->flags & FTRACE_ITER_ENABLED) &&
2400 !(rec->flags & ~FTRACE_FL_MASK))) {
2401
Steven Rostedt5072c592008-05-12 21:20:43 +02002402 rec = NULL;
2403 goto retry;
2404 }
2405 }
2406
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002407 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002408 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002409
2410 iter->func = rec;
2411
2412 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002413}
2414
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002415static void reset_iter_read(struct ftrace_iterator *iter)
2416{
2417 iter->pos = 0;
2418 iter->func_pos = 0;
2419 iter->flags &= ~(FTRACE_ITER_PRINTALL & FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002420}
2421
2422static void *t_start(struct seq_file *m, loff_t *pos)
2423{
2424 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002425 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002426 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002427 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002428
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002429 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002430
2431 if (unlikely(ftrace_disabled))
2432 return NULL;
2433
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002434 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002435 * If an lseek was done, then reset and start from beginning.
2436 */
2437 if (*pos < iter->pos)
2438 reset_iter_read(iter);
2439
2440 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002441 * For set_ftrace_filter reading, if we have the filter
2442 * off, we can short cut and just print out that all
2443 * functions are enabled.
2444 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05002445 if (iter->flags & FTRACE_ITER_FILTER &&
2446 ftrace_hash_empty(ops->filter_hash)) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002447 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002448 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002449 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002450 /* reset in case of seek/pread */
2451 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002452 return iter;
2453 }
2454
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002455 if (iter->flags & FTRACE_ITER_HASH)
2456 return t_hash_start(m, pos);
2457
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002458 /*
2459 * Unfortunately, we need to restart at ftrace_pages_start
2460 * every time we let go of the ftrace_mutex. This is because
2461 * those pointers can change without the lock.
2462 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002463 iter->pg = ftrace_pages_start;
2464 iter->idx = 0;
2465 for (l = 0; l <= *pos; ) {
2466 p = t_next(m, p, &l);
2467 if (!p)
2468 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002469 }
walimis5821e1b2008-11-15 15:19:06 +08002470
Steven Rostedt69a30832011-12-19 15:21:16 -05002471 if (!p)
2472 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002473
2474 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002475}
2476
2477static void t_stop(struct seq_file *m, void *p)
2478{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002479 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002480}
2481
2482static int t_show(struct seq_file *m, void *v)
2483{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002484 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002485 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02002486
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002487 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002488 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002489
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002490 if (iter->flags & FTRACE_ITER_PRINTALL) {
2491 seq_printf(m, "#### all functions enabled ####\n");
2492 return 0;
2493 }
2494
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002495 rec = iter->func;
2496
Steven Rostedt5072c592008-05-12 21:20:43 +02002497 if (!rec)
2498 return 0;
2499
Steven Rostedt647bcd02011-05-03 14:39:21 -04002500 seq_printf(m, "%ps", (void *)rec->ip);
2501 if (iter->flags & FTRACE_ITER_ENABLED)
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002502 seq_printf(m, " (%ld)%s",
2503 rec->flags & ~FTRACE_FL_MASK,
2504 rec->flags & FTRACE_FL_REGS ? " R" : "");
Steven Rostedt647bcd02011-05-03 14:39:21 -04002505 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02002506
2507 return 0;
2508}
2509
James Morris88e9d342009-09-22 16:43:43 -07002510static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002511 .start = t_start,
2512 .next = t_next,
2513 .stop = t_stop,
2514 .show = t_show,
2515};
2516
Ingo Molnare309b412008-05-12 21:20:51 +02002517static int
Steven Rostedt5072c592008-05-12 21:20:43 +02002518ftrace_avail_open(struct inode *inode, struct file *file)
2519{
2520 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002521
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002522 if (unlikely(ftrace_disabled))
2523 return -ENODEV;
2524
Jiri Olsa50e18b92012-04-25 10:23:39 +02002525 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2526 if (iter) {
2527 iter->pg = ftrace_pages_start;
2528 iter->ops = &global_ops;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002529 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002530
Jiri Olsa50e18b92012-04-25 10:23:39 +02002531 return iter ? 0 : -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02002532}
2533
Steven Rostedt647bcd02011-05-03 14:39:21 -04002534static int
2535ftrace_enabled_open(struct inode *inode, struct file *file)
2536{
2537 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002538
2539 if (unlikely(ftrace_disabled))
2540 return -ENODEV;
2541
Jiri Olsa50e18b92012-04-25 10:23:39 +02002542 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2543 if (iter) {
2544 iter->pg = ftrace_pages_start;
2545 iter->flags = FTRACE_ITER_ENABLED;
2546 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002547 }
2548
Jiri Olsa50e18b92012-04-25 10:23:39 +02002549 return iter ? 0 : -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002550}
2551
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002552static void ftrace_filter_reset(struct ftrace_hash *hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02002553{
Steven Rostedt52baf112009-02-14 01:15:39 -05002554 mutex_lock(&ftrace_lock);
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002555 ftrace_hash_clear(hash);
Steven Rostedt52baf112009-02-14 01:15:39 -05002556 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002557}
2558
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002559/**
2560 * ftrace_regex_open - initialize function tracer filter files
2561 * @ops: The ftrace_ops that hold the hash filters
2562 * @flag: The type of filter to process
2563 * @inode: The inode, usually passed in to your open routine
2564 * @file: The file, usually passed in to your open routine
2565 *
2566 * ftrace_regex_open() initializes the filter files for the
2567 * @ops. Depending on @flag it may process the filter hash or
2568 * the notrace hash of @ops. With this called from the open
2569 * routine, you can use ftrace_filter_write() for the write
2570 * routine if @flag has FTRACE_ITER_FILTER set, or
2571 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
2572 * ftrace_regex_lseek() should be used as the lseek routine, and
2573 * release must call ftrace_regex_release().
2574 */
2575int
Steven Rostedtf45948e2011-05-02 12:29:25 -04002576ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002577 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02002578{
2579 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002580 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02002581 int ret = 0;
2582
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002583 if (unlikely(ftrace_disabled))
2584 return -ENODEV;
2585
Steven Rostedt5072c592008-05-12 21:20:43 +02002586 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2587 if (!iter)
2588 return -ENOMEM;
2589
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002590 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2591 kfree(iter);
2592 return -ENOMEM;
2593 }
2594
Steven Rostedtf45948e2011-05-02 12:29:25 -04002595 if (flag & FTRACE_ITER_NOTRACE)
2596 hash = ops->notrace_hash;
2597 else
2598 hash = ops->filter_hash;
2599
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002600 iter->ops = ops;
2601 iter->flags = flag;
2602
2603 if (file->f_mode & FMODE_WRITE) {
2604 mutex_lock(&ftrace_lock);
2605 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2606 mutex_unlock(&ftrace_lock);
2607
2608 if (!iter->hash) {
2609 trace_parser_put(&iter->parser);
2610 kfree(iter);
2611 return -ENOMEM;
2612 }
2613 }
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002614
Steven Rostedt41c52c02008-05-22 11:46:33 -04002615 mutex_lock(&ftrace_regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002616
Steven Rostedt5072c592008-05-12 21:20:43 +02002617 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002618 (file->f_flags & O_TRUNC))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002619 ftrace_filter_reset(iter->hash);
Steven Rostedt5072c592008-05-12 21:20:43 +02002620
2621 if (file->f_mode & FMODE_READ) {
2622 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02002623
2624 ret = seq_open(file, &show_ftrace_seq_ops);
2625 if (!ret) {
2626 struct seq_file *m = file->private_data;
2627 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08002628 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002629 /* Failed */
2630 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08002631 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002632 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08002633 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002634 } else
2635 file->private_data = iter;
Steven Rostedt41c52c02008-05-22 11:46:33 -04002636 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002637
2638 return ret;
2639}
2640
Steven Rostedt41c52c02008-05-22 11:46:33 -04002641static int
2642ftrace_filter_open(struct inode *inode, struct file *file)
2643{
Steven Rostedt69a30832011-12-19 15:21:16 -05002644 return ftrace_regex_open(&global_ops,
2645 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2646 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002647}
2648
2649static int
2650ftrace_notrace_open(struct inode *inode, struct file *file)
2651{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002652 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002653 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002654}
2655
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002656loff_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04002657ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
Steven Rostedt5072c592008-05-12 21:20:43 +02002658{
2659 loff_t ret;
2660
2661 if (file->f_mode & FMODE_READ)
2662 ret = seq_lseek(file, offset, origin);
2663 else
2664 file->f_pos = ret = 1;
2665
2666 return ret;
2667}
2668
Steven Rostedt64e7c442009-02-13 17:08:48 -05002669static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002670{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002671 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08002672 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002673
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002674 switch (type) {
2675 case MATCH_FULL:
2676 if (strcmp(str, regex) == 0)
2677 matched = 1;
2678 break;
2679 case MATCH_FRONT_ONLY:
2680 if (strncmp(str, regex, len) == 0)
2681 matched = 1;
2682 break;
2683 case MATCH_MIDDLE_ONLY:
2684 if (strstr(str, regex))
2685 matched = 1;
2686 break;
2687 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08002688 slen = strlen(str);
2689 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002690 matched = 1;
2691 break;
2692 }
2693
2694 return matched;
2695}
2696
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002697static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002698enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04002699{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002700 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002701 int ret = 0;
2702
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002703 entry = ftrace_lookup_ip(hash, rec->ip);
2704 if (not) {
2705 /* Do nothing if it doesn't exist */
2706 if (!entry)
2707 return 0;
2708
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002709 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002710 } else {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002711 /* Do nothing if it exists */
2712 if (entry)
2713 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002714
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002715 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002716 }
2717 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04002718}
2719
Steven Rostedt64e7c442009-02-13 17:08:48 -05002720static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002721ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2722 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002723{
2724 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002725 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002726
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002727 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2728
2729 if (mod) {
2730 /* module lookup requires matching the module */
2731 if (!modname || strcmp(modname, mod))
2732 return 0;
2733
2734 /* blank search means to match all funcs in the mod */
2735 if (!len)
2736 return 1;
2737 }
2738
Steven Rostedt64e7c442009-02-13 17:08:48 -05002739 return ftrace_match(str, regex, len, type);
2740}
2741
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002742static int
2743match_records(struct ftrace_hash *hash, char *buff,
2744 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002745{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002746 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002747 struct ftrace_page *pg;
2748 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002749 int type = MATCH_FULL;
2750 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08002751 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002752 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002753
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002754 if (len) {
2755 type = filter_parse_regex(buff, len, &search, &not);
2756 search_len = strlen(search);
2757 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002758
Steven Rostedt52baf112009-02-14 01:15:39 -05002759 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002760
2761 if (unlikely(ftrace_disabled))
2762 goto out_unlock;
2763
Steven Rostedt265c8312009-02-13 12:43:56 -05002764 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002765 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002766 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002767 if (ret < 0) {
2768 found = ret;
2769 goto out_unlock;
2770 }
Li Zefan311d16d2009-12-08 11:15:11 +08002771 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05002772 }
2773 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002774 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05002775 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08002776
2777 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02002778}
2779
Steven Rostedt64e7c442009-02-13 17:08:48 -05002780static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002781ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002782{
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002783 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002784}
2785
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002786static int
2787ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002788{
Steven Rostedt64e7c442009-02-13 17:08:48 -05002789 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002790
Steven Rostedt64e7c442009-02-13 17:08:48 -05002791 /* blank or '*' mean the same */
2792 if (strcmp(buff, "*") == 0)
2793 buff[0] = 0;
2794
2795 /* handle the case of 'dont filter this module' */
2796 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2797 buff[0] = 0;
2798 not = 1;
2799 }
2800
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002801 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002802}
2803
Steven Rostedtf6180772009-02-14 00:40:25 -05002804/*
2805 * We register the module command as a template to show others how
2806 * to register the a command as well.
2807 */
2808
2809static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04002810ftrace_mod_callback(struct ftrace_hash *hash,
2811 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05002812{
2813 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002814 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05002815
2816 /*
2817 * cmd == 'mod' because we only registered this func
2818 * for the 'mod' ftrace_func_command.
2819 * But if you register one func with multiple commands,
2820 * you can tell which command was used by the cmd
2821 * parameter.
2822 */
2823
2824 /* we must have a module name */
2825 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002826 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002827
2828 mod = strsep(&param, ":");
2829 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002830 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002831
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002832 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002833 if (!ret)
2834 ret = -EINVAL;
2835 if (ret < 0)
2836 return ret;
2837
2838 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05002839}
2840
2841static struct ftrace_func_command ftrace_mod_cmd = {
2842 .name = "mod",
2843 .func = ftrace_mod_callback,
2844};
2845
2846static int __init ftrace_mod_cmd_init(void)
2847{
2848 return register_ftrace_command(&ftrace_mod_cmd);
2849}
2850device_initcall(ftrace_mod_cmd_init);
2851
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04002852static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04002853 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002854{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002855 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002856 struct hlist_head *hhd;
2857 struct hlist_node *n;
2858 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002859
2860 key = hash_long(ip, FTRACE_HASH_BITS);
2861
2862 hhd = &ftrace_func_hash[key];
2863
2864 if (hlist_empty(hhd))
2865 return;
2866
2867 /*
2868 * Disable preemption for these calls to prevent a RCU grace
2869 * period. This syncs the hash iteration and freeing of items
2870 * on the hash. rcu_read_lock is too dangerous here.
2871 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04002872 preempt_disable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002873 hlist_for_each_entry_rcu(entry, n, hhd, node) {
2874 if (entry->ip == ip)
2875 entry->ops->func(ip, parent_ip, &entry->data);
2876 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04002877 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002878}
2879
Steven Rostedtb6887d72009-02-17 12:32:04 -05002880static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05002881{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04002882 .func = function_trace_probe_call,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002883};
2884
Steven Rostedtb6887d72009-02-17 12:32:04 -05002885static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002886
Steven Rostedtb6887d72009-02-17 12:32:04 -05002887static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002888{
Steven Rostedtb8489142011-05-04 09:27:52 -04002889 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002890 int i;
2891
Steven Rostedtb6887d72009-02-17 12:32:04 -05002892 if (ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002893 return;
2894
2895 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2896 struct hlist_head *hhd = &ftrace_func_hash[i];
2897 if (hhd->first)
2898 break;
2899 }
2900 /* Nothing registered? */
2901 if (i == FTRACE_FUNC_HASHSIZE)
2902 return;
2903
Steven Rostedtb8489142011-05-04 09:27:52 -04002904 ret = __register_ftrace_function(&trace_probe_ops);
2905 if (!ret)
Steven Rostedta1cd6172011-05-23 15:24:25 -04002906 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04002907
Steven Rostedtb6887d72009-02-17 12:32:04 -05002908 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002909}
2910
Steven Rostedtb6887d72009-02-17 12:32:04 -05002911static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002912{
Steven Rostedtb8489142011-05-04 09:27:52 -04002913 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002914 int i;
2915
Steven Rostedtb6887d72009-02-17 12:32:04 -05002916 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002917 return;
2918
2919 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2920 struct hlist_head *hhd = &ftrace_func_hash[i];
2921 if (hhd->first)
2922 return;
2923 }
2924
2925 /* no more funcs left */
Steven Rostedtb8489142011-05-04 09:27:52 -04002926 ret = __unregister_ftrace_function(&trace_probe_ops);
2927 if (!ret)
2928 ftrace_shutdown(&trace_probe_ops, 0);
2929
Steven Rostedtb6887d72009-02-17 12:32:04 -05002930 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002931}
2932
2933
2934static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2935{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002936 struct ftrace_func_probe *entry =
2937 container_of(rhp, struct ftrace_func_probe, rcu);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002938
2939 if (entry->ops->free)
2940 entry->ops->free(&entry->data);
2941 kfree(entry);
2942}
2943
2944
2945int
Steven Rostedtb6887d72009-02-17 12:32:04 -05002946register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002947 void *data)
2948{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002949 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002950 struct ftrace_page *pg;
2951 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002952 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002953 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002954 int count = 0;
2955 char *search;
2956
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002957 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002958 len = strlen(search);
2959
Steven Rostedtb6887d72009-02-17 12:32:04 -05002960 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002961 if (WARN_ON(not))
2962 return -EINVAL;
2963
2964 mutex_lock(&ftrace_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002965
Steven Rostedt45a4a232011-04-21 23:16:46 -04002966 if (unlikely(ftrace_disabled))
2967 goto out_unlock;
2968
Steven Rostedt59df055f2009-02-14 15:29:06 -05002969 do_for_each_ftrace_rec(pg, rec) {
2970
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002971 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05002972 continue;
2973
2974 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2975 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002976 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002977 if (!count)
2978 count = -ENOMEM;
2979 goto out_unlock;
2980 }
2981
2982 count++;
2983
2984 entry->data = data;
2985
2986 /*
2987 * The caller might want to do something special
2988 * for each function we find. We call the callback
2989 * to give the caller an opportunity to do so.
2990 */
2991 if (ops->callback) {
2992 if (ops->callback(rec->ip, &entry->data) < 0) {
2993 /* caller does not like this func */
2994 kfree(entry);
2995 continue;
2996 }
2997 }
2998
2999 entry->ops = ops;
3000 entry->ip = rec->ip;
3001
3002 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3003 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3004
3005 } while_for_each_ftrace_rec();
Steven Rostedtb6887d72009-02-17 12:32:04 -05003006 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003007
3008 out_unlock:
3009 mutex_unlock(&ftrace_lock);
3010
3011 return count;
3012}
3013
3014enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003015 PROBE_TEST_FUNC = 1,
3016 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05003017};
3018
3019static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003020__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003021 void *data, int flags)
3022{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003023 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003024 struct hlist_node *n, *tmp;
3025 char str[KSYM_SYMBOL_LEN];
3026 int type = MATCH_FULL;
3027 int i, len = 0;
3028 char *search;
3029
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003030 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003031 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003032 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003033 int not;
3034
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003035 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003036 len = strlen(search);
3037
Steven Rostedtb6887d72009-02-17 12:32:04 -05003038 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003039 if (WARN_ON(not))
3040 return;
3041 }
3042
3043 mutex_lock(&ftrace_lock);
3044 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3045 struct hlist_head *hhd = &ftrace_func_hash[i];
3046
3047 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
3048
3049 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05003050 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003051 continue;
3052
Steven Rostedtb6887d72009-02-17 12:32:04 -05003053 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003054 continue;
3055
3056 /* do this last, since it is the most expensive */
3057 if (glob) {
3058 kallsyms_lookup(entry->ip, NULL, NULL,
3059 NULL, str);
3060 if (!ftrace_match(str, glob, len, type))
3061 continue;
3062 }
3063
3064 hlist_del(&entry->node);
3065 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
3066 }
3067 }
Steven Rostedtb6887d72009-02-17 12:32:04 -05003068 __disable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003069 mutex_unlock(&ftrace_lock);
3070}
3071
3072void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003073unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003074 void *data)
3075{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003076 __unregister_ftrace_function_probe(glob, ops, data,
3077 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003078}
3079
3080void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003081unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003082{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003083 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003084}
3085
Steven Rostedtb6887d72009-02-17 12:32:04 -05003086void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003087{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003088 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003089}
3090
Steven Rostedtf6180772009-02-14 00:40:25 -05003091static LIST_HEAD(ftrace_commands);
3092static DEFINE_MUTEX(ftrace_cmd_mutex);
3093
3094int register_ftrace_command(struct ftrace_func_command *cmd)
3095{
3096 struct ftrace_func_command *p;
3097 int ret = 0;
3098
3099 mutex_lock(&ftrace_cmd_mutex);
3100 list_for_each_entry(p, &ftrace_commands, list) {
3101 if (strcmp(cmd->name, p->name) == 0) {
3102 ret = -EBUSY;
3103 goto out_unlock;
3104 }
3105 }
3106 list_add(&cmd->list, &ftrace_commands);
3107 out_unlock:
3108 mutex_unlock(&ftrace_cmd_mutex);
3109
3110 return ret;
3111}
3112
3113int unregister_ftrace_command(struct ftrace_func_command *cmd)
3114{
3115 struct ftrace_func_command *p, *n;
3116 int ret = -ENODEV;
3117
3118 mutex_lock(&ftrace_cmd_mutex);
3119 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3120 if (strcmp(cmd->name, p->name) == 0) {
3121 ret = 0;
3122 list_del_init(&p->list);
3123 goto out_unlock;
3124 }
3125 }
3126 out_unlock:
3127 mutex_unlock(&ftrace_cmd_mutex);
3128
3129 return ret;
3130}
3131
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003132static int ftrace_process_regex(struct ftrace_hash *hash,
3133 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003134{
Steven Rostedtf6180772009-02-14 00:40:25 -05003135 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003136 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08003137 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003138
3139 func = strsep(&next, ":");
3140
3141 if (!next) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003142 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003143 if (!ret)
3144 ret = -EINVAL;
3145 if (ret < 0)
3146 return ret;
3147 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003148 }
3149
Steven Rostedtf6180772009-02-14 00:40:25 -05003150 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05003151
3152 command = strsep(&next, ":");
3153
Steven Rostedtf6180772009-02-14 00:40:25 -05003154 mutex_lock(&ftrace_cmd_mutex);
3155 list_for_each_entry(p, &ftrace_commands, list) {
3156 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003157 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05003158 goto out_unlock;
3159 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05003160 }
Steven Rostedtf6180772009-02-14 00:40:25 -05003161 out_unlock:
3162 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003163
Steven Rostedtf6180772009-02-14 00:40:25 -05003164 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003165}
3166
Ingo Molnare309b412008-05-12 21:20:51 +02003167static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003168ftrace_regex_write(struct file *file, const char __user *ubuf,
3169 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02003170{
3171 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003172 struct trace_parser *parser;
3173 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02003174
Li Zefan4ba79782009-09-22 13:52:20 +08003175 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02003176 return 0;
3177
Steven Rostedt41c52c02008-05-22 11:46:33 -04003178 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003179
Steven Rostedt45a4a232011-04-21 23:16:46 -04003180 ret = -ENODEV;
3181 if (unlikely(ftrace_disabled))
3182 goto out_unlock;
3183
Steven Rostedt5072c592008-05-12 21:20:43 +02003184 if (file->f_mode & FMODE_READ) {
3185 struct seq_file *m = file->private_data;
3186 iter = m->private;
3187 } else
3188 iter = file->private_data;
3189
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003190 parser = &iter->parser;
3191 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02003192
Li Zefan4ba79782009-09-22 13:52:20 +08003193 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003194 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003195 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003196 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08003197 trace_parser_clear(parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02003198 if (ret)
Li Zefaned146b252009-11-03 08:55:38 +08003199 goto out_unlock;
Steven Rostedt5072c592008-05-12 21:20:43 +02003200 }
3201
Steven Rostedt5072c592008-05-12 21:20:43 +02003202 ret = read;
Li Zefaned146b252009-11-03 08:55:38 +08003203out_unlock:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003204 mutex_unlock(&ftrace_regex_lock);
Li Zefaned146b252009-11-03 08:55:38 +08003205
Steven Rostedt5072c592008-05-12 21:20:43 +02003206 return ret;
3207}
3208
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003209ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003210ftrace_filter_write(struct file *file, const char __user *ubuf,
3211 size_t cnt, loff_t *ppos)
3212{
3213 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3214}
3215
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003216ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003217ftrace_notrace_write(struct file *file, const char __user *ubuf,
3218 size_t cnt, loff_t *ppos)
3219{
3220 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3221}
3222
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003223static int
Steven Rostedtf45948e2011-05-02 12:29:25 -04003224ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3225 int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003226{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003227 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003228 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003229 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003230
Steven Rostedt936e0742011-05-05 22:54:01 -04003231 /* All global ops uses the global ops filters */
3232 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3233 ops = &global_ops;
3234
Steven Rostedt41c52c02008-05-22 11:46:33 -04003235 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003236 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003237
Steven Rostedtf45948e2011-05-02 12:29:25 -04003238 if (enable)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003239 orig_hash = &ops->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003240 else
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003241 orig_hash = &ops->notrace_hash;
3242
3243 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3244 if (!hash)
3245 return -ENOMEM;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003246
Steven Rostedt41c52c02008-05-22 11:46:33 -04003247 mutex_lock(&ftrace_regex_lock);
3248 if (reset)
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003249 ftrace_filter_reset(hash);
Jiri Olsaac483c42012-01-02 10:04:14 +01003250 if (buf && !ftrace_match_records(hash, buf, len)) {
3251 ret = -EINVAL;
3252 goto out_regex_unlock;
3253 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003254
3255 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003256 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt072126f2011-07-13 15:08:31 -04003257 if (!ret && ops->flags & FTRACE_OPS_FL_ENABLED
3258 && ftrace_enabled)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01003259 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt072126f2011-07-13 15:08:31 -04003260
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003261 mutex_unlock(&ftrace_lock);
3262
Jiri Olsaac483c42012-01-02 10:04:14 +01003263 out_regex_unlock:
Steven Rostedt41c52c02008-05-22 11:46:33 -04003264 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003265
3266 free_ftrace_hash(hash);
3267 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003268}
3269
Steven Rostedt77a2b372008-05-12 21:20:45 +02003270/**
3271 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003272 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02003273 * @buf - the string that holds the function filter text.
3274 * @len - the length of the string.
3275 * @reset - non zero to reset all filters before applying this filter.
3276 *
3277 * Filters denote which functions should be enabled when tracing is enabled.
3278 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3279 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003280int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003281 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02003282{
Jiri Olsaac483c42012-01-02 10:04:14 +01003283 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003284}
Steven Rostedt936e0742011-05-05 22:54:01 -04003285EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003286
Steven Rostedt41c52c02008-05-22 11:46:33 -04003287/**
3288 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003289 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04003290 * @buf - the string that holds the function notrace text.
3291 * @len - the length of the string.
3292 * @reset - non zero to reset all filters before applying this filter.
3293 *
3294 * Notrace Filters denote which functions should not be enabled when tracing
3295 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3296 * for tracing.
3297 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003298int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003299 int len, int reset)
3300{
Jiri Olsaac483c42012-01-02 10:04:14 +01003301 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04003302}
3303EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3304/**
3305 * ftrace_set_filter - set a function to filter on in ftrace
3306 * @ops - the ops to set the filter with
3307 * @buf - the string that holds the function filter text.
3308 * @len - the length of the string.
3309 * @reset - non zero to reset all filters before applying this filter.
3310 *
3311 * Filters denote which functions should be enabled when tracing is enabled.
3312 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3313 */
3314void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3315{
3316 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3317}
3318EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3319
3320/**
3321 * ftrace_set_notrace - set a function to not trace in ftrace
3322 * @ops - the ops to set the notrace filter with
3323 * @buf - the string that holds the function notrace text.
3324 * @len - the length of the string.
3325 * @reset - non zero to reset all filters before applying this filter.
3326 *
3327 * Notrace Filters denote which functions should not be enabled when tracing
3328 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3329 * for tracing.
3330 */
3331void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003332{
Steven Rostedtf45948e2011-05-02 12:29:25 -04003333 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003334}
Steven Rostedt936e0742011-05-05 22:54:01 -04003335EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003336
Steven Rostedt2af15d62009-05-28 13:37:24 -04003337/*
3338 * command line interface to allow users to set filters on boot up.
3339 */
3340#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3341static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3342static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3343
3344static int __init set_ftrace_notrace(char *str)
3345{
3346 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
3347 return 1;
3348}
3349__setup("ftrace_notrace=", set_ftrace_notrace);
3350
3351static int __init set_ftrace_filter(char *str)
3352{
3353 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
3354 return 1;
3355}
3356__setup("ftrace_filter=", set_ftrace_filter);
3357
Stefan Assmann369bc182009-10-12 22:17:21 +02003358#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08003359static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Steven Rostedt801c29f2010-03-05 20:02:19 -05003360static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
3361
Stefan Assmann369bc182009-10-12 22:17:21 +02003362static int __init set_graph_function(char *str)
3363{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02003364 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02003365 return 1;
3366}
3367__setup("ftrace_graph_filter=", set_graph_function);
3368
3369static void __init set_ftrace_early_graph(char *buf)
3370{
3371 int ret;
3372 char *func;
3373
3374 while (buf) {
3375 func = strsep(&buf, ",");
3376 /* we allow only one expression at a time */
3377 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3378 func);
3379 if (ret)
3380 printk(KERN_DEBUG "ftrace: function %s not "
3381 "traceable\n", func);
3382 }
3383}
3384#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3385
Steven Rostedt2a85a372011-12-19 21:57:44 -05003386void __init
3387ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04003388{
3389 char *func;
3390
3391 while (buf) {
3392 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04003393 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003394 }
3395}
3396
3397static void __init set_ftrace_early_filters(void)
3398{
3399 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003400 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003401 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003402 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02003403#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3404 if (ftrace_graph_buf[0])
3405 set_ftrace_early_graph(ftrace_graph_buf);
3406#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04003407}
3408
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003409int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003410{
3411 struct seq_file *m = (struct seq_file *)file->private_data;
3412 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003413 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003414 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04003415 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003416 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02003417
Steven Rostedt41c52c02008-05-22 11:46:33 -04003418 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003419 if (file->f_mode & FMODE_READ) {
3420 iter = m->private;
3421
3422 seq_release(inode, file);
3423 } else
3424 iter = file->private_data;
3425
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003426 parser = &iter->parser;
3427 if (trace_parser_loaded(parser)) {
3428 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003429 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02003430 }
3431
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003432 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003433
Steven Rostedt058e2972011-04-29 22:35:33 -04003434 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04003435 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3436
3437 if (filter_hash)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003438 orig_hash = &iter->ops->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04003439 else
3440 orig_hash = &iter->ops->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003441
Steven Rostedt058e2972011-04-29 22:35:33 -04003442 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003443 ret = ftrace_hash_move(iter->ops, filter_hash,
3444 orig_hash, iter->hash);
3445 if (!ret && (iter->ops->flags & FTRACE_OPS_FL_ENABLED)
3446 && ftrace_enabled)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01003447 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003448
Steven Rostedt058e2972011-04-29 22:35:33 -04003449 mutex_unlock(&ftrace_lock);
3450 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003451 free_ftrace_hash(iter->hash);
3452 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04003453
Steven Rostedt41c52c02008-05-22 11:46:33 -04003454 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003455 return 0;
3456}
3457
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003458static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003459 .open = ftrace_avail_open,
3460 .read = seq_read,
3461 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08003462 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02003463};
3464
Steven Rostedt647bcd02011-05-03 14:39:21 -04003465static const struct file_operations ftrace_enabled_fops = {
3466 .open = ftrace_enabled_open,
3467 .read = seq_read,
3468 .llseek = seq_lseek,
3469 .release = seq_release_private,
3470};
3471
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003472static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003473 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003474 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02003475 .write = ftrace_filter_write,
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003476 .llseek = ftrace_regex_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003477 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02003478};
3479
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003480static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04003481 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003482 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003483 .write = ftrace_notrace_write,
3484 .llseek = ftrace_regex_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003485 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003486};
3487
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003488#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3489
3490static DEFINE_MUTEX(graph_lock);
3491
3492int ftrace_graph_count;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003493int ftrace_graph_filter_enabled;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003494unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3495
3496static void *
Li Zefan85951842009-06-24 09:54:00 +08003497__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003498{
Li Zefan85951842009-06-24 09:54:00 +08003499 if (*pos >= ftrace_graph_count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003500 return NULL;
Li Zefana4ec5e02009-09-18 14:06:28 +08003501 return &ftrace_graph_funcs[*pos];
Li Zefan85951842009-06-24 09:54:00 +08003502}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003503
Li Zefan85951842009-06-24 09:54:00 +08003504static void *
3505g_next(struct seq_file *m, void *v, loff_t *pos)
3506{
3507 (*pos)++;
3508 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003509}
3510
3511static void *g_start(struct seq_file *m, loff_t *pos)
3512{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003513 mutex_lock(&graph_lock);
3514
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003515 /* Nothing, tell g_show to print all functions are enabled */
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003516 if (!ftrace_graph_filter_enabled && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003517 return (void *)1;
3518
Li Zefan85951842009-06-24 09:54:00 +08003519 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003520}
3521
3522static void g_stop(struct seq_file *m, void *p)
3523{
3524 mutex_unlock(&graph_lock);
3525}
3526
3527static int g_show(struct seq_file *m, void *v)
3528{
3529 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003530
3531 if (!ptr)
3532 return 0;
3533
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003534 if (ptr == (unsigned long *)1) {
3535 seq_printf(m, "#### all functions enabled ####\n");
3536 return 0;
3537 }
3538
Steven Rostedtb375a112009-09-17 00:05:58 -04003539 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003540
3541 return 0;
3542}
3543
James Morris88e9d342009-09-22 16:43:43 -07003544static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003545 .start = g_start,
3546 .next = g_next,
3547 .stop = g_stop,
3548 .show = g_show,
3549};
3550
3551static int
3552ftrace_graph_open(struct inode *inode, struct file *file)
3553{
3554 int ret = 0;
3555
3556 if (unlikely(ftrace_disabled))
3557 return -ENODEV;
3558
3559 mutex_lock(&graph_lock);
3560 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04003561 (file->f_flags & O_TRUNC)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003562 ftrace_graph_filter_enabled = 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003563 ftrace_graph_count = 0;
3564 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3565 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003566 mutex_unlock(&graph_lock);
3567
Li Zefana4ec5e02009-09-18 14:06:28 +08003568 if (file->f_mode & FMODE_READ)
3569 ret = seq_open(file, &ftrace_graph_seq_ops);
3570
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003571 return ret;
3572}
3573
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003574static int
Li Zefan87827112009-07-23 11:29:11 +08003575ftrace_graph_release(struct inode *inode, struct file *file)
3576{
3577 if (file->f_mode & FMODE_READ)
3578 seq_release(inode, file);
3579 return 0;
3580}
3581
3582static int
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003583ftrace_set_func(unsigned long *array, int *idx, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003584{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003585 struct dyn_ftrace *rec;
3586 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003587 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003588 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003589 int type, not;
3590 char *search;
3591 bool exists;
3592 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003593
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003594 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003595 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003596 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3597 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003598
3599 search_len = strlen(search);
3600
Steven Rostedt52baf112009-02-14 01:15:39 -05003601 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003602
3603 if (unlikely(ftrace_disabled)) {
3604 mutex_unlock(&ftrace_lock);
3605 return -ENODEV;
3606 }
3607
Steven Rostedt265c8312009-02-13 12:43:56 -05003608 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003609
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003610 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003611 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003612 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003613 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003614 if (array[i] == rec->ip) {
3615 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05003616 break;
3617 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003618 }
3619
3620 if (!not) {
3621 fail = 0;
3622 if (!exists) {
3623 array[(*idx)++] = rec->ip;
3624 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3625 goto out;
3626 }
3627 } else {
3628 if (exists) {
3629 array[i] = array[--(*idx)];
3630 array[*idx] = 0;
3631 fail = 0;
3632 }
3633 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003634 }
Steven Rostedt265c8312009-02-13 12:43:56 -05003635 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003636out:
Steven Rostedt52baf112009-02-14 01:15:39 -05003637 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003638
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003639 if (fail)
3640 return -EINVAL;
3641
3642 ftrace_graph_filter_enabled = 1;
3643 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003644}
3645
3646static ssize_t
3647ftrace_graph_write(struct file *file, const char __user *ubuf,
3648 size_t cnt, loff_t *ppos)
3649{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003650 struct trace_parser parser;
Li Zefan4ba79782009-09-22 13:52:20 +08003651 ssize_t read, ret;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003652
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003653 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003654 return 0;
3655
3656 mutex_lock(&graph_lock);
3657
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003658 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3659 ret = -ENOMEM;
Li Zefan1eb90f12009-09-22 13:52:57 +08003660 goto out_unlock;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003661 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003662
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003663 read = trace_get_user(&parser, ubuf, cnt, ppos);
3664
Li Zefan4ba79782009-09-22 13:52:20 +08003665 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003666 parser.buffer[parser.idx] = 0;
3667
3668 /* we allow only one expression at a time */
Li Zefana4ec5e02009-09-18 14:06:28 +08003669 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003670 parser.buffer);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003671 if (ret)
Li Zefan1eb90f12009-09-22 13:52:57 +08003672 goto out_free;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003673 }
3674
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003675 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08003676
3677out_free:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003678 trace_parser_put(&parser);
Li Zefan1eb90f12009-09-22 13:52:57 +08003679out_unlock:
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003680 mutex_unlock(&graph_lock);
3681
3682 return ret;
3683}
3684
3685static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08003686 .open = ftrace_graph_open,
3687 .read = seq_read,
3688 .write = ftrace_graph_write,
3689 .release = ftrace_graph_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003690 .llseek = seq_lseek,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003691};
3692#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3693
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003694static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02003695{
Steven Rostedt5072c592008-05-12 21:20:43 +02003696
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003697 trace_create_file("available_filter_functions", 0444,
3698 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02003699
Steven Rostedt647bcd02011-05-03 14:39:21 -04003700 trace_create_file("enabled_functions", 0444,
3701 d_tracer, NULL, &ftrace_enabled_fops);
3702
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003703 trace_create_file("set_ftrace_filter", 0644, d_tracer,
3704 NULL, &ftrace_filter_fops);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003705
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003706 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003707 NULL, &ftrace_notrace_fops);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04003708
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003709#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003710 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003711 NULL,
3712 &ftrace_graph_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003713#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3714
Steven Rostedt5072c592008-05-12 21:20:43 +02003715 return 0;
3716}
3717
Steven Rostedt9fd49322012-04-24 22:32:06 -04003718static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05003719{
Steven Rostedt9fd49322012-04-24 22:32:06 -04003720 const unsigned long *ipa = a;
3721 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05003722
Steven Rostedt9fd49322012-04-24 22:32:06 -04003723 if (*ipa > *ipb)
3724 return 1;
3725 if (*ipa < *ipb)
3726 return -1;
3727 return 0;
3728}
3729
3730static void ftrace_swap_ips(void *a, void *b, int size)
3731{
3732 unsigned long *ipa = a;
3733 unsigned long *ipb = b;
3734 unsigned long t;
3735
3736 t = *ipa;
3737 *ipa = *ipb;
3738 *ipb = t;
Steven Rostedt68950612011-12-16 17:06:45 -05003739}
3740
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003741static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08003742 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003743 unsigned long *end)
3744{
Steven Rostedt706c81f2012-04-24 23:45:26 -04003745 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05003746 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04003747 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05003748 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003749 unsigned long *p;
3750 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04003751 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05003752 int ret = -ENOMEM;
3753
3754 count = end - start;
3755
3756 if (!count)
3757 return 0;
3758
Steven Rostedt9fd49322012-04-24 22:32:06 -04003759 sort(start, count, sizeof(*start),
3760 ftrace_cmp_ips, ftrace_swap_ips);
3761
Steven Rostedt706c81f2012-04-24 23:45:26 -04003762 start_pg = ftrace_allocate_pages(count);
3763 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05003764 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003765
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003766 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05003767
Steven Rostedt320823092011-12-16 14:42:37 -05003768 /*
3769 * Core and each module needs their own pages, as
3770 * modules will free them when they are removed.
3771 * Force a new page to be allocated for modules.
3772 */
Steven Rostedta7900872011-12-16 16:23:44 -05003773 if (!mod) {
3774 WARN_ON(ftrace_pages || ftrace_pages_start);
3775 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04003776 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05003777 } else {
Steven Rostedt320823092011-12-16 14:42:37 -05003778 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05003779 goto out;
Steven Rostedt320823092011-12-16 14:42:37 -05003780
Steven Rostedta7900872011-12-16 16:23:44 -05003781 if (WARN_ON(ftrace_pages->next)) {
3782 /* Hmm, we have free pages? */
3783 while (ftrace_pages->next)
3784 ftrace_pages = ftrace_pages->next;
Steven Rostedt320823092011-12-16 14:42:37 -05003785 }
Steven Rostedta7900872011-12-16 16:23:44 -05003786
Steven Rostedt706c81f2012-04-24 23:45:26 -04003787 ftrace_pages->next = start_pg;
Steven Rostedt320823092011-12-16 14:42:37 -05003788 }
3789
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003790 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04003791 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003792 while (p < end) {
3793 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08003794 /*
3795 * Some architecture linkers will pad between
3796 * the different mcount_loc sections of different
3797 * object files to satisfy alignments.
3798 * Skip any NULL pointers.
3799 */
3800 if (!addr)
3801 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04003802
3803 if (pg->index == pg->size) {
3804 /* We should have allocated enough */
3805 if (WARN_ON(!pg->next))
3806 break;
3807 pg = pg->next;
3808 }
3809
3810 rec = &pg->records[pg->index++];
3811 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003812 }
3813
Steven Rostedt706c81f2012-04-24 23:45:26 -04003814 /* We should have used all pages */
3815 WARN_ON(pg->next);
3816
3817 /* Assign the last page to ftrace_pages */
3818 ftrace_pages = pg;
3819
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003820 /* These new locations need to be initialized */
Steven Rostedt706c81f2012-04-24 23:45:26 -04003821 ftrace_new_pgs = start_pg;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003822
Steven Rostedta4f18ed2011-06-07 09:26:46 -04003823 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04003824 * We only need to disable interrupts on start up
3825 * because we are modifying code that an interrupt
3826 * may execute, and the modification is not atomic.
3827 * But for modules, nothing runs the code we modify
3828 * until we are finished with it, and there's no
3829 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04003830 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04003831 if (!mod)
3832 local_irq_save(flags);
Steven Rostedt31e88902008-11-14 16:21:19 -08003833 ftrace_update_code(mod);
Steven Rostedt4376cac2011-06-24 23:28:13 -04003834 if (!mod)
3835 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05003836 ret = 0;
3837 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003838 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003839
Steven Rostedta7900872011-12-16 16:23:44 -05003840 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003841}
3842
Steven Rostedt93eb6772009-04-15 13:24:06 -04003843#ifdef CONFIG_MODULES
Steven Rostedt320823092011-12-16 14:42:37 -05003844
3845#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
3846
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003847void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04003848{
3849 struct dyn_ftrace *rec;
Steven Rostedt320823092011-12-16 14:42:37 -05003850 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04003851 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05003852 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04003853
Steven Rostedt93eb6772009-04-15 13:24:06 -04003854 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003855
3856 if (ftrace_disabled)
3857 goto out_unlock;
3858
Steven Rostedt320823092011-12-16 14:42:37 -05003859 /*
3860 * Each module has its own ftrace_pages, remove
3861 * them from the list.
3862 */
3863 last_pg = &ftrace_pages_start;
3864 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
3865 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003866 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04003867 /*
Steven Rostedt320823092011-12-16 14:42:37 -05003868 * As core pages are first, the first
3869 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04003870 */
Steven Rostedt320823092011-12-16 14:42:37 -05003871 if (WARN_ON(pg == ftrace_pages_start))
3872 goto out_unlock;
3873
3874 /* Check if we are deleting the last page */
3875 if (pg == ftrace_pages)
3876 ftrace_pages = next_to_ftrace_page(last_pg);
3877
3878 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05003879 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3880 free_pages((unsigned long)pg->records, order);
3881 kfree(pg);
Steven Rostedt320823092011-12-16 14:42:37 -05003882 } else
3883 last_pg = &pg->next;
3884 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003885 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04003886 mutex_unlock(&ftrace_lock);
3887}
3888
3889static void ftrace_init_module(struct module *mod,
3890 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04003891{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04003892 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04003893 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003894 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04003895}
3896
Steven Rostedt93eb6772009-04-15 13:24:06 -04003897static int ftrace_module_notify(struct notifier_block *self,
3898 unsigned long val, void *data)
3899{
3900 struct module *mod = data;
3901
3902 switch (val) {
3903 case MODULE_STATE_COMING:
3904 ftrace_init_module(mod, mod->ftrace_callsites,
3905 mod->ftrace_callsites +
3906 mod->num_ftrace_callsites);
3907 break;
3908 case MODULE_STATE_GOING:
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003909 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04003910 break;
3911 }
3912
3913 return 0;
3914}
3915#else
3916static int ftrace_module_notify(struct notifier_block *self,
3917 unsigned long val, void *data)
3918{
3919 return 0;
3920}
3921#endif /* CONFIG_MODULES */
3922
3923struct notifier_block ftrace_module_nb = {
3924 .notifier_call = ftrace_module_notify,
3925 .priority = 0,
3926};
3927
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003928extern unsigned long __start_mcount_loc[];
3929extern unsigned long __stop_mcount_loc[];
3930
3931void __init ftrace_init(void)
3932{
3933 unsigned long count, addr, flags;
3934 int ret;
3935
3936 /* Keep the ftrace pointer to the stub */
3937 addr = (unsigned long)ftrace_stub;
3938
3939 local_irq_save(flags);
3940 ftrace_dyn_arch_init(&addr);
3941 local_irq_restore(flags);
3942
3943 /* ftrace_dyn_arch_init places the return code in addr */
3944 if (addr)
3945 goto failed;
3946
3947 count = __stop_mcount_loc - __start_mcount_loc;
3948
3949 ret = ftrace_dyn_table_alloc(count);
3950 if (ret)
3951 goto failed;
3952
3953 last_ftrace_enabled = ftrace_enabled = 1;
3954
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003955 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08003956 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003957 __stop_mcount_loc);
3958
Steven Rostedt93eb6772009-04-15 13:24:06 -04003959 ret = register_module_notifier(&ftrace_module_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08003960 if (ret)
Steven Rostedt93eb6772009-04-15 13:24:06 -04003961 pr_warning("Failed to register trace ftrace module notifier\n");
3962
Steven Rostedt2af15d62009-05-28 13:37:24 -04003963 set_ftrace_early_filters();
3964
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003965 return;
3966 failed:
3967 ftrace_disabled = 1;
3968}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003969
Steven Rostedt3d083392008-05-12 21:20:42 +02003970#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01003971
Steven Rostedt2b499382011-05-03 22:49:52 -04003972static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04003973 .func = ftrace_stub,
Steven Rostedt47409742012-07-20 11:04:44 -04003974 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Steven Rostedtbd69c302011-05-03 21:55:54 -04003975};
3976
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01003977static int __init ftrace_nodyn_init(void)
3978{
3979 ftrace_enabled = 1;
3980 return 0;
3981}
3982device_initcall(ftrace_nodyn_init);
3983
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003984static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
3985static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05003986/* Keep as macros so we do not need to define the commands */
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04003987# define ftrace_startup(ops, command) \
3988 ({ \
3989 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
3990 0; \
3991 })
Steven Rostedtbd69c302011-05-03 21:55:54 -04003992# define ftrace_shutdown(ops, command) do { } while (0)
Ingo Molnarc7aafc52008-05-12 21:20:45 +02003993# define ftrace_startup_sysctl() do { } while (0)
3994# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04003995
3996static inline int
3997ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
3998{
3999 return 1;
4000}
4001
Steven Rostedt3d083392008-05-12 21:20:42 +02004002#endif /* CONFIG_DYNAMIC_FTRACE */
4003
Steven Rostedtb8489142011-05-04 09:27:52 -04004004static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004005ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004006 struct ftrace_ops *op, struct pt_regs *regs)
Jiri Olsae2484912012-02-15 15:51:48 +01004007{
Jiri Olsae2484912012-02-15 15:51:48 +01004008 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4009 return;
4010
4011 /*
4012 * Some of the ops may be dynamically allocated,
4013 * they must be freed after a synchronize_sched().
4014 */
4015 preempt_disable_notrace();
4016 trace_recursion_set(TRACE_CONTROL_BIT);
4017 op = rcu_dereference_raw(ftrace_control_list);
4018 while (op != &ftrace_list_end) {
4019 if (!ftrace_function_local_disabled(op) &&
4020 ftrace_ops_test(op, ip))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004021 op->func(ip, parent_ip, op, regs);
Jiri Olsae2484912012-02-15 15:51:48 +01004022
4023 op = rcu_dereference_raw(op->next);
4024 };
4025 trace_recursion_clear(TRACE_CONTROL_BIT);
4026 preempt_enable_notrace();
4027}
4028
4029static struct ftrace_ops control_ops = {
4030 .func = ftrace_ops_control_func,
Steven Rostedt47409742012-07-20 11:04:44 -04004031 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Jiri Olsae2484912012-02-15 15:51:48 +01004032};
4033
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004034static inline void
4035__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004036 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004037{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004038 struct ftrace_ops *op;
Steven Rostedtb8489142011-05-04 09:27:52 -04004039
Steven Rostedtccf36722012-06-05 09:44:25 -04004040 if (function_trace_stop)
4041 return;
4042
Steven Rostedtb1cff0a2011-05-25 14:27:43 -04004043 if (unlikely(trace_recursion_test(TRACE_INTERNAL_BIT)))
4044 return;
4045
4046 trace_recursion_set(TRACE_INTERNAL_BIT);
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004047 /*
4048 * Some of the ops may be dynamically allocated,
4049 * they must be freed after a synchronize_sched().
4050 */
4051 preempt_disable_notrace();
4052 op = rcu_dereference_raw(ftrace_ops_list);
Steven Rostedtb8489142011-05-04 09:27:52 -04004053 while (op != &ftrace_list_end) {
4054 if (ftrace_ops_test(op, ip))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004055 op->func(ip, parent_ip, op, regs);
Steven Rostedtb8489142011-05-04 09:27:52 -04004056 op = rcu_dereference_raw(op->next);
4057 };
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004058 preempt_enable_notrace();
Steven Rostedtb1cff0a2011-05-25 14:27:43 -04004059 trace_recursion_clear(TRACE_INTERNAL_BIT);
Steven Rostedtb8489142011-05-04 09:27:52 -04004060}
4061
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004062/*
4063 * Some archs only support passing ip and parent_ip. Even though
4064 * the list function ignores the op parameter, we do not want any
4065 * C side effects, where a function is called without the caller
4066 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004067 * Archs are to support both the regs and ftrace_ops at the same time.
4068 * If they support ftrace_ops, it is assumed they support regs.
4069 * If call backs want to use regs, they must either check for regs
4070 * being NULL, or ARCH_SUPPORTS_FTRACE_SAVE_REGS.
4071 * Note, ARCH_SUPPORT_SAVE_REGS expects a full regs to be saved.
4072 * An architecture can pass partial regs with ftrace_ops and still
4073 * set the ARCH_SUPPORT_FTARCE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004074 */
4075#if ARCH_SUPPORTS_FTRACE_OPS
4076static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004077 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004078{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004079 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004080}
4081#else
4082static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4083{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004084 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004085}
4086#endif
4087
Steven Rostedte32d8952008-12-04 00:26:41 -05004088static void clear_ftrace_swapper(void)
4089{
4090 struct task_struct *p;
4091 int cpu;
4092
4093 get_online_cpus();
4094 for_each_online_cpu(cpu) {
4095 p = idle_task(cpu);
4096 clear_tsk_trace_trace(p);
4097 }
4098 put_online_cpus();
4099}
4100
4101static void set_ftrace_swapper(void)
4102{
4103 struct task_struct *p;
4104 int cpu;
4105
4106 get_online_cpus();
4107 for_each_online_cpu(cpu) {
4108 p = idle_task(cpu);
4109 set_tsk_trace_trace(p);
4110 }
4111 put_online_cpus();
4112}
4113
4114static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004115{
4116 struct task_struct *p;
4117
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004118 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05004119 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05004120 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05004121 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004122 rcu_read_unlock();
4123
Steven Rostedte32d8952008-12-04 00:26:41 -05004124 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004125}
4126
Steven Rostedte32d8952008-12-04 00:26:41 -05004127static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004128{
4129 struct task_struct *p;
4130
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004131 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004132 do_each_pid_task(pid, PIDTYPE_PID, p) {
4133 set_tsk_trace_trace(p);
4134 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004135 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004136}
4137
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004138static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004139{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004140 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004141 clear_ftrace_swapper();
4142 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004143 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05004144}
4145
4146static void set_ftrace_pid_task(struct pid *pid)
4147{
4148 if (pid == ftrace_swapper_pid)
4149 set_ftrace_swapper();
4150 else
4151 set_ftrace_pid(pid);
4152}
4153
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004154static int ftrace_pid_add(int p)
4155{
4156 struct pid *pid;
4157 struct ftrace_pid *fpid;
4158 int ret = -EINVAL;
4159
4160 mutex_lock(&ftrace_lock);
4161
4162 if (!p)
4163 pid = ftrace_swapper_pid;
4164 else
4165 pid = find_get_pid(p);
4166
4167 if (!pid)
4168 goto out;
4169
4170 ret = 0;
4171
4172 list_for_each_entry(fpid, &ftrace_pids, list)
4173 if (fpid->pid == pid)
4174 goto out_put;
4175
4176 ret = -ENOMEM;
4177
4178 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4179 if (!fpid)
4180 goto out_put;
4181
4182 list_add(&fpid->list, &ftrace_pids);
4183 fpid->pid = pid;
4184
4185 set_ftrace_pid_task(pid);
4186
4187 ftrace_update_pid_func();
4188 ftrace_startup_enable(0);
4189
4190 mutex_unlock(&ftrace_lock);
4191 return 0;
4192
4193out_put:
4194 if (pid != ftrace_swapper_pid)
4195 put_pid(pid);
4196
4197out:
4198 mutex_unlock(&ftrace_lock);
4199 return ret;
4200}
4201
4202static void ftrace_pid_reset(void)
4203{
4204 struct ftrace_pid *fpid, *safe;
4205
4206 mutex_lock(&ftrace_lock);
4207 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4208 struct pid *pid = fpid->pid;
4209
4210 clear_ftrace_pid_task(pid);
4211
4212 list_del(&fpid->list);
4213 kfree(fpid);
4214 }
4215
4216 ftrace_update_pid_func();
4217 ftrace_startup_enable(0);
4218
4219 mutex_unlock(&ftrace_lock);
4220}
4221
4222static void *fpid_start(struct seq_file *m, loff_t *pos)
4223{
4224 mutex_lock(&ftrace_lock);
4225
4226 if (list_empty(&ftrace_pids) && (!*pos))
4227 return (void *) 1;
4228
4229 return seq_list_start(&ftrace_pids, *pos);
4230}
4231
4232static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4233{
4234 if (v == (void *)1)
4235 return NULL;
4236
4237 return seq_list_next(v, &ftrace_pids, pos);
4238}
4239
4240static void fpid_stop(struct seq_file *m, void *p)
4241{
4242 mutex_unlock(&ftrace_lock);
4243}
4244
4245static int fpid_show(struct seq_file *m, void *v)
4246{
4247 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4248
4249 if (v == (void *)1) {
4250 seq_printf(m, "no pid\n");
4251 return 0;
4252 }
4253
4254 if (fpid->pid == ftrace_swapper_pid)
4255 seq_printf(m, "swapper tasks\n");
4256 else
4257 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4258
4259 return 0;
4260}
4261
4262static const struct seq_operations ftrace_pid_sops = {
4263 .start = fpid_start,
4264 .next = fpid_next,
4265 .stop = fpid_stop,
4266 .show = fpid_show,
4267};
4268
4269static int
4270ftrace_pid_open(struct inode *inode, struct file *file)
4271{
4272 int ret = 0;
4273
4274 if ((file->f_mode & FMODE_WRITE) &&
4275 (file->f_flags & O_TRUNC))
4276 ftrace_pid_reset();
4277
4278 if (file->f_mode & FMODE_READ)
4279 ret = seq_open(file, &ftrace_pid_sops);
4280
4281 return ret;
4282}
4283
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004284static ssize_t
4285ftrace_pid_write(struct file *filp, const char __user *ubuf,
4286 size_t cnt, loff_t *ppos)
4287{
Ingo Molnar457dc922009-11-23 11:03:28 +01004288 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004289 long val;
4290 int ret;
4291
4292 if (cnt >= sizeof(buf))
4293 return -EINVAL;
4294
4295 if (copy_from_user(&buf, ubuf, cnt))
4296 return -EFAULT;
4297
4298 buf[cnt] = 0;
4299
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004300 /*
4301 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4302 * to clean the filter quietly.
4303 */
Ingo Molnar457dc922009-11-23 11:03:28 +01004304 tmp = strstrip(buf);
4305 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004306 return 1;
4307
Ingo Molnar457dc922009-11-23 11:03:28 +01004308 ret = strict_strtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004309 if (ret < 0)
4310 return ret;
4311
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004312 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004313
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004314 return ret ? ret : cnt;
4315}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004316
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004317static int
4318ftrace_pid_release(struct inode *inode, struct file *file)
4319{
4320 if (file->f_mode & FMODE_READ)
4321 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004322
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004323 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004324}
4325
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004326static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004327 .open = ftrace_pid_open,
4328 .write = ftrace_pid_write,
4329 .read = seq_read,
4330 .llseek = seq_lseek,
4331 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004332};
4333
4334static __init int ftrace_init_debugfs(void)
4335{
4336 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004337
4338 d_tracer = tracing_init_dentry();
4339 if (!d_tracer)
4340 return 0;
4341
4342 ftrace_init_dyn_debugfs(d_tracer);
4343
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004344 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4345 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04004346
4347 ftrace_profile_debugfs(d_tracer);
4348
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004349 return 0;
4350}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004351fs_initcall(ftrace_init_debugfs);
4352
Steven Rostedt3d083392008-05-12 21:20:42 +02004353/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004354 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004355 *
4356 * This function should be used by panic code. It stops ftrace
4357 * but in a not so nice way. If you need to simply kill ftrace
4358 * from a non-atomic section, use ftrace_kill.
4359 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004360void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004361{
4362 ftrace_disabled = 1;
4363 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004364 clear_ftrace_function();
4365}
4366
4367/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04004368 * Test if ftrace is dead or not.
4369 */
4370int ftrace_is_dead(void)
4371{
4372 return ftrace_disabled;
4373}
4374
4375/**
Steven Rostedt3d083392008-05-12 21:20:42 +02004376 * register_ftrace_function - register a function for profiling
4377 * @ops - ops structure that holds the function for profiling.
4378 *
4379 * Register a function to be called by all functions in the
4380 * kernel.
4381 *
4382 * Note: @ops->func and all the functions it calls must be labeled
4383 * with "notrace", otherwise it will go into a
4384 * recursive loop.
4385 */
4386int register_ftrace_function(struct ftrace_ops *ops)
4387{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004388 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004389
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004390 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004391
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004392 ret = __register_ftrace_function(ops);
Steven Rostedtb8489142011-05-04 09:27:52 -04004393 if (!ret)
Steven Rostedta1cd6172011-05-23 15:24:25 -04004394 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04004395
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004396 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02004397
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004398 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02004399}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004400EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02004401
4402/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01004403 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02004404 * @ops - ops structure that holds the function to unregister
4405 *
4406 * Unregister a function that was added to be called by ftrace profiling.
4407 */
4408int unregister_ftrace_function(struct ftrace_ops *ops)
4409{
4410 int ret;
4411
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004412 mutex_lock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004413 ret = __unregister_ftrace_function(ops);
Steven Rostedtb8489142011-05-04 09:27:52 -04004414 if (!ret)
4415 ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004416 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004417
4418 return ret;
4419}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004420EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004421
Ingo Molnare309b412008-05-12 21:20:51 +02004422int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004423ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004424 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004425 loff_t *ppos)
4426{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004427 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004428
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004429 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004430
Steven Rostedt45a4a232011-04-21 23:16:46 -04004431 if (unlikely(ftrace_disabled))
4432 goto out;
4433
4434 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004435
Li Zefana32c7762009-06-26 16:55:51 +08004436 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004437 goto out;
4438
Li Zefana32c7762009-06-26 16:55:51 +08004439 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004440
4441 if (ftrace_enabled) {
4442
4443 ftrace_startup_sysctl();
4444
4445 /* we are starting ftrace again */
Steven Rostedtb8489142011-05-04 09:27:52 -04004446 if (ftrace_ops_list != &ftrace_list_end) {
4447 if (ftrace_ops_list->next == &ftrace_list_end)
4448 ftrace_trace_function = ftrace_ops_list->func;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004449 else
Steven Rostedtb8489142011-05-04 09:27:52 -04004450 ftrace_trace_function = ftrace_ops_list_func;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004451 }
4452
4453 } else {
4454 /* stopping ftrace calls (just send to ftrace_stub) */
4455 ftrace_trace_function = ftrace_stub;
4456
4457 ftrace_shutdown_sysctl();
4458 }
4459
4460 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004461 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004462 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02004463}
Ingo Molnarf17845e2008-10-24 12:47:10 +02004464
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004465#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004466
Steven Rostedt597af812009-04-03 15:24:12 -04004467static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004468static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004469
Steven Rostedte49dc192008-12-02 23:50:05 -05004470int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4471{
4472 return 0;
4473}
4474
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004475/* The callbacks that hook a function */
4476trace_func_graph_ret_t ftrace_graph_return =
4477 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004478trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004479
4480/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4481static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4482{
4483 int i;
4484 int ret = 0;
4485 unsigned long flags;
4486 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4487 struct task_struct *g, *t;
4488
4489 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4490 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4491 * sizeof(struct ftrace_ret_stack),
4492 GFP_KERNEL);
4493 if (!ret_stack_list[i]) {
4494 start = 0;
4495 end = i;
4496 ret = -ENOMEM;
4497 goto free;
4498 }
4499 }
4500
4501 read_lock_irqsave(&tasklist_lock, flags);
4502 do_each_thread(g, t) {
4503 if (start == end) {
4504 ret = -EAGAIN;
4505 goto unlock;
4506 }
4507
4508 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01004509 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004510 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04004511 t->curr_ret_stack = -1;
4512 /* Make sure the tasks see the -1 first: */
4513 smp_wmb();
4514 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004515 }
4516 } while_each_thread(g, t);
4517
4518unlock:
4519 read_unlock_irqrestore(&tasklist_lock, flags);
4520free:
4521 for (i = start; i < end; i++)
4522 kfree(ret_stack_list[i]);
4523 return ret;
4524}
4525
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004526static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04004527ftrace_graph_probe_sched_switch(void *ignore,
4528 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004529{
4530 unsigned long long timestamp;
4531 int index;
4532
Steven Rostedtbe6f1642009-03-24 11:06:24 -04004533 /*
4534 * Does the user want to count the time a function was asleep.
4535 * If so, do not update the time stamps.
4536 */
4537 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4538 return;
4539
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004540 timestamp = trace_clock_local();
4541
4542 prev->ftrace_timestamp = timestamp;
4543
4544 /* only process tasks that we timestamped */
4545 if (!next->ftrace_timestamp)
4546 return;
4547
4548 /*
4549 * Update all the counters in next to make up for the
4550 * time next was sleeping.
4551 */
4552 timestamp -= next->ftrace_timestamp;
4553
4554 for (index = next->curr_ret_stack; index >= 0; index--)
4555 next->ret_stack[index].calltime += timestamp;
4556}
4557
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004558/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004559static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004560{
4561 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004562 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004563
4564 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4565 sizeof(struct ftrace_ret_stack *),
4566 GFP_KERNEL);
4567
4568 if (!ret_stack_list)
4569 return -ENOMEM;
4570
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004571 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04004572 for_each_online_cpu(cpu) {
4573 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05004574 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04004575 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004576
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004577 do {
4578 ret = alloc_retstack_tasklist(ret_stack_list);
4579 } while (ret == -EAGAIN);
4580
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004581 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04004582 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004583 if (ret)
4584 pr_info("ftrace_graph: Couldn't activate tracepoint"
4585 " probe to kernel_sched_switch\n");
4586 }
4587
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004588 kfree(ret_stack_list);
4589 return ret;
4590}
4591
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004592/*
4593 * Hibernation protection.
4594 * The state of the current task is too much unstable during
4595 * suspend/restore to disk. We want to protect against that.
4596 */
4597static int
4598ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4599 void *unused)
4600{
4601 switch (state) {
4602 case PM_HIBERNATION_PREPARE:
4603 pause_graph_tracing();
4604 break;
4605
4606 case PM_POST_HIBERNATION:
4607 unpause_graph_tracing();
4608 break;
4609 }
4610 return NOTIFY_DONE;
4611}
4612
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004613int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4614 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004615{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004616 int ret = 0;
4617
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004618 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004619
Steven Rostedt05ce5812009-03-24 00:18:31 -04004620 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04004621 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04004622 ret = -EBUSY;
4623 goto out;
4624 }
4625
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004626 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
4627 register_pm_notifier(&ftrace_suspend_notifier);
4628
Steven Rostedt597af812009-04-03 15:24:12 -04004629 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004630 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004631 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04004632 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004633 goto out;
4634 }
Steven Rostedte53a6312008-11-26 00:16:25 -05004635
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004636 ftrace_graph_return = retfunc;
4637 ftrace_graph_entry = entryfunc;
Steven Rostedte53a6312008-11-26 00:16:25 -05004638
Steven Rostedta1cd6172011-05-23 15:24:25 -04004639 ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004640
4641out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004642 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004643 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004644}
4645
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004646void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004647{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004648 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004649
Steven Rostedt597af812009-04-03 15:24:12 -04004650 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004651 goto out;
4652
Steven Rostedt597af812009-04-03 15:24:12 -04004653 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004654 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004655 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedtbd69c302011-05-03 21:55:54 -04004656 ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004657 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04004658 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004659
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004660 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004661 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004662}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004663
Steven Rostedt868baf02011-02-10 21:26:13 -05004664static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
4665
4666static void
4667graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
4668{
4669 atomic_set(&t->tracing_graph_pause, 0);
4670 atomic_set(&t->trace_overrun, 0);
4671 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004672 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05004673 smp_wmb();
4674 t->ret_stack = ret_stack;
4675}
4676
4677/*
4678 * Allocate a return stack for the idle task. May be the first
4679 * time through, or it may be done by CPU hotplug online.
4680 */
4681void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
4682{
4683 t->curr_ret_stack = -1;
4684 /*
4685 * The idle task has no parent, it either has its own
4686 * stack or no stack at all.
4687 */
4688 if (t->ret_stack)
4689 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
4690
4691 if (ftrace_graph_active) {
4692 struct ftrace_ret_stack *ret_stack;
4693
4694 ret_stack = per_cpu(idle_ret_stack, cpu);
4695 if (!ret_stack) {
4696 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4697 * sizeof(struct ftrace_ret_stack),
4698 GFP_KERNEL);
4699 if (!ret_stack)
4700 return;
4701 per_cpu(idle_ret_stack, cpu) = ret_stack;
4702 }
4703 graph_init_task(t, ret_stack);
4704 }
4705}
4706
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004707/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004708void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004709{
Steven Rostedt84047e32009-06-02 16:51:55 -04004710 /* Make sure we do not use the parent ret_stack */
4711 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05004712 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04004713
Steven Rostedt597af812009-04-03 15:24:12 -04004714 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04004715 struct ftrace_ret_stack *ret_stack;
4716
4717 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004718 * sizeof(struct ftrace_ret_stack),
4719 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04004720 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004721 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05004722 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04004723 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004724}
4725
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004726void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004727{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004728 struct ftrace_ret_stack *ret_stack = t->ret_stack;
4729
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004730 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004731 /* NULL must become visible to IRQs before we free it: */
4732 barrier();
4733
4734 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004735}
Steven Rostedt14a866c2008-12-02 23:50:02 -05004736
4737void ftrace_graph_stop(void)
4738{
4739 ftrace_stop();
4740}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004741#endif