blob: affc35d829cc94589930cffffc745fd550476916 [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_bits __read_mostly;
490static int ftrace_profile_enabled __read_mostly;
491
492/* ftrace_profile_lock - synchronize the enable and disable of the profiler */
Steven Rostedt493762f2009-03-23 17:12:36 -0400493static DEFINE_MUTEX(ftrace_profile_lock);
494
Steven Rostedtcafb1682009-03-24 20:50:39 -0400495static DEFINE_PER_CPU(struct ftrace_profile_stat, ftrace_profile_stats);
Steven Rostedt493762f2009-03-23 17:12:36 -0400496
497#define FTRACE_PROFILE_HASH_SIZE 1024 /* must be power of 2 */
498
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
679 for (i = 0; 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 Rostedtcafb1682009-03-24 20:50:39 -0400727 if (!ftrace_profile_bits) {
728 size--;
Steven Rostedt493762f2009-03-23 17:12:36 -0400729
Steven Rostedtcafb1682009-03-24 20:50:39 -0400730 for (; size; size >>= 1)
731 ftrace_profile_bits++;
732 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400733
Steven Rostedt318e0a72009-03-25 20:06:34 -0400734 /* Preallocate the function profiling pages */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400735 if (ftrace_profile_pages_init(stat) < 0) {
736 kfree(stat->hash);
737 stat->hash = NULL;
Steven Rostedt493762f2009-03-23 17:12:36 -0400738 return -ENOMEM;
739 }
740
741 return 0;
742}
743
Steven Rostedtcafb1682009-03-24 20:50:39 -0400744static int ftrace_profile_init(void)
745{
746 int cpu;
747 int ret = 0;
748
749 for_each_online_cpu(cpu) {
750 ret = ftrace_profile_init_cpu(cpu);
751 if (ret)
752 break;
753 }
754
755 return ret;
756}
757
Steven Rostedt493762f2009-03-23 17:12:36 -0400758/* interrupts must be disabled */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400759static struct ftrace_profile *
760ftrace_find_profiled_func(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400761{
762 struct ftrace_profile *rec;
763 struct hlist_head *hhd;
Steven Rostedt493762f2009-03-23 17:12:36 -0400764 unsigned long key;
765
766 key = hash_long(ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400767 hhd = &stat->hash[key];
Steven Rostedt493762f2009-03-23 17:12:36 -0400768
769 if (hlist_empty(hhd))
770 return NULL;
771
Sasha Levinb67bfe02013-02-27 17:06:00 -0800772 hlist_for_each_entry_rcu(rec, hhd, node) {
Steven Rostedt493762f2009-03-23 17:12:36 -0400773 if (rec->ip == ip)
774 return rec;
775 }
776
777 return NULL;
778}
779
Steven Rostedtcafb1682009-03-24 20:50:39 -0400780static void ftrace_add_profile(struct ftrace_profile_stat *stat,
781 struct ftrace_profile *rec)
Steven Rostedt493762f2009-03-23 17:12:36 -0400782{
783 unsigned long key;
784
785 key = hash_long(rec->ip, ftrace_profile_bits);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400786 hlist_add_head_rcu(&rec->node, &stat->hash[key]);
Steven Rostedt493762f2009-03-23 17:12:36 -0400787}
788
Steven Rostedt318e0a72009-03-25 20:06:34 -0400789/*
790 * The memory is already allocated, this simply finds a new record to use.
791 */
Steven Rostedt493762f2009-03-23 17:12:36 -0400792static struct ftrace_profile *
Steven Rostedt318e0a72009-03-25 20:06:34 -0400793ftrace_profile_alloc(struct ftrace_profile_stat *stat, unsigned long ip)
Steven Rostedt493762f2009-03-23 17:12:36 -0400794{
795 struct ftrace_profile *rec = NULL;
796
Steven Rostedt318e0a72009-03-25 20:06:34 -0400797 /* prevent recursion (from NMIs) */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400798 if (atomic_inc_return(&stat->disabled) != 1)
Steven Rostedt493762f2009-03-23 17:12:36 -0400799 goto out;
800
Steven Rostedt493762f2009-03-23 17:12:36 -0400801 /*
Steven Rostedt318e0a72009-03-25 20:06:34 -0400802 * Try to find the function again since an NMI
803 * could have added it
Steven Rostedt493762f2009-03-23 17:12:36 -0400804 */
Steven Rostedtcafb1682009-03-24 20:50:39 -0400805 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400806 if (rec)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400807 goto out;
Steven Rostedt493762f2009-03-23 17:12:36 -0400808
Steven Rostedtcafb1682009-03-24 20:50:39 -0400809 if (stat->pages->index == PROFILES_PER_PAGE) {
810 if (!stat->pages->next)
811 goto out;
812 stat->pages = stat->pages->next;
Steven Rostedt493762f2009-03-23 17:12:36 -0400813 }
814
Steven Rostedtcafb1682009-03-24 20:50:39 -0400815 rec = &stat->pages->records[stat->pages->index++];
Steven Rostedt493762f2009-03-23 17:12:36 -0400816 rec->ip = ip;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400817 ftrace_add_profile(stat, rec);
Steven Rostedt493762f2009-03-23 17:12:36 -0400818
Steven Rostedt493762f2009-03-23 17:12:36 -0400819 out:
Steven Rostedtcafb1682009-03-24 20:50:39 -0400820 atomic_dec(&stat->disabled);
Steven Rostedt493762f2009-03-23 17:12:36 -0400821
822 return rec;
823}
824
Steven Rostedt493762f2009-03-23 17:12:36 -0400825static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -0400826function_profile_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -0400827 struct ftrace_ops *ops, struct pt_regs *regs)
Steven Rostedt493762f2009-03-23 17:12:36 -0400828{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400829 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -0400830 struct ftrace_profile *rec;
831 unsigned long flags;
Steven Rostedt493762f2009-03-23 17:12:36 -0400832
833 if (!ftrace_profile_enabled)
834 return;
835
Steven Rostedt493762f2009-03-23 17:12:36 -0400836 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400837
838 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400839 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400840 goto out;
841
842 rec = ftrace_find_profiled_func(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400843 if (!rec) {
Steven Rostedt318e0a72009-03-25 20:06:34 -0400844 rec = ftrace_profile_alloc(stat, ip);
Steven Rostedt493762f2009-03-23 17:12:36 -0400845 if (!rec)
846 goto out;
847 }
848
849 rec->counter++;
850 out:
851 local_irq_restore(flags);
852}
853
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400854#ifdef CONFIG_FUNCTION_GRAPH_TRACER
855static int profile_graph_entry(struct ftrace_graph_ent *trace)
856{
Steven Rostedta1e2e312011-08-09 12:50:46 -0400857 function_profile_call(trace->func, 0, NULL, NULL);
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400858 return 1;
859}
860
861static void profile_graph_return(struct ftrace_graph_ret *trace)
862{
Steven Rostedtcafb1682009-03-24 20:50:39 -0400863 struct ftrace_profile_stat *stat;
Steven Rostedta2a16d62009-03-24 23:17:58 -0400864 unsigned long long calltime;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400865 struct ftrace_profile *rec;
Steven Rostedtcafb1682009-03-24 20:50:39 -0400866 unsigned long flags;
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400867
868 local_irq_save(flags);
Steven Rostedtcafb1682009-03-24 20:50:39 -0400869 stat = &__get_cpu_var(ftrace_profile_stats);
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400870 if (!stat->hash || !ftrace_profile_enabled)
Steven Rostedtcafb1682009-03-24 20:50:39 -0400871 goto out;
872
Steven Rostedt37e44bc2010-04-27 21:04:24 -0400873 /* If the calltime was zero'd ignore it */
874 if (!trace->calltime)
875 goto out;
876
Steven Rostedta2a16d62009-03-24 23:17:58 -0400877 calltime = trace->rettime - trace->calltime;
878
879 if (!(trace_flags & TRACE_ITER_GRAPH_TIME)) {
880 int index;
881
882 index = trace->depth;
883
884 /* Append this call time to the parent time to subtract */
885 if (index)
886 current->ret_stack[index - 1].subtime += calltime;
887
888 if (current->ret_stack[index].subtime < calltime)
889 calltime -= current->ret_stack[index].subtime;
890 else
891 calltime = 0;
892 }
893
Steven Rostedtcafb1682009-03-24 20:50:39 -0400894 rec = ftrace_find_profiled_func(stat, trace->func);
Chase Douglase330b3b2010-04-26 14:02:05 -0400895 if (rec) {
Steven Rostedta2a16d62009-03-24 23:17:58 -0400896 rec->time += calltime;
Chase Douglase330b3b2010-04-26 14:02:05 -0400897 rec->time_squared += calltime * calltime;
898 }
Steven Rostedta2a16d62009-03-24 23:17:58 -0400899
Steven Rostedtcafb1682009-03-24 20:50:39 -0400900 out:
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400901 local_irq_restore(flags);
902}
903
904static int register_ftrace_profiler(void)
905{
906 return register_ftrace_graph(&profile_graph_return,
907 &profile_graph_entry);
908}
909
910static void unregister_ftrace_profiler(void)
911{
912 unregister_ftrace_graph();
913}
914#else
Paul McQuadebd38c0e2011-05-31 20:51:55 +0100915static struct ftrace_ops ftrace_profile_ops __read_mostly = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400916 .func = function_profile_call,
Steven Rostedt47409742012-07-20 11:04:44 -0400917 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Steven Rostedt493762f2009-03-23 17:12:36 -0400918};
919
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400920static int register_ftrace_profiler(void)
921{
922 return register_ftrace_function(&ftrace_profile_ops);
923}
924
925static void unregister_ftrace_profiler(void)
926{
927 unregister_ftrace_function(&ftrace_profile_ops);
928}
929#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
930
Steven Rostedt493762f2009-03-23 17:12:36 -0400931static ssize_t
932ftrace_profile_write(struct file *filp, const char __user *ubuf,
933 size_t cnt, loff_t *ppos)
934{
935 unsigned long val;
Steven Rostedt493762f2009-03-23 17:12:36 -0400936 int ret;
937
Peter Huewe22fe9b52011-06-07 21:58:27 +0200938 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
939 if (ret)
Steven Rostedt493762f2009-03-23 17:12:36 -0400940 return ret;
941
942 val = !!val;
943
944 mutex_lock(&ftrace_profile_lock);
945 if (ftrace_profile_enabled ^ val) {
946 if (val) {
947 ret = ftrace_profile_init();
948 if (ret < 0) {
949 cnt = ret;
950 goto out;
951 }
952
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400953 ret = register_ftrace_profiler();
954 if (ret < 0) {
955 cnt = ret;
956 goto out;
957 }
Steven Rostedt493762f2009-03-23 17:12:36 -0400958 ftrace_profile_enabled = 1;
959 } else {
960 ftrace_profile_enabled = 0;
Steven Rostedt0f6ce3d2009-06-01 21:51:28 -0400961 /*
962 * unregister_ftrace_profiler calls stop_machine
963 * so this acts like an synchronize_sched.
964 */
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400965 unregister_ftrace_profiler();
Steven Rostedt493762f2009-03-23 17:12:36 -0400966 }
967 }
968 out:
969 mutex_unlock(&ftrace_profile_lock);
970
Jiri Olsacf8517c2009-10-23 19:36:16 -0400971 *ppos += cnt;
Steven Rostedt493762f2009-03-23 17:12:36 -0400972
973 return cnt;
974}
975
976static ssize_t
977ftrace_profile_read(struct file *filp, char __user *ubuf,
978 size_t cnt, loff_t *ppos)
979{
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400980 char buf[64]; /* big enough to hold a number */
Steven Rostedt493762f2009-03-23 17:12:36 -0400981 int r;
982
983 r = sprintf(buf, "%u\n", ftrace_profile_enabled);
984 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
985}
986
987static const struct file_operations ftrace_profile_fops = {
988 .open = tracing_open_generic,
989 .read = ftrace_profile_read,
990 .write = ftrace_profile_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200991 .llseek = default_llseek,
Steven Rostedt493762f2009-03-23 17:12:36 -0400992};
993
Steven Rostedtcafb1682009-03-24 20:50:39 -0400994/* used to initialize the real stat files */
995static struct tracer_stat function_stats __initdata = {
Steven Rostedtfb9fb012009-03-25 13:26:41 -0400996 .name = "functions",
997 .stat_start = function_stat_start,
998 .stat_next = function_stat_next,
999 .stat_cmp = function_stat_cmp,
1000 .stat_headers = function_stat_headers,
1001 .stat_show = function_stat_show
Steven Rostedtcafb1682009-03-24 20:50:39 -04001002};
1003
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001004static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001005{
Steven Rostedtcafb1682009-03-24 20:50:39 -04001006 struct ftrace_profile_stat *stat;
Steven Rostedt493762f2009-03-23 17:12:36 -04001007 struct dentry *entry;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001008 char *name;
Steven Rostedt493762f2009-03-23 17:12:36 -04001009 int ret;
Steven Rostedtcafb1682009-03-24 20:50:39 -04001010 int cpu;
Steven Rostedt493762f2009-03-23 17:12:36 -04001011
Steven Rostedtcafb1682009-03-24 20:50:39 -04001012 for_each_possible_cpu(cpu) {
1013 stat = &per_cpu(ftrace_profile_stats, cpu);
1014
1015 /* allocate enough for function name + cpu number */
1016 name = kmalloc(32, GFP_KERNEL);
1017 if (!name) {
1018 /*
1019 * The files created are permanent, if something happens
1020 * we still do not free memory.
1021 */
Steven Rostedtcafb1682009-03-24 20:50:39 -04001022 WARN(1,
1023 "Could not allocate stat file for cpu %d\n",
1024 cpu);
1025 return;
1026 }
1027 stat->stat = function_stats;
1028 snprintf(name, 32, "function%d", cpu);
1029 stat->stat.name = name;
1030 ret = register_stat_tracer(&stat->stat);
1031 if (ret) {
1032 WARN(1,
1033 "Could not register function stat for cpu %d\n",
1034 cpu);
1035 kfree(name);
1036 return;
1037 }
Steven Rostedt493762f2009-03-23 17:12:36 -04001038 }
1039
1040 entry = debugfs_create_file("function_profile_enabled", 0644,
1041 d_tracer, NULL, &ftrace_profile_fops);
1042 if (!entry)
1043 pr_warning("Could not create debugfs "
1044 "'function_profile_enabled' entry\n");
1045}
1046
1047#else /* CONFIG_FUNCTION_PROFILER */
Steven Rostedt6ab5d662009-06-04 00:55:45 -04001048static __init void ftrace_profile_debugfs(struct dentry *d_tracer)
Steven Rostedt493762f2009-03-23 17:12:36 -04001049{
1050}
1051#endif /* CONFIG_FUNCTION_PROFILER */
1052
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001053static struct pid * const ftrace_swapper_pid = &init_struct_pid;
1054
Steven Rostedt3d083392008-05-12 21:20:42 +02001055#ifdef CONFIG_DYNAMIC_FTRACE
Ingo Molnar73d3fd92009-02-17 11:48:18 +01001056
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001057#ifndef CONFIG_FTRACE_MCOUNT_RECORD
Steven Rostedtcb7be3b2008-10-23 09:33:05 -04001058# error Dynamic ftrace depends on MCOUNT_RECORD
Steven Rostedt99ecdc42008-08-15 21:40:05 -04001059#endif
1060
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001061static struct hlist_head ftrace_func_hash[FTRACE_FUNC_HASHSIZE] __read_mostly;
1062
Steven Rostedtb6887d72009-02-17 12:32:04 -05001063struct ftrace_func_probe {
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001064 struct hlist_node node;
Steven Rostedtb6887d72009-02-17 12:32:04 -05001065 struct ftrace_probe_ops *ops;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05001066 unsigned long flags;
1067 unsigned long ip;
1068 void *data;
1069 struct rcu_head rcu;
1070};
1071
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001072struct ftrace_func_entry {
1073 struct hlist_node hlist;
1074 unsigned long ip;
1075};
1076
1077struct ftrace_hash {
1078 unsigned long size_bits;
1079 struct hlist_head *buckets;
1080 unsigned long count;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001081 struct rcu_head rcu;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001082};
1083
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001084/*
1085 * We make these constant because no one should touch them,
1086 * but they are used as the default "empty hash", to avoid allocating
1087 * it all the time. These are in a read only section such that if
1088 * anyone does try to modify it, it will cause an exception.
1089 */
1090static const struct hlist_head empty_buckets[1];
1091static const struct ftrace_hash empty_hash = {
1092 .buckets = (struct hlist_head *)empty_buckets,
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001093};
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001094#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02001095
Steven Rostedt2b499382011-05-03 22:49:52 -04001096static struct ftrace_ops global_ops = {
Steven Rostedtf45948e2011-05-02 12:29:25 -04001097 .func = ftrace_stub,
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001098 .notrace_hash = EMPTY_HASH,
1099 .filter_hash = EMPTY_HASH,
Steven Rostedt47409742012-07-20 11:04:44 -04001100 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Steven Rostedtf45948e2011-05-02 12:29:25 -04001101};
1102
Steven Rostedt41c52c02008-05-22 11:46:33 -04001103static DEFINE_MUTEX(ftrace_regex_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02001104
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001105struct ftrace_page {
1106 struct ftrace_page *next;
Steven Rostedta7900872011-12-16 16:23:44 -05001107 struct dyn_ftrace *records;
Steven Rostedt431aa3f2009-01-06 12:43:01 -05001108 int index;
Steven Rostedta7900872011-12-16 16:23:44 -05001109 int size;
David Milleraa5e5ce2008-05-13 22:06:56 -07001110};
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001111
Steven Rostedt85ae32a2011-12-16 16:30:31 -05001112static struct ftrace_page *ftrace_new_pgs;
1113
Steven Rostedta7900872011-12-16 16:23:44 -05001114#define ENTRY_SIZE sizeof(struct dyn_ftrace)
1115#define ENTRIES_PER_PAGE (PAGE_SIZE / ENTRY_SIZE)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001116
1117/* estimate from running different kernels */
1118#define NR_TO_INIT 10000
1119
1120static struct ftrace_page *ftrace_pages_start;
1121static struct ftrace_page *ftrace_pages;
1122
Steven Rostedt06a51d92011-12-19 19:07:36 -05001123static bool ftrace_hash_empty(struct ftrace_hash *hash)
1124{
1125 return !hash || !hash->count;
1126}
1127
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001128static struct ftrace_func_entry *
1129ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip)
1130{
1131 unsigned long key;
1132 struct ftrace_func_entry *entry;
1133 struct hlist_head *hhd;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001134
Steven Rostedt06a51d92011-12-19 19:07:36 -05001135 if (ftrace_hash_empty(hash))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001136 return NULL;
1137
1138 if (hash->size_bits > 0)
1139 key = hash_long(ip, hash->size_bits);
1140 else
1141 key = 0;
1142
1143 hhd = &hash->buckets[key];
1144
Sasha Levinb67bfe02013-02-27 17:06:00 -08001145 hlist_for_each_entry_rcu(entry, hhd, hlist) {
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001146 if (entry->ip == ip)
1147 return entry;
1148 }
1149 return NULL;
1150}
1151
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001152static void __add_hash_entry(struct ftrace_hash *hash,
1153 struct ftrace_func_entry *entry)
1154{
1155 struct hlist_head *hhd;
1156 unsigned long key;
1157
1158 if (hash->size_bits)
1159 key = hash_long(entry->ip, hash->size_bits);
1160 else
1161 key = 0;
1162
1163 hhd = &hash->buckets[key];
1164 hlist_add_head(&entry->hlist, hhd);
1165 hash->count++;
1166}
1167
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001168static int add_hash_entry(struct ftrace_hash *hash, unsigned long ip)
1169{
1170 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001171
1172 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
1173 if (!entry)
1174 return -ENOMEM;
1175
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001176 entry->ip = ip;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001177 __add_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001178
1179 return 0;
1180}
1181
1182static void
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001183free_hash_entry(struct ftrace_hash *hash,
1184 struct ftrace_func_entry *entry)
1185{
1186 hlist_del(&entry->hlist);
1187 kfree(entry);
1188 hash->count--;
1189}
1190
1191static void
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001192remove_hash_entry(struct ftrace_hash *hash,
1193 struct ftrace_func_entry *entry)
1194{
1195 hlist_del(&entry->hlist);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001196 hash->count--;
1197}
1198
1199static void ftrace_hash_clear(struct ftrace_hash *hash)
1200{
1201 struct hlist_head *hhd;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001202 struct hlist_node *tn;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001203 struct ftrace_func_entry *entry;
1204 int size = 1 << hash->size_bits;
1205 int i;
1206
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001207 if (!hash->count)
1208 return;
1209
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001210 for (i = 0; i < size; i++) {
1211 hhd = &hash->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001212 hlist_for_each_entry_safe(entry, tn, hhd, hlist)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001213 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04001214 }
1215 FTRACE_WARN_ON(hash->count);
1216}
1217
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001218static void free_ftrace_hash(struct ftrace_hash *hash)
1219{
1220 if (!hash || hash == EMPTY_HASH)
1221 return;
1222 ftrace_hash_clear(hash);
1223 kfree(hash->buckets);
1224 kfree(hash);
1225}
1226
Steven Rostedt07fd5512011-05-05 18:03:47 -04001227static void __free_ftrace_hash_rcu(struct rcu_head *rcu)
1228{
1229 struct ftrace_hash *hash;
1230
1231 hash = container_of(rcu, struct ftrace_hash, rcu);
1232 free_ftrace_hash(hash);
1233}
1234
1235static void free_ftrace_hash_rcu(struct ftrace_hash *hash)
1236{
1237 if (!hash || hash == EMPTY_HASH)
1238 return;
1239 call_rcu_sched(&hash->rcu, __free_ftrace_hash_rcu);
1240}
1241
Jiri Olsa5500fa52012-02-15 15:51:54 +01001242void ftrace_free_filter(struct ftrace_ops *ops)
1243{
1244 free_ftrace_hash(ops->filter_hash);
1245 free_ftrace_hash(ops->notrace_hash);
1246}
1247
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001248static struct ftrace_hash *alloc_ftrace_hash(int size_bits)
1249{
1250 struct ftrace_hash *hash;
1251 int size;
1252
1253 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
1254 if (!hash)
1255 return NULL;
1256
1257 size = 1 << size_bits;
Thomas Meyer47b0edc2011-11-29 22:08:00 +01001258 hash->buckets = kcalloc(size, sizeof(*hash->buckets), GFP_KERNEL);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001259
1260 if (!hash->buckets) {
1261 kfree(hash);
1262 return NULL;
1263 }
1264
1265 hash->size_bits = size_bits;
1266
1267 return hash;
1268}
1269
1270static struct ftrace_hash *
1271alloc_and_copy_ftrace_hash(int size_bits, struct ftrace_hash *hash)
1272{
1273 struct ftrace_func_entry *entry;
1274 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001275 int size;
1276 int ret;
1277 int i;
1278
1279 new_hash = alloc_ftrace_hash(size_bits);
1280 if (!new_hash)
1281 return NULL;
1282
1283 /* Empty hash? */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001284 if (ftrace_hash_empty(hash))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001285 return new_hash;
1286
1287 size = 1 << hash->size_bits;
1288 for (i = 0; i < size; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001289 hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001290 ret = add_hash_entry(new_hash, entry->ip);
1291 if (ret < 0)
1292 goto free_hash;
1293 }
1294 }
1295
1296 FTRACE_WARN_ON(new_hash->count != hash->count);
1297
1298 return new_hash;
1299
1300 free_hash:
1301 free_ftrace_hash(new_hash);
1302 return NULL;
1303}
1304
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001305static void
1306ftrace_hash_rec_disable(struct ftrace_ops *ops, int filter_hash);
1307static void
1308ftrace_hash_rec_enable(struct ftrace_ops *ops, int filter_hash);
1309
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001310static int
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001311ftrace_hash_move(struct ftrace_ops *ops, int enable,
1312 struct ftrace_hash **dst, struct ftrace_hash *src)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001313{
1314 struct ftrace_func_entry *entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001315 struct hlist_node *tn;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001316 struct hlist_head *hhd;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001317 struct ftrace_hash *old_hash;
1318 struct ftrace_hash *new_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001319 unsigned long key;
1320 int size = src->count;
1321 int bits = 0;
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001322 int ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001323 int i;
1324
1325 /*
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001326 * Remove the current set, update the hash and add
1327 * them back.
1328 */
1329 ftrace_hash_rec_disable(ops, enable);
1330
1331 /*
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001332 * If the new source is empty, just free dst and assign it
1333 * the empty_hash.
1334 */
1335 if (!src->count) {
Steven Rostedt07fd5512011-05-05 18:03:47 -04001336 free_ftrace_hash_rcu(*dst);
1337 rcu_assign_pointer(*dst, EMPTY_HASH);
Steven Rostedtd4d34b92011-11-04 20:32:39 -04001338 /* still need to update the function records */
1339 ret = 0;
1340 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001341 }
1342
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001343 /*
1344 * Make the hash size about 1/2 the # found
1345 */
1346 for (size /= 2; size; size >>= 1)
1347 bits++;
1348
1349 /* Don't allocate too much */
1350 if (bits > FTRACE_HASH_MAX_BITS)
1351 bits = FTRACE_HASH_MAX_BITS;
1352
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001353 ret = -ENOMEM;
Steven Rostedt07fd5512011-05-05 18:03:47 -04001354 new_hash = alloc_ftrace_hash(bits);
1355 if (!new_hash)
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001356 goto out;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001357
1358 size = 1 << src->size_bits;
1359 for (i = 0; i < size; i++) {
1360 hhd = &src->buckets[i];
Sasha Levinb67bfe02013-02-27 17:06:00 -08001361 hlist_for_each_entry_safe(entry, tn, hhd, hlist) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001362 if (bits > 0)
1363 key = hash_long(entry->ip, bits);
1364 else
1365 key = 0;
1366 remove_hash_entry(src, entry);
Steven Rostedt07fd5512011-05-05 18:03:47 -04001367 __add_hash_entry(new_hash, entry);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001368 }
1369 }
1370
Steven Rostedt07fd5512011-05-05 18:03:47 -04001371 old_hash = *dst;
1372 rcu_assign_pointer(*dst, new_hash);
1373 free_ftrace_hash_rcu(old_hash);
1374
Steven Rostedt41fb61c2011-07-13 15:03:44 -04001375 ret = 0;
1376 out:
1377 /*
1378 * Enable regardless of ret:
1379 * On success, we enable the new hash.
1380 * On failure, we re-enable the original hash.
1381 */
1382 ftrace_hash_rec_enable(ops, enable);
1383
1384 return ret;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04001385}
1386
Steven Rostedt265c8312009-02-13 12:43:56 -05001387/*
Steven Rostedtb8489142011-05-04 09:27:52 -04001388 * Test the hashes for this ops to see if we want to call
1389 * the ops->func or not.
1390 *
1391 * It's a match if the ip is in the ops->filter_hash or
1392 * the filter_hash does not exist or is empty,
1393 * AND
1394 * the ip is not in the ops->notrace_hash.
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04001395 *
1396 * This needs to be called with preemption disabled as
1397 * the hashes are freed with call_rcu_sched().
Steven Rostedtb8489142011-05-04 09:27:52 -04001398 */
1399static int
1400ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
1401{
1402 struct ftrace_hash *filter_hash;
1403 struct ftrace_hash *notrace_hash;
1404 int ret;
1405
Steven Rostedtb8489142011-05-04 09:27:52 -04001406 filter_hash = rcu_dereference_raw(ops->filter_hash);
1407 notrace_hash = rcu_dereference_raw(ops->notrace_hash);
1408
Steven Rostedt06a51d92011-12-19 19:07:36 -05001409 if ((ftrace_hash_empty(filter_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001410 ftrace_lookup_ip(filter_hash, ip)) &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001411 (ftrace_hash_empty(notrace_hash) ||
Steven Rostedtb8489142011-05-04 09:27:52 -04001412 !ftrace_lookup_ip(notrace_hash, ip)))
1413 ret = 1;
1414 else
1415 ret = 0;
Steven Rostedtb8489142011-05-04 09:27:52 -04001416
1417 return ret;
1418}
1419
1420/*
Steven Rostedt265c8312009-02-13 12:43:56 -05001421 * This is a double for. Do not use 'break' to break out of the loop,
1422 * you must use a goto.
1423 */
1424#define do_for_each_ftrace_rec(pg, rec) \
1425 for (pg = ftrace_pages_start; pg; pg = pg->next) { \
1426 int _____i; \
1427 for (_____i = 0; _____i < pg->index; _____i++) { \
1428 rec = &pg->records[_____i];
1429
1430#define while_for_each_ftrace_rec() \
1431 } \
1432 }
Abhishek Sagarecea6562008-06-21 23:47:53 +05301433
Steven Rostedt5855fea2011-12-16 19:27:42 -05001434
1435static int ftrace_cmp_recs(const void *a, const void *b)
1436{
Steven Rostedta650e022012-04-25 13:48:13 -04001437 const struct dyn_ftrace *key = a;
1438 const struct dyn_ftrace *rec = b;
Steven Rostedt5855fea2011-12-16 19:27:42 -05001439
Steven Rostedta650e022012-04-25 13:48:13 -04001440 if (key->flags < rec->ip)
Steven Rostedt5855fea2011-12-16 19:27:42 -05001441 return -1;
Steven Rostedta650e022012-04-25 13:48:13 -04001442 if (key->ip >= rec->ip + MCOUNT_INSN_SIZE)
1443 return 1;
1444 return 0;
1445}
1446
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001447static unsigned long ftrace_location_range(unsigned long start, unsigned long end)
Steven Rostedta650e022012-04-25 13:48:13 -04001448{
1449 struct ftrace_page *pg;
1450 struct dyn_ftrace *rec;
1451 struct dyn_ftrace key;
1452
1453 key.ip = start;
1454 key.flags = end; /* overload flags, as it is unsigned long */
1455
1456 for (pg = ftrace_pages_start; pg; pg = pg->next) {
1457 if (end < pg->records[0].ip ||
1458 start >= (pg->records[pg->index - 1].ip + MCOUNT_INSN_SIZE))
1459 continue;
1460 rec = bsearch(&key, pg->records, pg->index,
1461 sizeof(struct dyn_ftrace),
1462 ftrace_cmp_recs);
1463 if (rec)
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001464 return rec->ip;
Steven Rostedta650e022012-04-25 13:48:13 -04001465 }
1466
Steven Rostedt5855fea2011-12-16 19:27:42 -05001467 return 0;
1468}
1469
Steven Rostedtc88fd862011-08-16 09:53:39 -04001470/**
1471 * ftrace_location - return true if the ip giving is a traced location
1472 * @ip: the instruction pointer to check
1473 *
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001474 * Returns rec->ip if @ip given is a pointer to a ftrace location.
Steven Rostedtc88fd862011-08-16 09:53:39 -04001475 * That is, the instruction that is either a NOP or call to
1476 * the function tracer. It checks the ftrace internal tables to
1477 * determine if the address belongs or not.
1478 */
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001479unsigned long ftrace_location(unsigned long ip)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001480{
Steven Rostedta650e022012-04-25 13:48:13 -04001481 return ftrace_location_range(ip, ip);
1482}
Steven Rostedtc88fd862011-08-16 09:53:39 -04001483
Steven Rostedta650e022012-04-25 13:48:13 -04001484/**
1485 * ftrace_text_reserved - return true if range contains an ftrace location
1486 * @start: start of range to search
1487 * @end: end of range to search (inclusive). @end points to the last byte to check.
1488 *
1489 * Returns 1 if @start and @end contains a ftrace location.
1490 * That is, the instruction that is either a NOP or call to
1491 * the function tracer. It checks the ftrace internal tables to
1492 * determine if the address belongs or not.
1493 */
1494int ftrace_text_reserved(void *start, void *end)
1495{
Steven Rostedtf0cf9732012-04-25 14:39:54 -04001496 unsigned long ret;
1497
1498 ret = ftrace_location_range((unsigned long)start,
1499 (unsigned long)end);
1500
1501 return (int)!!ret;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001502}
1503
Steven Rostedted926f92011-05-03 13:25:24 -04001504static void __ftrace_hash_rec_update(struct ftrace_ops *ops,
1505 int filter_hash,
1506 bool inc)
1507{
1508 struct ftrace_hash *hash;
1509 struct ftrace_hash *other_hash;
1510 struct ftrace_page *pg;
1511 struct dyn_ftrace *rec;
1512 int count = 0;
1513 int all = 0;
1514
1515 /* Only update if the ops has been registered */
1516 if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
1517 return;
1518
1519 /*
1520 * In the filter_hash case:
1521 * If the count is zero, we update all records.
1522 * Otherwise we just update the items in the hash.
1523 *
1524 * In the notrace_hash case:
1525 * We enable the update in the hash.
1526 * As disabling notrace means enabling the tracing,
1527 * and enabling notrace means disabling, the inc variable
1528 * gets inversed.
1529 */
1530 if (filter_hash) {
1531 hash = ops->filter_hash;
1532 other_hash = ops->notrace_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05001533 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001534 all = 1;
1535 } else {
1536 inc = !inc;
1537 hash = ops->notrace_hash;
1538 other_hash = ops->filter_hash;
1539 /*
1540 * If the notrace hash has no items,
1541 * then there's nothing to do.
1542 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05001543 if (ftrace_hash_empty(hash))
Steven Rostedted926f92011-05-03 13:25:24 -04001544 return;
1545 }
1546
1547 do_for_each_ftrace_rec(pg, rec) {
1548 int in_other_hash = 0;
1549 int in_hash = 0;
1550 int match = 0;
1551
1552 if (all) {
1553 /*
1554 * Only the filter_hash affects all records.
1555 * Update if the record is not in the notrace hash.
1556 */
Steven Rostedtb8489142011-05-04 09:27:52 -04001557 if (!other_hash || !ftrace_lookup_ip(other_hash, rec->ip))
Steven Rostedted926f92011-05-03 13:25:24 -04001558 match = 1;
1559 } else {
Steven Rostedt06a51d92011-12-19 19:07:36 -05001560 in_hash = !!ftrace_lookup_ip(hash, rec->ip);
1561 in_other_hash = !!ftrace_lookup_ip(other_hash, rec->ip);
Steven Rostedted926f92011-05-03 13:25:24 -04001562
1563 /*
1564 *
1565 */
1566 if (filter_hash && in_hash && !in_other_hash)
1567 match = 1;
1568 else if (!filter_hash && in_hash &&
Steven Rostedt06a51d92011-12-19 19:07:36 -05001569 (in_other_hash || ftrace_hash_empty(other_hash)))
Steven Rostedted926f92011-05-03 13:25:24 -04001570 match = 1;
1571 }
1572 if (!match)
1573 continue;
1574
1575 if (inc) {
1576 rec->flags++;
1577 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == FTRACE_REF_MAX))
1578 return;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001579 /*
1580 * If any ops wants regs saved for this function
1581 * then all ops will get saved regs.
1582 */
1583 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
1584 rec->flags |= FTRACE_FL_REGS;
Steven Rostedted926f92011-05-03 13:25:24 -04001585 } else {
1586 if (FTRACE_WARN_ON((rec->flags & ~FTRACE_FL_MASK) == 0))
1587 return;
1588 rec->flags--;
1589 }
1590 count++;
1591 /* Shortcut, if we handled all records, we are done. */
1592 if (!all && count == hash->count)
1593 return;
1594 } while_for_each_ftrace_rec();
1595}
1596
1597static void ftrace_hash_rec_disable(struct ftrace_ops *ops,
1598 int filter_hash)
1599{
1600 __ftrace_hash_rec_update(ops, filter_hash, 0);
1601}
1602
1603static void ftrace_hash_rec_enable(struct ftrace_ops *ops,
1604 int filter_hash)
1605{
1606 __ftrace_hash_rec_update(ops, filter_hash, 1);
1607}
1608
Steven Rostedt05736a42008-09-22 14:55:47 -07001609static void print_ip_ins(const char *fmt, unsigned char *p)
1610{
1611 int i;
1612
1613 printk(KERN_CONT "%s", fmt);
1614
1615 for (i = 0; i < MCOUNT_INSN_SIZE; i++)
1616 printk(KERN_CONT "%s%02x", i ? ":" : "", p[i]);
1617}
1618
Steven Rostedtc88fd862011-08-16 09:53:39 -04001619/**
1620 * ftrace_bug - report and shutdown function tracer
1621 * @failed: The failed type (EFAULT, EINVAL, EPERM)
1622 * @ip: The address that failed
1623 *
1624 * The arch code that enables or disables the function tracing
1625 * can call ftrace_bug() when it has detected a problem in
1626 * modifying the code. @failed should be one of either:
1627 * EFAULT - if the problem happens on reading the @ip address
1628 * EINVAL - if what is read at @ip is not what was expected
1629 * EPERM - if the problem happens on writting to the @ip address
1630 */
1631void ftrace_bug(int failed, unsigned long ip)
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001632{
1633 switch (failed) {
1634 case -EFAULT:
1635 FTRACE_WARN_ON_ONCE(1);
1636 pr_info("ftrace faulted on modifying ");
1637 print_ip_sym(ip);
1638 break;
1639 case -EINVAL:
1640 FTRACE_WARN_ON_ONCE(1);
1641 pr_info("ftrace failed to modify ");
1642 print_ip_sym(ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001643 print_ip_ins(" actual: ", (unsigned char *)ip);
Steven Rostedtb17e8a32008-11-14 16:21:19 -08001644 printk(KERN_CONT "\n");
1645 break;
1646 case -EPERM:
1647 FTRACE_WARN_ON_ONCE(1);
1648 pr_info("ftrace faulted on writing ");
1649 print_ip_sym(ip);
1650 break;
1651 default:
1652 FTRACE_WARN_ON_ONCE(1);
1653 pr_info("ftrace faulted on unknown error ");
1654 print_ip_sym(ip);
1655 }
1656}
1657
Steven Rostedtc88fd862011-08-16 09:53:39 -04001658static int ftrace_check_record(struct dyn_ftrace *rec, int enable, int update)
Steven Rostedt5072c592008-05-12 21:20:43 +02001659{
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001660 unsigned long flag = 0UL;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01001661
Steven Rostedt982c3502008-11-15 16:31:41 -05001662 /*
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001663 * If we are updating calls:
Steven Rostedt982c3502008-11-15 16:31:41 -05001664 *
Steven Rostedted926f92011-05-03 13:25:24 -04001665 * If the record has a ref count, then we need to enable it
1666 * because someone is using it.
Steven Rostedt982c3502008-11-15 16:31:41 -05001667 *
Steven Rostedted926f92011-05-03 13:25:24 -04001668 * Otherwise we make sure its disabled.
1669 *
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01001670 * If we are disabling calls, then disable all records that
Steven Rostedted926f92011-05-03 13:25:24 -04001671 * are enabled.
Steven Rostedt982c3502008-11-15 16:31:41 -05001672 */
Steven Rostedtc88fd862011-08-16 09:53:39 -04001673 if (enable && (rec->flags & ~FTRACE_FL_MASK))
Steven Rostedted926f92011-05-03 13:25:24 -04001674 flag = FTRACE_FL_ENABLED;
Steven Rostedt5072c592008-05-12 21:20:43 +02001675
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001676 /*
1677 * If enabling and the REGS flag does not match the REGS_EN, then
1678 * do not ignore this record. Set flags to fail the compare against
1679 * ENABLED.
1680 */
1681 if (flag &&
1682 (!(rec->flags & FTRACE_FL_REGS) != !(rec->flags & FTRACE_FL_REGS_EN)))
1683 flag |= FTRACE_FL_REGS;
1684
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001685 /* If the state of this record hasn't changed, then do nothing */
1686 if ((rec->flags & FTRACE_FL_ENABLED) == flag)
Steven Rostedtc88fd862011-08-16 09:53:39 -04001687 return FTRACE_UPDATE_IGNORE;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001688
1689 if (flag) {
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001690 /* Save off if rec is being enabled (for return value) */
1691 flag ^= rec->flags & FTRACE_FL_ENABLED;
1692
1693 if (update) {
Steven Rostedtc88fd862011-08-16 09:53:39 -04001694 rec->flags |= FTRACE_FL_ENABLED;
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001695 if (flag & FTRACE_FL_REGS) {
1696 if (rec->flags & FTRACE_FL_REGS)
1697 rec->flags |= FTRACE_FL_REGS_EN;
1698 else
1699 rec->flags &= ~FTRACE_FL_REGS_EN;
1700 }
1701 }
1702
1703 /*
1704 * If this record is being updated from a nop, then
1705 * return UPDATE_MAKE_CALL.
1706 * Otherwise, if the EN flag is set, then return
1707 * UPDATE_MODIFY_CALL_REGS to tell the caller to convert
1708 * from the non-save regs, to a save regs function.
1709 * Otherwise,
1710 * return UPDATE_MODIFY_CALL to tell the caller to convert
1711 * from the save regs, to a non-save regs function.
1712 */
1713 if (flag & FTRACE_FL_ENABLED)
1714 return FTRACE_UPDATE_MAKE_CALL;
1715 else if (rec->flags & FTRACE_FL_REGS_EN)
1716 return FTRACE_UPDATE_MODIFY_CALL_REGS;
1717 else
1718 return FTRACE_UPDATE_MODIFY_CALL;
Xiao Guangrong64fbcd12009-07-15 12:32:15 +08001719 }
1720
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001721 if (update) {
1722 /* If there's no more users, clear all flags */
1723 if (!(rec->flags & ~FTRACE_FL_MASK))
1724 rec->flags = 0;
1725 else
1726 /* Just disable the record (keep REGS state) */
1727 rec->flags &= ~FTRACE_FL_ENABLED;
1728 }
Steven Rostedtc88fd862011-08-16 09:53:39 -04001729
1730 return FTRACE_UPDATE_MAKE_NOP;
1731}
1732
1733/**
1734 * ftrace_update_record, set a record that now is tracing or not
1735 * @rec: the record to update
1736 * @enable: set to 1 if the record is tracing, zero to force disable
1737 *
1738 * The records that represent all functions that can be traced need
1739 * to be updated when tracing has been enabled.
1740 */
1741int ftrace_update_record(struct dyn_ftrace *rec, int enable)
1742{
1743 return ftrace_check_record(rec, enable, 1);
1744}
1745
1746/**
1747 * ftrace_test_record, check if the record has been enabled or not
1748 * @rec: the record to test
1749 * @enable: set to 1 to check if enabled, 0 if it is disabled
1750 *
1751 * The arch code may need to test if a record is already set to
1752 * tracing to determine how to modify the function code that it
1753 * represents.
1754 */
1755int ftrace_test_record(struct dyn_ftrace *rec, int enable)
1756{
1757 return ftrace_check_record(rec, enable, 0);
1758}
1759
1760static int
1761__ftrace_replace_code(struct dyn_ftrace *rec, int enable)
1762{
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001763 unsigned long ftrace_old_addr;
Steven Rostedtc88fd862011-08-16 09:53:39 -04001764 unsigned long ftrace_addr;
1765 int ret;
1766
Steven Rostedtc88fd862011-08-16 09:53:39 -04001767 ret = ftrace_update_record(rec, enable);
1768
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001769 if (rec->flags & FTRACE_FL_REGS)
1770 ftrace_addr = (unsigned long)FTRACE_REGS_ADDR;
1771 else
1772 ftrace_addr = (unsigned long)FTRACE_ADDR;
1773
Steven Rostedtc88fd862011-08-16 09:53:39 -04001774 switch (ret) {
1775 case FTRACE_UPDATE_IGNORE:
1776 return 0;
1777
1778 case FTRACE_UPDATE_MAKE_CALL:
1779 return ftrace_make_call(rec, ftrace_addr);
1780
1781 case FTRACE_UPDATE_MAKE_NOP:
1782 return ftrace_make_nop(NULL, rec, ftrace_addr);
Steven Rostedt08f6fba2012-04-30 16:20:23 -04001783
1784 case FTRACE_UPDATE_MODIFY_CALL_REGS:
1785 case FTRACE_UPDATE_MODIFY_CALL:
1786 if (rec->flags & FTRACE_FL_REGS)
1787 ftrace_old_addr = (unsigned long)FTRACE_ADDR;
1788 else
1789 ftrace_old_addr = (unsigned long)FTRACE_REGS_ADDR;
1790
1791 return ftrace_modify_call(rec, ftrace_old_addr, ftrace_addr);
Steven Rostedtc88fd862011-08-16 09:53:39 -04001792 }
1793
1794 return -1; /* unknow ftrace bug */
Steven Rostedt5072c592008-05-12 21:20:43 +02001795}
1796
Steven Rostedte4f5d542012-04-27 09:13:18 -04001797void __weak ftrace_replace_code(int enable)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001798{
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001799 struct dyn_ftrace *rec;
1800 struct ftrace_page *pg;
Steven Rostedt6a24a242009-02-17 11:20:26 -05001801 int failed;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001802
Steven Rostedt45a4a232011-04-21 23:16:46 -04001803 if (unlikely(ftrace_disabled))
1804 return;
1805
Steven Rostedt265c8312009-02-13 12:43:56 -05001806 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedte4f5d542012-04-27 09:13:18 -04001807 failed = __ftrace_replace_code(rec, enable);
Zhaoleifa9d13c2009-03-13 17:16:34 +08001808 if (failed) {
Steven Rostedt3279ba32009-10-07 16:57:56 -04001809 ftrace_bug(failed, rec->ip);
1810 /* Stop processing */
1811 return;
Steven Rostedt265c8312009-02-13 12:43:56 -05001812 }
1813 } while_for_each_ftrace_rec();
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001814}
1815
Steven Rostedtc88fd862011-08-16 09:53:39 -04001816struct ftrace_rec_iter {
1817 struct ftrace_page *pg;
1818 int index;
1819};
1820
1821/**
1822 * ftrace_rec_iter_start, start up iterating over traced functions
1823 *
1824 * Returns an iterator handle that is used to iterate over all
1825 * the records that represent address locations where functions
1826 * are traced.
1827 *
1828 * May return NULL if no records are available.
1829 */
1830struct ftrace_rec_iter *ftrace_rec_iter_start(void)
1831{
1832 /*
1833 * We only use a single iterator.
1834 * Protected by the ftrace_lock mutex.
1835 */
1836 static struct ftrace_rec_iter ftrace_rec_iter;
1837 struct ftrace_rec_iter *iter = &ftrace_rec_iter;
1838
1839 iter->pg = ftrace_pages_start;
1840 iter->index = 0;
1841
1842 /* Could have empty pages */
1843 while (iter->pg && !iter->pg->index)
1844 iter->pg = iter->pg->next;
1845
1846 if (!iter->pg)
1847 return NULL;
1848
1849 return iter;
1850}
1851
1852/**
1853 * ftrace_rec_iter_next, get the next record to process.
1854 * @iter: The handle to the iterator.
1855 *
1856 * Returns the next iterator after the given iterator @iter.
1857 */
1858struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter)
1859{
1860 iter->index++;
1861
1862 if (iter->index >= iter->pg->index) {
1863 iter->pg = iter->pg->next;
1864 iter->index = 0;
1865
1866 /* Could have empty pages */
1867 while (iter->pg && !iter->pg->index)
1868 iter->pg = iter->pg->next;
1869 }
1870
1871 if (!iter->pg)
1872 return NULL;
1873
1874 return iter;
1875}
1876
1877/**
1878 * ftrace_rec_iter_record, get the record at the iterator location
1879 * @iter: The current iterator location
1880 *
1881 * Returns the record that the current @iter is at.
1882 */
1883struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter)
1884{
1885 return &iter->pg->records[iter->index];
1886}
1887
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301888static int
Steven Rostedt31e88902008-11-14 16:21:19 -08001889ftrace_code_disable(struct module *mod, struct dyn_ftrace *rec)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001890{
1891 unsigned long ip;
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001892 int ret;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001893
1894 ip = rec->ip;
1895
Steven Rostedt45a4a232011-04-21 23:16:46 -04001896 if (unlikely(ftrace_disabled))
1897 return 0;
1898
Shaohua Li25aac9d2009-01-09 11:29:40 +08001899 ret = ftrace_make_nop(mod, rec, MCOUNT_ADDR);
Steven Rostedt593eb8a2008-10-23 09:32:59 -04001900 if (ret) {
Steven Rostedt31e88902008-11-14 16:21:19 -08001901 ftrace_bug(ret, ip);
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301902 return 0;
Steven Rostedt37ad5082008-05-12 21:20:48 +02001903 }
Abhishek Sagar492a7ea2008-05-25 00:10:04 +05301904 return 1;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02001905}
1906
Steven Rostedt000ab692009-02-17 13:35:06 -05001907/*
1908 * archs can override this function if they must do something
1909 * before the modifying code is performed.
1910 */
1911int __weak ftrace_arch_code_modify_prepare(void)
1912{
1913 return 0;
1914}
1915
1916/*
1917 * archs can override this function if they must do something
1918 * after the modifying code is performed.
1919 */
1920int __weak ftrace_arch_code_modify_post_process(void)
1921{
1922 return 0;
1923}
1924
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001925void ftrace_modify_all_code(int command)
1926{
1927 if (command & FTRACE_UPDATE_CALLS)
1928 ftrace_replace_code(1);
1929 else if (command & FTRACE_DISABLE_CALLS)
1930 ftrace_replace_code(0);
1931
1932 if (command & FTRACE_UPDATE_TRACE_FUNC)
1933 ftrace_update_ftrace_func(ftrace_trace_function);
1934
1935 if (command & FTRACE_START_FUNC_RET)
1936 ftrace_enable_ftrace_graph_caller();
1937 else if (command & FTRACE_STOP_FUNC_RET)
1938 ftrace_disable_ftrace_graph_caller();
1939}
1940
Ingo Molnare309b412008-05-12 21:20:51 +02001941static int __ftrace_modify_code(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +02001942{
Steven Rostedtd61f82d2008-05-12 21:20:43 +02001943 int *command = data;
1944
Steven Rostedt8ed3e2c2012-04-26 14:59:43 -04001945 ftrace_modify_all_code(*command);
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05001946
Steven Rostedtc88fd862011-08-16 09:53:39 -04001947 return 0;
1948}
1949
1950/**
1951 * ftrace_run_stop_machine, go back to the stop machine method
1952 * @command: The command to tell ftrace what to do
1953 *
1954 * If an arch needs to fall back to the stop machine method, the
1955 * it can call this function.
1956 */
1957void ftrace_run_stop_machine(int command)
1958{
1959 stop_machine(__ftrace_modify_code, &command, NULL);
1960}
1961
1962/**
1963 * arch_ftrace_update_code, modify the code to trace or not trace
1964 * @command: The command that needs to be done
1965 *
1966 * Archs can override this function if it does not need to
1967 * run stop_machine() to modify code.
1968 */
1969void __weak arch_ftrace_update_code(int command)
1970{
1971 ftrace_run_stop_machine(command);
1972}
1973
1974static void ftrace_run_update_code(int command)
1975{
1976 int ret;
1977
1978 ret = ftrace_arch_code_modify_prepare();
1979 FTRACE_WARN_ON(ret);
1980 if (ret)
1981 return;
1982 /*
1983 * Do not call function tracer while we update the code.
1984 * We are in stop machine.
1985 */
1986 function_trace_stop++;
1987
1988 /*
1989 * By default we use stop_machine() to modify the code.
1990 * But archs can do what ever they want as long as it
1991 * is safe. The stop_machine() is the safest, but also
1992 * produces the most overhead.
1993 */
1994 arch_ftrace_update_code(command);
1995
Steven Rostedt6331c282011-07-13 15:11:02 -04001996 function_trace_stop--;
1997
Steven Rostedt000ab692009-02-17 13:35:06 -05001998 ret = ftrace_arch_code_modify_post_process();
1999 FTRACE_WARN_ON(ret);
Steven Rostedt3d083392008-05-12 21:20:42 +02002000}
2001
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002002static ftrace_func_t saved_ftrace_func;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002003static int ftrace_start_up;
Steven Rostedtb8489142011-05-04 09:27:52 -04002004static int global_start_up;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002005
2006static void ftrace_startup_enable(int command)
2007{
2008 if (saved_ftrace_func != ftrace_trace_function) {
2009 saved_ftrace_func = ftrace_trace_function;
2010 command |= FTRACE_UPDATE_TRACE_FUNC;
2011 }
2012
2013 if (!command || !ftrace_enabled)
2014 return;
2015
2016 ftrace_run_update_code(command);
2017}
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002018
Steven Rostedta1cd6172011-05-23 15:24:25 -04002019static int ftrace_startup(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002020{
Steven Rostedtb8489142011-05-04 09:27:52 -04002021 bool hash_enable = true;
2022
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002023 if (unlikely(ftrace_disabled))
Steven Rostedta1cd6172011-05-23 15:24:25 -04002024 return -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002025
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002026 ftrace_start_up++;
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002027 command |= FTRACE_UPDATE_CALLS;
Steven Rostedt3d083392008-05-12 21:20:42 +02002028
Steven Rostedtb8489142011-05-04 09:27:52 -04002029 /* ops marked global share the filter hashes */
2030 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2031 ops = &global_ops;
2032 /* Don't update hash if global is already set */
2033 if (global_start_up)
2034 hash_enable = false;
2035 global_start_up++;
2036 }
2037
Steven Rostedted926f92011-05-03 13:25:24 -04002038 ops->flags |= FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002039 if (hash_enable)
Steven Rostedted926f92011-05-03 13:25:24 -04002040 ftrace_hash_rec_enable(ops, 1);
2041
Steven Rostedtdf4fc312008-11-26 00:16:23 -05002042 ftrace_startup_enable(command);
Steven Rostedta1cd6172011-05-23 15:24:25 -04002043
2044 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +02002045}
2046
Steven Rostedtbd69c302011-05-03 21:55:54 -04002047static void ftrace_shutdown(struct ftrace_ops *ops, int command)
Steven Rostedt3d083392008-05-12 21:20:42 +02002048{
Steven Rostedtb8489142011-05-04 09:27:52 -04002049 bool hash_disable = true;
2050
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002051 if (unlikely(ftrace_disabled))
2052 return;
2053
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002054 ftrace_start_up--;
Frederic Weisbecker9ea1a152009-06-20 06:52:21 +02002055 /*
2056 * Just warn in case of unbalance, no need to kill ftrace, it's not
2057 * critical but the ftrace_call callers may be never nopped again after
2058 * further ftrace uses.
2059 */
2060 WARN_ON_ONCE(ftrace_start_up < 0);
2061
Steven Rostedtb8489142011-05-04 09:27:52 -04002062 if (ops->flags & FTRACE_OPS_FL_GLOBAL) {
2063 ops = &global_ops;
2064 global_start_up--;
2065 WARN_ON_ONCE(global_start_up < 0);
2066 /* Don't update hash if global still has users */
2067 if (global_start_up) {
2068 WARN_ON_ONCE(!ftrace_start_up);
2069 hash_disable = false;
2070 }
2071 }
2072
2073 if (hash_disable)
Steven Rostedted926f92011-05-03 13:25:24 -04002074 ftrace_hash_rec_disable(ops, 1);
2075
Steven Rostedtb8489142011-05-04 09:27:52 -04002076 if (ops != &global_ops || !global_start_up)
Steven Rostedted926f92011-05-03 13:25:24 -04002077 ops->flags &= ~FTRACE_OPS_FL_ENABLED;
Steven Rostedtb8489142011-05-04 09:27:52 -04002078
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002079 command |= FTRACE_UPDATE_CALLS;
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002080
2081 if (saved_ftrace_func != ftrace_trace_function) {
2082 saved_ftrace_func = ftrace_trace_function;
2083 command |= FTRACE_UPDATE_TRACE_FUNC;
2084 }
2085
2086 if (!command || !ftrace_enabled)
Steven Rostedte6ea44e2009-02-14 01:42:44 -05002087 return;
Steven Rostedt3d083392008-05-12 21:20:42 +02002088
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002089 ftrace_run_update_code(command);
Steven Rostedt3d083392008-05-12 21:20:42 +02002090}
2091
Ingo Molnare309b412008-05-12 21:20:51 +02002092static void ftrace_startup_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002093{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002094 if (unlikely(ftrace_disabled))
2095 return;
2096
Steven Rostedtd61f82d2008-05-12 21:20:43 +02002097 /* Force update next time */
2098 saved_ftrace_func = NULL;
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002099 /* ftrace_start_up is true if we want ftrace running */
2100 if (ftrace_start_up)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01002101 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002102}
2103
Ingo Molnare309b412008-05-12 21:20:51 +02002104static void ftrace_shutdown_sysctl(void)
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002105{
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002106 if (unlikely(ftrace_disabled))
2107 return;
2108
Steven Rostedt60a7ecf2008-11-05 16:05:44 -05002109 /* ftrace_start_up is true if ftrace is running */
2110 if (ftrace_start_up)
Steven Rostedt79e406d2010-09-14 22:19:46 -04002111 ftrace_run_update_code(FTRACE_DISABLE_CALLS);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02002112}
2113
Steven Rostedt3d083392008-05-12 21:20:42 +02002114static cycle_t ftrace_update_time;
2115static unsigned long ftrace_update_cnt;
2116unsigned long ftrace_update_tot_cnt;
2117
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002118static int ops_traces_mod(struct ftrace_ops *ops)
2119{
2120 struct ftrace_hash *hash;
2121
2122 hash = ops->filter_hash;
Steven Rostedt06a51d92011-12-19 19:07:36 -05002123 return ftrace_hash_empty(hash);
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002124}
2125
Steven Rostedt31e88902008-11-14 16:21:19 -08002126static int ftrace_update_code(struct module *mod)
Steven Rostedt3d083392008-05-12 21:20:42 +02002127{
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002128 struct ftrace_page *pg;
Lai Jiangshane94142a2009-03-13 17:51:27 +08002129 struct dyn_ftrace *p;
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302130 cycle_t start, stop;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002131 unsigned long ref = 0;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002132 int i;
Steven Rostedtf7bc8b62011-07-14 23:02:27 -04002133
2134 /*
2135 * When adding a module, we need to check if tracers are
2136 * currently enabled and if they are set to trace all functions.
2137 * If they are, we need to enable the module functions as well
2138 * as update the reference counts for those function records.
2139 */
2140 if (mod) {
2141 struct ftrace_ops *ops;
2142
2143 for (ops = ftrace_ops_list;
2144 ops != &ftrace_list_end; ops = ops->next) {
2145 if (ops->flags & FTRACE_OPS_FL_ENABLED &&
2146 ops_traces_mod(ops))
2147 ref++;
2148 }
2149 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002150
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002151 start = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002152 ftrace_update_cnt = 0;
2153
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002154 for (pg = ftrace_new_pgs; pg; pg = pg->next) {
Abhishek Sagarf22f9a82008-06-21 23:50:29 +05302155
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002156 for (i = 0; i < pg->index; i++) {
2157 /* If something went wrong, bail without enabling anything */
2158 if (unlikely(ftrace_disabled))
2159 return -1;
Steven Rostedt3d083392008-05-12 21:20:42 +02002160
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002161 p = &pg->records[i];
2162 p->flags = ref;
Abhishek Sagar0eb96702008-06-01 21:47:30 +05302163
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002164 /*
2165 * Do the initial record conversion from mcount jump
2166 * to the NOP instructions.
2167 */
2168 if (!ftrace_code_disable(mod, p))
2169 break;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002170
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002171 ftrace_update_cnt++;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002172
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002173 /*
2174 * If the tracing is enabled, go ahead and enable the record.
2175 *
2176 * The reason not to enable the record immediatelly is the
2177 * inherent check of ftrace_make_nop/ftrace_make_call for
2178 * correct previous instructions. Making first the NOP
2179 * conversion puts the module to the correct state, thus
2180 * passing the ftrace_make_call check.
2181 */
2182 if (ftrace_start_up && ref) {
2183 int failed = __ftrace_replace_code(p, 1);
2184 if (failed)
2185 ftrace_bug(failed, p->ip);
2186 }
Jiri Olsa5cb084b2009-10-13 16:33:53 -04002187 }
Steven Rostedt3d083392008-05-12 21:20:42 +02002188 }
2189
Steven Rostedt85ae32a2011-12-16 16:30:31 -05002190 ftrace_new_pgs = NULL;
2191
Ingo Molnar750ed1a2008-05-12 21:20:46 +02002192 stop = ftrace_now(raw_smp_processor_id());
Steven Rostedt3d083392008-05-12 21:20:42 +02002193 ftrace_update_time = stop - start;
2194 ftrace_update_tot_cnt += ftrace_update_cnt;
2195
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02002196 return 0;
2197}
2198
Steven Rostedta7900872011-12-16 16:23:44 -05002199static int ftrace_allocate_records(struct ftrace_page *pg, int count)
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002200{
Steven Rostedta7900872011-12-16 16:23:44 -05002201 int order;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002202 int cnt;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002203
Steven Rostedta7900872011-12-16 16:23:44 -05002204 if (WARN_ON(!count))
2205 return -EINVAL;
2206
2207 order = get_count_order(DIV_ROUND_UP(count, ENTRIES_PER_PAGE));
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002208
2209 /*
Steven Rostedta7900872011-12-16 16:23:44 -05002210 * We want to fill as much as possible. No more than a page
2211 * may be empty.
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002212 */
Steven Rostedta7900872011-12-16 16:23:44 -05002213 while ((PAGE_SIZE << order) / ENTRY_SIZE >= count + ENTRIES_PER_PAGE)
2214 order--;
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002215
Steven Rostedta7900872011-12-16 16:23:44 -05002216 again:
2217 pg->records = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
2218
2219 if (!pg->records) {
2220 /* if we can't allocate this size, try something smaller */
2221 if (!order)
2222 return -ENOMEM;
2223 order >>= 1;
2224 goto again;
2225 }
2226
2227 cnt = (PAGE_SIZE << order) / ENTRY_SIZE;
2228 pg->size = cnt;
2229
2230 if (cnt > count)
2231 cnt = count;
2232
2233 return cnt;
2234}
2235
2236static struct ftrace_page *
2237ftrace_allocate_pages(unsigned long num_to_init)
2238{
2239 struct ftrace_page *start_pg;
2240 struct ftrace_page *pg;
2241 int order;
2242 int cnt;
2243
2244 if (!num_to_init)
2245 return 0;
2246
2247 start_pg = pg = kzalloc(sizeof(*pg), GFP_KERNEL);
2248 if (!pg)
2249 return NULL;
2250
2251 /*
2252 * Try to allocate as much as possible in one continues
2253 * location that fills in all of the space. We want to
2254 * waste as little space as possible.
2255 */
2256 for (;;) {
2257 cnt = ftrace_allocate_records(pg, num_to_init);
2258 if (cnt < 0)
2259 goto free_pages;
2260
2261 num_to_init -= cnt;
2262 if (!num_to_init)
2263 break;
2264
2265 pg->next = kzalloc(sizeof(*pg), GFP_KERNEL);
2266 if (!pg->next)
2267 goto free_pages;
2268
2269 pg = pg->next;
2270 }
2271
2272 return start_pg;
2273
2274 free_pages:
2275 while (start_pg) {
2276 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
2277 free_pages((unsigned long)pg->records, order);
2278 start_pg = pg->next;
2279 kfree(pg);
2280 pg = start_pg;
2281 }
2282 pr_info("ftrace: FAILED to allocate memory for functions\n");
2283 return NULL;
2284}
2285
2286static int __init ftrace_dyn_table_alloc(unsigned long num_to_init)
2287{
2288 int cnt;
2289
2290 if (!num_to_init) {
2291 pr_info("ftrace: No functions to be traced?\n");
2292 return -1;
2293 }
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002294
Steven Rostedt68bf21a2008-08-14 15:45:08 -04002295 cnt = num_to_init / ENTRIES_PER_PAGE;
Steven Rostedt08f5ac902008-10-23 09:33:07 -04002296 pr_info("ftrace: allocating %ld entries in %d pages\n",
walimis5821e1b2008-11-15 15:19:06 +08002297 num_to_init, cnt + 1);
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002298
Steven Rostedt3c1720f2008-05-12 21:20:43 +02002299 return 0;
2300}
2301
Steven Rostedt5072c592008-05-12 21:20:43 +02002302#define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
2303
2304struct ftrace_iterator {
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002305 loff_t pos;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002306 loff_t func_pos;
2307 struct ftrace_page *pg;
2308 struct dyn_ftrace *func;
2309 struct ftrace_func_probe *probe;
2310 struct trace_parser parser;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002311 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002312 struct ftrace_ops *ops;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002313 int hidx;
2314 int idx;
2315 unsigned flags;
Steven Rostedt5072c592008-05-12 21:20:43 +02002316};
2317
Ingo Molnare309b412008-05-12 21:20:51 +02002318static void *
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002319t_hash_next(struct seq_file *m, loff_t *pos)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002320{
2321 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002322 struct hlist_node *hnd = NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002323 struct hlist_head *hhd;
2324
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002325 (*pos)++;
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002326 iter->pos = *pos;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002327
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002328 if (iter->probe)
2329 hnd = &iter->probe->node;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002330 retry:
2331 if (iter->hidx >= FTRACE_FUNC_HASHSIZE)
2332 return NULL;
2333
2334 hhd = &ftrace_func_hash[iter->hidx];
2335
2336 if (hlist_empty(hhd)) {
2337 iter->hidx++;
2338 hnd = NULL;
2339 goto retry;
2340 }
2341
2342 if (!hnd)
2343 hnd = hhd->first;
2344 else {
2345 hnd = hnd->next;
2346 if (!hnd) {
2347 iter->hidx++;
2348 goto retry;
2349 }
2350 }
2351
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002352 if (WARN_ON_ONCE(!hnd))
2353 return NULL;
2354
2355 iter->probe = hlist_entry(hnd, struct ftrace_func_probe, node);
2356
2357 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002358}
2359
2360static void *t_hash_start(struct seq_file *m, loff_t *pos)
2361{
2362 struct ftrace_iterator *iter = m->private;
2363 void *p = NULL;
Li Zefand82d6242009-06-24 09:54:54 +08002364 loff_t l;
2365
Steven Rostedt69a30832011-12-19 15:21:16 -05002366 if (!(iter->flags & FTRACE_ITER_DO_HASH))
2367 return NULL;
2368
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002369 if (iter->func_pos > *pos)
2370 return NULL;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002371
Li Zefand82d6242009-06-24 09:54:54 +08002372 iter->hidx = 0;
Steven Rostedt2bccfff2010-09-09 08:43:22 -04002373 for (l = 0; l <= (*pos - iter->func_pos); ) {
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002374 p = t_hash_next(m, &l);
Li Zefand82d6242009-06-24 09:54:54 +08002375 if (!p)
2376 break;
2377 }
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002378 if (!p)
2379 return NULL;
2380
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002381 /* Only set this if we have an item */
2382 iter->flags |= FTRACE_ITER_HASH;
2383
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002384 return iter;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002385}
2386
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002387static int
2388t_hash_show(struct seq_file *m, struct ftrace_iterator *iter)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002389{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002390 struct ftrace_func_probe *rec;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002391
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002392 rec = iter->probe;
2393 if (WARN_ON_ONCE(!rec))
2394 return -EIO;
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002395
Steven Rostedt809dcf22009-02-16 23:06:01 -05002396 if (rec->ops->print)
2397 return rec->ops->print(m, rec->ip, rec->ops, rec->data);
2398
Steven Rostedtb375a112009-09-17 00:05:58 -04002399 seq_printf(m, "%ps:%ps", (void *)rec->ip, (void *)rec->ops->func);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002400
2401 if (rec->data)
2402 seq_printf(m, ":%p", rec->data);
2403 seq_putc(m, '\n');
2404
2405 return 0;
2406}
2407
2408static void *
Steven Rostedt5072c592008-05-12 21:20:43 +02002409t_next(struct seq_file *m, void *v, loff_t *pos)
2410{
2411 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002412 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002413 struct dyn_ftrace *rec = NULL;
2414
Steven Rostedt45a4a232011-04-21 23:16:46 -04002415 if (unlikely(ftrace_disabled))
2416 return NULL;
2417
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002418 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002419 return t_hash_next(m, pos);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002420
Steven Rostedt5072c592008-05-12 21:20:43 +02002421 (*pos)++;
Jiri Olsa1106b692011-02-16 17:35:34 +01002422 iter->pos = iter->func_pos = *pos;
Steven Rostedt5072c592008-05-12 21:20:43 +02002423
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002424 if (iter->flags & FTRACE_ITER_PRINTALL)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002425 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002426
Steven Rostedt5072c592008-05-12 21:20:43 +02002427 retry:
2428 if (iter->idx >= iter->pg->index) {
2429 if (iter->pg->next) {
2430 iter->pg = iter->pg->next;
2431 iter->idx = 0;
2432 goto retry;
2433 }
2434 } else {
2435 rec = &iter->pg->records[iter->idx++];
Steven Rostedt32082302011-12-16 14:42:37 -05002436 if (((iter->flags & FTRACE_ITER_FILTER) &&
Steven Rostedtf45948e2011-05-02 12:29:25 -04002437 !(ftrace_lookup_ip(ops->filter_hash, rec->ip))) ||
Steven Rostedt0183fb12008-11-07 22:36:02 -05002438
Steven Rostedt41c52c02008-05-22 11:46:33 -04002439 ((iter->flags & FTRACE_ITER_NOTRACE) &&
Steven Rostedt647bcd02011-05-03 14:39:21 -04002440 !ftrace_lookup_ip(ops->notrace_hash, rec->ip)) ||
2441
2442 ((iter->flags & FTRACE_ITER_ENABLED) &&
2443 !(rec->flags & ~FTRACE_FL_MASK))) {
2444
Steven Rostedt5072c592008-05-12 21:20:43 +02002445 rec = NULL;
2446 goto retry;
2447 }
2448 }
2449
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002450 if (!rec)
Steven Rostedt57c072c2010-09-14 11:21:11 -04002451 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002452
2453 iter->func = rec;
2454
2455 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002456}
2457
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002458static void reset_iter_read(struct ftrace_iterator *iter)
2459{
2460 iter->pos = 0;
2461 iter->func_pos = 0;
Dan Carpenter70f77b32012-06-09 19:10:27 +03002462 iter->flags &= ~(FTRACE_ITER_PRINTALL | FTRACE_ITER_HASH);
Steven Rostedt5072c592008-05-12 21:20:43 +02002463}
2464
2465static void *t_start(struct seq_file *m, loff_t *pos)
2466{
2467 struct ftrace_iterator *iter = m->private;
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002468 struct ftrace_ops *ops = iter->ops;
Steven Rostedt5072c592008-05-12 21:20:43 +02002469 void *p = NULL;
Li Zefan694ce0a2009-06-24 09:54:19 +08002470 loff_t l;
Steven Rostedt5072c592008-05-12 21:20:43 +02002471
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002472 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04002473
2474 if (unlikely(ftrace_disabled))
2475 return NULL;
2476
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002477 /*
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002478 * If an lseek was done, then reset and start from beginning.
2479 */
2480 if (*pos < iter->pos)
2481 reset_iter_read(iter);
2482
2483 /*
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002484 * For set_ftrace_filter reading, if we have the filter
2485 * off, we can short cut and just print out that all
2486 * functions are enabled.
2487 */
Steven Rostedt06a51d92011-12-19 19:07:36 -05002488 if (iter->flags & FTRACE_ITER_FILTER &&
2489 ftrace_hash_empty(ops->filter_hash)) {
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002490 if (*pos > 0)
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002491 return t_hash_start(m, pos);
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002492 iter->flags |= FTRACE_ITER_PRINTALL;
Chris Wrightdf091622010-09-09 16:34:59 -07002493 /* reset in case of seek/pread */
2494 iter->flags &= ~FTRACE_ITER_HASH;
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002495 return iter;
2496 }
2497
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002498 if (iter->flags & FTRACE_ITER_HASH)
2499 return t_hash_start(m, pos);
2500
Steven Rostedt98c4fd02010-09-10 11:47:43 -04002501 /*
2502 * Unfortunately, we need to restart at ftrace_pages_start
2503 * every time we let go of the ftrace_mutex. This is because
2504 * those pointers can change without the lock.
2505 */
Li Zefan694ce0a2009-06-24 09:54:19 +08002506 iter->pg = ftrace_pages_start;
2507 iter->idx = 0;
2508 for (l = 0; l <= *pos; ) {
2509 p = t_next(m, p, &l);
2510 if (!p)
2511 break;
Liming Wang50cdaf02008-11-28 12:13:21 +08002512 }
walimis5821e1b2008-11-15 15:19:06 +08002513
Steven Rostedt69a30832011-12-19 15:21:16 -05002514 if (!p)
2515 return t_hash_start(m, pos);
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002516
2517 return iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002518}
2519
2520static void t_stop(struct seq_file *m, void *p)
2521{
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002522 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002523}
2524
2525static int t_show(struct seq_file *m, void *v)
2526{
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002527 struct ftrace_iterator *iter = m->private;
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002528 struct dyn_ftrace *rec;
Steven Rostedt5072c592008-05-12 21:20:43 +02002529
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002530 if (iter->flags & FTRACE_ITER_HASH)
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002531 return t_hash_show(m, iter);
Steven Rostedt8fc0c702009-02-16 15:28:00 -05002532
Steven Rostedt0c75a3e2009-02-16 11:21:52 -05002533 if (iter->flags & FTRACE_ITER_PRINTALL) {
2534 seq_printf(m, "#### all functions enabled ####\n");
2535 return 0;
2536 }
2537
Steven Rostedt4aeb6962010-09-09 10:00:28 -04002538 rec = iter->func;
2539
Steven Rostedt5072c592008-05-12 21:20:43 +02002540 if (!rec)
2541 return 0;
2542
Steven Rostedt647bcd02011-05-03 14:39:21 -04002543 seq_printf(m, "%ps", (void *)rec->ip);
2544 if (iter->flags & FTRACE_ITER_ENABLED)
Steven Rostedt08f6fba2012-04-30 16:20:23 -04002545 seq_printf(m, " (%ld)%s",
2546 rec->flags & ~FTRACE_FL_MASK,
2547 rec->flags & FTRACE_FL_REGS ? " R" : "");
Steven Rostedt647bcd02011-05-03 14:39:21 -04002548 seq_printf(m, "\n");
Steven Rostedt5072c592008-05-12 21:20:43 +02002549
2550 return 0;
2551}
2552
James Morris88e9d342009-09-22 16:43:43 -07002553static const struct seq_operations show_ftrace_seq_ops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02002554 .start = t_start,
2555 .next = t_next,
2556 .stop = t_stop,
2557 .show = t_show,
2558};
2559
Ingo Molnare309b412008-05-12 21:20:51 +02002560static int
Steven Rostedt5072c592008-05-12 21:20:43 +02002561ftrace_avail_open(struct inode *inode, struct file *file)
2562{
2563 struct ftrace_iterator *iter;
Steven Rostedt5072c592008-05-12 21:20:43 +02002564
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002565 if (unlikely(ftrace_disabled))
2566 return -ENODEV;
2567
Jiri Olsa50e18b92012-04-25 10:23:39 +02002568 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2569 if (iter) {
2570 iter->pg = ftrace_pages_start;
2571 iter->ops = &global_ops;
Ingo Molnar4bf39a92008-05-12 21:20:46 +02002572 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002573
Jiri Olsa50e18b92012-04-25 10:23:39 +02002574 return iter ? 0 : -ENOMEM;
Steven Rostedt5072c592008-05-12 21:20:43 +02002575}
2576
Steven Rostedt647bcd02011-05-03 14:39:21 -04002577static int
2578ftrace_enabled_open(struct inode *inode, struct file *file)
2579{
2580 struct ftrace_iterator *iter;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002581
2582 if (unlikely(ftrace_disabled))
2583 return -ENODEV;
2584
Jiri Olsa50e18b92012-04-25 10:23:39 +02002585 iter = __seq_open_private(file, &show_ftrace_seq_ops, sizeof(*iter));
2586 if (iter) {
2587 iter->pg = ftrace_pages_start;
2588 iter->flags = FTRACE_ITER_ENABLED;
2589 iter->ops = &global_ops;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002590 }
2591
Jiri Olsa50e18b92012-04-25 10:23:39 +02002592 return iter ? 0 : -ENOMEM;
Steven Rostedt647bcd02011-05-03 14:39:21 -04002593}
2594
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002595static void ftrace_filter_reset(struct ftrace_hash *hash)
Steven Rostedt5072c592008-05-12 21:20:43 +02002596{
Steven Rostedt52baf112009-02-14 01:15:39 -05002597 mutex_lock(&ftrace_lock);
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002598 ftrace_hash_clear(hash);
Steven Rostedt52baf112009-02-14 01:15:39 -05002599 mutex_unlock(&ftrace_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002600}
2601
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002602/**
2603 * ftrace_regex_open - initialize function tracer filter files
2604 * @ops: The ftrace_ops that hold the hash filters
2605 * @flag: The type of filter to process
2606 * @inode: The inode, usually passed in to your open routine
2607 * @file: The file, usually passed in to your open routine
2608 *
2609 * ftrace_regex_open() initializes the filter files for the
2610 * @ops. Depending on @flag it may process the filter hash or
2611 * the notrace hash of @ops. With this called from the open
2612 * routine, you can use ftrace_filter_write() for the write
2613 * routine if @flag has FTRACE_ITER_FILTER set, or
2614 * ftrace_notrace_write() if @flag has FTRACE_ITER_NOTRACE set.
2615 * ftrace_regex_lseek() should be used as the lseek routine, and
2616 * release must call ftrace_regex_release().
2617 */
2618int
Steven Rostedtf45948e2011-05-02 12:29:25 -04002619ftrace_regex_open(struct ftrace_ops *ops, int flag,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002620 struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02002621{
2622 struct ftrace_iterator *iter;
Steven Rostedtf45948e2011-05-02 12:29:25 -04002623 struct ftrace_hash *hash;
Steven Rostedt5072c592008-05-12 21:20:43 +02002624 int ret = 0;
2625
Steven Rostedt4eebcc82008-05-12 21:20:48 +02002626 if (unlikely(ftrace_disabled))
2627 return -ENODEV;
2628
Steven Rostedt5072c592008-05-12 21:20:43 +02002629 iter = kzalloc(sizeof(*iter), GFP_KERNEL);
2630 if (!iter)
2631 return -ENOMEM;
2632
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02002633 if (trace_parser_get_init(&iter->parser, FTRACE_BUFF_MAX)) {
2634 kfree(iter);
2635 return -ENOMEM;
2636 }
2637
Steven Rostedtf45948e2011-05-02 12:29:25 -04002638 if (flag & FTRACE_ITER_NOTRACE)
2639 hash = ops->notrace_hash;
2640 else
2641 hash = ops->filter_hash;
2642
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002643 iter->ops = ops;
2644 iter->flags = flag;
2645
2646 if (file->f_mode & FMODE_WRITE) {
2647 mutex_lock(&ftrace_lock);
2648 iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
2649 mutex_unlock(&ftrace_lock);
2650
2651 if (!iter->hash) {
2652 trace_parser_put(&iter->parser);
2653 kfree(iter);
2654 return -ENOMEM;
2655 }
2656 }
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002657
Steven Rostedt41c52c02008-05-22 11:46:33 -04002658 mutex_lock(&ftrace_regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002659
Steven Rostedt5072c592008-05-12 21:20:43 +02002660 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04002661 (file->f_flags & O_TRUNC))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002662 ftrace_filter_reset(iter->hash);
Steven Rostedt5072c592008-05-12 21:20:43 +02002663
2664 if (file->f_mode & FMODE_READ) {
2665 iter->pg = ftrace_pages_start;
Steven Rostedt5072c592008-05-12 21:20:43 +02002666
2667 ret = seq_open(file, &show_ftrace_seq_ops);
2668 if (!ret) {
2669 struct seq_file *m = file->private_data;
2670 m->private = iter;
Li Zefan79fe2492009-09-22 13:54:28 +08002671 } else {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002672 /* Failed */
2673 free_ftrace_hash(iter->hash);
Li Zefan79fe2492009-09-22 13:54:28 +08002674 trace_parser_put(&iter->parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02002675 kfree(iter);
Li Zefan79fe2492009-09-22 13:54:28 +08002676 }
Steven Rostedt5072c592008-05-12 21:20:43 +02002677 } else
2678 file->private_data = iter;
Steven Rostedt41c52c02008-05-22 11:46:33 -04002679 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02002680
2681 return ret;
2682}
2683
Steven Rostedt41c52c02008-05-22 11:46:33 -04002684static int
2685ftrace_filter_open(struct inode *inode, struct file *file)
2686{
Steven Rostedt69a30832011-12-19 15:21:16 -05002687 return ftrace_regex_open(&global_ops,
2688 FTRACE_ITER_FILTER | FTRACE_ITER_DO_HASH,
2689 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002690}
2691
2692static int
2693ftrace_notrace_open(struct inode *inode, struct file *file)
2694{
Steven Rostedtf45948e2011-05-02 12:29:25 -04002695 return ftrace_regex_open(&global_ops, FTRACE_ITER_NOTRACE,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002696 inode, file);
Steven Rostedt41c52c02008-05-22 11:46:33 -04002697}
2698
Steven Rostedtfc13cb02011-12-19 14:41:25 -05002699loff_t
Namhyung Kim6a76f8c2013-04-11 15:55:01 +09002700ftrace_filter_lseek(struct file *file, loff_t offset, int whence)
Steven Rostedt5072c592008-05-12 21:20:43 +02002701{
2702 loff_t ret;
2703
2704 if (file->f_mode & FMODE_READ)
Andrew Morton965c8e52012-12-17 15:59:39 -08002705 ret = seq_lseek(file, offset, whence);
Steven Rostedt5072c592008-05-12 21:20:43 +02002706 else
2707 file->f_pos = ret = 1;
2708
2709 return ret;
2710}
2711
Steven Rostedt64e7c442009-02-13 17:08:48 -05002712static int ftrace_match(char *str, char *regex, int len, int type)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002713{
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002714 int matched = 0;
Li Zefan751e9982010-01-14 10:53:02 +08002715 int slen;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002716
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002717 switch (type) {
2718 case MATCH_FULL:
2719 if (strcmp(str, regex) == 0)
2720 matched = 1;
2721 break;
2722 case MATCH_FRONT_ONLY:
2723 if (strncmp(str, regex, len) == 0)
2724 matched = 1;
2725 break;
2726 case MATCH_MIDDLE_ONLY:
2727 if (strstr(str, regex))
2728 matched = 1;
2729 break;
2730 case MATCH_END_ONLY:
Li Zefan751e9982010-01-14 10:53:02 +08002731 slen = strlen(str);
2732 if (slen >= len && memcmp(str + slen - len, regex, len) == 0)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002733 matched = 1;
2734 break;
2735 }
2736
2737 return matched;
2738}
2739
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002740static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002741enter_record(struct ftrace_hash *hash, struct dyn_ftrace *rec, int not)
Steven Rostedt996e87b2011-04-26 16:11:03 -04002742{
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002743 struct ftrace_func_entry *entry;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002744 int ret = 0;
2745
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002746 entry = ftrace_lookup_ip(hash, rec->ip);
2747 if (not) {
2748 /* Do nothing if it doesn't exist */
2749 if (!entry)
2750 return 0;
2751
Steven Rostedt33dc9b12011-05-02 17:34:47 -04002752 free_hash_entry(hash, entry);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002753 } else {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002754 /* Do nothing if it exists */
2755 if (entry)
2756 return 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002757
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002758 ret = add_hash_entry(hash, rec->ip);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002759 }
2760 return ret;
Steven Rostedt996e87b2011-04-26 16:11:03 -04002761}
2762
Steven Rostedt64e7c442009-02-13 17:08:48 -05002763static int
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002764ftrace_match_record(struct dyn_ftrace *rec, char *mod,
2765 char *regex, int len, int type)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002766{
2767 char str[KSYM_SYMBOL_LEN];
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002768 char *modname;
Steven Rostedt64e7c442009-02-13 17:08:48 -05002769
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002770 kallsyms_lookup(rec->ip, NULL, NULL, &modname, str);
2771
2772 if (mod) {
2773 /* module lookup requires matching the module */
2774 if (!modname || strcmp(modname, mod))
2775 return 0;
2776
2777 /* blank search means to match all funcs in the mod */
2778 if (!len)
2779 return 1;
2780 }
2781
Steven Rostedt64e7c442009-02-13 17:08:48 -05002782 return ftrace_match(str, regex, len, type);
2783}
2784
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002785static int
2786match_records(struct ftrace_hash *hash, char *buff,
2787 int len, char *mod, int not)
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002788{
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002789 unsigned search_len = 0;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002790 struct ftrace_page *pg;
2791 struct dyn_ftrace *rec;
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002792 int type = MATCH_FULL;
2793 char *search = buff;
Li Zefan311d16d2009-12-08 11:15:11 +08002794 int found = 0;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002795 int ret;
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002796
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002797 if (len) {
2798 type = filter_parse_regex(buff, len, &search, &not);
2799 search_len = strlen(search);
2800 }
Steven Rostedt9f4801e2009-02-13 15:56:43 -05002801
Steven Rostedt52baf112009-02-14 01:15:39 -05002802 mutex_lock(&ftrace_lock);
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002803
2804 if (unlikely(ftrace_disabled))
2805 goto out_unlock;
2806
Steven Rostedt265c8312009-02-13 12:43:56 -05002807 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002808 if (ftrace_match_record(rec, mod, search, search_len, type)) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002809 ret = enter_record(hash, rec, not);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002810 if (ret < 0) {
2811 found = ret;
2812 goto out_unlock;
2813 }
Li Zefan311d16d2009-12-08 11:15:11 +08002814 found = 1;
Steven Rostedt265c8312009-02-13 12:43:56 -05002815 }
2816 } while_for_each_ftrace_rec();
Steven Rostedtb9df92d2011-04-28 20:32:08 -04002817 out_unlock:
Steven Rostedt52baf112009-02-14 01:15:39 -05002818 mutex_unlock(&ftrace_lock);
Li Zefan311d16d2009-12-08 11:15:11 +08002819
2820 return found;
Steven Rostedt5072c592008-05-12 21:20:43 +02002821}
2822
Steven Rostedt64e7c442009-02-13 17:08:48 -05002823static int
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002824ftrace_match_records(struct ftrace_hash *hash, char *buff, int len)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002825{
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002826 return match_records(hash, buff, len, NULL, 0);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002827}
2828
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002829static int
2830ftrace_match_module_records(struct ftrace_hash *hash, char *buff, char *mod)
Steven Rostedt64e7c442009-02-13 17:08:48 -05002831{
Steven Rostedt64e7c442009-02-13 17:08:48 -05002832 int not = 0;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002833
Steven Rostedt64e7c442009-02-13 17:08:48 -05002834 /* blank or '*' mean the same */
2835 if (strcmp(buff, "*") == 0)
2836 buff[0] = 0;
2837
2838 /* handle the case of 'dont filter this module' */
2839 if (strcmp(buff, "!") == 0 || strcmp(buff, "!*") == 0) {
2840 buff[0] = 0;
2841 not = 1;
2842 }
2843
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002844 return match_records(hash, buff, strlen(buff), mod, not);
Steven Rostedt64e7c442009-02-13 17:08:48 -05002845}
2846
Steven Rostedtf6180772009-02-14 00:40:25 -05002847/*
2848 * We register the module command as a template to show others how
2849 * to register the a command as well.
2850 */
2851
2852static int
Steven Rostedt43dd61c2011-07-07 11:09:22 -04002853ftrace_mod_callback(struct ftrace_hash *hash,
2854 char *func, char *cmd, char *param, int enable)
Steven Rostedtf6180772009-02-14 00:40:25 -05002855{
2856 char *mod;
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002857 int ret = -EINVAL;
Steven Rostedtf6180772009-02-14 00:40:25 -05002858
2859 /*
2860 * cmd == 'mod' because we only registered this func
2861 * for the 'mod' ftrace_func_command.
2862 * But if you register one func with multiple commands,
2863 * you can tell which command was used by the cmd
2864 * parameter.
2865 */
2866
2867 /* we must have a module name */
2868 if (!param)
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002869 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002870
2871 mod = strsep(&param, ":");
2872 if (!strlen(mod))
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002873 return ret;
Steven Rostedtf6180772009-02-14 00:40:25 -05002874
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04002875 ret = ftrace_match_module_records(hash, func, mod);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04002876 if (!ret)
2877 ret = -EINVAL;
2878 if (ret < 0)
2879 return ret;
2880
2881 return 0;
Steven Rostedtf6180772009-02-14 00:40:25 -05002882}
2883
2884static struct ftrace_func_command ftrace_mod_cmd = {
2885 .name = "mod",
2886 .func = ftrace_mod_callback,
2887};
2888
2889static int __init ftrace_mod_cmd_init(void)
2890{
2891 return register_ftrace_command(&ftrace_mod_cmd);
2892}
Steven Rostedt6f415672012-10-05 12:13:07 -04002893core_initcall(ftrace_mod_cmd_init);
Steven Rostedtf6180772009-02-14 00:40:25 -05002894
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04002895static void function_trace_probe_call(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04002896 struct ftrace_ops *op, struct pt_regs *pt_regs)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002897{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002898 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002899 struct hlist_head *hhd;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002900 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002901
2902 key = hash_long(ip, FTRACE_HASH_BITS);
2903
2904 hhd = &ftrace_func_hash[key];
2905
2906 if (hlist_empty(hhd))
2907 return;
2908
2909 /*
2910 * Disable preemption for these calls to prevent a RCU grace
2911 * period. This syncs the hash iteration and freeing of items
2912 * on the hash. rcu_read_lock is too dangerous here.
2913 */
Steven Rostedt5168ae52010-06-03 09:36:50 -04002914 preempt_disable_notrace();
Sasha Levinb67bfe02013-02-27 17:06:00 -08002915 hlist_for_each_entry_rcu(entry, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05002916 if (entry->ip == ip)
2917 entry->ops->func(ip, parent_ip, &entry->data);
2918 }
Steven Rostedt5168ae52010-06-03 09:36:50 -04002919 preempt_enable_notrace();
Steven Rostedt59df055f2009-02-14 15:29:06 -05002920}
2921
Steven Rostedtb6887d72009-02-17 12:32:04 -05002922static struct ftrace_ops trace_probe_ops __read_mostly =
Steven Rostedt59df055f2009-02-14 15:29:06 -05002923{
Steven Rostedtfb9fb012009-03-25 13:26:41 -04002924 .func = function_trace_probe_call,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002925};
2926
Steven Rostedtb6887d72009-02-17 12:32:04 -05002927static int ftrace_probe_registered;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002928
Steven Rostedtb6887d72009-02-17 12:32:04 -05002929static void __enable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002930{
Steven Rostedtb8489142011-05-04 09:27:52 -04002931 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002932 int i;
2933
Steven Rostedtb6887d72009-02-17 12:32:04 -05002934 if (ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002935 return;
2936
2937 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2938 struct hlist_head *hhd = &ftrace_func_hash[i];
2939 if (hhd->first)
2940 break;
2941 }
2942 /* Nothing registered? */
2943 if (i == FTRACE_FUNC_HASHSIZE)
2944 return;
2945
Steven Rostedtb8489142011-05-04 09:27:52 -04002946 ret = __register_ftrace_function(&trace_probe_ops);
2947 if (!ret)
Steven Rostedta1cd6172011-05-23 15:24:25 -04002948 ret = ftrace_startup(&trace_probe_ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04002949
Steven Rostedtb6887d72009-02-17 12:32:04 -05002950 ftrace_probe_registered = 1;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002951}
2952
Steven Rostedtb6887d72009-02-17 12:32:04 -05002953static void __disable_ftrace_function_probe(void)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002954{
Steven Rostedtb8489142011-05-04 09:27:52 -04002955 int ret;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002956 int i;
2957
Steven Rostedtb6887d72009-02-17 12:32:04 -05002958 if (!ftrace_probe_registered)
Steven Rostedt59df055f2009-02-14 15:29:06 -05002959 return;
2960
2961 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
2962 struct hlist_head *hhd = &ftrace_func_hash[i];
2963 if (hhd->first)
2964 return;
2965 }
2966
2967 /* no more funcs left */
Steven Rostedtb8489142011-05-04 09:27:52 -04002968 ret = __unregister_ftrace_function(&trace_probe_ops);
2969 if (!ret)
2970 ftrace_shutdown(&trace_probe_ops, 0);
2971
Steven Rostedtb6887d72009-02-17 12:32:04 -05002972 ftrace_probe_registered = 0;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002973}
2974
2975
2976static void ftrace_free_entry_rcu(struct rcu_head *rhp)
2977{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002978 struct ftrace_func_probe *entry =
2979 container_of(rhp, struct ftrace_func_probe, rcu);
Steven Rostedt59df055f2009-02-14 15:29:06 -05002980
2981 if (entry->ops->free)
2982 entry->ops->free(&entry->data);
2983 kfree(entry);
2984}
2985
2986
2987int
Steven Rostedtb6887d72009-02-17 12:32:04 -05002988register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05002989 void *data)
2990{
Steven Rostedtb6887d72009-02-17 12:32:04 -05002991 struct ftrace_func_probe *entry;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002992 struct ftrace_page *pg;
2993 struct dyn_ftrace *rec;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002994 int type, len, not;
Steven Rostedt6a24a242009-02-17 11:20:26 -05002995 unsigned long key;
Steven Rostedt59df055f2009-02-14 15:29:06 -05002996 int count = 0;
2997 char *search;
2998
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02002999 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003000 len = strlen(search);
3001
Steven Rostedtb6887d72009-02-17 12:32:04 -05003002 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003003 if (WARN_ON(not))
3004 return -EINVAL;
3005
3006 mutex_lock(&ftrace_lock);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003007
Steven Rostedt45a4a232011-04-21 23:16:46 -04003008 if (unlikely(ftrace_disabled))
3009 goto out_unlock;
3010
Steven Rostedt59df055f2009-02-14 15:29:06 -05003011 do_for_each_ftrace_rec(pg, rec) {
3012
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003013 if (!ftrace_match_record(rec, NULL, search, len, type))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003014 continue;
3015
3016 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
3017 if (!entry) {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003018 /* If we did not process any, then return error */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003019 if (!count)
3020 count = -ENOMEM;
3021 goto out_unlock;
3022 }
3023
3024 count++;
3025
3026 entry->data = data;
3027
3028 /*
3029 * The caller might want to do something special
3030 * for each function we find. We call the callback
3031 * to give the caller an opportunity to do so.
3032 */
3033 if (ops->callback) {
3034 if (ops->callback(rec->ip, &entry->data) < 0) {
3035 /* caller does not like this func */
3036 kfree(entry);
3037 continue;
3038 }
3039 }
3040
3041 entry->ops = ops;
3042 entry->ip = rec->ip;
3043
3044 key = hash_long(entry->ip, FTRACE_HASH_BITS);
3045 hlist_add_head_rcu(&entry->node, &ftrace_func_hash[key]);
3046
3047 } while_for_each_ftrace_rec();
Steven Rostedtb6887d72009-02-17 12:32:04 -05003048 __enable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003049
3050 out_unlock:
3051 mutex_unlock(&ftrace_lock);
3052
3053 return count;
3054}
3055
3056enum {
Steven Rostedtb6887d72009-02-17 12:32:04 -05003057 PROBE_TEST_FUNC = 1,
3058 PROBE_TEST_DATA = 2
Steven Rostedt59df055f2009-02-14 15:29:06 -05003059};
3060
3061static void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003062__unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003063 void *data, int flags)
3064{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003065 struct ftrace_func_probe *entry;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003066 struct hlist_node *tmp;
Steven Rostedt59df055f2009-02-14 15:29:06 -05003067 char str[KSYM_SYMBOL_LEN];
3068 int type = MATCH_FULL;
3069 int i, len = 0;
3070 char *search;
3071
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003072 if (glob && (strcmp(glob, "*") == 0 || !strlen(glob)))
Steven Rostedt59df055f2009-02-14 15:29:06 -05003073 glob = NULL;
Atsushi Tsujib36461d2009-09-15 19:06:30 +09003074 else if (glob) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003075 int not;
3076
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003077 type = filter_parse_regex(glob, strlen(glob), &search, &not);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003078 len = strlen(search);
3079
Steven Rostedtb6887d72009-02-17 12:32:04 -05003080 /* we do not support '!' for function probes */
Steven Rostedt59df055f2009-02-14 15:29:06 -05003081 if (WARN_ON(not))
3082 return;
3083 }
3084
3085 mutex_lock(&ftrace_lock);
3086 for (i = 0; i < FTRACE_FUNC_HASHSIZE; i++) {
3087 struct hlist_head *hhd = &ftrace_func_hash[i];
3088
Sasha Levinb67bfe02013-02-27 17:06:00 -08003089 hlist_for_each_entry_safe(entry, tmp, hhd, node) {
Steven Rostedt59df055f2009-02-14 15:29:06 -05003090
3091 /* break up if statements for readability */
Steven Rostedtb6887d72009-02-17 12:32:04 -05003092 if ((flags & PROBE_TEST_FUNC) && entry->ops != ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003093 continue;
3094
Steven Rostedtb6887d72009-02-17 12:32:04 -05003095 if ((flags & PROBE_TEST_DATA) && entry->data != data)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003096 continue;
3097
3098 /* do this last, since it is the most expensive */
3099 if (glob) {
3100 kallsyms_lookup(entry->ip, NULL, NULL,
3101 NULL, str);
3102 if (!ftrace_match(str, glob, len, type))
3103 continue;
3104 }
3105
Steven Rostedt (Red Hat)740466b2013-03-13 11:15:19 -04003106 hlist_del_rcu(&entry->node);
3107 call_rcu_sched(&entry->rcu, ftrace_free_entry_rcu);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003108 }
3109 }
Steven Rostedtb6887d72009-02-17 12:32:04 -05003110 __disable_ftrace_function_probe();
Steven Rostedt59df055f2009-02-14 15:29:06 -05003111 mutex_unlock(&ftrace_lock);
3112}
3113
3114void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003115unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
Steven Rostedt59df055f2009-02-14 15:29:06 -05003116 void *data)
3117{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003118 __unregister_ftrace_function_probe(glob, ops, data,
3119 PROBE_TEST_FUNC | PROBE_TEST_DATA);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003120}
3121
3122void
Steven Rostedtb6887d72009-02-17 12:32:04 -05003123unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003124{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003125 __unregister_ftrace_function_probe(glob, ops, NULL, PROBE_TEST_FUNC);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003126}
3127
Steven Rostedtb6887d72009-02-17 12:32:04 -05003128void unregister_ftrace_function_probe_all(char *glob)
Steven Rostedt59df055f2009-02-14 15:29:06 -05003129{
Steven Rostedtb6887d72009-02-17 12:32:04 -05003130 __unregister_ftrace_function_probe(glob, NULL, NULL, 0);
Steven Rostedt59df055f2009-02-14 15:29:06 -05003131}
3132
Steven Rostedtf6180772009-02-14 00:40:25 -05003133static LIST_HEAD(ftrace_commands);
3134static DEFINE_MUTEX(ftrace_cmd_mutex);
3135
3136int register_ftrace_command(struct ftrace_func_command *cmd)
3137{
3138 struct ftrace_func_command *p;
3139 int ret = 0;
3140
3141 mutex_lock(&ftrace_cmd_mutex);
3142 list_for_each_entry(p, &ftrace_commands, list) {
3143 if (strcmp(cmd->name, p->name) == 0) {
3144 ret = -EBUSY;
3145 goto out_unlock;
3146 }
3147 }
3148 list_add(&cmd->list, &ftrace_commands);
3149 out_unlock:
3150 mutex_unlock(&ftrace_cmd_mutex);
3151
3152 return ret;
3153}
3154
3155int unregister_ftrace_command(struct ftrace_func_command *cmd)
3156{
3157 struct ftrace_func_command *p, *n;
3158 int ret = -ENODEV;
3159
3160 mutex_lock(&ftrace_cmd_mutex);
3161 list_for_each_entry_safe(p, n, &ftrace_commands, list) {
3162 if (strcmp(cmd->name, p->name) == 0) {
3163 ret = 0;
3164 list_del_init(&p->list);
3165 goto out_unlock;
3166 }
3167 }
3168 out_unlock:
3169 mutex_unlock(&ftrace_cmd_mutex);
3170
3171 return ret;
3172}
3173
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003174static int ftrace_process_regex(struct ftrace_hash *hash,
3175 char *buff, int len, int enable)
Steven Rostedt64e7c442009-02-13 17:08:48 -05003176{
Steven Rostedtf6180772009-02-14 00:40:25 -05003177 char *func, *command, *next = buff;
Steven Rostedt6a24a242009-02-17 11:20:26 -05003178 struct ftrace_func_command *p;
GuoWen Li0aff1c02011-06-01 19:18:47 +08003179 int ret = -EINVAL;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003180
3181 func = strsep(&next, ":");
3182
3183 if (!next) {
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003184 ret = ftrace_match_records(hash, func, len);
Steven Rostedtb448c4e2011-04-29 15:12:32 -04003185 if (!ret)
3186 ret = -EINVAL;
3187 if (ret < 0)
3188 return ret;
3189 return 0;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003190 }
3191
Steven Rostedtf6180772009-02-14 00:40:25 -05003192 /* command found */
Steven Rostedt64e7c442009-02-13 17:08:48 -05003193
3194 command = strsep(&next, ":");
3195
Steven Rostedtf6180772009-02-14 00:40:25 -05003196 mutex_lock(&ftrace_cmd_mutex);
3197 list_for_each_entry(p, &ftrace_commands, list) {
3198 if (strcmp(p->name, command) == 0) {
Steven Rostedt43dd61c2011-07-07 11:09:22 -04003199 ret = p->func(hash, func, command, next, enable);
Steven Rostedtf6180772009-02-14 00:40:25 -05003200 goto out_unlock;
3201 }
Steven Rostedt64e7c442009-02-13 17:08:48 -05003202 }
Steven Rostedtf6180772009-02-14 00:40:25 -05003203 out_unlock:
3204 mutex_unlock(&ftrace_cmd_mutex);
Steven Rostedt64e7c442009-02-13 17:08:48 -05003205
Steven Rostedtf6180772009-02-14 00:40:25 -05003206 return ret;
Steven Rostedt64e7c442009-02-13 17:08:48 -05003207}
3208
Ingo Molnare309b412008-05-12 21:20:51 +02003209static ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003210ftrace_regex_write(struct file *file, const char __user *ubuf,
3211 size_t cnt, loff_t *ppos, int enable)
Steven Rostedt5072c592008-05-12 21:20:43 +02003212{
3213 struct ftrace_iterator *iter;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003214 struct trace_parser *parser;
3215 ssize_t ret, read;
Steven Rostedt5072c592008-05-12 21:20:43 +02003216
Li Zefan4ba79782009-09-22 13:52:20 +08003217 if (!cnt)
Steven Rostedt5072c592008-05-12 21:20:43 +02003218 return 0;
3219
Steven Rostedt41c52c02008-05-22 11:46:33 -04003220 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003221
Steven Rostedt45a4a232011-04-21 23:16:46 -04003222 ret = -ENODEV;
3223 if (unlikely(ftrace_disabled))
3224 goto out_unlock;
3225
Steven Rostedt5072c592008-05-12 21:20:43 +02003226 if (file->f_mode & FMODE_READ) {
3227 struct seq_file *m = file->private_data;
3228 iter = m->private;
3229 } else
3230 iter = file->private_data;
3231
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003232 parser = &iter->parser;
3233 read = trace_get_user(parser, ubuf, cnt, ppos);
Steven Rostedt5072c592008-05-12 21:20:43 +02003234
Li Zefan4ba79782009-09-22 13:52:20 +08003235 if (read >= 0 && trace_parser_loaded(parser) &&
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003236 !trace_parser_cont(parser)) {
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003237 ret = ftrace_process_regex(iter->hash, parser->buffer,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003238 parser->idx, enable);
Li Zefan313254a2009-12-08 11:15:30 +08003239 trace_parser_clear(parser);
Steven Rostedt5072c592008-05-12 21:20:43 +02003240 if (ret)
Li Zefaned146b252009-11-03 08:55:38 +08003241 goto out_unlock;
Steven Rostedt5072c592008-05-12 21:20:43 +02003242 }
3243
Steven Rostedt5072c592008-05-12 21:20:43 +02003244 ret = read;
Li Zefaned146b252009-11-03 08:55:38 +08003245out_unlock:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003246 mutex_unlock(&ftrace_regex_lock);
Li Zefaned146b252009-11-03 08:55:38 +08003247
Steven Rostedt5072c592008-05-12 21:20:43 +02003248 return ret;
3249}
3250
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003251ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003252ftrace_filter_write(struct file *file, const char __user *ubuf,
3253 size_t cnt, loff_t *ppos)
3254{
3255 return ftrace_regex_write(file, ubuf, cnt, ppos, 1);
3256}
3257
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003258ssize_t
Steven Rostedt41c52c02008-05-22 11:46:33 -04003259ftrace_notrace_write(struct file *file, const char __user *ubuf,
3260 size_t cnt, loff_t *ppos)
3261{
3262 return ftrace_regex_write(file, ubuf, cnt, ppos, 0);
3263}
3264
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003265static int
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003266ftrace_match_addr(struct ftrace_hash *hash, unsigned long ip, int remove)
3267{
3268 struct ftrace_func_entry *entry;
3269
3270 if (!ftrace_location(ip))
3271 return -EINVAL;
3272
3273 if (remove) {
3274 entry = ftrace_lookup_ip(hash, ip);
3275 if (!entry)
3276 return -ENOENT;
3277 free_hash_entry(hash, entry);
3278 return 0;
3279 }
3280
3281 return add_hash_entry(hash, ip);
3282}
3283
3284static int
3285ftrace_set_hash(struct ftrace_ops *ops, unsigned char *buf, int len,
3286 unsigned long ip, int remove, int reset, int enable)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003287{
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003288 struct ftrace_hash **orig_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003289 struct ftrace_hash *hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003290 int ret;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003291
Steven Rostedt936e0742011-05-05 22:54:01 -04003292 /* All global ops uses the global ops filters */
3293 if (ops->flags & FTRACE_OPS_FL_GLOBAL)
3294 ops = &global_ops;
3295
Steven Rostedt41c52c02008-05-22 11:46:33 -04003296 if (unlikely(ftrace_disabled))
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003297 return -ENODEV;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003298
Steven Rostedtf45948e2011-05-02 12:29:25 -04003299 if (enable)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003300 orig_hash = &ops->filter_hash;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003301 else
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003302 orig_hash = &ops->notrace_hash;
3303
3304 hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, *orig_hash);
3305 if (!hash)
3306 return -ENOMEM;
Steven Rostedtf45948e2011-05-02 12:29:25 -04003307
Steven Rostedt41c52c02008-05-22 11:46:33 -04003308 mutex_lock(&ftrace_regex_lock);
3309 if (reset)
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003310 ftrace_filter_reset(hash);
Jiri Olsaac483c42012-01-02 10:04:14 +01003311 if (buf && !ftrace_match_records(hash, buf, len)) {
3312 ret = -EINVAL;
3313 goto out_regex_unlock;
3314 }
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003315 if (ip) {
3316 ret = ftrace_match_addr(hash, ip, remove);
3317 if (ret < 0)
3318 goto out_regex_unlock;
3319 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003320
3321 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003322 ret = ftrace_hash_move(ops, enable, orig_hash, hash);
Steven Rostedt072126f2011-07-13 15:08:31 -04003323 if (!ret && ops->flags & FTRACE_OPS_FL_ENABLED
3324 && ftrace_enabled)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01003325 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt072126f2011-07-13 15:08:31 -04003326
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003327 mutex_unlock(&ftrace_lock);
3328
Jiri Olsaac483c42012-01-02 10:04:14 +01003329 out_regex_unlock:
Steven Rostedt41c52c02008-05-22 11:46:33 -04003330 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003331
3332 free_ftrace_hash(hash);
3333 return ret;
Steven Rostedt41c52c02008-05-22 11:46:33 -04003334}
3335
Masami Hiramatsu647664e2012-06-05 19:28:08 +09003336static int
3337ftrace_set_addr(struct ftrace_ops *ops, unsigned long ip, int remove,
3338 int reset, int enable)
3339{
3340 return ftrace_set_hash(ops, 0, 0, ip, remove, reset, enable);
3341}
3342
3343/**
3344 * ftrace_set_filter_ip - set a function to filter on in ftrace by address
3345 * @ops - the ops to set the filter with
3346 * @ip - the address to add to or remove from the filter.
3347 * @remove - non zero to remove the ip from the filter
3348 * @reset - non zero to reset all filters before applying this filter.
3349 *
3350 * Filters denote which functions should be enabled when tracing is enabled
3351 * If @ip is NULL, it failes to update filter.
3352 */
3353int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
3354 int remove, int reset)
3355{
3356 return ftrace_set_addr(ops, ip, remove, reset, 1);
3357}
3358EXPORT_SYMBOL_GPL(ftrace_set_filter_ip);
3359
3360static int
3361ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,
3362 int reset, int enable)
3363{
3364 return ftrace_set_hash(ops, buf, len, 0, 0, reset, enable);
3365}
3366
Steven Rostedt77a2b372008-05-12 21:20:45 +02003367/**
3368 * ftrace_set_filter - set a function to filter on in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003369 * @ops - the ops to set the filter with
Steven Rostedt77a2b372008-05-12 21:20:45 +02003370 * @buf - the string that holds the function filter text.
3371 * @len - the length of the string.
3372 * @reset - non zero to reset all filters before applying this filter.
3373 *
3374 * Filters denote which functions should be enabled when tracing is enabled.
3375 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3376 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003377int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003378 int len, int reset)
Steven Rostedt77a2b372008-05-12 21:20:45 +02003379{
Jiri Olsaac483c42012-01-02 10:04:14 +01003380 return ftrace_set_regex(ops, buf, len, reset, 1);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003381}
Steven Rostedt936e0742011-05-05 22:54:01 -04003382EXPORT_SYMBOL_GPL(ftrace_set_filter);
Steven Rostedt4eebcc82008-05-12 21:20:48 +02003383
Steven Rostedt41c52c02008-05-22 11:46:33 -04003384/**
3385 * ftrace_set_notrace - set a function to not trace in ftrace
Steven Rostedt936e0742011-05-05 22:54:01 -04003386 * @ops - the ops to set the notrace filter with
Steven Rostedt41c52c02008-05-22 11:46:33 -04003387 * @buf - the string that holds the function notrace text.
3388 * @len - the length of the string.
3389 * @reset - non zero to reset all filters before applying this filter.
3390 *
3391 * Notrace Filters denote which functions should not be enabled when tracing
3392 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3393 * for tracing.
3394 */
Jiri Olsaac483c42012-01-02 10:04:14 +01003395int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
Steven Rostedt936e0742011-05-05 22:54:01 -04003396 int len, int reset)
3397{
Jiri Olsaac483c42012-01-02 10:04:14 +01003398 return ftrace_set_regex(ops, buf, len, reset, 0);
Steven Rostedt936e0742011-05-05 22:54:01 -04003399}
3400EXPORT_SYMBOL_GPL(ftrace_set_notrace);
3401/**
3402 * ftrace_set_filter - set a function to filter on in ftrace
3403 * @ops - the ops to set the filter with
3404 * @buf - the string that holds the function filter text.
3405 * @len - the length of the string.
3406 * @reset - non zero to reset all filters before applying this filter.
3407 *
3408 * Filters denote which functions should be enabled when tracing is enabled.
3409 * If @buf is NULL and reset is set, all functions will be enabled for tracing.
3410 */
3411void ftrace_set_global_filter(unsigned char *buf, int len, int reset)
3412{
3413 ftrace_set_regex(&global_ops, buf, len, reset, 1);
3414}
3415EXPORT_SYMBOL_GPL(ftrace_set_global_filter);
3416
3417/**
3418 * ftrace_set_notrace - set a function to not trace in ftrace
3419 * @ops - the ops to set the notrace filter with
3420 * @buf - the string that holds the function notrace text.
3421 * @len - the length of the string.
3422 * @reset - non zero to reset all filters before applying this filter.
3423 *
3424 * Notrace Filters denote which functions should not be enabled when tracing
3425 * is enabled. If @buf is NULL and reset is set, all functions will be enabled
3426 * for tracing.
3427 */
3428void ftrace_set_global_notrace(unsigned char *buf, int len, int reset)
Steven Rostedt41c52c02008-05-22 11:46:33 -04003429{
Steven Rostedtf45948e2011-05-02 12:29:25 -04003430 ftrace_set_regex(&global_ops, buf, len, reset, 0);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003431}
Steven Rostedt936e0742011-05-05 22:54:01 -04003432EXPORT_SYMBOL_GPL(ftrace_set_global_notrace);
Steven Rostedt77a2b372008-05-12 21:20:45 +02003433
Steven Rostedt2af15d62009-05-28 13:37:24 -04003434/*
3435 * command line interface to allow users to set filters on boot up.
3436 */
3437#define FTRACE_FILTER_SIZE COMMAND_LINE_SIZE
3438static char ftrace_notrace_buf[FTRACE_FILTER_SIZE] __initdata;
3439static char ftrace_filter_buf[FTRACE_FILTER_SIZE] __initdata;
3440
3441static int __init set_ftrace_notrace(char *str)
3442{
3443 strncpy(ftrace_notrace_buf, str, FTRACE_FILTER_SIZE);
3444 return 1;
3445}
3446__setup("ftrace_notrace=", set_ftrace_notrace);
3447
3448static int __init set_ftrace_filter(char *str)
3449{
3450 strncpy(ftrace_filter_buf, str, FTRACE_FILTER_SIZE);
3451 return 1;
3452}
3453__setup("ftrace_filter=", set_ftrace_filter);
3454
Stefan Assmann369bc182009-10-12 22:17:21 +02003455#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Lai Jiangshanf6060f42009-11-05 11:16:17 +08003456static char ftrace_graph_buf[FTRACE_FILTER_SIZE] __initdata;
Steven Rostedt801c29f2010-03-05 20:02:19 -05003457static int ftrace_set_func(unsigned long *array, int *idx, char *buffer);
3458
Stefan Assmann369bc182009-10-12 22:17:21 +02003459static int __init set_graph_function(char *str)
3460{
Frederic Weisbecker06f43d62009-10-14 20:43:39 +02003461 strlcpy(ftrace_graph_buf, str, FTRACE_FILTER_SIZE);
Stefan Assmann369bc182009-10-12 22:17:21 +02003462 return 1;
3463}
3464__setup("ftrace_graph_filter=", set_graph_function);
3465
3466static void __init set_ftrace_early_graph(char *buf)
3467{
3468 int ret;
3469 char *func;
3470
3471 while (buf) {
3472 func = strsep(&buf, ",");
3473 /* we allow only one expression at a time */
3474 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
3475 func);
3476 if (ret)
3477 printk(KERN_DEBUG "ftrace: function %s not "
3478 "traceable\n", func);
3479 }
3480}
3481#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3482
Steven Rostedt2a85a372011-12-19 21:57:44 -05003483void __init
3484ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable)
Steven Rostedt2af15d62009-05-28 13:37:24 -04003485{
3486 char *func;
3487
3488 while (buf) {
3489 func = strsep(&buf, ",");
Steven Rostedtf45948e2011-05-02 12:29:25 -04003490 ftrace_set_regex(ops, func, strlen(func), 0, enable);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003491 }
3492}
3493
3494static void __init set_ftrace_early_filters(void)
3495{
3496 if (ftrace_filter_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003497 ftrace_set_early_filter(&global_ops, ftrace_filter_buf, 1);
Steven Rostedt2af15d62009-05-28 13:37:24 -04003498 if (ftrace_notrace_buf[0])
Steven Rostedt2a85a372011-12-19 21:57:44 -05003499 ftrace_set_early_filter(&global_ops, ftrace_notrace_buf, 0);
Stefan Assmann369bc182009-10-12 22:17:21 +02003500#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3501 if (ftrace_graph_buf[0])
3502 set_ftrace_early_graph(ftrace_graph_buf);
3503#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Steven Rostedt2af15d62009-05-28 13:37:24 -04003504}
3505
Steven Rostedtfc13cb02011-12-19 14:41:25 -05003506int ftrace_regex_release(struct inode *inode, struct file *file)
Steven Rostedt5072c592008-05-12 21:20:43 +02003507{
3508 struct seq_file *m = (struct seq_file *)file->private_data;
3509 struct ftrace_iterator *iter;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003510 struct ftrace_hash **orig_hash;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003511 struct trace_parser *parser;
Steven Rostedted926f92011-05-03 13:25:24 -04003512 int filter_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003513 int ret;
Steven Rostedt5072c592008-05-12 21:20:43 +02003514
Steven Rostedt41c52c02008-05-22 11:46:33 -04003515 mutex_lock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003516 if (file->f_mode & FMODE_READ) {
3517 iter = m->private;
3518
3519 seq_release(inode, file);
3520 } else
3521 iter = file->private_data;
3522
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003523 parser = &iter->parser;
3524 if (trace_parser_loaded(parser)) {
3525 parser->buffer[parser->idx] = 0;
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003526 ftrace_match_records(iter->hash, parser->buffer, parser->idx);
Steven Rostedt5072c592008-05-12 21:20:43 +02003527 }
3528
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003529 trace_parser_put(parser);
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003530
Steven Rostedt058e2972011-04-29 22:35:33 -04003531 if (file->f_mode & FMODE_WRITE) {
Steven Rostedted926f92011-05-03 13:25:24 -04003532 filter_hash = !!(iter->flags & FTRACE_ITER_FILTER);
3533
3534 if (filter_hash)
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003535 orig_hash = &iter->ops->filter_hash;
Steven Rostedted926f92011-05-03 13:25:24 -04003536 else
3537 orig_hash = &iter->ops->notrace_hash;
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003538
Steven Rostedt058e2972011-04-29 22:35:33 -04003539 mutex_lock(&ftrace_lock);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003540 ret = ftrace_hash_move(iter->ops, filter_hash,
3541 orig_hash, iter->hash);
3542 if (!ret && (iter->ops->flags & FTRACE_OPS_FL_ENABLED)
3543 && ftrace_enabled)
Jiri Olsa30fb6aa2011-12-05 18:22:48 +01003544 ftrace_run_update_code(FTRACE_UPDATE_CALLS);
Steven Rostedt41fb61c2011-07-13 15:03:44 -04003545
Steven Rostedt058e2972011-04-29 22:35:33 -04003546 mutex_unlock(&ftrace_lock);
3547 }
Steven Rostedt33dc9b12011-05-02 17:34:47 -04003548 free_ftrace_hash(iter->hash);
3549 kfree(iter);
Steven Rostedt058e2972011-04-29 22:35:33 -04003550
Steven Rostedt41c52c02008-05-22 11:46:33 -04003551 mutex_unlock(&ftrace_regex_lock);
Steven Rostedt5072c592008-05-12 21:20:43 +02003552 return 0;
3553}
3554
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003555static const struct file_operations ftrace_avail_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003556 .open = ftrace_avail_open,
3557 .read = seq_read,
3558 .llseek = seq_lseek,
Li Zefan3be04b42009-08-17 16:54:03 +08003559 .release = seq_release_private,
Steven Rostedt5072c592008-05-12 21:20:43 +02003560};
3561
Steven Rostedt647bcd02011-05-03 14:39:21 -04003562static const struct file_operations ftrace_enabled_fops = {
3563 .open = ftrace_enabled_open,
3564 .read = seq_read,
3565 .llseek = seq_lseek,
3566 .release = seq_release_private,
3567};
3568
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003569static const struct file_operations ftrace_filter_fops = {
Steven Rostedt5072c592008-05-12 21:20:43 +02003570 .open = ftrace_filter_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003571 .read = seq_read,
Steven Rostedt5072c592008-05-12 21:20:43 +02003572 .write = ftrace_filter_write,
Namhyung Kim6a76f8c2013-04-11 15:55:01 +09003573 .llseek = ftrace_filter_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003574 .release = ftrace_regex_release,
Steven Rostedt5072c592008-05-12 21:20:43 +02003575};
3576
Steven Rostedt5e2336a2009-03-05 21:44:55 -05003577static const struct file_operations ftrace_notrace_fops = {
Steven Rostedt41c52c02008-05-22 11:46:33 -04003578 .open = ftrace_notrace_open,
Lai Jiangshan850a80c2009-03-13 17:47:23 +08003579 .read = seq_read,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003580 .write = ftrace_notrace_write,
Namhyung Kim6a76f8c2013-04-11 15:55:01 +09003581 .llseek = ftrace_filter_lseek,
Steven Rostedt1cf41dd2011-04-29 20:59:51 -04003582 .release = ftrace_regex_release,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003583};
3584
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003585#ifdef CONFIG_FUNCTION_GRAPH_TRACER
3586
3587static DEFINE_MUTEX(graph_lock);
3588
3589int ftrace_graph_count;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003590int ftrace_graph_filter_enabled;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003591unsigned long ftrace_graph_funcs[FTRACE_GRAPH_MAX_FUNCS] __read_mostly;
3592
3593static void *
Li Zefan85951842009-06-24 09:54:00 +08003594__g_next(struct seq_file *m, loff_t *pos)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003595{
Li Zefan85951842009-06-24 09:54:00 +08003596 if (*pos >= ftrace_graph_count)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003597 return NULL;
Li Zefana4ec5e02009-09-18 14:06:28 +08003598 return &ftrace_graph_funcs[*pos];
Li Zefan85951842009-06-24 09:54:00 +08003599}
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003600
Li Zefan85951842009-06-24 09:54:00 +08003601static void *
3602g_next(struct seq_file *m, void *v, loff_t *pos)
3603{
3604 (*pos)++;
3605 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003606}
3607
3608static void *g_start(struct seq_file *m, loff_t *pos)
3609{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003610 mutex_lock(&graph_lock);
3611
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003612 /* Nothing, tell g_show to print all functions are enabled */
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003613 if (!ftrace_graph_filter_enabled && !*pos)
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003614 return (void *)1;
3615
Li Zefan85951842009-06-24 09:54:00 +08003616 return __g_next(m, pos);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003617}
3618
3619static void g_stop(struct seq_file *m, void *p)
3620{
3621 mutex_unlock(&graph_lock);
3622}
3623
3624static int g_show(struct seq_file *m, void *v)
3625{
3626 unsigned long *ptr = v;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003627
3628 if (!ptr)
3629 return 0;
3630
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003631 if (ptr == (unsigned long *)1) {
3632 seq_printf(m, "#### all functions enabled ####\n");
3633 return 0;
3634 }
3635
Steven Rostedtb375a112009-09-17 00:05:58 -04003636 seq_printf(m, "%ps\n", (void *)*ptr);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003637
3638 return 0;
3639}
3640
James Morris88e9d342009-09-22 16:43:43 -07003641static const struct seq_operations ftrace_graph_seq_ops = {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003642 .start = g_start,
3643 .next = g_next,
3644 .stop = g_stop,
3645 .show = g_show,
3646};
3647
3648static int
3649ftrace_graph_open(struct inode *inode, struct file *file)
3650{
3651 int ret = 0;
3652
3653 if (unlikely(ftrace_disabled))
3654 return -ENODEV;
3655
3656 mutex_lock(&graph_lock);
3657 if ((file->f_mode & FMODE_WRITE) &&
Steven Rostedt8650ae32009-07-22 23:29:30 -04003658 (file->f_flags & O_TRUNC)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003659 ftrace_graph_filter_enabled = 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003660 ftrace_graph_count = 0;
3661 memset(ftrace_graph_funcs, 0, sizeof(ftrace_graph_funcs));
3662 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003663 mutex_unlock(&graph_lock);
3664
Li Zefana4ec5e02009-09-18 14:06:28 +08003665 if (file->f_mode & FMODE_READ)
3666 ret = seq_open(file, &ftrace_graph_seq_ops);
3667
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003668 return ret;
3669}
3670
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003671static int
Li Zefan87827112009-07-23 11:29:11 +08003672ftrace_graph_release(struct inode *inode, struct file *file)
3673{
3674 if (file->f_mode & FMODE_READ)
3675 seq_release(inode, file);
3676 return 0;
3677}
3678
3679static int
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003680ftrace_set_func(unsigned long *array, int *idx, char *buffer)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003681{
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003682 struct dyn_ftrace *rec;
3683 struct ftrace_page *pg;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003684 int search_len;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003685 int fail = 1;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003686 int type, not;
3687 char *search;
3688 bool exists;
3689 int i;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003690
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003691 /* decode regex */
Frederic Weisbecker3f6fe062009-09-24 21:31:51 +02003692 type = filter_parse_regex(buffer, strlen(buffer), &search, &not);
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003693 if (!not && *idx >= FTRACE_GRAPH_MAX_FUNCS)
3694 return -EBUSY;
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003695
3696 search_len = strlen(search);
3697
Steven Rostedt52baf112009-02-14 01:15:39 -05003698 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003699
3700 if (unlikely(ftrace_disabled)) {
3701 mutex_unlock(&ftrace_lock);
3702 return -ENODEV;
3703 }
3704
Steven Rostedt265c8312009-02-13 12:43:56 -05003705 do_for_each_ftrace_rec(pg, rec) {
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003706
Steven Rostedtb9df92d2011-04-28 20:32:08 -04003707 if (ftrace_match_record(rec, NULL, search, search_len, type)) {
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003708 /* if it is in the array */
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003709 exists = false;
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003710 for (i = 0; i < *idx; i++) {
Frederic Weisbeckerf9349a82009-02-19 21:13:12 +01003711 if (array[i] == rec->ip) {
3712 exists = true;
Steven Rostedt265c8312009-02-13 12:43:56 -05003713 break;
3714 }
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003715 }
3716
3717 if (!not) {
3718 fail = 0;
3719 if (!exists) {
3720 array[(*idx)++] = rec->ip;
3721 if (*idx >= FTRACE_GRAPH_MAX_FUNCS)
3722 goto out;
3723 }
3724 } else {
3725 if (exists) {
3726 array[i] = array[--(*idx)];
3727 array[*idx] = 0;
3728 fail = 0;
3729 }
3730 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003731 }
Steven Rostedt265c8312009-02-13 12:43:56 -05003732 } while_for_each_ftrace_rec();
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003733out:
Steven Rostedt52baf112009-02-14 01:15:39 -05003734 mutex_unlock(&ftrace_lock);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003735
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003736 if (fail)
3737 return -EINVAL;
3738
3739 ftrace_graph_filter_enabled = 1;
3740 return 0;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003741}
3742
3743static ssize_t
3744ftrace_graph_write(struct file *file, const char __user *ubuf,
3745 size_t cnt, loff_t *ppos)
3746{
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003747 struct trace_parser parser;
Li Zefan4ba79782009-09-22 13:52:20 +08003748 ssize_t read, ret;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003749
Li Zefanc7c6b1f2010-02-10 15:43:04 +08003750 if (!cnt)
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003751 return 0;
3752
3753 mutex_lock(&graph_lock);
3754
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003755 if (trace_parser_get_init(&parser, FTRACE_BUFF_MAX)) {
3756 ret = -ENOMEM;
Li Zefan1eb90f12009-09-22 13:52:57 +08003757 goto out_unlock;
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003758 }
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003759
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003760 read = trace_get_user(&parser, ubuf, cnt, ppos);
3761
Li Zefan4ba79782009-09-22 13:52:20 +08003762 if (read >= 0 && trace_parser_loaded((&parser))) {
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003763 parser.buffer[parser.idx] = 0;
3764
3765 /* we allow only one expression at a time */
Li Zefana4ec5e02009-09-18 14:06:28 +08003766 ret = ftrace_set_func(ftrace_graph_funcs, &ftrace_graph_count,
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003767 parser.buffer);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003768 if (ret)
Li Zefan1eb90f12009-09-22 13:52:57 +08003769 goto out_free;
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003770 }
3771
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003772 ret = read;
Li Zefan1eb90f12009-09-22 13:52:57 +08003773
3774out_free:
jolsa@redhat.com689fd8b2009-09-11 17:29:29 +02003775 trace_parser_put(&parser);
Li Zefan1eb90f12009-09-22 13:52:57 +08003776out_unlock:
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003777 mutex_unlock(&graph_lock);
3778
3779 return ret;
3780}
3781
3782static const struct file_operations ftrace_graph_fops = {
Li Zefan87827112009-07-23 11:29:11 +08003783 .open = ftrace_graph_open,
3784 .read = seq_read,
3785 .write = ftrace_graph_write,
Namhyung Kim6a76f8c2013-04-11 15:55:01 +09003786 .llseek = ftrace_filter_lseek,
Li Zefan87827112009-07-23 11:29:11 +08003787 .release = ftrace_graph_release,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003788};
3789#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3790
Steven Rostedtdf4fc312008-11-26 00:16:23 -05003791static __init int ftrace_init_dyn_debugfs(struct dentry *d_tracer)
Steven Rostedt5072c592008-05-12 21:20:43 +02003792{
Steven Rostedt5072c592008-05-12 21:20:43 +02003793
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003794 trace_create_file("available_filter_functions", 0444,
3795 d_tracer, NULL, &ftrace_avail_fops);
Steven Rostedt5072c592008-05-12 21:20:43 +02003796
Steven Rostedt647bcd02011-05-03 14:39:21 -04003797 trace_create_file("enabled_functions", 0444,
3798 d_tracer, NULL, &ftrace_enabled_fops);
3799
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003800 trace_create_file("set_ftrace_filter", 0644, d_tracer,
3801 NULL, &ftrace_filter_fops);
Steven Rostedt41c52c02008-05-22 11:46:33 -04003802
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003803 trace_create_file("set_ftrace_notrace", 0644, d_tracer,
Steven Rostedt41c52c02008-05-22 11:46:33 -04003804 NULL, &ftrace_notrace_fops);
Steven Rostedtad90c0e2008-05-27 20:48:37 -04003805
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003806#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbecker5452af62009-03-27 00:25:38 +01003807 trace_create_file("set_graph_function", 0444, d_tracer,
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003808 NULL,
3809 &ftrace_graph_fops);
Steven Rostedtea4e2bc2008-12-03 15:36:57 -05003810#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
3811
Steven Rostedt5072c592008-05-12 21:20:43 +02003812 return 0;
3813}
3814
Steven Rostedt9fd49322012-04-24 22:32:06 -04003815static int ftrace_cmp_ips(const void *a, const void *b)
Steven Rostedt68950612011-12-16 17:06:45 -05003816{
Steven Rostedt9fd49322012-04-24 22:32:06 -04003817 const unsigned long *ipa = a;
3818 const unsigned long *ipb = b;
Steven Rostedt68950612011-12-16 17:06:45 -05003819
Steven Rostedt9fd49322012-04-24 22:32:06 -04003820 if (*ipa > *ipb)
3821 return 1;
3822 if (*ipa < *ipb)
3823 return -1;
3824 return 0;
3825}
3826
3827static void ftrace_swap_ips(void *a, void *b, int size)
3828{
3829 unsigned long *ipa = a;
3830 unsigned long *ipb = b;
3831 unsigned long t;
3832
3833 t = *ipa;
3834 *ipa = *ipb;
3835 *ipb = t;
Steven Rostedt68950612011-12-16 17:06:45 -05003836}
3837
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003838static int ftrace_process_locs(struct module *mod,
Steven Rostedt31e88902008-11-14 16:21:19 -08003839 unsigned long *start,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003840 unsigned long *end)
3841{
Steven Rostedt706c81f2012-04-24 23:45:26 -04003842 struct ftrace_page *start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05003843 struct ftrace_page *pg;
Steven Rostedt706c81f2012-04-24 23:45:26 -04003844 struct dyn_ftrace *rec;
Steven Rostedta7900872011-12-16 16:23:44 -05003845 unsigned long count;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003846 unsigned long *p;
3847 unsigned long addr;
Steven Rostedt4376cac2011-06-24 23:28:13 -04003848 unsigned long flags = 0; /* Shut up gcc */
Steven Rostedta7900872011-12-16 16:23:44 -05003849 int ret = -ENOMEM;
3850
3851 count = end - start;
3852
3853 if (!count)
3854 return 0;
3855
Steven Rostedt9fd49322012-04-24 22:32:06 -04003856 sort(start, count, sizeof(*start),
3857 ftrace_cmp_ips, ftrace_swap_ips);
3858
Steven Rostedt706c81f2012-04-24 23:45:26 -04003859 start_pg = ftrace_allocate_pages(count);
3860 if (!start_pg)
Steven Rostedta7900872011-12-16 16:23:44 -05003861 return -ENOMEM;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003862
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003863 mutex_lock(&ftrace_lock);
Steven Rostedta7900872011-12-16 16:23:44 -05003864
Steven Rostedt32082302011-12-16 14:42:37 -05003865 /*
3866 * Core and each module needs their own pages, as
3867 * modules will free them when they are removed.
3868 * Force a new page to be allocated for modules.
3869 */
Steven Rostedta7900872011-12-16 16:23:44 -05003870 if (!mod) {
3871 WARN_ON(ftrace_pages || ftrace_pages_start);
3872 /* First initialization */
Steven Rostedt706c81f2012-04-24 23:45:26 -04003873 ftrace_pages = ftrace_pages_start = start_pg;
Steven Rostedta7900872011-12-16 16:23:44 -05003874 } else {
Steven Rostedt32082302011-12-16 14:42:37 -05003875 if (!ftrace_pages)
Steven Rostedta7900872011-12-16 16:23:44 -05003876 goto out;
Steven Rostedt32082302011-12-16 14:42:37 -05003877
Steven Rostedta7900872011-12-16 16:23:44 -05003878 if (WARN_ON(ftrace_pages->next)) {
3879 /* Hmm, we have free pages? */
3880 while (ftrace_pages->next)
3881 ftrace_pages = ftrace_pages->next;
Steven Rostedt32082302011-12-16 14:42:37 -05003882 }
Steven Rostedta7900872011-12-16 16:23:44 -05003883
Steven Rostedt706c81f2012-04-24 23:45:26 -04003884 ftrace_pages->next = start_pg;
Steven Rostedt32082302011-12-16 14:42:37 -05003885 }
3886
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003887 p = start;
Steven Rostedt706c81f2012-04-24 23:45:26 -04003888 pg = start_pg;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003889 while (p < end) {
3890 addr = ftrace_call_adjust(*p++);
Steven Rostedt20e52272008-11-14 16:21:19 -08003891 /*
3892 * Some architecture linkers will pad between
3893 * the different mcount_loc sections of different
3894 * object files to satisfy alignments.
3895 * Skip any NULL pointers.
3896 */
3897 if (!addr)
3898 continue;
Steven Rostedt706c81f2012-04-24 23:45:26 -04003899
3900 if (pg->index == pg->size) {
3901 /* We should have allocated enough */
3902 if (WARN_ON(!pg->next))
3903 break;
3904 pg = pg->next;
3905 }
3906
3907 rec = &pg->records[pg->index++];
3908 rec->ip = addr;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003909 }
3910
Steven Rostedt706c81f2012-04-24 23:45:26 -04003911 /* We should have used all pages */
3912 WARN_ON(pg->next);
3913
3914 /* Assign the last page to ftrace_pages */
3915 ftrace_pages = pg;
3916
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003917 /* These new locations need to be initialized */
Steven Rostedt706c81f2012-04-24 23:45:26 -04003918 ftrace_new_pgs = start_pg;
Steven Rostedt85ae32a2011-12-16 16:30:31 -05003919
Steven Rostedta4f18ed2011-06-07 09:26:46 -04003920 /*
Steven Rostedt4376cac2011-06-24 23:28:13 -04003921 * We only need to disable interrupts on start up
3922 * because we are modifying code that an interrupt
3923 * may execute, and the modification is not atomic.
3924 * But for modules, nothing runs the code we modify
3925 * until we are finished with it, and there's no
3926 * reason to cause large interrupt latencies while we do it.
Steven Rostedta4f18ed2011-06-07 09:26:46 -04003927 */
Steven Rostedt4376cac2011-06-24 23:28:13 -04003928 if (!mod)
3929 local_irq_save(flags);
Steven Rostedt31e88902008-11-14 16:21:19 -08003930 ftrace_update_code(mod);
Steven Rostedt4376cac2011-06-24 23:28:13 -04003931 if (!mod)
3932 local_irq_restore(flags);
Steven Rostedta7900872011-12-16 16:23:44 -05003933 ret = 0;
3934 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05003935 mutex_unlock(&ftrace_lock);
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003936
Steven Rostedta7900872011-12-16 16:23:44 -05003937 return ret;
Steven Rostedt68bf21a2008-08-14 15:45:08 -04003938}
3939
Steven Rostedt93eb6772009-04-15 13:24:06 -04003940#ifdef CONFIG_MODULES
Steven Rostedt32082302011-12-16 14:42:37 -05003941
3942#define next_to_ftrace_page(p) container_of(p, struct ftrace_page, next)
3943
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003944void ftrace_release_mod(struct module *mod)
Steven Rostedt93eb6772009-04-15 13:24:06 -04003945{
3946 struct dyn_ftrace *rec;
Steven Rostedt32082302011-12-16 14:42:37 -05003947 struct ftrace_page **last_pg;
Steven Rostedt93eb6772009-04-15 13:24:06 -04003948 struct ftrace_page *pg;
Steven Rostedta7900872011-12-16 16:23:44 -05003949 int order;
Steven Rostedt93eb6772009-04-15 13:24:06 -04003950
Steven Rostedt93eb6772009-04-15 13:24:06 -04003951 mutex_lock(&ftrace_lock);
Steven Rostedt45a4a232011-04-21 23:16:46 -04003952
3953 if (ftrace_disabled)
3954 goto out_unlock;
3955
Steven Rostedt32082302011-12-16 14:42:37 -05003956 /*
3957 * Each module has its own ftrace_pages, remove
3958 * them from the list.
3959 */
3960 last_pg = &ftrace_pages_start;
3961 for (pg = ftrace_pages_start; pg; pg = *last_pg) {
3962 rec = &pg->records[0];
jolsa@redhat.come7247a12009-10-07 19:00:35 +02003963 if (within_module_core(rec->ip, mod)) {
Steven Rostedt93eb6772009-04-15 13:24:06 -04003964 /*
Steven Rostedt32082302011-12-16 14:42:37 -05003965 * As core pages are first, the first
3966 * page should never be a module page.
Steven Rostedt93eb6772009-04-15 13:24:06 -04003967 */
Steven Rostedt32082302011-12-16 14:42:37 -05003968 if (WARN_ON(pg == ftrace_pages_start))
3969 goto out_unlock;
3970
3971 /* Check if we are deleting the last page */
3972 if (pg == ftrace_pages)
3973 ftrace_pages = next_to_ftrace_page(last_pg);
3974
3975 *last_pg = pg->next;
Steven Rostedta7900872011-12-16 16:23:44 -05003976 order = get_count_order(pg->size / ENTRIES_PER_PAGE);
3977 free_pages((unsigned long)pg->records, order);
3978 kfree(pg);
Steven Rostedt32082302011-12-16 14:42:37 -05003979 } else
3980 last_pg = &pg->next;
3981 }
Steven Rostedt45a4a232011-04-21 23:16:46 -04003982 out_unlock:
Steven Rostedt93eb6772009-04-15 13:24:06 -04003983 mutex_unlock(&ftrace_lock);
3984}
3985
3986static void ftrace_init_module(struct module *mod,
3987 unsigned long *start, unsigned long *end)
Steven Rostedt90d595f2008-08-14 15:45:09 -04003988{
Steven Rostedt00fd61a2008-08-15 21:40:04 -04003989 if (ftrace_disabled || start == end)
Steven Rostedtfed19392008-08-14 22:47:19 -04003990 return;
Jiri Olsa5cb084b2009-10-13 16:33:53 -04003991 ftrace_process_locs(mod, start, end);
Steven Rostedt90d595f2008-08-14 15:45:09 -04003992}
3993
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05003994static int ftrace_module_notify_enter(struct notifier_block *self,
3995 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04003996{
3997 struct module *mod = data;
3998
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05003999 if (val == MODULE_STATE_COMING)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004000 ftrace_init_module(mod, mod->ftrace_callsites,
4001 mod->ftrace_callsites +
4002 mod->num_ftrace_callsites);
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004003 return 0;
4004}
4005
4006static int ftrace_module_notify_exit(struct notifier_block *self,
4007 unsigned long val, void *data)
4008{
4009 struct module *mod = data;
4010
4011 if (val == MODULE_STATE_GOING)
jolsa@redhat.come7247a12009-10-07 19:00:35 +02004012 ftrace_release_mod(mod);
Steven Rostedt93eb6772009-04-15 13:24:06 -04004013
4014 return 0;
4015}
4016#else
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004017static int ftrace_module_notify_enter(struct notifier_block *self,
4018 unsigned long val, void *data)
4019{
4020 return 0;
4021}
4022static int ftrace_module_notify_exit(struct notifier_block *self,
4023 unsigned long val, void *data)
Steven Rostedt93eb6772009-04-15 13:24:06 -04004024{
4025 return 0;
4026}
4027#endif /* CONFIG_MODULES */
4028
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004029struct notifier_block ftrace_module_enter_nb = {
4030 .notifier_call = ftrace_module_notify_enter,
Steven Rostedtc1bf08a2012-12-14 09:48:15 -05004031 .priority = INT_MAX, /* Run before anything that can use kprobes */
Steven Rostedt93eb6772009-04-15 13:24:06 -04004032};
4033
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004034struct notifier_block ftrace_module_exit_nb = {
4035 .notifier_call = ftrace_module_notify_exit,
4036 .priority = INT_MIN, /* Run after anything that can remove kprobes */
4037};
4038
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004039extern unsigned long __start_mcount_loc[];
4040extern unsigned long __stop_mcount_loc[];
4041
4042void __init ftrace_init(void)
4043{
4044 unsigned long count, addr, flags;
4045 int ret;
4046
4047 /* Keep the ftrace pointer to the stub */
4048 addr = (unsigned long)ftrace_stub;
4049
4050 local_irq_save(flags);
4051 ftrace_dyn_arch_init(&addr);
4052 local_irq_restore(flags);
4053
4054 /* ftrace_dyn_arch_init places the return code in addr */
4055 if (addr)
4056 goto failed;
4057
4058 count = __stop_mcount_loc - __start_mcount_loc;
4059
4060 ret = ftrace_dyn_table_alloc(count);
4061 if (ret)
4062 goto failed;
4063
4064 last_ftrace_enabled = ftrace_enabled = 1;
4065
Jiri Olsa5cb084b2009-10-13 16:33:53 -04004066 ret = ftrace_process_locs(NULL,
Steven Rostedt31e88902008-11-14 16:21:19 -08004067 __start_mcount_loc,
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004068 __stop_mcount_loc);
4069
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004070 ret = register_module_notifier(&ftrace_module_enter_nb);
Ming Lei24ed0c42009-05-17 15:31:38 +08004071 if (ret)
Steven Rostedt (Red Hat)8c189ea2013-02-13 15:18:38 -05004072 pr_warning("Failed to register trace ftrace module enter notifier\n");
4073
4074 ret = register_module_notifier(&ftrace_module_exit_nb);
4075 if (ret)
4076 pr_warning("Failed to register trace ftrace module exit notifier\n");
Steven Rostedt93eb6772009-04-15 13:24:06 -04004077
Steven Rostedt2af15d62009-05-28 13:37:24 -04004078 set_ftrace_early_filters();
4079
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004080 return;
4081 failed:
4082 ftrace_disabled = 1;
4083}
Steven Rostedt68bf21a2008-08-14 15:45:08 -04004084
Steven Rostedt3d083392008-05-12 21:20:42 +02004085#else
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004086
Steven Rostedt2b499382011-05-03 22:49:52 -04004087static struct ftrace_ops global_ops = {
Steven Rostedtbd69c302011-05-03 21:55:54 -04004088 .func = ftrace_stub,
Steven Rostedt47409742012-07-20 11:04:44 -04004089 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Steven Rostedtbd69c302011-05-03 21:55:54 -04004090};
4091
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004092static int __init ftrace_nodyn_init(void)
4093{
4094 ftrace_enabled = 1;
4095 return 0;
4096}
Steven Rostedt6f415672012-10-05 12:13:07 -04004097core_initcall(ftrace_nodyn_init);
Frederic Weisbecker0b6e4d52008-10-28 20:17:38 +01004098
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004099static inline int ftrace_init_dyn_debugfs(struct dentry *d_tracer) { return 0; }
4100static inline void ftrace_startup_enable(int command) { }
Steven Rostedt5a45cfe2008-11-26 00:16:24 -05004101/* Keep as macros so we do not need to define the commands */
Steven Rostedt3b6cfdb2011-05-23 15:33:49 -04004102# define ftrace_startup(ops, command) \
4103 ({ \
4104 (ops)->flags |= FTRACE_OPS_FL_ENABLED; \
4105 0; \
4106 })
Steven Rostedtbd69c302011-05-03 21:55:54 -04004107# define ftrace_shutdown(ops, command) do { } while (0)
Ingo Molnarc7aafc52008-05-12 21:20:45 +02004108# define ftrace_startup_sysctl() do { } while (0)
4109# define ftrace_shutdown_sysctl() do { } while (0)
Steven Rostedtb8489142011-05-04 09:27:52 -04004110
4111static inline int
4112ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip)
4113{
4114 return 1;
4115}
4116
Steven Rostedt3d083392008-05-12 21:20:42 +02004117#endif /* CONFIG_DYNAMIC_FTRACE */
4118
Steven Rostedtb8489142011-05-04 09:27:52 -04004119static void
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004120ftrace_ops_control_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004121 struct ftrace_ops *op, struct pt_regs *regs)
Jiri Olsae2484912012-02-15 15:51:48 +01004122{
Jiri Olsae2484912012-02-15 15:51:48 +01004123 if (unlikely(trace_recursion_test(TRACE_CONTROL_BIT)))
4124 return;
4125
4126 /*
4127 * Some of the ops may be dynamically allocated,
4128 * they must be freed after a synchronize_sched().
4129 */
4130 preempt_disable_notrace();
4131 trace_recursion_set(TRACE_CONTROL_BIT);
Steven Rostedt0a016402012-11-02 17:03:03 -04004132 do_for_each_ftrace_op(op, ftrace_control_list) {
Steven Rostedt (Red Hat)395b97a2013-03-27 09:31:28 -04004133 if (!(op->flags & FTRACE_OPS_FL_STUB) &&
4134 !ftrace_function_local_disabled(op) &&
Jiri Olsae2484912012-02-15 15:51:48 +01004135 ftrace_ops_test(op, ip))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004136 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04004137 } while_for_each_ftrace_op(op);
Jiri Olsae2484912012-02-15 15:51:48 +01004138 trace_recursion_clear(TRACE_CONTROL_BIT);
4139 preempt_enable_notrace();
4140}
4141
4142static struct ftrace_ops control_ops = {
4143 .func = ftrace_ops_control_func,
Steven Rostedt47409742012-07-20 11:04:44 -04004144 .flags = FTRACE_OPS_FL_RECURSION_SAFE,
Jiri Olsae2484912012-02-15 15:51:48 +01004145};
4146
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004147static inline void
4148__ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004149 struct ftrace_ops *ignored, struct pt_regs *regs)
Steven Rostedtb8489142011-05-04 09:27:52 -04004150{
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004151 struct ftrace_ops *op;
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004152 int bit;
Steven Rostedtb8489142011-05-04 09:27:52 -04004153
Steven Rostedtccf36722012-06-05 09:44:25 -04004154 if (function_trace_stop)
4155 return;
4156
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004157 bit = trace_test_and_set_recursion(TRACE_LIST_START, TRACE_LIST_MAX);
4158 if (bit < 0)
4159 return;
Steven Rostedtc29f1222012-11-02 17:17:59 -04004160
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004161 /*
4162 * Some of the ops may be dynamically allocated,
4163 * they must be freed after a synchronize_sched().
4164 */
4165 preempt_disable_notrace();
Steven Rostedt0a016402012-11-02 17:03:03 -04004166 do_for_each_ftrace_op(op, ftrace_ops_list) {
Steven Rostedtb8489142011-05-04 09:27:52 -04004167 if (ftrace_ops_test(op, ip))
Steven Rostedta1e2e312011-08-09 12:50:46 -04004168 op->func(ip, parent_ip, op, regs);
Steven Rostedt0a016402012-11-02 17:03:03 -04004169 } while_for_each_ftrace_op(op);
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004170 preempt_enable_notrace();
Steven Rostedtedc15ca2012-11-02 17:47:21 -04004171 trace_clear_recursion(bit);
Steven Rostedtb8489142011-05-04 09:27:52 -04004172}
4173
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004174/*
4175 * Some archs only support passing ip and parent_ip. Even though
4176 * the list function ignores the op parameter, we do not want any
4177 * C side effects, where a function is called without the caller
4178 * sending a third parameter.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004179 * Archs are to support both the regs and ftrace_ops at the same time.
4180 * If they support ftrace_ops, it is assumed they support regs.
4181 * If call backs want to use regs, they must either check for regs
Masami Hiramatsu06aeaae2012-09-28 17:15:17 +09004182 * being NULL, or CONFIG_DYNAMIC_FTRACE_WITH_REGS.
4183 * Note, CONFIG_DYNAMIC_FTRACE_WITH_REGS expects a full regs to be saved.
Steven Rostedta1e2e312011-08-09 12:50:46 -04004184 * An architecture can pass partial regs with ftrace_ops and still
4185 * set the ARCH_SUPPORT_FTARCE_OPS.
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004186 */
4187#if ARCH_SUPPORTS_FTRACE_OPS
4188static void ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
Steven Rostedta1e2e312011-08-09 12:50:46 -04004189 struct ftrace_ops *op, struct pt_regs *regs)
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004190{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004191 __ftrace_ops_list_func(ip, parent_ip, NULL, regs);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004192}
4193#else
4194static void ftrace_ops_no_ops(unsigned long ip, unsigned long parent_ip)
4195{
Steven Rostedta1e2e312011-08-09 12:50:46 -04004196 __ftrace_ops_list_func(ip, parent_ip, NULL, NULL);
Steven Rostedt2f5f6ad2011-08-08 16:57:47 -04004197}
4198#endif
4199
Steven Rostedte32d8952008-12-04 00:26:41 -05004200static void clear_ftrace_swapper(void)
4201{
4202 struct task_struct *p;
4203 int cpu;
4204
4205 get_online_cpus();
4206 for_each_online_cpu(cpu) {
4207 p = idle_task(cpu);
4208 clear_tsk_trace_trace(p);
4209 }
4210 put_online_cpus();
4211}
4212
4213static void set_ftrace_swapper(void)
4214{
4215 struct task_struct *p;
4216 int cpu;
4217
4218 get_online_cpus();
4219 for_each_online_cpu(cpu) {
4220 p = idle_task(cpu);
4221 set_tsk_trace_trace(p);
4222 }
4223 put_online_cpus();
4224}
4225
4226static void clear_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004227{
4228 struct task_struct *p;
4229
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004230 rcu_read_lock();
Steven Rostedte32d8952008-12-04 00:26:41 -05004231 do_each_pid_task(pid, PIDTYPE_PID, p) {
Steven Rostedt978f3a42008-12-04 00:26:40 -05004232 clear_tsk_trace_trace(p);
Steven Rostedte32d8952008-12-04 00:26:41 -05004233 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004234 rcu_read_unlock();
4235
Steven Rostedte32d8952008-12-04 00:26:41 -05004236 put_pid(pid);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004237}
4238
Steven Rostedte32d8952008-12-04 00:26:41 -05004239static void set_ftrace_pid(struct pid *pid)
Steven Rostedt978f3a42008-12-04 00:26:40 -05004240{
4241 struct task_struct *p;
4242
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004243 rcu_read_lock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004244 do_each_pid_task(pid, PIDTYPE_PID, p) {
4245 set_tsk_trace_trace(p);
4246 } while_each_pid_task(pid, PIDTYPE_PID, p);
Oleg Nesterov229c4ef2009-02-03 20:39:04 +01004247 rcu_read_unlock();
Steven Rostedt978f3a42008-12-04 00:26:40 -05004248}
4249
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004250static void clear_ftrace_pid_task(struct pid *pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004251{
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004252 if (pid == ftrace_swapper_pid)
Steven Rostedte32d8952008-12-04 00:26:41 -05004253 clear_ftrace_swapper();
4254 else
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004255 clear_ftrace_pid(pid);
Steven Rostedte32d8952008-12-04 00:26:41 -05004256}
4257
4258static void set_ftrace_pid_task(struct pid *pid)
4259{
4260 if (pid == ftrace_swapper_pid)
4261 set_ftrace_swapper();
4262 else
4263 set_ftrace_pid(pid);
4264}
4265
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004266static int ftrace_pid_add(int p)
4267{
4268 struct pid *pid;
4269 struct ftrace_pid *fpid;
4270 int ret = -EINVAL;
4271
4272 mutex_lock(&ftrace_lock);
4273
4274 if (!p)
4275 pid = ftrace_swapper_pid;
4276 else
4277 pid = find_get_pid(p);
4278
4279 if (!pid)
4280 goto out;
4281
4282 ret = 0;
4283
4284 list_for_each_entry(fpid, &ftrace_pids, list)
4285 if (fpid->pid == pid)
4286 goto out_put;
4287
4288 ret = -ENOMEM;
4289
4290 fpid = kmalloc(sizeof(*fpid), GFP_KERNEL);
4291 if (!fpid)
4292 goto out_put;
4293
4294 list_add(&fpid->list, &ftrace_pids);
4295 fpid->pid = pid;
4296
4297 set_ftrace_pid_task(pid);
4298
4299 ftrace_update_pid_func();
4300 ftrace_startup_enable(0);
4301
4302 mutex_unlock(&ftrace_lock);
4303 return 0;
4304
4305out_put:
4306 if (pid != ftrace_swapper_pid)
4307 put_pid(pid);
4308
4309out:
4310 mutex_unlock(&ftrace_lock);
4311 return ret;
4312}
4313
4314static void ftrace_pid_reset(void)
4315{
4316 struct ftrace_pid *fpid, *safe;
4317
4318 mutex_lock(&ftrace_lock);
4319 list_for_each_entry_safe(fpid, safe, &ftrace_pids, list) {
4320 struct pid *pid = fpid->pid;
4321
4322 clear_ftrace_pid_task(pid);
4323
4324 list_del(&fpid->list);
4325 kfree(fpid);
4326 }
4327
4328 ftrace_update_pid_func();
4329 ftrace_startup_enable(0);
4330
4331 mutex_unlock(&ftrace_lock);
4332}
4333
4334static void *fpid_start(struct seq_file *m, loff_t *pos)
4335{
4336 mutex_lock(&ftrace_lock);
4337
4338 if (list_empty(&ftrace_pids) && (!*pos))
4339 return (void *) 1;
4340
4341 return seq_list_start(&ftrace_pids, *pos);
4342}
4343
4344static void *fpid_next(struct seq_file *m, void *v, loff_t *pos)
4345{
4346 if (v == (void *)1)
4347 return NULL;
4348
4349 return seq_list_next(v, &ftrace_pids, pos);
4350}
4351
4352static void fpid_stop(struct seq_file *m, void *p)
4353{
4354 mutex_unlock(&ftrace_lock);
4355}
4356
4357static int fpid_show(struct seq_file *m, void *v)
4358{
4359 const struct ftrace_pid *fpid = list_entry(v, struct ftrace_pid, list);
4360
4361 if (v == (void *)1) {
4362 seq_printf(m, "no pid\n");
4363 return 0;
4364 }
4365
4366 if (fpid->pid == ftrace_swapper_pid)
4367 seq_printf(m, "swapper tasks\n");
4368 else
4369 seq_printf(m, "%u\n", pid_vnr(fpid->pid));
4370
4371 return 0;
4372}
4373
4374static const struct seq_operations ftrace_pid_sops = {
4375 .start = fpid_start,
4376 .next = fpid_next,
4377 .stop = fpid_stop,
4378 .show = fpid_show,
4379};
4380
4381static int
4382ftrace_pid_open(struct inode *inode, struct file *file)
4383{
4384 int ret = 0;
4385
4386 if ((file->f_mode & FMODE_WRITE) &&
4387 (file->f_flags & O_TRUNC))
4388 ftrace_pid_reset();
4389
4390 if (file->f_mode & FMODE_READ)
4391 ret = seq_open(file, &ftrace_pid_sops);
4392
4393 return ret;
4394}
4395
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004396static ssize_t
4397ftrace_pid_write(struct file *filp, const char __user *ubuf,
4398 size_t cnt, loff_t *ppos)
4399{
Ingo Molnar457dc922009-11-23 11:03:28 +01004400 char buf[64], *tmp;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004401 long val;
4402 int ret;
4403
4404 if (cnt >= sizeof(buf))
4405 return -EINVAL;
4406
4407 if (copy_from_user(&buf, ubuf, cnt))
4408 return -EFAULT;
4409
4410 buf[cnt] = 0;
4411
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004412 /*
4413 * Allow "echo > set_ftrace_pid" or "echo -n '' > set_ftrace_pid"
4414 * to clean the filter quietly.
4415 */
Ingo Molnar457dc922009-11-23 11:03:28 +01004416 tmp = strstrip(buf);
4417 if (strlen(tmp) == 0)
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004418 return 1;
4419
Daniel Walterbcd83ea2012-09-26 22:08:38 +02004420 ret = kstrtol(tmp, 10, &val);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004421 if (ret < 0)
4422 return ret;
4423
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004424 ret = ftrace_pid_add(val);
Steven Rostedt978f3a42008-12-04 00:26:40 -05004425
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004426 return ret ? ret : cnt;
4427}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004428
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004429static int
4430ftrace_pid_release(struct inode *inode, struct file *file)
4431{
4432 if (file->f_mode & FMODE_READ)
4433 seq_release(inode, file);
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004434
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004435 return 0;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004436}
4437
Steven Rostedt5e2336a2009-03-05 21:44:55 -05004438static const struct file_operations ftrace_pid_fops = {
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004439 .open = ftrace_pid_open,
4440 .write = ftrace_pid_write,
4441 .read = seq_read,
Namhyung Kim6a76f8c2013-04-11 15:55:01 +09004442 .llseek = ftrace_filter_lseek,
jolsa@redhat.com756d17e2009-10-13 16:33:52 -04004443 .release = ftrace_pid_release,
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004444};
4445
4446static __init int ftrace_init_debugfs(void)
4447{
4448 struct dentry *d_tracer;
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004449
4450 d_tracer = tracing_init_dentry();
4451 if (!d_tracer)
4452 return 0;
4453
4454 ftrace_init_dyn_debugfs(d_tracer);
4455
Frederic Weisbecker5452af62009-03-27 00:25:38 +01004456 trace_create_file("set_ftrace_pid", 0644, d_tracer,
4457 NULL, &ftrace_pid_fops);
Steven Rostedt493762f2009-03-23 17:12:36 -04004458
4459 ftrace_profile_debugfs(d_tracer);
4460
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004461 return 0;
4462}
Steven Rostedtdf4fc312008-11-26 00:16:23 -05004463fs_initcall(ftrace_init_debugfs);
4464
Steven Rostedt3d083392008-05-12 21:20:42 +02004465/**
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004466 * ftrace_kill - kill ftrace
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004467 *
4468 * This function should be used by panic code. It stops ftrace
4469 * but in a not so nice way. If you need to simply kill ftrace
4470 * from a non-atomic section, use ftrace_kill.
4471 */
Steven Rostedt81adbdc2008-10-23 09:33:02 -04004472void ftrace_kill(void)
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004473{
4474 ftrace_disabled = 1;
4475 ftrace_enabled = 0;
Steven Rostedta2bb6a32008-07-10 20:58:15 -04004476 clear_ftrace_function();
4477}
4478
4479/**
Steven Rostedte0a413f2011-09-29 21:26:16 -04004480 * Test if ftrace is dead or not.
4481 */
4482int ftrace_is_dead(void)
4483{
4484 return ftrace_disabled;
4485}
4486
4487/**
Steven Rostedt3d083392008-05-12 21:20:42 +02004488 * register_ftrace_function - register a function for profiling
4489 * @ops - ops structure that holds the function for profiling.
4490 *
4491 * Register a function to be called by all functions in the
4492 * kernel.
4493 *
4494 * Note: @ops->func and all the functions it calls must be labeled
4495 * with "notrace", otherwise it will go into a
4496 * recursive loop.
4497 */
4498int register_ftrace_function(struct ftrace_ops *ops)
4499{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004500 int ret = -1;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004501
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004502 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004503
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004504 ret = __register_ftrace_function(ops);
Steven Rostedtb8489142011-05-04 09:27:52 -04004505 if (!ret)
Steven Rostedta1cd6172011-05-23 15:24:25 -04004506 ret = ftrace_startup(ops, 0);
Steven Rostedtb8489142011-05-04 09:27:52 -04004507
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004508 mutex_unlock(&ftrace_lock);
Borislav Petkov8d240dd2012-03-29 19:11:40 +02004509
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004510 return ret;
Steven Rostedt3d083392008-05-12 21:20:42 +02004511}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004512EXPORT_SYMBOL_GPL(register_ftrace_function);
Steven Rostedt3d083392008-05-12 21:20:42 +02004513
4514/**
Uwe Kleine-Koenig32632922009-01-12 23:35:50 +01004515 * unregister_ftrace_function - unregister a function for profiling.
Steven Rostedt3d083392008-05-12 21:20:42 +02004516 * @ops - ops structure that holds the function to unregister
4517 *
4518 * Unregister a function that was added to be called by ftrace profiling.
4519 */
4520int unregister_ftrace_function(struct ftrace_ops *ops)
4521{
4522 int ret;
4523
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004524 mutex_lock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004525 ret = __unregister_ftrace_function(ops);
Steven Rostedtb8489142011-05-04 09:27:52 -04004526 if (!ret)
4527 ftrace_shutdown(ops, 0);
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004528 mutex_unlock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004529
4530 return ret;
4531}
Steven Rostedtcdbe61b2011-05-05 21:14:55 -04004532EXPORT_SYMBOL_GPL(unregister_ftrace_function);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004533
Ingo Molnare309b412008-05-12 21:20:51 +02004534int
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004535ftrace_enable_sysctl(struct ctl_table *table, int write,
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004536 void __user *buffer, size_t *lenp,
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004537 loff_t *ppos)
4538{
Steven Rostedt45a4a232011-04-21 23:16:46 -04004539 int ret = -ENODEV;
Steven Rostedt4eebcc82008-05-12 21:20:48 +02004540
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004541 mutex_lock(&ftrace_lock);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004542
Steven Rostedt45a4a232011-04-21 23:16:46 -04004543 if (unlikely(ftrace_disabled))
4544 goto out;
4545
4546 ret = proc_dointvec(table, write, buffer, lenp, ppos);
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004547
Li Zefana32c7762009-06-26 16:55:51 +08004548 if (ret || !write || (last_ftrace_enabled == !!ftrace_enabled))
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004549 goto out;
4550
Li Zefana32c7762009-06-26 16:55:51 +08004551 last_ftrace_enabled = !!ftrace_enabled;
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004552
4553 if (ftrace_enabled) {
4554
4555 ftrace_startup_sysctl();
4556
4557 /* we are starting ftrace again */
Jan Kiszka5000c412013-03-26 17:53:03 +01004558 if (ftrace_ops_list != &ftrace_list_end)
4559 update_ftrace_function();
Steven Rostedtb0fc4942008-05-12 21:20:43 +02004560
4561 } else {
4562 /* stopping ftrace calls (just send to ftrace_stub) */
4563 ftrace_trace_function = ftrace_stub;
4564
4565 ftrace_shutdown_sysctl();
4566 }
4567
4568 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004569 mutex_unlock(&ftrace_lock);
Steven Rostedt3d083392008-05-12 21:20:42 +02004570 return ret;
Arnaldo Carvalho de Melo16444a82008-05-12 21:20:42 +02004571}
Ingo Molnarf17845e2008-10-24 12:47:10 +02004572
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004573#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004574
Steven Rostedt597af812009-04-03 15:24:12 -04004575static int ftrace_graph_active;
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004576static struct notifier_block ftrace_suspend_notifier;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004577
Steven Rostedte49dc192008-12-02 23:50:05 -05004578int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace)
4579{
4580 return 0;
4581}
4582
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004583/* The callbacks that hook a function */
4584trace_func_graph_ret_t ftrace_graph_return =
4585 (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004586trace_func_graph_ent_t ftrace_graph_entry = ftrace_graph_entry_stub;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004587
4588/* Try to assign a return stack array on FTRACE_RETSTACK_ALLOC_SIZE tasks. */
4589static int alloc_retstack_tasklist(struct ftrace_ret_stack **ret_stack_list)
4590{
4591 int i;
4592 int ret = 0;
4593 unsigned long flags;
4594 int start = 0, end = FTRACE_RETSTACK_ALLOC_SIZE;
4595 struct task_struct *g, *t;
4596
4597 for (i = 0; i < FTRACE_RETSTACK_ALLOC_SIZE; i++) {
4598 ret_stack_list[i] = kmalloc(FTRACE_RETFUNC_DEPTH
4599 * sizeof(struct ftrace_ret_stack),
4600 GFP_KERNEL);
4601 if (!ret_stack_list[i]) {
4602 start = 0;
4603 end = i;
4604 ret = -ENOMEM;
4605 goto free;
4606 }
4607 }
4608
4609 read_lock_irqsave(&tasklist_lock, flags);
4610 do_each_thread(g, t) {
4611 if (start == end) {
4612 ret = -EAGAIN;
4613 goto unlock;
4614 }
4615
4616 if (t->ret_stack == NULL) {
Frederic Weisbecker380c4b12008-12-06 03:43:41 +01004617 atomic_set(&t->tracing_graph_pause, 0);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004618 atomic_set(&t->trace_overrun, 0);
Steven Rostedt26c01622009-06-02 14:01:19 -04004619 t->curr_ret_stack = -1;
4620 /* Make sure the tasks see the -1 first: */
4621 smp_wmb();
4622 t->ret_stack = ret_stack_list[start++];
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004623 }
4624 } while_each_thread(g, t);
4625
4626unlock:
4627 read_unlock_irqrestore(&tasklist_lock, flags);
4628free:
4629 for (i = start; i < end; i++)
4630 kfree(ret_stack_list[i]);
4631 return ret;
4632}
4633
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004634static void
Steven Rostedt38516ab2010-04-20 17:04:50 -04004635ftrace_graph_probe_sched_switch(void *ignore,
4636 struct task_struct *prev, struct task_struct *next)
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004637{
4638 unsigned long long timestamp;
4639 int index;
4640
Steven Rostedtbe6f1642009-03-24 11:06:24 -04004641 /*
4642 * Does the user want to count the time a function was asleep.
4643 * If so, do not update the time stamps.
4644 */
4645 if (trace_flags & TRACE_ITER_SLEEP_TIME)
4646 return;
4647
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004648 timestamp = trace_clock_local();
4649
4650 prev->ftrace_timestamp = timestamp;
4651
4652 /* only process tasks that we timestamped */
4653 if (!next->ftrace_timestamp)
4654 return;
4655
4656 /*
4657 * Update all the counters in next to make up for the
4658 * time next was sleeping.
4659 */
4660 timestamp -= next->ftrace_timestamp;
4661
4662 for (index = next->curr_ret_stack; index >= 0; index--)
4663 next->ret_stack[index].calltime += timestamp;
4664}
4665
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004666/* Allocate a return stack for each task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004667static int start_graph_tracing(void)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004668{
4669 struct ftrace_ret_stack **ret_stack_list;
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004670 int ret, cpu;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004671
4672 ret_stack_list = kmalloc(FTRACE_RETSTACK_ALLOC_SIZE *
4673 sizeof(struct ftrace_ret_stack *),
4674 GFP_KERNEL);
4675
4676 if (!ret_stack_list)
4677 return -ENOMEM;
4678
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004679 /* The cpu_boot init_task->ret_stack will never be freed */
Steven Rostedt179c4982009-06-02 12:03:19 -04004680 for_each_online_cpu(cpu) {
4681 if (!idle_task(cpu)->ret_stack)
Steven Rostedt868baf02011-02-10 21:26:13 -05004682 ftrace_graph_init_idle_task(idle_task(cpu), cpu);
Steven Rostedt179c4982009-06-02 12:03:19 -04004683 }
Frederic Weisbecker5b058bc2009-02-17 18:35:34 +01004684
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004685 do {
4686 ret = alloc_retstack_tasklist(ret_stack_list);
4687 } while (ret == -EAGAIN);
4688
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004689 if (!ret) {
Steven Rostedt38516ab2010-04-20 17:04:50 -04004690 ret = register_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Steven Rostedt8aef2d22009-03-24 01:10:15 -04004691 if (ret)
4692 pr_info("ftrace_graph: Couldn't activate tracepoint"
4693 " probe to kernel_sched_switch\n");
4694 }
4695
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004696 kfree(ret_stack_list);
4697 return ret;
4698}
4699
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004700/*
4701 * Hibernation protection.
4702 * The state of the current task is too much unstable during
4703 * suspend/restore to disk. We want to protect against that.
4704 */
4705static int
4706ftrace_suspend_notifier_call(struct notifier_block *bl, unsigned long state,
4707 void *unused)
4708{
4709 switch (state) {
4710 case PM_HIBERNATION_PREPARE:
4711 pause_graph_tracing();
4712 break;
4713
4714 case PM_POST_HIBERNATION:
4715 unpause_graph_tracing();
4716 break;
4717 }
4718 return NOTIFY_DONE;
4719}
4720
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004721int register_ftrace_graph(trace_func_graph_ret_t retfunc,
4722 trace_func_graph_ent_t entryfunc)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004723{
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004724 int ret = 0;
4725
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004726 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004727
Steven Rostedt05ce5812009-03-24 00:18:31 -04004728 /* we currently allow only one tracer registered at a time */
Steven Rostedt597af812009-04-03 15:24:12 -04004729 if (ftrace_graph_active) {
Steven Rostedt05ce5812009-03-24 00:18:31 -04004730 ret = -EBUSY;
4731 goto out;
4732 }
4733
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004734 ftrace_suspend_notifier.notifier_call = ftrace_suspend_notifier_call;
4735 register_pm_notifier(&ftrace_suspend_notifier);
4736
Steven Rostedt597af812009-04-03 15:24:12 -04004737 ftrace_graph_active++;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004738 ret = start_graph_tracing();
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004739 if (ret) {
Steven Rostedt597af812009-04-03 15:24:12 -04004740 ftrace_graph_active--;
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004741 goto out;
4742 }
Steven Rostedte53a6312008-11-26 00:16:25 -05004743
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004744 ftrace_graph_return = retfunc;
4745 ftrace_graph_entry = entryfunc;
Steven Rostedte53a6312008-11-26 00:16:25 -05004746
Steven Rostedta1cd6172011-05-23 15:24:25 -04004747 ret = ftrace_startup(&global_ops, FTRACE_START_FUNC_RET);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004748
4749out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004750 mutex_unlock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004751 return ret;
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004752}
4753
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004754void unregister_ftrace_graph(void)
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004755{
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004756 mutex_lock(&ftrace_lock);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004757
Steven Rostedt597af812009-04-03 15:24:12 -04004758 if (unlikely(!ftrace_graph_active))
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004759 goto out;
4760
Steven Rostedt597af812009-04-03 15:24:12 -04004761 ftrace_graph_active--;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01004762 ftrace_graph_return = (trace_func_graph_ret_t)ftrace_stub;
Steven Rostedte49dc192008-12-02 23:50:05 -05004763 ftrace_graph_entry = ftrace_graph_entry_stub;
Steven Rostedtbd69c302011-05-03 21:55:54 -04004764 ftrace_shutdown(&global_ops, FTRACE_STOP_FUNC_RET);
Frederic Weisbecker4a2b8dd2009-01-14 13:33:27 -08004765 unregister_pm_notifier(&ftrace_suspend_notifier);
Steven Rostedt38516ab2010-04-20 17:04:50 -04004766 unregister_trace_sched_switch(ftrace_graph_probe_sched_switch, NULL);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +01004767
Steven Rostedt2aad1b72009-03-30 11:11:28 -04004768 out:
Steven Rostedte6ea44e2009-02-14 01:42:44 -05004769 mutex_unlock(&ftrace_lock);
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004770}
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004771
Steven Rostedt868baf02011-02-10 21:26:13 -05004772static DEFINE_PER_CPU(struct ftrace_ret_stack *, idle_ret_stack);
4773
4774static void
4775graph_init_task(struct task_struct *t, struct ftrace_ret_stack *ret_stack)
4776{
4777 atomic_set(&t->tracing_graph_pause, 0);
4778 atomic_set(&t->trace_overrun, 0);
4779 t->ftrace_timestamp = 0;
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004780 /* make curr_ret_stack visible before we add the ret_stack */
Steven Rostedt868baf02011-02-10 21:26:13 -05004781 smp_wmb();
4782 t->ret_stack = ret_stack;
4783}
4784
4785/*
4786 * Allocate a return stack for the idle task. May be the first
4787 * time through, or it may be done by CPU hotplug online.
4788 */
4789void ftrace_graph_init_idle_task(struct task_struct *t, int cpu)
4790{
4791 t->curr_ret_stack = -1;
4792 /*
4793 * The idle task has no parent, it either has its own
4794 * stack or no stack at all.
4795 */
4796 if (t->ret_stack)
4797 WARN_ON(t->ret_stack != per_cpu(idle_ret_stack, cpu));
4798
4799 if (ftrace_graph_active) {
4800 struct ftrace_ret_stack *ret_stack;
4801
4802 ret_stack = per_cpu(idle_ret_stack, cpu);
4803 if (!ret_stack) {
4804 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
4805 * sizeof(struct ftrace_ret_stack),
4806 GFP_KERNEL);
4807 if (!ret_stack)
4808 return;
4809 per_cpu(idle_ret_stack, cpu) = ret_stack;
4810 }
4811 graph_init_task(t, ret_stack);
4812 }
4813}
4814
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004815/* Allocate a return stack for newly created task */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004816void ftrace_graph_init_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004817{
Steven Rostedt84047e32009-06-02 16:51:55 -04004818 /* Make sure we do not use the parent ret_stack */
4819 t->ret_stack = NULL;
Steven Rostedtea14eb72010-03-12 19:41:23 -05004820 t->curr_ret_stack = -1;
Steven Rostedt84047e32009-06-02 16:51:55 -04004821
Steven Rostedt597af812009-04-03 15:24:12 -04004822 if (ftrace_graph_active) {
Steven Rostedt82310a32009-06-02 12:26:07 -04004823 struct ftrace_ret_stack *ret_stack;
4824
4825 ret_stack = kmalloc(FTRACE_RETFUNC_DEPTH
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004826 * sizeof(struct ftrace_ret_stack),
4827 GFP_KERNEL);
Steven Rostedt82310a32009-06-02 12:26:07 -04004828 if (!ret_stack)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004829 return;
Steven Rostedt868baf02011-02-10 21:26:13 -05004830 graph_init_task(t, ret_stack);
Steven Rostedt84047e32009-06-02 16:51:55 -04004831 }
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004832}
4833
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01004834void ftrace_graph_exit_task(struct task_struct *t)
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004835{
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004836 struct ftrace_ret_stack *ret_stack = t->ret_stack;
4837
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004838 t->ret_stack = NULL;
Frederic Weisbeckereae849c2008-11-23 17:33:12 +01004839 /* NULL must become visible to IRQs before we free it: */
4840 barrier();
4841
4842 kfree(ret_stack);
Frederic Weisbeckerf201ae22008-11-23 06:22:56 +01004843}
Steven Rostedt14a866c2008-12-02 23:50:02 -05004844
4845void ftrace_graph_stop(void)
4846{
4847 ftrace_stop();
4848}
Frederic Weisbecker15e6cb32008-11-11 07:14:25 +01004849#endif