blob: 1330969d8447708edc6214446e18bbe1b0496518 [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
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +010013 * Copyright (C) 2004 Nadia Yvette Chambers
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020014 */
15
Steven Rostedt3d083392008-05-12 21:20:42 +020016#include <linux/stop_machine.h>
17#include <linux/clocksource.h>
18#include <linux/kallsyms.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020019#include <linux/seq_file.h>
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -080020#include <linux/suspend.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020021#include <linux/debugfs.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020022#include <linux/hardirq.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010023#include <linux/kthread.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020024#include <linux/uaccess.h>
Steven Rostedt5855fea2011-12-16 19:27:42 -050025#include <linux/bsearch.h>
Paul Gortmaker56d82e02011-05-26 17:53:52 -040026#include <linux/module.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010027#include <linux/ftrace.h>
Steven Rostedtb0fc4942008-05-12 21:20:43 +020028#include <linux/sysctl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020030#include <linux/ctype.h>
Steven Rostedt68950612011-12-16 17:06:45 -050031#include <linux/sort.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020032#include <linux/list.h>
Steven Rostedt59df055f2009-02-14 15:29:06 -050033#include <linux/hash.h>
Paul E. McKenney3f379b02010-03-05 15:03:25 -080034#include <linux/rcupdate.h>
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020035
Steven Rostedtad8d75f2009-04-14 19:39:12 -040036#include <trace/events/sched.h>
Steven Rostedt8aef2d22009-03-24 01:10:15 -040037
Steven Rostedt2af15d62009-05-28 13:37:24 -040038#include <asm/setup.h>
Abhishek Sagar395a59d2008-06-21 23:47:27 +053039
Steven Rostedt0706f1c2009-03-23 23:12:58 -040040#include "trace_output.h"
Steven Rostedtbac429f2009-03-20 12:50:56 -040041#include "trace_stat.h"
Steven Rostedt3d083392008-05-12 21:20:42 +020042
Steven Rostedt69128962008-10-23 09:33:03 -040043#define FTRACE_WARN_ON(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040044 ({ \
45 int ___r = cond; \
46 if (WARN_ON(___r)) \
Steven Rostedt69128962008-10-23 09:33:03 -040047 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040048 ___r; \
49 })
Steven Rostedt69128962008-10-23 09:33:03 -040050
51#define FTRACE_WARN_ON_ONCE(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040052 ({ \
53 int ___r = cond; \
54 if (WARN_ON_ONCE(___r)) \
Steven Rostedt69128962008-10-23 09:33:03 -040055 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040056 ___r; \
57 })
Steven Rostedt69128962008-10-23 09:33:03 -040058
Steven Rostedt8fc0c702009-02-16 15:28:00 -050059/* hash bits for specific function selection */
60#define FTRACE_HASH_BITS 7
61#define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
Steven Rostedt33dc9b12011-05-02 17:34:47 -040062#define FTRACE_HASH_DEFAULT_BITS 10
63#define FTRACE_HASH_MAX_BITS 12
Steven Rostedt8fc0c702009-02-16 15:28:00 -050064
Jiri Olsae2484912012-02-15 15:51:48 +010065#define FL_GLOBAL_CONTROL_MASK (FTRACE_OPS_FL_GLOBAL | FTRACE_OPS_FL_CONTROL)
66
Steven 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
Steven Rostedt0a016402012-11-02 17:03:03 -0400114/*
115 * Traverse the ftrace_global_list, invoking all entries. The reason that we
116 * 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
119 * concurrent insertions into the ftrace_global_list.
120 *
121 * Silly Alpha and silly pointer-speculation compiler optimizations!
122 */
123#define do_for_each_ftrace_op(op, list) \
124 op = rcu_dereference_raw(list); \
125 do
126
127/*
128 * Optimized for just a single item in the list (as that is the normal case).
129 */
130#define while_for_each_ftrace_op(op) \
131 while (likely(op = rcu_dereference_raw((op)->next)) && \
132 unlikely((op) != &ftrace_list_end))
133
Steven Rostedtea701f12012-07-20 13:08:05 -0400134/**
135 * ftrace_nr_registered_ops - return number of ops registered
136 *
137 * Returns the number of ftrace_ops registered and tracing functions
138 */
139int ftrace_nr_registered_ops(void)
140{
141 struct ftrace_ops *ops;
142 int cnt = 0;
143
144 mutex_lock(&ftrace_lock);
145
146 for (ops = ftrace_ops_list;
147 ops != &ftrace_list_end; ops = ops->next)
148 cnt++;
149
150 mutex_unlock(&ftrace_lock);
151
152 return cnt;
153}
154
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400155static void
156ftrace_global_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400157 struct ftrace_ops *op, struct pt_regs *regs)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200158{
Steven Rostedtb1cff0a2011-05-25 14:27:43 -0400159 if (unlikely(trace_recursion_test(TRACE_GLOBAL_BIT)))
160 return;
161
162 trace_recursion_set(TRACE_GLOBAL_BIT);
Steven Rostedt0a016402012-11-02 17:03:03 -0400163 do_for_each_ftrace_op(op, ftrace_global_list) {
Steven Rostedta1e2e312011-08-09 12:50:46 -0400164 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -0400165 } while_for_each_ftrace_op(op);
Steven Rostedtb1cff0a2011-05-25 14:27:43 -0400166 trace_recursion_clear(TRACE_GLOBAL_BIT);
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200167}
168
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400169static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400170 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500171{
Steven Rostedt0ef8cde2008-12-03 15:36:58 -0500172 if (!test_tsk_trace_trace(current))
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500173 return;
174
Steven Rostedta1e2e312011-08-09 12:50:46 -0400175 ftrace_pid_function(ip, parent_ip, op, regs);
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500176}
177
178static void set_ftrace_pid_function(ftrace_func_t func)
179{
180 /* do not set ftrace_pid_function to itself! */
181 if (func != ftrace_pid_func)
182 ftrace_pid_function = func;
183}
184
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200185/**
Steven Rostedt3d083392008-05-12 21:20:42 +0200186 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200187 *
Steven Rostedt3d083392008-05-12 21:20:42 +0200188 * This NULLs the ftrace function and in essence stops
189 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200190 */
Steven Rostedt3d083392008-05-12 21:20:42 +0200191void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200192{
Steven Rostedt3d083392008-05-12 21:20:42 +0200193 ftrace_trace_function = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500194 ftrace_pid_function = ftrace_stub;
Steven Rostedt3d083392008-05-12 21:20:42 +0200195}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200196
Jiri Olsae2484912012-02-15 15:51:48 +0100197static void control_ops_disable_all(struct ftrace_ops *ops)
198{
199 int cpu;
200
201 for_each_possible_cpu(cpu)
202 *per_cpu_ptr(ops->disabled, cpu) = 1;
203}
204
205static int control_ops_alloc(struct ftrace_ops *ops)
206{
207 int __percpu *disabled;
208
209 disabled = alloc_percpu(int);
210 if (!disabled)
211 return -ENOMEM;
212
213 ops->disabled = disabled;
214 control_ops_disable_all(ops);
215 return 0;
216}
217
218static void control_ops_free(struct ftrace_ops *ops)
219{
220 free_percpu(ops->disabled);
221}
222
Steven Rostedt2b499382011-05-03 22:49:52 -0400223static void update_global_ops(void)
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400224{
225 ftrace_func_t func;
226
227 /*
228 * If there's only one function registered, then call that
229 * function directly. Otherwise, we need to iterate over the
230 * registered callers.
231 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400232 if (ftrace_global_list == &ftrace_list_end ||
Steven Rostedt63503792012-11-02 16:58:56 -0400233 ftrace_global_list->next == &ftrace_list_end) {
Steven Rostedtb8489142011-05-04 09:27:52 -0400234 func = ftrace_global_list->func;
Steven Rostedt63503792012-11-02 16:58:56 -0400235 /*
236 * As we are calling the function directly.
237 * If it does not have recursion protection,
238 * the function_trace_op needs to be updated
239 * accordingly.
240 */
241 if (ftrace_global_list->flags & FTRACE_OPS_FL_RECURSION_SAFE)
242 global_ops.flags |= FTRACE_OPS_FL_RECURSION_SAFE;
243 else
244 global_ops.flags &= ~FTRACE_OPS_FL_RECURSION_SAFE;
245 } else {
Steven Rostedtb8489142011-05-04 09:27:52 -0400246 func = ftrace_global_list_func;
Steven Rostedt63503792012-11-02 16:58:56 -0400247 /* The list has its own recursion protection. */
248 global_ops.flags |= FTRACE_OPS_FL_RECURSION_SAFE;
249 }
250
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400251
252 /* If we filter on pids, update to use the pid function */
253 if (!list_empty(&ftrace_pids)) {
254 set_ftrace_pid_function(func);
255 func = ftrace_pid_func;
256 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400257
258 global_ops.func = func;
259}
260
261static void update_ftrace_function(void)
262{
263 ftrace_func_t func;
264
265 update_global_ops();
266
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400267 /*
268 * If we are at the end of the list and this ops is
Steven Rostedt47409742012-07-20 11:04:44 -0400269 * recursion safe and not dynamic and the arch supports passing ops,
270 * then have the mcount trampoline call the function directly.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400271 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400272 if (ftrace_ops_list == &ftrace_list_end ||
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400273 (ftrace_ops_list->next == &ftrace_list_end &&
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400274 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC) &&
Steven Rostedt47409742012-07-20 11:04:44 -0400275 (ftrace_ops_list->flags & FTRACE_OPS_FL_RECURSION_SAFE) &&
Steven Rostedtccf36722012-06-05 09:44:25 -0400276 !FTRACE_FORCE_LIST_FUNC)) {
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400277 /* Set the ftrace_ops that the arch callback uses */
278 if (ftrace_ops_list == &global_ops)
279 function_trace_op = ftrace_global_list;
280 else
281 function_trace_op = ftrace_ops_list;
Steven Rostedtb8489142011-05-04 09:27:52 -0400282 func = ftrace_ops_list->func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400283 } else {
284 /* Just use the default ftrace_ops */
285 function_trace_op = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400286 func = ftrace_ops_list_func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400287 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400288
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400289 ftrace_trace_function = func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400290}
291
Steven Rostedt2b499382011-05-03 22:49:52 -0400292static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200293{
Steven Rostedt2b499382011-05-03 22:49:52 -0400294 ops->next = *list;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200295 /*
Steven Rostedtb8489142011-05-04 09:27:52 -0400296 * We are entering ops into the list but another
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200297 * CPU might be walking that list. We need to make sure
298 * the ops->next pointer is valid before another CPU sees
Steven Rostedtb8489142011-05-04 09:27:52 -0400299 * the ops pointer included into the list.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200300 */
Steven Rostedt2b499382011-05-03 22:49:52 -0400301 rcu_assign_pointer(*list, ops);
302}
Steven Rostedt3d083392008-05-12 21:20:42 +0200303
Steven Rostedt2b499382011-05-03 22:49:52 -0400304static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
305{
306 struct ftrace_ops **p;
307
308 /*
309 * If we are removing the last function, then simply point
310 * to the ftrace_stub.
311 */
312 if (*list == ops && ops->next == &ftrace_list_end) {
313 *list = &ftrace_list_end;
314 return 0;
315 }
316
317 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
318 if (*p == ops)
319 break;
320
321 if (*p != ops)
322 return -1;
323
324 *p = (*p)->next;
325 return 0;
326}
327
Jiri Olsae2484912012-02-15 15:51:48 +0100328static void add_ftrace_list_ops(struct ftrace_ops **list,
329 struct ftrace_ops *main_ops,
330 struct ftrace_ops *ops)
331{
332 int first = *list == &ftrace_list_end;
333 add_ftrace_ops(list, ops);
334 if (first)
335 add_ftrace_ops(&ftrace_ops_list, main_ops);
336}
337
338static int remove_ftrace_list_ops(struct ftrace_ops **list,
339 struct ftrace_ops *main_ops,
340 struct ftrace_ops *ops)
341{
342 int ret = remove_ftrace_ops(list, ops);
343 if (!ret && *list == &ftrace_list_end)
344 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
345 return ret;
346}
347
Steven Rostedt2b499382011-05-03 22:49:52 -0400348static int __register_ftrace_function(struct ftrace_ops *ops)
349{
Borislav Petkov8d240dd2012-03-29 19:11:40 +0200350 if (unlikely(ftrace_disabled))
Steven Rostedt2b499382011-05-03 22:49:52 -0400351 return -ENODEV;
352
353 if (FTRACE_WARN_ON(ops == &global_ops))
354 return -EINVAL;
355
Steven Rostedtb8489142011-05-04 09:27:52 -0400356 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
357 return -EBUSY;
358
Jiri Olsae2484912012-02-15 15:51:48 +0100359 /* We don't support both control and global flags set. */
360 if ((ops->flags & FL_GLOBAL_CONTROL_MASK) == FL_GLOBAL_CONTROL_MASK)
361 return -EINVAL;
362
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +0900363#ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
Steven Rostedt08f6fba2012-04-30 16:20:23 -0400364 /*
365 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
366 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
367 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
368 */
369 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
370 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
371 return -EINVAL;
372
373 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
374 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
375#endif
376
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400377 if (!core_kernel_data((unsigned long)ops))
378 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
379
Steven Rostedtb8489142011-05-04 09:27:52 -0400380 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100381 add_ftrace_list_ops(&ftrace_global_list, &global_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400382 ops->flags |= FTRACE_OPS_FL_ENABLED;
Jiri Olsae2484912012-02-15 15:51:48 +0100383 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
384 if (control_ops_alloc(ops))
385 return -ENOMEM;
386 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400387 } else
388 add_ftrace_ops(&ftrace_ops_list, ops);
389
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400390 if (ftrace_enabled)
391 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200392
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200393 return 0;
394}
395
Ingo Molnare309b412008-05-12 21:20:51 +0200396static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200397{
Steven Rostedt2b499382011-05-03 22:49:52 -0400398 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200399
Steven Rostedt2b499382011-05-03 22:49:52 -0400400 if (ftrace_disabled)
401 return -ENODEV;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200402
Steven Rostedtb8489142011-05-04 09:27:52 -0400403 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
404 return -EBUSY;
405
Steven Rostedt2b499382011-05-03 22:49:52 -0400406 if (FTRACE_WARN_ON(ops == &global_ops))
407 return -EINVAL;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200408
Steven Rostedtb8489142011-05-04 09:27:52 -0400409 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100410 ret = remove_ftrace_list_ops(&ftrace_global_list,
411 &global_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400412 if (!ret)
413 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Jiri Olsae2484912012-02-15 15:51:48 +0100414 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
415 ret = remove_ftrace_list_ops(&ftrace_control_list,
416 &control_ops, ops);
417 if (!ret) {
418 /*
419 * The ftrace_ops is now removed from the list,
420 * so there'll be no new users. We must ensure
421 * all current users are done before we free
422 * the control data.
423 */
424 synchronize_sched();
425 control_ops_free(ops);
426 }
Steven Rostedtb8489142011-05-04 09:27:52 -0400427 } else
428 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
429
Steven Rostedt2b499382011-05-03 22:49:52 -0400430 if (ret < 0)
431 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400432
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400433 if (ftrace_enabled)
434 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200435
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400436 /*
437 * Dynamic ops may be freed, we must make sure that all
438 * callers are done before leaving this function.
439 */
440 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
441 synchronize_sched();
442
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500443 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200444}
445
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500446static void ftrace_update_pid_func(void)
447{
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400448 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500449 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900450 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500451
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400452 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500453}
454
Steven Rostedt493762f2009-03-23 17:12:36 -0400455#ifdef CONFIG_FUNCTION_PROFILER
456struct ftrace_profile {
457 struct hlist_node node;
458 unsigned long ip;
459 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400460#ifdef CONFIG_FUNCTION_GRAPH_TRACER
461 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400462 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400463#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400464};
465
466struct ftrace_profile_page {
467 struct ftrace_profile_page *next;
468 unsigned long index;
469 struct ftrace_profile records[];
470};
471
Steven Rostedtcafb1682009-03-24 20:50:39 -0400472struct ftrace_profile_stat {
473 atomic_t disabled;
474 struct hlist_head *hash;
475 struct ftrace_profile_page *pages;
476 struct ftrace_profile_page *start;
477 struct tracer_stat stat;
478};
479
Steven Rostedt493762f2009-03-23 17:12:36 -0400480#define PROFILE_RECORDS_SIZE \
481 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
482
483#define PROFILES_PER_PAGE \
484 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
485
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400486static int ftrace_profile_bits __read_mostly;
487static int ftrace_profile_enabled __read_mostly;
488
489/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400490static DEFINE_MUTEX(ftrace_profile_lock);
491
Steven Rostedtcafb1682009-03-24 20:50:39 -0400492static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400493
494#define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
495
Steven Rostedt493762f2009-03-23 17:12:36 -0400496static void *
497function_stat_next(void *v, int idx)
498{
499 struct ftrace_profile *rec = v;
500 struct ftrace_profile_page *pg;
501
502 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
503
504 again:
Li Zefan0296e422009-06-26 11:15:37 +0800505 if (idx != 0)
506 rec++;
507
Steven Rostedt493762f2009-03-23 17:12:36 -0400508 if ((void *)rec >= (void *)&pg->records[pg->index]) {
509 pg = pg->next;
510 if (!pg)
511 return NULL;
512 rec = &pg->records[0];
513 if (!rec->counter)
514 goto again;
515 }
516
517 return rec;
518}
519
520static void *function_stat_start(struct tracer_stat *trace)
521{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400522 struct ftrace_profile_stat *stat =
523 container_of(trace, struct ftrace_profile_stat, stat);
524
525 if (!stat || !stat->start)
526 return NULL;
527
528 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400529}
530
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400531#ifdef CONFIG_FUNCTION_GRAPH_TRACER
532/* function graph compares on total time */
533static int function_stat_cmp(void *p1, void *p2)
534{
535 struct ftrace_profile *a = p1;
536 struct ftrace_profile *b = p2;
537
538 if (a->time < b->time)
539 return -1;
540 if (a->time > b->time)
541 return 1;
542 else
543 return 0;
544}
545#else
546/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400547static int function_stat_cmp(void *p1, void *p2)
548{
549 struct ftrace_profile *a = p1;
550 struct ftrace_profile *b = p2;
551
552 if (a->counter < b->counter)
553 return -1;
554 if (a->counter > b->counter)
555 return 1;
556 else
557 return 0;
558}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400559#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400560
561static int function_stat_headers(struct seq_file *m)
562{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400563#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400564 seq_printf(m, " Function "
Chase Douglase330b3b2010-04-26 14:02:05 -0400565 "Hit Time Avg s^2\n"
Steven Rostedt34886c82009-03-25 21:00:47 -0400566 " -------- "
Chase Douglase330b3b2010-04-26 14:02:05 -0400567 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400568#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400569 seq_printf(m, " Function Hit\n"
570 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400571#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400572 return 0;
573}
574
575static int function_stat_show(struct seq_file *m, void *v)
576{
577 struct ftrace_profile *rec = v;
578 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800579 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400580#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400581 static struct trace_seq s;
582 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400583 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400584#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800585 mutex_lock(&ftrace_profile_lock);
586
587 /* we raced with function_profile_reset() */
588 if (unlikely(rec->counter == 0)) {
589 ret = -EBUSY;
590 goto out;
591 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400592
593 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400594 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400595
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400596#ifdef CONFIG_FUNCTION_GRAPH_TRACER
597 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400598 avg = rec->time;
599 do_div(avg, rec->counter);
600
Chase Douglase330b3b2010-04-26 14:02:05 -0400601 /* Sample standard deviation (s^2) */
602 if (rec->counter <= 1)
603 stddev = 0;
604 else {
605 stddev = rec->time_squared - rec->counter * avg * avg;
606 /*
607 * Divide only 1000 for ns^2 -> us^2 conversion.
608 * trace_print_graph_duration will divide 1000 again.
609 */
610 do_div(stddev, (rec->counter - 1) * 1000);
611 }
612
Steven Rostedt34886c82009-03-25 21:00:47 -0400613 trace_seq_init(&s);
614 trace_print_graph_duration(rec->time, &s);
615 trace_seq_puts(&s, " ");
616 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400617 trace_seq_puts(&s, " ");
618 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400619 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400620#endif
621 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800622out:
623 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400624
Li Zefan3aaba202010-08-23 16:50:12 +0800625 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400626}
627
Steven Rostedtcafb1682009-03-24 20:50:39 -0400628static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400629{
630 struct ftrace_profile_page *pg;
631
Steven Rostedtcafb1682009-03-24 20:50:39 -0400632 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400633
634 while (pg) {
635 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
636 pg->index = 0;
637 pg = pg->next;
638 }
639
Steven Rostedtcafb1682009-03-24 20:50:39 -0400640 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400641 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
642}
643
Steven Rostedtcafb1682009-03-24 20:50:39 -0400644int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400645{
646 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400647 int functions;
648 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400649 int i;
650
651 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400652 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400653 return 0;
654
Steven Rostedtcafb1682009-03-24 20:50:39 -0400655 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
656 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400657 return -ENOMEM;
658
Steven Rostedt318e0a72009-03-25 20:06:34 -0400659#ifdef CONFIG_DYNAMIC_FTRACE
660 functions = ftrace_update_tot_cnt;
661#else
662 /*
663 * We do not know the number of functions that exist because
664 * dynamic tracing is what counts them. With past experience
665 * we have around 20K functions. That should be more than enough.
666 * It is highly unlikely we will execute every function in
667 * the kernel.
668 */
669 functions = 20000;
670#endif
671
Steven Rostedtcafb1682009-03-24 20:50:39 -0400672 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400673
Steven Rostedt318e0a72009-03-25 20:06:34 -0400674 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
675
676 for (i = 0; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400677 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400678 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400679 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400680 pg = pg->next;
681 }
682
683 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400684
685 out_free:
686 pg = stat->start;
687 while (pg) {
688 unsigned long tmp = (unsigned long)pg;
689
690 pg = pg->next;
691 free_page(tmp);
692 }
693
694 free_page((unsigned long)stat->pages);
695 stat->pages = NULL;
696 stat->start = NULL;
697
698 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400699}
700
Steven Rostedtcafb1682009-03-24 20:50:39 -0400701static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400702{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400703 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400704 int size;
705
Steven Rostedtcafb1682009-03-24 20:50:39 -0400706 stat = &per_cpu(ftrace_profile_stats, cpu);
707
708 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400709 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400710 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400711 return 0;
712 }
713
714 /*
715 * We are profiling all functions, but usually only a few thousand
716 * functions are hit. We'll make a hash of 1024 items.
717 */
718 size = FTRACE_PROFILE_HASH_SIZE;
719
Steven Rostedtcafb1682009-03-24 20:50:39 -0400720 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400721
Steven Rostedtcafb1682009-03-24 20:50:39 -0400722 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400723 return -ENOMEM;
724
Steven Rostedtcafb1682009-03-24 20:50:39 -0400725 if (!ftrace_profile_bits) {
726 size--;
Steven Rostedt493762f2009-03-23 17:12:36 -0400727
Steven Rostedtcafb1682009-03-24 20:50:39 -0400728 for (; size; size >>= 1)
729 ftrace_profile_bits++;
730 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400731
Steven Rostedt318e0a72009-03-25 20:06:34 -0400732 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400733 if (ftrace_profile_pages_init(stat) < 0) {
734 kfree(stat->hash);
735 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400736 return -ENOMEM;
737 }
738
739 return 0;
740}
741
Steven Rostedtcafb1682009-03-24 20:50:39 -0400742static int ftrace_profile_init(void)
743{
744 int cpu;
745 int ret = 0;
746
747 for_each_online_cpu(cpu) {
748 ret = ftrace_profile_init_cpu(cpu);
749 if (ret)
750 break;
751 }
752
753 return ret;
754}
755
Steven Rostedt493762f2009-03-23 17:12:36 -0400756/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400757static struct ftrace_profile *
758ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400759{
760 struct ftrace_profile *rec;
761 struct hlist_head *hhd;
762 struct hlist_node *n;
763 unsigned long key;
764
765 key = hash_long(ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400766 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400767
768 if (hlist_empty(hhd))
769 return NULL;
770
771 hlist_for_each_entry_rcu(rec, n, hhd, node) {
772 if (rec->ip == ip)
773 return rec;
774 }
775
776 return NULL;
777}
778
Steven Rostedtcafb1682009-03-24 20:50:39 -0400779static void ftrace_add_profile(struct ftrace_profile_stat *stat,
780 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400781{
782 unsigned long key;
783
784 key = hash_long(rec->ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400785 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400786}
787
Steven Rostedt318e0a72009-03-25 20:06:34 -0400788/*
789 * The memory is already allocated, this simply finds a new record to use.
790 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400791static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400792ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400793{
794 struct ftrace_profile *rec = NULL;
795
Steven Rostedt318e0a72009-03-25 20:06:34 -0400796 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400797 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400798 goto out;
799
Steven Rostedt493762f2009-03-23 17:12:36 -0400800 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400801 * Try to find the function again since an NMI
802 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400803 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400804 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400805 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400806 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400807
Steven Rostedtcafb1682009-03-24 20:50:39 -0400808 if (stat->pages->index == PROFILES_PER_PAGE) {
809 if (!stat->pages->next)
810 goto out;
811 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400812 }
813
Steven Rostedtcafb1682009-03-24 20:50:39 -0400814 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400815 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400816 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400817
Steven Rostedt493762f2009-03-23 17:12:36 -0400818 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400819 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400820
821 return rec;
822}
823
Steven Rostedt493762f2009-03-23 17:12:36 -0400824static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400825function_profile_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400826 struct ftrace_ops *ops, struct pt_regs *regs)
Steven Rostedt493762f2009-03-23 17:12:36 -0400827{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400828 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400829 struct ftrace_profile *rec;
830 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400831
832 if (!ftrace_profile_enabled)
833 return;
834
Steven Rostedt493762f2009-03-23 17:12:36 -0400835 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400836
837 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400838 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400839 goto out;
840
841 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400842 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400843 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400844 if (!rec)
845 goto out;
846 }
847
848 rec->counter++;
849 out:
850 local_irq_restore(flags);
851}
852
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400853#ifdef CONFIG_FUNCTION_GRAPH_TRACER
854static int profile_graph_entry(struct ftrace_graph_ent *trace)
855{
Steven Rostedta1e2e312011-08-09 12:50:46 -0400856 function_profile_call(trace->func, 0, NULL, NULL);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400857 return 1;
858}
859
860static void profile_graph_return(struct ftrace_graph_ret *trace)
861{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400862 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400863 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400864 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400865 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400866
867 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400868 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400869 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400870 goto out;
871
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400872 /* If the calltime was zero'd ignore it */
873 if (!trace->calltime)
874 goto out;
875
Steven Rostedta2a16d62009-03-24 23:17:58 -0400876 calltime = trace->rettime - trace->calltime;
877
878 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
879 int index;
880
881 index = trace->depth;
882
883 /* Append this call time to the parent time to subtract */
884 if (index)
885 current->ret_stack[index - 1].subtime += calltime;
886
887 if (current->ret_stack[index].subtime < calltime)
888 calltime -= current->ret_stack[index].subtime;
889 else
890 calltime = 0;
891 }
892
Steven Rostedtcafb1682009-03-24 20:50:39 -0400893 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400894 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400895 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400896 rec->time_squared += calltime * calltime;
897 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400898
Steven Rostedtcafb1682009-03-24 20:50:39 -0400899 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400900 local_irq_restore(flags);
901}
902
903static int register_ftrace_profiler(void)
904{
905 return register_ftrace_graph(&profile_graph_return,
906 &profile_graph_entry);
907}
908
909static void unregister_ftrace_profiler(void)
910{
911 unregister_ftrace_graph();
912}
913#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100914static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400915 .func = function_profile_call,
Steven Rostedt47409742012-07-20 11:04:44 -0400916 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Steven Rostedt493762f2009-03-23 17:12:36 -0400917};
918
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400919static int register_ftrace_profiler(void)
920{
921 return register_ftrace_function(&ftrace_profile_ops);
922}
923
924static void unregister_ftrace_profiler(void)
925{
926 unregister_ftrace_function(&ftrace_profile_ops);
927}
928#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
929
Steven Rostedt493762f2009-03-23 17:12:36 -0400930static ssize_t
931ftrace_profile_write(struct file *filp, const char __user *ubuf,
932 size_t cnt, loff_t *ppos)
933{
934 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400935 int ret;
936
Peter Huewe22fe9b52011-06-07 21:58:27 +0200937 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
938 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400939 return ret;
940
941 val = !!val;
942
943 mutex_lock(&ftrace_profile_lock);
944 if (ftrace_profile_enabled ^ val) {
945 if (val) {
946 ret = ftrace_profile_init();
947 if (ret < 0) {
948 cnt = ret;
949 goto out;
950 }
951
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400952 ret = register_ftrace_profiler();
953 if (ret < 0) {
954 cnt = ret;
955 goto out;
956 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400957 ftrace_profile_enabled = 1;
958 } else {
959 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400960 /*
961 * unregister_ftrace_profiler calls stop_machine
962 * so this acts like an synchronize_sched.
963 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400964 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400965 }
966 }
967 out:
968 mutex_unlock(&ftrace_profile_lock);
969
Jiri Olsacf8517c2009-10-23 19:36:16 -0400970 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400971
972 return cnt;
973}
974
975static ssize_t
976ftrace_profile_read(struct file *filp, char __user *ubuf,
977 size_t cnt, loff_t *ppos)
978{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400979 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400980 int r;
981
982 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
983 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
984}
985
986static const struct file_operations ftrace_profile_fops = {
987 .open = tracing_open_generic,
988 .read = ftrace_profile_read,
989 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200990 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -0400991};
992
Steven Rostedtcafb1682009-03-24 20:50:39 -0400993/* used to initialize the real stat files */
994static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400995 .name = "functions",
996 .stat_start = function_stat_start,
997 .stat_next = function_stat_next,
998 .stat_cmp = function_stat_cmp,
999 .stat_headers = function_stat_headers,
1000 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -04001001};
1002
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{
Steven Rostedtcafb1682009-03-24 20:50:39 -04001005 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -04001006 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001007 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -04001008 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001009 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -04001010
Steven Rostedtcafb1682009-03-24 20:50:39 -04001011 for_each_possible_cpu(cpu) {
1012 stat = &per_cpu(ftrace_profile_stats, cpu);
1013
1014 /* allocate enough for function name + cpu number */
1015 name = kmalloc(32, GFP_KERNEL);
1016 if (!name) {
1017 /*
1018 * The files created are permanent, if something happens
1019 * we still do not free memory.
1020 */
Steven Rostedtcafb1682009-03-24 20:50:39 -04001021 WARN(1,
1022 "Could not allocate stat file for cpu %d\n",
1023 cpu);
1024 return;
1025 }
1026 stat->stat = function_stats;
1027 snprintf(name, 32, "function%d", cpu);
1028 stat->stat.name = name;
1029 ret = register_stat_tracer(&stat->stat);
1030 if (ret) {
1031 WARN(1,
1032 "Could not register function stat for cpu %d\n",
1033 cpu);
1034 kfree(name);
1035 return;
1036 }
Steven Rostedt493762f2009-03-23 17:12:36 -04001037 }
1038
1039 entry = debugfs_create_file("function_profile_enabled", 0644,
1040 d_tracer, NULL, &ftrace_profile_fops);
1041 if (!entry)
1042 pr_warning("Could not create debugfs "
1043 "'function_profile_enabled' entry\n");
1044}
1045
1046#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001047static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001048{
1049}
1050#endif /* CONFIG_FUNCTION_PROFILER */
1051
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001052static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1053
Steven Rostedt3d083392008-05-12 21:20:42 +02001054#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001055
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001056#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001057# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001058#endif
1059
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001060static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1061
Steven Rostedtb6887d72009-02-17 12:32:04 -05001062struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001063 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -05001064 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001065 unsigned long flags;
1066 unsigned long ip;
1067 void *data;
1068 struct rcu_head rcu;
1069};
1070
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001071struct ftrace_func_entry {
1072 struct hlist_node hlist;
1073 unsigned long ip;
1074};
1075
1076struct ftrace_hash {
1077 unsigned long size_bits;
1078 struct hlist_head *buckets;
1079 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001080 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001081};
1082
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001083/*
1084 * We make these constant because no one should touch them,
1085 * but they are used as the default "empty hash", to avoid allocating
1086 * it all the time. These are in a read only section such that if
1087 * anyone does try to modify it, it will cause an exception.
1088 */
1089static const struct hlist_head empty_buckets[1];
1090static const struct ftrace_hash empty_hash = {
1091 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001092};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001093#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001094
Steven Rostedt2b499382011-05-03 22:49:52 -04001095static struct ftrace_ops global_ops = {
Steven Rostedtf45948e2011-05-02 12:29:25 -04001096 .func = ftrace_stub,
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001097 .notrace_hash = EMPTY_HASH,
1098 .filter_hash = EMPTY_HASH,
Steven Rostedt47409742012-07-20 11:04:44 -04001099 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Steven Rostedtf45948e2011-05-02 12:29:25 -04001100};
1101
Steven Rostedt41c52c02008-05-22 11:46:33 -04001102static DEFINE_MUTEX(ftrace_regex_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02001103
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001104struct ftrace_page {
1105 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001106 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001107 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001108 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001109};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001110
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001111static struct ftrace_page *ftrace_new_pgs;
1112
Steven Rostedta7900872011-12-16 16:23:44 -05001113#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1114#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001115
1116/* estimate from running different kernels */
1117#define NR_TO_INIT 10000
1118
1119static struct ftrace_page *ftrace_pages_start;
1120static struct ftrace_page *ftrace_pages;
1121
Steven Rostedt06a51d92011-12-19 19:07:36 -05001122static bool ftrace_hash_empty(struct ftrace_hash *hash)
1123{
1124 return !hash || !hash->count;
1125}
1126
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001127static struct ftrace_func_entry *
1128ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1129{
1130 unsigned long key;
1131 struct ftrace_func_entry *entry;
1132 struct hlist_head *hhd;
1133 struct hlist_node *n;
1134
Steven Rostedt06a51d92011-12-19 19:07:36 -05001135 if (ftrace_hash_empty(hash))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001136 return NULL;
1137
1138 if (hash->size_bits > 0)
1139 key = hash_long(ip, hash->size_bits);
1140 else
1141 key = 0;
1142
1143 hhd = &hash->buckets[key];
1144
1145 hlist_for_each_entry_rcu(entry, n, hhd, hlist) {
1146 if (entry->ip == ip)
1147 return entry;
1148 }
1149 return NULL;
1150}
1151
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001152static void __add_hash_entry(struct ftrace_hash *hash,
1153 struct ftrace_func_entry *entry)
1154{
1155 struct hlist_head *hhd;
1156 unsigned long key;
1157
1158 if (hash->size_bits)
1159 key = hash_long(entry->ip, hash->size_bits);
1160 else
1161 key = 0;
1162
1163 hhd = &hash->buckets[key];
1164 hlist_add_head(&entry->hlist, hhd);
1165 hash->count++;
1166}
1167
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001168static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1169{
1170 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001171
1172 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1173 if (!entry)
1174 return -ENOMEM;
1175
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001176 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001177 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001178
1179 return 0;
1180}
1181
1182static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001183free_hash_entry(struct ftrace_hash *hash,
1184 struct ftrace_func_entry *entry)
1185{
1186 hlist_del(&entry->hlist);
1187 kfree(entry);
1188 hash->count--;
1189}
1190
1191static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001192remove_hash_entry(struct ftrace_hash *hash,
1193 struct ftrace_func_entry *entry)
1194{
1195 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001196 hash->count--;
1197}
1198
1199static void ftrace_hash_clear(struct ftrace_hash *hash)
1200{
1201 struct hlist_head *hhd;
1202 struct hlist_node *tp, *tn;
1203 struct ftrace_func_entry *entry;
1204 int size = 1 << hash->size_bits;
1205 int i;
1206
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001207 if (!hash->count)
1208 return;
1209
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001210 for (i = 0; i < size; i++) {
1211 hhd = &hash->buckets[i];
1212 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001213 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001214 }
1215 FTRACE_WARN_ON(hash->count);
1216}
1217
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001218static void free_ftrace_hash(struct ftrace_hash *hash)
1219{
1220 if (!hash || hash == EMPTY_HASH)
1221 return;
1222 ftrace_hash_clear(hash);
1223 kfree(hash->buckets);
1224 kfree(hash);
1225}
1226
Steven Rostedt07fd5512011-05-05 18:03:47 -04001227static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1228{
1229 struct ftrace_hash *hash;
1230
1231 hash = container_of(rcu, struct ftrace_hash, rcu);
1232 free_ftrace_hash(hash);
1233}
1234
1235static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1236{
1237 if (!hash || hash == EMPTY_HASH)
1238 return;
1239 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1240}
1241
Jiri Olsa5500fa52012-02-15 15:51:54 +01001242void ftrace_free_filter(struct ftrace_ops *ops)
1243{
1244 free_ftrace_hash(ops->filter_hash);
1245 free_ftrace_hash(ops->notrace_hash);
1246}
1247
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001248static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1249{
1250 struct ftrace_hash *hash;
1251 int size;
1252
1253 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1254 if (!hash)
1255 return NULL;
1256
1257 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001258 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001259
1260 if (!hash->buckets) {
1261 kfree(hash);
1262 return NULL;
1263 }
1264
1265 hash->size_bits = size_bits;
1266
1267 return hash;
1268}
1269
1270static struct ftrace_hash *
1271alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1272{
1273 struct ftrace_func_entry *entry;
1274 struct ftrace_hash *new_hash;
1275 struct hlist_node *tp;
1276 int size;
1277 int ret;
1278 int i;
1279
1280 new_hash = alloc_ftrace_hash(size_bits);
1281 if (!new_hash)
1282 return NULL;
1283
1284 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001285 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001286 return new_hash;
1287
1288 size = 1 << hash->size_bits;
1289 for (i = 0; i < size; i++) {
1290 hlist_for_each_entry(entry, tp, &hash->buckets[i], hlist) {
1291 ret = add_hash_entry(new_hash, entry->ip);
1292 if (ret < 0)
1293 goto free_hash;
1294 }
1295 }
1296
1297 FTRACE_WARN_ON(new_hash->count != hash->count);
1298
1299 return new_hash;
1300
1301 free_hash:
1302 free_ftrace_hash(new_hash);
1303 return NULL;
1304}
1305
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001306static void
1307ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1308static void
1309ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1310
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001311static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001312ftrace_hash_move(struct ftrace_ops *ops, int enable,
1313 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001314{
1315 struct ftrace_func_entry *entry;
1316 struct hlist_node *tp, *tn;
1317 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001318 struct ftrace_hash *old_hash;
1319 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001320 unsigned long key;
1321 int size = src->count;
1322 int bits = 0;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001323 int ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001324 int i;
1325
1326 /*
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001327 * Remove the current set, update the hash and add
1328 * them back.
1329 */
1330 ftrace_hash_rec_disable(ops, enable);
1331
1332 /*
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001333 * If the new source is empty, just free dst and assign it
1334 * the empty_hash.
1335 */
1336 if (!src->count) {
Steven Rostedt07fd5512011-05-05 18:03:47 -04001337 free_ftrace_hash_rcu(*dst);
1338 rcu_assign_pointer(*dst, EMPTY_HASH);
Steven Rostedtd4d34b92011-11-04 20:32:39 -04001339 /* still need to update the function records */
1340 ret = 0;
1341 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001342 }
1343
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001344 /*
1345 * Make the hash size about 1/2 the # found
1346 */
1347 for (size /= 2; size; size >>= 1)
1348 bits++;
1349
1350 /* Don't allocate too much */
1351 if (bits > FTRACE_HASH_MAX_BITS)
1352 bits = FTRACE_HASH_MAX_BITS;
1353
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001354 ret = -ENOMEM;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001355 new_hash = alloc_ftrace_hash(bits);
1356 if (!new_hash)
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001357 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001358
1359 size = 1 << src->size_bits;
1360 for (i = 0; i < size; i++) {
1361 hhd = &src->buckets[i];
1362 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist) {
1363 if (bits > 0)
1364 key = hash_long(entry->ip, bits);
1365 else
1366 key = 0;
1367 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001368 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001369 }
1370 }
1371
Steven Rostedt07fd5512011-05-05 18:03:47 -04001372 old_hash = *dst;
1373 rcu_assign_pointer(*dst, new_hash);
1374 free_ftrace_hash_rcu(old_hash);
1375
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001376 ret = 0;
1377 out:
1378 /*
1379 * Enable regardless of ret:
1380 * On success, we enable the new hash.
1381 * On failure, we re-enable the original hash.
1382 */
1383 ftrace_hash_rec_enable(ops, enable);
1384
1385 return ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001386}
1387
Steven Rostedt265c8312009-02-13 12:43:56 -05001388/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001389 * Test the hashes for this ops to see if we want to call
1390 * the ops->func or not.
1391 *
1392 * It's a match if the ip is in the ops->filter_hash or
1393 * the filter_hash does not exist or is empty,
1394 * AND
1395 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001396 *
1397 * This needs to be called with preemption disabled as
1398 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001399 */
1400static int
1401ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
1402{
1403 struct ftrace_hash *filter_hash;
1404 struct ftrace_hash *notrace_hash;
1405 int ret;
1406
Steven Rostedtb8489142011-05-04 09:27:52 -04001407 filter_hash = rcu_dereference_raw(ops->filter_hash);
1408 notrace_hash = rcu_dereference_raw(ops->notrace_hash);
1409
Steven Rostedt06a51d92011-12-19 19:07:36 -05001410 if ((ftrace_hash_empty(filter_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001411 ftrace_lookup_ip(filter_hash, ip)) &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001412 (ftrace_hash_empty(notrace_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001413 !ftrace_lookup_ip(notrace_hash, ip)))
1414 ret = 1;
1415 else
1416 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001417
1418 return ret;
1419}
1420
1421/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001422 * This is a double for. Do not use 'break' to break out of the loop,
1423 * you must use a goto.
1424 */
1425#define do_for_each_ftrace_rec(pg, rec) \
1426 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1427 int _____i; \
1428 for (_____i = 0; _____i < pg->index; _____i++) { \
1429 rec = &pg->records[_____i];
1430
1431#define while_for_each_ftrace_rec() \
1432 } \
1433 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301434
Steven Rostedt5855fea2011-12-16 19:27:42 -05001435
1436static int ftrace_cmp_recs(const void *a, const void *b)
1437{
Steven Rostedta650e022012-04-25 13:48:13 -04001438 const struct dyn_ftrace *key = a;
1439 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001440
Steven Rostedta650e022012-04-25 13:48:13 -04001441 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001442 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001443 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1444 return 1;
1445 return 0;
1446}
1447
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001448static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001449{
1450 struct ftrace_page *pg;
1451 struct dyn_ftrace *rec;
1452 struct dyn_ftrace key;
1453
1454 key.ip = start;
1455 key.flags = end; /* overload flags, as it is unsigned long */
1456
1457 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1458 if (end < pg->records[0].ip ||
1459 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1460 continue;
1461 rec = bsearch(&key, pg->records, pg->index,
1462 sizeof(struct dyn_ftrace),
1463 ftrace_cmp_recs);
1464 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001465 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001466 }
1467
Steven Rostedt5855fea2011-12-16 19:27:42 -05001468 return 0;
1469}
1470
Steven Rostedtc88fd862011-08-16 09:53:39 -04001471/**
1472 * ftrace_location - return true if the ip giving is a traced location
1473 * @ip: the instruction pointer to check
1474 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001475 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001476 * That is, the instruction that is either a NOP or call to
1477 * the function tracer. It checks the ftrace internal tables to
1478 * determine if the address belongs or not.
1479 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001480unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001481{
Steven Rostedta650e022012-04-25 13:48:13 -04001482 return ftrace_location_range(ip, ip);
1483}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001484
Steven Rostedta650e022012-04-25 13:48:13 -04001485/**
1486 * ftrace_text_reserved - return true if range contains an ftrace location
1487 * @start: start of range to search
1488 * @end: end of range to search (inclusive). @end points to the last byte to check.
1489 *
1490 * Returns 1 if @start and @end contains a ftrace location.
1491 * That is, the instruction that is either a NOP or call to
1492 * the function tracer. It checks the ftrace internal tables to
1493 * determine if the address belongs or not.
1494 */
1495int ftrace_text_reserved(void *start, void *end)
1496{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001497 unsigned long ret;
1498
1499 ret = ftrace_location_range((unsigned long)start,
1500 (unsigned long)end);
1501
1502 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001503}
1504
Steven Rostedted926f92011-05-03 13:25:24 -04001505static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1506 int filter_hash,
1507 bool inc)
1508{
1509 struct ftrace_hash *hash;
1510 struct ftrace_hash *other_hash;
1511 struct ftrace_page *pg;
1512 struct dyn_ftrace *rec;
1513 int count = 0;
1514 int all = 0;
1515
1516 /* Only update if the ops has been registered */
1517 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1518 return;
1519
1520 /*
1521 * In the filter_hash case:
1522 * If the count is zero, we update all records.
1523 * Otherwise we just update the items in the hash.
1524 *
1525 * In the notrace_hash case:
1526 * We enable the update in the hash.
1527 * As disabling notrace means enabling the tracing,
1528 * and enabling notrace means disabling, the inc variable
1529 * gets inversed.
1530 */
1531 if (filter_hash) {
1532 hash = ops->filter_hash;
1533 other_hash = ops->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001534 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001535 all = 1;
1536 } else {
1537 inc = !inc;
1538 hash = ops->notrace_hash;
1539 other_hash = ops->filter_hash;
1540 /*
1541 * If the notrace hash has no items,
1542 * then there's nothing to do.
1543 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001544 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001545 return;
1546 }
1547
1548 do_for_each_ftrace_rec(pg, rec) {
1549 int in_other_hash = 0;
1550 int in_hash = 0;
1551 int match = 0;
1552
1553 if (all) {
1554 /*
1555 * Only the filter_hash affects all records.
1556 * Update if the record is not in the notrace hash.
1557 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001558 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001559 match = 1;
1560 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001561 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1562 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001563
1564 /*
1565 *
1566 */
1567 if (filter_hash && in_hash && !in_other_hash)
1568 match = 1;
1569 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001570 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001571 match = 1;
1572 }
1573 if (!match)
1574 continue;
1575
1576 if (inc) {
1577 rec->flags++;
1578 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1579 return;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001580 /*
1581 * If any ops wants regs saved for this function
1582 * then all ops will get saved regs.
1583 */
1584 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1585 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001586 } else {
1587 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1588 return;
1589 rec->flags--;
1590 }
1591 count++;
1592 /* Shortcut, if we handled all records, we are done. */
1593 if (!all && count == hash->count)
1594 return;
1595 } while_for_each_ftrace_rec();
1596}
1597
1598static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1599 int filter_hash)
1600{
1601 __ftrace_hash_rec_update(ops, filter_hash, 0);
1602}
1603
1604static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1605 int filter_hash)
1606{
1607 __ftrace_hash_rec_update(ops, filter_hash, 1);
1608}
1609
Steven Rostedt05736a42008-09-22 14:55:47 -07001610static void print_ip_ins(const char *fmt, unsigned char *p)
1611{
1612 int i;
1613
1614 printk(KERN_CONT "%s", fmt);
1615
1616 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1617 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1618}
1619
Steven Rostedtc88fd862011-08-16 09:53:39 -04001620/**
1621 * ftrace_bug - report and shutdown function tracer
1622 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1623 * @ip: The address that failed
1624 *
1625 * The arch code that enables or disables the function tracing
1626 * can call ftrace_bug() when it has detected a problem in
1627 * modifying the code. @failed should be one of either:
1628 * EFAULT - if the problem happens on reading the @ip address
1629 * EINVAL - if what is read at @ip is not what was expected
1630 * EPERM - if the problem happens on writting to the @ip address
1631 */
1632void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001633{
1634 switch (failed) {
1635 case -EFAULT:
1636 FTRACE_WARN_ON_ONCE(1);
1637 pr_info("ftrace faulted on modifying ");
1638 print_ip_sym(ip);
1639 break;
1640 case -EINVAL:
1641 FTRACE_WARN_ON_ONCE(1);
1642 pr_info("ftrace failed to modify ");
1643 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001644 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001645 printk(KERN_CONT "\n");
1646 break;
1647 case -EPERM:
1648 FTRACE_WARN_ON_ONCE(1);
1649 pr_info("ftrace faulted on writing ");
1650 print_ip_sym(ip);
1651 break;
1652 default:
1653 FTRACE_WARN_ON_ONCE(1);
1654 pr_info("ftrace faulted on unknown error ");
1655 print_ip_sym(ip);
1656 }
1657}
1658
Steven Rostedtc88fd862011-08-16 09:53:39 -04001659static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02001660{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001661 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001662
Steven Rostedt982c3502008-11-15 16:31:41 -05001663 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001664 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05001665 *
Steven Rostedted926f92011-05-03 13:25:24 -04001666 * If the record has a ref count, then we need to enable it
1667 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001668 *
Steven Rostedted926f92011-05-03 13:25:24 -04001669 * Otherwise we make sure its disabled.
1670 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001671 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04001672 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001673 */
Steven Rostedtc88fd862011-08-16 09:53:39 -04001674 if (enable && (rec->flags & ~FTRACE_FL_MASK))
Steven Rostedted926f92011-05-03 13:25:24 -04001675 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001676
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001677 /*
1678 * If enabling and the REGS flag does not match the REGS_EN, then
1679 * do not ignore this record. Set flags to fail the compare against
1680 * ENABLED.
1681 */
1682 if (flag &&
1683 (!(rec->flags & FTRACE_FL_REGS) != !(rec->flags & FTRACE_FL_REGS_EN)))
1684 flag |= FTRACE_FL_REGS;
1685
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001686 /* If the state of this record hasn't changed, then do nothing */
1687 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001688 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001689
1690 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001691 /* Save off if rec is being enabled (for return value) */
1692 flag ^= rec->flags & FTRACE_FL_ENABLED;
1693
1694 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04001695 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001696 if (flag & FTRACE_FL_REGS) {
1697 if (rec->flags & FTRACE_FL_REGS)
1698 rec->flags |= FTRACE_FL_REGS_EN;
1699 else
1700 rec->flags &= ~FTRACE_FL_REGS_EN;
1701 }
1702 }
1703
1704 /*
1705 * If this record is being updated from a nop, then
1706 * return UPDATE_MAKE_CALL.
1707 * Otherwise, if the EN flag is set, then return
1708 * UPDATE_MODIFY_CALL_REGS to tell the caller to convert
1709 * from the non-save regs, to a save regs function.
1710 * Otherwise,
1711 * return UPDATE_MODIFY_CALL to tell the caller to convert
1712 * from the save regs, to a non-save regs function.
1713 */
1714 if (flag & FTRACE_FL_ENABLED)
1715 return FTRACE_UPDATE_MAKE_CALL;
1716 else if (rec->flags & FTRACE_FL_REGS_EN)
1717 return FTRACE_UPDATE_MODIFY_CALL_REGS;
1718 else
1719 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001720 }
1721
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001722 if (update) {
1723 /* If there's no more users, clear all flags */
1724 if (!(rec->flags & ~FTRACE_FL_MASK))
1725 rec->flags = 0;
1726 else
1727 /* Just disable the record (keep REGS state) */
1728 rec->flags &= ~FTRACE_FL_ENABLED;
1729 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04001730
1731 return FTRACE_UPDATE_MAKE_NOP;
1732}
1733
1734/**
1735 * ftrace_update_record, set a record that now is tracing or not
1736 * @rec: the record to update
1737 * @enable: set to 1 if the record is tracing, zero to force disable
1738 *
1739 * The records that represent all functions that can be traced need
1740 * to be updated when tracing has been enabled.
1741 */
1742int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1743{
1744 return ftrace_check_record(rec, enable, 1);
1745}
1746
1747/**
1748 * ftrace_test_record, check if the record has been enabled or not
1749 * @rec: the record to test
1750 * @enable: set to 1 to check if enabled, 0 if it is disabled
1751 *
1752 * The arch code may need to test if a record is already set to
1753 * tracing to determine how to modify the function code that it
1754 * represents.
1755 */
1756int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1757{
1758 return ftrace_check_record(rec, enable, 0);
1759}
1760
1761static int
1762__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1763{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001764 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001765 unsigned long ftrace_addr;
1766 int ret;
1767
Steven Rostedtc88fd862011-08-16 09:53:39 -04001768 ret = ftrace_update_record(rec, enable);
1769
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001770 if (rec->flags & FTRACE_FL_REGS)
1771 ftrace_addr = (unsigned long)FTRACE_REGS_ADDR;
1772 else
1773 ftrace_addr = (unsigned long)FTRACE_ADDR;
1774
Steven Rostedtc88fd862011-08-16 09:53:39 -04001775 switch (ret) {
1776 case FTRACE_UPDATE_IGNORE:
1777 return 0;
1778
1779 case FTRACE_UPDATE_MAKE_CALL:
1780 return ftrace_make_call(rec, ftrace_addr);
1781
1782 case FTRACE_UPDATE_MAKE_NOP:
1783 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001784
1785 case FTRACE_UPDATE_MODIFY_CALL_REGS:
1786 case FTRACE_UPDATE_MODIFY_CALL:
1787 if (rec->flags & FTRACE_FL_REGS)
1788 ftrace_old_addr = (unsigned long)FTRACE_ADDR;
1789 else
1790 ftrace_old_addr = (unsigned long)FTRACE_REGS_ADDR;
1791
1792 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04001793 }
1794
1795 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02001796}
1797
Steven Rostedte4f5d542012-04-27 09:13:18 -04001798void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001799{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001800 struct dyn_ftrace *rec;
1801 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001802 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001803
Steven Rostedt45a4a232011-04-21 23:16:46 -04001804 if (unlikely(ftrace_disabled))
1805 return;
1806
Steven Rostedt265c8312009-02-13 12:43:56 -05001807 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedte4f5d542012-04-27 09:13:18 -04001808 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001809 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04001810 ftrace_bug(failed, rec->ip);
1811 /* Stop processing */
1812 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001813 }
1814 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001815}
1816
Steven Rostedtc88fd862011-08-16 09:53:39 -04001817struct ftrace_rec_iter {
1818 struct ftrace_page *pg;
1819 int index;
1820};
1821
1822/**
1823 * ftrace_rec_iter_start, start up iterating over traced functions
1824 *
1825 * Returns an iterator handle that is used to iterate over all
1826 * the records that represent address locations where functions
1827 * are traced.
1828 *
1829 * May return NULL if no records are available.
1830 */
1831struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1832{
1833 /*
1834 * We only use a single iterator.
1835 * Protected by the ftrace_lock mutex.
1836 */
1837 static struct ftrace_rec_iter ftrace_rec_iter;
1838 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1839
1840 iter->pg = ftrace_pages_start;
1841 iter->index = 0;
1842
1843 /* Could have empty pages */
1844 while (iter->pg && !iter->pg->index)
1845 iter->pg = iter->pg->next;
1846
1847 if (!iter->pg)
1848 return NULL;
1849
1850 return iter;
1851}
1852
1853/**
1854 * ftrace_rec_iter_next, get the next record to process.
1855 * @iter: The handle to the iterator.
1856 *
1857 * Returns the next iterator after the given iterator @iter.
1858 */
1859struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1860{
1861 iter->index++;
1862
1863 if (iter->index >= iter->pg->index) {
1864 iter->pg = iter->pg->next;
1865 iter->index = 0;
1866
1867 /* Could have empty pages */
1868 while (iter->pg && !iter->pg->index)
1869 iter->pg = iter->pg->next;
1870 }
1871
1872 if (!iter->pg)
1873 return NULL;
1874
1875 return iter;
1876}
1877
1878/**
1879 * ftrace_rec_iter_record, get the record at the iterator location
1880 * @iter: The current iterator location
1881 *
1882 * Returns the record that the current @iter is at.
1883 */
1884struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1885{
1886 return &iter->pg->records[iter->index];
1887}
1888
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301889static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001890ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001891{
1892 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001893 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001894
1895 ip = rec->ip;
1896
Steven Rostedt45a4a232011-04-21 23:16:46 -04001897 if (unlikely(ftrace_disabled))
1898 return 0;
1899
Shaohua Li25aac9d2009-01-09 11:29:40 +08001900 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001901 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001902 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301903 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02001904 }
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301905 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001906}
1907
Steven Rostedt000ab692009-02-17 13:35:06 -05001908/*
1909 * archs can override this function if they must do something
1910 * before the modifying code is performed.
1911 */
1912int __weak ftrace_arch_code_modify_prepare(void)
1913{
1914 return 0;
1915}
1916
1917/*
1918 * archs can override this function if they must do something
1919 * after the modifying code is performed.
1920 */
1921int __weak ftrace_arch_code_modify_post_process(void)
1922{
1923 return 0;
1924}
1925
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001926void ftrace_modify_all_code(int command)
1927{
1928 if (command & FTRACE_UPDATE_CALLS)
1929 ftrace_replace_code(1);
1930 else if (command & FTRACE_DISABLE_CALLS)
1931 ftrace_replace_code(0);
1932
1933 if (command & FTRACE_UPDATE_TRACE_FUNC)
1934 ftrace_update_ftrace_func(ftrace_trace_function);
1935
1936 if (command & FTRACE_START_FUNC_RET)
1937 ftrace_enable_ftrace_graph_caller();
1938 else if (command & FTRACE_STOP_FUNC_RET)
1939 ftrace_disable_ftrace_graph_caller();
1940}
1941
Ingo Molnare309b412008-05-12 21:20:51 +02001942static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02001943{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001944 int *command = data;
1945
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001946 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001947
Steven Rostedtc88fd862011-08-16 09:53:39 -04001948 return 0;
1949}
1950
1951/**
1952 * ftrace_run_stop_machine, go back to the stop machine method
1953 * @command: The command to tell ftrace what to do
1954 *
1955 * If an arch needs to fall back to the stop machine method, the
1956 * it can call this function.
1957 */
1958void ftrace_run_stop_machine(int command)
1959{
1960 stop_machine(__ftrace_modify_code, &command, NULL);
1961}
1962
1963/**
1964 * arch_ftrace_update_code, modify the code to trace or not trace
1965 * @command: The command that needs to be done
1966 *
1967 * Archs can override this function if it does not need to
1968 * run stop_machine() to modify code.
1969 */
1970void __weak arch_ftrace_update_code(int command)
1971{
1972 ftrace_run_stop_machine(command);
1973}
1974
1975static void ftrace_run_update_code(int command)
1976{
1977 int ret;
1978
1979 ret = ftrace_arch_code_modify_prepare();
1980 FTRACE_WARN_ON(ret);
1981 if (ret)
1982 return;
1983 /*
1984 * Do not call function tracer while we update the code.
1985 * We are in stop machine.
1986 */
1987 function_trace_stop++;
1988
1989 /*
1990 * By default we use stop_machine() to modify the code.
1991 * But archs can do what ever they want as long as it
1992 * is safe. The stop_machine() is the safest, but also
1993 * produces the most overhead.
1994 */
1995 arch_ftrace_update_code(command);
1996
Steven Rostedt6331c282011-07-13 15:11:02 -04001997 function_trace_stop--;
1998
Steven Rostedt000ab692009-02-17 13:35:06 -05001999 ret = ftrace_arch_code_modify_post_process();
2000 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002001}
2002
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002003static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002004static int ftrace_start_up;
Steven Rostedtb8489142011-05-04 09:27:52 -04002005static int global_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002006
2007static void ftrace_startup_enable(int command)
2008{
2009 if (saved_ftrace_func != ftrace_trace_function) {
2010 saved_ftrace_func = ftrace_trace_function;
2011 command |= FTRACE_UPDATE_TRACE_FUNC;
2012 }
2013
2014 if (!command || !ftrace_enabled)
2015 return;
2016
2017 ftrace_run_update_code(command);
2018}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002019
Steven Rostedta1cd6172011-05-23 15:24:25 -04002020static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002021{
Steven Rostedtb8489142011-05-04 09:27:52 -04002022 bool hash_enable = true;
2023
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002024 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002025 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002026
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002027 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002028 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02002029
Steven Rostedtb8489142011-05-04 09:27:52 -04002030 /* ops marked global share the filter hashes */
2031 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2032 ops = &global_ops;
2033 /* Don't update hash if global is already set */
2034 if (global_start_up)
2035 hash_enable = false;
2036 global_start_up++;
2037 }
2038
Steven Rostedted926f92011-05-03 13:25:24 -04002039 ops->flags |= FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002040 if (hash_enable)
Steven Rostedted926f92011-05-03 13:25:24 -04002041 ftrace_hash_rec_enable(ops, 1);
2042
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002043 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002044
2045 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002046}
2047
Steven Rostedtbd69c302011-05-03 21:55:54 -04002048static void ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002049{
Steven Rostedtb8489142011-05-04 09:27:52 -04002050 bool hash_disable = true;
2051
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002052 if (unlikely(ftrace_disabled))
2053 return;
2054
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002055 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002056 /*
2057 * Just warn in case of unbalance, no need to kill ftrace, it's not
2058 * critical but the ftrace_call callers may be never nopped again after
2059 * further ftrace uses.
2060 */
2061 WARN_ON_ONCE(ftrace_start_up < 0);
2062
Steven Rostedtb8489142011-05-04 09:27:52 -04002063 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2064 ops = &global_ops;
2065 global_start_up--;
2066 WARN_ON_ONCE(global_start_up < 0);
2067 /* Don't update hash if global still has users */
2068 if (global_start_up) {
2069 WARN_ON_ONCE(!ftrace_start_up);
2070 hash_disable = false;
2071 }
2072 }
2073
2074 if (hash_disable)
Steven Rostedted926f92011-05-03 13:25:24 -04002075 ftrace_hash_rec_disable(ops, 1);
2076
Steven Rostedtb8489142011-05-04 09:27:52 -04002077 if (ops != &global_ops || !global_start_up)
Steven Rostedted926f92011-05-03 13:25:24 -04002078 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002079
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002080 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002081
2082 if (saved_ftrace_func != ftrace_trace_function) {
2083 saved_ftrace_func = ftrace_trace_function;
2084 command |= FTRACE_UPDATE_TRACE_FUNC;
2085 }
2086
2087 if (!command || !ftrace_enabled)
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002088 return;
Steven Rostedt3d083392008-05-12 21:20:42 +02002089
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002090 ftrace_run_update_code(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02002091}
2092
Ingo Molnare309b412008-05-12 21:20:51 +02002093static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002094{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002095 if (unlikely(ftrace_disabled))
2096 return;
2097
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002098 /* Force update next time */
2099 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002100 /* ftrace_start_up is true if we want ftrace running */
2101 if (ftrace_start_up)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002102 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002103}
2104
Ingo Molnare309b412008-05-12 21:20:51 +02002105static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002106{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002107 if (unlikely(ftrace_disabled))
2108 return;
2109
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002110 /* ftrace_start_up is true if ftrace is running */
2111 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04002112 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002113}
2114
Steven Rostedt3d083392008-05-12 21:20:42 +02002115static cycle_t ftrace_update_time;
2116static unsigned long ftrace_update_cnt;
2117unsigned long ftrace_update_tot_cnt;
2118
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002119static int ops_traces_mod(struct ftrace_ops *ops)
2120{
2121 struct ftrace_hash *hash;
2122
2123 hash = ops->filter_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05002124 return ftrace_hash_empty(hash);
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002125}
2126
Steven Rostedt31e88902008-11-14 16:21:19 -08002127static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02002128{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002129 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002130 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302131 cycle_t start, stop;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002132 unsigned long ref = 0;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002133 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002134
2135 /*
2136 * When adding a module, we need to check if tracers are
2137 * currently enabled and if they are set to trace all functions.
2138 * If they are, we need to enable the module functions as well
2139 * as update the reference counts for those function records.
2140 */
2141 if (mod) {
2142 struct ftrace_ops *ops;
2143
2144 for (ops = ftrace_ops_list;
2145 ops != &ftrace_list_end; ops = ops->next) {
2146 if (ops->flags & FTRACE_OPS_FL_ENABLED &&
2147 ops_traces_mod(ops))
2148 ref++;
2149 }
2150 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002151
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002152 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002153 ftrace_update_cnt = 0;
2154
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002155 for (pg = ftrace_new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302156
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002157 for (i = 0; i < pg->index; i++) {
2158 /* If something went wrong, bail without enabling anything */
2159 if (unlikely(ftrace_disabled))
2160 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002161
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002162 p = &pg->records[i];
2163 p->flags = ref;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302164
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002165 /*
2166 * Do the initial record conversion from mcount jump
2167 * to the NOP instructions.
2168 */
2169 if (!ftrace_code_disable(mod, p))
2170 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002171
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002172 ftrace_update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002173
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002174 /*
2175 * If the tracing is enabled, go ahead and enable the record.
2176 *
2177 * The reason not to enable the record immediatelly is the
2178 * inherent check of ftrace_make_nop/ftrace_make_call for
2179 * correct previous instructions. Making first the NOP
2180 * conversion puts the module to the correct state, thus
2181 * passing the ftrace_make_call check.
2182 */
2183 if (ftrace_start_up && ref) {
2184 int failed = __ftrace_replace_code(p, 1);
2185 if (failed)
2186 ftrace_bug(failed, p->ip);
2187 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002188 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002189 }
2190
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002191 ftrace_new_pgs = NULL;
2192
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002193 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002194 ftrace_update_time = stop - start;
2195 ftrace_update_tot_cnt += ftrace_update_cnt;
2196
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002197 return 0;
2198}
2199
Steven Rostedta7900872011-12-16 16:23:44 -05002200static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002201{
Steven Rostedta7900872011-12-16 16:23:44 -05002202 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002203 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002204
Steven Rostedta7900872011-12-16 16:23:44 -05002205 if (WARN_ON(!count))
2206 return -EINVAL;
2207
2208 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002209
2210 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002211 * We want to fill as much as possible. No more than a page
2212 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002213 */
Steven Rostedta7900872011-12-16 16:23:44 -05002214 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2215 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002216
Steven Rostedta7900872011-12-16 16:23:44 -05002217 again:
2218 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2219
2220 if (!pg->records) {
2221 /* if we can't allocate this size, try something smaller */
2222 if (!order)
2223 return -ENOMEM;
2224 order >>= 1;
2225 goto again;
2226 }
2227
2228 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2229 pg->size = cnt;
2230
2231 if (cnt > count)
2232 cnt = count;
2233
2234 return cnt;
2235}
2236
2237static struct ftrace_page *
2238ftrace_allocate_pages(unsigned long num_to_init)
2239{
2240 struct ftrace_page *start_pg;
2241 struct ftrace_page *pg;
2242 int order;
2243 int cnt;
2244
2245 if (!num_to_init)
2246 return 0;
2247
2248 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2249 if (!pg)
2250 return NULL;
2251
2252 /*
2253 * Try to allocate as much as possible in one continues
2254 * location that fills in all of the space. We want to
2255 * waste as little space as possible.
2256 */
2257 for (;;) {
2258 cnt = ftrace_allocate_records(pg, num_to_init);
2259 if (cnt < 0)
2260 goto free_pages;
2261
2262 num_to_init -= cnt;
2263 if (!num_to_init)
2264 break;
2265
2266 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2267 if (!pg->next)
2268 goto free_pages;
2269
2270 pg = pg->next;
2271 }
2272
2273 return start_pg;
2274
2275 free_pages:
2276 while (start_pg) {
2277 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2278 free_pages((unsigned long)pg->records, order);
2279 start_pg = pg->next;
2280 kfree(pg);
2281 pg = start_pg;
2282 }
2283 pr_info("ftrace: FAILED to allocate memory for functions\n");
2284 return NULL;
2285}
2286
2287static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2288{
2289 int cnt;
2290
2291 if (!num_to_init) {
2292 pr_info("ftrace: No functions to be traced?\n");
2293 return -1;
2294 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002295
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002296 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04002297 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08002298 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002299
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002300 return 0;
2301}
2302
Steven Rostedt5072c592008-05-12 21:20:43 +02002303#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2304
2305struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002306 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002307 loff_t func_pos;
2308 struct ftrace_page *pg;
2309 struct dyn_ftrace *func;
2310 struct ftrace_func_probe *probe;
2311 struct trace_parser parser;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002312 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002313 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002314 int hidx;
2315 int idx;
2316 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002317};
2318
Ingo Molnare309b412008-05-12 21:20:51 +02002319static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002320t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002321{
2322 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002323 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002324 struct hlist_head *hhd;
2325
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002326 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002327 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002328
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002329 if (iter->probe)
2330 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002331 retry:
2332 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2333 return NULL;
2334
2335 hhd = &ftrace_func_hash[iter->hidx];
2336
2337 if (hlist_empty(hhd)) {
2338 iter->hidx++;
2339 hnd = NULL;
2340 goto retry;
2341 }
2342
2343 if (!hnd)
2344 hnd = hhd->first;
2345 else {
2346 hnd = hnd->next;
2347 if (!hnd) {
2348 iter->hidx++;
2349 goto retry;
2350 }
2351 }
2352
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002353 if (WARN_ON_ONCE(!hnd))
2354 return NULL;
2355
2356 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2357
2358 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002359}
2360
2361static void *t_hash_start(struct seq_file *m, loff_t *pos)
2362{
2363 struct ftrace_iterator *iter = m->private;
2364 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08002365 loff_t l;
2366
Steven Rostedt69a30832011-12-19 15:21:16 -05002367 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2368 return NULL;
2369
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002370 if (iter->func_pos > *pos)
2371 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002372
Li Zefand82d6242009-06-24 09:54:54 +08002373 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002374 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002375 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08002376 if (!p)
2377 break;
2378 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002379 if (!p)
2380 return NULL;
2381
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002382 /* Only set this if we have an item */
2383 iter->flags |= FTRACE_ITER_HASH;
2384
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002385 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002386}
2387
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002388static int
2389t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002390{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002391 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002392
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002393 rec = iter->probe;
2394 if (WARN_ON_ONCE(!rec))
2395 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002396
Steven Rostedt809dcf22009-02-16 23:06:01 -05002397 if (rec->ops->print)
2398 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2399
Steven Rostedtb375a112009-09-17 00:05:58 -04002400 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002401
2402 if (rec->data)
2403 seq_printf(m, ":%p", rec->data);
2404 seq_putc(m, '\n');
2405
2406 return 0;
2407}
2408
2409static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02002410t_next(struct seq_file *m, void *v, loff_t *pos)
2411{
2412 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002413 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002414 struct dyn_ftrace *rec = NULL;
2415
Steven Rostedt45a4a232011-04-21 23:16:46 -04002416 if (unlikely(ftrace_disabled))
2417 return NULL;
2418
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002419 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002420 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002421
Steven Rostedt5072c592008-05-12 21:20:43 +02002422 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01002423 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02002424
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002425 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002426 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002427
Steven Rostedt5072c592008-05-12 21:20:43 +02002428 retry:
2429 if (iter->idx >= iter->pg->index) {
2430 if (iter->pg->next) {
2431 iter->pg = iter->pg->next;
2432 iter->idx = 0;
2433 goto retry;
2434 }
2435 } else {
2436 rec = &iter->pg->records[iter->idx++];
Steven Rostedt32082302011-12-16 14:42:37 -05002437 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedtf45948e2011-05-02 12:29:25 -04002438 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
Steven Rostedt0183fb12008-11-07 22:36:02 -05002439
Steven Rostedt41c52c02008-05-22 11:46:33 -04002440 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt647bcd02011-05-03 14:39:21 -04002441 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2442
2443 ((iter->flags & FTRACE_ITER_ENABLED) &&
2444 !(rec->flags & ~FTRACE_FL_MASK))) {
2445
Steven Rostedt5072c592008-05-12 21:20:43 +02002446 rec = NULL;
2447 goto retry;
2448 }
2449 }
2450
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002451 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002452 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002453
2454 iter->func = rec;
2455
2456 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002457}
2458
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002459static void reset_iter_read(struct ftrace_iterator *iter)
2460{
2461 iter->pos = 0;
2462 iter->func_pos = 0;
Dan Carpenter70f77b32012-06-09 19:10:27 +03002463 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002464}
2465
2466static void *t_start(struct seq_file *m, loff_t *pos)
2467{
2468 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002469 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002470 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002471 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002472
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002473 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002474
2475 if (unlikely(ftrace_disabled))
2476 return NULL;
2477
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002478 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002479 * If an lseek was done, then reset and start from beginning.
2480 */
2481 if (*pos < iter->pos)
2482 reset_iter_read(iter);
2483
2484 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002485 * For set_ftrace_filter reading, if we have the filter
2486 * off, we can short cut and just print out that all
2487 * functions are enabled.
2488 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05002489 if (iter->flags & FTRACE_ITER_FILTER &&
2490 ftrace_hash_empty(ops->filter_hash)) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002491 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002492 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002493 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002494 /* reset in case of seek/pread */
2495 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002496 return iter;
2497 }
2498
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002499 if (iter->flags & FTRACE_ITER_HASH)
2500 return t_hash_start(m, pos);
2501
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002502 /*
2503 * Unfortunately, we need to restart at ftrace_pages_start
2504 * every time we let go of the ftrace_mutex. This is because
2505 * those pointers can change without the lock.
2506 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002507 iter->pg = ftrace_pages_start;
2508 iter->idx = 0;
2509 for (l = 0; l <= *pos; ) {
2510 p = t_next(m, p, &l);
2511 if (!p)
2512 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002513 }
walimis5821e1b2008-11-15 15:19:06 +08002514
Steven Rostedt69a30832011-12-19 15:21:16 -05002515 if (!p)
2516 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002517
2518 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002519}
2520
2521static void t_stop(struct seq_file *m, void *p)
2522{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002523 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002524}
2525
2526static int t_show(struct seq_file *m, void *v)
2527{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002528 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002529 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02002530
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002531 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002532 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002533
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002534 if (iter->flags & FTRACE_ITER_PRINTALL) {
2535 seq_printf(m, "#### all functions enabled ####\n");
2536 return 0;
2537 }
2538
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002539 rec = iter->func;
2540
Steven Rostedt5072c592008-05-12 21:20:43 +02002541 if (!rec)
2542 return 0;
2543
Steven Rostedt647bcd02011-05-03 14:39:21 -04002544 seq_printf(m, "%ps", (void *)rec->ip);
2545 if (iter->flags & FTRACE_ITER_ENABLED)
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002546 seq_printf(m, " (%ld)%s",
2547 rec->flags & ~FTRACE_FL_MASK,
2548 rec->flags & FTRACE_FL_REGS ? " R" : "");
Steven Rostedt647bcd02011-05-03 14:39:21 -04002549 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02002550
2551 return 0;
2552}
2553
James Morris88e9d342009-09-22 16:43:43 -07002554static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002555 .start = t_start,
2556 .next = t_next,
2557 .stop = t_stop,
2558 .show = t_show,
2559};
2560
Ingo Molnare309b412008-05-12 21:20:51 +02002561static int
Steven Rostedt5072c592008-05-12 21:20:43 +02002562ftrace_avail_open(struct inode *inode, struct file *file)
2563{
2564 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002565
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002566 if (unlikely(ftrace_disabled))
2567 return -ENODEV;
2568
Jiri Olsa50e18b92012-04-25 10:23:39 +02002569 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2570 if (iter) {
2571 iter->pg = ftrace_pages_start;
2572 iter->ops = &global_ops;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002573 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002574
Jiri Olsa50e18b92012-04-25 10:23:39 +02002575 return iter ? 0 : -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02002576}
2577
Steven Rostedt647bcd02011-05-03 14:39:21 -04002578static int
2579ftrace_enabled_open(struct inode *inode, struct file *file)
2580{
2581 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002582
2583 if (unlikely(ftrace_disabled))
2584 return -ENODEV;
2585
Jiri Olsa50e18b92012-04-25 10:23:39 +02002586 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2587 if (iter) {
2588 iter->pg = ftrace_pages_start;
2589 iter->flags = FTRACE_ITER_ENABLED;
2590 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002591 }
2592
Jiri Olsa50e18b92012-04-25 10:23:39 +02002593 return iter ? 0 : -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002594}
2595
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002596static void ftrace_filter_reset(struct ftrace_hash *hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02002597{
Steven Rostedt52baf112009-02-14 01:15:39 -05002598 mutex_lock(&ftrace_lock);
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002599 ftrace_hash_clear(hash);
Steven Rostedt52baf112009-02-14 01:15:39 -05002600 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002601}
2602
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002603/**
2604 * ftrace_regex_open - initialize function tracer filter files
2605 * @ops: The ftrace_ops that hold the hash filters
2606 * @flag: The type of filter to process
2607 * @inode: The inode, usually passed in to your open routine
2608 * @file: The file, usually passed in to your open routine
2609 *
2610 * ftrace_regex_open() initializes the filter files for the
2611 * @ops. Depending on @flag it may process the filter hash or
2612 * the notrace hash of @ops. With this called from the open
2613 * routine, you can use ftrace_filter_write() for the write
2614 * routine if @flag has FTRACE_ITER_FILTER set, or
2615 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
2616 * ftrace_regex_lseek() should be used as the lseek routine, and
2617 * release must call ftrace_regex_release().
2618 */
2619int
Steven Rostedtf45948e2011-05-02 12:29:25 -04002620ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002621 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02002622{
2623 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002624 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02002625 int ret = 0;
2626
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002627 if (unlikely(ftrace_disabled))
2628 return -ENODEV;
2629
Steven Rostedt5072c592008-05-12 21:20:43 +02002630 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2631 if (!iter)
2632 return -ENOMEM;
2633
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002634 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2635 kfree(iter);
2636 return -ENOMEM;
2637 }
2638
Steven Rostedtf45948e2011-05-02 12:29:25 -04002639 if (flag & FTRACE_ITER_NOTRACE)
2640 hash = ops->notrace_hash;
2641 else
2642 hash = ops->filter_hash;
2643
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002644 iter->ops = ops;
2645 iter->flags = flag;
2646
2647 if (file->f_mode & FMODE_WRITE) {
2648 mutex_lock(&ftrace_lock);
2649 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2650 mutex_unlock(&ftrace_lock);
2651
2652 if (!iter->hash) {
2653 trace_parser_put(&iter->parser);
2654 kfree(iter);
2655 return -ENOMEM;
2656 }
2657 }
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002658
Steven Rostedt41c52c02008-05-22 11:46:33 -04002659 mutex_lock(&ftrace_regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002660
Steven Rostedt5072c592008-05-12 21:20:43 +02002661 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002662 (file->f_flags & O_TRUNC))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002663 ftrace_filter_reset(iter->hash);
Steven Rostedt5072c592008-05-12 21:20:43 +02002664
2665 if (file->f_mode & FMODE_READ) {
2666 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02002667
2668 ret = seq_open(file, &show_ftrace_seq_ops);
2669 if (!ret) {
2670 struct seq_file *m = file->private_data;
2671 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08002672 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002673 /* Failed */
2674 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08002675 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002676 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08002677 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002678 } else
2679 file->private_data = iter;
Steven Rostedt41c52c02008-05-22 11:46:33 -04002680 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002681
2682 return ret;
2683}
2684
Steven Rostedt41c52c02008-05-22 11:46:33 -04002685static int
2686ftrace_filter_open(struct inode *inode, struct file *file)
2687{
Steven Rostedt69a30832011-12-19 15:21:16 -05002688 return ftrace_regex_open(&global_ops,
2689 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2690 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002691}
2692
2693static int
2694ftrace_notrace_open(struct inode *inode, struct file *file)
2695{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002696 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002697 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002698}
2699
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002700loff_t
Andrew Morton965c8e52012-12-17 15:59:39 -08002701ftrace_regex_lseek(struct file *file, loff_t offset, int whence)
Steven Rostedt5072c592008-05-12 21:20:43 +02002702{
2703 loff_t ret;
2704
2705 if (file->f_mode & FMODE_READ)
Andrew Morton965c8e52012-12-17 15:59:39 -08002706 ret = seq_lseek(file, offset, whence);
Steven Rostedt5072c592008-05-12 21:20:43 +02002707 else
2708 file->f_pos = ret = 1;
2709
2710 return ret;
2711}
2712
Steven Rostedt64e7c442009-02-13 17:08:48 -05002713static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002714{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002715 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08002716 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002717
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002718 switch (type) {
2719 case MATCH_FULL:
2720 if (strcmp(str, regex) == 0)
2721 matched = 1;
2722 break;
2723 case MATCH_FRONT_ONLY:
2724 if (strncmp(str, regex, len) == 0)
2725 matched = 1;
2726 break;
2727 case MATCH_MIDDLE_ONLY:
2728 if (strstr(str, regex))
2729 matched = 1;
2730 break;
2731 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08002732 slen = strlen(str);
2733 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002734 matched = 1;
2735 break;
2736 }
2737
2738 return matched;
2739}
2740
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002741static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002742enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04002743{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002744 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002745 int ret = 0;
2746
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002747 entry = ftrace_lookup_ip(hash, rec->ip);
2748 if (not) {
2749 /* Do nothing if it doesn't exist */
2750 if (!entry)
2751 return 0;
2752
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002753 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002754 } else {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002755 /* Do nothing if it exists */
2756 if (entry)
2757 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002758
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002759 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002760 }
2761 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04002762}
2763
Steven Rostedt64e7c442009-02-13 17:08:48 -05002764static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002765ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2766 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002767{
2768 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002769 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002770
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002771 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2772
2773 if (mod) {
2774 /* module lookup requires matching the module */
2775 if (!modname || strcmp(modname, mod))
2776 return 0;
2777
2778 /* blank search means to match all funcs in the mod */
2779 if (!len)
2780 return 1;
2781 }
2782
Steven Rostedt64e7c442009-02-13 17:08:48 -05002783 return ftrace_match(str, regex, len, type);
2784}
2785
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002786static int
2787match_records(struct ftrace_hash *hash, char *buff,
2788 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002789{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002790 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002791 struct ftrace_page *pg;
2792 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002793 int type = MATCH_FULL;
2794 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08002795 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002796 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002797
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002798 if (len) {
2799 type = filter_parse_regex(buff, len, &search, &not);
2800 search_len = strlen(search);
2801 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002802
Steven Rostedt52baf112009-02-14 01:15:39 -05002803 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002804
2805 if (unlikely(ftrace_disabled))
2806 goto out_unlock;
2807
Steven Rostedt265c8312009-02-13 12:43:56 -05002808 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002809 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002810 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002811 if (ret < 0) {
2812 found = ret;
2813 goto out_unlock;
2814 }
Li Zefan311d16d2009-12-08 11:15:11 +08002815 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05002816 }
2817 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002818 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05002819 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08002820
2821 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02002822}
2823
Steven Rostedt64e7c442009-02-13 17:08:48 -05002824static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002825ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002826{
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002827 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002828}
2829
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002830static int
2831ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002832{
Steven Rostedt64e7c442009-02-13 17:08:48 -05002833 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002834
Steven Rostedt64e7c442009-02-13 17:08:48 -05002835 /* blank or '*' mean the same */
2836 if (strcmp(buff, "*") == 0)
2837 buff[0] = 0;
2838
2839 /* handle the case of 'dont filter this module' */
2840 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2841 buff[0] = 0;
2842 not = 1;
2843 }
2844
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002845 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002846}
2847
Steven Rostedtf6180772009-02-14 00:40:25 -05002848/*
2849 * We register the module command as a template to show others how
2850 * to register the a command as well.
2851 */
2852
2853static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04002854ftrace_mod_callback(struct ftrace_hash *hash,
2855 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05002856{
2857 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002858 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05002859
2860 /*
2861 * cmd == 'mod' because we only registered this func
2862 * for the 'mod' ftrace_func_command.
2863 * But if you register one func with multiple commands,
2864 * you can tell which command was used by the cmd
2865 * parameter.
2866 */
2867
2868 /* we must have a module name */
2869 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002870 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002871
2872 mod = strsep(&param, ":");
2873 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002874 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002875
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002876 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002877 if (!ret)
2878 ret = -EINVAL;
2879 if (ret < 0)
2880 return ret;
2881
2882 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05002883}
2884
2885static struct ftrace_func_command ftrace_mod_cmd = {
2886 .name = "mod",
2887 .func = ftrace_mod_callback,
2888};
2889
2890static int __init ftrace_mod_cmd_init(void)
2891{
2892 return register_ftrace_command(&ftrace_mod_cmd);
2893}
Steven Rostedt6f415672012-10-05 12:13:07 -04002894core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05002895
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04002896static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04002897 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002898{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002899 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002900 struct hlist_head *hhd;
2901 struct hlist_node *n;
2902 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002903
2904 key = hash_long(ip, FTRACE_HASH_BITS);
2905
2906 hhd = &ftrace_func_hash[key];
2907
2908 if (hlist_empty(hhd))
2909 return;
2910
2911 /*
2912 * Disable preemption for these calls to prevent a RCU grace
2913 * period. This syncs the hash iteration and freeing of items
2914 * on the hash. rcu_read_lock is too dangerous here.
2915 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04002916 preempt_disable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002917 hlist_for_each_entry_rcu(entry, n, hhd, node) {
2918 if (entry->ip == ip)
2919 entry->ops->func(ip, parent_ip, &entry->data);
2920 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04002921 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002922}
2923
Steven Rostedtb6887d72009-02-17 12:32:04 -05002924static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05002925{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04002926 .func = function_trace_probe_call,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002927};
2928
Steven Rostedtb6887d72009-02-17 12:32:04 -05002929static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002930
Steven Rostedtb6887d72009-02-17 12:32:04 -05002931static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002932{
Steven Rostedtb8489142011-05-04 09:27:52 -04002933 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002934 int i;
2935
Steven Rostedtb6887d72009-02-17 12:32:04 -05002936 if (ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002937 return;
2938
2939 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2940 struct hlist_head *hhd = &ftrace_func_hash[i];
2941 if (hhd->first)
2942 break;
2943 }
2944 /* Nothing registered? */
2945 if (i == FTRACE_FUNC_HASHSIZE)
2946 return;
2947
Steven Rostedtb8489142011-05-04 09:27:52 -04002948 ret = __register_ftrace_function(&trace_probe_ops);
2949 if (!ret)
Steven Rostedta1cd6172011-05-23 15:24:25 -04002950 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04002951
Steven Rostedtb6887d72009-02-17 12:32:04 -05002952 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002953}
2954
Steven Rostedtb6887d72009-02-17 12:32:04 -05002955static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002956{
Steven Rostedtb8489142011-05-04 09:27:52 -04002957 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002958 int i;
2959
Steven Rostedtb6887d72009-02-17 12:32:04 -05002960 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002961 return;
2962
2963 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2964 struct hlist_head *hhd = &ftrace_func_hash[i];
2965 if (hhd->first)
2966 return;
2967 }
2968
2969 /* no more funcs left */
Steven Rostedtb8489142011-05-04 09:27:52 -04002970 ret = __unregister_ftrace_function(&trace_probe_ops);
2971 if (!ret)
2972 ftrace_shutdown(&trace_probe_ops, 0);
2973
Steven Rostedtb6887d72009-02-17 12:32:04 -05002974 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002975}
2976
2977
2978static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2979{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002980 struct ftrace_func_probe *entry =
2981 container_of(rhp, struct ftrace_func_probe, rcu);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002982
2983 if (entry->ops->free)
2984 entry->ops->free(&entry->data);
2985 kfree(entry);
2986}
2987
2988
2989int
Steven Rostedtb6887d72009-02-17 12:32:04 -05002990register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002991 void *data)
2992{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002993 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002994 struct ftrace_page *pg;
2995 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002996 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002997 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002998 int count = 0;
2999 char *search;
3000
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003001 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003002 len = strlen(search);
3003
Steven Rostedtb6887d72009-02-17 12:32:04 -05003004 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003005 if (WARN_ON(not))
3006 return -EINVAL;
3007
3008 mutex_lock(&ftrace_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003009
Steven Rostedt45a4a232011-04-21 23:16:46 -04003010 if (unlikely(ftrace_disabled))
3011 goto out_unlock;
3012
Steven Rostedt59df055f2009-02-14 15:29:06 -05003013 do_for_each_ftrace_rec(pg, rec) {
3014
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003015 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003016 continue;
3017
3018 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3019 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003020 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003021 if (!count)
3022 count = -ENOMEM;
3023 goto out_unlock;
3024 }
3025
3026 count++;
3027
3028 entry->data = data;
3029
3030 /*
3031 * The caller might want to do something special
3032 * for each function we find. We call the callback
3033 * to give the caller an opportunity to do so.
3034 */
3035 if (ops->callback) {
3036 if (ops->callback(rec->ip, &entry->data) < 0) {
3037 /* caller does not like this func */
3038 kfree(entry);
3039 continue;
3040 }
3041 }
3042
3043 entry->ops = ops;
3044 entry->ip = rec->ip;
3045
3046 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3047 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3048
3049 } while_for_each_ftrace_rec();
Steven Rostedtb6887d72009-02-17 12:32:04 -05003050 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003051
3052 out_unlock:
3053 mutex_unlock(&ftrace_lock);
3054
3055 return count;
3056}
3057
3058enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003059 PROBE_TEST_FUNC = 1,
3060 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05003061};
3062
3063static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003064__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003065 void *data, int flags)
3066{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003067 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003068 struct hlist_node *n, *tmp;
3069 char str[KSYM_SYMBOL_LEN];
3070 int type = MATCH_FULL;
3071 int i, len = 0;
3072 char *search;
3073
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003074 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003075 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003076 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003077 int not;
3078
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003079 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003080 len = strlen(search);
3081
Steven Rostedtb6887d72009-02-17 12:32:04 -05003082 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003083 if (WARN_ON(not))
3084 return;
3085 }
3086
3087 mutex_lock(&ftrace_lock);
3088 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3089 struct hlist_head *hhd = &ftrace_func_hash[i];
3090
3091 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
3092
3093 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05003094 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003095 continue;
3096
Steven Rostedtb6887d72009-02-17 12:32:04 -05003097 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003098 continue;
3099
3100 /* do this last, since it is the most expensive */
3101 if (glob) {
3102 kallsyms_lookup(entry->ip, NULL, NULL,
3103 NULL, str);
3104 if (!ftrace_match(str, glob, len, type))
3105 continue;
3106 }
3107
3108 hlist_del(&entry->node);
3109 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
3110 }
3111 }
Steven Rostedtb6887d72009-02-17 12:32:04 -05003112 __disable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003113 mutex_unlock(&ftrace_lock);
3114}
3115
3116void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003117unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003118 void *data)
3119{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003120 __unregister_ftrace_function_probe(glob, ops, data,
3121 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003122}
3123
3124void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003125unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003126{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003127 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003128}
3129
Steven Rostedtb6887d72009-02-17 12:32:04 -05003130void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003131{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003132 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003133}
3134
Steven Rostedtf6180772009-02-14 00:40:25 -05003135static LIST_HEAD(ftrace_commands);
3136static DEFINE_MUTEX(ftrace_cmd_mutex);
3137
3138int register_ftrace_command(struct ftrace_func_command *cmd)
3139{
3140 struct ftrace_func_command *p;
3141 int ret = 0;
3142
3143 mutex_lock(&ftrace_cmd_mutex);
3144 list_for_each_entry(p, &ftrace_commands, list) {
3145 if (strcmp(cmd->name, p->name) == 0) {
3146 ret = -EBUSY;
3147 goto out_unlock;
3148 }
3149 }
3150 list_add(&cmd->list, &ftrace_commands);
3151 out_unlock:
3152 mutex_unlock(&ftrace_cmd_mutex);
3153
3154 return ret;
3155}
3156
3157int unregister_ftrace_command(struct ftrace_func_command *cmd)
3158{
3159 struct ftrace_func_command *p, *n;
3160 int ret = -ENODEV;
3161
3162 mutex_lock(&ftrace_cmd_mutex);
3163 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3164 if (strcmp(cmd->name, p->name) == 0) {
3165 ret = 0;
3166 list_del_init(&p->list);
3167 goto out_unlock;
3168 }
3169 }
3170 out_unlock:
3171 mutex_unlock(&ftrace_cmd_mutex);
3172
3173 return ret;
3174}
3175
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003176static int ftrace_process_regex(struct ftrace_hash *hash,
3177 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003178{
Steven Rostedtf6180772009-02-14 00:40:25 -05003179 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003180 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08003181 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003182
3183 func = strsep(&next, ":");
3184
3185 if (!next) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003186 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003187 if (!ret)
3188 ret = -EINVAL;
3189 if (ret < 0)
3190 return ret;
3191 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003192 }
3193
Steven Rostedtf6180772009-02-14 00:40:25 -05003194 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05003195
3196 command = strsep(&next, ":");
3197
Steven Rostedtf6180772009-02-14 00:40:25 -05003198 mutex_lock(&ftrace_cmd_mutex);
3199 list_for_each_entry(p, &ftrace_commands, list) {
3200 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003201 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05003202 goto out_unlock;
3203 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05003204 }
Steven Rostedtf6180772009-02-14 00:40:25 -05003205 out_unlock:
3206 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003207
Steven Rostedtf6180772009-02-14 00:40:25 -05003208 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003209}
3210
Ingo Molnare309b412008-05-12 21:20:51 +02003211static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003212ftrace_regex_write(struct file *file, const char __user *ubuf,
3213 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02003214{
3215 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003216 struct trace_parser *parser;
3217 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02003218
Li Zefan4ba79782009-09-22 13:52:20 +08003219 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02003220 return 0;
3221
Steven Rostedt41c52c02008-05-22 11:46:33 -04003222 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003223
Steven Rostedt45a4a232011-04-21 23:16:46 -04003224 ret = -ENODEV;
3225 if (unlikely(ftrace_disabled))
3226 goto out_unlock;
3227
Steven Rostedt5072c592008-05-12 21:20:43 +02003228 if (file->f_mode & FMODE_READ) {
3229 struct seq_file *m = file->private_data;
3230 iter = m->private;
3231 } else
3232 iter = file->private_data;
3233
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003234 parser = &iter->parser;
3235 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02003236
Li Zefan4ba79782009-09-22 13:52:20 +08003237 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003238 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003239 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003240 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08003241 trace_parser_clear(parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02003242 if (ret)
Li Zefaned146b252009-11-03 08:55:38 +08003243 goto out_unlock;
Steven Rostedt5072c592008-05-12 21:20:43 +02003244 }
3245
Steven Rostedt5072c592008-05-12 21:20:43 +02003246 ret = read;
Li Zefaned146b252009-11-03 08:55:38 +08003247out_unlock:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003248 mutex_unlock(&ftrace_regex_lock);
Li Zefaned146b252009-11-03 08:55:38 +08003249
Steven Rostedt5072c592008-05-12 21:20:43 +02003250 return ret;
3251}
3252
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003253ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003254ftrace_filter_write(struct file *file, const char __user *ubuf,
3255 size_t cnt, loff_t *ppos)
3256{
3257 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3258}
3259
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003260ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003261ftrace_notrace_write(struct file *file, const char __user *ubuf,
3262 size_t cnt, loff_t *ppos)
3263{
3264 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3265}
3266
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003267static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003268ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3269{
3270 struct ftrace_func_entry *entry;
3271
3272 if (!ftrace_location(ip))
3273 return -EINVAL;
3274
3275 if (remove) {
3276 entry = ftrace_lookup_ip(hash, ip);
3277 if (!entry)
3278 return -ENOENT;
3279 free_hash_entry(hash, entry);
3280 return 0;
3281 }
3282
3283 return add_hash_entry(hash, ip);
3284}
3285
3286static int
3287ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3288 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003289{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003290 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003291 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003292 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003293
Steven Rostedt936e0742011-05-05 22:54:01 -04003294 /* All global ops uses the global ops filters */
3295 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3296 ops = &global_ops;
3297
Steven Rostedt41c52c02008-05-22 11:46:33 -04003298 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003299 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003300
Steven Rostedtf45948e2011-05-02 12:29:25 -04003301 if (enable)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003302 orig_hash = &ops->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003303 else
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003304 orig_hash = &ops->notrace_hash;
3305
3306 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3307 if (!hash)
3308 return -ENOMEM;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003309
Steven Rostedt41c52c02008-05-22 11:46:33 -04003310 mutex_lock(&ftrace_regex_lock);
3311 if (reset)
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003312 ftrace_filter_reset(hash);
Jiri Olsaac483c42012-01-02 10:04:14 +01003313 if (buf && !ftrace_match_records(hash, buf, len)) {
3314 ret = -EINVAL;
3315 goto out_regex_unlock;
3316 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003317 if (ip) {
3318 ret = ftrace_match_addr(hash, ip, remove);
3319 if (ret < 0)
3320 goto out_regex_unlock;
3321 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003322
3323 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003324 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt072126f2011-07-13 15:08:31 -04003325 if (!ret && ops->flags & FTRACE_OPS_FL_ENABLED
3326 && ftrace_enabled)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01003327 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt072126f2011-07-13 15:08:31 -04003328
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003329 mutex_unlock(&ftrace_lock);
3330
Jiri Olsaac483c42012-01-02 10:04:14 +01003331 out_regex_unlock:
Steven Rostedt41c52c02008-05-22 11:46:33 -04003332 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003333
3334 free_ftrace_hash(hash);
3335 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003336}
3337
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003338static int
3339ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3340 int reset, int enable)
3341{
3342 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3343}
3344
3345/**
3346 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3347 * @ops - the ops to set the filter with
3348 * @ip - the address to add to or remove from the filter.
3349 * @remove - non zero to remove the ip from the filter
3350 * @reset - non zero to reset all filters before applying this filter.
3351 *
3352 * Filters denote which functions should be enabled when tracing is enabled
3353 * If @ip is NULL, it failes to update filter.
3354 */
3355int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3356 int remove, int reset)
3357{
3358 return ftrace_set_addr(ops, ip, remove, reset, 1);
3359}
3360EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3361
3362static int
3363ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3364 int reset, int enable)
3365{
3366 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3367}
3368
Steven Rostedt77a2b372008-05-12 21:20:45 +02003369/**
3370 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003371 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02003372 * @buf - the string that holds the function filter text.
3373 * @len - the length of the string.
3374 * @reset - non zero to reset all filters before applying this filter.
3375 *
3376 * Filters denote which functions should be enabled when tracing is enabled.
3377 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3378 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003379int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003380 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02003381{
Jiri Olsaac483c42012-01-02 10:04:14 +01003382 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003383}
Steven Rostedt936e0742011-05-05 22:54:01 -04003384EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003385
Steven Rostedt41c52c02008-05-22 11:46:33 -04003386/**
3387 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003388 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04003389 * @buf - the string that holds the function notrace text.
3390 * @len - the length of the string.
3391 * @reset - non zero to reset all filters before applying this filter.
3392 *
3393 * Notrace Filters denote which functions should not be enabled when tracing
3394 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3395 * for tracing.
3396 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003397int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003398 int len, int reset)
3399{
Jiri Olsaac483c42012-01-02 10:04:14 +01003400 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04003401}
3402EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3403/**
3404 * ftrace_set_filter - set a function to filter on in ftrace
3405 * @ops - the ops to set the filter with
3406 * @buf - the string that holds the function filter text.
3407 * @len - the length of the string.
3408 * @reset - non zero to reset all filters before applying this filter.
3409 *
3410 * Filters denote which functions should be enabled when tracing is enabled.
3411 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3412 */
3413void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3414{
3415 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3416}
3417EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3418
3419/**
3420 * ftrace_set_notrace - set a function to not trace in ftrace
3421 * @ops - the ops to set the notrace filter with
3422 * @buf - the string that holds the function notrace text.
3423 * @len - the length of the string.
3424 * @reset - non zero to reset all filters before applying this filter.
3425 *
3426 * Notrace Filters denote which functions should not be enabled when tracing
3427 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3428 * for tracing.
3429 */
3430void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003431{
Steven Rostedtf45948e2011-05-02 12:29:25 -04003432 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003433}
Steven Rostedt936e0742011-05-05 22:54:01 -04003434EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003435
Steven Rostedt2af15d62009-05-28 13:37:24 -04003436/*
3437 * command line interface to allow users to set filters on boot up.
3438 */
3439#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3440static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3441static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3442
3443static int __init set_ftrace_notrace(char *str)
3444{
3445 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
3446 return 1;
3447}
3448__setup("ftrace_notrace=", set_ftrace_notrace);
3449
3450static int __init set_ftrace_filter(char *str)
3451{
3452 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
3453 return 1;
3454}
3455__setup("ftrace_filter=", set_ftrace_filter);
3456
Stefan Assmann369bc182009-10-12 22:17:21 +02003457#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08003458static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Steven Rostedt801c29f2010-03-05 20:02:19 -05003459static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
3460
Stefan Assmann369bc182009-10-12 22:17:21 +02003461static int __init set_graph_function(char *str)
3462{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02003463 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02003464 return 1;
3465}
3466__setup("ftrace_graph_filter=", set_graph_function);
3467
3468static void __init set_ftrace_early_graph(char *buf)
3469{
3470 int ret;
3471 char *func;
3472
3473 while (buf) {
3474 func = strsep(&buf, ",");
3475 /* we allow only one expression at a time */
3476 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3477 func);
3478 if (ret)
3479 printk(KERN_DEBUG "ftrace: function %s not "
3480 "traceable\n", func);
3481 }
3482}
3483#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3484
Steven Rostedt2a85a372011-12-19 21:57:44 -05003485void __init
3486ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04003487{
3488 char *func;
3489
3490 while (buf) {
3491 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04003492 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003493 }
3494}
3495
3496static void __init set_ftrace_early_filters(void)
3497{
3498 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003499 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003500 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003501 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02003502#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3503 if (ftrace_graph_buf[0])
3504 set_ftrace_early_graph(ftrace_graph_buf);
3505#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04003506}
3507
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003508int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003509{
3510 struct seq_file *m = (struct seq_file *)file->private_data;
3511 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003512 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003513 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04003514 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003515 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02003516
Steven Rostedt41c52c02008-05-22 11:46:33 -04003517 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003518 if (file->f_mode & FMODE_READ) {
3519 iter = m->private;
3520
3521 seq_release(inode, file);
3522 } else
3523 iter = file->private_data;
3524
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003525 parser = &iter->parser;
3526 if (trace_parser_loaded(parser)) {
3527 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003528 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02003529 }
3530
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003531 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003532
Steven Rostedt058e2972011-04-29 22:35:33 -04003533 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04003534 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3535
3536 if (filter_hash)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003537 orig_hash = &iter->ops->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04003538 else
3539 orig_hash = &iter->ops->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003540
Steven Rostedt058e2972011-04-29 22:35:33 -04003541 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003542 ret = ftrace_hash_move(iter->ops, filter_hash,
3543 orig_hash, iter->hash);
3544 if (!ret && (iter->ops->flags & FTRACE_OPS_FL_ENABLED)
3545 && ftrace_enabled)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01003546 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003547
Steven Rostedt058e2972011-04-29 22:35:33 -04003548 mutex_unlock(&ftrace_lock);
3549 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003550 free_ftrace_hash(iter->hash);
3551 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04003552
Steven Rostedt41c52c02008-05-22 11:46:33 -04003553 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003554 return 0;
3555}
3556
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003557static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003558 .open = ftrace_avail_open,
3559 .read = seq_read,
3560 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08003561 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02003562};
3563
Steven Rostedt647bcd02011-05-03 14:39:21 -04003564static const struct file_operations ftrace_enabled_fops = {
3565 .open = ftrace_enabled_open,
3566 .read = seq_read,
3567 .llseek = seq_lseek,
3568 .release = seq_release_private,
3569};
3570
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003571static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003572 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003573 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02003574 .write = ftrace_filter_write,
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003575 .llseek = ftrace_regex_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003576 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02003577};
3578
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003579static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04003580 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003581 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003582 .write = ftrace_notrace_write,
3583 .llseek = ftrace_regex_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003584 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003585};
3586
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003587#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3588
3589static DEFINE_MUTEX(graph_lock);
3590
3591int ftrace_graph_count;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003592int ftrace_graph_filter_enabled;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003593unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3594
3595static void *
Li Zefan85951842009-06-24 09:54:00 +08003596__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003597{
Li Zefan85951842009-06-24 09:54:00 +08003598 if (*pos >= ftrace_graph_count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003599 return NULL;
Li Zefana4ec5e02009-09-18 14:06:28 +08003600 return &ftrace_graph_funcs[*pos];
Li Zefan85951842009-06-24 09:54:00 +08003601}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003602
Li Zefan85951842009-06-24 09:54:00 +08003603static void *
3604g_next(struct seq_file *m, void *v, loff_t *pos)
3605{
3606 (*pos)++;
3607 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003608}
3609
3610static void *g_start(struct seq_file *m, loff_t *pos)
3611{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003612 mutex_lock(&graph_lock);
3613
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003614 /* Nothing, tell g_show to print all functions are enabled */
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003615 if (!ftrace_graph_filter_enabled && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003616 return (void *)1;
3617
Li Zefan85951842009-06-24 09:54:00 +08003618 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003619}
3620
3621static void g_stop(struct seq_file *m, void *p)
3622{
3623 mutex_unlock(&graph_lock);
3624}
3625
3626static int g_show(struct seq_file *m, void *v)
3627{
3628 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003629
3630 if (!ptr)
3631 return 0;
3632
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003633 if (ptr == (unsigned long *)1) {
3634 seq_printf(m, "#### all functions enabled ####\n");
3635 return 0;
3636 }
3637
Steven Rostedtb375a112009-09-17 00:05:58 -04003638 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003639
3640 return 0;
3641}
3642
James Morris88e9d342009-09-22 16:43:43 -07003643static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003644 .start = g_start,
3645 .next = g_next,
3646 .stop = g_stop,
3647 .show = g_show,
3648};
3649
3650static int
3651ftrace_graph_open(struct inode *inode, struct file *file)
3652{
3653 int ret = 0;
3654
3655 if (unlikely(ftrace_disabled))
3656 return -ENODEV;
3657
3658 mutex_lock(&graph_lock);
3659 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04003660 (file->f_flags & O_TRUNC)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003661 ftrace_graph_filter_enabled = 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003662 ftrace_graph_count = 0;
3663 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3664 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003665 mutex_unlock(&graph_lock);
3666
Li Zefana4ec5e02009-09-18 14:06:28 +08003667 if (file->f_mode & FMODE_READ)
3668 ret = seq_open(file, &ftrace_graph_seq_ops);
3669
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003670 return ret;
3671}
3672
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003673static int
Li Zefan87827112009-07-23 11:29:11 +08003674ftrace_graph_release(struct inode *inode, struct file *file)
3675{
3676 if (file->f_mode & FMODE_READ)
3677 seq_release(inode, file);
3678 return 0;
3679}
3680
3681static int
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003682ftrace_set_func(unsigned long *array, int *idx, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003683{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003684 struct dyn_ftrace *rec;
3685 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003686 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003687 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003688 int type, not;
3689 char *search;
3690 bool exists;
3691 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003692
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003693 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003694 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003695 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3696 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003697
3698 search_len = strlen(search);
3699
Steven Rostedt52baf112009-02-14 01:15:39 -05003700 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003701
3702 if (unlikely(ftrace_disabled)) {
3703 mutex_unlock(&ftrace_lock);
3704 return -ENODEV;
3705 }
3706
Steven Rostedt265c8312009-02-13 12:43:56 -05003707 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003708
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003709 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003710 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003711 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003712 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003713 if (array[i] == rec->ip) {
3714 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05003715 break;
3716 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003717 }
3718
3719 if (!not) {
3720 fail = 0;
3721 if (!exists) {
3722 array[(*idx)++] = rec->ip;
3723 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3724 goto out;
3725 }
3726 } else {
3727 if (exists) {
3728 array[i] = array[--(*idx)];
3729 array[*idx] = 0;
3730 fail = 0;
3731 }
3732 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003733 }
Steven Rostedt265c8312009-02-13 12:43:56 -05003734 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003735out:
Steven Rostedt52baf112009-02-14 01:15:39 -05003736 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003737
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003738 if (fail)
3739 return -EINVAL;
3740
3741 ftrace_graph_filter_enabled = 1;
3742 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003743}
3744
3745static ssize_t
3746ftrace_graph_write(struct file *file, const char __user *ubuf,
3747 size_t cnt, loff_t *ppos)
3748{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003749 struct trace_parser parser;
Li Zefan4ba79782009-09-22 13:52:20 +08003750 ssize_t read, ret;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003751
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003752 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003753 return 0;
3754
3755 mutex_lock(&graph_lock);
3756
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003757 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3758 ret = -ENOMEM;
Li Zefan1eb90f12009-09-22 13:52:57 +08003759 goto out_unlock;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003760 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003761
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003762 read = trace_get_user(&parser, ubuf, cnt, ppos);
3763
Li Zefan4ba79782009-09-22 13:52:20 +08003764 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003765 parser.buffer[parser.idx] = 0;
3766
3767 /* we allow only one expression at a time */
Li Zefana4ec5e02009-09-18 14:06:28 +08003768 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003769 parser.buffer);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003770 if (ret)
Li Zefan1eb90f12009-09-22 13:52:57 +08003771 goto out_free;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003772 }
3773
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003774 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08003775
3776out_free:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003777 trace_parser_put(&parser);
Li Zefan1eb90f12009-09-22 13:52:57 +08003778out_unlock:
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003779 mutex_unlock(&graph_lock);
3780
3781 return ret;
3782}
3783
3784static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08003785 .open = ftrace_graph_open,
3786 .read = seq_read,
3787 .write = ftrace_graph_write,
3788 .release = ftrace_graph_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003789 .llseek = seq_lseek,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003790};
3791#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3792
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003793static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02003794{
Steven Rostedt5072c592008-05-12 21:20:43 +02003795
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003796 trace_create_file("available_filter_functions", 0444,
3797 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02003798
Steven Rostedt647bcd02011-05-03 14:39:21 -04003799 trace_create_file("enabled_functions", 0444,
3800 d_tracer, NULL, &ftrace_enabled_fops);
3801
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003802 trace_create_file("set_ftrace_filter", 0644, d_tracer,
3803 NULL, &ftrace_filter_fops);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003804
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003805 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003806 NULL, &ftrace_notrace_fops);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04003807
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003808#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003809 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003810 NULL,
3811 &ftrace_graph_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003812#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3813
Steven Rostedt5072c592008-05-12 21:20:43 +02003814 return 0;
3815}
3816
Steven Rostedt9fd49322012-04-24 22:32:06 -04003817static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05003818{
Steven Rostedt9fd49322012-04-24 22:32:06 -04003819 const unsigned long *ipa = a;
3820 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05003821
Steven Rostedt9fd49322012-04-24 22:32:06 -04003822 if (*ipa > *ipb)
3823 return 1;
3824 if (*ipa < *ipb)
3825 return -1;
3826 return 0;
3827}
3828
3829static void ftrace_swap_ips(void *a, void *b, int size)
3830{
3831 unsigned long *ipa = a;
3832 unsigned long *ipb = b;
3833 unsigned long t;
3834
3835 t = *ipa;
3836 *ipa = *ipb;
3837 *ipb = t;
Steven Rostedt68950612011-12-16 17:06:45 -05003838}
3839
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003840static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08003841 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003842 unsigned long *end)
3843{
Steven Rostedt706c81f2012-04-24 23:45:26 -04003844 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05003845 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04003846 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05003847 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003848 unsigned long *p;
3849 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04003850 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05003851 int ret = -ENOMEM;
3852
3853 count = end - start;
3854
3855 if (!count)
3856 return 0;
3857
Steven Rostedt9fd49322012-04-24 22:32:06 -04003858 sort(start, count, sizeof(*start),
3859 ftrace_cmp_ips, ftrace_swap_ips);
3860
Steven Rostedt706c81f2012-04-24 23:45:26 -04003861 start_pg = ftrace_allocate_pages(count);
3862 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05003863 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003864
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003865 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05003866
Steven Rostedt32082302011-12-16 14:42:37 -05003867 /*
3868 * Core and each module needs their own pages, as
3869 * modules will free them when they are removed.
3870 * Force a new page to be allocated for modules.
3871 */
Steven Rostedta7900872011-12-16 16:23:44 -05003872 if (!mod) {
3873 WARN_ON(ftrace_pages || ftrace_pages_start);
3874 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04003875 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05003876 } else {
Steven Rostedt32082302011-12-16 14:42:37 -05003877 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05003878 goto out;
Steven Rostedt32082302011-12-16 14:42:37 -05003879
Steven Rostedta7900872011-12-16 16:23:44 -05003880 if (WARN_ON(ftrace_pages->next)) {
3881 /* Hmm, we have free pages? */
3882 while (ftrace_pages->next)
3883 ftrace_pages = ftrace_pages->next;
Steven Rostedt32082302011-12-16 14:42:37 -05003884 }
Steven Rostedta7900872011-12-16 16:23:44 -05003885
Steven Rostedt706c81f2012-04-24 23:45:26 -04003886 ftrace_pages->next = start_pg;
Steven Rostedt32082302011-12-16 14:42:37 -05003887 }
3888
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003889 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04003890 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003891 while (p < end) {
3892 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08003893 /*
3894 * Some architecture linkers will pad between
3895 * the different mcount_loc sections of different
3896 * object files to satisfy alignments.
3897 * Skip any NULL pointers.
3898 */
3899 if (!addr)
3900 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04003901
3902 if (pg->index == pg->size) {
3903 /* We should have allocated enough */
3904 if (WARN_ON(!pg->next))
3905 break;
3906 pg = pg->next;
3907 }
3908
3909 rec = &pg->records[pg->index++];
3910 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003911 }
3912
Steven Rostedt706c81f2012-04-24 23:45:26 -04003913 /* We should have used all pages */
3914 WARN_ON(pg->next);
3915
3916 /* Assign the last page to ftrace_pages */
3917 ftrace_pages = pg;
3918
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003919 /* These new locations need to be initialized */
Steven Rostedt706c81f2012-04-24 23:45:26 -04003920 ftrace_new_pgs = start_pg;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003921
Steven Rostedta4f18ed2011-06-07 09:26:46 -04003922 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04003923 * We only need to disable interrupts on start up
3924 * because we are modifying code that an interrupt
3925 * may execute, and the modification is not atomic.
3926 * But for modules, nothing runs the code we modify
3927 * until we are finished with it, and there's no
3928 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04003929 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04003930 if (!mod)
3931 local_irq_save(flags);
Steven Rostedt31e88902008-11-14 16:21:19 -08003932 ftrace_update_code(mod);
Steven Rostedt4376cac2011-06-24 23:28:13 -04003933 if (!mod)
3934 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05003935 ret = 0;
3936 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003937 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003938
Steven Rostedta7900872011-12-16 16:23:44 -05003939 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003940}
3941
Steven Rostedt93eb6772009-04-15 13:24:06 -04003942#ifdef CONFIG_MODULES
Steven Rostedt32082302011-12-16 14:42:37 -05003943
3944#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
3945
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003946void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04003947{
3948 struct dyn_ftrace *rec;
Steven Rostedt32082302011-12-16 14:42:37 -05003949 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04003950 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05003951 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04003952
Steven Rostedt93eb6772009-04-15 13:24:06 -04003953 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003954
3955 if (ftrace_disabled)
3956 goto out_unlock;
3957
Steven Rostedt32082302011-12-16 14:42:37 -05003958 /*
3959 * Each module has its own ftrace_pages, remove
3960 * them from the list.
3961 */
3962 last_pg = &ftrace_pages_start;
3963 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
3964 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003965 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04003966 /*
Steven Rostedt32082302011-12-16 14:42:37 -05003967 * As core pages are first, the first
3968 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04003969 */
Steven Rostedt32082302011-12-16 14:42:37 -05003970 if (WARN_ON(pg == ftrace_pages_start))
3971 goto out_unlock;
3972
3973 /* Check if we are deleting the last page */
3974 if (pg == ftrace_pages)
3975 ftrace_pages = next_to_ftrace_page(last_pg);
3976
3977 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05003978 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3979 free_pages((unsigned long)pg->records, order);
3980 kfree(pg);
Steven Rostedt32082302011-12-16 14:42:37 -05003981 } else
3982 last_pg = &pg->next;
3983 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003984 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04003985 mutex_unlock(&ftrace_lock);
3986}
3987
3988static void ftrace_init_module(struct module *mod,
3989 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04003990{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04003991 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04003992 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003993 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04003994}
3995
Steven Rostedt93eb6772009-04-15 13:24:06 -04003996static int ftrace_module_notify(struct notifier_block *self,
3997 unsigned long val, void *data)
3998{
3999 struct module *mod = data;
4000
4001 switch (val) {
4002 case MODULE_STATE_COMING:
4003 ftrace_init_module(mod, mod->ftrace_callsites,
4004 mod->ftrace_callsites +
4005 mod->num_ftrace_callsites);
4006 break;
4007 case MODULE_STATE_GOING:
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004008 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04004009 break;
4010 }
4011
4012 return 0;
4013}
4014#else
4015static int ftrace_module_notify(struct notifier_block *self,
4016 unsigned long val, void *data)
4017{
4018 return 0;
4019}
4020#endif /* CONFIG_MODULES */
4021
4022struct notifier_block ftrace_module_nb = {
4023 .notifier_call = ftrace_module_notify,
Steven Rostedtc1bf08a2012-12-14 09:48:15 -05004024 .priority = INT_MAX, /* Run before anything that can use kprobes */
Steven Rostedt93eb6772009-04-15 13:24:06 -04004025};
4026
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004027extern unsigned long __start_mcount_loc[];
4028extern unsigned long __stop_mcount_loc[];
4029
4030void __init ftrace_init(void)
4031{
4032 unsigned long count, addr, flags;
4033 int ret;
4034
4035 /* Keep the ftrace pointer to the stub */
4036 addr = (unsigned long)ftrace_stub;
4037
4038 local_irq_save(flags);
4039 ftrace_dyn_arch_init(&addr);
4040 local_irq_restore(flags);
4041
4042 /* ftrace_dyn_arch_init places the return code in addr */
4043 if (addr)
4044 goto failed;
4045
4046 count = __stop_mcount_loc - __start_mcount_loc;
4047
4048 ret = ftrace_dyn_table_alloc(count);
4049 if (ret)
4050 goto failed;
4051
4052 last_ftrace_enabled = ftrace_enabled = 1;
4053
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004054 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08004055 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004056 __stop_mcount_loc);
4057
Steven Rostedt93eb6772009-04-15 13:24:06 -04004058 ret = register_module_notifier(&ftrace_module_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08004059 if (ret)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004060 pr_warning("Failed to register trace ftrace module notifier\n");
4061
Steven Rostedt2af15d62009-05-28 13:37:24 -04004062 set_ftrace_early_filters();
4063
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004064 return;
4065 failed:
4066 ftrace_disabled = 1;
4067}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004068
Steven Rostedt3d083392008-05-12 21:20:42 +02004069#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004070
Steven Rostedt2b499382011-05-03 22:49:52 -04004071static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04004072 .func = ftrace_stub,
Steven Rostedt47409742012-07-20 11:04:44 -04004073 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Steven Rostedtbd69c302011-05-03 21:55:54 -04004074};
4075
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004076static int __init ftrace_nodyn_init(void)
4077{
4078 ftrace_enabled = 1;
4079 return 0;
4080}
Steven Rostedt6f415672012-10-05 12:13:07 -04004081core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004082
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004083static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4084static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05004085/* Keep as macros so we do not need to define the commands */
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04004086# define ftrace_startup(ops, command) \
4087 ({ \
4088 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4089 0; \
4090 })
Steven Rostedtbd69c302011-05-03 21:55:54 -04004091# define ftrace_shutdown(ops, command) do { } while (0)
Ingo Molnarc7aafc52008-05-12 21:20:45 +02004092# define ftrace_startup_sysctl() do { } while (0)
4093# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04004094
4095static inline int
4096ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
4097{
4098 return 1;
4099}
4100
Steven Rostedt3d083392008-05-12 21:20:42 +02004101#endif /* CONFIG_DYNAMIC_FTRACE */
4102
Steven Rostedtb8489142011-05-04 09:27:52 -04004103static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004104ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004105 struct ftrace_ops *op, struct pt_regs *regs)
Jiri Olsae2484912012-02-15 15:51:48 +01004106{
Jiri Olsae2484912012-02-15 15:51:48 +01004107 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4108 return;
4109
4110 /*
4111 * Some of the ops may be dynamically allocated,
4112 * they must be freed after a synchronize_sched().
4113 */
4114 preempt_disable_notrace();
4115 trace_recursion_set(TRACE_CONTROL_BIT);
Steven Rostedt0a016402012-11-02 17:03:03 -04004116 do_for_each_ftrace_op(op, ftrace_control_list) {
Jiri Olsae2484912012-02-15 15:51:48 +01004117 if (!ftrace_function_local_disabled(op) &&
4118 ftrace_ops_test(op, ip))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004119 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04004120 } while_for_each_ftrace_op(op);
Jiri Olsae2484912012-02-15 15:51:48 +01004121 trace_recursion_clear(TRACE_CONTROL_BIT);
4122 preempt_enable_notrace();
4123}
4124
4125static struct ftrace_ops control_ops = {
4126 .func = ftrace_ops_control_func,
Steven Rostedt47409742012-07-20 11:04:44 -04004127 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Jiri Olsae2484912012-02-15 15:51:48 +01004128};
4129
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004130static inline void
4131__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004132 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004133{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004134 struct ftrace_ops *op;
Steven Rostedtb8489142011-05-04 09:27:52 -04004135
Steven Rostedtccf36722012-06-05 09:44:25 -04004136 if (function_trace_stop)
4137 return;
4138
Steven Rostedtb1cff0a2011-05-25 14:27:43 -04004139 if (unlikely(trace_recursion_test(TRACE_INTERNAL_BIT)))
4140 return;
4141
4142 trace_recursion_set(TRACE_INTERNAL_BIT);
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004143 /*
4144 * Some of the ops may be dynamically allocated,
4145 * they must be freed after a synchronize_sched().
4146 */
4147 preempt_disable_notrace();
Steven Rostedt0a016402012-11-02 17:03:03 -04004148 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedtb8489142011-05-04 09:27:52 -04004149 if (ftrace_ops_test(op, ip))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004150 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04004151 } while_for_each_ftrace_op(op);
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004152 preempt_enable_notrace();
Steven Rostedtb1cff0a2011-05-25 14:27:43 -04004153 trace_recursion_clear(TRACE_INTERNAL_BIT);
Steven Rostedtb8489142011-05-04 09:27:52 -04004154}
4155
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004156/*
4157 * Some archs only support passing ip and parent_ip. Even though
4158 * the list function ignores the op parameter, we do not want any
4159 * C side effects, where a function is called without the caller
4160 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004161 * Archs are to support both the regs and ftrace_ops at the same time.
4162 * If they support ftrace_ops, it is assumed they support regs.
4163 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09004164 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4165 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004166 * An architecture can pass partial regs with ftrace_ops and still
4167 * set the ARCH_SUPPORT_FTARCE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004168 */
4169#if ARCH_SUPPORTS_FTRACE_OPS
4170static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004171 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004172{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004173 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004174}
4175#else
4176static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4177{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004178 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004179}
4180#endif
4181
Steven Rostedte32d8952008-12-04 00:26:41 -05004182static void clear_ftrace_swapper(void)
4183{
4184 struct task_struct *p;
4185 int cpu;
4186
4187 get_online_cpus();
4188 for_each_online_cpu(cpu) {
4189 p = idle_task(cpu);
4190 clear_tsk_trace_trace(p);
4191 }
4192 put_online_cpus();
4193}
4194
4195static void set_ftrace_swapper(void)
4196{
4197 struct task_struct *p;
4198 int cpu;
4199
4200 get_online_cpus();
4201 for_each_online_cpu(cpu) {
4202 p = idle_task(cpu);
4203 set_tsk_trace_trace(p);
4204 }
4205 put_online_cpus();
4206}
4207
4208static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004209{
4210 struct task_struct *p;
4211
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004212 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05004213 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05004214 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05004215 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004216 rcu_read_unlock();
4217
Steven Rostedte32d8952008-12-04 00:26:41 -05004218 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004219}
4220
Steven Rostedte32d8952008-12-04 00:26:41 -05004221static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004222{
4223 struct task_struct *p;
4224
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004225 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004226 do_each_pid_task(pid, PIDTYPE_PID, p) {
4227 set_tsk_trace_trace(p);
4228 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004229 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004230}
4231
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004232static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004233{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004234 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004235 clear_ftrace_swapper();
4236 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004237 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05004238}
4239
4240static void set_ftrace_pid_task(struct pid *pid)
4241{
4242 if (pid == ftrace_swapper_pid)
4243 set_ftrace_swapper();
4244 else
4245 set_ftrace_pid(pid);
4246}
4247
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004248static int ftrace_pid_add(int p)
4249{
4250 struct pid *pid;
4251 struct ftrace_pid *fpid;
4252 int ret = -EINVAL;
4253
4254 mutex_lock(&ftrace_lock);
4255
4256 if (!p)
4257 pid = ftrace_swapper_pid;
4258 else
4259 pid = find_get_pid(p);
4260
4261 if (!pid)
4262 goto out;
4263
4264 ret = 0;
4265
4266 list_for_each_entry(fpid, &ftrace_pids, list)
4267 if (fpid->pid == pid)
4268 goto out_put;
4269
4270 ret = -ENOMEM;
4271
4272 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4273 if (!fpid)
4274 goto out_put;
4275
4276 list_add(&fpid->list, &ftrace_pids);
4277 fpid->pid = pid;
4278
4279 set_ftrace_pid_task(pid);
4280
4281 ftrace_update_pid_func();
4282 ftrace_startup_enable(0);
4283
4284 mutex_unlock(&ftrace_lock);
4285 return 0;
4286
4287out_put:
4288 if (pid != ftrace_swapper_pid)
4289 put_pid(pid);
4290
4291out:
4292 mutex_unlock(&ftrace_lock);
4293 return ret;
4294}
4295
4296static void ftrace_pid_reset(void)
4297{
4298 struct ftrace_pid *fpid, *safe;
4299
4300 mutex_lock(&ftrace_lock);
4301 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4302 struct pid *pid = fpid->pid;
4303
4304 clear_ftrace_pid_task(pid);
4305
4306 list_del(&fpid->list);
4307 kfree(fpid);
4308 }
4309
4310 ftrace_update_pid_func();
4311 ftrace_startup_enable(0);
4312
4313 mutex_unlock(&ftrace_lock);
4314}
4315
4316static void *fpid_start(struct seq_file *m, loff_t *pos)
4317{
4318 mutex_lock(&ftrace_lock);
4319
4320 if (list_empty(&ftrace_pids) && (!*pos))
4321 return (void *) 1;
4322
4323 return seq_list_start(&ftrace_pids, *pos);
4324}
4325
4326static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4327{
4328 if (v == (void *)1)
4329 return NULL;
4330
4331 return seq_list_next(v, &ftrace_pids, pos);
4332}
4333
4334static void fpid_stop(struct seq_file *m, void *p)
4335{
4336 mutex_unlock(&ftrace_lock);
4337}
4338
4339static int fpid_show(struct seq_file *m, void *v)
4340{
4341 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4342
4343 if (v == (void *)1) {
4344 seq_printf(m, "no pid\n");
4345 return 0;
4346 }
4347
4348 if (fpid->pid == ftrace_swapper_pid)
4349 seq_printf(m, "swapper tasks\n");
4350 else
4351 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4352
4353 return 0;
4354}
4355
4356static const struct seq_operations ftrace_pid_sops = {
4357 .start = fpid_start,
4358 .next = fpid_next,
4359 .stop = fpid_stop,
4360 .show = fpid_show,
4361};
4362
4363static int
4364ftrace_pid_open(struct inode *inode, struct file *file)
4365{
4366 int ret = 0;
4367
4368 if ((file->f_mode & FMODE_WRITE) &&
4369 (file->f_flags & O_TRUNC))
4370 ftrace_pid_reset();
4371
4372 if (file->f_mode & FMODE_READ)
4373 ret = seq_open(file, &ftrace_pid_sops);
4374
4375 return ret;
4376}
4377
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004378static ssize_t
4379ftrace_pid_write(struct file *filp, const char __user *ubuf,
4380 size_t cnt, loff_t *ppos)
4381{
Ingo Molnar457dc922009-11-23 11:03:28 +01004382 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004383 long val;
4384 int ret;
4385
4386 if (cnt >= sizeof(buf))
4387 return -EINVAL;
4388
4389 if (copy_from_user(&buf, ubuf, cnt))
4390 return -EFAULT;
4391
4392 buf[cnt] = 0;
4393
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004394 /*
4395 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4396 * to clean the filter quietly.
4397 */
Ingo Molnar457dc922009-11-23 11:03:28 +01004398 tmp = strstrip(buf);
4399 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004400 return 1;
4401
Daniel Walterbcd83ea2012-09-26 22:08:38 +02004402 ret = kstrtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004403 if (ret < 0)
4404 return ret;
4405
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004406 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004407
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004408 return ret ? ret : cnt;
4409}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004410
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004411static int
4412ftrace_pid_release(struct inode *inode, struct file *file)
4413{
4414 if (file->f_mode & FMODE_READ)
4415 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004416
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004417 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004418}
4419
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004420static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004421 .open = ftrace_pid_open,
4422 .write = ftrace_pid_write,
4423 .read = seq_read,
4424 .llseek = seq_lseek,
4425 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004426};
4427
4428static __init int ftrace_init_debugfs(void)
4429{
4430 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004431
4432 d_tracer = tracing_init_dentry();
4433 if (!d_tracer)
4434 return 0;
4435
4436 ftrace_init_dyn_debugfs(d_tracer);
4437
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004438 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4439 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04004440
4441 ftrace_profile_debugfs(d_tracer);
4442
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004443 return 0;
4444}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004445fs_initcall(ftrace_init_debugfs);
4446
Steven Rostedt3d083392008-05-12 21:20:42 +02004447/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004448 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004449 *
4450 * This function should be used by panic code. It stops ftrace
4451 * but in a not so nice way. If you need to simply kill ftrace
4452 * from a non-atomic section, use ftrace_kill.
4453 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004454void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004455{
4456 ftrace_disabled = 1;
4457 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004458 clear_ftrace_function();
4459}
4460
4461/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04004462 * Test if ftrace is dead or not.
4463 */
4464int ftrace_is_dead(void)
4465{
4466 return ftrace_disabled;
4467}
4468
4469/**
Steven Rostedt3d083392008-05-12 21:20:42 +02004470 * register_ftrace_function - register a function for profiling
4471 * @ops - ops structure that holds the function for profiling.
4472 *
4473 * Register a function to be called by all functions in the
4474 * kernel.
4475 *
4476 * Note: @ops->func and all the functions it calls must be labeled
4477 * with "notrace", otherwise it will go into a
4478 * recursive loop.
4479 */
4480int register_ftrace_function(struct ftrace_ops *ops)
4481{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004482 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004483
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004484 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004485
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004486 ret = __register_ftrace_function(ops);
Steven Rostedtb8489142011-05-04 09:27:52 -04004487 if (!ret)
Steven Rostedta1cd6172011-05-23 15:24:25 -04004488 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04004489
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004490 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02004491
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004492 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02004493}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004494EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02004495
4496/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01004497 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02004498 * @ops - ops structure that holds the function to unregister
4499 *
4500 * Unregister a function that was added to be called by ftrace profiling.
4501 */
4502int unregister_ftrace_function(struct ftrace_ops *ops)
4503{
4504 int ret;
4505
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004506 mutex_lock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004507 ret = __unregister_ftrace_function(ops);
Steven Rostedtb8489142011-05-04 09:27:52 -04004508 if (!ret)
4509 ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004510 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004511
4512 return ret;
4513}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004514EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004515
Ingo Molnare309b412008-05-12 21:20:51 +02004516int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004517ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004518 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004519 loff_t *ppos)
4520{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004521 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004522
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004523 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004524
Steven Rostedt45a4a232011-04-21 23:16:46 -04004525 if (unlikely(ftrace_disabled))
4526 goto out;
4527
4528 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004529
Li Zefana32c7762009-06-26 16:55:51 +08004530 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004531 goto out;
4532
Li Zefana32c7762009-06-26 16:55:51 +08004533 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004534
4535 if (ftrace_enabled) {
4536
4537 ftrace_startup_sysctl();
4538
4539 /* we are starting ftrace again */
Steven Rostedtb8489142011-05-04 09:27:52 -04004540 if (ftrace_ops_list != &ftrace_list_end) {
4541 if (ftrace_ops_list->next == &ftrace_list_end)
4542 ftrace_trace_function = ftrace_ops_list->func;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004543 else
Steven Rostedtb8489142011-05-04 09:27:52 -04004544 ftrace_trace_function = ftrace_ops_list_func;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004545 }
4546
4547 } else {
4548 /* stopping ftrace calls (just send to ftrace_stub) */
4549 ftrace_trace_function = ftrace_stub;
4550
4551 ftrace_shutdown_sysctl();
4552 }
4553
4554 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004555 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004556 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02004557}
Ingo Molnarf17845e2008-10-24 12:47:10 +02004558
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004559#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004560
Steven Rostedt597af812009-04-03 15:24:12 -04004561static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004562static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004563
Steven Rostedte49dc192008-12-02 23:50:05 -05004564int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4565{
4566 return 0;
4567}
4568
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004569/* The callbacks that hook a function */
4570trace_func_graph_ret_t ftrace_graph_return =
4571 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004572trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004573
4574/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4575static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4576{
4577 int i;
4578 int ret = 0;
4579 unsigned long flags;
4580 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4581 struct task_struct *g, *t;
4582
4583 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4584 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4585 * sizeof(struct ftrace_ret_stack),
4586 GFP_KERNEL);
4587 if (!ret_stack_list[i]) {
4588 start = 0;
4589 end = i;
4590 ret = -ENOMEM;
4591 goto free;
4592 }
4593 }
4594
4595 read_lock_irqsave(&tasklist_lock, flags);
4596 do_each_thread(g, t) {
4597 if (start == end) {
4598 ret = -EAGAIN;
4599 goto unlock;
4600 }
4601
4602 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01004603 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004604 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04004605 t->curr_ret_stack = -1;
4606 /* Make sure the tasks see the -1 first: */
4607 smp_wmb();
4608 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004609 }
4610 } while_each_thread(g, t);
4611
4612unlock:
4613 read_unlock_irqrestore(&tasklist_lock, flags);
4614free:
4615 for (i = start; i < end; i++)
4616 kfree(ret_stack_list[i]);
4617 return ret;
4618}
4619
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004620static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04004621ftrace_graph_probe_sched_switch(void *ignore,
4622 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004623{
4624 unsigned long long timestamp;
4625 int index;
4626
Steven Rostedtbe6f1642009-03-24 11:06:24 -04004627 /*
4628 * Does the user want to count the time a function was asleep.
4629 * If so, do not update the time stamps.
4630 */
4631 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4632 return;
4633
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004634 timestamp = trace_clock_local();
4635
4636 prev->ftrace_timestamp = timestamp;
4637
4638 /* only process tasks that we timestamped */
4639 if (!next->ftrace_timestamp)
4640 return;
4641
4642 /*
4643 * Update all the counters in next to make up for the
4644 * time next was sleeping.
4645 */
4646 timestamp -= next->ftrace_timestamp;
4647
4648 for (index = next->curr_ret_stack; index >= 0; index--)
4649 next->ret_stack[index].calltime += timestamp;
4650}
4651
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004652/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004653static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004654{
4655 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004656 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004657
4658 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4659 sizeof(struct ftrace_ret_stack *),
4660 GFP_KERNEL);
4661
4662 if (!ret_stack_list)
4663 return -ENOMEM;
4664
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004665 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04004666 for_each_online_cpu(cpu) {
4667 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05004668 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04004669 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004670
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004671 do {
4672 ret = alloc_retstack_tasklist(ret_stack_list);
4673 } while (ret == -EAGAIN);
4674
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004675 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04004676 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004677 if (ret)
4678 pr_info("ftrace_graph: Couldn't activate tracepoint"
4679 " probe to kernel_sched_switch\n");
4680 }
4681
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004682 kfree(ret_stack_list);
4683 return ret;
4684}
4685
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004686/*
4687 * Hibernation protection.
4688 * The state of the current task is too much unstable during
4689 * suspend/restore to disk. We want to protect against that.
4690 */
4691static int
4692ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4693 void *unused)
4694{
4695 switch (state) {
4696 case PM_HIBERNATION_PREPARE:
4697 pause_graph_tracing();
4698 break;
4699
4700 case PM_POST_HIBERNATION:
4701 unpause_graph_tracing();
4702 break;
4703 }
4704 return NOTIFY_DONE;
4705}
4706
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004707int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4708 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004709{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004710 int ret = 0;
4711
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004712 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004713
Steven Rostedt05ce5812009-03-24 00:18:31 -04004714 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04004715 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04004716 ret = -EBUSY;
4717 goto out;
4718 }
4719
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004720 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
4721 register_pm_notifier(&ftrace_suspend_notifier);
4722
Steven Rostedt597af812009-04-03 15:24:12 -04004723 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004724 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004725 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04004726 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004727 goto out;
4728 }
Steven Rostedte53a6312008-11-26 00:16:25 -05004729
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004730 ftrace_graph_return = retfunc;
4731 ftrace_graph_entry = entryfunc;
Steven Rostedte53a6312008-11-26 00:16:25 -05004732
Steven Rostedta1cd6172011-05-23 15:24:25 -04004733 ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004734
4735out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004736 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004737 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004738}
4739
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004740void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004741{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004742 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004743
Steven Rostedt597af812009-04-03 15:24:12 -04004744 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004745 goto out;
4746
Steven Rostedt597af812009-04-03 15:24:12 -04004747 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004748 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004749 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedtbd69c302011-05-03 21:55:54 -04004750 ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004751 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04004752 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004753
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004754 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004755 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004756}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004757
Steven Rostedt868baf02011-02-10 21:26:13 -05004758static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
4759
4760static void
4761graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
4762{
4763 atomic_set(&t->tracing_graph_pause, 0);
4764 atomic_set(&t->trace_overrun, 0);
4765 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004766 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05004767 smp_wmb();
4768 t->ret_stack = ret_stack;
4769}
4770
4771/*
4772 * Allocate a return stack for the idle task. May be the first
4773 * time through, or it may be done by CPU hotplug online.
4774 */
4775void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
4776{
4777 t->curr_ret_stack = -1;
4778 /*
4779 * The idle task has no parent, it either has its own
4780 * stack or no stack at all.
4781 */
4782 if (t->ret_stack)
4783 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
4784
4785 if (ftrace_graph_active) {
4786 struct ftrace_ret_stack *ret_stack;
4787
4788 ret_stack = per_cpu(idle_ret_stack, cpu);
4789 if (!ret_stack) {
4790 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4791 * sizeof(struct ftrace_ret_stack),
4792 GFP_KERNEL);
4793 if (!ret_stack)
4794 return;
4795 per_cpu(idle_ret_stack, cpu) = ret_stack;
4796 }
4797 graph_init_task(t, ret_stack);
4798 }
4799}
4800
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004801/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004802void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004803{
Steven Rostedt84047e32009-06-02 16:51:55 -04004804 /* Make sure we do not use the parent ret_stack */
4805 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05004806 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04004807
Steven Rostedt597af812009-04-03 15:24:12 -04004808 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04004809 struct ftrace_ret_stack *ret_stack;
4810
4811 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004812 * sizeof(struct ftrace_ret_stack),
4813 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04004814 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004815 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05004816 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04004817 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004818}
4819
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004820void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004821{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004822 struct ftrace_ret_stack *ret_stack = t->ret_stack;
4823
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004824 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004825 /* NULL must become visible to IRQs before we free it: */
4826 barrier();
4827
4828 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004829}
Steven Rostedt14a866c2008-12-02 23:50:02 -05004830
4831void ftrace_graph_stop(void)
4832{
4833 ftrace_stop();
4834}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004835#endif