blob: d85a0ad81a67ab5e0b20e2e6be941bd804c058bd [file] [log] [blame]
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02001/*
2 * Infrastructure for profiling code inserted by 'gcc -pg'.
3 *
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 * Copyright (C) 2004-2008 Ingo Molnar <mingo@redhat.com>
6 *
7 * Originally ported from the -rt patch by:
8 * Copyright (C) 2007 Arnaldo Carvalho de Melo <acme@redhat.com>
9 *
10 * Based on code in the latency_tracer, that is:
11 *
12 * Copyright (C) 2004-2006 Ingo Molnar
Nadia Yvette Chambers6d49e352012-12-06 10:39:54 +010013 * Copyright (C) 2004 Nadia Yvette Chambers
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020014 */
15
Steven Rostedt3d083392008-05-12 21:20:42 +020016#include <linux/stop_machine.h>
17#include <linux/clocksource.h>
18#include <linux/kallsyms.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020019#include <linux/seq_file.h>
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -080020#include <linux/suspend.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020021#include <linux/debugfs.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020022#include <linux/hardirq.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010023#include <linux/kthread.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020024#include <linux/uaccess.h>
Steven Rostedt5855fea2011-12-16 19:27:42 -050025#include <linux/bsearch.h>
Paul Gortmaker56d82e02011-05-26 17:53:52 -040026#include <linux/module.h>
Ingo Molnar2d8b8202008-02-23 16:55:50 +010027#include <linux/ftrace.h>
Steven Rostedtb0fc4942008-05-12 21:20:43 +020028#include <linux/sysctl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Steven Rostedt5072c592008-05-12 21:20:43 +020030#include <linux/ctype.h>
Steven Rostedt68950612011-12-16 17:06:45 -050031#include <linux/sort.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020032#include <linux/list.h>
Steven Rostedt59df055f2009-02-14 15:29:06 -050033#include <linux/hash.h>
Paul E. McKenney3f379b02010-03-05 15:03:25 -080034#include <linux/rcupdate.h>
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +020035
Steven Rostedtad8d75f2009-04-14 19:39:12 -040036#include <trace/events/sched.h>
Steven Rostedt8aef2d22009-03-24 01:10:15 -040037
Steven Rostedt2af15d62009-05-28 13:37:24 -040038#include <asm/setup.h>
Abhishek Sagar395a59d2008-06-21 23:47:27 +053039
Steven Rostedt0706f1c2009-03-23 23:12:58 -040040#include "trace_output.h"
Steven Rostedtbac429f2009-03-20 12:50:56 -040041#include "trace_stat.h"
Steven Rostedt3d083392008-05-12 21:20:42 +020042
Steven Rostedt69128962008-10-23 09:33:03 -040043#define FTRACE_WARN_ON(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040044 ({ \
45 int ___r = cond; \
46 if (WARN_ON(___r)) \
Steven Rostedt69128962008-10-23 09:33:03 -040047 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040048 ___r; \
49 })
Steven Rostedt69128962008-10-23 09:33:03 -040050
51#define FTRACE_WARN_ON_ONCE(cond) \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040052 ({ \
53 int ___r = cond; \
54 if (WARN_ON_ONCE(___r)) \
Steven Rostedt69128962008-10-23 09:33:03 -040055 ftrace_kill(); \
Steven Rostedt0778d9a2011-04-29 10:36:31 -040056 ___r; \
57 })
Steven Rostedt69128962008-10-23 09:33:03 -040058
Steven Rostedt8fc0c702009-02-16 15:28:00 -050059/* hash bits for specific function selection */
60#define FTRACE_HASH_BITS 7
61#define FTRACE_FUNC_HASHSIZE (1 << FTRACE_HASH_BITS)
Steven Rostedt33dc9b12011-05-02 17:34:47 -040062#define FTRACE_HASH_DEFAULT_BITS 10
63#define FTRACE_HASH_MAX_BITS 12
Steven Rostedt8fc0c702009-02-16 15:28:00 -050064
Jiri Olsae2484912012-02-15 15:51:48 +010065#define FL_GLOBAL_CONTROL_MASK (FTRACE_OPS_FL_GLOBAL | FTRACE_OPS_FL_CONTROL)
66
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040067static struct ftrace_ops ftrace_list_end __read_mostly = {
68 .func = ftrace_stub,
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -040069 .flags = FTRACE_OPS_FL_RECURSION_SAFE | FTRACE_OPS_FL_STUB,
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040070};
71
Steven Rostedt4eebcc82008-05-12 21:20:48 +020072/* ftrace_enabled is a method to turn ftrace on or off */
73int ftrace_enabled __read_mostly;
Steven Rostedtd61f82d2008-05-12 21:20:43 +020074static int last_ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +020075
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050076/* Quick disabling of function tracer. */
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -040077int function_trace_stop __read_mostly;
78
79/* Current function tracing op */
80struct ftrace_ops *function_trace_op __read_mostly = &ftrace_list_end;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -050081
jolsa@redhat.com756d17e2009-10-13 16:33:52 -040082/* List for set_ftrace_pid's pids. */
83LIST_HEAD(ftrace_pids);
84struct ftrace_pid {
85 struct list_head list;
86 struct pid *pid;
87};
88
Steven Rostedt4eebcc82008-05-12 21:20:48 +020089/*
90 * ftrace_disabled is set when an anomaly is discovered.
91 * ftrace_disabled is much stronger than ftrace_enabled.
92 */
93static int ftrace_disabled __read_mostly;
94
Steven Rostedt52baf112009-02-14 01:15:39 -050095static DEFINE_MUTEX(ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +020096
Steven Rostedtb8489142011-05-04 09:27:52 -040097static struct ftrace_ops *ftrace_global_list __read_mostly = &ftrace_list_end;
Jiri Olsae2484912012-02-15 15:51:48 +010098static struct ftrace_ops *ftrace_control_list __read_mostly = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -040099static struct ftrace_ops *ftrace_ops_list __read_mostly = &ftrace_list_end;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200100ftrace_func_t ftrace_trace_function __read_mostly = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500101ftrace_func_t ftrace_pid_function __read_mostly = ftrace_stub;
Steven Rostedt2b499382011-05-03 22:49:52 -0400102static struct ftrace_ops global_ops;
Jiri Olsae2484912012-02-15 15:51:48 +0100103static struct ftrace_ops control_ops;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200104
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400105#if ARCH_SUPPORTS_FTRACE_OPS
106static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400107 struct ftrace_ops *op, struct pt_regs *regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400108#else
109/* See comment below, where ftrace_ops_list_func is defined */
110static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip);
111#define ftrace_ops_list_func ((ftrace_func_t)ftrace_ops_no_ops)
112#endif
Steven Rostedtb8489142011-05-04 09:27:52 -0400113
Steven Rostedt0a016402012-11-02 17:03:03 -0400114/*
115 * Traverse the ftrace_global_list, invoking all entries. The reason that we
116 * can use rcu_dereference_raw() is that elements removed from this list
117 * are simply leaked, so there is no need to interact with a grace-period
118 * mechanism. The rcu_dereference_raw() calls are needed to handle
119 * concurrent insertions into the ftrace_global_list.
120 *
121 * Silly Alpha and silly pointer-speculation compiler optimizations!
122 */
123#define do_for_each_ftrace_op(op, list) \
124 op = rcu_dereference_raw(list); \
125 do
126
127/*
128 * Optimized for just a single item in the list (as that is the normal case).
129 */
130#define while_for_each_ftrace_op(op) \
131 while (likely(op = rcu_dereference_raw((op)->next)) && \
132 unlikely((op) != &ftrace_list_end))
133
Steven Rostedtea701f12012-07-20 13:08:05 -0400134/**
135 * ftrace_nr_registered_ops - return number of ops registered
136 *
137 * Returns the number of ftrace_ops registered and tracing functions
138 */
139int ftrace_nr_registered_ops(void)
140{
141 struct ftrace_ops *ops;
142 int cnt = 0;
143
144 mutex_lock(&ftrace_lock);
145
146 for (ops = ftrace_ops_list;
147 ops != &ftrace_list_end; ops = ops->next)
148 cnt++;
149
150 mutex_unlock(&ftrace_lock);
151
152 return cnt;
153}
154
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400155static void
156ftrace_global_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400157 struct ftrace_ops *op, struct pt_regs *regs)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200158{
Steven Rostedtc29f1222012-11-02 17:17:59 -0400159 int bit;
160
Steven Rostedtedc15ca2012-11-02 17:47:21 -0400161 bit = trace_test_and_set_recursion(TRACE_GLOBAL_START, TRACE_GLOBAL_MAX);
162 if (bit < 0)
Steven Rostedtb1cff0a2011-05-25 14:27:43 -0400163 return;
164
Steven Rostedt0a016402012-11-02 17:03:03 -0400165 do_for_each_ftrace_op(op, ftrace_global_list) {
Steven Rostedta1e2e312011-08-09 12:50:46 -0400166 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -0400167 } while_for_each_ftrace_op(op);
Steven Rostedtedc15ca2012-11-02 17:47:21 -0400168
169 trace_clear_recursion(bit);
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200170}
171
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400172static void ftrace_pid_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400173 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500174{
Steven Rostedt0ef8cde2008-12-03 15:36:58 -0500175 if (!test_tsk_trace_trace(current))
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500176 return;
177
Steven Rostedta1e2e312011-08-09 12:50:46 -0400178 ftrace_pid_function(ip, parent_ip, op, regs);
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500179}
180
181static void set_ftrace_pid_function(ftrace_func_t func)
182{
183 /* do not set ftrace_pid_function to itself! */
184 if (func != ftrace_pid_func)
185 ftrace_pid_function = func;
186}
187
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200188/**
Steven Rostedt3d083392008-05-12 21:20:42 +0200189 * clear_ftrace_function - reset the ftrace function
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200190 *
Steven Rostedt3d083392008-05-12 21:20:42 +0200191 * This NULLs the ftrace function and in essence stops
192 * tracing. There may be lag
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200193 */
Steven Rostedt3d083392008-05-12 21:20:42 +0200194void clear_ftrace_function(void)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200195{
Steven Rostedt3d083392008-05-12 21:20:42 +0200196 ftrace_trace_function = ftrace_stub;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500197 ftrace_pid_function = ftrace_stub;
Steven Rostedt3d083392008-05-12 21:20:42 +0200198}
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200199
Jiri Olsae2484912012-02-15 15:51:48 +0100200static void control_ops_disable_all(struct ftrace_ops *ops)
201{
202 int cpu;
203
204 for_each_possible_cpu(cpu)
205 *per_cpu_ptr(ops->disabled, cpu) = 1;
206}
207
208static int control_ops_alloc(struct ftrace_ops *ops)
209{
210 int __percpu *disabled;
211
212 disabled = alloc_percpu(int);
213 if (!disabled)
214 return -ENOMEM;
215
216 ops->disabled = disabled;
217 control_ops_disable_all(ops);
218 return 0;
219}
220
221static void control_ops_free(struct ftrace_ops *ops)
222{
223 free_percpu(ops->disabled);
224}
225
Steven Rostedt2b499382011-05-03 22:49:52 -0400226static void update_global_ops(void)
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400227{
228 ftrace_func_t func;
229
230 /*
231 * If there's only one function registered, then call that
232 * function directly. Otherwise, we need to iterate over the
233 * registered callers.
234 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400235 if (ftrace_global_list == &ftrace_list_end ||
Steven Rostedt63503792012-11-02 16:58:56 -0400236 ftrace_global_list->next == &ftrace_list_end) {
Steven Rostedtb8489142011-05-04 09:27:52 -0400237 func = ftrace_global_list->func;
Steven Rostedt63503792012-11-02 16:58:56 -0400238 /*
239 * As we are calling the function directly.
240 * If it does not have recursion protection,
241 * the function_trace_op needs to be updated
242 * accordingly.
243 */
244 if (ftrace_global_list->flags & FTRACE_OPS_FL_RECURSION_SAFE)
245 global_ops.flags |= FTRACE_OPS_FL_RECURSION_SAFE;
246 else
247 global_ops.flags &= ~FTRACE_OPS_FL_RECURSION_SAFE;
248 } else {
Steven Rostedtb8489142011-05-04 09:27:52 -0400249 func = ftrace_global_list_func;
Steven Rostedt63503792012-11-02 16:58:56 -0400250 /* The list has its own recursion protection. */
251 global_ops.flags |= FTRACE_OPS_FL_RECURSION_SAFE;
252 }
253
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400254
255 /* If we filter on pids, update to use the pid function */
256 if (!list_empty(&ftrace_pids)) {
257 set_ftrace_pid_function(func);
258 func = ftrace_pid_func;
259 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400260
261 global_ops.func = func;
262}
263
264static void update_ftrace_function(void)
265{
266 ftrace_func_t func;
267
268 update_global_ops();
269
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400270 /*
271 * If we are at the end of the list and this ops is
Steven Rostedt47409742012-07-20 11:04:44 -0400272 * recursion safe and not dynamic and the arch supports passing ops,
273 * then have the mcount trampoline call the function directly.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400274 */
Steven Rostedtb8489142011-05-04 09:27:52 -0400275 if (ftrace_ops_list == &ftrace_list_end ||
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400276 (ftrace_ops_list->next == &ftrace_list_end &&
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400277 !(ftrace_ops_list->flags & FTRACE_OPS_FL_DYNAMIC) &&
Steven Rostedt47409742012-07-20 11:04:44 -0400278 (ftrace_ops_list->flags & FTRACE_OPS_FL_RECURSION_SAFE) &&
Steven Rostedtccf36722012-06-05 09:44:25 -0400279 !FTRACE_FORCE_LIST_FUNC)) {
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400280 /* Set the ftrace_ops that the arch callback uses */
281 if (ftrace_ops_list == &global_ops)
282 function_trace_op = ftrace_global_list;
283 else
284 function_trace_op = ftrace_ops_list;
Steven Rostedtb8489142011-05-04 09:27:52 -0400285 func = ftrace_ops_list->func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400286 } else {
287 /* Just use the default ftrace_ops */
288 function_trace_op = &ftrace_list_end;
Steven Rostedtb8489142011-05-04 09:27:52 -0400289 func = ftrace_ops_list_func;
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400290 }
Steven Rostedt2b499382011-05-03 22:49:52 -0400291
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400292 ftrace_trace_function = func;
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400293}
294
Steven Rostedt2b499382011-05-03 22:49:52 -0400295static void add_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
Steven Rostedt3d083392008-05-12 21:20:42 +0200296{
Steven Rostedt2b499382011-05-03 22:49:52 -0400297 ops->next = *list;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200298 /*
Steven Rostedtb8489142011-05-04 09:27:52 -0400299 * We are entering ops into the list but another
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200300 * CPU might be walking that list. We need to make sure
301 * the ops->next pointer is valid before another CPU sees
Steven Rostedtb8489142011-05-04 09:27:52 -0400302 * the ops pointer included into the list.
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200303 */
Steven Rostedt2b499382011-05-03 22:49:52 -0400304 rcu_assign_pointer(*list, ops);
305}
Steven Rostedt3d083392008-05-12 21:20:42 +0200306
Steven Rostedt2b499382011-05-03 22:49:52 -0400307static int remove_ftrace_ops(struct ftrace_ops **list, struct ftrace_ops *ops)
308{
309 struct ftrace_ops **p;
310
311 /*
312 * If we are removing the last function, then simply point
313 * to the ftrace_stub.
314 */
315 if (*list == ops && ops->next == &ftrace_list_end) {
316 *list = &ftrace_list_end;
317 return 0;
318 }
319
320 for (p = list; *p != &ftrace_list_end; p = &(*p)->next)
321 if (*p == ops)
322 break;
323
324 if (*p != ops)
325 return -1;
326
327 *p = (*p)->next;
328 return 0;
329}
330
Jiri Olsae2484912012-02-15 15:51:48 +0100331static void add_ftrace_list_ops(struct ftrace_ops **list,
332 struct ftrace_ops *main_ops,
333 struct ftrace_ops *ops)
334{
335 int first = *list == &ftrace_list_end;
336 add_ftrace_ops(list, ops);
337 if (first)
338 add_ftrace_ops(&ftrace_ops_list, main_ops);
339}
340
341static int remove_ftrace_list_ops(struct ftrace_ops **list,
342 struct ftrace_ops *main_ops,
343 struct ftrace_ops *ops)
344{
345 int ret = remove_ftrace_ops(list, ops);
346 if (!ret && *list == &ftrace_list_end)
347 ret = remove_ftrace_ops(&ftrace_ops_list, main_ops);
348 return ret;
349}
350
Steven Rostedt2b499382011-05-03 22:49:52 -0400351static int __register_ftrace_function(struct ftrace_ops *ops)
352{
Borislav Petkov8d240dd2012-03-29 19:11:40 +0200353 if (unlikely(ftrace_disabled))
Steven Rostedt2b499382011-05-03 22:49:52 -0400354 return -ENODEV;
355
356 if (FTRACE_WARN_ON(ops == &global_ops))
357 return -EINVAL;
358
Steven Rostedtb8489142011-05-04 09:27:52 -0400359 if (WARN_ON(ops->flags & FTRACE_OPS_FL_ENABLED))
360 return -EBUSY;
361
Jiri Olsae2484912012-02-15 15:51:48 +0100362 /* We don't support both control and global flags set. */
363 if ((ops->flags & FL_GLOBAL_CONTROL_MASK) == FL_GLOBAL_CONTROL_MASK)
364 return -EINVAL;
365
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +0900366#ifndef CONFIG_DYNAMIC_FTRACE_WITH_REGS
Steven Rostedt08f6fba2012-04-30 16:20:23 -0400367 /*
368 * If the ftrace_ops specifies SAVE_REGS, then it only can be used
369 * if the arch supports it, or SAVE_REGS_IF_SUPPORTED is also set.
370 * Setting SAVE_REGS_IF_SUPPORTED makes SAVE_REGS irrelevant.
371 */
372 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS &&
373 !(ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED))
374 return -EINVAL;
375
376 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED)
377 ops->flags |= FTRACE_OPS_FL_SAVE_REGS;
378#endif
379
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400380 if (!core_kernel_data((unsigned long)ops))
381 ops->flags |= FTRACE_OPS_FL_DYNAMIC;
382
Steven Rostedtb8489142011-05-04 09:27:52 -0400383 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100384 add_ftrace_list_ops(&ftrace_global_list, &global_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400385 ops->flags |= FTRACE_OPS_FL_ENABLED;
Jiri Olsae2484912012-02-15 15:51:48 +0100386 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
387 if (control_ops_alloc(ops))
388 return -ENOMEM;
389 add_ftrace_list_ops(&ftrace_control_list, &control_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400390 } else
391 add_ftrace_ops(&ftrace_ops_list, ops);
392
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400393 if (ftrace_enabled)
394 update_ftrace_function();
Steven Rostedt3d083392008-05-12 21:20:42 +0200395
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200396 return 0;
397}
398
Ingo Molnare309b412008-05-12 21:20:51 +0200399static int __unregister_ftrace_function(struct ftrace_ops *ops)
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200400{
Steven Rostedt2b499382011-05-03 22:49:52 -0400401 int ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200402
Steven Rostedt2b499382011-05-03 22:49:52 -0400403 if (ftrace_disabled)
404 return -ENODEV;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200405
Steven Rostedtb8489142011-05-04 09:27:52 -0400406 if (WARN_ON(!(ops->flags & FTRACE_OPS_FL_ENABLED)))
407 return -EBUSY;
408
Steven Rostedt2b499382011-05-03 22:49:52 -0400409 if (FTRACE_WARN_ON(ops == &global_ops))
410 return -EINVAL;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200411
Steven Rostedtb8489142011-05-04 09:27:52 -0400412 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
Jiri Olsae2484912012-02-15 15:51:48 +0100413 ret = remove_ftrace_list_ops(&ftrace_global_list,
414 &global_ops, ops);
Steven Rostedtb8489142011-05-04 09:27:52 -0400415 if (!ret)
416 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Jiri Olsae2484912012-02-15 15:51:48 +0100417 } else if (ops->flags & FTRACE_OPS_FL_CONTROL) {
418 ret = remove_ftrace_list_ops(&ftrace_control_list,
419 &control_ops, ops);
420 if (!ret) {
421 /*
422 * The ftrace_ops is now removed from the list,
423 * so there'll be no new users. We must ensure
424 * all current users are done before we free
425 * the control data.
426 */
427 synchronize_sched();
428 control_ops_free(ops);
429 }
Steven Rostedtb8489142011-05-04 09:27:52 -0400430 } else
431 ret = remove_ftrace_ops(&ftrace_ops_list, ops);
432
Steven Rostedt2b499382011-05-03 22:49:52 -0400433 if (ret < 0)
434 return ret;
Steven Rostedtb8489142011-05-04 09:27:52 -0400435
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400436 if (ftrace_enabled)
437 update_ftrace_function();
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +0200438
Steven Rostedtcdbe61b2011-05-05 21:14:55 -0400439 /*
440 * Dynamic ops may be freed, we must make sure that all
441 * callers are done before leaving this function.
442 */
443 if (ops->flags & FTRACE_OPS_FL_DYNAMIC)
444 synchronize_sched();
445
Steven Rostedte6ea44e2009-02-14 01:42:44 -0500446 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200447}
448
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500449static void ftrace_update_pid_func(void)
450{
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400451 /* Only do something if we are tracing something */
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500452 if (ftrace_trace_function == ftrace_stub)
KOSAKI Motohiro10dd3eb2009-03-06 15:29:04 +0900453 return;
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500454
Steven Rostedt491d0dc2011-04-27 21:43:36 -0400455 update_ftrace_function();
Steven Rostedtdf4fc312008-11-26 00:16:23 -0500456}
457
Steven Rostedt493762f2009-03-23 17:12:36 -0400458#ifdef CONFIG_FUNCTION_PROFILER
459struct ftrace_profile {
460 struct hlist_node node;
461 unsigned long ip;
462 unsigned long counter;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400463#ifdef CONFIG_FUNCTION_GRAPH_TRACER
464 unsigned long long time;
Chase Douglase330b3b2010-04-26 14:02:05 -0400465 unsigned long long time_squared;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400466#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400467};
468
469struct ftrace_profile_page {
470 struct ftrace_profile_page *next;
471 unsigned long index;
472 struct ftrace_profile records[];
473};
474
Steven Rostedtcafb1682009-03-24 20:50:39 -0400475struct ftrace_profile_stat {
476 atomic_t disabled;
477 struct hlist_head *hash;
478 struct ftrace_profile_page *pages;
479 struct ftrace_profile_page *start;
480 struct tracer_stat stat;
481};
482
Steven Rostedt493762f2009-03-23 17:12:36 -0400483#define PROFILE_RECORDS_SIZE \
484 (PAGE_SIZE - offsetof(struct ftrace_profile_page, records))
485
486#define PROFILES_PER_PAGE \
487 (PROFILE_RECORDS_SIZE / sizeof(struct ftrace_profile))
488
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400489static int ftrace_profile_enabled __read_mostly;
490
491/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400492static DEFINE_MUTEX(ftrace_profile_lock);
493
Steven Rostedtcafb1682009-03-24 20:50:39 -0400494static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400495
Namhyung Kim20079eb2013-04-10 08:55:50 +0900496#define FTRACE_PROFILE_HASH_BITS 10
497#define FTRACE_PROFILE_HASH_SIZE (1 << FTRACE_PROFILE_HASH_BITS)
Steven Rostedt493762f2009-03-23 17:12:36 -0400498
Steven Rostedt493762f2009-03-23 17:12:36 -0400499static void *
500function_stat_next(void *v, int idx)
501{
502 struct ftrace_profile *rec = v;
503 struct ftrace_profile_page *pg;
504
505 pg = (struct ftrace_profile_page *)((unsigned long)rec & PAGE_MASK);
506
507 again:
Li Zefan0296e422009-06-26 11:15:37 +0800508 if (idx != 0)
509 rec++;
510
Steven Rostedt493762f2009-03-23 17:12:36 -0400511 if ((void *)rec >= (void *)&pg->records[pg->index]) {
512 pg = pg->next;
513 if (!pg)
514 return NULL;
515 rec = &pg->records[0];
516 if (!rec->counter)
517 goto again;
518 }
519
520 return rec;
521}
522
523static void *function_stat_start(struct tracer_stat *trace)
524{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400525 struct ftrace_profile_stat *stat =
526 container_of(trace, struct ftrace_profile_stat, stat);
527
528 if (!stat || !stat->start)
529 return NULL;
530
531 return function_stat_next(&stat->start->records[0], 0);
Steven Rostedt493762f2009-03-23 17:12:36 -0400532}
533
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400534#ifdef CONFIG_FUNCTION_GRAPH_TRACER
535/* function graph compares on total time */
536static int function_stat_cmp(void *p1, void *p2)
537{
538 struct ftrace_profile *a = p1;
539 struct ftrace_profile *b = p2;
540
541 if (a->time < b->time)
542 return -1;
543 if (a->time > b->time)
544 return 1;
545 else
546 return 0;
547}
548#else
549/* not function graph compares against hits */
Steven Rostedt493762f2009-03-23 17:12:36 -0400550static int function_stat_cmp(void *p1, void *p2)
551{
552 struct ftrace_profile *a = p1;
553 struct ftrace_profile *b = p2;
554
555 if (a->counter < b->counter)
556 return -1;
557 if (a->counter > b->counter)
558 return 1;
559 else
560 return 0;
561}
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400562#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400563
564static int function_stat_headers(struct seq_file *m)
565{
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400566#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400567 seq_printf(m, " Function "
Chase Douglase330b3b2010-04-26 14:02:05 -0400568 "Hit Time Avg s^2\n"
Steven Rostedt34886c82009-03-25 21:00:47 -0400569 " -------- "
Chase Douglase330b3b2010-04-26 14:02:05 -0400570 "--- ---- --- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400571#else
Steven Rostedt493762f2009-03-23 17:12:36 -0400572 seq_printf(m, " Function Hit\n"
573 " -------- ---\n");
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400574#endif
Steven Rostedt493762f2009-03-23 17:12:36 -0400575 return 0;
576}
577
578static int function_stat_show(struct seq_file *m, void *v)
579{
580 struct ftrace_profile *rec = v;
581 char str[KSYM_SYMBOL_LEN];
Li Zefan3aaba202010-08-23 16:50:12 +0800582 int ret = 0;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400583#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Steven Rostedt34886c82009-03-25 21:00:47 -0400584 static struct trace_seq s;
585 unsigned long long avg;
Chase Douglase330b3b2010-04-26 14:02:05 -0400586 unsigned long long stddev;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400587#endif
Li Zefan3aaba202010-08-23 16:50:12 +0800588 mutex_lock(&ftrace_profile_lock);
589
590 /* we raced with function_profile_reset() */
591 if (unlikely(rec->counter == 0)) {
592 ret = -EBUSY;
593 goto out;
594 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400595
596 kallsyms_lookup(rec->ip, NULL, NULL, NULL, str);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400597 seq_printf(m, " %-30.30s %10lu", str, rec->counter);
Steven Rostedt493762f2009-03-23 17:12:36 -0400598
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400599#ifdef CONFIG_FUNCTION_GRAPH_TRACER
600 seq_printf(m, " ");
Steven Rostedt34886c82009-03-25 21:00:47 -0400601 avg = rec->time;
602 do_div(avg, rec->counter);
603
Chase Douglase330b3b2010-04-26 14:02:05 -0400604 /* Sample standard deviation (s^2) */
605 if (rec->counter <= 1)
606 stddev = 0;
607 else {
608 stddev = rec->time_squared - rec->counter * avg * avg;
609 /*
610 * Divide only 1000 for ns^2 -> us^2 conversion.
611 * trace_print_graph_duration will divide 1000 again.
612 */
613 do_div(stddev, (rec->counter - 1) * 1000);
614 }
615
Steven Rostedt34886c82009-03-25 21:00:47 -0400616 trace_seq_init(&s);
617 trace_print_graph_duration(rec->time, &s);
618 trace_seq_puts(&s, " ");
619 trace_print_graph_duration(avg, &s);
Chase Douglase330b3b2010-04-26 14:02:05 -0400620 trace_seq_puts(&s, " ");
621 trace_print_graph_duration(stddev, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400622 trace_print_seq(m, &s);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400623#endif
624 seq_putc(m, '\n');
Li Zefan3aaba202010-08-23 16:50:12 +0800625out:
626 mutex_unlock(&ftrace_profile_lock);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400627
Li Zefan3aaba202010-08-23 16:50:12 +0800628 return ret;
Steven Rostedt493762f2009-03-23 17:12:36 -0400629}
630
Steven Rostedtcafb1682009-03-24 20:50:39 -0400631static void ftrace_profile_reset(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400632{
633 struct ftrace_profile_page *pg;
634
Steven Rostedtcafb1682009-03-24 20:50:39 -0400635 pg = stat->pages = stat->start;
Steven Rostedt493762f2009-03-23 17:12:36 -0400636
637 while (pg) {
638 memset(pg->records, 0, PROFILE_RECORDS_SIZE);
639 pg->index = 0;
640 pg = pg->next;
641 }
642
Steven Rostedtcafb1682009-03-24 20:50:39 -0400643 memset(stat->hash, 0,
Steven Rostedt493762f2009-03-23 17:12:36 -0400644 FTRACE_PROFILE_HASH_SIZE * sizeof(struct hlist_head));
645}
646
Steven Rostedtcafb1682009-03-24 20:50:39 -0400647int ftrace_profile_pages_init(struct ftrace_profile_stat *stat)
Steven Rostedt493762f2009-03-23 17:12:36 -0400648{
649 struct ftrace_profile_page *pg;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400650 int functions;
651 int pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400652 int i;
653
654 /* If we already allocated, do nothing */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400655 if (stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400656 return 0;
657
Steven Rostedtcafb1682009-03-24 20:50:39 -0400658 stat->pages = (void *)get_zeroed_page(GFP_KERNEL);
659 if (!stat->pages)
Steven Rostedt493762f2009-03-23 17:12:36 -0400660 return -ENOMEM;
661
Steven Rostedt318e0a72009-03-25 20:06:34 -0400662#ifdef CONFIG_DYNAMIC_FTRACE
663 functions = ftrace_update_tot_cnt;
664#else
665 /*
666 * We do not know the number of functions that exist because
667 * dynamic tracing is what counts them. With past experience
668 * we have around 20K functions. That should be more than enough.
669 * It is highly unlikely we will execute every function in
670 * the kernel.
671 */
672 functions = 20000;
673#endif
674
Steven Rostedtcafb1682009-03-24 20:50:39 -0400675 pg = stat->start = stat->pages;
Steven Rostedt493762f2009-03-23 17:12:36 -0400676
Steven Rostedt318e0a72009-03-25 20:06:34 -0400677 pages = DIV_ROUND_UP(functions, PROFILES_PER_PAGE);
678
Namhyung Kim39e30cd2013-04-01 21:46:24 +0900679 for (i = 1; i < pages; i++) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400680 pg->next = (void *)get_zeroed_page(GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400681 if (!pg->next)
Steven Rostedt318e0a72009-03-25 20:06:34 -0400682 goto out_free;
Steven Rostedt493762f2009-03-23 17:12:36 -0400683 pg = pg->next;
684 }
685
686 return 0;
Steven Rostedt318e0a72009-03-25 20:06:34 -0400687
688 out_free:
689 pg = stat->start;
690 while (pg) {
691 unsigned long tmp = (unsigned long)pg;
692
693 pg = pg->next;
694 free_page(tmp);
695 }
696
Steven Rostedt318e0a72009-03-25 20:06:34 -0400697 stat->pages = NULL;
698 stat->start = NULL;
699
700 return -ENOMEM;
Steven Rostedt493762f2009-03-23 17:12:36 -0400701}
702
Steven Rostedtcafb1682009-03-24 20:50:39 -0400703static int ftrace_profile_init_cpu(int cpu)
Steven Rostedt493762f2009-03-23 17:12:36 -0400704{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400705 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400706 int size;
707
Steven Rostedtcafb1682009-03-24 20:50:39 -0400708 stat = &per_cpu(ftrace_profile_stats, cpu);
709
710 if (stat->hash) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400711 /* If the profile is already created, simply reset it */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400712 ftrace_profile_reset(stat);
Steven Rostedt493762f2009-03-23 17:12:36 -0400713 return 0;
714 }
715
716 /*
717 * We are profiling all functions, but usually only a few thousand
718 * functions are hit. We'll make a hash of 1024 items.
719 */
720 size = FTRACE_PROFILE_HASH_SIZE;
721
Steven Rostedtcafb1682009-03-24 20:50:39 -0400722 stat->hash = kzalloc(sizeof(struct hlist_head) * size, GFP_KERNEL);
Steven Rostedt493762f2009-03-23 17:12:36 -0400723
Steven Rostedtcafb1682009-03-24 20:50:39 -0400724 if (!stat->hash)
Steven Rostedt493762f2009-03-23 17:12:36 -0400725 return -ENOMEM;
726
Steven Rostedt318e0a72009-03-25 20:06:34 -0400727 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400728 if (ftrace_profile_pages_init(stat) < 0) {
729 kfree(stat->hash);
730 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400731 return -ENOMEM;
732 }
733
734 return 0;
735}
736
Steven Rostedtcafb1682009-03-24 20:50:39 -0400737static int ftrace_profile_init(void)
738{
739 int cpu;
740 int ret = 0;
741
742 for_each_online_cpu(cpu) {
743 ret = ftrace_profile_init_cpu(cpu);
744 if (ret)
745 break;
746 }
747
748 return ret;
749}
750
Steven Rostedt493762f2009-03-23 17:12:36 -0400751/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400752static struct ftrace_profile *
753ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400754{
755 struct ftrace_profile *rec;
756 struct hlist_head *hhd;
Steven Rostedt493762f2009-03-23 17:12:36 -0400757 unsigned long key;
758
Namhyung Kim20079eb2013-04-10 08:55:50 +0900759 key = hash_long(ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400760 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400761
762 if (hlist_empty(hhd))
763 return NULL;
764
Sasha Levinb67bfe02013-02-27 17:06:00 -0800765 hlist_for_each_entry_rcu(rec, hhd, node) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400766 if (rec->ip == ip)
767 return rec;
768 }
769
770 return NULL;
771}
772
Steven Rostedtcafb1682009-03-24 20:50:39 -0400773static void ftrace_add_profile(struct ftrace_profile_stat *stat,
774 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400775{
776 unsigned long key;
777
Namhyung Kim20079eb2013-04-10 08:55:50 +0900778 key = hash_long(rec->ip, FTRACE_PROFILE_HASH_BITS);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400779 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400780}
781
Steven Rostedt318e0a72009-03-25 20:06:34 -0400782/*
783 * The memory is already allocated, this simply finds a new record to use.
784 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400785static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400786ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400787{
788 struct ftrace_profile *rec = NULL;
789
Steven Rostedt318e0a72009-03-25 20:06:34 -0400790 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400791 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400792 goto out;
793
Steven Rostedt493762f2009-03-23 17:12:36 -0400794 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400795 * Try to find the function again since an NMI
796 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400797 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400798 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400799 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400800 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400801
Steven Rostedtcafb1682009-03-24 20:50:39 -0400802 if (stat->pages->index == PROFILES_PER_PAGE) {
803 if (!stat->pages->next)
804 goto out;
805 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400806 }
807
Steven Rostedtcafb1682009-03-24 20:50:39 -0400808 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400809 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400810 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400811
Steven Rostedt493762f2009-03-23 17:12:36 -0400812 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400813 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400814
815 return rec;
816}
817
Steven Rostedt493762f2009-03-23 17:12:36 -0400818static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400819function_profile_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400820 struct ftrace_ops *ops, struct pt_regs *regs)
Steven Rostedt493762f2009-03-23 17:12:36 -0400821{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400822 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400823 struct ftrace_profile *rec;
824 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400825
826 if (!ftrace_profile_enabled)
827 return;
828
Steven Rostedt493762f2009-03-23 17:12:36 -0400829 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400830
831 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400832 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400833 goto out;
834
835 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400836 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400837 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400838 if (!rec)
839 goto out;
840 }
841
842 rec->counter++;
843 out:
844 local_irq_restore(flags);
845}
846
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400847#ifdef CONFIG_FUNCTION_GRAPH_TRACER
848static int profile_graph_entry(struct ftrace_graph_ent *trace)
849{
Steven Rostedta1e2e312011-08-09 12:50:46 -0400850 function_profile_call(trace->func, 0, NULL, NULL);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400851 return 1;
852}
853
854static void profile_graph_return(struct ftrace_graph_ret *trace)
855{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400856 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400857 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400858 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400859 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400860
861 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400862 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400863 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400864 goto out;
865
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400866 /* If the calltime was zero'd ignore it */
867 if (!trace->calltime)
868 goto out;
869
Steven Rostedta2a16d62009-03-24 23:17:58 -0400870 calltime = trace->rettime - trace->calltime;
871
872 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
873 int index;
874
875 index = trace->depth;
876
877 /* Append this call time to the parent time to subtract */
878 if (index)
879 current->ret_stack[index - 1].subtime += calltime;
880
881 if (current->ret_stack[index].subtime < calltime)
882 calltime -= current->ret_stack[index].subtime;
883 else
884 calltime = 0;
885 }
886
Steven Rostedtcafb1682009-03-24 20:50:39 -0400887 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400888 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400889 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400890 rec->time_squared += calltime * calltime;
891 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400892
Steven Rostedtcafb1682009-03-24 20:50:39 -0400893 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400894 local_irq_restore(flags);
895}
896
897static int register_ftrace_profiler(void)
898{
899 return register_ftrace_graph(&profile_graph_return,
900 &profile_graph_entry);
901}
902
903static void unregister_ftrace_profiler(void)
904{
905 unregister_ftrace_graph();
906}
907#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100908static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400909 .func = function_profile_call,
Steven Rostedt47409742012-07-20 11:04:44 -0400910 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Steven Rostedt493762f2009-03-23 17:12:36 -0400911};
912
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400913static int register_ftrace_profiler(void)
914{
915 return register_ftrace_function(&ftrace_profile_ops);
916}
917
918static void unregister_ftrace_profiler(void)
919{
920 unregister_ftrace_function(&ftrace_profile_ops);
921}
922#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
923
Steven Rostedt493762f2009-03-23 17:12:36 -0400924static ssize_t
925ftrace_profile_write(struct file *filp, const char __user *ubuf,
926 size_t cnt, loff_t *ppos)
927{
928 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400929 int ret;
930
Peter Huewe22fe9b52011-06-07 21:58:27 +0200931 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
932 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400933 return ret;
934
935 val = !!val;
936
937 mutex_lock(&ftrace_profile_lock);
938 if (ftrace_profile_enabled ^ val) {
939 if (val) {
940 ret = ftrace_profile_init();
941 if (ret < 0) {
942 cnt = ret;
943 goto out;
944 }
945
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400946 ret = register_ftrace_profiler();
947 if (ret < 0) {
948 cnt = ret;
949 goto out;
950 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400951 ftrace_profile_enabled = 1;
952 } else {
953 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400954 /*
955 * unregister_ftrace_profiler calls stop_machine
956 * so this acts like an synchronize_sched.
957 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400958 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400959 }
960 }
961 out:
962 mutex_unlock(&ftrace_profile_lock);
963
Jiri Olsacf8517c2009-10-23 19:36:16 -0400964 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400965
966 return cnt;
967}
968
969static ssize_t
970ftrace_profile_read(struct file *filp, char __user *ubuf,
971 size_t cnt, loff_t *ppos)
972{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400973 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400974 int r;
975
976 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
977 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
978}
979
980static const struct file_operations ftrace_profile_fops = {
981 .open = tracing_open_generic,
982 .read = ftrace_profile_read,
983 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200984 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -0400985};
986
Steven Rostedtcafb1682009-03-24 20:50:39 -0400987/* used to initialize the real stat files */
988static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400989 .name = "functions",
990 .stat_start = function_stat_start,
991 .stat_next = function_stat_next,
992 .stat_cmp = function_stat_cmp,
993 .stat_headers = function_stat_headers,
994 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -0400995};
996
Steven Rostedt6ab5d662009-06-04 00:55:45 -0400997static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -0400998{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400999 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -04001000 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001001 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -04001002 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001003 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -04001004
Steven Rostedtcafb1682009-03-24 20:50:39 -04001005 for_each_possible_cpu(cpu) {
1006 stat = &per_cpu(ftrace_profile_stats, cpu);
1007
1008 /* allocate enough for function name + cpu number */
1009 name = kmalloc(32, GFP_KERNEL);
1010 if (!name) {
1011 /*
1012 * The files created are permanent, if something happens
1013 * we still do not free memory.
1014 */
Steven Rostedtcafb1682009-03-24 20:50:39 -04001015 WARN(1,
1016 "Could not allocate stat file for cpu %d\n",
1017 cpu);
1018 return;
1019 }
1020 stat->stat = function_stats;
1021 snprintf(name, 32, "function%d", cpu);
1022 stat->stat.name = name;
1023 ret = register_stat_tracer(&stat->stat);
1024 if (ret) {
1025 WARN(1,
1026 "Could not register function stat for cpu %d\n",
1027 cpu);
1028 kfree(name);
1029 return;
1030 }
Steven Rostedt493762f2009-03-23 17:12:36 -04001031 }
1032
1033 entry = debugfs_create_file("function_profile_enabled", 0644,
1034 d_tracer, NULL, &ftrace_profile_fops);
1035 if (!entry)
1036 pr_warning("Could not create debugfs "
1037 "'function_profile_enabled' entry\n");
1038}
1039
1040#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001041static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001042{
1043}
1044#endif /* CONFIG_FUNCTION_PROFILER */
1045
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001046static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1047
Steven Rostedt (Red Hat)7f49ef62013-04-12 16:40:13 -04001048loff_t
1049ftrace_filter_lseek(struct file *file, loff_t offset, int whence)
1050{
1051 loff_t ret;
1052
1053 if (file->f_mode & FMODE_READ)
1054 ret = seq_lseek(file, offset, whence);
1055 else
1056 file->f_pos = ret = 1;
1057
1058 return ret;
1059}
1060
Steven Rostedt3d083392008-05-12 21:20:42 +02001061#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001062
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001063#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001064# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001065#endif
1066
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001067static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1068
Steven Rostedtb6887d72009-02-17 12:32:04 -05001069struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001070 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -05001071 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001072 unsigned long flags;
1073 unsigned long ip;
1074 void *data;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04001075 struct list_head free_list;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001076};
1077
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001078struct ftrace_func_entry {
1079 struct hlist_node hlist;
1080 unsigned long ip;
1081};
1082
1083struct ftrace_hash {
1084 unsigned long size_bits;
1085 struct hlist_head *buckets;
1086 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001087 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001088};
1089
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001090/*
1091 * We make these constant because no one should touch them,
1092 * but they are used as the default "empty hash", to avoid allocating
1093 * it all the time. These are in a read only section such that if
1094 * anyone does try to modify it, it will cause an exception.
1095 */
1096static const struct hlist_head empty_buckets[1];
1097static const struct ftrace_hash empty_hash = {
1098 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001099};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001100#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001101
Steven Rostedt2b499382011-05-03 22:49:52 -04001102static struct ftrace_ops global_ops = {
Steven Rostedtf45948e2011-05-02 12:29:25 -04001103 .func = ftrace_stub,
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001104 .notrace_hash = EMPTY_HASH,
1105 .filter_hash = EMPTY_HASH,
Steven Rostedt47409742012-07-20 11:04:44 -04001106 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Steven Rostedtf45948e2011-05-02 12:29:25 -04001107};
1108
Steven Rostedt41c52c02008-05-22 11:46:33 -04001109static DEFINE_MUTEX(ftrace_regex_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02001110
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001111struct ftrace_page {
1112 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001113 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001114 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001115 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001116};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001117
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001118static struct ftrace_page *ftrace_new_pgs;
1119
Steven Rostedta7900872011-12-16 16:23:44 -05001120#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1121#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001122
1123/* estimate from running different kernels */
1124#define NR_TO_INIT 10000
1125
1126static struct ftrace_page *ftrace_pages_start;
1127static struct ftrace_page *ftrace_pages;
1128
Steven Rostedt06a51d92011-12-19 19:07:36 -05001129static bool ftrace_hash_empty(struct ftrace_hash *hash)
1130{
1131 return !hash || !hash->count;
1132}
1133
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001134static struct ftrace_func_entry *
1135ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1136{
1137 unsigned long key;
1138 struct ftrace_func_entry *entry;
1139 struct hlist_head *hhd;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001140
Steven Rostedt06a51d92011-12-19 19:07:36 -05001141 if (ftrace_hash_empty(hash))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001142 return NULL;
1143
1144 if (hash->size_bits > 0)
1145 key = hash_long(ip, hash->size_bits);
1146 else
1147 key = 0;
1148
1149 hhd = &hash->buckets[key];
1150
Sasha Levinb67bfe02013-02-27 17:06:00 -08001151 hlist_for_each_entry_rcu(entry, hhd, hlist) {
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001152 if (entry->ip == ip)
1153 return entry;
1154 }
1155 return NULL;
1156}
1157
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001158static void __add_hash_entry(struct ftrace_hash *hash,
1159 struct ftrace_func_entry *entry)
1160{
1161 struct hlist_head *hhd;
1162 unsigned long key;
1163
1164 if (hash->size_bits)
1165 key = hash_long(entry->ip, hash->size_bits);
1166 else
1167 key = 0;
1168
1169 hhd = &hash->buckets[key];
1170 hlist_add_head(&entry->hlist, hhd);
1171 hash->count++;
1172}
1173
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001174static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1175{
1176 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001177
1178 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1179 if (!entry)
1180 return -ENOMEM;
1181
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001182 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001183 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001184
1185 return 0;
1186}
1187
1188static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001189free_hash_entry(struct ftrace_hash *hash,
1190 struct ftrace_func_entry *entry)
1191{
1192 hlist_del(&entry->hlist);
1193 kfree(entry);
1194 hash->count--;
1195}
1196
1197static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001198remove_hash_entry(struct ftrace_hash *hash,
1199 struct ftrace_func_entry *entry)
1200{
1201 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001202 hash->count--;
1203}
1204
1205static void ftrace_hash_clear(struct ftrace_hash *hash)
1206{
1207 struct hlist_head *hhd;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001208 struct hlist_node *tn;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001209 struct ftrace_func_entry *entry;
1210 int size = 1 << hash->size_bits;
1211 int i;
1212
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001213 if (!hash->count)
1214 return;
1215
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001216 for (i = 0; i < size; i++) {
1217 hhd = &hash->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001218 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001219 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001220 }
1221 FTRACE_WARN_ON(hash->count);
1222}
1223
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001224static void free_ftrace_hash(struct ftrace_hash *hash)
1225{
1226 if (!hash || hash == EMPTY_HASH)
1227 return;
1228 ftrace_hash_clear(hash);
1229 kfree(hash->buckets);
1230 kfree(hash);
1231}
1232
Steven Rostedt07fd5512011-05-05 18:03:47 -04001233static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1234{
1235 struct ftrace_hash *hash;
1236
1237 hash = container_of(rcu, struct ftrace_hash, rcu);
1238 free_ftrace_hash(hash);
1239}
1240
1241static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1242{
1243 if (!hash || hash == EMPTY_HASH)
1244 return;
1245 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1246}
1247
Jiri Olsa5500fa52012-02-15 15:51:54 +01001248void ftrace_free_filter(struct ftrace_ops *ops)
1249{
1250 free_ftrace_hash(ops->filter_hash);
1251 free_ftrace_hash(ops->notrace_hash);
1252}
1253
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001254static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1255{
1256 struct ftrace_hash *hash;
1257 int size;
1258
1259 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1260 if (!hash)
1261 return NULL;
1262
1263 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001264 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001265
1266 if (!hash->buckets) {
1267 kfree(hash);
1268 return NULL;
1269 }
1270
1271 hash->size_bits = size_bits;
1272
1273 return hash;
1274}
1275
1276static struct ftrace_hash *
1277alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1278{
1279 struct ftrace_func_entry *entry;
1280 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001281 int size;
1282 int ret;
1283 int i;
1284
1285 new_hash = alloc_ftrace_hash(size_bits);
1286 if (!new_hash)
1287 return NULL;
1288
1289 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001290 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001291 return new_hash;
1292
1293 size = 1 << hash->size_bits;
1294 for (i = 0; i < size; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001295 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001296 ret = add_hash_entry(new_hash, entry->ip);
1297 if (ret < 0)
1298 goto free_hash;
1299 }
1300 }
1301
1302 FTRACE_WARN_ON(new_hash->count != hash->count);
1303
1304 return new_hash;
1305
1306 free_hash:
1307 free_ftrace_hash(new_hash);
1308 return NULL;
1309}
1310
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001311static void
1312ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1313static void
1314ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1315
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001316static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001317ftrace_hash_move(struct ftrace_ops *ops, int enable,
1318 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001319{
1320 struct ftrace_func_entry *entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001321 struct hlist_node *tn;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001322 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001323 struct ftrace_hash *old_hash;
1324 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001325 int size = src->count;
1326 int bits = 0;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001327 int ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001328 int i;
1329
1330 /*
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001331 * Remove the current set, update the hash and add
1332 * them back.
1333 */
1334 ftrace_hash_rec_disable(ops, enable);
1335
1336 /*
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001337 * If the new source is empty, just free dst and assign it
1338 * the empty_hash.
1339 */
1340 if (!src->count) {
Steven Rostedt07fd5512011-05-05 18:03:47 -04001341 free_ftrace_hash_rcu(*dst);
1342 rcu_assign_pointer(*dst, EMPTY_HASH);
Steven Rostedtd4d34b92011-11-04 20:32:39 -04001343 /* still need to update the function records */
1344 ret = 0;
1345 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001346 }
1347
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001348 /*
1349 * Make the hash size about 1/2 the # found
1350 */
1351 for (size /= 2; size; size >>= 1)
1352 bits++;
1353
1354 /* Don't allocate too much */
1355 if (bits > FTRACE_HASH_MAX_BITS)
1356 bits = FTRACE_HASH_MAX_BITS;
1357
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001358 ret = -ENOMEM;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001359 new_hash = alloc_ftrace_hash(bits);
1360 if (!new_hash)
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001361 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001362
1363 size = 1 << src->size_bits;
1364 for (i = 0; i < size; i++) {
1365 hhd = &src->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001366 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001367 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001368 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001369 }
1370 }
1371
Steven Rostedt07fd5512011-05-05 18:03:47 -04001372 old_hash = *dst;
1373 rcu_assign_pointer(*dst, new_hash);
1374 free_ftrace_hash_rcu(old_hash);
1375
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001376 ret = 0;
1377 out:
1378 /*
1379 * Enable regardless of ret:
1380 * On success, we enable the new hash.
1381 * On failure, we re-enable the original hash.
1382 */
1383 ftrace_hash_rec_enable(ops, enable);
1384
1385 return ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001386}
1387
Steven Rostedt265c8312009-02-13 12:43:56 -05001388/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001389 * Test the hashes for this ops to see if we want to call
1390 * the ops->func or not.
1391 *
1392 * It's a match if the ip is in the ops->filter_hash or
1393 * the filter_hash does not exist or is empty,
1394 * AND
1395 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001396 *
1397 * This needs to be called with preemption disabled as
1398 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001399 */
1400static int
1401ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
1402{
1403 struct ftrace_hash *filter_hash;
1404 struct ftrace_hash *notrace_hash;
1405 int ret;
1406
Steven Rostedtb8489142011-05-04 09:27:52 -04001407 filter_hash = rcu_dereference_raw(ops->filter_hash);
1408 notrace_hash = rcu_dereference_raw(ops->notrace_hash);
1409
Steven Rostedt06a51d92011-12-19 19:07:36 -05001410 if ((ftrace_hash_empty(filter_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001411 ftrace_lookup_ip(filter_hash, ip)) &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001412 (ftrace_hash_empty(notrace_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001413 !ftrace_lookup_ip(notrace_hash, ip)))
1414 ret = 1;
1415 else
1416 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001417
1418 return ret;
1419}
1420
1421/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001422 * This is a double for. Do not use 'break' to break out of the loop,
1423 * you must use a goto.
1424 */
1425#define do_for_each_ftrace_rec(pg, rec) \
1426 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1427 int _____i; \
1428 for (_____i = 0; _____i < pg->index; _____i++) { \
1429 rec = &pg->records[_____i];
1430
1431#define while_for_each_ftrace_rec() \
1432 } \
1433 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301434
Steven Rostedt5855fea2011-12-16 19:27:42 -05001435
1436static int ftrace_cmp_recs(const void *a, const void *b)
1437{
Steven Rostedta650e022012-04-25 13:48:13 -04001438 const struct dyn_ftrace *key = a;
1439 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001440
Steven Rostedta650e022012-04-25 13:48:13 -04001441 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001442 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001443 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1444 return 1;
1445 return 0;
1446}
1447
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001448static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001449{
1450 struct ftrace_page *pg;
1451 struct dyn_ftrace *rec;
1452 struct dyn_ftrace key;
1453
1454 key.ip = start;
1455 key.flags = end; /* overload flags, as it is unsigned long */
1456
1457 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1458 if (end < pg->records[0].ip ||
1459 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1460 continue;
1461 rec = bsearch(&key, pg->records, pg->index,
1462 sizeof(struct dyn_ftrace),
1463 ftrace_cmp_recs);
1464 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001465 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001466 }
1467
Steven Rostedt5855fea2011-12-16 19:27:42 -05001468 return 0;
1469}
1470
Steven Rostedtc88fd862011-08-16 09:53:39 -04001471/**
1472 * ftrace_location - return true if the ip giving is a traced location
1473 * @ip: the instruction pointer to check
1474 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001475 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001476 * That is, the instruction that is either a NOP or call to
1477 * the function tracer. It checks the ftrace internal tables to
1478 * determine if the address belongs or not.
1479 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001480unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001481{
Steven Rostedta650e022012-04-25 13:48:13 -04001482 return ftrace_location_range(ip, ip);
1483}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001484
Steven Rostedta650e022012-04-25 13:48:13 -04001485/**
1486 * ftrace_text_reserved - return true if range contains an ftrace location
1487 * @start: start of range to search
1488 * @end: end of range to search (inclusive). @end points to the last byte to check.
1489 *
1490 * Returns 1 if @start and @end contains a ftrace location.
1491 * That is, the instruction that is either a NOP or call to
1492 * the function tracer. It checks the ftrace internal tables to
1493 * determine if the address belongs or not.
1494 */
1495int ftrace_text_reserved(void *start, void *end)
1496{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001497 unsigned long ret;
1498
1499 ret = ftrace_location_range((unsigned long)start,
1500 (unsigned long)end);
1501
1502 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001503}
1504
Steven Rostedted926f92011-05-03 13:25:24 -04001505static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1506 int filter_hash,
1507 bool inc)
1508{
1509 struct ftrace_hash *hash;
1510 struct ftrace_hash *other_hash;
1511 struct ftrace_page *pg;
1512 struct dyn_ftrace *rec;
1513 int count = 0;
1514 int all = 0;
1515
1516 /* Only update if the ops has been registered */
1517 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1518 return;
1519
1520 /*
1521 * In the filter_hash case:
1522 * If the count is zero, we update all records.
1523 * Otherwise we just update the items in the hash.
1524 *
1525 * In the notrace_hash case:
1526 * We enable the update in the hash.
1527 * As disabling notrace means enabling the tracing,
1528 * and enabling notrace means disabling, the inc variable
1529 * gets inversed.
1530 */
1531 if (filter_hash) {
1532 hash = ops->filter_hash;
1533 other_hash = ops->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001534 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001535 all = 1;
1536 } else {
1537 inc = !inc;
1538 hash = ops->notrace_hash;
1539 other_hash = ops->filter_hash;
1540 /*
1541 * If the notrace hash has no items,
1542 * then there's nothing to do.
1543 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001544 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001545 return;
1546 }
1547
1548 do_for_each_ftrace_rec(pg, rec) {
1549 int in_other_hash = 0;
1550 int in_hash = 0;
1551 int match = 0;
1552
1553 if (all) {
1554 /*
1555 * Only the filter_hash affects all records.
1556 * Update if the record is not in the notrace hash.
1557 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001558 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001559 match = 1;
1560 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001561 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1562 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001563
1564 /*
1565 *
1566 */
1567 if (filter_hash && in_hash && !in_other_hash)
1568 match = 1;
1569 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001570 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001571 match = 1;
1572 }
1573 if (!match)
1574 continue;
1575
1576 if (inc) {
1577 rec->flags++;
1578 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1579 return;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001580 /*
1581 * If any ops wants regs saved for this function
1582 * then all ops will get saved regs.
1583 */
1584 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1585 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001586 } else {
1587 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1588 return;
1589 rec->flags--;
1590 }
1591 count++;
1592 /* Shortcut, if we handled all records, we are done. */
1593 if (!all && count == hash->count)
1594 return;
1595 } while_for_each_ftrace_rec();
1596}
1597
1598static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1599 int filter_hash)
1600{
1601 __ftrace_hash_rec_update(ops, filter_hash, 0);
1602}
1603
1604static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1605 int filter_hash)
1606{
1607 __ftrace_hash_rec_update(ops, filter_hash, 1);
1608}
1609
Steven Rostedt05736a42008-09-22 14:55:47 -07001610static void print_ip_ins(const char *fmt, unsigned char *p)
1611{
1612 int i;
1613
1614 printk(KERN_CONT "%s", fmt);
1615
1616 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1617 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1618}
1619
Steven Rostedtc88fd862011-08-16 09:53:39 -04001620/**
1621 * ftrace_bug - report and shutdown function tracer
1622 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1623 * @ip: The address that failed
1624 *
1625 * The arch code that enables or disables the function tracing
1626 * can call ftrace_bug() when it has detected a problem in
1627 * modifying the code. @failed should be one of either:
1628 * EFAULT - if the problem happens on reading the @ip address
1629 * EINVAL - if what is read at @ip is not what was expected
1630 * EPERM - if the problem happens on writting to the @ip address
1631 */
1632void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001633{
1634 switch (failed) {
1635 case -EFAULT:
1636 FTRACE_WARN_ON_ONCE(1);
1637 pr_info("ftrace faulted on modifying ");
1638 print_ip_sym(ip);
1639 break;
1640 case -EINVAL:
1641 FTRACE_WARN_ON_ONCE(1);
1642 pr_info("ftrace failed to modify ");
1643 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001644 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001645 printk(KERN_CONT "\n");
1646 break;
1647 case -EPERM:
1648 FTRACE_WARN_ON_ONCE(1);
1649 pr_info("ftrace faulted on writing ");
1650 print_ip_sym(ip);
1651 break;
1652 default:
1653 FTRACE_WARN_ON_ONCE(1);
1654 pr_info("ftrace faulted on unknown error ");
1655 print_ip_sym(ip);
1656 }
1657}
1658
Steven Rostedtc88fd862011-08-16 09:53:39 -04001659static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02001660{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001661 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001662
Steven Rostedt982c3502008-11-15 16:31:41 -05001663 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001664 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05001665 *
Steven Rostedted926f92011-05-03 13:25:24 -04001666 * If the record has a ref count, then we need to enable it
1667 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001668 *
Steven Rostedted926f92011-05-03 13:25:24 -04001669 * Otherwise we make sure its disabled.
1670 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001671 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04001672 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001673 */
Steven Rostedtc88fd862011-08-16 09:53:39 -04001674 if (enable && (rec->flags & ~FTRACE_FL_MASK))
Steven Rostedted926f92011-05-03 13:25:24 -04001675 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001676
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001677 /*
1678 * If enabling and the REGS flag does not match the REGS_EN, then
1679 * do not ignore this record. Set flags to fail the compare against
1680 * ENABLED.
1681 */
1682 if (flag &&
1683 (!(rec->flags & FTRACE_FL_REGS) != !(rec->flags & FTRACE_FL_REGS_EN)))
1684 flag |= FTRACE_FL_REGS;
1685
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001686 /* If the state of this record hasn't changed, then do nothing */
1687 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001688 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001689
1690 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001691 /* Save off if rec is being enabled (for return value) */
1692 flag ^= rec->flags & FTRACE_FL_ENABLED;
1693
1694 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04001695 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001696 if (flag & FTRACE_FL_REGS) {
1697 if (rec->flags & FTRACE_FL_REGS)
1698 rec->flags |= FTRACE_FL_REGS_EN;
1699 else
1700 rec->flags &= ~FTRACE_FL_REGS_EN;
1701 }
1702 }
1703
1704 /*
1705 * If this record is being updated from a nop, then
1706 * return UPDATE_MAKE_CALL.
1707 * Otherwise, if the EN flag is set, then return
1708 * UPDATE_MODIFY_CALL_REGS to tell the caller to convert
1709 * from the non-save regs, to a save regs function.
1710 * Otherwise,
1711 * return UPDATE_MODIFY_CALL to tell the caller to convert
1712 * from the save regs, to a non-save regs function.
1713 */
1714 if (flag & FTRACE_FL_ENABLED)
1715 return FTRACE_UPDATE_MAKE_CALL;
1716 else if (rec->flags & FTRACE_FL_REGS_EN)
1717 return FTRACE_UPDATE_MODIFY_CALL_REGS;
1718 else
1719 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001720 }
1721
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001722 if (update) {
1723 /* If there's no more users, clear all flags */
1724 if (!(rec->flags & ~FTRACE_FL_MASK))
1725 rec->flags = 0;
1726 else
1727 /* Just disable the record (keep REGS state) */
1728 rec->flags &= ~FTRACE_FL_ENABLED;
1729 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04001730
1731 return FTRACE_UPDATE_MAKE_NOP;
1732}
1733
1734/**
1735 * ftrace_update_record, set a record that now is tracing or not
1736 * @rec: the record to update
1737 * @enable: set to 1 if the record is tracing, zero to force disable
1738 *
1739 * The records that represent all functions that can be traced need
1740 * to be updated when tracing has been enabled.
1741 */
1742int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1743{
1744 return ftrace_check_record(rec, enable, 1);
1745}
1746
1747/**
1748 * ftrace_test_record, check if the record has been enabled or not
1749 * @rec: the record to test
1750 * @enable: set to 1 to check if enabled, 0 if it is disabled
1751 *
1752 * The arch code may need to test if a record is already set to
1753 * tracing to determine how to modify the function code that it
1754 * represents.
1755 */
1756int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1757{
1758 return ftrace_check_record(rec, enable, 0);
1759}
1760
1761static int
1762__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1763{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001764 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001765 unsigned long ftrace_addr;
1766 int ret;
1767
Steven Rostedtc88fd862011-08-16 09:53:39 -04001768 ret = ftrace_update_record(rec, enable);
1769
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001770 if (rec->flags & FTRACE_FL_REGS)
1771 ftrace_addr = (unsigned long)FTRACE_REGS_ADDR;
1772 else
1773 ftrace_addr = (unsigned long)FTRACE_ADDR;
1774
Steven Rostedtc88fd862011-08-16 09:53:39 -04001775 switch (ret) {
1776 case FTRACE_UPDATE_IGNORE:
1777 return 0;
1778
1779 case FTRACE_UPDATE_MAKE_CALL:
1780 return ftrace_make_call(rec, ftrace_addr);
1781
1782 case FTRACE_UPDATE_MAKE_NOP:
1783 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001784
1785 case FTRACE_UPDATE_MODIFY_CALL_REGS:
1786 case FTRACE_UPDATE_MODIFY_CALL:
1787 if (rec->flags & FTRACE_FL_REGS)
1788 ftrace_old_addr = (unsigned long)FTRACE_ADDR;
1789 else
1790 ftrace_old_addr = (unsigned long)FTRACE_REGS_ADDR;
1791
1792 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04001793 }
1794
1795 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02001796}
1797
Steven Rostedte4f5d542012-04-27 09:13:18 -04001798void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001799{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001800 struct dyn_ftrace *rec;
1801 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001802 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001803
Steven Rostedt45a4a232011-04-21 23:16:46 -04001804 if (unlikely(ftrace_disabled))
1805 return;
1806
Steven Rostedt265c8312009-02-13 12:43:56 -05001807 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedte4f5d542012-04-27 09:13:18 -04001808 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001809 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04001810 ftrace_bug(failed, rec->ip);
1811 /* Stop processing */
1812 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001813 }
1814 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001815}
1816
Steven Rostedtc88fd862011-08-16 09:53:39 -04001817struct ftrace_rec_iter {
1818 struct ftrace_page *pg;
1819 int index;
1820};
1821
1822/**
1823 * ftrace_rec_iter_start, start up iterating over traced functions
1824 *
1825 * Returns an iterator handle that is used to iterate over all
1826 * the records that represent address locations where functions
1827 * are traced.
1828 *
1829 * May return NULL if no records are available.
1830 */
1831struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1832{
1833 /*
1834 * We only use a single iterator.
1835 * Protected by the ftrace_lock mutex.
1836 */
1837 static struct ftrace_rec_iter ftrace_rec_iter;
1838 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1839
1840 iter->pg = ftrace_pages_start;
1841 iter->index = 0;
1842
1843 /* Could have empty pages */
1844 while (iter->pg && !iter->pg->index)
1845 iter->pg = iter->pg->next;
1846
1847 if (!iter->pg)
1848 return NULL;
1849
1850 return iter;
1851}
1852
1853/**
1854 * ftrace_rec_iter_next, get the next record to process.
1855 * @iter: The handle to the iterator.
1856 *
1857 * Returns the next iterator after the given iterator @iter.
1858 */
1859struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1860{
1861 iter->index++;
1862
1863 if (iter->index >= iter->pg->index) {
1864 iter->pg = iter->pg->next;
1865 iter->index = 0;
1866
1867 /* Could have empty pages */
1868 while (iter->pg && !iter->pg->index)
1869 iter->pg = iter->pg->next;
1870 }
1871
1872 if (!iter->pg)
1873 return NULL;
1874
1875 return iter;
1876}
1877
1878/**
1879 * ftrace_rec_iter_record, get the record at the iterator location
1880 * @iter: The current iterator location
1881 *
1882 * Returns the record that the current @iter is at.
1883 */
1884struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1885{
1886 return &iter->pg->records[iter->index];
1887}
1888
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301889static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001890ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001891{
1892 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001893 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001894
1895 ip = rec->ip;
1896
Steven Rostedt45a4a232011-04-21 23:16:46 -04001897 if (unlikely(ftrace_disabled))
1898 return 0;
1899
Shaohua Li25aac9d2009-01-09 11:29:40 +08001900 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001901 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001902 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301903 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02001904 }
Abhishek Sagar492a7ea52008-05-25 00:10:04 +05301905 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001906}
1907
Steven Rostedt000ab692009-02-17 13:35:06 -05001908/*
1909 * archs can override this function if they must do something
1910 * before the modifying code is performed.
1911 */
1912int __weak ftrace_arch_code_modify_prepare(void)
1913{
1914 return 0;
1915}
1916
1917/*
1918 * archs can override this function if they must do something
1919 * after the modifying code is performed.
1920 */
1921int __weak ftrace_arch_code_modify_post_process(void)
1922{
1923 return 0;
1924}
1925
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001926void ftrace_modify_all_code(int command)
1927{
1928 if (command & FTRACE_UPDATE_CALLS)
1929 ftrace_replace_code(1);
1930 else if (command & FTRACE_DISABLE_CALLS)
1931 ftrace_replace_code(0);
1932
1933 if (command & FTRACE_UPDATE_TRACE_FUNC)
1934 ftrace_update_ftrace_func(ftrace_trace_function);
1935
1936 if (command & FTRACE_START_FUNC_RET)
1937 ftrace_enable_ftrace_graph_caller();
1938 else if (command & FTRACE_STOP_FUNC_RET)
1939 ftrace_disable_ftrace_graph_caller();
1940}
1941
Ingo Molnare309b412008-05-12 21:20:51 +02001942static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02001943{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001944 int *command = data;
1945
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001946 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001947
Steven Rostedtc88fd862011-08-16 09:53:39 -04001948 return 0;
1949}
1950
1951/**
1952 * ftrace_run_stop_machine, go back to the stop machine method
1953 * @command: The command to tell ftrace what to do
1954 *
1955 * If an arch needs to fall back to the stop machine method, the
1956 * it can call this function.
1957 */
1958void ftrace_run_stop_machine(int command)
1959{
1960 stop_machine(__ftrace_modify_code, &command, NULL);
1961}
1962
1963/**
1964 * arch_ftrace_update_code, modify the code to trace or not trace
1965 * @command: The command that needs to be done
1966 *
1967 * Archs can override this function if it does not need to
1968 * run stop_machine() to modify code.
1969 */
1970void __weak arch_ftrace_update_code(int command)
1971{
1972 ftrace_run_stop_machine(command);
1973}
1974
1975static void ftrace_run_update_code(int command)
1976{
1977 int ret;
1978
1979 ret = ftrace_arch_code_modify_prepare();
1980 FTRACE_WARN_ON(ret);
1981 if (ret)
1982 return;
1983 /*
1984 * Do not call function tracer while we update the code.
1985 * We are in stop machine.
1986 */
1987 function_trace_stop++;
1988
1989 /*
1990 * By default we use stop_machine() to modify the code.
1991 * But archs can do what ever they want as long as it
1992 * is safe. The stop_machine() is the safest, but also
1993 * produces the most overhead.
1994 */
1995 arch_ftrace_update_code(command);
1996
Steven Rostedt6331c282011-07-13 15:11:02 -04001997 function_trace_stop--;
1998
Steven Rostedt000ab692009-02-17 13:35:06 -05001999 ret = ftrace_arch_code_modify_post_process();
2000 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002001}
2002
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002003static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002004static int ftrace_start_up;
Steven Rostedtb8489142011-05-04 09:27:52 -04002005static int global_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002006
2007static void ftrace_startup_enable(int command)
2008{
2009 if (saved_ftrace_func != ftrace_trace_function) {
2010 saved_ftrace_func = ftrace_trace_function;
2011 command |= FTRACE_UPDATE_TRACE_FUNC;
2012 }
2013
2014 if (!command || !ftrace_enabled)
2015 return;
2016
2017 ftrace_run_update_code(command);
2018}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002019
Steven Rostedta1cd6172011-05-23 15:24:25 -04002020static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002021{
Steven Rostedtb8489142011-05-04 09:27:52 -04002022 bool hash_enable = true;
2023
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002024 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002025 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002026
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002027 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002028 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02002029
Steven Rostedtb8489142011-05-04 09:27:52 -04002030 /* ops marked global share the filter hashes */
2031 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2032 ops = &global_ops;
2033 /* Don't update hash if global is already set */
2034 if (global_start_up)
2035 hash_enable = false;
2036 global_start_up++;
2037 }
2038
Steven Rostedted926f92011-05-03 13:25:24 -04002039 ops->flags |= FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002040 if (hash_enable)
Steven Rostedted926f92011-05-03 13:25:24 -04002041 ftrace_hash_rec_enable(ops, 1);
2042
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002043 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002044
2045 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002046}
2047
Steven Rostedtbd69c302011-05-03 21:55:54 -04002048static void ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002049{
Steven Rostedtb8489142011-05-04 09:27:52 -04002050 bool hash_disable = true;
2051
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002052 if (unlikely(ftrace_disabled))
2053 return;
2054
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002055 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002056 /*
2057 * Just warn in case of unbalance, no need to kill ftrace, it's not
2058 * critical but the ftrace_call callers may be never nopped again after
2059 * further ftrace uses.
2060 */
2061 WARN_ON_ONCE(ftrace_start_up < 0);
2062
Steven Rostedtb8489142011-05-04 09:27:52 -04002063 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2064 ops = &global_ops;
2065 global_start_up--;
2066 WARN_ON_ONCE(global_start_up < 0);
2067 /* Don't update hash if global still has users */
2068 if (global_start_up) {
2069 WARN_ON_ONCE(!ftrace_start_up);
2070 hash_disable = false;
2071 }
2072 }
2073
2074 if (hash_disable)
Steven Rostedted926f92011-05-03 13:25:24 -04002075 ftrace_hash_rec_disable(ops, 1);
2076
Steven Rostedtb8489142011-05-04 09:27:52 -04002077 if (ops != &global_ops || !global_start_up)
Steven Rostedted926f92011-05-03 13:25:24 -04002078 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002079
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002080 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002081
2082 if (saved_ftrace_func != ftrace_trace_function) {
2083 saved_ftrace_func = ftrace_trace_function;
2084 command |= FTRACE_UPDATE_TRACE_FUNC;
2085 }
2086
2087 if (!command || !ftrace_enabled)
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002088 return;
Steven Rostedt3d083392008-05-12 21:20:42 +02002089
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002090 ftrace_run_update_code(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02002091}
2092
Ingo Molnare309b412008-05-12 21:20:51 +02002093static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002094{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002095 if (unlikely(ftrace_disabled))
2096 return;
2097
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002098 /* Force update next time */
2099 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002100 /* ftrace_start_up is true if we want ftrace running */
2101 if (ftrace_start_up)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002102 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002103}
2104
Ingo Molnare309b412008-05-12 21:20:51 +02002105static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002106{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002107 if (unlikely(ftrace_disabled))
2108 return;
2109
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002110 /* ftrace_start_up is true if ftrace is running */
2111 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04002112 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002113}
2114
Steven Rostedt3d083392008-05-12 21:20:42 +02002115static cycle_t ftrace_update_time;
2116static unsigned long ftrace_update_cnt;
2117unsigned long ftrace_update_tot_cnt;
2118
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002119static int ops_traces_mod(struct ftrace_ops *ops)
2120{
2121 struct ftrace_hash *hash;
2122
2123 hash = ops->filter_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05002124 return ftrace_hash_empty(hash);
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002125}
2126
Steven Rostedt31e88902008-11-14 16:21:19 -08002127static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02002128{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002129 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002130 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302131 cycle_t start, stop;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002132 unsigned long ref = 0;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002133 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002134
2135 /*
2136 * When adding a module, we need to check if tracers are
2137 * currently enabled and if they are set to trace all functions.
2138 * If they are, we need to enable the module functions as well
2139 * as update the reference counts for those function records.
2140 */
2141 if (mod) {
2142 struct ftrace_ops *ops;
2143
2144 for (ops = ftrace_ops_list;
2145 ops != &ftrace_list_end; ops = ops->next) {
2146 if (ops->flags & FTRACE_OPS_FL_ENABLED &&
2147 ops_traces_mod(ops))
2148 ref++;
2149 }
2150 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002151
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002152 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002153 ftrace_update_cnt = 0;
2154
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002155 for (pg = ftrace_new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302156
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002157 for (i = 0; i < pg->index; i++) {
2158 /* If something went wrong, bail without enabling anything */
2159 if (unlikely(ftrace_disabled))
2160 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002161
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002162 p = &pg->records[i];
2163 p->flags = ref;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302164
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002165 /*
2166 * Do the initial record conversion from mcount jump
2167 * to the NOP instructions.
2168 */
2169 if (!ftrace_code_disable(mod, p))
2170 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002171
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002172 ftrace_update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002173
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002174 /*
2175 * If the tracing is enabled, go ahead and enable the record.
2176 *
2177 * The reason not to enable the record immediatelly is the
2178 * inherent check of ftrace_make_nop/ftrace_make_call for
2179 * correct previous instructions. Making first the NOP
2180 * conversion puts the module to the correct state, thus
2181 * passing the ftrace_make_call check.
2182 */
2183 if (ftrace_start_up && ref) {
2184 int failed = __ftrace_replace_code(p, 1);
2185 if (failed)
2186 ftrace_bug(failed, p->ip);
2187 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002188 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002189 }
2190
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002191 ftrace_new_pgs = NULL;
2192
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002193 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002194 ftrace_update_time = stop - start;
2195 ftrace_update_tot_cnt += ftrace_update_cnt;
2196
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002197 return 0;
2198}
2199
Steven Rostedta7900872011-12-16 16:23:44 -05002200static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002201{
Steven Rostedta7900872011-12-16 16:23:44 -05002202 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002203 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002204
Steven Rostedta7900872011-12-16 16:23:44 -05002205 if (WARN_ON(!count))
2206 return -EINVAL;
2207
2208 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002209
2210 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002211 * We want to fill as much as possible. No more than a page
2212 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002213 */
Steven Rostedta7900872011-12-16 16:23:44 -05002214 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2215 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002216
Steven Rostedta7900872011-12-16 16:23:44 -05002217 again:
2218 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2219
2220 if (!pg->records) {
2221 /* if we can't allocate this size, try something smaller */
2222 if (!order)
2223 return -ENOMEM;
2224 order >>= 1;
2225 goto again;
2226 }
2227
2228 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2229 pg->size = cnt;
2230
2231 if (cnt > count)
2232 cnt = count;
2233
2234 return cnt;
2235}
2236
2237static struct ftrace_page *
2238ftrace_allocate_pages(unsigned long num_to_init)
2239{
2240 struct ftrace_page *start_pg;
2241 struct ftrace_page *pg;
2242 int order;
2243 int cnt;
2244
2245 if (!num_to_init)
2246 return 0;
2247
2248 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2249 if (!pg)
2250 return NULL;
2251
2252 /*
2253 * Try to allocate as much as possible in one continues
2254 * location that fills in all of the space. We want to
2255 * waste as little space as possible.
2256 */
2257 for (;;) {
2258 cnt = ftrace_allocate_records(pg, num_to_init);
2259 if (cnt < 0)
2260 goto free_pages;
2261
2262 num_to_init -= cnt;
2263 if (!num_to_init)
2264 break;
2265
2266 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2267 if (!pg->next)
2268 goto free_pages;
2269
2270 pg = pg->next;
2271 }
2272
2273 return start_pg;
2274
2275 free_pages:
2276 while (start_pg) {
2277 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2278 free_pages((unsigned long)pg->records, order);
2279 start_pg = pg->next;
2280 kfree(pg);
2281 pg = start_pg;
2282 }
2283 pr_info("ftrace: FAILED to allocate memory for functions\n");
2284 return NULL;
2285}
2286
2287static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2288{
2289 int cnt;
2290
2291 if (!num_to_init) {
2292 pr_info("ftrace: No functions to be traced?\n");
2293 return -1;
2294 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002295
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002296 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04002297 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08002298 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002299
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002300 return 0;
2301}
2302
Steven Rostedt5072c592008-05-12 21:20:43 +02002303#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2304
2305struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002306 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002307 loff_t func_pos;
2308 struct ftrace_page *pg;
2309 struct dyn_ftrace *func;
2310 struct ftrace_func_probe *probe;
2311 struct trace_parser parser;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002312 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002313 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002314 int hidx;
2315 int idx;
2316 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002317};
2318
Ingo Molnare309b412008-05-12 21:20:51 +02002319static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002320t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002321{
2322 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002323 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002324 struct hlist_head *hhd;
2325
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002326 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002327 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002328
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002329 if (iter->probe)
2330 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002331 retry:
2332 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2333 return NULL;
2334
2335 hhd = &ftrace_func_hash[iter->hidx];
2336
2337 if (hlist_empty(hhd)) {
2338 iter->hidx++;
2339 hnd = NULL;
2340 goto retry;
2341 }
2342
2343 if (!hnd)
2344 hnd = hhd->first;
2345 else {
2346 hnd = hnd->next;
2347 if (!hnd) {
2348 iter->hidx++;
2349 goto retry;
2350 }
2351 }
2352
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002353 if (WARN_ON_ONCE(!hnd))
2354 return NULL;
2355
2356 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2357
2358 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002359}
2360
2361static void *t_hash_start(struct seq_file *m, loff_t *pos)
2362{
2363 struct ftrace_iterator *iter = m->private;
2364 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08002365 loff_t l;
2366
Steven Rostedt69a30832011-12-19 15:21:16 -05002367 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2368 return NULL;
2369
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002370 if (iter->func_pos > *pos)
2371 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002372
Li Zefand82d6242009-06-24 09:54:54 +08002373 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002374 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002375 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08002376 if (!p)
2377 break;
2378 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002379 if (!p)
2380 return NULL;
2381
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002382 /* Only set this if we have an item */
2383 iter->flags |= FTRACE_ITER_HASH;
2384
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002385 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002386}
2387
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002388static int
2389t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002390{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002391 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002392
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002393 rec = iter->probe;
2394 if (WARN_ON_ONCE(!rec))
2395 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002396
Steven Rostedt809dcf22009-02-16 23:06:01 -05002397 if (rec->ops->print)
2398 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2399
Steven Rostedtb375a112009-09-17 00:05:58 -04002400 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002401
2402 if (rec->data)
2403 seq_printf(m, ":%p", rec->data);
2404 seq_putc(m, '\n');
2405
2406 return 0;
2407}
2408
2409static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02002410t_next(struct seq_file *m, void *v, loff_t *pos)
2411{
2412 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002413 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002414 struct dyn_ftrace *rec = NULL;
2415
Steven Rostedt45a4a232011-04-21 23:16:46 -04002416 if (unlikely(ftrace_disabled))
2417 return NULL;
2418
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002419 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002420 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002421
Steven Rostedt5072c592008-05-12 21:20:43 +02002422 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01002423 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02002424
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002425 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002426 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002427
Steven Rostedt5072c592008-05-12 21:20:43 +02002428 retry:
2429 if (iter->idx >= iter->pg->index) {
2430 if (iter->pg->next) {
2431 iter->pg = iter->pg->next;
2432 iter->idx = 0;
2433 goto retry;
2434 }
2435 } else {
2436 rec = &iter->pg->records[iter->idx++];
Steven Rostedt32082302011-12-16 14:42:37 -05002437 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedtf45948e2011-05-02 12:29:25 -04002438 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
Steven Rostedt0183fb12008-11-07 22:36:02 -05002439
Steven Rostedt41c52c02008-05-22 11:46:33 -04002440 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt647bcd02011-05-03 14:39:21 -04002441 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2442
2443 ((iter->flags & FTRACE_ITER_ENABLED) &&
2444 !(rec->flags & ~FTRACE_FL_MASK))) {
2445
Steven Rostedt5072c592008-05-12 21:20:43 +02002446 rec = NULL;
2447 goto retry;
2448 }
2449 }
2450
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002451 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002452 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002453
2454 iter->func = rec;
2455
2456 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002457}
2458
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002459static void reset_iter_read(struct ftrace_iterator *iter)
2460{
2461 iter->pos = 0;
2462 iter->func_pos = 0;
Dan Carpenter70f77b32012-06-09 19:10:27 +03002463 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002464}
2465
2466static void *t_start(struct seq_file *m, loff_t *pos)
2467{
2468 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002469 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002470 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002471 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002472
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002473 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002474
2475 if (unlikely(ftrace_disabled))
2476 return NULL;
2477
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002478 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002479 * If an lseek was done, then reset and start from beginning.
2480 */
2481 if (*pos < iter->pos)
2482 reset_iter_read(iter);
2483
2484 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002485 * For set_ftrace_filter reading, if we have the filter
2486 * off, we can short cut and just print out that all
2487 * functions are enabled.
2488 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05002489 if (iter->flags & FTRACE_ITER_FILTER &&
2490 ftrace_hash_empty(ops->filter_hash)) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002491 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002492 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002493 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002494 /* reset in case of seek/pread */
2495 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002496 return iter;
2497 }
2498
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002499 if (iter->flags & FTRACE_ITER_HASH)
2500 return t_hash_start(m, pos);
2501
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002502 /*
2503 * Unfortunately, we need to restart at ftrace_pages_start
2504 * every time we let go of the ftrace_mutex. This is because
2505 * those pointers can change without the lock.
2506 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002507 iter->pg = ftrace_pages_start;
2508 iter->idx = 0;
2509 for (l = 0; l <= *pos; ) {
2510 p = t_next(m, p, &l);
2511 if (!p)
2512 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002513 }
walimis5821e1b2008-11-15 15:19:06 +08002514
Steven Rostedt69a30832011-12-19 15:21:16 -05002515 if (!p)
2516 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002517
2518 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002519}
2520
2521static void t_stop(struct seq_file *m, void *p)
2522{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002523 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002524}
2525
2526static int t_show(struct seq_file *m, void *v)
2527{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002528 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002529 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02002530
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002531 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002532 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002533
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002534 if (iter->flags & FTRACE_ITER_PRINTALL) {
2535 seq_printf(m, "#### all functions enabled ####\n");
2536 return 0;
2537 }
2538
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002539 rec = iter->func;
2540
Steven Rostedt5072c592008-05-12 21:20:43 +02002541 if (!rec)
2542 return 0;
2543
Steven Rostedt647bcd02011-05-03 14:39:21 -04002544 seq_printf(m, "%ps", (void *)rec->ip);
2545 if (iter->flags & FTRACE_ITER_ENABLED)
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002546 seq_printf(m, " (%ld)%s",
2547 rec->flags & ~FTRACE_FL_MASK,
2548 rec->flags & FTRACE_FL_REGS ? " R" : "");
Steven Rostedt647bcd02011-05-03 14:39:21 -04002549 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02002550
2551 return 0;
2552}
2553
James Morris88e9d342009-09-22 16:43:43 -07002554static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002555 .start = t_start,
2556 .next = t_next,
2557 .stop = t_stop,
2558 .show = t_show,
2559};
2560
Ingo Molnare309b412008-05-12 21:20:51 +02002561static int
Steven Rostedt5072c592008-05-12 21:20:43 +02002562ftrace_avail_open(struct inode *inode, struct file *file)
2563{
2564 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002565
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002566 if (unlikely(ftrace_disabled))
2567 return -ENODEV;
2568
Jiri Olsa50e18b92012-04-25 10:23:39 +02002569 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2570 if (iter) {
2571 iter->pg = ftrace_pages_start;
2572 iter->ops = &global_ops;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002573 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002574
Jiri Olsa50e18b92012-04-25 10:23:39 +02002575 return iter ? 0 : -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02002576}
2577
Steven Rostedt647bcd02011-05-03 14:39:21 -04002578static int
2579ftrace_enabled_open(struct inode *inode, struct file *file)
2580{
2581 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002582
2583 if (unlikely(ftrace_disabled))
2584 return -ENODEV;
2585
Jiri Olsa50e18b92012-04-25 10:23:39 +02002586 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2587 if (iter) {
2588 iter->pg = ftrace_pages_start;
2589 iter->flags = FTRACE_ITER_ENABLED;
2590 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002591 }
2592
Jiri Olsa50e18b92012-04-25 10:23:39 +02002593 return iter ? 0 : -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002594}
2595
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002596static void ftrace_filter_reset(struct ftrace_hash *hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02002597{
Steven Rostedt52baf112009-02-14 01:15:39 -05002598 mutex_lock(&ftrace_lock);
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002599 ftrace_hash_clear(hash);
Steven Rostedt52baf112009-02-14 01:15:39 -05002600 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002601}
2602
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002603/**
2604 * ftrace_regex_open - initialize function tracer filter files
2605 * @ops: The ftrace_ops that hold the hash filters
2606 * @flag: The type of filter to process
2607 * @inode: The inode, usually passed in to your open routine
2608 * @file: The file, usually passed in to your open routine
2609 *
2610 * ftrace_regex_open() initializes the filter files for the
2611 * @ops. Depending on @flag it may process the filter hash or
2612 * the notrace hash of @ops. With this called from the open
2613 * routine, you can use ftrace_filter_write() for the write
2614 * routine if @flag has FTRACE_ITER_FILTER set, or
2615 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
Steven Rostedt (Red Hat)7f49ef62013-04-12 16:40:13 -04002616 * ftrace_filter_lseek() should be used as the lseek routine, and
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002617 * release must call ftrace_regex_release().
2618 */
2619int
Steven Rostedtf45948e2011-05-02 12:29:25 -04002620ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002621 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02002622{
2623 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002624 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02002625 int ret = 0;
2626
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002627 if (unlikely(ftrace_disabled))
2628 return -ENODEV;
2629
Steven Rostedt5072c592008-05-12 21:20:43 +02002630 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2631 if (!iter)
2632 return -ENOMEM;
2633
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002634 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2635 kfree(iter);
2636 return -ENOMEM;
2637 }
2638
Steven Rostedtf45948e2011-05-02 12:29:25 -04002639 if (flag & FTRACE_ITER_NOTRACE)
2640 hash = ops->notrace_hash;
2641 else
2642 hash = ops->filter_hash;
2643
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002644 iter->ops = ops;
2645 iter->flags = flag;
2646
2647 if (file->f_mode & FMODE_WRITE) {
2648 mutex_lock(&ftrace_lock);
2649 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2650 mutex_unlock(&ftrace_lock);
2651
2652 if (!iter->hash) {
2653 trace_parser_put(&iter->parser);
2654 kfree(iter);
2655 return -ENOMEM;
2656 }
2657 }
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002658
Steven Rostedt41c52c02008-05-22 11:46:33 -04002659 mutex_lock(&ftrace_regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002660
Steven Rostedt5072c592008-05-12 21:20:43 +02002661 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002662 (file->f_flags & O_TRUNC))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002663 ftrace_filter_reset(iter->hash);
Steven Rostedt5072c592008-05-12 21:20:43 +02002664
2665 if (file->f_mode & FMODE_READ) {
2666 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02002667
2668 ret = seq_open(file, &show_ftrace_seq_ops);
2669 if (!ret) {
2670 struct seq_file *m = file->private_data;
2671 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08002672 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002673 /* Failed */
2674 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08002675 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002676 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08002677 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002678 } else
2679 file->private_data = iter;
Steven Rostedt41c52c02008-05-22 11:46:33 -04002680 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002681
2682 return ret;
2683}
2684
Steven Rostedt41c52c02008-05-22 11:46:33 -04002685static int
2686ftrace_filter_open(struct inode *inode, struct file *file)
2687{
Steven Rostedt69a30832011-12-19 15:21:16 -05002688 return ftrace_regex_open(&global_ops,
2689 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2690 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002691}
2692
2693static int
2694ftrace_notrace_open(struct inode *inode, struct file *file)
2695{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002696 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002697 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002698}
2699
Steven Rostedt64e7c442009-02-13 17:08:48 -05002700static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002701{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002702 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08002703 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002704
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002705 switch (type) {
2706 case MATCH_FULL:
2707 if (strcmp(str, regex) == 0)
2708 matched = 1;
2709 break;
2710 case MATCH_FRONT_ONLY:
2711 if (strncmp(str, regex, len) == 0)
2712 matched = 1;
2713 break;
2714 case MATCH_MIDDLE_ONLY:
2715 if (strstr(str, regex))
2716 matched = 1;
2717 break;
2718 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08002719 slen = strlen(str);
2720 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002721 matched = 1;
2722 break;
2723 }
2724
2725 return matched;
2726}
2727
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002728static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002729enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04002730{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002731 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002732 int ret = 0;
2733
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002734 entry = ftrace_lookup_ip(hash, rec->ip);
2735 if (not) {
2736 /* Do nothing if it doesn't exist */
2737 if (!entry)
2738 return 0;
2739
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002740 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002741 } else {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002742 /* Do nothing if it exists */
2743 if (entry)
2744 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002745
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002746 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002747 }
2748 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04002749}
2750
Steven Rostedt64e7c442009-02-13 17:08:48 -05002751static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002752ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2753 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002754{
2755 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002756 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002757
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002758 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2759
2760 if (mod) {
2761 /* module lookup requires matching the module */
2762 if (!modname || strcmp(modname, mod))
2763 return 0;
2764
2765 /* blank search means to match all funcs in the mod */
2766 if (!len)
2767 return 1;
2768 }
2769
Steven Rostedt64e7c442009-02-13 17:08:48 -05002770 return ftrace_match(str, regex, len, type);
2771}
2772
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002773static int
2774match_records(struct ftrace_hash *hash, char *buff,
2775 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002776{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002777 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002778 struct ftrace_page *pg;
2779 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002780 int type = MATCH_FULL;
2781 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08002782 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002783 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002784
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002785 if (len) {
2786 type = filter_parse_regex(buff, len, &search, &not);
2787 search_len = strlen(search);
2788 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002789
Steven Rostedt52baf112009-02-14 01:15:39 -05002790 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002791
2792 if (unlikely(ftrace_disabled))
2793 goto out_unlock;
2794
Steven Rostedt265c8312009-02-13 12:43:56 -05002795 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002796 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002797 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002798 if (ret < 0) {
2799 found = ret;
2800 goto out_unlock;
2801 }
Li Zefan311d16d2009-12-08 11:15:11 +08002802 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05002803 }
2804 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002805 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05002806 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08002807
2808 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02002809}
2810
Steven Rostedt64e7c442009-02-13 17:08:48 -05002811static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002812ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002813{
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002814 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002815}
2816
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002817static int
2818ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002819{
Steven Rostedt64e7c442009-02-13 17:08:48 -05002820 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002821
Steven Rostedt64e7c442009-02-13 17:08:48 -05002822 /* blank or '*' mean the same */
2823 if (strcmp(buff, "*") == 0)
2824 buff[0] = 0;
2825
2826 /* handle the case of 'dont filter this module' */
2827 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2828 buff[0] = 0;
2829 not = 1;
2830 }
2831
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002832 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002833}
2834
Steven Rostedtf6180772009-02-14 00:40:25 -05002835/*
2836 * We register the module command as a template to show others how
2837 * to register the a command as well.
2838 */
2839
2840static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04002841ftrace_mod_callback(struct ftrace_hash *hash,
2842 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05002843{
2844 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002845 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05002846
2847 /*
2848 * cmd == 'mod' because we only registered this func
2849 * for the 'mod' ftrace_func_command.
2850 * But if you register one func with multiple commands,
2851 * you can tell which command was used by the cmd
2852 * parameter.
2853 */
2854
2855 /* we must have a module name */
2856 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002857 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002858
2859 mod = strsep(&param, ":");
2860 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002861 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002862
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002863 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002864 if (!ret)
2865 ret = -EINVAL;
2866 if (ret < 0)
2867 return ret;
2868
2869 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05002870}
2871
2872static struct ftrace_func_command ftrace_mod_cmd = {
2873 .name = "mod",
2874 .func = ftrace_mod_callback,
2875};
2876
2877static int __init ftrace_mod_cmd_init(void)
2878{
2879 return register_ftrace_command(&ftrace_mod_cmd);
2880}
Steven Rostedt6f415672012-10-05 12:13:07 -04002881core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05002882
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04002883static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04002884 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002885{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002886 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002887 struct hlist_head *hhd;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002888 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002889
2890 key = hash_long(ip, FTRACE_HASH_BITS);
2891
2892 hhd = &ftrace_func_hash[key];
2893
2894 if (hlist_empty(hhd))
2895 return;
2896
2897 /*
2898 * Disable preemption for these calls to prevent a RCU grace
2899 * period. This syncs the hash iteration and freeing of items
2900 * on the hash. rcu_read_lock is too dangerous here.
2901 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04002902 preempt_disable_notrace();
Sasha Levinb67bfe02013-02-27 17:06:00 -08002903 hlist_for_each_entry_rcu(entry, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05002904 if (entry->ip == ip)
2905 entry->ops->func(ip, parent_ip, &entry->data);
2906 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04002907 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002908}
2909
Steven Rostedtb6887d72009-02-17 12:32:04 -05002910static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05002911{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04002912 .func = function_trace_probe_call,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002913};
2914
Steven Rostedtb6887d72009-02-17 12:32:04 -05002915static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002916
Steven Rostedtb6887d72009-02-17 12:32:04 -05002917static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002918{
Steven Rostedtb8489142011-05-04 09:27:52 -04002919 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002920 int i;
2921
Steven Rostedtb6887d72009-02-17 12:32:04 -05002922 if (ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002923 return;
2924
2925 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2926 struct hlist_head *hhd = &ftrace_func_hash[i];
2927 if (hhd->first)
2928 break;
2929 }
2930 /* Nothing registered? */
2931 if (i == FTRACE_FUNC_HASHSIZE)
2932 return;
2933
Steven Rostedtb8489142011-05-04 09:27:52 -04002934 ret = __register_ftrace_function(&trace_probe_ops);
2935 if (!ret)
Steven Rostedta1cd6172011-05-23 15:24:25 -04002936 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04002937
Steven Rostedtb6887d72009-02-17 12:32:04 -05002938 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002939}
2940
Steven Rostedtb6887d72009-02-17 12:32:04 -05002941static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002942{
Steven Rostedtb8489142011-05-04 09:27:52 -04002943 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002944 int i;
2945
Steven Rostedtb6887d72009-02-17 12:32:04 -05002946 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002947 return;
2948
2949 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2950 struct hlist_head *hhd = &ftrace_func_hash[i];
2951 if (hhd->first)
2952 return;
2953 }
2954
2955 /* no more funcs left */
Steven Rostedtb8489142011-05-04 09:27:52 -04002956 ret = __unregister_ftrace_function(&trace_probe_ops);
2957 if (!ret)
2958 ftrace_shutdown(&trace_probe_ops, 0);
2959
Steven Rostedtb6887d72009-02-17 12:32:04 -05002960 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002961}
2962
2963
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04002964static void ftrace_free_entry(struct ftrace_func_probe *entry)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002965{
Steven Rostedt59df055f2009-02-14 15:29:06 -05002966 if (entry->ops->free)
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04002967 entry->ops->free(entry->ops, entry->ip, &entry->data);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002968 kfree(entry);
2969}
2970
Steven Rostedt59df055f2009-02-14 15:29:06 -05002971int
Steven Rostedtb6887d72009-02-17 12:32:04 -05002972register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002973 void *data)
2974{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002975 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04002976 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
2977 struct ftrace_hash *hash;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002978 struct ftrace_page *pg;
2979 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002980 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002981 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002982 int count = 0;
2983 char *search;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04002984 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002985
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002986 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002987 len = strlen(search);
2988
Steven Rostedtb6887d72009-02-17 12:32:04 -05002989 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05002990 if (WARN_ON(not))
2991 return -EINVAL;
2992
2993 mutex_lock(&ftrace_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002994
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04002995 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
2996 if (!hash) {
2997 count = -ENOMEM;
Steven Rostedt45a4a232011-04-21 23:16:46 -04002998 goto out_unlock;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04002999 }
3000
3001 if (unlikely(ftrace_disabled)) {
3002 count = -ENODEV;
3003 goto out_unlock;
3004 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003005
Steven Rostedt59df055f2009-02-14 15:29:06 -05003006 do_for_each_ftrace_rec(pg, rec) {
3007
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003008 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003009 continue;
3010
3011 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3012 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003013 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003014 if (!count)
3015 count = -ENOMEM;
3016 goto out_unlock;
3017 }
3018
3019 count++;
3020
3021 entry->data = data;
3022
3023 /*
3024 * The caller might want to do something special
3025 * for each function we find. We call the callback
3026 * to give the caller an opportunity to do so.
3027 */
Steven Rostedt (Red Hat)e67efb92013-03-12 15:07:59 -04003028 if (ops->init) {
3029 if (ops->init(ops, rec->ip, &entry->data) < 0) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003030 /* caller does not like this func */
3031 kfree(entry);
3032 continue;
3033 }
3034 }
3035
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003036 ret = enter_record(hash, rec, 0);
3037 if (ret < 0) {
3038 kfree(entry);
3039 count = ret;
3040 goto out_unlock;
3041 }
3042
Steven Rostedt59df055f2009-02-14 15:29:06 -05003043 entry->ops = ops;
3044 entry->ip = rec->ip;
3045
3046 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3047 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3048
3049 } while_for_each_ftrace_rec();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003050
3051 ret = ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
3052 if (ret < 0)
3053 count = ret;
3054
Steven Rostedtb6887d72009-02-17 12:32:04 -05003055 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003056
3057 out_unlock:
3058 mutex_unlock(&ftrace_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003059 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003060
3061 return count;
3062}
3063
3064enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003065 PROBE_TEST_FUNC = 1,
3066 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05003067};
3068
3069static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003070__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003071 void *data, int flags)
3072{
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003073 struct ftrace_func_entry *rec_entry;
Steven Rostedtb6887d72009-02-17 12:32:04 -05003074 struct ftrace_func_probe *entry;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003075 struct ftrace_func_probe *p;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003076 struct ftrace_hash **orig_hash = &trace_probe_ops.filter_hash;
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003077 struct list_head free_list;
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003078 struct ftrace_hash *hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003079 struct hlist_node *tmp;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003080 char str[KSYM_SYMBOL_LEN];
3081 int type = MATCH_FULL;
3082 int i, len = 0;
3083 char *search;
3084
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003085 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003086 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003087 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003088 int not;
3089
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003090 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003091 len = strlen(search);
3092
Steven Rostedtb6887d72009-02-17 12:32:04 -05003093 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003094 if (WARN_ON(not))
3095 return;
3096 }
3097
3098 mutex_lock(&ftrace_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003099
3100 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3101 if (!hash)
3102 /* Hmm, should report this somehow */
3103 goto out_unlock;
3104
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003105 INIT_LIST_HEAD(&free_list);
3106
Steven Rostedt59df055f2009-02-14 15:29:06 -05003107 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3108 struct hlist_head *hhd = &ftrace_func_hash[i];
3109
Sasha Levinb67bfe02013-02-27 17:06:00 -08003110 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003111
3112 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05003113 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003114 continue;
3115
Steven Rostedtb6887d72009-02-17 12:32:04 -05003116 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003117 continue;
3118
3119 /* do this last, since it is the most expensive */
3120 if (glob) {
3121 kallsyms_lookup(entry->ip, NULL, NULL,
3122 NULL, str);
3123 if (!ftrace_match(str, glob, len, type))
3124 continue;
3125 }
3126
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003127 rec_entry = ftrace_lookup_ip(hash, entry->ip);
3128 /* It is possible more than one entry had this ip */
3129 if (rec_entry)
3130 free_hash_entry(hash, rec_entry);
3131
Steven Rostedt (Red Hat)740466b2013-03-13 11:15:19 -04003132 hlist_del_rcu(&entry->node);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003133 list_add(&entry->free_list, &free_list);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003134 }
3135 }
Steven Rostedtb6887d72009-02-17 12:32:04 -05003136 __disable_ftrace_function_probe();
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003137 /*
3138 * Remove after the disable is called. Otherwise, if the last
3139 * probe is removed, a null hash means *all enabled*.
3140 */
3141 ftrace_hash_move(&trace_probe_ops, 1, orig_hash, hash);
Steven Rostedt (Red Hat)7818b382013-03-13 12:42:58 -04003142 synchronize_sched();
3143 list_for_each_entry_safe(entry, p, &free_list, free_list) {
3144 list_del(&entry->free_list);
3145 ftrace_free_entry(entry);
3146 }
3147
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003148 out_unlock:
Steven Rostedt59df055f2009-02-14 15:29:06 -05003149 mutex_unlock(&ftrace_lock);
Steven Rostedt (Red Hat)e1df4cb2013-03-12 10:09:42 -04003150 free_ftrace_hash(hash);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003151}
3152
3153void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003154unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003155 void *data)
3156{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003157 __unregister_ftrace_function_probe(glob, ops, data,
3158 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003159}
3160
3161void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003162unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003163{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003164 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003165}
3166
Steven Rostedtb6887d72009-02-17 12:32:04 -05003167void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003168{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003169 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003170}
3171
Steven Rostedtf6180772009-02-14 00:40:25 -05003172static LIST_HEAD(ftrace_commands);
3173static DEFINE_MUTEX(ftrace_cmd_mutex);
3174
3175int register_ftrace_command(struct ftrace_func_command *cmd)
3176{
3177 struct ftrace_func_command *p;
3178 int ret = 0;
3179
3180 mutex_lock(&ftrace_cmd_mutex);
3181 list_for_each_entry(p, &ftrace_commands, list) {
3182 if (strcmp(cmd->name, p->name) == 0) {
3183 ret = -EBUSY;
3184 goto out_unlock;
3185 }
3186 }
3187 list_add(&cmd->list, &ftrace_commands);
3188 out_unlock:
3189 mutex_unlock(&ftrace_cmd_mutex);
3190
3191 return ret;
3192}
3193
3194int unregister_ftrace_command(struct ftrace_func_command *cmd)
3195{
3196 struct ftrace_func_command *p, *n;
3197 int ret = -ENODEV;
3198
3199 mutex_lock(&ftrace_cmd_mutex);
3200 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3201 if (strcmp(cmd->name, p->name) == 0) {
3202 ret = 0;
3203 list_del_init(&p->list);
3204 goto out_unlock;
3205 }
3206 }
3207 out_unlock:
3208 mutex_unlock(&ftrace_cmd_mutex);
3209
3210 return ret;
3211}
3212
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003213static int ftrace_process_regex(struct ftrace_hash *hash,
3214 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003215{
Steven Rostedtf6180772009-02-14 00:40:25 -05003216 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003217 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08003218 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003219
3220 func = strsep(&next, ":");
3221
3222 if (!next) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003223 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003224 if (!ret)
3225 ret = -EINVAL;
3226 if (ret < 0)
3227 return ret;
3228 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003229 }
3230
Steven Rostedtf6180772009-02-14 00:40:25 -05003231 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05003232
3233 command = strsep(&next, ":");
3234
Steven Rostedtf6180772009-02-14 00:40:25 -05003235 mutex_lock(&ftrace_cmd_mutex);
3236 list_for_each_entry(p, &ftrace_commands, list) {
3237 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003238 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05003239 goto out_unlock;
3240 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05003241 }
Steven Rostedtf6180772009-02-14 00:40:25 -05003242 out_unlock:
3243 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003244
Steven Rostedtf6180772009-02-14 00:40:25 -05003245 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003246}
3247
Ingo Molnare309b412008-05-12 21:20:51 +02003248static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003249ftrace_regex_write(struct file *file, const char __user *ubuf,
3250 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02003251{
3252 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003253 struct trace_parser *parser;
3254 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02003255
Li Zefan4ba79782009-09-22 13:52:20 +08003256 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02003257 return 0;
3258
Steven Rostedt41c52c02008-05-22 11:46:33 -04003259 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003260
Steven Rostedt45a4a232011-04-21 23:16:46 -04003261 ret = -ENODEV;
3262 if (unlikely(ftrace_disabled))
3263 goto out_unlock;
3264
Steven Rostedt5072c592008-05-12 21:20:43 +02003265 if (file->f_mode & FMODE_READ) {
3266 struct seq_file *m = file->private_data;
3267 iter = m->private;
3268 } else
3269 iter = file->private_data;
3270
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003271 parser = &iter->parser;
3272 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02003273
Li Zefan4ba79782009-09-22 13:52:20 +08003274 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003275 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003276 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003277 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08003278 trace_parser_clear(parser);
Steven Rostedt (Red Hat)7c088b52013-05-09 11:35:12 -04003279 if (ret < 0)
Li Zefaned146b252009-11-03 08:55:38 +08003280 goto out_unlock;
Steven Rostedt5072c592008-05-12 21:20:43 +02003281 }
3282
Steven Rostedt5072c592008-05-12 21:20:43 +02003283 ret = read;
Li Zefaned146b252009-11-03 08:55:38 +08003284out_unlock:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003285 mutex_unlock(&ftrace_regex_lock);
Li Zefaned146b252009-11-03 08:55:38 +08003286
Steven Rostedt5072c592008-05-12 21:20:43 +02003287 return ret;
3288}
3289
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003290ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003291ftrace_filter_write(struct file *file, const char __user *ubuf,
3292 size_t cnt, loff_t *ppos)
3293{
3294 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3295}
3296
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003297ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003298ftrace_notrace_write(struct file *file, const char __user *ubuf,
3299 size_t cnt, loff_t *ppos)
3300{
3301 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3302}
3303
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003304static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003305ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3306{
3307 struct ftrace_func_entry *entry;
3308
3309 if (!ftrace_location(ip))
3310 return -EINVAL;
3311
3312 if (remove) {
3313 entry = ftrace_lookup_ip(hash, ip);
3314 if (!entry)
3315 return -ENOENT;
3316 free_hash_entry(hash, entry);
3317 return 0;
3318 }
3319
3320 return add_hash_entry(hash, ip);
3321}
3322
3323static int
3324ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3325 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003326{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003327 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003328 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003329 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003330
Steven Rostedt936e0742011-05-05 22:54:01 -04003331 /* All global ops uses the global ops filters */
3332 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3333 ops = &global_ops;
3334
Steven Rostedt41c52c02008-05-22 11:46:33 -04003335 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003336 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003337
Steven Rostedtf45948e2011-05-02 12:29:25 -04003338 if (enable)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003339 orig_hash = &ops->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003340 else
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003341 orig_hash = &ops->notrace_hash;
3342
3343 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3344 if (!hash)
3345 return -ENOMEM;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003346
Steven Rostedt41c52c02008-05-22 11:46:33 -04003347 mutex_lock(&ftrace_regex_lock);
3348 if (reset)
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003349 ftrace_filter_reset(hash);
Jiri Olsaac483c42012-01-02 10:04:14 +01003350 if (buf && !ftrace_match_records(hash, buf, len)) {
3351 ret = -EINVAL;
3352 goto out_regex_unlock;
3353 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003354 if (ip) {
3355 ret = ftrace_match_addr(hash, ip, remove);
3356 if (ret < 0)
3357 goto out_regex_unlock;
3358 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003359
3360 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003361 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt072126f2011-07-13 15:08:31 -04003362 if (!ret && ops->flags & FTRACE_OPS_FL_ENABLED
3363 && ftrace_enabled)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01003364 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt072126f2011-07-13 15:08:31 -04003365
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003366 mutex_unlock(&ftrace_lock);
3367
Jiri Olsaac483c42012-01-02 10:04:14 +01003368 out_regex_unlock:
Steven Rostedt41c52c02008-05-22 11:46:33 -04003369 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003370
3371 free_ftrace_hash(hash);
3372 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003373}
3374
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003375static int
3376ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3377 int reset, int enable)
3378{
3379 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3380}
3381
3382/**
3383 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3384 * @ops - the ops to set the filter with
3385 * @ip - the address to add to or remove from the filter.
3386 * @remove - non zero to remove the ip from the filter
3387 * @reset - non zero to reset all filters before applying this filter.
3388 *
3389 * Filters denote which functions should be enabled when tracing is enabled
3390 * If @ip is NULL, it failes to update filter.
3391 */
3392int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3393 int remove, int reset)
3394{
3395 return ftrace_set_addr(ops, ip, remove, reset, 1);
3396}
3397EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3398
3399static int
3400ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3401 int reset, int enable)
3402{
3403 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3404}
3405
Steven Rostedt77a2b372008-05-12 21:20:45 +02003406/**
3407 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003408 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02003409 * @buf - the string that holds the function filter text.
3410 * @len - the length of the string.
3411 * @reset - non zero to reset all filters before applying this filter.
3412 *
3413 * Filters denote which functions should be enabled when tracing is enabled.
3414 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3415 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003416int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003417 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02003418{
Jiri Olsaac483c42012-01-02 10:04:14 +01003419 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003420}
Steven Rostedt936e0742011-05-05 22:54:01 -04003421EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003422
Steven Rostedt41c52c02008-05-22 11:46:33 -04003423/**
3424 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003425 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04003426 * @buf - the string that holds the function notrace text.
3427 * @len - the length of the string.
3428 * @reset - non zero to reset all filters before applying this filter.
3429 *
3430 * Notrace Filters denote which functions should not be enabled when tracing
3431 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3432 * for tracing.
3433 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003434int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003435 int len, int reset)
3436{
Jiri Olsaac483c42012-01-02 10:04:14 +01003437 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04003438}
3439EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3440/**
3441 * ftrace_set_filter - set a function to filter on in ftrace
3442 * @ops - the ops to set the filter with
3443 * @buf - the string that holds the function filter text.
3444 * @len - the length of the string.
3445 * @reset - non zero to reset all filters before applying this filter.
3446 *
3447 * Filters denote which functions should be enabled when tracing is enabled.
3448 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3449 */
3450void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3451{
3452 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3453}
3454EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3455
3456/**
3457 * ftrace_set_notrace - set a function to not trace in ftrace
3458 * @ops - the ops to set the notrace filter with
3459 * @buf - the string that holds the function notrace text.
3460 * @len - the length of the string.
3461 * @reset - non zero to reset all filters before applying this filter.
3462 *
3463 * Notrace Filters denote which functions should not be enabled when tracing
3464 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3465 * for tracing.
3466 */
3467void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003468{
Steven Rostedtf45948e2011-05-02 12:29:25 -04003469 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003470}
Steven Rostedt936e0742011-05-05 22:54:01 -04003471EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003472
Steven Rostedt2af15d62009-05-28 13:37:24 -04003473/*
3474 * command line interface to allow users to set filters on boot up.
3475 */
3476#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3477static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3478static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3479
3480static int __init set_ftrace_notrace(char *str)
3481{
Chen Gang75761cc2013-04-08 12:12:39 +08003482 strlcpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003483 return 1;
3484}
3485__setup("ftrace_notrace=", set_ftrace_notrace);
3486
3487static int __init set_ftrace_filter(char *str)
3488{
Chen Gang75761cc2013-04-08 12:12:39 +08003489 strlcpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003490 return 1;
3491}
3492__setup("ftrace_filter=", set_ftrace_filter);
3493
Stefan Assmann369bc182009-10-12 22:17:21 +02003494#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08003495static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Steven Rostedt801c29f2010-03-05 20:02:19 -05003496static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
3497
Stefan Assmann369bc182009-10-12 22:17:21 +02003498static int __init set_graph_function(char *str)
3499{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02003500 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02003501 return 1;
3502}
3503__setup("ftrace_graph_filter=", set_graph_function);
3504
3505static void __init set_ftrace_early_graph(char *buf)
3506{
3507 int ret;
3508 char *func;
3509
3510 while (buf) {
3511 func = strsep(&buf, ",");
3512 /* we allow only one expression at a time */
3513 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3514 func);
3515 if (ret)
3516 printk(KERN_DEBUG "ftrace: function %s not "
3517 "traceable\n", func);
3518 }
3519}
3520#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3521
Steven Rostedt2a85a372011-12-19 21:57:44 -05003522void __init
3523ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04003524{
3525 char *func;
3526
3527 while (buf) {
3528 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04003529 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003530 }
3531}
3532
3533static void __init set_ftrace_early_filters(void)
3534{
3535 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003536 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003537 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003538 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02003539#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3540 if (ftrace_graph_buf[0])
3541 set_ftrace_early_graph(ftrace_graph_buf);
3542#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04003543}
3544
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003545int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003546{
3547 struct seq_file *m = (struct seq_file *)file->private_data;
3548 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003549 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003550 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04003551 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003552 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02003553
Steven Rostedt41c52c02008-05-22 11:46:33 -04003554 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003555 if (file->f_mode & FMODE_READ) {
3556 iter = m->private;
3557
3558 seq_release(inode, file);
3559 } else
3560 iter = file->private_data;
3561
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003562 parser = &iter->parser;
3563 if (trace_parser_loaded(parser)) {
3564 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003565 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02003566 }
3567
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003568 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003569
Steven Rostedt058e2972011-04-29 22:35:33 -04003570 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04003571 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3572
3573 if (filter_hash)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003574 orig_hash = &iter->ops->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04003575 else
3576 orig_hash = &iter->ops->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003577
Steven Rostedt058e2972011-04-29 22:35:33 -04003578 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003579 ret = ftrace_hash_move(iter->ops, filter_hash,
3580 orig_hash, iter->hash);
3581 if (!ret && (iter->ops->flags & FTRACE_OPS_FL_ENABLED)
3582 && ftrace_enabled)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01003583 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003584
Steven Rostedt058e2972011-04-29 22:35:33 -04003585 mutex_unlock(&ftrace_lock);
3586 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003587 free_ftrace_hash(iter->hash);
3588 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04003589
Steven Rostedt41c52c02008-05-22 11:46:33 -04003590 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003591 return 0;
3592}
3593
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003594static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003595 .open = ftrace_avail_open,
3596 .read = seq_read,
3597 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08003598 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02003599};
3600
Steven Rostedt647bcd02011-05-03 14:39:21 -04003601static const struct file_operations ftrace_enabled_fops = {
3602 .open = ftrace_enabled_open,
3603 .read = seq_read,
3604 .llseek = seq_lseek,
3605 .release = seq_release_private,
3606};
3607
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003608static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003609 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003610 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02003611 .write = ftrace_filter_write,
Namhyung Kim6a76f8c2013-04-11 15:55:01 +09003612 .llseek = ftrace_filter_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003613 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02003614};
3615
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003616static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04003617 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003618 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003619 .write = ftrace_notrace_write,
Namhyung Kim6a76f8c2013-04-11 15:55:01 +09003620 .llseek = ftrace_filter_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003621 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003622};
3623
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003624#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3625
3626static DEFINE_MUTEX(graph_lock);
3627
3628int ftrace_graph_count;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003629int ftrace_graph_filter_enabled;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003630unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3631
3632static void *
Li Zefan85951842009-06-24 09:54:00 +08003633__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003634{
Li Zefan85951842009-06-24 09:54:00 +08003635 if (*pos >= ftrace_graph_count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003636 return NULL;
Li Zefana4ec5e02009-09-18 14:06:28 +08003637 return &ftrace_graph_funcs[*pos];
Li Zefan85951842009-06-24 09:54:00 +08003638}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003639
Li Zefan85951842009-06-24 09:54:00 +08003640static void *
3641g_next(struct seq_file *m, void *v, loff_t *pos)
3642{
3643 (*pos)++;
3644 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003645}
3646
3647static void *g_start(struct seq_file *m, loff_t *pos)
3648{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003649 mutex_lock(&graph_lock);
3650
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003651 /* Nothing, tell g_show to print all functions are enabled */
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003652 if (!ftrace_graph_filter_enabled && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003653 return (void *)1;
3654
Li Zefan85951842009-06-24 09:54:00 +08003655 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003656}
3657
3658static void g_stop(struct seq_file *m, void *p)
3659{
3660 mutex_unlock(&graph_lock);
3661}
3662
3663static int g_show(struct seq_file *m, void *v)
3664{
3665 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003666
3667 if (!ptr)
3668 return 0;
3669
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003670 if (ptr == (unsigned long *)1) {
3671 seq_printf(m, "#### all functions enabled ####\n");
3672 return 0;
3673 }
3674
Steven Rostedtb375a112009-09-17 00:05:58 -04003675 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003676
3677 return 0;
3678}
3679
James Morris88e9d342009-09-22 16:43:43 -07003680static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003681 .start = g_start,
3682 .next = g_next,
3683 .stop = g_stop,
3684 .show = g_show,
3685};
3686
3687static int
3688ftrace_graph_open(struct inode *inode, struct file *file)
3689{
3690 int ret = 0;
3691
3692 if (unlikely(ftrace_disabled))
3693 return -ENODEV;
3694
3695 mutex_lock(&graph_lock);
3696 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04003697 (file->f_flags & O_TRUNC)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003698 ftrace_graph_filter_enabled = 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003699 ftrace_graph_count = 0;
3700 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3701 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003702 mutex_unlock(&graph_lock);
3703
Li Zefana4ec5e02009-09-18 14:06:28 +08003704 if (file->f_mode & FMODE_READ)
3705 ret = seq_open(file, &ftrace_graph_seq_ops);
3706
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003707 return ret;
3708}
3709
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003710static int
Li Zefan87827112009-07-23 11:29:11 +08003711ftrace_graph_release(struct inode *inode, struct file *file)
3712{
3713 if (file->f_mode & FMODE_READ)
3714 seq_release(inode, file);
3715 return 0;
3716}
3717
3718static int
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003719ftrace_set_func(unsigned long *array, int *idx, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003720{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003721 struct dyn_ftrace *rec;
3722 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003723 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003724 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003725 int type, not;
3726 char *search;
3727 bool exists;
3728 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003729
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003730 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003731 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003732 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3733 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003734
3735 search_len = strlen(search);
3736
Steven Rostedt52baf112009-02-14 01:15:39 -05003737 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003738
3739 if (unlikely(ftrace_disabled)) {
3740 mutex_unlock(&ftrace_lock);
3741 return -ENODEV;
3742 }
3743
Steven Rostedt265c8312009-02-13 12:43:56 -05003744 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003745
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003746 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003747 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003748 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003749 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003750 if (array[i] == rec->ip) {
3751 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05003752 break;
3753 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003754 }
3755
3756 if (!not) {
3757 fail = 0;
3758 if (!exists) {
3759 array[(*idx)++] = rec->ip;
3760 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3761 goto out;
3762 }
3763 } else {
3764 if (exists) {
3765 array[i] = array[--(*idx)];
3766 array[*idx] = 0;
3767 fail = 0;
3768 }
3769 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003770 }
Steven Rostedt265c8312009-02-13 12:43:56 -05003771 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003772out:
Steven Rostedt52baf112009-02-14 01:15:39 -05003773 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003774
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003775 if (fail)
3776 return -EINVAL;
3777
Namhyung Kim9f50afc2013-04-11 16:01:38 +09003778 ftrace_graph_filter_enabled = !!(*idx);
3779
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003780 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003781}
3782
3783static ssize_t
3784ftrace_graph_write(struct file *file, const char __user *ubuf,
3785 size_t cnt, loff_t *ppos)
3786{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003787 struct trace_parser parser;
Li Zefan4ba79782009-09-22 13:52:20 +08003788 ssize_t read, ret;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003789
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003790 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003791 return 0;
3792
3793 mutex_lock(&graph_lock);
3794
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003795 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3796 ret = -ENOMEM;
Li Zefan1eb90f12009-09-22 13:52:57 +08003797 goto out_unlock;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003798 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003799
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003800 read = trace_get_user(&parser, ubuf, cnt, ppos);
3801
Li Zefan4ba79782009-09-22 13:52:20 +08003802 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003803 parser.buffer[parser.idx] = 0;
3804
3805 /* we allow only one expression at a time */
Li Zefana4ec5e02009-09-18 14:06:28 +08003806 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003807 parser.buffer);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003808 if (ret)
Li Zefan1eb90f12009-09-22 13:52:57 +08003809 goto out_free;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003810 }
3811
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003812 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08003813
3814out_free:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003815 trace_parser_put(&parser);
Li Zefan1eb90f12009-09-22 13:52:57 +08003816out_unlock:
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003817 mutex_unlock(&graph_lock);
3818
3819 return ret;
3820}
3821
3822static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08003823 .open = ftrace_graph_open,
3824 .read = seq_read,
3825 .write = ftrace_graph_write,
Namhyung Kim6a76f8c2013-04-11 15:55:01 +09003826 .llseek = ftrace_filter_lseek,
Li Zefan87827112009-07-23 11:29:11 +08003827 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003828};
3829#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3830
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003831static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02003832{
Steven Rostedt5072c592008-05-12 21:20:43 +02003833
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003834 trace_create_file("available_filter_functions", 0444,
3835 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02003836
Steven Rostedt647bcd02011-05-03 14:39:21 -04003837 trace_create_file("enabled_functions", 0444,
3838 d_tracer, NULL, &ftrace_enabled_fops);
3839
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003840 trace_create_file("set_ftrace_filter", 0644, d_tracer,
3841 NULL, &ftrace_filter_fops);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003842
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003843 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003844 NULL, &ftrace_notrace_fops);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04003845
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003846#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003847 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003848 NULL,
3849 &ftrace_graph_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003850#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3851
Steven Rostedt5072c592008-05-12 21:20:43 +02003852 return 0;
3853}
3854
Steven Rostedt9fd49322012-04-24 22:32:06 -04003855static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05003856{
Steven Rostedt9fd49322012-04-24 22:32:06 -04003857 const unsigned long *ipa = a;
3858 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05003859
Steven Rostedt9fd49322012-04-24 22:32:06 -04003860 if (*ipa > *ipb)
3861 return 1;
3862 if (*ipa < *ipb)
3863 return -1;
3864 return 0;
3865}
3866
3867static void ftrace_swap_ips(void *a, void *b, int size)
3868{
3869 unsigned long *ipa = a;
3870 unsigned long *ipb = b;
3871 unsigned long t;
3872
3873 t = *ipa;
3874 *ipa = *ipb;
3875 *ipb = t;
Steven Rostedt68950612011-12-16 17:06:45 -05003876}
3877
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003878static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08003879 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003880 unsigned long *end)
3881{
Steven Rostedt706c81f2012-04-24 23:45:26 -04003882 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05003883 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04003884 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05003885 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003886 unsigned long *p;
3887 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04003888 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05003889 int ret = -ENOMEM;
3890
3891 count = end - start;
3892
3893 if (!count)
3894 return 0;
3895
Steven Rostedt9fd49322012-04-24 22:32:06 -04003896 sort(start, count, sizeof(*start),
3897 ftrace_cmp_ips, ftrace_swap_ips);
3898
Steven Rostedt706c81f2012-04-24 23:45:26 -04003899 start_pg = ftrace_allocate_pages(count);
3900 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05003901 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003902
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003903 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05003904
Steven Rostedt32082302011-12-16 14:42:37 -05003905 /*
3906 * Core and each module needs their own pages, as
3907 * modules will free them when they are removed.
3908 * Force a new page to be allocated for modules.
3909 */
Steven Rostedta7900872011-12-16 16:23:44 -05003910 if (!mod) {
3911 WARN_ON(ftrace_pages || ftrace_pages_start);
3912 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04003913 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05003914 } else {
Steven Rostedt32082302011-12-16 14:42:37 -05003915 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05003916 goto out;
Steven Rostedt32082302011-12-16 14:42:37 -05003917
Steven Rostedta7900872011-12-16 16:23:44 -05003918 if (WARN_ON(ftrace_pages->next)) {
3919 /* Hmm, we have free pages? */
3920 while (ftrace_pages->next)
3921 ftrace_pages = ftrace_pages->next;
Steven Rostedt32082302011-12-16 14:42:37 -05003922 }
Steven Rostedta7900872011-12-16 16:23:44 -05003923
Steven Rostedt706c81f2012-04-24 23:45:26 -04003924 ftrace_pages->next = start_pg;
Steven Rostedt32082302011-12-16 14:42:37 -05003925 }
3926
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003927 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04003928 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003929 while (p < end) {
3930 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08003931 /*
3932 * Some architecture linkers will pad between
3933 * the different mcount_loc sections of different
3934 * object files to satisfy alignments.
3935 * Skip any NULL pointers.
3936 */
3937 if (!addr)
3938 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04003939
3940 if (pg->index == pg->size) {
3941 /* We should have allocated enough */
3942 if (WARN_ON(!pg->next))
3943 break;
3944 pg = pg->next;
3945 }
3946
3947 rec = &pg->records[pg->index++];
3948 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003949 }
3950
Steven Rostedt706c81f2012-04-24 23:45:26 -04003951 /* We should have used all pages */
3952 WARN_ON(pg->next);
3953
3954 /* Assign the last page to ftrace_pages */
3955 ftrace_pages = pg;
3956
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003957 /* These new locations need to be initialized */
Steven Rostedt706c81f2012-04-24 23:45:26 -04003958 ftrace_new_pgs = start_pg;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003959
Steven Rostedta4f18ed2011-06-07 09:26:46 -04003960 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04003961 * We only need to disable interrupts on start up
3962 * because we are modifying code that an interrupt
3963 * may execute, and the modification is not atomic.
3964 * But for modules, nothing runs the code we modify
3965 * until we are finished with it, and there's no
3966 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04003967 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04003968 if (!mod)
3969 local_irq_save(flags);
Steven Rostedt31e88902008-11-14 16:21:19 -08003970 ftrace_update_code(mod);
Steven Rostedt4376cac2011-06-24 23:28:13 -04003971 if (!mod)
3972 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05003973 ret = 0;
3974 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003975 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003976
Steven Rostedta7900872011-12-16 16:23:44 -05003977 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003978}
3979
Steven Rostedt93eb6772009-04-15 13:24:06 -04003980#ifdef CONFIG_MODULES
Steven Rostedt32082302011-12-16 14:42:37 -05003981
3982#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
3983
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003984void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04003985{
3986 struct dyn_ftrace *rec;
Steven Rostedt32082302011-12-16 14:42:37 -05003987 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04003988 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05003989 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04003990
Steven Rostedt93eb6772009-04-15 13:24:06 -04003991 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003992
3993 if (ftrace_disabled)
3994 goto out_unlock;
3995
Steven Rostedt32082302011-12-16 14:42:37 -05003996 /*
3997 * Each module has its own ftrace_pages, remove
3998 * them from the list.
3999 */
4000 last_pg = &ftrace_pages_start;
4001 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
4002 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004003 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04004004 /*
Steven Rostedt32082302011-12-16 14:42:37 -05004005 * As core pages are first, the first
4006 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04004007 */
Steven Rostedt32082302011-12-16 14:42:37 -05004008 if (WARN_ON(pg == ftrace_pages_start))
4009 goto out_unlock;
4010
4011 /* Check if we are deleting the last page */
4012 if (pg == ftrace_pages)
4013 ftrace_pages = next_to_ftrace_page(last_pg);
4014
4015 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05004016 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
4017 free_pages((unsigned long)pg->records, order);
4018 kfree(pg);
Steven Rostedt32082302011-12-16 14:42:37 -05004019 } else
4020 last_pg = &pg->next;
4021 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04004022 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04004023 mutex_unlock(&ftrace_lock);
4024}
4025
4026static void ftrace_init_module(struct module *mod,
4027 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04004028{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04004029 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04004030 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004031 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04004032}
4033
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004034static int ftrace_module_notify_enter(struct notifier_block *self,
4035 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004036{
4037 struct module *mod = data;
4038
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004039 if (val == MODULE_STATE_COMING)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004040 ftrace_init_module(mod, mod->ftrace_callsites,
4041 mod->ftrace_callsites +
4042 mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004043 return 0;
4044}
4045
4046static int ftrace_module_notify_exit(struct notifier_block *self,
4047 unsigned long val, void *data)
4048{
4049 struct module *mod = data;
4050
4051 if (val == MODULE_STATE_GOING)
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004052 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04004053
4054 return 0;
4055}
4056#else
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004057static int ftrace_module_notify_enter(struct notifier_block *self,
4058 unsigned long val, void *data)
4059{
4060 return 0;
4061}
4062static int ftrace_module_notify_exit(struct notifier_block *self,
4063 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004064{
4065 return 0;
4066}
4067#endif /* CONFIG_MODULES */
4068
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004069struct notifier_block ftrace_module_enter_nb = {
4070 .notifier_call = ftrace_module_notify_enter,
Steven Rostedtc1bf08a2012-12-14 09:48:15 -05004071 .priority = INT_MAX, /* Run before anything that can use kprobes */
Steven Rostedt93eb6772009-04-15 13:24:06 -04004072};
4073
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004074struct notifier_block ftrace_module_exit_nb = {
4075 .notifier_call = ftrace_module_notify_exit,
4076 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4077};
4078
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004079extern unsigned long __start_mcount_loc[];
4080extern unsigned long __stop_mcount_loc[];
4081
4082void __init ftrace_init(void)
4083{
4084 unsigned long count, addr, flags;
4085 int ret;
4086
4087 /* Keep the ftrace pointer to the stub */
4088 addr = (unsigned long)ftrace_stub;
4089
4090 local_irq_save(flags);
4091 ftrace_dyn_arch_init(&addr);
4092 local_irq_restore(flags);
4093
4094 /* ftrace_dyn_arch_init places the return code in addr */
4095 if (addr)
4096 goto failed;
4097
4098 count = __stop_mcount_loc - __start_mcount_loc;
4099
4100 ret = ftrace_dyn_table_alloc(count);
4101 if (ret)
4102 goto failed;
4103
4104 last_ftrace_enabled = ftrace_enabled = 1;
4105
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004106 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08004107 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004108 __stop_mcount_loc);
4109
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004110 ret = register_module_notifier(&ftrace_module_enter_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08004111 if (ret)
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004112 pr_warning("Failed to register trace ftrace module enter notifier\n");
4113
4114 ret = register_module_notifier(&ftrace_module_exit_nb);
4115 if (ret)
4116 pr_warning("Failed to register trace ftrace module exit notifier\n");
Steven Rostedt93eb6772009-04-15 13:24:06 -04004117
Steven Rostedt2af15d62009-05-28 13:37:24 -04004118 set_ftrace_early_filters();
4119
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004120 return;
4121 failed:
4122 ftrace_disabled = 1;
4123}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004124
Steven Rostedt3d083392008-05-12 21:20:42 +02004125#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004126
Steven Rostedt2b499382011-05-03 22:49:52 -04004127static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04004128 .func = ftrace_stub,
Steven Rostedt47409742012-07-20 11:04:44 -04004129 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Steven Rostedtbd69c302011-05-03 21:55:54 -04004130};
4131
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004132static int __init ftrace_nodyn_init(void)
4133{
4134 ftrace_enabled = 1;
4135 return 0;
4136}
Steven Rostedt6f415672012-10-05 12:13:07 -04004137core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004138
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004139static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4140static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05004141/* Keep as macros so we do not need to define the commands */
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04004142# define ftrace_startup(ops, command) \
4143 ({ \
4144 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4145 0; \
4146 })
Steven Rostedtbd69c302011-05-03 21:55:54 -04004147# define ftrace_shutdown(ops, command) do { } while (0)
Ingo Molnarc7aafc52008-05-12 21:20:45 +02004148# define ftrace_startup_sysctl() do { } while (0)
4149# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04004150
4151static inline int
4152ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
4153{
4154 return 1;
4155}
4156
Steven Rostedt3d083392008-05-12 21:20:42 +02004157#endif /* CONFIG_DYNAMIC_FTRACE */
4158
Steven Rostedtb8489142011-05-04 09:27:52 -04004159static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004160ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004161 struct ftrace_ops *op, struct pt_regs *regs)
Jiri Olsae2484912012-02-15 15:51:48 +01004162{
Jiri Olsae2484912012-02-15 15:51:48 +01004163 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4164 return;
4165
4166 /*
4167 * Some of the ops may be dynamically allocated,
4168 * they must be freed after a synchronize_sched().
4169 */
4170 preempt_disable_notrace();
4171 trace_recursion_set(TRACE_CONTROL_BIT);
Steven Rostedt0a016402012-11-02 17:03:03 -04004172 do_for_each_ftrace_op(op, ftrace_control_list) {
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -04004173 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
4174 !ftrace_function_local_disabled(op) &&
Jiri Olsae2484912012-02-15 15:51:48 +01004175 ftrace_ops_test(op, ip))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004176 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04004177 } while_for_each_ftrace_op(op);
Jiri Olsae2484912012-02-15 15:51:48 +01004178 trace_recursion_clear(TRACE_CONTROL_BIT);
4179 preempt_enable_notrace();
4180}
4181
4182static struct ftrace_ops control_ops = {
4183 .func = ftrace_ops_control_func,
Steven Rostedt47409742012-07-20 11:04:44 -04004184 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Jiri Olsae2484912012-02-15 15:51:48 +01004185};
4186
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004187static inline void
4188__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004189 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004190{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004191 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004192 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04004193
Steven Rostedtccf36722012-06-05 09:44:25 -04004194 if (function_trace_stop)
4195 return;
4196
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004197 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4198 if (bit < 0)
4199 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04004200
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004201 /*
4202 * Some of the ops may be dynamically allocated,
4203 * they must be freed after a synchronize_sched().
4204 */
4205 preempt_disable_notrace();
Steven Rostedt0a016402012-11-02 17:03:03 -04004206 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedtb8489142011-05-04 09:27:52 -04004207 if (ftrace_ops_test(op, ip))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004208 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04004209 } while_for_each_ftrace_op(op);
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004210 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004211 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04004212}
4213
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004214/*
4215 * Some archs only support passing ip and parent_ip. Even though
4216 * the list function ignores the op parameter, we do not want any
4217 * C side effects, where a function is called without the caller
4218 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004219 * Archs are to support both the regs and ftrace_ops at the same time.
4220 * If they support ftrace_ops, it is assumed they support regs.
4221 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09004222 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4223 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004224 * An architecture can pass partial regs with ftrace_ops and still
4225 * set the ARCH_SUPPORT_FTARCE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004226 */
4227#if ARCH_SUPPORTS_FTRACE_OPS
4228static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004229 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004230{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004231 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004232}
4233#else
4234static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4235{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004236 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004237}
4238#endif
4239
Steven Rostedte32d8952008-12-04 00:26:41 -05004240static void clear_ftrace_swapper(void)
4241{
4242 struct task_struct *p;
4243 int cpu;
4244
4245 get_online_cpus();
4246 for_each_online_cpu(cpu) {
4247 p = idle_task(cpu);
4248 clear_tsk_trace_trace(p);
4249 }
4250 put_online_cpus();
4251}
4252
4253static void set_ftrace_swapper(void)
4254{
4255 struct task_struct *p;
4256 int cpu;
4257
4258 get_online_cpus();
4259 for_each_online_cpu(cpu) {
4260 p = idle_task(cpu);
4261 set_tsk_trace_trace(p);
4262 }
4263 put_online_cpus();
4264}
4265
4266static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004267{
4268 struct task_struct *p;
4269
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004270 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05004271 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05004272 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05004273 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004274 rcu_read_unlock();
4275
Steven Rostedte32d8952008-12-04 00:26:41 -05004276 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004277}
4278
Steven Rostedte32d8952008-12-04 00:26:41 -05004279static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004280{
4281 struct task_struct *p;
4282
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004283 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004284 do_each_pid_task(pid, PIDTYPE_PID, p) {
4285 set_tsk_trace_trace(p);
4286 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004287 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004288}
4289
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004290static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004291{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004292 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004293 clear_ftrace_swapper();
4294 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004295 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05004296}
4297
4298static void set_ftrace_pid_task(struct pid *pid)
4299{
4300 if (pid == ftrace_swapper_pid)
4301 set_ftrace_swapper();
4302 else
4303 set_ftrace_pid(pid);
4304}
4305
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004306static int ftrace_pid_add(int p)
4307{
4308 struct pid *pid;
4309 struct ftrace_pid *fpid;
4310 int ret = -EINVAL;
4311
4312 mutex_lock(&ftrace_lock);
4313
4314 if (!p)
4315 pid = ftrace_swapper_pid;
4316 else
4317 pid = find_get_pid(p);
4318
4319 if (!pid)
4320 goto out;
4321
4322 ret = 0;
4323
4324 list_for_each_entry(fpid, &ftrace_pids, list)
4325 if (fpid->pid == pid)
4326 goto out_put;
4327
4328 ret = -ENOMEM;
4329
4330 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4331 if (!fpid)
4332 goto out_put;
4333
4334 list_add(&fpid->list, &ftrace_pids);
4335 fpid->pid = pid;
4336
4337 set_ftrace_pid_task(pid);
4338
4339 ftrace_update_pid_func();
4340 ftrace_startup_enable(0);
4341
4342 mutex_unlock(&ftrace_lock);
4343 return 0;
4344
4345out_put:
4346 if (pid != ftrace_swapper_pid)
4347 put_pid(pid);
4348
4349out:
4350 mutex_unlock(&ftrace_lock);
4351 return ret;
4352}
4353
4354static void ftrace_pid_reset(void)
4355{
4356 struct ftrace_pid *fpid, *safe;
4357
4358 mutex_lock(&ftrace_lock);
4359 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4360 struct pid *pid = fpid->pid;
4361
4362 clear_ftrace_pid_task(pid);
4363
4364 list_del(&fpid->list);
4365 kfree(fpid);
4366 }
4367
4368 ftrace_update_pid_func();
4369 ftrace_startup_enable(0);
4370
4371 mutex_unlock(&ftrace_lock);
4372}
4373
4374static void *fpid_start(struct seq_file *m, loff_t *pos)
4375{
4376 mutex_lock(&ftrace_lock);
4377
4378 if (list_empty(&ftrace_pids) && (!*pos))
4379 return (void *) 1;
4380
4381 return seq_list_start(&ftrace_pids, *pos);
4382}
4383
4384static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4385{
4386 if (v == (void *)1)
4387 return NULL;
4388
4389 return seq_list_next(v, &ftrace_pids, pos);
4390}
4391
4392static void fpid_stop(struct seq_file *m, void *p)
4393{
4394 mutex_unlock(&ftrace_lock);
4395}
4396
4397static int fpid_show(struct seq_file *m, void *v)
4398{
4399 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4400
4401 if (v == (void *)1) {
4402 seq_printf(m, "no pid\n");
4403 return 0;
4404 }
4405
4406 if (fpid->pid == ftrace_swapper_pid)
4407 seq_printf(m, "swapper tasks\n");
4408 else
4409 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4410
4411 return 0;
4412}
4413
4414static const struct seq_operations ftrace_pid_sops = {
4415 .start = fpid_start,
4416 .next = fpid_next,
4417 .stop = fpid_stop,
4418 .show = fpid_show,
4419};
4420
4421static int
4422ftrace_pid_open(struct inode *inode, struct file *file)
4423{
4424 int ret = 0;
4425
4426 if ((file->f_mode & FMODE_WRITE) &&
4427 (file->f_flags & O_TRUNC))
4428 ftrace_pid_reset();
4429
4430 if (file->f_mode & FMODE_READ)
4431 ret = seq_open(file, &ftrace_pid_sops);
4432
4433 return ret;
4434}
4435
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004436static ssize_t
4437ftrace_pid_write(struct file *filp, const char __user *ubuf,
4438 size_t cnt, loff_t *ppos)
4439{
Ingo Molnar457dc922009-11-23 11:03:28 +01004440 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004441 long val;
4442 int ret;
4443
4444 if (cnt >= sizeof(buf))
4445 return -EINVAL;
4446
4447 if (copy_from_user(&buf, ubuf, cnt))
4448 return -EFAULT;
4449
4450 buf[cnt] = 0;
4451
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004452 /*
4453 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4454 * to clean the filter quietly.
4455 */
Ingo Molnar457dc922009-11-23 11:03:28 +01004456 tmp = strstrip(buf);
4457 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004458 return 1;
4459
Daniel Walterbcd83ea2012-09-26 22:08:38 +02004460 ret = kstrtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004461 if (ret < 0)
4462 return ret;
4463
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004464 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004465
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004466 return ret ? ret : cnt;
4467}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004468
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004469static int
4470ftrace_pid_release(struct inode *inode, struct file *file)
4471{
4472 if (file->f_mode & FMODE_READ)
4473 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004474
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004475 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004476}
4477
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004478static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004479 .open = ftrace_pid_open,
4480 .write = ftrace_pid_write,
4481 .read = seq_read,
Namhyung Kim6a76f8c2013-04-11 15:55:01 +09004482 .llseek = ftrace_filter_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004483 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004484};
4485
4486static __init int ftrace_init_debugfs(void)
4487{
4488 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004489
4490 d_tracer = tracing_init_dentry();
4491 if (!d_tracer)
4492 return 0;
4493
4494 ftrace_init_dyn_debugfs(d_tracer);
4495
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004496 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4497 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04004498
4499 ftrace_profile_debugfs(d_tracer);
4500
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004501 return 0;
4502}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004503fs_initcall(ftrace_init_debugfs);
4504
Steven Rostedt3d083392008-05-12 21:20:42 +02004505/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004506 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004507 *
4508 * This function should be used by panic code. It stops ftrace
4509 * but in a not so nice way. If you need to simply kill ftrace
4510 * from a non-atomic section, use ftrace_kill.
4511 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004512void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004513{
4514 ftrace_disabled = 1;
4515 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004516 clear_ftrace_function();
4517}
4518
4519/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04004520 * Test if ftrace is dead or not.
4521 */
4522int ftrace_is_dead(void)
4523{
4524 return ftrace_disabled;
4525}
4526
4527/**
Steven Rostedt3d083392008-05-12 21:20:42 +02004528 * register_ftrace_function - register a function for profiling
4529 * @ops - ops structure that holds the function for profiling.
4530 *
4531 * Register a function to be called by all functions in the
4532 * kernel.
4533 *
4534 * Note: @ops->func and all the functions it calls must be labeled
4535 * with "notrace", otherwise it will go into a
4536 * recursive loop.
4537 */
4538int register_ftrace_function(struct ftrace_ops *ops)
4539{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004540 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004541
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004542 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004543
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004544 ret = __register_ftrace_function(ops);
Steven Rostedtb8489142011-05-04 09:27:52 -04004545 if (!ret)
Steven Rostedta1cd6172011-05-23 15:24:25 -04004546 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04004547
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004548 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02004549
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004550 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02004551}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004552EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02004553
4554/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01004555 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02004556 * @ops - ops structure that holds the function to unregister
4557 *
4558 * Unregister a function that was added to be called by ftrace profiling.
4559 */
4560int unregister_ftrace_function(struct ftrace_ops *ops)
4561{
4562 int ret;
4563
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004564 mutex_lock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004565 ret = __unregister_ftrace_function(ops);
Steven Rostedtb8489142011-05-04 09:27:52 -04004566 if (!ret)
4567 ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004568 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004569
4570 return ret;
4571}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004572EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004573
Ingo Molnare309b412008-05-12 21:20:51 +02004574int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004575ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004576 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004577 loff_t *ppos)
4578{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004579 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004580
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004581 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004582
Steven Rostedt45a4a232011-04-21 23:16:46 -04004583 if (unlikely(ftrace_disabled))
4584 goto out;
4585
4586 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004587
Li Zefana32c7762009-06-26 16:55:51 +08004588 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004589 goto out;
4590
Li Zefana32c7762009-06-26 16:55:51 +08004591 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004592
4593 if (ftrace_enabled) {
4594
4595 ftrace_startup_sysctl();
4596
4597 /* we are starting ftrace again */
Jan Kiszka5000c412013-03-26 17:53:03 +01004598 if (ftrace_ops_list != &ftrace_list_end)
4599 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004600
4601 } else {
4602 /* stopping ftrace calls (just send to ftrace_stub) */
4603 ftrace_trace_function = ftrace_stub;
4604
4605 ftrace_shutdown_sysctl();
4606 }
4607
4608 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004609 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004610 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02004611}
Ingo Molnarf17845e2008-10-24 12:47:10 +02004612
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004613#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004614
Steven Rostedt597af812009-04-03 15:24:12 -04004615static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004616static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004617
Steven Rostedte49dc192008-12-02 23:50:05 -05004618int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4619{
4620 return 0;
4621}
4622
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004623/* The callbacks that hook a function */
4624trace_func_graph_ret_t ftrace_graph_return =
4625 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004626trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004627
4628/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4629static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4630{
4631 int i;
4632 int ret = 0;
4633 unsigned long flags;
4634 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4635 struct task_struct *g, *t;
4636
4637 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4638 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4639 * sizeof(struct ftrace_ret_stack),
4640 GFP_KERNEL);
4641 if (!ret_stack_list[i]) {
4642 start = 0;
4643 end = i;
4644 ret = -ENOMEM;
4645 goto free;
4646 }
4647 }
4648
4649 read_lock_irqsave(&tasklist_lock, flags);
4650 do_each_thread(g, t) {
4651 if (start == end) {
4652 ret = -EAGAIN;
4653 goto unlock;
4654 }
4655
4656 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01004657 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004658 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04004659 t->curr_ret_stack = -1;
4660 /* Make sure the tasks see the -1 first: */
4661 smp_wmb();
4662 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004663 }
4664 } while_each_thread(g, t);
4665
4666unlock:
4667 read_unlock_irqrestore(&tasklist_lock, flags);
4668free:
4669 for (i = start; i < end; i++)
4670 kfree(ret_stack_list[i]);
4671 return ret;
4672}
4673
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004674static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04004675ftrace_graph_probe_sched_switch(void *ignore,
4676 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004677{
4678 unsigned long long timestamp;
4679 int index;
4680
Steven Rostedtbe6f1642009-03-24 11:06:24 -04004681 /*
4682 * Does the user want to count the time a function was asleep.
4683 * If so, do not update the time stamps.
4684 */
4685 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4686 return;
4687
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004688 timestamp = trace_clock_local();
4689
4690 prev->ftrace_timestamp = timestamp;
4691
4692 /* only process tasks that we timestamped */
4693 if (!next->ftrace_timestamp)
4694 return;
4695
4696 /*
4697 * Update all the counters in next to make up for the
4698 * time next was sleeping.
4699 */
4700 timestamp -= next->ftrace_timestamp;
4701
4702 for (index = next->curr_ret_stack; index >= 0; index--)
4703 next->ret_stack[index].calltime += timestamp;
4704}
4705
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004706/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004707static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004708{
4709 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004710 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004711
4712 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4713 sizeof(struct ftrace_ret_stack *),
4714 GFP_KERNEL);
4715
4716 if (!ret_stack_list)
4717 return -ENOMEM;
4718
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004719 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04004720 for_each_online_cpu(cpu) {
4721 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05004722 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04004723 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004724
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004725 do {
4726 ret = alloc_retstack_tasklist(ret_stack_list);
4727 } while (ret == -EAGAIN);
4728
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004729 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04004730 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004731 if (ret)
4732 pr_info("ftrace_graph: Couldn't activate tracepoint"
4733 " probe to kernel_sched_switch\n");
4734 }
4735
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004736 kfree(ret_stack_list);
4737 return ret;
4738}
4739
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004740/*
4741 * Hibernation protection.
4742 * The state of the current task is too much unstable during
4743 * suspend/restore to disk. We want to protect against that.
4744 */
4745static int
4746ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4747 void *unused)
4748{
4749 switch (state) {
4750 case PM_HIBERNATION_PREPARE:
4751 pause_graph_tracing();
4752 break;
4753
4754 case PM_POST_HIBERNATION:
4755 unpause_graph_tracing();
4756 break;
4757 }
4758 return NOTIFY_DONE;
4759}
4760
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004761int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4762 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004763{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004764 int ret = 0;
4765
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004766 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004767
Steven Rostedt05ce5812009-03-24 00:18:31 -04004768 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04004769 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04004770 ret = -EBUSY;
4771 goto out;
4772 }
4773
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004774 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
4775 register_pm_notifier(&ftrace_suspend_notifier);
4776
Steven Rostedt597af812009-04-03 15:24:12 -04004777 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004778 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004779 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04004780 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004781 goto out;
4782 }
Steven Rostedte53a6312008-11-26 00:16:25 -05004783
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004784 ftrace_graph_return = retfunc;
4785 ftrace_graph_entry = entryfunc;
Steven Rostedte53a6312008-11-26 00:16:25 -05004786
Steven Rostedta1cd6172011-05-23 15:24:25 -04004787 ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004788
4789out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004790 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004791 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004792}
4793
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004794void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004795{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004796 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004797
Steven Rostedt597af812009-04-03 15:24:12 -04004798 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004799 goto out;
4800
Steven Rostedt597af812009-04-03 15:24:12 -04004801 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004802 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004803 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedtbd69c302011-05-03 21:55:54 -04004804 ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004805 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04004806 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004807
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004808 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004809 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004810}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004811
Steven Rostedt868baf02011-02-10 21:26:13 -05004812static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
4813
4814static void
4815graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
4816{
4817 atomic_set(&t->tracing_graph_pause, 0);
4818 atomic_set(&t->trace_overrun, 0);
4819 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004820 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05004821 smp_wmb();
4822 t->ret_stack = ret_stack;
4823}
4824
4825/*
4826 * Allocate a return stack for the idle task. May be the first
4827 * time through, or it may be done by CPU hotplug online.
4828 */
4829void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
4830{
4831 t->curr_ret_stack = -1;
4832 /*
4833 * The idle task has no parent, it either has its own
4834 * stack or no stack at all.
4835 */
4836 if (t->ret_stack)
4837 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
4838
4839 if (ftrace_graph_active) {
4840 struct ftrace_ret_stack *ret_stack;
4841
4842 ret_stack = per_cpu(idle_ret_stack, cpu);
4843 if (!ret_stack) {
4844 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4845 * sizeof(struct ftrace_ret_stack),
4846 GFP_KERNEL);
4847 if (!ret_stack)
4848 return;
4849 per_cpu(idle_ret_stack, cpu) = ret_stack;
4850 }
4851 graph_init_task(t, ret_stack);
4852 }
4853}
4854
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004855/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004856void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004857{
Steven Rostedt84047e32009-06-02 16:51:55 -04004858 /* Make sure we do not use the parent ret_stack */
4859 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05004860 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04004861
Steven Rostedt597af812009-04-03 15:24:12 -04004862 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04004863 struct ftrace_ret_stack *ret_stack;
4864
4865 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004866 * sizeof(struct ftrace_ret_stack),
4867 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04004868 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004869 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05004870 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04004871 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004872}
4873
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004874void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004875{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004876 struct ftrace_ret_stack *ret_stack = t->ret_stack;
4877
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004878 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004879 /* NULL must become visible to IRQs before we free it: */
4880 barrier();
4881
4882 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004883}
Steven Rostedt14a866c2008-12-02 23:50:02 -05004884
4885void ftrace_graph_stop(void)
4886{
4887 ftrace_stop();
4888}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004889#endif