blob: 4451aa3a55a0f56400d8ae11b12700af1bb53ecf [file] [log] [blame]
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02001/*
2 * Infrastructure for profiling code inserted by 'gcc -pg'.
3 *
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
6 *
7 * Originally ported from the -rt patch by:
8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
9 *
10 * Based on code in the latency_tracer, that is:
11 *
12 * Copyright (C) 2004-2006 Ingo Molnar
13 * Copyright (C) 2004 William Lee Irwin III
14 */
15
Steven Rostedt3d083392008-05-12 21:20:42 +020016#include <linux/stop_machine.h>
17#include <linux/clocksource.h>
18#include <linux/kallsyms.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020019#include <linux/seq_file.h>
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -080020#include <linux/suspend.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020021#include <linux/debugfs.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020022#include <linux/hardirq.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010023#include <linux/kthread.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020024#include <linux/uaccess.h>
Steven Rostedt5855fea2011-12-16 19:27:42 -050025#include <linux/bsearch.h>
Paul Gortmaker56d82e02011-05-26 17:53:52 -040026#include <linux/module.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010027#include <linux/ftrace.h>
Steven Rostedtb0fc4942008-05-12 21:20:43 +020028#include <linux/sysctl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020030#include <linux/ctype.h>
Steven Rostedt68950612011-12-16 17:06:45 -050031#include <linux/sort.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020032#include <linux/list.h>
Steven Rostedt59df055f2009-02-14 15:29:06 -050033#include <linux/hash.h>
Paul E. McKenney3f379b02010-03-05 15:03:25 -080034#include <linux/rcupdate.h>
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020035
Steven Rostedtad8d75f2009-04-14 19:39:12 -040036#include <trace/events/sched.h>
Steven Rostedt8aef2d22009-03-24 01:10:15 -040037
Steven Rostedt2af15d62009-05-28 13:37:24 -040038#include <asm/setup.h>
Abhishek Sagar395a59d2008-06-21 23:47:27 +053039
Steven Rostedt0706f1c2009-03-23 23:12:58 -040040#include "trace_output.h"
Steven Rostedtbac429f2009-03-20 12:50:56 -040041#include "trace_stat.h"
Steven Rostedt3d083392008-05-12 21:20:42 +020042
Steven Rostedt6912896e2008-10-23 09:33:03 -040043#define FTRACE_WARN_ON(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040044 ({ \
45 int ___r = cond; \
46 if (WARN_ON(___r)) \
Steven Rostedt6912896e2008-10-23 09:33:03 -040047 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040048 ___r; \
49 })
Steven Rostedt6912896e2008-10-23 09:33:03 -040050
51#define FTRACE_WARN_ON_ONCE(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040052 ({ \
53 int ___r = cond; \
54 if (WARN_ON_ONCE(___r)) \
Steven Rostedt6912896e2008-10-23 09:33:03 -040055 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040056 ___r; \
57 })
Steven Rostedt6912896e2008-10-23 09:33:03 -040058
Steven Rostedt8fc0c702009-02-16 15:28:00 -050059/* hash bits for specific function selection */
60#define FTRACE_HASH_BITS 7
61#define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
Steven Rostedt33dc9b12011-05-02 17:34:47 -040062#define FTRACE_HASH_DEFAULT_BITS 10
63#define FTRACE_HASH_MAX_BITS 12
Steven Rostedt8fc0c702009-02-16 15:28:00 -050064
Jiri Olsae2484912012-02-15 15:51:48 +010065#define FL_GLOBAL_CONTROL_MASK (FTRACE_OPS_FL_GLOBAL | FTRACE_OPS_FL_CONTROL)
66
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040067static struct ftrace_ops ftrace_list_end __read_mostly = {
68 .func = ftrace_stub,
Steven Rostedt47409742012-07-20 11:04:44 -040069 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040070};
71
Steven Rostedt4eebcc82008-05-12 21:20:48 +020072/* ftrace_enabled is a method to turn ftrace on or off */
73int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020074static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020075
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050076/* Quick disabling of function tracer. */
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040077int function_trace_stop __read_mostly;
78
79/* Current function tracing op */
80struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050081
jolsa@redhat.com756d17e2009-10-13 16:33:52 -040082/* List for set_ftrace_pid's pids. */
83LIST_HEAD(ftrace_pids);
84struct ftrace_pid {
85 struct list_head list;
86 struct pid *pid;
87};
88
Steven Rostedt4eebcc82008-05-12 21:20:48 +020089/*
90 * ftrace_disabled is set when an anomaly is discovered.
91 * ftrace_disabled is much stronger than ftrace_enabled.
92 */
93static int ftrace_disabled __read_mostly;
94
Steven Rostedt52baf112009-02-14 01:15:39 -050095static DEFINE_MUTEX(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +020096
Steven Rostedtb8489142011-05-04 09:27:52 -040097static struct ftrace_ops *ftrace_global_list __read_mostly = &ftrace_list_end;
Jiri Olsae2484912012-02-15 15:51:48 +010098static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -040099static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200100ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500101ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
Steven Rostedt2b499382011-05-03 22:49:52 -0400102static struct ftrace_ops global_ops;
Jiri Olsae2484912012-02-15 15:51:48 +0100103static struct ftrace_ops control_ops;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200104
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400105#if ARCH_SUPPORTS_FTRACE_OPS
106static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400107 struct ftrace_ops *op, struct pt_regs *regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400108#else
109/* See comment below, where ftrace_ops_list_func is defined */
110static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
111#define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
112#endif
Steven Rostedtb8489142011-05-04 09:27:52 -0400113
Steven Rostedtea701f12012-07-20 13:08:05 -0400114/**
115 * ftrace_nr_registered_ops - return number of ops registered
116 *
117 * Returns the number of ftrace_ops registered and tracing functions
118 */
119int ftrace_nr_registered_ops(void)
120{
121 struct ftrace_ops *ops;
122 int cnt = 0;
123
124 mutex_lock(&ftrace_lock);
125
126 for (ops = ftrace_ops_list;
127 ops != &ftrace_list_end; ops = ops->next)
128 cnt++;
129
130 mutex_unlock(&ftrace_lock);
131
132 return cnt;
133}
134
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800135/*
Steven Rostedtb8489142011-05-04 09:27:52 -0400136 * Traverse the ftrace_global_list, invoking all entries. The reason that we
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800137 * can use rcu_dereference_raw() is that elements removed from this list
138 * are simply leaked, so there is no need to interact with a grace-period
139 * mechanism. The rcu_dereference_raw() calls are needed to handle
Steven Rostedtb8489142011-05-04 09:27:52 -0400140 * concurrent insertions into the ftrace_global_list.
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800141 *
142 * Silly Alpha and silly pointer-speculation compiler optimizations!
143 */
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400144static void
145ftrace_global_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400146 struct ftrace_ops *op, struct pt_regs *regs)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200147{
Steven Rostedtb1cff0a2011-05-25 14:27:43 -0400148 if (unlikely(trace_recursion_test(TRACE_GLOBAL_BIT)))
149 return;
150
151 trace_recursion_set(TRACE_GLOBAL_BIT);
152 op = rcu_dereference_raw(ftrace_global_list); /*see above*/
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200153 while (op != &ftrace_list_end) {
Steven Rostedta1e2e312011-08-09 12:50:46 -0400154 op->func(ip, parent_ip, op, regs);
Paul E. McKenney3f379b02010-03-05 15:03:25 -0800155 op = rcu_dereference_raw(op->next); /*see above*/
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200156 };
Steven Rostedtb1cff0a2011-05-25 14:27:43 -0400157 trace_recursion_clear(TRACE_GLOBAL_BIT);
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200158}
159
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400160static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400161 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500162{
Steven Rostedt0ef8cde2008-12-03 15:36:58 -0500163 if (!test_tsk_trace_trace(current))
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500164 return;
165
Steven Rostedta1e2e312011-08-09 12:50:46 -0400166 ftrace_pid_function(ip, parent_ip, op, regs);
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500167}
168
169static void set_ftrace_pid_function(ftrace_func_t func)
170{
171 /* do not set ftrace_pid_function to itself! */
172 if (func != ftrace_pid_func)
173 ftrace_pid_function = func;
174}
175
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200176/**
Steven Rostedt3d083392008-05-12 21:20:42 +0200177 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200178 *
Steven Rostedt3d083392008-05-12 21:20:42 +0200179 * This NULLs the ftrace function and in essence stops
180 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200181 */
Steven Rostedt3d083392008-05-12 21:20:42 +0200182void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200183{
Steven Rostedt3d083392008-05-12 21:20:42 +0200184 ftrace_trace_function = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500185 ftrace_pid_function = ftrace_stub;
Steven Rostedt3d083392008-05-12 21:20:42 +0200186}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200187
Jiri Olsae2484912012-02-15 15:51:48 +0100188static void control_ops_disable_all(struct ftrace_ops *ops)
189{
190 int cpu;
191
192 for_each_possible_cpu(cpu)
193 *per_cpu_ptr(ops->disabled, cpu) = 1;
194}
195
196static int control_ops_alloc(struct ftrace_ops *ops)
197{
198 int __percpu *disabled;
199
200 disabled = alloc_percpu(int);
201 if (!disabled)
202 return -ENOMEM;
203
204 ops->disabled = disabled;
205 control_ops_disable_all(ops);
206 return 0;
207}
208
209static void control_ops_free(struct ftrace_ops *ops)
210{
211 free_percpu(ops->disabled);
212}
213
Steven Rostedt2b499382011-05-03 22:49:52 -0400214static void update_global_ops(void)
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400215{
216 ftrace_func_t func;
217
218 /*
219 * If there's only one function registered, then call that
220 * function directly. Otherwise, we need to iterate over the
221 * registered callers.
222 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400223 if (ftrace_global_list == &ftrace_list_end ||
224 ftrace_global_list->next == &ftrace_list_end)
225 func = ftrace_global_list->func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400226 else
Steven Rostedtb8489142011-05-04 09:27:52 -0400227 func = ftrace_global_list_func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400228
229 /* If we filter on pids, update to use the pid function */
230 if (!list_empty(&ftrace_pids)) {
231 set_ftrace_pid_function(func);
232 func = ftrace_pid_func;
233 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400234
235 global_ops.func = func;
236}
237
238static void update_ftrace_function(void)
239{
240 ftrace_func_t func;
241
242 update_global_ops();
243
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400244 /*
245 * If we are at the end of the list and this ops is
Steven Rostedt47409742012-07-20 11:04:44 -0400246 * recursion safe and not dynamic and the arch supports passing ops,
247 * then have the mcount trampoline call the function directly.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400248 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400249 if (ftrace_ops_list == &ftrace_list_end ||
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400250 (ftrace_ops_list->next == &ftrace_list_end &&
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400251 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC) &&
Steven Rostedt47409742012-07-20 11:04:44 -0400252 (ftrace_ops_list->flags & FTRACE_OPS_FL_RECURSION_SAFE) &&
Steven Rostedtccf36722012-06-05 09:44:25 -0400253 !FTRACE_FORCE_LIST_FUNC)) {
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400254 /* Set the ftrace_ops that the arch callback uses */
255 if (ftrace_ops_list == &global_ops)
256 function_trace_op = ftrace_global_list;
257 else
258 function_trace_op = ftrace_ops_list;
Steven Rostedtb8489142011-05-04 09:27:52 -0400259 func = ftrace_ops_list->func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400260 } else {
261 /* Just use the default ftrace_ops */
262 function_trace_op = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400263 func = ftrace_ops_list_func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400264 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400265
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400266 ftrace_trace_function = func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400267}
268
Steven Rostedt2b499382011-05-03 22:49:52 -0400269static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200270{
Steven Rostedt2b499382011-05-03 22:49:52 -0400271 ops->next = *list;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200272 /*
Steven Rostedtb8489142011-05-04 09:27:52 -0400273 * We are entering ops into the list but another
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200274 * CPU might be walking that list. We need to make sure
275 * the ops->next pointer is valid before another CPU sees
Steven Rostedtb8489142011-05-04 09:27:52 -0400276 * the ops pointer included into the list.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200277 */
Steven Rostedt2b499382011-05-03 22:49:52 -0400278 rcu_assign_pointer(*list, ops);
279}
Steven Rostedt3d083392008-05-12 21:20:42 +0200280
Steven Rostedt2b499382011-05-03 22:49:52 -0400281static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
282{
283 struct ftrace_ops **p;
284
285 /*
286 * If we are removing the last function, then simply point
287 * to the ftrace_stub.
288 */
289 if (*list == ops && ops->next == &ftrace_list_end) {
290 *list = &ftrace_list_end;
291 return 0;
292 }
293
294 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
295 if (*p == ops)
296 break;
297
298 if (*p != ops)
299 return -1;
300
301 *p = (*p)->next;
302 return 0;
303}
304
Jiri Olsae2484912012-02-15 15:51:48 +0100305static void add_ftrace_list_ops(struct ftrace_ops **list,
306 struct ftrace_ops *main_ops,
307 struct ftrace_ops *ops)
308{
309 int first = *list == &ftrace_list_end;
310 add_ftrace_ops(list, ops);
311 if (first)
312 add_ftrace_ops(&ftrace_ops_list, main_ops);
313}
314
315static int remove_ftrace_list_ops(struct ftrace_ops **list,
316 struct ftrace_ops *main_ops,
317 struct ftrace_ops *ops)
318{
319 int ret = remove_ftrace_ops(list, ops);
320 if (!ret && *list == &ftrace_list_end)
321 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
322 return ret;
323}
324
Steven Rostedt2b499382011-05-03 22:49:52 -0400325static int __register_ftrace_function(struct ftrace_ops *ops)
326{
Borislav Petkov8d240dd2012-03-29 19:11:40 +0200327 if (unlikely(ftrace_disabled))
Steven Rostedt2b499382011-05-03 22:49:52 -0400328 return -ENODEV;
329
330 if (FTRACE_WARN_ON(ops == &global_ops))
331 return -EINVAL;
332
Steven Rostedtb8489142011-05-04 09:27:52 -0400333 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
334 return -EBUSY;
335
Jiri Olsae2484912012-02-15 15:51:48 +0100336 /* We don't support both control and global flags set. */
337 if ((ops->flags & FL_GLOBAL_CONTROL_MASK) == FL_GLOBAL_CONTROL_MASK)
338 return -EINVAL;
339
Steven Rostedt08f6fba2012-04-30 16:20:23 -0400340#ifndef ARCH_SUPPORTS_FTRACE_SAVE_REGS
341 /*
342 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
343 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
344 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
345 */
346 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
347 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
348 return -EINVAL;
349
350 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
351 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
352#endif
353
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400354 if (!core_kernel_data((unsigned long)ops))
355 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
356
Steven Rostedtb8489142011-05-04 09:27:52 -0400357 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100358 add_ftrace_list_ops(&ftrace_global_list, &global_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400359 ops->flags |= FTRACE_OPS_FL_ENABLED;
Jiri Olsae2484912012-02-15 15:51:48 +0100360 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
361 if (control_ops_alloc(ops))
362 return -ENOMEM;
363 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400364 } else
365 add_ftrace_ops(&ftrace_ops_list, ops);
366
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400367 if (ftrace_enabled)
368 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200369
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200370 return 0;
371}
372
Ingo Molnare309b412008-05-12 21:20:51 +0200373static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200374{
Steven Rostedt2b499382011-05-03 22:49:52 -0400375 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200376
Steven Rostedt2b499382011-05-03 22:49:52 -0400377 if (ftrace_disabled)
378 return -ENODEV;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200379
Steven Rostedtb8489142011-05-04 09:27:52 -0400380 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
381 return -EBUSY;
382
Steven Rostedt2b499382011-05-03 22:49:52 -0400383 if (FTRACE_WARN_ON(ops == &global_ops))
384 return -EINVAL;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200385
Steven Rostedtb8489142011-05-04 09:27:52 -0400386 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100387 ret = remove_ftrace_list_ops(&ftrace_global_list,
388 &global_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400389 if (!ret)
390 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Jiri Olsae2484912012-02-15 15:51:48 +0100391 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
392 ret = remove_ftrace_list_ops(&ftrace_control_list,
393 &control_ops, ops);
394 if (!ret) {
395 /*
396 * The ftrace_ops is now removed from the list,
397 * so there'll be no new users. We must ensure
398 * all current users are done before we free
399 * the control data.
400 */
401 synchronize_sched();
402 control_ops_free(ops);
403 }
Steven Rostedtb8489142011-05-04 09:27:52 -0400404 } else
405 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
406
Steven Rostedt2b499382011-05-03 22:49:52 -0400407 if (ret < 0)
408 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400409
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400410 if (ftrace_enabled)
411 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200412
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400413 /*
414 * Dynamic ops may be freed, we must make sure that all
415 * callers are done before leaving this function.
416 */
417 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
418 synchronize_sched();
419
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500420 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200421}
422
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500423static void ftrace_update_pid_func(void)
424{
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400425 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500426 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900427 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500428
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400429 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500430}
431
Steven Rostedt493762f2009-03-23 17:12:36 -0400432#ifdef CONFIG_FUNCTION_PROFILER
433struct ftrace_profile {
434 struct hlist_node node;
435 unsigned long ip;
436 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400437#ifdef CONFIG_FUNCTION_GRAPH_TRACER
438 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400439 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400440#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400441};
442
443struct ftrace_profile_page {
444 struct ftrace_profile_page *next;
445 unsigned long index;
446 struct ftrace_profile records[];
447};
448
Steven Rostedtcafb1682009-03-24 20:50:39 -0400449struct ftrace_profile_stat {
450 atomic_t disabled;
451 struct hlist_head *hash;
452 struct ftrace_profile_page *pages;
453 struct ftrace_profile_page *start;
454 struct tracer_stat stat;
455};
456
Steven Rostedt493762f2009-03-23 17:12:36 -0400457#define PROFILE_RECORDS_SIZE \
458 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
459
460#define PROFILES_PER_PAGE \
461 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
462
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400463static int ftrace_profile_bits __read_mostly;
464static int ftrace_profile_enabled __read_mostly;
465
466/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400467static DEFINE_MUTEX(ftrace_profile_lock);
468
Steven Rostedtcafb1682009-03-24 20:50:39 -0400469static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400470
471#define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
472
Steven Rostedt493762f2009-03-23 17:12:36 -0400473static void *
474function_stat_next(void *v, int idx)
475{
476 struct ftrace_profile *rec = v;
477 struct ftrace_profile_page *pg;
478
479 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
480
481 again:
Li Zefan0296e422009-06-26 11:15:37 +0800482 if (idx != 0)
483 rec++;
484
Steven Rostedt493762f2009-03-23 17:12:36 -0400485 if ((void *)rec >= (void *)&pg->records[pg->index]) {
486 pg = pg->next;
487 if (!pg)
488 return NULL;
489 rec = &pg->records[0];
490 if (!rec->counter)
491 goto again;
492 }
493
494 return rec;
495}
496
497static void *function_stat_start(struct tracer_stat *trace)
498{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400499 struct ftrace_profile_stat *stat =
500 container_of(trace, struct ftrace_profile_stat, stat);
501
502 if (!stat || !stat->start)
503 return NULL;
504
505 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400506}
507
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400508#ifdef CONFIG_FUNCTION_GRAPH_TRACER
509/* function graph compares on total time */
510static int function_stat_cmp(void *p1, void *p2)
511{
512 struct ftrace_profile *a = p1;
513 struct ftrace_profile *b = p2;
514
515 if (a->time < b->time)
516 return -1;
517 if (a->time > b->time)
518 return 1;
519 else
520 return 0;
521}
522#else
523/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400524static int function_stat_cmp(void *p1, void *p2)
525{
526 struct ftrace_profile *a = p1;
527 struct ftrace_profile *b = p2;
528
529 if (a->counter < b->counter)
530 return -1;
531 if (a->counter > b->counter)
532 return 1;
533 else
534 return 0;
535}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400536#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400537
538static int function_stat_headers(struct seq_file *m)
539{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400540#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400541 seq_printf(m, " Function "
Chase Douglase330b3b2010-04-26 14:02:05 -0400542 "Hit Time Avg s^2\n"
Steven Rostedt34886c82009-03-25 21:00:47 -0400543 " -------- "
Chase Douglase330b3b2010-04-26 14:02:05 -0400544 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400545#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400546 seq_printf(m, " Function Hit\n"
547 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400548#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400549 return 0;
550}
551
552static int function_stat_show(struct seq_file *m, void *v)
553{
554 struct ftrace_profile *rec = v;
555 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800556 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400557#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400558 static struct trace_seq s;
559 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400560 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400561#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800562 mutex_lock(&ftrace_profile_lock);
563
564 /* we raced with function_profile_reset() */
565 if (unlikely(rec->counter == 0)) {
566 ret = -EBUSY;
567 goto out;
568 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400569
570 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400571 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400572
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400573#ifdef CONFIG_FUNCTION_GRAPH_TRACER
574 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400575 avg = rec->time;
576 do_div(avg, rec->counter);
577
Chase Douglase330b3b2010-04-26 14:02:05 -0400578 /* Sample standard deviation (s^2) */
579 if (rec->counter <= 1)
580 stddev = 0;
581 else {
582 stddev = rec->time_squared - rec->counter * avg * avg;
583 /*
584 * Divide only 1000 for ns^2 -> us^2 conversion.
585 * trace_print_graph_duration will divide 1000 again.
586 */
587 do_div(stddev, (rec->counter - 1) * 1000);
588 }
589
Steven Rostedt34886c82009-03-25 21:00:47 -0400590 trace_seq_init(&s);
591 trace_print_graph_duration(rec->time, &s);
592 trace_seq_puts(&s, " ");
593 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400594 trace_seq_puts(&s, " ");
595 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400596 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400597#endif
598 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800599out:
600 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400601
Li Zefan3aaba202010-08-23 16:50:12 +0800602 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400603}
604
Steven Rostedtcafb1682009-03-24 20:50:39 -0400605static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400606{
607 struct ftrace_profile_page *pg;
608
Steven Rostedtcafb1682009-03-24 20:50:39 -0400609 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400610
611 while (pg) {
612 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
613 pg->index = 0;
614 pg = pg->next;
615 }
616
Steven Rostedtcafb1682009-03-24 20:50:39 -0400617 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400618 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
619}
620
Steven Rostedtcafb1682009-03-24 20:50:39 -0400621int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400622{
623 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400624 int functions;
625 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400626 int i;
627
628 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400629 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400630 return 0;
631
Steven Rostedtcafb1682009-03-24 20:50:39 -0400632 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
633 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400634 return -ENOMEM;
635
Steven Rostedt318e0a72009-03-25 20:06:34 -0400636#ifdef CONFIG_DYNAMIC_FTRACE
637 functions = ftrace_update_tot_cnt;
638#else
639 /*
640 * We do not know the number of functions that exist because
641 * dynamic tracing is what counts them. With past experience
642 * we have around 20K functions. That should be more than enough.
643 * It is highly unlikely we will execute every function in
644 * the kernel.
645 */
646 functions = 20000;
647#endif
648
Steven Rostedtcafb1682009-03-24 20:50:39 -0400649 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400650
Steven Rostedt318e0a72009-03-25 20:06:34 -0400651 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
652
653 for (i = 0; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400654 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400655 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400656 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400657 pg = pg->next;
658 }
659
660 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400661
662 out_free:
663 pg = stat->start;
664 while (pg) {
665 unsigned long tmp = (unsigned long)pg;
666
667 pg = pg->next;
668 free_page(tmp);
669 }
670
671 free_page((unsigned long)stat->pages);
672 stat->pages = NULL;
673 stat->start = NULL;
674
675 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400676}
677
Steven Rostedtcafb1682009-03-24 20:50:39 -0400678static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400679{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400680 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400681 int size;
682
Steven Rostedtcafb1682009-03-24 20:50:39 -0400683 stat = &per_cpu(ftrace_profile_stats, cpu);
684
685 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400686 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400687 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400688 return 0;
689 }
690
691 /*
692 * We are profiling all functions, but usually only a few thousand
693 * functions are hit. We'll make a hash of 1024 items.
694 */
695 size = FTRACE_PROFILE_HASH_SIZE;
696
Steven Rostedtcafb1682009-03-24 20:50:39 -0400697 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400698
Steven Rostedtcafb1682009-03-24 20:50:39 -0400699 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400700 return -ENOMEM;
701
Steven Rostedtcafb1682009-03-24 20:50:39 -0400702 if (!ftrace_profile_bits) {
703 size--;
Steven Rostedt493762f2009-03-23 17:12:36 -0400704
Steven Rostedtcafb1682009-03-24 20:50:39 -0400705 for (; size; size >>= 1)
706 ftrace_profile_bits++;
707 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400708
Steven Rostedt318e0a72009-03-25 20:06:34 -0400709 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400710 if (ftrace_profile_pages_init(stat) < 0) {
711 kfree(stat->hash);
712 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400713 return -ENOMEM;
714 }
715
716 return 0;
717}
718
Steven Rostedtcafb1682009-03-24 20:50:39 -0400719static int ftrace_profile_init(void)
720{
721 int cpu;
722 int ret = 0;
723
724 for_each_online_cpu(cpu) {
725 ret = ftrace_profile_init_cpu(cpu);
726 if (ret)
727 break;
728 }
729
730 return ret;
731}
732
Steven Rostedt493762f2009-03-23 17:12:36 -0400733/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400734static struct ftrace_profile *
735ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400736{
737 struct ftrace_profile *rec;
738 struct hlist_head *hhd;
739 struct hlist_node *n;
740 unsigned long key;
741
742 key = hash_long(ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400743 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400744
745 if (hlist_empty(hhd))
746 return NULL;
747
748 hlist_for_each_entry_rcu(rec, n, hhd, node) {
749 if (rec->ip == ip)
750 return rec;
751 }
752
753 return NULL;
754}
755
Steven Rostedtcafb1682009-03-24 20:50:39 -0400756static void ftrace_add_profile(struct ftrace_profile_stat *stat,
757 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400758{
759 unsigned long key;
760
761 key = hash_long(rec->ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400762 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400763}
764
Steven Rostedt318e0a72009-03-25 20:06:34 -0400765/*
766 * The memory is already allocated, this simply finds a new record to use.
767 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400768static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400769ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400770{
771 struct ftrace_profile *rec = NULL;
772
Steven Rostedt318e0a72009-03-25 20:06:34 -0400773 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400774 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400775 goto out;
776
Steven Rostedt493762f2009-03-23 17:12:36 -0400777 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400778 * Try to find the function again since an NMI
779 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400780 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400781 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400782 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400783 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400784
Steven Rostedtcafb1682009-03-24 20:50:39 -0400785 if (stat->pages->index == PROFILES_PER_PAGE) {
786 if (!stat->pages->next)
787 goto out;
788 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400789 }
790
Steven Rostedtcafb1682009-03-24 20:50:39 -0400791 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400792 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400793 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400794
Steven Rostedt493762f2009-03-23 17:12:36 -0400795 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400796 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400797
798 return rec;
799}
800
Steven Rostedt493762f2009-03-23 17:12:36 -0400801static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400802function_profile_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400803 struct ftrace_ops *ops, struct pt_regs *regs)
Steven Rostedt493762f2009-03-23 17:12:36 -0400804{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400805 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400806 struct ftrace_profile *rec;
807 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400808
809 if (!ftrace_profile_enabled)
810 return;
811
Steven Rostedt493762f2009-03-23 17:12:36 -0400812 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400813
814 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400815 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400816 goto out;
817
818 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400819 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400820 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400821 if (!rec)
822 goto out;
823 }
824
825 rec->counter++;
826 out:
827 local_irq_restore(flags);
828}
829
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400830#ifdef CONFIG_FUNCTION_GRAPH_TRACER
831static int profile_graph_entry(struct ftrace_graph_ent *trace)
832{
Steven Rostedta1e2e312011-08-09 12:50:46 -0400833 function_profile_call(trace->func, 0, NULL, NULL);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400834 return 1;
835}
836
837static void profile_graph_return(struct ftrace_graph_ret *trace)
838{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400839 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400840 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400841 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400842 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400843
844 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400845 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400846 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400847 goto out;
848
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400849 /* If the calltime was zero'd ignore it */
850 if (!trace->calltime)
851 goto out;
852
Steven Rostedta2a16d62009-03-24 23:17:58 -0400853 calltime = trace->rettime - trace->calltime;
854
855 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
856 int index;
857
858 index = trace->depth;
859
860 /* Append this call time to the parent time to subtract */
861 if (index)
862 current->ret_stack[index - 1].subtime += calltime;
863
864 if (current->ret_stack[index].subtime < calltime)
865 calltime -= current->ret_stack[index].subtime;
866 else
867 calltime = 0;
868 }
869
Steven Rostedtcafb1682009-03-24 20:50:39 -0400870 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400871 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400872 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400873 rec->time_squared += calltime * calltime;
874 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400875
Steven Rostedtcafb1682009-03-24 20:50:39 -0400876 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400877 local_irq_restore(flags);
878}
879
880static int register_ftrace_profiler(void)
881{
882 return register_ftrace_graph(&profile_graph_return,
883 &profile_graph_entry);
884}
885
886static void unregister_ftrace_profiler(void)
887{
888 unregister_ftrace_graph();
889}
890#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100891static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400892 .func = function_profile_call,
Steven Rostedt47409742012-07-20 11:04:44 -0400893 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Steven Rostedt493762f2009-03-23 17:12:36 -0400894};
895
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400896static int register_ftrace_profiler(void)
897{
898 return register_ftrace_function(&ftrace_profile_ops);
899}
900
901static void unregister_ftrace_profiler(void)
902{
903 unregister_ftrace_function(&ftrace_profile_ops);
904}
905#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
906
Steven Rostedt493762f2009-03-23 17:12:36 -0400907static ssize_t
908ftrace_profile_write(struct file *filp, const char __user *ubuf,
909 size_t cnt, loff_t *ppos)
910{
911 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400912 int ret;
913
Peter Huewe22fe9b52011-06-07 21:58:27 +0200914 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
915 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400916 return ret;
917
918 val = !!val;
919
920 mutex_lock(&ftrace_profile_lock);
921 if (ftrace_profile_enabled ^ val) {
922 if (val) {
923 ret = ftrace_profile_init();
924 if (ret < 0) {
925 cnt = ret;
926 goto out;
927 }
928
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400929 ret = register_ftrace_profiler();
930 if (ret < 0) {
931 cnt = ret;
932 goto out;
933 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400934 ftrace_profile_enabled = 1;
935 } else {
936 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400937 /*
938 * unregister_ftrace_profiler calls stop_machine
939 * so this acts like an synchronize_sched.
940 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400941 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400942 }
943 }
944 out:
945 mutex_unlock(&ftrace_profile_lock);
946
Jiri Olsacf8517c2009-10-23 19:36:16 -0400947 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400948
949 return cnt;
950}
951
952static ssize_t
953ftrace_profile_read(struct file *filp, char __user *ubuf,
954 size_t cnt, loff_t *ppos)
955{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400956 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400957 int r;
958
959 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
960 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
961}
962
963static const struct file_operations ftrace_profile_fops = {
964 .open = tracing_open_generic,
965 .read = ftrace_profile_read,
966 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200967 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -0400968};
969
Steven Rostedtcafb1682009-03-24 20:50:39 -0400970/* used to initialize the real stat files */
971static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400972 .name = "functions",
973 .stat_start = function_stat_start,
974 .stat_next = function_stat_next,
975 .stat_cmp = function_stat_cmp,
976 .stat_headers = function_stat_headers,
977 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -0400978};
979
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400980static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400981{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400982 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400983 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400984 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -0400985 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400986 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -0400987
Steven Rostedtcafb1682009-03-24 20:50:39 -0400988 for_each_possible_cpu(cpu) {
989 stat = &per_cpu(ftrace_profile_stats, cpu);
990
991 /* allocate enough for function name + cpu number */
992 name = kmalloc(32, GFP_KERNEL);
993 if (!name) {
994 /*
995 * The files created are permanent, if something happens
996 * we still do not free memory.
997 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400998 WARN(1,
999 "Could not allocate stat file for cpu %d\n",
1000 cpu);
1001 return;
1002 }
1003 stat->stat = function_stats;
1004 snprintf(name, 32, "function%d", cpu);
1005 stat->stat.name = name;
1006 ret = register_stat_tracer(&stat->stat);
1007 if (ret) {
1008 WARN(1,
1009 "Could not register function stat for cpu %d\n",
1010 cpu);
1011 kfree(name);
1012 return;
1013 }
Steven Rostedt493762f2009-03-23 17:12:36 -04001014 }
1015
1016 entry = debugfs_create_file("function_profile_enabled", 0644,
1017 d_tracer, NULL, &ftrace_profile_fops);
1018 if (!entry)
1019 pr_warning("Could not create debugfs "
1020 "'function_profile_enabled' entry\n");
1021}
1022
1023#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001024static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001025{
1026}
1027#endif /* CONFIG_FUNCTION_PROFILER */
1028
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001029static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1030
Steven Rostedt3d083392008-05-12 21:20:42 +02001031#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001032
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001033#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001034# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001035#endif
1036
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001037static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1038
Steven Rostedtb6887d72009-02-17 12:32:04 -05001039struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001040 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -05001041 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001042 unsigned long flags;
1043 unsigned long ip;
1044 void *data;
1045 struct rcu_head rcu;
1046};
1047
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001048struct ftrace_func_entry {
1049 struct hlist_node hlist;
1050 unsigned long ip;
1051};
1052
1053struct ftrace_hash {
1054 unsigned long size_bits;
1055 struct hlist_head *buckets;
1056 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001057 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001058};
1059
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001060/*
1061 * We make these constant because no one should touch them,
1062 * but they are used as the default "empty hash", to avoid allocating
1063 * it all the time. These are in a read only section such that if
1064 * anyone does try to modify it, it will cause an exception.
1065 */
1066static const struct hlist_head empty_buckets[1];
1067static const struct ftrace_hash empty_hash = {
1068 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001069};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001070#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001071
Steven Rostedt2b499382011-05-03 22:49:52 -04001072static struct ftrace_ops global_ops = {
Steven Rostedtf45948e2011-05-02 12:29:25 -04001073 .func = ftrace_stub,
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001074 .notrace_hash = EMPTY_HASH,
1075 .filter_hash = EMPTY_HASH,
Steven Rostedt47409742012-07-20 11:04:44 -04001076 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Steven Rostedtf45948e2011-05-02 12:29:25 -04001077};
1078
Steven Rostedt41c52c02008-05-22 11:46:33 -04001079static DEFINE_MUTEX(ftrace_regex_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02001080
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001081struct ftrace_page {
1082 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001083 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001084 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001085 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001086};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001087
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001088static struct ftrace_page *ftrace_new_pgs;
1089
Steven Rostedta7900872011-12-16 16:23:44 -05001090#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1091#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001092
1093/* estimate from running different kernels */
1094#define NR_TO_INIT 10000
1095
1096static struct ftrace_page *ftrace_pages_start;
1097static struct ftrace_page *ftrace_pages;
1098
Steven Rostedt06a51d92011-12-19 19:07:36 -05001099static bool ftrace_hash_empty(struct ftrace_hash *hash)
1100{
1101 return !hash || !hash->count;
1102}
1103
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001104static struct ftrace_func_entry *
1105ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1106{
1107 unsigned long key;
1108 struct ftrace_func_entry *entry;
1109 struct hlist_head *hhd;
1110 struct hlist_node *n;
1111
Steven Rostedt06a51d92011-12-19 19:07:36 -05001112 if (ftrace_hash_empty(hash))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001113 return NULL;
1114
1115 if (hash->size_bits > 0)
1116 key = hash_long(ip, hash->size_bits);
1117 else
1118 key = 0;
1119
1120 hhd = &hash->buckets[key];
1121
1122 hlist_for_each_entry_rcu(entry, n, hhd, hlist) {
1123 if (entry->ip == ip)
1124 return entry;
1125 }
1126 return NULL;
1127}
1128
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001129static void __add_hash_entry(struct ftrace_hash *hash,
1130 struct ftrace_func_entry *entry)
1131{
1132 struct hlist_head *hhd;
1133 unsigned long key;
1134
1135 if (hash->size_bits)
1136 key = hash_long(entry->ip, hash->size_bits);
1137 else
1138 key = 0;
1139
1140 hhd = &hash->buckets[key];
1141 hlist_add_head(&entry->hlist, hhd);
1142 hash->count++;
1143}
1144
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001145static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1146{
1147 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001148
1149 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1150 if (!entry)
1151 return -ENOMEM;
1152
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001153 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001154 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001155
1156 return 0;
1157}
1158
1159static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001160free_hash_entry(struct ftrace_hash *hash,
1161 struct ftrace_func_entry *entry)
1162{
1163 hlist_del(&entry->hlist);
1164 kfree(entry);
1165 hash->count--;
1166}
1167
1168static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001169remove_hash_entry(struct ftrace_hash *hash,
1170 struct ftrace_func_entry *entry)
1171{
1172 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001173 hash->count--;
1174}
1175
1176static void ftrace_hash_clear(struct ftrace_hash *hash)
1177{
1178 struct hlist_head *hhd;
1179 struct hlist_node *tp, *tn;
1180 struct ftrace_func_entry *entry;
1181 int size = 1 << hash->size_bits;
1182 int i;
1183
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001184 if (!hash->count)
1185 return;
1186
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001187 for (i = 0; i < size; i++) {
1188 hhd = &hash->buckets[i];
1189 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001190 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001191 }
1192 FTRACE_WARN_ON(hash->count);
1193}
1194
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001195static void free_ftrace_hash(struct ftrace_hash *hash)
1196{
1197 if (!hash || hash == EMPTY_HASH)
1198 return;
1199 ftrace_hash_clear(hash);
1200 kfree(hash->buckets);
1201 kfree(hash);
1202}
1203
Steven Rostedt07fd5512011-05-05 18:03:47 -04001204static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1205{
1206 struct ftrace_hash *hash;
1207
1208 hash = container_of(rcu, struct ftrace_hash, rcu);
1209 free_ftrace_hash(hash);
1210}
1211
1212static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1213{
1214 if (!hash || hash == EMPTY_HASH)
1215 return;
1216 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1217}
1218
Jiri Olsa5500fa52012-02-15 15:51:54 +01001219void ftrace_free_filter(struct ftrace_ops *ops)
1220{
1221 free_ftrace_hash(ops->filter_hash);
1222 free_ftrace_hash(ops->notrace_hash);
1223}
1224
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001225static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1226{
1227 struct ftrace_hash *hash;
1228 int size;
1229
1230 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1231 if (!hash)
1232 return NULL;
1233
1234 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001235 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001236
1237 if (!hash->buckets) {
1238 kfree(hash);
1239 return NULL;
1240 }
1241
1242 hash->size_bits = size_bits;
1243
1244 return hash;
1245}
1246
1247static struct ftrace_hash *
1248alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1249{
1250 struct ftrace_func_entry *entry;
1251 struct ftrace_hash *new_hash;
1252 struct hlist_node *tp;
1253 int size;
1254 int ret;
1255 int i;
1256
1257 new_hash = alloc_ftrace_hash(size_bits);
1258 if (!new_hash)
1259 return NULL;
1260
1261 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001262 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001263 return new_hash;
1264
1265 size = 1 << hash->size_bits;
1266 for (i = 0; i < size; i++) {
1267 hlist_for_each_entry(entry, tp, &hash->buckets[i], hlist) {
1268 ret = add_hash_entry(new_hash, entry->ip);
1269 if (ret < 0)
1270 goto free_hash;
1271 }
1272 }
1273
1274 FTRACE_WARN_ON(new_hash->count != hash->count);
1275
1276 return new_hash;
1277
1278 free_hash:
1279 free_ftrace_hash(new_hash);
1280 return NULL;
1281}
1282
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001283static void
1284ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1285static void
1286ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1287
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001288static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001289ftrace_hash_move(struct ftrace_ops *ops, int enable,
1290 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001291{
1292 struct ftrace_func_entry *entry;
1293 struct hlist_node *tp, *tn;
1294 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001295 struct ftrace_hash *old_hash;
1296 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001297 unsigned long key;
1298 int size = src->count;
1299 int bits = 0;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001300 int ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001301 int i;
1302
1303 /*
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001304 * Remove the current set, update the hash and add
1305 * them back.
1306 */
1307 ftrace_hash_rec_disable(ops, enable);
1308
1309 /*
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001310 * If the new source is empty, just free dst and assign it
1311 * the empty_hash.
1312 */
1313 if (!src->count) {
Steven Rostedt07fd5512011-05-05 18:03:47 -04001314 free_ftrace_hash_rcu(*dst);
1315 rcu_assign_pointer(*dst, EMPTY_HASH);
Steven Rostedtd4d34b92011-11-04 20:32:39 -04001316 /* still need to update the function records */
1317 ret = 0;
1318 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001319 }
1320
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001321 /*
1322 * Make the hash size about 1/2 the # found
1323 */
1324 for (size /= 2; size; size >>= 1)
1325 bits++;
1326
1327 /* Don't allocate too much */
1328 if (bits > FTRACE_HASH_MAX_BITS)
1329 bits = FTRACE_HASH_MAX_BITS;
1330
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001331 ret = -ENOMEM;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001332 new_hash = alloc_ftrace_hash(bits);
1333 if (!new_hash)
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001334 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001335
1336 size = 1 << src->size_bits;
1337 for (i = 0; i < size; i++) {
1338 hhd = &src->buckets[i];
1339 hlist_for_each_entry_safe(entry, tp, tn, hhd, hlist) {
1340 if (bits > 0)
1341 key = hash_long(entry->ip, bits);
1342 else
1343 key = 0;
1344 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001345 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001346 }
1347 }
1348
Steven Rostedt07fd5512011-05-05 18:03:47 -04001349 old_hash = *dst;
1350 rcu_assign_pointer(*dst, new_hash);
1351 free_ftrace_hash_rcu(old_hash);
1352
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001353 ret = 0;
1354 out:
1355 /*
1356 * Enable regardless of ret:
1357 * On success, we enable the new hash.
1358 * On failure, we re-enable the original hash.
1359 */
1360 ftrace_hash_rec_enable(ops, enable);
1361
1362 return ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001363}
1364
Steven Rostedt265c8312009-02-13 12:43:56 -05001365/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001366 * Test the hashes for this ops to see if we want to call
1367 * the ops->func or not.
1368 *
1369 * It's a match if the ip is in the ops->filter_hash or
1370 * the filter_hash does not exist or is empty,
1371 * AND
1372 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001373 *
1374 * This needs to be called with preemption disabled as
1375 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001376 */
1377static int
1378ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
1379{
1380 struct ftrace_hash *filter_hash;
1381 struct ftrace_hash *notrace_hash;
1382 int ret;
1383
Steven Rostedtb8489142011-05-04 09:27:52 -04001384 filter_hash = rcu_dereference_raw(ops->filter_hash);
1385 notrace_hash = rcu_dereference_raw(ops->notrace_hash);
1386
Steven Rostedt06a51d92011-12-19 19:07:36 -05001387 if ((ftrace_hash_empty(filter_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001388 ftrace_lookup_ip(filter_hash, ip)) &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001389 (ftrace_hash_empty(notrace_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001390 !ftrace_lookup_ip(notrace_hash, ip)))
1391 ret = 1;
1392 else
1393 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001394
1395 return ret;
1396}
1397
1398/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001399 * This is a double for. Do not use 'break' to break out of the loop,
1400 * you must use a goto.
1401 */
1402#define do_for_each_ftrace_rec(pg, rec) \
1403 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1404 int _____i; \
1405 for (_____i = 0; _____i < pg->index; _____i++) { \
1406 rec = &pg->records[_____i];
1407
1408#define while_for_each_ftrace_rec() \
1409 } \
1410 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301411
Steven Rostedt5855fea2011-12-16 19:27:42 -05001412
1413static int ftrace_cmp_recs(const void *a, const void *b)
1414{
Steven Rostedta650e022012-04-25 13:48:13 -04001415 const struct dyn_ftrace *key = a;
1416 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001417
Steven Rostedta650e022012-04-25 13:48:13 -04001418 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001419 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001420 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1421 return 1;
1422 return 0;
1423}
1424
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001425static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001426{
1427 struct ftrace_page *pg;
1428 struct dyn_ftrace *rec;
1429 struct dyn_ftrace key;
1430
1431 key.ip = start;
1432 key.flags = end; /* overload flags, as it is unsigned long */
1433
1434 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1435 if (end < pg->records[0].ip ||
1436 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1437 continue;
1438 rec = bsearch(&key, pg->records, pg->index,
1439 sizeof(struct dyn_ftrace),
1440 ftrace_cmp_recs);
1441 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001442 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001443 }
1444
Steven Rostedt5855fea2011-12-16 19:27:42 -05001445 return 0;
1446}
1447
Steven Rostedtc88fd862011-08-16 09:53:39 -04001448/**
1449 * ftrace_location - return true if the ip giving is a traced location
1450 * @ip: the instruction pointer to check
1451 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001452 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001453 * That is, the instruction that is either a NOP or call to
1454 * the function tracer. It checks the ftrace internal tables to
1455 * determine if the address belongs or not.
1456 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001457unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001458{
Steven Rostedta650e022012-04-25 13:48:13 -04001459 return ftrace_location_range(ip, ip);
1460}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001461
Steven Rostedta650e022012-04-25 13:48:13 -04001462/**
1463 * ftrace_text_reserved - return true if range contains an ftrace location
1464 * @start: start of range to search
1465 * @end: end of range to search (inclusive). @end points to the last byte to check.
1466 *
1467 * Returns 1 if @start and @end contains a ftrace location.
1468 * That is, the instruction that is either a NOP or call to
1469 * the function tracer. It checks the ftrace internal tables to
1470 * determine if the address belongs or not.
1471 */
1472int ftrace_text_reserved(void *start, void *end)
1473{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001474 unsigned long ret;
1475
1476 ret = ftrace_location_range((unsigned long)start,
1477 (unsigned long)end);
1478
1479 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001480}
1481
Steven Rostedted926f92011-05-03 13:25:24 -04001482static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1483 int filter_hash,
1484 bool inc)
1485{
1486 struct ftrace_hash *hash;
1487 struct ftrace_hash *other_hash;
1488 struct ftrace_page *pg;
1489 struct dyn_ftrace *rec;
1490 int count = 0;
1491 int all = 0;
1492
1493 /* Only update if the ops has been registered */
1494 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1495 return;
1496
1497 /*
1498 * In the filter_hash case:
1499 * If the count is zero, we update all records.
1500 * Otherwise we just update the items in the hash.
1501 *
1502 * In the notrace_hash case:
1503 * We enable the update in the hash.
1504 * As disabling notrace means enabling the tracing,
1505 * and enabling notrace means disabling, the inc variable
1506 * gets inversed.
1507 */
1508 if (filter_hash) {
1509 hash = ops->filter_hash;
1510 other_hash = ops->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001511 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001512 all = 1;
1513 } else {
1514 inc = !inc;
1515 hash = ops->notrace_hash;
1516 other_hash = ops->filter_hash;
1517 /*
1518 * If the notrace hash has no items,
1519 * then there's nothing to do.
1520 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001521 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001522 return;
1523 }
1524
1525 do_for_each_ftrace_rec(pg, rec) {
1526 int in_other_hash = 0;
1527 int in_hash = 0;
1528 int match = 0;
1529
1530 if (all) {
1531 /*
1532 * Only the filter_hash affects all records.
1533 * Update if the record is not in the notrace hash.
1534 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001535 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001536 match = 1;
1537 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001538 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1539 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001540
1541 /*
1542 *
1543 */
1544 if (filter_hash && in_hash && !in_other_hash)
1545 match = 1;
1546 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001547 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001548 match = 1;
1549 }
1550 if (!match)
1551 continue;
1552
1553 if (inc) {
1554 rec->flags++;
1555 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1556 return;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001557 /*
1558 * If any ops wants regs saved for this function
1559 * then all ops will get saved regs.
1560 */
1561 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1562 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001563 } else {
1564 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1565 return;
1566 rec->flags--;
1567 }
1568 count++;
1569 /* Shortcut, if we handled all records, we are done. */
1570 if (!all && count == hash->count)
1571 return;
1572 } while_for_each_ftrace_rec();
1573}
1574
1575static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1576 int filter_hash)
1577{
1578 __ftrace_hash_rec_update(ops, filter_hash, 0);
1579}
1580
1581static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1582 int filter_hash)
1583{
1584 __ftrace_hash_rec_update(ops, filter_hash, 1);
1585}
1586
Steven Rostedt05736a42008-09-22 14:55:47 -07001587static void print_ip_ins(const char *fmt, unsigned char *p)
1588{
1589 int i;
1590
1591 printk(KERN_CONT "%s", fmt);
1592
1593 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1594 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1595}
1596
Steven Rostedtc88fd862011-08-16 09:53:39 -04001597/**
1598 * ftrace_bug - report and shutdown function tracer
1599 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1600 * @ip: The address that failed
1601 *
1602 * The arch code that enables or disables the function tracing
1603 * can call ftrace_bug() when it has detected a problem in
1604 * modifying the code. @failed should be one of either:
1605 * EFAULT - if the problem happens on reading the @ip address
1606 * EINVAL - if what is read at @ip is not what was expected
1607 * EPERM - if the problem happens on writting to the @ip address
1608 */
1609void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001610{
1611 switch (failed) {
1612 case -EFAULT:
1613 FTRACE_WARN_ON_ONCE(1);
1614 pr_info("ftrace faulted on modifying ");
1615 print_ip_sym(ip);
1616 break;
1617 case -EINVAL:
1618 FTRACE_WARN_ON_ONCE(1);
1619 pr_info("ftrace failed to modify ");
1620 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001621 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001622 printk(KERN_CONT "\n");
1623 break;
1624 case -EPERM:
1625 FTRACE_WARN_ON_ONCE(1);
1626 pr_info("ftrace faulted on writing ");
1627 print_ip_sym(ip);
1628 break;
1629 default:
1630 FTRACE_WARN_ON_ONCE(1);
1631 pr_info("ftrace faulted on unknown error ");
1632 print_ip_sym(ip);
1633 }
1634}
1635
Steven Rostedtc88fd862011-08-16 09:53:39 -04001636static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02001637{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001638 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001639
Steven Rostedt982c3502008-11-15 16:31:41 -05001640 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001641 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05001642 *
Steven Rostedted926f92011-05-03 13:25:24 -04001643 * If the record has a ref count, then we need to enable it
1644 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001645 *
Steven Rostedted926f92011-05-03 13:25:24 -04001646 * Otherwise we make sure its disabled.
1647 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001648 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04001649 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001650 */
Steven Rostedtc88fd862011-08-16 09:53:39 -04001651 if (enable && (rec->flags & ~FTRACE_FL_MASK))
Steven Rostedted926f92011-05-03 13:25:24 -04001652 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001653
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001654 /*
1655 * If enabling and the REGS flag does not match the REGS_EN, then
1656 * do not ignore this record. Set flags to fail the compare against
1657 * ENABLED.
1658 */
1659 if (flag &&
1660 (!(rec->flags & FTRACE_FL_REGS) != !(rec->flags & FTRACE_FL_REGS_EN)))
1661 flag |= FTRACE_FL_REGS;
1662
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001663 /* If the state of this record hasn't changed, then do nothing */
1664 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001665 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001666
1667 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001668 /* Save off if rec is being enabled (for return value) */
1669 flag ^= rec->flags & FTRACE_FL_ENABLED;
1670
1671 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04001672 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001673 if (flag & FTRACE_FL_REGS) {
1674 if (rec->flags & FTRACE_FL_REGS)
1675 rec->flags |= FTRACE_FL_REGS_EN;
1676 else
1677 rec->flags &= ~FTRACE_FL_REGS_EN;
1678 }
1679 }
1680
1681 /*
1682 * If this record is being updated from a nop, then
1683 * return UPDATE_MAKE_CALL.
1684 * Otherwise, if the EN flag is set, then return
1685 * UPDATE_MODIFY_CALL_REGS to tell the caller to convert
1686 * from the non-save regs, to a save regs function.
1687 * Otherwise,
1688 * return UPDATE_MODIFY_CALL to tell the caller to convert
1689 * from the save regs, to a non-save regs function.
1690 */
1691 if (flag & FTRACE_FL_ENABLED)
1692 return FTRACE_UPDATE_MAKE_CALL;
1693 else if (rec->flags & FTRACE_FL_REGS_EN)
1694 return FTRACE_UPDATE_MODIFY_CALL_REGS;
1695 else
1696 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001697 }
1698
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001699 if (update) {
1700 /* If there's no more users, clear all flags */
1701 if (!(rec->flags & ~FTRACE_FL_MASK))
1702 rec->flags = 0;
1703 else
1704 /* Just disable the record (keep REGS state) */
1705 rec->flags &= ~FTRACE_FL_ENABLED;
1706 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04001707
1708 return FTRACE_UPDATE_MAKE_NOP;
1709}
1710
1711/**
1712 * ftrace_update_record, set a record that now is tracing or not
1713 * @rec: the record to update
1714 * @enable: set to 1 if the record is tracing, zero to force disable
1715 *
1716 * The records that represent all functions that can be traced need
1717 * to be updated when tracing has been enabled.
1718 */
1719int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1720{
1721 return ftrace_check_record(rec, enable, 1);
1722}
1723
1724/**
1725 * ftrace_test_record, check if the record has been enabled or not
1726 * @rec: the record to test
1727 * @enable: set to 1 to check if enabled, 0 if it is disabled
1728 *
1729 * The arch code may need to test if a record is already set to
1730 * tracing to determine how to modify the function code that it
1731 * represents.
1732 */
1733int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1734{
1735 return ftrace_check_record(rec, enable, 0);
1736}
1737
1738static int
1739__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1740{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001741 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001742 unsigned long ftrace_addr;
1743 int ret;
1744
Steven Rostedtc88fd862011-08-16 09:53:39 -04001745 ret = ftrace_update_record(rec, enable);
1746
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001747 if (rec->flags & FTRACE_FL_REGS)
1748 ftrace_addr = (unsigned long)FTRACE_REGS_ADDR;
1749 else
1750 ftrace_addr = (unsigned long)FTRACE_ADDR;
1751
Steven Rostedtc88fd862011-08-16 09:53:39 -04001752 switch (ret) {
1753 case FTRACE_UPDATE_IGNORE:
1754 return 0;
1755
1756 case FTRACE_UPDATE_MAKE_CALL:
1757 return ftrace_make_call(rec, ftrace_addr);
1758
1759 case FTRACE_UPDATE_MAKE_NOP:
1760 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001761
1762 case FTRACE_UPDATE_MODIFY_CALL_REGS:
1763 case FTRACE_UPDATE_MODIFY_CALL:
1764 if (rec->flags & FTRACE_FL_REGS)
1765 ftrace_old_addr = (unsigned long)FTRACE_ADDR;
1766 else
1767 ftrace_old_addr = (unsigned long)FTRACE_REGS_ADDR;
1768
1769 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04001770 }
1771
1772 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02001773}
1774
Steven Rostedte4f5d542012-04-27 09:13:18 -04001775void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001776{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001777 struct dyn_ftrace *rec;
1778 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001779 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001780
Steven Rostedt45a4a232011-04-21 23:16:46 -04001781 if (unlikely(ftrace_disabled))
1782 return;
1783
Steven Rostedt265c8312009-02-13 12:43:56 -05001784 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedte4f5d542012-04-27 09:13:18 -04001785 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001786 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04001787 ftrace_bug(failed, rec->ip);
1788 /* Stop processing */
1789 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001790 }
1791 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001792}
1793
Steven Rostedtc88fd862011-08-16 09:53:39 -04001794struct ftrace_rec_iter {
1795 struct ftrace_page *pg;
1796 int index;
1797};
1798
1799/**
1800 * ftrace_rec_iter_start, start up iterating over traced functions
1801 *
1802 * Returns an iterator handle that is used to iterate over all
1803 * the records that represent address locations where functions
1804 * are traced.
1805 *
1806 * May return NULL if no records are available.
1807 */
1808struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1809{
1810 /*
1811 * We only use a single iterator.
1812 * Protected by the ftrace_lock mutex.
1813 */
1814 static struct ftrace_rec_iter ftrace_rec_iter;
1815 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1816
1817 iter->pg = ftrace_pages_start;
1818 iter->index = 0;
1819
1820 /* Could have empty pages */
1821 while (iter->pg && !iter->pg->index)
1822 iter->pg = iter->pg->next;
1823
1824 if (!iter->pg)
1825 return NULL;
1826
1827 return iter;
1828}
1829
1830/**
1831 * ftrace_rec_iter_next, get the next record to process.
1832 * @iter: The handle to the iterator.
1833 *
1834 * Returns the next iterator after the given iterator @iter.
1835 */
1836struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1837{
1838 iter->index++;
1839
1840 if (iter->index >= iter->pg->index) {
1841 iter->pg = iter->pg->next;
1842 iter->index = 0;
1843
1844 /* Could have empty pages */
1845 while (iter->pg && !iter->pg->index)
1846 iter->pg = iter->pg->next;
1847 }
1848
1849 if (!iter->pg)
1850 return NULL;
1851
1852 return iter;
1853}
1854
1855/**
1856 * ftrace_rec_iter_record, get the record at the iterator location
1857 * @iter: The current iterator location
1858 *
1859 * Returns the record that the current @iter is at.
1860 */
1861struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1862{
1863 return &iter->pg->records[iter->index];
1864}
1865
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301866static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001867ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001868{
1869 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001870 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001871
1872 ip = rec->ip;
1873
Steven Rostedt45a4a232011-04-21 23:16:46 -04001874 if (unlikely(ftrace_disabled))
1875 return 0;
1876
Shaohua Li25aac9d2009-01-09 11:29:40 +08001877 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001878 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001879 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301880 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02001881 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301882 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001883}
1884
Steven Rostedt000ab692009-02-17 13:35:06 -05001885/*
1886 * archs can override this function if they must do something
1887 * before the modifying code is performed.
1888 */
1889int __weak ftrace_arch_code_modify_prepare(void)
1890{
1891 return 0;
1892}
1893
1894/*
1895 * archs can override this function if they must do something
1896 * after the modifying code is performed.
1897 */
1898int __weak ftrace_arch_code_modify_post_process(void)
1899{
1900 return 0;
1901}
1902
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001903void ftrace_modify_all_code(int command)
1904{
1905 if (command & FTRACE_UPDATE_CALLS)
1906 ftrace_replace_code(1);
1907 else if (command & FTRACE_DISABLE_CALLS)
1908 ftrace_replace_code(0);
1909
1910 if (command & FTRACE_UPDATE_TRACE_FUNC)
1911 ftrace_update_ftrace_func(ftrace_trace_function);
1912
1913 if (command & FTRACE_START_FUNC_RET)
1914 ftrace_enable_ftrace_graph_caller();
1915 else if (command & FTRACE_STOP_FUNC_RET)
1916 ftrace_disable_ftrace_graph_caller();
1917}
1918
Ingo Molnare309b412008-05-12 21:20:51 +02001919static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02001920{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001921 int *command = data;
1922
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001923 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001924
Steven Rostedtc88fd862011-08-16 09:53:39 -04001925 return 0;
1926}
1927
1928/**
1929 * ftrace_run_stop_machine, go back to the stop machine method
1930 * @command: The command to tell ftrace what to do
1931 *
1932 * If an arch needs to fall back to the stop machine method, the
1933 * it can call this function.
1934 */
1935void ftrace_run_stop_machine(int command)
1936{
1937 stop_machine(__ftrace_modify_code, &command, NULL);
1938}
1939
1940/**
1941 * arch_ftrace_update_code, modify the code to trace or not trace
1942 * @command: The command that needs to be done
1943 *
1944 * Archs can override this function if it does not need to
1945 * run stop_machine() to modify code.
1946 */
1947void __weak arch_ftrace_update_code(int command)
1948{
1949 ftrace_run_stop_machine(command);
1950}
1951
1952static void ftrace_run_update_code(int command)
1953{
1954 int ret;
1955
1956 ret = ftrace_arch_code_modify_prepare();
1957 FTRACE_WARN_ON(ret);
1958 if (ret)
1959 return;
1960 /*
1961 * Do not call function tracer while we update the code.
1962 * We are in stop machine.
1963 */
1964 function_trace_stop++;
1965
1966 /*
1967 * By default we use stop_machine() to modify the code.
1968 * But archs can do what ever they want as long as it
1969 * is safe. The stop_machine() is the safest, but also
1970 * produces the most overhead.
1971 */
1972 arch_ftrace_update_code(command);
1973
Steven Rostedt6331c282011-07-13 15:11:02 -04001974 function_trace_stop--;
1975
Steven Rostedt000ab692009-02-17 13:35:06 -05001976 ret = ftrace_arch_code_modify_post_process();
1977 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02001978}
1979
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001980static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05001981static int ftrace_start_up;
Steven Rostedtb8489142011-05-04 09:27:52 -04001982static int global_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05001983
1984static void ftrace_startup_enable(int command)
1985{
1986 if (saved_ftrace_func != ftrace_trace_function) {
1987 saved_ftrace_func = ftrace_trace_function;
1988 command |= FTRACE_UPDATE_TRACE_FUNC;
1989 }
1990
1991 if (!command || !ftrace_enabled)
1992 return;
1993
1994 ftrace_run_update_code(command);
1995}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001996
Steven Rostedta1cd6172011-05-23 15:24:25 -04001997static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02001998{
Steven Rostedtb8489142011-05-04 09:27:52 -04001999 bool hash_enable = true;
2000
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002001 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002002 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002003
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002004 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002005 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02002006
Steven Rostedtb8489142011-05-04 09:27:52 -04002007 /* ops marked global share the filter hashes */
2008 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2009 ops = &global_ops;
2010 /* Don't update hash if global is already set */
2011 if (global_start_up)
2012 hash_enable = false;
2013 global_start_up++;
2014 }
2015
Steven Rostedted926f92011-05-03 13:25:24 -04002016 ops->flags |= FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002017 if (hash_enable)
Steven Rostedted926f92011-05-03 13:25:24 -04002018 ftrace_hash_rec_enable(ops, 1);
2019
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002020 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002021
2022 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002023}
2024
Steven Rostedtbd69c302011-05-03 21:55:54 -04002025static void ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002026{
Steven Rostedtb8489142011-05-04 09:27:52 -04002027 bool hash_disable = true;
2028
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002029 if (unlikely(ftrace_disabled))
2030 return;
2031
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002032 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002033 /*
2034 * Just warn in case of unbalance, no need to kill ftrace, it's not
2035 * critical but the ftrace_call callers may be never nopped again after
2036 * further ftrace uses.
2037 */
2038 WARN_ON_ONCE(ftrace_start_up < 0);
2039
Steven Rostedtb8489142011-05-04 09:27:52 -04002040 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2041 ops = &global_ops;
2042 global_start_up--;
2043 WARN_ON_ONCE(global_start_up < 0);
2044 /* Don't update hash if global still has users */
2045 if (global_start_up) {
2046 WARN_ON_ONCE(!ftrace_start_up);
2047 hash_disable = false;
2048 }
2049 }
2050
2051 if (hash_disable)
Steven Rostedted926f92011-05-03 13:25:24 -04002052 ftrace_hash_rec_disable(ops, 1);
2053
Steven Rostedtb8489142011-05-04 09:27:52 -04002054 if (ops != &global_ops || !global_start_up)
Steven Rostedted926f92011-05-03 13:25:24 -04002055 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002056
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002057 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002058
2059 if (saved_ftrace_func != ftrace_trace_function) {
2060 saved_ftrace_func = ftrace_trace_function;
2061 command |= FTRACE_UPDATE_TRACE_FUNC;
2062 }
2063
2064 if (!command || !ftrace_enabled)
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002065 return;
Steven Rostedt3d083392008-05-12 21:20:42 +02002066
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002067 ftrace_run_update_code(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02002068}
2069
Ingo Molnare309b412008-05-12 21:20:51 +02002070static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002071{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002072 if (unlikely(ftrace_disabled))
2073 return;
2074
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002075 /* Force update next time */
2076 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002077 /* ftrace_start_up is true if we want ftrace running */
2078 if (ftrace_start_up)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002079 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002080}
2081
Ingo Molnare309b412008-05-12 21:20:51 +02002082static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002083{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002084 if (unlikely(ftrace_disabled))
2085 return;
2086
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002087 /* ftrace_start_up is true if ftrace is running */
2088 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04002089 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002090}
2091
Steven Rostedt3d083392008-05-12 21:20:42 +02002092static cycle_t ftrace_update_time;
2093static unsigned long ftrace_update_cnt;
2094unsigned long ftrace_update_tot_cnt;
2095
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002096static int ops_traces_mod(struct ftrace_ops *ops)
2097{
2098 struct ftrace_hash *hash;
2099
2100 hash = ops->filter_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05002101 return ftrace_hash_empty(hash);
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002102}
2103
Steven Rostedt31e88902008-11-14 16:21:19 -08002104static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02002105{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002106 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002107 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302108 cycle_t start, stop;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002109 unsigned long ref = 0;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002110 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002111
2112 /*
2113 * When adding a module, we need to check if tracers are
2114 * currently enabled and if they are set to trace all functions.
2115 * If they are, we need to enable the module functions as well
2116 * as update the reference counts for those function records.
2117 */
2118 if (mod) {
2119 struct ftrace_ops *ops;
2120
2121 for (ops = ftrace_ops_list;
2122 ops != &ftrace_list_end; ops = ops->next) {
2123 if (ops->flags & FTRACE_OPS_FL_ENABLED &&
2124 ops_traces_mod(ops))
2125 ref++;
2126 }
2127 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002128
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002129 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002130 ftrace_update_cnt = 0;
2131
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002132 for (pg = ftrace_new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302133
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002134 for (i = 0; i < pg->index; i++) {
2135 /* If something went wrong, bail without enabling anything */
2136 if (unlikely(ftrace_disabled))
2137 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002138
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002139 p = &pg->records[i];
2140 p->flags = ref;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302141
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002142 /*
2143 * Do the initial record conversion from mcount jump
2144 * to the NOP instructions.
2145 */
2146 if (!ftrace_code_disable(mod, p))
2147 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002148
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002149 ftrace_update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002150
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002151 /*
2152 * If the tracing is enabled, go ahead and enable the record.
2153 *
2154 * The reason not to enable the record immediatelly is the
2155 * inherent check of ftrace_make_nop/ftrace_make_call for
2156 * correct previous instructions. Making first the NOP
2157 * conversion puts the module to the correct state, thus
2158 * passing the ftrace_make_call check.
2159 */
2160 if (ftrace_start_up && ref) {
2161 int failed = __ftrace_replace_code(p, 1);
2162 if (failed)
2163 ftrace_bug(failed, p->ip);
2164 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002165 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002166 }
2167
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002168 ftrace_new_pgs = NULL;
2169
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002170 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002171 ftrace_update_time = stop - start;
2172 ftrace_update_tot_cnt += ftrace_update_cnt;
2173
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002174 return 0;
2175}
2176
Steven Rostedta7900872011-12-16 16:23:44 -05002177static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002178{
Steven Rostedta7900872011-12-16 16:23:44 -05002179 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002180 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002181
Steven Rostedta7900872011-12-16 16:23:44 -05002182 if (WARN_ON(!count))
2183 return -EINVAL;
2184
2185 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002186
2187 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002188 * We want to fill as much as possible. No more than a page
2189 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002190 */
Steven Rostedta7900872011-12-16 16:23:44 -05002191 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2192 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002193
Steven Rostedta7900872011-12-16 16:23:44 -05002194 again:
2195 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2196
2197 if (!pg->records) {
2198 /* if we can't allocate this size, try something smaller */
2199 if (!order)
2200 return -ENOMEM;
2201 order >>= 1;
2202 goto again;
2203 }
2204
2205 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2206 pg->size = cnt;
2207
2208 if (cnt > count)
2209 cnt = count;
2210
2211 return cnt;
2212}
2213
2214static struct ftrace_page *
2215ftrace_allocate_pages(unsigned long num_to_init)
2216{
2217 struct ftrace_page *start_pg;
2218 struct ftrace_page *pg;
2219 int order;
2220 int cnt;
2221
2222 if (!num_to_init)
2223 return 0;
2224
2225 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2226 if (!pg)
2227 return NULL;
2228
2229 /*
2230 * Try to allocate as much as possible in one continues
2231 * location that fills in all of the space. We want to
2232 * waste as little space as possible.
2233 */
2234 for (;;) {
2235 cnt = ftrace_allocate_records(pg, num_to_init);
2236 if (cnt < 0)
2237 goto free_pages;
2238
2239 num_to_init -= cnt;
2240 if (!num_to_init)
2241 break;
2242
2243 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2244 if (!pg->next)
2245 goto free_pages;
2246
2247 pg = pg->next;
2248 }
2249
2250 return start_pg;
2251
2252 free_pages:
2253 while (start_pg) {
2254 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2255 free_pages((unsigned long)pg->records, order);
2256 start_pg = pg->next;
2257 kfree(pg);
2258 pg = start_pg;
2259 }
2260 pr_info("ftrace: FAILED to allocate memory for functions\n");
2261 return NULL;
2262}
2263
2264static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2265{
2266 int cnt;
2267
2268 if (!num_to_init) {
2269 pr_info("ftrace: No functions to be traced?\n");
2270 return -1;
2271 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002272
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002273 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04002274 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08002275 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002276
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002277 return 0;
2278}
2279
Steven Rostedt5072c592008-05-12 21:20:43 +02002280#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2281
2282struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002283 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002284 loff_t func_pos;
2285 struct ftrace_page *pg;
2286 struct dyn_ftrace *func;
2287 struct ftrace_func_probe *probe;
2288 struct trace_parser parser;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002289 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002290 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002291 int hidx;
2292 int idx;
2293 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002294};
2295
Ingo Molnare309b412008-05-12 21:20:51 +02002296static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002297t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002298{
2299 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002300 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002301 struct hlist_head *hhd;
2302
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002303 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002304 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002305
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002306 if (iter->probe)
2307 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002308 retry:
2309 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2310 return NULL;
2311
2312 hhd = &ftrace_func_hash[iter->hidx];
2313
2314 if (hlist_empty(hhd)) {
2315 iter->hidx++;
2316 hnd = NULL;
2317 goto retry;
2318 }
2319
2320 if (!hnd)
2321 hnd = hhd->first;
2322 else {
2323 hnd = hnd->next;
2324 if (!hnd) {
2325 iter->hidx++;
2326 goto retry;
2327 }
2328 }
2329
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002330 if (WARN_ON_ONCE(!hnd))
2331 return NULL;
2332
2333 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2334
2335 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002336}
2337
2338static void *t_hash_start(struct seq_file *m, loff_t *pos)
2339{
2340 struct ftrace_iterator *iter = m->private;
2341 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08002342 loff_t l;
2343
Steven Rostedt69a30832011-12-19 15:21:16 -05002344 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2345 return NULL;
2346
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002347 if (iter->func_pos > *pos)
2348 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002349
Li Zefand82d6242009-06-24 09:54:54 +08002350 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002351 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002352 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08002353 if (!p)
2354 break;
2355 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002356 if (!p)
2357 return NULL;
2358
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002359 /* Only set this if we have an item */
2360 iter->flags |= FTRACE_ITER_HASH;
2361
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002362 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002363}
2364
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002365static int
2366t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002367{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002368 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002369
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002370 rec = iter->probe;
2371 if (WARN_ON_ONCE(!rec))
2372 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002373
Steven Rostedt809dcf22009-02-16 23:06:01 -05002374 if (rec->ops->print)
2375 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2376
Steven Rostedtb375a112009-09-17 00:05:58 -04002377 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002378
2379 if (rec->data)
2380 seq_printf(m, ":%p", rec->data);
2381 seq_putc(m, '\n');
2382
2383 return 0;
2384}
2385
2386static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02002387t_next(struct seq_file *m, void *v, loff_t *pos)
2388{
2389 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002390 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002391 struct dyn_ftrace *rec = NULL;
2392
Steven Rostedt45a4a232011-04-21 23:16:46 -04002393 if (unlikely(ftrace_disabled))
2394 return NULL;
2395
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002396 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002397 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002398
Steven Rostedt5072c592008-05-12 21:20:43 +02002399 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01002400 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02002401
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002402 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002403 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002404
Steven Rostedt5072c592008-05-12 21:20:43 +02002405 retry:
2406 if (iter->idx >= iter->pg->index) {
2407 if (iter->pg->next) {
2408 iter->pg = iter->pg->next;
2409 iter->idx = 0;
2410 goto retry;
2411 }
2412 } else {
2413 rec = &iter->pg->records[iter->idx++];
Steven Rostedt32082302011-12-16 14:42:37 -05002414 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedtf45948e2011-05-02 12:29:25 -04002415 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
Steven Rostedt0183fb12008-11-07 22:36:02 -05002416
Steven Rostedt41c52c02008-05-22 11:46:33 -04002417 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt647bcd02011-05-03 14:39:21 -04002418 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2419
2420 ((iter->flags & FTRACE_ITER_ENABLED) &&
2421 !(rec->flags & ~FTRACE_FL_MASK))) {
2422
Steven Rostedt5072c592008-05-12 21:20:43 +02002423 rec = NULL;
2424 goto retry;
2425 }
2426 }
2427
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002428 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002429 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002430
2431 iter->func = rec;
2432
2433 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002434}
2435
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002436static void reset_iter_read(struct ftrace_iterator *iter)
2437{
2438 iter->pos = 0;
2439 iter->func_pos = 0;
2440 iter->flags &= ~(FTRACE_ITER_PRINTALL & FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002441}
2442
2443static void *t_start(struct seq_file *m, loff_t *pos)
2444{
2445 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002446 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002447 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002448 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002449
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002450 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002451
2452 if (unlikely(ftrace_disabled))
2453 return NULL;
2454
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002455 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002456 * If an lseek was done, then reset and start from beginning.
2457 */
2458 if (*pos < iter->pos)
2459 reset_iter_read(iter);
2460
2461 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002462 * For set_ftrace_filter reading, if we have the filter
2463 * off, we can short cut and just print out that all
2464 * functions are enabled.
2465 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05002466 if (iter->flags & FTRACE_ITER_FILTER &&
2467 ftrace_hash_empty(ops->filter_hash)) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002468 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002469 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002470 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002471 /* reset in case of seek/pread */
2472 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002473 return iter;
2474 }
2475
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002476 if (iter->flags & FTRACE_ITER_HASH)
2477 return t_hash_start(m, pos);
2478
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002479 /*
2480 * Unfortunately, we need to restart at ftrace_pages_start
2481 * every time we let go of the ftrace_mutex. This is because
2482 * those pointers can change without the lock.
2483 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002484 iter->pg = ftrace_pages_start;
2485 iter->idx = 0;
2486 for (l = 0; l <= *pos; ) {
2487 p = t_next(m, p, &l);
2488 if (!p)
2489 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002490 }
walimis5821e1b2008-11-15 15:19:06 +08002491
Steven Rostedt69a30832011-12-19 15:21:16 -05002492 if (!p)
2493 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002494
2495 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002496}
2497
2498static void t_stop(struct seq_file *m, void *p)
2499{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002500 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002501}
2502
2503static int t_show(struct seq_file *m, void *v)
2504{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002505 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002506 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02002507
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002508 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002509 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002510
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002511 if (iter->flags & FTRACE_ITER_PRINTALL) {
2512 seq_printf(m, "#### all functions enabled ####\n");
2513 return 0;
2514 }
2515
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002516 rec = iter->func;
2517
Steven Rostedt5072c592008-05-12 21:20:43 +02002518 if (!rec)
2519 return 0;
2520
Steven Rostedt647bcd02011-05-03 14:39:21 -04002521 seq_printf(m, "%ps", (void *)rec->ip);
2522 if (iter->flags & FTRACE_ITER_ENABLED)
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002523 seq_printf(m, " (%ld)%s",
2524 rec->flags & ~FTRACE_FL_MASK,
2525 rec->flags & FTRACE_FL_REGS ? " R" : "");
Steven Rostedt647bcd02011-05-03 14:39:21 -04002526 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02002527
2528 return 0;
2529}
2530
James Morris88e9d342009-09-22 16:43:43 -07002531static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002532 .start = t_start,
2533 .next = t_next,
2534 .stop = t_stop,
2535 .show = t_show,
2536};
2537
Ingo Molnare309b412008-05-12 21:20:51 +02002538static int
Steven Rostedt5072c592008-05-12 21:20:43 +02002539ftrace_avail_open(struct inode *inode, struct file *file)
2540{
2541 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002542
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002543 if (unlikely(ftrace_disabled))
2544 return -ENODEV;
2545
Jiri Olsa50e18b92012-04-25 10:23:39 +02002546 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2547 if (iter) {
2548 iter->pg = ftrace_pages_start;
2549 iter->ops = &global_ops;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002550 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002551
Jiri Olsa50e18b92012-04-25 10:23:39 +02002552 return iter ? 0 : -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02002553}
2554
Steven Rostedt647bcd02011-05-03 14:39:21 -04002555static int
2556ftrace_enabled_open(struct inode *inode, struct file *file)
2557{
2558 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002559
2560 if (unlikely(ftrace_disabled))
2561 return -ENODEV;
2562
Jiri Olsa50e18b92012-04-25 10:23:39 +02002563 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2564 if (iter) {
2565 iter->pg = ftrace_pages_start;
2566 iter->flags = FTRACE_ITER_ENABLED;
2567 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002568 }
2569
Jiri Olsa50e18b92012-04-25 10:23:39 +02002570 return iter ? 0 : -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002571}
2572
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002573static void ftrace_filter_reset(struct ftrace_hash *hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02002574{
Steven Rostedt52baf112009-02-14 01:15:39 -05002575 mutex_lock(&ftrace_lock);
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002576 ftrace_hash_clear(hash);
Steven Rostedt52baf112009-02-14 01:15:39 -05002577 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002578}
2579
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002580/**
2581 * ftrace_regex_open - initialize function tracer filter files
2582 * @ops: The ftrace_ops that hold the hash filters
2583 * @flag: The type of filter to process
2584 * @inode: The inode, usually passed in to your open routine
2585 * @file: The file, usually passed in to your open routine
2586 *
2587 * ftrace_regex_open() initializes the filter files for the
2588 * @ops. Depending on @flag it may process the filter hash or
2589 * the notrace hash of @ops. With this called from the open
2590 * routine, you can use ftrace_filter_write() for the write
2591 * routine if @flag has FTRACE_ITER_FILTER set, or
2592 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
2593 * ftrace_regex_lseek() should be used as the lseek routine, and
2594 * release must call ftrace_regex_release().
2595 */
2596int
Steven Rostedtf45948e2011-05-02 12:29:25 -04002597ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002598 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02002599{
2600 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002601 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02002602 int ret = 0;
2603
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002604 if (unlikely(ftrace_disabled))
2605 return -ENODEV;
2606
Steven Rostedt5072c592008-05-12 21:20:43 +02002607 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2608 if (!iter)
2609 return -ENOMEM;
2610
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002611 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2612 kfree(iter);
2613 return -ENOMEM;
2614 }
2615
Steven Rostedtf45948e2011-05-02 12:29:25 -04002616 if (flag & FTRACE_ITER_NOTRACE)
2617 hash = ops->notrace_hash;
2618 else
2619 hash = ops->filter_hash;
2620
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002621 iter->ops = ops;
2622 iter->flags = flag;
2623
2624 if (file->f_mode & FMODE_WRITE) {
2625 mutex_lock(&ftrace_lock);
2626 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2627 mutex_unlock(&ftrace_lock);
2628
2629 if (!iter->hash) {
2630 trace_parser_put(&iter->parser);
2631 kfree(iter);
2632 return -ENOMEM;
2633 }
2634 }
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002635
Steven Rostedt41c52c02008-05-22 11:46:33 -04002636 mutex_lock(&ftrace_regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002637
Steven Rostedt5072c592008-05-12 21:20:43 +02002638 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002639 (file->f_flags & O_TRUNC))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002640 ftrace_filter_reset(iter->hash);
Steven Rostedt5072c592008-05-12 21:20:43 +02002641
2642 if (file->f_mode & FMODE_READ) {
2643 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02002644
2645 ret = seq_open(file, &show_ftrace_seq_ops);
2646 if (!ret) {
2647 struct seq_file *m = file->private_data;
2648 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08002649 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002650 /* Failed */
2651 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08002652 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002653 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08002654 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002655 } else
2656 file->private_data = iter;
Steven Rostedt41c52c02008-05-22 11:46:33 -04002657 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002658
2659 return ret;
2660}
2661
Steven Rostedt41c52c02008-05-22 11:46:33 -04002662static int
2663ftrace_filter_open(struct inode *inode, struct file *file)
2664{
Steven Rostedt69a30832011-12-19 15:21:16 -05002665 return ftrace_regex_open(&global_ops,
2666 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2667 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002668}
2669
2670static int
2671ftrace_notrace_open(struct inode *inode, struct file *file)
2672{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002673 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002674 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002675}
2676
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002677loff_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04002678ftrace_regex_lseek(struct file *file, loff_t offset, int origin)
Steven Rostedt5072c592008-05-12 21:20:43 +02002679{
2680 loff_t ret;
2681
2682 if (file->f_mode & FMODE_READ)
2683 ret = seq_lseek(file, offset, origin);
2684 else
2685 file->f_pos = ret = 1;
2686
2687 return ret;
2688}
2689
Steven Rostedt64e7c442009-02-13 17:08:48 -05002690static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002691{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002692 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08002693 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002694
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002695 switch (type) {
2696 case MATCH_FULL:
2697 if (strcmp(str, regex) == 0)
2698 matched = 1;
2699 break;
2700 case MATCH_FRONT_ONLY:
2701 if (strncmp(str, regex, len) == 0)
2702 matched = 1;
2703 break;
2704 case MATCH_MIDDLE_ONLY:
2705 if (strstr(str, regex))
2706 matched = 1;
2707 break;
2708 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08002709 slen = strlen(str);
2710 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002711 matched = 1;
2712 break;
2713 }
2714
2715 return matched;
2716}
2717
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002718static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002719enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04002720{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002721 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002722 int ret = 0;
2723
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002724 entry = ftrace_lookup_ip(hash, rec->ip);
2725 if (not) {
2726 /* Do nothing if it doesn't exist */
2727 if (!entry)
2728 return 0;
2729
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002730 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002731 } else {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002732 /* Do nothing if it exists */
2733 if (entry)
2734 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002735
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002736 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002737 }
2738 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04002739}
2740
Steven Rostedt64e7c442009-02-13 17:08:48 -05002741static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002742ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2743 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002744{
2745 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002746 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002747
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002748 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2749
2750 if (mod) {
2751 /* module lookup requires matching the module */
2752 if (!modname || strcmp(modname, mod))
2753 return 0;
2754
2755 /* blank search means to match all funcs in the mod */
2756 if (!len)
2757 return 1;
2758 }
2759
Steven Rostedt64e7c442009-02-13 17:08:48 -05002760 return ftrace_match(str, regex, len, type);
2761}
2762
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002763static int
2764match_records(struct ftrace_hash *hash, char *buff,
2765 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002766{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002767 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002768 struct ftrace_page *pg;
2769 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002770 int type = MATCH_FULL;
2771 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08002772 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002773 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002774
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002775 if (len) {
2776 type = filter_parse_regex(buff, len, &search, &not);
2777 search_len = strlen(search);
2778 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002779
Steven Rostedt52baf112009-02-14 01:15:39 -05002780 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002781
2782 if (unlikely(ftrace_disabled))
2783 goto out_unlock;
2784
Steven Rostedt265c8312009-02-13 12:43:56 -05002785 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002786 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002787 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002788 if (ret < 0) {
2789 found = ret;
2790 goto out_unlock;
2791 }
Li Zefan311d16d2009-12-08 11:15:11 +08002792 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05002793 }
2794 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002795 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05002796 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08002797
2798 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02002799}
2800
Steven Rostedt64e7c442009-02-13 17:08:48 -05002801static int
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002802ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002803{
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002804 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002805}
2806
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002807static int
2808ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002809{
Steven Rostedt64e7c442009-02-13 17:08:48 -05002810 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002811
Steven Rostedt64e7c442009-02-13 17:08:48 -05002812 /* blank or '*' mean the same */
2813 if (strcmp(buff, "*") == 0)
2814 buff[0] = 0;
2815
2816 /* handle the case of 'dont filter this module' */
2817 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2818 buff[0] = 0;
2819 not = 1;
2820 }
2821
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002822 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002823}
2824
Steven Rostedtf6180772009-02-14 00:40:25 -05002825/*
2826 * We register the module command as a template to show others how
2827 * to register the a command as well.
2828 */
2829
2830static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04002831ftrace_mod_callback(struct ftrace_hash *hash,
2832 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05002833{
2834 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002835 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05002836
2837 /*
2838 * cmd == 'mod' because we only registered this func
2839 * for the 'mod' ftrace_func_command.
2840 * But if you register one func with multiple commands,
2841 * you can tell which command was used by the cmd
2842 * parameter.
2843 */
2844
2845 /* we must have a module name */
2846 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002847 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002848
2849 mod = strsep(&param, ":");
2850 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002851 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002852
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04002853 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002854 if (!ret)
2855 ret = -EINVAL;
2856 if (ret < 0)
2857 return ret;
2858
2859 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05002860}
2861
2862static struct ftrace_func_command ftrace_mod_cmd = {
2863 .name = "mod",
2864 .func = ftrace_mod_callback,
2865};
2866
2867static int __init ftrace_mod_cmd_init(void)
2868{
2869 return register_ftrace_command(&ftrace_mod_cmd);
2870}
Steven Rostedt6f415672012-10-05 12:13:07 -04002871core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05002872
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04002873static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04002874 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002875{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002876 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002877 struct hlist_head *hhd;
2878 struct hlist_node *n;
2879 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002880
2881 key = hash_long(ip, FTRACE_HASH_BITS);
2882
2883 hhd = &ftrace_func_hash[key];
2884
2885 if (hlist_empty(hhd))
2886 return;
2887
2888 /*
2889 * Disable preemption for these calls to prevent a RCU grace
2890 * period. This syncs the hash iteration and freeing of items
2891 * on the hash. rcu_read_lock is too dangerous here.
2892 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04002893 preempt_disable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002894 hlist_for_each_entry_rcu(entry, n, hhd, node) {
2895 if (entry->ip == ip)
2896 entry->ops->func(ip, parent_ip, &entry->data);
2897 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04002898 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002899}
2900
Steven Rostedtb6887d72009-02-17 12:32:04 -05002901static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05002902{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04002903 .func = function_trace_probe_call,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002904};
2905
Steven Rostedtb6887d72009-02-17 12:32:04 -05002906static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002907
Steven Rostedtb6887d72009-02-17 12:32:04 -05002908static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002909{
Steven Rostedtb8489142011-05-04 09:27:52 -04002910 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002911 int i;
2912
Steven Rostedtb6887d72009-02-17 12:32:04 -05002913 if (ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002914 return;
2915
2916 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2917 struct hlist_head *hhd = &ftrace_func_hash[i];
2918 if (hhd->first)
2919 break;
2920 }
2921 /* Nothing registered? */
2922 if (i == FTRACE_FUNC_HASHSIZE)
2923 return;
2924
Steven Rostedtb8489142011-05-04 09:27:52 -04002925 ret = __register_ftrace_function(&trace_probe_ops);
2926 if (!ret)
Steven Rostedta1cd6172011-05-23 15:24:25 -04002927 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04002928
Steven Rostedtb6887d72009-02-17 12:32:04 -05002929 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002930}
2931
Steven Rostedtb6887d72009-02-17 12:32:04 -05002932static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002933{
Steven Rostedtb8489142011-05-04 09:27:52 -04002934 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002935 int i;
2936
Steven Rostedtb6887d72009-02-17 12:32:04 -05002937 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002938 return;
2939
2940 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2941 struct hlist_head *hhd = &ftrace_func_hash[i];
2942 if (hhd->first)
2943 return;
2944 }
2945
2946 /* no more funcs left */
Steven Rostedtb8489142011-05-04 09:27:52 -04002947 ret = __unregister_ftrace_function(&trace_probe_ops);
2948 if (!ret)
2949 ftrace_shutdown(&trace_probe_ops, 0);
2950
Steven Rostedtb6887d72009-02-17 12:32:04 -05002951 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002952}
2953
2954
2955static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2956{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002957 struct ftrace_func_probe *entry =
2958 container_of(rhp, struct ftrace_func_probe, rcu);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002959
2960 if (entry->ops->free)
2961 entry->ops->free(&entry->data);
2962 kfree(entry);
2963}
2964
2965
2966int
Steven Rostedtb6887d72009-02-17 12:32:04 -05002967register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002968 void *data)
2969{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002970 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002971 struct ftrace_page *pg;
2972 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002973 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002974 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002975 int count = 0;
2976 char *search;
2977
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002978 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002979 len = strlen(search);
2980
Steven Rostedtb6887d72009-02-17 12:32:04 -05002981 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002982 if (WARN_ON(not))
2983 return -EINVAL;
2984
2985 mutex_lock(&ftrace_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002986
Steven Rostedt45a4a232011-04-21 23:16:46 -04002987 if (unlikely(ftrace_disabled))
2988 goto out_unlock;
2989
Steven Rostedt59df055f2009-02-14 15:29:06 -05002990 do_for_each_ftrace_rec(pg, rec) {
2991
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002992 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05002993 continue;
2994
2995 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
2996 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05002997 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002998 if (!count)
2999 count = -ENOMEM;
3000 goto out_unlock;
3001 }
3002
3003 count++;
3004
3005 entry->data = data;
3006
3007 /*
3008 * The caller might want to do something special
3009 * for each function we find. We call the callback
3010 * to give the caller an opportunity to do so.
3011 */
3012 if (ops->callback) {
3013 if (ops->callback(rec->ip, &entry->data) < 0) {
3014 /* caller does not like this func */
3015 kfree(entry);
3016 continue;
3017 }
3018 }
3019
3020 entry->ops = ops;
3021 entry->ip = rec->ip;
3022
3023 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3024 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3025
3026 } while_for_each_ftrace_rec();
Steven Rostedtb6887d72009-02-17 12:32:04 -05003027 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003028
3029 out_unlock:
3030 mutex_unlock(&ftrace_lock);
3031
3032 return count;
3033}
3034
3035enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003036 PROBE_TEST_FUNC = 1,
3037 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05003038};
3039
3040static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003041__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003042 void *data, int flags)
3043{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003044 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003045 struct hlist_node *n, *tmp;
3046 char str[KSYM_SYMBOL_LEN];
3047 int type = MATCH_FULL;
3048 int i, len = 0;
3049 char *search;
3050
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003051 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003052 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003053 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003054 int not;
3055
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003056 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003057 len = strlen(search);
3058
Steven Rostedtb6887d72009-02-17 12:32:04 -05003059 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003060 if (WARN_ON(not))
3061 return;
3062 }
3063
3064 mutex_lock(&ftrace_lock);
3065 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3066 struct hlist_head *hhd = &ftrace_func_hash[i];
3067
3068 hlist_for_each_entry_safe(entry, n, tmp, hhd, node) {
3069
3070 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05003071 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003072 continue;
3073
Steven Rostedtb6887d72009-02-17 12:32:04 -05003074 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003075 continue;
3076
3077 /* do this last, since it is the most expensive */
3078 if (glob) {
3079 kallsyms_lookup(entry->ip, NULL, NULL,
3080 NULL, str);
3081 if (!ftrace_match(str, glob, len, type))
3082 continue;
3083 }
3084
3085 hlist_del(&entry->node);
3086 call_rcu(&entry->rcu, ftrace_free_entry_rcu);
3087 }
3088 }
Steven Rostedtb6887d72009-02-17 12:32:04 -05003089 __disable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003090 mutex_unlock(&ftrace_lock);
3091}
3092
3093void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003094unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003095 void *data)
3096{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003097 __unregister_ftrace_function_probe(glob, ops, data,
3098 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003099}
3100
3101void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003102unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003103{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003104 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003105}
3106
Steven Rostedtb6887d72009-02-17 12:32:04 -05003107void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003108{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003109 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003110}
3111
Steven Rostedtf6180772009-02-14 00:40:25 -05003112static LIST_HEAD(ftrace_commands);
3113static DEFINE_MUTEX(ftrace_cmd_mutex);
3114
3115int register_ftrace_command(struct ftrace_func_command *cmd)
3116{
3117 struct ftrace_func_command *p;
3118 int ret = 0;
3119
3120 mutex_lock(&ftrace_cmd_mutex);
3121 list_for_each_entry(p, &ftrace_commands, list) {
3122 if (strcmp(cmd->name, p->name) == 0) {
3123 ret = -EBUSY;
3124 goto out_unlock;
3125 }
3126 }
3127 list_add(&cmd->list, &ftrace_commands);
3128 out_unlock:
3129 mutex_unlock(&ftrace_cmd_mutex);
3130
3131 return ret;
3132}
3133
3134int unregister_ftrace_command(struct ftrace_func_command *cmd)
3135{
3136 struct ftrace_func_command *p, *n;
3137 int ret = -ENODEV;
3138
3139 mutex_lock(&ftrace_cmd_mutex);
3140 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3141 if (strcmp(cmd->name, p->name) == 0) {
3142 ret = 0;
3143 list_del_init(&p->list);
3144 goto out_unlock;
3145 }
3146 }
3147 out_unlock:
3148 mutex_unlock(&ftrace_cmd_mutex);
3149
3150 return ret;
3151}
3152
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003153static int ftrace_process_regex(struct ftrace_hash *hash,
3154 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003155{
Steven Rostedtf6180772009-02-14 00:40:25 -05003156 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003157 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08003158 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003159
3160 func = strsep(&next, ":");
3161
3162 if (!next) {
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003163 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003164 if (!ret)
3165 ret = -EINVAL;
3166 if (ret < 0)
3167 return ret;
3168 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003169 }
3170
Steven Rostedtf6180772009-02-14 00:40:25 -05003171 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05003172
3173 command = strsep(&next, ":");
3174
Steven Rostedtf6180772009-02-14 00:40:25 -05003175 mutex_lock(&ftrace_cmd_mutex);
3176 list_for_each_entry(p, &ftrace_commands, list) {
3177 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003178 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05003179 goto out_unlock;
3180 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05003181 }
Steven Rostedtf6180772009-02-14 00:40:25 -05003182 out_unlock:
3183 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003184
Steven Rostedtf6180772009-02-14 00:40:25 -05003185 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003186}
3187
Ingo Molnare309b412008-05-12 21:20:51 +02003188static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003189ftrace_regex_write(struct file *file, const char __user *ubuf,
3190 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02003191{
3192 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003193 struct trace_parser *parser;
3194 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02003195
Li Zefan4ba79782009-09-22 13:52:20 +08003196 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02003197 return 0;
3198
Steven Rostedt41c52c02008-05-22 11:46:33 -04003199 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003200
Steven Rostedt45a4a232011-04-21 23:16:46 -04003201 ret = -ENODEV;
3202 if (unlikely(ftrace_disabled))
3203 goto out_unlock;
3204
Steven Rostedt5072c592008-05-12 21:20:43 +02003205 if (file->f_mode & FMODE_READ) {
3206 struct seq_file *m = file->private_data;
3207 iter = m->private;
3208 } else
3209 iter = file->private_data;
3210
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003211 parser = &iter->parser;
3212 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02003213
Li Zefan4ba79782009-09-22 13:52:20 +08003214 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003215 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003216 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003217 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08003218 trace_parser_clear(parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02003219 if (ret)
Li Zefaned146b252009-11-03 08:55:38 +08003220 goto out_unlock;
Steven Rostedt5072c592008-05-12 21:20:43 +02003221 }
3222
Steven Rostedt5072c592008-05-12 21:20:43 +02003223 ret = read;
Li Zefaned146b252009-11-03 08:55:38 +08003224out_unlock:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003225 mutex_unlock(&ftrace_regex_lock);
Li Zefaned146b252009-11-03 08:55:38 +08003226
Steven Rostedt5072c592008-05-12 21:20:43 +02003227 return ret;
3228}
3229
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003230ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003231ftrace_filter_write(struct file *file, const char __user *ubuf,
3232 size_t cnt, loff_t *ppos)
3233{
3234 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3235}
3236
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003237ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003238ftrace_notrace_write(struct file *file, const char __user *ubuf,
3239 size_t cnt, loff_t *ppos)
3240{
3241 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3242}
3243
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003244static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003245ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3246{
3247 struct ftrace_func_entry *entry;
3248
3249 if (!ftrace_location(ip))
3250 return -EINVAL;
3251
3252 if (remove) {
3253 entry = ftrace_lookup_ip(hash, ip);
3254 if (!entry)
3255 return -ENOENT;
3256 free_hash_entry(hash, entry);
3257 return 0;
3258 }
3259
3260 return add_hash_entry(hash, ip);
3261}
3262
3263static int
3264ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3265 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003266{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003267 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003268 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003269 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003270
Steven Rostedt936e0742011-05-05 22:54:01 -04003271 /* All global ops uses the global ops filters */
3272 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3273 ops = &global_ops;
3274
Steven Rostedt41c52c02008-05-22 11:46:33 -04003275 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003276 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003277
Steven Rostedtf45948e2011-05-02 12:29:25 -04003278 if (enable)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003279 orig_hash = &ops->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003280 else
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003281 orig_hash = &ops->notrace_hash;
3282
3283 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3284 if (!hash)
3285 return -ENOMEM;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003286
Steven Rostedt41c52c02008-05-22 11:46:33 -04003287 mutex_lock(&ftrace_regex_lock);
3288 if (reset)
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003289 ftrace_filter_reset(hash);
Jiri Olsaac483c42012-01-02 10:04:14 +01003290 if (buf && !ftrace_match_records(hash, buf, len)) {
3291 ret = -EINVAL;
3292 goto out_regex_unlock;
3293 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003294 if (ip) {
3295 ret = ftrace_match_addr(hash, ip, remove);
3296 if (ret < 0)
3297 goto out_regex_unlock;
3298 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003299
3300 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003301 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt072126f2011-07-13 15:08:31 -04003302 if (!ret && ops->flags & FTRACE_OPS_FL_ENABLED
3303 && ftrace_enabled)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01003304 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt072126f2011-07-13 15:08:31 -04003305
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003306 mutex_unlock(&ftrace_lock);
3307
Jiri Olsaac483c42012-01-02 10:04:14 +01003308 out_regex_unlock:
Steven Rostedt41c52c02008-05-22 11:46:33 -04003309 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003310
3311 free_ftrace_hash(hash);
3312 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003313}
3314
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003315static int
3316ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3317 int reset, int enable)
3318{
3319 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3320}
3321
3322/**
3323 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3324 * @ops - the ops to set the filter with
3325 * @ip - the address to add to or remove from the filter.
3326 * @remove - non zero to remove the ip from the filter
3327 * @reset - non zero to reset all filters before applying this filter.
3328 *
3329 * Filters denote which functions should be enabled when tracing is enabled
3330 * If @ip is NULL, it failes to update filter.
3331 */
3332int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3333 int remove, int reset)
3334{
3335 return ftrace_set_addr(ops, ip, remove, reset, 1);
3336}
3337EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3338
3339static int
3340ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3341 int reset, int enable)
3342{
3343 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3344}
3345
Steven Rostedt77a2b372008-05-12 21:20:45 +02003346/**
3347 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003348 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02003349 * @buf - the string that holds the function filter text.
3350 * @len - the length of the string.
3351 * @reset - non zero to reset all filters before applying this filter.
3352 *
3353 * Filters denote which functions should be enabled when tracing is enabled.
3354 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3355 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003356int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003357 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02003358{
Jiri Olsaac483c42012-01-02 10:04:14 +01003359 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003360}
Steven Rostedt936e0742011-05-05 22:54:01 -04003361EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003362
Steven Rostedt41c52c02008-05-22 11:46:33 -04003363/**
3364 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003365 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04003366 * @buf - the string that holds the function notrace text.
3367 * @len - the length of the string.
3368 * @reset - non zero to reset all filters before applying this filter.
3369 *
3370 * Notrace Filters denote which functions should not be enabled when tracing
3371 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3372 * for tracing.
3373 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003374int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003375 int len, int reset)
3376{
Jiri Olsaac483c42012-01-02 10:04:14 +01003377 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04003378}
3379EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3380/**
3381 * ftrace_set_filter - set a function to filter on in ftrace
3382 * @ops - the ops to set the filter with
3383 * @buf - the string that holds the function filter text.
3384 * @len - the length of the string.
3385 * @reset - non zero to reset all filters before applying this filter.
3386 *
3387 * Filters denote which functions should be enabled when tracing is enabled.
3388 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3389 */
3390void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3391{
3392 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3393}
3394EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3395
3396/**
3397 * ftrace_set_notrace - set a function to not trace in ftrace
3398 * @ops - the ops to set the notrace filter with
3399 * @buf - the string that holds the function notrace text.
3400 * @len - the length of the string.
3401 * @reset - non zero to reset all filters before applying this filter.
3402 *
3403 * Notrace Filters denote which functions should not be enabled when tracing
3404 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3405 * for tracing.
3406 */
3407void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003408{
Steven Rostedtf45948e2011-05-02 12:29:25 -04003409 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003410}
Steven Rostedt936e0742011-05-05 22:54:01 -04003411EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003412
Steven Rostedt2af15d62009-05-28 13:37:24 -04003413/*
3414 * command line interface to allow users to set filters on boot up.
3415 */
3416#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3417static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3418static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3419
3420static int __init set_ftrace_notrace(char *str)
3421{
3422 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
3423 return 1;
3424}
3425__setup("ftrace_notrace=", set_ftrace_notrace);
3426
3427static int __init set_ftrace_filter(char *str)
3428{
3429 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
3430 return 1;
3431}
3432__setup("ftrace_filter=", set_ftrace_filter);
3433
Stefan Assmann369bc182009-10-12 22:17:21 +02003434#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08003435static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Steven Rostedt801c29f2010-03-05 20:02:19 -05003436static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
3437
Stefan Assmann369bc182009-10-12 22:17:21 +02003438static int __init set_graph_function(char *str)
3439{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02003440 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02003441 return 1;
3442}
3443__setup("ftrace_graph_filter=", set_graph_function);
3444
3445static void __init set_ftrace_early_graph(char *buf)
3446{
3447 int ret;
3448 char *func;
3449
3450 while (buf) {
3451 func = strsep(&buf, ",");
3452 /* we allow only one expression at a time */
3453 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3454 func);
3455 if (ret)
3456 printk(KERN_DEBUG "ftrace: function %s not "
3457 "traceable\n", func);
3458 }
3459}
3460#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3461
Steven Rostedt2a85a372011-12-19 21:57:44 -05003462void __init
3463ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04003464{
3465 char *func;
3466
3467 while (buf) {
3468 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04003469 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003470 }
3471}
3472
3473static void __init set_ftrace_early_filters(void)
3474{
3475 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003476 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003477 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003478 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02003479#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3480 if (ftrace_graph_buf[0])
3481 set_ftrace_early_graph(ftrace_graph_buf);
3482#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04003483}
3484
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003485int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003486{
3487 struct seq_file *m = (struct seq_file *)file->private_data;
3488 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003489 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003490 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04003491 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003492 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02003493
Steven Rostedt41c52c02008-05-22 11:46:33 -04003494 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003495 if (file->f_mode & FMODE_READ) {
3496 iter = m->private;
3497
3498 seq_release(inode, file);
3499 } else
3500 iter = file->private_data;
3501
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003502 parser = &iter->parser;
3503 if (trace_parser_loaded(parser)) {
3504 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003505 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02003506 }
3507
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003508 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003509
Steven Rostedt058e2972011-04-29 22:35:33 -04003510 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04003511 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3512
3513 if (filter_hash)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003514 orig_hash = &iter->ops->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04003515 else
3516 orig_hash = &iter->ops->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003517
Steven Rostedt058e2972011-04-29 22:35:33 -04003518 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003519 ret = ftrace_hash_move(iter->ops, filter_hash,
3520 orig_hash, iter->hash);
3521 if (!ret && (iter->ops->flags & FTRACE_OPS_FL_ENABLED)
3522 && ftrace_enabled)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01003523 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003524
Steven Rostedt058e2972011-04-29 22:35:33 -04003525 mutex_unlock(&ftrace_lock);
3526 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003527 free_ftrace_hash(iter->hash);
3528 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04003529
Steven Rostedt41c52c02008-05-22 11:46:33 -04003530 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003531 return 0;
3532}
3533
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003534static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003535 .open = ftrace_avail_open,
3536 .read = seq_read,
3537 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08003538 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02003539};
3540
Steven Rostedt647bcd02011-05-03 14:39:21 -04003541static const struct file_operations ftrace_enabled_fops = {
3542 .open = ftrace_enabled_open,
3543 .read = seq_read,
3544 .llseek = seq_lseek,
3545 .release = seq_release_private,
3546};
3547
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003548static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003549 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003550 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02003551 .write = ftrace_filter_write,
Steven Rostedt98c4fd02010-09-10 11:47:43 -04003552 .llseek = ftrace_regex_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003553 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02003554};
3555
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003556static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04003557 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003558 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003559 .write = ftrace_notrace_write,
3560 .llseek = ftrace_regex_lseek,
Steven Rostedt1cf41dd72011-04-29 20:59:51 -04003561 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003562};
3563
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003564#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3565
3566static DEFINE_MUTEX(graph_lock);
3567
3568int ftrace_graph_count;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003569int ftrace_graph_filter_enabled;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003570unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3571
3572static void *
Li Zefan85951842009-06-24 09:54:00 +08003573__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003574{
Li Zefan85951842009-06-24 09:54:00 +08003575 if (*pos >= ftrace_graph_count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003576 return NULL;
Li Zefana4ec5e02009-09-18 14:06:28 +08003577 return &ftrace_graph_funcs[*pos];
Li Zefan85951842009-06-24 09:54:00 +08003578}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003579
Li Zefan85951842009-06-24 09:54:00 +08003580static void *
3581g_next(struct seq_file *m, void *v, loff_t *pos)
3582{
3583 (*pos)++;
3584 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003585}
3586
3587static void *g_start(struct seq_file *m, loff_t *pos)
3588{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003589 mutex_lock(&graph_lock);
3590
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003591 /* Nothing, tell g_show to print all functions are enabled */
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003592 if (!ftrace_graph_filter_enabled && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003593 return (void *)1;
3594
Li Zefan85951842009-06-24 09:54:00 +08003595 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003596}
3597
3598static void g_stop(struct seq_file *m, void *p)
3599{
3600 mutex_unlock(&graph_lock);
3601}
3602
3603static int g_show(struct seq_file *m, void *v)
3604{
3605 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003606
3607 if (!ptr)
3608 return 0;
3609
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003610 if (ptr == (unsigned long *)1) {
3611 seq_printf(m, "#### all functions enabled ####\n");
3612 return 0;
3613 }
3614
Steven Rostedtb375a112009-09-17 00:05:58 -04003615 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003616
3617 return 0;
3618}
3619
James Morris88e9d342009-09-22 16:43:43 -07003620static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003621 .start = g_start,
3622 .next = g_next,
3623 .stop = g_stop,
3624 .show = g_show,
3625};
3626
3627static int
3628ftrace_graph_open(struct inode *inode, struct file *file)
3629{
3630 int ret = 0;
3631
3632 if (unlikely(ftrace_disabled))
3633 return -ENODEV;
3634
3635 mutex_lock(&graph_lock);
3636 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04003637 (file->f_flags & O_TRUNC)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003638 ftrace_graph_filter_enabled = 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003639 ftrace_graph_count = 0;
3640 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3641 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003642 mutex_unlock(&graph_lock);
3643
Li Zefana4ec5e02009-09-18 14:06:28 +08003644 if (file->f_mode & FMODE_READ)
3645 ret = seq_open(file, &ftrace_graph_seq_ops);
3646
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003647 return ret;
3648}
3649
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003650static int
Li Zefan87827112009-07-23 11:29:11 +08003651ftrace_graph_release(struct inode *inode, struct file *file)
3652{
3653 if (file->f_mode & FMODE_READ)
3654 seq_release(inode, file);
3655 return 0;
3656}
3657
3658static int
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003659ftrace_set_func(unsigned long *array, int *idx, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003660{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003661 struct dyn_ftrace *rec;
3662 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003663 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003664 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003665 int type, not;
3666 char *search;
3667 bool exists;
3668 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003669
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003670 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003671 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003672 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3673 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003674
3675 search_len = strlen(search);
3676
Steven Rostedt52baf112009-02-14 01:15:39 -05003677 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003678
3679 if (unlikely(ftrace_disabled)) {
3680 mutex_unlock(&ftrace_lock);
3681 return -ENODEV;
3682 }
3683
Steven Rostedt265c8312009-02-13 12:43:56 -05003684 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003685
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003686 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003687 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003688 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003689 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003690 if (array[i] == rec->ip) {
3691 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05003692 break;
3693 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003694 }
3695
3696 if (!not) {
3697 fail = 0;
3698 if (!exists) {
3699 array[(*idx)++] = rec->ip;
3700 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3701 goto out;
3702 }
3703 } else {
3704 if (exists) {
3705 array[i] = array[--(*idx)];
3706 array[*idx] = 0;
3707 fail = 0;
3708 }
3709 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003710 }
Steven Rostedt265c8312009-02-13 12:43:56 -05003711 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003712out:
Steven Rostedt52baf112009-02-14 01:15:39 -05003713 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003714
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003715 if (fail)
3716 return -EINVAL;
3717
3718 ftrace_graph_filter_enabled = 1;
3719 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003720}
3721
3722static ssize_t
3723ftrace_graph_write(struct file *file, const char __user *ubuf,
3724 size_t cnt, loff_t *ppos)
3725{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003726 struct trace_parser parser;
Li Zefan4ba79782009-09-22 13:52:20 +08003727 ssize_t read, ret;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003728
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003729 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003730 return 0;
3731
3732 mutex_lock(&graph_lock);
3733
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003734 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3735 ret = -ENOMEM;
Li Zefan1eb90f12009-09-22 13:52:57 +08003736 goto out_unlock;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003737 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003738
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003739 read = trace_get_user(&parser, ubuf, cnt, ppos);
3740
Li Zefan4ba79782009-09-22 13:52:20 +08003741 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003742 parser.buffer[parser.idx] = 0;
3743
3744 /* we allow only one expression at a time */
Li Zefana4ec5e02009-09-18 14:06:28 +08003745 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003746 parser.buffer);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003747 if (ret)
Li Zefan1eb90f12009-09-22 13:52:57 +08003748 goto out_free;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003749 }
3750
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003751 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08003752
3753out_free:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003754 trace_parser_put(&parser);
Li Zefan1eb90f12009-09-22 13:52:57 +08003755out_unlock:
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003756 mutex_unlock(&graph_lock);
3757
3758 return ret;
3759}
3760
3761static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08003762 .open = ftrace_graph_open,
3763 .read = seq_read,
3764 .write = ftrace_graph_write,
3765 .release = ftrace_graph_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003766 .llseek = seq_lseek,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003767};
3768#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3769
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003770static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02003771{
Steven Rostedt5072c592008-05-12 21:20:43 +02003772
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003773 trace_create_file("available_filter_functions", 0444,
3774 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02003775
Steven Rostedt647bcd02011-05-03 14:39:21 -04003776 trace_create_file("enabled_functions", 0444,
3777 d_tracer, NULL, &ftrace_enabled_fops);
3778
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003779 trace_create_file("set_ftrace_filter", 0644, d_tracer,
3780 NULL, &ftrace_filter_fops);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003781
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003782 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003783 NULL, &ftrace_notrace_fops);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04003784
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003785#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003786 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003787 NULL,
3788 &ftrace_graph_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003789#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3790
Steven Rostedt5072c592008-05-12 21:20:43 +02003791 return 0;
3792}
3793
Steven Rostedt9fd49322012-04-24 22:32:06 -04003794static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05003795{
Steven Rostedt9fd49322012-04-24 22:32:06 -04003796 const unsigned long *ipa = a;
3797 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05003798
Steven Rostedt9fd49322012-04-24 22:32:06 -04003799 if (*ipa > *ipb)
3800 return 1;
3801 if (*ipa < *ipb)
3802 return -1;
3803 return 0;
3804}
3805
3806static void ftrace_swap_ips(void *a, void *b, int size)
3807{
3808 unsigned long *ipa = a;
3809 unsigned long *ipb = b;
3810 unsigned long t;
3811
3812 t = *ipa;
3813 *ipa = *ipb;
3814 *ipb = t;
Steven Rostedt68950612011-12-16 17:06:45 -05003815}
3816
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003817static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08003818 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003819 unsigned long *end)
3820{
Steven Rostedt706c81f2012-04-24 23:45:26 -04003821 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05003822 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04003823 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05003824 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003825 unsigned long *p;
3826 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04003827 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05003828 int ret = -ENOMEM;
3829
3830 count = end - start;
3831
3832 if (!count)
3833 return 0;
3834
Steven Rostedt9fd49322012-04-24 22:32:06 -04003835 sort(start, count, sizeof(*start),
3836 ftrace_cmp_ips, ftrace_swap_ips);
3837
Steven Rostedt706c81f2012-04-24 23:45:26 -04003838 start_pg = ftrace_allocate_pages(count);
3839 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05003840 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003841
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003842 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05003843
Steven Rostedt32082302011-12-16 14:42:37 -05003844 /*
3845 * Core and each module needs their own pages, as
3846 * modules will free them when they are removed.
3847 * Force a new page to be allocated for modules.
3848 */
Steven Rostedta7900872011-12-16 16:23:44 -05003849 if (!mod) {
3850 WARN_ON(ftrace_pages || ftrace_pages_start);
3851 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04003852 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05003853 } else {
Steven Rostedt32082302011-12-16 14:42:37 -05003854 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05003855 goto out;
Steven Rostedt32082302011-12-16 14:42:37 -05003856
Steven Rostedta7900872011-12-16 16:23:44 -05003857 if (WARN_ON(ftrace_pages->next)) {
3858 /* Hmm, we have free pages? */
3859 while (ftrace_pages->next)
3860 ftrace_pages = ftrace_pages->next;
Steven Rostedt32082302011-12-16 14:42:37 -05003861 }
Steven Rostedta7900872011-12-16 16:23:44 -05003862
Steven Rostedt706c81f2012-04-24 23:45:26 -04003863 ftrace_pages->next = start_pg;
Steven Rostedt32082302011-12-16 14:42:37 -05003864 }
3865
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003866 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04003867 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003868 while (p < end) {
3869 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08003870 /*
3871 * Some architecture linkers will pad between
3872 * the different mcount_loc sections of different
3873 * object files to satisfy alignments.
3874 * Skip any NULL pointers.
3875 */
3876 if (!addr)
3877 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04003878
3879 if (pg->index == pg->size) {
3880 /* We should have allocated enough */
3881 if (WARN_ON(!pg->next))
3882 break;
3883 pg = pg->next;
3884 }
3885
3886 rec = &pg->records[pg->index++];
3887 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003888 }
3889
Steven Rostedt706c81f2012-04-24 23:45:26 -04003890 /* We should have used all pages */
3891 WARN_ON(pg->next);
3892
3893 /* Assign the last page to ftrace_pages */
3894 ftrace_pages = pg;
3895
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003896 /* These new locations need to be initialized */
Steven Rostedt706c81f2012-04-24 23:45:26 -04003897 ftrace_new_pgs = start_pg;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003898
Steven Rostedta4f18ed2011-06-07 09:26:46 -04003899 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04003900 * We only need to disable interrupts on start up
3901 * because we are modifying code that an interrupt
3902 * may execute, and the modification is not atomic.
3903 * But for modules, nothing runs the code we modify
3904 * until we are finished with it, and there's no
3905 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04003906 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04003907 if (!mod)
3908 local_irq_save(flags);
Steven Rostedt31e88902008-11-14 16:21:19 -08003909 ftrace_update_code(mod);
Steven Rostedt4376cac2011-06-24 23:28:13 -04003910 if (!mod)
3911 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05003912 ret = 0;
3913 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003914 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003915
Steven Rostedta7900872011-12-16 16:23:44 -05003916 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003917}
3918
Steven Rostedt93eb6772009-04-15 13:24:06 -04003919#ifdef CONFIG_MODULES
Steven Rostedt32082302011-12-16 14:42:37 -05003920
3921#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
3922
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003923void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04003924{
3925 struct dyn_ftrace *rec;
Steven Rostedt32082302011-12-16 14:42:37 -05003926 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04003927 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05003928 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04003929
Steven Rostedt93eb6772009-04-15 13:24:06 -04003930 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003931
3932 if (ftrace_disabled)
3933 goto out_unlock;
3934
Steven Rostedt32082302011-12-16 14:42:37 -05003935 /*
3936 * Each module has its own ftrace_pages, remove
3937 * them from the list.
3938 */
3939 last_pg = &ftrace_pages_start;
3940 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
3941 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003942 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04003943 /*
Steven Rostedt32082302011-12-16 14:42:37 -05003944 * As core pages are first, the first
3945 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04003946 */
Steven Rostedt32082302011-12-16 14:42:37 -05003947 if (WARN_ON(pg == ftrace_pages_start))
3948 goto out_unlock;
3949
3950 /* Check if we are deleting the last page */
3951 if (pg == ftrace_pages)
3952 ftrace_pages = next_to_ftrace_page(last_pg);
3953
3954 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05003955 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3956 free_pages((unsigned long)pg->records, order);
3957 kfree(pg);
Steven Rostedt32082302011-12-16 14:42:37 -05003958 } else
3959 last_pg = &pg->next;
3960 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003961 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04003962 mutex_unlock(&ftrace_lock);
3963}
3964
3965static void ftrace_init_module(struct module *mod,
3966 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04003967{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04003968 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04003969 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003970 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04003971}
3972
Steven Rostedt93eb6772009-04-15 13:24:06 -04003973static int ftrace_module_notify(struct notifier_block *self,
3974 unsigned long val, void *data)
3975{
3976 struct module *mod = data;
3977
3978 switch (val) {
3979 case MODULE_STATE_COMING:
3980 ftrace_init_module(mod, mod->ftrace_callsites,
3981 mod->ftrace_callsites +
3982 mod->num_ftrace_callsites);
3983 break;
3984 case MODULE_STATE_GOING:
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003985 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04003986 break;
3987 }
3988
3989 return 0;
3990}
3991#else
3992static int ftrace_module_notify(struct notifier_block *self,
3993 unsigned long val, void *data)
3994{
3995 return 0;
3996}
3997#endif /* CONFIG_MODULES */
3998
3999struct notifier_block ftrace_module_nb = {
4000 .notifier_call = ftrace_module_notify,
4001 .priority = 0,
4002};
4003
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004004extern unsigned long __start_mcount_loc[];
4005extern unsigned long __stop_mcount_loc[];
4006
4007void __init ftrace_init(void)
4008{
4009 unsigned long count, addr, flags;
4010 int ret;
4011
4012 /* Keep the ftrace pointer to the stub */
4013 addr = (unsigned long)ftrace_stub;
4014
4015 local_irq_save(flags);
4016 ftrace_dyn_arch_init(&addr);
4017 local_irq_restore(flags);
4018
4019 /* ftrace_dyn_arch_init places the return code in addr */
4020 if (addr)
4021 goto failed;
4022
4023 count = __stop_mcount_loc - __start_mcount_loc;
4024
4025 ret = ftrace_dyn_table_alloc(count);
4026 if (ret)
4027 goto failed;
4028
4029 last_ftrace_enabled = ftrace_enabled = 1;
4030
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004031 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08004032 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004033 __stop_mcount_loc);
4034
Steven Rostedt93eb6772009-04-15 13:24:06 -04004035 ret = register_module_notifier(&ftrace_module_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08004036 if (ret)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004037 pr_warning("Failed to register trace ftrace module notifier\n");
4038
Steven Rostedt2af15d62009-05-28 13:37:24 -04004039 set_ftrace_early_filters();
4040
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004041 return;
4042 failed:
4043 ftrace_disabled = 1;
4044}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004045
Steven Rostedt3d083392008-05-12 21:20:42 +02004046#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004047
Steven Rostedt2b499382011-05-03 22:49:52 -04004048static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04004049 .func = ftrace_stub,
Steven Rostedt47409742012-07-20 11:04:44 -04004050 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Steven Rostedtbd69c302011-05-03 21:55:54 -04004051};
4052
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004053static int __init ftrace_nodyn_init(void)
4054{
4055 ftrace_enabled = 1;
4056 return 0;
4057}
Steven Rostedt6f415672012-10-05 12:13:07 -04004058core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004059
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004060static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4061static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05004062/* Keep as macros so we do not need to define the commands */
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04004063# define ftrace_startup(ops, command) \
4064 ({ \
4065 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4066 0; \
4067 })
Steven Rostedtbd69c302011-05-03 21:55:54 -04004068# define ftrace_shutdown(ops, command) do { } while (0)
Ingo Molnarc7aafc52008-05-12 21:20:45 +02004069# define ftrace_startup_sysctl() do { } while (0)
4070# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04004071
4072static inline int
4073ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
4074{
4075 return 1;
4076}
4077
Steven Rostedt3d083392008-05-12 21:20:42 +02004078#endif /* CONFIG_DYNAMIC_FTRACE */
4079
Steven Rostedtb8489142011-05-04 09:27:52 -04004080static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004081ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004082 struct ftrace_ops *op, struct pt_regs *regs)
Jiri Olsae2484912012-02-15 15:51:48 +01004083{
Jiri Olsae2484912012-02-15 15:51:48 +01004084 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4085 return;
4086
4087 /*
4088 * Some of the ops may be dynamically allocated,
4089 * they must be freed after a synchronize_sched().
4090 */
4091 preempt_disable_notrace();
4092 trace_recursion_set(TRACE_CONTROL_BIT);
4093 op = rcu_dereference_raw(ftrace_control_list);
4094 while (op != &ftrace_list_end) {
4095 if (!ftrace_function_local_disabled(op) &&
4096 ftrace_ops_test(op, ip))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004097 op->func(ip, parent_ip, op, regs);
Jiri Olsae2484912012-02-15 15:51:48 +01004098
4099 op = rcu_dereference_raw(op->next);
4100 };
4101 trace_recursion_clear(TRACE_CONTROL_BIT);
4102 preempt_enable_notrace();
4103}
4104
4105static struct ftrace_ops control_ops = {
4106 .func = ftrace_ops_control_func,
Steven Rostedt47409742012-07-20 11:04:44 -04004107 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Jiri Olsae2484912012-02-15 15:51:48 +01004108};
4109
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004110static inline void
4111__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004112 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004113{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004114 struct ftrace_ops *op;
Steven Rostedtb8489142011-05-04 09:27:52 -04004115
Steven Rostedtccf36722012-06-05 09:44:25 -04004116 if (function_trace_stop)
4117 return;
4118
Steven Rostedtb1cff0a2011-05-25 14:27:43 -04004119 if (unlikely(trace_recursion_test(TRACE_INTERNAL_BIT)))
4120 return;
4121
4122 trace_recursion_set(TRACE_INTERNAL_BIT);
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004123 /*
4124 * Some of the ops may be dynamically allocated,
4125 * they must be freed after a synchronize_sched().
4126 */
4127 preempt_disable_notrace();
4128 op = rcu_dereference_raw(ftrace_ops_list);
Steven Rostedtb8489142011-05-04 09:27:52 -04004129 while (op != &ftrace_list_end) {
4130 if (ftrace_ops_test(op, ip))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004131 op->func(ip, parent_ip, op, regs);
Steven Rostedtb8489142011-05-04 09:27:52 -04004132 op = rcu_dereference_raw(op->next);
4133 };
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004134 preempt_enable_notrace();
Steven Rostedtb1cff0a2011-05-25 14:27:43 -04004135 trace_recursion_clear(TRACE_INTERNAL_BIT);
Steven Rostedtb8489142011-05-04 09:27:52 -04004136}
4137
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004138/*
4139 * Some archs only support passing ip and parent_ip. Even though
4140 * the list function ignores the op parameter, we do not want any
4141 * C side effects, where a function is called without the caller
4142 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004143 * Archs are to support both the regs and ftrace_ops at the same time.
4144 * If they support ftrace_ops, it is assumed they support regs.
4145 * If call backs want to use regs, they must either check for regs
4146 * being NULL, or ARCH_SUPPORTS_FTRACE_SAVE_REGS.
4147 * Note, ARCH_SUPPORT_SAVE_REGS expects a full regs to be saved.
4148 * An architecture can pass partial regs with ftrace_ops and still
4149 * set the ARCH_SUPPORT_FTARCE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004150 */
4151#if ARCH_SUPPORTS_FTRACE_OPS
4152static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004153 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004154{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004155 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004156}
4157#else
4158static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4159{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004160 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004161}
4162#endif
4163
Steven Rostedte32d8952008-12-04 00:26:41 -05004164static void clear_ftrace_swapper(void)
4165{
4166 struct task_struct *p;
4167 int cpu;
4168
4169 get_online_cpus();
4170 for_each_online_cpu(cpu) {
4171 p = idle_task(cpu);
4172 clear_tsk_trace_trace(p);
4173 }
4174 put_online_cpus();
4175}
4176
4177static void set_ftrace_swapper(void)
4178{
4179 struct task_struct *p;
4180 int cpu;
4181
4182 get_online_cpus();
4183 for_each_online_cpu(cpu) {
4184 p = idle_task(cpu);
4185 set_tsk_trace_trace(p);
4186 }
4187 put_online_cpus();
4188}
4189
4190static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004191{
4192 struct task_struct *p;
4193
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004194 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05004195 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05004196 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05004197 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004198 rcu_read_unlock();
4199
Steven Rostedte32d8952008-12-04 00:26:41 -05004200 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004201}
4202
Steven Rostedte32d8952008-12-04 00:26:41 -05004203static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004204{
4205 struct task_struct *p;
4206
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004207 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004208 do_each_pid_task(pid, PIDTYPE_PID, p) {
4209 set_tsk_trace_trace(p);
4210 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004211 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004212}
4213
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004214static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004215{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004216 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004217 clear_ftrace_swapper();
4218 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004219 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05004220}
4221
4222static void set_ftrace_pid_task(struct pid *pid)
4223{
4224 if (pid == ftrace_swapper_pid)
4225 set_ftrace_swapper();
4226 else
4227 set_ftrace_pid(pid);
4228}
4229
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004230static int ftrace_pid_add(int p)
4231{
4232 struct pid *pid;
4233 struct ftrace_pid *fpid;
4234 int ret = -EINVAL;
4235
4236 mutex_lock(&ftrace_lock);
4237
4238 if (!p)
4239 pid = ftrace_swapper_pid;
4240 else
4241 pid = find_get_pid(p);
4242
4243 if (!pid)
4244 goto out;
4245
4246 ret = 0;
4247
4248 list_for_each_entry(fpid, &ftrace_pids, list)
4249 if (fpid->pid == pid)
4250 goto out_put;
4251
4252 ret = -ENOMEM;
4253
4254 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4255 if (!fpid)
4256 goto out_put;
4257
4258 list_add(&fpid->list, &ftrace_pids);
4259 fpid->pid = pid;
4260
4261 set_ftrace_pid_task(pid);
4262
4263 ftrace_update_pid_func();
4264 ftrace_startup_enable(0);
4265
4266 mutex_unlock(&ftrace_lock);
4267 return 0;
4268
4269out_put:
4270 if (pid != ftrace_swapper_pid)
4271 put_pid(pid);
4272
4273out:
4274 mutex_unlock(&ftrace_lock);
4275 return ret;
4276}
4277
4278static void ftrace_pid_reset(void)
4279{
4280 struct ftrace_pid *fpid, *safe;
4281
4282 mutex_lock(&ftrace_lock);
4283 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4284 struct pid *pid = fpid->pid;
4285
4286 clear_ftrace_pid_task(pid);
4287
4288 list_del(&fpid->list);
4289 kfree(fpid);
4290 }
4291
4292 ftrace_update_pid_func();
4293 ftrace_startup_enable(0);
4294
4295 mutex_unlock(&ftrace_lock);
4296}
4297
4298static void *fpid_start(struct seq_file *m, loff_t *pos)
4299{
4300 mutex_lock(&ftrace_lock);
4301
4302 if (list_empty(&ftrace_pids) && (!*pos))
4303 return (void *) 1;
4304
4305 return seq_list_start(&ftrace_pids, *pos);
4306}
4307
4308static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4309{
4310 if (v == (void *)1)
4311 return NULL;
4312
4313 return seq_list_next(v, &ftrace_pids, pos);
4314}
4315
4316static void fpid_stop(struct seq_file *m, void *p)
4317{
4318 mutex_unlock(&ftrace_lock);
4319}
4320
4321static int fpid_show(struct seq_file *m, void *v)
4322{
4323 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4324
4325 if (v == (void *)1) {
4326 seq_printf(m, "no pid\n");
4327 return 0;
4328 }
4329
4330 if (fpid->pid == ftrace_swapper_pid)
4331 seq_printf(m, "swapper tasks\n");
4332 else
4333 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4334
4335 return 0;
4336}
4337
4338static const struct seq_operations ftrace_pid_sops = {
4339 .start = fpid_start,
4340 .next = fpid_next,
4341 .stop = fpid_stop,
4342 .show = fpid_show,
4343};
4344
4345static int
4346ftrace_pid_open(struct inode *inode, struct file *file)
4347{
4348 int ret = 0;
4349
4350 if ((file->f_mode & FMODE_WRITE) &&
4351 (file->f_flags & O_TRUNC))
4352 ftrace_pid_reset();
4353
4354 if (file->f_mode & FMODE_READ)
4355 ret = seq_open(file, &ftrace_pid_sops);
4356
4357 return ret;
4358}
4359
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004360static ssize_t
4361ftrace_pid_write(struct file *filp, const char __user *ubuf,
4362 size_t cnt, loff_t *ppos)
4363{
Ingo Molnar457dc922009-11-23 11:03:28 +01004364 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004365 long val;
4366 int ret;
4367
4368 if (cnt >= sizeof(buf))
4369 return -EINVAL;
4370
4371 if (copy_from_user(&buf, ubuf, cnt))
4372 return -EFAULT;
4373
4374 buf[cnt] = 0;
4375
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004376 /*
4377 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4378 * to clean the filter quietly.
4379 */
Ingo Molnar457dc922009-11-23 11:03:28 +01004380 tmp = strstrip(buf);
4381 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004382 return 1;
4383
Daniel Walterbcd83ea2012-09-26 22:08:38 +02004384 ret = kstrtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004385 if (ret < 0)
4386 return ret;
4387
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004388 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004389
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004390 return ret ? ret : cnt;
4391}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004392
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004393static int
4394ftrace_pid_release(struct inode *inode, struct file *file)
4395{
4396 if (file->f_mode & FMODE_READ)
4397 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004398
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004399 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004400}
4401
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004402static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004403 .open = ftrace_pid_open,
4404 .write = ftrace_pid_write,
4405 .read = seq_read,
4406 .llseek = seq_lseek,
4407 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004408};
4409
4410static __init int ftrace_init_debugfs(void)
4411{
4412 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004413
4414 d_tracer = tracing_init_dentry();
4415 if (!d_tracer)
4416 return 0;
4417
4418 ftrace_init_dyn_debugfs(d_tracer);
4419
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004420 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4421 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04004422
4423 ftrace_profile_debugfs(d_tracer);
4424
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004425 return 0;
4426}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004427fs_initcall(ftrace_init_debugfs);
4428
Steven Rostedt3d083392008-05-12 21:20:42 +02004429/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004430 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004431 *
4432 * This function should be used by panic code. It stops ftrace
4433 * but in a not so nice way. If you need to simply kill ftrace
4434 * from a non-atomic section, use ftrace_kill.
4435 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004436void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004437{
4438 ftrace_disabled = 1;
4439 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004440 clear_ftrace_function();
4441}
4442
4443/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04004444 * Test if ftrace is dead or not.
4445 */
4446int ftrace_is_dead(void)
4447{
4448 return ftrace_disabled;
4449}
4450
4451/**
Steven Rostedt3d083392008-05-12 21:20:42 +02004452 * register_ftrace_function - register a function for profiling
4453 * @ops - ops structure that holds the function for profiling.
4454 *
4455 * Register a function to be called by all functions in the
4456 * kernel.
4457 *
4458 * Note: @ops->func and all the functions it calls must be labeled
4459 * with "notrace", otherwise it will go into a
4460 * recursive loop.
4461 */
4462int register_ftrace_function(struct ftrace_ops *ops)
4463{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004464 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004465
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004466 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004467
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004468 ret = __register_ftrace_function(ops);
Steven Rostedtb8489142011-05-04 09:27:52 -04004469 if (!ret)
Steven Rostedta1cd6172011-05-23 15:24:25 -04004470 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04004471
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004472 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02004473
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004474 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02004475}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004476EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02004477
4478/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01004479 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02004480 * @ops - ops structure that holds the function to unregister
4481 *
4482 * Unregister a function that was added to be called by ftrace profiling.
4483 */
4484int unregister_ftrace_function(struct ftrace_ops *ops)
4485{
4486 int ret;
4487
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004488 mutex_lock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004489 ret = __unregister_ftrace_function(ops);
Steven Rostedtb8489142011-05-04 09:27:52 -04004490 if (!ret)
4491 ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004492 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004493
4494 return ret;
4495}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004496EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004497
Ingo Molnare309b412008-05-12 21:20:51 +02004498int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004499ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004500 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004501 loff_t *ppos)
4502{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004503 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004504
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004505 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004506
Steven Rostedt45a4a232011-04-21 23:16:46 -04004507 if (unlikely(ftrace_disabled))
4508 goto out;
4509
4510 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004511
Li Zefana32c7762009-06-26 16:55:51 +08004512 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004513 goto out;
4514
Li Zefana32c7762009-06-26 16:55:51 +08004515 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004516
4517 if (ftrace_enabled) {
4518
4519 ftrace_startup_sysctl();
4520
4521 /* we are starting ftrace again */
Steven Rostedtb8489142011-05-04 09:27:52 -04004522 if (ftrace_ops_list != &ftrace_list_end) {
4523 if (ftrace_ops_list->next == &ftrace_list_end)
4524 ftrace_trace_function = ftrace_ops_list->func;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004525 else
Steven Rostedtb8489142011-05-04 09:27:52 -04004526 ftrace_trace_function = ftrace_ops_list_func;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004527 }
4528
4529 } else {
4530 /* stopping ftrace calls (just send to ftrace_stub) */
4531 ftrace_trace_function = ftrace_stub;
4532
4533 ftrace_shutdown_sysctl();
4534 }
4535
4536 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004537 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004538 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02004539}
Ingo Molnarf17845e2008-10-24 12:47:10 +02004540
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004541#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004542
Steven Rostedt597af812009-04-03 15:24:12 -04004543static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004544static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004545
Steven Rostedte49dc192008-12-02 23:50:05 -05004546int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4547{
4548 return 0;
4549}
4550
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004551/* The callbacks that hook a function */
4552trace_func_graph_ret_t ftrace_graph_return =
4553 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004554trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004555
4556/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4557static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4558{
4559 int i;
4560 int ret = 0;
4561 unsigned long flags;
4562 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4563 struct task_struct *g, *t;
4564
4565 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4566 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4567 * sizeof(struct ftrace_ret_stack),
4568 GFP_KERNEL);
4569 if (!ret_stack_list[i]) {
4570 start = 0;
4571 end = i;
4572 ret = -ENOMEM;
4573 goto free;
4574 }
4575 }
4576
4577 read_lock_irqsave(&tasklist_lock, flags);
4578 do_each_thread(g, t) {
4579 if (start == end) {
4580 ret = -EAGAIN;
4581 goto unlock;
4582 }
4583
4584 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01004585 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004586 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04004587 t->curr_ret_stack = -1;
4588 /* Make sure the tasks see the -1 first: */
4589 smp_wmb();
4590 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004591 }
4592 } while_each_thread(g, t);
4593
4594unlock:
4595 read_unlock_irqrestore(&tasklist_lock, flags);
4596free:
4597 for (i = start; i < end; i++)
4598 kfree(ret_stack_list[i]);
4599 return ret;
4600}
4601
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004602static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04004603ftrace_graph_probe_sched_switch(void *ignore,
4604 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004605{
4606 unsigned long long timestamp;
4607 int index;
4608
Steven Rostedtbe6f1642009-03-24 11:06:24 -04004609 /*
4610 * Does the user want to count the time a function was asleep.
4611 * If so, do not update the time stamps.
4612 */
4613 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4614 return;
4615
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004616 timestamp = trace_clock_local();
4617
4618 prev->ftrace_timestamp = timestamp;
4619
4620 /* only process tasks that we timestamped */
4621 if (!next->ftrace_timestamp)
4622 return;
4623
4624 /*
4625 * Update all the counters in next to make up for the
4626 * time next was sleeping.
4627 */
4628 timestamp -= next->ftrace_timestamp;
4629
4630 for (index = next->curr_ret_stack; index >= 0; index--)
4631 next->ret_stack[index].calltime += timestamp;
4632}
4633
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004634/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004635static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004636{
4637 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004638 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004639
4640 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4641 sizeof(struct ftrace_ret_stack *),
4642 GFP_KERNEL);
4643
4644 if (!ret_stack_list)
4645 return -ENOMEM;
4646
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004647 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04004648 for_each_online_cpu(cpu) {
4649 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05004650 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04004651 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004652
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004653 do {
4654 ret = alloc_retstack_tasklist(ret_stack_list);
4655 } while (ret == -EAGAIN);
4656
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004657 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04004658 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004659 if (ret)
4660 pr_info("ftrace_graph: Couldn't activate tracepoint"
4661 " probe to kernel_sched_switch\n");
4662 }
4663
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004664 kfree(ret_stack_list);
4665 return ret;
4666}
4667
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004668/*
4669 * Hibernation protection.
4670 * The state of the current task is too much unstable during
4671 * suspend/restore to disk. We want to protect against that.
4672 */
4673static int
4674ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4675 void *unused)
4676{
4677 switch (state) {
4678 case PM_HIBERNATION_PREPARE:
4679 pause_graph_tracing();
4680 break;
4681
4682 case PM_POST_HIBERNATION:
4683 unpause_graph_tracing();
4684 break;
4685 }
4686 return NOTIFY_DONE;
4687}
4688
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004689int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4690 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004691{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004692 int ret = 0;
4693
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004694 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004695
Steven Rostedt05ce5812009-03-24 00:18:31 -04004696 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04004697 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04004698 ret = -EBUSY;
4699 goto out;
4700 }
4701
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004702 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
4703 register_pm_notifier(&ftrace_suspend_notifier);
4704
Steven Rostedt597af812009-04-03 15:24:12 -04004705 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004706 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004707 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04004708 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004709 goto out;
4710 }
Steven Rostedte53a6312008-11-26 00:16:25 -05004711
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004712 ftrace_graph_return = retfunc;
4713 ftrace_graph_entry = entryfunc;
Steven Rostedte53a6312008-11-26 00:16:25 -05004714
Steven Rostedta1cd6172011-05-23 15:24:25 -04004715 ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004716
4717out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004718 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004719 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004720}
4721
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004722void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004723{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004724 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004725
Steven Rostedt597af812009-04-03 15:24:12 -04004726 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004727 goto out;
4728
Steven Rostedt597af812009-04-03 15:24:12 -04004729 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004730 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004731 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedtbd69c302011-05-03 21:55:54 -04004732 ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004733 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04004734 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004735
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004736 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004737 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004738}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004739
Steven Rostedt868baf02011-02-10 21:26:13 -05004740static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
4741
4742static void
4743graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
4744{
4745 atomic_set(&t->tracing_graph_pause, 0);
4746 atomic_set(&t->trace_overrun, 0);
4747 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004748 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05004749 smp_wmb();
4750 t->ret_stack = ret_stack;
4751}
4752
4753/*
4754 * Allocate a return stack for the idle task. May be the first
4755 * time through, or it may be done by CPU hotplug online.
4756 */
4757void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
4758{
4759 t->curr_ret_stack = -1;
4760 /*
4761 * The idle task has no parent, it either has its own
4762 * stack or no stack at all.
4763 */
4764 if (t->ret_stack)
4765 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
4766
4767 if (ftrace_graph_active) {
4768 struct ftrace_ret_stack *ret_stack;
4769
4770 ret_stack = per_cpu(idle_ret_stack, cpu);
4771 if (!ret_stack) {
4772 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4773 * sizeof(struct ftrace_ret_stack),
4774 GFP_KERNEL);
4775 if (!ret_stack)
4776 return;
4777 per_cpu(idle_ret_stack, cpu) = ret_stack;
4778 }
4779 graph_init_task(t, ret_stack);
4780 }
4781}
4782
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004783/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004784void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004785{
Steven Rostedt84047e32009-06-02 16:51:55 -04004786 /* Make sure we do not use the parent ret_stack */
4787 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05004788 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04004789
Steven Rostedt597af812009-04-03 15:24:12 -04004790 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04004791 struct ftrace_ret_stack *ret_stack;
4792
4793 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004794 * sizeof(struct ftrace_ret_stack),
4795 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04004796 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004797 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05004798 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04004799 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004800}
4801
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004802void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004803{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004804 struct ftrace_ret_stack *ret_stack = t->ret_stack;
4805
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004806 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004807 /* NULL must become visible to IRQs before we free it: */
4808 barrier();
4809
4810 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004811}
Steven Rostedt14a866c2008-12-02 23:50:02 -05004812
4813void ftrace_graph_stop(void)
4814{
4815 ftrace_stop();
4816}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004817#endif